summaryrefslogtreecommitdiffstats
path: root/WDL/xmlparse.h
blob: 177176eba0a41c68bc9a1f7f3a1281618627019d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
/*
    WDL - xmlparse.h
    Copyright (C) 2016 and later, Cockos Incorporated
  
    This software is provided 'as-is', without any express or implied
    warranty.  In no event will the authors be held liable for any damages
    arising from the use of this software.

    Permission is granted to anyone to use this software for any purpose,
    including commercial applications, and to alter it and redistribute it
    freely, subject to the following restrictions:

    1. The origin of this software must not be misrepresented; you must not
       claim that you wrote the original software. If you use this software
       in a product, an acknowledgment in the product documentation would be
       appreciated but is not required.
    2. Altered source versions must be plainly marked as such, and must not be
       misrepresented as being the original software.
    3. This notice may not be removed or altered from any source distribution.
      
very very very lightweight XML parser

reads: <?xml, <!DOCTYPE, <![CDATA[, &lt;&gt;&amp;&quot;&apos;&#xABC;&#123; top level <? blocks, ignores unknown <?tag blocks?> inside elements
always uses 8-bit characters, uses UTF-8 encoding for &#xyz
relatively strict. for overflow safety, enforces a token length limit of 512MB

*/

#ifndef _WDL_XML_PARSE_H_
#define _WDL_XML_PARSE_H_
#include "ptrlist.h"
#include "assocarray.h"
#include "wdlstring.h"
#include "wdlutf8.h"
#include "wdlcstring.h"

class wdl_xml_element {
    static void attr_free(char *a) { free(a); }
  public:
    wdl_xml_element(const char *_name, int _line, int _col, bool _sort_attr=true) : 
      elements(32),
      attributes(WDL_assocarray_cmpstr<char>,NULL,attr_free,attr_free), name(strdup(_name)), line(_line), col(_col),
      m_sort_attributes(_sort_attr), m_has_discrete_close(false) {
        attributes.SetGranul(32);
      }
    ~wdl_xml_element() { free(name); elements.Empty(true); }

    WDL_PtrList<wdl_xml_element> elements;
    WDL_AssocArray<char *, char *> attributes;
    WDL_FastString value; // value excluding any leading whitespace and excluding any elements

    char *name;
    int line, col;
    bool m_sort_attributes;
    bool m_has_discrete_close;

    const char *get_attribute(const char *v, const char *def=NULL) const
    {
      if (!m_sort_attributes)
      {
        const int n = attributes.GetSize();
        for (int x = 0; x < n; x ++) 
        {
          char *key = NULL;
          const char *val = attributes.Enumerate(x,&key);
          if (key && !strcmp(key,v)) return val;
        }
      }
      return attributes.Get((char*)v,(char*)def);
    }

    wdl_xml_element *get_element_by_name(const char *searchname, int cnt=0)
    {
      for (int x = 0; x < elements.GetSize(); x ++)
        if (elements.Get(x)->name && !strcmp(searchname,elements.Get(x)->name) && !cnt--)
          return elements.Get(x);
      return NULL;
    }
};

class wdl_xml_parser {
  public:
    wdl_xml_parser(const char *rdptr, int rdptr_len, bool sort_attributes=true) : 
      element_xml(NULL), element_root(NULL), 
      m_rdptr((const unsigned char *)rdptr), m_err(NULL),
      m_rdptr_len(rdptr_len), m_line(1), m_col(0), m_lastchar(0), 
      m_last_line(1),m_last_col(0),
      m_sort_attributes(sort_attributes)
    { 
    }
    virtual ~wdl_xml_parser() 
    { 
      delete element_xml; 
      delete element_root; 
      element_doctype_tokens.Empty(true,free); 
      element_root_meta.Empty(true);
    }

