Line data Source code
1 : #include "burp.h"
2 : #include "alloc.h"
3 : #include "log.h"
4 : #include "pathcmp.h"
5 : #include "regexp.h"
6 : #include "strlist.h"
7 :
8 : // Maybe rename this stuff to 'struct pathlist'.
9 :
10 853 : static void strlist_free(struct strlist *strlist)
11 : {
12 853 : if(!strlist) return;
13 853 : regex_free(&strlist->re);
14 853 : free_w(&strlist->path);
15 853 : free_v((void **)&strlist);
16 : }
17 :
18 13110 : void strlists_free(struct strlist **strlist)
19 : {
20 : struct strlist *s;
21 : struct strlist *shead;
22 13110 : if(!strlist) return;
23 13104 : shead=*strlist;
24 27057 : while(shead)
25 : {
26 849 : s=shead;
27 849 : shead=shead->next;
28 849 : strlist_free(s);
29 : }
30 13104 : *strlist=NULL;
31 : }
32 :
33 853 : static struct strlist *strlist_alloc(const char *path, long flag)
34 : {
35 853 : struct strlist *slnew=NULL;
36 853 : if(!path)
37 : {
38 0 : logp("%s called with NULL path!\n", __func__);
39 0 : return NULL;
40 : }
41 853 : if(!(slnew=(struct strlist *)
42 : calloc_w(1, sizeof(struct strlist), __func__))
43 853 : || !(slnew->path=strdup_w(path, __func__)))
44 : return NULL;
45 853 : slnew->flag=flag;
46 853 : return slnew;
47 : }
48 :
49 853 : static int do_strlist_add(struct strlist **strlist,
50 : const char *path, long flag, int sorted, int uniq)
51 : {
52 853 : int p=0;
53 853 : struct strlist *s=NULL;
54 853 : struct strlist *slast=NULL;
55 853 : struct strlist *slnew=NULL;
56 :
57 853 : if(!(slnew=strlist_alloc(path, flag))) return -1;
58 :
59 : // Insert into a sorted position in the list, or if the sorted flag
60 : // was zero, add to the end of the list.
61 : // FIX THIS: Unsorted means that it goes through the whole list to
62 : // find the last entry. Can this be made better?
63 1303 : for(s=*strlist; s; s=s->next)
64 : {
65 475 : if(uniq && !pathcmp(path, s->path) && flag==s->flag)
66 : {
67 4 : strlist_free(slnew);
68 4 : return 0;
69 : }
70 471 : if(sorted) {
71 140 : if((p=pathcmp(path, s->path))<0)
72 : break;
73 120 : if(!p && flag<s->flag)
74 : break;
75 : }
76 450 : slast=s;
77 : }
78 849 : if(slast)
79 : {
80 248 : slnew->next=slast->next;
81 248 : slast->next=slnew;
82 : }
83 : else
84 : {
85 601 : *strlist=slnew;
86 601 : slnew->next=s;
87 : }
88 :
89 : return 0;
90 : }
91 :
92 645 : int strlist_add(struct strlist **strlist,
93 : const char *path, long flag)
94 : {
95 645 : return do_strlist_add(strlist, path, flag, 0 /* unsorted */, 0 /* not uniq */);
96 : }
97 :
98 158 : int strlist_add_sorted(struct strlist **strlist,
99 : const char *path, long flag)
100 : {
101 158 : return do_strlist_add(strlist, path, flag, 1 /* sorted */, 0 /* not uniq */);
102 : }
103 :
104 50 : int strlist_add_sorted_uniq(struct strlist **strlist,
105 : const char *path, long flag)
106 : {
107 50 : return do_strlist_add(strlist, path, flag, 1 /* sorted */, 1 /* uniq */);
108 : }
109 :
110 358 : int strlist_compile_regexes(struct strlist *strlist)
111 : {
112 : struct strlist *l;
113 : // FIX THIS: when the regex does not compile, should remove the
114 : // strlist entry completely.
115 362 : for(l=strlist; l; l=l->next)
116 4 : if(!(l->re=regex_compile_backup(l->path)))
117 0 : logp("unable to compile regex: %s\n", l->path);
118 358 : return 0;
119 : }
120 :
121 6 : int strlist_find(struct strlist *strlist, const char *path, long flag)
122 : {
123 : struct strlist *s;
124 14 : for(s=strlist; s; s=s->next)
125 8 : if(!strcmp(path, s->path) && flag==s->flag)
126 : return 1;
127 : return 0;
128 : }
|