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.

1379 lines
42 KiB

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