bproto_msgproto.h 72 KB

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