summaryrefslogtreecommitdiff
blob: 87ba5d0c2e269df508164daf391ecafd04897717 (plain)
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
#include "../include/extract_alloc.h"

#include "mem.h"
#include "memento.h"
#include "outf.h"
#include "xml.h"

#include <assert.h>
#include <errno.h>
#include <float.h>
#include <limits.h>

#ifdef _MSC_VER
    #include "compat_stdint.h"
    #include "compat_strtoll.h"
#else
    #include <stdint.h>
#endif

#include <stdlib.h>
#include <string.h>


/* These str_*() functions realloc buffer as required. All return 0 or -1 with
errno set. */

/* Appends first <s_len> chars of string <s> to *p. */
static int str_catl(extract_alloc_t* alloc, char** p, const char* s, int s_len)
{
    size_t p_len = (*p) ? strlen(*p) : 0;
    if (extract_realloc2(
            alloc,
            p,
            p_len + 1,
            p_len + s_len + 1
            )) return -1;
    memcpy(*p + p_len, s, s_len);
    (*p)[p_len + s_len] = 0;
    return 0;
}

/* Appends a char.  */
static int str_catc(extract_alloc_t* alloc, char** p, char c)
{
    return str_catl(alloc, p, &c, 1);
}

/* Unused but usefult o keep code here. */
#if 0
/* Appends a string. */
static int str_cat(extract_alloc_t* alloc, char** p, const char* s)
{
    return str_catl(alloc, p, s, strlen(s));
}
#endif

char* extract_xml_tag_attributes_find(extract_xml_tag_t* tag, const char* name)
{
    int i;
    for (i=0; i<tag->attributes_num; ++i) {
        if (!strcmp(tag->attributes[i].name, name)) {
            char* ret = tag->attributes[i].value;
            return ret;
        }
    }
    outf("Failed to find attribute '%s'",name);
    return NULL;
}

int extract_xml_tag_attributes_find_float(
        extract_xml_tag_t*  tag,
        const char*         name,
        float*              o_out
        )
{
    const char* value = extract_xml_tag_attributes_find(tag, name);
    if (!value) {
        errno = ESRCH;
        return -1;
    }
    if (extract_xml_str_to_float(value, o_out)) return -1;
    return 0;
}

int extract_xml_tag_attributes_find_double(
        extract_xml_tag_t*  tag,
        const char*         name,
        double*             o_out
        )
{
    const char* value = extract_xml_tag_attributes_find(tag, name);
    if (!value) {
        errno = ESRCH;
        return -1;
    }
    if (extract_xml_str_to_double(value, o_out)) return -1;
    return 0;
}

int extract_xml_tag_attributes_find_int(
        extract_xml_tag_t*  tag,
        const char*         name,
        int*                o_out
        )
{
    const char* text = extract_xml_tag_attributes_find(tag, name);
    return extract_xml_str_to_int(text, o_out);
}

int extract_xml_tag_attributes_find_uint(
        extract_xml_tag_t*  tag,
        const char*         name,
        unsigned*           o_out
        )
{
    const char* text = extract_xml_tag_attributes_find(tag, name);
    return extract_xml_str_to_uint(text, o_out);
}

int extract_xml_tag_attributes_find_size(
        extract_xml_tag_t*  tag,
        const char*         name,
        size_t*             o_out
        )
{
    const char* text = extract_xml_tag_attributes_find(tag, name);
    return extract_xml_str_to_size(text, o_out);
}

int extract_xml_str_to_llint(const char* text, long long* o_out)
{
    char* endptr;
    long long x;
    if (!text) {
        errno = ESRCH;
        return -1;
    }
    if (text[0] == 0) {
        errno = EINVAL;
        return -1;
    }
    errno = 0;
    x = strtoll(text, &endptr, 10 /*base*/);
    if (errno) {
        return -1;
    }
    if (*endptr) {
        errno = EINVAL;
        return -1;
    }
    *o_out = x;
    return 0;
}

int extract_xml_str_to_ullint(const char* text, unsigned long long* o_out)
{
    char* endptr;
    unsigned long long x;
    if (!text) {
        errno = ESRCH;
        return -1;
    }
    if (text[0] == 0) {
        errno = EINVAL;
        return -1;
    }
    errno = 0;
    x = strtoull(text, &endptr, 10 /*base*/);
    if (errno) {
        return -1;
    }
    if (*endptr) {
        errno = EINVAL;
        return -1;
    }
    *o_out = x;
    return 0;
}

int extract_xml_str_to_int(const char* text, int* o_out)
{
    long long x;
    if (extract_xml_str_to_llint(text, &x)) return -1;
    if (x > INT_MAX || x < INT_MIN) {
        errno = ERANGE;
        return -1;
    }
    *o_out = (int) x;
    return 0;
}

