pb_decode.c 46 KB

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