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