xref: /openbmc/u-boot/lib/hashtable.c (revision 6b7c0f5e)
1 /*
2  * This implementation is based on code from uClibc-0.9.30.3 but was
3  * modified and extended for use within U-Boot.
4  *
5  * Copyright (C) 2010 Wolfgang Denk <wd@denx.de>
6  *
7  * Original license header:
8  *
9  * Copyright (C) 1993, 1995, 1996, 1997, 2002 Free Software Foundation, Inc.
10  * This file is part of the GNU C Library.
11  * Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1993.
12  *
13  * The GNU C Library is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU Lesser General Public
15  * License as published by the Free Software Foundation; either
16  * version 2.1 of the License, or (at your option) any later version.
17  *
18  * The GNU C Library is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21  * Lesser General Public License for more details.
22  *
23  * You should have received a copy of the GNU Lesser General Public
24  * License along with the GNU C Library; if not, write to the Free
25  * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
26  * 02111-1307 USA.
27  */
28 
29 #include <errno.h>
30 #include <malloc.h>
31 
32 #ifdef USE_HOSTCC		/* HOST build */
33 # include <string.h>
34 # include <assert.h>
35 
36 # ifndef debug
37 #  ifdef DEBUG
38 #   define debug(fmt,args...)	printf(fmt ,##args)
39 #  else
40 #   define debug(fmt,args...)
41 #  endif
42 # endif
43 #else				/* U-Boot build */
44 # include <common.h>
45 # include <linux/string.h>
46 #endif
47 
48 #ifndef	CONFIG_ENV_MIN_ENTRIES	/* minimum number of entries */
49 #define	CONFIG_ENV_MIN_ENTRIES 64
50 #endif
51 #ifndef	CONFIG_ENV_MAX_ENTRIES	/* maximum number of entries */
52 #define	CONFIG_ENV_MAX_ENTRIES 512
53 #endif
54 
55 #include "search.h"
56 
57 /*
58  * [Aho,Sethi,Ullman] Compilers: Principles, Techniques and Tools, 1986
59  * [Knuth]	      The Art of Computer Programming, part 3 (6.4)
60  */
61 
62 /*
63  * The reentrant version has no static variables to maintain the state.
64  * Instead the interface of all functions is extended to take an argument
65  * which describes the current status.
66  */
67 typedef struct _ENTRY {
68 	unsigned int used;
69 	ENTRY entry;
70 } _ENTRY;
71 
72 
73 /*
74  * hcreate()
75  */
76 
77 /*
78  * For the used double hash method the table size has to be a prime. To
79  * correct the user given table size we need a prime test.  This trivial
80  * algorithm is adequate because
81  * a)  the code is (most probably) called a few times per program run and
82  * b)  the number is small because the table must fit in the core
83  * */
84 static int isprime(unsigned int number)
85 {
86 	/* no even number will be passed */
87 	unsigned int div = 3;
88 
89 	while (div * div < number && number % div != 0)
90 		div += 2;
91 
92 	return number % div != 0;
93 }
94 
95 /*
96  * Before using the hash table we must allocate memory for it.
97  * Test for an existing table are done. We allocate one element
98  * more as the found prime number says. This is done for more effective
99  * indexing as explained in the comment for the hsearch function.
100  * The contents of the table is zeroed, especially the field used
101  * becomes zero.
102  */
103 
104 int hcreate_r(size_t nel, struct hsearch_data *htab)
105 {
106 	/* Test for correct arguments.  */
107 	if (htab == NULL) {
108 		__set_errno(EINVAL);
109 		return 0;
110 	}
111 
112 	/* There is still another table active. Return with error. */
113 	if (htab->table != NULL)
114 		return 0;
115 
116 	/* Change nel to the first prime number not smaller as nel. */
117 	nel |= 1;		/* make odd */
118 	while (!isprime(nel))
119 		nel += 2;
120 
121 	htab->size = nel;
122 	htab->filled = 0;
123 
124 	/* allocate memory and zero out */
125 	htab->table = (_ENTRY *) calloc(htab->size + 1, sizeof(_ENTRY));
126 	if (htab->table == NULL)
127 		return 0;
128 
129 	/* everything went alright */
130 	return 1;
131 }
132 
133 
134 /*
135  * hdestroy()
136  */
137 
138 /*
139  * After using the hash table it has to be destroyed. The used memory can
140  * be freed and the local static variable can be marked as not used.
141  */
142 
143 void hdestroy_r(struct hsearch_data *htab)
144 {
145 	int i;
146 
147 	/* Test for correct arguments.  */
148 	if (htab == NULL) {
149 		__set_errno(EINVAL);
150 		return;
151 	}
152 
153 	/* free used memory */
154 	for (i = 1; i <= htab->size; ++i) {
155 		if (htab->table[i].used) {
156 			ENTRY *ep = &htab->table[i].entry;
157 
158 			free(ep->key);
159 			free(ep->data);
160 		}
161 	}
162 	free(htab->table);
163 
164 	/* the sign for an existing table is an value != NULL in htable */
165 	htab->table = NULL;
166 }
167 
168 /*
169  * hsearch()
170  */
171 
172 /*
173  * This is the search function. It uses double hashing with open addressing.
174  * The argument item.key has to be a pointer to an zero terminated, most
175  * probably strings of chars. The function for generating a number of the
176  * strings is simple but fast. It can be replaced by a more complex function
177  * like ajw (see [Aho,Sethi,Ullman]) if the needs are shown.
178  *
179  * We use an trick to speed up the lookup. The table is created by hcreate
180  * with one more element available. This enables us to use the index zero
181  * special. This index will never be used because we store the first hash
182  * index in the field used where zero means not used. Every other value
183  * means used. The used field can be used as a first fast comparison for
184  * equality of the stored and the parameter value. This helps to prevent
185  * unnecessary expensive calls of strcmp.
186  *
187  * This implementation differs from the standard library version of
188  * this function in a number of ways:
189  *
190  * - While the standard version does not make any assumptions about
191  *   the type of the stored data objects at all, this implementation
192  *   works with NUL terminated strings only.
193  * - Instead of storing just pointers to the original objects, we
194  *   create local copies so the caller does not need to care about the
195  *   data any more.
196  * - The standard implementation does not provide a way to update an
197  *   existing entry.  This version will create a new entry or update an
198  *   existing one when both "action == ENTER" and "item.data != NULL".
199  * - Instead of returning 1 on success, we return the index into the
200  *   internal hash table, which is also guaranteed to be positive.
201  *   This allows us direct access to the found hash table slot for
202  *   example for functions like hdelete().
203  */
204 
205 int hmatch_r(const char *match, int last_idx, ENTRY ** retval,
206 	     struct hsearch_data *htab)
207 {
208 	unsigned int idx;
209 	size_t key_len = strlen(match);
210 
211 	for (idx = last_idx + 1; idx < htab->size; ++idx) {
212 		if (!htab->table[idx].used)
213 			continue;
214 		if (!strncmp(match, htab->table[idx].entry.key, key_len)) {
215 			*retval = &htab->table[idx].entry;
216 			return idx;
217 		}
218 	}
219 
220 	__set_errno(ESRCH);
221 	*retval = NULL;
222 	return 0;
223 }
224 
225 int hsearch_r(ENTRY item, ACTION action, ENTRY ** retval,
226 	      struct hsearch_data *htab)
227 {
228 	unsigned int hval;
229 	unsigned int count;
230 	unsigned int len = strlen(item.key);
231 	unsigned int idx;
232 
233 	/* Compute an value for the given string. Perhaps use a better method. */
234 	hval = len;
235 	count = len;
236 	while (count-- > 0) {
237 		hval <<= 4;
238 		hval += item.key[count];
239 	}
240 
241 	/*
242 	 * First hash function:
243 	 * simply take the modul but prevent zero.
244 	 */
245 	hval %= htab->size;
246 	if (hval == 0)
247 		++hval;
248 
249 	/* The first index tried. */
250 	idx = hval;
251 
252 	if (htab->table[idx].used) {
253 		/*
254 		 * Further action might be required according to the
255 		 * action value.
256 		 */
257 		unsigned hval2;
258 
259 		if (htab->table[idx].used == hval
260 		    && strcmp(item.key, htab->table[idx].entry.key) == 0) {
261 			/* Overwrite existing value? */
262 			if ((action == ENTER) && (item.data != NULL)) {
263 				free(htab->table[idx].entry.data);
264 				htab->table[idx].entry.data =
265 					strdup(item.data);
266 				if (!htab->table[idx].entry.data) {
267 					__set_errno(ENOMEM);
268 					*retval = NULL;
269 					return 0;
270 				}
271 			}
272 			/* return found entry */
273 			*retval = &htab->table[idx].entry;
274 			return idx;
275 		}
276 
277 		/*
278 		 * Second hash function:
279 		 * as suggested in [Knuth]
280 		 */
281 		hval2 = 1 + hval % (htab->size - 2);
282 
283 		do {
284 			/*
285 			 * Because SIZE is prime this guarantees to
286 			 * step through all available indices.
287 			 */
288 			if (idx <= hval2)
289 				idx = htab->size + idx - hval2;
290 			else
291 				idx -= hval2;
292 
293 			/*
294 			 * If we visited all entries leave the loop
295 			 * unsuccessfully.
296 			 */
297 			if (idx == hval)
298 				break;
299 
300 			/* If entry is found use it. */
301 			if ((htab->table[idx].used == hval)
302 			    && strcmp(item.key, htab->table[idx].entry.key) == 0) {
303 				/* Overwrite existing value? */
304 				if ((action == ENTER) && (item.data != NULL)) {
305 					free(htab->table[idx].entry.data);
306 					htab->table[idx].entry.data =
307 						strdup(item.data);
308 					if (!htab->table[idx].entry.data) {
309 						__set_errno(ENOMEM);
310 						*retval = NULL;
311 						return 0;
312 					}
313 				}
314 				/* return found entry */
315 				*retval = &htab->table[idx].entry;
316 				return idx;
317 			}
318 		}
319 		while (htab->table[idx].used);
320 	}
321 
322 	/* An empty bucket has been found. */
323 	if (action == ENTER) {
324 		/*
325 		 * If table is full and another entry should be
326 		 * entered return with error.
327 		 */
328 		if (htab->filled == htab->size) {
329 			__set_errno(ENOMEM);
330 			*retval = NULL;
331 			return 0;
332 		}
333 
334 		/*
335 		 * Create new entry;
336 		 * create copies of item.key and item.data
337 		 */
338 		htab->table[idx].used = hval;
339 		htab->table[idx].entry.key = strdup(item.key);
340 		htab->table[idx].entry.data = strdup(item.data);
341 		if (!htab->table[idx].entry.key ||
342 		    !htab->table[idx].entry.data) {
343 			__set_errno(ENOMEM);
344 			*retval = NULL;
345 			return 0;
346 		}
347 
348 		++htab->filled;
349 
350 		/* return new entry */
351 		*retval = &htab->table[idx].entry;
352 		return 1;
353 	}
354 
355 	__set_errno(ESRCH);
356 	*retval = NULL;
357 	return 0;
358 }
359 
360 
361 /*
362  * hdelete()
363  */
364 
365 /*
366  * The standard implementation of hsearch(3) does not provide any way
367  * to delete any entries from the hash table.  We extend the code to
368  * do that.
369  */
370 
371 int hdelete_r(const char *key, struct hsearch_data *htab)
372 {
373 	ENTRY e, *ep;
374 	int idx;
375 
376 	debug("hdelete: DELETE key \"%s\"\n", key);
377 
378 	e.key = (char *)key;
379 
380 	if ((idx = hsearch_r(e, FIND, &ep, htab)) == 0) {
381 		__set_errno(ESRCH);
382 		return 0;	/* not found */
383 	}
384 
385 	/* free used ENTRY */
386 	debug("hdelete: DELETING key \"%s\"\n", key);
387 
388 	free(ep->key);
389 	free(ep->data);
390 	htab->table[idx].used = 0;
391 
392 	--htab->filled;
393 
394 	return 1;
395 }
396 
397 /*
398  * hexport()
399  */
400 
401 /*
402  * Export the data stored in the hash table in linearized form.
403  *
404  * Entries are exported as "name=value" strings, separated by an
405  * arbitrary (non-NUL, of course) separator character. This allows to
406  * use this function both when formatting the U-Boot environment for
407  * external storage (using '\0' as separator), but also when using it
408  * for the "printenv" command to print all variables, simply by using
409  * as '\n" as separator. This can also be used for new features like
410  * exporting the environment data as text file, including the option
411  * for later re-import.
412  *
413  * The entries in the result list will be sorted by ascending key
414  * values.
415  *
416  * If the separator character is different from NUL, then any
417  * separator characters and backslash characters in the values will
418  * be escaped by a preceeding backslash in output. This is needed for
419  * example to enable multi-line values, especially when the output
420  * shall later be parsed (for example, for re-import).
421  *
422  * There are several options how the result buffer is handled:
423  *
424  * *resp  size
425  * -----------
426  *  NULL    0	A string of sufficient length will be allocated.
427  *  NULL   >0	A string of the size given will be
428  *		allocated. An error will be returned if the size is
429  *		not sufficient.  Any unused bytes in the string will
430  *		be '\0'-padded.
431  * !NULL    0	The user-supplied buffer will be used. No length
432  *		checking will be performed, i. e. it is assumed that
433  *		the buffer size will always be big enough. DANGEROUS.
434  * !NULL   >0	The user-supplied buffer will be used. An error will
435  *		be returned if the size is not sufficient.  Any unused
436  *		bytes in the string will be '\0'-padded.
437  */
438 
439 static int cmpkey(const void *p1, const void *p2)
440 {
441 	ENTRY *e1 = *(ENTRY **) p1;
442 	ENTRY *e2 = *(ENTRY **) p2;
443 
444 	return (strcmp(e1->key, e2->key));
445 }
446 
447 ssize_t hexport_r(struct hsearch_data *htab, const char sep,
448 		 char **resp, size_t size)
449 {
450 	ENTRY *list[htab->size];
451 	char *res, *p;
452 	size_t totlen;
453 	int i, n;
454 
455 	/* Test for correct arguments.  */
456 	if ((resp == NULL) || (htab == NULL)) {
457 		__set_errno(EINVAL);
458 		return (-1);
459 	}
460 
461 	debug("EXPORT  table = %p, htab.size = %d, htab.filled = %d, size = %d\n",
462 		htab, htab->size, htab->filled, size);
463 	/*
464 	 * Pass 1:
465 	 * search used entries,
466 	 * save addresses and compute total length
467 	 */
468 	for (i = 1, n = 0, totlen = 0; i <= htab->size; ++i) {
469 
470 		if (htab->table[i].used) {
471 			ENTRY *ep = &htab->table[i].entry;
472 
473 			list[n++] = ep;
474 
475 			totlen += strlen(ep->key) + 2;
476 
477 			if (sep == '\0') {
478 				totlen += strlen(ep->data);
479 			} else {	/* check if escapes are needed */
480 				char *s = ep->data;
481 
482 				while (*s) {
483 					++totlen;
484 					/* add room for needed escape chars */
485 					if ((*s == sep) || (*s == '\\'))
486 						++totlen;
487 					++s;
488 				}
489 			}
490 			totlen += 2;	/* for '=' and 'sep' char */
491 		}
492 	}
493 
494 #ifdef DEBUG
495 	/* Pass 1a: print unsorted list */
496 	printf("Unsorted: n=%d\n", n);
497 	for (i = 0; i < n; ++i) {
498 		printf("\t%3d: %p ==> %-10s => %s\n",
499 		       i, list[i], list[i]->key, list[i]->data);
500 	}
501 #endif
502 
503 	/* Sort list by keys */
504 	qsort(list, n, sizeof(ENTRY *), cmpkey);
505 
506 	/* Check if the user supplied buffer size is sufficient */
507 	if (size) {
508 		if (size < totlen + 1) {	/* provided buffer too small */
509 			debug("### buffer too small: %d, but need %d\n",
510 				size, totlen + 1);
511 			__set_errno(ENOMEM);
512 			return (-1);
513 		}
514 	} else {
515 		size = totlen + 1;
516 	}
517 
518 	/* Check if the user provided a buffer */
519 	if (*resp) {
520 		/* yes; clear it */
521 		res = *resp;
522 		memset(res, '\0', size);
523 	} else {
524 		/* no, allocate and clear one */
525 		*resp = res = calloc(1, size);
526 		if (res == NULL) {
527 			__set_errno(ENOMEM);
528 			return (-1);
529 		}
530 	}
531 	/*
532 	 * Pass 2:
533 	 * export sorted list of result data
534 	 */
535 	for (i = 0, p = res; i < n; ++i) {
536 		char *s;
537 
538 		s = list[i]->key;
539 		while (*s)
540 			*p++ = *s++;
541 		*p++ = '=';
542 
543 		s = list[i]->data;
544 
545 		while (*s) {
546 			if ((*s == sep) || (*s == '\\'))
547 				*p++ = '\\';	/* escape */
548 			*p++ = *s++;
549 		}
550 		*p++ = sep;
551 	}
552 	*p = '\0';		/* terminate result */
553 
554 	return size;
555 }
556 
557 
558 /*
559  * himport()
560  */
561 
562 /*
563  * Import linearized data into hash table.
564  *
565  * This is the inverse function to hexport(): it takes a linear list
566  * of "name=value" pairs and creates hash table entries from it.
567  *
568  * Entries without "value", i. e. consisting of only "name" or
569  * "name=", will cause this entry to be deleted from the hash table.
570  *
571  * The "flag" argument can be used to control the behaviour: when the
572  * H_NOCLEAR bit is set, then an existing hash table will kept, i. e.
573  * new data will be added to an existing hash table; otherwise, old
574  * data will be discarded and a new hash table will be created.
575  *
576  * The separator character for the "name=value" pairs can be selected,
577  * so we both support importing from externally stored environment
578  * data (separated by NUL characters) and from plain text files
579  * (entries separated by newline characters).
580  *
581  * To allow for nicely formatted text input, leading white space
582  * (sequences of SPACE and TAB chars) is ignored, and entries starting
583  * (after removal of any leading white space) with a '#' character are
584  * considered comments and ignored.
585  *
586  * [NOTE: this means that a variable name cannot start with a '#'
587  * character.]
588  *
589  * When using a non-NUL separator character, backslash is used as
590  * escape character in the value part, allowing for example for
591  * multi-line values.
592  *
593  * In theory, arbitrary separator characters can be used, but only
594  * '\0' and '\n' have really been tested.
595  */
596 
597 int himport_r(struct hsearch_data *htab,
598 	      const char *env, size_t size, const char sep, int flag)
599 {
600 	char *data, *sp, *dp, *name, *value;
601 
602 	/* Test for correct arguments.  */
603 	if (htab == NULL) {
604 		__set_errno(EINVAL);
605 		return 0;
606 	}
607 
608 	/* we allocate new space to make sure we can write to the array */
609 	if ((data = malloc(size)) == NULL) {
610 		debug("himport_r: can't malloc %d bytes\n", size);
611 		__set_errno(ENOMEM);
612 		return 0;
613 	}
614 	memcpy(data, env, size);
615 	dp = data;
616 
617 	if ((flag & H_NOCLEAR) == 0) {
618 		/* Destroy old hash table if one exists */
619 		debug("Destroy Hash Table: %p table = %p\n", htab,
620 		       htab->table);
621 		if (htab->table)
622 			hdestroy_r(htab);
623 	}
624 
625 	/*
626 	 * Create new hash table (if needed).  The computation of the hash
627 	 * table size is based on heuristics: in a sample of some 70+
628 	 * existing systems we found an average size of 39+ bytes per entry
629 	 * in the environment (for the whole key=value pair). Assuming a
630 	 * size of 8 per entry (= safety factor of ~5) should provide enough
631 	 * safety margin for any existing environment definitions and still
632 	 * allow for more than enough dynamic additions. Note that the
633 	 * "size" argument is supposed to give the maximum enviroment size
634 	 * (CONFIG_ENV_SIZE).  This heuristics will result in
635 	 * unreasonably large numbers (and thus memory footprint) for
636 	 * big flash environments (>8,000 entries for 64 KB
637 	 * envrionment size), so we clip it to a reasonable value.
638 	 * On the other hand we need to add some more entries for free
639 	 * space when importing very small buffers. Both boundaries can
640 	 * be overwritten in the board config file if needed.
641 	 */
642 
643 	if (!htab->table) {
644 		int nent = CONFIG_ENV_MIN_ENTRIES + size / 8;
645 
646 		if (nent > CONFIG_ENV_MAX_ENTRIES)
647 			nent = CONFIG_ENV_MAX_ENTRIES;
648 
649 		debug("Create Hash Table: N=%d\n", nent);
650 
651 		if (hcreate_r(nent, htab) == 0) {
652 			free(data);
653 			return 0;
654 		}
655 	}
656 
657 	/* Parse environment; allow for '\0' and 'sep' as separators */
658 	do {
659 		ENTRY e, *rv;
660 
661 		/* skip leading white space */
662 		while ((*dp == ' ') || (*dp == '\t'))
663 			++dp;
664 
665 		/* skip comment lines */
666 		if (*dp == '#') {
667 			while (*dp && (*dp != sep))
668 				++dp;
669 			++dp;
670 			continue;
671 		}
672 
673 		/* parse name */
674 		for (name = dp; *dp != '=' && *dp && *dp != sep; ++dp)
675 			;
676 
677 		/* deal with "name" and "name=" entries (delete var) */
678 		if (*dp == '\0' || *(dp + 1) == '\0' ||
679 		    *dp == sep || *(dp + 1) == sep) {
680 			if (*dp == '=')
681 				*dp++ = '\0';
682 			*dp++ = '\0';	/* terminate name */
683 
684 			debug("DELETE CANDIDATE: \"%s\"\n", name);
685 
686 			if (hdelete_r(name, htab) == 0)
687 				debug("DELETE ERROR ##############################\n");
688 
689 			continue;
690 		}
691 		*dp++ = '\0';	/* terminate name */
692 
693 		/* parse value; deal with escapes */
694 		for (value = sp = dp; *dp && (*dp != sep); ++dp) {
695 			if ((*dp == '\\') && *(dp + 1))
696 				++dp;
697 			*sp++ = *dp;
698 		}
699 		*sp++ = '\0';	/* terminate value */
700 		++dp;
701 
702 		/* enter into hash table */
703 		e.key = name;
704 		e.data = value;
705 
706 		hsearch_r(e, ENTER, &rv, htab);
707 		if (rv == NULL) {
708 			printf("himport_r: can't insert \"%s=%s\" into hash table\n",
709 				name, value);
710 			return 0;
711 		}
712 
713 		debug("INSERT: table %p, filled %d/%d rv %p ==> name=\"%s\" value=\"%s\"\n",
714 			htab, htab->filled, htab->size,
715 			rv, name, value);
716 	} while ((dp < data + size) && *dp);	/* size check needed for text */
717 						/* without '\0' termination */
718 	debug("INSERT: free(data = %p)\n", data);
719 	free(data);
720 
721 	debug("INSERT: done\n");
722 	return 1;		/* everything OK */
723 }
724