BadVPN – Blame information for rev 1
?pathlinks?
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 msg_SIZEtype (sizeof(struct BProto_header_s) + sizeof(struct BProto_uint16_s)) |
||
15 | #define msg_SIZEpayload(_len) (sizeof(struct BProto_header_s) + sizeof(struct BProto_data_header_s) + (_len)) |
||
16 | |||
17 | typedef struct { |
||
18 | uint8_t *out; |
||
19 | int used; |
||
20 | int type_count; |
||
21 | int payload_count; |
||
22 | } msgWriter; |
||
23 | |||
24 | static void msgWriter_Init (msgWriter *o, uint8_t *out); |
||
25 | static int msgWriter_Finish (msgWriter *o); |
||
26 | static void msgWriter_Addtype (msgWriter *o, uint16_t v); |
||
27 | static uint8_t * msgWriter_Addpayload (msgWriter *o, int len); |
||
28 | |||
29 | typedef struct { |
||
30 | uint8_t *buf; |
||
31 | int buf_len; |
||
32 | int type_start; |
||
33 | int type_span; |
||
34 | int type_pos; |
||
35 | int payload_start; |
||
36 | int payload_span; |
||
37 | int payload_pos; |
||
38 | } msgParser; |
||
39 | |||
40 | static int msgParser_Init (msgParser *o, uint8_t *buf, int buf_len); |
||
41 | static int msgParser_GotEverything (msgParser *o); |
||
42 | static int msgParser_Gettype (msgParser *o, uint16_t *v); |
||
43 | static void msgParser_Resettype (msgParser *o); |
||
44 | static void msgParser_Forwardtype (msgParser *o); |
||
45 | static int msgParser_Getpayload (msgParser *o, uint8_t **data, int *data_len); |
||
46 | static void msgParser_Resetpayload (msgParser *o); |
||
47 | static void msgParser_Forwardpayload (msgParser *o); |
||
48 | |||
49 | void msgWriter_Init (msgWriter *o, uint8_t *out) |
||
50 | { |
||
51 | o->out = out; |
||
52 | o->used = 0; |
||
53 | o->type_count = 0; |
||
54 | o->payload_count = 0; |
||
55 | } |
||
56 | |||
57 | int msgWriter_Finish (msgWriter *o) |
||
58 | { |
||
59 | ASSERT(o->used >= 0) |
||
60 | ASSERT(o->type_count == 1) |
||
61 | ASSERT(o->payload_count == 1) |
||
62 | |||
63 | return o->used; |
||
64 | } |
||
65 | |||
66 | void msgWriter_Addtype (msgWriter *o, uint16_t v) |
||
67 | { |
||
68 | ASSERT(o->used >= 0) |
||
69 | ASSERT(o->type_count == 0) |
||
70 | |||
71 | |||
72 | struct BProto_header_s header; |
||
73 | header.id = htol16(1); |
||
74 | header.type = htol16(BPROTO_TYPE_UINT16); |
||
75 | memcpy(o->out + o->used, &header, sizeof(header)); |
||
76 | o->used += sizeof(struct BProto_header_s); |
||
77 | |||
78 | struct BProto_uint16_s data; |
||
79 | data.v = htol16(v); |
||
80 | memcpy(o->out + o->used, &data, sizeof(data)); |
||
81 | o->used += sizeof(struct BProto_uint16_s); |
||
82 | |||
83 | o->type_count++; |
||
84 | } |
||
85 | |||
86 | uint8_t * msgWriter_Addpayload (msgWriter *o, int len) |
||
87 | { |
||
88 | ASSERT(o->used >= 0) |
||
89 | ASSERT(o->payload_count == 0) |
||
90 | ASSERT(len >= 0 && len <= UINT32_MAX) |
||
91 | |||
92 | struct BProto_header_s header; |
||
93 | header.id = htol16(2); |
||
94 | header.type = htol16(BPROTO_TYPE_DATA); |
||
95 | memcpy(o->out + o->used, &header, sizeof(header)); |
||
96 | o->used += sizeof(struct BProto_header_s); |
||
97 | |||
98 | struct BProto_data_header_s data; |
||
99 | data.len = htol32(len); |
||
100 | memcpy(o->out + o->used, &data, sizeof(data)); |
||
101 | o->used += sizeof(struct BProto_data_header_s); |
||
102 | |||
103 | uint8_t *dest = (o->out + o->used); |
||
104 | o->used += len; |
||
105 | |||
106 | o->payload_count++; |
||
107 | |||
108 | return dest; |
||
109 | } |
||
110 | |||
111 | int msgParser_Init (msgParser *o, uint8_t *buf, int buf_len) |
||
112 | { |
||
113 | ASSERT(buf_len >= 0) |
||
114 | |||
115 | o->buf = buf; |
||
116 | o->buf_len = buf_len; |
||
117 | o->type_start = o->buf_len; |
||
118 | o->type_span = 0; |
||
119 | o->type_pos = 0; |
||
120 | o->payload_start = o->buf_len; |
||
121 | o->payload_span = 0; |
||
122 | o->payload_pos = 0; |
||
123 | |||
124 | int type_count = 0; |
||
125 | int payload_count = 0; |
||
126 | |||
127 | int pos = 0; |
||
128 | int left = o->buf_len; |
||
129 | |||
130 | while (left > 0) { |
||
131 | int entry_pos = pos; |
||
132 | |||
133 | if (!(left >= sizeof(struct BProto_header_s))) { |
||
134 | return 0; |
||
135 | } |
||
136 | struct BProto_header_s header; |
||
137 | memcpy(&header, o->buf + pos, sizeof(header)); |
||
138 | pos += sizeof(struct BProto_header_s); |
||
139 | left -= sizeof(struct BProto_header_s); |
||
140 | uint16_t type = ltoh16(header.type); |
||
141 | uint16_t id = ltoh16(header.id); |
||
142 | |||
143 | switch (type) { |
||
144 | case BPROTO_TYPE_UINT8: { |
||
145 | if (!(left >= sizeof(struct BProto_uint8_s))) { |
||
146 | return 0; |
||
147 | } |
||
148 | pos += sizeof(struct BProto_uint8_s); |
||
149 | left -= sizeof(struct BProto_uint8_s); |
||
150 | |||
151 | switch (id) { |
||
152 | default: |
||
153 | return 0; |
||
154 | } |
||
155 | } break; |
||
156 | case BPROTO_TYPE_UINT16: { |
||
157 | if (!(left >= sizeof(struct BProto_uint16_s))) { |
||
158 | return 0; |
||
159 | } |
||
160 | pos += sizeof(struct BProto_uint16_s); |
||
161 | left -= sizeof(struct BProto_uint16_s); |
||
162 | |||
163 | switch (id) { |
||
164 | case 1: |
||
165 | if (o->type_start == o->buf_len) { |
||
166 | o->type_start = entry_pos; |
||
167 | } |
||
168 | o->type_span = pos - o->type_start; |
||
169 | type_count++; |
||
170 | break; |
||
171 | default: |
||
172 | return 0; |
||
173 | } |
||
174 | } break; |
||
175 | case BPROTO_TYPE_UINT32: { |
||
176 | if (!(left >= sizeof(struct BProto_uint32_s))) { |
||
177 | return 0; |
||
178 | } |
||
179 | pos += sizeof(struct BProto_uint32_s); |
||
180 | left -= sizeof(struct BProto_uint32_s); |
||
181 | |||
182 | switch (id) { |
||
183 | default: |
||
184 | return 0; |
||
185 | } |
||
186 | } break; |
||
187 | case BPROTO_TYPE_UINT64: { |
||
188 | if (!(left >= sizeof(struct BProto_uint64_s))) { |
||
189 | return 0; |
||
190 | } |
||
191 | pos += sizeof(struct BProto_uint64_s); |
||
192 | left -= sizeof(struct BProto_uint64_s); |
||
193 | |||
194 | switch (id) { |
||
195 | default: |
||
196 | return 0; |
||
197 | } |
||
198 | } break; |
||
199 | case BPROTO_TYPE_DATA: |
||
200 | case BPROTO_TYPE_CONSTDATA: |
||
201 | { |
||
202 | if (!(left >= sizeof(struct BProto_data_header_s))) { |
||
203 | return 0; |
||
204 | } |
||
205 | struct BProto_data_header_s val; |
||
206 | memcpy(&val, o->buf + pos, sizeof(val)); |
||
207 | pos += sizeof(struct BProto_data_header_s); |
||
208 | left -= sizeof(struct BProto_data_header_s); |
||
209 | |||
210 | uint32_t payload_len = ltoh32(val.len); |
||
211 | if (!(left >= payload_len)) { |
||
212 | return 0; |
||
213 | } |
||
214 | pos += payload_len; |
||
215 | left -= payload_len; |
||
216 | |||
217 | switch (id) { |
||
218 | case 2: |
||
219 | if (!(type == BPROTO_TYPE_DATA)) { |
||
220 | return 0; |
||
221 | } |
||
222 | if (o->payload_start == o->buf_len) { |
||
223 | o->payload_start = entry_pos; |
||
224 | } |
||
225 | o->payload_span = pos - o->payload_start; |
||
226 | payload_count++; |
||
227 | break; |
||
228 | default: |
||
229 | return 0; |
||
230 | } |
||
231 | } break; |
||
232 | default: |
||
233 | return 0; |
||
234 | } |
||
235 | } |
||
236 | |||
237 | if (!(type_count == 1)) { |
||
238 | return 0; |
||
239 | } |
||
240 | if (!(payload_count == 1)) { |
||
241 | return 0; |
||
242 | } |
||
243 | |||
244 | return 1; |
||
245 | } |
||
246 | |||
247 | int msgParser_GotEverything (msgParser *o) |
||
248 | { |
||
249 | return ( |
||
250 | o->type_pos == o->type_span |
||
251 | && |
||
252 | o->payload_pos == o->payload_span |
||
253 | ); |
||
254 | } |
||
255 | |||
256 | int msgParser_Gettype (msgParser *o, uint16_t *v) |
||
257 | { |
||
258 | ASSERT(o->type_pos >= 0) |
||
259 | ASSERT(o->type_pos <= o->type_span) |
||
260 | |||
261 | int left = o->type_span - o->type_pos; |
||
262 | |||
263 | while (left > 0) { |
||
264 | ASSERT(left >= sizeof(struct BProto_header_s)) |
||
265 | struct BProto_header_s header; |
||
266 | memcpy(&header, o->buf + o->type_start + o->type_pos, sizeof(header)); |
||
267 | o->type_pos += sizeof(struct BProto_header_s); |
||
268 | left -= sizeof(struct BProto_header_s); |
||
269 | uint16_t type = ltoh16(header.type); |
||
270 | uint16_t id = ltoh16(header.id); |
||
271 | |||
272 | switch (type) { |
||
273 | case BPROTO_TYPE_UINT8: { |
||
274 | ASSERT(left >= sizeof(struct BProto_uint8_s)) |
||
275 | o->type_pos += sizeof(struct BProto_uint8_s); |
||
276 | left -= sizeof(struct BProto_uint8_s); |
||
277 | } break; |
||
278 | case BPROTO_TYPE_UINT16: { |
||
279 | ASSERT(left >= sizeof(struct BProto_uint16_s)) |
||
280 | struct BProto_uint16_s val; |
||
281 | memcpy(&val, o->buf + o->type_start + o->type_pos, sizeof(val)); |
||
282 | o->type_pos += sizeof(struct BProto_uint16_s); |
||
283 | left -= sizeof(struct BProto_uint16_s); |
||
284 | |||
285 | if (id == 1) { |
||
286 | *v = ltoh16(val.v); |
||
287 | return 1; |
||
288 | } |
||
289 | } break; |
||
290 | case BPROTO_TYPE_UINT32: { |
||
291 | ASSERT(left >= sizeof(struct BProto_uint32_s)) |
||
292 | o->type_pos += sizeof(struct BProto_uint32_s); |
||
293 | left -= sizeof(struct BProto_uint32_s); |
||
294 | } break; |
||
295 | case BPROTO_TYPE_UINT64: { |
||
296 | ASSERT(left >= sizeof(struct BProto_uint64_s)) |
||
297 | o->type_pos += sizeof(struct BProto_uint64_s); |
||
298 | left -= sizeof(struct BProto_uint64_s); |
||
299 | } break; |
||
300 | case BPROTO_TYPE_DATA: |
||
301 | case BPROTO_TYPE_CONSTDATA: |
||
302 | { |
||
303 | ASSERT(left >= sizeof(struct BProto_data_header_s)) |
||
304 | struct BProto_data_header_s val; |
||
305 | memcpy(&val, o->buf + o->type_start + o->type_pos, sizeof(val)); |
||
306 | o->type_pos += sizeof(struct BProto_data_header_s); |
||
307 | left -= sizeof(struct BProto_data_header_s); |
||
308 | |||
309 | uint32_t payload_len = ltoh32(val.len); |
||
310 | ASSERT(left >= payload_len) |
||
311 | o->type_pos += payload_len; |
||
312 | left -= payload_len; |
||
313 | } break; |
||
314 | default: |
||
315 | ASSERT(0); |
||
316 | } |
||
317 | } |
||
318 | |||
319 | return 0; |
||
320 | } |
||
321 | |||
322 | void msgParser_Resettype (msgParser *o) |
||
323 | { |
||
324 | o->type_pos = 0; |
||
325 | } |
||
326 | |||
327 | void msgParser_Forwardtype (msgParser *o) |
||
328 | { |
||
329 | o->type_pos = o->type_span; |
||
330 | } |
||
331 | |||
332 | int msgParser_Getpayload (msgParser *o, uint8_t **data, int *data_len) |
||
333 | { |
||
334 | ASSERT(o->payload_pos >= 0) |
||
335 | ASSERT(o->payload_pos <= o->payload_span) |
||
336 | |||
337 | int left = o->payload_span - o->payload_pos; |
||
338 | |||
339 | while (left > 0) { |
||
340 | ASSERT(left >= sizeof(struct BProto_header_s)) |
||
341 | struct BProto_header_s header; |
||
342 | memcpy(&header, o->buf + o->payload_start + o->payload_pos, sizeof(header)); |
||
343 | o->payload_pos += sizeof(struct BProto_header_s); |
||
344 | left -= sizeof(struct BProto_header_s); |
||
345 | uint16_t type = ltoh16(header.type); |
||
346 | uint16_t id = ltoh16(header.id); |
||
347 | |||
348 | switch (type) { |
||
349 | case BPROTO_TYPE_UINT8: { |
||
350 | ASSERT(left >= sizeof(struct BProto_uint8_s)) |
||
351 | o->payload_pos += sizeof(struct BProto_uint8_s); |
||
352 | left -= sizeof(struct BProto_uint8_s); |
||
353 | } break; |
||
354 | case BPROTO_TYPE_UINT16: { |
||
355 | ASSERT(left >= sizeof(struct BProto_uint16_s)) |
||
356 | o->payload_pos += sizeof(struct BProto_uint16_s); |
||
357 | left -= sizeof(struct BProto_uint16_s); |
||
358 | } break; |
||
359 | case BPROTO_TYPE_UINT32: { |
||
360 | ASSERT(left >= sizeof(struct BProto_uint32_s)) |
||
361 | o->payload_pos += sizeof(struct BProto_uint32_s); |
||
362 | left -= sizeof(struct BProto_uint32_s); |
||
363 | } break; |
||
364 | case BPROTO_TYPE_UINT64: { |
||
365 | ASSERT(left >= sizeof(struct BProto_uint64_s)) |
||
366 | o->payload_pos += sizeof(struct BProto_uint64_s); |
||
367 | left -= sizeof(struct BProto_uint64_s); |
||
368 | } break; |
||
369 | case BPROTO_TYPE_DATA: |
||
370 | case BPROTO_TYPE_CONSTDATA: |
||
371 | { |
||
372 | ASSERT(left >= sizeof(struct BProto_data_header_s)) |
||
373 | struct BProto_data_header_s val; |
||
374 | memcpy(&val, o->buf + o->payload_start + o->payload_pos, sizeof(val)); |
||
375 | o->payload_pos += sizeof(struct BProto_data_header_s); |
||
376 | left -= sizeof(struct BProto_data_header_s); |
||
377 | |||
378 | uint32_t payload_len = ltoh32(val.len); |
||
379 | ASSERT(left >= payload_len) |
||
380 | uint8_t *payload = o->buf + o->payload_start + o->payload_pos; |
||
381 | o->payload_pos += payload_len; |
||
382 | left -= payload_len; |
||
383 | |||
384 | if (type == BPROTO_TYPE_DATA && id == 2) { |
||
385 | *data = payload; |
||
386 | *data_len = payload_len; |
||
387 | return 1; |
||
388 | } |
||
389 | } break; |
||
390 | default: |
||
391 | ASSERT(0); |
||
392 | } |
||
393 | } |
||
394 | |||
395 | return 0; |
||
396 | } |
||
397 | |||
398 | void msgParser_Resetpayload (msgParser *o) |
||
399 | { |
||
400 | o->payload_pos = 0; |
||
401 | } |
||
402 | |||
403 | void msgParser_Forwardpayload (msgParser *o) |
||
404 | { |
||
405 | o->payload_pos = o->payload_span; |
||
406 | } |
||
407 | |||
408 | #define msg_youconnect_SIZEaddr(_len) (sizeof(struct BProto_header_s) + sizeof(struct BProto_data_header_s) + (_len)) |
||
409 | #define msg_youconnect_SIZEkey(_len) (sizeof(struct BProto_header_s) + sizeof(struct BProto_data_header_s) + (_len)) |
||
410 | #define msg_youconnect_SIZEpassword (sizeof(struct BProto_header_s) + sizeof(struct BProto_uint64_s)) |
||
411 | |||
412 | typedef struct { |
||
413 | uint8_t *out; |
||
414 | int used; |
||
415 | int addr_count; |
||
416 | int key_count; |
||
417 | int password_count; |
||
418 | } msg_youconnectWriter; |
||
419 | |||
420 | static void msg_youconnectWriter_Init (msg_youconnectWriter *o, uint8_t *out); |
||
421 | static int msg_youconnectWriter_Finish (msg_youconnectWriter *o); |
||
422 | static uint8_t * msg_youconnectWriter_Addaddr (msg_youconnectWriter *o, int len); |
||
423 | static uint8_t * msg_youconnectWriter_Addkey (msg_youconnectWriter *o, int len); |
||
424 | static void msg_youconnectWriter_Addpassword (msg_youconnectWriter *o, uint64_t v); |
||
425 | |||
426 | typedef struct { |
||
427 | uint8_t *buf; |
||
428 | int buf_len; |
||
429 | int addr_start; |
||
430 | int addr_span; |
||
431 | int addr_pos; |
||
432 | int key_start; |
||
433 | int key_span; |
||
434 | int key_pos; |
||
435 | int password_start; |
||
436 | int password_span; |
||
437 | int password_pos; |
||
438 | } msg_youconnectParser; |
||
439 | |||
440 | static int msg_youconnectParser_Init (msg_youconnectParser *o, uint8_t *buf, int buf_len); |
||
441 | static int msg_youconnectParser_GotEverything (msg_youconnectParser *o); |
||
442 | static int msg_youconnectParser_Getaddr (msg_youconnectParser *o, uint8_t **data, int *data_len); |
||
443 | static void msg_youconnectParser_Resetaddr (msg_youconnectParser *o); |
||
444 | static void msg_youconnectParser_Forwardaddr (msg_youconnectParser *o); |
||
445 | static int msg_youconnectParser_Getkey (msg_youconnectParser *o, uint8_t **data, int *data_len); |
||
446 | static void msg_youconnectParser_Resetkey (msg_youconnectParser *o); |
||
447 | static void msg_youconnectParser_Forwardkey (msg_youconnectParser *o); |
||
448 | static int msg_youconnectParser_Getpassword (msg_youconnectParser *o, uint64_t *v); |
||
449 | static void msg_youconnectParser_Resetpassword (msg_youconnectParser *o); |
||
450 | static void msg_youconnectParser_Forwardpassword (msg_youconnectParser *o); |
||
451 | |||
452 | void msg_youconnectWriter_Init (msg_youconnectWriter *o, uint8_t *out) |
||
453 | { |
||
454 | o->out = out; |
||
455 | o->used = 0; |
||
456 | o->addr_count = 0; |
||
457 | o->key_count = 0; |
||
458 | o->password_count = 0; |
||
459 | } |
||
460 | |||
461 | int msg_youconnectWriter_Finish (msg_youconnectWriter *o) |
||
462 | { |
||
463 | ASSERT(o->used >= 0) |
||
464 | ASSERT(o->addr_count >= 1) |
||
465 | ASSERT(o->key_count >= 0 && o->key_count <= 1) |
||
466 | ASSERT(o->password_count >= 0 && o->password_count <= 1) |
||
467 | |||
468 | return o->used; |
||
469 | } |
||
470 | |||
471 | uint8_t * msg_youconnectWriter_Addaddr (msg_youconnectWriter *o, int len) |
||
472 | { |
||
473 | ASSERT(o->used >= 0) |
||
474 | |||
475 | ASSERT(len >= 0 && len <= UINT32_MAX) |
||
476 | |||
477 | struct BProto_header_s header; |
||
478 | header.id = htol16(1); |
||
479 | header.type = htol16(BPROTO_TYPE_DATA); |
||
480 | memcpy(o->out + o->used, &header, sizeof(header)); |
||
481 | o->used += sizeof(struct BProto_header_s); |
||
482 | |||
483 | struct BProto_data_header_s data; |
||
484 | data.len = htol32(len); |
||
485 | memcpy(o->out + o->used, &data, sizeof(data)); |
||
486 | o->used += sizeof(struct BProto_data_header_s); |
||
487 | |||
488 | uint8_t *dest = (o->out + o->used); |
||
489 | o->used += len; |
||
490 | |||
491 | o->addr_count++; |
||
492 | |||
493 | return dest; |
||
494 | } |
||
495 | |||
496 | uint8_t * msg_youconnectWriter_Addkey (msg_youconnectWriter *o, int len) |
||
497 | { |
||
498 | ASSERT(o->used >= 0) |
||
499 | ASSERT(o->key_count == 0) |
||
500 | ASSERT(len >= 0 && len <= UINT32_MAX) |
||
501 | |||
502 | struct BProto_header_s header; |
||
503 | header.id = htol16(2); |
||
504 | header.type = htol16(BPROTO_TYPE_DATA); |
||
505 | memcpy(o->out + o->used, &header, sizeof(header)); |
||
506 | o->used += sizeof(struct BProto_header_s); |
||
507 | |||
508 | struct BProto_data_header_s data; |
||
509 | data.len = htol32(len); |
||
510 | memcpy(o->out + o->used, &data, sizeof(data)); |
||
511 | o->used += sizeof(struct BProto_data_header_s); |
||
512 | |||
513 | uint8_t *dest = (o->out + o->used); |
||
514 | o->used += len; |
||
515 | |||
516 | o->key_count++; |
||
517 | |||
518 | return dest; |
||
519 | } |
||
520 | |||
521 | void msg_youconnectWriter_Addpassword (msg_youconnectWriter *o, uint64_t v) |
||
522 | { |
||
523 | ASSERT(o->used >= 0) |
||
524 | ASSERT(o->password_count == 0) |
||
525 | |||
526 | |||
527 | struct BProto_header_s header; |
||
528 | header.id = htol16(3); |
||
529 | header.type = htol16(BPROTO_TYPE_UINT64); |
||
530 | memcpy(o->out + o->used, &header, sizeof(header)); |
||
531 | o->used += sizeof(struct BProto_header_s); |
||
532 | |||
533 | struct BProto_uint64_s data; |
||
534 | data.v = htol64(v); |
||
535 | memcpy(o->out + o->used, &data, sizeof(data)); |
||
536 | o->used += sizeof(struct BProto_uint64_s); |
||
537 | |||
538 | o->password_count++; |
||
539 | } |
||
540 | |||
541 | int msg_youconnectParser_Init (msg_youconnectParser *o, uint8_t *buf, int buf_len) |
||
542 | { |
||
543 | ASSERT(buf_len >= 0) |
||
544 | |||
545 | o->buf = buf; |
||
546 | o->buf_len = buf_len; |
||
547 | o->addr_start = o->buf_len; |
||
548 | o->addr_span = 0; |
||
549 | o->addr_pos = 0; |
||
550 | o->key_start = o->buf_len; |
||
551 | o->key_span = 0; |
||
552 | o->key_pos = 0; |
||
553 | o->password_start = o->buf_len; |
||
554 | o->password_span = 0; |
||
555 | o->password_pos = 0; |
||
556 | |||
557 | int addr_count = 0; |
||
558 | int key_count = 0; |
||
559 | int password_count = 0; |
||
560 | |||
561 | int pos = 0; |
||
562 | int left = o->buf_len; |
||
563 | |||
564 | while (left > 0) { |
||
565 | int entry_pos = pos; |
||
566 | |||
567 | if (!(left >= sizeof(struct BProto_header_s))) { |
||
568 | return 0; |
||
569 | } |
||
570 | struct BProto_header_s header; |
||
571 | memcpy(&header, o->buf + pos, sizeof(header)); |
||
572 | pos += sizeof(struct BProto_header_s); |
||
573 | left -= sizeof(struct BProto_header_s); |
||
574 | uint16_t type = ltoh16(header.type); |
||
575 | uint16_t id = ltoh16(header.id); |
||
576 | |||
577 | switch (type) { |
||
578 | case BPROTO_TYPE_UINT8: { |
||
579 | if (!(left >= sizeof(struct BProto_uint8_s))) { |
||
580 | return 0; |
||
581 | } |
||
582 | pos += sizeof(struct BProto_uint8_s); |
||
583 | left -= sizeof(struct BProto_uint8_s); |
||
584 | |||
585 | switch (id) { |
||
586 | default: |
||
587 | return 0; |
||
588 | } |
||
589 | } break; |
||
590 | case BPROTO_TYPE_UINT16: { |
||
591 | if (!(left >= sizeof(struct BProto_uint16_s))) { |
||
592 | return 0; |
||
593 | } |
||
594 | pos += sizeof(struct BProto_uint16_s); |
||
595 | left -= sizeof(struct BProto_uint16_s); |
||
596 | |||
597 | switch (id) { |
||
598 | default: |
||
599 | return 0; |
||
600 | } |
||
601 | } break; |
||
602 | case BPROTO_TYPE_UINT32: { |
||
603 | if (!(left >= sizeof(struct BProto_uint32_s))) { |
||
604 | return 0; |
||
605 | } |
||
606 | pos += sizeof(struct BProto_uint32_s); |
||
607 | left -= sizeof(struct BProto_uint32_s); |
||
608 | |||
609 | switch (id) { |
||
610 | default: |
||
611 | return 0; |
||
612 | } |
||
613 | } break; |
||
614 | case BPROTO_TYPE_UINT64: { |
||
615 | if (!(left >= sizeof(struct BProto_uint64_s))) { |
||
616 | return 0; |
||
617 | } |
||
618 | pos += sizeof(struct BProto_uint64_s); |
||
619 | left -= sizeof(struct BProto_uint64_s); |
||
620 | |||
621 | switch (id) { |
||
622 | case 3: |
||
623 | if (o->password_start == o->buf_len) { |
||
624 | o->password_start = entry_pos; |
||
625 | } |
||
626 | o->password_span = pos - o->password_start; |
||
627 | password_count++; |
||
628 | break; |
||
629 | default: |
||
630 | return 0; |
||
631 | } |
||
632 | } break; |
||
633 | case BPROTO_TYPE_DATA: |
||
634 | case BPROTO_TYPE_CONSTDATA: |
||
635 | { |
||
636 | if (!(left >= sizeof(struct BProto_data_header_s))) { |
||
637 | return 0; |
||
638 | } |
||
639 | struct BProto_data_header_s val; |
||
640 | memcpy(&val, o->buf + pos, sizeof(val)); |
||
641 | pos += sizeof(struct BProto_data_header_s); |
||
642 | left -= sizeof(struct BProto_data_header_s); |
||
643 | |||
644 | uint32_t payload_len = ltoh32(val.len); |
||
645 | if (!(left >= payload_len)) { |
||
646 | return 0; |
||
647 | } |
||
648 | pos += payload_len; |
||
649 | left -= payload_len; |
||
650 | |||
651 | switch (id) { |
||
652 | case 1: |
||
653 | if (!(type == BPROTO_TYPE_DATA)) { |
||
654 | return 0; |
||
655 | } |
||
656 | if (o->addr_start == o->buf_len) { |
||
657 | o->addr_start = entry_pos; |
||
658 | } |
||
659 | o->addr_span = pos - o->addr_start; |
||
660 | addr_count++; |
||
661 | break; |
||
662 | case 2: |
||
663 | if (!(type == BPROTO_TYPE_DATA)) { |
||
664 | return 0; |
||
665 | } |
||
666 | if (o->key_start == o->buf_len) { |
||
667 | o->key_start = entry_pos; |
||
668 | } |
||
669 | o->key_span = pos - o->key_start; |
||
670 | key_count++; |
||
671 | break; |
||
672 | default: |
||
673 | return 0; |
||
674 | } |
||
675 | } break; |
||
676 | default: |
||
677 | return 0; |
||
678 | } |
||
679 | } |
||
680 | |||
681 | if (!(addr_count >= 1)) { |
||
682 | return 0; |
||
683 | } |
||
684 | if (!(key_count <= 1)) { |
||
685 | return 0; |
||
686 | } |
||
687 | if (!(password_count <= 1)) { |
||
688 | return 0; |
||
689 | } |
||
690 | |||
691 | return 1; |
||
692 | } |
||
693 | |||
694 | int msg_youconnectParser_GotEverything (msg_youconnectParser *o) |
||
695 | { |
||
696 | return ( |
||
697 | o->addr_pos == o->addr_span |
||
698 | && |
||
699 | o->key_pos == o->key_span |
||
700 | && |
||
701 | o->password_pos == o->password_span |
||
702 | ); |
||
703 | } |
||
704 | |||
705 | int msg_youconnectParser_Getaddr (msg_youconnectParser *o, uint8_t **data, int *data_len) |
||
706 | { |
||
707 | ASSERT(o->addr_pos >= 0) |
||
708 | ASSERT(o->addr_pos <= o->addr_span) |
||
709 | |||
710 | int left = o->addr_span - o->addr_pos; |
||
711 | |||
712 | while (left > 0) { |
||
713 | ASSERT(left >= sizeof(struct BProto_header_s)) |
||
714 | struct BProto_header_s header; |
||
715 | memcpy(&header, o->buf + o->addr_start + o->addr_pos, sizeof(header)); |
||
716 | o->addr_pos += sizeof(struct BProto_header_s); |
||
717 | left -= sizeof(struct BProto_header_s); |
||
718 | uint16_t type = ltoh16(header.type); |
||
719 | uint16_t id = ltoh16(header.id); |
||
720 | |||
721 | switch (type) { |
||
722 | case BPROTO_TYPE_UINT8: { |
||
723 | ASSERT(left >= sizeof(struct BProto_uint8_s)) |
||
724 | o->addr_pos += sizeof(struct BProto_uint8_s); |
||
725 | left -= sizeof(struct BProto_uint8_s); |
||
726 | } break; |
||
727 | case BPROTO_TYPE_UINT16: { |
||
728 | ASSERT(left >= sizeof(struct BProto_uint16_s)) |
||
729 | o->addr_pos += sizeof(struct BProto_uint16_s); |
||
730 | left -= sizeof(struct BProto_uint16_s); |
||
731 | } break; |
||
732 | case BPROTO_TYPE_UINT32: { |
||
733 | ASSERT(left >= sizeof(struct BProto_uint32_s)) |
||
734 | o->addr_pos += sizeof(struct BProto_uint32_s); |
||
735 | left -= sizeof(struct BProto_uint32_s); |
||
736 | } break; |
||
737 | case BPROTO_TYPE_UINT64: { |
||
738 | ASSERT(left >= sizeof(struct BProto_uint64_s)) |
||
739 | o->addr_pos += sizeof(struct BProto_uint64_s); |
||
740 | left -= sizeof(struct BProto_uint64_s); |
||
741 | } break; |
||
742 | case BPROTO_TYPE_DATA: |
||
743 | case BPROTO_TYPE_CONSTDATA: |
||
744 | { |
||
745 | ASSERT(left >= sizeof(struct BProto_data_header_s)) |
||
746 | struct BProto_data_header_s val; |
||
747 | memcpy(&val, o->buf + o->addr_start + o->addr_pos, sizeof(val)); |
||
748 | o->addr_pos += sizeof(struct BProto_data_header_s); |
||
749 | left -= sizeof(struct BProto_data_header_s); |
||
750 | |||
751 | uint32_t payload_len = ltoh32(val.len); |
||
752 | ASSERT(left >= payload_len) |
||
753 | uint8_t *payload = o->buf + o->addr_start + o->addr_pos; |
||
754 | o->addr_pos += payload_len; |
||
755 | left -= payload_len; |
||
756 | |||
757 | if (type == BPROTO_TYPE_DATA && id == 1) { |
||
758 | *data = payload; |
||
759 | *data_len = payload_len; |
||
760 | return 1; |
||
761 | } |
||
762 | } break; |
||
763 | default: |
||
764 | ASSERT(0); |
||
765 | } |
||
766 | } |
||
767 | |||
768 | return 0; |
||
769 | } |
||
770 | |||
771 | void msg_youconnectParser_Resetaddr (msg_youconnectParser *o) |
||
772 | { |
||
773 | o->addr_pos = 0; |
||
774 | } |
||
775 | |||
776 | void msg_youconnectParser_Forwardaddr (msg_youconnectParser *o) |
||
777 | { |
||
778 | o->addr_pos = o->addr_span; |
||
779 | } |
||
780 | |||
781 | int msg_youconnectParser_Getkey (msg_youconnectParser *o, uint8_t **data, int *data_len) |
||
782 | { |
||
783 | ASSERT(o->key_pos >= 0) |
||
784 | ASSERT(o->key_pos <= o->key_span) |
||
785 | |||
786 | int left = o->key_span - o->key_pos; |
||
787 | |||
788 | while (left > 0) { |
||
789 | ASSERT(left >= sizeof(struct BProto_header_s)) |
||
790 | struct BProto_header_s header; |
||
791 | memcpy(&header, o->buf + o->key_start + o->key_pos, sizeof(header)); |
||
792 | o->key_pos += sizeof(struct BProto_header_s); |
||
793 | left -= sizeof(struct BProto_header_s); |
||
794 | uint16_t type = ltoh16(header.type); |
||
795 | uint16_t id = ltoh16(header.id); |
||
796 | |||
797 | switch (type) { |
||
798 | case BPROTO_TYPE_UINT8: { |
||
799 | ASSERT(left >= sizeof(struct BProto_uint8_s)) |
||
800 | o->key_pos += sizeof(struct BProto_uint8_s); |
||
801 | left -= sizeof(struct BProto_uint8_s); |
||
802 | } break; |
||
803 | case BPROTO_TYPE_UINT16: { |
||
804 | ASSERT(left >= sizeof(struct BProto_uint16_s)) |
||
805 | o->key_pos += sizeof(struct BProto_uint16_s); |
||
806 | left -= sizeof(struct BProto_uint16_s); |
||
807 | } break; |
||
808 | case BPROTO_TYPE_UINT32: { |
||
809 | ASSERT(left >= sizeof(struct BProto_uint32_s)) |
||
810 | o->key_pos += sizeof(struct BProto_uint32_s); |
||
811 | left -= sizeof(struct BProto_uint32_s); |
||
812 | } break; |
||
813 | case BPROTO_TYPE_UINT64: { |
||
814 | ASSERT(left >= sizeof(struct BProto_uint64_s)) |
||
815 | o->key_pos += sizeof(struct BProto_uint64_s); |
||
816 | left -= sizeof(struct BProto_uint64_s); |
||
817 | } break; |
||
818 | case BPROTO_TYPE_DATA: |
||
819 | case BPROTO_TYPE_CONSTDATA: |
||
820 | { |
||
821 | ASSERT(left >= sizeof(struct BProto_data_header_s)) |
||
822 | struct BProto_data_header_s val; |
||
823 | memcpy(&val, o->buf + o->key_start + o->key_pos, sizeof(val)); |
||
824 | o->key_pos += sizeof(struct BProto_data_header_s); |
||
825 | left -= sizeof(struct BProto_data_header_s); |
||
826 | |||
827 | uint32_t payload_len = ltoh32(val.len); |
||
828 | ASSERT(left >= payload_len) |
||
829 | uint8_t *payload = o->buf + o->key_start + o->key_pos; |
||
830 | o->key_pos += payload_len; |
||
831 | left -= payload_len; |
||
832 | |||
833 | if (type == BPROTO_TYPE_DATA && id == 2) { |
||
834 | *data = payload; |
||
835 | *data_len = payload_len; |
||
836 | return 1; |
||
837 | } |
||
838 | } break; |
||
839 | default: |
||
840 | ASSERT(0); |
||
841 | } |
||
842 | } |
||
843 | |||
844 | return 0; |
||
845 | } |
||
846 | |||
847 | void msg_youconnectParser_Resetkey (msg_youconnectParser *o) |
||
848 | { |
||
849 | o->key_pos = 0; |
||
850 | } |
||
851 | |||
852 | void msg_youconnectParser_Forwardkey (msg_youconnectParser *o) |
||
853 | { |
||
854 | o->key_pos = o->key_span; |
||
855 | } |
||
856 | |||
857 | int msg_youconnectParser_Getpassword (msg_youconnectParser *o, uint64_t *v) |
||
858 | { |
||
859 | ASSERT(o->password_pos >= 0) |
||
860 | ASSERT(o->password_pos <= o->password_span) |
||
861 | |||
862 | int left = o->password_span - o->password_pos; |
||
863 | |||
864 | while (left > 0) { |
||
865 | ASSERT(left >= sizeof(struct BProto_header_s)) |
||
866 | struct BProto_header_s header; |
||
867 | memcpy(&header, o->buf + o->password_start + o->password_pos, sizeof(header)); |
||
868 | o->password_pos += sizeof(struct BProto_header_s); |
||
869 | left -= sizeof(struct BProto_header_s); |
||
870 | uint16_t type = ltoh16(header.type); |
||
871 | uint16_t id = ltoh16(header.id); |
||
872 | |||
873 | switch (type) { |
||
874 | case BPROTO_TYPE_UINT8: { |
||
875 | ASSERT(left >= sizeof(struct BProto_uint8_s)) |
||
876 | o->password_pos += sizeof(struct BProto_uint8_s); |
||
877 | left -= sizeof(struct BProto_uint8_s); |
||
878 | } break; |
||
879 | case BPROTO_TYPE_UINT16: { |
||
880 | ASSERT(left >= sizeof(struct BProto_uint16_s)) |
||
881 | o->password_pos += sizeof(struct BProto_uint16_s); |
||
882 | left -= sizeof(struct BProto_uint16_s); |
||
883 | } break; |
||
884 | case BPROTO_TYPE_UINT32: { |
||
885 | ASSERT(left >= sizeof(struct BProto_uint32_s)) |
||
886 | o->password_pos += sizeof(struct BProto_uint32_s); |
||
887 | left -= sizeof(struct BProto_uint32_s); |
||
888 | } break; |
||
889 | case BPROTO_TYPE_UINT64: { |
||
890 | ASSERT(left >= sizeof(struct BProto_uint64_s)) |
||
891 | struct BProto_uint64_s val; |
||
892 | memcpy(&val, o->buf + o->password_start + o->password_pos, sizeof(val)); |
||
893 | o->password_pos += sizeof(struct BProto_uint64_s); |
||
894 | left -= sizeof(struct BProto_uint64_s); |
||
895 | |||
896 | if (id == 3) { |
||
897 | *v = ltoh64(val.v); |
||
898 | return 1; |
||
899 | } |
||
900 | } break; |
||
901 | case BPROTO_TYPE_DATA: |
||
902 | case BPROTO_TYPE_CONSTDATA: |
||
903 | { |
||
904 | ASSERT(left >= sizeof(struct BProto_data_header_s)) |
||
905 | struct BProto_data_header_s val; |
||
906 | memcpy(&val, o->buf + o->password_start + o->password_pos, sizeof(val)); |
||
907 | o->password_pos += sizeof(struct BProto_data_header_s); |
||
908 | left -= sizeof(struct BProto_data_header_s); |
||
909 | |||
910 | uint32_t payload_len = ltoh32(val.len); |
||
911 | ASSERT(left >= payload_len) |
||
912 | o->password_pos += payload_len; |
||
913 | left -= payload_len; |
||
914 | } break; |
||
915 | default: |
||
916 | ASSERT(0); |
||
917 | } |
||
918 | } |
||
919 | |||
920 | return 0; |
||
921 | } |
||
922 | |||
923 | void msg_youconnectParser_Resetpassword (msg_youconnectParser *o) |
||
924 | { |
||
925 | o->password_pos = 0; |
||
926 | } |
||
927 | |||
928 | void msg_youconnectParser_Forwardpassword (msg_youconnectParser *o) |
||
929 | { |
||
930 | o->password_pos = o->password_span; |
||
931 | } |
||
932 | |||
933 | #define msg_youconnect_addr_SIZEname(_len) (sizeof(struct BProto_header_s) + sizeof(struct BProto_data_header_s) + (_len)) |
||
934 | #define msg_youconnect_addr_SIZEaddr(_len) (sizeof(struct BProto_header_s) + sizeof(struct BProto_data_header_s) + (_len)) |
||
935 | |||
936 | typedef struct { |
||
937 | uint8_t *out; |
||
938 | int used; |
||
939 | int name_count; |
||
940 | int addr_count; |
||
941 | } msg_youconnect_addrWriter; |
||
942 | |||
943 | static void msg_youconnect_addrWriter_Init (msg_youconnect_addrWriter *o, uint8_t *out); |
||
944 | static int msg_youconnect_addrWriter_Finish (msg_youconnect_addrWriter *o); |
||
945 | static uint8_t * msg_youconnect_addrWriter_Addname (msg_youconnect_addrWriter *o, int len); |
||
946 | static uint8_t * msg_youconnect_addrWriter_Addaddr (msg_youconnect_addrWriter *o, int len); |
||
947 | |||
948 | typedef struct { |
||
949 | uint8_t *buf; |
||
950 | int buf_len; |
||
951 | int name_start; |
||
952 | int name_span; |
||
953 | int name_pos; |
||
954 | int addr_start; |
||
955 | int addr_span; |
||
956 | int addr_pos; |
||
957 | } msg_youconnect_addrParser; |
||
958 | |||
959 | static int msg_youconnect_addrParser_Init (msg_youconnect_addrParser *o, uint8_t *buf, int buf_len); |
||
960 | static int msg_youconnect_addrParser_GotEverything (msg_youconnect_addrParser *o); |
||
961 | static int msg_youconnect_addrParser_Getname (msg_youconnect_addrParser *o, uint8_t **data, int *data_len); |
||
962 | static void msg_youconnect_addrParser_Resetname (msg_youconnect_addrParser *o); |
||
963 | static void msg_youconnect_addrParser_Forwardname (msg_youconnect_addrParser *o); |
||
964 | static int msg_youconnect_addrParser_Getaddr (msg_youconnect_addrParser *o, uint8_t **data, int *data_len); |
||
965 | static void msg_youconnect_addrParser_Resetaddr (msg_youconnect_addrParser *o); |
||
966 | static void msg_youconnect_addrParser_Forwardaddr (msg_youconnect_addrParser *o); |
||
967 | |||
968 | void msg_youconnect_addrWriter_Init (msg_youconnect_addrWriter *o, uint8_t *out) |
||
969 | { |
||
970 | o->out = out; |
||
971 | o->used = 0; |
||
972 | o->name_count = 0; |
||
973 | o->addr_count = 0; |
||
974 | } |
||
975 | |||
976 | int msg_youconnect_addrWriter_Finish (msg_youconnect_addrWriter *o) |
||
977 | { |
||
978 | ASSERT(o->used >= 0) |
||
979 | ASSERT(o->name_count == 1) |
||
980 | ASSERT(o->addr_count == 1) |
||
981 | |||
982 | return o->used; |
||
983 | } |
||
984 | |||
985 | uint8_t * msg_youconnect_addrWriter_Addname (msg_youconnect_addrWriter *o, int len) |
||
986 | { |
||
987 | ASSERT(o->used >= 0) |
||
988 | ASSERT(o->name_count == 0) |
||
989 | ASSERT(len >= 0 && len <= UINT32_MAX) |
||
990 | |||
991 | struct BProto_header_s header; |
||
992 | header.id = htol16(1); |
||
993 | header.type = htol16(BPROTO_TYPE_DATA); |
||
994 | memcpy(o->out + o->used, &header, sizeof(header)); |
||
995 | o->used += sizeof(struct BProto_header_s); |
||
996 | |||
997 | struct BProto_data_header_s data; |
||
998 | data.len = htol32(len); |
||
999 | memcpy(o->out + o->used, &data, sizeof(data)); |
||
1000 | o->used += sizeof(struct BProto_data_header_s); |
||
1001 | |||
1002 | uint8_t *dest = (o->out + o->used); |
||
1003 | o->used += len; |
||
1004 | |||
1005 | o->name_count++; |
||
1006 | |||
1007 | return dest; |
||
1008 | } |
||
1009 | |||
1010 | uint8_t * msg_youconnect_addrWriter_Addaddr (msg_youconnect_addrWriter *o, int len) |
||
1011 | { |
||
1012 | ASSERT(o->used >= 0) |
||
1013 | ASSERT(o->addr_count == 0) |
||
1014 | ASSERT(len >= 0 && len <= UINT32_MAX) |
||
1015 | |||
1016 | struct BProto_header_s header; |
||
1017 | header.id = htol16(2); |
||
1018 | header.type = htol16(BPROTO_TYPE_DATA); |
||
1019 | memcpy(o->out + o->used, &header, sizeof(header)); |
||
1020 | o->used += sizeof(struct BProto_header_s); |
||
1021 | |||
1022 | struct BProto_data_header_s data; |
||
1023 | data.len = htol32(len); |
||
1024 | memcpy(o->out + o->used, &data, sizeof(data)); |
||
1025 | o->used += sizeof(struct BProto_data_header_s); |
||
1026 | |||
1027 | uint8_t *dest = (o->out + o->used); |
||
1028 | o->used += len; |
||
1029 | |||
1030 | o->addr_count++; |
||
1031 | |||
1032 | return dest; |
||
1033 | } |
||
1034 | |||
1035 | int msg_youconnect_addrParser_Init (msg_youconnect_addrParser *o, uint8_t *buf, int buf_len) |
||
1036 | { |
||
1037 | ASSERT(buf_len >= 0) |
||
1038 | |||
1039 | o->buf = buf; |
||
1040 | o->buf_len = buf_len; |
||
1041 | o->name_start = o->buf_len; |
||
1042 | o->name_span = 0; |
||
1043 | o->name_pos = 0; |
||
1044 | o->addr_start = o->buf_len; |
||
1045 | o->addr_span = 0; |
||
1046 | o->addr_pos = 0; |
||
1047 | |||
1048 | int name_count = 0; |
||
1049 | int addr_count = 0; |
||
1050 | |||
1051 | int pos = 0; |
||
1052 | int left = o->buf_len; |
||
1053 | |||
1054 | while (left > 0) { |
||
1055 | int entry_pos = pos; |
||
1056 | |||
1057 | if (!(left >= sizeof(struct BProto_header_s))) { |
||
1058 | return 0; |
||
1059 | } |
||
1060 | struct BProto_header_s header; |
||
1061 | memcpy(&header, o->buf + pos, sizeof(header)); |
||
1062 | pos += sizeof(struct BProto_header_s); |
||
1063 | left -= sizeof(struct BProto_header_s); |
||
1064 | uint16_t type = ltoh16(header.type); |
||
1065 | uint16_t id = ltoh16(header.id); |
||
1066 | |||
1067 | switch (type) { |
||
1068 | case BPROTO_TYPE_UINT8: { |
||
1069 | if (!(left >= sizeof(struct BProto_uint8_s))) { |
||
1070 | return 0; |
||
1071 | } |
||
1072 | pos += sizeof(struct BProto_uint8_s); |
||
1073 | left -= sizeof(struct BProto_uint8_s); |
||
1074 | |||
1075 | switch (id) { |
||
1076 | default: |
||
1077 | return 0; |
||
1078 | } |
||
1079 | } break; |
||
1080 | case BPROTO_TYPE_UINT16: { |
||
1081 | if (!(left >= sizeof(struct BProto_uint16_s))) { |
||
1082 | return 0; |
||
1083 | } |
||
1084 | pos += sizeof(struct BProto_uint16_s); |
||
1085 | left -= sizeof(struct BProto_uint16_s); |
||
1086 | |||
1087 | switch (id) { |
||
1088 | default: |
||
1089 | return 0; |
||
1090 | } |
||
1091 | } break; |
||
1092 | case BPROTO_TYPE_UINT32: { |
||
1093 | if (!(left >= sizeof(struct BProto_uint32_s))) { |
||
1094 | return 0; |
||
1095 | } |
||
1096 | pos += sizeof(struct BProto_uint32_s); |
||
1097 | left -= sizeof(struct BProto_uint32_s); |
||
1098 | |||
1099 | switch (id) { |
||
1100 | default: |
||
1101 | return 0; |
||
1102 | } |
||
1103 | } break; |
||
1104 | case BPROTO_TYPE_UINT64: { |
||
1105 | if (!(left >= sizeof(struct BProto_uint64_s))) { |
||
1106 | return 0; |
||
1107 | } |
||
1108 | pos += sizeof(struct BProto_uint64_s); |
||
1109 | left -= sizeof(struct BProto_uint64_s); |
||
1110 | |||
1111 | switch (id) { |
||
1112 | default: |
||
1113 | return 0; |
||
1114 | } |
||
1115 | } break; |
||
1116 | case BPROTO_TYPE_DATA: |
||
1117 | case BPROTO_TYPE_CONSTDATA: |
||
1118 | { |
||
1119 | if (!(left >= sizeof(struct BProto_data_header_s))) { |
||
1120 | return 0; |
||
1121 | } |
||
1122 | struct BProto_data_header_s val; |
||
1123 | memcpy(&val, o->buf + pos, sizeof(val)); |
||
1124 | pos += sizeof(struct BProto_data_header_s); |
||
1125 | left -= sizeof(struct BProto_data_header_s); |
||
1126 | |||
1127 | uint32_t payload_len = ltoh32(val.len); |
||
1128 | if (!(left >= payload_len)) { |
||
1129 | return 0; |
||
1130 | } |
||
1131 | pos += payload_len; |
||
1132 | left -= payload_len; |
||
1133 | |||
1134 | switch (id) { |
||
1135 | case 1: |
||
1136 | if (!(type == BPROTO_TYPE_DATA)) { |
||
1137 | return 0; |
||
1138 | } |
||
1139 | if (o->name_start == o->buf_len) { |
||
1140 | o->name_start = entry_pos; |
||
1141 | } |
||
1142 | o->name_span = pos - o->name_start; |
||
1143 | name_count++; |
||
1144 | break; |
||
1145 | case 2: |
||
1146 | if (!(type == BPROTO_TYPE_DATA)) { |
||
1147 | return 0; |
||
1148 | } |
||
1149 | if (o->addr_start == o->buf_len) { |
||
1150 | o->addr_start = entry_pos; |
||
1151 | } |
||
1152 | o->addr_span = pos - o->addr_start; |
||
1153 | addr_count++; |
||
1154 | break; |
||
1155 | default: |
||
1156 | return 0; |
||
1157 | } |
||
1158 | } break; |
||
1159 | default: |
||
1160 | return 0; |
||
1161 | } |
||
1162 | } |
||
1163 | |||
1164 | if (!(name_count == 1)) { |
||
1165 | return 0; |
||
1166 | } |
||
1167 | if (!(addr_count == 1)) { |
||
1168 | return 0; |
||
1169 | } |
||
1170 | |||
1171 | return 1; |
||
1172 | } |
||
1173 | |||
1174 | int msg_youconnect_addrParser_GotEverything (msg_youconnect_addrParser *o) |
||
1175 | { |
||
1176 | return ( |
||
1177 | o->name_pos == o->name_span |
||
1178 | && |
||
1179 | o->addr_pos == o->addr_span |
||
1180 | ); |
||
1181 | } |
||
1182 | |||
1183 | int msg_youconnect_addrParser_Getname (msg_youconnect_addrParser *o, uint8_t **data, int *data_len) |
||
1184 | { |
||
1185 | ASSERT(o->name_pos >= 0) |
||
1186 | ASSERT(o->name_pos <= o->name_span) |
||
1187 | |||
1188 | int left = o->name_span - o->name_pos; |
||
1189 | |||
1190 | while (left > 0) { |
||
1191 | ASSERT(left >= sizeof(struct BProto_header_s)) |
||
1192 | struct BProto_header_s header; |
||
1193 | memcpy(&header, o->buf + o->name_start + o->name_pos, sizeof(header)); |
||
1194 | o->name_pos += sizeof(struct BProto_header_s); |
||
1195 | left -= sizeof(struct BProto_header_s); |
||
1196 | uint16_t type = ltoh16(header.type); |
||
1197 | uint16_t id = ltoh16(header.id); |
||
1198 | |||
1199 | switch (type) { |
||
1200 | case BPROTO_TYPE_UINT8: { |
||
1201 | ASSERT(left >= sizeof(struct BProto_uint8_s)) |
||
1202 | o->name_pos += sizeof(struct BProto_uint8_s); |
||
1203 | left -= sizeof(struct BProto_uint8_s); |
||
1204 | } break; |
||
1205 | case BPROTO_TYPE_UINT16: { |
||
1206 | ASSERT(left >= sizeof(struct BProto_uint16_s)) |
||
1207 | o->name_pos += sizeof(struct BProto_uint16_s); |
||
1208 | left -= sizeof(struct BProto_uint16_s); |
||
1209 | } break; |
||
1210 | case BPROTO_TYPE_UINT32: { |
||
1211 | ASSERT(left >= sizeof(struct BProto_uint32_s)) |
||
1212 | o->name_pos += sizeof(struct BProto_uint32_s); |
||
1213 | left -= sizeof(struct BProto_uint32_s); |
||
1214 | } break; |
||
1215 | case BPROTO_TYPE_UINT64: { |
||
1216 | ASSERT(left >= sizeof(struct BProto_uint64_s)) |
||
1217 | o->name_pos += sizeof(struct BProto_uint64_s); |
||
1218 | left -= sizeof(struct BProto_uint64_s); |
||
1219 | } break; |
||
1220 | case BPROTO_TYPE_DATA: |
||
1221 | case BPROTO_TYPE_CONSTDATA: |
||
1222 | { |
||
1223 | ASSERT(left >= sizeof(struct BProto_data_header_s)) |
||
1224 | struct BProto_data_header_s val; |
||
1225 | memcpy(&val, o->buf + o->name_start + o->name_pos, sizeof(val)); |
||
1226 | o->name_pos += sizeof(struct BProto_data_header_s); |
||
1227 | left -= sizeof(struct BProto_data_header_s); |
||
1228 | |||
1229 | uint32_t payload_len = ltoh32(val.len); |
||
1230 | ASSERT(left >= payload_len) |
||
1231 | uint8_t *payload = o->buf + o->name_start + o->name_pos; |
||
1232 | o->name_pos += payload_len; |
||
1233 | left -= payload_len; |
||
1234 | |||
1235 | if (type == BPROTO_TYPE_DATA && id == 1) { |
||
1236 | *data = payload; |
||
1237 | *data_len = payload_len; |
||
1238 | return 1; |
||
1239 | } |
||
1240 | } break; |
||
1241 | default: |
||
1242 | ASSERT(0); |
||
1243 | } |
||
1244 | } |
||
1245 | |||
1246 | return 0; |
||
1247 | } |
||
1248 | |||
1249 | void msg_youconnect_addrParser_Resetname (msg_youconnect_addrParser *o) |
||
1250 | { |
||
1251 | o->name_pos = 0; |
||
1252 | } |
||
1253 | |||
1254 | void msg_youconnect_addrParser_Forwardname (msg_youconnect_addrParser *o) |
||
1255 | { |
||
1256 | o->name_pos = o->name_span; |
||
1257 | } |
||
1258 | |||
1259 | int msg_youconnect_addrParser_Getaddr (msg_youconnect_addrParser *o, uint8_t **data, int *data_len) |
||
1260 | { |
||
1261 | ASSERT(o->addr_pos >= 0) |
||
1262 | ASSERT(o->addr_pos <= o->addr_span) |
||
1263 | |||
1264 | int left = o->addr_span - o->addr_pos; |
||
1265 | |||
1266 | while (left > 0) { |
||
1267 | ASSERT(left >= sizeof(struct BProto_header_s)) |
||
1268 | struct BProto_header_s header; |
||
1269 | memcpy(&header, o->buf + o->addr_start + o->addr_pos, sizeof(header)); |
||
1270 | o->addr_pos += sizeof(struct BProto_header_s); |
||
1271 | left -= sizeof(struct BProto_header_s); |
||
1272 | uint16_t type = ltoh16(header.type); |
||
1273 | uint16_t id = ltoh16(header.id); |
||
1274 | |||
1275 | switch (type) { |
||
1276 | case BPROTO_TYPE_UINT8: { |
||
1277 | ASSERT(left >= sizeof(struct BProto_uint8_s)) |
||
1278 | o->addr_pos += sizeof(struct BProto_uint8_s); |
||
1279 | left -= sizeof(struct BProto_uint8_s); |
||
1280 | } break; |
||
1281 | case BPROTO_TYPE_UINT16: { |
||
1282 | ASSERT(left >= sizeof(struct BProto_uint16_s)) |
||
1283 | o->addr_pos += sizeof(struct BProto_uint16_s); |
||
1284 | left -= sizeof(struct BProto_uint16_s); |
||
1285 | } break; |
||
1286 | case BPROTO_TYPE_UINT32: { |
||
1287 | ASSERT(left >= sizeof(struct BProto_uint32_s)) |
||
1288 | o->addr_pos += sizeof(struct BProto_uint32_s); |
||
1289 | left -= sizeof(struct BProto_uint32_s); |
||
1290 | } break; |
||
1291 | case BPROTO_TYPE_UINT64: { |
||
1292 | ASSERT(left >= sizeof(struct BProto_uint64_s)) |
||
1293 | o->addr_pos += sizeof(struct BProto_uint64_s); |
||
1294 | left -= sizeof(struct BProto_uint64_s); |
||
1295 | } break; |
||
1296 | case BPROTO_TYPE_DATA: |
||
1297 | case BPROTO_TYPE_CONSTDATA: |
||
1298 | { |
||
1299 | ASSERT(left >= sizeof(struct BProto_data_header_s)) |
||
1300 | struct BProto_data_header_s val; |
||
1301 | memcpy(&val, o->buf + o->addr_start + o->addr_pos, sizeof(val)); |
||
1302 | o->addr_pos += sizeof(struct BProto_data_header_s); |
||
1303 | left -= sizeof(struct BProto_data_header_s); |
||
1304 | |||
1305 | uint32_t payload_len = ltoh32(val.len); |
||
1306 | ASSERT(left >= payload_len) |
||
1307 | uint8_t *payload = o->buf + o->addr_start + o->addr_pos; |
||
1308 | o->addr_pos += payload_len; |
||
1309 | left -= payload_len; |
||
1310 | |||
1311 | if (type == BPROTO_TYPE_DATA && id == 2) { |
||
1312 | *data = payload; |
||
1313 | *data_len = payload_len; |
||
1314 | return 1; |
||
1315 | } |
||
1316 | } break; |
||
1317 | default: |
||
1318 | ASSERT(0); |
||
1319 | } |
||
1320 | } |
||
1321 | |||
1322 | return 0; |
||
1323 | } |
||
1324 | |||
1325 | void msg_youconnect_addrParser_Resetaddr (msg_youconnect_addrParser *o) |
||
1326 | { |
||
1327 | o->addr_pos = 0; |
||
1328 | } |
||
1329 | |||
1330 | void msg_youconnect_addrParser_Forwardaddr (msg_youconnect_addrParser *o) |
||
1331 | { |
||
1332 | o->addr_pos = o->addr_span; |
||
1333 | } |
||
1334 | |||
1335 | #define msg_seed_SIZEseed_id (sizeof(struct BProto_header_s) + sizeof(struct BProto_uint16_s)) |
||
1336 | #define msg_seed_SIZEkey(_len) (sizeof(struct BProto_header_s) + sizeof(struct BProto_data_header_s) + (_len)) |
||
1337 | #define msg_seed_SIZEiv(_len) (sizeof(struct BProto_header_s) + sizeof(struct BProto_data_header_s) + (_len)) |
||
1338 | |||
1339 | typedef struct { |
||
1340 | uint8_t *out; |
||
1341 | int used; |
||
1342 | int seed_id_count; |
||
1343 | int key_count; |
||
1344 | int iv_count; |
||
1345 | } msg_seedWriter; |
||
1346 | |||
1347 | static void msg_seedWriter_Init (msg_seedWriter *o, uint8_t *out); |
||
1348 | static int msg_seedWriter_Finish (msg_seedWriter *o); |
||
1349 | static void msg_seedWriter_Addseed_id (msg_seedWriter *o, uint16_t v); |
||
1350 | static uint8_t * msg_seedWriter_Addkey (msg_seedWriter *o, int len); |
||
1351 | static uint8_t * msg_seedWriter_Addiv (msg_seedWriter *o, int len); |
||
1352 | |||
1353 | typedef struct { |
||
1354 | uint8_t *buf; |
||
1355 | int buf_len; |
||
1356 | int seed_id_start; |
||
1357 | int seed_id_span; |
||
1358 | int seed_id_pos; |
||
1359 | int key_start; |
||
1360 | int key_span; |
||
1361 | int key_pos; |
||
1362 | int iv_start; |
||
1363 | int iv_span; |
||
1364 | int iv_pos; |
||
1365 | } msg_seedParser; |
||
1366 | |||
1367 | static int msg_seedParser_Init (msg_seedParser *o, uint8_t *buf, int buf_len); |
||
1368 | static int msg_seedParser_GotEverything (msg_seedParser *o); |
||
1369 | static int msg_seedParser_Getseed_id (msg_seedParser *o, uint16_t *v); |
||
1370 | static void msg_seedParser_Resetseed_id (msg_seedParser *o); |
||
1371 | static void msg_seedParser_Forwardseed_id (msg_seedParser *o); |
||
1372 | static int msg_seedParser_Getkey (msg_seedParser *o, uint8_t **data, int *data_len); |
||
1373 | static void msg_seedParser_Resetkey (msg_seedParser *o); |
||
1374 | static void msg_seedParser_Forwardkey (msg_seedParser *o); |
||
1375 | static int msg_seedParser_Getiv (msg_seedParser *o, uint8_t **data, int *data_len); |
||
1376 | static void msg_seedParser_Resetiv (msg_seedParser *o); |
||
1377 | static void msg_seedParser_Forwardiv (msg_seedParser *o); |
||
1378 | |||
1379 | void msg_seedWriter_Init (msg_seedWriter *o, uint8_t *out) |
||
1380 | { |
||
1381 | o->out = out; |
||
1382 | o->used = 0; |
||
1383 | o->seed_id_count = 0; |
||
1384 | o->key_count = 0; |
||
1385 | o->iv_count = 0; |
||
1386 | } |
||
1387 | |||
1388 | int msg_seedWriter_Finish (msg_seedWriter *o) |
||
1389 | { |
||
1390 | ASSERT(o->used >= 0) |
||
1391 | ASSERT(o->seed_id_count == 1) |
||
1392 | ASSERT(o->key_count == 1) |
||
1393 | ASSERT(o->iv_count == 1) |
||
1394 | |||
1395 | return o->used; |
||
1396 | } |
||
1397 | |||
1398 | void msg_seedWriter_Addseed_id (msg_seedWriter *o, uint16_t v) |
||
1399 | { |
||
1400 | ASSERT(o->used >= 0) |
||
1401 | ASSERT(o->seed_id_count == 0) |
||
1402 | |||
1403 | |||
1404 | struct BProto_header_s header; |
||
1405 | header.id = htol16(1); |
||
1406 | header.type = htol16(BPROTO_TYPE_UINT16); |
||
1407 | memcpy(o->out + o->used, &header, sizeof(header)); |
||
1408 | o->used += sizeof(struct BProto_header_s); |
||
1409 | |||
1410 | struct BProto_uint16_s data; |
||
1411 | data.v = htol16(v); |
||
1412 | memcpy(o->out + o->used, &data, sizeof(data)); |
||
1413 | o->used += sizeof(struct BProto_uint16_s); |
||
1414 | |||
1415 | o->seed_id_count++; |
||
1416 | } |
||
1417 | |||
1418 | uint8_t * msg_seedWriter_Addkey (msg_seedWriter *o, int len) |
||
1419 | { |
||
1420 | ASSERT(o->used >= 0) |
||
1421 | ASSERT(o->key_count == 0) |
||
1422 | ASSERT(len >= 0 && len <= UINT32_MAX) |
||
1423 | |||
1424 | struct BProto_header_s header; |
||
1425 | header.id = htol16(2); |
||
1426 | header.type = htol16(BPROTO_TYPE_DATA); |
||
1427 | memcpy(o->out + o->used, &header, sizeof(header)); |
||
1428 | o->used += sizeof(struct BProto_header_s); |
||
1429 | |||
1430 | struct BProto_data_header_s data; |
||
1431 | data.len = htol32(len); |
||
1432 | memcpy(o->out + o->used, &data, sizeof(data)); |
||
1433 | o->used += sizeof(struct BProto_data_header_s); |
||
1434 | |||
1435 | uint8_t *dest = (o->out + o->used); |
||
1436 | o->used += len; |
||
1437 | |||
1438 | o->key_count++; |
||
1439 | |||
1440 | return dest; |
||
1441 | } |
||
1442 | |||
1443 | uint8_t * msg_seedWriter_Addiv (msg_seedWriter *o, int len) |
||
1444 | { |
||
1445 | ASSERT(o->used >= 0) |
||
1446 | ASSERT(o->iv_count == 0) |
||
1447 | ASSERT(len >= 0 && len <= UINT32_MAX) |
||
1448 | |||
1449 | struct BProto_header_s header; |
||
1450 | header.id = htol16(3); |
||
1451 | header.type = htol16(BPROTO_TYPE_DATA); |
||
1452 | memcpy(o->out + o->used, &header, sizeof(header)); |
||
1453 | o->used += sizeof(struct BProto_header_s); |
||
1454 | |||
1455 | struct BProto_data_header_s data; |
||
1456 | data.len = htol32(len); |
||
1457 | memcpy(o->out + o->used, &data, sizeof(data)); |
||
1458 | o->used += sizeof(struct BProto_data_header_s); |
||
1459 | |||
1460 | uint8_t *dest = (o->out + o->used); |
||
1461 | o->used += len; |
||
1462 | |||
1463 | o->iv_count++; |
||
1464 | |||
1465 | return dest; |
||
1466 | } |
||
1467 | |||
1468 | int msg_seedParser_Init (msg_seedParser *o, uint8_t *buf, int buf_len) |
||
1469 | { |
||
1470 | ASSERT(buf_len >= 0) |
||
1471 | |||
1472 | o->buf = buf; |
||
1473 | o->buf_len = buf_len; |
||
1474 | o->seed_id_start = o->buf_len; |
||
1475 | o->seed_id_span = 0; |
||
1476 | o->seed_id_pos = 0; |
||
1477 | o->key_start = o->buf_len; |
||
1478 | o->key_span = 0; |
||
1479 | o->key_pos = 0; |
||
1480 | o->iv_start = o->buf_len; |
||
1481 | o->iv_span = 0; |
||
1482 | o->iv_pos = 0; |
||
1483 | |||
1484 | int seed_id_count = 0; |
||
1485 | int key_count = 0; |
||
1486 | int iv_count = 0; |
||
1487 | |||
1488 | int pos = 0; |
||
1489 | int left = o->buf_len; |
||
1490 | |||
1491 | while (left > 0) { |
||
1492 | int entry_pos = pos; |
||
1493 | |||
1494 | if (!(left >= sizeof(struct BProto_header_s))) { |
||
1495 | return 0; |
||
1496 | } |
||
1497 | struct BProto_header_s header; |
||
1498 | memcpy(&header, o->buf + pos, sizeof(header)); |
||
1499 | pos += sizeof(struct BProto_header_s); |
||
1500 | left -= sizeof(struct BProto_header_s); |
||
1501 | uint16_t type = ltoh16(header.type); |
||
1502 | uint16_t id = ltoh16(header.id); |
||
1503 | |||
1504 | switch (type) { |
||
1505 | case BPROTO_TYPE_UINT8: { |
||
1506 | if (!(left >= sizeof(struct BProto_uint8_s))) { |
||
1507 | return 0; |
||
1508 | } |
||
1509 | pos += sizeof(struct BProto_uint8_s); |
||
1510 | left -= sizeof(struct BProto_uint8_s); |
||
1511 | |||
1512 | switch (id) { |
||
1513 | default: |
||
1514 | return 0; |
||
1515 | } |
||
1516 | } break; |
||
1517 | case BPROTO_TYPE_UINT16: { |
||
1518 | if (!(left >= sizeof(struct BProto_uint16_s))) { |
||
1519 | return 0; |
||
1520 | } |
||
1521 | pos += sizeof(struct BProto_uint16_s); |
||
1522 | left -= sizeof(struct BProto_uint16_s); |
||
1523 | |||
1524 | switch (id) { |
||
1525 | case 1: |
||
1526 | if (o->seed_id_start == o->buf_len) { |
||
1527 | o->seed_id_start = entry_pos; |
||
1528 | } |
||
1529 | o->seed_id_span = pos - o->seed_id_start; |
||
1530 | seed_id_count++; |
||
1531 | break; |
||
1532 | default: |
||
1533 | return 0; |
||
1534 | } |
||
1535 | } break; |
||
1536 | case BPROTO_TYPE_UINT32: { |
||
1537 | if (!(left >= sizeof(struct BProto_uint32_s))) { |
||
1538 | return 0; |
||
1539 | } |
||
1540 | pos += sizeof(struct BProto_uint32_s); |
||
1541 | left -= sizeof(struct BProto_uint32_s); |
||
1542 | |||
1543 | switch (id) { |
||
1544 | default: |
||
1545 | return 0; |
||
1546 | } |
||
1547 | } break; |
||
1548 | case BPROTO_TYPE_UINT64: { |
||
1549 | if (!(left >= sizeof(struct BProto_uint64_s))) { |
||
1550 | return 0; |
||
1551 | } |
||
1552 | pos += sizeof(struct BProto_uint64_s); |
||
1553 | left -= sizeof(struct BProto_uint64_s); |
||
1554 | |||
1555 | switch (id) { |
||
1556 | default: |
||
1557 | return 0; |
||
1558 | } |
||
1559 | } break; |
||
1560 | case BPROTO_TYPE_DATA: |
||
1561 | case BPROTO_TYPE_CONSTDATA: |
||
1562 | { |
||
1563 | if (!(left >= sizeof(struct BProto_data_header_s))) { |
||
1564 | return 0; |
||
1565 | } |
||
1566 | struct BProto_data_header_s val; |
||
1567 | memcpy(&val, o->buf + pos, sizeof(val)); |
||
1568 | pos += sizeof(struct BProto_data_header_s); |
||
1569 | left -= sizeof(struct BProto_data_header_s); |
||
1570 | |||
1571 | uint32_t payload_len = ltoh32(val.len); |
||
1572 | if (!(left >= payload_len)) { |
||
1573 | return 0; |
||
1574 | } |
||
1575 | pos += payload_len; |
||
1576 | left -= payload_len; |
||
1577 | |||
1578 | switch (id) { |
||
1579 | case 2: |
||
1580 | if (!(type == BPROTO_TYPE_DATA)) { |
||
1581 | return 0; |
||
1582 | } |
||
1583 | if (o->key_start == o->buf_len) { |
||
1584 | o->key_start = entry_pos; |
||
1585 | } |
||
1586 | o->key_span = pos - o->key_start; |
||
1587 | key_count++; |
||
1588 | break; |
||
1589 | case 3: |
||
1590 | if (!(type == BPROTO_TYPE_DATA)) { |
||
1591 | return 0; |
||
1592 | } |
||
1593 | if (o->iv_start == o->buf_len) { |
||
1594 | o->iv_start = entry_pos; |
||
1595 | } |
||
1596 | o->iv_span = pos - o->iv_start; |
||
1597 | iv_count++; |
||
1598 | break; |
||
1599 | default: |
||
1600 | return 0; |
||
1601 | } |
||
1602 | } break; |
||
1603 | default: |
||
1604 | return 0; |
||
1605 | } |
||
1606 | } |
||
1607 | |||
1608 | if (!(seed_id_count == 1)) { |
||
1609 | return 0; |
||
1610 | } |
||
1611 | if (!(key_count == 1)) { |
||
1612 | return 0; |
||
1613 | } |
||
1614 | if (!(iv_count == 1)) { |
||
1615 | return 0; |
||
1616 | } |
||
1617 | |||
1618 | return 1; |
||
1619 | } |
||
1620 | |||
1621 | int msg_seedParser_GotEverything (msg_seedParser *o) |
||
1622 | { |
||
1623 | return ( |
||
1624 | o->seed_id_pos == o->seed_id_span |
||
1625 | && |
||
1626 | o->key_pos == o->key_span |
||
1627 | && |
||
1628 | o->iv_pos == o->iv_span |
||
1629 | ); |
||
1630 | } |
||
1631 | |||
1632 | int msg_seedParser_Getseed_id (msg_seedParser *o, uint16_t *v) |
||
1633 | { |
||
1634 | ASSERT(o->seed_id_pos >= 0) |
||
1635 | ASSERT(o->seed_id_pos <= o->seed_id_span) |
||
1636 | |||
1637 | int left = o->seed_id_span - o->seed_id_pos; |
||
1638 | |||
1639 | while (left > 0) { |
||
1640 | ASSERT(left >= sizeof(struct BProto_header_s)) |
||
1641 | struct BProto_header_s header; |
||
1642 | memcpy(&header, o->buf + o->seed_id_start + o->seed_id_pos, sizeof(header)); |
||
1643 | o->seed_id_pos += sizeof(struct BProto_header_s); |
||
1644 | left -= sizeof(struct BProto_header_s); |
||
1645 | uint16_t type = ltoh16(header.type); |
||
1646 | uint16_t id = ltoh16(header.id); |
||
1647 | |||
1648 | switch (type) { |
||
1649 | case BPROTO_TYPE_UINT8: { |
||
1650 | ASSERT(left >= sizeof(struct BProto_uint8_s)) |
||
1651 | o->seed_id_pos += sizeof(struct BProto_uint8_s); |
||
1652 | left -= sizeof(struct BProto_uint8_s); |
||
1653 | } break; |
||
1654 | case BPROTO_TYPE_UINT16: { |
||
1655 | ASSERT(left >= sizeof(struct BProto_uint16_s)) |
||
1656 | struct BProto_uint16_s val; |
||
1657 | memcpy(&val, o->buf + o->seed_id_start + o->seed_id_pos, sizeof(val)); |
||
1658 | o->seed_id_pos += sizeof(struct BProto_uint16_s); |
||
1659 | left -= sizeof(struct BProto_uint16_s); |
||
1660 | |||
1661 | if (id == 1) { |
||
1662 | *v = ltoh16(val.v); |
||
1663 | return 1; |
||
1664 | } |
||
1665 | } break; |
||
1666 | case BPROTO_TYPE_UINT32: { |
||
1667 | ASSERT(left >= sizeof(struct BProto_uint32_s)) |
||
1668 | o->seed_id_pos += sizeof(struct BProto_uint32_s); |
||
1669 | left -= sizeof(struct BProto_uint32_s); |
||
1670 | } break; |
||
1671 | case BPROTO_TYPE_UINT64: { |
||
1672 | ASSERT(left >= sizeof(struct BProto_uint64_s)) |
||
1673 | o->seed_id_pos += sizeof(struct BProto_uint64_s); |
||
1674 | left -= sizeof(struct BProto_uint64_s); |
||
1675 | } break; |
||
1676 | case BPROTO_TYPE_DATA: |
||
1677 | case BPROTO_TYPE_CONSTDATA: |
||
1678 | { |
||
1679 | ASSERT(left >= sizeof(struct BProto_data_header_s)) |
||
1680 | struct BProto_data_header_s val; |
||
1681 | memcpy(&val, o->buf + o->seed_id_start + o->seed_id_pos, sizeof(val)); |
||
1682 | o->seed_id_pos += sizeof(struct BProto_data_header_s); |
||
1683 | left -= sizeof(struct BProto_data_header_s); |
||
1684 | |||
1685 | uint32_t payload_len = ltoh32(val.len); |
||
1686 | ASSERT(left >= payload_len) |
||
1687 | o->seed_id_pos += payload_len; |
||
1688 | left -= payload_len; |
||
1689 | } break; |
||
1690 | default: |
||
1691 | ASSERT(0); |
||
1692 | } |
||
1693 | } |
||
1694 | |||
1695 | return 0; |
||
1696 | } |
||
1697 | |||
1698 | void msg_seedParser_Resetseed_id (msg_seedParser *o) |
||
1699 | { |
||
1700 | o->seed_id_pos = 0; |
||
1701 | } |
||
1702 | |||
1703 | void msg_seedParser_Forwardseed_id (msg_seedParser *o) |
||
1704 | { |
||
1705 | o->seed_id_pos = o->seed_id_span; |
||
1706 | } |
||
1707 | |||
1708 | int msg_seedParser_Getkey (msg_seedParser *o, uint8_t **data, int *data_len) |
||
1709 | { |
||
1710 | ASSERT(o->key_pos >= 0) |
||
1711 | ASSERT(o->key_pos <= o->key_span) |
||
1712 | |||
1713 | int left = o->key_span - o->key_pos; |
||
1714 | |||
1715 | while (left > 0) { |
||
1716 | ASSERT(left >= sizeof(struct BProto_header_s)) |
||
1717 | struct BProto_header_s header; |
||
1718 | memcpy(&header, o->buf + o->key_start + o->key_pos, sizeof(header)); |
||
1719 | o->key_pos += sizeof(struct BProto_header_s); |
||
1720 | left -= sizeof(struct BProto_header_s); |
||
1721 | uint16_t type = ltoh16(header.type); |
||
1722 | uint16_t id = ltoh16(header.id); |
||
1723 | |||
1724 | switch (type) { |
||
1725 | case BPROTO_TYPE_UINT8: { |
||
1726 | ASSERT(left >= sizeof(struct BProto_uint8_s)) |
||
1727 | o->key_pos += sizeof(struct BProto_uint8_s); |
||
1728 | left -= sizeof(struct BProto_uint8_s); |
||
1729 | } break; |
||
1730 | case BPROTO_TYPE_UINT16: { |
||
1731 | ASSERT(left >= sizeof(struct BProto_uint16_s)) |
||
1732 | o->key_pos += sizeof(struct BProto_uint16_s); |
||
1733 | left -= sizeof(struct BProto_uint16_s); |
||
1734 | } break; |
||
1735 | case BPROTO_TYPE_UINT32: { |
||
1736 | ASSERT(left >= sizeof(struct BProto_uint32_s)) |
||
1737 | o->key_pos += sizeof(struct BProto_uint32_s); |
||
1738 | left -= sizeof(struct BProto_uint32_s); |
||
1739 | } break; |
||
1740 | case BPROTO_TYPE_UINT64: { |
||
1741 | ASSERT(left >= sizeof(struct BProto_uint64_s)) |
||
1742 | o->key_pos += sizeof(struct BProto_uint64_s); |
||
1743 | left -= sizeof(struct BProto_uint64_s); |
||
1744 | } break; |
||
1745 | case BPROTO_TYPE_DATA: |
||
1746 | case BPROTO_TYPE_CONSTDATA: |
||
1747 | { |
||
1748 | ASSERT(left >= sizeof(struct BProto_data_header_s)) |
||
1749 | struct BProto_data_header_s val; |
||
1750 | memcpy(&val, o->buf + o->key_start + o->key_pos, sizeof(val)); |
||
1751 | o->key_pos += sizeof(struct BProto_data_header_s); |
||
1752 | left -= sizeof(struct BProto_data_header_s); |
||
1753 | |||
1754 | uint32_t payload_len = ltoh32(val.len); |
||
1755 | ASSERT(left >= payload_len) |
||
1756 | uint8_t *payload = o->buf + o->key_start + o->key_pos; |
||
1757 | o->key_pos += payload_len; |
||
1758 | left -= payload_len; |
||
1759 | |||
1760 | if (type == BPROTO_TYPE_DATA && id == 2) { |
||
1761 | *data = payload; |
||
1762 | *data_len = payload_len; |
||
1763 | return 1; |
||
1764 | } |
||
1765 | } break; |
||
1766 | default: |
||
1767 | ASSERT(0); |
||
1768 | } |
||
1769 | } |
||
1770 | |||
1771 | return 0; |
||
1772 | } |
||
1773 | |||
1774 | void msg_seedParser_Resetkey (msg_seedParser *o) |
||
1775 | { |
||
1776 | o->key_pos = 0; |
||
1777 | } |
||
1778 | |||
1779 | void msg_seedParser_Forwardkey (msg_seedParser *o) |
||
1780 | { |
||
1781 | o->key_pos = o->key_span; |
||
1782 | } |
||
1783 | |||
1784 | int msg_seedParser_Getiv (msg_seedParser *o, uint8_t **data, int *data_len) |
||
1785 | { |
||
1786 | ASSERT(o->iv_pos >= 0) |
||
1787 | ASSERT(o->iv_pos <= o->iv_span) |
||
1788 | |||
1789 | int left = o->iv_span - o->iv_pos; |
||
1790 | |||
1791 | while (left > 0) { |
||
1792 | ASSERT(left >= sizeof(struct BProto_header_s)) |
||
1793 | struct BProto_header_s header; |
||
1794 | memcpy(&header, o->buf + o->iv_start + o->iv_pos, sizeof(header)); |
||
1795 | o->iv_pos += sizeof(struct BProto_header_s); |
||
1796 | left -= sizeof(struct BProto_header_s); |
||
1797 | uint16_t type = ltoh16(header.type); |
||
1798 | uint16_t id = ltoh16(header.id); |
||
1799 | |||
1800 | switch (type) { |
||
1801 | case BPROTO_TYPE_UINT8: { |
||
1802 | ASSERT(left >= sizeof(struct BProto_uint8_s)) |
||
1803 | o->iv_pos += sizeof(struct BProto_uint8_s); |
||
1804 | left -= sizeof(struct BProto_uint8_s); |
||
1805 | } break; |
||
1806 | case BPROTO_TYPE_UINT16: { |
||
1807 | ASSERT(left >= sizeof(struct BProto_uint16_s)) |
||
1808 | o->iv_pos += sizeof(struct BProto_uint16_s); |
||
1809 | left -= sizeof(struct BProto_uint16_s); |
||
1810 | } break; |
||
1811 | case BPROTO_TYPE_UINT32: { |
||
1812 | ASSERT(left >= sizeof(struct BProto_uint32_s)) |
||
1813 | o->iv_pos += sizeof(struct BProto_uint32_s); |
||
1814 | left -= sizeof(struct BProto_uint32_s); |
||
1815 | } break; |
||
1816 | case BPROTO_TYPE_UINT64: { |
||
1817 | ASSERT(left >= sizeof(struct BProto_uint64_s)) |
||
1818 | o->iv_pos += sizeof(struct BProto_uint64_s); |
||
1819 | left -= sizeof(struct BProto_uint64_s); |
||
1820 | } break; |
||
1821 | case BPROTO_TYPE_DATA: |
||
1822 | case BPROTO_TYPE_CONSTDATA: |
||
1823 | { |
||
1824 | ASSERT(left >= sizeof(struct BProto_data_header_s)) |
||
1825 | struct BProto_data_header_s val; |
||
1826 | memcpy(&val, o->buf + o->iv_start + o->iv_pos, sizeof(val)); |
||
1827 | o->iv_pos += sizeof(struct BProto_data_header_s); |
||
1828 | left -= sizeof(struct BProto_data_header_s); |
||
1829 | |||
1830 | uint32_t payload_len = ltoh32(val.len); |
||
1831 | ASSERT(left >= payload_len) |
||
1832 | uint8_t *payload = o->buf + o->iv_start + o->iv_pos; |
||
1833 | o->iv_pos += payload_len; |
||
1834 | left -= payload_len; |
||
1835 | |||
1836 | if (type == BPROTO_TYPE_DATA && id == 3) { |
||
1837 | *data = payload; |
||
1838 | *data_len = payload_len; |
||
1839 | return 1; |
||
1840 | } |
||
1841 | } break; |
||
1842 | default: |
||
1843 | ASSERT(0); |
||
1844 | } |
||
1845 | } |
||
1846 | |||
1847 | return 0; |
||
1848 | } |
||
1849 | |||
1850 | void msg_seedParser_Resetiv (msg_seedParser *o) |
||
1851 | { |
||
1852 | o->iv_pos = 0; |
||
1853 | } |
||
1854 | |||
1855 | void msg_seedParser_Forwardiv (msg_seedParser *o) |
||
1856 | { |
||
1857 | o->iv_pos = o->iv_span; |
||
1858 | } |
||
1859 | |||
1860 | #define msg_confirmseed_SIZEseed_id (sizeof(struct BProto_header_s) + sizeof(struct BProto_uint16_s)) |
||
1861 | |||
1862 | typedef struct { |
||
1863 | uint8_t *out; |
||
1864 | int used; |
||
1865 | int seed_id_count; |
||
1866 | } msg_confirmseedWriter; |
||
1867 | |||
1868 | static void msg_confirmseedWriter_Init (msg_confirmseedWriter *o, uint8_t *out); |
||
1869 | static int msg_confirmseedWriter_Finish (msg_confirmseedWriter *o); |
||
1870 | static void msg_confirmseedWriter_Addseed_id (msg_confirmseedWriter *o, uint16_t v); |
||
1871 | |||
1872 | typedef struct { |
||
1873 | uint8_t *buf; |
||
1874 | int buf_len; |
||
1875 | int seed_id_start; |
||
1876 | int seed_id_span; |
||
1877 | int seed_id_pos; |
||
1878 | } msg_confirmseedParser; |
||
1879 | |||
1880 | static int msg_confirmseedParser_Init (msg_confirmseedParser *o, uint8_t *buf, int buf_len); |
||
1881 | static int msg_confirmseedParser_GotEverything (msg_confirmseedParser *o); |
||
1882 | static int msg_confirmseedParser_Getseed_id (msg_confirmseedParser *o, uint16_t *v); |
||
1883 | static void msg_confirmseedParser_Resetseed_id (msg_confirmseedParser *o); |
||
1884 | static void msg_confirmseedParser_Forwardseed_id (msg_confirmseedParser *o); |
||
1885 | |||
1886 | void msg_confirmseedWriter_Init (msg_confirmseedWriter *o, uint8_t *out) |
||
1887 | { |
||
1888 | o->out = out; |
||
1889 | o->used = 0; |
||
1890 | o->seed_id_count = 0; |
||
1891 | } |
||
1892 | |||
1893 | int msg_confirmseedWriter_Finish (msg_confirmseedWriter *o) |
||
1894 | { |
||
1895 | ASSERT(o->used >= 0) |
||
1896 | ASSERT(o->seed_id_count == 1) |
||
1897 | |||
1898 | return o->used; |
||
1899 | } |
||
1900 | |||
1901 | void msg_confirmseedWriter_Addseed_id (msg_confirmseedWriter *o, uint16_t v) |
||
1902 | { |
||
1903 | ASSERT(o->used >= 0) |
||
1904 | ASSERT(o->seed_id_count == 0) |
||
1905 | |||
1906 | |||
1907 | struct BProto_header_s header; |
||
1908 | header.id = htol16(1); |
||
1909 | header.type = htol16(BPROTO_TYPE_UINT16); |
||
1910 | memcpy(o->out + o->used, &header, sizeof(header)); |
||
1911 | o->used += sizeof(struct BProto_header_s); |
||
1912 | |||
1913 | struct BProto_uint16_s data; |
||
1914 | data.v = htol16(v); |
||
1915 | memcpy(o->out + o->used, &data, sizeof(data)); |
||
1916 | o->used += sizeof(struct BProto_uint16_s); |
||
1917 | |||
1918 | o->seed_id_count++; |
||
1919 | } |
||
1920 | |||
1921 | int msg_confirmseedParser_Init (msg_confirmseedParser *o, uint8_t *buf, int buf_len) |
||
1922 | { |
||
1923 | ASSERT(buf_len >= 0) |
||
1924 | |||
1925 | o->buf = buf; |
||
1926 | o->buf_len = buf_len; |
||
1927 | o->seed_id_start = o->buf_len; |
||
1928 | o->seed_id_span = 0; |
||
1929 | o->seed_id_pos = 0; |
||
1930 | |||
1931 | int seed_id_count = 0; |
||
1932 | |||
1933 | int pos = 0; |
||
1934 | int left = o->buf_len; |
||
1935 | |||
1936 | while (left > 0) { |
||
1937 | int entry_pos = pos; |
||
1938 | |||
1939 | if (!(left >= sizeof(struct BProto_header_s))) { |
||
1940 | return 0; |
||
1941 | } |
||
1942 | struct BProto_header_s header; |
||
1943 | memcpy(&header, o->buf + pos, sizeof(header)); |
||
1944 | pos += sizeof(struct BProto_header_s); |
||
1945 | left -= sizeof(struct BProto_header_s); |
||
1946 | uint16_t type = ltoh16(header.type); |
||
1947 | uint16_t id = ltoh16(header.id); |
||
1948 | |||
1949 | switch (type) { |
||
1950 | case BPROTO_TYPE_UINT8: { |
||
1951 | if (!(left >= sizeof(struct BProto_uint8_s))) { |
||
1952 | return 0; |
||
1953 | } |
||
1954 | pos += sizeof(struct BProto_uint8_s); |
||
1955 | left -= sizeof(struct BProto_uint8_s); |
||
1956 | |||
1957 | switch (id) { |
||
1958 | default: |
||
1959 | return 0; |
||
1960 | } |
||
1961 | } break; |
||
1962 | case BPROTO_TYPE_UINT16: { |
||
1963 | if (!(left >= sizeof(struct BProto_uint16_s))) { |
||
1964 | return 0; |
||
1965 | } |
||
1966 | pos += sizeof(struct BProto_uint16_s); |
||
1967 | left -= sizeof(struct BProto_uint16_s); |
||
1968 | |||
1969 | switch (id) { |
||
1970 | case 1: |
||
1971 | if (o->seed_id_start == o->buf_len) { |
||
1972 | o->seed_id_start = entry_pos; |
||
1973 | } |
||
1974 | o->seed_id_span = pos - o->seed_id_start; |
||
1975 | seed_id_count++; |
||
1976 | break; |
||
1977 | default: |
||
1978 | return 0; |
||
1979 | } |
||
1980 | } break; |
||
1981 | case BPROTO_TYPE_UINT32: { |
||
1982 | if (!(left >= sizeof(struct BProto_uint32_s))) { |
||
1983 | return 0; |
||
1984 | } |
||
1985 | pos += sizeof(struct BProto_uint32_s); |
||
1986 | left -= sizeof(struct BProto_uint32_s); |
||
1987 | |||
1988 | switch (id) { |
||
1989 | default: |
||
1990 | return 0; |
||
1991 | } |
||
1992 | } break; |
||
1993 | case BPROTO_TYPE_UINT64: { |
||
1994 | if (!(left >= sizeof(struct BProto_uint64_s))) { |
||
1995 | return 0; |
||
1996 | } |
||
1997 | pos += sizeof(struct BProto_uint64_s); |
||
1998 | left -= sizeof(struct BProto_uint64_s); |
||
1999 | |||
2000 | switch (id) { |
||
2001 | default: |
||
2002 | return 0; |
||
2003 | } |
||
2004 | } break; |
||
2005 | case BPROTO_TYPE_DATA: |
||
2006 | case BPROTO_TYPE_CONSTDATA: |
||
2007 | { |
||
2008 | if (!(left >= sizeof(struct BProto_data_header_s))) { |
||
2009 | return 0; |
||
2010 | } |
||
2011 | struct BProto_data_header_s val; |
||
2012 | memcpy(&val, o->buf + pos, sizeof(val)); |
||
2013 | pos += sizeof(struct BProto_data_header_s); |
||
2014 | left -= sizeof(struct BProto_data_header_s); |
||
2015 | |||
2016 | uint32_t payload_len = ltoh32(val.len); |
||
2017 | if (!(left >= payload_len)) { |
||
2018 | return 0; |
||
2019 | } |
||
2020 | pos += payload_len; |
||
2021 | left -= payload_len; |
||
2022 | |||
2023 | switch (id) { |
||
2024 | default: |
||
2025 | return 0; |
||
2026 | } |
||
2027 | } break; |
||
2028 | default: |
||
2029 | return 0; |
||
2030 | } |
||
2031 | } |
||
2032 | |||
2033 | if (!(seed_id_count == 1)) { |
||
2034 | return 0; |
||
2035 | } |
||
2036 | |||
2037 | return 1; |
||
2038 | } |
||
2039 | |||
2040 | int msg_confirmseedParser_GotEverything (msg_confirmseedParser *o) |
||
2041 | { |
||
2042 | return ( |
||
2043 | o->seed_id_pos == o->seed_id_span |
||
2044 | ); |
||
2045 | } |
||
2046 | |||
2047 | int msg_confirmseedParser_Getseed_id (msg_confirmseedParser *o, uint16_t *v) |
||
2048 | { |
||
2049 | ASSERT(o->seed_id_pos >= 0) |
||
2050 | ASSERT(o->seed_id_pos <= o->seed_id_span) |
||
2051 | |||
2052 | int left = o->seed_id_span - o->seed_id_pos; |
||
2053 | |||
2054 | while (left > 0) { |
||
2055 | ASSERT(left >= sizeof(struct BProto_header_s)) |
||
2056 | struct BProto_header_s header; |
||
2057 | memcpy(&header, o->buf + o->seed_id_start + o->seed_id_pos, sizeof(header)); |
||
2058 | o->seed_id_pos += sizeof(struct BProto_header_s); |
||
2059 | left -= sizeof(struct BProto_header_s); |
||
2060 | uint16_t type = ltoh16(header.type); |
||
2061 | uint16_t id = ltoh16(header.id); |
||
2062 | |||
2063 | switch (type) { |
||
2064 | case BPROTO_TYPE_UINT8: { |
||
2065 | ASSERT(left >= sizeof(struct BProto_uint8_s)) |
||
2066 | o->seed_id_pos += sizeof(struct BProto_uint8_s); |
||
2067 | left -= sizeof(struct BProto_uint8_s); |
||
2068 | } break; |
||
2069 | case BPROTO_TYPE_UINT16: { |
||
2070 | ASSERT(left >= sizeof(struct BProto_uint16_s)) |
||
2071 | struct BProto_uint16_s val; |
||
2072 | memcpy(&val, o->buf + o->seed_id_start + o->seed_id_pos, sizeof(val)); |
||
2073 | o->seed_id_pos += sizeof(struct BProto_uint16_s); |
||
2074 | left -= sizeof(struct BProto_uint16_s); |
||
2075 | |||
2076 | if (id == 1) { |
||
2077 | *v = ltoh16(val.v); |
||
2078 | return 1; |
||
2079 | } |
||
2080 | } break; |
||
2081 | case BPROTO_TYPE_UINT32: { |
||
2082 | ASSERT(left >= sizeof(struct BProto_uint32_s)) |
||
2083 | o->seed_id_pos += sizeof(struct BProto_uint32_s); |
||
2084 | left -= sizeof(struct BProto_uint32_s); |
||
2085 | } break; |
||
2086 | case BPROTO_TYPE_UINT64: { |
||
2087 | ASSERT(left >= sizeof(struct BProto_uint64_s)) |
||
2088 | o->seed_id_pos += sizeof(struct BProto_uint64_s); |
||
2089 | left -= sizeof(struct BProto_uint64_s); |
||
2090 | } break; |
||
2091 | case BPROTO_TYPE_DATA: |
||
2092 | case BPROTO_TYPE_CONSTDATA: |
||
2093 | { |
||
2094 | ASSERT(left >= sizeof(struct BProto_data_header_s)) |
||
2095 | struct BProto_data_header_s val; |
||
2096 | memcpy(&val, o->buf + o->seed_id_start + o->seed_id_pos, sizeof(val)); |
||
2097 | o->seed_id_pos += sizeof(struct BProto_data_header_s); |
||
2098 | left -= sizeof(struct BProto_data_header_s); |
||
2099 | |||
2100 | uint32_t payload_len = ltoh32(val.len); |
||
2101 | ASSERT(left >= payload_len) |
||
2102 | o->seed_id_pos += payload_len; |
||
2103 | left -= payload_len; |
||
2104 | } break; |
||
2105 | default: |
||
2106 | ASSERT(0); |
||
2107 | } |
||
2108 | } |
||
2109 | |||
2110 | return 0; |
||
2111 | } |
||
2112 | |||
2113 | void msg_confirmseedParser_Resetseed_id (msg_confirmseedParser *o) |
||
2114 | { |
||
2115 | o->seed_id_pos = 0; |
||
2116 | } |
||
2117 | |||
2118 | void msg_confirmseedParser_Forwardseed_id (msg_confirmseedParser *o) |
||
2119 | { |
||
2120 | o->seed_id_pos = o->seed_id_span; |
||
2121 | } |
||
2122 |