xref: /OK3568_Linux_fs/u-boot/lib/hashtable.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * This implementation is based on code from uClibc-0.9.30.3 but was
3*4882a593Smuzhiyun  * modified and extended for use within U-Boot.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2010-2013 Wolfgang Denk <wd@denx.de>
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Original license header:
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * Copyright (C) 1993, 1995, 1996, 1997, 2002 Free Software Foundation, Inc.
10*4882a593Smuzhiyun  * This file is part of the GNU C Library.
11*4882a593Smuzhiyun  * Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1993.
12*4882a593Smuzhiyun  *
13*4882a593Smuzhiyun  * SPDX-License-Identifier:	LGPL-2.1+
14*4882a593Smuzhiyun  */
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun #include <errno.h>
17*4882a593Smuzhiyun #include <malloc.h>
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun #ifdef USE_HOSTCC		/* HOST build */
20*4882a593Smuzhiyun # include <string.h>
21*4882a593Smuzhiyun # include <assert.h>
22*4882a593Smuzhiyun # include <ctype.h>
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun # ifndef debug
25*4882a593Smuzhiyun #  ifdef DEBUG
26*4882a593Smuzhiyun #   define debug(fmt,args...)	printf(fmt ,##args)
27*4882a593Smuzhiyun #  else
28*4882a593Smuzhiyun #   define debug(fmt,args...)
29*4882a593Smuzhiyun #  endif
30*4882a593Smuzhiyun # endif
31*4882a593Smuzhiyun #else				/* U-Boot build */
32*4882a593Smuzhiyun # include <common.h>
33*4882a593Smuzhiyun # include <linux/string.h>
34*4882a593Smuzhiyun # include <linux/ctype.h>
35*4882a593Smuzhiyun #endif
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun #ifndef	CONFIG_ENV_MIN_ENTRIES	/* minimum number of entries */
38*4882a593Smuzhiyun #define	CONFIG_ENV_MIN_ENTRIES 64
39*4882a593Smuzhiyun #endif
40*4882a593Smuzhiyun #ifndef	CONFIG_ENV_MAX_ENTRIES	/* maximum number of entries */
41*4882a593Smuzhiyun #define	CONFIG_ENV_MAX_ENTRIES 512
42*4882a593Smuzhiyun #endif
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun #include <env_callback.h>
45*4882a593Smuzhiyun #include <env_flags.h>
46*4882a593Smuzhiyun #include <search.h>
47*4882a593Smuzhiyun #include <slre.h>
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun /*
50*4882a593Smuzhiyun  * [Aho,Sethi,Ullman] Compilers: Principles, Techniques and Tools, 1986
51*4882a593Smuzhiyun  * [Knuth]	      The Art of Computer Programming, part 3 (6.4)
52*4882a593Smuzhiyun  */
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun /*
55*4882a593Smuzhiyun  * The reentrant version has no static variables to maintain the state.
56*4882a593Smuzhiyun  * Instead the interface of all functions is extended to take an argument
57*4882a593Smuzhiyun  * which describes the current status.
58*4882a593Smuzhiyun  */
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun typedef struct _ENTRY {
61*4882a593Smuzhiyun 	int used;
62*4882a593Smuzhiyun 	ENTRY entry;
63*4882a593Smuzhiyun } _ENTRY;
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun static void _hdelete(const char *key, struct hsearch_data *htab, ENTRY *ep,
67*4882a593Smuzhiyun 	int idx);
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun /*
70*4882a593Smuzhiyun  * hcreate()
71*4882a593Smuzhiyun  */
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun /*
74*4882a593Smuzhiyun  * For the used double hash method the table size has to be a prime. To
75*4882a593Smuzhiyun  * correct the user given table size we need a prime test.  This trivial
76*4882a593Smuzhiyun  * algorithm is adequate because
77*4882a593Smuzhiyun  * a)  the code is (most probably) called a few times per program run and
78*4882a593Smuzhiyun  * b)  the number is small because the table must fit in the core
79*4882a593Smuzhiyun  * */
isprime(unsigned int number)80*4882a593Smuzhiyun static int isprime(unsigned int number)
81*4882a593Smuzhiyun {
82*4882a593Smuzhiyun 	/* no even number will be passed */
83*4882a593Smuzhiyun 	unsigned int div = 3;
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun 	while (div * div < number && number % div != 0)
86*4882a593Smuzhiyun 		div += 2;
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun 	return number % div != 0;
89*4882a593Smuzhiyun }
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun /*
92*4882a593Smuzhiyun  * Before using the hash table we must allocate memory for it.
93*4882a593Smuzhiyun  * Test for an existing table are done. We allocate one element
94*4882a593Smuzhiyun  * more as the found prime number says. This is done for more effective
95*4882a593Smuzhiyun  * indexing as explained in the comment for the hsearch function.
96*4882a593Smuzhiyun  * The contents of the table is zeroed, especially the field used
97*4882a593Smuzhiyun  * becomes zero.
98*4882a593Smuzhiyun  */
99*4882a593Smuzhiyun 
hcreate_r(size_t nel,struct hsearch_data * htab)100*4882a593Smuzhiyun int hcreate_r(size_t nel, struct hsearch_data *htab)
101*4882a593Smuzhiyun {
102*4882a593Smuzhiyun 	/* Test for correct arguments.  */
103*4882a593Smuzhiyun 	if (htab == NULL) {
104*4882a593Smuzhiyun 		__set_errno(EINVAL);
105*4882a593Smuzhiyun 		return 0;
106*4882a593Smuzhiyun 	}
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun 	/* There is still another table active. Return with error. */
109*4882a593Smuzhiyun 	if (htab->table != NULL)
110*4882a593Smuzhiyun 		return 0;
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun 	/* Change nel to the first prime number not smaller as nel. */
113*4882a593Smuzhiyun 	nel |= 1;		/* make odd */
114*4882a593Smuzhiyun 	while (!isprime(nel))
115*4882a593Smuzhiyun 		nel += 2;
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun 	htab->size = nel;
118*4882a593Smuzhiyun 	htab->filled = 0;
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun 	/* allocate memory and zero out */
121*4882a593Smuzhiyun 	htab->table = (_ENTRY *) calloc(htab->size + 1, sizeof(_ENTRY));
122*4882a593Smuzhiyun 	if (htab->table == NULL)
123*4882a593Smuzhiyun 		return 0;
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun 	/* everything went alright */
126*4882a593Smuzhiyun 	return 1;
127*4882a593Smuzhiyun }
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun /*
131*4882a593Smuzhiyun  * hdestroy()
132*4882a593Smuzhiyun  */
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun /*
135*4882a593Smuzhiyun  * After using the hash table it has to be destroyed. The used memory can
136*4882a593Smuzhiyun  * be freed and the local static variable can be marked as not used.
137*4882a593Smuzhiyun  */
138*4882a593Smuzhiyun 
hdestroy_r(struct hsearch_data * htab)139*4882a593Smuzhiyun void hdestroy_r(struct hsearch_data *htab)
140*4882a593Smuzhiyun {
141*4882a593Smuzhiyun 	int i;
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun 	/* Test for correct arguments.  */
144*4882a593Smuzhiyun 	if (htab == NULL) {
145*4882a593Smuzhiyun 		__set_errno(EINVAL);
146*4882a593Smuzhiyun 		return;
147*4882a593Smuzhiyun 	}
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun 	/* free used memory */
150*4882a593Smuzhiyun 	for (i = 1; i <= htab->size; ++i) {
151*4882a593Smuzhiyun 		if (htab->table[i].used > 0) {
152*4882a593Smuzhiyun 			ENTRY *ep = &htab->table[i].entry;
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun 			free((void *)ep->key);
155*4882a593Smuzhiyun 			free(ep->data);
156*4882a593Smuzhiyun 		}
157*4882a593Smuzhiyun 	}
158*4882a593Smuzhiyun 	free(htab->table);
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun 	/* the sign for an existing table is an value != NULL in htable */
161*4882a593Smuzhiyun 	htab->table = NULL;
162*4882a593Smuzhiyun }
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun /*
165*4882a593Smuzhiyun  * hsearch()
166*4882a593Smuzhiyun  */
167*4882a593Smuzhiyun 
168*4882a593Smuzhiyun /*
169*4882a593Smuzhiyun  * This is the search function. It uses double hashing with open addressing.
170*4882a593Smuzhiyun  * The argument item.key has to be a pointer to an zero terminated, most
171*4882a593Smuzhiyun  * probably strings of chars. The function for generating a number of the
172*4882a593Smuzhiyun  * strings is simple but fast. It can be replaced by a more complex function
173*4882a593Smuzhiyun  * like ajw (see [Aho,Sethi,Ullman]) if the needs are shown.
174*4882a593Smuzhiyun  *
175*4882a593Smuzhiyun  * We use an trick to speed up the lookup. The table is created by hcreate
176*4882a593Smuzhiyun  * with one more element available. This enables us to use the index zero
177*4882a593Smuzhiyun  * special. This index will never be used because we store the first hash
178*4882a593Smuzhiyun  * index in the field used where zero means not used. Every other value
179*4882a593Smuzhiyun  * means used. The used field can be used as a first fast comparison for
180*4882a593Smuzhiyun  * equality of the stored and the parameter value. This helps to prevent
181*4882a593Smuzhiyun  * unnecessary expensive calls of strcmp.
182*4882a593Smuzhiyun  *
183*4882a593Smuzhiyun  * This implementation differs from the standard library version of
184*4882a593Smuzhiyun  * this function in a number of ways:
185*4882a593Smuzhiyun  *
186*4882a593Smuzhiyun  * - While the standard version does not make any assumptions about
187*4882a593Smuzhiyun  *   the type of the stored data objects at all, this implementation
188*4882a593Smuzhiyun  *   works with NUL terminated strings only.
189*4882a593Smuzhiyun  * - Instead of storing just pointers to the original objects, we
190*4882a593Smuzhiyun  *   create local copies so the caller does not need to care about the
191*4882a593Smuzhiyun  *   data any more.
192*4882a593Smuzhiyun  * - The standard implementation does not provide a way to update an
193*4882a593Smuzhiyun  *   existing entry.  This version will create a new entry or update an
194*4882a593Smuzhiyun  *   existing one when both "action == ENTER" and "item.data != NULL".
195*4882a593Smuzhiyun  * - Instead of returning 1 on success, we return the index into the
196*4882a593Smuzhiyun  *   internal hash table, which is also guaranteed to be positive.
197*4882a593Smuzhiyun  *   This allows us direct access to the found hash table slot for
198*4882a593Smuzhiyun  *   example for functions like hdelete().
199*4882a593Smuzhiyun  */
200*4882a593Smuzhiyun 
hmatch_r(const char * match,int last_idx,ENTRY ** retval,struct hsearch_data * htab)201*4882a593Smuzhiyun int hmatch_r(const char *match, int last_idx, ENTRY ** retval,
202*4882a593Smuzhiyun 	     struct hsearch_data *htab)
203*4882a593Smuzhiyun {
204*4882a593Smuzhiyun 	unsigned int idx;
205*4882a593Smuzhiyun 	size_t key_len = strlen(match);
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun 	for (idx = last_idx + 1; idx < htab->size; ++idx) {
208*4882a593Smuzhiyun 		if (htab->table[idx].used <= 0)
209*4882a593Smuzhiyun 			continue;
210*4882a593Smuzhiyun 		if (!strncmp(match, htab->table[idx].entry.key, key_len)) {
211*4882a593Smuzhiyun 			*retval = &htab->table[idx].entry;
212*4882a593Smuzhiyun 			return idx;
213*4882a593Smuzhiyun 		}
214*4882a593Smuzhiyun 	}
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun 	__set_errno(ESRCH);
217*4882a593Smuzhiyun 	*retval = NULL;
218*4882a593Smuzhiyun 	return 0;
219*4882a593Smuzhiyun }
220*4882a593Smuzhiyun 
221*4882a593Smuzhiyun /*
222*4882a593Smuzhiyun  * Compare an existing entry with the desired key, and overwrite if the action
223*4882a593Smuzhiyun  * is ENTER.  This is simply a helper function for hsearch_r().
224*4882a593Smuzhiyun  */
_compare_and_overwrite_entry(ENTRY item,ACTION action,ENTRY ** retval,struct hsearch_data * htab,int flag,unsigned int hval,unsigned int idx)225*4882a593Smuzhiyun static inline int _compare_and_overwrite_entry(ENTRY item, ACTION action,
226*4882a593Smuzhiyun 	ENTRY **retval, struct hsearch_data *htab, int flag,
227*4882a593Smuzhiyun 	unsigned int hval, unsigned int idx)
228*4882a593Smuzhiyun {
229*4882a593Smuzhiyun 	if (htab->table[idx].used == hval
230*4882a593Smuzhiyun 	    && strcmp(item.key, htab->table[idx].entry.key) == 0) {
231*4882a593Smuzhiyun 		/* Overwrite existing value? */
232*4882a593Smuzhiyun 		if ((action == ENTER) && (item.data != NULL)) {
233*4882a593Smuzhiyun 			/* check for permission */
234*4882a593Smuzhiyun 			if (htab->change_ok != NULL && htab->change_ok(
235*4882a593Smuzhiyun 			    &htab->table[idx].entry, item.data,
236*4882a593Smuzhiyun 			    env_op_overwrite, flag)) {
237*4882a593Smuzhiyun 				debug("change_ok() rejected setting variable "
238*4882a593Smuzhiyun 					"%s, skipping it!\n", item.key);
239*4882a593Smuzhiyun 				__set_errno(EPERM);
240*4882a593Smuzhiyun 				*retval = NULL;
241*4882a593Smuzhiyun 				return 0;
242*4882a593Smuzhiyun 			}
243*4882a593Smuzhiyun 
244*4882a593Smuzhiyun 			/* If there is a callback, call it */
245*4882a593Smuzhiyun 			if (htab->table[idx].entry.callback &&
246*4882a593Smuzhiyun 			    htab->table[idx].entry.callback(item.key,
247*4882a593Smuzhiyun 			    item.data, env_op_overwrite, flag)) {
248*4882a593Smuzhiyun 				debug("callback() rejected setting variable "
249*4882a593Smuzhiyun 					"%s, skipping it!\n", item.key);
250*4882a593Smuzhiyun 				__set_errno(EINVAL);
251*4882a593Smuzhiyun 				*retval = NULL;
252*4882a593Smuzhiyun 				return 0;
253*4882a593Smuzhiyun 			}
254*4882a593Smuzhiyun 
255*4882a593Smuzhiyun 			free(htab->table[idx].entry.data);
256*4882a593Smuzhiyun 			htab->table[idx].entry.data = strdup(item.data);
257*4882a593Smuzhiyun 			if (!htab->table[idx].entry.data) {
258*4882a593Smuzhiyun 				__set_errno(ENOMEM);
259*4882a593Smuzhiyun 				*retval = NULL;
260*4882a593Smuzhiyun 				return 0;
261*4882a593Smuzhiyun 			}
262*4882a593Smuzhiyun 		}
263*4882a593Smuzhiyun 		/* return found entry */
264*4882a593Smuzhiyun 		*retval = &htab->table[idx].entry;
265*4882a593Smuzhiyun 		return idx;
266*4882a593Smuzhiyun 	}
267*4882a593Smuzhiyun 	/* keep searching */
268*4882a593Smuzhiyun 	return -1;
269*4882a593Smuzhiyun }
270*4882a593Smuzhiyun 
hsearch_r(ENTRY item,ACTION action,ENTRY ** retval,struct hsearch_data * htab,int flag)271*4882a593Smuzhiyun int hsearch_r(ENTRY item, ACTION action, ENTRY ** retval,
272*4882a593Smuzhiyun 	      struct hsearch_data *htab, int flag)
273*4882a593Smuzhiyun {
274*4882a593Smuzhiyun 	unsigned int hval;
275*4882a593Smuzhiyun 	unsigned int count;
276*4882a593Smuzhiyun 	unsigned int len = strlen(item.key);
277*4882a593Smuzhiyun 	unsigned int idx;
278*4882a593Smuzhiyun 	unsigned int first_deleted = 0;
279*4882a593Smuzhiyun 	int ret;
280*4882a593Smuzhiyun 
281*4882a593Smuzhiyun 	/* Compute an value for the given string. Perhaps use a better method. */
282*4882a593Smuzhiyun 	hval = len;
283*4882a593Smuzhiyun 	count = len;
284*4882a593Smuzhiyun 	while (count-- > 0) {
285*4882a593Smuzhiyun 		hval <<= 4;
286*4882a593Smuzhiyun 		hval += item.key[count];
287*4882a593Smuzhiyun 	}
288*4882a593Smuzhiyun 
289*4882a593Smuzhiyun 	/*
290*4882a593Smuzhiyun 	 * First hash function:
291*4882a593Smuzhiyun 	 * simply take the modul but prevent zero.
292*4882a593Smuzhiyun 	 */
293*4882a593Smuzhiyun 	hval %= htab->size;
294*4882a593Smuzhiyun 	if (hval == 0)
295*4882a593Smuzhiyun 		++hval;
296*4882a593Smuzhiyun 
297*4882a593Smuzhiyun 	/* The first index tried. */
298*4882a593Smuzhiyun 	idx = hval;
299*4882a593Smuzhiyun 
300*4882a593Smuzhiyun 	if (htab->table[idx].used) {
301*4882a593Smuzhiyun 		/*
302*4882a593Smuzhiyun 		 * Further action might be required according to the
303*4882a593Smuzhiyun 		 * action value.
304*4882a593Smuzhiyun 		 */
305*4882a593Smuzhiyun 		unsigned hval2;
306*4882a593Smuzhiyun 
307*4882a593Smuzhiyun 		if (htab->table[idx].used == -1
308*4882a593Smuzhiyun 		    && !first_deleted)
309*4882a593Smuzhiyun 			first_deleted = idx;
310*4882a593Smuzhiyun 
311*4882a593Smuzhiyun 		ret = _compare_and_overwrite_entry(item, action, retval, htab,
312*4882a593Smuzhiyun 			flag, hval, idx);
313*4882a593Smuzhiyun 		if (ret != -1)
314*4882a593Smuzhiyun 			return ret;
315*4882a593Smuzhiyun 
316*4882a593Smuzhiyun 		/*
317*4882a593Smuzhiyun 		 * Second hash function:
318*4882a593Smuzhiyun 		 * as suggested in [Knuth]
319*4882a593Smuzhiyun 		 */
320*4882a593Smuzhiyun 		hval2 = 1 + hval % (htab->size - 2);
321*4882a593Smuzhiyun 
322*4882a593Smuzhiyun 		do {
323*4882a593Smuzhiyun 			/*
324*4882a593Smuzhiyun 			 * Because SIZE is prime this guarantees to
325*4882a593Smuzhiyun 			 * step through all available indices.
326*4882a593Smuzhiyun 			 */
327*4882a593Smuzhiyun 			if (idx <= hval2)
328*4882a593Smuzhiyun 				idx = htab->size + idx - hval2;
329*4882a593Smuzhiyun 			else
330*4882a593Smuzhiyun 				idx -= hval2;
331*4882a593Smuzhiyun 
332*4882a593Smuzhiyun 			/*
333*4882a593Smuzhiyun 			 * If we visited all entries leave the loop
334*4882a593Smuzhiyun 			 * unsuccessfully.
335*4882a593Smuzhiyun 			 */
336*4882a593Smuzhiyun 			if (idx == hval)
337*4882a593Smuzhiyun 				break;
338*4882a593Smuzhiyun 
339*4882a593Smuzhiyun 			/* If entry is found use it. */
340*4882a593Smuzhiyun 			ret = _compare_and_overwrite_entry(item, action, retval,
341*4882a593Smuzhiyun 				htab, flag, hval, idx);
342*4882a593Smuzhiyun 			if (ret != -1)
343*4882a593Smuzhiyun 				return ret;
344*4882a593Smuzhiyun 		}
345*4882a593Smuzhiyun 		while (htab->table[idx].used);
346*4882a593Smuzhiyun 	}
347*4882a593Smuzhiyun 
348*4882a593Smuzhiyun 	/* An empty bucket has been found. */
349*4882a593Smuzhiyun 	if (action == ENTER) {
350*4882a593Smuzhiyun 		/*
351*4882a593Smuzhiyun 		 * If table is full and another entry should be
352*4882a593Smuzhiyun 		 * entered return with error.
353*4882a593Smuzhiyun 		 */
354*4882a593Smuzhiyun 		if (htab->filled == htab->size) {
355*4882a593Smuzhiyun 			__set_errno(ENOMEM);
356*4882a593Smuzhiyun 			*retval = NULL;
357*4882a593Smuzhiyun 			return 0;
358*4882a593Smuzhiyun 		}
359*4882a593Smuzhiyun 
360*4882a593Smuzhiyun 		/*
361*4882a593Smuzhiyun 		 * Create new entry;
362*4882a593Smuzhiyun 		 * create copies of item.key and item.data
363*4882a593Smuzhiyun 		 */
364*4882a593Smuzhiyun 		if (first_deleted)
365*4882a593Smuzhiyun 			idx = first_deleted;
366*4882a593Smuzhiyun 
367*4882a593Smuzhiyun 		htab->table[idx].used = hval;
368*4882a593Smuzhiyun 		htab->table[idx].entry.key = strdup(item.key);
369*4882a593Smuzhiyun 		htab->table[idx].entry.data = strdup(item.data);
370*4882a593Smuzhiyun 		if (!htab->table[idx].entry.key ||
371*4882a593Smuzhiyun 		    !htab->table[idx].entry.data) {
372*4882a593Smuzhiyun 			__set_errno(ENOMEM);
373*4882a593Smuzhiyun 			*retval = NULL;
374*4882a593Smuzhiyun 			return 0;
375*4882a593Smuzhiyun 		}
376*4882a593Smuzhiyun 
377*4882a593Smuzhiyun 		++htab->filled;
378*4882a593Smuzhiyun 
379*4882a593Smuzhiyun 		/* This is a new entry, so look up a possible callback */
380*4882a593Smuzhiyun 		env_callback_init(&htab->table[idx].entry);
381*4882a593Smuzhiyun 		/* Also look for flags */
382*4882a593Smuzhiyun 		env_flags_init(&htab->table[idx].entry);
383*4882a593Smuzhiyun 
384*4882a593Smuzhiyun 		/* check for permission */
385*4882a593Smuzhiyun 		if (htab->change_ok != NULL && htab->change_ok(
386*4882a593Smuzhiyun 		    &htab->table[idx].entry, item.data, env_op_create, flag)) {
387*4882a593Smuzhiyun 			debug("change_ok() rejected setting variable "
388*4882a593Smuzhiyun 				"%s, skipping it!\n", item.key);
389*4882a593Smuzhiyun 			_hdelete(item.key, htab, &htab->table[idx].entry, idx);
390*4882a593Smuzhiyun 			__set_errno(EPERM);
391*4882a593Smuzhiyun 			*retval = NULL;
392*4882a593Smuzhiyun 			return 0;
393*4882a593Smuzhiyun 		}
394*4882a593Smuzhiyun 
395*4882a593Smuzhiyun 		/* If there is a callback, call it */
396*4882a593Smuzhiyun 		if (htab->table[idx].entry.callback &&
397*4882a593Smuzhiyun 		    htab->table[idx].entry.callback(item.key, item.data,
398*4882a593Smuzhiyun 		    env_op_create, flag)) {
399*4882a593Smuzhiyun 			debug("callback() rejected setting variable "
400*4882a593Smuzhiyun 				"%s, skipping it!\n", item.key);
401*4882a593Smuzhiyun 			_hdelete(item.key, htab, &htab->table[idx].entry, idx);
402*4882a593Smuzhiyun 			__set_errno(EINVAL);
403*4882a593Smuzhiyun 			*retval = NULL;
404*4882a593Smuzhiyun 			return 0;
405*4882a593Smuzhiyun 		}
406*4882a593Smuzhiyun 
407*4882a593Smuzhiyun 		/* return new entry */
408*4882a593Smuzhiyun 		*retval = &htab->table[idx].entry;
409*4882a593Smuzhiyun 		return 1;
410*4882a593Smuzhiyun 	}
411*4882a593Smuzhiyun 
412*4882a593Smuzhiyun 	__set_errno(ESRCH);
413*4882a593Smuzhiyun 	*retval = NULL;
414*4882a593Smuzhiyun 	return 0;
415*4882a593Smuzhiyun }
416*4882a593Smuzhiyun 
417*4882a593Smuzhiyun 
418*4882a593Smuzhiyun /*
419*4882a593Smuzhiyun  * hdelete()
420*4882a593Smuzhiyun  */
421*4882a593Smuzhiyun 
422*4882a593Smuzhiyun /*
423*4882a593Smuzhiyun  * The standard implementation of hsearch(3) does not provide any way
424*4882a593Smuzhiyun  * to delete any entries from the hash table.  We extend the code to
425*4882a593Smuzhiyun  * do that.
426*4882a593Smuzhiyun  */
427*4882a593Smuzhiyun 
_hdelete(const char * key,struct hsearch_data * htab,ENTRY * ep,int idx)428*4882a593Smuzhiyun static void _hdelete(const char *key, struct hsearch_data *htab, ENTRY *ep,
429*4882a593Smuzhiyun 	int idx)
430*4882a593Smuzhiyun {
431*4882a593Smuzhiyun 	/* free used ENTRY */
432*4882a593Smuzhiyun 	debug("hdelete: DELETING key \"%s\"\n", key);
433*4882a593Smuzhiyun 	free((void *)ep->key);
434*4882a593Smuzhiyun 	free(ep->data);
435*4882a593Smuzhiyun 	ep->callback = NULL;
436*4882a593Smuzhiyun 	ep->flags = 0;
437*4882a593Smuzhiyun 	htab->table[idx].used = -1;
438*4882a593Smuzhiyun 
439*4882a593Smuzhiyun 	--htab->filled;
440*4882a593Smuzhiyun }
441*4882a593Smuzhiyun 
hdelete_r(const char * key,struct hsearch_data * htab,int flag)442*4882a593Smuzhiyun int hdelete_r(const char *key, struct hsearch_data *htab, int flag)
443*4882a593Smuzhiyun {
444*4882a593Smuzhiyun 	ENTRY e, *ep;
445*4882a593Smuzhiyun 	int idx;
446*4882a593Smuzhiyun 
447*4882a593Smuzhiyun 	debug("hdelete: DELETE key \"%s\"\n", key);
448*4882a593Smuzhiyun 
449*4882a593Smuzhiyun 	e.key = (char *)key;
450*4882a593Smuzhiyun 
451*4882a593Smuzhiyun 	idx = hsearch_r(e, FIND, &ep, htab, 0);
452*4882a593Smuzhiyun 	if (idx == 0) {
453*4882a593Smuzhiyun 		__set_errno(ESRCH);
454*4882a593Smuzhiyun 		return 0;	/* not found */
455*4882a593Smuzhiyun 	}
456*4882a593Smuzhiyun 
457*4882a593Smuzhiyun 	/* Check for permission */
458*4882a593Smuzhiyun 	if (htab->change_ok != NULL &&
459*4882a593Smuzhiyun 	    htab->change_ok(ep, NULL, env_op_delete, flag)) {
460*4882a593Smuzhiyun 		debug("change_ok() rejected deleting variable "
461*4882a593Smuzhiyun 			"%s, skipping it!\n", key);
462*4882a593Smuzhiyun 		__set_errno(EPERM);
463*4882a593Smuzhiyun 		return 0;
464*4882a593Smuzhiyun 	}
465*4882a593Smuzhiyun 
466*4882a593Smuzhiyun 	/* If there is a callback, call it */
467*4882a593Smuzhiyun 	if (htab->table[idx].entry.callback &&
468*4882a593Smuzhiyun 	    htab->table[idx].entry.callback(key, NULL, env_op_delete, flag)) {
469*4882a593Smuzhiyun 		debug("callback() rejected deleting variable "
470*4882a593Smuzhiyun 			"%s, skipping it!\n", key);
471*4882a593Smuzhiyun 		__set_errno(EINVAL);
472*4882a593Smuzhiyun 		return 0;
473*4882a593Smuzhiyun 	}
474*4882a593Smuzhiyun 
475*4882a593Smuzhiyun 	_hdelete(key, htab, ep, idx);
476*4882a593Smuzhiyun 
477*4882a593Smuzhiyun 	return 1;
478*4882a593Smuzhiyun }
479*4882a593Smuzhiyun 
480*4882a593Smuzhiyun #if !(defined(CONFIG_SPL_BUILD) && !defined(CONFIG_SPL_SAVEENV))
481*4882a593Smuzhiyun /*
482*4882a593Smuzhiyun  * hexport()
483*4882a593Smuzhiyun  */
484*4882a593Smuzhiyun 
485*4882a593Smuzhiyun /*
486*4882a593Smuzhiyun  * Export the data stored in the hash table in linearized form.
487*4882a593Smuzhiyun  *
488*4882a593Smuzhiyun  * Entries are exported as "name=value" strings, separated by an
489*4882a593Smuzhiyun  * arbitrary (non-NUL, of course) separator character. This allows to
490*4882a593Smuzhiyun  * use this function both when formatting the U-Boot environment for
491*4882a593Smuzhiyun  * external storage (using '\0' as separator), but also when using it
492*4882a593Smuzhiyun  * for the "printenv" command to print all variables, simply by using
493*4882a593Smuzhiyun  * as '\n" as separator. This can also be used for new features like
494*4882a593Smuzhiyun  * exporting the environment data as text file, including the option
495*4882a593Smuzhiyun  * for later re-import.
496*4882a593Smuzhiyun  *
497*4882a593Smuzhiyun  * The entries in the result list will be sorted by ascending key
498*4882a593Smuzhiyun  * values.
499*4882a593Smuzhiyun  *
500*4882a593Smuzhiyun  * If the separator character is different from NUL, then any
501*4882a593Smuzhiyun  * separator characters and backslash characters in the values will
502*4882a593Smuzhiyun  * be escaped by a preceding backslash in output. This is needed for
503*4882a593Smuzhiyun  * example to enable multi-line values, especially when the output
504*4882a593Smuzhiyun  * shall later be parsed (for example, for re-import).
505*4882a593Smuzhiyun  *
506*4882a593Smuzhiyun  * There are several options how the result buffer is handled:
507*4882a593Smuzhiyun  *
508*4882a593Smuzhiyun  * *resp  size
509*4882a593Smuzhiyun  * -----------
510*4882a593Smuzhiyun  *  NULL    0	A string of sufficient length will be allocated.
511*4882a593Smuzhiyun  *  NULL   >0	A string of the size given will be
512*4882a593Smuzhiyun  *		allocated. An error will be returned if the size is
513*4882a593Smuzhiyun  *		not sufficient.  Any unused bytes in the string will
514*4882a593Smuzhiyun  *		be '\0'-padded.
515*4882a593Smuzhiyun  * !NULL    0	The user-supplied buffer will be used. No length
516*4882a593Smuzhiyun  *		checking will be performed, i. e. it is assumed that
517*4882a593Smuzhiyun  *		the buffer size will always be big enough. DANGEROUS.
518*4882a593Smuzhiyun  * !NULL   >0	The user-supplied buffer will be used. An error will
519*4882a593Smuzhiyun  *		be returned if the size is not sufficient.  Any unused
520*4882a593Smuzhiyun  *		bytes in the string will be '\0'-padded.
521*4882a593Smuzhiyun  */
522*4882a593Smuzhiyun 
cmpkey(const void * p1,const void * p2)523*4882a593Smuzhiyun static int cmpkey(const void *p1, const void *p2)
524*4882a593Smuzhiyun {
525*4882a593Smuzhiyun 	ENTRY *e1 = *(ENTRY **) p1;
526*4882a593Smuzhiyun 	ENTRY *e2 = *(ENTRY **) p2;
527*4882a593Smuzhiyun 
528*4882a593Smuzhiyun 	return (strcmp(e1->key, e2->key));
529*4882a593Smuzhiyun }
530*4882a593Smuzhiyun 
match_string(int flag,const char * str,const char * pat,void * priv)531*4882a593Smuzhiyun static int match_string(int flag, const char *str, const char *pat, void *priv)
532*4882a593Smuzhiyun {
533*4882a593Smuzhiyun 	switch (flag & H_MATCH_METHOD) {
534*4882a593Smuzhiyun 	case H_MATCH_IDENT:
535*4882a593Smuzhiyun 		if (strcmp(str, pat) == 0)
536*4882a593Smuzhiyun 			return 1;
537*4882a593Smuzhiyun 		break;
538*4882a593Smuzhiyun 	case H_MATCH_SUBSTR:
539*4882a593Smuzhiyun 		if (strstr(str, pat))
540*4882a593Smuzhiyun 			return 1;
541*4882a593Smuzhiyun 		break;
542*4882a593Smuzhiyun #ifdef CONFIG_REGEX
543*4882a593Smuzhiyun 	case H_MATCH_REGEX:
544*4882a593Smuzhiyun 		{
545*4882a593Smuzhiyun 			struct slre *slrep = (struct slre *)priv;
546*4882a593Smuzhiyun 			struct cap caps[slrep->num_caps + 2];
547*4882a593Smuzhiyun 
548*4882a593Smuzhiyun 			if (slre_match(slrep, str, strlen(str), caps))
549*4882a593Smuzhiyun 				return 1;
550*4882a593Smuzhiyun 		}
551*4882a593Smuzhiyun 		break;
552*4882a593Smuzhiyun #endif
553*4882a593Smuzhiyun 	default:
554*4882a593Smuzhiyun 		printf("## ERROR: unsupported match method: 0x%02x\n",
555*4882a593Smuzhiyun 			flag & H_MATCH_METHOD);
556*4882a593Smuzhiyun 		break;
557*4882a593Smuzhiyun 	}
558*4882a593Smuzhiyun 	return 0;
559*4882a593Smuzhiyun }
560*4882a593Smuzhiyun 
match_entry(ENTRY * ep,int flag,int argc,char * const argv[])561*4882a593Smuzhiyun static int match_entry(ENTRY *ep, int flag,
562*4882a593Smuzhiyun 		 int argc, char * const argv[])
563*4882a593Smuzhiyun {
564*4882a593Smuzhiyun 	int arg;
565*4882a593Smuzhiyun 	void *priv = NULL;
566*4882a593Smuzhiyun 
567*4882a593Smuzhiyun 	for (arg = 0; arg < argc; ++arg) {
568*4882a593Smuzhiyun #ifdef CONFIG_REGEX
569*4882a593Smuzhiyun 		struct slre slre;
570*4882a593Smuzhiyun 
571*4882a593Smuzhiyun 		if (slre_compile(&slre, argv[arg]) == 0) {
572*4882a593Smuzhiyun 			printf("Error compiling regex: %s\n", slre.err_str);
573*4882a593Smuzhiyun 			return 0;
574*4882a593Smuzhiyun 		}
575*4882a593Smuzhiyun 
576*4882a593Smuzhiyun 		priv = (void *)&slre;
577*4882a593Smuzhiyun #endif
578*4882a593Smuzhiyun 		if (flag & H_MATCH_KEY) {
579*4882a593Smuzhiyun 			if (match_string(flag, ep->key, argv[arg], priv))
580*4882a593Smuzhiyun 				return 1;
581*4882a593Smuzhiyun 		}
582*4882a593Smuzhiyun 		if (flag & H_MATCH_DATA) {
583*4882a593Smuzhiyun 			if (match_string(flag, ep->data, argv[arg], priv))
584*4882a593Smuzhiyun 				return 1;
585*4882a593Smuzhiyun 		}
586*4882a593Smuzhiyun 	}
587*4882a593Smuzhiyun 	return 0;
588*4882a593Smuzhiyun }
589*4882a593Smuzhiyun 
hexport_r(struct hsearch_data * htab,const char sep,int flag,char ** resp,size_t size,int argc,char * const argv[])590*4882a593Smuzhiyun ssize_t hexport_r(struct hsearch_data *htab, const char sep, int flag,
591*4882a593Smuzhiyun 		 char **resp, size_t size,
592*4882a593Smuzhiyun 		 int argc, char * const argv[])
593*4882a593Smuzhiyun {
594*4882a593Smuzhiyun 	ENTRY *list[htab->size];
595*4882a593Smuzhiyun 	char *res, *p;
596*4882a593Smuzhiyun 	size_t totlen;
597*4882a593Smuzhiyun 	int i, n;
598*4882a593Smuzhiyun 
599*4882a593Smuzhiyun 	/* Test for correct arguments.  */
600*4882a593Smuzhiyun 	if ((resp == NULL) || (htab == NULL)) {
601*4882a593Smuzhiyun 		__set_errno(EINVAL);
602*4882a593Smuzhiyun 		return (-1);
603*4882a593Smuzhiyun 	}
604*4882a593Smuzhiyun 
605*4882a593Smuzhiyun 	debug("EXPORT  table = %p, htab.size = %d, htab.filled = %d, size = %lu\n",
606*4882a593Smuzhiyun 	      htab, htab->size, htab->filled, (ulong)size);
607*4882a593Smuzhiyun 	/*
608*4882a593Smuzhiyun 	 * Pass 1:
609*4882a593Smuzhiyun 	 * search used entries,
610*4882a593Smuzhiyun 	 * save addresses and compute total length
611*4882a593Smuzhiyun 	 */
612*4882a593Smuzhiyun 	for (i = 1, n = 0, totlen = 0; i <= htab->size; ++i) {
613*4882a593Smuzhiyun 
614*4882a593Smuzhiyun 		if (htab->table[i].used > 0) {
615*4882a593Smuzhiyun 			ENTRY *ep = &htab->table[i].entry;
616*4882a593Smuzhiyun 			int found = match_entry(ep, flag, argc, argv);
617*4882a593Smuzhiyun 
618*4882a593Smuzhiyun 			if ((argc > 0) && (found == 0))
619*4882a593Smuzhiyun 				continue;
620*4882a593Smuzhiyun 
621*4882a593Smuzhiyun 			if ((flag & H_HIDE_DOT) && ep->key[0] == '.')
622*4882a593Smuzhiyun 				continue;
623*4882a593Smuzhiyun 
624*4882a593Smuzhiyun 			list[n++] = ep;
625*4882a593Smuzhiyun 
626*4882a593Smuzhiyun 			totlen += strlen(ep->key) + 2;
627*4882a593Smuzhiyun 
628*4882a593Smuzhiyun 			if (sep == '\0') {
629*4882a593Smuzhiyun 				totlen += strlen(ep->data);
630*4882a593Smuzhiyun 			} else {	/* check if escapes are needed */
631*4882a593Smuzhiyun 				char *s = ep->data;
632*4882a593Smuzhiyun 
633*4882a593Smuzhiyun 				while (*s) {
634*4882a593Smuzhiyun 					++totlen;
635*4882a593Smuzhiyun 					/* add room for needed escape chars */
636*4882a593Smuzhiyun 					if ((*s == sep) || (*s == '\\'))
637*4882a593Smuzhiyun 						++totlen;
638*4882a593Smuzhiyun 					++s;
639*4882a593Smuzhiyun 				}
640*4882a593Smuzhiyun 			}
641*4882a593Smuzhiyun 			totlen += 2;	/* for '=' and 'sep' char */
642*4882a593Smuzhiyun 		}
643*4882a593Smuzhiyun 	}
644*4882a593Smuzhiyun 
645*4882a593Smuzhiyun #ifdef DEBUG
646*4882a593Smuzhiyun 	/* Pass 1a: print unsorted list */
647*4882a593Smuzhiyun 	printf("Unsorted: n=%d\n", n);
648*4882a593Smuzhiyun 	for (i = 0; i < n; ++i) {
649*4882a593Smuzhiyun 		printf("\t%3d: %p ==> %-10s => %s\n",
650*4882a593Smuzhiyun 		       i, list[i], list[i]->key, list[i]->data);
651*4882a593Smuzhiyun 	}
652*4882a593Smuzhiyun #endif
653*4882a593Smuzhiyun 
654*4882a593Smuzhiyun 	/* Sort list by keys */
655*4882a593Smuzhiyun 	qsort(list, n, sizeof(ENTRY *), cmpkey);
656*4882a593Smuzhiyun 
657*4882a593Smuzhiyun 	/* Check if the user supplied buffer size is sufficient */
658*4882a593Smuzhiyun 	if (size) {
659*4882a593Smuzhiyun 		if (size < totlen + 1) {	/* provided buffer too small */
660*4882a593Smuzhiyun 			printf("Env export buffer too small: %lu, but need %lu\n",
661*4882a593Smuzhiyun 			       (ulong)size, (ulong)totlen + 1);
662*4882a593Smuzhiyun 			__set_errno(ENOMEM);
663*4882a593Smuzhiyun 			return (-1);
664*4882a593Smuzhiyun 		}
665*4882a593Smuzhiyun 	} else {
666*4882a593Smuzhiyun 		size = totlen + 1;
667*4882a593Smuzhiyun 	}
668*4882a593Smuzhiyun 
669*4882a593Smuzhiyun 	/* Check if the user provided a buffer */
670*4882a593Smuzhiyun 	if (*resp) {
671*4882a593Smuzhiyun 		/* yes; clear it */
672*4882a593Smuzhiyun 		res = *resp;
673*4882a593Smuzhiyun 		memset(res, '\0', size);
674*4882a593Smuzhiyun 	} else {
675*4882a593Smuzhiyun 		/* no, allocate and clear one */
676*4882a593Smuzhiyun 		*resp = res = calloc(1, size);
677*4882a593Smuzhiyun 		if (res == NULL) {
678*4882a593Smuzhiyun 			__set_errno(ENOMEM);
679*4882a593Smuzhiyun 			return (-1);
680*4882a593Smuzhiyun 		}
681*4882a593Smuzhiyun 	}
682*4882a593Smuzhiyun 	/*
683*4882a593Smuzhiyun 	 * Pass 2:
684*4882a593Smuzhiyun 	 * export sorted list of result data
685*4882a593Smuzhiyun 	 */
686*4882a593Smuzhiyun 	for (i = 0, p = res; i < n; ++i) {
687*4882a593Smuzhiyun 		const char *s;
688*4882a593Smuzhiyun 
689*4882a593Smuzhiyun 		s = list[i]->key;
690*4882a593Smuzhiyun 		while (*s)
691*4882a593Smuzhiyun 			*p++ = *s++;
692*4882a593Smuzhiyun 		*p++ = '=';
693*4882a593Smuzhiyun 
694*4882a593Smuzhiyun 		s = list[i]->data;
695*4882a593Smuzhiyun 
696*4882a593Smuzhiyun 		while (*s) {
697*4882a593Smuzhiyun 			if ((*s == sep) || (*s == '\\'))
698*4882a593Smuzhiyun 				*p++ = '\\';	/* escape */
699*4882a593Smuzhiyun 			*p++ = *s++;
700*4882a593Smuzhiyun 		}
701*4882a593Smuzhiyun 		*p++ = sep;
702*4882a593Smuzhiyun 	}
703*4882a593Smuzhiyun 	*p = '\0';		/* terminate result */
704*4882a593Smuzhiyun 
705*4882a593Smuzhiyun 	return size;
706*4882a593Smuzhiyun }
707*4882a593Smuzhiyun #endif
708*4882a593Smuzhiyun 
709*4882a593Smuzhiyun 
710*4882a593Smuzhiyun /*
711*4882a593Smuzhiyun  * himport()
712*4882a593Smuzhiyun  */
713*4882a593Smuzhiyun 
714*4882a593Smuzhiyun /*
715*4882a593Smuzhiyun  * Check whether variable 'name' is amongst vars[],
716*4882a593Smuzhiyun  * and remove all instances by setting the pointer to NULL
717*4882a593Smuzhiyun  */
drop_var_from_set(const char * name,int nvars,char * vars[])718*4882a593Smuzhiyun static int drop_var_from_set(const char *name, int nvars, char * vars[])
719*4882a593Smuzhiyun {
720*4882a593Smuzhiyun 	int i = 0;
721*4882a593Smuzhiyun 	int res = 0;
722*4882a593Smuzhiyun 
723*4882a593Smuzhiyun 	/* No variables specified means process all of them */
724*4882a593Smuzhiyun 	if (nvars == 0)
725*4882a593Smuzhiyun 		return 1;
726*4882a593Smuzhiyun 
727*4882a593Smuzhiyun 	for (i = 0; i < nvars; i++) {
728*4882a593Smuzhiyun 		if (vars[i] == NULL)
729*4882a593Smuzhiyun 			continue;
730*4882a593Smuzhiyun 		/* If we found it, delete all of them */
731*4882a593Smuzhiyun 		if (!strcmp(name, vars[i])) {
732*4882a593Smuzhiyun 			vars[i] = NULL;
733*4882a593Smuzhiyun 			res = 1;
734*4882a593Smuzhiyun 		}
735*4882a593Smuzhiyun 	}
736*4882a593Smuzhiyun 	if (!res)
737*4882a593Smuzhiyun 		debug("Skipping non-listed variable %s\n", name);
738*4882a593Smuzhiyun 
739*4882a593Smuzhiyun 	return res;
740*4882a593Smuzhiyun }
741*4882a593Smuzhiyun 
742*4882a593Smuzhiyun /*
743*4882a593Smuzhiyun  * Import linearized data into hash table.
744*4882a593Smuzhiyun  *
745*4882a593Smuzhiyun  * This is the inverse function to hexport(): it takes a linear list
746*4882a593Smuzhiyun  * of "name=value" pairs and creates hash table entries from it.
747*4882a593Smuzhiyun  *
748*4882a593Smuzhiyun  * Entries without "value", i. e. consisting of only "name" or
749*4882a593Smuzhiyun  * "name=", will cause this entry to be deleted from the hash table.
750*4882a593Smuzhiyun  *
751*4882a593Smuzhiyun  * The "flag" argument can be used to control the behaviour: when the
752*4882a593Smuzhiyun  * H_NOCLEAR bit is set, then an existing hash table will kept, i. e.
753*4882a593Smuzhiyun  * new data will be added to an existing hash table; otherwise, old
754*4882a593Smuzhiyun  * data will be discarded and a new hash table will be created.
755*4882a593Smuzhiyun  *
756*4882a593Smuzhiyun  * The separator character for the "name=value" pairs can be selected,
757*4882a593Smuzhiyun  * so we both support importing from externally stored environment
758*4882a593Smuzhiyun  * data (separated by NUL characters) and from plain text files
759*4882a593Smuzhiyun  * (entries separated by newline characters).
760*4882a593Smuzhiyun  *
761*4882a593Smuzhiyun  * To allow for nicely formatted text input, leading white space
762*4882a593Smuzhiyun  * (sequences of SPACE and TAB chars) is ignored, and entries starting
763*4882a593Smuzhiyun  * (after removal of any leading white space) with a '#' character are
764*4882a593Smuzhiyun  * considered comments and ignored.
765*4882a593Smuzhiyun  *
766*4882a593Smuzhiyun  * [NOTE: this means that a variable name cannot start with a '#'
767*4882a593Smuzhiyun  * character.]
768*4882a593Smuzhiyun  *
769*4882a593Smuzhiyun  * When using a non-NUL separator character, backslash is used as
770*4882a593Smuzhiyun  * escape character in the value part, allowing for example for
771*4882a593Smuzhiyun  * multi-line values.
772*4882a593Smuzhiyun  *
773*4882a593Smuzhiyun  * In theory, arbitrary separator characters can be used, but only
774*4882a593Smuzhiyun  * '\0' and '\n' have really been tested.
775*4882a593Smuzhiyun  */
776*4882a593Smuzhiyun 
himport_r(struct hsearch_data * htab,const char * env,size_t size,const char sep,int flag,int crlf_is_lf,int nvars,char * const vars[])777*4882a593Smuzhiyun int himport_r(struct hsearch_data *htab,
778*4882a593Smuzhiyun 		const char *env, size_t size, const char sep, int flag,
779*4882a593Smuzhiyun 		int crlf_is_lf, int nvars, char * const vars[])
780*4882a593Smuzhiyun {
781*4882a593Smuzhiyun 	char *data, *sp, *dp, *name, *value;
782*4882a593Smuzhiyun 	char *localvars[nvars];
783*4882a593Smuzhiyun 
784*4882a593Smuzhiyun 	/* Test for correct arguments.  */
785*4882a593Smuzhiyun 	if (htab == NULL) {
786*4882a593Smuzhiyun 		__set_errno(EINVAL);
787*4882a593Smuzhiyun 		return 0;
788*4882a593Smuzhiyun 	}
789*4882a593Smuzhiyun 
790*4882a593Smuzhiyun 	/* we allocate new space to make sure we can write to the array */
791*4882a593Smuzhiyun 	if ((data = malloc(size + 1)) == NULL) {
792*4882a593Smuzhiyun 		debug("himport_r: can't malloc %lu bytes\n", (ulong)size + 1);
793*4882a593Smuzhiyun 		__set_errno(ENOMEM);
794*4882a593Smuzhiyun 		return 0;
795*4882a593Smuzhiyun 	}
796*4882a593Smuzhiyun 	memcpy(data, env, size);
797*4882a593Smuzhiyun 	data[size] = '\0';
798*4882a593Smuzhiyun 	dp = data;
799*4882a593Smuzhiyun 
800*4882a593Smuzhiyun 	/* make a local copy of the list of variables */
801*4882a593Smuzhiyun 	if (nvars)
802*4882a593Smuzhiyun 		memcpy(localvars, vars, sizeof(vars[0]) * nvars);
803*4882a593Smuzhiyun 
804*4882a593Smuzhiyun 	if ((flag & H_NOCLEAR) == 0) {
805*4882a593Smuzhiyun 		/* Destroy old hash table if one exists */
806*4882a593Smuzhiyun 		debug("Destroy Hash Table: %p table = %p\n", htab,
807*4882a593Smuzhiyun 		       htab->table);
808*4882a593Smuzhiyun 		if (htab->table)
809*4882a593Smuzhiyun 			hdestroy_r(htab);
810*4882a593Smuzhiyun 	}
811*4882a593Smuzhiyun 
812*4882a593Smuzhiyun 	/*
813*4882a593Smuzhiyun 	 * Create new hash table (if needed).  The computation of the hash
814*4882a593Smuzhiyun 	 * table size is based on heuristics: in a sample of some 70+
815*4882a593Smuzhiyun 	 * existing systems we found an average size of 39+ bytes per entry
816*4882a593Smuzhiyun 	 * in the environment (for the whole key=value pair). Assuming a
817*4882a593Smuzhiyun 	 * size of 8 per entry (= safety factor of ~5) should provide enough
818*4882a593Smuzhiyun 	 * safety margin for any existing environment definitions and still
819*4882a593Smuzhiyun 	 * allow for more than enough dynamic additions. Note that the
820*4882a593Smuzhiyun 	 * "size" argument is supposed to give the maximum environment size
821*4882a593Smuzhiyun 	 * (CONFIG_ENV_SIZE).  This heuristics will result in
822*4882a593Smuzhiyun 	 * unreasonably large numbers (and thus memory footprint) for
823*4882a593Smuzhiyun 	 * big flash environments (>8,000 entries for 64 KB
824*4882a593Smuzhiyun 	 * environment size), so we clip it to a reasonable value.
825*4882a593Smuzhiyun 	 * On the other hand we need to add some more entries for free
826*4882a593Smuzhiyun 	 * space when importing very small buffers. Both boundaries can
827*4882a593Smuzhiyun 	 * be overwritten in the board config file if needed.
828*4882a593Smuzhiyun 	 */
829*4882a593Smuzhiyun 
830*4882a593Smuzhiyun 	if (!htab->table) {
831*4882a593Smuzhiyun 		int nent = CONFIG_ENV_MIN_ENTRIES + size / 8;
832*4882a593Smuzhiyun 
833*4882a593Smuzhiyun 		if (nent > CONFIG_ENV_MAX_ENTRIES)
834*4882a593Smuzhiyun 			nent = CONFIG_ENV_MAX_ENTRIES;
835*4882a593Smuzhiyun 
836*4882a593Smuzhiyun 		debug("Create Hash Table: N=%d\n", nent);
837*4882a593Smuzhiyun 
838*4882a593Smuzhiyun 		if (hcreate_r(nent, htab) == 0) {
839*4882a593Smuzhiyun 			free(data);
840*4882a593Smuzhiyun 			return 0;
841*4882a593Smuzhiyun 		}
842*4882a593Smuzhiyun 	}
843*4882a593Smuzhiyun 
844*4882a593Smuzhiyun 	if (!size) {
845*4882a593Smuzhiyun 		free(data);
846*4882a593Smuzhiyun 		return 1;		/* everything OK */
847*4882a593Smuzhiyun 	}
848*4882a593Smuzhiyun 	if(crlf_is_lf) {
849*4882a593Smuzhiyun 		/* Remove Carriage Returns in front of Line Feeds */
850*4882a593Smuzhiyun 		unsigned ignored_crs = 0;
851*4882a593Smuzhiyun 		for(;dp < data + size && *dp; ++dp) {
852*4882a593Smuzhiyun 			if(*dp == '\r' &&
853*4882a593Smuzhiyun 			   dp < data + size - 1 && *(dp+1) == '\n')
854*4882a593Smuzhiyun 				++ignored_crs;
855*4882a593Smuzhiyun 			else
856*4882a593Smuzhiyun 				*(dp-ignored_crs) = *dp;
857*4882a593Smuzhiyun 		}
858*4882a593Smuzhiyun 		size -= ignored_crs;
859*4882a593Smuzhiyun 		dp = data;
860*4882a593Smuzhiyun 	}
861*4882a593Smuzhiyun 	/* Parse environment; allow for '\0' and 'sep' as separators */
862*4882a593Smuzhiyun 	do {
863*4882a593Smuzhiyun 		ENTRY e, *rv;
864*4882a593Smuzhiyun 
865*4882a593Smuzhiyun 		/* skip leading white space */
866*4882a593Smuzhiyun 		while (isblank(*dp))
867*4882a593Smuzhiyun 			++dp;
868*4882a593Smuzhiyun 
869*4882a593Smuzhiyun 		/* skip comment lines */
870*4882a593Smuzhiyun 		if (*dp == '#') {
871*4882a593Smuzhiyun 			while (*dp && (*dp != sep))
872*4882a593Smuzhiyun 				++dp;
873*4882a593Smuzhiyun 			++dp;
874*4882a593Smuzhiyun 			continue;
875*4882a593Smuzhiyun 		}
876*4882a593Smuzhiyun 
877*4882a593Smuzhiyun 		/* parse name */
878*4882a593Smuzhiyun 		for (name = dp; *dp != '=' && *dp && *dp != sep; ++dp)
879*4882a593Smuzhiyun 			;
880*4882a593Smuzhiyun 
881*4882a593Smuzhiyun 		/* deal with "name" and "name=" entries (delete var) */
882*4882a593Smuzhiyun 		if (*dp == '\0' || *(dp + 1) == '\0' ||
883*4882a593Smuzhiyun 		    *dp == sep || *(dp + 1) == sep) {
884*4882a593Smuzhiyun 			if (*dp == '=')
885*4882a593Smuzhiyun 				*dp++ = '\0';
886*4882a593Smuzhiyun 			*dp++ = '\0';	/* terminate name */
887*4882a593Smuzhiyun 
888*4882a593Smuzhiyun 			debug("DELETE CANDIDATE: \"%s\"\n", name);
889*4882a593Smuzhiyun 			if (!drop_var_from_set(name, nvars, localvars))
890*4882a593Smuzhiyun 				continue;
891*4882a593Smuzhiyun 
892*4882a593Smuzhiyun 			if (hdelete_r(name, htab, flag) == 0)
893*4882a593Smuzhiyun 				debug("DELETE ERROR ##############################\n");
894*4882a593Smuzhiyun 
895*4882a593Smuzhiyun 			continue;
896*4882a593Smuzhiyun 		}
897*4882a593Smuzhiyun 		*dp++ = '\0';	/* terminate name */
898*4882a593Smuzhiyun 
899*4882a593Smuzhiyun 		/* parse value; deal with escapes */
900*4882a593Smuzhiyun 		for (value = sp = dp; *dp && (*dp != sep); ++dp) {
901*4882a593Smuzhiyun 			if ((*dp == '\\') && *(dp + 1))
902*4882a593Smuzhiyun 				++dp;
903*4882a593Smuzhiyun 			*sp++ = *dp;
904*4882a593Smuzhiyun 		}
905*4882a593Smuzhiyun 		*sp++ = '\0';	/* terminate value */
906*4882a593Smuzhiyun 		++dp;
907*4882a593Smuzhiyun 
908*4882a593Smuzhiyun 		if (*name == 0) {
909*4882a593Smuzhiyun 			debug("INSERT: unable to use an empty key\n");
910*4882a593Smuzhiyun 			__set_errno(EINVAL);
911*4882a593Smuzhiyun 			free(data);
912*4882a593Smuzhiyun 			return 0;
913*4882a593Smuzhiyun 		}
914*4882a593Smuzhiyun 
915*4882a593Smuzhiyun 		/* Skip variables which are not supposed to be processed */
916*4882a593Smuzhiyun 		if (!drop_var_from_set(name, nvars, localvars))
917*4882a593Smuzhiyun 			continue;
918*4882a593Smuzhiyun 
919*4882a593Smuzhiyun 		/* enter into hash table */
920*4882a593Smuzhiyun 		e.key = name;
921*4882a593Smuzhiyun 		e.data = value;
922*4882a593Smuzhiyun 
923*4882a593Smuzhiyun 		hsearch_r(e, ENTER, &rv, htab, flag);
924*4882a593Smuzhiyun 		if (rv == NULL)
925*4882a593Smuzhiyun 			printf("himport_r: can't insert \"%s=%s\" into hash table\n",
926*4882a593Smuzhiyun 				name, value);
927*4882a593Smuzhiyun 
928*4882a593Smuzhiyun 		debug("INSERT: table %p, filled %d/%d rv %p ==> name=\"%s\" value=\"%s\"\n",
929*4882a593Smuzhiyun 			htab, htab->filled, htab->size,
930*4882a593Smuzhiyun 			rv, name, value);
931*4882a593Smuzhiyun 	} while ((dp < data + size) && *dp);	/* size check needed for text */
932*4882a593Smuzhiyun 						/* without '\0' termination */
933*4882a593Smuzhiyun 	debug("INSERT: free(data = %p)\n", data);
934*4882a593Smuzhiyun 	free(data);
935*4882a593Smuzhiyun 
936*4882a593Smuzhiyun 	/*
937*4882a593Smuzhiyun 	 * CONFIG_ENVF=y: don't delete the default variables when they are
938*4882a593Smuzhiyun 	 * not present in env.img
939*4882a593Smuzhiyun 	 */
940*4882a593Smuzhiyun #ifndef CONFIG_ENVF
941*4882a593Smuzhiyun 	int i;
942*4882a593Smuzhiyun 
943*4882a593Smuzhiyun 	/* process variables which were not considered */
944*4882a593Smuzhiyun 	for (i = 0; i < nvars; i++) {
945*4882a593Smuzhiyun 		if (localvars[i] == NULL)
946*4882a593Smuzhiyun 			continue;
947*4882a593Smuzhiyun 		/*
948*4882a593Smuzhiyun 		 * All variables which were not deleted from the variable list
949*4882a593Smuzhiyun 		 * were not present in the imported env
950*4882a593Smuzhiyun 		 * This could mean two things:
951*4882a593Smuzhiyun 		 * a) if the variable was present in current env, we delete it
952*4882a593Smuzhiyun 		 * b) if the variable was not present in current env, we notify
953*4882a593Smuzhiyun 		 *    it might be a typo
954*4882a593Smuzhiyun 		 */
955*4882a593Smuzhiyun 		if (hdelete_r(localvars[i], htab, flag) == 0)
956*4882a593Smuzhiyun 			printf("WARNING: '%s' neither in running nor in imported env!\n", localvars[i]);
957*4882a593Smuzhiyun 		else
958*4882a593Smuzhiyun 			printf("WARNING: '%s' not in imported env, deleting it!\n", localvars[i]);
959*4882a593Smuzhiyun 	}
960*4882a593Smuzhiyun #endif
961*4882a593Smuzhiyun 	debug("INSERT: done\n");
962*4882a593Smuzhiyun 	return 1;		/* everything OK */
963*4882a593Smuzhiyun }
964*4882a593Smuzhiyun 
965*4882a593Smuzhiyun /*
966*4882a593Smuzhiyun  * hwalk_r()
967*4882a593Smuzhiyun  */
968*4882a593Smuzhiyun 
969*4882a593Smuzhiyun /*
970*4882a593Smuzhiyun  * Walk all of the entries in the hash, calling the callback for each one.
971*4882a593Smuzhiyun  * this allows some generic operation to be performed on each element.
972*4882a593Smuzhiyun  */
hwalk_r(struct hsearch_data * htab,int (* callback)(ENTRY *))973*4882a593Smuzhiyun int hwalk_r(struct hsearch_data *htab, int (*callback)(ENTRY *))
974*4882a593Smuzhiyun {
975*4882a593Smuzhiyun 	int i;
976*4882a593Smuzhiyun 	int retval;
977*4882a593Smuzhiyun 
978*4882a593Smuzhiyun 	for (i = 1; i <= htab->size; ++i) {
979*4882a593Smuzhiyun 		if (htab->table[i].used > 0) {
980*4882a593Smuzhiyun 			retval = callback(&htab->table[i].entry);
981*4882a593Smuzhiyun 			if (retval)
982*4882a593Smuzhiyun 				return retval;
983*4882a593Smuzhiyun 		}
984*4882a593Smuzhiyun 	}
985*4882a593Smuzhiyun 
986*4882a593Smuzhiyun 	return 0;
987*4882a593Smuzhiyun }
988