-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcc.c
712 lines (601 loc) · 15.8 KB
/
cc.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
#include "cc-internal.h"
#define x(t) #t,
char *cc_types[] = { CC_TYPES };
#undef x
static int is_type_name(char *name, size_t len) {
unsigned int t;
int sc;
for(t=0; t < CC_MAX; t++) {
sc = strncmp(cc_types[t], name, len);
if (sc == 0) return t;
}
return -1;
}
static int slurp(char *file, char **text, size_t *len) {
int fd=-1, rc=-1;
struct stat s;
ssize_t nr;
*text=NULL;
*len = 0;
if (stat(file, &s) == -1) {
fprintf(stderr,"can't stat %s: %s", file, strerror(errno));
goto done;
}
*len = s.st_size;
if (*len == 0) { /* special case, empty file */
rc=0;
goto done;
}
fd = open(file, O_RDONLY);
if (fd == -1) {
fprintf(stderr,"can't open %s: %s", file, strerror(errno));
goto done;
}
*text = malloc(*len);
if (*text == NULL) {
fprintf(stderr,"out of memory\n");
goto done;
}
nr = read(fd, *text, *len);
if (nr < 0) {
fprintf(stderr,"read failed: %s", strerror(errno));
goto done;
}
rc = 0;
done:
if ((rc < 0) && *text) { free(*text); *text=NULL; }
if (fd != -1) close(fd);
return rc;
}
/*
* find start and length of column N (one-based)
* in input buffer buf of length buflen
*
* columns must be space delimited
* returns NULL if column not found
*
* the final column may end in newline or eob
*
* col: column index (1-based)
* len: OUTPUT parameter (column length)
* buf: buffer to find columns in
* buflen: length of buf
*
* returns:
* pointer to column N, or NULL
*/
static
char *get_col(int col, size_t *len, char *buf, size_t buflen) {
char *b, *start=NULL, *eob;
int num;
eob = buf + buflen;
b = buf;
num = 0; /* column number */
*len = 0; /* column length */
while (b < eob) {
if ((*b == ' ') && (num == col)) break; /* end of sought column */
if (*b == '\n') break; /* end of line */
if (*b == ' ') *len = 0; /* skip over whitespace */
if ((*b != ' ') && (*len == 0)) { /* record start of column */
num++;
start = b;
}
if (*b != ' ') (*len)++; /* increment column length */
b++;
}
if ((*len) && (num == col)) return start;
return NULL;
}
/*
* is_empty_line
*
* used to skip whitespace/empty lines
*
* line: buffer to scan
* len: buffer length (may surpass line's newline delimiter)
*
* returns
* 0 (line not empty, nl points to first non-ws byte)
* 1 (line is empty, and nl points to newline,
or nl is NULL if buffer ended before newline)
*
*/
static int ws[256] = {
['\n'] = 2,
[' '] = 1,
['\t'] = 1,
};
static int is_empty_line(char *line, size_t len, char **nl) {
int wc;
*nl = line;
while(len--) {
wc = ws[ (size_t)(**nl) ];
if (wc == 0) return 0; /* found non-ws */
if (wc == 2) return 1; /* found newline */
assert(wc == 1); /* regular ws */
(*nl)++;
}
/* buffer ran out, containing only ws, no newline */
*nl = NULL;
return 1;
}
/*
* parse_cc
*
* parse cc cast file
* having lines of the form
* <type> <name> [default]
*
* returns
* 0 success
* < 0 error
*
*/
static int parse_cc(struct cc *cc, char *buf, size_t sz) {
char *line, *name, *type, *defult, *b;
size_t len1, len2, len3, left;
int lno=1, type_i;
line = buf;
while (line < buf+sz) {
left = sz - (line-buf);
if (is_empty_line(line, left, &b)) {
if (b == NULL) break; /* empty and runs into eob */
goto next_line; /* empty, b is its newline */
}
type = get_col(1, &len1, line, left);
type_i = type ? is_type_name(type, len1) : -1;
name = get_col(2, &len2, line, left);
defult = get_col(3, &len3, line, left);
if ((type_i == -1) || (name == NULL)) {
fprintf(stderr, "parse_cc: syntax error on line %d\n", lno);
return -1;
}
/* type */
utvector_push(&cc->output_types, &type_i);
/* name */
utstring_clear(&cc->tmp);
utstring_bincpy(&cc->tmp, name, len2);
utvector_push(&cc->names, &cc->tmp);
/* default */
utstring_clear(&cc->tmp);
if (defult) utstring_bincpy(&cc->tmp, defult, len3);
utvector_push(&cc->defaults, &cc->tmp);
/* maps */
utvector_extend(&cc->caller_addrs);
utvector_extend(&cc->caller_types);
utvector_extend(&cc->dissect_map);
/* advance to next line */
b = defult ? (defult+len3) : (name+len2);
while ((b < buf+sz) && (*b != '\n')) b++;
next_line:
line = b+1;
lno++;
}
return 0;
}
/* open the cc file describing the buffer format */
struct cc * cc_open( char *file_or_text, int flags, ...) {
int rc = -1, need_free=0, sc;
char *text=NULL, *file;
struct cc *cc = NULL;
size_t len=0;
va_list ap;
va_start(ap, flags);
/* only allow one mode or the other */
if (((flags & CC_FILE) ^ (flags & CC_BUFFER)) == 0) {
fprintf(stderr,"cc_open: invalid flags\n");
goto done;
}
cc = calloc(1, sizeof(*cc));
if (cc == NULL) {
fprintf(stderr,"cc_open: out of memory\n");
goto done;
}
utmm_init(&cc_mm,cc,1);
if (flags & CC_FILE) {
file = file_or_text;
if (slurp(file, &text, &len) < 0) goto done;
need_free = 1;
}
if (flags & CC_BUFFER) {
text = file_or_text;
len = va_arg(ap, size_t);
}
assert(len > 0);
sc = parse_cc(cc, text, len);
if (sc < 0) goto done;
rc = 0;
done:
if (text && need_free) free(text);
if ((rc < 0) && cc) {
utmm_fini(&cc_mm,cc,1);
free(cc);
cc = NULL;
}
va_end(ap);
return cc;
}
int cc_close(struct cc *cc) {
utmm_fini(&cc_mm,cc,1);
free(cc);
return 0;
}
/* get the slot index for the field having given name */
static int get_index(struct cc *cc, char *name) {
int i=0;
UT_string *s = NULL;
while ( (s = utvector_next(&cc->names, s))) {
if (strcmp(name, utstring_body(s)) == 0) break;
i++;
}
return s ? i : -1;
}
static void cc_mapv_clear(struct cc *cc)
{
void **mp = NULL;
while ( (mp = utvector_next(&cc->caller_addrs, mp))) {
*mp = NULL;
}
}
/* associate pointers into caller memory with cc fields */
int cc_mapv(struct cc *cc, struct cc_map *map, int count) {
int rc=-1, i, n, nmapped=0;
struct cc_map *m;
cc_type *ot, *ct;
void **mp;
cc_mapv_clear(cc);
for(n=0; n < count; n++) {
m = &map[n];
i = get_index(cc,m->name);
if (i < 0) {
m->addr = NULL; /* ignore field; inform caller */
continue;
}
mp = utvector_elt(&cc->caller_addrs, i);
ot = utvector_elt(&cc->output_types, i);
ct = utvector_elt(&cc->caller_types, i);
*ct = m->type;
*mp = m->addr;
if (cc_conversions[*ct][*ot] == NULL) goto done;
nmapped++;
}
rc = 0;
done:
return (rc < 0) ? rc : nmapped;
}
/*
* cc_capture
*
* pack caller memory from previously established cc_map
* to flattened buffer.
*
* DO NOT free the output buffer (out)
* it is internal memory associated with the struct cc
* and is released on cc_close. it is also overwritten
* by a subsequent call to cc_capture
*
* this flattened buffer can be transmitted or saved,
* as is. it can also be dissected into its fields
* (cc_dissect) or dumped to json (cc_to_json).
*
* returns
* 0 success
* -1 error (such as, an unmapped field in the cc_map)
*
*/
int cc_capture(struct cc *cc, char **out, size_t *len) {
int rc = -1, i=0, sc;
UT_string *df, *fn;
cc_type *ot, *ct, t;
void **mp, *p;
char *def, *n;
utstring_clear(&cc->flat);
*out = NULL;
*len = 0;
fn = NULL;
while( (fn = utvector_next(&cc->names, fn))) {
mp = utvector_elt(&cc->caller_addrs, i);
ot = utvector_elt(&cc->output_types, i);
ct = utvector_elt(&cc->caller_types, i);
df = utvector_elt(&cc->defaults, i);
i++;
def = utstring_body(df);
n = utstring_body(fn);
t = *ct;
p = *mp;
if (p == NULL) { /* no caller pointer for this field */
if (utstring_len(df) > 0) { /* use default */
t = CC_str;
p = &def;
} else {
fprintf(stderr, "required field absent: %s\n", n);
goto done;
}
}
xcpf fcn = cc_conversions[t][*ot];
if (fcn == NULL) {
fprintf(stderr, "cc_capture: unsupported conversion (%s -> %s)\n",
cc_types[t], cc_types[*ot]);
goto done;
}
sc = fcn(&cc->flat, p, CC_MEM2FLAT);
if (sc < 0) {
fprintf(stderr,"conversion error (%s)\n", n);
goto done;
}
}
*out = utstring_body(&cc->flat);
*len = utstring_len(&cc->flat);
rc = 0;
done:
return rc;
}
/*
* cc_to_json
*
* convert a flat buffer to JSON
*
* the flat buffer is one previously obtained from
* cc_capture
*
* NOTE: output is volatile internal memory!
* DO NOT free! (it is freed in cc_close)
* use immediately, or copy.
* it is overwritten on the next call to cc_to_json
*
* flags:
* CC_PRETTY pretty print JSON
* CC_NEWLINE append newline
*
* returns
* 0 success
* -1 error
*
*/
int cc_to_json(struct cc *cc, char **out, size_t *out_len,
char *in, size_t in_len, int flags) {
int rc = -1, i, u, json_flags=0;
char *key, *f, *o;
UT_string *fn;
cc_type *ot;
json_t *j;
size_t l;
f = in;
l = in_len;
/* iterate over the cast format to interpret buffer.
* convert each slot to json, adding to json object */
i = 0;
fn = NULL;
json_object_clear(cc->json);
while ( (fn = utvector_next(&cc->names, fn))) {
key = utstring_body(fn);
ot = utvector_elt(&cc->output_types, i);
u = slot_to_json(*ot,f,l,&j);
if (u < 0) goto done;
if (json_object_set_new(cc->json, key, j) < 0) goto done;
f += u;
l -= u;
i++;
}
/* dump resulting json into temp output buffer */
utstring_clear(&cc->tmp);
json_flags |= JSON_SORT_KEYS;
json_flags |= (flags & CC_PRETTY) ? JSON_INDENT(1) : 0;
#if JANSSON_VERSION_HEX >= 0x021000
int failsafe = 0;
tryagain:
*out = utstring_body(&cc->tmp);
*out_len = json_dumpb(cc->json, *out, cc->tmp.n, json_flags);
if (*out_len > cc->tmp.n) {
utstring_reserve(&cc->tmp,*out_len);
if (failsafe++) goto done;
goto tryagain;
}
#else
/* older jansson allocates */
o = json_dumps(cc->json, json_flags);
if (o == NULL) goto done;
*out_len = strlen(o);
utstring_bincpy(&cc->tmp, o, *out_len);
*out = utstring_body(&cc->tmp);
free(o);
#endif
if (flags & CC_NEWLINE) {
utstring_reserve(&cc->tmp, 1);
utstring_bincpy(&cc->tmp, "\n", 1);
*out = utstring_body(&cc->tmp);
*out_len = (*out_len) + 1;
}
rc = 0;
done:
return rc;
}
static size_t cc_is_fixed_length(cc_type t) {
if (CC_i8 == t) return sizeof(uint8_t);
if (CC_i16 == t) return sizeof(int16_t);
if (CC_u16 == t) return sizeof(uint16_t);
if (CC_i32 == t) return sizeof(int32_t);
if (CC_ipv4 == t) return sizeof(int32_t);
if (CC_mac == t) return 6 * sizeof(char);
if (CC_d64 == t) return sizeof(double);
return 0;
}
/*
* cc_restore
*
* extract data from flattened buffer to caller mappings
*
* given a flattened buffer (in), and a struct cc the caller
* has mapped using cc_mapv, this function unpacks the values
* from the flattened buffer into caller memory.
*
* CALLER MUST ASSUME THE RESTORED VALUES ARE IN VOLATILE MEMORY!
* That means, the values are only valid immediately when this
* function returns. They are invalidated by a subsequent call
* of cc_restore, cc_dissect, cc_capture or cc_close.
*
* in: flattened input buffer (e.g. from cc_capture)
* in_len: length of in
* flags: must be 0
* returns
* 0 success
* -1 error (such as input buffer fails validation)
*
*/
int cc_restore(struct cc *cc, char *in, size_t in_len, int flags) {
void **mp, **ca, *p, *rest_before=NULL;
int sc, rc = -1, count, i;
struct cc_map *map = NULL;
size_t off, l;
cc_type *ct;
if (flags) goto done;
sc = cc_dissect(cc, &map, &count, in, in_len, 0);
if (sc < 0) goto done;
while(cc->rest.d != rest_before) {
utstring_clear(&cc->rest);
rest_before = cc->rest.d;
for(i = 0; i < count; i++) {
/* has caller mapped this item? */
mp = utvector_elt(&cc->caller_addrs, i);
if (*mp == NULL) continue;
/* convert stored type to the caller type */
ct = utvector_elt(&cc->caller_types, i);
ca = utvector_elt(&cc->caller_addrs, i);
xcpf fcn = cc_conversions[map[i].type][*ct];
if (fcn == NULL) {
fprintf(stderr, "cc_restore: unsupported conversion (%s -> %s)\n",
cc_types[map[i].type], cc_types[*ct]);
goto done;
}
/* record offset of next datum */
off = cc->rest.i;
sc = fcn(&cc->rest, map[i].addr, CC_FLAT2MEM);
if (sc < 0) {
fprintf(stderr,"conversion error\n");
goto done;
}
/* give caller the converted item by value,
* or by pointer if its not of fixed length */
l = cc_is_fixed_length(*ct);
if (l) memcpy(*ca, cc->rest.d + off, l);
else if (*ct == CC_ipv46) {
p = cc->rest.d + off;
memcpy(*ca, &p, sizeof(void*));
} else if ((*ct == CC_str8) || (*ct == CC_str)) {
p = cc->rest.d + off;
memcpy(*ca, &p, sizeof(void*));
} else if (*ct == CC_blob) {
p = cc->rest.d + off;
struct cc_blob *bp = (struct cc_blob*)(*ca);
memcpy(&bp->len, p, sizeof(uint32_t));
bp->buf = p + sizeof(uint32_t);
} else {
assert(0);
goto done;
}
}
}
rc = 0;
done:
return rc;
}
/*
* cc_dissect
*
* convert a flattened buffer to a list of cc_map
*
* given a flattened buffer (in) this function
* parses its contents, populating a cc_map[]
* with one element for each field:
*
* map[n].name - C string with field name
* map[n].addr - pointer into input buffer
* map[n].type - CC_i8, CC_i16, etc field type
*
* the map array is volatile; it's internal to the cc structure.
* it remains valid while the caller keeps the input buffer intact
* only until the next call to cc_dissect, cc_restore or cc_close.
*
* map: receives the map
* count: receives number of elements in map
* in: flattened input buffer (e.g. from cc_capture)
* in_len: length of in
* flags: must be 0
* returns
* 0 success
* -1 error (such as input buffer fails validation)
*
*/
int cc_dissect(struct cc *cc, struct cc_map **map, int *count,
char *in, size_t in_len, int flags) {
struct cc_map *dm;
int rc = -1, i;
UT_string *fn;
uint32_t u32;
cc_type *ot;
uint8_t u8;
size_t l,r;
char *p;
if (flags) goto done;
*count = utvector_len(&cc->dissect_map);
*map = utvector_elt(&cc->dissect_map, 0);
dm = *map;
fn = NULL;
p = in;
r = in_len;
for(i = 0; i < *count; i++) {
fn = utvector_elt(&cc->names, i);
ot = utvector_elt(&cc->output_types, i);
dm[i].name = utstring_body(fn);
dm[i].type = *ot;
dm[i].addr = p;
switch( dm[i].type ) {
case CC_i8: l = sizeof(uint8_t); break;
case CC_i16: l = sizeof(int16_t); break;
case CC_u16: l = sizeof(uint16_t); break;
case CC_i32: l = sizeof(int32_t); break;
case CC_d64: l = sizeof(double); break;
case CC_mac: l = 6; break;
case CC_ipv4: l = 4; break;
case CC_ipv46:
if (r < sizeof(uint8_t)) goto done;
memcpy(&u8, p, sizeof(uint8_t));
assert((u8 == 4) || (u8 == 16));
l = sizeof(uint8_t) + u8;
break;
case CC_str8:
if (r < sizeof(uint8_t)) goto done;
memcpy(&u8, p, sizeof(uint8_t));
l = sizeof(uint8_t) + u8;
break;
case CC_str: /* FALL THRU */
case CC_blob:
if (r < sizeof(uint32_t)) goto done;
memcpy(&u32, p, sizeof(uint32_t));
l = sizeof(uint32_t) + u32;
break;
default:
assert(0);
break;
}
if (r < l) goto done;
p += l;
r -= l;
}
/* require input was fully consumed */
if (r > 0) goto done;
rc = 0;
done:
return rc;
}
/*
* cc_count
*
* get the number of fields in the cc map
*
*/
int cc_count(struct cc *cc) {
int c;
c = utvector_len(&cc->names);
return c;
}