    const char *parse() // call only once, returns NULL on success, error message on failure
    {
      m_lastchar = nextchar();

      if (!m_tok.ResizeOK(256)) return "token buffer malloc fail";

      const char *p = parse_element_body(NULL);
      if (!m_err)
      {
        if (!p) return NULL;
        snprintf(m_errbuf,sizeof(m_errbuf),"%s at %d:%d",p,m_line,m_col);
        return m_errbuf;
      }
      if (!*m_err) m_err="unexpected end of file";
      if (!p) return m_err;
      snprintf(m_errbuf,sizeof(m_errbuf),"%s: %s at %d:%d",p,m_err,m_line,m_col);
      return m_errbuf;
    }

    // output
    WDL_PtrList<char> element_doctype_tokens; // tokens after <!DOCTYPE
    wdl_xml_element *element_xml, *element_root;

    WDL_PtrList<wdl_xml_element> element_root_meta; // any topen level <? elements?> other than <?xml which goes into element_xml

    // get location  after parse() returns error
    int getLine() const { return m_last_line; }
    int getCol() const { return m_last_col; }


  private:

    WDL_HeapBuf m_tok;
    const unsigned char *m_rdptr;
    const char *m_err; // NULL if no error, "" if EOF
    char m_errbuf[128];
    int m_rdptr_len, m_line, m_col, m_lastchar, m_last_line,m_last_col;
    bool m_sort_attributes;

    virtual int moredata(const char **dataOut) { return 0; }

    int nextchar()
    {
      if (m_rdptr_len < 1 && (m_rdptr_len = moredata((const char **)&m_rdptr)) < 1) return -1;

      m_rdptr_len--;
      const int ret = (int)*m_rdptr++;

      if (ret == '\n') { m_line++; m_col=0; }
      else m_col++;

      return ret;
    }

    int skip_whitespace()
    {
      int rv=0, lc = m_lastchar;
      while (char_type(lc) < 0) { lc = nextchar(); rv++; }
      m_lastchar = lc;
      return rv;
    }

    static int char_type(int c)
    {
      switch (c)
      {
        case ' ': case '\r': case '\n': case '\t':
          return -1;

        case '/': case '!': case '\\': case '\'': case '"': case '#': case '$': 
        case '%': case '(': case ')': case '*': case '+': case ',': case ';': 
        case '=': case '>': case '?': case '@': case '[': case ']': case '^': 
        case '`': case '{': case '|': case '}': case '~':
          return 1;

        case '<': case '&':
          return 2;

        case '-': case '.': 
          return 4;
      }
      return 0;
    }

    unsigned char *realloc_tok(int &tok_sz)
    {
      tok_sz += tok_sz + tok_sz / 4;
      if (tok_sz >= (1<<29)) 
      {
        m_err="token buffer tried to malloc() more than 512MB, probably unsafe and invalid XML";
        return NULL;
      }
      unsigned char *t = (unsigned char *) m_tok.ResizeOK(tok_sz);
      if (!t) m_err="token buffer malloc fail";
      return t;
    }

