summaryrefslogtreecommitdiffstats
path: root/collectors/log2journal/log2journal-json.c
blob: 2ca294e4db32808d944e70c0fa0beb3d81087066 (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
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
630
// SPDX-License-Identifier: GPL-3.0-or-later

#include "log2journal.h"

#define JSON_ERROR_LINE_MAX 1024
#define JSON_KEY_MAX 1024
#define JSON_DEPTH_MAX 100

struct log_json_state {
    LOG_JOB *jb;

    const char *line;
    uint32_t pos;
    uint32_t depth;
    char *stack[JSON_DEPTH_MAX];

    char key[JSON_KEY_MAX];
    char msg[JSON_ERROR_LINE_MAX];
};

static inline bool json_parse_object(LOG_JSON_STATE *js);
static inline bool json_parse_array(LOG_JSON_STATE *js);

#define json_current_pos(js) &(js)->line[(js)->pos]
#define json_consume_char(js) ++(js)->pos

static inline void json_process_key_value(LOG_JSON_STATE *js, const char *value, size_t len) {
    log_job_send_extracted_key_value(js->jb, js->key, value, len);
}

static inline void json_skip_spaces(LOG_JSON_STATE *js) {
    const char *s = json_current_pos(js);
    const char *start = s;

    while(isspace(*s)) s++;

    js->pos += s - start;
}

static inline bool json_expect_char_after_white_space(LOG_JSON_STATE *js, const char *expected) {
    json_skip_spaces(js);

    const char *s = json_current_pos(js);
    for(const char *e = expected; *e ;e++) {
        if (*s == *e)
            return true;
    }

    snprintf(js->msg, sizeof(js->msg),
             "JSON PARSER: character '%c' is not one of the expected characters (%s), at pos %zu",
             *s ? *s : '?', expected, js->pos);

    return false;
}

static inline bool json_parse_null(LOG_JSON_STATE *js) {
    const char *s = json_current_pos(js);
    if (strncmp(s, "null", 4) == 0) {
        json_process_key_value(js, "null", 4);
        js->pos += 4;
        return true;
    }
    else {
        snprintf(js->msg, sizeof(js->msg),
                 "JSON PARSER: expected 'null', found '%.4s' at position %zu", s, js->pos);
        return false;
    }
}

static inline bool json_parse_true(LOG_JSON_STATE *js) {
    const char *s = json_current_pos(js);
    if (strncmp(s, "true", 4) == 0) {
        json_process_key_value(js, "true", 4);
        js->pos += 4;
        return true;
    }
    else {
        snprintf(js->msg, sizeof(js->msg),
                 "JSON PARSER: expected 'true', found '%.4s' at position %zu", s, js->pos);
        return false;
    }
}

static inline bool json_parse_false(LOG_JSON_STATE *js) {
    const char *s = json_current_pos(js);
    if (strncmp(s, "false", 5) == 0) {
        json_process_key_value(js, "false", 5);
        js->pos += 5;
        return true;
    }
    else {
        snprintf(js->msg, sizeof(js->msg),
                 "JSON PARSER: expected 'false', found '%.4s' at position %zu", s, js->pos);
        return false;
    }
}

static inline bool json_parse_number(LOG_JSON_STATE *js) {
    static __thread char value[8192];

    value[0] = '\0';
    char *d = value;
    const char *s = json_current_pos(js);
    size_t remaining = sizeof(value) - 1; // Reserve space for null terminator

    // Optional minus sign
    if (*s == '-') {
        *d++ = *s++;
        remaining--;
    }

    // Digits before decimal point
    while (*s >= '0' && *s <= '9') {
        if (remaining < 2) {
            snprintf(js->msg, sizeof(js->msg), "JSON PARSER: truncated number value at pos %zu", js->pos);
            return false;
        }
        *d++ = *s++;
        remaining--;
    }

    // Decimal point and fractional part
    if (*s == '.') {
        *d++ = *s++;
        remaining--;

        while (*s >= '0' && *s <= '9') {
            if (remaining < 2) {
                snprintf(js->msg, sizeof(js->msg), "JSON PARSER: truncated fractional part at pos %zu", js->pos);
                return false;
            }
            *d++ = *s++;
            remaining--;
        }
    }

    // Exponent part
    if (*s == 'e' || *s == 'E') {
        *d++ = *s++;
        remaining--;

        // Optional sign in exponent
        if (*s == '+' || *s == '-') {
            *d++ = *s++;
            remaining--;
        }

        while (*s >= '0' && *s <= '9') {
            if (remaining < 2) {
                snprintf(js->msg, sizeof(js->msg), "JSON PARSER: truncated exponent at pos %zu", js->pos);
                return false;
            }
            *d++ = *s++;
            remaining--;
        }
    }

    *d = '\0';
    js->pos += d - value;

    if (d > value) {
        json_process_key_value(js, value, d - value);
        return true;
    } else {
        snprintf(js->msg, sizeof(js->msg), "JSON PARSER: invalid number format at pos %zu", js->pos);
        return false;
    }
}

static inline bool encode_utf8(unsigned codepoint, char **d, size_t *remaining) {
    if (codepoint <= 0x7F) {
        // 1-byte sequence
        if (*remaining < 2) return false; // +1 for the null
        *(*d)++ = (char)codepoint;
        (*remaining)--;
    }
    else if (codepoint <= 0x7FF) {
        // 2-byte sequence
        if (*remaining < 3) return false; // +1 for the null
        *(*d)++ = (char)(0xC0 | ((codepoint >> 6) & 0x1F));
        *(*d)++ = (char)(0x80 | (codepoint & 0x3F));
        (*remaining) -= 2;
    }
    else if (codepoint <= 0xFFFF) {
        // 3-byte sequence
        if (*remaining < 4) return false; // +1 for the null
        *(*d)++ = (char)(0xE0 | ((codepoint >> 12) & 0x0F));
        *(*d)++ = (char)(0x80 | ((codepoint >> 6) & 0x3F));
        *(*d)++ = (char)(0x80 | (codepoint & 0x3F));
        (*remaining) -= 3;
    }
    else if (codepoint <= 0x10FFFF) {
        // 4-byte sequence
        if (*remaining < 5) return false; // +1 for the null
        *(*d)++ = (char)(0xF0 | ((codepoint >> 18) & 0x07));
        *(*d)++ = (char)(0x80 | ((codepoint >> 12) & 0x3F));
        *(*d)++ = (char)(0x80 | ((codepoint >> 6) & 0x3F));
        *(*d)++ = (char)(0x80 | (codepoint & 0x3F));
        (*remaining) -= 4;
    }
    else
        // Invalid code point
        return false;

    return true;
}

size_t parse_surrogate(const char *s, char *d, size_t *remaining) {
    if (s[0] != '\\' || (s[1] != 'u' && s[1] != 'U')) {
        return 0; // Not a valid Unicode escape sequence
    }

    char hex[9] = {0}; // Buffer for the hexadecimal value
    unsigned codepoint;

    if (s[1] == 'u') {
        // Handle \uXXXX
        if (!isxdigit(s[2]) || !isxdigit(s[3]) || !isxdigit(s[4]) || !isxdigit(s[5])) {
            return 0; // Not a valid \uXXXX sequence
        }

        hex[0] = s[2];
        hex[1] = s[3];
        hex[2] = s[4];
        hex[3] = s[5];
        codepoint = (unsigned)strtoul(hex, NULL, 16);

        if (codepoint >= 0xD800 && codepoint <= 0xDBFF) {
            // Possible start of surrogate pair
            if (s[6] == '\\' && s[7] == 'u' && isxdigit(s[8]) && isxdigit(s[9]) &&
                isxdigit(s[10]) && isxdigit(s[11])) {
                // Valid low surrogate
                unsigned low_surrogate = strtoul(&s[8], NULL, 16);
                if (low_surrogate < 0xDC00 || low_surrogate > 0xDFFF) {
                    return 0; // Invalid low surrogate
                }
                codepoint = 0x10000 + ((codepoint - 0xD800) << 10) + (low_surrogate - 0xDC00);
                return encode_utf8(codepoint, &d, remaining) ? 12 : 0; // \uXXXX\uXXXX
            }
        }

        // Single \uXXXX
        return encode_utf8(codepoint, &d, remaining) ? 6 : 0;
    }
    else {
        // Handle \UXXXXXXXX
        for (int i = 2; i < 10; i++) {
            if (!isxdigit(s[i])) {
                return 0; // Not a valid \UXXXXXXXX sequence
            }
            hex[i - 2] = s[i];
        }
        codepoint = (unsigned)strtoul(hex, NULL, 16);
        return encode_utf8(codepoint, &d, remaining) ? 10 : 0; // \UXXXXXXXX
    }
}

static inline void copy_newline(LOG_JSON_STATE *js __maybe_unused, char **d, size_t *remaining) {
    if(*remaining > 3) {
        *(*d)++ = '\\';
        *(*d)++ = 'n';
        (*remaining) -= 2;
    }
}

static inline void copy_tab(LOG_JSON_STATE *js __maybe_unused, char **d, size_t *remaining) {
    if(*remaining > 3) {
        *(*d)++ = '\\';
        *(*d)++ = 't';
        (*remaining) -= 2;
    }
}

static inline bool json_parse_string(LOG_JSON_STATE *js) {
    static __thread char value[JOURNAL_MAX_VALUE_LEN];

    if(!json_expect_char_after_white_space(js, "\""))
        return false;

    json_consume_char(js);

    value[0] = '\0';
    char *d = value;
    const char *s = json_current_pos(js);
    size_t remaining = sizeof(value);

    while (*s && *s != '"') {
        char c;

        if (*s == '\\') {
            s++;

            switch (*s) {
                case 'n':
                    copy_newline(js, &d, &remaining);
                    s++;
                    continue;

                case 't':
                    copy_tab(js, &d, &remaining);
                    s++;
                    continue;

                case 'f':
                case 'b':
                case 'r':
                    c = ' ';
                    s++;
                    break;

                case 'u': {
                        size_t old_remaining = remaining;
                        size_t consumed = parse_surrogate(s - 1, d, &remaining);
                        if (consumed > 0) {
                            s += consumed - 1; // -1 because we already incremented s after '\\'
                            d += old_remaining - remaining;
                            continue;
                        }
                        else {
                            *d++ = '\\';
                            remaining--;
                            c = *s++;
                        }
                    }
                    break;

                default:
                    c = *s++;
                    break;
            }
        }
        else
            c = *s++;

        if(remaining < 2) {
            snprintf(js->msg, sizeof(js->msg),
                     "JSON PARSER: truncated string value at pos %zu", js->pos);
            return false;
        }
        else {
            *d++ = c;
            remaining--;
        }
    }
    *d = '\0';
    js->pos += s - json_current_pos(js);

    if(!json_expect_char_after_white_space(js, "\""))
        return false;

    json_consume_char(js);

    if(d > value)
        json_process_key_value(js, value, d - value);

    return true;
}

static inline bool json_parse_key_and_push(LOG_JSON_STATE *js) {
    if (!json_expect_char_after_white_space(js, "\""))
        return false;

    if(js->depth >= JSON_DEPTH_MAX - 1) {
        snprintf(js->msg, sizeof(js->msg),
                 "JSON PARSER: object too deep, at pos %zu", js->pos);
        return false;
    }

    json_consume_char(js);

    char *d = js->stack[js->depth];
    if(js->depth)
        *d++ = '_';

    size_t remaining = sizeof(js->key) - (d - js->key);

    const char *s = json_current_pos(js);
    char last_c = '\0';
    while(*s && *s != '\"') {
        char c;

        if (*s == '\\') {
            s++;
            c = (char)((*s == 'u') ? '_' : journal_key_characters_map[(unsigned char)*s]);
            s += (*s == 'u') ? 5 : 1;
        }
        else
            c = journal_key_characters_map[(unsigned char)*s++];

        if(c == '_' && last_c == '_')
            continue;
        else {
            if(remaining < 2) {
                snprintf(js->msg, sizeof(js->msg),
                         "JSON PARSER: key buffer full - keys are too long, at pos %zu", js->pos);
                return false;
            }
            *d++ = c;
            remaining--;
        }

        last_c = c;
    }
    *d = '\0';
    js->pos += s - json_current_pos(js);

    if (!json_expect_char_after_white_space(js, "\""))
        return false;

    json_consume_char(js);

    js->stack[++js->depth] = d;

    return true;
}

static inline bool json_key_pop(LOG_JSON_STATE *js) {
    if(js->depth <= 0) {
        snprintf(js->msg, sizeof(js->msg),
                 "JSON PARSER: cannot pop a key at depth %zu, at pos %zu", js->depth, js->pos);
        return false;
    }

    char *k = js->stack[js->depth--];
    *k = '\0';
    return true;
}

static inline bool json_parse_value(LOG_JSON_STATE *js) {
    if(!json_expect_char_after_white_space(js, "-.0123456789tfn\"{["))
        return false;

    const char *s = json_current_pos(js);
    switch(*s) {
        case '-':
        case '0':
        case '1':
        case '2':
        case '3':
        case '4':
        case '5':
        case '6':
        case '7':
        case '8':
        case '9':
            return json_parse_number(js);

        case 't':
            return json_parse_true(js);

        case 'f':
            return json_parse_false(js);

        case 'n':
            return json_parse_null(js);

        case '"':
            return json_parse_string(js);

        case '{':
            return json_parse_object(js);

        case '[':
            return json_parse_array(js);
    }

    snprintf(js->msg, sizeof(js->msg),
             "JSON PARSER: unexpected character at pos %zu", js->pos);
    return false;
}

static inline bool json_key_index_and_push(LOG_JSON_STATE *js, size_t index) {
    char *d = js->stack[js->depth];
    if(js->depth > 0) {
        *d++ = '_';
    }

    // Convert index to string manually
    char temp[32];
    char *t = temp + sizeof(temp) - 1; // Start at the end of the buffer
    *t = '\0';

    do {
        *--t = (char)((index % 10) + '0');
        index /= 10;
    } while (index > 0);

    size_t remaining = sizeof(js->key) - (d - js->key);

    // Append the index to the key
    while (*t) {
        if(remaining < 2) {
            snprintf(js->msg, sizeof(js->msg),
                     "JSON PARSER: key buffer full - keys are too long, at pos %zu", js->pos);
            return false;
        }

        *d++ = *t++;
        remaining--;
    }

    *d = '\0'; // Null-terminate the key
    js->stack[++js->depth] = d;

    return true;
}

static inline bool json_parse_array(LOG_JSON_STATE *js) {
    if(!json_expect_char_after_white_space(js, "["))
        return false;

    json_consume_char(js);

    size_t index = 0;
    do {
        if(!json_key_index_and_push(js, index))
            return false;

        if(!json_parse_value(js))
            return false;

        json_key_pop(js);

        if(!json_expect_char_after_white_space(js, ",]"))
            return false;

        const char *s = json_current_pos(js);
        json_consume_char(js);
        if(*s == ',') {
            index++;
            continue;
        }
        else // }
            break;

    } while(true);

    return true;
}

static inline bool json_parse_object(LOG_JSON_STATE *js) {
    if(!json_expect_char_after_white_space(js, "{"))
        return false;

    json_consume_char(js);

    do {
        if (!json_expect_char_after_white_space(js, "\""))
            return false;

        if(!json_parse_key_and_push(js))
            return false;

        if(!json_expect_char_after_white_space(js, ":"))
            return false;

        json_consume_char(js);

        if(!json_parse_value(js))
            return false;

        json_key_pop(js);

        if(!json_expect_char_after_white_space(js, ",}"))
            return false;

        const char *s = json_current_pos(js);
        json_consume_char(js);
        if(*s == ',')
            continue;
        else // }
            break;

    } while(true);

    return true;
}

LOG_JSON_STATE *json_parser_create(LOG_JOB *jb) {
    LOG_JSON_STATE *js = mallocz(sizeof(LOG_JSON_STATE));
    memset(js, 0, sizeof(LOG_JSON_STATE));
    js->jb = jb;

    if(jb->prefix)
        copy_to_buffer(js->key, sizeof(js->key), js->jb->prefix, strlen(js->jb->prefix));

    js->stack[0] = &js->key[strlen(js->key)];

    return js;
}

void json_parser_destroy(LOG_JSON_STATE *js) {
    if(js)
        freez(js);
}

const char *json_parser_error(LOG_JSON_STATE *js) {
    return js->msg;
}

bool json_parse_document(LOG_JSON_STATE *js, const char *txt) {
    js->line = txt;
    js->pos = 0;
    js->msg[0] = '\0';
    js->stack[0][0] = '\0';
    js->depth = 0;

    if(!json_parse_object(js))
        return false;

    json_skip_spaces(js);
    const char *s = json_current_pos(js);

    if(*s) {
        snprintf(js->msg, sizeof(js->msg),
                 "JSON PARSER: excess characters found after document is finished, at pos %zu", js->pos);
        return false;
    }

    return true;
}

void json_test(void) {
    LOG_JOB jb = { .prefix = "NIGNX_" };
    LOG_JSON_STATE *json = json_parser_create(&jb);

    json_parse_document(json, "{\"value\":\"\\u\\u039A\\u03B1\\u03BB\\u03B7\\u03BC\\u03AD\\u03C1\\u03B1\"}");

    json_parser_destroy(json);
}