2
0

pb_encode.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  1. /* pb_encode.c -- encode a protobuf using minimal resources
  2. *
  3. * 2011 Petteri Aimonen <jpa@kapsi.fi>
  4. */
  5. #include "pb.h"
  6. #include "pb_encode.h"
  7. #include "pb_common.h"
  8. /* Use the GCC warn_unused_result attribute to check that all return values
  9. * are propagated correctly. On other compilers and gcc before 3.4.0 just
  10. * ignore the annotation.
  11. */
  12. #if !defined(__GNUC__) || ( __GNUC__ < 3) || (__GNUC__ == 3 && __GNUC_MINOR__ < 4)
  13. #define checkreturn
  14. #else
  15. #define checkreturn __attribute__((warn_unused_result))
  16. #endif
  17. /**************************************
  18. * Declarations internal to this file *
  19. **************************************/
  20. static bool checkreturn buf_write(pb_ostream_t *stream, const pb_byte_t *buf, size_t count);
  21. static bool checkreturn encode_array(pb_ostream_t *stream, pb_field_iter_t *field);
  22. static bool checkreturn pb_check_proto3_default_value(const pb_field_iter_t *field);
  23. static bool checkreturn encode_basic_field(pb_ostream_t *stream, const pb_field_iter_t *field);
  24. static bool checkreturn encode_callback_field(pb_ostream_t *stream, const pb_field_iter_t *field);
  25. static bool checkreturn encode_field(pb_ostream_t *stream, pb_field_iter_t *field);
  26. static bool checkreturn encode_extension_field(pb_ostream_t *stream, const pb_field_iter_t *field);
  27. static bool checkreturn default_extension_encoder(pb_ostream_t *stream, const pb_extension_t *extension);
  28. static bool checkreturn pb_encode_varint_32(pb_ostream_t *stream, uint32_t low, uint32_t high);
  29. static bool checkreturn pb_enc_bool(pb_ostream_t *stream, const pb_field_iter_t *field);
  30. static bool checkreturn pb_enc_varint(pb_ostream_t *stream, const pb_field_iter_t *field);
  31. static bool checkreturn pb_enc_fixed(pb_ostream_t *stream, const pb_field_iter_t *field);
  32. static bool checkreturn pb_enc_bytes(pb_ostream_t *stream, const pb_field_iter_t *field);
  33. static bool checkreturn pb_enc_string(pb_ostream_t *stream, const pb_field_iter_t *field);
  34. static bool checkreturn pb_enc_submessage(pb_ostream_t *stream, const pb_field_iter_t *field);
  35. static bool checkreturn pb_enc_fixed_length_bytes(pb_ostream_t *stream, const pb_field_iter_t *field);
  36. #ifdef PB_WITHOUT_64BIT
  37. #define pb_int64_t int32_t
  38. #define pb_uint64_t uint32_t
  39. #else
  40. #define pb_int64_t int64_t
  41. #define pb_uint64_t uint64_t
  42. #endif
  43. /*******************************
  44. * pb_ostream_t implementation *
  45. *******************************/
  46. static bool checkreturn buf_write(pb_ostream_t *stream, const pb_byte_t *buf, size_t count)
  47. {
  48. pb_byte_t *dest = (pb_byte_t*)stream->state;
  49. stream->state = dest + count;
  50. memcpy(dest, buf, count * sizeof(pb_byte_t));
  51. return true;
  52. }
  53. pb_ostream_t pb_ostream_from_buffer(pb_byte_t *buf, size_t bufsize)
  54. {
  55. pb_ostream_t stream;
  56. #ifdef PB_BUFFER_ONLY
  57. /* In PB_BUFFER_ONLY configuration the callback pointer is just int*.
  58. * NULL pointer marks a sizing field, so put a non-NULL value to mark a buffer stream.
  59. */
  60. static const int marker = 0;
  61. stream.callback = &marker;
  62. #else
  63. stream.callback = &buf_write;
  64. #endif
  65. stream.state = buf;
  66. stream.max_size = bufsize;
  67. stream.bytes_written = 0;
  68. #ifndef PB_NO_ERRMSG
  69. stream.errmsg = NULL;
  70. #endif
  71. return stream;
  72. }
  73. bool checkreturn pb_write(pb_ostream_t *stream, const pb_byte_t *buf, size_t count)
  74. {
  75. if (count > 0 && stream->callback != NULL)
  76. {
  77. if (stream->bytes_written + count < stream->bytes_written ||
  78. stream->bytes_written + count > stream->max_size)
  79. {
  80. PB_RETURN_ERROR(stream, "stream full");
  81. }
  82. #ifdef PB_BUFFER_ONLY
  83. if (!buf_write(stream, buf, count))
  84. PB_RETURN_ERROR(stream, "io error");
  85. #else
  86. if (!stream->callback(stream, buf, count))
  87. PB_RETURN_ERROR(stream, "io error");
  88. #endif
  89. }
  90. stream->bytes_written += count;
  91. return true;
  92. }
  93. /*************************
  94. * Encode a single field *
  95. *************************/
  96. /* Read a bool value without causing undefined behavior even if the value
  97. * is invalid. See issue #434 and
  98. * https://stackoverflow.com/questions/27661768/weird-results-for-conditional
  99. */
  100. static bool safe_read_bool(const void *pSize)
  101. {
  102. const char *p = (const char *)pSize;
  103. size_t i;
  104. for (i = 0; i < sizeof(bool); i++)
  105. {
  106. if (p[i] != 0)
  107. return true;
  108. }
  109. return false;
  110. }
  111. /* Encode a static array. Handles the size calculations and possible packing. */
  112. static bool checkreturn encode_array(pb_ostream_t *stream, pb_field_iter_t *field)
  113. {
  114. pb_size_t i;
  115. pb_size_t count;
  116. #ifndef PB_ENCODE_ARRAYS_UNPACKED
  117. size_t size;
  118. #endif
  119. count = *(pb_size_t*)field->pSize;
  120. if (count == 0)
  121. return true;
  122. if (PB_ATYPE(field->type) != PB_ATYPE_POINTER && count > field->array_size)
  123. PB_RETURN_ERROR(stream, "array max size exceeded");
  124. #ifndef PB_ENCODE_ARRAYS_UNPACKED
  125. /* We always pack arrays if the datatype allows it. */
  126. if (PB_LTYPE(field->type) <= PB_LTYPE_LAST_PACKABLE)
  127. {
  128. if (!pb_encode_tag(stream, PB_WT_STRING, field->tag))
  129. return false;
  130. /* Determine the total size of packed array. */
  131. if (PB_LTYPE(field->type) == PB_LTYPE_FIXED32)
  132. {
  133. size = 4 * (size_t)count;
  134. }
  135. else if (PB_LTYPE(field->type) == PB_LTYPE_FIXED64)
  136. {
  137. size = 8 * (size_t)count;
  138. }
  139. else
  140. {
  141. pb_ostream_t sizestream = PB_OSTREAM_SIZING;
  142. void *pData_orig = field->pData;
  143. for (i = 0; i < count; i++)
  144. {
  145. if (!pb_enc_varint(&sizestream, field))
  146. PB_RETURN_ERROR(stream, PB_GET_ERROR(&sizestream));
  147. field->pData = (char*)field->pData + field->data_size;
  148. }
  149. field->pData = pData_orig;
  150. size = sizestream.bytes_written;
  151. }
  152. if (!pb_encode_varint(stream, (pb_uint64_t)size))
  153. return false;
  154. if (stream->callback == NULL)
  155. return pb_write(stream, NULL, size); /* Just sizing.. */
  156. /* Write the data */
  157. for (i = 0; i < count; i++)
  158. {
  159. if (PB_LTYPE(field->type) == PB_LTYPE_FIXED32 || PB_LTYPE(field->type) == PB_LTYPE_FIXED64)
  160. {
  161. if (!pb_enc_fixed(stream, field))
  162. return false;
  163. }
  164. else
  165. {
  166. if (!pb_enc_varint(stream, field))
  167. return false;
  168. }
  169. field->pData = (char*)field->pData + field->data_size;
  170. }
  171. }
  172. else /* Unpacked fields */
  173. #endif
  174. {
  175. for (i = 0; i < count; i++)
  176. {
  177. /* Normally the data is stored directly in the array entries, but
  178. * for pointer-type string and bytes fields, the array entries are
  179. * actually pointers themselves also. So we have to dereference once
  180. * more to get to the actual data. */
  181. if (PB_ATYPE(field->type) == PB_ATYPE_POINTER &&
  182. (PB_LTYPE(field->type) == PB_LTYPE_STRING ||
  183. PB_LTYPE(field->type) == PB_LTYPE_BYTES))
  184. {
  185. bool status;
  186. void *pData_orig = field->pData;
  187. field->pData = *(void* const*)field->pData;
  188. if (!field->pData)
  189. {
  190. /* Null pointer in array is treated as empty string / bytes */
  191. status = pb_encode_tag_for_field(stream, field) &&
  192. pb_encode_varint(stream, 0);
  193. }
  194. else
  195. {
  196. status = encode_basic_field(stream, field);
  197. }
  198. field->pData = pData_orig;
  199. if (!status)
  200. return false;
  201. }
  202. else
  203. {
  204. if (!encode_basic_field(stream, field))
  205. return false;
  206. }
  207. field->pData = (char*)field->pData + field->data_size;
  208. }
  209. }
  210. return true;
  211. }
  212. /* In proto3, all fields are optional and are only encoded if their value is "non-zero".
  213. * This function implements the check for the zero value. */
  214. static bool checkreturn pb_check_proto3_default_value(const pb_field_iter_t *field)
  215. {
  216. pb_type_t type = field->type;
  217. if (PB_ATYPE(type) == PB_ATYPE_STATIC)
  218. {
  219. if (PB_HTYPE(type) == PB_HTYPE_REQUIRED)
  220. {
  221. /* Required proto2 fields inside proto3 submessage, pretty rare case */
  222. return false;
  223. }
  224. else if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
  225. {
  226. /* Repeated fields inside proto3 submessage: present if count != 0 */
  227. return *(const pb_size_t*)field->pSize == 0;
  228. }
  229. else if (PB_HTYPE(type) == PB_HTYPE_ONEOF)
  230. {
  231. /* Oneof fields */
  232. return *(const pb_size_t*)field->pSize == 0;
  233. }
  234. else if (PB_HTYPE(type) == PB_HTYPE_OPTIONAL && field->pSize != NULL)
  235. {
  236. /* Proto2 optional fields inside proto3 message, or proto3
  237. * submessage fields. */
  238. return safe_read_bool(field->pSize) == false;
  239. }
  240. else if (field->descriptor->default_value)
  241. {
  242. /* Proto3 messages do not have default values, but proto2 messages
  243. * can contain optional fields without has_fields (generator option 'proto3').
  244. * In this case they must always be encoded, to make sure that the
  245. * non-zero default value is overwritten.
  246. */
  247. return false;
  248. }
  249. /* Rest is proto3 singular fields */
  250. if (PB_LTYPE(type) <= PB_LTYPE_LAST_PACKABLE)
  251. {
  252. /* Simple integer / float fields */
  253. pb_size_t i;
  254. const char *p = (const char*)field->pData;
  255. for (i = 0; i < field->data_size; i++)
  256. {
  257. if (p[i] != 0)
  258. {
  259. return false;
  260. }
  261. }
  262. return true;
  263. }
  264. else if (PB_LTYPE(type) == PB_LTYPE_BYTES)
  265. {
  266. const pb_bytes_array_t *bytes = (const pb_bytes_array_t*)field->pData;
  267. return bytes->size == 0;
  268. }
  269. else if (PB_LTYPE(type) == PB_LTYPE_STRING)
  270. {
  271. return *(const char*)field->pData == '\0';
  272. }
  273. else if (PB_LTYPE(type) == PB_LTYPE_FIXED_LENGTH_BYTES)
  274. {
  275. /* Fixed length bytes is only empty if its length is fixed
  276. * as 0. Which would be pretty strange, but we can check
  277. * it anyway. */
  278. return field->data_size == 0;
  279. }
  280. else if (PB_LTYPE_IS_SUBMSG(type))
  281. {
  282. /* Check all fields in the submessage to find if any of them
  283. * are non-zero. The comparison cannot be done byte-per-byte
  284. * because the C struct may contain padding bytes that must
  285. * be skipped. Note that usually proto3 submessages have
  286. * a separate has_field that is checked earlier in this if.
  287. */
  288. pb_field_iter_t iter;
  289. if (pb_field_iter_begin(&iter, field->submsg_desc, field->pData))
  290. {
  291. do
  292. {
  293. if (!pb_check_proto3_default_value(&iter))
  294. {
  295. return false;
  296. }
  297. } while (pb_field_iter_next(&iter));
  298. }
  299. return true;
  300. }
  301. }
  302. else if (PB_ATYPE(type) == PB_ATYPE_POINTER)
  303. {
  304. return field->pData == NULL;
  305. }
  306. else if (PB_ATYPE(type) == PB_ATYPE_CALLBACK)
  307. {
  308. if (PB_LTYPE(type) == PB_LTYPE_EXTENSION)
  309. {
  310. const pb_extension_t *extension = *(const pb_extension_t* const *)field->pData;
  311. return extension == NULL;
  312. }
  313. else if (field->descriptor->field_callback == pb_default_field_callback)
  314. {
  315. pb_callback_t *pCallback = (pb_callback_t*)field->pData;
  316. return pCallback->funcs.encode == NULL;
  317. }
  318. else
  319. {
  320. return field->descriptor->field_callback == NULL;
  321. }
  322. }
  323. return false; /* Not typically reached, safe default for weird special cases. */
  324. }
  325. /* Encode a field with static or pointer allocation, i.e. one whose data
  326. * is available to the encoder directly. */
  327. static bool checkreturn encode_basic_field(pb_ostream_t *stream, const pb_field_iter_t *field)
  328. {
  329. if (!field->pData)
  330. {
  331. /* Missing pointer field */
  332. return true;
  333. }
  334. if (!pb_encode_tag_for_field(stream, field))
  335. return false;
  336. switch (PB_LTYPE(field->type))
  337. {
  338. case PB_LTYPE_BOOL:
  339. return pb_enc_bool(stream, field);
  340. case PB_LTYPE_VARINT:
  341. case PB_LTYPE_UVARINT:
  342. case PB_LTYPE_SVARINT:
  343. return pb_enc_varint(stream, field);
  344. case PB_LTYPE_FIXED32:
  345. case PB_LTYPE_FIXED64:
  346. return pb_enc_fixed(stream, field);
  347. case PB_LTYPE_BYTES:
  348. return pb_enc_bytes(stream, field);
  349. case PB_LTYPE_STRING:
  350. return pb_enc_string(stream, field);
  351. case PB_LTYPE_SUBMESSAGE:
  352. case PB_LTYPE_SUBMSG_W_CB:
  353. return pb_enc_submessage(stream, field);
  354. case PB_LTYPE_FIXED_LENGTH_BYTES:
  355. return pb_enc_fixed_length_bytes(stream, field);
  356. default:
  357. PB_RETURN_ERROR(stream, "invalid field type");
  358. }
  359. }
  360. /* Encode a field with callback semantics. This means that a user function is
  361. * called to provide and encode the actual data. */
  362. static bool checkreturn encode_callback_field(pb_ostream_t *stream, const pb_field_iter_t *field)
  363. {
  364. if (field->descriptor->field_callback != NULL)
  365. {
  366. if (!field->descriptor->field_callback(NULL, stream, field))
  367. PB_RETURN_ERROR(stream, "callback error");
  368. }
  369. return true;
  370. }
  371. /* Encode a single field of any callback, pointer or static type. */
  372. static bool checkreturn encode_field(pb_ostream_t *stream, pb_field_iter_t *field)
  373. {
  374. /* Check field presence */
  375. if (PB_HTYPE(field->type) == PB_HTYPE_ONEOF)
  376. {
  377. if (*(const pb_size_t*)field->pSize != field->tag)
  378. {
  379. /* Different type oneof field */
  380. return true;
  381. }
  382. }
  383. else if (PB_HTYPE(field->type) == PB_HTYPE_OPTIONAL)
  384. {
  385. if (field->pSize)
  386. {
  387. if (safe_read_bool(field->pSize) == false)
  388. {
  389. /* Missing optional field */
  390. return true;
  391. }
  392. }
  393. else if (PB_ATYPE(field->type) == PB_ATYPE_STATIC)
  394. {
  395. /* Proto3 singular field */
  396. if (pb_check_proto3_default_value(field))
  397. return true;
  398. }
  399. }
  400. if (!field->pData)
  401. {
  402. if (PB_HTYPE(field->type) == PB_HTYPE_REQUIRED)
  403. PB_RETURN_ERROR(stream, "missing required field");
  404. /* Pointer field set to NULL */
  405. return true;
  406. }
  407. /* Then encode field contents */
  408. if (PB_ATYPE(field->type) == PB_ATYPE_CALLBACK)
  409. {
  410. return encode_callback_field(stream, field);
  411. }
  412. else if (PB_HTYPE(field->type) == PB_HTYPE_REPEATED)
  413. {
  414. return encode_array(stream, field);
  415. }
  416. else
  417. {
  418. return encode_basic_field(stream, field);
  419. }
  420. }
  421. /* Default handler for extension fields. Expects to have a pb_msgdesc_t
  422. * pointer in the extension->type->arg field, pointing to a message with
  423. * only one field in it. */
  424. static bool checkreturn default_extension_encoder(pb_ostream_t *stream, const pb_extension_t *extension)
  425. {
  426. pb_field_iter_t iter;
  427. if (!pb_field_iter_begin_extension_const(&iter, extension))
  428. PB_RETURN_ERROR(stream, "invalid extension");
  429. return encode_field(stream, &iter);
  430. }
  431. /* Walk through all the registered extensions and give them a chance
  432. * to encode themselves. */
  433. static bool checkreturn encode_extension_field(pb_ostream_t *stream, const pb_field_iter_t *field)
  434. {
  435. const pb_extension_t *extension = *(const pb_extension_t* const *)field->pData;
  436. while (extension)
  437. {
  438. bool status;
  439. if (extension->type->encode)
  440. status = extension->type->encode(stream, extension);
  441. else
  442. status = default_extension_encoder(stream, extension);
  443. if (!status)
  444. return false;
  445. extension = extension->next;
  446. }
  447. return true;
  448. }
  449. /*********************
  450. * Encode all fields *
  451. *********************/
  452. bool checkreturn pb_encode(pb_ostream_t *stream, const pb_msgdesc_t *fields, const void *src_struct)
  453. {
  454. pb_field_iter_t iter;
  455. if (!pb_field_iter_begin_const(&iter, fields, src_struct))
  456. return true; /* Empty message type */
  457. do {
  458. if (PB_LTYPE(iter.type) == PB_LTYPE_EXTENSION)
  459. {
  460. /* Special case for the extension field placeholder */
  461. if (!encode_extension_field(stream, &iter))
  462. return false;
  463. }
  464. else
  465. {
  466. /* Regular field */
  467. if (!encode_field(stream, &iter))
  468. return false;
  469. }
  470. } while (pb_field_iter_next(&iter));
  471. return true;
  472. }
  473. bool checkreturn pb_encode_ex(pb_ostream_t *stream, const pb_msgdesc_t *fields, const void *src_struct, unsigned int flags)
  474. {
  475. if ((flags & PB_ENCODE_DELIMITED) != 0)
  476. {
  477. return pb_encode_submessage(stream, fields, src_struct);
  478. }
  479. else if ((flags & PB_ENCODE_NULLTERMINATED) != 0)
  480. {
  481. const pb_byte_t zero = 0;
  482. if (!pb_encode(stream, fields, src_struct))
  483. return false;
  484. return pb_write(stream, &zero, 1);
  485. }
  486. else
  487. {
  488. return pb_encode(stream, fields, src_struct);
  489. }
  490. }
  491. bool pb_get_encoded_size(size_t *size, const pb_msgdesc_t *fields, const void *src_struct)
  492. {
  493. pb_ostream_t stream = PB_OSTREAM_SIZING;
  494. if (!pb_encode(&stream, fields, src_struct))
  495. return false;
  496. *size = stream.bytes_written;
  497. return true;
  498. }
  499. /********************
  500. * Helper functions *
  501. ********************/
  502. /* This function avoids 64-bit shifts as they are quite slow on many platforms. */
  503. static bool checkreturn pb_encode_varint_32(pb_ostream_t *stream, uint32_t low, uint32_t high)
  504. {
  505. size_t i = 0;
  506. pb_byte_t buffer[10];
  507. pb_byte_t byte = (pb_byte_t)(low & 0x7F);
  508. low >>= 7;
  509. while (i < 4 && (low != 0 || high != 0))
  510. {
  511. byte |= 0x80;
  512. buffer[i++] = byte;
  513. byte = (pb_byte_t)(low & 0x7F);
  514. low >>= 7;
  515. }
  516. if (high)
  517. {
  518. byte = (pb_byte_t)(byte | ((high & 0x07) << 4));
  519. high >>= 3;
  520. while (high)
  521. {
  522. byte |= 0x80;
  523. buffer[i++] = byte;
  524. byte = (pb_byte_t)(high & 0x7F);
  525. high >>= 7;
  526. }
  527. }
  528. buffer[i++] = byte;
  529. return pb_write(stream, buffer, i);
  530. }
  531. bool checkreturn pb_encode_varint(pb_ostream_t *stream, pb_uint64_t value)
  532. {
  533. if (value <= 0x7F)
  534. {
  535. /* Fast path: single byte */
  536. pb_byte_t byte = (pb_byte_t)value;
  537. return pb_write(stream, &byte, 1);
  538. }
  539. else
  540. {
  541. #ifdef PB_WITHOUT_64BIT
  542. return pb_encode_varint_32(stream, value, 0);
  543. #else
  544. return pb_encode_varint_32(stream, (uint32_t)value, (uint32_t)(value >> 32));
  545. #endif
  546. }
  547. }
  548. bool checkreturn pb_encode_svarint(pb_ostream_t *stream, pb_int64_t value)
  549. {
  550. pb_uint64_t zigzagged;
  551. pb_uint64_t mask = ((pb_uint64_t)-1) >> 1; /* Satisfy clang -fsanitize=integer */
  552. if (value < 0)
  553. zigzagged = ~(((pb_uint64_t)value & mask) << 1);
  554. else
  555. zigzagged = (pb_uint64_t)value << 1;
  556. return pb_encode_varint(stream, zigzagged);
  557. }
  558. bool checkreturn pb_encode_fixed32(pb_ostream_t *stream, const void *value)
  559. {
  560. #if defined(PB_LITTLE_ENDIAN_8BIT) && PB_LITTLE_ENDIAN_8BIT == 1
  561. /* Fast path if we know that we're on little endian */
  562. return pb_write(stream, (const pb_byte_t*)value, 4);
  563. #else
  564. uint32_t val = *(const uint32_t*)value;
  565. pb_byte_t bytes[4];
  566. bytes[0] = (pb_byte_t)(val & 0xFF);
  567. bytes[1] = (pb_byte_t)((val >> 8) & 0xFF);
  568. bytes[2] = (pb_byte_t)((val >> 16) & 0xFF);
  569. bytes[3] = (pb_byte_t)((val >> 24) & 0xFF);
  570. return pb_write(stream, bytes, 4);
  571. #endif
  572. }
  573. #ifndef PB_WITHOUT_64BIT
  574. bool checkreturn pb_encode_fixed64(pb_ostream_t *stream, const void *value)
  575. {
  576. #if defined(PB_LITTLE_ENDIAN_8BIT) && PB_LITTLE_ENDIAN_8BIT == 1
  577. /* Fast path if we know that we're on little endian */
  578. return pb_write(stream, (const pb_byte_t*)value, 8);
  579. #else
  580. uint64_t val = *(const uint64_t*)value;
  581. pb_byte_t bytes[8];
  582. bytes[0] = (pb_byte_t)(val & 0xFF);
  583. bytes[1] = (pb_byte_t)((val >> 8) & 0xFF);
  584. bytes[2] = (pb_byte_t)((val >> 16) & 0xFF);
  585. bytes[3] = (pb_byte_t)((val >> 24) & 0xFF);
  586. bytes[4] = (pb_byte_t)((val >> 32) & 0xFF);
  587. bytes[5] = (pb_byte_t)((val >> 40) & 0xFF);
  588. bytes[6] = (pb_byte_t)((val >> 48) & 0xFF);
  589. bytes[7] = (pb_byte_t)((val >> 56) & 0xFF);
  590. return pb_write(stream, bytes, 8);
  591. #endif
  592. }
  593. #endif
  594. bool checkreturn pb_encode_tag(pb_ostream_t *stream, pb_wire_type_t wiretype, uint32_t field_number)
  595. {
  596. pb_uint64_t tag = ((pb_uint64_t)field_number << 3) | wiretype;
  597. return pb_encode_varint(stream, tag);
  598. }
  599. bool pb_encode_tag_for_field ( pb_ostream_t* stream, const pb_field_iter_t* field )
  600. {
  601. pb_wire_type_t wiretype;
  602. switch (PB_LTYPE(field->type))
  603. {
  604. case PB_LTYPE_BOOL:
  605. case PB_LTYPE_VARINT:
  606. case PB_LTYPE_UVARINT:
  607. case PB_LTYPE_SVARINT:
  608. wiretype = PB_WT_VARINT;
  609. break;
  610. case PB_LTYPE_FIXED32:
  611. wiretype = PB_WT_32BIT;
  612. break;
  613. case PB_LTYPE_FIXED64:
  614. wiretype = PB_WT_64BIT;
  615. break;
  616. case PB_LTYPE_BYTES:
  617. case PB_LTYPE_STRING:
  618. case PB_LTYPE_SUBMESSAGE:
  619. case PB_LTYPE_SUBMSG_W_CB:
  620. case PB_LTYPE_FIXED_LENGTH_BYTES:
  621. wiretype = PB_WT_STRING;
  622. break;
  623. default:
  624. PB_RETURN_ERROR(stream, "invalid field type");
  625. }
  626. return pb_encode_tag(stream, wiretype, field->tag);
  627. }
  628. bool checkreturn pb_encode_string(pb_ostream_t *stream, const pb_byte_t *buffer, size_t size)
  629. {
  630. if (!pb_encode_varint(stream, (pb_uint64_t)size))
  631. return false;
  632. return pb_write(stream, buffer, size);
  633. }
  634. bool checkreturn pb_encode_submessage(pb_ostream_t *stream, const pb_msgdesc_t *fields, const void *src_struct)
  635. {
  636. /* First calculate the message size using a non-writing substream. */
  637. pb_ostream_t substream = PB_OSTREAM_SIZING;
  638. size_t size;
  639. bool status;
  640. if (!pb_encode(&substream, fields, src_struct))
  641. {
  642. #ifndef PB_NO_ERRMSG
  643. stream->errmsg = substream.errmsg;
  644. #endif
  645. return false;
  646. }
  647. size = substream.bytes_written;
  648. if (!pb_encode_varint(stream, (pb_uint64_t)size))
  649. return false;
  650. if (stream->callback == NULL)
  651. return pb_write(stream, NULL, size); /* Just sizing */
  652. if (stream->bytes_written + size > stream->max_size)
  653. PB_RETURN_ERROR(stream, "stream full");
  654. /* Use a substream to verify that a callback doesn't write more than
  655. * what it did the first time. */
  656. substream.callback = stream->callback;
  657. substream.state = stream->state;
  658. substream.max_size = size;
  659. substream.bytes_written = 0;
  660. #ifndef PB_NO_ERRMSG
  661. substream.errmsg = NULL;
  662. #endif
  663. status = pb_encode(&substream, fields, src_struct);
  664. stream->bytes_written += substream.bytes_written;
  665. stream->state = substream.state;
  666. #ifndef PB_NO_ERRMSG
  667. stream->errmsg = substream.errmsg;
  668. #endif
  669. if (substream.bytes_written != size)
  670. PB_RETURN_ERROR(stream, "submsg size changed");
  671. return status;
  672. }
  673. /* Field encoders */
  674. static bool checkreturn pb_enc_bool(pb_ostream_t *stream, const pb_field_iter_t *field)
  675. {
  676. uint32_t value = safe_read_bool(field->pData) ? 1 : 0;
  677. PB_UNUSED(field);
  678. return pb_encode_varint(stream, value);
  679. }
  680. static bool checkreturn pb_enc_varint(pb_ostream_t *stream, const pb_field_iter_t *field)
  681. {
  682. if (PB_LTYPE(field->type) == PB_LTYPE_UVARINT)
  683. {
  684. /* Perform unsigned integer extension */
  685. pb_uint64_t value = 0;
  686. if (field->data_size == sizeof(uint_least8_t))
  687. value = *(const uint_least8_t*)field->pData;
  688. else if (field->data_size == sizeof(uint_least16_t))
  689. value = *(const uint_least16_t*)field->pData;
  690. else if (field->data_size == sizeof(uint32_t))
  691. value = *(const uint32_t*)field->pData;
  692. else if (field->data_size == sizeof(pb_uint64_t))
  693. value = *(const pb_uint64_t*)field->pData;
  694. else
  695. PB_RETURN_ERROR(stream, "invalid data_size");
  696. return pb_encode_varint(stream, value);
  697. }
  698. else
  699. {
  700. /* Perform signed integer extension */
  701. pb_int64_t value = 0;
  702. if (field->data_size == sizeof(int_least8_t))
  703. value = *(const int_least8_t*)field->pData;
  704. else if (field->data_size == sizeof(int_least16_t))
  705. value = *(const int_least16_t*)field->pData;
  706. else if (field->data_size == sizeof(int32_t))
  707. value = *(const int32_t*)field->pData;
  708. else if (field->data_size == sizeof(pb_int64_t))
  709. value = *(const pb_int64_t*)field->pData;
  710. else
  711. PB_RETURN_ERROR(stream, "invalid data_size");
  712. if (PB_LTYPE(field->type) == PB_LTYPE_SVARINT)
  713. return pb_encode_svarint(stream, value);
  714. #ifdef PB_WITHOUT_64BIT
  715. else if (value < 0)
  716. return pb_encode_varint_32(stream, (uint32_t)value, (uint32_t)-1);
  717. #endif
  718. else
  719. return pb_encode_varint(stream, (pb_uint64_t)value);
  720. }
  721. }
  722. static bool checkreturn pb_enc_fixed(pb_ostream_t *stream, const pb_field_iter_t *field)
  723. {
  724. #ifdef PB_CONVERT_DOUBLE_FLOAT
  725. if (field->data_size == sizeof(float) && PB_LTYPE(field->type) == PB_LTYPE_FIXED64)
  726. {
  727. return pb_encode_float_as_double(stream, *(float*)field->pData);
  728. }
  729. #endif
  730. if (field->data_size == sizeof(uint32_t))
  731. {
  732. return pb_encode_fixed32(stream, field->pData);
  733. }
  734. #ifndef PB_WITHOUT_64BIT
  735. else if (field->data_size == sizeof(uint64_t))
  736. {
  737. return pb_encode_fixed64(stream, field->pData);
  738. }
  739. #endif
  740. else
  741. {
  742. PB_RETURN_ERROR(stream, "invalid data_size");
  743. }
  744. }
  745. static bool checkreturn pb_enc_bytes(pb_ostream_t *stream, const pb_field_iter_t *field)
  746. {
  747. const pb_bytes_array_t *bytes = NULL;
  748. bytes = (const pb_bytes_array_t*)field->pData;
  749. if (bytes == NULL)
  750. {
  751. /* Treat null pointer as an empty bytes field */
  752. return pb_encode_string(stream, NULL, 0);
  753. }
  754. if (PB_ATYPE(field->type) == PB_ATYPE_STATIC &&
  755. bytes->size > field->data_size - offsetof(pb_bytes_array_t, bytes))
  756. {
  757. PB_RETURN_ERROR(stream, "bytes size exceeded");
  758. }
  759. return pb_encode_string(stream, bytes->bytes, (size_t)bytes->size);
  760. }
  761. static bool checkreturn pb_enc_string(pb_ostream_t *stream, const pb_field_iter_t *field)
  762. {
  763. size_t size = 0;
  764. size_t max_size = (size_t)field->data_size;
  765. const char *str = (const char*)field->pData;
  766. if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
  767. {
  768. max_size = (size_t)-1;
  769. }
  770. else
  771. {
  772. /* pb_dec_string() assumes string fields end with a null
  773. * terminator when the type isn't PB_ATYPE_POINTER, so we
  774. * shouldn't allow more than max-1 bytes to be written to
  775. * allow space for the null terminator.
  776. */
  777. if (max_size == 0)
  778. PB_RETURN_ERROR(stream, "zero-length string");
  779. max_size -= 1;
  780. }
  781. if (str == NULL)
  782. {
  783. size = 0; /* Treat null pointer as an empty string */
  784. }
  785. else
  786. {
  787. const char *p = str;
  788. /* strnlen() is not always available, so just use a loop */
  789. while (size < max_size && *p != '\0')
  790. {
  791. size++;
  792. p++;
  793. }
  794. if (*p != '\0')
  795. {
  796. PB_RETURN_ERROR(stream, "unterminated string");
  797. }
  798. }
  799. #ifdef PB_VALIDATE_UTF8
  800. if (!pb_validate_utf8(str))
  801. PB_RETURN_ERROR(stream, "invalid utf8");
  802. #endif
  803. return pb_encode_string(stream, (const pb_byte_t*)str, size);
  804. }
  805. static bool checkreturn pb_enc_submessage(pb_ostream_t *stream, const pb_field_iter_t *field)
  806. {
  807. if (field->submsg_desc == NULL)
  808. PB_RETURN_ERROR(stream, "invalid field descriptor");
  809. if (PB_LTYPE(field->type) == PB_LTYPE_SUBMSG_W_CB && field->pSize != NULL)
  810. {
  811. /* Message callback is stored right before pSize. */
  812. pb_callback_t *callback = (pb_callback_t*)field->pSize - 1;
  813. if (callback->funcs.encode)
  814. {
  815. if (!callback->funcs.encode(stream, field, &callback->arg))
  816. return false;
  817. }
  818. }
  819. return pb_encode_submessage(stream, field->submsg_desc, field->pData);
  820. }
  821. static bool checkreturn pb_enc_fixed_length_bytes(pb_ostream_t *stream, const pb_field_iter_t *field)
  822. {
  823. return pb_encode_string(stream, (const pb_byte_t*)field->pData, (size_t)field->data_size);
  824. }
  825. #ifdef PB_CONVERT_DOUBLE_FLOAT
  826. bool pb_encode_float_as_double(pb_ostream_t *stream, float value)
  827. {
  828. union { float f; uint32_t i; } in;
  829. uint_least8_t sign;
  830. int exponent;
  831. uint64_t mantissa;
  832. in.f = value;
  833. /* Decompose input value */
  834. sign = (uint_least8_t)((in.i >> 31) & 1);
  835. exponent = (int)((in.i >> 23) & 0xFF) - 127;
  836. mantissa = in.i & 0x7FFFFF;
  837. if (exponent == 128)
  838. {
  839. /* Special value (NaN etc.) */
  840. exponent = 1024;
  841. }
  842. else if (exponent == -127)
  843. {
  844. if (!mantissa)
  845. {
  846. /* Zero */
  847. exponent = -1023;
  848. }
  849. else
  850. {
  851. /* Denormalized */
  852. mantissa <<= 1;
  853. while (!(mantissa & 0x800000))
  854. {
  855. mantissa <<= 1;
  856. exponent--;
  857. }
  858. mantissa &= 0x7FFFFF;
  859. }
  860. }
  861. /* Combine fields */
  862. mantissa <<= 29;
  863. mantissa |= (uint64_t)(exponent + 1023) << 52;
  864. mantissa |= (uint64_t)sign << 63;
  865. return pb_encode_fixed64(stream, &mantissa);
  866. }
  867. #endif