pb_decode.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564
  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. if (PB_ATYPE(iter->pos->type) == PB_ATYPE_POINTER)
  978. {
  979. /* Initialize the pointer to NULL to make sure it is valid
  980. * even in case of error return. */
  981. *(void**)iter->pData = NULL;
  982. }
  983. return true;
  984. }
  985. static void pb_release_single_field(const pb_field_iter_t *iter)
  986. {
  987. pb_type_t type;
  988. type = iter->pos->type;
  989. if (PB_HTYPE(type) == PB_HTYPE_ONEOF)
  990. {
  991. if (*(pb_size_t*)iter->pSize != iter->pos->tag)
  992. return; /* This is not the current field in the union */
  993. }
  994. /* Release anything contained inside an extension or submsg.
  995. * This has to be done even if the submsg itself is statically
  996. * allocated. */
  997. if (PB_LTYPE(type) == PB_LTYPE_EXTENSION)
  998. {
  999. /* Release fields from all extensions in the linked list */
  1000. pb_extension_t *ext = *(pb_extension_t**)iter->pData;
  1001. while (ext != NULL)
  1002. {
  1003. pb_field_iter_t ext_iter;
  1004. iter_from_extension(&ext_iter, ext);
  1005. pb_release_single_field(&ext_iter);
  1006. ext = ext->next;
  1007. }
  1008. }
  1009. else if (PB_LTYPE(type) == PB_LTYPE_SUBMESSAGE && PB_ATYPE(type) != PB_ATYPE_CALLBACK)
  1010. {
  1011. /* Release fields in submessage or submsg array */
  1012. void *pItem = iter->pData;
  1013. pb_size_t count = 1;
  1014. if (PB_ATYPE(type) == PB_ATYPE_POINTER)
  1015. {
  1016. pItem = *(void**)iter->pData;
  1017. }
  1018. if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
  1019. {
  1020. if (PB_ATYPE(type) == PB_ATYPE_STATIC && iter->pSize == iter->pData) {
  1021. /* No _count field so use size of the array */
  1022. count = iter->pos->array_size;
  1023. } else {
  1024. count = *(pb_size_t*)iter->pSize;
  1025. }
  1026. if (PB_ATYPE(type) == PB_ATYPE_STATIC && count > iter->pos->array_size)
  1027. {
  1028. /* Protect against corrupted _count fields */
  1029. count = iter->pos->array_size;
  1030. }
  1031. }
  1032. if (pItem)
  1033. {
  1034. while (count--)
  1035. {
  1036. pb_release((const pb_field_t*)iter->pos->ptr, pItem);
  1037. pItem = (char*)pItem + iter->pos->data_size;
  1038. }
  1039. }
  1040. }
  1041. if (PB_ATYPE(type) == PB_ATYPE_POINTER)
  1042. {
  1043. if (PB_HTYPE(type) == PB_HTYPE_REPEATED &&
  1044. (PB_LTYPE(type) == PB_LTYPE_STRING ||
  1045. PB_LTYPE(type) == PB_LTYPE_BYTES))
  1046. {
  1047. /* Release entries in repeated string or bytes array */
  1048. void **pItem = *(void***)iter->pData;
  1049. pb_size_t count = *(pb_size_t*)iter->pSize;
  1050. while (count--)
  1051. {
  1052. pb_free(*pItem);
  1053. *pItem++ = NULL;
  1054. }
  1055. }
  1056. if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
  1057. {
  1058. /* We are going to release the array, so set the size to 0 */
  1059. *(pb_size_t*)iter->pSize = 0;
  1060. }
  1061. /* Release main item */
  1062. pb_free(*(void**)iter->pData);
  1063. *(void**)iter->pData = NULL;
  1064. }
  1065. }
  1066. void pb_release(const pb_field_t fields[], void *dest_struct)
  1067. {
  1068. pb_field_iter_t iter;
  1069. if (!dest_struct)
  1070. return; /* Ignore NULL pointers, similar to free() */
  1071. if (!pb_field_iter_begin(&iter, fields, dest_struct))
  1072. return; /* Empty message type */
  1073. do
  1074. {
  1075. pb_release_single_field(&iter);
  1076. } while (pb_field_iter_next(&iter));
  1077. }
  1078. #endif
  1079. /* Field decoders */
  1080. bool pb_decode_bool(pb_istream_t *stream, bool *dest)
  1081. {
  1082. return pb_dec_bool(stream, NULL, (void*)dest);
  1083. }
  1084. bool pb_decode_svarint(pb_istream_t *stream, pb_int64_t *dest)
  1085. {
  1086. pb_uint64_t value;
  1087. if (!pb_decode_varint(stream, &value))
  1088. return false;
  1089. if (value & 1)
  1090. *dest = (pb_int64_t)(~(value >> 1));
  1091. else
  1092. *dest = (pb_int64_t)(value >> 1);
  1093. return true;
  1094. }
  1095. bool pb_decode_fixed32(pb_istream_t *stream, void *dest)
  1096. {
  1097. pb_byte_t bytes[4];
  1098. if (!pb_read(stream, bytes, 4))
  1099. return false;
  1100. *(uint32_t*)dest = ((uint32_t)bytes[0] << 0) |
  1101. ((uint32_t)bytes[1] << 8) |
  1102. ((uint32_t)bytes[2] << 16) |
  1103. ((uint32_t)bytes[3] << 24);
  1104. return true;
  1105. }
  1106. #ifndef PB_WITHOUT_64BIT
  1107. bool pb_decode_fixed64(pb_istream_t *stream, void *dest)
  1108. {
  1109. pb_byte_t bytes[8];
  1110. if (!pb_read(stream, bytes, 8))
  1111. return false;
  1112. *(uint64_t*)dest = ((uint64_t)bytes[0] << 0) |
  1113. ((uint64_t)bytes[1] << 8) |
  1114. ((uint64_t)bytes[2] << 16) |
  1115. ((uint64_t)bytes[3] << 24) |
  1116. ((uint64_t)bytes[4] << 32) |
  1117. ((uint64_t)bytes[5] << 40) |
  1118. ((uint64_t)bytes[6] << 48) |
  1119. ((uint64_t)bytes[7] << 56);
  1120. return true;
  1121. }
  1122. #endif
  1123. static bool checkreturn pb_dec_bool(pb_istream_t *stream, const pb_field_t *field, void *dest)
  1124. {
  1125. uint32_t value;
  1126. PB_UNUSED(field);
  1127. if (!pb_decode_varint32(stream, &value))
  1128. return false;
  1129. *(bool*)dest = (value != 0);
  1130. return true;
  1131. }
  1132. static bool checkreturn pb_dec_varint(pb_istream_t *stream, const pb_field_t *field, void *dest)
  1133. {
  1134. pb_uint64_t value;
  1135. pb_int64_t svalue;
  1136. pb_int64_t clamped;
  1137. if (!pb_decode_varint(stream, &value))
  1138. return false;
  1139. /* See issue 97: Google's C++ protobuf allows negative varint values to
  1140. * be cast as int32_t, instead of the int64_t that should be used when
  1141. * encoding. Previous nanopb versions had a bug in encoding. In order to
  1142. * not break decoding of such messages, we cast <=32 bit fields to
  1143. * int32_t first to get the sign correct.
  1144. */
  1145. if (field->data_size == sizeof(pb_int64_t))
  1146. svalue = (pb_int64_t)value;
  1147. else
  1148. svalue = (int32_t)value;
  1149. /* Cast to the proper field size, while checking for overflows */
  1150. if (field->data_size == sizeof(pb_int64_t))
  1151. clamped = *(pb_int64_t*)dest = svalue;
  1152. else if (field->data_size == sizeof(int32_t))
  1153. clamped = *(int32_t*)dest = (int32_t)svalue;
  1154. else if (field->data_size == sizeof(int_least16_t))
  1155. clamped = *(int_least16_t*)dest = (int_least16_t)svalue;
  1156. else if (field->data_size == sizeof(int_least8_t))
  1157. clamped = *(int_least8_t*)dest = (int_least8_t)svalue;
  1158. else
  1159. PB_RETURN_ERROR(stream, "invalid data_size");
  1160. if (clamped != svalue)
  1161. PB_RETURN_ERROR(stream, "integer too large");
  1162. return true;
  1163. }
  1164. static bool checkreturn pb_dec_uvarint(pb_istream_t *stream, const pb_field_t *field, void *dest)
  1165. {
  1166. pb_uint64_t value, clamped;
  1167. if (!pb_decode_varint(stream, &value))
  1168. return false;
  1169. /* Cast to the proper field size, while checking for overflows */
  1170. if (field->data_size == sizeof(pb_uint64_t))
  1171. clamped = *(pb_uint64_t*)dest = value;
  1172. else if (field->data_size == sizeof(uint32_t))
  1173. clamped = *(uint32_t*)dest = (uint32_t)value;
  1174. else if (field->data_size == sizeof(uint_least16_t))
  1175. clamped = *(uint_least16_t*)dest = (uint_least16_t)value;
  1176. else if (field->data_size == sizeof(uint_least8_t))
  1177. clamped = *(uint_least8_t*)dest = (uint_least8_t)value;
  1178. else
  1179. PB_RETURN_ERROR(stream, "invalid data_size");
  1180. if (clamped != value)
  1181. PB_RETURN_ERROR(stream, "integer too large");
  1182. return true;
  1183. }
  1184. static bool checkreturn pb_dec_svarint(pb_istream_t *stream, const pb_field_t *field, void *dest)
  1185. {
  1186. pb_int64_t value, clamped;
  1187. if (!pb_decode_svarint(stream, &value))
  1188. return false;
  1189. /* Cast to the proper field size, while checking for overflows */
  1190. if (field->data_size == sizeof(pb_int64_t))
  1191. clamped = *(pb_int64_t*)dest = value;
  1192. else if (field->data_size == sizeof(int32_t))
  1193. clamped = *(int32_t*)dest = (int32_t)value;
  1194. else if (field->data_size == sizeof(int_least16_t))
  1195. clamped = *(int_least16_t*)dest = (int_least16_t)value;
  1196. else if (field->data_size == sizeof(int_least8_t))
  1197. clamped = *(int_least8_t*)dest = (int_least8_t)value;
  1198. else
  1199. PB_RETURN_ERROR(stream, "invalid data_size");
  1200. if (clamped != value)
  1201. PB_RETURN_ERROR(stream, "integer too large");
  1202. return true;
  1203. }
  1204. static bool checkreturn pb_dec_fixed32(pb_istream_t *stream, const pb_field_t *field, void *dest)
  1205. {
  1206. PB_UNUSED(field);
  1207. return pb_decode_fixed32(stream, dest);
  1208. }
  1209. static bool checkreturn pb_dec_fixed64(pb_istream_t *stream, const pb_field_t *field, void *dest)
  1210. {
  1211. PB_UNUSED(field);
  1212. #ifndef PB_WITHOUT_64BIT
  1213. return pb_decode_fixed64(stream, dest);
  1214. #else
  1215. PB_UNUSED(dest);
  1216. PB_RETURN_ERROR(stream, "no 64bit support");
  1217. #endif
  1218. }
  1219. static bool checkreturn pb_dec_bytes(pb_istream_t *stream, const pb_field_t *field, void *dest)
  1220. {
  1221. uint32_t size;
  1222. size_t alloc_size;
  1223. pb_bytes_array_t *bdest;
  1224. if (!pb_decode_varint32(stream, &size))
  1225. return false;
  1226. if (size > PB_SIZE_MAX)
  1227. PB_RETURN_ERROR(stream, "bytes overflow");
  1228. alloc_size = PB_BYTES_ARRAY_T_ALLOCSIZE(size);
  1229. if (size > alloc_size)
  1230. PB_RETURN_ERROR(stream, "size too large");
  1231. if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
  1232. {
  1233. #ifndef PB_ENABLE_MALLOC
  1234. PB_RETURN_ERROR(stream, "no malloc support");
  1235. #else
  1236. if (stream->bytes_left < size)
  1237. PB_RETURN_ERROR(stream, "end-of-stream");
  1238. if (!allocate_field(stream, dest, alloc_size, 1))
  1239. return false;
  1240. bdest = *(pb_bytes_array_t**)dest;
  1241. #endif
  1242. }
  1243. else
  1244. {
  1245. if (alloc_size > field->data_size)
  1246. PB_RETURN_ERROR(stream, "bytes overflow");
  1247. bdest = (pb_bytes_array_t*)dest;
  1248. }
  1249. bdest->size = (pb_size_t)size;
  1250. return pb_read(stream, bdest->bytes, size);
  1251. }
  1252. static bool checkreturn pb_dec_string(pb_istream_t *stream, const pb_field_t *field, void *dest)
  1253. {
  1254. uint32_t size;
  1255. size_t alloc_size;
  1256. bool status;
  1257. if (!pb_decode_varint32(stream, &size))
  1258. return false;
  1259. /* Space for null terminator */
  1260. alloc_size = size + 1;
  1261. if (alloc_size < size)
  1262. PB_RETURN_ERROR(stream, "size too large");
  1263. if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
  1264. {
  1265. #ifndef PB_ENABLE_MALLOC
  1266. PB_RETURN_ERROR(stream, "no malloc support");
  1267. #else
  1268. if (stream->bytes_left < size)
  1269. PB_RETURN_ERROR(stream, "end-of-stream");
  1270. if (!allocate_field(stream, dest, alloc_size, 1))
  1271. return false;
  1272. dest = *(void**)dest;
  1273. #endif
  1274. }
  1275. else
  1276. {
  1277. if (alloc_size > field->data_size)
  1278. PB_RETURN_ERROR(stream, "string overflow");
  1279. }
  1280. status = pb_read(stream, (pb_byte_t*)dest, size);
  1281. *((pb_byte_t*)dest + size) = 0;
  1282. return status;
  1283. }
  1284. static bool checkreturn pb_dec_submessage(pb_istream_t *stream, const pb_field_t *field, void *dest)
  1285. {
  1286. bool status;
  1287. pb_istream_t substream;
  1288. const pb_field_t* submsg_fields = (const pb_field_t*)field->ptr;
  1289. if (!pb_make_string_substream(stream, &substream))
  1290. return false;
  1291. if (field->ptr == NULL)
  1292. PB_RETURN_ERROR(stream, "invalid field descriptor");
  1293. /* New array entries need to be initialized, while required and optional
  1294. * submessages have already been initialized in the top-level pb_decode. */
  1295. if (PB_HTYPE(field->type) == PB_HTYPE_REPEATED)
  1296. status = pb_decode(&substream, submsg_fields, dest);
  1297. else
  1298. status = pb_decode_noinit(&substream, submsg_fields, dest);
  1299. if (!pb_close_string_substream(stream, &substream))
  1300. return false;
  1301. return status;
  1302. }
  1303. static bool checkreturn pb_dec_fixed_length_bytes(pb_istream_t *stream, const pb_field_t *field, void *dest)
  1304. {
  1305. uint32_t size;
  1306. if (!pb_decode_varint32(stream, &size))
  1307. return false;
  1308. if (size > PB_SIZE_MAX)
  1309. PB_RETURN_ERROR(stream, "bytes overflow");
  1310. if (size == 0)
  1311. {
  1312. /* As a special case, treat empty bytes string as all zeros for fixed_length_bytes. */
  1313. memset(dest, 0, field->data_size);
  1314. return true;
  1315. }
  1316. if (size != field->data_size)
  1317. PB_RETURN_ERROR(stream, "incorrect fixed length bytes size");
  1318. return pb_read(stream, (pb_byte_t*)dest, field->data_size);
  1319. }