    // gets a token, normally skipping whitespace, but if get_tok(true), then return NULL on whitespace
    const char *get_tok(bool no_skip_whitespace=false)
    {
      if (!no_skip_whitespace) skip_whitespace();

      m_last_line = m_line;
      m_last_col = m_col;

      int wrpos=0, lc = m_lastchar, tok_sz = m_tok.GetSize();
      unsigned char *tok_buf = (unsigned char *)m_tok.Get();
      switch (lc > 0 ? char_type(lc) : -2)
      {
        case 0:
          do
          {
            tok_buf[wrpos++] = lc;
            if (WDL_unlikely(wrpos >= tok_sz) && WDL_unlikely(!(tok_buf=realloc_tok(tok_sz)))) return NULL;
            lc = nextchar();
          }
          while (lc > 0 && !(char_type(lc)&~4));
        break;

        case 1:
        case 2:
        case 4:
          if (lc == '\'' || lc == '\"')
          {
            const int endc = lc;
            tok_buf[wrpos++] = lc;
            lc = nextchar();
            while (lc > 0)
            {
              if (lc == '<')
              {
                m_last_line=m_line; m_last_col=m_col;
                m_err="illegal '<' character in quoted string";
                m_lastchar = lc;
                return NULL;
              }

              if (lc == '&')
              {
                m_lastchar = lc;
                if (WDL_unlikely(wrpos+8 >= tok_sz) && WDL_unlikely(!(tok_buf=realloc_tok(tok_sz)))) return NULL;

                const int tmp[2]={m_line,m_col};
                if (!decode_entity((char*)tok_buf+wrpos)) 
                {
                  m_last_line=tmp[0]; m_last_col=tmp[1];
                  m_err="unknown entity in quoted string";
                  return NULL;
                }
                lc = m_lastchar;
                while (tok_buf[wrpos]) wrpos++;
              }
              else
              {
                const int llc = lc;
                tok_buf[wrpos++] = lc;
                if (WDL_unlikely(wrpos >= tok_sz) && WDL_unlikely(!(tok_buf=realloc_tok(tok_sz)))) return NULL;
                lc = nextchar();

                if (llc == endc) break;
              }
            }
          }
          else
          {
            tok_buf[wrpos++] = lc;
            if (WDL_unlikely(wrpos >= tok_sz) && WDL_unlikely(!(tok_buf=realloc_tok(tok_sz)))) return NULL;

            lc = nextchar();
          }
        break;
        case -1:
          m_err="unexpected whitespace";
        return NULL; 
        default: 
          m_err=""; // EOF
        return NULL; 
      }
      tok_buf[wrpos]=0;
      m_lastchar = lc;
      return (char *)tok_buf;
    }

    bool decode_entity(char *wr) // will never write more than 8 bytes
    {
      char tmp[32];
      int i=0;
      while (i < 31 && (m_lastchar = nextchar()) > 0 && m_lastchar != ';')
      {
        if (char_type(m_lastchar) && m_lastchar != '#') break;
        tmp[i++] = m_lastchar;
      }
      int byteval = 0;
      if (m_lastchar == ';') 
      {
        tmp[i]=0;
        if (!strcmp(tmp,"lt")) byteval = '<';
        else if (!strcmp(tmp,"gt")) byteval = '>';
        else if (!strcmp(tmp,"amp")) byteval = '&';
        else if (!strcmp(tmp,"apos")) byteval = '\'';
        else if (!strcmp(tmp,"quot")) byteval = '"';
        else if (tmp[0] == '#')
        {
          if (tmp[1] >= '0' && tmp[1] <= '9') byteval = atoi(tmp+1);
          if (tmp[1] == 'x') byteval = strtol(tmp+2,NULL,16);
        }
      }
      if (!byteval) return false;
      WDL_MakeUTFChar((char*)wr,byteval,8);
      m_lastchar = nextchar();
      return true;
    }

    bool skip_until(const char *s) // raw search, no tokenization
    {
      int state = 0, c = m_lastchar;
      while (c>0 && s[state])
      {
        state = (state && c == (unsigned char)s[state]) ? (state+1) : (c == (unsigned char)s[0]);
        c = nextchar();
      }
      m_lastchar = c;
      return !s[state];
    }

    const char *parse_element_attributes(wdl_xml_element *elem)
    {
      char *attr_name=NULL;
      for (;;)
      {
        const char *tok = get_tok();
        if (!tok) break;

        if (*tok == '-' || *tok == '.' || (*tok >= '0' && *tok <= '9')) { m_err="attribute must not begin with .- or number"; break; }

        if (char_type(*tok)) return tok;

        attr_name = strdup(tok);
        if (!attr_name) { m_err="malloc fail"; break; }

        if (m_sort_attributes && 
            elem->attributes.Get(attr_name)) 
        { 
          m_err="attribute specified more than once"; 
          break; 
        }

        tok = get_tok();
        if (!tok) break;
        if (*tok != '=') { m_err="attribute name must be followed by '='"; break; }

        tok = get_tok();
        if (!tok) break;
        if (*tok != '\'' && *tok != '"') { m_err="attribute value must be quoted string"; break; }

        const size_t tok_len = strlen(tok);
        if (tok_len < 2 || tok[tok_len-1] != tok[0]) { m_err="attribute value missing trailing quote"; break;  }

        char *value = (char *)malloc(tok_len-2+1);
        if (!value) { m_err="malloc fail"; break; }

        memcpy(value,tok+1,tok_len-2);
        value[tok_len-2]=0;

        if (m_sort_attributes)
          elem->attributes.Insert(attr_name,value);
        else
          elem->attributes.AddUnsorted(attr_name,value);

        attr_name = NULL;
      }
      free(attr_name);
      return NULL;
    }

