BadVPN – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /*
2 DO NOT EDIT THIS FILE!
3 This file was automatically generated by the bproto generator.
4 */
5  
6 #include <stdint.h>
7 #include <string.h>
8  
9 #include <misc/debug.h>
10 #include <misc/byteorder.h>
11 #include <bproto/BProto.h>
12  
13  
14 #define msg1_SIZEa (sizeof(struct BProto_header_s) + sizeof(struct BProto_uint16_s))
15 #define msg1_SIZEb (sizeof(struct BProto_header_s) + sizeof(struct BProto_uint32_s))
16 #define msg1_SIZEc (sizeof(struct BProto_header_s) + sizeof(struct BProto_uint64_s))
17 #define msg1_SIZEd (sizeof(struct BProto_header_s) + sizeof(struct BProto_uint16_s))
18 #define msg1_SIZEe (sizeof(struct BProto_header_s) + sizeof(struct BProto_uint8_s))
19 #define msg1_SIZEf(_len) (sizeof(struct BProto_header_s) + sizeof(struct BProto_data_header_s) + (_len))
20 #define msg1_SIZEg (sizeof(struct BProto_header_s) + sizeof(struct BProto_data_header_s) + (4))
21  
22 typedef struct {
23 uint8_t *out;
24 int used;
25 int a_count;
26 int b_count;
27 int c_count;
28 int d_count;
29 int e_count;
30 int f_count;
31 int g_count;
32 } msg1Writer;
33  
34 static void msg1Writer_Init (msg1Writer *o, uint8_t *out);
35 static int msg1Writer_Finish (msg1Writer *o);
36 static void msg1Writer_Adda (msg1Writer *o, uint16_t v);
37 static void msg1Writer_Addb (msg1Writer *o, uint32_t v);
38 static void msg1Writer_Addc (msg1Writer *o, uint64_t v);
39 static void msg1Writer_Addd (msg1Writer *o, uint16_t v);
40 static void msg1Writer_Adde (msg1Writer *o, uint8_t v);
41 static uint8_t * msg1Writer_Addf (msg1Writer *o, int len);
42 static uint8_t * msg1Writer_Addg (msg1Writer *o);
43  
44 typedef struct {
45 uint8_t *buf;
46 int buf_len;
47 int a_start;
48 int a_span;
49 int a_pos;
50 int b_start;
51 int b_span;
52 int b_pos;
53 int c_start;
54 int c_span;
55 int c_pos;
56 int d_start;
57 int d_span;
58 int d_pos;
59 int e_start;
60 int e_span;
61 int e_pos;
62 int f_start;
63 int f_span;
64 int f_pos;
65 int g_start;
66 int g_span;
67 int g_pos;
68 } msg1Parser;
69  
70 static int msg1Parser_Init (msg1Parser *o, uint8_t *buf, int buf_len);
71 static int msg1Parser_GotEverything (msg1Parser *o);
72 static int msg1Parser_Geta (msg1Parser *o, uint16_t *v);
73 static void msg1Parser_Reseta (msg1Parser *o);
74 static void msg1Parser_Forwarda (msg1Parser *o);
75 static int msg1Parser_Getb (msg1Parser *o, uint32_t *v);
76 static void msg1Parser_Resetb (msg1Parser *o);
77 static void msg1Parser_Forwardb (msg1Parser *o);
78 static int msg1Parser_Getc (msg1Parser *o, uint64_t *v);
79 static void msg1Parser_Resetc (msg1Parser *o);
80 static void msg1Parser_Forwardc (msg1Parser *o);
81 static int msg1Parser_Getd (msg1Parser *o, uint16_t *v);
82 static void msg1Parser_Resetd (msg1Parser *o);
83 static void msg1Parser_Forwardd (msg1Parser *o);
84 static int msg1Parser_Gete (msg1Parser *o, uint8_t *v);
85 static void msg1Parser_Resete (msg1Parser *o);
86 static void msg1Parser_Forwarde (msg1Parser *o);
87 static int msg1Parser_Getf (msg1Parser *o, uint8_t **data, int *data_len);
88 static void msg1Parser_Resetf (msg1Parser *o);
89 static void msg1Parser_Forwardf (msg1Parser *o);
90 static int msg1Parser_Getg (msg1Parser *o, uint8_t **data);
91 static void msg1Parser_Resetg (msg1Parser *o);
92 static void msg1Parser_Forwardg (msg1Parser *o);
93  
94 void msg1Writer_Init (msg1Writer *o, uint8_t *out)
95 {
96 o->out = out;
97 o->used = 0;
98 o->a_count = 0;
99 o->b_count = 0;
100 o->c_count = 0;
101 o->d_count = 0;
102 o->e_count = 0;
103 o->f_count = 0;
104 o->g_count = 0;
105 }
106  
107 int msg1Writer_Finish (msg1Writer *o)
108 {
109 ASSERT(o->used >= 0)
110 ASSERT(o->a_count == 1)
111 ASSERT(o->b_count >= 0 && o->b_count <= 1)
112 ASSERT(o->c_count >= 1)
113 ASSERT(o->d_count >= 0)
114 ASSERT(o->e_count == 1)
115 ASSERT(o->f_count == 1)
116 ASSERT(o->g_count == 1)
117  
118 return o->used;
119 }
120  
121 void msg1Writer_Adda (msg1Writer *o, uint16_t v)
122 {
123 ASSERT(o->used >= 0)
124 ASSERT(o->a_count == 0)
125  
126  
127 struct BProto_header_s header;
128 header.id = htol16(5);
129 header.type = htol16(BPROTO_TYPE_UINT16);
130 memcpy(o->out + o->used, &header, sizeof(header));
131 o->used += sizeof(struct BProto_header_s);
132  
133 struct BProto_uint16_s data;
134 data.v = htol16(v);
135 memcpy(o->out + o->used, &data, sizeof(data));
136 o->used += sizeof(struct BProto_uint16_s);
137  
138 o->a_count++;
139 }
140  
141 void msg1Writer_Addb (msg1Writer *o, uint32_t v)
142 {
143 ASSERT(o->used >= 0)
144 ASSERT(o->b_count == 0)
145  
146  
147 struct BProto_header_s header;
148 header.id = htol16(6);
149 header.type = htol16(BPROTO_TYPE_UINT32);
150 memcpy(o->out + o->used, &header, sizeof(header));
151 o->used += sizeof(struct BProto_header_s);
152  
153 struct BProto_uint32_s data;
154 data.v = htol32(v);
155 memcpy(o->out + o->used, &data, sizeof(data));
156 o->used += sizeof(struct BProto_uint32_s);
157  
158 o->b_count++;
159 }
160  
161 void msg1Writer_Addc (msg1Writer *o, uint64_t v)
162 {
163 ASSERT(o->used >= 0)
164  
165  
166  
167 struct BProto_header_s header;
168 header.id = htol16(7);
169 header.type = htol16(BPROTO_TYPE_UINT64);
170 memcpy(o->out + o->used, &header, sizeof(header));
171 o->used += sizeof(struct BProto_header_s);
172  
173 struct BProto_uint64_s data;
174 data.v = htol64(v);
175 memcpy(o->out + o->used, &data, sizeof(data));
176 o->used += sizeof(struct BProto_uint64_s);
177  
178 o->c_count++;
179 }
180  
181 void msg1Writer_Addd (msg1Writer *o, uint16_t v)
182 {
183 ASSERT(o->used >= 0)
184  
185  
186  
187 struct BProto_header_s header;
188 header.id = htol16(8);
189 header.type = htol16(BPROTO_TYPE_UINT16);
190 memcpy(o->out + o->used, &header, sizeof(header));
191 o->used += sizeof(struct BProto_header_s);
192  
193 struct BProto_uint16_s data;
194 data.v = htol16(v);
195 memcpy(o->out + o->used, &data, sizeof(data));
196 o->used += sizeof(struct BProto_uint16_s);
197  
198 o->d_count++;
199 }
200  
201 void msg1Writer_Adde (msg1Writer *o, uint8_t v)
202 {
203 ASSERT(o->used >= 0)
204 ASSERT(o->e_count == 0)
205  
206  
207 struct BProto_header_s header;
208 header.id = htol16(9);
209 header.type = htol16(BPROTO_TYPE_UINT8);
210 memcpy(o->out + o->used, &header, sizeof(header));
211 o->used += sizeof(struct BProto_header_s);
212  
213 struct BProto_uint8_s data;
214 data.v = htol8(v);
215 memcpy(o->out + o->used, &data, sizeof(data));
216 o->used += sizeof(struct BProto_uint8_s);
217  
218 o->e_count++;
219 }
220  
221 uint8_t * msg1Writer_Addf (msg1Writer *o, int len)
222 {
223 ASSERT(o->used >= 0)
224 ASSERT(o->f_count == 0)
225 ASSERT(len >= 0 && len <= UINT32_MAX)
226  
227 struct BProto_header_s header;
228 header.id = htol16(10);
229 header.type = htol16(BPROTO_TYPE_DATA);
230 memcpy(o->out + o->used, &header, sizeof(header));
231 o->used += sizeof(struct BProto_header_s);
232  
233 struct BProto_data_header_s data;
234 data.len = htol32(len);
235 memcpy(o->out + o->used, &data, sizeof(data));
236 o->used += sizeof(struct BProto_data_header_s);
237  
238 uint8_t *dest = (o->out + o->used);
239 o->used += len;
240  
241 o->f_count++;
242  
243 return dest;
244 }
245  
246 uint8_t * msg1Writer_Addg (msg1Writer *o)
247 {
248 ASSERT(o->used >= 0)
249 ASSERT(o->g_count == 0)
250  
251  
252 struct BProto_header_s header;
253 header.id = htol16(11);
254 header.type = htol16(BPROTO_TYPE_CONSTDATA);
255 memcpy(o->out + o->used, &header, sizeof(header));
256 o->used += sizeof(struct BProto_header_s);
257  
258 struct BProto_data_header_s data;
259 data.len = htol32(4);
260 memcpy(o->out + o->used, &data, sizeof(data));
261 o->used += sizeof(struct BProto_data_header_s);
262  
263 uint8_t *dest = (o->out + o->used);
264 o->used += (4);
265  
266 o->g_count++;
267  
268 return dest;
269 }
270  
271 int msg1Parser_Init (msg1Parser *o, uint8_t *buf, int buf_len)
272 {
273 ASSERT(buf_len >= 0)
274  
275 o->buf = buf;
276 o->buf_len = buf_len;
277 o->a_start = o->buf_len;
278 o->a_span = 0;
279 o->a_pos = 0;
280 o->b_start = o->buf_len;
281 o->b_span = 0;
282 o->b_pos = 0;
283 o->c_start = o->buf_len;
284 o->c_span = 0;
285 o->c_pos = 0;
286 o->d_start = o->buf_len;
287 o->d_span = 0;
288 o->d_pos = 0;
289 o->e_start = o->buf_len;
290 o->e_span = 0;
291 o->e_pos = 0;
292 o->f_start = o->buf_len;
293 o->f_span = 0;
294 o->f_pos = 0;
295 o->g_start = o->buf_len;
296 o->g_span = 0;
297 o->g_pos = 0;
298  
299 int a_count = 0;
300 int b_count = 0;
301 int c_count = 0;
302 int d_count = 0;
303 int e_count = 0;
304 int f_count = 0;
305 int g_count = 0;
306  
307 int pos = 0;
308 int left = o->buf_len;
309  
310 while (left > 0) {
311 int entry_pos = pos;
312  
313 if (!(left >= sizeof(struct BProto_header_s))) {
314 return 0;
315 }
316 struct BProto_header_s header;
317 memcpy(&header, o->buf + pos, sizeof(header));
318 pos += sizeof(struct BProto_header_s);
319 left -= sizeof(struct BProto_header_s);
320 uint16_t type = ltoh16(header.type);
321 uint16_t id = ltoh16(header.id);
322  
323 switch (type) {
324 case BPROTO_TYPE_UINT8: {
325 if (!(left >= sizeof(struct BProto_uint8_s))) {
326 return 0;
327 }
328 pos += sizeof(struct BProto_uint8_s);
329 left -= sizeof(struct BProto_uint8_s);
330  
331 switch (id) {
332 case 9:
333 if (o->e_start == o->buf_len) {
334 o->e_start = entry_pos;
335 }
336 o->e_span = pos - o->e_start;
337 e_count++;
338 break;
339 default:
340 return 0;
341 }
342 } break;
343 case BPROTO_TYPE_UINT16: {
344 if (!(left >= sizeof(struct BProto_uint16_s))) {
345 return 0;
346 }
347 pos += sizeof(struct BProto_uint16_s);
348 left -= sizeof(struct BProto_uint16_s);
349  
350 switch (id) {
351 case 5:
352 if (o->a_start == o->buf_len) {
353 o->a_start = entry_pos;
354 }
355 o->a_span = pos - o->a_start;
356 a_count++;
357 break;
358 case 8:
359 if (o->d_start == o->buf_len) {
360 o->d_start = entry_pos;
361 }
362 o->d_span = pos - o->d_start;
363 d_count++;
364 break;
365 default:
366 return 0;
367 }
368 } break;
369 case BPROTO_TYPE_UINT32: {
370 if (!(left >= sizeof(struct BProto_uint32_s))) {
371 return 0;
372 }
373 pos += sizeof(struct BProto_uint32_s);
374 left -= sizeof(struct BProto_uint32_s);
375  
376 switch (id) {
377 case 6:
378 if (o->b_start == o->buf_len) {
379 o->b_start = entry_pos;
380 }
381 o->b_span = pos - o->b_start;
382 b_count++;
383 break;
384 default:
385 return 0;
386 }
387 } break;
388 case BPROTO_TYPE_UINT64: {
389 if (!(left >= sizeof(struct BProto_uint64_s))) {
390 return 0;
391 }
392 pos += sizeof(struct BProto_uint64_s);
393 left -= sizeof(struct BProto_uint64_s);
394  
395 switch (id) {
396 case 7:
397 if (o->c_start == o->buf_len) {
398 o->c_start = entry_pos;
399 }
400 o->c_span = pos - o->c_start;
401 c_count++;
402 break;
403 default:
404 return 0;
405 }
406 } break;
407 case BPROTO_TYPE_DATA:
408 case BPROTO_TYPE_CONSTDATA:
409 {
410 if (!(left >= sizeof(struct BProto_data_header_s))) {
411 return 0;
412 }
413 struct BProto_data_header_s val;
414 memcpy(&val, o->buf + pos, sizeof(val));
415 pos += sizeof(struct BProto_data_header_s);
416 left -= sizeof(struct BProto_data_header_s);
417  
418 uint32_t payload_len = ltoh32(val.len);
419 if (!(left >= payload_len)) {
420 return 0;
421 }
422 pos += payload_len;
423 left -= payload_len;
424  
425 switch (id) {
426 case 10:
427 if (!(type == BPROTO_TYPE_DATA)) {
428 return 0;
429 }
430 if (o->f_start == o->buf_len) {
431 o->f_start = entry_pos;
432 }
433 o->f_span = pos - o->f_start;
434 f_count++;
435 break;
436 case 11:
437 if (!(type == BPROTO_TYPE_CONSTDATA)) {
438 return 0;
439 }
440 if (!(payload_len == (4))) {
441 return 0;
442 }
443 if (o->g_start == o->buf_len) {
444 o->g_start = entry_pos;
445 }
446 o->g_span = pos - o->g_start;
447 g_count++;
448 break;
449 default:
450 return 0;
451 }
452 } break;
453 default:
454 return 0;
455 }
456 }
457  
458 if (!(a_count == 1)) {
459 return 0;
460 }
461 if (!(b_count <= 1)) {
462 return 0;
463 }
464 if (!(c_count >= 1)) {
465 return 0;
466 }
467 if (!(e_count == 1)) {
468 return 0;
469 }
470 if (!(f_count == 1)) {
471 return 0;
472 }
473 if (!(g_count == 1)) {
474 return 0;
475 }
476  
477 return 1;
478 }
479  
480 int msg1Parser_GotEverything (msg1Parser *o)
481 {
482 return (
483 o->a_pos == o->a_span
484 &&
485 o->b_pos == o->b_span
486 &&
487 o->c_pos == o->c_span
488 &&
489 o->d_pos == o->d_span
490 &&
491 o->e_pos == o->e_span
492 &&
493 o->f_pos == o->f_span
494 &&
495 o->g_pos == o->g_span
496 );
497 }
498  
499 int msg1Parser_Geta (msg1Parser *o, uint16_t *v)
500 {
501 ASSERT(o->a_pos >= 0)
502 ASSERT(o->a_pos <= o->a_span)
503  
504 int left = o->a_span - o->a_pos;
505  
506 while (left > 0) {
507 ASSERT(left >= sizeof(struct BProto_header_s))
508 struct BProto_header_s header;
509 memcpy(&header, o->buf + o->a_start + o->a_pos, sizeof(header));
510 o->a_pos += sizeof(struct BProto_header_s);
511 left -= sizeof(struct BProto_header_s);
512 uint16_t type = ltoh16(header.type);
513 uint16_t id = ltoh16(header.id);
514  
515 switch (type) {
516 case BPROTO_TYPE_UINT8: {
517 ASSERT(left >= sizeof(struct BProto_uint8_s))
518 o->a_pos += sizeof(struct BProto_uint8_s);
519 left -= sizeof(struct BProto_uint8_s);
520 } break;
521 case BPROTO_TYPE_UINT16: {
522 ASSERT(left >= sizeof(struct BProto_uint16_s))
523 struct BProto_uint16_s val;
524 memcpy(&val, o->buf + o->a_start + o->a_pos, sizeof(val));
525 o->a_pos += sizeof(struct BProto_uint16_s);
526 left -= sizeof(struct BProto_uint16_s);
527  
528 if (id == 5) {
529 *v = ltoh16(val.v);
530 return 1;
531 }
532 } break;
533 case BPROTO_TYPE_UINT32: {
534 ASSERT(left >= sizeof(struct BProto_uint32_s))
535 o->a_pos += sizeof(struct BProto_uint32_s);
536 left -= sizeof(struct BProto_uint32_s);
537 } break;
538 case BPROTO_TYPE_UINT64: {
539 ASSERT(left >= sizeof(struct BProto_uint64_s))
540 o->a_pos += sizeof(struct BProto_uint64_s);
541 left -= sizeof(struct BProto_uint64_s);
542 } break;
543 case BPROTO_TYPE_DATA:
544 case BPROTO_TYPE_CONSTDATA:
545 {
546 ASSERT(left >= sizeof(struct BProto_data_header_s))
547 struct BProto_data_header_s val;
548 memcpy(&val, o->buf + o->a_start + o->a_pos, sizeof(val));
549 o->a_pos += sizeof(struct BProto_data_header_s);
550 left -= sizeof(struct BProto_data_header_s);
551  
552 uint32_t payload_len = ltoh32(val.len);
553 ASSERT(left >= payload_len)
554 o->a_pos += payload_len;
555 left -= payload_len;
556 } break;
557 default:
558 ASSERT(0);
559 }
560 }
561  
562 return 0;
563 }
564  
565 void msg1Parser_Reseta (msg1Parser *o)
566 {
567 o->a_pos = 0;
568 }
569  
570 void msg1Parser_Forwarda (msg1Parser *o)
571 {
572 o->a_pos = o->a_span;
573 }
574  
575 int msg1Parser_Getb (msg1Parser *o, uint32_t *v)
576 {
577 ASSERT(o->b_pos >= 0)
578 ASSERT(o->b_pos <= o->b_span)
579  
580 int left = o->b_span - o->b_pos;
581  
582 while (left > 0) {
583 ASSERT(left >= sizeof(struct BProto_header_s))
584 struct BProto_header_s header;
585 memcpy(&header, o->buf + o->b_start + o->b_pos, sizeof(header));
586 o->b_pos += sizeof(struct BProto_header_s);
587 left -= sizeof(struct BProto_header_s);
588 uint16_t type = ltoh16(header.type);
589 uint16_t id = ltoh16(header.id);
590  
591 switch (type) {
592 case BPROTO_TYPE_UINT8: {
593 ASSERT(left >= sizeof(struct BProto_uint8_s))
594 o->b_pos += sizeof(struct BProto_uint8_s);
595 left -= sizeof(struct BProto_uint8_s);
596 } break;
597 case BPROTO_TYPE_UINT16: {
598 ASSERT(left >= sizeof(struct BProto_uint16_s))
599 o->b_pos += sizeof(struct BProto_uint16_s);
600 left -= sizeof(struct BProto_uint16_s);
601 } break;
602 case BPROTO_TYPE_UINT32: {
603 ASSERT(left >= sizeof(struct BProto_uint32_s))
604 struct BProto_uint32_s val;
605 memcpy(&val, o->buf + o->b_start + o->b_pos, sizeof(val));
606 o->b_pos += sizeof(struct BProto_uint32_s);
607 left -= sizeof(struct BProto_uint32_s);
608  
609 if (id == 6) {
610 *v = ltoh32(val.v);
611 return 1;
612 }
613 } break;
614 case BPROTO_TYPE_UINT64: {
615 ASSERT(left >= sizeof(struct BProto_uint64_s))
616 o->b_pos += sizeof(struct BProto_uint64_s);
617 left -= sizeof(struct BProto_uint64_s);
618 } break;
619 case BPROTO_TYPE_DATA:
620 case BPROTO_TYPE_CONSTDATA:
621 {
622 ASSERT(left >= sizeof(struct BProto_data_header_s))
623 struct BProto_data_header_s val;
624 memcpy(&val, o->buf + o->b_start + o->b_pos, sizeof(val));
625 o->b_pos += sizeof(struct BProto_data_header_s);
626 left -= sizeof(struct BProto_data_header_s);
627  
628 uint32_t payload_len = ltoh32(val.len);
629 ASSERT(left >= payload_len)
630 o->b_pos += payload_len;
631 left -= payload_len;
632 } break;
633 default:
634 ASSERT(0);
635 }
636 }
637  
638 return 0;
639 }
640  
641 void msg1Parser_Resetb (msg1Parser *o)
642 {
643 o->b_pos = 0;
644 }
645  
646 void msg1Parser_Forwardb (msg1Parser *o)
647 {
648 o->b_pos = o->b_span;
649 }
650  
651 int msg1Parser_Getc (msg1Parser *o, uint64_t *v)
652 {
653 ASSERT(o->c_pos >= 0)
654 ASSERT(o->c_pos <= o->c_span)
655  
656 int left = o->c_span - o->c_pos;
657  
658 while (left > 0) {
659 ASSERT(left >= sizeof(struct BProto_header_s))
660 struct BProto_header_s header;
661 memcpy(&header, o->buf + o->c_start + o->c_pos, sizeof(header));
662 o->c_pos += sizeof(struct BProto_header_s);
663 left -= sizeof(struct BProto_header_s);
664 uint16_t type = ltoh16(header.type);
665 uint16_t id = ltoh16(header.id);
666  
667 switch (type) {
668 case BPROTO_TYPE_UINT8: {
669 ASSERT(left >= sizeof(struct BProto_uint8_s))
670 o->c_pos += sizeof(struct BProto_uint8_s);
671 left -= sizeof(struct BProto_uint8_s);
672 } break;
673 case BPROTO_TYPE_UINT16: {
674 ASSERT(left >= sizeof(struct BProto_uint16_s))
675 o->c_pos += sizeof(struct BProto_uint16_s);
676 left -= sizeof(struct BProto_uint16_s);
677 } break;
678 case BPROTO_TYPE_UINT32: {
679 ASSERT(left >= sizeof(struct BProto_uint32_s))
680 o->c_pos += sizeof(struct BProto_uint32_s);
681 left -= sizeof(struct BProto_uint32_s);
682 } break;
683 case BPROTO_TYPE_UINT64: {
684 ASSERT(left >= sizeof(struct BProto_uint64_s))
685 struct BProto_uint64_s val;
686 memcpy(&val, o->buf + o->c_start + o->c_pos, sizeof(val));
687 o->c_pos += sizeof(struct BProto_uint64_s);
688 left -= sizeof(struct BProto_uint64_s);
689  
690 if (id == 7) {
691 *v = ltoh64(val.v);
692 return 1;
693 }
694 } break;
695 case BPROTO_TYPE_DATA:
696 case BPROTO_TYPE_CONSTDATA:
697 {
698 ASSERT(left >= sizeof(struct BProto_data_header_s))
699 struct BProto_data_header_s val;
700 memcpy(&val, o->buf + o->c_start + o->c_pos, sizeof(val));
701 o->c_pos += sizeof(struct BProto_data_header_s);
702 left -= sizeof(struct BProto_data_header_s);
703  
704 uint32_t payload_len = ltoh32(val.len);
705 ASSERT(left >= payload_len)
706 o->c_pos += payload_len;
707 left -= payload_len;
708 } break;
709 default:
710 ASSERT(0);
711 }
712 }
713  
714 return 0;
715 }
716  
717 void msg1Parser_Resetc (msg1Parser *o)
718 {
719 o->c_pos = 0;
720 }
721  
722 void msg1Parser_Forwardc (msg1Parser *o)
723 {
724 o->c_pos = o->c_span;
725 }
726  
727 int msg1Parser_Getd (msg1Parser *o, uint16_t *v)
728 {
729 ASSERT(o->d_pos >= 0)
730 ASSERT(o->d_pos <= o->d_span)
731  
732 int left = o->d_span - o->d_pos;
733  
734 while (left > 0) {
735 ASSERT(left >= sizeof(struct BProto_header_s))
736 struct BProto_header_s header;
737 memcpy(&header, o->buf + o->d_start + o->d_pos, sizeof(header));
738 o->d_pos += sizeof(struct BProto_header_s);
739 left -= sizeof(struct BProto_header_s);
740 uint16_t type = ltoh16(header.type);
741 uint16_t id = ltoh16(header.id);
742  
743 switch (type) {
744 case BPROTO_TYPE_UINT8: {
745 ASSERT(left >= sizeof(struct BProto_uint8_s))
746 o->d_pos += sizeof(struct BProto_uint8_s);
747 left -= sizeof(struct BProto_uint8_s);
748 } break;
749 case BPROTO_TYPE_UINT16: {
750 ASSERT(left >= sizeof(struct BProto_uint16_s))
751 struct BProto_uint16_s val;
752 memcpy(&val, o->buf + o->d_start + o->d_pos, sizeof(val));
753 o->d_pos += sizeof(struct BProto_uint16_s);
754 left -= sizeof(struct BProto_uint16_s);
755  
756 if (id == 8) {
757 *v = ltoh16(val.v);
758 return 1;
759 }
760 } break;
761 case BPROTO_TYPE_UINT32: {
762 ASSERT(left >= sizeof(struct BProto_uint32_s))
763 o->d_pos += sizeof(struct BProto_uint32_s);
764 left -= sizeof(struct BProto_uint32_s);
765 } break;
766 case BPROTO_TYPE_UINT64: {
767 ASSERT(left >= sizeof(struct BProto_uint64_s))
768 o->d_pos += sizeof(struct BProto_uint64_s);
769 left -= sizeof(struct BProto_uint64_s);
770 } break;
771 case BPROTO_TYPE_DATA:
772 case BPROTO_TYPE_CONSTDATA:
773 {
774 ASSERT(left >= sizeof(struct BProto_data_header_s))
775 struct BProto_data_header_s val;
776 memcpy(&val, o->buf + o->d_start + o->d_pos, sizeof(val));
777 o->d_pos += sizeof(struct BProto_data_header_s);
778 left -= sizeof(struct BProto_data_header_s);
779  
780 uint32_t payload_len = ltoh32(val.len);
781 ASSERT(left >= payload_len)
782 o->d_pos += payload_len;
783 left -= payload_len;
784 } break;
785 default:
786 ASSERT(0);
787 }
788 }
789  
790 return 0;
791 }
792  
793 void msg1Parser_Resetd (msg1Parser *o)
794 {
795 o->d_pos = 0;
796 }
797  
798 void msg1Parser_Forwardd (msg1Parser *o)
799 {
800 o->d_pos = o->d_span;
801 }
802  
803 int msg1Parser_Gete (msg1Parser *o, uint8_t *v)
804 {
805 ASSERT(o->e_pos >= 0)
806 ASSERT(o->e_pos <= o->e_span)
807  
808 int left = o->e_span - o->e_pos;
809  
810 while (left > 0) {
811 ASSERT(left >= sizeof(struct BProto_header_s))
812 struct BProto_header_s header;
813 memcpy(&header, o->buf + o->e_start + o->e_pos, sizeof(header));
814 o->e_pos += sizeof(struct BProto_header_s);
815 left -= sizeof(struct BProto_header_s);
816 uint16_t type = ltoh16(header.type);
817 uint16_t id = ltoh16(header.id);
818  
819 switch (type) {
820 case BPROTO_TYPE_UINT8: {
821 ASSERT(left >= sizeof(struct BProto_uint8_s))
822 struct BProto_uint8_s val;
823 memcpy(&val, o->buf + o->e_start + o->e_pos, sizeof(val));
824 o->e_pos += sizeof(struct BProto_uint8_s);
825 left -= sizeof(struct BProto_uint8_s);
826  
827 if (id == 9) {
828 *v = ltoh8(val.v);
829 return 1;
830 }
831 } break;
832 case BPROTO_TYPE_UINT16: {
833 ASSERT(left >= sizeof(struct BProto_uint16_s))
834 o->e_pos += sizeof(struct BProto_uint16_s);
835 left -= sizeof(struct BProto_uint16_s);
836 } break;
837 case BPROTO_TYPE_UINT32: {
838 ASSERT(left >= sizeof(struct BProto_uint32_s))
839 o->e_pos += sizeof(struct BProto_uint32_s);
840 left -= sizeof(struct BProto_uint32_s);
841 } break;
842 case BPROTO_TYPE_UINT64: {
843 ASSERT(left >= sizeof(struct BProto_uint64_s))
844 o->e_pos += sizeof(struct BProto_uint64_s);
845 left -= sizeof(struct BProto_uint64_s);
846 } break;
847 case BPROTO_TYPE_DATA:
848 case BPROTO_TYPE_CONSTDATA:
849 {
850 ASSERT(left >= sizeof(struct BProto_data_header_s))
851 struct BProto_data_header_s val;
852 memcpy(&val, o->buf + o->e_start + o->e_pos, sizeof(val));
853 o->e_pos += sizeof(struct BProto_data_header_s);
854 left -= sizeof(struct BProto_data_header_s);
855  
856 uint32_t payload_len = ltoh32(val.len);
857 ASSERT(left >= payload_len)
858 o->e_pos += payload_len;
859 left -= payload_len;
860 } break;
861 default:
862 ASSERT(0);
863 }
864 }
865  
866 return 0;
867 }
868  
869 void msg1Parser_Resete (msg1Parser *o)
870 {
871 o->e_pos = 0;
872 }
873  
874 void msg1Parser_Forwarde (msg1Parser *o)
875 {
876 o->e_pos = o->e_span;
877 }
878  
879 int msg1Parser_Getf (msg1Parser *o, uint8_t **data, int *data_len)
880 {
881 ASSERT(o->f_pos >= 0)
882 ASSERT(o->f_pos <= o->f_span)
883  
884 int left = o->f_span - o->f_pos;
885  
886 while (left > 0) {
887 ASSERT(left >= sizeof(struct BProto_header_s))
888 struct BProto_header_s header;
889 memcpy(&header, o->buf + o->f_start + o->f_pos, sizeof(header));
890 o->f_pos += sizeof(struct BProto_header_s);
891 left -= sizeof(struct BProto_header_s);
892 uint16_t type = ltoh16(header.type);
893 uint16_t id = ltoh16(header.id);
894  
895 switch (type) {
896 case BPROTO_TYPE_UINT8: {
897 ASSERT(left >= sizeof(struct BProto_uint8_s))
898 o->f_pos += sizeof(struct BProto_uint8_s);
899 left -= sizeof(struct BProto_uint8_s);
900 } break;
901 case BPROTO_TYPE_UINT16: {
902 ASSERT(left >= sizeof(struct BProto_uint16_s))
903 o->f_pos += sizeof(struct BProto_uint16_s);
904 left -= sizeof(struct BProto_uint16_s);
905 } break;
906 case BPROTO_TYPE_UINT32: {
907 ASSERT(left >= sizeof(struct BProto_uint32_s))
908 o->f_pos += sizeof(struct BProto_uint32_s);
909 left -= sizeof(struct BProto_uint32_s);
910 } break;
911 case BPROTO_TYPE_UINT64: {
912 ASSERT(left >= sizeof(struct BProto_uint64_s))
913 o->f_pos += sizeof(struct BProto_uint64_s);
914 left -= sizeof(struct BProto_uint64_s);
915 } break;
916 case BPROTO_TYPE_DATA:
917 case BPROTO_TYPE_CONSTDATA:
918 {
919 ASSERT(left >= sizeof(struct BProto_data_header_s))
920 struct BProto_data_header_s val;
921 memcpy(&val, o->buf + o->f_start + o->f_pos, sizeof(val));
922 o->f_pos += sizeof(struct BProto_data_header_s);
923 left -= sizeof(struct BProto_data_header_s);
924  
925 uint32_t payload_len = ltoh32(val.len);
926 ASSERT(left >= payload_len)
927 uint8_t *payload = o->buf + o->f_start + o->f_pos;
928 o->f_pos += payload_len;
929 left -= payload_len;
930  
931 if (type == BPROTO_TYPE_DATA && id == 10) {
932 *data = payload;
933 *data_len = payload_len;
934 return 1;
935 }
936 } break;
937 default:
938 ASSERT(0);
939 }
940 }
941  
942 return 0;
943 }
944  
945 void msg1Parser_Resetf (msg1Parser *o)
946 {
947 o->f_pos = 0;
948 }
949  
950 void msg1Parser_Forwardf (msg1Parser *o)
951 {
952 o->f_pos = o->f_span;
953 }
954  
955 int msg1Parser_Getg (msg1Parser *o, uint8_t **data)
956 {
957 ASSERT(o->g_pos >= 0)
958 ASSERT(o->g_pos <= o->g_span)
959  
960 int left = o->g_span - o->g_pos;
961  
962 while (left > 0) {
963 ASSERT(left >= sizeof(struct BProto_header_s))
964 struct BProto_header_s header;
965 memcpy(&header, o->buf + o->g_start + o->g_pos, sizeof(header));
966 o->g_pos += sizeof(struct BProto_header_s);
967 left -= sizeof(struct BProto_header_s);
968 uint16_t type = ltoh16(header.type);
969 uint16_t id = ltoh16(header.id);
970  
971 switch (type) {
972 case BPROTO_TYPE_UINT8: {
973 ASSERT(left >= sizeof(struct BProto_uint8_s))
974 o->g_pos += sizeof(struct BProto_uint8_s);
975 left -= sizeof(struct BProto_uint8_s);
976 } break;
977 case BPROTO_TYPE_UINT16: {
978 ASSERT(left >= sizeof(struct BProto_uint16_s))
979 o->g_pos += sizeof(struct BProto_uint16_s);
980 left -= sizeof(struct BProto_uint16_s);
981 } break;
982 case BPROTO_TYPE_UINT32: {
983 ASSERT(left >= sizeof(struct BProto_uint32_s))
984 o->g_pos += sizeof(struct BProto_uint32_s);
985 left -= sizeof(struct BProto_uint32_s);
986 } break;
987 case BPROTO_TYPE_UINT64: {
988 ASSERT(left >= sizeof(struct BProto_uint64_s))
989 o->g_pos += sizeof(struct BProto_uint64_s);
990 left -= sizeof(struct BProto_uint64_s);
991 } break;
992 case BPROTO_TYPE_DATA:
993 case BPROTO_TYPE_CONSTDATA:
994 {
995 ASSERT(left >= sizeof(struct BProto_data_header_s))
996 struct BProto_data_header_s val;
997 memcpy(&val, o->buf + o->g_start + o->g_pos, sizeof(val));
998 o->g_pos += sizeof(struct BProto_data_header_s);
999 left -= sizeof(struct BProto_data_header_s);
1000  
1001 uint32_t payload_len = ltoh32(val.len);
1002 ASSERT(left >= payload_len)
1003 uint8_t *payload = o->buf + o->g_start + o->g_pos;
1004 o->g_pos += payload_len;
1005 left -= payload_len;
1006  
1007 if (type == BPROTO_TYPE_CONSTDATA && id == 11) {
1008 *data = payload;
1009 return 1;
1010 }
1011 } break;
1012 default:
1013 ASSERT(0);
1014 }
1015 }
1016  
1017 return 0;
1018 }
1019  
1020 void msg1Parser_Resetg (msg1Parser *o)
1021 {
1022 o->g_pos = 0;
1023 }
1024  
1025 void msg1Parser_Forwardg (msg1Parser *o)
1026 {
1027 o->g_pos = o->g_span;
1028 }
1029