bproto_bproto_test.h 31 KB

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