-
Notifications
You must be signed in to change notification settings - Fork 0
/
format.c
746 lines (568 loc) · 15.5 KB
/
format.c
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
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
/*
* Copyright (c) 2014 Wikimedia Foundation
* Copyright (c) 2014 Magnus Edenhill <[email protected]>
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#define _GNU_SOURCE /* for strndupa() */
#include <string.h>
#include <yajl/yajl_common.h>
#include <yajl/yajl_parse.h>
#include <yajl/yajl_version.h>
#if YAJL_MAJOR >= 2
#define YAJL_SIZE_TYPE size_t
#else
#define YAJL_SIZE_TYPE unsigned int
#endif
#include "kafkatee.h"
/**
* Temporary scratch buffer
*/
struct tmpbuf {
struct tmpbuf *next;
size_t size;
int of;
char buf[0]; /* Must be at end of struct: allocated to '.size' */
};
/**
* Resize the scratch buffer.
* This is a destructive operation, the original data will be lost.
*/
static void render_scratch_resize (struct render *render, int size) {
assert( size >= 0 );
if (render->scratch_size < size) {
if (render->scratch)
free(render->scratch);
render->scratch_size = size;
render->scratch = malloc(size);
}
render->scratch_of = 0;
}
/**
* Allocate space 'len' in scratch buffer.
* The scratch buffer size is bound by its initial render_scratch_resize().
*/
static char *render_scratch_alloc (struct render *render, int len) {
char *p;
assert(render->scratch_of + len <= render->scratch_size);
p = render->scratch + render->scratch_of;
render->scratch_of += len;
return p;
}
/**
* Find a %{field} formatter variable reference.
*/
static struct fmtvar *fmtvar_find (const struct fmt_conf *fconf,
const char *var, ssize_t varlen) {
int i;
for (i = 0 ; i < fconf->fmtvar_cnt ; i++) {
struct fmtvar *fmtvar = &fconf->fmtvar[i];
if (fmtvar->varlen == varlen &&
!strncmp(fmtvar->var, var, varlen))
return fmtvar;
}
return NULL;
}
/**
* Write value for %{field} formatter
*/
static void render_fmtval_write (struct render *render,
struct fmtval *fmtval,
const char *val, int vallen) {
if (unlikely(fmtval->val != NULL))
return;
if (unlikely(!(fmtval->val = render_scratch_alloc(render, vallen))))
return;
memcpy(fmtval->val, val, vallen);
fmtval->vallen = vallen;
}
/**
* One renderer per input thread.
*/
static __thread struct render *thread_render = NULL;
/**
* Destroys the thread-specific renderer.
*/
void render_destroy (void) {
struct render *render = thread_render;
if (!render)
return;
thread_render = NULL;
if (render->scratch)
free(render->scratch);
free(render->fmtval);
free(render);
}
/**
* Create new thread-specific renderer.
*/
static struct render *render_new (const struct fmt_conf *fconf) {
struct render *render;
render = calloc(1, sizeof(*render));
render->fmtval = calloc(sizeof(*render->fmtval), fconf->fmtvar_cnt);
render->fconf = fconf;
assert(!thread_render);
thread_render = render;
return render;
}
static void render_reset (struct render *render) {
memset(render->fmtval, 0,
sizeof(*render->fmtval) * render->fconf->fmtvar_cnt);
}
/**
* All constant strings in the format are placed in 'const_string' which
* hopefully will be small enough to fit a single cache line.
*/
#define CONST_STRING_SIZE 4096
static char const_string[CONST_STRING_SIZE];
static size_t const_string_len = 0;
/**
* Adds a constant string to the constant string area.
* If the string is already found in the area, return it instead.
*/
static char *const_string_add (const char *in, int inlen) {
char *ret;
const char *instr = strndupa(in, inlen);
assert( inlen >= 0 );
if (!(ret = strstr(const_string, instr))) {
assert(const_string_len + inlen < CONST_STRING_SIZE);
/* Append new string */
ret = const_string + const_string_len;
memcpy(ret, in, inlen);
ret[inlen] = '\0';
const_string_len += inlen;
}
return ret;
}
/**
* Render output to '*dstp' according to 'output.format' config.
*/
static int render_out (struct render *render, size_t maxlen,
void **dstp, size_t *dstlenp) {
char *dst;
int of = 0;
int i;
dst = malloc(maxlen);
/* Traverse the formatter list */
for (i = 0 ; i < render->fconf->fmt_cnt ; i++) {
struct fmt *fmt = &render->fconf->fmt[i];
struct fmtval *fmtval;
const char *ptr;
int len;
if (fmt->varidx != -1 &&
(fmtval = &render->fmtval[fmt->varidx]) &&
fmtval->seen) {
ptr = fmtval->val;
len = fmtval->vallen;
} else {
ptr = fmt->def;
len = fmt->deflen;
}
if (len == 0)
continue;
if (unlikely(of + len > maxlen)) {
maxlen = (maxlen + 256) * 2;
dst = realloc(dst, maxlen);
}
memcpy(dst+of, ptr, len);
of += len;
}
*dstp = dst;
*dstlenp = of;
return 0;
}
/**
* JSON parser callbacks (jp_..())
*/
struct json_parse_ctx {
enum {
JP_PARSE, /* Parse next field */
JP_SKIP, /* Skip next value: key didnt match */
} state;
struct fmtval *fmtval; /* write RHS value to this val. */
struct render *render; /* output rendering instance */
};
#define _JP_CHECK() do { \
if (jp->state == JP_SKIP) { \
jp->state = JP_PARSE; \
return 1; \
} \
} while (0)
static int jp_write (struct json_parse_ctx *jp, const char *val, size_t len) {
if (unlikely(!jp->fmtval))
return 1;
render_fmtval_write(jp->render, jp->fmtval, val, len);
jp->fmtval->seen++;
jp->fmtval = NULL;
return 1;
}
static int jp_bool (void *opaque, int val) {
struct json_parse_ctx *jp = opaque;
_JP_CHECK();
if (val)
return jp_write(jp, "true", 4);
else
return jp_write(jp, "false", 5);
}
static int jp_number (void *opaque, const char *numval,
YAJL_SIZE_TYPE numlen) {
struct json_parse_ctx *jp = opaque;
_JP_CHECK();
return jp_write(jp, numval, numlen);
}
static int jp_string (void *opaque, const unsigned char *val,
YAJL_SIZE_TYPE len) {
struct json_parse_ctx *jp = opaque;
_JP_CHECK();
return jp_write(jp, (const char *)val, len);
}
static int jp_start_map (void *opaque) {
/* Maps are flattened for now */
return 1;
}
static int jp_map_key (void *opaque, const unsigned char *key,
YAJL_SIZE_TYPE len) {
struct json_parse_ctx *jp = opaque;
const struct fmtvar *fmtvar;
/* Left hand side: find matching formatter variable */
if (!(fmtvar = fmtvar_find(jp->render->fconf,
(const char *)key, len))) {
/* Formatter key not found: skip right hand side */
jp->state = JP_SKIP;
return 1;
}
jp->fmtval = &jp->render->fmtval[fmtvar->idx];
jp->state = JP_PARSE;
return 1;
}
static int jp_end_map (void *opaque) {
return 1;
}
/**
* Transform payload to JSON.
*/
static int payload_transform_from_json (void **dstp, size_t *dstlenp,
const void *src, size_t srclen) {
static const yajl_callbacks callbacks = {
.yajl_boolean = jp_bool,
.yajl_number = jp_number,
.yajl_string = jp_string,
.yajl_start_map = jp_start_map,
.yajl_map_key = jp_map_key,
.yajl_end_map = jp_end_map,
};
yajl_handle yh;
struct json_parse_ctx jp = {};
yajl_status status;
struct render *render;
if (unlikely(!(render = thread_render)))
render = render_new(&conf.fconf);
else
render_reset(render);
/* JSON does not expand when transformed to string,
* so we can use the JSON size for the scratch buffer. */
render_scratch_resize(render, srclen);
jp.render = render;
/* Set up JSON parser */
yh = yajl_alloc(&callbacks, NULL,
#if YAJL_MAJOR < 2
NULL,
#endif
&jp);
#if YAJL_MAJOR >= 2
yajl_config(yh, yajl_dont_validate_strings, 1);
yajl_config(yh, yajl_allow_trailing_garbage, 1); /* e.g. newlines */
#endif
/* Parse JSON */
status = yajl_parse(yh, src, srclen);
if (likely(status == yajl_status_ok)) {
#if YAJL_MAJOR >= 2
status = yajl_complete_parse(yh);
#else
status = yajl_parse_complete(yh);
#endif
}
/* Handle parsing errors, if any */
if (unlikely(status == yajl_status_error)) {
char *errstr = (char *)yajl_get_error(yh, 0, NULL, 0);
if (errstr[strlen(errstr)-1] == '\n')
errstr[strlen(errstr)-1] = '\0';
kt_log(LOG_ERR, "JSON parse failed: %s: %.*s%s", errstr,
(int)(srclen > 200 ? 200 : srclen), (const char *)src,
srclen > 200 ? "..." : "");
yajl_free_error(yh, (unsigned char *)errstr);
yajl_free(yh);
return -1;
}
yajl_free(yh);
return render_out(render, srclen, dstp, dstlenp);
}
/**
* Transform payload from one encoding to another.
* Returns -1 on error.
*/
int payload_transform (encoding_t in_enc,
void **dstp, size_t *dstlenp,
const void *src, size_t srclen) {
assert(in_enc != conf.fconf.encoding);
if (in_enc == ENC_JSON)
return payload_transform_from_json(dstp, dstlenp, src, srclen);
else
return -1;
}
/**
* Looks for any matching character from 'match' in 's' and returns
* a pointer to the first match, or NULL if none of 'match' matched 's'.
*/
static char *strnchrs (const char *s, int len, const char *match) {
const char *end = s + len;
char map[256] = {};
while (*match)
map[(int)*(match++)] = 1;
while (s < end) {
if (map[(int)*s])
return (char *)s;
s++;
}
return NULL;
}
/**
* Print parsed format string: formatters
*/
static __attribute__((unused)) void fmt_dump (const struct fmt_conf *fconf) {
const struct fmtvar *fmtvar;
int i;
_DBG("%i/%i formats:",
fconf->fmt_cnt, fconf->fmt_size);
for (i = 0 ; i < fconf->fmt_cnt ; i++) {
if (fconf->fmt[i].varidx != -1)
fmtvar = &fconf->fmtvar[fconf->fmt[i].varidx];
else
fmtvar = NULL;
_DBG(" #%-3i \"%.*s\", varidx %i, def (%i)\"%.*s\"%s",
i,
fmtvar ? fmtvar->varlen : 0,
fmtvar ? fmtvar->var : NULL,
fconf->fmt[i].varidx,
fconf->fmt[i].deflen, fconf->fmt[i].deflen,
fconf->fmt[i].def,
fconf->fmt[i].flags & FMT_F_ESCAPE ? ", escape" : "");
}
_DBG("%i/%i fmtvars:", fconf->fmtvar_cnt, fconf->fmtvar_size);
for (i = 0 ; i < fconf->fmtvar_cnt ; i++) {
fmtvar = &fconf->fmtvar[i];
_DBG(" #%i/%i: \"%.*s\"",
fmtvar->idx, i, fmtvar->varlen, fmtvar->var);
}
}
/**
* Adds a parsed formatter to the list of formatters
*/
static struct fmt *format_add (struct fmt_conf *fconf,
const char *var, ssize_t varlen,
const char *def, ssize_t deflen,
int flags,
char *errstr, size_t errstr_size) {
struct fmt *fmt;
if (fconf->fmt_cnt >= fconf->fmt_size) {
fconf->fmt_size = (fconf->fmt_size ? : 32) * 2;
fconf->fmt = realloc(fconf->fmt,
fconf->fmt_size * sizeof(*fconf->fmt));
}
fmt = &fconf->fmt[fconf->fmt_cnt];
memset(fmt, 0, sizeof(*fmt));
fmt->idx = fconf->fmt_cnt;
fmt->flags = flags;
fmt->varidx = -1;
if (!def)
def = "-";
if (deflen == -1)
deflen = strlen(def);
fmt->deflen = deflen;
fmt->def = const_string_add(def, deflen);
fconf->fmt_cnt++;
return fmt;
}
/**
* Adds a %{field} formatter reference.
*/
static struct fmtvar *fmtvar_add (struct fmt_conf *fconf,
const char *var, ssize_t varlen) {
struct fmtvar *fmtvar;
if (fconf->fmtvar_cnt >= fconf->fmtvar_size) {
fconf->fmtvar_size = (fconf->fmtvar_size ? : 32) * 2;
fconf->fmtvar = realloc(fconf->fmtvar,
fconf->fmtvar_size *
sizeof(*fconf->fmtvar));
}
fmtvar = &fconf->fmtvar[fconf->fmtvar_cnt];
fmtvar->var = const_string_add(var, varlen);
fmtvar->varlen = varlen;
fmtvar->idx = fconf->fmtvar_cnt++;
return fmtvar;
}
/**
* Parse the format string and build a parsing array.
*/
int format_parse (struct fmt_conf *fconf, const char *format_orig,
char *errstr, size_t errstr_size) {
const char *s, *t;
const char *format;
int cnt = 0;
/* Perform legacy replacements. */
format = strdupa(format_orig);
/* Parse the format string */
s = t = format;
while (*s) {
const char *begin;
const char *var = NULL;
int varlen = 0;
const char *def = "-";
int deflen = 1;
int flags = 0;
const char *a;
const char *b;
const char *q;
struct fmt *fmt;
struct fmtvar *fmtvar;
if (*s != '%') {
s++;
continue;
}
/* ".....%... "
* ^---^ add this part as verbatim string */
if (s > t)
if (!format_add(fconf,
NULL, 0,
t, (int)(s - t),
0, errstr, errstr_size))
return -1;
begin = s;
s++;
if (*s != '{') {
s++;
continue;
}
/* Parse '{VAR}X': '*s' will be set to X, and 'var' to VAR.
* Features:
*
* VAR?DEF where DEF is a default value.
*
*/
a = s+1;
b = strchr(a, '}');
if (!b) {
snprintf(errstr, errstr_size,
"Expecting '}' after \"%.*s...\"",
30, begin);
return -1;
}
if (a == b) {
snprintf(errstr, errstr_size,
"Empty {} identifier at \"%.*s...\"",
30, begin);
return -1;
}
s = b+1;
var = a;
/* Check for ?DEF and !OPTIONs */
if ((q = strnchrs(a, (int)(b-a), "?!"))) {
const char *q2 = q;
varlen = (int)(q - a);
if (varlen == 0)
var = NULL;
/* Scan all ?DEF and !OPTIONs */
do {
int qlen;
q++;
if ((q2 = strnchrs(q, (int)(b-q2-1),
"@?!")))
qlen = (int)(q2-q);
else
qlen = (int)(b-q);
switch (*(q-1))
{
case '?':
/* Default value */
def = q;
deflen = qlen;
break;
case '!':
/* Options */
if (0) {
} else {
snprintf(errstr,
errstr_size,
"Unknown "
"formatter "
"option "
"\"%.*s\" at "
"\"%.*s...\"",
qlen, q,
30, a);
return -1;
}
break;
}
} while ((q = q2));
} else
varlen = (int)(b-a);
/* Add formatter to ordered list of formatters */
if (!(fmt = format_add(fconf, var, varlen,
def, deflen, flags,
errstr, errstr_size)))
return -1;
cnt++;
/* Now add a reference to the variable name, unless it is
* already found. */
if (!(fmtvar = fmtvar_find(fconf, var, varlen)))
fmtvar = fmtvar_add(fconf, var, varlen);
fmt->varidx = fmtvar->idx;
t = s = b+1;
}
/* "..%{..}....."
* ^---^ add this part as verbatim string */
if (s > t)
if (!format_add(fconf, NULL, 0,
t, (int)(s - t), 0,
errstr, errstr_size))
return -1;
/* Add output delimiter to tail of format */
format_add(fconf, NULL, 0,
conf.output_delimiter, strlen(conf.output_delimiter),
0, errstr, errstr_size);
/* Dump parsed format string. */
if (conf.log_level >= 7)
fmt_dump(fconf);
if (fconf->fmt_cnt == 0) {
snprintf(errstr, errstr_size,
"format string is empty");
return -1;
} else if (cnt == 0) {
snprintf(errstr, errstr_size,
"No %%.. formatters in format");
return -1;
}
return fconf->fmt_cnt;
}