暂无描述

pb_decode.c 46KB

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