int extract_xml_str_to_uint(const char* text, unsigned* o_out)
{
    unsigned long long x;
    if (extract_xml_str_to_ullint(text, &x)) return -1;
    if (x > UINT_MAX) {
        errno = ERANGE;
        return -1;
    }
    *o_out = (unsigned) x;
    return 0;
}

int extract_xml_str_to_size(const char* text, size_t* o_out)
{
    unsigned long long x;
    if (extract_xml_str_to_ullint(text, &x)) return -1;
    if (x > SIZE_MAX) {
        errno = ERANGE;
        return -1;
    }
    *o_out = (size_t) x;
    return 0;
}

int extract_xml_str_to_double(const char* text, double* o_out)
{
    char* endptr;
    double x;
    if (!text) {
        errno = ESRCH;
        return -1;
    }
    if (text[0] == 0) {
        errno = EINVAL;
        return -1;
    }
    errno = 0;
    x = strtod(text, &endptr);
    if (errno) {
        return -1;
    }
    if (*endptr) {
        errno = EINVAL;
        return -1;
    }
    *o_out = x;
    return 0;
}

int extract_xml_str_to_float(const char* text, float* o_out)
{
    double x;
    if (extract_xml_str_to_double(text, &x)) {
        return -1;
    }
    if (x > FLT_MAX || x < -FLT_MAX) {
        errno = ERANGE;
        return -1;
    }
    *o_out = (float) x;
    return 0;
}

static int extract_xml_tag_attributes_append(
        extract_alloc_t* alloc,
        extract_xml_tag_t*  tag,
        char*               name,
        char*               value
        )
{
    if (extract_realloc2(
            alloc,
            &tag->attributes,
            sizeof(extract_xml_attribute_t) * tag->attributes_num,
            sizeof(extract_xml_attribute_t) * (tag->attributes_num+1)
            )) return -1;
    tag->attributes[tag->attributes_num].name = name;
    tag->attributes[tag->attributes_num].value = value;
    tag->attributes_num += 1;
    return 0;
}

void extract_xml_tag_init(extract_xml_tag_t* tag)
{
    tag->name = NULL;
    tag->attributes = NULL;
    tag->attributes_num = 0;
    extract_astring_init(&tag->text);
}

void extract_xml_tag_free(extract_alloc_t* alloc, extract_xml_tag_t* tag)
{
    int i;
    extract_free(alloc, &tag->name);
    for (i=0; i<tag->attributes_num; ++i) {
        extract_xml_attribute_t* attribute = &tag->attributes[i];
        extract_free(alloc, &attribute->name);
        extract_free(alloc, &attribute->value);
    }
    extract_free(alloc, &tag->attributes);
    extract_astring_free(alloc, &tag->text);
    extract_xml_tag_init(tag);
}

/* Unused but useful to keep code here. */
#if 0
/* Like strcmp() but also handles NULL. */
static int extract_xml_strcmp_null(const char* a, const char* b)
{
    if (!a && !b) return 0;
    if (!a) return -1;
    if (!b) return 1;
    return strcmp(a, b);
}
#endif

/* Unused but usefult o keep code here. */
#if 0
/* Compares tag name, then attributes; returns -1, 0 or +1. Does not compare
extract_xml_tag_t::text members. */
int extract_xml_compare_tags(const extract_xml_tag_t* lhs, const extract_xml_tag_t* rhs)
{
    int d;
    int i;
    d = extract_xml_strcmp_null(lhs->name, rhs->name);
    if (d)  return d;
    for(i=0;; ++i) {
        if (i >= lhs->attributes_num || i >= rhs->attributes_num) {
            break;
        }
        const extract_xml_attribute_t* lhs_attribute = &lhs->attributes[i];
        const extract_xml_attribute_t* rhs_attribute = &rhs->attributes[i];
        d = extract_xml_strcmp_null(lhs_attribute->name, rhs_attribute->name);
        if (d)  return d;
        d = extract_xml_strcmp_null(lhs_attribute->value, rhs_attribute->value);
        if (d)  return d;
    }
    if (lhs->attributes_num > rhs->attributes_num) return +1;
    if (lhs->attributes_num < rhs->attributes_num) return -1;
    return 0;
}
#endif


int extract_xml_pparse_init(extract_alloc_t* alloc, extract_buffer_t* buffer, const char* first_line)
{
    char* first_line_buffer = NULL;
    int e = -1;

    if (first_line) {
        size_t first_line_len = strlen(first_line);
        size_t actual;
        if (extract_malloc(alloc, &first_line_buffer, first_line_len + 1)) goto end;

        if (extract_buffer_read(buffer, first_line_buffer, first_line_len, &actual)) {
            outf("error: failed to read first line.");
            goto end;
        }
        first_line_buffer[actual] = 0;
        if (strcmp(first_line, first_line_buffer)) {
            outf("Unrecognised prefix: %s", first_line_buffer);
            errno = ESRCH;
            goto end;
        }
    }

    for(;;) {
        char c;
        int ee = extract_buffer_read(buffer, &c, 1, NULL);
        if (ee) {
            if (ee==1) errno = ESRCH;   /* EOF. */
            goto end;
        }
        if (c == '<') {
            break;
        }
        else if (c == ' ' || c == '\n') {}
        else {
            outf("Expected '<' but found c=%i", c);
            goto end;
        }
    }
    e = 0;

    end:
    extract_free(alloc, &first_line_buffer);
    return e;
}

