LCOV - code coverage report
Current view: top level - src - strlist.c (source / functions) Hit Total Coverage
Test: burp-coverage-clean.info Lines: 49 52 94.2 %
Date: 2020-08-31 23:02:24 Functions: 9 9 100.0 %

          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         979 : static void strlist_free(struct strlist *strlist)
      11             : {
      12         979 :         if(!strlist) return;
      13         979 :         regex_free(&strlist->re);
      14         979 :         free_w(&strlist->path);
      15         979 :         free_v((void **)&strlist);
      16             : }
      17             : 
      18       16018 : void strlists_free(struct strlist **strlist)
      19             : {
      20             :         struct strlist *s;
      21             :         struct strlist *shead;
      22       16018 :         if(!strlist) return;
      23       16006 :         shead=*strlist;
      24       32990 :         while(shead)
      25             :         {
      26         978 :                 s=shead;
      27         978 :                 shead=shead->next;
      28         978 :                 strlist_free(s);
      29             :         }
      30       16006 :         *strlist=NULL;
      31             : }
      32             : 
      33         979 : static struct strlist *strlist_alloc(const char *path, long flag)
      34             : {
      35         979 :         struct strlist *slnew=NULL;
      36         979 :         if(!path)
      37             :         {
      38           0 :                 logp("%s called with NULL path!\n", __func__);
      39           0 :                 return NULL;
      40             :         }
      41         979 :         if(!(slnew=(struct strlist *)
      42             :                 calloc_w(1, sizeof(struct strlist), __func__))
      43         979 :           || !(slnew->path=strdup_w(path, __func__)))
      44             :                 return NULL;
      45         979 :         slnew->flag=flag;
      46         979 :         return slnew;
      47             : }
      48             : 
      49         979 : static int do_strlist_add(struct strlist **strlist,
      50             :         const char *path, long flag, int sorted, int uniq)
      51             : {
      52         979 :         struct strlist *s=NULL;
      53         979 :         struct strlist *slast=NULL;
      54         979 :         struct strlist *slnew=NULL;
      55             : 
      56         979 :         if(!(slnew=strlist_alloc(path, flag))) return -1;
      57             : 
      58             :         // Insert into a sorted position in the list, or if the sorted flag
      59             :         // was zero, add to the end of the list.
      60             :         // FIX THIS: Unsorted means that it goes through the whole list to
      61             :         // find the last entry. Can this be made better?
      62        1492 :         for(s=*strlist; s; s=s->next)
      63             :         {
      64         535 :                 if(uniq && !pathcmp(path, s->path))
      65             :                 {
      66           1 :                         strlist_free(slnew);
      67           1 :                         return 0;
      68             :                 }
      69         534 :                 if(sorted && pathcmp(path, s->path)<0) break;
      70         513 :                 slast=s;
      71             :         }
      72         978 :         if(slast)
      73             :         {
      74         301 :                 slnew->next=slast->next;
      75         301 :                 slast->next=slnew;
      76             :         }
      77             :         else
      78             :         {
      79         677 :                 *strlist=slnew;
      80         677 :                 slnew->next=s;
      81             :         }
      82             : 
      83             :         return 0;
      84             : }
      85             : 
      86         775 : int strlist_add(struct strlist **strlist,
      87             :         const char *path, long flag)
      88             : {
      89         775 :         return do_strlist_add(strlist, path, flag, 0 /* unsorted */, 0 /* not uniq */);
      90             : }
      91             : 
      92         201 : int strlist_add_sorted(struct strlist **strlist,
      93             :         const char *path, long flag)
      94             : {
      95         201 :         return do_strlist_add(strlist, path, flag, 1 /* sorted */, 0 /* not uniq */);
      96             : }
      97             : 
      98           3 : int strlist_add_sorted_uniq(struct strlist **strlist,
      99             :         const char *path, long flag)
     100             : {
     101           3 :         return do_strlist_add(strlist, path, flag, 1 /* sorted */, 1 /* uniq */);
     102             : }
     103             : 
     104         378 : int strlist_compile_regexes(struct strlist *strlist)
     105             : {
     106             :         struct strlist *l;
     107             :         // FIX THIS: when the regex does not compile, should remove the
     108             :         // strlist entry completely.
     109         382 :         for(l=strlist; l; l=l->next)
     110           4 :                 if(!(l->re=regex_compile(l->path)))
     111           0 :                         logp("unable to compile regex: %s\n", l->path);
     112         378 :         return 0;
     113             : }
     114             : 
     115           6 : int strlist_find(struct strlist *strlist, const char *path, long flag)
     116             : {
     117             :         struct strlist *s;
     118          14 :         for(s=strlist; s; s=s->next)
     119           8 :                 if(!strcmp(path, s->path) && flag==s->flag)
     120             :                         return 1;
     121             :         return 0;
     122             : }

Generated by: LCOV version 1.13