bproto_bproto_test.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  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. pos += sizeof(struct BProto_uint8_s);
  241. left -= sizeof(struct BProto_uint8_s);
  242. switch (id) {
  243. case 9:
  244. if (o->e_start == o->buf_len) {
  245. o->e_start = entry_pos;
  246. }
  247. o->e_span = pos - o->e_start;
  248. e_count++;
  249. break;
  250. default:
  251. return 0;
  252. }
  253. } break;
  254. case BPROTO_TYPE_UINT16: {
  255. if (!(left >= sizeof(struct BProto_uint16_s))) {
  256. return 0;
  257. }
  258. pos += sizeof(struct BProto_uint16_s);
  259. left -= sizeof(struct BProto_uint16_s);
  260. switch (id) {
  261. case 5:
  262. if (o->a_start == o->buf_len) {
  263. o->a_start = entry_pos;
  264. }
  265. o->a_span = pos - o->a_start;
  266. a_count++;
  267. break;
  268. case 8:
  269. if (o->d_start == o->buf_len) {
  270. o->d_start = entry_pos;
  271. }
  272. o->d_span = pos - o->d_start;
  273. d_count++;
  274. break;
  275. default:
  276. return 0;
  277. }
  278. } break;
  279. case BPROTO_TYPE_UINT32: {
  280. if (!(left >= sizeof(struct BProto_uint32_s))) {
  281. return 0;
  282. }
  283. pos += sizeof(struct BProto_uint32_s);
  284. left -= sizeof(struct BProto_uint32_s);
  285. switch (id) {
  286. case 6:
  287. if (o->b_start == o->buf_len) {
  288. o->b_start = entry_pos;
  289. }
  290. o->b_span = pos - o->b_start;
  291. b_count++;
  292. break;
  293. default:
  294. return 0;
  295. }
  296. } break;
  297. case BPROTO_TYPE_UINT64: {
  298. if (!(left >= sizeof(struct BProto_uint64_s))) {
  299. return 0;
  300. }
  301. pos += sizeof(struct BProto_uint64_s);
  302. left -= sizeof(struct BProto_uint64_s);
  303. switch (id) {
  304. case 7:
  305. if (o->c_start == o->buf_len) {
  306. o->c_start = entry_pos;
  307. }
  308. o->c_span = pos - o->c_start;
  309. c_count++;
  310. break;
  311. default:
  312. return 0;
  313. }
  314. } break;
  315. case BPROTO_TYPE_DATA:
  316. case BPROTO_TYPE_CONSTDATA:
  317. {
  318. if (!(left >= sizeof(struct BProto_data_header_s))) {
  319. return 0;
  320. }
  321. struct BProto_data_header_s *val = (struct BProto_data_header_s *)(o->buf + pos);
  322. pos += sizeof(struct BProto_data_header_s);
  323. left -= sizeof(struct BProto_data_header_s);
  324. uint32_t payload_len = ltoh32(val->len);
  325. if (!(left >= payload_len)) {
  326. return 0;
  327. }
  328. pos += payload_len;
  329. left -= payload_len;
  330. switch (id) {
  331. case 10:
  332. if (!(type == BPROTO_TYPE_DATA)) {
  333. return 0;
  334. }
  335. if (o->f_start == o->buf_len) {
  336. o->f_start = entry_pos;
  337. }
  338. o->f_span = pos - o->f_start;
  339. f_count++;
  340. break;
  341. case 11:
  342. if (!(type == BPROTO_TYPE_CONSTDATA)) {
  343. return 0;
  344. }
  345. if (!(payload_len == (4))) {
  346. return 0;
  347. }
  348. if (o->g_start == o->buf_len) {
  349. o->g_start = entry_pos;
  350. }
  351. o->g_span = pos - o->g_start;
  352. g_count++;
  353. break;
  354. default:
  355. return 0;
  356. }
  357. } break;
  358. default:
  359. return 0;
  360. }
  361. }
  362. if (!(a_count == 1)) {
  363. return 0;
  364. }
  365. if (!(b_count <= 1)) {
  366. return 0;
  367. }
  368. if (!(c_count >= 1)) {
  369. return 0;
  370. }
  371. if (!(e_count == 1)) {
  372. return 0;
  373. }
  374. if (!(f_count == 1)) {
  375. return 0;
  376. }
  377. if (!(g_count == 1)) {
  378. return 0;
  379. }
  380. return 1;
  381. }
  382. int msg1Parser_GotEverything (msg1Parser *o)
  383. {
  384. return (
  385. o->a_pos == o->a_span
  386. &&
  387. o->b_pos == o->b_span
  388. &&
  389. o->c_pos == o->c_span
  390. &&
  391. o->d_pos == o->d_span
  392. &&
  393. o->e_pos == o->e_span
  394. &&
  395. o->f_pos == o->f_span
  396. &&
  397. o->g_pos == o->g_span
  398. );
  399. }
  400. int msg1Parser_Geta (msg1Parser *o, uint16_t *v)
  401. {
  402. ASSERT(o->a_pos >= 0)
  403. ASSERT(o->a_pos <= o->a_span)
  404. int left = o->a_span - o->a_pos;
  405. while (left > 0) {
  406. ASSERT(left >= sizeof(struct BProto_header_s))
  407. struct BProto_header_s *header = (struct BProto_header_s *)(o->buf + o->a_start + o->a_pos);
  408. o->a_pos += sizeof(struct BProto_header_s);
  409. left -= sizeof(struct BProto_header_s);
  410. uint16_t type = ltoh16(header->type);
  411. uint16_t id = ltoh16(header->id);
  412. switch (type) {
  413. case BPROTO_TYPE_UINT8: {
  414. ASSERT(left >= sizeof(struct BProto_uint8_s))
  415. o->a_pos += sizeof(struct BProto_uint8_s);
  416. left -= sizeof(struct BProto_uint8_s);
  417. } break;
  418. case BPROTO_TYPE_UINT16: {
  419. ASSERT(left >= sizeof(struct BProto_uint16_s))
  420. struct BProto_uint16_s *val = (struct BProto_uint16_s *)(o->buf + o->a_start + o->a_pos);
  421. o->a_pos += sizeof(struct BProto_uint16_s);
  422. left -= sizeof(struct BProto_uint16_s);
  423. if (id == 5) {
  424. *v = ltoh16(val->v);
  425. return 1;
  426. }
  427. } break;
  428. case BPROTO_TYPE_UINT32: {
  429. ASSERT(left >= sizeof(struct BProto_uint32_s))
  430. o->a_pos += sizeof(struct BProto_uint32_s);
  431. left -= sizeof(struct BProto_uint32_s);
  432. } break;
  433. case BPROTO_TYPE_UINT64: {
  434. ASSERT(left >= sizeof(struct BProto_uint64_s))
  435. o->a_pos += sizeof(struct BProto_uint64_s);
  436. left -= sizeof(struct BProto_uint64_s);
  437. } break;
  438. case BPROTO_TYPE_DATA:
  439. case BPROTO_TYPE_CONSTDATA:
  440. {
  441. ASSERT(left >= sizeof(struct BProto_data_header_s))
  442. struct BProto_data_header_s *val = (struct BProto_data_header_s *)(o->buf + o->a_start + o->a_pos);
  443. o->a_pos += sizeof(struct BProto_data_header_s);
  444. left -= sizeof(struct BProto_data_header_s);
  445. uint32_t payload_len = ltoh32(val->len);
  446. ASSERT(left >= payload_len)
  447. o->a_pos += payload_len;
  448. left -= payload_len;
  449. } break;
  450. default:
  451. ASSERT(0);
  452. }
  453. }
  454. return 0;
  455. }
  456. void msg1Parser_Reseta (msg1Parser *o)
  457. {
  458. o->a_pos = 0;
  459. }
  460. void msg1Parser_Forwarda (msg1Parser *o)
  461. {
  462. o->a_pos = o->a_span;
  463. }
  464. int msg1Parser_Getb (msg1Parser *o, uint32_t *v)
  465. {
  466. ASSERT(o->b_pos >= 0)
  467. ASSERT(o->b_pos <= o->b_span)
  468. int left = o->b_span - o->b_pos;
  469. while (left > 0) {
  470. ASSERT(left >= sizeof(struct BProto_header_s))
  471. struct BProto_header_s *header = (struct BProto_header_s *)(o->buf + o->b_start + o->b_pos);
  472. o->b_pos += sizeof(struct BProto_header_s);
  473. left -= sizeof(struct BProto_header_s);
  474. uint16_t type = ltoh16(header->type);
  475. uint16_t id = ltoh16(header->id);
  476. switch (type) {
  477. case BPROTO_TYPE_UINT8: {
  478. ASSERT(left >= sizeof(struct BProto_uint8_s))
  479. o->b_pos += sizeof(struct BProto_uint8_s);
  480. left -= sizeof(struct BProto_uint8_s);
  481. } break;
  482. case BPROTO_TYPE_UINT16: {
  483. ASSERT(left >= sizeof(struct BProto_uint16_s))
  484. o->b_pos += sizeof(struct BProto_uint16_s);
  485. left -= sizeof(struct BProto_uint16_s);
  486. } break;
  487. case BPROTO_TYPE_UINT32: {
  488. ASSERT(left >= sizeof(struct BProto_uint32_s))
  489. struct BProto_uint32_s *val = (struct BProto_uint32_s *)(o->buf + o->b_start + o->b_pos);
  490. o->b_pos += sizeof(struct BProto_uint32_s);
  491. left -= sizeof(struct BProto_uint32_s);
  492. if (id == 6) {
  493. *v = ltoh32(val->v);
  494. return 1;
  495. }
  496. } break;
  497. case BPROTO_TYPE_UINT64: {
  498. ASSERT(left >= sizeof(struct BProto_uint64_s))
  499. o->b_pos += sizeof(struct BProto_uint64_s);
  500. left -= sizeof(struct BProto_uint64_s);
  501. } break;
  502. case BPROTO_TYPE_DATA:
  503. case BPROTO_TYPE_CONSTDATA:
  504. {
  505. ASSERT(left >= sizeof(struct BProto_data_header_s))
  506. struct BProto_data_header_s *val = (struct BProto_data_header_s *)(o->buf + o->b_start + o->b_pos);
  507. o->b_pos += sizeof(struct BProto_data_header_s);
  508. left -= sizeof(struct BProto_data_header_s);
  509. uint32_t payload_len = ltoh32(val->len);
  510. ASSERT(left >= payload_len)
  511. o->b_pos += payload_len;
  512. left -= payload_len;
  513. } break;
  514. default:
  515. ASSERT(0);
  516. }
  517. }
  518. return 0;
  519. }
  520. void msg1Parser_Resetb (msg1Parser *o)
  521. {
  522. o->b_pos = 0;
  523. }
  524. void msg1Parser_Forwardb (msg1Parser *o)
  525. {
  526. o->b_pos = o->b_span;
  527. }
  528. int msg1Parser_Getc (msg1Parser *o, uint64_t *v)
  529. {
  530. ASSERT(o->c_pos >= 0)
  531. ASSERT(o->c_pos <= o->c_span)
  532. int left = o->c_span - o->c_pos;
  533. while (left > 0) {
  534. ASSERT(left >= sizeof(struct BProto_header_s))
  535. struct BProto_header_s *header = (struct BProto_header_s *)(o->buf + o->c_start + o->c_pos);
  536. o->c_pos += sizeof(struct BProto_header_s);
  537. left -= sizeof(struct BProto_header_s);
  538. uint16_t type = ltoh16(header->type);
  539. uint16_t id = ltoh16(header->id);
  540. switch (type) {
  541. case BPROTO_TYPE_UINT8: {
  542. ASSERT(left >= sizeof(struct BProto_uint8_s))
  543. o->c_pos += sizeof(struct BProto_uint8_s);
  544. left -= sizeof(struct BProto_uint8_s);
  545. } break;
  546. case BPROTO_TYPE_UINT16: {
  547. ASSERT(left >= sizeof(struct BProto_uint16_s))
  548. o->c_pos += sizeof(struct BProto_uint16_s);
  549. left -= sizeof(struct BProto_uint16_s);
  550. } break;
  551. case BPROTO_TYPE_UINT32: {
  552. ASSERT(left >= sizeof(struct BProto_uint32_s))
  553. o->c_pos += sizeof(struct BProto_uint32_s);
  554. left -= sizeof(struct BProto_uint32_s);
  555. } break;
  556. case BPROTO_TYPE_UINT64: {
  557. ASSERT(left >= sizeof(struct BProto_uint64_s))
  558. struct BProto_uint64_s *val = (struct BProto_uint64_s *)(o->buf + o->c_start + o->c_pos);
  559. o->c_pos += sizeof(struct BProto_uint64_s);
  560. left -= sizeof(struct BProto_uint64_s);
  561. if (id == 7) {
  562. *v = ltoh64(val->v);
  563. return 1;
  564. }
  565. } break;
  566. case BPROTO_TYPE_DATA:
  567. case BPROTO_TYPE_CONSTDATA:
  568. {
  569. ASSERT(left >= sizeof(struct BProto_data_header_s))
  570. struct BProto_data_header_s *val = (struct BProto_data_header_s *)(o->buf + o->c_start + o->c_pos);
  571. o->c_pos += sizeof(struct BProto_data_header_s);
  572. left -= sizeof(struct BProto_data_header_s);
  573. uint32_t payload_len = ltoh32(val->len);
  574. ASSERT(left >= payload_len)
  575. o->c_pos += payload_len;
  576. left -= payload_len;
  577. } break;
  578. default:
  579. ASSERT(0);
  580. }
  581. }
  582. return 0;
  583. }
  584. void msg1Parser_Resetc (msg1Parser *o)
  585. {
  586. o->c_pos = 0;
  587. }
  588. void msg1Parser_Forwardc (msg1Parser *o)
  589. {
  590. o->c_pos = o->c_span;
  591. }
  592. int msg1Parser_Getd (msg1Parser *o, uint16_t *v)
  593. {
  594. ASSERT(o->d_pos >= 0)
  595. ASSERT(o->d_pos <= o->d_span)
  596. int left = o->d_span - o->d_pos;
  597. while (left > 0) {
  598. ASSERT(left >= sizeof(struct BProto_header_s))
  599. struct BProto_header_s *header = (struct BProto_header_s *)(o->buf + o->d_start + o->d_pos);
  600. o->d_pos += sizeof(struct BProto_header_s);
  601. left -= sizeof(struct BProto_header_s);
  602. uint16_t type = ltoh16(header->type);
  603. uint16_t id = ltoh16(header->id);
  604. switch (type) {
  605. case BPROTO_TYPE_UINT8: {
  606. ASSERT(left >= sizeof(struct BProto_uint8_s))
  607. o->d_pos += sizeof(struct BProto_uint8_s);
  608. left -= sizeof(struct BProto_uint8_s);
  609. } break;
  610. case BPROTO_TYPE_UINT16: {
  611. ASSERT(left >= sizeof(struct BProto_uint16_s))
  612. struct BProto_uint16_s *val = (struct BProto_uint16_s *)(o->buf + o->d_start + o->d_pos);
  613. o->d_pos += sizeof(struct BProto_uint16_s);
  614. left -= sizeof(struct BProto_uint16_s);
  615. if (id == 8) {
  616. *v = ltoh16(val->v);
  617. return 1;
  618. }
  619. } break;
  620. case BPROTO_TYPE_UINT32: {
  621. ASSERT(left >= sizeof(struct BProto_uint32_s))
  622. o->d_pos += sizeof(struct BProto_uint32_s);
  623. left -= sizeof(struct BProto_uint32_s);
  624. } break;
  625. case BPROTO_TYPE_UINT64: {
  626. ASSERT(left >= sizeof(struct BProto_uint64_s))
  627. o->d_pos += sizeof(struct BProto_uint64_s);
  628. left -= sizeof(struct BProto_uint64_s);
  629. } break;
  630. case BPROTO_TYPE_DATA:
  631. case BPROTO_TYPE_CONSTDATA:
  632. {
  633. ASSERT(left >= sizeof(struct BProto_data_header_s))
  634. struct BProto_data_header_s *val = (struct BProto_data_header_s *)(o->buf + o->d_start + o->d_pos);
  635. o->d_pos += sizeof(struct BProto_data_header_s);
  636. left -= sizeof(struct BProto_data_header_s);
  637. uint32_t payload_len = ltoh32(val->len);
  638. ASSERT(left >= payload_len)
  639. o->d_pos += payload_len;
  640. left -= payload_len;
  641. } break;
  642. default:
  643. ASSERT(0);
  644. }
  645. }
  646. return 0;
  647. }
  648. void msg1Parser_Resetd (msg1Parser *o)
  649. {
  650. o->d_pos = 0;
  651. }
  652. void msg1Parser_Forwardd (msg1Parser *o)
  653. {
  654. o->d_pos = o->d_span;
  655. }
  656. int msg1Parser_Gete (msg1Parser *o, uint8_t *v)
  657. {
  658. ASSERT(o->e_pos >= 0)
  659. ASSERT(o->e_pos <= o->e_span)
  660. int left = o->e_span - o->e_pos;
  661. while (left > 0) {
  662. ASSERT(left >= sizeof(struct BProto_header_s))
  663. struct BProto_header_s *header = (struct BProto_header_s *)(o->buf + o->e_start + o->e_pos);
  664. o->e_pos += sizeof(struct BProto_header_s);
  665. left -= sizeof(struct BProto_header_s);
  666. uint16_t type = ltoh16(header->type);
  667. uint16_t id = ltoh16(header->id);
  668. switch (type) {
  669. case BPROTO_TYPE_UINT8: {
  670. ASSERT(left >= sizeof(struct BProto_uint8_s))
  671. struct BProto_uint8_s *val = (struct BProto_uint8_s *)(o->buf + o->e_start + o->e_pos);
  672. o->e_pos += sizeof(struct BProto_uint8_s);
  673. left -= sizeof(struct BProto_uint8_s);
  674. if (id == 9) {
  675. *v = ltoh8(val->v);
  676. return 1;
  677. }
  678. } break;
  679. case BPROTO_TYPE_UINT16: {
  680. ASSERT(left >= sizeof(struct BProto_uint16_s))
  681. o->e_pos += sizeof(struct BProto_uint16_s);
  682. left -= sizeof(struct BProto_uint16_s);
  683. } break;
  684. case BPROTO_TYPE_UINT32: {
  685. ASSERT(left >= sizeof(struct BProto_uint32_s))
  686. o->e_pos += sizeof(struct BProto_uint32_s);
  687. left -= sizeof(struct BProto_uint32_s);
  688. } break;
  689. case BPROTO_TYPE_UINT64: {
  690. ASSERT(left >= sizeof(struct BProto_uint64_s))
  691. o->e_pos += sizeof(struct BProto_uint64_s);
  692. left -= sizeof(struct BProto_uint64_s);
  693. } break;
  694. case BPROTO_TYPE_DATA:
  695. case BPROTO_TYPE_CONSTDATA:
  696. {
  697. ASSERT(left >= sizeof(struct BProto_data_header_s))
  698. struct BProto_data_header_s *val = (struct BProto_data_header_s *)(o->buf + o->e_start + o->e_pos);
  699. o->e_pos += sizeof(struct BProto_data_header_s);
  700. left -= sizeof(struct BProto_data_header_s);
  701. uint32_t payload_len = ltoh32(val->len);
  702. ASSERT(left >= payload_len)
  703. o->e_pos += payload_len;
  704. left -= payload_len;
  705. } break;
  706. default:
  707. ASSERT(0);
  708. }
  709. }
  710. return 0;
  711. }
  712. void msg1Parser_Resete (msg1Parser *o)
  713. {
  714. o->e_pos = 0;
  715. }
  716. void msg1Parser_Forwarde (msg1Parser *o)
  717. {
  718. o->e_pos = o->e_span;
  719. }
  720. int msg1Parser_Getf (msg1Parser *o, uint8_t **data, int *data_len)
  721. {
  722. ASSERT(o->f_pos >= 0)
  723. ASSERT(o->f_pos <= o->f_span)
  724. int left = o->f_span - o->f_pos;
  725. while (left > 0) {
  726. ASSERT(left >= sizeof(struct BProto_header_s))
  727. struct BProto_header_s *header = (struct BProto_header_s *)(o->buf + o->f_start + o->f_pos);
  728. o->f_pos += sizeof(struct BProto_header_s);
  729. left -= sizeof(struct BProto_header_s);
  730. uint16_t type = ltoh16(header->type);
  731. uint16_t id = ltoh16(header->id);
  732. switch (type) {
  733. case BPROTO_TYPE_UINT8: {
  734. ASSERT(left >= sizeof(struct BProto_uint8_s))
  735. o->f_pos += sizeof(struct BProto_uint8_s);
  736. left -= sizeof(struct BProto_uint8_s);
  737. } break;
  738. case BPROTO_TYPE_UINT16: {
  739. ASSERT(left >= sizeof(struct BProto_uint16_s))
  740. o->f_pos += sizeof(struct BProto_uint16_s);
  741. left -= sizeof(struct BProto_uint16_s);
  742. } break;
  743. case BPROTO_TYPE_UINT32: {
  744. ASSERT(left >= sizeof(struct BProto_uint32_s))
  745. o->f_pos += sizeof(struct BProto_uint32_s);
  746. left -= sizeof(struct BProto_uint32_s);
  747. } break;
  748. case BPROTO_TYPE_UINT64: {
  749. ASSERT(left >= sizeof(struct BProto_uint64_s))
  750. o->f_pos += sizeof(struct BProto_uint64_s);
  751. left -= sizeof(struct BProto_uint64_s);
  752. } break;
  753. case BPROTO_TYPE_DATA:
  754. case BPROTO_TYPE_CONSTDATA:
  755. {
  756. ASSERT(left >= sizeof(struct BProto_data_header_s))
  757. struct BProto_data_header_s *val = (struct BProto_data_header_s *)(o->buf + o->f_start + o->f_pos);
  758. o->f_pos += sizeof(struct BProto_data_header_s);
  759. left -= sizeof(struct BProto_data_header_s);
  760. uint32_t payload_len = ltoh32(val->len);
  761. ASSERT(left >= payload_len)
  762. uint8_t *payload = o->buf + o->f_start + o->f_pos;
  763. o->f_pos += payload_len;
  764. left -= payload_len;
  765. if (type == BPROTO_TYPE_DATA && id == 10) {
  766. *data = payload;
  767. *data_len = payload_len;
  768. return 1;
  769. }
  770. } break;
  771. default:
  772. ASSERT(0);
  773. }
  774. }
  775. return 0;
  776. }
  777. void msg1Parser_Resetf (msg1Parser *o)
  778. {
  779. o->f_pos = 0;
  780. }
  781. void msg1Parser_Forwardf (msg1Parser *o)
  782. {
  783. o->f_pos = o->f_span;
  784. }
  785. int msg1Parser_Getg (msg1Parser *o, uint8_t **data)
  786. {
  787. ASSERT(o->g_pos >= 0)
  788. ASSERT(o->g_pos <= o->g_span)
  789. int left = o->g_span - o->g_pos;
  790. while (left > 0) {
  791. ASSERT(left >= sizeof(struct BProto_header_s))
  792. struct BProto_header_s *header = (struct BProto_header_s *)(o->buf + o->g_start + o->g_pos);
  793. o->g_pos += sizeof(struct BProto_header_s);
  794. left -= sizeof(struct BProto_header_s);
  795. uint16_t type = ltoh16(header->type);
  796. uint16_t id = ltoh16(header->id);
  797. switch (type) {
  798. case BPROTO_TYPE_UINT8: {
  799. ASSERT(left >= sizeof(struct BProto_uint8_s))
  800. o->g_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->g_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->g_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->g_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 = (struct BProto_data_header_s *)(o->buf + o->g_start + o->g_pos);
  823. o->g_pos += sizeof(struct BProto_data_header_s);
  824. left -= sizeof(struct BProto_data_header_s);
  825. uint32_t payload_len = ltoh32(val->len);
  826. ASSERT(left >= payload_len)
  827. uint8_t *payload = o->buf + o->g_start + o->g_pos;
  828. o->g_pos += payload_len;
  829. left -= payload_len;
  830. if (type == BPROTO_TYPE_CONSTDATA && id == 11) {
  831. *data = payload;
  832. return 1;
  833. }
  834. } break;
  835. default:
  836. ASSERT(0);
  837. }
  838. }
  839. return 0;
  840. }
  841. void msg1Parser_Resetg (msg1Parser *o)
  842. {
  843. o->g_pos = 0;
  844. }
  845. void msg1Parser_Forwardg (msg1Parser *o)
  846. {
  847. o->g_pos = o->g_span;
  848. }