hashtable.c 19 KB

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