pb_decode.c 41 KB

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