container.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  1. /* Copyright 2003-2004 Roger Dingledine
  2. Copyright 2004-2005 Roger Dingledine, Nick Mathewson */
  3. /* See LICENSE for licensing information */
  4. /* $Id$ */
  5. const char container_c_id[] = "$Id$";
  6. /**
  7. * \file container.c
  8. * \brief Implements a smartlist (a resizable array) along
  9. * with helper functions to use smartlists. Also includes a
  10. * splay-tree implementation of the string-to-void* map.
  11. **/
  12. #include "compat.h"
  13. #include "util.h"
  14. #include "log.h"
  15. #include "../or/tree.h"
  16. #include "container.h"
  17. #include "crypto.h"
  18. #ifdef HAVE_CTYPE_H
  19. #include <ctype.h>
  20. #endif
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <assert.h>
  24. /* All newly allocated smartlists have this capacity.
  25. */
  26. #define SMARTLIST_DEFAULT_CAPACITY 32
  27. /** Allocate and return an empty smartlist.
  28. */
  29. smartlist_t *
  30. smartlist_create(void)
  31. {
  32. smartlist_t *sl = tor_malloc(sizeof(smartlist_t));
  33. sl->num_used = 0;
  34. sl->capacity = SMARTLIST_DEFAULT_CAPACITY;
  35. sl->list = tor_malloc(sizeof(void *) * sl->capacity);
  36. return sl;
  37. }
  38. /** Deallocate a smartlist. Does not release storage associated with the
  39. * list's elements.
  40. */
  41. void
  42. smartlist_free(smartlist_t *sl)
  43. {
  44. tor_free(sl->list);
  45. tor_free(sl);
  46. }
  47. /** Change the capacity of the smartlist to <b>n</b>, so that we can grow
  48. * the list up to <b>n</b> elements with no further reallocation or wasted
  49. * space. If <b>n</b> is less than or equal to the number of elements
  50. * currently in the list, reduce the list's capacity as much as
  51. * possible without losing elements.
  52. */
  53. void
  54. smartlist_set_capacity(smartlist_t *sl, int n)
  55. {
  56. if (n < sl->num_used)
  57. n = sl->num_used;
  58. if (sl->capacity != n) {
  59. sl->capacity = n;
  60. sl->list = tor_realloc(sl->list, sizeof(void*)*sl->capacity);
  61. }
  62. }
  63. /** Remove all elements from the list.
  64. */
  65. void
  66. smartlist_clear(smartlist_t *sl)
  67. {
  68. sl->num_used = 0;
  69. }
  70. /** Set the list's new length to <b>len</b> (which must be \<= the list's
  71. * current size). Remove the last smartlist_len(sl)-len elements from the
  72. * list.
  73. */
  74. void
  75. smartlist_truncate(smartlist_t *sl, int len)
  76. {
  77. tor_assert(len <= sl->num_used);
  78. sl->num_used = len;
  79. }
  80. /** Append element to the end of the list. */
  81. void
  82. smartlist_add(smartlist_t *sl, void *element)
  83. {
  84. if (sl->num_used >= sl->capacity) {
  85. int higher = sl->capacity * 2;
  86. tor_assert(higher > sl->capacity); /* detect overflow */
  87. sl->capacity = higher;
  88. sl->list = tor_realloc(sl->list, sizeof(void*)*sl->capacity);
  89. }
  90. sl->list[sl->num_used++] = element;
  91. }
  92. /** Append each element from S2 to the end of S1. */
  93. void
  94. smartlist_add_all(smartlist_t *sl, const smartlist_t *s2)
  95. {
  96. int n2 = sl->num_used + s2->num_used;
  97. if (n2 > sl->capacity) {
  98. int higher = sl->capacity * 2;
  99. while (n2 > higher)
  100. higher *= 2;
  101. sl->capacity = higher;
  102. sl->list = tor_realloc(sl->list, sizeof(void*)*sl->capacity);
  103. }
  104. memcpy(sl->list + sl->num_used, s2->list, s2->num_used*sizeof(void*));
  105. sl->num_used += s2->num_used;
  106. }
  107. /** Remove all elements E from sl such that E==element. Preserve
  108. * the order of any elements before E, but elements after E can be
  109. * rearranged.
  110. */
  111. void
  112. smartlist_remove(smartlist_t *sl, void *element)
  113. {
  114. int i;
  115. if (element == NULL)
  116. return;
  117. for (i=0; i < sl->num_used; i++)
  118. if (sl->list[i] == element) {
  119. sl->list[i] = sl->list[--sl->num_used]; /* swap with the end */
  120. i--; /* so we process the new i'th element */
  121. }
  122. }
  123. /** If there are any strings in sl equal to element, remove and free them.
  124. * Does not preserve order. */
  125. void
  126. smartlist_string_remove(smartlist_t *sl, const char *element)
  127. {
  128. int i;
  129. tor_assert(sl);
  130. tor_assert(element);
  131. for (i = 0; i < sl->num_used; ++i) {
  132. if (!strcmp(element, sl->list[i])) {
  133. tor_free(sl->list[i]);
  134. sl->list[i] = sl->list[--sl->num_used]; /* swap with the end */
  135. i--; /* so we process the new i'th element */
  136. }
  137. }
  138. }
  139. /** Return true iff some element E of sl has E==element.
  140. */
  141. int
  142. smartlist_isin(const smartlist_t *sl, void *element)
  143. {
  144. int i;
  145. for (i=0; i < sl->num_used; i++)
  146. if (sl->list[i] == element)
  147. return 1;
  148. return 0;
  149. }
  150. /** Return true iff <b>sl</b> has some element E such that
  151. * !strcmp(E,<b>element</b>)
  152. */
  153. int
  154. smartlist_string_isin(const smartlist_t *sl, const char *element)
  155. {
  156. int i;
  157. if (!sl) return 0;
  158. for (i=0; i < sl->num_used; i++)
  159. if (strcmp((const char*)sl->list[i],element)==0)
  160. return 1;
  161. return 0;
  162. }
  163. /** Return true iff <b>sl</b> has some element E such that E is equal
  164. * to the decimal encoding of <b>num</b>.
  165. */
  166. int
  167. smartlist_string_num_isin(const smartlist_t *sl, int num)
  168. {
  169. char buf[16];
  170. tor_snprintf(buf,sizeof(buf),"%d", num);
  171. return smartlist_string_isin(sl, buf);
  172. }
  173. /** Return true iff some element E of sl2 has smartlist_isin(sl1,E).
  174. */
  175. int
  176. smartlist_overlap(const smartlist_t *sl1, const smartlist_t *sl2)
  177. {
  178. int i;
  179. for (i=0; i < sl2->num_used; i++)
  180. if (smartlist_isin(sl1, sl2->list[i]))
  181. return 1;
  182. return 0;
  183. }
  184. /** Remove every element E of sl1 such that !smartlist_isin(sl2,E).
  185. * Does not preserve the order of sl1.
  186. */
  187. void
  188. smartlist_intersect(smartlist_t *sl1, const smartlist_t *sl2)
  189. {
  190. int i;
  191. for (i=0; i < sl1->num_used; i++)
  192. if (!smartlist_isin(sl2, sl1->list[i])) {
  193. sl1->list[i] = sl1->list[--sl1->num_used]; /* swap with the end */
  194. i--; /* so we process the new i'th element */
  195. }
  196. }
  197. /** Remove every element E of sl1 such that smartlist_isin(sl2,E).
  198. * Does not preserve the order of sl1.
  199. */
  200. void
  201. smartlist_subtract(smartlist_t *sl1, const smartlist_t *sl2)
  202. {
  203. int i;
  204. for (i=0; i < sl2->num_used; i++)
  205. smartlist_remove(sl1, sl2->list[i]);
  206. }
  207. /** Remove the <b>idx</b>th element of sl; if idx is not the last
  208. * element, swap the last element of sl into the <b>idx</b>th space.
  209. * Return the old value of the <b>idx</b>th element.
  210. */
  211. void
  212. smartlist_del(smartlist_t *sl, int idx)
  213. {
  214. tor_assert(sl);
  215. tor_assert(idx>=0);
  216. tor_assert(idx < sl->num_used);
  217. sl->list[idx] = sl->list[--sl->num_used];
  218. }
  219. /** Remove the <b>idx</b>th element of sl; if idx is not the last element,
  220. * moving all subsequent elements back one space. Return the old value
  221. * of the <b>idx</b>th element.
  222. */
  223. void
  224. smartlist_del_keeporder(smartlist_t *sl, int idx)
  225. {
  226. tor_assert(sl);
  227. tor_assert(idx>=0);
  228. tor_assert(idx < sl->num_used);
  229. --sl->num_used;
  230. if (idx < sl->num_used)
  231. memmove(sl->list+idx, sl->list+idx+1, sizeof(void*)*(sl->num_used-idx));
  232. }
  233. /** Insert the value <b>val</b> as the new <b>idx</b>th element of
  234. * <b>sl</b>, moving all items previously at <b>idx</b> or later
  235. * forward one space.
  236. */
  237. void
  238. smartlist_insert(smartlist_t *sl, int idx, void *val)
  239. {
  240. tor_assert(sl);
  241. tor_assert(idx>=0);
  242. tor_assert(idx <= sl->num_used);
  243. if (idx == sl->num_used) {
  244. smartlist_add(sl, val);
  245. } else {
  246. /* Ensure sufficient capacity */
  247. if (sl->num_used >= sl->capacity) {
  248. sl->capacity *= 2;
  249. sl->list = tor_realloc(sl->list, sizeof(void*)*sl->capacity);
  250. }
  251. /* Move other elements away */
  252. if (idx < sl->num_used)
  253. memmove(sl->list + idx + 1, sl->list + idx,
  254. sizeof(void*)*(sl->num_used-idx));
  255. sl->num_used++;
  256. sl->list[idx] = val;
  257. }
  258. }
  259. /**
  260. * Split a string <b>str</b> along all occurrences of <b>sep</b>,
  261. * adding the split strings, in order, to <b>sl</b>. If
  262. * <b>flags</b>&amp;SPLIT_SKIP_SPACE is true, remove initial and
  263. * trailing space from each entry. If
  264. * <b>flags</b>&amp;SPLIT_IGNORE_BLANK is true, remove any entries of
  265. * length 0. If max>0, divide the string into no more than <b>max</b>
  266. * pieces. If <b>sep</b> is NULL, split on any sequence of horizontal space.
  267. */
  268. int
  269. smartlist_split_string(smartlist_t *sl, const char *str, const char *sep,
  270. int flags, int max)
  271. {
  272. const char *cp, *end, *next;
  273. int n = 0;
  274. tor_assert(sl);
  275. tor_assert(str);
  276. cp = str;
  277. while (1) {
  278. if (flags&SPLIT_SKIP_SPACE) {
  279. while (TOR_ISSPACE(*cp)) ++cp;
  280. }
  281. if (max>0 && n == max-1) {
  282. end = strchr(cp,'\0');
  283. } else if (sep) {
  284. end = strstr(cp,sep);
  285. if (!end)
  286. end = strchr(cp,'\0');
  287. } else {
  288. for (end = cp; *end && *end != '\t' && *end != ' '; ++end)
  289. ;
  290. }
  291. if (!*end) {
  292. next = NULL;
  293. } else if (sep) {
  294. next = end+strlen(sep);
  295. } else {
  296. next = end+1;
  297. while (*next == '\t' || *next == ' ')
  298. ++next;
  299. }
  300. if (flags&SPLIT_SKIP_SPACE) {
  301. while (end > cp && TOR_ISSPACE(*(end-1)))
  302. --end;
  303. }
  304. if (end != cp || !(flags&SPLIT_IGNORE_BLANK)) {
  305. smartlist_add(sl, tor_strndup(cp, end-cp));
  306. ++n;
  307. }
  308. if (!next)
  309. break;
  310. cp = next;
  311. }
  312. return n;
  313. }
  314. /** Allocate and return a new string containing the concatenation of
  315. * the elements of <b>sl</b>, in order, separated by <b>join</b>. If
  316. * <b>terminate</b> is true, also terminate the string with <b>join</b>.
  317. * If <b>len_out</b> is not NULL, set <b>len_out</b> to the length of
  318. * the returned string. Requires that every element of <b>sl</b> is
  319. * NUL-terminated string.
  320. */
  321. char *
  322. smartlist_join_strings(smartlist_t *sl, const char *join,
  323. int terminate, size_t *len_out)
  324. {
  325. return smartlist_join_strings2(sl,join,strlen(join),terminate,len_out);
  326. }
  327. /** As smartlist_join_strings, but instead of separating/terminated with a
  328. * NUL-terminated string <b>join</b>, uses the <b>join_len</b>-byte sequence
  329. * at <b>join</b>. (Useful for generating a sequence of NUL-terminated
  330. * strings.)
  331. */
  332. char *
  333. smartlist_join_strings2(smartlist_t *sl, const char *join,
  334. size_t join_len, int terminate, size_t *len_out)
  335. {
  336. int i;
  337. size_t n = 0;
  338. char *r = NULL, *dst, *src;
  339. tor_assert(sl);
  340. tor_assert(join);
  341. if (terminate)
  342. n = join_len;
  343. for (i = 0; i < sl->num_used; ++i) {
  344. n += strlen(sl->list[i]);
  345. if (i+1 < sl->num_used) /* avoid double-counting the last one */
  346. n += join_len;
  347. }
  348. dst = r = tor_malloc(n+1);
  349. for (i = 0; i < sl->num_used; ) {
  350. for (src = sl->list[i]; *src; )
  351. *dst++ = *src++;
  352. if (++i < sl->num_used) {
  353. memcpy(dst, join, join_len);
  354. dst += join_len;
  355. }
  356. }
  357. if (terminate) {
  358. memcpy(dst, join, join_len);
  359. dst += join_len;
  360. }
  361. *dst = '\0';
  362. if (len_out)
  363. *len_out = dst-r;
  364. return r;
  365. }
  366. /** Sort the members of <b>sl</b> into an order defined by
  367. * the ordering function <b>compare</b>, which returns less then 0 if a
  368. * precedes b, greater than 0 if b precedes a, and 0 if a 'equals' b.
  369. */
  370. void
  371. smartlist_sort(smartlist_t *sl, int (*compare)(const void **a, const void **b))
  372. {
  373. if (!sl->num_used)
  374. return;
  375. qsort(sl->list, sl->num_used, sizeof(void*),
  376. (int (*)(const void *,const void*))compare);
  377. }
  378. /** Assuming the members of <b>sl</b> are in order, return a pointer to the
  379. * member which matches <b>key</b>. Ordering and matching are defined by a
  380. * <b>compare</b> function, which returns 0 on a match; less than 0 if key is
  381. * less than member, and greater than 0 if key is greater then member.
  382. */
  383. void *
  384. smartlist_bsearch(smartlist_t *sl, const void *key,
  385. int (*compare)(const void *key, const void **member))
  386. {
  387. void ** r;
  388. if (!sl->num_used)
  389. return NULL;
  390. r = bsearch(key, sl->list, sl->num_used, sizeof(void*),
  391. (int (*)(const void *, const void *))compare);
  392. return r ? *r : NULL;
  393. }
  394. /** Helper: compare two const char **s. */
  395. static int
  396. _compare_string_ptrs(const void **_a, const void **_b)
  397. {
  398. return strcmp((const char*)*_a, (const char*)*_b);
  399. }
  400. /** Sort a smartlist <b>sl</b> containing strings into lexically ascending
  401. * order. */
  402. void
  403. smartlist_sort_strings(smartlist_t *sl)
  404. {
  405. smartlist_sort(sl, _compare_string_ptrs);
  406. }
  407. #define DEFINE_MAP_STRUCTS(maptype, keydecl, prefix) \
  408. typedef struct prefix ## entry_t { \
  409. SPLAY_ENTRY(prefix ## entry_t) node; \
  410. keydecl; \
  411. void *val; \
  412. } prefix ## entry_t; \
  413. struct maptype { \
  414. SPLAY_HEAD(prefix ## tree, prefix ## entry_t) head; \
  415. };
  416. DEFINE_MAP_STRUCTS(strmap_t, char *key, strmap_);
  417. DEFINE_MAP_STRUCTS(digestmap_t, char key[DIGEST_LEN], digestmap_);
  418. /** Helper: compare strmap_t_entry objects by key value. */
  419. static int
  420. compare_strmap_entries(strmap_entry_t *a,
  421. strmap_entry_t *b)
  422. {
  423. return strcmp(a->key, b->key);
  424. }
  425. /** Helper: compare digestmap_entry_t objects by key value. */
  426. static int
  427. compare_digestmap_entries(digestmap_entry_t *a,
  428. digestmap_entry_t *b)
  429. {
  430. return memcmp(a->key, b->key, DIGEST_LEN);
  431. }
  432. SPLAY_PROTOTYPE(strmap_tree, strmap_entry_t, node, compare_strmap_entries);
  433. SPLAY_GENERATE(strmap_tree, strmap_entry_t, node, compare_strmap_entries);
  434. SPLAY_PROTOTYPE(digestmap_tree, digestmap_entry_t, node,
  435. compare_digestmap_entries);
  436. SPLAY_GENERATE(digestmap_tree, digestmap_entry_t, node,
  437. compare_digestmap_entries);
  438. /** Define function reate a new empty map from strings to void*'s.
  439. */
  440. strmap_t *
  441. strmap_new(void)
  442. {
  443. strmap_t *result;
  444. result = tor_malloc(sizeof(strmap_t));
  445. SPLAY_INIT(&result->head);
  446. return result;
  447. }
  448. /** Define function reate a new empty map from digests to void*'s.
  449. */
  450. digestmap_t *
  451. digestmap_new(void)
  452. {
  453. digestmap_t *result;
  454. result = tor_malloc(sizeof(digestmap_t));
  455. SPLAY_INIT(&result->head);
  456. return result;
  457. }
  458. /** Set the current value for <b>key</b> to <b>val</b>. Returns the previous
  459. * value for <b>key</b> if one was set, or NULL if one was not.
  460. *
  461. * This function makes a copy of <b>key</b> if necessary, but not of
  462. * <b>val</b>.
  463. */
  464. void *
  465. strmap_set(strmap_t *map, const char *key, void *val)
  466. {
  467. strmap_entry_t *resolve;
  468. strmap_entry_t search;
  469. void *oldval;
  470. tor_assert(map);
  471. tor_assert(key);
  472. tor_assert(val);
  473. search.key = (char*)key;
  474. resolve = SPLAY_FIND(strmap_tree, &map->head, &search);
  475. if (resolve) {
  476. oldval = resolve->val;
  477. resolve->val = val;
  478. return oldval;
  479. } else {
  480. resolve = tor_malloc_zero(sizeof(strmap_entry_t));
  481. resolve->key = tor_strdup(key);
  482. resolve->val = val;
  483. SPLAY_INSERT(strmap_tree, &map->head, resolve);
  484. return NULL;
  485. }
  486. }
  487. void *
  488. digestmap_set(digestmap_t *map, const char *key, void *val)
  489. {
  490. digestmap_entry_t *resolve;
  491. digestmap_entry_t search;
  492. void *oldval;
  493. tor_assert(map);
  494. tor_assert(key);
  495. tor_assert(val);
  496. memcpy(&search.key, key, DIGEST_LEN);
  497. resolve = SPLAY_FIND(digestmap_tree, &map->head, &search);
  498. if (resolve) {
  499. oldval = resolve->val;
  500. resolve->val = val;
  501. return oldval;
  502. } else {
  503. resolve = tor_malloc_zero(sizeof(digestmap_entry_t));
  504. memcpy(resolve->key, key, DIGEST_LEN);
  505. resolve->val = val;
  506. SPLAY_INSERT(digestmap_tree, &map->head, resolve);
  507. return NULL;
  508. }
  509. }
  510. /** Return the current value associated with <b>key</b>, or NULL if no
  511. * value is set.
  512. */
  513. void *
  514. strmap_get(strmap_t *map, const char *key)
  515. {
  516. strmap_entry_t *resolve;
  517. strmap_entry_t search;
  518. tor_assert(map);
  519. tor_assert(key);
  520. search.key = (char*)key;
  521. resolve = SPLAY_FIND(strmap_tree, &map->head, &search);
  522. if (resolve) {
  523. return resolve->val;
  524. } else {
  525. return NULL;
  526. }
  527. }
  528. void *
  529. digestmap_get(digestmap_t *map, const char *key)
  530. {
  531. digestmap_entry_t *resolve;
  532. digestmap_entry_t search;
  533. tor_assert(map);
  534. tor_assert(key);
  535. memcpy(&search.key, key, DIGEST_LEN);
  536. resolve = SPLAY_FIND(digestmap_tree, &map->head, &search);
  537. if (resolve) {
  538. return resolve->val;
  539. } else {
  540. return NULL;
  541. }
  542. }
  543. /** Remove the value currently associated with <b>key</b> from the map.
  544. * Return the value if one was set, or NULL if there was no entry for
  545. * <b>key</b>.
  546. *
  547. * Note: you must free any storage associated with the returned value.
  548. */
  549. void *
  550. strmap_remove(strmap_t *map, const char *key)
  551. {
  552. strmap_entry_t *resolve;
  553. strmap_entry_t search;
  554. void *oldval;
  555. tor_assert(map);
  556. tor_assert(key);
  557. search.key = (char*)key;
  558. resolve = SPLAY_FIND(strmap_tree, &map->head, &search);
  559. if (resolve) {
  560. oldval = resolve->val;
  561. SPLAY_REMOVE(strmap_tree, &map->head, resolve);
  562. tor_free(resolve->key);
  563. tor_free(resolve);
  564. return oldval;
  565. } else {
  566. return NULL;
  567. }
  568. }
  569. void *
  570. digestmap_remove(digestmap_t *map, const char *key)
  571. {
  572. digestmap_entry_t *resolve;
  573. digestmap_entry_t search;
  574. void *oldval;
  575. tor_assert(map);
  576. tor_assert(key);
  577. memcpy(&search.key, key, DIGEST_LEN);
  578. resolve = SPLAY_FIND(digestmap_tree, &map->head, &search);
  579. if (resolve) {
  580. oldval = resolve->val;
  581. SPLAY_REMOVE(digestmap_tree, &map->head, resolve);
  582. tor_free(resolve);
  583. return oldval;
  584. } else {
  585. return NULL;
  586. }
  587. }
  588. /** Same as strmap_set, but first converts <b>key</b> to lowercase. */
  589. void *
  590. strmap_set_lc(strmap_t *map, const char *key, void *val)
  591. {
  592. /* We could be a little faster by using strcasecmp instead, and a separate
  593. * type, but I don't think it matters. */
  594. void *v;
  595. char *lc_key = tor_strdup(key);
  596. tor_strlower(lc_key);
  597. v = strmap_set(map,lc_key,val);
  598. tor_free(lc_key);
  599. return v;
  600. }
  601. /** Same as strmap_get, but first converts <b>key</b> to lowercase. */
  602. void *
  603. strmap_get_lc(strmap_t *map, const char *key)
  604. {
  605. void *v;
  606. char *lc_key = tor_strdup(key);
  607. tor_strlower(lc_key);
  608. v = strmap_get(map,lc_key);
  609. tor_free(lc_key);
  610. return v;
  611. }
  612. /** Same as strmap_remove, but first converts <b>key</b> to lowercase */
  613. void *
  614. strmap_remove_lc(strmap_t *map, const char *key)
  615. {
  616. void *v;
  617. char *lc_key = tor_strdup(key);
  618. tor_strlower(lc_key);
  619. v = strmap_remove(map,lc_key);
  620. tor_free(lc_key);
  621. return v;
  622. }
  623. /** Invoke fn() on every entry of the map, in order. For every entry,
  624. * fn() is invoked with that entry's key, that entry's value, and the
  625. * value of <b>data</b> supplied to strmap_foreach. fn() must return a new
  626. * (possibly unmodified) value for each entry: if fn() returns NULL, the
  627. * entry is removed.
  628. *
  629. * Example:
  630. * \code
  631. * static void* upcase_and_remove_empty_vals(const char *key, void *val,
  632. * void* data) {
  633. * char *cp = (char*)val;
  634. * if (!*cp) { // val is an empty string.
  635. * free(val);
  636. * return NULL;
  637. * } else {
  638. * for (; *cp; cp++)
  639. * *cp = toupper(*cp);
  640. * }
  641. * return val;
  642. * }
  643. * }
  644. *
  645. * ...
  646. *
  647. * strmap_foreach(map, upcase_and_remove_empty_vals, NULL);
  648. * \endcode
  649. */
  650. void
  651. strmap_foreach(strmap_t *map,
  652. void* (*fn)(const char *key, void *val, void *data),
  653. void *data)
  654. {
  655. strmap_entry_t *ptr, *next;
  656. tor_assert(map);
  657. tor_assert(fn);
  658. for (ptr = SPLAY_MIN(strmap_tree, &map->head); ptr != NULL; ptr = next) {
  659. /* This remove-in-place usage is specifically blessed in tree(3). */
  660. next = SPLAY_NEXT(strmap_tree, &map->head, ptr);
  661. ptr->val = fn(ptr->key, ptr->val, data);
  662. if (!ptr->val) {
  663. SPLAY_REMOVE(strmap_tree, &map->head, ptr);
  664. tor_free(ptr->key);
  665. tor_free(ptr);
  666. }
  667. }
  668. }
  669. /** return an <b>iterator</b> pointer to the front of a map.
  670. *
  671. * Iterator example:
  672. *
  673. * \code
  674. * // uppercase values in "map", removing empty values.
  675. *
  676. * strmap_iter_t *iter;
  677. * const char *key;
  678. * void *val;
  679. * char *cp;
  680. *
  681. * for (iter = strmap_iter_init(map); !strmap_iter_done(iter); ) {
  682. * strmap_iter_get(iter, &key, &val);
  683. * cp = (char*)val;
  684. * if (!*cp) {
  685. * iter = strmap_iter_next_rmv(iter);
  686. * free(val);
  687. * } else {
  688. * for (;*cp;cp++) *cp = toupper(*cp);
  689. * iter = strmap_iter_next(iter);
  690. * }
  691. * }
  692. * \endcode
  693. *
  694. */
  695. strmap_iter_t *
  696. strmap_iter_init(strmap_t *map)
  697. {
  698. tor_assert(map);
  699. return SPLAY_MIN(strmap_tree, &map->head);
  700. }
  701. digestmap_iter_t *
  702. digestmap_iter_init(digestmap_t *map)
  703. {
  704. tor_assert(map);
  705. return SPLAY_MIN(digestmap_tree, &map->head);
  706. }
  707. /** Advance the iterator <b>iter</b> for map a single step to the next entry.
  708. */
  709. strmap_iter_t *
  710. strmap_iter_next(strmap_t *map, strmap_iter_t *iter)
  711. {
  712. tor_assert(map);
  713. tor_assert(iter);
  714. return SPLAY_NEXT(strmap_tree, &map->head, iter);
  715. }
  716. digestmap_iter_t *
  717. digestmap_iter_next(digestmap_t *map, digestmap_iter_t *iter)
  718. {
  719. tor_assert(map);
  720. tor_assert(iter);
  721. return SPLAY_NEXT(digestmap_tree, &map->head, iter);
  722. }
  723. /** Advance the iterator <b>iter</b> a single step to the next entry, removing
  724. * the current entry.
  725. */
  726. strmap_iter_t *
  727. strmap_iter_next_rmv(strmap_t *map, strmap_iter_t *iter)
  728. {
  729. strmap_iter_t *next;
  730. tor_assert(map);
  731. tor_assert(iter);
  732. next = SPLAY_NEXT(strmap_tree, &map->head, iter);
  733. SPLAY_REMOVE(strmap_tree, &map->head, iter);
  734. tor_free(iter->key);
  735. tor_free(iter);
  736. return next;
  737. }
  738. digestmap_iter_t *
  739. digestmap_iter_next_rmv(digestmap_t *map, digestmap_iter_t *iter)
  740. {
  741. digestmap_iter_t *next;
  742. tor_assert(map);
  743. tor_assert(iter);
  744. next = SPLAY_NEXT(digestmap_tree, &map->head, iter);
  745. SPLAY_REMOVE(digestmap_tree, &map->head, iter);
  746. tor_free(iter);
  747. return next;
  748. }
  749. /** Set *keyp and *valp to the current entry pointed to by iter.
  750. */
  751. void
  752. strmap_iter_get(strmap_iter_t *iter, const char **keyp, void **valp)
  753. {
  754. tor_assert(iter);
  755. tor_assert(keyp);
  756. tor_assert(valp);
  757. *keyp = iter->key;
  758. *valp = iter->val;
  759. }
  760. void
  761. digestmap_iter_get(digestmap_iter_t *iter, const char **keyp, void **valp)
  762. {
  763. tor_assert(iter);
  764. tor_assert(keyp);
  765. tor_assert(valp);
  766. *keyp = iter->key;
  767. *valp = iter->val;
  768. }
  769. /** Return true iff iter has advanced past the last entry of map.
  770. */
  771. int
  772. strmap_iter_done(strmap_iter_t *iter)
  773. {
  774. return iter == NULL;
  775. }
  776. int
  777. digestmap_iter_done(digestmap_iter_t *iter)
  778. {
  779. return iter == NULL;
  780. }
  781. /** Remove all entries from <b>map</b>, and deallocate storage for those entries.
  782. * If free_val is provided, it is invoked on every value in <b>map</b>.
  783. */
  784. void
  785. strmap_free(strmap_t *map, void (*free_val)(void*))
  786. {
  787. strmap_entry_t *ent, *next;
  788. for (ent = SPLAY_MIN(strmap_tree, &map->head); ent != NULL; ent = next) {
  789. next = SPLAY_NEXT(strmap_tree, &map->head, ent);
  790. SPLAY_REMOVE(strmap_tree, &map->head, ent);
  791. tor_free(ent->key);
  792. if (free_val)
  793. free_val(ent->val);
  794. tor_free(ent);
  795. }
  796. tor_assert(SPLAY_EMPTY(&map->head));
  797. tor_free(map);
  798. }
  799. void
  800. digestmap_free(digestmap_t *map, void (*free_val)(void*))
  801. {
  802. digestmap_entry_t *ent, *next;
  803. for (ent = SPLAY_MIN(digestmap_tree, &map->head); ent != NULL; ent = next) {
  804. next = SPLAY_NEXT(digestmap_tree, &map->head, ent);
  805. SPLAY_REMOVE(digestmap_tree, &map->head, ent);
  806. if (free_val)
  807. free_val(ent->val);
  808. tor_free(ent);
  809. }
  810. tor_assert(SPLAY_EMPTY(&map->head));
  811. tor_free(map);
  812. }
  813. /* Return true iff <b>map</b> has no entries.
  814. */
  815. int
  816. strmap_isempty(strmap_t *map)
  817. {
  818. return SPLAY_EMPTY(&map->head);
  819. }
  820. int
  821. digestmap_isempty(digestmap_t *map)
  822. {
  823. return SPLAY_EMPTY(&map->head);
  824. }