8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / lib / libsecdb / common / secdb.c
blobe012d95ce035b0d72a4b2c5cf66b8c9d6fe243d4
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <strings.h>
29 #include <secdb.h>
30 #include <ctype.h>
32 /* From libnsl */
33 extern char *_strdup_null(char *);
34 extern char *_strtok_escape(char *, char *, char **);
35 extern char *_strpbrk_escape(char *, char *);
36 extern char *_unescape(char *, char *);
38 char *_do_unescape(char *);
42 * kva_match(): Given a key-value array and a key, return a pointer to the
43 * value that matches the key.
45 char *
46 kva_match(kva_t *kva, char *key)
48 int i;
49 kv_t *data;
51 if (kva == NULL || key == NULL) {
52 return (NULL);
54 data = kva->data;
55 for (i = 0; i < kva->length; i++) {
56 if (strcmp(data[i].key, key) == 0) {
57 return (data[i].value);
61 return (NULL);
65 * _kva_free(): Free up memory.
67 void
68 _kva_free(kva_t *kva)
70 int i;
71 kv_t *data;
73 if (kva == NULL) {
74 return;
76 data = kva->data;
77 for (i = 0; i < kva->length; i++) {
78 if (data[i].key != NULL) {
79 free(data[i].key);
80 data[i].key = NULL;
82 if (data[i].value != NULL) {
83 free(data[i].value);
84 data[i].value = NULL;
87 free(kva->data);
88 free(kva);
92 * _kva_free_value(): Free up memory (value) for all the occurrences of
93 * the given key.
95 void
96 _kva_free_value(kva_t *kva, char *key)
98 int ctr;
99 kv_t *data;
101 if (kva == NULL) {
102 return;
105 ctr = kva->length;
106 data = kva->data;
108 while (ctr--) {
109 if (strcmp(data->key, key) == 0 && data->value != NULL) {
110 free(data->value);
111 data->value = NULL;
113 data++;
118 * new_kva(): Allocate a key-value array.
120 kva_t *
121 _new_kva(int size)
123 kva_t *new_kva;
125 if ((new_kva = (kva_t *)calloc(1, sizeof (kva_t))) == NULL) {
126 return (NULL);
128 if ((new_kva->data = (kv_t *)calloc(1, (size*sizeof (kv_t)))) == NULL) {
129 free(new_kva);
130 return (NULL);
133 return (new_kva);
137 * _str2kva(): Given a string (s) of key-value pairs, separated by delimeter
138 * (del), place the values into the key value array (nkva).
140 kva_t *
141 _str2kva(char *s, char *ass, char *del)
143 int n = 0;
144 int m;
145 int size = KV_ADD_KEYS;
146 char *buf;
147 char *p;
148 char *pair;
149 char *key;
150 char *last_pair;
151 char *last_key;
152 kv_t *data;
153 kva_t *nkva;
155 if (s == NULL ||
156 ass == NULL ||
157 del == NULL ||
158 *s == '\0' ||
159 *s == '\n' ||
160 (strlen(s) <= 1)) {
161 return (NULL);
163 p = s;
164 while ((p = _strpbrk_escape(p, ass)) != NULL) {
165 n++;
166 p++;
168 if (n > size) {
169 m = n/size;
170 if (n%size) {
171 ++m;
173 size = m * KV_ADD_KEYS;
175 if ((nkva = _new_kva(size)) == NULL) {
176 return (NULL);
178 data = nkva->data;
179 nkva->length = 0;
180 if ((buf = strdup(s)) == NULL) {
181 return (NULL);
183 pair = _strtok_escape(buf, del, &last_pair);
184 do {
185 key = _strtok_escape(pair, ass, &last_key);
186 if (key != NULL) {
187 data[nkva->length].key = _do_unescape(key);
188 data[nkva->length].value = _do_unescape(last_key);
189 nkva->length++;
191 } while ((pair = _strtok_escape(NULL, del, &last_pair)) != NULL);
192 free(buf);
193 return (nkva);
197 * _kva2str(): Given an array of key-value pairs, place them into a string
198 * (buf). Use delimeter (del) to separate pairs. Use assignment character
199 * (ass) to separate keys and values.
201 * Return Values: 0 Success 1 Buffer too small
204 _kva2str(kva_t *kva, char *buf, int buflen, char *ass, char *del)
206 int i;
207 int len;
208 int off = 0;
209 kv_t *data;
211 if (kva == NULL) {
212 return (0);
215 buf[0] = '\0';
216 data = kva->data;
218 for (i = 0; i < kva->length; i++) {
219 if (data[i].value != NULL) {
220 len = snprintf(buf + off, buflen - off, "%s%s%s%s",
221 data[i].key, ass, data[i].value, del);
222 if (len < 0 || len + off >= buflen) {
223 return (1);
225 off += len;
229 return (0);
233 _insert2kva(kva_t *kva, char *key, char *value)
235 int i;
236 kv_t *data;
238 if (kva == NULL) {
239 return (0);
241 data = kva->data;
242 for (i = 0; i < kva->length; i++) {
243 if (strcmp(data[i].key, key) == 0) {
244 if (data[i].value != NULL)
245 free(data[i].value);
246 data[i].value = _strdup_null(value);
247 return (0);
250 return (1);
253 kva_t *
254 _kva_dup(kva_t *old_kva)
256 int i;
257 int size;
258 kv_t *old_data;
259 kv_t *new_data;
260 kva_t *nkva = NULL;
262 if (old_kva == NULL) {
263 return (NULL);
265 old_data = old_kva->data;
266 size = old_kva->length;
267 if ((nkva = _new_kva(size)) == NULL) {
268 return (NULL);
270 new_data = nkva->data;
271 nkva->length = old_kva->length;
272 for (i = 0; i < nkva->length; i++) {
273 new_data[i].key = _strdup_null(old_data[i].key);
274 new_data[i].value = _strdup_null(old_data[i].value);
277 return (nkva);
280 static void
281 strip_spaces(char **valuep)
283 char *p, *start;
285 /* Find first non-white space character and return pointer to it */
286 for (p = *valuep; *p != '\0' && isspace((unsigned char)*p); p++)
289 *valuep = start = p;
291 if (*p == '\0')
292 return;
294 p = p + strlen(p) - 1;
296 /* Remove trailing spaces */
297 while (p > start && isspace((unsigned char)*p))
298 p--;
300 p[1] = '\0';
303 char *
304 _do_unescape(char *src)
306 char *tmp = NULL;
307 char *dst = NULL;
309 if (src == NULL) {
310 dst = _strdup_null(src);
311 } else {
312 strip_spaces(&src);
313 tmp = _unescape(src, "=;:,\\");
314 dst = (tmp == NULL) ? _strdup_null(src) : tmp;
317 return (dst);
322 * Some utilities for handling comma-separated lists.
324 char *
325 _argv_to_csl(char **strings)
327 int len = 0;
328 int i = 0;
329 char *newstr = NULL;
331 if (strings == NULL)
332 return (NULL);
333 for (i = 0; strings[i] != NULL; i++) {
334 len += strlen(strings[i]) + 1;
336 if ((len > 0) && ((newstr = (char *)malloc(len + 1)) != NULL)) {
337 (void) memset(newstr, 0, len);
338 for (i = 0; strings[i] != NULL; i++) {
339 (void) strcat(newstr, strings[i]);
340 (void) strcat(newstr, ",");
342 newstr[len-1] = NULL;
343 return (newstr);
344 } else
345 return (NULL);
349 char **
350 _csl_to_argv(char *csl)
352 int len = 0;
353 int ncommas = 0;
354 int i = 0;
355 char **spc = NULL;
356 char *copy = NULL;
357 char *pc;
358 char *lasts = NULL;
360 len = strlen(csl);
361 for (i = 0; i < len; i++) {
362 if (csl[i] == ',')
363 ncommas++;
365 if ((spc = (char **)malloc((ncommas + 2) * sizeof (char *))) == NULL) {
366 return (NULL);
368 copy = strdup(csl);
369 for (pc = strtok_r(copy, ",", &lasts), i = 0; pc != NULL;
370 pc = strtok_r(NULL, ",", &lasts), i++) {
371 spc[i] = strdup(pc);
373 spc[i] = NULL;
374 free(copy);
375 return (spc);
379 void
380 _free_argv(char **p_argv)
382 char **p_a;
384 for (p_a = p_argv; *p_a != NULL; p_a++)
385 free(*p_a);
386 free(p_argv);
390 #ifdef DEBUG
391 void
392 print_kva(kva_t *kva)
394 int i;
395 kv_t *data;
397 if (kva == NULL) {
398 (void) printf(" (empty)\n");
399 return;
401 data = kva->data;
402 for (i = 0; i < kva->length; i++) {
403 (void) printf(" %s = %s\n",
404 data[i].key != NULL ? data[i].key : "NULL",
405 data[i].value != NULL ? data[i].value : "NULL");
408 #endif /* DEBUG */