-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecoder.c
More file actions
1641 lines (1510 loc) · 49 KB
/
decoder.c
File metadata and controls
1641 lines (1510 loc) · 49 KB
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
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This file is a part of ACTF.
*
* Copyright (C) 2024 Adam Wendelin <adwe live se>
*
* ACTF is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* ACTF is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
* Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with ACTF. If not, see
* <https://www.gnu.org/licenses/>.
*/
#include <assert.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "breader.h"
#include "event_int.h"
#include "decoder.h"
#include "crust/common.h"
#include "crust/arena.h"
#include "event_generator.h"
#include "fld.h"
#include "fld_cls_int.h"
#include "metadata_int.h"
#include "pkt_int.h"
#include "error.h"
#include "pkt_state.h"
enum dec_ctx {
DEC_CTX_PKT_HEADER,
DEC_CTX_PKT_CTX,
DEC_CTX_EVENT_HEADER,
DEC_CTX_EVENT_COMMON_CTX,
DEC_CTX_EVENT_SPECIFIC_CTX,
DEC_CTX_EVENT_PAYLOAD,
};
struct dec_state {
struct arena pkt_arena;
struct arena ev_arena;
struct pkt_state pkt_s;
struct actf_pkt pkt;
struct actf_event *ev;
enum dec_ctx ctx;
};
enum decoding_state {
DECODING_STATE_OK = 0,
DECODING_STATE_RESUME_PKT = 1 << 0,
DECODING_STATE_RESUME_SEEK = 1 << 1,
DECODING_STATE_ERROR = 1 << 2,
};
struct actf_decoder {
// the state is multi-faceted to allow for a successful seek to
// exist alongside a dormant error. MULTI-LEVEL ERROR DRIFTING.
enum decoding_state state;
// stored error code if state & DECODING_STATE_ERROR since we
// want to return all valid events before returning an error.
int err_rc;
// the idx of evs to return if state & DECODING_STATE_RESUME_SEEK
// it is the first event matching the seek query.
size_t seek_evs_off;
size_t seek_evs_len;
const struct actf_metadata *metadata;
struct breader br;
struct actf_event **evs;
size_t evs_cap;
struct dec_state dec_s;
struct error err;
};
static int fld_cls_decode(struct actf_decoder *dec, const struct actf_fld_cls *cls,
struct actf_fld *val);
static struct actf_fld *fld_loc_locate(const struct actf_fld_loc loc, struct actf_decoder *dec,
struct actf_fld *cur);
static struct actf_fld *get_struct_member(struct actf_fld *val, const char *name)
{
assert(val->type == ACTF_FLD_TYPE_STRUCT);
for (size_t i = 0; i < val->cls->cls.struct_.n_members &&
val->cls->cls.struct_.member_clses[i].name; i++) {
if (strcmp(name, val->cls->cls.struct_.member_clses[i].name) == 0) {
return &val->d.struct_.vals[i];
}
}
return NULL;
}
static int get_arr_cls_len(struct actf_fld *val, struct actf_decoder *dec, size_t *len)
{
struct error *e = &dec->err;
switch (val->cls->type) {
case ACTF_FLD_CLS_STATIC_LEN_ARR:
*len = val->cls->cls.static_len_arr.len;
return ACTF_OK;
case ACTF_FLD_CLS_DYN_LEN_ARR: {
struct actf_fld *len_val =
fld_loc_locate(val->cls->cls.dyn_len_arr.len_fld_loc,
dec, val);
if (!len_val) {
eprependf(e, "no dynamic-length-array length");
return ACTF_MISSING_FLD_LOC;
}
if (len_val->type != ACTF_FLD_TYPE_UINT) {
eprintf(e,
"dynamic-length-array field has a length indicator that is not an unsigned integer");
return ACTF_WRONG_FLD_TYPE;
}
*len = len_val->d.uint.val;
return ACTF_OK;
}
default:
eprintf(e, "trying to retrieve length of a non-array field class");
return ACTF_INTERNAL;
}
}
/* Not finding a field is considered an error. */
static struct actf_fld *fld_loc_locate(const struct actf_fld_loc loc, struct actf_decoder *dec,
struct actf_fld *cur)
{
struct actf_pkt *pkt = &dec->dec_s.pkt;
struct actf_event *ev = dec->dec_s.ev;
struct error *e = &dec->err;
// 6.3.2 Field location procedure
struct actf_fld *fld;
switch (loc.origin) {
case ACTF_FLD_LOC_ORIGIN_NONE:
fld = cur->parent;
break;
case ACTF_FLD_LOC_ORIGIN_PKT_HEADER:
fld = &pkt->props[ACTF_PKT_PROP_HEADER];
break;
case ACTF_FLD_LOC_ORIGIN_PKT_CTX:
fld = &pkt->props[ACTF_PKT_PROP_CTX];
break;
case ACTF_FLD_LOC_ORIGIN_EVENT_HEADER:
fld = ev ? &ev->props[ACTF_EVENT_PROP_HEADER] : NULL;
break;
case ACTF_FLD_LOC_ORIGIN_EVENT_COMMON_CTX:
fld = ev ? &ev->props[ACTF_EVENT_PROP_COMMON_CTX] : NULL;
break;
case ACTF_FLD_LOC_ORIGIN_EVENT_SPECIFIC_CTX:
fld = ev ? &ev->props[ACTF_EVENT_PROP_SPECIFIC_CTX] : NULL;
break;
case ACTF_FLD_LOC_ORIGIN_EVENT_PAYLOAD:
fld = ev ? &ev->props[ACTF_EVENT_PROP_PAYLOAD] : NULL;
break;
default:
fld = NULL;
break;
}
if (!fld || fld->type != ACTF_FLD_TYPE_STRUCT) {
eprintf(e, "unable to locate field with origin: %s",
actf_fld_loc_origin_name(loc.origin) ? actf_fld_loc_origin_name(loc.
origin) :
"relative");
return NULL;
}
for (size_t i = 0; i < loc.path_len; i++) {
const char *next_path = loc.path[i];
if (next_path) {
fld = get_struct_member(fld, next_path);
if (!fld) {
eprintf(e, "field location struct has no member named %s",
next_path);
return NULL;
}
if (fld->type == ACTF_FLD_TYPE_NIL) {
eprintf(e,
"field location points to a field which is not yet decoded");
return NULL;
}
} else {
if (!fld->parent) {
eprintf(e,
"field location points to a field's containing struct, but the field has no encompassing struct");
return NULL;
}
fld = fld->parent;
}
switch (fld->type) {
case ACTF_FLD_TYPE_BOOL:
case ACTF_FLD_TYPE_SINT:
case ACTF_FLD_TYPE_UINT:
if (i != (loc.path_len - 1)) {
eprintf(e, "field location points to an integer-based field-value"
" but there are remaining elements in the field location path");
return NULL;
}
return fld;
case ACTF_FLD_TYPE_STRUCT:
break;
case ACTF_FLD_TYPE_ARR: {
/* While V is an array:
- If V isn’t currently being decoded, then report an error and abort the data stream decoding process.
- Set V to the element of V currently being decoded.
*/
do {
size_t len;
if (get_arr_cls_len(fld, dec, &len) < 0) {
return NULL;
}
if (fld->d.arr.n_vals == len) {
eprintf(e,
"trying to lookup a field location in an already decoded array");
return NULL;
}
fld = &fld->d.arr.vals[fld->d.arr.n_vals];
} while (fld->type == ACTF_FLD_TYPE_ARR);
break;
}
default:
eprintf(e, "field location points to a non-supported field-class");
return NULL;
}
}
eprintf(e, "unable to find field location");
return NULL;
}
/* Returns the number of content bits left in the provided packet. */
static uint64_t pkt_bits_remaining(struct pkt_state *pkt_s, struct breader *br)
{
if ((br->tot_bit_cnt - pkt_s->bit_off) < pkt_s->content_len) {
return pkt_s->content_len - (br->tot_bit_cnt - pkt_s->bit_off);
} else {
return 0;
}
}
/* CTF2-SPEC-2.0 says:
* "If X ≥ PKT_CONTENT_LEN, then report an error and abort the data stream decoding process."
* X is "Current decoding offset/position (bits) from the beginning of P[acket]."
* PKT_CONTENT_LEN is "Current content length (bits) of P[acket]."
*
* In code below, X is represented by `br->tot_bit_cnt - pkt_s->bit_off`
* and PKT_CONTENT_LEN is represented by `pkt_s->content_len`.
*
* Aligning to the absolute end of the packet's content length
* should be permitted since we have not read out-of-bounds with
* regards to the content length, so X >= PKT_CONTENT_LEN in the
* specification should probably be X > PKT_CONTENT_LEN. A
* field-class can have zero size so aligning to the end of the
* content can still yield a successful decoding.
*/
#define do_alignm(br, align, pkt_s, e, bo) \
do { \
breader_align_##bo(br, align); \
if (unlikely((br->tot_bit_cnt - pkt_s->bit_off) > pkt_s->content_len)) { \
eprintf(e, "trying to read more bits than content length of packet"); \
return ACTF_NOT_ENOUGH_BITS; \
} \
return ACTF_OK; \
} while (0)
static int do_align_le(struct breader *br, size_t align, struct pkt_state *pkt_s, struct error *e)
{
do_alignm(br, align, pkt_s, e, le);
}
static int do_align_be(struct breader *br, size_t align, struct pkt_state *pkt_s, struct error *e)
{
do_alignm(br, align, pkt_s, e, be);
}
static int do_align(struct breader *br, size_t align, struct pkt_state *pkt_s, struct error *e)
{
if (br->bo == ACTF_LIL_ENDIAN) {
return do_align_le(br, align, pkt_s, e);
} else {
return do_align_be(br, align, pkt_s, e);
}
}
/* Reverses the bits of v. Based on
* https://graphics.stanford.edu/~seander/bithacks.html#BitReverseObvious
* Public Domain code 2024-04-07 */
static uint64_t reverse_bits(uint64_t v, size_t len)
{
uint64_t r = v; // r will be reversed bits of v; first get LSB of v
int s = sizeof(v) * 8 - 1; // extra shift needed at end
for (v >>= 1; v; v >>= 1) {
r <<= 1;
r |= v & 1;
s--;
}
r <<= s; // shift when v's highest bits are zero
r >>= (sizeof(v) * 8 - len);
return r;
}
static inline bool needs_reverse_bits_le(const struct fxd_len_bit_arr_fld_cls *cls)
{
return cls->bito == ACTF_LAST_TO_FIRST;
}
static inline bool needs_reverse_bits_be(const struct fxd_len_bit_arr_fld_cls *cls)
{
return cls->bito == ACTF_FIRST_TO_LAST;
}
static inline uint64_t read_upper_bits_le(struct breader *br, uint64_t result,
size_t remain_bits, size_t first_bits)
{
return result | (breader_peek_le(br, remain_bits) << first_bits);
}
static inline uint64_t read_upper_bits_be(struct breader *br, uint64_t result,
size_t remain_bits, size_t first_bits)
{
(void) first_bits;
return (result << remain_bits) | breader_peek_be(br, remain_bits);
}
#define fxd_len_bit_arr_decodem(dec, cls, val, BO) \
do { \
assert(cls->len <= 64); \
int rc; \
struct pkt_state *pkt_s = &dec->dec_s.pkt_s; \
struct breader *br = &dec->br; \
struct error *e = &dec->err; \
\
breader_set_bo(br, cls->bo); \
if (unlikely((rc = do_align_##BO(br, cls->align, pkt_s, e)) < 0)) { \
return rc; \
} \
if (unlikely(pkt_bits_remaining(pkt_s, br) < cls->len)) { \
eprintf(e, "not enough bits to read in packet"); \
return ACTF_NOT_ENOUGH_BITS; \
} \
if (unlikely((pkt_s->opt_flags & PKT_LAST_BO) && \
pkt_s->last_bo != cls->bo && \
! breader_byte_aligned(br))) { \
eprintf(e, "changing byte-order in the middle of a byte"); \
return ACTF_MID_BYTE_ENDIAN_SWAP; \
} \
/* refill is run unconditionally here, it could be more efficient \
* to decode lots of small ints by only refilling if cls->len < \
* br->lookahead_bit_cnt. \
*/ \
size_t avail_bits = breader_refill_##BO(br); \
if (unlikely(! avail_bits)) { \
eprintf(e, "not enough bits to read in bit stream"); \
return ACTF_NOT_ENOUGH_BITS; \
} \
/* The sanity check below should not be needed since an equivalent \
* check will be done for `remain_bits below`. Not checking for \
* bits here just means that we will consume the bits before \
* realizing they won't be enough. \
*/ \
/* if (cls->len < MAX_READ_BITS && avail_bits < cls->len) { */ \
/* eprintf(e, "Not enough bits to read"); */ \
/* return ACTF_NOT_ENOUGH_BITS; */ \
/* } */ \
\
/* Read up to MAX_READ_BITS */ \
size_t first_bits = MIN(avail_bits, cls->len); \
uint64_t result = breader_peek_##BO(br, first_bits); \
breader_consume_##BO(br, first_bits); \
\
size_t remain_bits = cls->len - first_bits; \
if (remain_bits) { \
/* Read the last cls->len - MAX_READ_BITS bits */ \
avail_bits = breader_refill_##BO(br); \
if (unlikely(avail_bits < remain_bits)) { \
eprintf(e, "not enough bits to read in bit stream"); \
return ACTF_NOT_ENOUGH_BITS; \
} \
result = read_upper_bits_##BO(br, result, remain_bits, first_bits); \
breader_consume_##BO(br, remain_bits); \
} \
/* Reverse the bits if necessary. */ \
if (needs_reverse_bits_##BO(cls)) { \
*val = reverse_bits(result, cls->len); \
} else { \
*val = result; \
} \
\
pkt_s->last_bo = cls->bo; \
pkt_s->opt_flags |= PKT_LAST_BO; \
return ACTF_OK; \
} while (0) \
static int fxd_len_bit_arr_decode(struct actf_decoder *dec,
const struct fxd_len_bit_arr_fld_cls *cls, uint64_t *val)
{
if (cls->bo == ACTF_LIL_ENDIAN) {
fxd_len_bit_arr_decodem(dec, cls, val, le);
} else {
fxd_len_bit_arr_decodem(dec, cls, val, be);
}
}
static int fld_cls_fxd_len_bit_arr_decode(struct actf_decoder *dec,
const struct actf_fld_cls *gen_cls, struct actf_fld *val)
{
assert(gen_cls->type == ACTF_FLD_CLS_FXD_LEN_BIT_ARR);
int rc;
const struct fxd_len_bit_arr_fld_cls *cls = &gen_cls->cls.fxd_len_bit_arr;
uint64_t bit_arr;
if ((rc = fxd_len_bit_arr_decode(dec, cls, &bit_arr)) < 0) {
eprependf(&dec->err, actf_fld_cls_type_name(gen_cls->type));
return rc;
}
val->type = ACTF_FLD_TYPE_UINT;
val->d.uint.val = bit_arr;
return ACTF_OK;
}
static int fld_cls_fxd_len_bit_map_decode(struct actf_decoder *dec,
const struct actf_fld_cls *gen_cls, struct actf_fld *val)
{
assert(gen_cls->type == ACTF_FLD_CLS_FXD_LEN_BIT_MAP);
int rc;
const struct fxd_len_bit_map_fld_cls *cls = &gen_cls->cls.fxd_len_bit_map;
uint64_t bit_arr;
if ((rc = fxd_len_bit_arr_decode(dec, &cls->bit_arr, &bit_arr)) < 0) {
eprependf(&dec->err, actf_fld_cls_type_name(gen_cls->type));
return rc;
}
val->type = ACTF_FLD_TYPE_BIT_MAP;
val->d.bit_map.val = bit_arr;
return ACTF_OK;
}
static uint64_t sext(uint64_t val, uint64_t n_bits)
{
uint64_t sign_bit = val >> (n_bits - 1);
if (n_bits < 64 && sign_bit) {
uint64_t sign_mask = ((uint64_t) 1 << (64 - n_bits)) - 1;
val |= (sign_mask << n_bits);
}
return val;
}
static int fld_cls_fxd_len_sint_decode(struct actf_decoder *dec,
const struct actf_fld_cls *gen_cls, struct actf_fld *val)
{
assert(gen_cls->type == ACTF_FLD_CLS_FXD_LEN_SINT);
int rc;
const struct fxd_len_int_fld_cls *cls = &gen_cls->cls.fxd_len_int;
uint64_t bit_arr;
if ((rc = fxd_len_bit_arr_decode(dec, &cls->bit_arr, &bit_arr)) < 0) {
eprependf(&dec->err, actf_fld_cls_type_name(gen_cls->type));
return rc;
}
val->type = ACTF_FLD_TYPE_SINT;
val->d.int_.val = sext(bit_arr, cls->bit_arr.len);
return ACTF_OK;
}
static int fld_cls_fxd_len_uint_decode(struct actf_decoder *dec,
const struct actf_fld_cls *gen_cls, struct actf_fld *val)
{
assert(gen_cls->type == ACTF_FLD_CLS_FXD_LEN_UINT);
int rc;
const struct fxd_len_int_fld_cls *cls = &gen_cls->cls.fxd_len_int;
uint64_t bit_arr;
if ((rc = fxd_len_bit_arr_decode(dec, &cls->bit_arr, &bit_arr)) < 0) {
eprependf(&dec->err, actf_fld_cls_type_name(gen_cls->type));
return rc;
}
val->type = ACTF_FLD_TYPE_UINT;
val->d.uint.val = bit_arr;
return ACTF_OK;
}
static int fld_cls_fxd_len_bool_decode(struct actf_decoder *dec,
const struct actf_fld_cls *gen_cls, struct actf_fld *val)
{
assert(gen_cls->type == ACTF_FLD_CLS_FXD_LEN_BOOL);
int rc;
const struct fxd_len_bool_fld_cls *cls = &gen_cls->cls.fxd_len_bool;
uint64_t bit_arr;
if ((rc = fxd_len_bit_arr_decode(dec, &cls->bit_arr, &bit_arr)) < 0) {
eprependf(&dec->err, actf_fld_cls_type_name(gen_cls->type));
return rc;
}
val->type = ACTF_FLD_TYPE_BOOL;
val->d.bool_.val = !!bit_arr;
return ACTF_OK;
}
static int fld_cls_fxd_len_float_decode(struct actf_decoder *dec,
const struct actf_fld_cls *gen_cls, struct actf_fld *val)
{
assert(gen_cls->type == ACTF_FLD_CLS_FXD_LEN_FLOAT);
int rc;
const struct fxd_len_float_fld_cls *cls = &gen_cls->cls.fxd_len_float;
uint64_t bit_arr;
if ((rc = fxd_len_bit_arr_decode(dec, &cls->bit_arr, &bit_arr)) < 0) {
eprependf(&dec->err, actf_fld_cls_type_name(gen_cls->type));
return rc;
}
val->type = ACTF_FLD_TYPE_REAL;
switch (cls->bit_arr.len) {
case 32: {
union {
uint32_t u;
float f;
} conv = {.u = bit_arr };
val->d.real.f32 = conv.f;
break;
}
case 64: {
union {
uint64_t u;
double f;
} conv = {.u = bit_arr };
val->d.real.f64 = conv.f;
break;
}
default:
eprintf(&dec->err, "unsupported float of length %" PRIu64, cls->bit_arr.len);
return ACTF_UNSUPPORTED_LENGTH;
}
return ACTF_OK;
}
static int fld_cls_var_len_int_decode(struct actf_decoder *dec,
const struct actf_fld_cls *gen_cls,
uint64_t *val, uint64_t *n_bits)
{
int rc;
struct pkt_state *pkt_s = &dec->dec_s.pkt_s;
struct breader *br = &dec->br;
struct error *e = &dec->err;
breader_set_bo(br, ACTF_LIL_ENDIAN); // not req if I read bytes.
if ((rc = do_align(br, actf_fld_cls_get_align_req(gen_cls), pkt_s, e)) < 0) {
return rc;
}
if (pkt_bits_remaining(pkt_s, br) < 8) {
eprintf(e, "not enough bits to read in packet");
return ACTF_NOT_ENOUGH_BITS;
}
uint64_t result = 0;
unsigned shift = 0;
bool fin = false;
while (!fin) {
size_t avail_bits = breader_refill(br); // will be divisable by 8 due to align
if (!avail_bits) {
eprintf(e, "not enough bits to decode variable length integer");
return ACTF_NOT_ENOUGH_BITS;
}
while (!fin && avail_bits) {
result |= (breader_peek(br, 7) << shift);
breader_consume(br, 7);
fin = !breader_peek(br, 1);
breader_consume(br, 1);
avail_bits -= 8;
shift += 7;
}
if ((!fin && pkt_bits_remaining(pkt_s, br) < 8) ||
(fin && (br->tot_bit_cnt - pkt_s->bit_off) > pkt_s->content_len)) {
eprintf(e, "not enough bits to read in packet");
return ACTF_NOT_ENOUGH_BITS;
}
}
*val = result;
*n_bits = MIN(shift, 64); // Result is always truncated to 64-bits
return ACTF_OK;
}
static int fld_cls_var_len_uint_decode(struct actf_decoder *dec,
const struct actf_fld_cls *gen_cls, struct actf_fld *val)
{
assert(gen_cls->type == ACTF_FLD_CLS_VAR_LEN_UINT);
int rc;
uint64_t uval, n_bits;
if ((rc = fld_cls_var_len_int_decode(dec, gen_cls, &uval, &n_bits)) < 0) {
eprependf(&dec->err, actf_fld_cls_type_name(gen_cls->type));
return rc;
}
val->type = ACTF_FLD_TYPE_UINT;
val->d.uint.val = uval;
val->d.uint.len = n_bits;
return ACTF_OK;
}
static int fld_cls_var_len_sint_decode(struct actf_decoder *dec,
const struct actf_fld_cls *gen_cls, struct actf_fld *val)
{
assert(gen_cls->type == ACTF_FLD_CLS_VAR_LEN_SINT);
int rc;
uint64_t uval, n_bits;
if ((rc = fld_cls_var_len_int_decode(dec, gen_cls, &uval, &n_bits)) < 0) {
eprependf(&dec->err, actf_fld_cls_type_name(gen_cls->type));
return rc;
}
val->type = ACTF_FLD_TYPE_SINT;
val->d.int_.val = sext(uval, n_bits);
return ACTF_OK;
}
/* find_null_term returns a pointer to the last byte of the null
* terminator if it exists. */
static uint8_t *find_null_term(uint8_t *ptr, size_t n_bytes, enum actf_encoding enc)
{
uint8_t *term = NULL; // Will point to the last byte of the null terminator
size_t codepoint_sz = actf_encoding_to_codepoint_size(enc);
switch (codepoint_sz) {
case 1:
term = memchr(ptr, 0, n_bytes);
break;
default:
for (size_t i = 0; i < n_bytes; i += codepoint_sz) {
size_t j;
for (j = 0; j < codepoint_sz; j++) {
if (ptr[i + j]) {
break;
}
}
if (j == codepoint_sz) {
term = ptr + i + j - 1;
break;
}
}
break;
}
return term;
}
static int fld_cls_null_term_str_decode(struct actf_decoder *dec,
const struct actf_fld_cls *gen_cls, struct actf_fld *val)
{
assert(gen_cls->type == ACTF_FLD_CLS_NULL_TERM_STR);
int rc;
const struct null_term_str_fld_cls *cls = &gen_cls->cls.null_term_str;
struct pkt_state *pkt_s = &dec->dec_s.pkt_s;
struct breader *br = &dec->br;
struct error *e = &dec->err;
if ((rc = do_align(br, actf_fld_cls_get_align_req(gen_cls), pkt_s, e)) < 0) {
return rc;
}
if (pkt_bits_remaining(pkt_s, br) < actf_encoding_to_codepoint_size(cls->base.enc) * 8) {
eprintf(e, "not enough bits to read in packet");
return ACTF_NOT_ENOUGH_BITS;
}
uint8_t *ptr = breader_peek_bytes(br);
size_t n_bytes = MIN(breader_bytes_remaining(br), pkt_bits_remaining(pkt_s, br) / 8);
uint8_t *term = find_null_term(ptr, n_bytes, cls->base.enc);
if (!term) {
eprintf(e, "not enough bytes to decode null terminated string");
return ACTF_NOT_ENOUGH_BITS;
}
size_t str_len = term - ptr;
str_len++;
if (!breader_read_bytes(br, str_len)) {
// We have already verified with bytes_remaining that it is
// fine, but we check anyway in case brain was unlucky.
eprintf(e, "not enough bytes but it should have been ok");
return ACTF_INTERNAL;
}
val->type = ACTF_FLD_TYPE_STR;
val->d.str.ptr = ptr;
val->d.str.len = str_len;
return ACTF_OK;
}
/* is_valid_str_sz checks if str_sz is a valid amount of bytes for the
* specified encoding. For example, 5 bytes is NOT ok for an utf-32
* encoding, but would be fine for a utf-8 encoding. */
static bool is_valid_str_sz(size_t str_sz, enum actf_encoding enc)
{
size_t codepoint_sz = actf_encoding_to_codepoint_size(enc);
return !(str_sz % codepoint_sz);
}
// NOTE: Could pull fld_cls_static_len_str_decode and
// fld_cls_dyn_len_str_decode together into the same function if I use
// fld_cls helpers to get encoding and len from them.
static int fld_cls_static_len_str_decode(struct actf_decoder *dec,
const struct actf_fld_cls *gen_cls, struct actf_fld *val)
{
assert(gen_cls->type == ACTF_FLD_CLS_STATIC_LEN_STR);
int rc;
const struct static_len_str_fld_cls *cls = &gen_cls->cls.static_len_str;
struct pkt_state *pkt_s = &dec->dec_s.pkt_s;
struct breader *br = &dec->br;
struct error *e = &dec->err;
if ((rc = do_align(br, actf_fld_cls_get_align_req(gen_cls), pkt_s, e)) < 0) {
return rc;
}
if (pkt_bits_remaining(pkt_s, br) < cls->len * 8) {
eprintf(e, "not enough bits to read in packet");
return ACTF_NOT_ENOUGH_BITS;
}
uint8_t *ptr = breader_read_bytes(br, cls->len);
if (!ptr) {
eprintf(e, "not enough bytes to decode static-length-string");
return ACTF_NOT_ENOUGH_BITS;
}
uint8_t *term = find_null_term(ptr, cls->len, cls->base.enc);
size_t str_len;
if (term) {
str_len = term - ptr;
str_len++; // +1 to include null-terminator in the len.
} else {
/* No requirement that the string is actually null-terminated.
* Let it be non-null-terminated.
*/
if (!is_valid_str_sz(cls->len, cls->base.enc)) {
eprintf(e, "invalid amount of bytes in \"%s\" string",
actf_encoding_to_name(cls->base.enc));
return ACTF_INVALID_STR_LEN;
}
str_len = cls->len;
}
val->type = ACTF_FLD_TYPE_STR;
val->d.str.ptr = ptr;
val->d.str.len = str_len;
return ACTF_OK;
}
static int fld_cls_dyn_len_str_decode(struct actf_decoder *dec,
const struct actf_fld_cls *gen_cls, struct actf_fld *val)
{
assert(gen_cls->type == ACTF_FLD_CLS_DYN_LEN_STR);
int rc;
const struct dyn_len_str_fld_cls *cls = &gen_cls->cls.dyn_len_str;
struct pkt_state *pkt_s = &dec->dec_s.pkt_s;
struct breader *br = &dec->br;
struct error *e = &dec->err;
/* The value of the field that indicates the length */
struct actf_fld *len_val = fld_loc_locate(cls->len_fld_loc, dec, val);
if (!len_val) {
eprependf(e, "no dynamic-length-string length");
return ACTF_MISSING_FLD_LOC;
}
if (len_val->type != ACTF_FLD_TYPE_UINT) {
eprintf(e,
"dynamic-length-string field has a length indicator that is not an unsigned integer");
return ACTF_WRONG_FLD_TYPE;
}
size_t len = len_val->d.uint.val;
if ((rc = do_align(br, actf_fld_cls_get_align_req(gen_cls), pkt_s, e)) < 0) {
return rc;
}
if (pkt_bits_remaining(pkt_s, br) < len * 8) {
eprintf(e, "not enough bits to read in packet");
return ACTF_NOT_ENOUGH_BITS;
}
uint8_t *ptr = breader_read_bytes(br, len);
if (!ptr) {
eprintf(e, "not enough bytes to decode dynamic-length-string");
return ACTF_NOT_ENOUGH_BITS;
}
uint8_t *term = find_null_term(ptr, len, cls->base.enc);
size_t str_len;
if (term) {
str_len = term - ptr;
str_len++; // +1 to include null-terminator in the len.
} else {
/* No requirement that the string is actually null-terminated.
* Let it be non-null-terminated.
*/
if (!is_valid_str_sz(len, cls->base.enc)) {
eprintf(e, "invalid amount of bytes in \"%s\" string",
actf_encoding_to_name(cls->base.enc));
return ACTF_INVALID_STR_LEN;
}
str_len = len;
}
val->type = ACTF_FLD_TYPE_STR;
val->d.str.ptr = ptr;
val->d.str.len = str_len;
return ACTF_OK;
}
static int fld_cls_static_len_blob_decode(struct actf_decoder *dec,
const struct actf_fld_cls *gen_cls, struct actf_fld *val)
{
assert(gen_cls->type == ACTF_FLD_CLS_STATIC_LEN_BLOB);
int rc;
const struct static_len_blob_fld_cls *cls = &gen_cls->cls.static_len_blob;
struct pkt_state *pkt_s = &dec->dec_s.pkt_s;
struct breader *br = &dec->br;
struct error *e = &dec->err;
if ((rc = do_align(br, actf_fld_cls_get_align_req(gen_cls), pkt_s, e)) < 0) {
return rc;
}
if (pkt_bits_remaining(pkt_s, br) < cls->len * 8) {
eprintf(e, "not enough bits to read in packet");
return ACTF_NOT_ENOUGH_BITS;
}
uint8_t *ptr = breader_read_bytes(br, cls->len);
if (!ptr) {
eprintf(e, "not enough bytes to decode static-length-blob");
return ACTF_NOT_ENOUGH_BITS;
}
val->type = ACTF_FLD_TYPE_BLOB;
val->d.blob.ptr = ptr;
val->d.blob.len = cls->len;
return ACTF_OK;
}
/* Very similar to dyn_len_str, could maybe commonalize. */
static int fld_cls_dyn_len_blob_decode(struct actf_decoder *dec,
const struct actf_fld_cls *gen_cls, struct actf_fld *val)
{
assert(gen_cls->type == ACTF_FLD_CLS_DYN_LEN_BLOB);
int rc;
const struct dyn_len_blob_fld_cls *cls = &gen_cls->cls.dyn_len_blob;
struct pkt_state *pkt_s = &dec->dec_s.pkt_s;
struct breader *br = &dec->br;
struct error *e = &dec->err;
/* The value of the field that indicates the length */
struct actf_fld *len_val = fld_loc_locate(cls->len_fld_loc, dec, val);
if (!len_val) {
eprependf(e, "no dynamic-length-blob length");
return ACTF_MISSING_FLD_LOC;
}
if (len_val->type != ACTF_FLD_TYPE_UINT) {
eprintf(e,
"dynamic-length-blob field has a length indicator that is not an unsigned integer");
return ACTF_WRONG_FLD_TYPE;
}
size_t len = len_val->d.uint.val;
if ((rc = do_align(br, actf_fld_cls_get_align_req(gen_cls), pkt_s, e)) < 0) {
return rc;
}
if (pkt_bits_remaining(pkt_s, br) < len * 8) {
eprintf(e, "not enough bits to read in packet");
return ACTF_NOT_ENOUGH_BITS;
}
uint8_t *ptr = breader_read_bytes(br, len);
if (!ptr) {
eprintf(e, "not enough bytes to decode dynamic-length-blob");
return ACTF_NOT_ENOUGH_BITS;
}
val->type = ACTF_FLD_TYPE_BLOB;
val->d.blob.ptr = ptr;
val->d.blob.len = len;
return ACTF_OK;
}
uint64_t calc_new_def_clk_val(const struct actf_fld_cls *cls, struct actf_fld *val,
uint64_t def_clk_val)
{
// 6.2.1. Clock value update
uint64_t v = actf_fld_uint64(val);
uint64_t l;
switch (cls->type) {
case ACTF_FLD_CLS_FXD_LEN_UINT:
l = cls->cls.fxd_len_int.bit_arr.len;
break;
case ACTF_FLD_CLS_VAR_LEN_UINT:
l = val->d.uint.len / 7 + !!(val->d.uint.len % 7);
break;
default:
// Ignore updating for a non-supported type.
return def_clk_val;
}
uint64_t mask = ((uint64_t) 2 << (l - 1)) - 1;
uint64_t h = def_clk_val & (~mask);
uint64_t cur = def_clk_val & mask;
if (v >= cur) {
return h + v;
} else {
return h + mask + 1 + v;
}
}
/* ev_s can be NULL when decoding the packet related properties. */
static int handle_fld_roles(struct actf_decoder *dec, enum actf_role roles,
const struct actf_fld_cls *cls, struct actf_fld *val)
{
struct pkt_state *pkt_s = &dec->dec_s.pkt_s;
struct event_state *ev_s = dec->dec_s.ev ? &dec->dec_s.ev->ev_s : NULL;
const struct actf_preamble *preamble = &dec->metadata->preamble;
struct error *e = &dec->err;
switch (dec->dec_s.ctx) {
case DEC_CTX_PKT_HEADER:
if (roles & ACTF_ROLE_DSTREAM_CLS_ID) {
uint64_t dsc_id = actf_fld_uint64(val);
pkt_s->dsc.id = dsc_id;
}
if (roles & ACTF_ROLE_DSTREAM_ID) {
uint64_t ds_id = actf_fld_uint64(val);
pkt_s->ds_id = ds_id;
pkt_s->opt_flags |= PKT_DSTREAM_ID;
}
if (roles & ACTF_ROLE_PKT_MAGIC_NUM) {
uint64_t magic = actf_fld_uint64(val);
if (magic != PACKET_MAGIC_NUMBER) {
/* TODO: Maybe add a flag or something to not exit out */
eprintf(e,
"packet magic number 0x%" PRIx64
" is incorrect, should be 0x%" PRIx64, magic,
PACKET_MAGIC_NUMBER);
return ACTF_MAGIC_MISMATCH;
}
}
if (roles & ACTF_ROLE_METADATA_STREAM_UUID) {
assert(val->type == ACTF_FLD_TYPE_BLOB);
if (memcmp(preamble->uuid.d, val->d.blob.ptr, ACTF_UUID_N_BYTES) != 0) {
eprintf(e, "UUID in data stream does not match UUID in metadata");
return ACTF_UUID_MISMATCH;
}
}
break;
case DEC_CTX_PKT_CTX:
if (roles & ACTF_ROLE_DEF_CLK_TSTAMP) {
pkt_s->def_clk_val = calc_new_def_clk_val(cls, val, pkt_s->def_clk_val);
pkt_s->begin_def_clk_val = pkt_s->def_clk_val;
}
if (roles & ACTF_ROLE_DISC_EVENT_CNT_SNAPSHOT) {
pkt_s->disc_er_snap = actf_fld_uint64(val);
pkt_s->opt_flags |= PKT_DISC_ER_SNAP;
}
if (roles & ACTF_ROLE_PKT_CONTENT_LEN) {
pkt_s->content_len = actf_fld_uint64(val);
}
if (roles & ACTF_ROLE_PKT_END_DEF_CLK_TSTAMP) {
pkt_s->end_def_clk_val = actf_fld_uint64(val);
pkt_s->opt_flags |= PKT_END_DEF_CLK_VAL;
}
if (roles & ACTF_ROLE_PKT_SEQ_NUM) {
pkt_s->seq_num = actf_fld_uint64(val);
pkt_s->opt_flags |= PKT_SEQ_NUM;
}
if (roles & ACTF_ROLE_PKT_TOT_LEN) {
pkt_s->tot_len = actf_fld_uint64(val);
}
break;
case DEC_CTX_EVENT_HEADER:
if (roles & ACTF_ROLE_EVENT_CLS_ID) {
ev_s->id = actf_fld_uint64(val);
}
if (roles & ACTF_ROLE_DEF_CLK_TSTAMP) {
pkt_s->def_clk_val = calc_new_def_clk_val(cls, val, pkt_s->def_clk_val);
ev_s->def_clk_val = pkt_s->def_clk_val;
}
break;
default:
break;
}
return ACTF_OK;
}
static int fld_cls_optional_decode(struct actf_decoder *dec,