xref: /illumos-gate/usr/src/lib/libnisdb/db_item.cc (revision 1da57d55)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  *	db_item.cc
24*7c478bd9Sstevel@tonic-gate  *
25*7c478bd9Sstevel@tonic-gate  *	Copyright (c) 1988-2000 by Sun Microsystems, Inc.
26*7c478bd9Sstevel@tonic-gate  *	All Rights Reserved.
27*7c478bd9Sstevel@tonic-gate  */
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <stdio.h>
30*7c478bd9Sstevel@tonic-gate #include <string.h>
31*7c478bd9Sstevel@tonic-gate #include <ctype.h>
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include "db_headers.h"
34*7c478bd9Sstevel@tonic-gate #include "db_item.h"
35*7c478bd9Sstevel@tonic-gate #include "nisdb_mt.h"
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate #define	HASHSHIFT	3
38*7c478bd9Sstevel@tonic-gate #define	HASHMASK	0x1f
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate #ifdef TDRPC
41*7c478bd9Sstevel@tonic-gate #define	LOWER(c)	(isupper((c)) ? tolower((c)) : (c))
42*7c478bd9Sstevel@tonic-gate extern "C" {
43*7c478bd9Sstevel@tonic-gate 	int strncasecmp(const char *s1, const char *s2, int n);
44*7c478bd9Sstevel@tonic-gate };
45*7c478bd9Sstevel@tonic-gate #else
46*7c478bd9Sstevel@tonic-gate #define	LOWER(c)	(isupper((c)) ? _tolower((c)) : (c))
47*7c478bd9Sstevel@tonic-gate #endif
48*7c478bd9Sstevel@tonic-gate 
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate /* Constructor: creates item using given character sequence and length */
item(char * str,int n)51*7c478bd9Sstevel@tonic-gate item::item(char *str, int n)
52*7c478bd9Sstevel@tonic-gate {
53*7c478bd9Sstevel@tonic-gate 	len = n;
54*7c478bd9Sstevel@tonic-gate 	if ((value = new char[len]) == NULL)
55*7c478bd9Sstevel@tonic-gate 		FATAL("item::item: cannot allocate space", DB_MEMORY_LIMIT);
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate 	(void) memcpy(value, str, len);
58*7c478bd9Sstevel@tonic-gate }
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate /* Constructor: creates item by copying given item */
item(item * model)62*7c478bd9Sstevel@tonic-gate item::item(item *model)
63*7c478bd9Sstevel@tonic-gate {
64*7c478bd9Sstevel@tonic-gate 	len = model->len;
65*7c478bd9Sstevel@tonic-gate 	if ((value = new char[len]) == NULL)
66*7c478bd9Sstevel@tonic-gate 		FATAL(" item::item: cannot allocate space (2)",
67*7c478bd9Sstevel@tonic-gate 			DB_MEMORY_LIMIT);
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate 	(void) memcpy(value, model->value, len);
70*7c478bd9Sstevel@tonic-gate }
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate /* Prints contents of item to stdout */
73*7c478bd9Sstevel@tonic-gate void
print()74*7c478bd9Sstevel@tonic-gate item::print()
75*7c478bd9Sstevel@tonic-gate {
76*7c478bd9Sstevel@tonic-gate 	int i;
77*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < len; i++)
78*7c478bd9Sstevel@tonic-gate 		putchar(value[i]);
79*7c478bd9Sstevel@tonic-gate }
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate /* Equality test.  'casein' TRUE means case insensitive test. */
82*7c478bd9Sstevel@tonic-gate bool_t
equal(item * other,bool_t casein)83*7c478bd9Sstevel@tonic-gate item::equal(item* other, bool_t casein)
84*7c478bd9Sstevel@tonic-gate {
85*7c478bd9Sstevel@tonic-gate 	if (casein)	// case-insensitive
86*7c478bd9Sstevel@tonic-gate 		return ((len == other->len) &&
87*7c478bd9Sstevel@tonic-gate 			(!strncasecmp(value, other->value, len)));
88*7c478bd9Sstevel@tonic-gate 	else		// case sensitive
89*7c478bd9Sstevel@tonic-gate 		return ((len == other->len) &&
90*7c478bd9Sstevel@tonic-gate 			(!memcmp(value, other->value, len)));
91*7c478bd9Sstevel@tonic-gate }
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate bool_t
equal(char * other,int olen,bool_t casein)94*7c478bd9Sstevel@tonic-gate item::equal(char* other, int olen, bool_t casein)
95*7c478bd9Sstevel@tonic-gate {
96*7c478bd9Sstevel@tonic-gate 	if (casein)	// case-insensitive
97*7c478bd9Sstevel@tonic-gate 		return ((len == olen) && (!strncasecmp(value, other, len)));
98*7c478bd9Sstevel@tonic-gate 	else		// case sensitive
99*7c478bd9Sstevel@tonic-gate 		return ((len == olen) && (!memcmp(value, other, len)));
100*7c478bd9Sstevel@tonic-gate }
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate /* Return hash value.  'casein' TRUE means case insensitive test. */
103*7c478bd9Sstevel@tonic-gate u_int
get_hashval(bool_t casein)104*7c478bd9Sstevel@tonic-gate item::get_hashval(bool_t casein)
105*7c478bd9Sstevel@tonic-gate {
106*7c478bd9Sstevel@tonic-gate 	int i;
107*7c478bd9Sstevel@tonic-gate 	u_int hval = 0;
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate 	// we want to separate the cases so that we don't needlessly do
110*7c478bd9Sstevel@tonic-gate 	// an extra test for the case-sensitive branch in the for loop
111*7c478bd9Sstevel@tonic-gate 	if (casein) {	// case insensitive
112*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < len; i++) {
113*7c478bd9Sstevel@tonic-gate 			hval = ((hval<<HASHSHIFT)^hval);
114*7c478bd9Sstevel@tonic-gate 			hval += (LOWER(value[i]) & HASHMASK);
115*7c478bd9Sstevel@tonic-gate 		}
116*7c478bd9Sstevel@tonic-gate 	}  else {	// case sensitive
117*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < len; i++) {
118*7c478bd9Sstevel@tonic-gate 			hval = ((hval<<HASHSHIFT)^hval);
119*7c478bd9Sstevel@tonic-gate 			hval += (value[i] & HASHMASK);
120*7c478bd9Sstevel@tonic-gate 		}
121*7c478bd9Sstevel@tonic-gate 	}
122*7c478bd9Sstevel@tonic-gate 
123*7c478bd9Sstevel@tonic-gate 	return (hval);
124*7c478bd9Sstevel@tonic-gate }
125