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