pb_decode.c 52 KB

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