xref: /illumos-gate/usr/src/lib/libnisdb/db_entry.cc (revision 1da57d551424de5a9d469760be7c4b4d4f10a755)
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_entry.cc
24*7c478bd9Sstevel@tonic-gate  *
25*7c478bd9Sstevel@tonic-gate  *	Copyright (c) 1988-2001 by Sun Microsystems, Inc.
26*7c478bd9Sstevel@tonic-gate  *	All Rights Reserved.
27*7c478bd9Sstevel@tonic-gate  */
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
30*7c478bd9Sstevel@tonic-gate #include <stdio.h>
31*7c478bd9Sstevel@tonic-gate #include <string.h>
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include "db_headers.h"
34*7c478bd9Sstevel@tonic-gate #include "db_table.h"  /* must come before db_entry */
35*7c478bd9Sstevel@tonic-gate #include "db_entry.h"
36*7c478bd9Sstevel@tonic-gate #include "nisdb_mt.h"
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate #define	PRINT_WIDTH 32
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate void
print_entry(entryp location,entry_object * e)41*7c478bd9Sstevel@tonic-gate print_entry(entryp location, entry_object *e)
42*7c478bd9Sstevel@tonic-gate {
43*7c478bd9Sstevel@tonic-gate 	printf("entry at location %d: \n", location);
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate 	if (e == NULL) {
46*7c478bd9Sstevel@tonic-gate 		printf("\tnull object\n");
47*7c478bd9Sstevel@tonic-gate 		return;
48*7c478bd9Sstevel@tonic-gate 	}
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate 	int size = e->en_cols.en_cols_len, i, j, col_width;
51*7c478bd9Sstevel@tonic-gate 	entry_col * entry = e->en_cols.en_cols_val;
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate 	printf("\ttype: %s\n", e->en_type ? e->en_type : "none");
54*7c478bd9Sstevel@tonic-gate 	printf("\tnumber of columns: %d\n", size);
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < size; i++) {
57*7c478bd9Sstevel@tonic-gate 		printf("\t\t%d: flags=0x%x, length=%d, value=",
58*7c478bd9Sstevel@tonic-gate 			i, entry[i].ec_flags, entry[i].ec_value.ec_value_len);
59*7c478bd9Sstevel@tonic-gate 		col_width = ((entry[i].ec_value.ec_value_len > PRINT_WIDTH) ?
60*7c478bd9Sstevel@tonic-gate 				PRINT_WIDTH : entry[i].ec_value.ec_value_len);
61*7c478bd9Sstevel@tonic-gate 		for (j = 0; j < col_width; j++) {
62*7c478bd9Sstevel@tonic-gate 			if (entry[i].ec_value.ec_value_val[j] < 32) {
63*7c478bd9Sstevel@tonic-gate 				putchar('^');
64*7c478bd9Sstevel@tonic-gate 				putchar(entry[i].ec_value.ec_value_val[j]+32);
65*7c478bd9Sstevel@tonic-gate 			} else {
66*7c478bd9Sstevel@tonic-gate 				putchar(entry[i].ec_value.ec_value_val[j]);
67*7c478bd9Sstevel@tonic-gate 			}
68*7c478bd9Sstevel@tonic-gate 		}
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate 		putchar('\n');
71*7c478bd9Sstevel@tonic-gate 	}
72*7c478bd9Sstevel@tonic-gate }
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate entry_object*
new_entry(entry_object * old)75*7c478bd9Sstevel@tonic-gate new_entry(entry_object *old)
76*7c478bd9Sstevel@tonic-gate {
77*7c478bd9Sstevel@tonic-gate 	entry_object* newobj = new entry_object;
78*7c478bd9Sstevel@tonic-gate 	if (newobj == NULL)
79*7c478bd9Sstevel@tonic-gate 	    FATAL3("new_entry:: cannot allocate space", DB_MEMORY_LIMIT,
80*7c478bd9Sstevel@tonic-gate 			NULL);
81*7c478bd9Sstevel@tonic-gate 
82*7c478bd9Sstevel@tonic-gate 	if (copy_entry(old, newobj))
83*7c478bd9Sstevel@tonic-gate 		return (newobj);
84*7c478bd9Sstevel@tonic-gate 	else {
85*7c478bd9Sstevel@tonic-gate 	    delete newobj;
86*7c478bd9Sstevel@tonic-gate 	    return (NULL);
87*7c478bd9Sstevel@tonic-gate 	}
88*7c478bd9Sstevel@tonic-gate }
89*7c478bd9Sstevel@tonic-gate 
90*7c478bd9Sstevel@tonic-gate bool_t
copy_entry(entry_object * old,entry_object * nb)91*7c478bd9Sstevel@tonic-gate copy_entry(entry_object * old, entry_object *nb)
92*7c478bd9Sstevel@tonic-gate {
93*7c478bd9Sstevel@tonic-gate 	int tlen, j, i;
94*7c478bd9Sstevel@tonic-gate 	int num_cols = 0;
95*7c478bd9Sstevel@tonic-gate 	entry_col *cols, *newcols = NULL;
96*7c478bd9Sstevel@tonic-gate 
97*7c478bd9Sstevel@tonic-gate 	if (old == NULL) return FALSE;
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate 	if (old->en_type == NULL)
100*7c478bd9Sstevel@tonic-gate 		nb->en_type = NULL;
101*7c478bd9Sstevel@tonic-gate 	else {
102*7c478bd9Sstevel@tonic-gate 		nb->en_type = strdup(old->en_type);
103*7c478bd9Sstevel@tonic-gate 		if (nb->en_type == NULL)
104*7c478bd9Sstevel@tonic-gate 			FATAL3(
105*7c478bd9Sstevel@tonic-gate 			    "copy_entry: cannot allocate space for entry type",
106*7c478bd9Sstevel@tonic-gate 			    DB_MEMORY_LIMIT, FALSE);
107*7c478bd9Sstevel@tonic-gate 	}
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate 	num_cols = old->en_cols.en_cols_len;
110*7c478bd9Sstevel@tonic-gate 	cols = old->en_cols.en_cols_val;
111*7c478bd9Sstevel@tonic-gate 	if (num_cols == 0)
112*7c478bd9Sstevel@tonic-gate 		nb->en_cols.en_cols_val = NULL;
113*7c478bd9Sstevel@tonic-gate 	else {
114*7c478bd9Sstevel@tonic-gate 		newcols = new entry_col[num_cols];
115*7c478bd9Sstevel@tonic-gate 		if (newcols == NULL) {
116*7c478bd9Sstevel@tonic-gate 			if (nb->en_type)
117*7c478bd9Sstevel@tonic-gate 			delete nb->en_type;
118*7c478bd9Sstevel@tonic-gate 			FATAL3("copy_entry: cannot allocate space for columns",
119*7c478bd9Sstevel@tonic-gate 				DB_MEMORY_LIMIT, FALSE);
120*7c478bd9Sstevel@tonic-gate 		}
121*7c478bd9Sstevel@tonic-gate 		for (j = 0; j < num_cols; j++) {
122*7c478bd9Sstevel@tonic-gate 			newcols[j].ec_flags = cols[j].ec_flags;
123*7c478bd9Sstevel@tonic-gate 			tlen = newcols[j].ec_value.ec_value_len =
124*7c478bd9Sstevel@tonic-gate 				cols[j].ec_value.ec_value_len;
125*7c478bd9Sstevel@tonic-gate 			newcols[j].ec_value.ec_value_val = new char[ tlen ];
126*7c478bd9Sstevel@tonic-gate 			if (newcols[j].ec_value.ec_value_val == NULL) {
127*7c478bd9Sstevel@tonic-gate 				// cleanup space already allocated
128*7c478bd9Sstevel@tonic-gate 				if (nb->en_type)
129*7c478bd9Sstevel@tonic-gate 					delete nb->en_type;
130*7c478bd9Sstevel@tonic-gate 				for (i = 0; i < j; i++)
131*7c478bd9Sstevel@tonic-gate 					delete newcols[i].ec_value.ec_value_val;
132*7c478bd9Sstevel@tonic-gate 				delete newcols;
133*7c478bd9Sstevel@tonic-gate 				FATAL3(
134*7c478bd9Sstevel@tonic-gate 			"copy_entry: cannot allocate space for column value",
135*7c478bd9Sstevel@tonic-gate 			DB_MEMORY_LIMIT, FALSE);
136*7c478bd9Sstevel@tonic-gate 			}
137*7c478bd9Sstevel@tonic-gate 			memcpy(newcols[j].ec_value.ec_value_val,
138*7c478bd9Sstevel@tonic-gate 				cols[j].ec_value.ec_value_val,
139*7c478bd9Sstevel@tonic-gate 				tlen);
140*7c478bd9Sstevel@tonic-gate 		}
141*7c478bd9Sstevel@tonic-gate 	}
142*7c478bd9Sstevel@tonic-gate 	nb->en_cols.en_cols_len = num_cols;
143*7c478bd9Sstevel@tonic-gate 	nb->en_cols.en_cols_val = newcols;
144*7c478bd9Sstevel@tonic-gate 	return (TRUE);
145*7c478bd9Sstevel@tonic-gate }
146*7c478bd9Sstevel@tonic-gate 
147*7c478bd9Sstevel@tonic-gate void
free_entry(entry_object * obj)148*7c478bd9Sstevel@tonic-gate free_entry(entry_object * obj)
149*7c478bd9Sstevel@tonic-gate {
150*7c478bd9Sstevel@tonic-gate 	int i;
151*7c478bd9Sstevel@tonic-gate 	int num_cols;
152*7c478bd9Sstevel@tonic-gate 	entry_col *cols;
153*7c478bd9Sstevel@tonic-gate 
154*7c478bd9Sstevel@tonic-gate 	if (obj != NULL) {
155*7c478bd9Sstevel@tonic-gate 		num_cols = obj->en_cols.en_cols_len;
156*7c478bd9Sstevel@tonic-gate 		cols = obj->en_cols.en_cols_val;
157*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < num_cols; i++)
158*7c478bd9Sstevel@tonic-gate 			if (cols[i].ec_value.ec_value_val != NULL)
159*7c478bd9Sstevel@tonic-gate 				delete cols[i].ec_value.ec_value_val;
160*7c478bd9Sstevel@tonic-gate 		if (cols)
161*7c478bd9Sstevel@tonic-gate 			delete cols;
162*7c478bd9Sstevel@tonic-gate 		if (obj->en_type)
163*7c478bd9Sstevel@tonic-gate 			delete obj->en_type;
164*7c478bd9Sstevel@tonic-gate 		delete obj;
165*7c478bd9Sstevel@tonic-gate 	}
166*7c478bd9Sstevel@tonic-gate }
167*7c478bd9Sstevel@tonic-gate 
168*7c478bd9Sstevel@tonic-gate bool_t
sameEntry(entry_object * a,entry_object * b)169*7c478bd9Sstevel@tonic-gate sameEntry(entry_object *a, entry_object *b) {
170*7c478bd9Sstevel@tonic-gate 	uint_t	i;
171*7c478bd9Sstevel@tonic-gate 
172*7c478bd9Sstevel@tonic-gate 	if (a == 0)
173*7c478bd9Sstevel@tonic-gate 		return (b == 0);
174*7c478bd9Sstevel@tonic-gate 	if (b == 0)
175*7c478bd9Sstevel@tonic-gate 		return (FALSE);
176*7c478bd9Sstevel@tonic-gate 
177*7c478bd9Sstevel@tonic-gate 	if (a->en_type != 0 && b->en_type != 0) {
178*7c478bd9Sstevel@tonic-gate 		if (strcmp(a->en_type, b->en_type) != 0)
179*7c478bd9Sstevel@tonic-gate 			return (FALSE);
180*7c478bd9Sstevel@tonic-gate 	} else if (a->en_type != b->en_type) {
181*7c478bd9Sstevel@tonic-gate 		return (FALSE);
182*7c478bd9Sstevel@tonic-gate 	}
183*7c478bd9Sstevel@tonic-gate 
184*7c478bd9Sstevel@tonic-gate 	if (a->en_cols.en_cols_len != b->en_cols.en_cols_len)
185*7c478bd9Sstevel@tonic-gate 		return (FALSE);
186*7c478bd9Sstevel@tonic-gate 
187*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < a->en_cols.en_cols_len; i++) {
188*7c478bd9Sstevel@tonic-gate 		if (a->en_cols.en_cols_val[i].ec_flags !=
189*7c478bd9Sstevel@tonic-gate 				b->en_cols.en_cols_val[i].ec_flags)
190*7c478bd9Sstevel@tonic-gate 			return (FALSE);
191*7c478bd9Sstevel@tonic-gate 		if (a->en_cols.en_cols_val[i].ec_value.ec_value_len !=
192*7c478bd9Sstevel@tonic-gate 			b->en_cols.en_cols_val[i].ec_value.ec_value_len)
193*7c478bd9Sstevel@tonic-gate 			return (FALSE);
194*7c478bd9Sstevel@tonic-gate 		if (memcmp(a->en_cols.en_cols_val[i].ec_value.ec_value_val,
195*7c478bd9Sstevel@tonic-gate 			b->en_cols.en_cols_val[i].ec_value.ec_value_val,
196*7c478bd9Sstevel@tonic-gate 			a->en_cols.en_cols_val[i].ec_value.ec_value_len) != 0)
197*7c478bd9Sstevel@tonic-gate 			return (FALSE);
198*7c478bd9Sstevel@tonic-gate 	}
199*7c478bd9Sstevel@tonic-gate 
200*7c478bd9Sstevel@tonic-gate 	return (TRUE);
201*7c478bd9Sstevel@tonic-gate }
202