    const char *parse_element_body(wdl_xml_element *elem) // return NULL on success, error message on failure
    {
      int cnt=0;
      for (;;)
      {
        if (elem)
        {
          bool want_add = elem->value.GetLength() > 0;
          while (m_lastchar != '<' && m_lastchar > 0)
          { 
            if (!want_add && char_type(m_lastchar)>=0) want_add=true;

            bool adv=true;
            if (m_lastchar == '&')
            {
              m_last_line=m_line; m_last_col=m_col;
              char buf[8];
              if (!decode_entity(buf)) return "unknown entity in element body";
              elem->value.Append(buf);
              adv=false;
            }
            else if (want_add)
            {
              unsigned char c = (unsigned char)m_lastchar;
              elem->value.Append((const char *)&c,1);
            }

            if (adv) m_lastchar = nextchar();
          }
        }

        const char *tok = get_tok(elem != NULL);
        const int start_line = m_last_line, start_col = m_last_col;
        if (!tok)
        {
          if (m_err && *m_err == 0 && !elem) m_err = NULL; // clear m_error if EOF and top level
          return elem ? "unterminated block" : NULL;
        }
        if (*tok != '<') return "expected < tag";
    
        tok = get_tok(true);
        if (!tok) return "expected token after <";

        if (tok[0] == '!')
        {
          tok = get_tok(true);
          if (!tok) return "expected token following <!";

          if (*tok == '-')
          {
            tok = get_tok(true);
            if (!tok) return "expected token following <!-";
            if (*tok != '-') return "unknown token following <!-";
            if (!skip_until("--"))
            {
              m_last_line=start_line;
              m_last_col=start_col;
              return "unterminated comment";
            }
            tok = get_tok(true);
            if (!tok || tok[0] != '>') return "-- not allowed in comment";
          }
          else if (*tok == '[')
          {
            if (!elem) return "<![ not allowed at document level";
            tok = get_tok(true);
            if (!tok || strcmp(tok,"CDATA")) return "unknown token beginning <![";
            tok=get_tok(true);
            if (!tok || tok[0] != '[') return "unknown token beginning <![CDATA but without trailing [";

            // add content literally until ]]>
            int lc=m_lastchar, last1=0,last2=0;
            for (;;)
            {
              if (lc == '>' && last1 == ']' && last2 == ']') break;

              unsigned char c = (unsigned char)lc;
              elem->value.Append((const char *)&c,1);
              last2 = last1;
              last1 = lc;

              lc = nextchar(); 
              if (lc <= 0)
              {
                m_lastchar = -1;
                m_last_line=start_line;
                m_last_col=start_col;
                return "unterminated <![CDATA[";
              }
            }
            elem->value.SetLen(elem->value.GetLength()-2); // remove ]]
            m_lastchar = nextchar(); 

          }
          else if (!strcmp(tok,"DOCTYPE"))
          {
            if (elem) return "<!DOCTYPE must be at top level";
            if (element_doctype_tokens.GetSize()) return "<!DOCTYPE already specified";

            tok = get_tok();
            if (!tok || char_type(*tok)) return "expected document type token following <!DOCTYPE";
            do
            {
              element_doctype_tokens.Add(strdup(tok));
              tok = get_tok();
              if (!tok) 
              {
                m_last_line=start_line;
                m_last_col=start_col;
                return "unterminated <!DOCTYPE";
              }
            } while (tok[0] != '>');
          }
          else return "unknown token following <!";
        }
        else if (tok[0] == '?')
        {
          tok = get_tok(true);
          if (!tok) return "expected token following <?";

          if (!strcmp(tok,"xml"))
          {
            if (elem || cnt || element_xml || element_root_meta.GetSize()) return "<?xml must begin document";

            element_xml = new wdl_xml_element("xml",start_line,start_col,m_sort_attributes);
            tok = parse_element_attributes(element_xml);
            if (!tok || tok[0] != '?' || !(tok=get_tok(true)) || tok[0] != '>')
              return "<?xml not terminated";
          }
          else
          {
            if (!elem)
            {
              wdl_xml_element *ne = new wdl_xml_element(tok,start_line,start_col,m_sort_attributes);
              tok = parse_element_attributes(ne);
              if (!tok || tok[0] != '?' || !(tok=get_tok(true)) || tok[0] != '>')
              {
                delete ne;
                return "<? element not terminated";
              }
              element_root_meta.Add(ne);
            }
            else if (!skip_until("?>")) // ignore <? inside elements
            {
              m_last_line=start_line;
              m_last_col=start_col;
              return "unterminated <? block";
            }
          }
        }
        else if (tok[0] == '/')
        {
          if (!elem) return "unexpected </ at root level";

          tok = get_tok(true);
          if (strcmp(tok,elem->name)) 
          {
            return "mismatched </ tag name";
          }

          tok = get_tok();
          if (!tok || tok[0] != '>') return "expected > following </tag";
          // done!
          elem->m_has_discrete_close = true;
          return NULL;
        }
        else
        {
          if (!elem && element_root)
            return "multiple top level elements";

          if (*tok == '-' || *tok == '.' || (*tok >= '0' && *tok <= '9'))
            return "element name must not begin with .- or number";

          wdl_xml_element *sub = new wdl_xml_element(tok,start_line,start_col,m_sort_attributes);
          if (elem) elem->elements.Add(sub);
          else element_root = sub;

          tok = parse_element_attributes(sub);
          if (!tok) return "unterminated element";

          if (*tok == '/') 
          {
            tok = get_tok(true);
            if (!tok || *tok != '>') return "expected > following / to end element";
          }
          else if (*tok == '>')
          {
            const char *ret = parse_element_body(sub);
            if (ret) return ret;
          }
          else 
          {
            return "unknown token in element"; 
          }
        }
        cnt++;
      }
    }
};

