pb_decode.c 59 KB

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