xref: /titanic_41/usr/src/cmd/geniconvtbl/hash.h (revision 8d483882aa3390058094b043f3d62187b5d1de03)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 1999 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #ifndef	_ICONV_TM_HASH_H
28 #define	_ICONV_TM_HASH_H
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 
33 #ifdef	__cplusplus
34 extern "C" {
35 #endif
36 
37 
38 
39 static itm_size_t	hash(const char *, itm_size_t, itm_size_t);
40 static itm_num_t	hash_dense_encoding(const unsigned char *, itm_size_t,
41 				    const unsigned char *,
42 				    const unsigned char *);
43 
44 
45 static itm_size_t
46 hash(const char *ptr, itm_size_t size, itm_size_t hash_size)
47 {
48 	itm_size_t	value;
49 
50 	value = *(ptr++);
51 	--size;
52 	for (; 0 < size; --size) {
53 		value *= 27239;
54 		value += *(ptr++);
55 	}
56 	return (value % hash_size);
57 }
58 
59 static itm_num_t
60 hash_dense_encoding(
61 	const unsigned char	*byte_seq,
62 	itm_size_t		length,
63 	const unsigned char	*byte_seq_min,
64 	const unsigned char	*byte_seq_max)
65 {
66 	long		i;
67 	itm_num_t	num;
68 
69 	num = (*byte_seq - *byte_seq_min);
70 	byte_seq_min++;
71 	byte_seq_max++;
72 	for (i = 1, byte_seq++; i < length;
73 	    i++, byte_seq++, byte_seq_min++, byte_seq_max++) {
74 		if ((*byte_seq < *byte_seq_min) ||
75 		    (*byte_seq_max < *byte_seq)) {
76 			return (-1);
77 		}
78 		num *= (*byte_seq_max - *byte_seq_min + 1);
79 		num += (*byte_seq - *byte_seq_min);
80 	}
81 	return (num);
82 }
83 
84 #ifdef	__cplusplus
85 }
86 #endif
87 
88 #endif /* !_ICONV_TM_HASH_H */
89