xref: /illumos-gate/usr/src/cmd/geniconvtbl/hash.h (revision d48be21240dfd051b689384ce2b23479d757f2d8)
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 #ifdef	__cplusplus
31 extern "C" {
32 #endif
33 
34 
35 
36 static itm_size_t	hash(const char *, itm_size_t, itm_size_t);
37 static itm_num_t	hash_dense_encoding(const unsigned char *, itm_size_t,
38 				    const unsigned char *,
39 				    const unsigned char *);
40 
41 
42 static itm_size_t
43 hash(const char *ptr, itm_size_t size, itm_size_t hash_size)
44 {
45 	itm_size_t	value;
46 
47 	value = *(ptr++);
48 	--size;
49 	for (; 0 < size; --size) {
50 		value *= 27239;
51 		value += *(ptr++);
52 	}
53 	return (value % hash_size);
54 }
55 
56 static itm_num_t
57 hash_dense_encoding(
58 	const unsigned char	*byte_seq,
59 	itm_size_t		length,
60 	const unsigned char	*byte_seq_min,
61 	const unsigned char	*byte_seq_max)
62 {
63 	long		i;
64 	itm_num_t	num;
65 
66 	num = (*byte_seq - *byte_seq_min);
67 	byte_seq_min++;
68 	byte_seq_max++;
69 	for (i = 1, byte_seq++; i < length;
70 	    i++, byte_seq++, byte_seq_min++, byte_seq_max++) {
71 		if ((*byte_seq < *byte_seq_min) ||
72 		    (*byte_seq_max < *byte_seq)) {
73 			return (-1);
74 		}
75 		num *= (*byte_seq_max - *byte_seq_min + 1);
76 		num += (*byte_seq - *byte_seq_min);
77 	}
78 	return (num);
79 }
80 
81 #ifdef	__cplusplus
82 }
83 #endif
84 
85 #endif /* !_ICONV_TM_HASH_H */
86