static int s_next(extract_buffer_t* buffer, int* ret, char* o_c)
/* Reads next char, but if EOF sets *ret=+1, errno=ESRCH and returns +1. */
{
    int e = extract_buffer_read(buffer, o_c, 1, NULL);
    if (e == +1) {
        *ret = +1;
        errno = ESRCH;
    }
    return e;
}

static const char* extract_xml_tag_string(extract_alloc_t* alloc, extract_xml_tag_t* tag)
{
    static char* buffer = NULL;
    extract_free(alloc, &buffer);
    if (extract_asprintf(alloc, &buffer, "<name=%s>", tag->name ? tag->name : ""))
    {
        return "";
    }
    return buffer;
}

int extract_xml_pparse_next(extract_buffer_t* buffer, extract_xml_tag_t* out)
{
    int     ret = -1;
    char*   attribute_name = NULL;
    char*   attribute_value = NULL;
    char    c;
    int     i;
    extract_alloc_t* alloc = extract_buffer_alloc(buffer);

    if (0) outf("out is: %s", extract_xml_tag_string(extract_buffer_alloc(buffer), out));
    assert(buffer);
    extract_xml_tag_free(alloc, out);

    /* Read tag name. Initialise it to empty string so we never return
    out->name==null on success. */
    if (str_catl( alloc, &out->name, NULL, 0)) goto end;
    for( i=0;; ++i) {
        int e = extract_buffer_read(buffer, &c, 1, NULL);
        if (e) {
            if (e == +1) ret = 1;   /* EOF is not an error here. */
            goto end;
        }
        if (c == '>' || c == ' ')  break;
        if (str_catc(alloc, &out->name, c)) goto end;
    }
    if (c == ' ') {

        /* Read attributes. */
        for(;;) {

            /* Read attribute name. */
            for(;;) {
                if (s_next(buffer, &ret, &c)) goto end;
                if (c == '=' || c == '>' || c == ' ') break;
                if (str_catc(alloc, &attribute_name, c)) goto end;
            }
            if (c == '>') break;

            if (c == '=') {
                /* Read attribute value. */
                int quote_single = 0;
                int quote_double = 0;
                size_t l;
                if (str_catl( alloc, &attribute_value, NULL, 0)) goto end;
                for(;;) {
                    if (s_next(buffer, &ret, &c)) goto end;
                    if (c == '\'')      quote_single = !quote_single;
                    else if (c == '"')  quote_double = !quote_double;
                    else if (!quote_single && !quote_double
                            && (c == ' ' || c == '/' || c == '>')
                            ) {
                        /* We are at end of attribute value. */
                        break;
                    }
                    else if (c == '\\') {
                        // Escape next character.
                        if (s_next(buffer, &ret, &c)) goto end;
                    }
                    if (str_catc(alloc, &attribute_value, c)) goto end;
                }

                /* Remove any enclosing quotes. */
                l = strlen(attribute_value);
                if (l >= 2) {
                    if (
                            (attribute_value[0] == '"' && attribute_value[l-1] == '"')
                            ||
                            (attribute_value[0] == '\'' && attribute_value[l-1] == '\'')
                            ) {
                        memmove(attribute_value, attribute_value+1, l-2);
                        attribute_value[l-2] = 0;
                    }
                }
            }

            /* Ensure name and value are not NULL. */
            if (str_catl( alloc, &attribute_name, NULL, 0)) goto end;
            if (str_catl( alloc, &attribute_value, NULL, 0)) goto end;

            if (extract_xml_tag_attributes_append(alloc, out, attribute_name, attribute_value)) goto end;
            attribute_name = NULL;
            attribute_value = NULL;
            if (c == '/') {
                if (s_next(buffer, &ret, &c)) goto end;
            }
            if (c == '>') break;
        }
    }

    /* Read plain text until next '<'. */
    for(;;) {
        /* We don't use s_next() here because EOF is not an error. */
        int e = extract_buffer_read(buffer, &c, 1, NULL);
        if (e == +1) {
            break;   /* EOF is not an error here. */
        }
        if (e) goto end;
        if (c == '<') break;
        if (extract_astring_catc(alloc, &out->text, c)) goto end;
    }

    ret = 0;

    end:

    extract_free(alloc, &attribute_name);
    extract_free(alloc, &attribute_value);
    if (ret) {
        extract_xml_tag_free(alloc, out);
    }
    return ret;
}