class wdl_xml_fileread : public wdl_xml_parser {
  FILE *m_fp;
  char m_buf[1024];
  int m_charset;

  virtual int moredata(const char **dataptr) 
  { 
    *dataptr = m_buf;
    const int cs = m_charset;
    if (m_fp) switch (cs)
    {
      case 0:
        return (int) fread(m_buf,1,sizeof(m_buf),m_fp);
      case 1:
      case 2:
        {
          unsigned char tmp[128];
          const int l = (int) fread(tmp,1,sizeof(tmp),m_fp);
          int rd=0, wpos=0;
          while (rd+1 < l)
          {
            const int amt=wdl_utf8_makechar(cs==1 ? ((tmp[rd]<<8)|tmp[rd+1]) : (tmp[rd]|(tmp[rd+1]<<8)), 
                m_buf+wpos,
                (int)sizeof(m_buf)-wpos);
            if (amt>0) wpos += amt;

            rd+=2;
          }
          return wpos;
        }
    }
    return 0;
  }
public:
  wdl_xml_fileread(FILE *fp) : wdl_xml_parser(NULL,0) 
  { 
    m_fp=fp; 
    m_charset=0; // default to utf-8
    if (fp)
    {
      unsigned char bom[2];
      if (fread(bom,1,2,fp)==2)
      {
        if (bom[0] == 0xEF && bom[1] == 0xBB && fgetc(fp) == 0xBF) m_charset=0;
        else if (bom[0] == 0xFE && bom[1] == 0xFF) m_charset=1; // utf-16 BE
        else if (bom[0] == 0xFF && bom[1] == 0xFE) m_charset=2; // utf-16 LE
        else fseek(fp,0,SEEK_SET); // rewind
      }
    }
  }
  virtual ~wdl_xml_fileread() { if (m_fp) fclose(m_fp); }
};

#endif