xref: /illumos-gate/usr/src/lib/libnisdb/db_item.cc (revision 7a6d80f1660abd4755c68cbd094d4a914681d26e)
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  *	db_item.cc
24  *
25  *	Copyright (c) 1988-2000 by Sun Microsystems, Inc.
26  *	All Rights Reserved.
27  */
28 
29 #include <stdio.h>
30 #include <string.h>
31 #include <ctype.h>
32 
33 #include "db_headers.h"
34 #include "db_item.h"
35 #include "nisdb_mt.h"
36 
37 #define	HASHSHIFT	3
38 #define	HASHMASK	0x1f
39 
40 #ifdef TDRPC
41 #define	LOWER(c)	(isupper((c)) ? tolower((c)) : (c))
42 extern "C" {
43 	int strncasecmp(const char *s1, const char *s2, int n);
44 };
45 #else
46 #define	LOWER(c)	(isupper((c)) ? _tolower((c)) : (c))
47 #endif
48 
49 
50 /* Constructor: creates item using given character sequence and length */
51 item::item(char *str, int n)
52 {
53 	len = n;
54 	if ((value = new char[len]) == NULL)
55 		FATAL("item::item: cannot allocate space", DB_MEMORY_LIMIT);
56 
57 	(void) memcpy(value, str, len);
58 }
59 
60 
61 /* Constructor: creates item by copying given item */
62 item::item(item *model)
63 {
64 	len = model->len;
65 	if ((value = new char[len]) == NULL)
66 		FATAL(" item::item: cannot allocate space (2)",
67 			DB_MEMORY_LIMIT);
68 
69 	(void) memcpy(value, model->value, len);
70 }
71 
72 /* Prints contents of item to stdout */
73 void
74 item::print()
75 {
76 	int i;
77 	for (i = 0; i < len; i++)
78 		putchar(value[i]);
79 }
80 
81 /* Equality test.  'casein' TRUE means case insensitive test. */
82 bool_t
83 item::equal(item* other, bool_t casein)
84 {
85 	if (casein)	// case-insensitive
86 		return ((len == other->len) &&
87 			(!strncasecmp(value, other->value, len)));
88 	else		// case sensitive
89 		return ((len == other->len) &&
90 			(!memcmp(value, other->value, len)));
91 }
92 
93 bool_t
94 item::equal(char* other, int olen, bool_t casein)
95 {
96 	if (casein)	// case-insensitive
97 		return ((len == olen) && (!strncasecmp(value, other, len)));
98 	else		// case sensitive
99 		return ((len == olen) && (!memcmp(value, other, len)));
100 }
101 
102 /* Return hash value.  'casein' TRUE means case insensitive test. */
103 u_int
104 item::get_hashval(bool_t casein)
105 {
106 	int i;
107 	u_int hval = 0;
108 
109 	// we want to separate the cases so that we don't needlessly do
110 	// an extra test for the case-sensitive branch in the for loop
111 	if (casein) {	// case insensitive
112 		for (i = 0; i < len; i++) {
113 			hval = ((hval<<HASHSHIFT)^hval);
114 			hval += (LOWER(value[i]) & HASHMASK);
115 		}
116 	}  else {	// case sensitive
117 		for (i = 0; i < len; i++) {
118 			hval = ((hval<<HASHSHIFT)^hval);
119 			hval += (value[i] & HASHMASK);
120 		}
121 	}
122 
123 	return (hval);
124 }
125