-
Notifications
You must be signed in to change notification settings - Fork 27
/
tlv.c
791 lines (701 loc) · 16.8 KB
/
tlv.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
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
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Copyright (c) 2017, Joyent Inc
* Author: Alex Wilson <alex.wilson@joyent.com>
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdint.h>
#include <string.h>
#include "debug.h"
#include "utils.h"
#include "tlv.h"
#include "openssh/config.h"
#include "openssh/sshbuf.h"
#if defined(__sun) || defined(__APPLE__)
#include <netinet/in.h>
#define htobe16(v) (htons(v))
#else
#include <endian.h>
#endif
struct tlv_context {
struct tlv_context *tc_next;
size_t tc_begin; /* R: beginning index in tc_buf */
size_t tc_end; /* RW: final index in tc_buf */
int tc_depth; /* RW: root = 0, tag = 1, child = 2 */
uint8_t *tc_buf; /* RW: data buffer */
size_t tc_pos; /* RW: pos in tc_buf */
boolean_t tc_freebuf; /* W: we should free tc_buf */
};
struct tlv_state {
struct tlv_context *ts_root; /* top-level ctx spanning whole buf */
struct tlv_context *ts_now; /* current tag ctx */
boolean_t ts_debug;
};
enum tlv_tag_bits {
TLV_TYPE_MASK = (1 << 7 | 1 << 6 | 1 << 5),
TLV_TAG_MASK = ~(TLV_TYPE_MASK),
TLV_TAG_CONT = 0xFF & TLV_TAG_MASK,
};
struct tlv_state *
tlv_init(const uint8_t *buf, size_t offset, size_t len)
{
struct tlv_state *ts = calloc(1, sizeof (struct tlv_state));
if (ts == NULL)
return (NULL);
struct tlv_context *tc = calloc(1, sizeof (struct tlv_context));
if (tc == NULL) {
free(ts);
return (NULL);
}
ts->ts_root = tc;
ts->ts_now = tc;
tc->tc_buf = (uint8_t *)buf;
tc->tc_pos = offset;
tc->tc_begin = offset;
tc->tc_end = offset + len;
return (ts);
}
void
tlv_enable_debug(struct tlv_state *ts)
{
ts->ts_debug = B_TRUE;
}
struct tlv_state *
tlv_init_write(void)
{
struct tlv_state *ts = calloc(1, sizeof (struct tlv_state));
if (ts == NULL)
return (NULL);
struct tlv_context *tc = calloc(1, sizeof (struct tlv_context));
if (tc == NULL) {
free(ts);
return (NULL);
}
ts->ts_root = tc;
ts->ts_now = tc;
tc->tc_buf = calloc(1, MAX_APDU_SIZE);
if (tc->tc_buf == NULL) {
free(ts);
free(tc);
return (NULL);
}
tc->tc_freebuf = B_TRUE;
tc->tc_end = MAX_APDU_SIZE;
return (ts);
}
const uint8_t TLV_CONT = (1 << 7);
static void
tlv_ctx_push(struct tlv_state *ts, struct tlv_context *tc)
{
assert(ts->ts_now != NULL);
tc->tc_next = ts->ts_now;
tc->tc_depth = ts->ts_now->tc_depth + 1;
ts->ts_now = tc;
}
static struct tlv_context *
tlv_ctx_pop(struct tlv_state *ts)
{
struct tlv_context *tc = ts->ts_now;
VERIFY(tc != NULL);
VERIFY(tc != ts->ts_root);
ts->ts_now = tc->tc_next;
return (tc);
}
void
tlv_push(struct tlv_state *ts, uint tag)
{
struct tlv_context *tc;
struct tlv_context *p = ts->ts_now;
assert(p != NULL);
tc = calloc(1, sizeof (struct tlv_context));
VERIFYN(tc);
/* Write the tag into our parent buffer now */
tlv_write_u8to32(ts, tag);
/*
* Reserve enough space in the new context for the longest possible
* length
*/
tc->tc_end = p->tc_end - p->tc_pos - 4;
tc->tc_buf = malloc(tc->tc_end);
VERIFYN(tc->tc_buf);
tc->tc_freebuf = B_TRUE;
tlv_ctx_push(ts, tc);
}
void
tlv_pop(struct tlv_state *ts)
{
struct tlv_context *tc = tlv_ctx_pop(ts);
struct tlv_context *p = ts->ts_now;
uint8_t *buf = p->tc_buf;
size_t len = tc->tc_pos;
/*
* We wrote just the tag in tlv_push(), now we know the length, so
* write that and then the actual data inside the tag.
*/
if (len < (1 << 7)) {
VERIFY3U(p->tc_pos + len + 1, <=, p->tc_end);
buf[p->tc_pos++] = len;
} else if (len < (1 << 8)) {
VERIFY3U(p->tc_pos + len + 2, <=, p->tc_end);
buf[p->tc_pos++] = 0x81;
buf[p->tc_pos++] = len;
} else if (len < (1 << 16)) {
VERIFY3U(p->tc_pos + len + 3, <=, p->tc_end);
buf[p->tc_pos++] = 0x82;
buf[p->tc_pos++] = (len & 0xFF00) >> 8;
buf[p->tc_pos++] = (len & 0x00FF);
} else if (len < (1 << 24)) {
VERIFY3U(p->tc_pos + len + 4, <=, p->tc_end);
buf[p->tc_pos++] = 0x83;
buf[p->tc_pos++] = (len & 0xFF0000) >> 16;
buf[p->tc_pos++] = (len & 0x00FF00) >> 8;
buf[p->tc_pos++] = (len & 0x0000FF);
} else {
VERIFY(0);
}
bcopy(tc->tc_buf, &buf[p->tc_pos], len);
p->tc_pos += len;
/* We're done with the child tag context now. */
VERIFY(tc->tc_freebuf);
explicit_bzero(tc->tc_buf, tc->tc_pos);
free(tc->tc_buf);
free(tc);
}
errf_t *
tlv_peek_tag(struct tlv_state *ts, uint *ptag)
{
struct tlv_context *p = ts->ts_now;
const uint8_t *buf = p->tc_buf;
uint8_t d;
uint tag;
size_t origin = p->tc_pos;
errf_t *error;
if (tlv_at_end(ts)) {
error = errf("LengthError", NULL, "tlv_read_tag called "
"past end of context");
__CPROVER_assume(error != NULL);
return (error);
}
d = buf[p->tc_pos++];
tag = d;
if ((d & TLV_TAG_MASK) == TLV_TAG_CONT) {
do {
if (tlv_at_end(ts)) {
error = errf("LengthError", NULL, "TLV tag "
"continued past end of context");
__CPROVER_assume(error != NULL);
p->tc_pos = origin;
return (error);
}
d = buf[p->tc_pos++];
tag <<= 8;
tag |= d;
} while ((d & TLV_CONT) == TLV_CONT);
}
if (tlv_at_end(ts)) {
error = errf("LengthError", NULL, "TLV tag length continued "
"past end of context");
__CPROVER_assume(error != NULL);
p->tc_pos = origin;
return (error);
}
p->tc_pos = origin;
*ptag = tag;
return (ERRF_OK);
}
errf_t *
tlv_read_tag(struct tlv_state *ts, uint *ptag)
{
struct tlv_context *p = ts->ts_now;
const uint8_t *buf = p->tc_buf;
uint8_t d;
uint tag, octs;
size_t len;
struct tlv_context *tc;
size_t origin = p->tc_pos;
errf_t *error;
tc = calloc(1, sizeof (struct tlv_context));
if (tc == NULL)
return (ERRF_NOMEM);
tc->tc_buf = p->tc_buf;
if (tlv_at_end(ts)) {
error = errf("LengthError", NULL, "tlv_read_tag called "
"past end of context");
__CPROVER_assume(error != NULL);
free(tc);
return (error);
}
d = buf[p->tc_pos++];
tag = d;
if ((d & TLV_TAG_MASK) == TLV_TAG_CONT) {
do {
if (tlv_at_end(ts)) {
error = errf("LengthError", NULL, "TLV tag "
"continued past end of context");
__CPROVER_assume(error != NULL);
free(tc);
return (error);
}
d = buf[p->tc_pos++];
tag <<= 8;
tag |= d;
} while ((d & TLV_CONT) == TLV_CONT);
}
if (tlv_at_end(ts)) {
error = errf("LengthError", NULL, "TLV tag length continued "
"past end of context");
__CPROVER_assume(error != NULL);
free(tc);
return (error);
}
d = buf[p->tc_pos++];
if ((d & TLV_CONT) == TLV_CONT) {
octs = d & (~TLV_CONT);
if (octs < 1 || octs > 4) {
error = errf("LengthError", NULL, "TLV tag had invalid "
"length indicator: %d octets", octs);
__CPROVER_assume(error != NULL);
free(tc);
return (error);
}
len = 0;
if (tlv_rem(ts) < octs) {
error = errf("LengthError", NULL, "TLV tag length "
"bytes continued past end of context");
__CPROVER_assume(error != NULL);
free(tc);
return (error);
}
for (; octs > 0; --octs) {
d = buf[p->tc_pos++];
len <<= 8;
len |= d;
}
} else {
len = d;
}
if (tlv_root_rem(ts) < len) {
error = errf("LengthError", NULL, "TLV tag length is too "
"long for buffer: %zu", len);
__CPROVER_assume(error != NULL);
free(tc);
return (error);
}
if (tlv_rem(ts) < len) {
error = errf("LengthError", NULL, "TLV tag length is too "
"long for enclosing tag: %zu", len);
__CPROVER_assume(error != NULL);
free(tc);
return (error);
}
tc->tc_begin = p->tc_pos;
tc->tc_pos = p->tc_pos;
tc->tc_end = p->tc_pos + len;
p->tc_pos += len;
tlv_ctx_push(ts, tc);
if (ts->ts_debug) {
fprintf(stderr, "%*stag at +%zu: 0x%x (%zu bytes)\n",
tc->tc_depth, "", origin, tag, len);
}
*ptag = tag;
return (ERRF_OK);
}
errf_t *
tlv_end(struct tlv_state *ts)
{
struct tlv_context *tc = tlv_ctx_pop(ts);
struct tlv_context *p = ts->ts_now;
if (ts->ts_debug) {
fprintf(stderr, "%*send tag from +%zu (%zu bytes left)\n",
tc->tc_depth, "", tc->tc_begin, tc->tc_end - tc->tc_pos);
}
VERIFY3U(p->tc_pos, >=, tc->tc_end);
if (tc->tc_pos != tc->tc_end) {
return (errf("LengthError", NULL, "tlv_end() called at +%zu "
"but tag ends at +%zu", tc->tc_pos, tc->tc_end));
}
free(tc);
return (NULL);
}
void
tlv_skip(struct tlv_state *ts)
{
struct tlv_context *tc = tlv_ctx_pop(ts);
struct tlv_context *p = ts->ts_now;
if (ts->ts_debug) {
fprintf(stderr, "%*sskip tag from +%zu (%zu bytes left)\n",
tc->tc_depth, "", tc->tc_begin, tc->tc_end - tc->tc_pos);
}
VERIFY3U(tc->tc_pos, >=, tc->tc_begin);
VERIFY3U(tc->tc_pos, <=, tc->tc_end);
VERIFY3U(p->tc_pos, >=, tc->tc_end);
free(tc);
}
void
tlv_abort(struct tlv_state *ts)
{
struct tlv_context *tc = ts->ts_now;
while (tc != ts->ts_root) {
struct tlv_context *tofree = tc;
tc = tc->tc_next;
VERIFY3U(tc->tc_pos, >=, tc->tc_begin);
VERIFY3U(tc->tc_pos, <=, tc->tc_end);
if (tofree->tc_freebuf)
free(tofree->tc_buf);
free(tofree);
}
ts->ts_now = ts->ts_root;
ts->ts_root->tc_pos = ts->ts_root->tc_end;
}
errf_t *
tlv_read_u8(struct tlv_state *ts, uint8_t *out)
{
struct tlv_context *tc = ts->ts_now;
if (tlv_at_end(ts)) {
return (errf("LengthError", NULL, "tlv_read_u8() called "
"with no bytes remaining"));
}
*out = tc->tc_buf[tc->tc_pos++];
return (NULL);
}
errf_t *
tlv_read_u16(struct tlv_state *ts, uint16_t *out)
{
struct tlv_context *tc = ts->ts_now;
if (tlv_rem(ts) < 2) {
return (errf("LengthError", NULL, "tlv_read_u16() called "
"with only %zu bytes remaining", tlv_rem(ts)));
}
*out = tc->tc_buf[tc->tc_pos++] << 8;
*out |= tc->tc_buf[tc->tc_pos++];
return (NULL);
}
errf_t *
tlv_read_u8to32(struct tlv_state *ts, uint32_t *out)
{
struct tlv_context *tc = ts->ts_now;
*out = 0;
const uint8_t *buf = tc->tc_buf;
if (tlv_rem(ts) < 1) {
return (errf("LengthError", NULL, "tlv_read_u8to32() called "
"with no bytes remaining"));
}
if (tlv_rem(ts) > 4) {
return (errf("LengthError", NULL, "tlv_read_u8to32() called "
"with %zu bytes remaining (supports max 4)", tlv_rem(ts)));
}
while (!tlv_at_end(ts)) {
*out <<= 8;
*out |= buf[tc->tc_pos++];
}
return (NULL);
}
errf_t *
tlv_read_upto(struct tlv_state *ts, uint8_t *dest, size_t maxLen,
size_t *plen)
{
struct tlv_context *tc = ts->ts_now;
size_t len = maxLen;
if (len > tlv_rem(ts))
len = tlv_rem(ts);
if (len == 0) {
return (errf("LengthError", NULL, "tlv_read() called "
"with no bytes remaining"));
}
bcopy(&tc->tc_buf[tc->tc_pos], dest, len);
tc->tc_pos += len;
*plen = len;
return (NULL);
}
errf_t *
tlv_read_string(struct tlv_state *ts, char **dest)
{
struct tlv_context *tc = ts->ts_now;
const size_t len = tlv_rem(ts);
char *buf;
size_t i;
buf = malloc(len + 1);
if (buf == NULL)
return (ERRF_NOMEM);
for (i = 0; i < len; ++i) {
if (tc->tc_buf[tc->tc_pos] == 0) {
free(buf);
return (errf("StringError", NULL, "tlv_read_string() "
"encountered a NUL character unexpectedly"));
}
buf[i] = (char)tc->tc_buf[tc->tc_pos++];
}
buf[len] = '\0';
*dest = buf;
return (NULL);
}
errf_t *
tlv_read_alloc(struct tlv_state *ts, uint8_t **pdata, size_t *plen)
{
struct tlv_context *tc = ts->ts_now;
size_t len = tlv_rem(ts);
if (len == 0) {
*pdata = NULL;
*plen = 0;
return (NULL);
}
uint8_t *data;
data = calloc(1, len);
if (data == NULL)
return (ERRF_NOMEM);
*plen = len;
bcopy(&tc->tc_buf[tc->tc_pos], data, len);
tc->tc_pos += len;
*pdata = data;
return (NULL);
}
errf_t *
tlv_read(struct tlv_state *ts, uint8_t *dest, size_t len)
{
struct tlv_context *tc = ts->ts_now;
if (tlv_rem(ts) != len) {
return (errf("LengthError", NULL, "tlv_read() called "
"for %zu bytes but %zu are left in tag", len,
tlv_rem(ts)));
}
bcopy(&tc->tc_buf[tc->tc_pos], dest, len);
tc->tc_pos += len;
return (NULL);
}
void
tlv_free(struct tlv_state *ts)
{
struct tlv_context *root;
if (ts == NULL)
return;
root = ts->ts_root;
VERIFY(root == ts->ts_now);
if (root->tc_freebuf) {
explicit_bzero(&root->tc_buf[root->tc_begin],
root->tc_end - root->tc_begin);
free(root->tc_buf);
}
free(root);
free(ts);
}
void
tlv_write(struct tlv_state *ts, const uint8_t *src, size_t len)
{
struct tlv_context *tc = ts->ts_now;
VERIFY3U(tlv_rem(ts), >=, len);
bcopy(src, &tc->tc_buf[tc->tc_pos], len);
tc->tc_pos += len;
}
void
tlv_write_u16(struct tlv_state *ts, uint16_t val)
{
struct tlv_context *tc = ts->ts_now;
VERIFY3U(tlv_rem(ts), >=, sizeof (val));
val = htobe16(val);
bcopy(&val, &tc->tc_buf[tc->tc_pos], sizeof (val));
tc->tc_pos += sizeof (val);
}
void
tlv_write_u8to32(struct tlv_state *ts, uint32_t val)
{
struct tlv_context *tc;
uint8_t *buf;
uint32_t mask = 0xFFUL << 24;
int shift = 24;
uint32_t part;
tc = ts->ts_now;
assert(tc != NULL);
buf = tc->tc_buf;
assert(buf != NULL);
if (val == 0) {
VERIFY(!tlv_at_end(ts));
buf[tc->tc_pos++] = 0;
return;
}
/* Skip any leading zero bytes. */
while (shift >= 0) {
part = (val & mask) >> shift;
if (part != 0)
break;
mask >>= 8;
shift -= 8;
}
/* And then write out the rest. */
while (shift >= 0) {
part = (val & mask) >> shift;
VERIFY(!tlv_at_end(ts));
buf[tc->tc_pos++] = part;
mask >>= 8;
shift -= 8;
}
}
void
tlv_write_byte(struct tlv_state *ts, uint8_t val)
{
struct tlv_context *tc = ts->ts_now;
VERIFY(!tlv_at_end(ts));
tc->tc_buf[tc->tc_pos++] = val;
}
boolean_t
tlv_at_root_end(const struct tlv_state *ts)
{
return (ts->ts_now->tc_pos >= ts->ts_root->tc_end);
}
boolean_t
tlv_at_end(const struct tlv_state *ts)
{
return (tlv_at_root_end(ts) || ts->ts_now->tc_pos >= ts->ts_now->tc_end);
}
size_t
tlv_root_rem(const struct tlv_state *ts)
{
return (ts->ts_root->tc_end - ts->ts_now->tc_pos);
}
size_t
tlv_rem(const struct tlv_state *ts)
{
return (ts->ts_now->tc_end - ts->ts_now->tc_pos);
}
uint8_t *
tlv_buf(const struct tlv_state *ts)
{
return (ts->ts_root->tc_buf);
}
uint8_t *
tlv_ptr(const struct tlv_state *ts)
{
const struct tlv_context *tc = ts->ts_now;
return (&tc->tc_buf[tc->tc_pos]);
}
size_t
tlv_len(const struct tlv_state *ts)
{
return (ts->ts_root->tc_pos);
}
#if defined(__CPROVER) && __CPROVER_MAIN == __FILE_tlv_c
uint8_t nondet_uchar(void);
int
main(int argc, char *argv[])
{
struct tlv_state *tlv;
errf_t *err;
uint tag;
char *str;
__CPROVER_assume(ERRF_NOMEM != NULL);
tlv = tlv_init_write();
VERIFYN(tlv);
tlv_push(tlv, 0xabcd);
tlv_push(tlv, 0xa2);
tlv_write_u8to32(tlv, 0x12345678);
tlv_pop(tlv);
tlv_push(tlv, 0xa1);
tlv_write_byte(tlv, 0x00);
tlv_pop(tlv);
tlv_pop(tlv);
tlv_free(tlv);
const uint8_t buf[] = { 0x01, 0x07, 0xa1, 0x01, 0x00, 0xa2, 0x02, 0xab,
0xcd };
tlv = tlv_init(buf, 0, sizeof (buf));
VERIFYN(tlv);
if ((err = tlv_read_tag(tlv, &tag)) != NULL) {
errfx(1, err, "reading tag");
return (1);
}
assert(tag == 0x01);
if ((err = tlv_read_tag(tlv, &tag)) != NULL) {
errfx(1, err, "reading tag");
return (1);
}
assert(tag == 0xa1);
tlv_skip(tlv);
if ((err = tlv_read_tag(tlv, &tag)) != NULL) {
errfx(1, err, "reading tag");
return (1);
}
assert(tag == 0xa2);
tlv_skip(tlv);
tlv_end(tlv);
tlv_free(tlv);
const uint8_t buf2[] = { 0x01, 0x08, 0xa1, 0xff, 0x00, 0xa2, 0x02, 0xab,
0xcd };
tlv = tlv_init(buf2, 0, sizeof (buf2));
VERIFYN(tlv);
err = tlv_read_tag(tlv, &tag);
assert(err != NULL);
tlv_abort(tlv);
tlv_free(tlv);
const uint8_t buf3[] = { 0x01, 0x05, 'h', 'e', 'l', 'l', 'o' };
tlv = tlv_init(buf3, 0, sizeof (buf3));
VERIFYN(tlv);
if ((err = tlv_read_tag(tlv, &tag)) != NULL) {
errfx(1, err, "reading tag");
return (1);
}
assert(tag == 0x01);
if ((err = tlv_read_string(tlv, &str)) != NULL) {
errfx(1, err, "reading string");
return (1);
}
assert(strlen(str) == 5);
assert(strcmp(str, "hello") == 0);
tlv_abort(tlv);
tlv_free(tlv);
const uint8_t buf4[] = { nondet_uchar(), nondet_uchar(), nondet_uchar(),
nondet_uchar() };
tlv = tlv_init(buf4, 0, sizeof (buf4));
VERIFYN(tlv);
err = tlv_read_tag(tlv, &tag);
if (err == ERRF_OK) {
assert(tlv_rem(tlv) <= 2);
assert(tlv_ptr(tlv) >= &buf4[2]);
assert(tlv_ptr(tlv) <= &buf4[3] ||
(tlv_ptr(tlv) == &buf4[4] && tlv_rem(tlv) == 0));
err = tlv_read_tag(tlv, &tag);
if (err == ERRF_OK) {
/*
* only 4 bytes of data, it could be 2 zero-length tags
* but no more
*/
assert(tlv_rem(tlv) == 0);
tlv_end(tlv);
} else {
errf_free(err);
}
} else {
errf_free(err);
}
tlv_abort(tlv);
tlv_free(tlv);
const uint8_t buf5[] = { nondet_uchar(), nondet_uchar(), nondet_uchar(),
nondet_uchar(), nondet_uchar(), nondet_uchar(), nondet_uchar() };
tlv = tlv_init(buf5, 0, sizeof (buf5));
VERIFYN(tlv);
err = tlv_read_tag(tlv, &tag);
if (err == ERRF_OK) {
err = tlv_read_tag(tlv, &tag);
if (err == ERRF_OK) {
err = tlv_read_tag(tlv, &tag);
if (err == ERRF_OK) {
assert(tlv_rem(tlv) <= 1);
tlv_skip(tlv);
}
assert(tlv_rem(tlv) <= 3);
tlv_skip(tlv);
} else {
errf_free(err);
}
assert(tlv_rem(tlv) <= 5);
tlv_skip(tlv);
} else {
errf_free(err);
}
tlv_abort(tlv);
tlv_free(tlv);
return (0);
}
#endif