pb_decode.c 59 KB

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