pb_decode.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165
  1. /* pb_decode.c -- decode a protobuf using minimal resources
  2. *
  3. * 2011 Petteri Aimonen <jpa@kapsi.fi>
  4. */
  5. /* Use the GCC warn_unused_result attribute to check that all return values
  6. * are propagated correctly. On other compilers and gcc before 3.4.0 just
  7. * ignore the annotation.
  8. */
  9. #if !defined(__GNUC__) || ( __GNUC__ < 3) || (__GNUC__ == 3 && __GNUC_MINOR__ < 4)
  10. #define checkreturn
  11. #else
  12. #define checkreturn __attribute__((warn_unused_result))
  13. #endif
  14. #include "pb.h"
  15. #include "pb_decode.h"
  16. #include "pb_common.h"
  17. /**************************************
  18. * Declarations internal to this file *
  19. **************************************/
  20. typedef bool (*pb_decoder_t)(pb_istream_t *stream, const pb_field_t *field, void *dest) checkreturn;
  21. static bool checkreturn buf_read(pb_istream_t *stream, uint8_t *buf, size_t count);
  22. static bool checkreturn pb_decode_varint32(pb_istream_t *stream, uint32_t *dest);
  23. static bool checkreturn read_raw_value(pb_istream_t *stream, pb_wire_type_t wire_type, uint8_t *buf, size_t *size);
  24. static bool checkreturn decode_static_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter);
  25. static bool checkreturn decode_callback_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter);
  26. static bool checkreturn decode_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter);
  27. static bool checkreturn default_extension_decoder(pb_istream_t *stream, pb_extension_t *extension, uint32_t tag, pb_wire_type_t wire_type);
  28. static bool checkreturn decode_extension(pb_istream_t *stream, uint32_t tag, pb_wire_type_t wire_type, pb_field_iter_t *iter);
  29. static bool checkreturn find_extension_field(pb_field_iter_t *iter);
  30. static void pb_message_set_to_defaults(const pb_field_t fields[], void *dest_struct);
  31. static bool checkreturn pb_dec_varint(pb_istream_t *stream, const pb_field_t *field, void *dest);
  32. static bool checkreturn pb_dec_uvarint(pb_istream_t *stream, const pb_field_t *field, void *dest);
  33. static bool checkreturn pb_dec_svarint(pb_istream_t *stream, const pb_field_t *field, void *dest);
  34. static bool checkreturn pb_dec_fixed32(pb_istream_t *stream, const pb_field_t *field, void *dest);
  35. static bool checkreturn pb_dec_fixed64(pb_istream_t *stream, const pb_field_t *field, void *dest);
  36. static bool checkreturn pb_dec_bytes(pb_istream_t *stream, const pb_field_t *field, void *dest);
  37. static bool checkreturn pb_dec_string(pb_istream_t *stream, const pb_field_t *field, void *dest);
  38. static bool checkreturn pb_dec_submessage(pb_istream_t *stream, const pb_field_t *field, void *dest);
  39. static bool checkreturn pb_skip_varint(pb_istream_t *stream);
  40. static bool checkreturn pb_skip_string(pb_istream_t *stream);
  41. #ifdef PB_ENABLE_MALLOC
  42. static bool checkreturn allocate_field(pb_istream_t *stream, void *pData, size_t data_size, size_t array_size);
  43. static void pb_release_single_field(const pb_field_iter_t *iter);
  44. #endif
  45. /* --- Function pointers to field decoders ---
  46. * Order in the array must match pb_action_t LTYPE numbering.
  47. */
  48. static const pb_decoder_t PB_DECODERS[PB_LTYPES_COUNT] = {
  49. &pb_dec_varint,
  50. &pb_dec_uvarint,
  51. &pb_dec_svarint,
  52. &pb_dec_fixed32,
  53. &pb_dec_fixed64,
  54. &pb_dec_bytes,
  55. &pb_dec_string,
  56. &pb_dec_submessage,
  57. NULL /* extensions */
  58. };
  59. /*******************************
  60. * pb_istream_t implementation *
  61. *******************************/
  62. static bool checkreturn buf_read(pb_istream_t *stream, uint8_t *buf, size_t count)
  63. {
  64. uint8_t *source = (uint8_t*)stream->state;
  65. stream->state = source + count;
  66. if (buf != NULL)
  67. {
  68. while (count--)
  69. *buf++ = *source++;
  70. }
  71. return true;
  72. }
  73. bool checkreturn pb_read(pb_istream_t *stream, uint8_t *buf, size_t count)
  74. {
  75. #ifndef PB_BUFFER_ONLY
  76. if (buf == NULL && stream->callback != buf_read)
  77. {
  78. /* Skip input bytes */
  79. uint8_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, uint8_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 = *(uint8_t*)stream->state;
  112. stream->state = (uint8_t*)stream->state + 1;
  113. #endif
  114. stream->bytes_left--;
  115. return true;
  116. }
  117. pb_istream_t pb_istream_from_buffer(uint8_t *buf, size_t bufsize)
  118. {
  119. pb_istream_t stream;
  120. #ifdef PB_BUFFER_ONLY
  121. stream.callback = NULL;
  122. #else
  123. stream.callback = &buf_read;
  124. #endif
  125. stream.state = buf;
  126. stream.bytes_left = bufsize;
  127. #ifndef PB_NO_ERRMSG
  128. stream.errmsg = NULL;
  129. #endif
  130. return stream;
  131. }
  132. /********************
  133. * Helper functions *
  134. ********************/
  135. static bool checkreturn pb_decode_varint32(pb_istream_t *stream, uint32_t *dest)
  136. {
  137. uint8_t byte;
  138. uint32_t result;
  139. if (!pb_readbyte(stream, &byte))
  140. return false;
  141. if ((byte & 0x80) == 0)
  142. {
  143. /* Quick case, 1 byte value */
  144. result = byte;
  145. }
  146. else
  147. {
  148. /* Multibyte case */
  149. uint8_t bitpos = 7;
  150. result = byte & 0x7F;
  151. do
  152. {
  153. if (bitpos >= 32)
  154. PB_RETURN_ERROR(stream, "varint overflow");
  155. if (!pb_readbyte(stream, &byte))
  156. return false;
  157. result |= (uint32_t)(byte & 0x7F) << bitpos;
  158. bitpos = (uint8_t)(bitpos + 7);
  159. } while (byte & 0x80);
  160. }
  161. *dest = result;
  162. return true;
  163. }
  164. bool checkreturn pb_decode_varint(pb_istream_t *stream, uint64_t *dest)
  165. {
  166. uint8_t byte;
  167. uint8_t bitpos = 0;
  168. uint64_t result = 0;
  169. do
  170. {
  171. if (bitpos >= 64)
  172. PB_RETURN_ERROR(stream, "varint overflow");
  173. if (!pb_readbyte(stream, &byte))
  174. return false;
  175. result |= (uint64_t)(byte & 0x7F) << bitpos;
  176. bitpos = (uint8_t)(bitpos + 7);
  177. } while (byte & 0x80);
  178. *dest = result;
  179. return true;
  180. }
  181. bool checkreturn pb_skip_varint(pb_istream_t *stream)
  182. {
  183. uint8_t byte;
  184. do
  185. {
  186. if (!pb_read(stream, &byte, 1))
  187. return false;
  188. } while (byte & 0x80);
  189. return true;
  190. }
  191. bool checkreturn pb_skip_string(pb_istream_t *stream)
  192. {
  193. uint32_t length;
  194. if (!pb_decode_varint32(stream, &length))
  195. return false;
  196. return pb_read(stream, NULL, length);
  197. }
  198. bool checkreturn pb_decode_tag(pb_istream_t *stream, pb_wire_type_t *wire_type, uint32_t *tag, bool *eof)
  199. {
  200. uint32_t temp;
  201. *eof = false;
  202. *wire_type = (pb_wire_type_t) 0;
  203. *tag = 0;
  204. if (!pb_decode_varint32(stream, &temp))
  205. {
  206. if (stream->bytes_left == 0)
  207. *eof = true;
  208. return false;
  209. }
  210. if (temp == 0)
  211. {
  212. *eof = true; /* Special feature: allow 0-terminated messages. */
  213. return false;
  214. }
  215. *tag = temp >> 3;
  216. *wire_type = (pb_wire_type_t)(temp & 7);
  217. return true;
  218. }
  219. bool checkreturn pb_skip_field(pb_istream_t *stream, pb_wire_type_t wire_type)
  220. {
  221. switch (wire_type)
  222. {
  223. case PB_WT_VARINT: return pb_skip_varint(stream);
  224. case PB_WT_64BIT: return pb_read(stream, NULL, 8);
  225. case PB_WT_STRING: return pb_skip_string(stream);
  226. case PB_WT_32BIT: return pb_read(stream, NULL, 4);
  227. default: PB_RETURN_ERROR(stream, "invalid wire_type");
  228. }
  229. }
  230. /* Read a raw value to buffer, for the purpose of passing it to callback as
  231. * a substream. Size is maximum size on call, and actual size on return.
  232. */
  233. static bool checkreturn read_raw_value(pb_istream_t *stream, pb_wire_type_t wire_type, uint8_t *buf, size_t *size)
  234. {
  235. size_t max_size = *size;
  236. switch (wire_type)
  237. {
  238. case PB_WT_VARINT:
  239. *size = 0;
  240. do
  241. {
  242. (*size)++;
  243. if (*size > max_size) return false;
  244. if (!pb_read(stream, buf, 1)) return false;
  245. } while (*buf++ & 0x80);
  246. return true;
  247. case PB_WT_64BIT:
  248. *size = 8;
  249. return pb_read(stream, buf, 8);
  250. case PB_WT_32BIT:
  251. *size = 4;
  252. return pb_read(stream, buf, 4);
  253. default: PB_RETURN_ERROR(stream, "invalid wire_type");
  254. }
  255. }
  256. /* Decode string length from stream and return a substream with limited length.
  257. * Remember to close the substream using pb_close_string_substream().
  258. */
  259. bool checkreturn pb_make_string_substream(pb_istream_t *stream, pb_istream_t *substream)
  260. {
  261. uint32_t size;
  262. if (!pb_decode_varint32(stream, &size))
  263. return false;
  264. *substream = *stream;
  265. if (substream->bytes_left < size)
  266. PB_RETURN_ERROR(stream, "parent stream too short");
  267. substream->bytes_left = size;
  268. stream->bytes_left -= size;
  269. return true;
  270. }
  271. void pb_close_string_substream(pb_istream_t *stream, pb_istream_t *substream)
  272. {
  273. stream->state = substream->state;
  274. #ifndef PB_NO_ERRMSG
  275. stream->errmsg = substream->errmsg;
  276. #endif
  277. }
  278. /*************************
  279. * Decode a single field *
  280. *************************/
  281. static bool checkreturn decode_static_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
  282. {
  283. pb_type_t type;
  284. pb_decoder_t func;
  285. type = iter->pos->type;
  286. func = PB_DECODERS[PB_LTYPE(type)];
  287. switch (PB_HTYPE(type))
  288. {
  289. case PB_HTYPE_REQUIRED:
  290. return func(stream, iter->pos, iter->pData);
  291. case PB_HTYPE_OPTIONAL:
  292. *(bool*)iter->pSize = true;
  293. return func(stream, iter->pos, iter->pData);
  294. case PB_HTYPE_REPEATED:
  295. if (wire_type == PB_WT_STRING
  296. && PB_LTYPE(type) <= PB_LTYPE_LAST_PACKABLE)
  297. {
  298. /* Packed array */
  299. bool status = true;
  300. pb_size_t *size = (pb_size_t*)iter->pSize;
  301. pb_istream_t substream;
  302. if (!pb_make_string_substream(stream, &substream))
  303. return false;
  304. while (substream.bytes_left > 0 && *size < iter->pos->array_size)
  305. {
  306. void *pItem = (uint8_t*)iter->pData + iter->pos->data_size * (*size);
  307. if (!func(&substream, iter->pos, pItem))
  308. {
  309. status = false;
  310. break;
  311. }
  312. (*size)++;
  313. }
  314. pb_close_string_substream(stream, &substream);
  315. if (substream.bytes_left != 0)
  316. PB_RETURN_ERROR(stream, "array overflow");
  317. return status;
  318. }
  319. else
  320. {
  321. /* Repeated field */
  322. pb_size_t *size = (pb_size_t*)iter->pSize;
  323. void *pItem = (uint8_t*)iter->pData + iter->pos->data_size * (*size);
  324. if (*size >= iter->pos->array_size)
  325. PB_RETURN_ERROR(stream, "array overflow");
  326. (*size)++;
  327. return func(stream, iter->pos, pItem);
  328. }
  329. default:
  330. PB_RETURN_ERROR(stream, "invalid field type");
  331. }
  332. }
  333. #ifdef PB_ENABLE_MALLOC
  334. /* Allocate storage for the field and store the pointer at iter->pData.
  335. * array_size is the number of entries to reserve in an array.
  336. * Zero size is not allowed, use pb_free() for releasing.
  337. */
  338. static bool checkreturn allocate_field(pb_istream_t *stream, void *pData, size_t data_size, size_t array_size)
  339. {
  340. void *ptr = *(void**)pData;
  341. if (data_size == 0 || array_size == 0)
  342. PB_RETURN_ERROR(stream, "invalid size");
  343. /* Check for multiplication overflows.
  344. * This code avoids the costly division if the sizes are small enough.
  345. * Multiplication is safe as long as only half of bits are set
  346. * in either multiplicand.
  347. */
  348. {
  349. const size_t check_limit = (size_t)1 << (sizeof(size_t) * 4);
  350. if (data_size >= check_limit || array_size >= check_limit)
  351. {
  352. const size_t size_max = (size_t)-1;
  353. if (size_max / array_size < data_size)
  354. {
  355. PB_RETURN_ERROR(stream, "size too large");
  356. }
  357. }
  358. }
  359. /* Allocate new or expand previous allocation */
  360. /* Note: on failure the old pointer will remain in the structure,
  361. * the message must be freed by caller also on error return. */
  362. ptr = pb_realloc(ptr, array_size * data_size);
  363. if (ptr == NULL)
  364. PB_RETURN_ERROR(stream, "realloc failed");
  365. *(void**)pData = ptr;
  366. return true;
  367. }
  368. /* Clear a newly allocated item in case it contains a pointer, or is a submessage. */
  369. static void initialize_pointer_field(void *pItem, pb_field_iter_t *iter)
  370. {
  371. if (PB_LTYPE(iter->pos->type) == PB_LTYPE_STRING ||
  372. PB_LTYPE(iter->pos->type) == PB_LTYPE_BYTES)
  373. {
  374. *(void**)pItem = NULL;
  375. }
  376. else if (PB_LTYPE(iter->pos->type) == PB_LTYPE_SUBMESSAGE)
  377. {
  378. pb_message_set_to_defaults((const pb_field_t *) iter->pos->ptr, pItem);
  379. }
  380. }
  381. #endif
  382. static bool checkreturn decode_pointer_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
  383. {
  384. #ifndef PB_ENABLE_MALLOC
  385. PB_UNUSED(wire_type);
  386. PB_UNUSED(iter);
  387. PB_RETURN_ERROR(stream, "no malloc support");
  388. #else
  389. pb_type_t type;
  390. pb_decoder_t func;
  391. type = iter->pos->type;
  392. func = PB_DECODERS[PB_LTYPE(type)];
  393. switch (PB_HTYPE(type))
  394. {
  395. case PB_HTYPE_REQUIRED:
  396. case PB_HTYPE_OPTIONAL:
  397. if (PB_LTYPE(type) == PB_LTYPE_SUBMESSAGE &&
  398. *(void**)iter->pData != NULL)
  399. {
  400. /* Duplicate field, have to release the old allocation first. */
  401. pb_release_single_field(iter);
  402. }
  403. if (PB_LTYPE(type) == PB_LTYPE_STRING ||
  404. PB_LTYPE(type) == PB_LTYPE_BYTES)
  405. {
  406. return func(stream, iter->pos, iter->pData);
  407. }
  408. else
  409. {
  410. if (!allocate_field(stream, iter->pData, iter->pos->data_size, 1))
  411. return false;
  412. initialize_pointer_field(*(void**)iter->pData, iter);
  413. return func(stream, iter->pos, *(void**)iter->pData);
  414. }
  415. case PB_HTYPE_REPEATED:
  416. if (wire_type == PB_WT_STRING
  417. && PB_LTYPE(type) <= PB_LTYPE_LAST_PACKABLE)
  418. {
  419. /* Packed array, multiple items come in at once. */
  420. bool status = true;
  421. pb_size_t *size = (pb_size_t*)iter->pSize;
  422. size_t allocated_size = *size;
  423. void *pItem;
  424. pb_istream_t substream;
  425. if (!pb_make_string_substream(stream, &substream))
  426. return false;
  427. while (substream.bytes_left)
  428. {
  429. if ((size_t)*size + 1 > allocated_size)
  430. {
  431. /* Allocate more storage. This tries to guess the
  432. * number of remaining entries. Round the division
  433. * upwards. */
  434. allocated_size += (substream.bytes_left - 1) / iter->pos->data_size + 1;
  435. if (!allocate_field(&substream, iter->pData, iter->pos->data_size, allocated_size))
  436. {
  437. status = false;
  438. break;
  439. }
  440. }
  441. /* Decode the array entry */
  442. pItem = *(uint8_t**)iter->pData + iter->pos->data_size * (*size);
  443. initialize_pointer_field(pItem, iter);
  444. if (!func(&substream, iter->pos, pItem))
  445. {
  446. status = false;
  447. break;
  448. }
  449. if (*size == PB_SIZE_MAX)
  450. {
  451. #ifndef PB_NO_ERRMSG
  452. stream->errmsg = "too many array entries";
  453. #endif
  454. status = false;
  455. break;
  456. }
  457. (*size)++;
  458. }
  459. pb_close_string_substream(stream, &substream);
  460. return status;
  461. }
  462. else
  463. {
  464. /* Normal repeated field, i.e. only one item at a time. */
  465. pb_size_t *size = (pb_size_t*)iter->pSize;
  466. void *pItem;
  467. if (*size == PB_SIZE_MAX)
  468. PB_RETURN_ERROR(stream, "too many array entries");
  469. (*size)++;
  470. if (!allocate_field(stream, iter->pData, iter->pos->data_size, *size))
  471. return false;
  472. pItem = *(uint8_t**)iter->pData + iter->pos->data_size * (*size - 1);
  473. initialize_pointer_field(pItem, iter);
  474. return func(stream, iter->pos, pItem);
  475. }
  476. default:
  477. PB_RETURN_ERROR(stream, "invalid field type");
  478. }
  479. #endif
  480. }
  481. static bool checkreturn decode_callback_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
  482. {
  483. pb_callback_t *pCallback = (pb_callback_t*)iter->pData;
  484. #ifdef PB_OLD_CALLBACK_STYLE
  485. void *arg = pCallback->arg;
  486. #else
  487. void **arg = &(pCallback->arg);
  488. #endif
  489. if (pCallback->funcs.decode == NULL)
  490. return pb_skip_field(stream, wire_type);
  491. if (wire_type == PB_WT_STRING)
  492. {
  493. pb_istream_t substream;
  494. if (!pb_make_string_substream(stream, &substream))
  495. return false;
  496. do
  497. {
  498. if (!pCallback->funcs.decode(&substream, iter->pos, arg))
  499. PB_RETURN_ERROR(stream, "callback failed");
  500. } while (substream.bytes_left);
  501. pb_close_string_substream(stream, &substream);
  502. return true;
  503. }
  504. else
  505. {
  506. /* Copy the single scalar value to stack.
  507. * This is required so that we can limit the stream length,
  508. * which in turn allows to use same callback for packed and
  509. * not-packed fields. */
  510. pb_istream_t substream;
  511. uint8_t buffer[10];
  512. size_t size = sizeof(buffer);
  513. if (!read_raw_value(stream, wire_type, buffer, &size))
  514. return false;
  515. substream = pb_istream_from_buffer(buffer, size);
  516. return pCallback->funcs.decode(&substream, iter->pos, arg);
  517. }
  518. }
  519. static bool checkreturn decode_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
  520. {
  521. switch (PB_ATYPE(iter->pos->type))
  522. {
  523. case PB_ATYPE_STATIC:
  524. return decode_static_field(stream, wire_type, iter);
  525. case PB_ATYPE_POINTER:
  526. return decode_pointer_field(stream, wire_type, iter);
  527. case PB_ATYPE_CALLBACK:
  528. return decode_callback_field(stream, wire_type, iter);
  529. default:
  530. PB_RETURN_ERROR(stream, "invalid field type");
  531. }
  532. }
  533. /* Default handler for extension fields. Expects a pb_field_t structure
  534. * in extension->type->arg. */
  535. static bool checkreturn default_extension_decoder(pb_istream_t *stream,
  536. pb_extension_t *extension, uint32_t tag, pb_wire_type_t wire_type)
  537. {
  538. const pb_field_t *field = (const pb_field_t*)extension->type->arg;
  539. pb_field_iter_t iter;
  540. if (field->tag != tag)
  541. return true;
  542. /* Fake a field iterator for the extension field.
  543. * It is not actually safe to advance this iterator, but decode_field
  544. * will not even try to. */
  545. (void)pb_field_iter_begin(&iter, field, extension->dest);
  546. iter.pData = extension->dest;
  547. iter.pSize = &extension->found;
  548. return decode_field(stream, wire_type, &iter);
  549. }
  550. /* Try to decode an unknown field as an extension field. Tries each extension
  551. * decoder in turn, until one of them handles the field or loop ends. */
  552. static bool checkreturn decode_extension(pb_istream_t *stream,
  553. uint32_t tag, pb_wire_type_t wire_type, pb_field_iter_t *iter)
  554. {
  555. pb_extension_t *extension = *(pb_extension_t* const *)iter->pData;
  556. size_t pos = stream->bytes_left;
  557. while (extension != NULL && pos == stream->bytes_left)
  558. {
  559. bool status;
  560. if (extension->type->decode)
  561. status = extension->type->decode(stream, extension, tag, wire_type);
  562. else
  563. status = default_extension_decoder(stream, extension, tag, wire_type);
  564. if (!status)
  565. return false;
  566. extension = extension->next;
  567. }
  568. return true;
  569. }
  570. /* Step through the iterator until an extension field is found or until all
  571. * entries have been checked. There can be only one extension field per
  572. * message. Returns false if no extension field is found. */
  573. static bool checkreturn find_extension_field(pb_field_iter_t *iter)
  574. {
  575. const pb_field_t *start = iter->pos;
  576. do {
  577. if (PB_LTYPE(iter->pos->type) == PB_LTYPE_EXTENSION)
  578. return true;
  579. (void)pb_field_iter_next(iter);
  580. } while (iter->pos != start);
  581. return false;
  582. }
  583. /* Initialize message fields to default values, recursively */
  584. static void pb_message_set_to_defaults(const pb_field_t fields[], void *dest_struct)
  585. {
  586. pb_field_iter_t iter;
  587. if (!pb_field_iter_begin(&iter, fields, dest_struct))
  588. return; /* Empty message type */
  589. do
  590. {
  591. pb_type_t type;
  592. type = iter.pos->type;
  593. if (PB_ATYPE(type) == PB_ATYPE_STATIC)
  594. {
  595. if (PB_HTYPE(type) == PB_HTYPE_OPTIONAL)
  596. {
  597. /* Set has_field to false. Still initialize the optional field
  598. * itself also. */
  599. *(bool*)iter.pSize = false;
  600. }
  601. else if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
  602. {
  603. /* Set array count to 0, no need to initialize contents. */
  604. *(pb_size_t*)iter.pSize = 0;
  605. continue;
  606. }
  607. if (PB_LTYPE(iter.pos->type) == PB_LTYPE_SUBMESSAGE)
  608. {
  609. /* Initialize submessage to defaults */
  610. pb_message_set_to_defaults((const pb_field_t *) iter.pos->ptr, iter.pData);
  611. }
  612. else if (iter.pos->ptr != NULL)
  613. {
  614. /* Initialize to default value */
  615. memcpy(iter.pData, iter.pos->ptr, iter.pos->data_size);
  616. }
  617. else
  618. {
  619. /* Initialize to zeros */
  620. memset(iter.pData, 0, iter.pos->data_size);
  621. }
  622. }
  623. else if (PB_ATYPE(type) == PB_ATYPE_POINTER)
  624. {
  625. /* Initialize the pointer to NULL. */
  626. *(void**)iter.pData = NULL;
  627. /* Initialize array count to 0. */
  628. if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
  629. {
  630. *(pb_size_t*)iter.pSize = 0;
  631. }
  632. }
  633. else if (PB_ATYPE(type) == PB_ATYPE_CALLBACK)
  634. {
  635. /* Don't overwrite callback */
  636. }
  637. } while (pb_field_iter_next(&iter));
  638. }
  639. /*********************
  640. * Decode all fields *
  641. *********************/
  642. bool checkreturn pb_decode_noinit(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
  643. {
  644. uint8_t fields_seen[(PB_MAX_REQUIRED_FIELDS + 7) / 8] = {0, 0, 0, 0, 0, 0, 0, 0};
  645. uint32_t extension_range_start = 0;
  646. pb_field_iter_t iter;
  647. /* Return value ignored, as empty message types will be correctly handled by
  648. * pb_field_iter_find() anyway. */
  649. (void)pb_field_iter_begin(&iter, fields, dest_struct);
  650. while (stream->bytes_left)
  651. {
  652. uint32_t tag;
  653. pb_wire_type_t wire_type;
  654. bool eof;
  655. if (!pb_decode_tag(stream, &wire_type, &tag, &eof))
  656. {
  657. if (eof)
  658. break;
  659. else
  660. return false;
  661. }
  662. if (!pb_field_iter_find(&iter, tag))
  663. {
  664. /* No match found, check if it matches an extension. */
  665. if (tag >= extension_range_start)
  666. {
  667. if (!find_extension_field(&iter))
  668. extension_range_start = (uint32_t)-1;
  669. else
  670. extension_range_start = iter.pos->tag;
  671. if (tag >= extension_range_start)
  672. {
  673. size_t pos = stream->bytes_left;
  674. if (!decode_extension(stream, tag, wire_type, &iter))
  675. return false;
  676. if (pos != stream->bytes_left)
  677. {
  678. /* The field was handled */
  679. continue;
  680. }
  681. }
  682. }
  683. /* No match found, skip data */
  684. if (!pb_skip_field(stream, wire_type))
  685. return false;
  686. continue;
  687. }
  688. if (PB_HTYPE(iter.pos->type) == PB_HTYPE_REQUIRED
  689. && iter.required_field_index < PB_MAX_REQUIRED_FIELDS)
  690. {
  691. fields_seen[iter.required_field_index >> 3] |= (uint8_t)(1 << (iter.required_field_index & 7));
  692. }
  693. if (!decode_field(stream, wire_type, &iter))
  694. return false;
  695. }
  696. /* Check that all required fields were present. */
  697. {
  698. /* First figure out the number of required fields by
  699. * seeking to the end of the field array. Usually we
  700. * are already close to end after decoding.
  701. */
  702. unsigned req_field_count;
  703. pb_type_t last_type;
  704. unsigned i;
  705. do {
  706. req_field_count = iter.required_field_index;
  707. last_type = iter.pos->type;
  708. } while (pb_field_iter_next(&iter));
  709. /* Fixup if last field was also required. */
  710. if (PB_HTYPE(last_type) == PB_HTYPE_REQUIRED && iter.pos->tag != 0)
  711. req_field_count++;
  712. /* Check the whole bytes */
  713. for (i = 0; i < (req_field_count >> 3); i++)
  714. {
  715. if (fields_seen[i] != 0xFF)
  716. PB_RETURN_ERROR(stream, "missing required field");
  717. }
  718. /* Check the remaining bits */
  719. if (fields_seen[req_field_count >> 3] != (0xFF >> (8 - (req_field_count & 7))))
  720. PB_RETURN_ERROR(stream, "missing required field");
  721. }
  722. return true;
  723. }
  724. bool checkreturn pb_decode(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
  725. {
  726. bool status;
  727. pb_message_set_to_defaults(fields, dest_struct);
  728. status = pb_decode_noinit(stream, fields, dest_struct);
  729. #ifdef PB_ENABLE_MALLOC
  730. if (!status)
  731. pb_release(fields, dest_struct);
  732. #endif
  733. return status;
  734. }
  735. bool pb_decode_delimited(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
  736. {
  737. pb_istream_t substream;
  738. bool status;
  739. if (!pb_make_string_substream(stream, &substream))
  740. return false;
  741. status = pb_decode(&substream, fields, dest_struct);
  742. pb_close_string_substream(stream, &substream);
  743. return status;
  744. }
  745. #ifdef PB_ENABLE_MALLOC
  746. static void pb_release_single_field(const pb_field_iter_t *iter)
  747. {
  748. pb_type_t type;
  749. type = iter->pos->type;
  750. if (PB_ATYPE(type) == PB_ATYPE_POINTER)
  751. {
  752. if (PB_HTYPE(type) == PB_HTYPE_REPEATED &&
  753. (PB_LTYPE(type) == PB_LTYPE_STRING ||
  754. PB_LTYPE(type) == PB_LTYPE_BYTES))
  755. {
  756. /* Release entries in repeated string or bytes array */
  757. void **pItem = *(void***)iter->pData;
  758. pb_size_t count = *(pb_size_t*)iter->pSize;
  759. while (count--)
  760. {
  761. pb_free(*pItem);
  762. *pItem++ = NULL;
  763. }
  764. *(pb_size_t*)iter->pSize = 0;
  765. }
  766. else if (PB_LTYPE(type) == PB_LTYPE_SUBMESSAGE)
  767. {
  768. /* Release fields in submessages */
  769. void *pItem = *(void**)iter->pData;
  770. if (pItem)
  771. {
  772. pb_size_t count = 1;
  773. if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
  774. {
  775. count = *(pb_size_t*)iter->pSize;
  776. *(pb_size_t*)iter->pSize = 0;
  777. }
  778. while (count--)
  779. {
  780. pb_release((const pb_field_t*)iter->pos->ptr, pItem);
  781. pItem = (uint8_t*)pItem + iter->pos->data_size;
  782. }
  783. }
  784. }
  785. /* Release main item */
  786. pb_free(*(void**)iter->pData);
  787. *(void**)iter->pData = NULL;
  788. }
  789. }
  790. void pb_release(const pb_field_t fields[], void *dest_struct)
  791. {
  792. pb_field_iter_t iter;
  793. if (!pb_field_iter_begin(&iter, fields, dest_struct))
  794. return; /* Empty message type */
  795. do
  796. {
  797. pb_release_single_field(&iter);
  798. } while (pb_field_iter_next(&iter));
  799. }
  800. #endif
  801. /* Field decoders */
  802. bool pb_decode_svarint(pb_istream_t *stream, int64_t *dest)
  803. {
  804. uint64_t value;
  805. if (!pb_decode_varint(stream, &value))
  806. return false;
  807. if (value & 1)
  808. *dest = (int64_t)(~(value >> 1));
  809. else
  810. *dest = (int64_t)(value >> 1);
  811. return true;
  812. }
  813. bool pb_decode_fixed32(pb_istream_t *stream, void *dest)
  814. {
  815. #ifdef __BIG_ENDIAN__
  816. uint8_t *bytes = (uint8_t*)dest;
  817. uint8_t lebytes[4];
  818. if (!pb_read(stream, lebytes, 4))
  819. return false;
  820. bytes[0] = lebytes[3];
  821. bytes[1] = lebytes[2];
  822. bytes[2] = lebytes[1];
  823. bytes[3] = lebytes[0];
  824. return true;
  825. #else
  826. return pb_read(stream, (uint8_t*)dest, 4);
  827. #endif
  828. }
  829. bool pb_decode_fixed64(pb_istream_t *stream, void *dest)
  830. {
  831. #ifdef __BIG_ENDIAN__
  832. uint8_t *bytes = (uint8_t*)dest;
  833. uint8_t lebytes[8];
  834. if (!pb_read(stream, lebytes, 8))
  835. return false;
  836. bytes[0] = lebytes[7];
  837. bytes[1] = lebytes[6];
  838. bytes[2] = lebytes[5];
  839. bytes[3] = lebytes[4];
  840. bytes[4] = lebytes[3];
  841. bytes[5] = lebytes[2];
  842. bytes[6] = lebytes[1];
  843. bytes[7] = lebytes[0];
  844. return true;
  845. #else
  846. return pb_read(stream, (uint8_t*)dest, 8);
  847. #endif
  848. }
  849. static bool checkreturn pb_dec_varint(pb_istream_t *stream, const pb_field_t *field, void *dest)
  850. {
  851. uint64_t value;
  852. if (!pb_decode_varint(stream, &value))
  853. return false;
  854. switch (field->data_size)
  855. {
  856. case 1: *(int8_t*)dest = (int8_t)value; break;
  857. case 2: *(int16_t*)dest = (int16_t)value; break;
  858. case 4: *(int32_t*)dest = (int32_t)value; break;
  859. case 8: *(int64_t*)dest = (int64_t)value; break;
  860. default: PB_RETURN_ERROR(stream, "invalid data_size");
  861. }
  862. return true;
  863. }
  864. static bool checkreturn pb_dec_uvarint(pb_istream_t *stream, const pb_field_t *field, void *dest)
  865. {
  866. uint64_t value;
  867. if (!pb_decode_varint(stream, &value))
  868. return false;
  869. switch (field->data_size)
  870. {
  871. case 4: *(uint32_t*)dest = (uint32_t)value; break;
  872. case 8: *(uint64_t*)dest = value; break;
  873. default: PB_RETURN_ERROR(stream, "invalid data_size");
  874. }
  875. return true;
  876. }
  877. static bool checkreturn pb_dec_svarint(pb_istream_t *stream, const pb_field_t *field, void *dest)
  878. {
  879. int64_t value;
  880. if (!pb_decode_svarint(stream, &value))
  881. return false;
  882. switch (field->data_size)
  883. {
  884. case 4: *(int32_t*)dest = (int32_t)value; break;
  885. case 8: *(int64_t*)dest = value; break;
  886. default: PB_RETURN_ERROR(stream, "invalid data_size");
  887. }
  888. return true;
  889. }
  890. static bool checkreturn pb_dec_fixed32(pb_istream_t *stream, const pb_field_t *field, void *dest)
  891. {
  892. PB_UNUSED(field);
  893. return pb_decode_fixed32(stream, dest);
  894. }
  895. static bool checkreturn pb_dec_fixed64(pb_istream_t *stream, const pb_field_t *field, void *dest)
  896. {
  897. PB_UNUSED(field);
  898. return pb_decode_fixed64(stream, dest);
  899. }
  900. static bool checkreturn pb_dec_bytes(pb_istream_t *stream, const pb_field_t *field, void *dest)
  901. {
  902. uint32_t size;
  903. size_t alloc_size;
  904. pb_bytes_array_t *bdest;
  905. if (!pb_decode_varint32(stream, &size))
  906. return false;
  907. if (size > PB_SIZE_MAX)
  908. PB_RETURN_ERROR(stream, "bytes overflow");
  909. alloc_size = PB_BYTES_ARRAY_T_ALLOCSIZE(size);
  910. if (size > alloc_size)
  911. PB_RETURN_ERROR(stream, "size too large");
  912. if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
  913. {
  914. #ifndef PB_ENABLE_MALLOC
  915. PB_RETURN_ERROR(stream, "no malloc support");
  916. #else
  917. if (!allocate_field(stream, dest, alloc_size, 1))
  918. return false;
  919. bdest = *(pb_bytes_array_t**)dest;
  920. #endif
  921. }
  922. else
  923. {
  924. if (alloc_size > field->data_size)
  925. PB_RETURN_ERROR(stream, "bytes overflow");
  926. bdest = (pb_bytes_array_t*)dest;
  927. }
  928. bdest->size = (pb_size_t)size;
  929. return pb_read(stream, bdest->bytes, size);
  930. }
  931. static bool checkreturn pb_dec_string(pb_istream_t *stream, const pb_field_t *field, void *dest)
  932. {
  933. uint32_t size;
  934. size_t alloc_size;
  935. bool status;
  936. if (!pb_decode_varint32(stream, &size))
  937. return false;
  938. /* Space for null terminator */
  939. alloc_size = size + 1;
  940. if (alloc_size < size)
  941. PB_RETURN_ERROR(stream, "size too large");
  942. if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
  943. {
  944. #ifndef PB_ENABLE_MALLOC
  945. PB_RETURN_ERROR(stream, "no malloc support");
  946. #else
  947. if (!allocate_field(stream, dest, alloc_size, 1))
  948. return false;
  949. dest = *(void**)dest;
  950. #endif
  951. }
  952. else
  953. {
  954. if (alloc_size > field->data_size)
  955. PB_RETURN_ERROR(stream, "string overflow");
  956. }
  957. status = pb_read(stream, (uint8_t*)dest, size);
  958. *((uint8_t*)dest + size) = 0;
  959. return status;
  960. }
  961. static bool checkreturn pb_dec_submessage(pb_istream_t *stream, const pb_field_t *field, void *dest)
  962. {
  963. bool status;
  964. pb_istream_t substream;
  965. const pb_field_t* submsg_fields = (const pb_field_t*)field->ptr;
  966. if (!pb_make_string_substream(stream, &substream))
  967. return false;
  968. if (field->ptr == NULL)
  969. PB_RETURN_ERROR(stream, "invalid field descriptor");
  970. /* New array entries need to be initialized, while required and optional
  971. * submessages have already been initialized in the top-level pb_decode. */
  972. if (PB_HTYPE(field->type) == PB_HTYPE_REPEATED)
  973. status = pb_decode(&substream, submsg_fields, dest);
  974. else
  975. status = pb_decode_noinit(&substream, submsg_fields, dest);
  976. pb_close_string_substream(stream, &substream);
  977. return status;
  978. }