1 const char list_rcs[] = "$Id: list.c,v 1.15.2.2 2004/05/25 02:04:23 david__schmidt Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/Attic/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 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.
37 * Revision 1.15.2.2 2004/05/25 02:04:23 david__schmidt
38 * Removed the "arbitrary" 1000 filter limit in file.c. See tracker #911950.
40 * Revision 1.15.2.1 2002/11/28 18:14:54 oes
41 * Added unmap function that removes all items with a given
44 * Revision 1.15 2002/03/26 22:29:55 swa
45 * we have a new homepage!
47 * Revision 1.14 2002/03/24 13:25:43 swa
48 * name change related issues
50 * Revision 1.13 2002/03/07 03:46:17 oes
51 * Fixed compiler warnings
53 * Revision 1.12 2001/10/25 03:40:48 david__schmidt
54 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
55 * threads to call select() simultaneously. So, it's time to do a real, live,
56 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
57 * (native). Both versions will work, but using __OS2__ offers multi-threading.
59 * Revision 1.11 2001/10/23 21:21:03 jongfoster
60 * New error handling - error codes are now jb_errs, not ints.
61 * Changed the way map() handles out-of-memory, to dramatically
62 * reduce the amount of error-checking clutter needed.
64 * Revision 1.10 2001/09/16 17:30:24 jongfoster
65 * Fixing a compiler warning.
67 * Revision 1.9 2001/09/16 13:20:29 jongfoster
68 * Rewrite of list library. Now has seperate header and list_entry
69 * structures. Also added a large sprinking of assert()s to the list
72 * Revision 1.8 2001/08/07 14:00:20 oes
75 * Revision 1.7 2001/08/05 16:06:20 jongfoster
76 * Modifiying "struct map" so that there are now separate header and
77 * "map_entry" structures. This means that functions which modify a
78 * map no longer need to return a pointer to the modified map.
79 * Also, it no longer reverses the order of the entries (which may be
80 * important with some advanced template substitutions).
82 * Revision 1.6 2001/07/31 14:44:51 oes
83 * list_to_text() now appends empty line at end
85 * Revision 1.5 2001/06/29 21:45:41 oes
86 * Indentation, CRLF->LF, Tab-> Space
88 * Revision 1.4 2001/06/29 13:30:22 oes
89 * - Added Convenience function enlist_unique_header(),
90 * which takes the Header name and value as separate
91 * arguments and thus saves the pain of sprintf()ing
92 * and determining the Header name length to enlist_unique
94 * - Removed logentry from cancelled commit
96 * Revision 1.3 2001/06/03 19:12:24 oes
97 * functions for new struct map, extended enlist_unique
99 * Revision 1.2 2001/06/01 18:49:17 jongfoster
100 * Replaced "list_share" with "list" - the tiny memory gain was not
101 * worth the extra complexity.
103 * Revision 1.1 2001/05/31 21:11:53 jongfoster
104 * - Moved linked list support to new "list.c" file.
105 * Structure definitions are still in project.h,
106 * function prototypes are now in "list.h".
107 * - Added support for "struct list_share", which is identical
108 * to "struct list" except it saves memory by not duplicating
109 * the strings. Obviously, this only works if there is some
110 * other way of managing the memory used by the strings.
111 * (These list_share lists are used for lists which last
112 * for only 1 request, and where all the list entries are
113 * just coming directly from entries in the actionsfile.)
114 * Note that you still need to destroy list_share lists
115 * properly to free the nodes - it's only the strings
119 *********************************************************************/
125 /* FIXME: The following headers are not needed for Win32. Are they
126 * needed on other platforms?
129 #include <sys/types.h>
135 #if !defined(_WIN32) && !defined(__OS2__)
143 #include "miscutil.h"
145 const char list_h_rcs[] = LIST_H_VERSION;
148 static int list_is_valid (const struct list *the_list);
151 /*********************************************************************
153 * Function : list_init
155 * Description : Create a new, empty list in user-allocated memory.
156 * Caller should allocate a "struct list" variable,
157 * then pass it to this function.
158 * (Implementation note: Rather than calling this
159 * function, you can also just memset the memory to
160 * zero, e.g. if you have a larger structure you
161 * want to initialize quickly. However, that isn't
162 * really good design.)
165 * 1 : the_list = pointer to list
169 *********************************************************************/
170 void init_list(struct list *the_list)
172 memset(the_list, '\0', sizeof(*the_list));
176 /*********************************************************************
178 * Function : destroy_list
180 * Description : Destroy a string list (opposite of list_init).
181 * On return, the memory used by the list entries has
182 * been freed, but not the memory used by the_list
183 * itself. You should not re-use the_list without
184 * calling list_init().
186 * (Implementation note: You *can* reuse the_list
187 * without calling list_init(), but please don't.
188 * If you want to remove all entries from a list
189 * and still have a usable list, then use
190 * list_remove_all().)
193 * 1 : the_list = pointer to list
197 *********************************************************************/
198 void destroy_list (struct list *the_list)
200 struct list_entry *cur_entry, *next_entry;
204 for (cur_entry = the_list->first; cur_entry ; cur_entry = next_entry)
206 next_entry = cur_entry->next;
207 freez(cur_entry->str);
211 the_list->first = NULL;
212 the_list->last = NULL;
216 /*********************************************************************
218 * Function : list_is_valid
220 * Description : Check that a string list is valid. The intended
221 * usage is "assert(list_is_valid(the_list))".
222 * Currently this checks that "the_list->last"
223 * is correct, and that the list dosn't contain
224 * circular references. It is likely to crash if
225 * it's passed complete garbage.
228 * 1 : the_list = pointer to list. Must be non-null.
230 * Returns : 1 if list is valid, 0 otherwise.
232 *********************************************************************/
233 static int list_is_valid (const struct list *the_list)
236 * If you don't want this check, just change the line below
237 * from "#if 1" to "#if 0".
240 const struct list_entry *cur_entry;
241 const struct list_entry *last_entry = NULL;
246 for (cur_entry = the_list->first; cur_entry ; cur_entry = cur_entry->next)
248 last_entry = cur_entry;
253 * Just check that this string can be accessed - i.e. it's a valid
256 strlen(cur_entry->str);
260 * Check for looping back to first
262 if ((length != 0) && (cur_entry == the_list->first))
268 * Arbitrarily limit length to prevent infinite loops.
269 * Note that the 1000 limit was hit by a real user in tracker 911950;
270 * removing it for now. Symptoms of a real circular reference will
271 * include 100% CPU usage, I'd imagine. It'll be obvious, anyway.
281 * Check this isn't marked as the last entry, unless of course it's
282 * *really* the last entry.
284 if ((the_list->last == cur_entry) && (cur_entry->next != NULL))
286 /* This is the last entry, but there's data after it !!?? */
291 return (the_list->last == last_entry);
297 /*********************************************************************
301 * Description : Append a string into a specified string list.
304 * 1 : the_list = pointer to list
305 * 2 : str = string to add to the list (maybe NULL)
307 * Returns : JB_ERR_OK on success
308 * JB_ERR_MEMORY on out-of-memory error.
309 * On error, the_list will be unchanged.
311 *********************************************************************/
312 jb_err enlist(struct list *the_list, const char *str)
314 struct list_entry *cur;
317 assert(list_is_valid(the_list));
319 if (NULL == (cur = (struct list_entry *)zalloc(sizeof(*cur))))
321 return JB_ERR_MEMORY;
326 if (NULL == (cur->str = strdup(str)))
329 return JB_ERR_MEMORY;
332 /* else { cur->str = NULL; } - implied by zalloc */
334 /* cur->next = NULL; - implied by zalloc */
338 the_list->last->next = cur;
339 the_list->last = cur;
343 the_list->first = cur;
344 the_list->last = cur;
347 assert(list_is_valid(the_list));
352 /*********************************************************************
354 * Function : enlist_first
356 * Description : Append a string as first element into a specified
360 * 1 : the_list = pointer to list
361 * 2 : str = string to add to the list (maybe NULL)
363 * Returns : JB_ERR_OK on success
364 * JB_ERR_MEMORY on out-of-memory error.
365 * On error, the_list will be unchanged.
367 *********************************************************************/
368 jb_err enlist_first(struct list *the_list, const char *str)
370 struct list_entry *cur;
373 assert(list_is_valid(the_list));
375 if (NULL == (cur = (struct list_entry *)zalloc(sizeof(*cur))))
377 return JB_ERR_MEMORY;
382 if (NULL == (cur->str = strdup(str)))
385 return JB_ERR_MEMORY;
388 /* else { cur->str = NULL; } - implied by zalloc */
390 cur->next = the_list->first;
392 the_list->first = cur;
393 if (the_list->last == NULL)
395 the_list->last = cur;
398 assert(list_is_valid(the_list));
403 /*********************************************************************
405 * Function : enlist_unique
407 * Description : Append a string into a specified string list,
408 * if & only if it's not there already.
409 * If the num_significant_chars argument is nonzero,
410 * only compare up to the nth character.
413 * 1 : the_list = pointer to list
414 * 2 : str = string to add to the list
415 * 3 : num_significant_chars = number of chars to use
416 * for uniqueness test, or 0 to require an exact match.
418 * Returns : JB_ERR_OK on success
419 * JB_ERR_MEMORY on out-of-memory error.
420 * On error, the_list will be unchanged.
421 * "Success" does not indicate whether or not the
422 * item was already in the list.
424 *********************************************************************/
425 jb_err enlist_unique(struct list *the_list, const char *str,
426 size_t num_significant_chars)
428 struct list_entry *cur_entry;
431 assert(list_is_valid(the_list));
433 assert(num_significant_chars >= 0);
434 assert(num_significant_chars <= strlen(str));
436 if (num_significant_chars > 0)
438 for (cur_entry = the_list->first; cur_entry != NULL; cur_entry = cur_entry->next)
440 if ( (cur_entry->str != NULL)
441 && (0 == strncmp(str, cur_entry->str, num_significant_chars)))
450 /* Test whole string */
451 for (cur_entry = the_list->first; cur_entry != NULL; cur_entry = cur_entry->next)
453 if ( (cur_entry->str != NULL) && (0 == strcmp(str, cur_entry->str)))
461 return enlist(the_list, str);
465 /*********************************************************************
467 * Function : enlist_unique_header
469 * Description : Make a HTTP header from the two strings name and value,
470 * and append the result into a specified string list,
471 * if & only if there isn't already a header with that name.
474 * 1 : the_list = pointer to list
475 * 2 : name = HTTP header name (e.g. "Content-type")
476 * 3 : value = HTTP header value (e.g. "text/html")
478 * Returns : JB_ERR_OK on success
479 * JB_ERR_MEMORY on out-of-memory error.
480 * On error, the_list will be unchanged.
481 * "Success" does not indicate whether or not the
482 * header was already in the list.
484 *********************************************************************/
485 jb_err enlist_unique_header(struct list *the_list, const char *name,
493 assert(list_is_valid(the_list));
497 length = strlen(name) + 2;
498 if (NULL == (str = (char *)malloc(length + strlen(value) + 1)))
500 return JB_ERR_MEMORY;
503 str[length - 2] = ':';
504 str[length - 1] = ' ';
505 strcpy(str + length, value);
507 result = enlist_unique(the_list, str, length);
511 assert(list_is_valid(the_list));
518 /*********************************************************************
520 * Function : list_remove_all
522 * Description : Remove all entries from a list. On return, the_list
523 * is a valid, empty list. Note that this is similar
524 * to destroy_list(), but the difference is that this
525 * function guarantees that the list structure is still
526 * valid after the call.
529 * 1 : the_list = pointer to list
533 *********************************************************************/
534 void list_remove_all(struct list *the_list)
536 struct list_entry *cur_entry;
537 struct list_entry *next_entry;
540 assert(list_is_valid(the_list));
542 for (cur_entry = the_list->first; cur_entry ; cur_entry = next_entry)
544 next_entry = cur_entry->next;
545 freez(cur_entry->str);
549 the_list->first = the_list->last = NULL;
551 assert(list_is_valid(the_list));
555 /*********************************************************************
557 * Function : list_to_text
559 * Description : "Flatten" a string list into 1 long \r\n delimited string,
560 * adding an empty line at the end. NULL entries are ignored.
561 * This function does not change the_list.
564 * 1 : the_list = pointer to list
566 * Returns : NULL on malloc error, else new long string.
567 * Caller must free() it.
569 *********************************************************************/
570 char *list_to_text(const struct list *the_list)
572 struct list_entry *cur_entry;
578 assert(list_is_valid(the_list));
580 for (cur_entry = the_list->first; cur_entry ; cur_entry = cur_entry->next)
584 size += strlen(cur_entry->str) + 2;
588 if ((ret = (char *)malloc(size + 1)) == NULL)
597 for (cur_entry = the_list->first; cur_entry ; cur_entry = cur_entry->next)
601 strcpy(s, cur_entry->str);
603 *s++ = '\r'; *s++ = '\n';
606 *s++ = '\r'; *s++ = '\n';
612 /*********************************************************************
614 * Function : list_remove_item
616 * Description : Remove a string from a specified string list.
619 * 1 : the_list = pointer to list
620 * 2 : str = string to remove from the list - non-NULL
622 * Returns : Number of times it was removed.
624 *********************************************************************/
625 int list_remove_item(struct list *the_list, const char *str)
627 struct list_entry *prev = NULL;
628 struct list_entry *cur;
629 struct list_entry *next;
633 assert(list_is_valid(the_list));
636 cur = the_list->first;
642 if ((cur->str != NULL) && (0 == strcmp(str, cur->str)))
652 the_list->first = next;
654 free((char *)cur->str);
664 the_list->last = prev;
666 assert(list_is_valid(the_list));
672 /*********************************************************************
674 * Function : list_remove_list
676 * Description : Remove all strings in one list from another list.
677 * This is currently a brute-force algorithm
678 * (it compares every pair of strings).
681 * 1 : dest = list to change
682 * 2 : src = list of strings to remove
684 * Returns : Total number of strings removed.
686 *********************************************************************/
687 int list_remove_list(struct list *dest, const struct list *src)
689 struct list_entry *cur;
694 assert(list_is_valid(src));
695 assert(list_is_valid(dest));
697 for (cur = src->first; cur != NULL; cur = cur->next)
699 if (cur->str != NULL)
701 count += list_remove_item(dest, cur->str);
705 assert(list_is_valid(src));
706 assert(list_is_valid(dest));
712 /*********************************************************************
714 * Function : list_duplicate
716 * Description : Copy a string list
719 * 1 : dest = Destination list. Must be a valid list.
720 * All existing entries will be removed.
721 * 1 : src = pointer to source list for copy.
723 * Returns : JB_ERR_OK on success
724 * JB_ERR_MEMORY on out-of-memory error.
725 * On error, dest will be empty.
727 *********************************************************************/
728 jb_err list_duplicate(struct list *dest, const struct list *src)
730 struct list_entry * cur_src;
731 struct list_entry * cur_dest;
735 assert(list_is_valid(src));
736 assert(list_is_valid(dest));
738 list_remove_all(dest);
740 /* Need to process first entry specially so we can set dest->first */
741 cur_src = src->first;
744 cur_dest = dest->first = (struct list_entry *)zalloc(sizeof(*cur_dest));
745 if (cur_dest == NULL)
749 assert(list_is_valid(src));
750 assert(list_is_valid(dest));
752 return JB_ERR_MEMORY;
757 cur_dest->str = strdup(cur_src->str);
758 if (cur_dest->str == NULL)
762 assert(list_is_valid(src));
763 assert(list_is_valid(dest));
765 return JB_ERR_MEMORY;
768 /* else { cur_dest->str = NULL; } - implied by zalloc */
770 /* Now process the rest */
771 for (cur_src = cur_src->next; cur_src; cur_src = cur_src->next)
773 cur_dest = cur_dest->next = (struct list_entry *)zalloc(sizeof(*cur_dest));
774 if (cur_dest == NULL)
778 assert(list_is_valid(src));
779 assert(list_is_valid(dest));
781 return JB_ERR_MEMORY;
785 cur_dest->str = strdup(cur_src->str);
786 if (cur_dest->str == NULL)
790 assert(list_is_valid(src));
791 assert(list_is_valid(dest));
793 return JB_ERR_MEMORY;
796 /* else { cur_dest->str = NULL; } - implied by zalloc */
799 dest->last = cur_dest;
802 assert(list_is_valid(src));
803 assert(list_is_valid(dest));
809 /*********************************************************************
811 * Function : list_append_list_unique
813 * Description : Append a string list to another list.
814 * Duplicate items are not added.
817 * 1 : dest = pointer to destination list for merge.
818 * 2 : src = pointer to source for merge.
820 * Returns : JB_ERR_OK on success
821 * JB_ERR_MEMORY on out-of-memory error.
822 * On error, some (but not all) of src might have
823 * been copied into dest.
825 *********************************************************************/
826 jb_err list_append_list_unique(struct list *dest, const struct list *src)
828 struct list_entry * cur;
832 assert(list_is_valid(src));
833 assert(list_is_valid(dest));
835 for (cur = src->first; cur; cur = cur->next)
839 if (enlist_unique(dest, cur->str, 0))
841 assert(list_is_valid(src));
842 assert(list_is_valid(dest));
844 return JB_ERR_MEMORY;
849 assert(list_is_valid(src));
850 assert(list_is_valid(dest));
856 /*********************************************************************
858 * Function : list_is_empty
860 * Description : Test whether a list is empty. Does not change the list.
863 * 1 : the_list = pointer to list to test.
865 * Returns : Nonzero iff the list contains no entries.
867 *********************************************************************/
868 int list_is_empty(const struct list *the_list)
871 assert(list_is_valid(the_list));
873 return (the_list->first == NULL);
877 /*********************************************************************
881 * Description : Create a new, empty map.
885 * Returns : A new, empty map, or NULL if out of memory.
887 *********************************************************************/
888 struct map *new_map(void)
890 return (struct map *) zalloc(sizeof(struct map));
894 /*********************************************************************
896 * Function : free_map
898 * Description : Free the memory occupied by a map and its
902 * 1 : the_map = map to be freed. May be NULL.
906 *********************************************************************/
907 void free_map(struct map *the_map)
909 struct map_entry *cur_entry;
910 struct map_entry *next_entry;
917 for (cur_entry = the_map->first; cur_entry != NULL; cur_entry = next_entry)
919 freez(cur_entry->name);
920 freez(cur_entry->value);
922 next_entry = cur_entry->next;
926 the_map->first = the_map->last = NULL;
932 /*********************************************************************
936 * Description : Add a mapping from given name to given value to a
939 * Note: Since all strings will be free()d in free_map()
940 * later, set the copy flags for constants or
941 * strings that will be independantly free()d.
943 * Note2: This function allows NULL parameters - it
944 * returns JB_ERR_MEMORY in that case.
946 * Note3: If this function returns JB_ERR_MEMORY,
947 * it will free(name) unless you specify
948 * name_needs_copying, and similarly it will
949 * free(value) unless you specify
950 * value_needs_copying.
952 * Due to Note2 and Note3 above, the following code
953 * is legal, and will never crash or leak memory even
954 * if the system runs out of memory:
956 * err = map(mymap, "xyz", 1, html_encode(somestring), 0);
958 * err will be set to JB_ERR_MEMORY if either call runs
959 * out-of-memory. Without these features, you would
960 * need to check the return value of html_encode in the
961 * above example for NULL, which (at least) doubles the
962 * amount of error-checking code needed.
965 * 1 : the_map = map to add to
966 * 2 : name = name to add
967 * 3 : name_needs_copying = flag set if a copy of name should be used
968 * 4 : value = value to add
969 * 5 : value_needs_copying = flag set if a copy of value should be used
971 * Returns : JB_ERR_OK on success
972 * JB_ERR_MEMORY on out-of-memory error.
974 *********************************************************************/
975 jb_err map(struct map *the_map,
976 const char *name, int name_needs_copying,
977 const char *value, int value_needs_copying)
979 struct map_entry *new_entry;
985 || (NULL == (new_entry = zalloc(sizeof(*new_entry)))) )
987 if ((name != NULL) && (!name_needs_copying))
991 if ((value != NULL) && (!value_needs_copying))
995 return JB_ERR_MEMORY;
998 if (name_needs_copying)
1000 if (NULL == (name = strdup(name)))
1003 if (!value_needs_copying)
1005 free((char *)value);
1007 return JB_ERR_MEMORY;
1011 if (value_needs_copying)
1013 if (NULL == (value = strdup(value)))
1017 return JB_ERR_MEMORY;
1021 new_entry->name = name;
1022 new_entry->value = value;
1023 /* new_entry->next = NULL; - implied by zalloc */
1027 the_map->last->next = new_entry;
1028 the_map->last = new_entry;
1032 the_map->first = new_entry;
1033 the_map->last = new_entry;
1040 /*********************************************************************
1044 * Description : Remove all map_entry structs with a given name from
1048 * 1 : the_map = map to look in
1049 * 2 : name = name to unmap
1051 * Returns : JB_ERR_OK
1053 *********************************************************************/
1054 jb_err unmap(struct map *the_map, const char *name)
1056 struct map_entry *cur_entry, *last_entry;
1061 last_entry = the_map->first;
1063 for (cur_entry = the_map->first; cur_entry != NULL; cur_entry = cur_entry->next)
1065 if (!strcmp(name, cur_entry->name))
1068 * Update the incoming pointer
1070 if (cur_entry == the_map->first)
1072 the_map->first = cur_entry->next;
1076 last_entry->next = cur_entry->next;
1080 * Update the map's last pointer
1082 if (cur_entry == the_map->last)
1084 the_map->last = last_entry;
1088 * Free the map_entry
1090 freez(cur_entry->name);
1091 freez(cur_entry->value);
1094 cur_entry = last_entry;
1098 last_entry = cur_entry;
1105 /*********************************************************************
1109 * Description : Look up an item with a given name in a map, and
1113 * 1 : the_map = map to look in
1114 * 2 : name = name parameter to look for
1116 * Returns : the value if found, else the empty string.
1117 * Return value is alloced as part of the map, so
1118 * it is freed when the map is destroyed. Caller
1119 * must not free or modify it.
1121 *********************************************************************/
1122 const char *lookup(const struct map *the_map, const char *name)
1124 const struct map_entry *cur_entry;
1129 for (cur_entry = the_map->first; cur_entry != NULL; cur_entry = cur_entry->next)
1131 if (!strcmp(name, cur_entry->name))
1133 return cur_entry->value;