658ec491a31c6e776286a537772a2abbb44ec2d8
7 { ['0'] = "0000", ['1'] = "0001", ['2'] = "0010", ['3'] = "0011"
8 , ['4'] = "0100", ['5'] = "0101", ['6'] = "0110", ['7'] = "0111"
9 , ['8'] = "1000", ['9'] = "1001", ['A'] = "1010", ['B'] = "1011"
10 , ['C'] = "1100", ['D'] = "1101", ['E'] = "1110", ['F'] = "1111"
13 struct stream
{ int pos
; char *buf
; };
15 int next(struct stream
*f
)
17 if (*f
->buf
== '\0') {
25 int r
=*(f
->buf
++) == '1' ? 1 : 0;
30 int bin2int(struct stream
*f
, int n
)
33 for (int i
= 0; i
<n
; i
++)
38 int parse_packet(struct stream
*f
)
40 int packetversion
= bin2int(f
, 3);
41 int packettype
= bin2int(f
, 3);
47 while (next(f
) == 1) {
48 num
= num
*16+bin2int(f
, 4);
50 num
= num
*16+bin2int(f
, 4);
54 int lengthtypeid
= next(f
);
57 if (lengthtypeid
== 0) {
58 int lengthsubpackets
= bin2int(f
, 15);
61 while (f
->pos
- oldpos
< lengthsubpackets
)
62 packetversion
+= parse_packet(f
);
64 int numsubpackets
= bin2int(f
, 11);
65 for (int i
= 0; i
<numsubpackets
; i
++)
66 packetversion
+= parse_packet(f
);
75 struct stream f
= {.pos
=0, .buf
=""};
76 printf("r: %d\n", parse_packet(&f
));