pb_decode.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570
  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. if (pItem == NULL)
  549. {
  550. /* Shouldn't happen, but satisfies static analyzers */
  551. status = false;
  552. break;
  553. }
  554. initialize_pointer_field(pItem, iter);
  555. if (!func(&substream, iter->pos, pItem))
  556. {
  557. status = false;
  558. break;
  559. }
  560. (*size)++;
  561. }
  562. if (!pb_close_string_substream(stream, &substream))
  563. return false;
  564. return status;
  565. }
  566. else
  567. {
  568. /* Normal repeated field, i.e. only one item at a time. */
  569. pb_size_t *size = (pb_size_t*)iter->pSize;
  570. void *pItem;
  571. if (*size == PB_SIZE_MAX)
  572. PB_RETURN_ERROR(stream, "too many array entries");
  573. if (!allocate_field(stream, iter->pData, iter->pos->data_size, (size_t)(*size + 1)))
  574. return false;
  575. pItem = *(char**)iter->pData + iter->pos->data_size * (*size);
  576. (*size)++;
  577. initialize_pointer_field(pItem, iter);
  578. return func(stream, iter->pos, pItem);
  579. }
  580. default:
  581. PB_RETURN_ERROR(stream, "invalid field type");
  582. }
  583. #endif
  584. }
  585. static bool checkreturn decode_callback_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
  586. {
  587. pb_callback_t *pCallback = (pb_callback_t*)iter->pData;
  588. #ifdef PB_OLD_CALLBACK_STYLE
  589. void *arg;
  590. #else
  591. void **arg;
  592. #endif
  593. if (pCallback == NULL || pCallback->funcs.decode == NULL)
  594. return pb_skip_field(stream, wire_type);
  595. #ifdef PB_OLD_CALLBACK_STYLE
  596. arg = pCallback->arg;
  597. #else
  598. arg = &(pCallback->arg);
  599. #endif
  600. if (wire_type == PB_WT_STRING)
  601. {
  602. pb_istream_t substream;
  603. if (!pb_make_string_substream(stream, &substream))
  604. return false;
  605. do
  606. {
  607. if (!pCallback->funcs.decode(&substream, iter->pos, arg))
  608. PB_RETURN_ERROR(stream, "callback failed");
  609. } while (substream.bytes_left);
  610. if (!pb_close_string_substream(stream, &substream))
  611. return false;
  612. return true;
  613. }
  614. else
  615. {
  616. /* Copy the single scalar value to stack.
  617. * This is required so that we can limit the stream length,
  618. * which in turn allows to use same callback for packed and
  619. * not-packed fields. */
  620. pb_istream_t substream;
  621. pb_byte_t buffer[10];
  622. size_t size = sizeof(buffer);
  623. if (!read_raw_value(stream, wire_type, buffer, &size))
  624. return false;
  625. substream = pb_istream_from_buffer(buffer, size);
  626. return pCallback->funcs.decode(&substream, iter->pos, arg);
  627. }
  628. }
  629. static bool checkreturn decode_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
  630. {
  631. #ifdef PB_ENABLE_MALLOC
  632. /* When decoding an oneof field, check if there is old data that must be
  633. * released first. */
  634. if (PB_HTYPE(iter->pos->type) == PB_HTYPE_ONEOF)
  635. {
  636. if (!pb_release_union_field(stream, iter))
  637. return false;
  638. }
  639. #endif
  640. switch (PB_ATYPE(iter->pos->type))
  641. {
  642. case PB_ATYPE_STATIC:
  643. return decode_static_field(stream, wire_type, iter);
  644. case PB_ATYPE_POINTER:
  645. return decode_pointer_field(stream, wire_type, iter);
  646. case PB_ATYPE_CALLBACK:
  647. return decode_callback_field(stream, wire_type, iter);
  648. default:
  649. PB_RETURN_ERROR(stream, "invalid field type");
  650. }
  651. }
  652. static void iter_from_extension(pb_field_iter_t *iter, pb_extension_t *extension)
  653. {
  654. /* Fake a field iterator for the extension field.
  655. * It is not actually safe to advance this iterator, but decode_field
  656. * will not even try to. */
  657. const pb_field_t *field = (const pb_field_t*)extension->type->arg;
  658. (void)pb_field_iter_begin(iter, field, extension->dest);
  659. iter->pData = extension->dest;
  660. iter->pSize = &extension->found;
  661. if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
  662. {
  663. /* For pointer extensions, the pointer is stored directly
  664. * in the extension structure. This avoids having an extra
  665. * indirection. */
  666. iter->pData = &extension->dest;
  667. }
  668. }
  669. /* Default handler for extension fields. Expects a pb_field_t structure
  670. * in extension->type->arg. */
  671. static bool checkreturn default_extension_decoder(pb_istream_t *stream,
  672. pb_extension_t *extension, uint32_t tag, pb_wire_type_t wire_type)
  673. {
  674. const pb_field_t *field = (const pb_field_t*)extension->type->arg;
  675. pb_field_iter_t iter;
  676. if (field->tag != tag)
  677. return true;
  678. iter_from_extension(&iter, extension);
  679. extension->found = true;
  680. return decode_field(stream, wire_type, &iter);
  681. }
  682. /* Try to decode an unknown field as an extension field. Tries each extension
  683. * decoder in turn, until one of them handles the field or loop ends. */
  684. static bool checkreturn decode_extension(pb_istream_t *stream,
  685. uint32_t tag, pb_wire_type_t wire_type, pb_field_iter_t *iter)
  686. {
  687. pb_extension_t *extension = *(pb_extension_t* const *)iter->pData;
  688. size_t pos = stream->bytes_left;
  689. while (extension != NULL && pos == stream->bytes_left)
  690. {
  691. bool status;
  692. if (extension->type->decode)
  693. status = extension->type->decode(stream, extension, tag, wire_type);
  694. else
  695. status = default_extension_decoder(stream, extension, tag, wire_type);
  696. if (!status)
  697. return false;
  698. extension = extension->next;
  699. }
  700. return true;
  701. }
  702. /* Step through the iterator until an extension field is found or until all
  703. * entries have been checked. There can be only one extension field per
  704. * message. Returns false if no extension field is found. */
  705. static bool checkreturn find_extension_field(pb_field_iter_t *iter)
  706. {
  707. const pb_field_t *start = iter->pos;
  708. do {
  709. if (PB_LTYPE(iter->pos->type) == PB_LTYPE_EXTENSION)
  710. return true;
  711. (void)pb_field_iter_next(iter);
  712. } while (iter->pos != start);
  713. return false;
  714. }
  715. /* Initialize message fields to default values, recursively */
  716. static void pb_field_set_to_default(pb_field_iter_t *iter)
  717. {
  718. pb_type_t type;
  719. type = iter->pos->type;
  720. if (PB_LTYPE(type) == PB_LTYPE_EXTENSION)
  721. {
  722. pb_extension_t *ext = *(pb_extension_t* const *)iter->pData;
  723. while (ext != NULL)
  724. {
  725. pb_field_iter_t ext_iter;
  726. ext->found = false;
  727. iter_from_extension(&ext_iter, ext);
  728. pb_field_set_to_default(&ext_iter);
  729. ext = ext->next;
  730. }
  731. }
  732. else if (PB_ATYPE(type) == PB_ATYPE_STATIC)
  733. {
  734. bool init_data = true;
  735. if (PB_HTYPE(type) == PB_HTYPE_OPTIONAL && iter->pSize != iter->pData)
  736. {
  737. /* Set has_field to false. Still initialize the optional field
  738. * itself also. */
  739. *(bool*)iter->pSize = false;
  740. }
  741. else if (PB_HTYPE(type) == PB_HTYPE_REPEATED ||
  742. PB_HTYPE(type) == PB_HTYPE_ONEOF)
  743. {
  744. /* REPEATED: Set array count to 0, no need to initialize contents.
  745. ONEOF: Set which_field to 0. */
  746. *(pb_size_t*)iter->pSize = 0;
  747. init_data = false;
  748. }
  749. if (init_data)
  750. {
  751. if (PB_LTYPE(iter->pos->type) == PB_LTYPE_SUBMESSAGE)
  752. {
  753. /* Initialize submessage to defaults */
  754. pb_message_set_to_defaults((const pb_field_t *) iter->pos->ptr, iter->pData);
  755. }
  756. else if (iter->pos->ptr != NULL)
  757. {
  758. /* Initialize to default value */
  759. memcpy(iter->pData, iter->pos->ptr, iter->pos->data_size);
  760. }
  761. else
  762. {
  763. /* Initialize to zeros */
  764. memset(iter->pData, 0, iter->pos->data_size);
  765. }
  766. }
  767. }
  768. else if (PB_ATYPE(type) == PB_ATYPE_POINTER)
  769. {
  770. /* Initialize the pointer to NULL. */
  771. *(void**)iter->pData = NULL;
  772. /* Initialize array count to 0. */
  773. if (PB_HTYPE(type) == PB_HTYPE_REPEATED ||
  774. PB_HTYPE(type) == PB_HTYPE_ONEOF)
  775. {
  776. *(pb_size_t*)iter->pSize = 0;
  777. }
  778. }
  779. else if (PB_ATYPE(type) == PB_ATYPE_CALLBACK)
  780. {
  781. /* Don't overwrite callback */
  782. }
  783. }
  784. static void pb_message_set_to_defaults(const pb_field_t fields[], void *dest_struct)
  785. {
  786. pb_field_iter_t iter;
  787. if (!pb_field_iter_begin(&iter, fields, dest_struct))
  788. return; /* Empty message type */
  789. do
  790. {
  791. pb_field_set_to_default(&iter);
  792. } while (pb_field_iter_next(&iter));
  793. }
  794. /*********************
  795. * Decode all fields *
  796. *********************/
  797. bool checkreturn pb_decode_noinit(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
  798. {
  799. uint32_t fields_seen[(PB_MAX_REQUIRED_FIELDS + 31) / 32] = {0, 0};
  800. const uint32_t allbits = ~(uint32_t)0;
  801. uint32_t extension_range_start = 0;
  802. pb_field_iter_t iter;
  803. /* 'fixed_count_field' and 'fixed_count_size' track position of a repeated fixed
  804. * count field. This can only handle _one_ repeated fixed count field that
  805. * is unpacked and unordered among other (non repeated fixed count) fields.
  806. */
  807. const pb_field_t *fixed_count_field = NULL;
  808. pb_size_t fixed_count_size = 0;
  809. /* Return value ignored, as empty message types will be correctly handled by
  810. * pb_field_iter_find() anyway. */
  811. (void)pb_field_iter_begin(&iter, fields, dest_struct);
  812. while (stream->bytes_left)
  813. {
  814. uint32_t tag;
  815. pb_wire_type_t wire_type;
  816. bool eof;
  817. if (!pb_decode_tag(stream, &wire_type, &tag, &eof))
  818. {
  819. if (eof)
  820. break;
  821. else
  822. return false;
  823. }
  824. if (!pb_field_iter_find(&iter, tag))
  825. {
  826. /* No match found, check if it matches an extension. */
  827. if (tag >= extension_range_start)
  828. {
  829. if (!find_extension_field(&iter))
  830. extension_range_start = (uint32_t)-1;
  831. else
  832. extension_range_start = iter.pos->tag;
  833. if (tag >= extension_range_start)
  834. {
  835. size_t pos = stream->bytes_left;
  836. if (!decode_extension(stream, tag, wire_type, &iter))
  837. return false;
  838. if (pos != stream->bytes_left)
  839. {
  840. /* The field was handled */
  841. continue;
  842. }
  843. }
  844. }
  845. /* No match found, skip data */
  846. if (!pb_skip_field(stream, wire_type))
  847. return false;
  848. continue;
  849. }
  850. /* If a repeated fixed count field was found, get size from
  851. * 'fixed_count_field' as there is no counter contained in the struct.
  852. */
  853. if (PB_HTYPE(iter.pos->type) == PB_HTYPE_REPEATED
  854. && iter.pSize == iter.pData)
  855. {
  856. if (fixed_count_field != iter.pos) {
  857. /* If the new fixed count field does not match the previous one,
  858. * check that the previous one is NULL or that it finished
  859. * receiving all the expected data.
  860. */
  861. if (fixed_count_field != NULL &&
  862. fixed_count_size != fixed_count_field->array_size)
  863. {
  864. PB_RETURN_ERROR(stream, "wrong size for fixed count field");
  865. }
  866. fixed_count_field = iter.pos;
  867. fixed_count_size = 0;
  868. }
  869. iter.pSize = &fixed_count_size;
  870. }
  871. if (PB_HTYPE(iter.pos->type) == PB_HTYPE_REQUIRED
  872. && iter.required_field_index < PB_MAX_REQUIRED_FIELDS)
  873. {
  874. uint32_t tmp = ((uint32_t)1 << (iter.required_field_index & 31));
  875. fields_seen[iter.required_field_index >> 5] |= tmp;
  876. }
  877. if (!decode_field(stream, wire_type, &iter))
  878. return false;
  879. }
  880. /* Check that all elements of the last decoded fixed count field were present. */
  881. if (fixed_count_field != NULL &&
  882. fixed_count_size != fixed_count_field->array_size)
  883. {
  884. PB_RETURN_ERROR(stream, "wrong size for fixed count field");
  885. }
  886. /* Check that all required fields were present. */
  887. {
  888. /* First figure out the number of required fields by
  889. * seeking to the end of the field array. Usually we
  890. * are already close to end after decoding.
  891. */
  892. unsigned req_field_count;
  893. pb_type_t last_type;
  894. unsigned i;
  895. do {
  896. req_field_count = iter.required_field_index;
  897. last_type = iter.pos->type;
  898. } while (pb_field_iter_next(&iter));
  899. /* Fixup if last field was also required. */
  900. if (PB_HTYPE(last_type) == PB_HTYPE_REQUIRED && iter.pos->tag != 0)
  901. req_field_count++;
  902. if (req_field_count > PB_MAX_REQUIRED_FIELDS)
  903. req_field_count = PB_MAX_REQUIRED_FIELDS;
  904. if (req_field_count > 0)
  905. {
  906. /* Check the whole words */
  907. for (i = 0; i < (req_field_count >> 5); i++)
  908. {
  909. if (fields_seen[i] != allbits)
  910. PB_RETURN_ERROR(stream, "missing required field");
  911. }
  912. /* Check the remaining bits (if any) */
  913. if ((req_field_count & 31) != 0)
  914. {
  915. if (fields_seen[req_field_count >> 5] !=
  916. (allbits >> (32 - (req_field_count & 31))))
  917. {
  918. PB_RETURN_ERROR(stream, "missing required field");
  919. }
  920. }
  921. }
  922. }
  923. return true;
  924. }
  925. bool checkreturn pb_decode(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
  926. {
  927. bool status;
  928. pb_message_set_to_defaults(fields, dest_struct);
  929. status = pb_decode_noinit(stream, fields, dest_struct);
  930. #ifdef PB_ENABLE_MALLOC
  931. if (!status)
  932. pb_release(fields, dest_struct);
  933. #endif
  934. return status;
  935. }
  936. bool pb_decode_delimited_noinit(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
  937. {
  938. pb_istream_t substream;
  939. bool status;
  940. if (!pb_make_string_substream(stream, &substream))
  941. return false;
  942. status = pb_decode_noinit(&substream, fields, dest_struct);
  943. if (!pb_close_string_substream(stream, &substream))
  944. return false;
  945. return status;
  946. }
  947. bool pb_decode_delimited(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
  948. {
  949. pb_istream_t substream;
  950. bool status;
  951. if (!pb_make_string_substream(stream, &substream))
  952. return false;
  953. status = pb_decode(&substream, fields, dest_struct);
  954. if (!pb_close_string_substream(stream, &substream))
  955. return false;
  956. return status;
  957. }
  958. bool pb_decode_nullterminated(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
  959. {
  960. /* This behaviour will be separated in nanopb-0.4.0, see issue #278. */
  961. return pb_decode(stream, fields, dest_struct);
  962. }
  963. #ifdef PB_ENABLE_MALLOC
  964. /* Given an oneof field, if there has already been a field inside this oneof,
  965. * release it before overwriting with a different one. */
  966. static bool pb_release_union_field(pb_istream_t *stream, pb_field_iter_t *iter)
  967. {
  968. pb_size_t old_tag = *(pb_size_t*)iter->pSize; /* Previous which_ value */
  969. pb_size_t new_tag = iter->pos->tag; /* New which_ value */
  970. if (old_tag == 0)
  971. return true; /* Ok, no old data in union */
  972. if (old_tag == new_tag)
  973. return true; /* Ok, old data is of same type => merge */
  974. /* Release old data. The find can fail if the message struct contains
  975. * invalid data. */
  976. if (!pb_field_iter_find(iter, old_tag))
  977. PB_RETURN_ERROR(stream, "invalid union tag");
  978. pb_release_single_field(iter);
  979. /* Restore iterator to where it should be.
  980. * This shouldn't fail unless the pb_field_t structure is corrupted. */
  981. if (!pb_field_iter_find(iter, new_tag))
  982. PB_RETURN_ERROR(stream, "iterator error");
  983. if (PB_ATYPE(iter->pos->type) == PB_ATYPE_POINTER)
  984. {
  985. /* Initialize the pointer to NULL to make sure it is valid
  986. * even in case of error return. */
  987. *(void**)iter->pData = NULL;
  988. }
  989. return true;
  990. }
  991. static void pb_release_single_field(const pb_field_iter_t *iter)
  992. {
  993. pb_type_t type;
  994. type = iter->pos->type;
  995. if (PB_HTYPE(type) == PB_HTYPE_ONEOF)
  996. {
  997. if (*(pb_size_t*)iter->pSize != iter->pos->tag)
  998. return; /* This is not the current field in the union */
  999. }
  1000. /* Release anything contained inside an extension or submsg.
  1001. * This has to be done even if the submsg itself is statically
  1002. * allocated. */
  1003. if (PB_LTYPE(type) == PB_LTYPE_EXTENSION)
  1004. {
  1005. /* Release fields from all extensions in the linked list */
  1006. pb_extension_t *ext = *(pb_extension_t**)iter->pData;
  1007. while (ext != NULL)
  1008. {
  1009. pb_field_iter_t ext_iter;
  1010. iter_from_extension(&ext_iter, ext);
  1011. pb_release_single_field(&ext_iter);
  1012. ext = ext->next;
  1013. }
  1014. }
  1015. else if (PB_LTYPE(type) == PB_LTYPE_SUBMESSAGE && PB_ATYPE(type) != PB_ATYPE_CALLBACK)
  1016. {
  1017. /* Release fields in submessage or submsg array */
  1018. void *pItem = iter->pData;
  1019. pb_size_t count = 1;
  1020. if (PB_ATYPE(type) == PB_ATYPE_POINTER)
  1021. {
  1022. pItem = *(void**)iter->pData;
  1023. }
  1024. if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
  1025. {
  1026. if (PB_ATYPE(type) == PB_ATYPE_STATIC && iter->pSize == iter->pData) {
  1027. /* No _count field so use size of the array */
  1028. count = iter->pos->array_size;
  1029. } else {
  1030. count = *(pb_size_t*)iter->pSize;
  1031. }
  1032. if (PB_ATYPE(type) == PB_ATYPE_STATIC && count > iter->pos->array_size)
  1033. {
  1034. /* Protect against corrupted _count fields */
  1035. count = iter->pos->array_size;
  1036. }
  1037. }
  1038. if (pItem)
  1039. {
  1040. for (; count > 0; count--)
  1041. {
  1042. pb_release((const pb_field_t*)iter->pos->ptr, pItem);
  1043. pItem = (char*)pItem + iter->pos->data_size;
  1044. }
  1045. }
  1046. }
  1047. if (PB_ATYPE(type) == PB_ATYPE_POINTER)
  1048. {
  1049. if (PB_HTYPE(type) == PB_HTYPE_REPEATED &&
  1050. (PB_LTYPE(type) == PB_LTYPE_STRING ||
  1051. PB_LTYPE(type) == PB_LTYPE_BYTES))
  1052. {
  1053. /* Release entries in repeated string or bytes array */
  1054. void **pItem = *(void***)iter->pData;
  1055. pb_size_t count = *(pb_size_t*)iter->pSize;
  1056. for (; count > 0; count--)
  1057. {
  1058. pb_free(*pItem);
  1059. *pItem++ = NULL;
  1060. }
  1061. }
  1062. if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
  1063. {
  1064. /* We are going to release the array, so set the size to 0 */
  1065. *(pb_size_t*)iter->pSize = 0;
  1066. }
  1067. /* Release main item */
  1068. pb_free(*(void**)iter->pData);
  1069. *(void**)iter->pData = NULL;
  1070. }
  1071. }
  1072. void pb_release(const pb_field_t fields[], void *dest_struct)
  1073. {
  1074. pb_field_iter_t iter;
  1075. if (!dest_struct)
  1076. return; /* Ignore NULL pointers, similar to free() */
  1077. if (!pb_field_iter_begin(&iter, fields, dest_struct))
  1078. return; /* Empty message type */
  1079. do
  1080. {
  1081. pb_release_single_field(&iter);
  1082. } while (pb_field_iter_next(&iter));
  1083. }
  1084. #endif
  1085. /* Field decoders */
  1086. bool pb_decode_bool(pb_istream_t *stream, bool *dest)
  1087. {
  1088. return pb_dec_bool(stream, NULL, (void*)dest);
  1089. }
  1090. bool pb_decode_svarint(pb_istream_t *stream, pb_int64_t *dest)
  1091. {
  1092. pb_uint64_t value;
  1093. if (!pb_decode_varint(stream, &value))
  1094. return false;
  1095. if (value & 1)
  1096. *dest = (pb_int64_t)(~(value >> 1));
  1097. else
  1098. *dest = (pb_int64_t)(value >> 1);
  1099. return true;
  1100. }
  1101. bool pb_decode_fixed32(pb_istream_t *stream, void *dest)
  1102. {
  1103. pb_byte_t bytes[4];
  1104. if (!pb_read(stream, bytes, 4))
  1105. return false;
  1106. *(uint32_t*)dest = ((uint32_t)bytes[0] << 0) |
  1107. ((uint32_t)bytes[1] << 8) |
  1108. ((uint32_t)bytes[2] << 16) |
  1109. ((uint32_t)bytes[3] << 24);
  1110. return true;
  1111. }
  1112. #ifndef PB_WITHOUT_64BIT
  1113. bool pb_decode_fixed64(pb_istream_t *stream, void *dest)
  1114. {
  1115. pb_byte_t bytes[8];
  1116. if (!pb_read(stream, bytes, 8))
  1117. return false;
  1118. *(uint64_t*)dest = ((uint64_t)bytes[0] << 0) |
  1119. ((uint64_t)bytes[1] << 8) |
  1120. ((uint64_t)bytes[2] << 16) |
  1121. ((uint64_t)bytes[3] << 24) |
  1122. ((uint64_t)bytes[4] << 32) |
  1123. ((uint64_t)bytes[5] << 40) |
  1124. ((uint64_t)bytes[6] << 48) |
  1125. ((uint64_t)bytes[7] << 56);
  1126. return true;
  1127. }
  1128. #endif
  1129. static bool checkreturn pb_dec_bool(pb_istream_t *stream, const pb_field_t *field, void *dest)
  1130. {
  1131. uint32_t value;
  1132. PB_UNUSED(field);
  1133. if (!pb_decode_varint32(stream, &value))
  1134. return false;
  1135. *(bool*)dest = (value != 0);
  1136. return true;
  1137. }
  1138. static bool checkreturn pb_dec_varint(pb_istream_t *stream, const pb_field_t *field, void *dest)
  1139. {
  1140. pb_uint64_t value;
  1141. pb_int64_t svalue;
  1142. pb_int64_t clamped;
  1143. if (!pb_decode_varint(stream, &value))
  1144. return false;
  1145. /* See issue 97: Google's C++ protobuf allows negative varint values to
  1146. * be cast as int32_t, instead of the int64_t that should be used when
  1147. * encoding. Previous nanopb versions had a bug in encoding. In order to
  1148. * not break decoding of such messages, we cast <=32 bit fields to
  1149. * int32_t first to get the sign correct.
  1150. */
  1151. if (field->data_size == sizeof(pb_int64_t))
  1152. svalue = (pb_int64_t)value;
  1153. else
  1154. svalue = (int32_t)value;
  1155. /* Cast to the proper field size, while checking for overflows */
  1156. if (field->data_size == sizeof(pb_int64_t))
  1157. clamped = *(pb_int64_t*)dest = svalue;
  1158. else if (field->data_size == sizeof(int32_t))
  1159. clamped = *(int32_t*)dest = (int32_t)svalue;
  1160. else if (field->data_size == sizeof(int_least16_t))
  1161. clamped = *(int_least16_t*)dest = (int_least16_t)svalue;
  1162. else if (field->data_size == sizeof(int_least8_t))
  1163. clamped = *(int_least8_t*)dest = (int_least8_t)svalue;
  1164. else
  1165. PB_RETURN_ERROR(stream, "invalid data_size");
  1166. if (clamped != svalue)
  1167. PB_RETURN_ERROR(stream, "integer too large");
  1168. return true;
  1169. }
  1170. static bool checkreturn pb_dec_uvarint(pb_istream_t *stream, const pb_field_t *field, void *dest)
  1171. {
  1172. pb_uint64_t value, clamped;
  1173. if (!pb_decode_varint(stream, &value))
  1174. return false;
  1175. /* Cast to the proper field size, while checking for overflows */
  1176. if (field->data_size == sizeof(pb_uint64_t))
  1177. clamped = *(pb_uint64_t*)dest = value;
  1178. else if (field->data_size == sizeof(uint32_t))
  1179. clamped = *(uint32_t*)dest = (uint32_t)value;
  1180. else if (field->data_size == sizeof(uint_least16_t))
  1181. clamped = *(uint_least16_t*)dest = (uint_least16_t)value;
  1182. else if (field->data_size == sizeof(uint_least8_t))
  1183. clamped = *(uint_least8_t*)dest = (uint_least8_t)value;
  1184. else
  1185. PB_RETURN_ERROR(stream, "invalid data_size");
  1186. if (clamped != value)
  1187. PB_RETURN_ERROR(stream, "integer too large");
  1188. return true;
  1189. }
  1190. static bool checkreturn pb_dec_svarint(pb_istream_t *stream, const pb_field_t *field, void *dest)
  1191. {
  1192. pb_int64_t value, clamped;
  1193. if (!pb_decode_svarint(stream, &value))
  1194. return false;
  1195. /* Cast to the proper field size, while checking for overflows */
  1196. if (field->data_size == sizeof(pb_int64_t))
  1197. clamped = *(pb_int64_t*)dest = value;
  1198. else if (field->data_size == sizeof(int32_t))
  1199. clamped = *(int32_t*)dest = (int32_t)value;
  1200. else if (field->data_size == sizeof(int_least16_t))
  1201. clamped = *(int_least16_t*)dest = (int_least16_t)value;
  1202. else if (field->data_size == sizeof(int_least8_t))
  1203. clamped = *(int_least8_t*)dest = (int_least8_t)value;
  1204. else
  1205. PB_RETURN_ERROR(stream, "invalid data_size");
  1206. if (clamped != value)
  1207. PB_RETURN_ERROR(stream, "integer too large");
  1208. return true;
  1209. }
  1210. static bool checkreturn pb_dec_fixed32(pb_istream_t *stream, const pb_field_t *field, void *dest)
  1211. {
  1212. PB_UNUSED(field);
  1213. return pb_decode_fixed32(stream, dest);
  1214. }
  1215. static bool checkreturn pb_dec_fixed64(pb_istream_t *stream, const pb_field_t *field, void *dest)
  1216. {
  1217. PB_UNUSED(field);
  1218. #ifndef PB_WITHOUT_64BIT
  1219. return pb_decode_fixed64(stream, dest);
  1220. #else
  1221. PB_UNUSED(dest);
  1222. PB_RETURN_ERROR(stream, "no 64bit support");
  1223. #endif
  1224. }
  1225. static bool checkreturn pb_dec_bytes(pb_istream_t *stream, const pb_field_t *field, void *dest)
  1226. {
  1227. uint32_t size;
  1228. size_t alloc_size;
  1229. pb_bytes_array_t *bdest;
  1230. if (!pb_decode_varint32(stream, &size))
  1231. return false;
  1232. if (size > PB_SIZE_MAX)
  1233. PB_RETURN_ERROR(stream, "bytes overflow");
  1234. alloc_size = PB_BYTES_ARRAY_T_ALLOCSIZE(size);
  1235. if (size > alloc_size)
  1236. PB_RETURN_ERROR(stream, "size too large");
  1237. if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
  1238. {
  1239. #ifndef PB_ENABLE_MALLOC
  1240. PB_RETURN_ERROR(stream, "no malloc support");
  1241. #else
  1242. if (stream->bytes_left < size)
  1243. PB_RETURN_ERROR(stream, "end-of-stream");
  1244. if (!allocate_field(stream, dest, alloc_size, 1))
  1245. return false;
  1246. bdest = *(pb_bytes_array_t**)dest;
  1247. #endif
  1248. }
  1249. else
  1250. {
  1251. if (alloc_size > field->data_size)
  1252. PB_RETURN_ERROR(stream, "bytes overflow");
  1253. bdest = (pb_bytes_array_t*)dest;
  1254. }
  1255. bdest->size = (pb_size_t)size;
  1256. return pb_read(stream, bdest->bytes, size);
  1257. }
  1258. static bool checkreturn pb_dec_string(pb_istream_t *stream, const pb_field_t *field, void *dest)
  1259. {
  1260. uint32_t size;
  1261. size_t alloc_size;
  1262. bool status;
  1263. if (!pb_decode_varint32(stream, &size))
  1264. return false;
  1265. /* Space for null terminator */
  1266. alloc_size = size + 1;
  1267. if (alloc_size < size)
  1268. PB_RETURN_ERROR(stream, "size too large");
  1269. if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
  1270. {
  1271. #ifndef PB_ENABLE_MALLOC
  1272. PB_RETURN_ERROR(stream, "no malloc support");
  1273. #else
  1274. if (stream->bytes_left < size)
  1275. PB_RETURN_ERROR(stream, "end-of-stream");
  1276. if (!allocate_field(stream, dest, alloc_size, 1))
  1277. return false;
  1278. dest = *(void**)dest;
  1279. #endif
  1280. }
  1281. else
  1282. {
  1283. if (alloc_size > field->data_size)
  1284. PB_RETURN_ERROR(stream, "string overflow");
  1285. }
  1286. status = pb_read(stream, (pb_byte_t*)dest, size);
  1287. *((pb_byte_t*)dest + size) = 0;
  1288. return status;
  1289. }
  1290. static bool checkreturn pb_dec_submessage(pb_istream_t *stream, const pb_field_t *field, void *dest)
  1291. {
  1292. bool status;
  1293. pb_istream_t substream;
  1294. const pb_field_t* submsg_fields = (const pb_field_t*)field->ptr;
  1295. if (!pb_make_string_substream(stream, &substream))
  1296. return false;
  1297. if (field->ptr == NULL)
  1298. PB_RETURN_ERROR(stream, "invalid field descriptor");
  1299. /* New array entries need to be initialized, while required and optional
  1300. * submessages have already been initialized in the top-level pb_decode. */
  1301. if (PB_HTYPE(field->type) == PB_HTYPE_REPEATED)
  1302. status = pb_decode(&substream, submsg_fields, dest);
  1303. else
  1304. status = pb_decode_noinit(&substream, submsg_fields, dest);
  1305. if (!pb_close_string_substream(stream, &substream))
  1306. return false;
  1307. return status;
  1308. }
  1309. static bool checkreturn pb_dec_fixed_length_bytes(pb_istream_t *stream, const pb_field_t *field, void *dest)
  1310. {
  1311. uint32_t size;
  1312. if (!pb_decode_varint32(stream, &size))
  1313. return false;
  1314. if (size > PB_SIZE_MAX)
  1315. PB_RETURN_ERROR(stream, "bytes overflow");
  1316. if (size == 0)
  1317. {
  1318. /* As a special case, treat empty bytes string as all zeros for fixed_length_bytes. */
  1319. memset(dest, 0, field->data_size);
  1320. return true;
  1321. }
  1322. if (size != field->data_size)
  1323. PB_RETURN_ERROR(stream, "incorrect fixed length bytes size");
  1324. return pb_read(stream, (pb_byte_t*)dest, field->data_size);
  1325. }