pb_decode.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551
  1. /* pb_decode.c -- decode a protobuf using minimal resources
  2. *
  3. * 2011 Petteri Aimonen <jpa@kapsi.fi>
  4. */
  5. /* Use the GCC warn_unused_result attribute to check that all return values
  6. * are propagated correctly. On other compilers and gcc before 3.4.0 just
  7. * ignore the annotation.
  8. */
  9. #if !defined(__GNUC__) || ( __GNUC__ < 3) || (__GNUC__ == 3 && __GNUC_MINOR__ < 4)
  10. #define checkreturn
  11. #else
  12. #define checkreturn __attribute__((warn_unused_result))
  13. #endif
  14. #include "pb.h"
  15. #include "pb_decode.h"
  16. #include "pb_common.h"
  17. /**************************************
  18. * Declarations internal to this file *
  19. **************************************/
  20. typedef bool (*pb_decoder_t)(pb_istream_t *stream, const pb_field_t *field, void *dest) checkreturn;
  21. static bool checkreturn buf_read(pb_istream_t *stream, pb_byte_t *buf, size_t count);
  22. static bool checkreturn read_raw_value(pb_istream_t *stream, pb_wire_type_t wire_type, pb_byte_t *buf, size_t *size);
  23. static bool checkreturn decode_static_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter);
  24. static bool checkreturn decode_callback_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter);
  25. static bool checkreturn decode_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter);
  26. static void iter_from_extension(pb_field_iter_t *iter, pb_extension_t *extension);
  27. static bool checkreturn default_extension_decoder(pb_istream_t *stream, pb_extension_t *extension, uint32_t tag, pb_wire_type_t wire_type);
  28. static bool checkreturn decode_extension(pb_istream_t *stream, uint32_t tag, pb_wire_type_t wire_type, pb_field_iter_t *iter);
  29. static bool checkreturn find_extension_field(pb_field_iter_t *iter);
  30. static void pb_field_set_to_default(pb_field_iter_t *iter);
  31. static void pb_message_set_to_defaults(const pb_field_t fields[], void *dest_struct);
  32. static bool checkreturn pb_dec_bool(pb_istream_t *stream, const pb_field_t *field, void *dest);
  33. static bool checkreturn pb_dec_varint(pb_istream_t *stream, const pb_field_t *field, void *dest);
  34. static bool checkreturn pb_decode_varint32_eof(pb_istream_t *stream, uint32_t *dest, bool *eof);
  35. static bool checkreturn pb_dec_uvarint(pb_istream_t *stream, const pb_field_t *field, void *dest);
  36. static bool checkreturn pb_dec_svarint(pb_istream_t *stream, const pb_field_t *field, void *dest);
  37. static bool checkreturn pb_dec_fixed32(pb_istream_t *stream, const pb_field_t *field, void *dest);
  38. static bool checkreturn pb_dec_fixed64(pb_istream_t *stream, const pb_field_t *field, void *dest);
  39. static bool checkreturn pb_dec_bytes(pb_istream_t *stream, const pb_field_t *field, void *dest);
  40. static bool checkreturn pb_dec_string(pb_istream_t *stream, const pb_field_t *field, void *dest);
  41. static bool checkreturn pb_dec_submessage(pb_istream_t *stream, const pb_field_t *field, void *dest);
  42. static bool checkreturn pb_dec_fixed_length_bytes(pb_istream_t *stream, const pb_field_t *field, void *dest);
  43. static bool checkreturn pb_skip_varint(pb_istream_t *stream);
  44. static bool checkreturn pb_skip_string(pb_istream_t *stream);
  45. #ifdef PB_ENABLE_MALLOC
  46. static bool checkreturn allocate_field(pb_istream_t *stream, void *pData, size_t data_size, size_t array_size);
  47. static bool checkreturn pb_release_union_field(pb_istream_t *stream, pb_field_iter_t *iter);
  48. static void pb_release_single_field(const pb_field_iter_t *iter);
  49. #endif
  50. #ifdef PB_WITHOUT_64BIT
  51. #define pb_int64_t int32_t
  52. #define pb_uint64_t uint32_t
  53. #else
  54. #define pb_int64_t int64_t
  55. #define pb_uint64_t uint64_t
  56. #endif
  57. /* --- Function pointers to field decoders ---
  58. * Order in the array must match pb_action_t LTYPE numbering.
  59. */
  60. static const pb_decoder_t PB_DECODERS[PB_LTYPES_COUNT] = {
  61. &pb_dec_bool,
  62. &pb_dec_varint,
  63. &pb_dec_uvarint,
  64. &pb_dec_svarint,
  65. &pb_dec_fixed32,
  66. &pb_dec_fixed64,
  67. &pb_dec_bytes,
  68. &pb_dec_string,
  69. &pb_dec_submessage,
  70. NULL, /* extensions */
  71. &pb_dec_fixed_length_bytes
  72. };
  73. /*******************************
  74. * pb_istream_t implementation *
  75. *******************************/
  76. static bool checkreturn buf_read(pb_istream_t *stream, pb_byte_t *buf, size_t count)
  77. {
  78. size_t i;
  79. const pb_byte_t *source = (const pb_byte_t*)stream->state;
  80. stream->state = (pb_byte_t*)stream->state + count;
  81. if (buf != NULL)
  82. {
  83. for (i = 0; i < count; i++)
  84. buf[i] = source[i];
  85. }
  86. return true;
  87. }
  88. bool checkreturn pb_read(pb_istream_t *stream, pb_byte_t *buf, size_t count)
  89. {
  90. if (count == 0)
  91. return true;
  92. #ifndef PB_BUFFER_ONLY
  93. if (buf == NULL && stream->callback != buf_read)
  94. {
  95. /* Skip input bytes */
  96. pb_byte_t tmp[16];
  97. while (count > 16)
  98. {
  99. if (!pb_read(stream, tmp, 16))
  100. return false;
  101. count -= 16;
  102. }
  103. return pb_read(stream, tmp, count);
  104. }
  105. #endif
  106. if (stream->bytes_left < count)
  107. PB_RETURN_ERROR(stream, "end-of-stream");
  108. #ifndef PB_BUFFER_ONLY
  109. if (!stream->callback(stream, buf, count))
  110. PB_RETURN_ERROR(stream, "io error");
  111. #else
  112. if (!buf_read(stream, buf, count))
  113. return false;
  114. #endif
  115. stream->bytes_left -= count;
  116. return true;
  117. }
  118. /* Read a single byte from input stream. buf may not be NULL.
  119. * This is an optimization for the varint decoding. */
  120. static bool checkreturn pb_readbyte(pb_istream_t *stream, pb_byte_t *buf)
  121. {
  122. if (stream->bytes_left == 0)
  123. PB_RETURN_ERROR(stream, "end-of-stream");
  124. #ifndef PB_BUFFER_ONLY
  125. if (!stream->callback(stream, buf, 1))
  126. PB_RETURN_ERROR(stream, "io error");
  127. #else
  128. *buf = *(const pb_byte_t*)stream->state;
  129. stream->state = (pb_byte_t*)stream->state + 1;
  130. #endif
  131. stream->bytes_left--;
  132. return true;
  133. }
  134. pb_istream_t pb_istream_from_buffer(const pb_byte_t *buf, size_t bufsize)
  135. {
  136. pb_istream_t stream;
  137. /* Cast away the const from buf without a compiler error. We are
  138. * careful to use it only in a const manner in the callbacks.
  139. */
  140. union {
  141. void *state;
  142. const void *c_state;
  143. } state;
  144. #ifdef PB_BUFFER_ONLY
  145. stream.callback = NULL;
  146. #else
  147. stream.callback = &buf_read;
  148. #endif
  149. state.c_state = buf;
  150. stream.state = state.state;
  151. stream.bytes_left = bufsize;
  152. #ifndef PB_NO_ERRMSG
  153. stream.errmsg = NULL;
  154. #endif
  155. return stream;
  156. }
  157. /********************
  158. * Helper functions *
  159. ********************/
  160. static bool checkreturn pb_decode_varint32_eof(pb_istream_t *stream, uint32_t *dest, bool *eof)
  161. {
  162. pb_byte_t byte;
  163. uint32_t result;
  164. if (!pb_readbyte(stream, &byte))
  165. {
  166. if (stream->bytes_left == 0)
  167. {
  168. if (eof)
  169. {
  170. *eof = true;
  171. }
  172. }
  173. return false;
  174. }
  175. if ((byte & 0x80) == 0)
  176. {
  177. /* Quick case, 1 byte value */
  178. result = byte;
  179. }
  180. else
  181. {
  182. /* Multibyte case */
  183. uint_fast8_t bitpos = 7;
  184. result = byte & 0x7F;
  185. do
  186. {
  187. if (!pb_readbyte(stream, &byte))
  188. return false;
  189. if (bitpos >= 32)
  190. {
  191. /* Note: The varint could have trailing 0x80 bytes, or 0xFF for negative. */
  192. uint8_t sign_extension = (bitpos < 63) ? 0xFF : 0x01;
  193. if ((byte & 0x7F) != 0x00 && ((result >> 31) == 0 || byte != sign_extension))
  194. {
  195. PB_RETURN_ERROR(stream, "varint overflow");
  196. }
  197. }
  198. else
  199. {
  200. result |= (uint32_t)(byte & 0x7F) << bitpos;
  201. }
  202. bitpos = (uint_fast8_t)(bitpos + 7);
  203. } while (byte & 0x80);
  204. if (bitpos == 35 && (byte & 0x70) != 0)
  205. {
  206. /* The last byte was at bitpos=28, so only bottom 4 bits fit. */
  207. PB_RETURN_ERROR(stream, "varint overflow");
  208. }
  209. }
  210. *dest = result;
  211. return true;
  212. }
  213. bool checkreturn pb_decode_varint32(pb_istream_t *stream, uint32_t *dest)
  214. {
  215. return pb_decode_varint32_eof(stream, dest, NULL);
  216. }
  217. #ifndef PB_WITHOUT_64BIT
  218. bool checkreturn pb_decode_varint(pb_istream_t *stream, uint64_t *dest)
  219. {
  220. pb_byte_t byte;
  221. uint_fast8_t bitpos = 0;
  222. uint64_t result = 0;
  223. do
  224. {
  225. if (bitpos >= 64)
  226. PB_RETURN_ERROR(stream, "varint overflow");
  227. if (!pb_readbyte(stream, &byte))
  228. return false;
  229. result |= (uint64_t)(byte & 0x7F) << bitpos;
  230. bitpos = (uint_fast8_t)(bitpos + 7);
  231. } while (byte & 0x80);
  232. *dest = result;
  233. return true;
  234. }
  235. #endif
  236. bool checkreturn pb_skip_varint(pb_istream_t *stream)
  237. {
  238. pb_byte_t byte;
  239. do
  240. {
  241. if (!pb_read(stream, &byte, 1))
  242. return false;
  243. } while (byte & 0x80);
  244. return true;
  245. }
  246. bool checkreturn pb_skip_string(pb_istream_t *stream)
  247. {
  248. uint32_t length;
  249. if (!pb_decode_varint32(stream, &length))
  250. return false;
  251. return pb_read(stream, NULL, length);
  252. }
  253. bool checkreturn pb_decode_tag(pb_istream_t *stream, pb_wire_type_t *wire_type, uint32_t *tag, bool *eof)
  254. {
  255. uint32_t temp;
  256. *eof = false;
  257. *wire_type = (pb_wire_type_t) 0;
  258. *tag = 0;
  259. if (!pb_decode_varint32_eof(stream, &temp, eof))
  260. {
  261. return false;
  262. }
  263. if (temp == 0)
  264. {
  265. *eof = true; /* Special feature: allow 0-terminated messages. */
  266. return false;
  267. }
  268. *tag = temp >> 3;
  269. *wire_type = (pb_wire_type_t)(temp & 7);
  270. return true;
  271. }
  272. bool checkreturn pb_skip_field(pb_istream_t *stream, pb_wire_type_t wire_type)
  273. {
  274. switch (wire_type)
  275. {
  276. case PB_WT_VARINT: return pb_skip_varint(stream);
  277. case PB_WT_64BIT: return pb_read(stream, NULL, 8);
  278. case PB_WT_STRING: return pb_skip_string(stream);
  279. case PB_WT_32BIT: return pb_read(stream, NULL, 4);
  280. default: PB_RETURN_ERROR(stream, "invalid wire_type");
  281. }
  282. }
  283. /* Read a raw value to buffer, for the purpose of passing it to callback as
  284. * a substream. Size is maximum size on call, and actual size on return.
  285. */
  286. static bool checkreturn read_raw_value(pb_istream_t *stream, pb_wire_type_t wire_type, pb_byte_t *buf, size_t *size)
  287. {
  288. size_t max_size = *size;
  289. switch (wire_type)
  290. {
  291. case PB_WT_VARINT:
  292. *size = 0;
  293. do
  294. {
  295. (*size)++;
  296. if (*size > max_size) return false;
  297. if (!pb_read(stream, buf, 1)) return false;
  298. } while (*buf++ & 0x80);
  299. return true;
  300. case PB_WT_64BIT:
  301. *size = 8;
  302. return pb_read(stream, buf, 8);
  303. case PB_WT_32BIT:
  304. *size = 4;
  305. return pb_read(stream, buf, 4);
  306. case PB_WT_STRING:
  307. /* Calling read_raw_value with a PB_WT_STRING is an error.
  308. * Explicitly handle this case and fallthrough to default to avoid
  309. * compiler warnings.
  310. */
  311. default: PB_RETURN_ERROR(stream, "invalid wire_type");
  312. }
  313. }
  314. /* Decode string length from stream and return a substream with limited length.
  315. * Remember to close the substream using pb_close_string_substream().
  316. */
  317. bool checkreturn pb_make_string_substream(pb_istream_t *stream, pb_istream_t *substream)
  318. {
  319. uint32_t size;
  320. if (!pb_decode_varint32(stream, &size))
  321. return false;
  322. *substream = *stream;
  323. if (substream->bytes_left < size)
  324. PB_RETURN_ERROR(stream, "parent stream too short");
  325. substream->bytes_left = size;
  326. stream->bytes_left -= size;
  327. return true;
  328. }
  329. bool checkreturn pb_close_string_substream(pb_istream_t *stream, pb_istream_t *substream)
  330. {
  331. if (substream->bytes_left) {
  332. if (!pb_read(substream, NULL, substream->bytes_left))
  333. return false;
  334. }
  335. stream->state = substream->state;
  336. #ifndef PB_NO_ERRMSG
  337. stream->errmsg = substream->errmsg;
  338. #endif
  339. return true;
  340. }
  341. /*************************
  342. * Decode a single field *
  343. *************************/
  344. static bool checkreturn decode_static_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
  345. {
  346. pb_type_t type;
  347. pb_decoder_t func;
  348. type = iter->pos->type;
  349. func = PB_DECODERS[PB_LTYPE(type)];
  350. switch (PB_HTYPE(type))
  351. {
  352. case PB_HTYPE_REQUIRED:
  353. return func(stream, iter->pos, iter->pData);
  354. case PB_HTYPE_OPTIONAL:
  355. if (iter->pSize != iter->pData)
  356. *(bool*)iter->pSize = true;
  357. return func(stream, iter->pos, iter->pData);
  358. case PB_HTYPE_REPEATED:
  359. if (wire_type == PB_WT_STRING
  360. && PB_LTYPE(type) <= PB_LTYPE_LAST_PACKABLE)
  361. {
  362. /* Packed array */
  363. bool status = true;
  364. pb_size_t *size = (pb_size_t*)iter->pSize;
  365. pb_istream_t substream;
  366. if (!pb_make_string_substream(stream, &substream))
  367. return false;
  368. while (substream.bytes_left > 0 && *size < iter->pos->array_size)
  369. {
  370. void *pItem = (char*)iter->pData + iter->pos->data_size * (*size);
  371. if (!func(&substream, iter->pos, pItem))
  372. {
  373. status = false;
  374. break;
  375. }
  376. (*size)++;
  377. }
  378. if (substream.bytes_left != 0)
  379. PB_RETURN_ERROR(stream, "array overflow");
  380. if (!pb_close_string_substream(stream, &substream))
  381. return false;
  382. return status;
  383. }
  384. else
  385. {
  386. /* Repeated field */
  387. pb_size_t *size = (pb_size_t*)iter->pSize;
  388. char *pItem = (char*)iter->pData + iter->pos->data_size * (*size);
  389. if ((*size)++ >= iter->pos->array_size)
  390. PB_RETURN_ERROR(stream, "array overflow");
  391. return func(stream, iter->pos, pItem);
  392. }
  393. case PB_HTYPE_ONEOF:
  394. if (PB_LTYPE(type) == PB_LTYPE_SUBMESSAGE &&
  395. *(pb_size_t*)iter->pSize != iter->pos->tag)
  396. {
  397. /* We memset to zero so that any callbacks are set to NULL.
  398. * This is because the callbacks might otherwise have values
  399. * from some other union field. */
  400. memset(iter->pData, 0, iter->pos->data_size);
  401. pb_message_set_to_defaults((const pb_field_t*)iter->pos->ptr, iter->pData);
  402. }
  403. *(pb_size_t*)iter->pSize = iter->pos->tag;
  404. return func(stream, iter->pos, iter->pData);
  405. default:
  406. PB_RETURN_ERROR(stream, "invalid field type");
  407. }
  408. }
  409. #ifdef PB_ENABLE_MALLOC
  410. /* Allocate storage for the field and store the pointer at iter->pData.
  411. * array_size is the number of entries to reserve in an array.
  412. * Zero size is not allowed, use pb_free() for releasing.
  413. */
  414. static bool checkreturn allocate_field(pb_istream_t *stream, void *pData, size_t data_size, size_t array_size)
  415. {
  416. void *ptr = *(void**)pData;
  417. if (data_size == 0 || array_size == 0)
  418. PB_RETURN_ERROR(stream, "invalid size");
  419. #ifdef __AVR__
  420. /* Workaround for AVR libc bug 53284: http://savannah.nongnu.org/bugs/?53284
  421. * Realloc to size of 1 byte can cause corruption of the malloc structures.
  422. */
  423. if (data_size == 1 && array_size == 1)
  424. {
  425. data_size = 2;
  426. }
  427. #endif
  428. /* Check for multiplication overflows.
  429. * This code avoids the costly division if the sizes are small enough.
  430. * Multiplication is safe as long as only half of bits are set
  431. * in either multiplicand.
  432. */
  433. {
  434. const size_t check_limit = (size_t)1 << (sizeof(size_t) * 4);
  435. if (data_size >= check_limit || array_size >= check_limit)
  436. {
  437. const size_t size_max = (size_t)-1;
  438. if (size_max / array_size < data_size)
  439. {
  440. PB_RETURN_ERROR(stream, "size too large");
  441. }
  442. }
  443. }
  444. /* Allocate new or expand previous allocation */
  445. /* Note: on failure the old pointer will remain in the structure,
  446. * the message must be freed by caller also on error return. */
  447. ptr = pb_realloc(ptr, array_size * data_size);
  448. if (ptr == NULL)
  449. PB_RETURN_ERROR(stream, "realloc failed");
  450. *(void**)pData = ptr;
  451. return true;
  452. }
  453. /* Clear a newly allocated item in case it contains a pointer, or is a submessage. */
  454. static void initialize_pointer_field(void *pItem, pb_field_iter_t *iter)
  455. {
  456. if (PB_LTYPE(iter->pos->type) == PB_LTYPE_STRING ||
  457. PB_LTYPE(iter->pos->type) == PB_LTYPE_BYTES)
  458. {
  459. *(void**)pItem = NULL;
  460. }
  461. else if (PB_LTYPE(iter->pos->type) == PB_LTYPE_SUBMESSAGE)
  462. {
  463. /* We memset to zero so that any callbacks are set to NULL.
  464. * Then set any default values. */
  465. memset(pItem, 0, iter->pos->data_size);
  466. pb_message_set_to_defaults((const pb_field_t *) iter->pos->ptr, pItem);
  467. }
  468. }
  469. #endif
  470. static bool checkreturn decode_pointer_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
  471. {
  472. #ifndef PB_ENABLE_MALLOC
  473. PB_UNUSED(wire_type);
  474. PB_UNUSED(iter);
  475. PB_RETURN_ERROR(stream, "no malloc support");
  476. #else
  477. pb_type_t type;
  478. pb_decoder_t func;
  479. type = iter->pos->type;
  480. func = PB_DECODERS[PB_LTYPE(type)];
  481. switch (PB_HTYPE(type))
  482. {
  483. case PB_HTYPE_REQUIRED:
  484. case PB_HTYPE_OPTIONAL:
  485. case PB_HTYPE_ONEOF:
  486. if (PB_LTYPE(type) == PB_LTYPE_SUBMESSAGE &&
  487. *(void**)iter->pData != NULL)
  488. {
  489. /* Duplicate field, have to release the old allocation first. */
  490. pb_release_single_field(iter);
  491. }
  492. if (PB_HTYPE(type) == PB_HTYPE_ONEOF)
  493. {
  494. *(pb_size_t*)iter->pSize = iter->pos->tag;
  495. }
  496. if (PB_LTYPE(type) == PB_LTYPE_STRING ||
  497. PB_LTYPE(type) == PB_LTYPE_BYTES)
  498. {
  499. return func(stream, iter->pos, iter->pData);
  500. }
  501. else
  502. {
  503. if (!allocate_field(stream, iter->pData, iter->pos->data_size, 1))
  504. return false;
  505. initialize_pointer_field(*(void**)iter->pData, iter);
  506. return func(stream, iter->pos, *(void**)iter->pData);
  507. }
  508. case PB_HTYPE_REPEATED:
  509. if (wire_type == PB_WT_STRING
  510. && PB_LTYPE(type) <= PB_LTYPE_LAST_PACKABLE)
  511. {
  512. /* Packed array, multiple items come in at once. */
  513. bool status = true;
  514. pb_size_t *size = (pb_size_t*)iter->pSize;
  515. size_t allocated_size = *size;
  516. void *pItem;
  517. pb_istream_t substream;
  518. if (!pb_make_string_substream(stream, &substream))
  519. return false;
  520. while (substream.bytes_left)
  521. {
  522. if (*size == PB_SIZE_MAX)
  523. {
  524. #ifndef PB_NO_ERRMSG
  525. stream->errmsg = "too many array entries";
  526. #endif
  527. status = false;
  528. break;
  529. }
  530. if ((size_t)*size + 1 > allocated_size)
  531. {
  532. /* Allocate more storage. This tries to guess the
  533. * number of remaining entries. Round the division
  534. * upwards. */
  535. size_t remain = (substream.bytes_left - 1) / iter->pos->data_size + 1;
  536. if (remain < PB_SIZE_MAX - allocated_size)
  537. allocated_size += remain;
  538. else
  539. allocated_size += 1;
  540. if (!allocate_field(&substream, iter->pData, iter->pos->data_size, allocated_size))
  541. {
  542. status = false;
  543. break;
  544. }
  545. }
  546. /* Decode the array entry */
  547. pItem = *(char**)iter->pData + iter->pos->data_size * (*size);
  548. initialize_pointer_field(pItem, iter);
  549. if (!func(&substream, iter->pos, pItem))
  550. {
  551. status = false;
  552. break;
  553. }
  554. (*size)++;
  555. }
  556. if (!pb_close_string_substream(stream, &substream))
  557. return false;
  558. return status;
  559. }
  560. else
  561. {
  562. /* Normal repeated field, i.e. only one item at a time. */
  563. pb_size_t *size = (pb_size_t*)iter->pSize;
  564. void *pItem;
  565. if (*size == PB_SIZE_MAX)
  566. PB_RETURN_ERROR(stream, "too many array entries");
  567. if (!allocate_field(stream, iter->pData, iter->pos->data_size, (size_t)(*size + 1)))
  568. return false;
  569. pItem = *(char**)iter->pData + iter->pos->data_size * (*size);
  570. (*size)++;
  571. initialize_pointer_field(pItem, iter);
  572. return func(stream, iter->pos, pItem);
  573. }
  574. default:
  575. PB_RETURN_ERROR(stream, "invalid field type");
  576. }
  577. #endif
  578. }
  579. static bool checkreturn decode_callback_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
  580. {
  581. pb_callback_t *pCallback = (pb_callback_t*)iter->pData;
  582. #ifdef PB_OLD_CALLBACK_STYLE
  583. void *arg;
  584. #else
  585. void **arg;
  586. #endif
  587. if (pCallback == NULL || pCallback->funcs.decode == NULL)
  588. return pb_skip_field(stream, wire_type);
  589. #ifdef PB_OLD_CALLBACK_STYLE
  590. arg = pCallback->arg;
  591. #else
  592. arg = &(pCallback->arg);
  593. #endif
  594. if (wire_type == PB_WT_STRING)
  595. {
  596. pb_istream_t substream;
  597. if (!pb_make_string_substream(stream, &substream))
  598. return false;
  599. do
  600. {
  601. if (!pCallback->funcs.decode(&substream, iter->pos, arg))
  602. PB_RETURN_ERROR(stream, "callback failed");
  603. } while (substream.bytes_left);
  604. if (!pb_close_string_substream(stream, &substream))
  605. return false;
  606. return true;
  607. }
  608. else
  609. {
  610. /* Copy the single scalar value to stack.
  611. * This is required so that we can limit the stream length,
  612. * which in turn allows to use same callback for packed and
  613. * not-packed fields. */
  614. pb_istream_t substream;
  615. pb_byte_t buffer[10];
  616. size_t size = sizeof(buffer);
  617. if (!read_raw_value(stream, wire_type, buffer, &size))
  618. return false;
  619. substream = pb_istream_from_buffer(buffer, size);
  620. return pCallback->funcs.decode(&substream, iter->pos, arg);
  621. }
  622. }
  623. static bool checkreturn decode_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
  624. {
  625. #ifdef PB_ENABLE_MALLOC
  626. /* When decoding an oneof field, check if there is old data that must be
  627. * released first. */
  628. if (PB_HTYPE(iter->pos->type) == PB_HTYPE_ONEOF)
  629. {
  630. if (!pb_release_union_field(stream, iter))
  631. return false;
  632. }
  633. #endif
  634. switch (PB_ATYPE(iter->pos->type))
  635. {
  636. case PB_ATYPE_STATIC:
  637. return decode_static_field(stream, wire_type, iter);
  638. case PB_ATYPE_POINTER:
  639. return decode_pointer_field(stream, wire_type, iter);
  640. case PB_ATYPE_CALLBACK:
  641. return decode_callback_field(stream, wire_type, iter);
  642. default:
  643. PB_RETURN_ERROR(stream, "invalid field type");
  644. }
  645. }
  646. static void iter_from_extension(pb_field_iter_t *iter, pb_extension_t *extension)
  647. {
  648. /* Fake a field iterator for the extension field.
  649. * It is not actually safe to advance this iterator, but decode_field
  650. * will not even try to. */
  651. const pb_field_t *field = (const pb_field_t*)extension->type->arg;
  652. (void)pb_field_iter_begin(iter, field, extension->dest);
  653. iter->pData = extension->dest;
  654. iter->pSize = &extension->found;
  655. if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
  656. {
  657. /* For pointer extensions, the pointer is stored directly
  658. * in the extension structure. This avoids having an extra
  659. * indirection. */
  660. iter->pData = &extension->dest;
  661. }
  662. }
  663. /* Default handler for extension fields. Expects a pb_field_t structure
  664. * in extension->type->arg. */
  665. static bool checkreturn default_extension_decoder(pb_istream_t *stream,
  666. pb_extension_t *extension, uint32_t tag, pb_wire_type_t wire_type)
  667. {
  668. const pb_field_t *field = (const pb_field_t*)extension->type->arg;
  669. pb_field_iter_t iter;
  670. if (field->tag != tag)
  671. return true;
  672. iter_from_extension(&iter, extension);
  673. extension->found = true;
  674. return decode_field(stream, wire_type, &iter);
  675. }
  676. /* Try to decode an unknown field as an extension field. Tries each extension
  677. * decoder in turn, until one of them handles the field or loop ends. */
  678. static bool checkreturn decode_extension(pb_istream_t *stream,
  679. uint32_t tag, pb_wire_type_t wire_type, pb_field_iter_t *iter)
  680. {
  681. pb_extension_t *extension = *(pb_extension_t* const *)iter->pData;
  682. size_t pos = stream->bytes_left;
  683. while (extension != NULL && pos == stream->bytes_left)
  684. {
  685. bool status;
  686. if (extension->type->decode)
  687. status = extension->type->decode(stream, extension, tag, wire_type);
  688. else
  689. status = default_extension_decoder(stream, extension, tag, wire_type);
  690. if (!status)
  691. return false;
  692. extension = extension->next;
  693. }
  694. return true;
  695. }
  696. /* Step through the iterator until an extension field is found or until all
  697. * entries have been checked. There can be only one extension field per
  698. * message. Returns false if no extension field is found. */
  699. static bool checkreturn find_extension_field(pb_field_iter_t *iter)
  700. {
  701. const pb_field_t *start = iter->pos;
  702. do {
  703. if (PB_LTYPE(iter->pos->type) == PB_LTYPE_EXTENSION)
  704. return true;
  705. (void)pb_field_iter_next(iter);
  706. } while (iter->pos != start);
  707. return false;
  708. }
  709. /* Initialize message fields to default values, recursively */
  710. static void pb_field_set_to_default(pb_field_iter_t *iter)
  711. {
  712. pb_type_t type;
  713. type = iter->pos->type;
  714. if (PB_LTYPE(type) == PB_LTYPE_EXTENSION)
  715. {
  716. pb_extension_t *ext = *(pb_extension_t* const *)iter->pData;
  717. while (ext != NULL)
  718. {
  719. pb_field_iter_t ext_iter;
  720. ext->found = false;
  721. iter_from_extension(&ext_iter, ext);
  722. pb_field_set_to_default(&ext_iter);
  723. ext = ext->next;
  724. }
  725. }
  726. else if (PB_ATYPE(type) == PB_ATYPE_STATIC)
  727. {
  728. bool init_data = true;
  729. if (PB_HTYPE(type) == PB_HTYPE_OPTIONAL && iter->pSize != iter->pData)
  730. {
  731. /* Set has_field to false. Still initialize the optional field
  732. * itself also. */
  733. *(bool*)iter->pSize = false;
  734. }
  735. else if (PB_HTYPE(type) == PB_HTYPE_REPEATED ||
  736. PB_HTYPE(type) == PB_HTYPE_ONEOF)
  737. {
  738. /* REPEATED: Set array count to 0, no need to initialize contents.
  739. ONEOF: Set which_field to 0. */
  740. *(pb_size_t*)iter->pSize = 0;
  741. init_data = false;
  742. }
  743. if (init_data)
  744. {
  745. if (PB_LTYPE(iter->pos->type) == PB_LTYPE_SUBMESSAGE)
  746. {
  747. /* Initialize submessage to defaults */
  748. pb_message_set_to_defaults((const pb_field_t *) iter->pos->ptr, iter->pData);
  749. }
  750. else if (iter->pos->ptr != NULL)
  751. {
  752. /* Initialize to default value */
  753. memcpy(iter->pData, iter->pos->ptr, iter->pos->data_size);
  754. }
  755. else
  756. {
  757. /* Initialize to zeros */
  758. memset(iter->pData, 0, iter->pos->data_size);
  759. }
  760. }
  761. }
  762. else if (PB_ATYPE(type) == PB_ATYPE_POINTER)
  763. {
  764. /* Initialize the pointer to NULL. */
  765. *(void**)iter->pData = NULL;
  766. /* Initialize array count to 0. */
  767. if (PB_HTYPE(type) == PB_HTYPE_REPEATED ||
  768. PB_HTYPE(type) == PB_HTYPE_ONEOF)
  769. {
  770. *(pb_size_t*)iter->pSize = 0;
  771. }
  772. }
  773. else if (PB_ATYPE(type) == PB_ATYPE_CALLBACK)
  774. {
  775. /* Don't overwrite callback */
  776. }
  777. }
  778. static void pb_message_set_to_defaults(const pb_field_t fields[], void *dest_struct)
  779. {
  780. pb_field_iter_t iter;
  781. if (!pb_field_iter_begin(&iter, fields, dest_struct))
  782. return; /* Empty message type */
  783. do
  784. {
  785. pb_field_set_to_default(&iter);
  786. } while (pb_field_iter_next(&iter));
  787. }
  788. /*********************
  789. * Decode all fields *
  790. *********************/
  791. bool checkreturn pb_decode_noinit(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
  792. {
  793. uint32_t fields_seen[(PB_MAX_REQUIRED_FIELDS + 31) / 32] = {0, 0};
  794. const uint32_t allbits = ~(uint32_t)0;
  795. uint32_t extension_range_start = 0;
  796. pb_field_iter_t iter;
  797. /* 'fixed_count_field' and 'fixed_count_size' track position of a repeated fixed
  798. * count field. This can only handle _one_ repeated fixed count field that
  799. * is unpacked and unordered among other (non repeated fixed count) fields.
  800. */
  801. const pb_field_t *fixed_count_field = NULL;
  802. pb_size_t fixed_count_size = 0;
  803. /* Return value ignored, as empty message types will be correctly handled by
  804. * pb_field_iter_find() anyway. */
  805. (void)pb_field_iter_begin(&iter, fields, dest_struct);
  806. while (stream->bytes_left)
  807. {
  808. uint32_t tag;
  809. pb_wire_type_t wire_type;
  810. bool eof;
  811. if (!pb_decode_tag(stream, &wire_type, &tag, &eof))
  812. {
  813. if (eof)
  814. break;
  815. else
  816. return false;
  817. }
  818. if (!pb_field_iter_find(&iter, tag))
  819. {
  820. /* No match found, check if it matches an extension. */
  821. if (tag >= extension_range_start)
  822. {
  823. if (!find_extension_field(&iter))
  824. extension_range_start = (uint32_t)-1;
  825. else
  826. extension_range_start = iter.pos->tag;
  827. if (tag >= extension_range_start)
  828. {
  829. size_t pos = stream->bytes_left;
  830. if (!decode_extension(stream, tag, wire_type, &iter))
  831. return false;
  832. if (pos != stream->bytes_left)
  833. {
  834. /* The field was handled */
  835. continue;
  836. }
  837. }
  838. }
  839. /* No match found, skip data */
  840. if (!pb_skip_field(stream, wire_type))
  841. return false;
  842. continue;
  843. }
  844. /* If a repeated fixed count field was found, get size from
  845. * 'fixed_count_field' as there is no counter contained in the struct.
  846. */
  847. if (PB_HTYPE(iter.pos->type) == PB_HTYPE_REPEATED
  848. && iter.pSize == iter.pData)
  849. {
  850. if (fixed_count_field != iter.pos) {
  851. /* If the new fixed count field does not match the previous one,
  852. * check that the previous one is NULL or that it finished
  853. * receiving all the expected data.
  854. */
  855. if (fixed_count_field != NULL &&
  856. fixed_count_size != fixed_count_field->array_size)
  857. {
  858. PB_RETURN_ERROR(stream, "wrong size for fixed count field");
  859. }
  860. fixed_count_field = iter.pos;
  861. fixed_count_size = 0;
  862. }
  863. iter.pSize = &fixed_count_size;
  864. }
  865. if (PB_HTYPE(iter.pos->type) == PB_HTYPE_REQUIRED
  866. && iter.required_field_index < PB_MAX_REQUIRED_FIELDS)
  867. {
  868. uint32_t tmp = ((uint32_t)1 << (iter.required_field_index & 31));
  869. fields_seen[iter.required_field_index >> 5] |= tmp;
  870. }
  871. if (!decode_field(stream, wire_type, &iter))
  872. return false;
  873. }
  874. /* Check that all elements of the last decoded fixed count field were present. */
  875. if (fixed_count_field != NULL &&
  876. fixed_count_size != fixed_count_field->array_size)
  877. {
  878. PB_RETURN_ERROR(stream, "wrong size for fixed count field");
  879. }
  880. /* Check that all required fields were present. */
  881. {
  882. /* First figure out the number of required fields by
  883. * seeking to the end of the field array. Usually we
  884. * are already close to end after decoding.
  885. */
  886. unsigned req_field_count;
  887. pb_type_t last_type;
  888. unsigned i;
  889. do {
  890. req_field_count = iter.required_field_index;
  891. last_type = iter.pos->type;
  892. } while (pb_field_iter_next(&iter));
  893. /* Fixup if last field was also required. */
  894. if (PB_HTYPE(last_type) == PB_HTYPE_REQUIRED && iter.pos->tag != 0)
  895. req_field_count++;
  896. if (req_field_count > PB_MAX_REQUIRED_FIELDS)
  897. req_field_count = PB_MAX_REQUIRED_FIELDS;
  898. if (req_field_count > 0)
  899. {
  900. /* Check the whole words */
  901. for (i = 0; i < (req_field_count >> 5); i++)
  902. {
  903. if (fields_seen[i] != allbits)
  904. PB_RETURN_ERROR(stream, "missing required field");
  905. }
  906. /* Check the remaining bits (if any) */
  907. if ((req_field_count & 31) != 0)
  908. {
  909. if (fields_seen[req_field_count >> 5] !=
  910. (allbits >> (32 - (req_field_count & 31))))
  911. {
  912. PB_RETURN_ERROR(stream, "missing required field");
  913. }
  914. }
  915. }
  916. }
  917. return true;
  918. }
  919. bool checkreturn pb_decode(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
  920. {
  921. bool status;
  922. pb_message_set_to_defaults(fields, dest_struct);
  923. status = pb_decode_noinit(stream, fields, dest_struct);
  924. #ifdef PB_ENABLE_MALLOC
  925. if (!status)
  926. pb_release(fields, dest_struct);
  927. #endif
  928. return status;
  929. }
  930. bool pb_decode_delimited_noinit(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
  931. {
  932. pb_istream_t substream;
  933. bool status;
  934. if (!pb_make_string_substream(stream, &substream))
  935. return false;
  936. status = pb_decode_noinit(&substream, fields, dest_struct);
  937. if (!pb_close_string_substream(stream, &substream))
  938. return false;
  939. return status;
  940. }
  941. bool pb_decode_delimited(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
  942. {
  943. pb_istream_t substream;
  944. bool status;
  945. if (!pb_make_string_substream(stream, &substream))
  946. return false;
  947. status = pb_decode(&substream, fields, dest_struct);
  948. if (!pb_close_string_substream(stream, &substream))
  949. return false;
  950. return status;
  951. }
  952. bool pb_decode_nullterminated(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
  953. {
  954. /* This behaviour will be separated in nanopb-0.4.0, see issue #278. */
  955. return pb_decode(stream, fields, dest_struct);
  956. }
  957. #ifdef PB_ENABLE_MALLOC
  958. /* Given an oneof field, if there has already been a field inside this oneof,
  959. * release it before overwriting with a different one. */
  960. static bool pb_release_union_field(pb_istream_t *stream, pb_field_iter_t *iter)
  961. {
  962. pb_size_t old_tag = *(pb_size_t*)iter->pSize; /* Previous which_ value */
  963. pb_size_t new_tag = iter->pos->tag; /* New which_ value */
  964. if (old_tag == 0)
  965. return true; /* Ok, no old data in union */
  966. if (old_tag == new_tag)
  967. return true; /* Ok, old data is of same type => merge */
  968. /* Release old data. The find can fail if the message struct contains
  969. * invalid data. */
  970. if (!pb_field_iter_find(iter, old_tag))
  971. PB_RETURN_ERROR(stream, "invalid union tag");
  972. pb_release_single_field(iter);
  973. /* Restore iterator to where it should be.
  974. * This shouldn't fail unless the pb_field_t structure is corrupted. */
  975. if (!pb_field_iter_find(iter, new_tag))
  976. PB_RETURN_ERROR(stream, "iterator error");
  977. return true;
  978. }
  979. static void pb_release_single_field(const pb_field_iter_t *iter)
  980. {
  981. pb_type_t type;
  982. type = iter->pos->type;
  983. if (PB_HTYPE(type) == PB_HTYPE_ONEOF)
  984. {
  985. if (*(pb_size_t*)iter->pSize != iter->pos->tag)
  986. return; /* This is not the current field in the union */
  987. }
  988. /* Release anything contained inside an extension or submsg.
  989. * This has to be done even if the submsg itself is statically
  990. * allocated. */
  991. if (PB_LTYPE(type) == PB_LTYPE_EXTENSION)
  992. {
  993. /* Release fields from all extensions in the linked list */
  994. pb_extension_t *ext = *(pb_extension_t**)iter->pData;
  995. while (ext != NULL)
  996. {
  997. pb_field_iter_t ext_iter;
  998. iter_from_extension(&ext_iter, ext);
  999. pb_release_single_field(&ext_iter);
  1000. ext = ext->next;
  1001. }
  1002. }
  1003. else if (PB_LTYPE(type) == PB_LTYPE_SUBMESSAGE && PB_ATYPE(type) != PB_ATYPE_CALLBACK)
  1004. {
  1005. /* Release fields in submessage or submsg array */
  1006. void *pItem = iter->pData;
  1007. pb_size_t count = 1;
  1008. if (PB_ATYPE(type) == PB_ATYPE_POINTER)
  1009. {
  1010. pItem = *(void**)iter->pData;
  1011. }
  1012. if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
  1013. {
  1014. if (PB_ATYPE(type) == PB_ATYPE_STATIC && iter->pSize == iter->pData) {
  1015. /* No _count field so use size of the array */
  1016. count = iter->pos->array_size;
  1017. } else {
  1018. count = *(pb_size_t*)iter->pSize;
  1019. }
  1020. if (PB_ATYPE(type) == PB_ATYPE_STATIC && count > iter->pos->array_size)
  1021. {
  1022. /* Protect against corrupted _count fields */
  1023. count = iter->pos->array_size;
  1024. }
  1025. }
  1026. if (pItem)
  1027. {
  1028. while (count--)
  1029. {
  1030. pb_release((const pb_field_t*)iter->pos->ptr, pItem);
  1031. pItem = (char*)pItem + iter->pos->data_size;
  1032. }
  1033. }
  1034. }
  1035. if (PB_ATYPE(type) == PB_ATYPE_POINTER)
  1036. {
  1037. if (PB_HTYPE(type) == PB_HTYPE_REPEATED &&
  1038. (PB_LTYPE(type) == PB_LTYPE_STRING ||
  1039. PB_LTYPE(type) == PB_LTYPE_BYTES))
  1040. {
  1041. /* Release entries in repeated string or bytes array */
  1042. void **pItem = *(void***)iter->pData;
  1043. pb_size_t count = *(pb_size_t*)iter->pSize;
  1044. while (count--)
  1045. {
  1046. pb_free(*pItem);
  1047. *pItem++ = NULL;
  1048. }
  1049. }
  1050. if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
  1051. {
  1052. /* We are going to release the array, so set the size to 0 */
  1053. *(pb_size_t*)iter->pSize = 0;
  1054. }
  1055. /* Release main item */
  1056. pb_free(*(void**)iter->pData);
  1057. *(void**)iter->pData = NULL;
  1058. }
  1059. }
  1060. void pb_release(const pb_field_t fields[], void *dest_struct)
  1061. {
  1062. pb_field_iter_t iter;
  1063. if (!dest_struct)
  1064. return; /* Ignore NULL pointers, similar to free() */
  1065. if (!pb_field_iter_begin(&iter, fields, dest_struct))
  1066. return; /* Empty message type */
  1067. do
  1068. {
  1069. pb_release_single_field(&iter);
  1070. } while (pb_field_iter_next(&iter));
  1071. }
  1072. #endif
  1073. /* Field decoders */
  1074. bool pb_decode_bool(pb_istream_t *stream, bool *dest)
  1075. {
  1076. return pb_dec_bool(stream, NULL, (void*)dest);
  1077. }
  1078. bool pb_decode_svarint(pb_istream_t *stream, pb_int64_t *dest)
  1079. {
  1080. pb_uint64_t value;
  1081. if (!pb_decode_varint(stream, &value))
  1082. return false;
  1083. if (value & 1)
  1084. *dest = (pb_int64_t)(~(value >> 1));
  1085. else
  1086. *dest = (pb_int64_t)(value >> 1);
  1087. return true;
  1088. }
  1089. bool pb_decode_fixed32(pb_istream_t *stream, void *dest)
  1090. {
  1091. pb_byte_t bytes[4];
  1092. if (!pb_read(stream, bytes, 4))
  1093. return false;
  1094. *(uint32_t*)dest = ((uint32_t)bytes[0] << 0) |
  1095. ((uint32_t)bytes[1] << 8) |
  1096. ((uint32_t)bytes[2] << 16) |
  1097. ((uint32_t)bytes[3] << 24);
  1098. return true;
  1099. }
  1100. #ifndef PB_WITHOUT_64BIT
  1101. bool pb_decode_fixed64(pb_istream_t *stream, void *dest)
  1102. {
  1103. pb_byte_t bytes[8];
  1104. if (!pb_read(stream, bytes, 8))
  1105. return false;
  1106. *(uint64_t*)dest = ((uint64_t)bytes[0] << 0) |
  1107. ((uint64_t)bytes[1] << 8) |
  1108. ((uint64_t)bytes[2] << 16) |
  1109. ((uint64_t)bytes[3] << 24) |
  1110. ((uint64_t)bytes[4] << 32) |
  1111. ((uint64_t)bytes[5] << 40) |
  1112. ((uint64_t)bytes[6] << 48) |
  1113. ((uint64_t)bytes[7] << 56);
  1114. return true;
  1115. }
  1116. #endif
  1117. static bool checkreturn pb_dec_bool(pb_istream_t *stream, const pb_field_t *field, void *dest)
  1118. {
  1119. uint32_t value;
  1120. PB_UNUSED(field);
  1121. if (!pb_decode_varint32(stream, &value))
  1122. return false;
  1123. *(bool*)dest = (value != 0);
  1124. return true;
  1125. }
  1126. static bool checkreturn pb_dec_varint(pb_istream_t *stream, const pb_field_t *field, void *dest)
  1127. {
  1128. pb_uint64_t value;
  1129. pb_int64_t svalue;
  1130. pb_int64_t clamped;
  1131. if (!pb_decode_varint(stream, &value))
  1132. return false;
  1133. /* See issue 97: Google's C++ protobuf allows negative varint values to
  1134. * be cast as int32_t, instead of the int64_t that should be used when
  1135. * encoding. Previous nanopb versions had a bug in encoding. In order to
  1136. * not break decoding of such messages, we cast <=32 bit fields to
  1137. * int32_t first to get the sign correct.
  1138. */
  1139. if (field->data_size == sizeof(pb_int64_t))
  1140. svalue = (pb_int64_t)value;
  1141. else
  1142. svalue = (int32_t)value;
  1143. /* Cast to the proper field size, while checking for overflows */
  1144. if (field->data_size == sizeof(pb_int64_t))
  1145. clamped = *(pb_int64_t*)dest = svalue;
  1146. else if (field->data_size == sizeof(int32_t))
  1147. clamped = *(int32_t*)dest = (int32_t)svalue;
  1148. else if (field->data_size == sizeof(int_least16_t))
  1149. clamped = *(int_least16_t*)dest = (int_least16_t)svalue;
  1150. else if (field->data_size == sizeof(int_least8_t))
  1151. clamped = *(int_least8_t*)dest = (int_least8_t)svalue;
  1152. else
  1153. PB_RETURN_ERROR(stream, "invalid data_size");
  1154. if (clamped != svalue)
  1155. PB_RETURN_ERROR(stream, "integer too large");
  1156. return true;
  1157. }
  1158. static bool checkreturn pb_dec_uvarint(pb_istream_t *stream, const pb_field_t *field, void *dest)
  1159. {
  1160. pb_uint64_t value, clamped;
  1161. if (!pb_decode_varint(stream, &value))
  1162. return false;
  1163. /* Cast to the proper field size, while checking for overflows */
  1164. if (field->data_size == sizeof(pb_uint64_t))
  1165. clamped = *(pb_uint64_t*)dest = value;
  1166. else if (field->data_size == sizeof(uint32_t))
  1167. clamped = *(uint32_t*)dest = (uint32_t)value;
  1168. else if (field->data_size == sizeof(uint_least16_t))
  1169. clamped = *(uint_least16_t*)dest = (uint_least16_t)value;
  1170. else if (field->data_size == sizeof(uint_least8_t))
  1171. clamped = *(uint_least8_t*)dest = (uint_least8_t)value;
  1172. else
  1173. PB_RETURN_ERROR(stream, "invalid data_size");
  1174. if (clamped != value)
  1175. PB_RETURN_ERROR(stream, "integer too large");
  1176. return true;
  1177. }
  1178. static bool checkreturn pb_dec_svarint(pb_istream_t *stream, const pb_field_t *field, void *dest)
  1179. {
  1180. pb_int64_t value, clamped;
  1181. if (!pb_decode_svarint(stream, &value))
  1182. return false;
  1183. /* Cast to the proper field size, while checking for overflows */
  1184. if (field->data_size == sizeof(pb_int64_t))
  1185. clamped = *(pb_int64_t*)dest = value;
  1186. else if (field->data_size == sizeof(int32_t))
  1187. clamped = *(int32_t*)dest = (int32_t)value;
  1188. else if (field->data_size == sizeof(int_least16_t))
  1189. clamped = *(int_least16_t*)dest = (int_least16_t)value;
  1190. else if (field->data_size == sizeof(int_least8_t))
  1191. clamped = *(int_least8_t*)dest = (int_least8_t)value;
  1192. else
  1193. PB_RETURN_ERROR(stream, "invalid data_size");
  1194. if (clamped != value)
  1195. PB_RETURN_ERROR(stream, "integer too large");
  1196. return true;
  1197. }
  1198. static bool checkreturn pb_dec_fixed32(pb_istream_t *stream, const pb_field_t *field, void *dest)
  1199. {
  1200. PB_UNUSED(field);
  1201. return pb_decode_fixed32(stream, dest);
  1202. }
  1203. static bool checkreturn pb_dec_fixed64(pb_istream_t *stream, const pb_field_t *field, void *dest)
  1204. {
  1205. PB_UNUSED(field);
  1206. #ifndef PB_WITHOUT_64BIT
  1207. return pb_decode_fixed64(stream, dest);
  1208. #else
  1209. PB_UNUSED(dest);
  1210. PB_RETURN_ERROR(stream, "no 64bit support");
  1211. #endif
  1212. }
  1213. static bool checkreturn pb_dec_bytes(pb_istream_t *stream, const pb_field_t *field, void *dest)
  1214. {
  1215. uint32_t size;
  1216. size_t alloc_size;
  1217. pb_bytes_array_t *bdest;
  1218. if (!pb_decode_varint32(stream, &size))
  1219. return false;
  1220. if (size > PB_SIZE_MAX)
  1221. PB_RETURN_ERROR(stream, "bytes overflow");
  1222. alloc_size = PB_BYTES_ARRAY_T_ALLOCSIZE(size);
  1223. if (size > alloc_size)
  1224. PB_RETURN_ERROR(stream, "size too large");
  1225. if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
  1226. {
  1227. #ifndef PB_ENABLE_MALLOC
  1228. PB_RETURN_ERROR(stream, "no malloc support");
  1229. #else
  1230. if (!allocate_field(stream, dest, alloc_size, 1))
  1231. return false;
  1232. bdest = *(pb_bytes_array_t**)dest;
  1233. #endif
  1234. }
  1235. else
  1236. {
  1237. if (alloc_size > field->data_size)
  1238. PB_RETURN_ERROR(stream, "bytes overflow");
  1239. bdest = (pb_bytes_array_t*)dest;
  1240. }
  1241. bdest->size = (pb_size_t)size;
  1242. return pb_read(stream, bdest->bytes, size);
  1243. }
  1244. static bool checkreturn pb_dec_string(pb_istream_t *stream, const pb_field_t *field, void *dest)
  1245. {
  1246. uint32_t size;
  1247. size_t alloc_size;
  1248. bool status;
  1249. if (!pb_decode_varint32(stream, &size))
  1250. return false;
  1251. /* Space for null terminator */
  1252. alloc_size = size + 1;
  1253. if (alloc_size < size)
  1254. PB_RETURN_ERROR(stream, "size too large");
  1255. if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
  1256. {
  1257. #ifndef PB_ENABLE_MALLOC
  1258. PB_RETURN_ERROR(stream, "no malloc support");
  1259. #else
  1260. if (!allocate_field(stream, dest, alloc_size, 1))
  1261. return false;
  1262. dest = *(void**)dest;
  1263. #endif
  1264. }
  1265. else
  1266. {
  1267. if (alloc_size > field->data_size)
  1268. PB_RETURN_ERROR(stream, "string overflow");
  1269. }
  1270. status = pb_read(stream, (pb_byte_t*)dest, size);
  1271. *((pb_byte_t*)dest + size) = 0;
  1272. return status;
  1273. }
  1274. static bool checkreturn pb_dec_submessage(pb_istream_t *stream, const pb_field_t *field, void *dest)
  1275. {
  1276. bool status;
  1277. pb_istream_t substream;
  1278. const pb_field_t* submsg_fields = (const pb_field_t*)field->ptr;
  1279. if (!pb_make_string_substream(stream, &substream))
  1280. return false;
  1281. if (field->ptr == NULL)
  1282. PB_RETURN_ERROR(stream, "invalid field descriptor");
  1283. /* New array entries need to be initialized, while required and optional
  1284. * submessages have already been initialized in the top-level pb_decode. */
  1285. if (PB_HTYPE(field->type) == PB_HTYPE_REPEATED)
  1286. status = pb_decode(&substream, submsg_fields, dest);
  1287. else
  1288. status = pb_decode_noinit(&substream, submsg_fields, dest);
  1289. if (!pb_close_string_substream(stream, &substream))
  1290. return false;
  1291. return status;
  1292. }
  1293. static bool checkreturn pb_dec_fixed_length_bytes(pb_istream_t *stream, const pb_field_t *field, void *dest)
  1294. {
  1295. uint32_t size;
  1296. if (!pb_decode_varint32(stream, &size))
  1297. return false;
  1298. if (size > PB_SIZE_MAX)
  1299. PB_RETURN_ERROR(stream, "bytes overflow");
  1300. if (size == 0)
  1301. {
  1302. /* As a special case, treat empty bytes string as all zeros for fixed_length_bytes. */
  1303. memset(dest, 0, field->data_size);
  1304. return true;
  1305. }
  1306. if (size != field->data_size)
  1307. PB_RETURN_ERROR(stream, "incorrect fixed length bytes size");
  1308. return pb_read(stream, (pb_byte_t*)dest, field->data_size);
  1309. }