bproto_msgproto.h 67 KB

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