bproto_msgproto.h 66 KB

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