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