bproto_bproto_test.h 34 KB

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