2
0

pb_decode.c 52 KB

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