main.c - randomcrap - random crap programs of varying quality
 (HTM) git clone git://git.codemadness.org/randomcrap
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
       main.c (3735B)
       ---
            1 #include <ctype.h>
            2 #include <stdio.h>
            3 #include <string.h>
            4 
            5 #include "xml.h"
            6 
            7 #define LEN(x) (sizeof((x)) / sizeof((x[0])))
            8 
            9 struct pkg {
           10         char names[256];
           11         char ranges[256];
           12 };
           13 
           14 struct vuln {
           15         char topic[1024];
           16 
           17         struct pkg pkgs[32];
           18         int npkg;
           19 
           20         char discoverydate[64];
           21         char entrydate[64];
           22 
           23         char cves[1024];
           24         char urls[4096];
           25 };
           26 
           27 static struct vuln v;
           28 static int invuln, inrange;
           29 
           30 /* TODO: optimize */
           31 void
           32 trim(char *s)
           33 {
           34         char *sp;
           35         int len;
           36 
           37         for (sp = s; isspace((unsigned char)*sp); sp++)
           38                 ;
           39 
           40         if (sp != s) {
           41                 len = strlen(s) - (sp - s);
           42                 memmove(s, sp, len + 1); /* copy + NUL byte */
           43         }
           44         for (sp = s + strlen(s); sp > s; sp--) {
           45                 if (!isspace((unsigned char)sp[-1]))
           46                         break;
           47                 sp[-1] = '\0';
           48         }
           49 
           50         for (; *s; s++) {
           51                 if (iscntrl((unsigned char)*s))
           52                         *s = ' ';
           53         }
           54 }
           55 
           56 void
           57 xmltagstart(XMLParser *x, const char *t, size_t tl)
           58 {
           59         const char *op;
           60 
           61         if (!invuln && !strcmp(t, "vuln")) {
           62                 invuln = 1;
           63                 memset(&v, 0, sizeof(v));
           64         }
           65 
           66         if (inrange) {
           67                 if (v.npkg >= LEN(v.pkgs))
           68                         return;
           69                 op = NULL;
           70                 if (!strcmp(t, "lt"))
           71                         op = "<";
           72                 else if (!strcmp(t, "le"))
           73                         op = "<=";
           74                 else if (!strcmp(t, "eq"))
           75                         op = "==";
           76                 else if (!strcmp(t, "ge"))
           77                         op = ">=";
           78                 else if (!strcmp(t, "gt"))
           79                         op = ">";
           80                 if (op)
           81                         strlcat(v.pkgs[v.npkg].ranges, op, sizeof(v.pkgs[v.npkg].ranges));
           82         }
           83 
           84         if (invuln) {
           85                 /* append multiple fields, separated by space */
           86                 if (!strcmp(t, "name")) {
           87                         if (v.npkg >= LEN(v.pkgs))
           88                                 return;
           89 
           90                         if (v.pkgs[v.npkg].names[0])
           91                                 strlcat(v.pkgs[v.npkg].names, " ", sizeof(v.pkgs[v.npkg].names));
           92                 } else if (!strcmp(t, "range")) {
           93                         inrange = 1;
           94 
           95                         if (v.npkg >= LEN(v.pkgs))
           96                                 return;
           97 
           98                         if (v.pkgs[v.npkg].ranges[0])
           99                                 strlcat(v.pkgs[v.npkg].ranges, " ", sizeof(v.pkgs[v.npkg].ranges));
          100                 } else if (!strcmp(t, "cvename")) {
          101                         if (v.cves[0])
          102                                 strlcat(v.cves,  " ", sizeof(v.cves));
          103                 } else if (!strcmp(t, "url")) {
          104                         if (v.urls[0])
          105                                 strlcat(v.urls, " ", sizeof(v.urls));
          106                 }
          107         }
          108 }
          109 
          110 void
          111 xmltagend(XMLParser *x, const char *t, size_t tl, int isshort)
          112 {
          113         int i;
          114 
          115         if (!strcmp(t, "package")) {
          116                 v.npkg++;
          117         }
          118 
          119         if (!strcmp(t, "range")) {
          120                 inrange = 0;
          121         } else if (!strcmp(t, "vuln")) {
          122                 invuln = 0;
          123 
          124                 if (!v.pkgs[0].names[0])
          125                         return; /* cancelled or no name */
          126 
          127                 trim(v.discoverydate);
          128                 trim(v.entrydate);
          129                 trim(v.topic);
          130                 trim(v.cves);
          131                 trim(v.urls);
          132 
          133                 for (i = 0; i < v.npkg; i++) {
          134                         trim(v.pkgs[i].names);
          135                         trim(v.pkgs[i].ranges);
          136 
          137                         printf("%s\t%s\t%s\t%s\t%s\t%s\t%s\n", v.pkgs[i].names, v.pkgs[i].ranges,
          138                                v.discoverydate, v.entrydate, v.topic, v.cves, v.urls);
          139                 }
          140         }
          141 }
          142 
          143 void
          144 xmldata(XMLParser *x, const char *d, size_t dl)
          145 {
          146         if (!invuln)
          147                 return;
          148 
          149         if (inrange) {
          150                 /* must be in tag eq, le, ge, lt etc */
          151                 if (x->tag[0] == '/' || x->taglen != 2 || v.npkg >= LEN(v.pkgs))
          152                         return;
          153 
          154                 strlcat(v.pkgs[v.npkg].ranges, d, sizeof(v.pkgs[v.npkg].ranges));
          155         }
          156 
          157         if (!strcmp(x->tag, "name")) {
          158                 if (v.npkg < LEN(v.pkgs))
          159                         strlcat(v.pkgs[v.npkg].names, d, sizeof(v.pkgs[v.npkg].names));
          160         } else if (!strcmp(x->tag, "topic")) {
          161                 strlcat(v.topic, d, sizeof(v.topic));
          162         } else if (!strcmp(x->tag, "discovery")) {
          163                 strlcat(v.discoverydate, d, sizeof(v.discoverydate));
          164         } else if (!strcmp(x->tag, "entry")) {
          165                 strlcat(v.entrydate, d, sizeof(v.entrydate));
          166         } else if (!strcmp(x->tag, "cvename")) {
          167                 strlcat(v.cves, d, sizeof(v.cves));
          168         } else if (!strcmp(x->tag, "url")) {
          169                 strlcat(v.urls, d, sizeof(v.urls));
          170         }
          171 }
          172 
          173 void
          174 xmldataentity(XMLParser *x, const char *d, size_t dl)
          175 {
          176         xmldata(x, d, dl); /* TODO, convert entity */
          177 }
          178 
          179 int
          180 main(void)
          181 {
          182         XMLParser x = { 0 };
          183 
          184         x.xmltagstart = xmltagstart;
          185         x.xmltagend = xmltagend;
          186         x.xmldata = xmldata;
          187         x.xmldataentity = xmldataentity;
          188 
          189         /*x.getnext = getchar;*/
          190 
          191         xml_parse(&x);
          192 
          193         return 0;
          194 }