You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1548 lines
47 KiB

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