1 const char list_rcs[] = "$Id: list.c,v 1.20 2007/05/14 16:56:07 fabiankeil Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/list.c,v $
6 * Purpose : Declares functions to handle lists.
7 * Functions declared include:
8 * `destroy_list', `enlist' and `list_to_text'
10 * Copyright : Written by and Copyright (C) 2001-2007 the SourceForge
11 * Privoxy team. http://www.privoxy.org/
13 * Based on the Internet Junkbuster originally written
14 * by and Copyright (C) 1997 Anonymous Coders and
15 * Junkbusters Corporation. http://www.junkbusters.com
17 * This program is free software; you can redistribute it
18 * and/or modify it under the terms of the GNU General
19 * Public License as published by the Free Software
20 * Foundation; either version 2 of the License, or (at
21 * your option) any later version.
23 * This program is distributed in the hope that it will
24 * be useful, but WITHOUT ANY WARRANTY; without even the
25 * implied warranty of MERCHANTABILITY or FITNESS FOR A
26 * PARTICULAR PURPOSE. See the GNU General Public
27 * License for more details.
29 * The GNU General Public License should be included with
30 * this file. If not, you can view it at
31 * http://www.gnu.org/copyleft/gpl.html
32 * or write to the Free Software Foundation, Inc., 59
33 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
35 *********************************************************************/
41 /* FIXME: The following headers are not needed for Win32. Are they
42 * needed on other platforms?
45 #include <sys/types.h>
51 #if !defined(_WIN32) && !defined(__OS2__)
61 const char list_h_rcs[] = LIST_H_VERSION;
64 static int list_is_valid (const struct list *the_list);
67 /*********************************************************************
69 * Function : init_list
71 * Description : Create a new, empty list in user-allocated memory.
72 * Caller should allocate a "struct list" variable,
73 * then pass it to this function.
74 * (Implementation note: Rather than calling this
75 * function, you can also just memset the memory to
76 * zero, e.g. if you have a larger structure you
77 * want to initialize quickly. However, that isn't
78 * really good design.)
81 * 1 : the_list = pointer to list
85 *********************************************************************/
86 void init_list(struct list *the_list)
88 memset(the_list, '\0', sizeof(*the_list));
92 /*********************************************************************
94 * Function : destroy_list
96 * Description : Destroy a string list (opposite of list_init).
97 * On return, the memory used by the list entries has
98 * been freed, but not the memory used by the_list
99 * itself. You should not re-use the_list without
100 * calling list_init().
102 * (Implementation note: You *can* reuse the_list
103 * without calling list_init(), but please don't.
104 * If you want to remove all entries from a list
105 * and still have a usable list, then use
106 * list_remove_all().)
109 * 1 : the_list = pointer to list
113 *********************************************************************/
114 void destroy_list (struct list *the_list)
116 struct list_entry *cur_entry, *next_entry;
120 for (cur_entry = the_list->first; cur_entry ; cur_entry = next_entry)
122 next_entry = cur_entry->next;
123 freez(cur_entry->str);
127 the_list->first = NULL;
128 the_list->last = NULL;
132 /*********************************************************************
134 * Function : list_is_valid
136 * Description : Check that a string list is valid. The intended
137 * usage is "assert(list_is_valid(the_list))".
138 * Currently this checks that "the_list->last"
139 * is correct, and that the list dosn't contain
140 * circular references. It is likely to crash if
141 * it's passed complete garbage.
144 * 1 : the_list = pointer to list. Must be non-null.
146 * Returns : 1 if list is valid, 0 otherwise.
148 *********************************************************************/
149 static int list_is_valid (const struct list *the_list)
152 * If you don't want this check, just change the line below
153 * from "#if 1" to "#if 0".
156 const struct list_entry *cur_entry;
157 const struct list_entry *last_entry = NULL;
162 for (cur_entry = the_list->first; cur_entry ; cur_entry = cur_entry->next)
164 last_entry = cur_entry;
169 * Just check that this string can be accessed - i.e. it's a valid
172 (void)strlen(cur_entry->str);
176 * Check for looping back to first
178 if ((entry++ != 0) && (cur_entry == the_list->first))
184 * Arbitrarily limit list length to prevent infinite loops.
185 * Note that the 1000 limit was hit by a real user in tracker 911950;
186 * removing it for now. Real circular references should eventually
187 * be caught by the check above, anyway.
197 * Check this isn't marked as the last entry, unless of course it's
198 * *really* the last entry.
200 if ((the_list->last == cur_entry) && (cur_entry->next != NULL))
202 /* This is the last entry, but there's data after it !!?? */
207 return (the_list->last == last_entry);
213 /*********************************************************************
217 * Description : Append a string into a specified string list.
220 * 1 : the_list = pointer to list
221 * 2 : str = string to add to the list (maybe NULL)
223 * Returns : JB_ERR_OK on success
224 * JB_ERR_MEMORY on out-of-memory error.
225 * On error, the_list will be unchanged.
227 *********************************************************************/
228 jb_err enlist(struct list *the_list, const char *str)
230 struct list_entry *cur;
233 assert(list_is_valid(the_list));
235 if (NULL == (cur = (struct list_entry *)zalloc(sizeof(*cur))))
237 return JB_ERR_MEMORY;
242 if (NULL == (cur->str = strdup(str)))
245 return JB_ERR_MEMORY;
248 /* else { cur->str = NULL; } - implied by zalloc */
250 /* cur->next = NULL; - implied by zalloc */
254 the_list->last->next = cur;
255 the_list->last = cur;
259 the_list->first = cur;
260 the_list->last = cur;
263 assert(list_is_valid(the_list));
268 /*********************************************************************
270 * Function : enlist_first
272 * Description : Append a string as first element into a specified
276 * 1 : the_list = pointer to list
277 * 2 : str = string to add to the list (maybe NULL)
279 * Returns : JB_ERR_OK on success
280 * JB_ERR_MEMORY on out-of-memory error.
281 * On error, the_list will be unchanged.
283 *********************************************************************/
284 jb_err enlist_first(struct list *the_list, const char *str)
286 struct list_entry *cur;
289 assert(list_is_valid(the_list));
291 if (NULL == (cur = (struct list_entry *)zalloc(sizeof(*cur))))
293 return JB_ERR_MEMORY;
298 if (NULL == (cur->str = strdup(str)))
301 return JB_ERR_MEMORY;
304 /* else { cur->str = NULL; } - implied by zalloc */
306 cur->next = the_list->first;
308 the_list->first = cur;
309 if (the_list->last == NULL)
311 the_list->last = cur;
314 assert(list_is_valid(the_list));
319 /*********************************************************************
321 * Function : enlist_unique
323 * Description : Append a string into a specified string list,
324 * if & only if it's not there already.
325 * If the num_significant_chars argument is nonzero,
326 * only compare up to the nth character.
329 * 1 : the_list = pointer to list
330 * 2 : str = string to add to the list
331 * 3 : num_significant_chars = number of chars to use
332 * for uniqueness test, or 0 to require an exact match.
334 * Returns : JB_ERR_OK on success
335 * JB_ERR_MEMORY on out-of-memory error.
336 * On error, the_list will be unchanged.
337 * "Success" does not indicate whether or not the
338 * item was already in the list.
340 *********************************************************************/
341 jb_err enlist_unique(struct list *the_list, const char *str,
342 size_t num_significant_chars)
344 struct list_entry *cur_entry;
347 assert(list_is_valid(the_list));
349 assert(num_significant_chars >= 0);
350 assert(num_significant_chars <= strlen(str));
352 if (num_significant_chars > 0)
354 for (cur_entry = the_list->first; cur_entry != NULL; cur_entry = cur_entry->next)
356 if ( (cur_entry->str != NULL)
357 && (0 == strncmp(str, cur_entry->str, num_significant_chars)))
366 /* Test whole string */
367 for (cur_entry = the_list->first; cur_entry != NULL; cur_entry = cur_entry->next)
369 if ( (cur_entry->str != NULL) && (0 == strcmp(str, cur_entry->str)))
377 return enlist(the_list, str);
381 /*********************************************************************
383 * Function : enlist_unique_header
385 * Description : Make a HTTP header from the two strings name and value,
386 * and append the result into a specified string list,
387 * if & only if there isn't already a header with that name.
390 * 1 : the_list = pointer to list
391 * 2 : name = HTTP header name (e.g. "Content-type")
392 * 3 : value = HTTP header value (e.g. "text/html")
394 * Returns : JB_ERR_OK on success
395 * JB_ERR_MEMORY on out-of-memory error.
396 * On error, the_list will be unchanged.
397 * "Success" does not indicate whether or not the
398 * header was already in the list.
400 *********************************************************************/
401 jb_err enlist_unique_header(struct list *the_list, const char *name,
404 jb_err result = JB_ERR_MEMORY;
409 assert(list_is_valid(the_list));
413 /* + 2 for the ': ', + 1 for the \0 */
414 header_size = strlen(name) + 2 + strlen(value) + 1;
415 header = (char *)malloc(header_size);
419 const size_t bytes_to_compare = strlen(name) + 2;
421 snprintf(header, header_size, "%s: %s", name, value);
422 result = enlist_unique(the_list, header, bytes_to_compare);
424 assert(list_is_valid(the_list));
432 /*********************************************************************
434 * Function : list_remove_all
436 * Description : Remove all entries from a list. On return, the_list
437 * is a valid, empty list. Note that this is similar
438 * to destroy_list(), but the difference is that this
439 * function guarantees that the list structure is still
440 * valid after the call.
443 * 1 : the_list = pointer to list
447 *********************************************************************/
448 void list_remove_all(struct list *the_list)
450 struct list_entry *cur_entry;
451 struct list_entry *next_entry;
454 assert(list_is_valid(the_list));
456 for (cur_entry = the_list->first; cur_entry ; cur_entry = next_entry)
458 next_entry = cur_entry->next;
459 freez(cur_entry->str);
463 the_list->first = the_list->last = NULL;
465 assert(list_is_valid(the_list));
469 /*********************************************************************
471 * Function : list_to_text
473 * Description : "Flatten" a string list into 1 long \r\n delimited string,
474 * adding an empty line at the end. NULL entries are ignored.
475 * This function does not change the_list.
477 * XXX: Should probably be renamed as it's only
478 * useful (and used) to flatten header lists.
481 * 1 : the_list = pointer to list
483 * Returns : NULL on malloc error, else new long string.
484 * Caller must free() it.
486 *********************************************************************/
487 char *list_to_text(const struct list *the_list)
489 struct list_entry *cur_entry;
496 assert(list_is_valid(the_list));
499 * Calculate the lenght of the final text.
500 * '2' because of the '\r\n' at the end of
501 * each string and at the end of the text.
504 for (cur_entry = the_list->first; cur_entry; cur_entry = cur_entry->next)
508 text_length += strlen(cur_entry->str) + 2;
512 bytes_left = text_length + 1;
514 text = (char *)malloc(bytes_left);
522 for (cur_entry = the_list->first; cur_entry; cur_entry = cur_entry->next)
526 const int written = snprintf(cursor, bytes_left, "%s\r\n", cur_entry->str);
529 assert(written < bytes_left);
531 bytes_left -= (size_t)written;
532 cursor += (size_t)written;
536 assert(bytes_left == 3);
542 assert(text_length == cursor - text);
543 assert(text[text_length] == '\0');
549 /*********************************************************************
551 * Function : list_remove_item
553 * Description : Remove a string from a specified string list.
556 * 1 : the_list = pointer to list
557 * 2 : str = string to remove from the list - non-NULL
559 * Returns : Number of times it was removed.
561 *********************************************************************/
562 int list_remove_item(struct list *the_list, const char *str)
564 struct list_entry *prev = NULL;
565 struct list_entry *cur;
566 struct list_entry *next;
570 assert(list_is_valid(the_list));
573 cur = the_list->first;
579 if ((cur->str != NULL) && (0 == strcmp(str, cur->str)))
589 the_list->first = next;
591 free((char *)cur->str);
601 the_list->last = prev;
603 assert(list_is_valid(the_list));
609 /*********************************************************************
611 * Function : list_remove_list
613 * Description : Remove all strings in one list from another list.
614 * This is currently a brute-force algorithm
615 * (it compares every pair of strings).
618 * 1 : dest = list to change
619 * 2 : src = list of strings to remove
621 * Returns : Total number of strings removed.
623 *********************************************************************/
624 int list_remove_list(struct list *dest, const struct list *src)
626 struct list_entry *cur;
631 assert(list_is_valid(src));
632 assert(list_is_valid(dest));
634 for (cur = src->first; cur != NULL; cur = cur->next)
636 if (cur->str != NULL)
638 count += list_remove_item(dest, cur->str);
642 assert(list_is_valid(src));
643 assert(list_is_valid(dest));
649 /*********************************************************************
651 * Function : list_duplicate
653 * Description : Copy a string list
656 * 1 : dest = Destination list. Must be a valid list.
657 * All existing entries will be removed.
658 * 1 : src = pointer to source list for copy.
660 * Returns : JB_ERR_OK on success
661 * JB_ERR_MEMORY on out-of-memory error.
662 * On error, dest will be empty.
664 *********************************************************************/
665 jb_err list_duplicate(struct list *dest, const struct list *src)
667 struct list_entry * cur_src;
668 struct list_entry * cur_dest;
672 assert(list_is_valid(src));
673 assert(list_is_valid(dest));
675 list_remove_all(dest);
677 /* Need to process first entry specially so we can set dest->first */
678 cur_src = src->first;
681 cur_dest = dest->first = (struct list_entry *)zalloc(sizeof(*cur_dest));
682 if (cur_dest == NULL)
686 assert(list_is_valid(src));
687 assert(list_is_valid(dest));
689 return JB_ERR_MEMORY;
694 cur_dest->str = strdup(cur_src->str);
695 if (cur_dest->str == NULL)
699 assert(list_is_valid(src));
700 assert(list_is_valid(dest));
702 return JB_ERR_MEMORY;
705 /* else { cur_dest->str = NULL; } - implied by zalloc */
707 /* Now process the rest */
708 for (cur_src = cur_src->next; cur_src; cur_src = cur_src->next)
710 cur_dest = cur_dest->next = (struct list_entry *)zalloc(sizeof(*cur_dest));
711 if (cur_dest == NULL)
715 assert(list_is_valid(src));
716 assert(list_is_valid(dest));
718 return JB_ERR_MEMORY;
722 cur_dest->str = strdup(cur_src->str);
723 if (cur_dest->str == NULL)
727 assert(list_is_valid(src));
728 assert(list_is_valid(dest));
730 return JB_ERR_MEMORY;
733 /* else { cur_dest->str = NULL; } - implied by zalloc */
736 dest->last = cur_dest;
739 assert(list_is_valid(src));
740 assert(list_is_valid(dest));
746 /*********************************************************************
748 * Function : list_append_list_unique
750 * Description : Append a string list to another list.
751 * Duplicate items are not added.
754 * 1 : dest = pointer to destination list for merge.
755 * 2 : src = pointer to source for merge.
757 * Returns : JB_ERR_OK on success
758 * JB_ERR_MEMORY on out-of-memory error.
759 * On error, some (but not all) of src might have
760 * been copied into dest.
762 *********************************************************************/
763 jb_err list_append_list_unique(struct list *dest, const struct list *src)
765 struct list_entry * cur;
769 assert(list_is_valid(src));
770 assert(list_is_valid(dest));
772 for (cur = src->first; cur; cur = cur->next)
776 if (enlist_unique(dest, cur->str, 0))
778 assert(list_is_valid(src));
779 assert(list_is_valid(dest));
781 return JB_ERR_MEMORY;
786 assert(list_is_valid(src));
787 assert(list_is_valid(dest));
793 /*********************************************************************
795 * Function : list_is_empty
797 * Description : Test whether a list is empty. Does not change the list.
800 * 1 : the_list = pointer to list to test.
802 * Returns : Nonzero if the list contains no entries.
804 *********************************************************************/
805 int list_is_empty(const struct list *the_list)
808 assert(list_is_valid(the_list));
810 return (the_list->first == NULL);
814 /*********************************************************************
816 * Function : list_contains_item
818 * Description : Tests whether a list item is already set.
819 * Does not change the list.
822 * 1 : the_list = list to search in
823 * 2 : str = string to search for
825 * Returns : TRUE if the item was found,
828 *********************************************************************/
829 int list_contains_item(const struct list *the_list, const char *str)
831 struct list_entry *entry;
834 assert(list_is_valid(the_list));
837 for (entry = the_list->first; entry != NULL; entry = entry->next)
839 if (entry->str == NULL)
842 * NULL pointers are allowed in some lists.
843 * For example for csp->headers in case a
844 * header was removed.
849 if (0 == strcmp(str, entry->str))
860 /*********************************************************************
864 * Description : Create a new, empty map.
868 * Returns : A new, empty map, or NULL if out of memory.
870 *********************************************************************/
871 struct map *new_map(void)
873 return (struct map *) zalloc(sizeof(struct map));
877 /*********************************************************************
879 * Function : free_map
881 * Description : Free the memory occupied by a map and its
885 * 1 : the_map = map to be freed. May be NULL.
889 *********************************************************************/
890 void free_map(struct map *the_map)
892 struct map_entry *cur_entry;
893 struct map_entry *next_entry;
900 for (cur_entry = the_map->first; cur_entry != NULL; cur_entry = next_entry)
902 freez(cur_entry->name);
903 freez(cur_entry->value);
905 next_entry = cur_entry->next;
909 the_map->first = the_map->last = NULL;
915 /*********************************************************************
919 * Description : Add a mapping from given name to given value to a
922 * Note: Since all strings will be free()d in free_map()
923 * later, set the copy flags for constants or
924 * strings that will be independantly free()d.
926 * Note2: This function allows NULL parameters - it
927 * returns JB_ERR_MEMORY in that case.
929 * Note3: If this function returns JB_ERR_MEMORY,
930 * it will free(name) unless you specify
931 * name_needs_copying, and similarly it will
932 * free(value) unless you specify
933 * value_needs_copying.
935 * Due to Note2 and Note3 above, the following code
936 * is legal, and will never crash or leak memory even
937 * if the system runs out of memory:
939 * err = map(mymap, "xyz", 1, html_encode(somestring), 0);
941 * err will be set to JB_ERR_MEMORY if either call runs
942 * out-of-memory. Without these features, you would
943 * need to check the return value of html_encode in the
944 * above example for NULL, which (at least) doubles the
945 * amount of error-checking code needed.
948 * 1 : the_map = map to add to
949 * 2 : name = name to add
950 * 3 : name_needs_copying = flag set if a copy of name should be used
951 * 4 : value = value to add
952 * 5 : value_needs_copying = flag set if a copy of value should be used
954 * Returns : JB_ERR_OK on success
955 * JB_ERR_MEMORY on out-of-memory error.
957 *********************************************************************/
958 jb_err map(struct map *the_map,
959 const char *name, int name_needs_copying,
960 const char *value, int value_needs_copying)
962 struct map_entry *new_entry;
968 || (NULL == (new_entry = zalloc(sizeof(*new_entry)))) )
970 if ((name != NULL) && (!name_needs_copying))
974 if ((value != NULL) && (!value_needs_copying))
978 return JB_ERR_MEMORY;
981 if (name_needs_copying)
983 if (NULL == (name = strdup(name)))
986 if (!value_needs_copying)
990 return JB_ERR_MEMORY;
994 if (value_needs_copying)
996 if (NULL == (value = strdup(value)))
1000 return JB_ERR_MEMORY;
1004 new_entry->name = name;
1005 new_entry->value = value;
1006 /* new_entry->next = NULL; - implied by zalloc */
1010 the_map->last->next = new_entry;
1011 the_map->last = new_entry;
1015 the_map->first = new_entry;
1016 the_map->last = new_entry;
1023 /*********************************************************************
1027 * Description : Remove all map_entry structs with a given name from
1031 * 1 : the_map = map to look in
1032 * 2 : name = name to unmap
1034 * Returns : JB_ERR_OK
1036 *********************************************************************/
1037 jb_err unmap(struct map *the_map, const char *name)
1039 struct map_entry *cur_entry, *last_entry;
1044 last_entry = the_map->first;
1046 for (cur_entry = the_map->first; cur_entry != NULL; cur_entry = cur_entry->next)
1048 if (!strcmp(name, cur_entry->name))
1051 * Update the incoming pointer
1053 if (cur_entry == the_map->first)
1055 the_map->first = cur_entry->next;
1059 last_entry->next = cur_entry->next;
1063 * Update the map's last pointer
1065 if (cur_entry == the_map->last)
1067 the_map->last = last_entry;
1071 * Free the map_entry
1073 freez(cur_entry->name);
1074 freez(cur_entry->value);
1077 cur_entry = last_entry;
1081 last_entry = cur_entry;
1088 /*********************************************************************
1092 * Description : Look up an item with a given name in a map, and
1096 * 1 : the_map = map to look in
1097 * 2 : name = name parameter to look for
1099 * Returns : the value if found, else the empty string.
1100 * Return value is alloced as part of the map, so
1101 * it is freed when the map is destroyed. Caller
1102 * must not free or modify it.
1104 *********************************************************************/
1105 const char *lookup(const struct map *the_map, const char *name)
1107 const struct map_entry *cur_entry;
1112 for (cur_entry = the_map->first; cur_entry != NULL; cur_entry = cur_entry->next)
1114 if (!strcmp(name, cur_entry->name))
1116 return cur_entry->value;