PageRenderTime 71ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/src/google/protobuf-c/protobuf-c.c

http://protobuf-c.googlecode.com/
C | 2163 lines | 1861 code | 114 blank | 188 comment | 317 complexity | 0d5846277d9eca2be77613833c4b4f92 MD5 | raw file
Possible License(s): BSD-3-Clause

Large files files are truncated, but you can click here to view the full file

  1. /* --- protobuf-c.c: public protobuf c runtime implementation --- */
  2. /*
  3. * Copyright (c) 2008-2011, Dave Benson.
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with
  8. * or without modification, are permitted provided that the
  9. * following conditions are met:
  10. *
  11. * Redistributions of source code must retain the above
  12. * copyright notice, this list of conditions and the following
  13. * disclaimer.
  14. * Redistributions in binary form must reproduce
  15. * the above copyright notice, this list of conditions and
  16. * the following disclaimer in the documentation and/or other
  17. * materials provided with the distribution.
  18. *
  19. * Neither the name
  20. * of "protobuf-c" nor the names of its contributors
  21. * may be used to endorse or promote products derived from
  22. * this software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  25. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  26. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  27. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
  29. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  30. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  31. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  32. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  33. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  34. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  35. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  36. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  37. * POSSIBILITY OF SUCH DAMAGE.
  38. */
  39. /* TODO items:
  40. * 64-BIT OPTIMIZATION: certain implementations use 32-bit math even on 64-bit platforms
  41. (uint64_size, uint64_pack, parse_uint64)
  42. * get_packed_size and pack seem to use type-prefixed names,
  43. whereas parse uses type-suffixed names. pick one and stick with it.
  44. Decision: go with type-suffixed, since the type (or its instance)
  45. is typically the object of the verb.
  46. NOTE: perhaps the "parse" methods should be reanemd to "unpack"
  47. at the same time. (this only affects internal (static) functions)
  48. * use TRUE and FALSE instead of 1 and 0 as appropriate
  49. * use size_t consistently
  50. */
  51. #if HAVE_PROTOBUF_C_CONFIG_H
  52. #include "protobuf-c-config.h"
  53. #endif
  54. #include <stdio.h> /* for occasional printf()s */
  55. #include <stdlib.h> /* for abort(), malloc() etc */
  56. #include <string.h> /* for strlen(), memcpy(), memmove() */
  57. #if 0 /* we no longer use alloca. */
  58. #if HAVE_ALLOCA_H
  59. #include <alloca.h>
  60. #elif HAVE_MALLOC_H
  61. #include <malloc.h>
  62. #endif
  63. #endif
  64. #ifndef PRINT_UNPACK_ERRORS
  65. #define PRINT_UNPACK_ERRORS 1
  66. #endif
  67. #include "protobuf-c.h"
  68. unsigned protobuf_c_major = PROTOBUF_C_MAJOR;
  69. unsigned protobuf_c_minor = PROTOBUF_C_MINOR;
  70. #define MAX_UINT64_ENCODED_SIZE 10
  71. /* This should be roughly the biggest message you think you'll encounter.
  72. However, the only point of the hashing is to detect uninitialized required members.
  73. I doubt many messages have 128 REQUIRED fields, so hopefully this'll be fine.
  74. TODO: A better solution is to separately in the descriptor index the required fields,
  75. and use the allcoator if the required field count is too big. */
  76. #define MAX_MEMBERS_FOR_HASH_SIZE 128
  77. /* convenience macros */
  78. #define TMPALLOC(allocator, size) ((allocator)->tmp_alloc ((allocator)->allocator_data, (size)))
  79. #define FREE(allocator, ptr) \
  80. do { if ((ptr) != NULL) ((allocator)->free ((allocator)->allocator_data, (ptr))); } while(0)
  81. #define UNALIGNED_ALLOC(allocator, size) ALLOC (allocator, size) /* placeholder */
  82. #define STRUCT_MEMBER_P(struct_p, struct_offset) \
  83. ((void *) ((uint8_t*) (struct_p) + (struct_offset)))
  84. #define STRUCT_MEMBER(member_type, struct_p, struct_offset) \
  85. (*(member_type*) STRUCT_MEMBER_P ((struct_p), (struct_offset)))
  86. #define STRUCT_MEMBER_PTR(member_type, struct_p, struct_offset) \
  87. ((member_type*) STRUCT_MEMBER_P ((struct_p), (struct_offset)))
  88. #define TRUE 1
  89. #define FALSE 0
  90. static void
  91. alloc_failed_warning (unsigned size, const char *filename, unsigned line)
  92. {
  93. fprintf (stderr,
  94. "WARNING: out-of-memory allocating a block of size %u (%s:%u)\n",
  95. size, filename, line);
  96. }
  97. /* Try to allocate memory, running some special code if it fails. */
  98. #define DO_ALLOC(dst, allocator, size, fail_code) \
  99. { size_t da__allocation_size = (size); \
  100. if (da__allocation_size == 0) \
  101. dst = NULL; \
  102. else if ((dst=((allocator)->alloc ((allocator)->allocator_data, \
  103. da__allocation_size))) == NULL) \
  104. { \
  105. alloc_failed_warning (da__allocation_size, __FILE__, __LINE__); \
  106. fail_code; \
  107. } \
  108. }
  109. #define DO_UNALIGNED_ALLOC DO_ALLOC /* placeholder */
  110. #define ASSERT_IS_ENUM_DESCRIPTOR(desc) \
  111. assert((desc)->magic == PROTOBUF_C_ENUM_DESCRIPTOR_MAGIC)
  112. #define ASSERT_IS_MESSAGE_DESCRIPTOR(desc) \
  113. assert((desc)->magic == PROTOBUF_C_MESSAGE_DESCRIPTOR_MAGIC)
  114. #define ASSERT_IS_MESSAGE(message) \
  115. ASSERT_IS_MESSAGE_DESCRIPTOR((message)->descriptor)
  116. #define ASSERT_IS_SERVICE_DESCRIPTOR(desc) \
  117. assert((desc)->magic == PROTOBUF_C_SERVICE_DESCRIPTOR_MAGIC)
  118. /* --- allocator --- */
  119. static void protobuf_c_out_of_memory_default (void)
  120. {
  121. fprintf (stderr, "Out Of Memory!!!\n");
  122. abort ();
  123. }
  124. void (*protobuf_c_out_of_memory) (void) = protobuf_c_out_of_memory_default;
  125. static void *system_alloc(void *allocator_data, size_t size)
  126. {
  127. void *rv;
  128. (void) allocator_data;
  129. if (size == 0)
  130. return NULL;
  131. rv = malloc (size);
  132. if (rv == NULL)
  133. protobuf_c_out_of_memory ();
  134. return rv;
  135. }
  136. static void system_free (void *allocator_data, void *data)
  137. {
  138. (void) allocator_data;
  139. if (data)
  140. free (data);
  141. }
  142. /* Some users may configure the default allocator;
  143. providing your own allocator to unpack() is prefered.
  144. this allocator is still used for packing nested messages. */
  145. ProtobufCAllocator protobuf_c_default_allocator =
  146. {
  147. system_alloc,
  148. system_free,
  149. NULL, /* tmp_alloc */
  150. 8192, /* max_alloca */
  151. NULL /* allocator_data */
  152. };
  153. /* Users should NOT modify this structure,
  154. but it's difficult to prevent.
  155. please modify protobuf_c_default_allocator instead. */
  156. ProtobufCAllocator protobuf_c_system_allocator =
  157. {
  158. system_alloc,
  159. system_free,
  160. NULL, /* tmp_alloc */
  161. 8192, /* max_alloca */
  162. NULL /* allocator_data */
  163. };
  164. /* === buffer-simple === */
  165. void
  166. protobuf_c_buffer_simple_append (ProtobufCBuffer *buffer,
  167. size_t len,
  168. const uint8_t *data)
  169. {
  170. ProtobufCBufferSimple *simp = (ProtobufCBufferSimple *) buffer;
  171. size_t new_len = simp->len + len;
  172. if (new_len > simp->alloced)
  173. {
  174. size_t new_alloced = simp->alloced * 2;
  175. uint8_t *new_data;
  176. while (new_alloced < new_len)
  177. new_alloced += new_alloced;
  178. DO_ALLOC (new_data, &protobuf_c_default_allocator, new_alloced, return);
  179. memcpy (new_data, simp->data, simp->len);
  180. if (simp->must_free_data)
  181. FREE (&protobuf_c_default_allocator, simp->data);
  182. else
  183. simp->must_free_data = 1;
  184. simp->data = new_data;
  185. simp->alloced = new_alloced;
  186. }
  187. memcpy (simp->data + simp->len, data, len);
  188. simp->len = new_len;
  189. }
  190. /* === get_packed_size() === */
  191. /* Return the number of bytes required to store the
  192. tag for the field (which includes 3 bits for
  193. the wire-type, and a single bit that denotes the end-of-tag. */
  194. static inline size_t
  195. get_tag_size (unsigned number)
  196. {
  197. if (number < (1<<4))
  198. return 1;
  199. else if (number < (1<<11))
  200. return 2;
  201. else if (number < (1<<18))
  202. return 3;
  203. else if (number < (1<<25))
  204. return 4;
  205. else
  206. return 5;
  207. }
  208. /* Return the number of bytes required to store
  209. a variable-length unsigned integer that fits in 32-bit uint
  210. in base-128 encoding. */
  211. static inline size_t
  212. uint32_size (uint32_t v)
  213. {
  214. if (v < (1<<7))
  215. return 1;
  216. else if (v < (1<<14))
  217. return 2;
  218. else if (v < (1<<21))
  219. return 3;
  220. else if (v < (1<<28))
  221. return 4;
  222. else
  223. return 5;
  224. }
  225. /* Return the number of bytes required to store
  226. a variable-length signed integer that fits in 32-bit int
  227. in base-128 encoding. */
  228. static inline size_t
  229. int32_size (int32_t v)
  230. {
  231. if (v < 0)
  232. return 10;
  233. else if (v < (1<<7))
  234. return 1;
  235. else if (v < (1<<14))
  236. return 2;
  237. else if (v < (1<<21))
  238. return 3;
  239. else if (v < (1<<28))
  240. return 4;
  241. else
  242. return 5;
  243. }
  244. /* return the zigzag-encoded 32-bit unsigned int from a 32-bit signed int */
  245. static inline uint32_t
  246. zigzag32 (int32_t v)
  247. {
  248. if (v < 0)
  249. return ((uint32_t)(-v)) * 2 - 1;
  250. else
  251. return v * 2;
  252. }
  253. /* Return the number of bytes required to store
  254. a variable-length signed integer that fits in 32-bit int,
  255. converted to unsigned via the zig-zag algorithm,
  256. then packed using base-128 encoding. */
  257. static inline size_t
  258. sint32_size (int32_t v)
  259. {
  260. return uint32_size(zigzag32(v));
  261. }
  262. /* Return the number of bytes required to store
  263. a variable-length unsigned integer that fits in 64-bit uint
  264. in base-128 encoding. */
  265. static inline size_t
  266. uint64_size (uint64_t v)
  267. {
  268. uint32_t upper_v = (uint32_t )(v>>32);
  269. if (upper_v == 0)
  270. return uint32_size ((uint32_t)v);
  271. else if (upper_v < (1<<3))
  272. return 5;
  273. else if (upper_v < (1<<10))
  274. return 6;
  275. else if (upper_v < (1<<17))
  276. return 7;
  277. else if (upper_v < (1<<24))
  278. return 8;
  279. else if (upper_v < (1U<<31))
  280. return 9;
  281. else
  282. return 10;
  283. }
  284. /* return the zigzag-encoded 64-bit unsigned int from a 64-bit signed int */
  285. static inline uint64_t
  286. zigzag64 (int64_t v)
  287. {
  288. if (v < 0)
  289. return ((uint64_t)(-v)) * 2 - 1;
  290. else
  291. return v * 2;
  292. }
  293. /* Return the number of bytes required to store
  294. a variable-length signed integer that fits in 64-bit int,
  295. converted to unsigned via the zig-zag algorithm,
  296. then packed using base-128 encoding. */
  297. static inline size_t
  298. sint64_size (int64_t v)
  299. {
  300. return uint64_size(zigzag64(v));
  301. }
  302. /* Get serialized size of a single field in the message,
  303. including the space needed by the identifying tag. */
  304. static size_t
  305. required_field_get_packed_size (const ProtobufCFieldDescriptor *field,
  306. const void *member)
  307. {
  308. size_t rv = get_tag_size (field->id);
  309. switch (field->type)
  310. {
  311. case PROTOBUF_C_TYPE_SINT32:
  312. return rv + sint32_size (*(const int32_t *) member);
  313. case PROTOBUF_C_TYPE_INT32:
  314. return rv + int32_size (*(const uint32_t *) member);
  315. case PROTOBUF_C_TYPE_UINT32:
  316. return rv + uint32_size (*(const uint32_t *) member);
  317. case PROTOBUF_C_TYPE_SINT64:
  318. return rv + sint64_size (*(const int64_t *) member);
  319. case PROTOBUF_C_TYPE_INT64:
  320. case PROTOBUF_C_TYPE_UINT64:
  321. return rv + uint64_size (*(const uint64_t *) member);
  322. case PROTOBUF_C_TYPE_SFIXED32:
  323. case PROTOBUF_C_TYPE_FIXED32:
  324. return rv + 4;
  325. case PROTOBUF_C_TYPE_SFIXED64:
  326. case PROTOBUF_C_TYPE_FIXED64:
  327. return rv + 8;
  328. case PROTOBUF_C_TYPE_BOOL:
  329. return rv + 1;
  330. case PROTOBUF_C_TYPE_FLOAT:
  331. return rv + 4;
  332. case PROTOBUF_C_TYPE_DOUBLE:
  333. return rv + 8;
  334. case PROTOBUF_C_TYPE_ENUM:
  335. // TODO: is this correct for negative-valued enums?
  336. return rv + uint32_size (*(const uint32_t *) member);
  337. case PROTOBUF_C_TYPE_STRING:
  338. {
  339. const char *str = *(char * const *) member;
  340. size_t len = str ? strlen (str) : 0;
  341. return rv + uint32_size (len) + len;
  342. }
  343. case PROTOBUF_C_TYPE_BYTES:
  344. {
  345. size_t len = ((const ProtobufCBinaryData*) member)->len;
  346. return rv + uint32_size (len) + len;
  347. }
  348. //case PROTOBUF_C_TYPE_GROUP:
  349. case PROTOBUF_C_TYPE_MESSAGE:
  350. {
  351. const ProtobufCMessage *msg = * (ProtobufCMessage * const *) member;
  352. size_t subrv = msg ? protobuf_c_message_get_packed_size (msg) : 0;
  353. return rv + uint32_size (subrv) + subrv;
  354. }
  355. }
  356. PROTOBUF_C_ASSERT_NOT_REACHED ();
  357. return 0;
  358. }
  359. /* Get serialized size of a single optional field in the message,
  360. including the space needed by the identifying tag.
  361. Returns 0 if the optional field isn't set. */
  362. static size_t
  363. optional_field_get_packed_size (const ProtobufCFieldDescriptor *field,
  364. const protobuf_c_boolean *has,
  365. const void *member)
  366. {
  367. if (field->type == PROTOBUF_C_TYPE_MESSAGE
  368. || field->type == PROTOBUF_C_TYPE_STRING)
  369. {
  370. const void *ptr = * (const void * const *) member;
  371. if (ptr == NULL
  372. || ptr == field->default_value)
  373. return 0;
  374. }
  375. else
  376. {
  377. if (!*has)
  378. return 0;
  379. }
  380. return required_field_get_packed_size (field, member);
  381. }
  382. /* Get serialized size of a repeated field in the message,
  383. which may consist of any number of values (including 0).
  384. Includes the space needed by the identifying tags (as needed). */
  385. static size_t
  386. repeated_field_get_packed_size (const ProtobufCFieldDescriptor *field,
  387. size_t count,
  388. const void *member)
  389. {
  390. size_t header_size;
  391. size_t rv = 0;
  392. unsigned i;
  393. void *array = * (void * const *) member;
  394. if (count == 0)
  395. return 0;
  396. header_size = get_tag_size (field->id);
  397. if (!field->packed)
  398. header_size *= count;
  399. switch (field->type)
  400. {
  401. case PROTOBUF_C_TYPE_SINT32:
  402. for (i = 0; i < count; i++)
  403. rv += sint32_size (((int32_t*)array)[i]);
  404. break;
  405. case PROTOBUF_C_TYPE_INT32:
  406. for (i = 0; i < count; i++)
  407. rv += int32_size (((uint32_t*)array)[i]);
  408. break;
  409. case PROTOBUF_C_TYPE_UINT32:
  410. case PROTOBUF_C_TYPE_ENUM:
  411. for (i = 0; i < count; i++)
  412. rv += uint32_size (((uint32_t*)array)[i]);
  413. break;
  414. case PROTOBUF_C_TYPE_SINT64:
  415. for (i = 0; i < count; i++)
  416. rv += sint64_size (((int64_t*)array)[i]);
  417. break;
  418. case PROTOBUF_C_TYPE_INT64:
  419. case PROTOBUF_C_TYPE_UINT64:
  420. for (i = 0; i < count; i++)
  421. rv += uint64_size (((uint64_t*)array)[i]);
  422. break;
  423. case PROTOBUF_C_TYPE_SFIXED32:
  424. case PROTOBUF_C_TYPE_FIXED32:
  425. case PROTOBUF_C_TYPE_FLOAT:
  426. rv += 4 * count;
  427. break;
  428. case PROTOBUF_C_TYPE_SFIXED64:
  429. case PROTOBUF_C_TYPE_FIXED64:
  430. case PROTOBUF_C_TYPE_DOUBLE:
  431. rv += 8 * count;
  432. break;
  433. case PROTOBUF_C_TYPE_BOOL:
  434. rv += count;
  435. break;
  436. case PROTOBUF_C_TYPE_STRING:
  437. for (i = 0; i < count; i++)
  438. {
  439. size_t len = strlen (((char**) array)[i]);
  440. rv += uint32_size (len) + len;
  441. }
  442. break;
  443. case PROTOBUF_C_TYPE_BYTES:
  444. for (i = 0; i < count; i++)
  445. {
  446. size_t len = ((ProtobufCBinaryData*) array)[i].len;
  447. rv += uint32_size (len) + len;
  448. }
  449. break;
  450. case PROTOBUF_C_TYPE_MESSAGE:
  451. for (i = 0; i < count; i++)
  452. {
  453. size_t len = protobuf_c_message_get_packed_size (((ProtobufCMessage **) array)[i]);
  454. rv += uint32_size (len) + len;
  455. }
  456. break;
  457. //case PROTOBUF_C_TYPE_GROUP: // NOT SUPPORTED
  458. }
  459. if (field->packed)
  460. header_size += uint32_size (rv);
  461. return header_size + rv;
  462. }
  463. /* Get the packed size of a unknown field (meaning one that
  464. is passed through mostly uninterpreted... this is done
  465. for forward compatibilty with the addition of new fields). */
  466. static inline size_t
  467. unknown_field_get_packed_size (const ProtobufCMessageUnknownField *field)
  468. {
  469. return get_tag_size (field->tag) + field->len;
  470. }
  471. /* Get the number of bytes that the message will occupy once serialized. */
  472. size_t
  473. protobuf_c_message_get_packed_size(const ProtobufCMessage *message)
  474. {
  475. unsigned i;
  476. size_t rv = 0;
  477. ASSERT_IS_MESSAGE (message);
  478. for (i = 0; i < message->descriptor->n_fields; i++)
  479. {
  480. const ProtobufCFieldDescriptor *field = message->descriptor->fields + i;
  481. const void *member = ((const char *) message) + field->offset;
  482. const void *qmember = ((const char *) message) + field->quantifier_offset;
  483. if (field->label == PROTOBUF_C_LABEL_REQUIRED)
  484. rv += required_field_get_packed_size (field, member);
  485. else if (field->label == PROTOBUF_C_LABEL_OPTIONAL)
  486. rv += optional_field_get_packed_size (field, qmember, member);
  487. else
  488. rv += repeated_field_get_packed_size (field, * (const size_t *) qmember, member);
  489. }
  490. for (i = 0; i < message->n_unknown_fields; i++)
  491. rv += unknown_field_get_packed_size (&message->unknown_fields[i]);
  492. return rv;
  493. }
  494. /* === pack() === */
  495. /* Pack an unsigned 32-bit integer in base-128 encoding, and return the number of bytes needed:
  496. this will be 5 or less. */
  497. static inline size_t
  498. uint32_pack (uint32_t value, uint8_t *out)
  499. {
  500. unsigned rv = 0;
  501. if (value >= 0x80)
  502. {
  503. out[rv++] = value | 0x80;
  504. value >>= 7;
  505. if (value >= 0x80)
  506. {
  507. out[rv++] = value | 0x80;
  508. value >>= 7;
  509. if (value >= 0x80)
  510. {
  511. out[rv++] = value | 0x80;
  512. value >>= 7;
  513. if (value >= 0x80)
  514. {
  515. out[rv++] = value | 0x80;
  516. value >>= 7;
  517. }
  518. }
  519. }
  520. }
  521. /* assert: value<128 */
  522. out[rv++] = value;
  523. return rv;
  524. }
  525. /* Pack a 32-bit signed integer, returning the number of bytes needed.
  526. Negative numbers are packed as twos-complement 64-bit integers. */
  527. static inline size_t
  528. int32_pack (int32_t value, uint8_t *out)
  529. {
  530. if (value < 0)
  531. {
  532. out[0] = value | 0x80;
  533. out[1] = (value>>7) | 0x80;
  534. out[2] = (value>>14) | 0x80;
  535. out[3] = (value>>21) | 0x80;
  536. out[4] = (value>>28) | 0x80;
  537. out[5] = out[6] = out[7] = out[8] = 0xff;
  538. out[9] = 0x01;
  539. return 10;
  540. }
  541. else
  542. return uint32_pack (value, out);
  543. }
  544. /* Pack a 32-bit integer in zigzag encoding. */
  545. static inline size_t
  546. sint32_pack (int32_t value, uint8_t *out)
  547. {
  548. return uint32_pack (zigzag32 (value), out);
  549. }
  550. /* Pack a 64-bit unsigned integer that fits in a 64-bit uint,
  551. using base-128 encoding. */
  552. static size_t
  553. uint64_pack (uint64_t value, uint8_t *out)
  554. {
  555. uint32_t hi = (uint32_t )(value>>32);
  556. uint32_t lo = (uint32_t )value;
  557. unsigned rv;
  558. if (hi == 0)
  559. return uint32_pack ((uint32_t)lo, out);
  560. out[0] = (lo) | 0x80;
  561. out[1] = (lo>>7) | 0x80;
  562. out[2] = (lo>>14) | 0x80;
  563. out[3] = (lo>>21) | 0x80;
  564. if (hi < 8)
  565. {
  566. out[4] = (hi<<4) | (lo>>28);
  567. return 5;
  568. }
  569. else
  570. {
  571. out[4] = ((hi&7)<<4) | (lo>>28) | 0x80;
  572. hi >>= 3;
  573. }
  574. rv = 5;
  575. while (hi >= 128)
  576. {
  577. out[rv++] = hi | 0x80;
  578. hi >>= 7;
  579. }
  580. out[rv++] = hi;
  581. return rv;
  582. }
  583. /* Pack a 64-bit signed integer in zigzan encoding,
  584. return the size of the packed output.
  585. (Max returned value is 10) */
  586. static inline size_t
  587. sint64_pack (int64_t value, uint8_t *out)
  588. {
  589. return uint64_pack (zigzag64 (value), out);
  590. }
  591. /* Pack a 32-bit value, little-endian.
  592. Used for fixed32, sfixed32, float) */
  593. static inline size_t
  594. fixed32_pack (uint32_t value, void *out)
  595. {
  596. #if IS_LITTLE_ENDIAN
  597. memcpy (out, &value, 4);
  598. #else
  599. uint8_t *buf = out;
  600. buf[0] = value;
  601. buf[1] = value>>8;
  602. buf[2] = value>>16;
  603. buf[3] = value>>24;
  604. #endif
  605. return 4;
  606. }
  607. /* Pack a 64-bit fixed-length value.
  608. (Used for fixed64, sfixed64, double) */
  609. /* XXX: the big-endian impl is really only good for 32-bit machines,
  610. a 64-bit version would be appreciated, plus a way
  611. to decide to use 64-bit math where convenient. */
  612. static inline size_t
  613. fixed64_pack (uint64_t value, void *out)
  614. {
  615. #if IS_LITTLE_ENDIAN
  616. memcpy (out, &value, 8);
  617. #else
  618. fixed32_pack (value, out);
  619. fixed32_pack (value>>32, ((char*)out)+4);
  620. #endif
  621. return 8;
  622. }
  623. /* Pack a boolean as 0 or 1, even though the protobuf_c_boolean
  624. can really assume any integer value. */
  625. /* XXX: perhaps on some platforms "*out = !!value" would be
  626. a better impl, b/c that is idiotmatic c++ in some stl impls. */
  627. static inline size_t
  628. boolean_pack (protobuf_c_boolean value, uint8_t *out)
  629. {
  630. *out = value ? 1 : 0;
  631. return 1;
  632. }
  633. /* Pack a length-prefixed string.
  634. The input string is NUL-terminated.
  635. The NULL pointer is treated as an empty string.
  636. This isn't really necessary, but it allows people
  637. to leave required strings blank.
  638. (See Issue 13 in the bug tracker for a
  639. little more explanation).
  640. */
  641. static inline size_t
  642. string_pack (const char * str, uint8_t *out)
  643. {
  644. if (str == NULL)
  645. {
  646. out[0] = 0;
  647. return 1;
  648. }
  649. else
  650. {
  651. size_t len = strlen (str);
  652. size_t rv = uint32_pack (len, out);
  653. memcpy (out + rv, str, len);
  654. return rv + len;
  655. }
  656. }
  657. static inline size_t
  658. binary_data_pack (const ProtobufCBinaryData *bd, uint8_t *out)
  659. {
  660. size_t len = bd->len;
  661. size_t rv = uint32_pack (len, out);
  662. memcpy (out + rv, bd->data, len);
  663. return rv + len;
  664. }
  665. static inline size_t
  666. prefixed_message_pack (const ProtobufCMessage *message, uint8_t *out)
  667. {
  668. if (message == NULL)
  669. {
  670. out[0] = 0;
  671. return 1;
  672. }
  673. else
  674. {
  675. size_t rv = protobuf_c_message_pack (message, out + 1);
  676. uint32_t rv_packed_size = uint32_size (rv);
  677. if (rv_packed_size != 1)
  678. memmove (out + rv_packed_size, out + 1, rv);
  679. return uint32_pack (rv, out) + rv;
  680. }
  681. }
  682. /* wire-type will be added in required_field_pack() */
  683. /* XXX: just call uint64_pack on 64-bit platforms. */
  684. static size_t
  685. tag_pack (uint32_t id, uint8_t *out)
  686. {
  687. if (id < (1<<(32-3)))
  688. return uint32_pack (id<<3, out);
  689. else
  690. return uint64_pack (((uint64_t)id) << 3, out);
  691. }
  692. static size_t
  693. required_field_pack (const ProtobufCFieldDescriptor *field,
  694. const void *member,
  695. uint8_t *out)
  696. {
  697. size_t rv = tag_pack (field->id, out);
  698. switch (field->type)
  699. {
  700. case PROTOBUF_C_TYPE_SINT32:
  701. out[0] |= PROTOBUF_C_WIRE_TYPE_VARINT;
  702. return rv + sint32_pack (*(const int32_t *) member, out + rv);
  703. case PROTOBUF_C_TYPE_INT32:
  704. out[0] |= PROTOBUF_C_WIRE_TYPE_VARINT;
  705. return rv + int32_pack (*(const uint32_t *) member, out + rv);
  706. case PROTOBUF_C_TYPE_UINT32:
  707. case PROTOBUF_C_TYPE_ENUM:
  708. out[0] |= PROTOBUF_C_WIRE_TYPE_VARINT;
  709. return rv + uint32_pack (*(const uint32_t *) member, out + rv);
  710. case PROTOBUF_C_TYPE_SINT64:
  711. out[0] |= PROTOBUF_C_WIRE_TYPE_VARINT;
  712. return rv + sint64_pack (*(const int64_t *) member, out + rv);
  713. case PROTOBUF_C_TYPE_INT64:
  714. case PROTOBUF_C_TYPE_UINT64:
  715. out[0] |= PROTOBUF_C_WIRE_TYPE_VARINT;
  716. return rv + uint64_pack (*(const uint64_t *) member, out + rv);
  717. case PROTOBUF_C_TYPE_SFIXED32:
  718. case PROTOBUF_C_TYPE_FIXED32:
  719. case PROTOBUF_C_TYPE_FLOAT:
  720. out[0] |= PROTOBUF_C_WIRE_TYPE_32BIT;
  721. return rv + fixed32_pack (*(const uint32_t *) member, out + rv);
  722. case PROTOBUF_C_TYPE_SFIXED64:
  723. case PROTOBUF_C_TYPE_FIXED64:
  724. case PROTOBUF_C_TYPE_DOUBLE:
  725. out[0] |= PROTOBUF_C_WIRE_TYPE_64BIT;
  726. return rv + fixed64_pack (*(const uint64_t *) member, out + rv);
  727. case PROTOBUF_C_TYPE_BOOL:
  728. out[0] |= PROTOBUF_C_WIRE_TYPE_VARINT;
  729. return rv + boolean_pack (*(const protobuf_c_boolean *) member, out + rv);
  730. case PROTOBUF_C_TYPE_STRING:
  731. {
  732. out[0] |= PROTOBUF_C_WIRE_TYPE_LENGTH_PREFIXED;
  733. return rv + string_pack (*(char * const *) member, out + rv);
  734. }
  735. case PROTOBUF_C_TYPE_BYTES:
  736. {
  737. const ProtobufCBinaryData * bd = ((const ProtobufCBinaryData*) member);
  738. out[0] |= PROTOBUF_C_WIRE_TYPE_LENGTH_PREFIXED;
  739. return rv + binary_data_pack (bd, out + rv);
  740. }
  741. //case PROTOBUF_C_TYPE_GROUP: // NOT SUPPORTED
  742. case PROTOBUF_C_TYPE_MESSAGE:
  743. {
  744. out[0] |= PROTOBUF_C_WIRE_TYPE_LENGTH_PREFIXED;
  745. return rv + prefixed_message_pack (*(ProtobufCMessage * const *) member,
  746. out + rv);
  747. }
  748. }
  749. PROTOBUF_C_ASSERT_NOT_REACHED ();
  750. return 0;
  751. }
  752. static size_t
  753. optional_field_pack (const ProtobufCFieldDescriptor *field,
  754. const protobuf_c_boolean *has,
  755. const void *member,
  756. uint8_t *out)
  757. {
  758. if (field->type == PROTOBUF_C_TYPE_MESSAGE
  759. || field->type == PROTOBUF_C_TYPE_STRING)
  760. {
  761. const void *ptr = * (const void * const *) member;
  762. if (ptr == NULL
  763. || ptr == field->default_value)
  764. return 0;
  765. }
  766. else
  767. {
  768. if (!*has)
  769. return 0;
  770. }
  771. return required_field_pack (field, member, out);
  772. }
  773. /* TODO: implement as a table lookup */
  774. static inline size_t
  775. sizeof_elt_in_repeated_array (ProtobufCType type)
  776. {
  777. switch (type)
  778. {
  779. case PROTOBUF_C_TYPE_SINT32:
  780. case PROTOBUF_C_TYPE_INT32:
  781. case PROTOBUF_C_TYPE_UINT32:
  782. case PROTOBUF_C_TYPE_SFIXED32:
  783. case PROTOBUF_C_TYPE_FIXED32:
  784. case PROTOBUF_C_TYPE_FLOAT:
  785. case PROTOBUF_C_TYPE_ENUM:
  786. return 4;
  787. case PROTOBUF_C_TYPE_SINT64:
  788. case PROTOBUF_C_TYPE_INT64:
  789. case PROTOBUF_C_TYPE_UINT64:
  790. case PROTOBUF_C_TYPE_SFIXED64:
  791. case PROTOBUF_C_TYPE_FIXED64:
  792. case PROTOBUF_C_TYPE_DOUBLE:
  793. return 8;
  794. case PROTOBUF_C_TYPE_BOOL:
  795. return sizeof (protobuf_c_boolean);
  796. case PROTOBUF_C_TYPE_STRING:
  797. case PROTOBUF_C_TYPE_MESSAGE:
  798. return sizeof (void *);
  799. case PROTOBUF_C_TYPE_BYTES:
  800. return sizeof (ProtobufCBinaryData);
  801. }
  802. PROTOBUF_C_ASSERT_NOT_REACHED ();
  803. return 0;
  804. }
  805. static void
  806. copy_to_little_endian_32 (void *out, const void *in, unsigned N)
  807. {
  808. #if IS_LITTLE_ENDIAN
  809. memcpy (out, in, N * 4);
  810. #else
  811. unsigned i;
  812. const uint32_t *ini = in;
  813. for (i = 0; i < N; i++)
  814. fixed32_pack (ini[i], (uint32_t*)out + i);
  815. #endif
  816. }
  817. static void
  818. copy_to_little_endian_64 (void *out, const void *in, unsigned N)
  819. {
  820. #if IS_LITTLE_ENDIAN
  821. memcpy (out, in, N * 8);
  822. #else
  823. unsigned i;
  824. const uint64_t *ini = in;
  825. for (i = 0; i < N; i++)
  826. fixed64_pack (ini[i], (uint64_t*)out + i);
  827. #endif
  828. }
  829. static unsigned
  830. get_type_min_size (ProtobufCType type)
  831. {
  832. if (type == PROTOBUF_C_TYPE_SFIXED32
  833. || type == PROTOBUF_C_TYPE_FIXED32
  834. || type == PROTOBUF_C_TYPE_FLOAT)
  835. return 4;
  836. if (type == PROTOBUF_C_TYPE_SFIXED64
  837. || type == PROTOBUF_C_TYPE_FIXED64
  838. || type == PROTOBUF_C_TYPE_DOUBLE)
  839. return 8;
  840. return 1;
  841. }
  842. static size_t
  843. repeated_field_pack (const ProtobufCFieldDescriptor *field,
  844. size_t count,
  845. const void *member,
  846. uint8_t *out)
  847. {
  848. char *array = * (char * const *) member;
  849. unsigned i;
  850. if (field->packed)
  851. {
  852. unsigned header_len;
  853. unsigned len_start;
  854. unsigned min_length;
  855. unsigned payload_len;
  856. unsigned length_size_min;
  857. unsigned actual_length_size;
  858. uint8_t *payload_at;
  859. if (count == 0)
  860. return 0;
  861. header_len = tag_pack (field->id, out);
  862. out[0] |= PROTOBUF_C_WIRE_TYPE_LENGTH_PREFIXED;
  863. len_start = header_len;
  864. min_length = get_type_min_size (field->type) * count;
  865. length_size_min = uint32_size (min_length);
  866. header_len += length_size_min;
  867. payload_at = out + header_len;
  868. switch (field->type)
  869. {
  870. case PROTOBUF_C_TYPE_SFIXED32:
  871. case PROTOBUF_C_TYPE_FIXED32:
  872. case PROTOBUF_C_TYPE_FLOAT:
  873. copy_to_little_endian_32 (payload_at, array, count);
  874. payload_at += count * 4;
  875. break;
  876. case PROTOBUF_C_TYPE_SFIXED64:
  877. case PROTOBUF_C_TYPE_FIXED64:
  878. case PROTOBUF_C_TYPE_DOUBLE:
  879. copy_to_little_endian_64 (payload_at, array, count);
  880. payload_at += count * 8;
  881. break;
  882. case PROTOBUF_C_TYPE_INT32:
  883. {
  884. const int32_t *arr = (const int32_t *) array;
  885. for (i = 0; i < count; i++)
  886. payload_at += int32_pack (arr[i], payload_at);
  887. }
  888. break;
  889. case PROTOBUF_C_TYPE_SINT32:
  890. {
  891. const int32_t *arr = (const int32_t *) array;
  892. for (i = 0; i < count; i++)
  893. payload_at += sint32_pack (arr[i], payload_at);
  894. }
  895. break;
  896. case PROTOBUF_C_TYPE_SINT64:
  897. {
  898. const int64_t *arr = (const int64_t *) array;
  899. for (i = 0; i < count; i++)
  900. payload_at += sint64_pack (arr[i], payload_at);
  901. }
  902. break;
  903. case PROTOBUF_C_TYPE_ENUM:
  904. case PROTOBUF_C_TYPE_UINT32:
  905. {
  906. const uint32_t *arr = (const uint32_t *) array;
  907. for (i = 0; i < count; i++)
  908. payload_at += uint32_pack (arr[i], payload_at);
  909. }
  910. break;
  911. case PROTOBUF_C_TYPE_INT64:
  912. case PROTOBUF_C_TYPE_UINT64:
  913. {
  914. const uint64_t *arr = (const uint64_t *) array;
  915. for (i = 0; i < count; i++)
  916. payload_at += uint64_pack (arr[i], payload_at);
  917. }
  918. break;
  919. case PROTOBUF_C_TYPE_BOOL:
  920. {
  921. const protobuf_c_boolean *arr = (const protobuf_c_boolean *) array;
  922. for (i = 0; i < count; i++)
  923. payload_at += boolean_pack (arr[i], payload_at);
  924. }
  925. break;
  926. default:
  927. assert (0);
  928. }
  929. payload_len = payload_at - (out + header_len);
  930. actual_length_size = uint32_size (payload_len);
  931. if (length_size_min != actual_length_size)
  932. {
  933. assert (actual_length_size == length_size_min + 1);
  934. memmove (out + header_len + 1, out + header_len, payload_len);
  935. header_len++;
  936. }
  937. uint32_pack (payload_len, out + len_start);
  938. return header_len + payload_len;
  939. }
  940. else
  941. {
  942. /* CONSIDER: optimize this case a bit (by putting the loop inside the switch) */
  943. size_t rv = 0;
  944. unsigned siz = sizeof_elt_in_repeated_array (field->type);
  945. for (i = 0; i < count; i++)
  946. {
  947. rv += required_field_pack (field, array, out + rv);
  948. array += siz;
  949. }
  950. return rv;
  951. }
  952. }
  953. static size_t
  954. unknown_field_pack (const ProtobufCMessageUnknownField *field,
  955. uint8_t *out)
  956. {
  957. size_t rv = tag_pack (field->tag, out);
  958. out[0] |= field->wire_type;
  959. memcpy (out + rv, field->data, field->len);
  960. return rv + field->len;
  961. }
  962. size_t
  963. protobuf_c_message_pack (const ProtobufCMessage *message,
  964. uint8_t *out)
  965. {
  966. unsigned i;
  967. size_t rv = 0;
  968. ASSERT_IS_MESSAGE (message);
  969. for (i = 0; i < message->descriptor->n_fields; i++)
  970. {
  971. const ProtobufCFieldDescriptor *field = message->descriptor->fields + i;
  972. const void *member = ((const char *) message) + field->offset;
  973. /* it doesn't hurt to compute qmember (a pointer to the quantifier
  974. field of the structure), but the pointer is only valid if
  975. the field is one of:
  976. - a repeated field
  977. - an optional field that isn't a pointer type
  978. (meaning: not a message or a string) */
  979. const void *qmember = ((const char *) message) + field->quantifier_offset;
  980. if (field->label == PROTOBUF_C_LABEL_REQUIRED)
  981. rv += required_field_pack (field, member, out + rv);
  982. else if (field->label == PROTOBUF_C_LABEL_OPTIONAL)
  983. /* note that qmember is bogus for strings and messages,
  984. but it isn't used */
  985. rv += optional_field_pack (field, qmember, member, out + rv);
  986. else
  987. rv += repeated_field_pack (field, * (const size_t *) qmember, member, out + rv);
  988. }
  989. for (i = 0; i < message->n_unknown_fields; i++)
  990. rv += unknown_field_pack (&message->unknown_fields[i], out + rv);
  991. return rv;
  992. }
  993. /* === pack_to_buffer() === */
  994. static size_t
  995. required_field_pack_to_buffer (const ProtobufCFieldDescriptor *field,
  996. const void *member,
  997. ProtobufCBuffer *buffer)
  998. {
  999. size_t rv;
  1000. uint8_t scratch[MAX_UINT64_ENCODED_SIZE * 2];
  1001. rv = tag_pack (field->id, scratch);
  1002. switch (field->type)
  1003. {
  1004. case PROTOBUF_C_TYPE_SINT32:
  1005. scratch[0] |= PROTOBUF_C_WIRE_TYPE_VARINT;
  1006. rv += sint32_pack (*(const int32_t *) member, scratch + rv);
  1007. buffer->append (buffer, rv, scratch);
  1008. break;
  1009. case PROTOBUF_C_TYPE_INT32:
  1010. scratch[0] |= PROTOBUF_C_WIRE_TYPE_VARINT;
  1011. rv += int32_pack (*(const uint32_t *) member, scratch + rv);
  1012. buffer->append (buffer, rv, scratch);
  1013. break;
  1014. case PROTOBUF_C_TYPE_UINT32:
  1015. case PROTOBUF_C_TYPE_ENUM:
  1016. scratch[0] |= PROTOBUF_C_WIRE_TYPE_VARINT;
  1017. rv += uint32_pack (*(const uint32_t *) member, scratch + rv);
  1018. buffer->append (buffer, rv, scratch);
  1019. break;
  1020. case PROTOBUF_C_TYPE_SINT64:
  1021. scratch[0] |= PROTOBUF_C_WIRE_TYPE_VARINT;
  1022. rv += sint64_pack (*(const int64_t *) member, scratch + rv);
  1023. buffer->append (buffer, rv, scratch);
  1024. break;
  1025. case PROTOBUF_C_TYPE_INT64:
  1026. case PROTOBUF_C_TYPE_UINT64:
  1027. scratch[0] |= PROTOBUF_C_WIRE_TYPE_VARINT;
  1028. rv += uint64_pack (*(const uint64_t *) member, scratch + rv);
  1029. buffer->append (buffer, rv, scratch);
  1030. break;
  1031. case PROTOBUF_C_TYPE_SFIXED32:
  1032. case PROTOBUF_C_TYPE_FIXED32:
  1033. case PROTOBUF_C_TYPE_FLOAT:
  1034. scratch[0] |= PROTOBUF_C_WIRE_TYPE_32BIT;
  1035. rv += fixed32_pack (*(const uint32_t *) member, scratch + rv);
  1036. buffer->append (buffer, rv, scratch);
  1037. break;
  1038. case PROTOBUF_C_TYPE_SFIXED64:
  1039. case PROTOBUF_C_TYPE_FIXED64:
  1040. case PROTOBUF_C_TYPE_DOUBLE:
  1041. scratch[0] |= PROTOBUF_C_WIRE_TYPE_64BIT;
  1042. rv += fixed64_pack (*(const uint64_t *) member, scratch + rv);
  1043. buffer->append (buffer, rv, scratch);
  1044. break;
  1045. case PROTOBUF_C_TYPE_BOOL:
  1046. scratch[0] |= PROTOBUF_C_WIRE_TYPE_VARINT;
  1047. rv += boolean_pack (*(const protobuf_c_boolean *) member, scratch + rv);
  1048. buffer->append (buffer, rv, scratch);
  1049. break;
  1050. case PROTOBUF_C_TYPE_STRING:
  1051. {
  1052. const char *str = *(char * const *) member;
  1053. size_t sublen = str ? strlen (str) : 0;
  1054. scratch[0] |= PROTOBUF_C_WIRE_TYPE_LENGTH_PREFIXED;
  1055. rv += uint32_pack (sublen, scratch + rv);
  1056. buffer->append (buffer, rv, scratch);
  1057. buffer->append (buffer, sublen, (const uint8_t *) str);
  1058. rv += sublen;
  1059. break;
  1060. }
  1061. case PROTOBUF_C_TYPE_BYTES:
  1062. {
  1063. const ProtobufCBinaryData * bd = ((const ProtobufCBinaryData*) member);
  1064. size_t sublen = bd->len;
  1065. scratch[0] |= PROTOBUF_C_WIRE_TYPE_LENGTH_PREFIXED;
  1066. rv += uint32_pack (sublen, scratch + rv);
  1067. buffer->append (buffer, rv, scratch);
  1068. buffer->append (buffer, sublen, bd->data);
  1069. rv += sublen;
  1070. break;
  1071. }
  1072. //PROTOBUF_C_TYPE_GROUP, // NOT SUPPORTED
  1073. case PROTOBUF_C_TYPE_MESSAGE:
  1074. {
  1075. uint8_t simple_buffer_scratch[256];
  1076. size_t sublen;
  1077. ProtobufCBufferSimple simple_buffer
  1078. = PROTOBUF_C_BUFFER_SIMPLE_INIT (simple_buffer_scratch);
  1079. const ProtobufCMessage *msg = *(ProtobufCMessage * const *) member;
  1080. scratch[0] |= PROTOBUF_C_WIRE_TYPE_LENGTH_PREFIXED;
  1081. if (msg == NULL)
  1082. sublen = 0;
  1083. else
  1084. sublen = protobuf_c_message_pack_to_buffer (msg, &simple_buffer.base);
  1085. rv += uint32_pack (sublen, scratch + rv);
  1086. buffer->append (buffer, rv, scratch);
  1087. buffer->append (buffer, sublen, simple_buffer.data);
  1088. rv += sublen;
  1089. PROTOBUF_C_BUFFER_SIMPLE_CLEAR (&simple_buffer);
  1090. break;
  1091. }
  1092. default:
  1093. PROTOBUF_C_ASSERT_NOT_REACHED ();
  1094. }
  1095. return rv;
  1096. }
  1097. static size_t
  1098. optional_field_pack_to_buffer (const ProtobufCFieldDescriptor *field,
  1099. const protobuf_c_boolean *has,
  1100. const void *member,
  1101. ProtobufCBuffer *buffer)
  1102. {
  1103. if (field->type == PROTOBUF_C_TYPE_MESSAGE
  1104. || field->type == PROTOBUF_C_TYPE_STRING)
  1105. {
  1106. const void *ptr = * (const void * const *) member;
  1107. if (ptr == NULL
  1108. || ptr == field->default_value)
  1109. return 0;
  1110. }
  1111. else
  1112. {
  1113. if (!*has)
  1114. return 0;
  1115. }
  1116. return required_field_pack_to_buffer (field, member, buffer);
  1117. }
  1118. static size_t
  1119. get_packed_payload_length (const ProtobufCFieldDescriptor *field,
  1120. unsigned count,
  1121. const void *array)
  1122. {
  1123. unsigned rv = 0;
  1124. unsigned i;
  1125. switch (field->type)
  1126. {
  1127. case PROTOBUF_C_TYPE_SFIXED32:
  1128. case PROTOBUF_C_TYPE_FIXED32:
  1129. case PROTOBUF_C_TYPE_FLOAT:
  1130. return count * 4;
  1131. case PROTOBUF_C_TYPE_SFIXED64:
  1132. case PROTOBUF_C_TYPE_FIXED64:
  1133. case PROTOBUF_C_TYPE_DOUBLE:
  1134. return count * 8;
  1135. case PROTOBUF_C_TYPE_INT32:
  1136. {
  1137. const int32_t *arr = (const int32_t *) array;
  1138. for (i = 0; i < count; i++)
  1139. rv += int32_size (arr[i]);
  1140. }
  1141. break;
  1142. case PROTOBUF_C_TYPE_SINT32:
  1143. {
  1144. const int32_t *arr = (const int32_t *) array;
  1145. for (i = 0; i < count; i++)
  1146. rv += sint32_size (arr[i]);
  1147. }
  1148. break;
  1149. case PROTOBUF_C_TYPE_ENUM:
  1150. case PROTOBUF_C_TYPE_UINT32:
  1151. {
  1152. const uint32_t *arr = (const uint32_t *) array;
  1153. for (i = 0; i < count; i++)
  1154. rv += uint32_size (arr[i]);
  1155. }
  1156. break;
  1157. case PROTOBUF_C_TYPE_SINT64:
  1158. {
  1159. const int64_t *arr = (const int64_t *) array;
  1160. for (i = 0; i < count; i++)
  1161. rv += sint64_size (arr[i]);
  1162. }
  1163. break;
  1164. case PROTOBUF_C_TYPE_INT64:
  1165. case PROTOBUF_C_TYPE_UINT64:
  1166. {
  1167. const uint64_t *arr = (const uint64_t *) array;
  1168. for (i = 0; i < count; i++)
  1169. rv += uint64_size (arr[i]);
  1170. }
  1171. break;
  1172. case PROTOBUF_C_TYPE_BOOL:
  1173. return count;
  1174. default:
  1175. assert (0);
  1176. }
  1177. return rv;
  1178. }
  1179. static size_t
  1180. pack_buffer_packed_payload (const ProtobufCFieldDescriptor *field,
  1181. unsigned count,
  1182. const void *array,
  1183. ProtobufCBuffer *buffer)
  1184. {
  1185. uint8_t scratch[16];
  1186. size_t rv = 0;
  1187. unsigned i;
  1188. switch (field->type)
  1189. {
  1190. case PROTOBUF_C_TYPE_SFIXED32:
  1191. case PROTOBUF_C_TYPE_FIXED32:
  1192. case PROTOBUF_C_TYPE_FLOAT:
  1193. #if IS_LITTLE_ENDIAN
  1194. rv = count * 4;
  1195. goto no_packing_needed;
  1196. #else
  1197. for (i = 0; i < count; i++)
  1198. {
  1199. unsigned len = fixed32_pack (((uint32_t*)array)[i], scratch);
  1200. buffer->append (buffer, len, scratch);
  1201. rv += len;
  1202. }
  1203. #endif
  1204. break;
  1205. case PROTOBUF_C_TYPE_SFIXED64:
  1206. case PROTOBUF_C_TYPE_FIXED64:
  1207. case PROTOBUF_C_TYPE_DOUBLE:
  1208. #if IS_LITTLE_ENDIAN
  1209. rv = count * 8;
  1210. goto no_packing_needed;
  1211. #else
  1212. for (i = 0; i < count; i++)
  1213. {
  1214. unsigned len = fixed64_pack (((uint64_t*)array)[i], scratch);
  1215. buffer->append (buffer, len, scratch);
  1216. rv += len;
  1217. }
  1218. break;
  1219. #endif
  1220. case PROTOBUF_C_TYPE_INT32:
  1221. for (i = 0; i < count; i++)
  1222. {
  1223. unsigned len = int32_pack (((int32_t*)array)[i], scratch);
  1224. buffer->append (buffer, len, scratch);
  1225. rv += len;
  1226. }
  1227. break;
  1228. case PROTOBUF_C_TYPE_SINT32:
  1229. for (i = 0; i < count; i++)
  1230. {
  1231. unsigned len = sint32_pack (((int32_t*)array)[i], scratch);
  1232. buffer->append (buffer, len, scratch);
  1233. rv += len;
  1234. }
  1235. break;
  1236. case PROTOBUF_C_TYPE_ENUM:
  1237. case PROTOBUF_C_TYPE_UINT32:
  1238. for (i = 0; i < count; i++)
  1239. {
  1240. unsigned len = uint32_pack (((uint32_t*)array)[i], scratch);
  1241. buffer->append (buffer, len, scratch);
  1242. rv += len;
  1243. }
  1244. break;
  1245. case PROTOBUF_C_TYPE_SINT64:
  1246. for (i = 0; i < count; i++)
  1247. {
  1248. unsigned len = sint64_pack (((int64_t*)array)[i], scratch);
  1249. buffer->append (buffer, len, scratch);
  1250. rv += len;
  1251. }
  1252. break;
  1253. case PROTOBUF_C_TYPE_INT64:
  1254. case PROTOBUF_C_TYPE_UINT64:
  1255. for (i = 0; i < count; i++)
  1256. {
  1257. unsigned len = uint64_pack (((uint64_t*)array)[i], scratch);
  1258. buffer->append (buffer, len, scratch);
  1259. rv += len;
  1260. }
  1261. break;
  1262. case PROTOBUF_C_TYPE_BOOL:
  1263. for (i = 0; i < count; i++)
  1264. {
  1265. unsigned len = boolean_pack (((protobuf_c_boolean*)array)[i], scratch);
  1266. buffer->append (buffer, len, scratch);
  1267. rv += len;
  1268. }
  1269. return count;
  1270. default:
  1271. assert(0);
  1272. }
  1273. return rv;
  1274. no_packing_needed:
  1275. buffer->append (buffer, rv, array);
  1276. return rv;
  1277. }
  1278. static size_t
  1279. repeated_field_pack_to_buffer (const ProtobufCFieldDescriptor *field,
  1280. unsigned count,
  1281. const void *member,
  1282. ProtobufCBuffer *buffer)
  1283. {
  1284. char *array = * (char * const *) member;
  1285. if (count == 0)
  1286. return 0;
  1287. if (field->packed)
  1288. {
  1289. uint8_t scratch[MAX_UINT64_ENCODED_SIZE * 2];
  1290. size_t rv = tag_pack (field->id, scratch);
  1291. size_t payload_len = get_packed_payload_length (field, count, array);
  1292. size_t tmp;
  1293. scratch[0] |= PROTOBUF_C_WIRE_TYPE_LENGTH_PREFIXED;
  1294. rv += uint32_pack (payload_len, scratch + rv);
  1295. buffer->append (buffer, rv, scratch);
  1296. tmp = pack_buffer_packed_payload (field, count, array, buffer);
  1297. assert (tmp == payload_len);
  1298. return rv + payload_len;
  1299. }
  1300. else
  1301. {
  1302. size_t siz;
  1303. unsigned i;
  1304. /* CONSIDER: optimize this case a bit (by putting the loop inside the switch) */
  1305. unsigned rv = 0;
  1306. siz = sizeof_elt_in_repeated_array (field->type);
  1307. for (i = 0; i < count; i++)
  1308. {
  1309. rv += required_field_pack_to_buffer (field, array, buffer);
  1310. array += siz;
  1311. }
  1312. return rv;
  1313. }
  1314. }
  1315. static size_t
  1316. unknown_field_pack_to_buffer (const ProtobufCMessageUnknownField *field,
  1317. ProtobufCBuffer *buffer)
  1318. {
  1319. uint8_t header[MAX_UINT64_ENCODED_SIZE];
  1320. size_t rv = tag_pack (field->tag, header);
  1321. header[0] |= field->wire_type;
  1322. buffer->append (buffer, rv, header);
  1323. buffer->append (buffer, field->len, field->data);
  1324. return rv + field->len;
  1325. }
  1326. size_t
  1327. protobuf_c_message_pack_to_buffer (const ProtobufCMessage *message,
  1328. ProtobufCBuffer *buffer)
  1329. {
  1330. unsigned i;
  1331. size_t rv = 0;
  1332. ASSERT_IS_MESSAGE (message);
  1333. for (i = 0; i < message->descriptor->n_fields; i++)
  1334. {
  1335. const ProtobufCFieldDescriptor *field = message->descriptor->fields + i;
  1336. const void *member = ((const char *) message) + field->offset;
  1337. const void *qmember = ((const char *) message) + field->quantifier_offset;
  1338. if (field->label == PROTOBUF_C_LABEL_REQUIRED)
  1339. rv += required_field_pack_to_buffer (field, member, buffer);
  1340. else if (field->label == PROTOBUF_C_LABEL_OPTIONAL)
  1341. rv += optional_field_pack_to_buffer (field, qmember, member, buffer);
  1342. else
  1343. rv += repeated_field_pack_to_buffer (field, * (const size_t *) qmember, member, buffer);
  1344. }
  1345. for (i = 0; i < message->n_unknown_fields; i++)
  1346. rv += unknown_field_pack_to_buffer (&message->unknown_fields[i], buffer);
  1347. return rv;
  1348. }
  1349. /* === unpacking === */
  1350. #if PRINT_UNPACK_ERRORS
  1351. # define UNPACK_ERROR(args) do { printf args;printf("\n"); }while(0)
  1352. #else
  1353. # define UNPACK_ERROR(args) do { } while (0)
  1354. #endif
  1355. static inline int
  1356. int_range_lookup (unsigned n_ranges,
  1357. const ProtobufCIntRange *ranges,
  1358. int value)
  1359. {
  1360. unsigned start, n;
  1361. if (n_ranges == 0)
  1362. return -1;
  1363. start = 0;
  1364. n = n_ranges;
  1365. while (n > 1)
  1366. {
  1367. unsigned mid = start + n / 2;
  1368. if (value < ranges[mid].start_value)
  1369. {
  1370. n = mid - start;
  1371. }
  1372. else if (value >= ranges[mid].start_value + (int)(ranges[mid+1].orig_index-ranges[mid].orig_index))
  1373. {
  1374. unsigned new_start = mid + 1;
  1375. n = start + n - new_start;
  1376. start = new_start;
  1377. }
  1378. else
  1379. return (value - ranges[mid].start_value) + ranges[mid].orig_index;
  1380. }
  1381. if (n > 0)
  1382. {
  1383. unsigned start_orig_index = ranges[start].orig_index;
  1384. unsigned range_size = ranges[start+1].orig_index - start_orig_index;
  1385. if (ranges[start].start_value <= value
  1386. && value < (int)(ranges[start].start_value + range_size))
  1387. return (value - ranges[start].start_value) + start_orig_index;
  1388. }
  1389. return -1;
  1390. }
  1391. static size_t
  1392. parse_tag_and_wiretype (size_t len,
  1393. const uint8_t *data,
  1394. uint32_t *tag_out,
  1395. ProtobufCWireType *wiretype_out)
  1396. {
  1397. unsigned max_rv = len > 5 ? 5 : len;
  1398. uint32_t tag = (data[0]&0x7f) >> 3;
  1399. unsigned shift = 4;
  1400. unsigned rv;
  1401. *wiretype_out = data[0] & 7;
  1402. if ((data[0] & 0x80) == 0)
  1403. {
  1404. *tag_out = tag;
  1405. return 1;
  1406. }
  1407. for (rv = 1; rv < max_rv; rv++)
  1408. if (data[rv] & 0x80)
  1409. {
  1410. tag |= (data[rv] & 0x7f) << shift;
  1411. shift += 7;
  1412. }
  1413. else
  1414. {
  1415. tag |= data[rv] << shift;
  1416. *tag_out = tag;
  1417. return rv + 1;
  1418. }
  1419. return 0; /* error: bad header */
  1420. }
  1421. /* sizeof(ScannedMember) must be <= (1<<BOUND_SIZEOF_SCANNED_MEMBER_LOG2) */
  1422. #define BOUND_SIZEOF_SCANNED_MEMBER_LOG2 5
  1423. typedef struct _ScannedMember ScannedMember;
  1424. struct _ScannedMember
  1425. {
  1426. uint32_t tag;
  1427. uint8_t wire_type;
  1428. uint8_t length_prefix_len;
  1429. const ProtobufCFieldDescriptor *field;
  1430. size_t len;
  1431. const uint8_t *data;
  1432. };
  1433. static inline uint32_t
  1434. scan_length_prefixed_data (size_t len, const uint8_t *data, size_t *prefix_len_out)
  1435. {
  1436. unsigned hdr_max = len < 5 ? len : 5;
  1437. unsigned hdr_len;
  1438. uint32_t val = 0;
  1439. unsigned i;
  1440. unsigned shift = 0;
  1441. for (i = 0; i < hdr_max; i++)
  1442. {
  1443. val |= (data[i] & 0x7f) << shift;
  1444. shift += 7;
  1445. if ((data[i] & 0x80) == 0)
  1446. break;
  1447. }
  1448. if (i == hdr_max)
  1449. {
  1450. UNPACK_ERROR (("error parsing length for length-prefixed data"));
  1451. return 0;
  1452. }
  1453. hdr_len = i + 1;
  1454. *prefix_len_out = hdr_len;
  1455. if (hdr_len + val > len)
  1456. {
  1457. UNPACK_ERROR (("data too short after length-prefix of %u",
  1458. val));
  1459. return 0;
  1460. }
  1461. return hdr_len + val;
  1462. }
  1463. static size_t
  1464. max_b128_numbers (size_t len, const uint8_t *data)
  1465. {
  1466. size_t rv = 0;
  1467. while (len--)
  1468. if ((*data++ & 0x80) == 0)
  1469. ++rv;
  1470. return rv;
  1471. }
  1472. /* Given a raw slab of packed-repeated values,
  1473. determine the number of elements.
  1474. This function detects certain kinds of errors
  1475. but not others; the remaining error checking is done by
  1476. parse_packed_repeated_member() */
  1477. static protobuf_c_boolean
  1478. count_packed_elements (ProtobufCType type,
  1479. size_t len,
  1480. const uint8_t *data,
  1481. size_t *count_out)
  1482. {
  1483. switch (type)
  1484. {
  1485. case PROTOBUF_C_TYPE_SFIXED32:
  1486. case PROTOBUF_C_TYPE_FIXED32:
  1487. case PROTOBUF_C_TYPE_FLOAT:
  1488. if (len % 4 != 0)
  1489. {
  1490. UNPACK_ERROR (("length must be a multiple of 4 for fixed-length 32-bit types"));
  1491. return FALSE;
  1492. }
  1493. *count_out = len / 4;
  1494. return TRUE;
  1495. case PROTOBUF_C_TYPE_SFIXED64:
  1496. case PROTOBUF_C_TYPE_FIXED64:
  1497. case PROTOBUF_C_TYPE_DOUBLE:
  1498. if (len % 8 != 0)
  1499. {
  1500. UNPACK_ERROR (("length must be a multiple of 8 for fixed-length 64-bit types"));
  1501. return FALSE;
  1502. }
  1503. *count_out = len / 8;
  1504. return TRUE;
  1505. case PROTOBUF_C_TYPE_INT32:
  1506. case PROTOBUF_C_TYPE_SINT32:
  1507. case PROTOBUF_C_TYPE_ENUM:
  1508. case PROTOBUF_C_TYPE_UINT32:
  1509. case PROTOBUF_C_TYPE_INT64:
  1510. case PROTOBUF_C_TYPE_SINT64:
  1511. case PROTOBUF_C_TYPE_UINT64:
  1512. *count_out = max_b128_numbers (len, data);
  1513. return TRUE;
  1514. case PROTOBUF_C_TYPE_BOOL:
  1515. *count_out = len;
  1516. return TRUE;
  1517. case PROTOBUF_C_TYPE_STRING:
  1518. case PROTOBUF_C_TYPE_BYTES:
  1519. case PROTOBUF_C_TYPE_MESSAGE:
  1520. default:
  1521. UNPACK_ERROR (("bad protobuf-c type %u for packed-repeated", type));
  1522. return FALSE;
  1523. }
  1524. }
  1525. static inline uint32_t
  1526. parse_uint32 (unsigned len, const uint8_t *data)
  1527. {
  1528. unsigned rv = data[0] & 0x7f;
  1529. if (len > 1)
  1530. {
  1531. rv |= ((data[1] & 0x7f) << 7);
  1532. if (len > 2)
  1533. {
  1534. rv |= ((data[2] & 0x7f) << 14);
  1535. if (len > 3)
  1536. {
  1537. rv |= ((data[3] & 0x7f) << 21);
  1538. if (len > 4)
  1539. rv |= (data[4] << 28);
  1540. }
  1541. }
  1542. }
  1543. return rv;
  1544. }
  1545. static inline uint32_t
  1546. parse_int32 (unsigned len, const uint8_t *data)
  1547. {
  1548. return parse_uint32 (len, data);
  1549. }
  1550. static inline int32_t
  1551. unzigzag32 (uint32_t v)
  1552. {
  1553. if (v&1)
  1554. return -(v>>1) - 1;
  1555. else
  1556. return v>>1;
  1557. }
  1558. static inline uint32_t
  1559. parse_fixed_uint32 (const uint8_t *data)
  1560. {
  1561. #if IS_LITTLE_ENDIAN
  1562. uint32_t t;
  1563. memcpy (&t, data, 4);
  1564. return t;
  1565. #else
  1566. return data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24);
  1567. #endif
  1568. }
  1569. static uint64_t
  1570. parse_uint64 (unsigned len, const uint8_t *data)
  1571. {
  1572. unsigned shift, i;
  1573. uint64_t rv;
  1574. if (len < 5)
  1575. return parse_uint32 (len, data);
  1576. rv = ((data[0] & 0x7f))
  1577. | ((data[1] & 0x7f)<<7)
  1578. | ((data[2] & 0x7f)<<14)
  1579. | ((data[3] & 0x7f)<<21);
  1580. shift = 28;
  1581. for (i = 4; i < len; i++)
  1582. {
  1583. rv |= (((uint64_t)(data[i]&0x7f)) << shift);
  1584. shift += 7;
  1585. }
  1586. return rv;
  1587. }
  1588. static inline int64_t
  1589. unzigzag64 (uint64_t v)
  1590. {
  1591. if (v&1)
  1592. return -(v>>1) - 1;
  1593. else
  1594. return v>>1;
  1595. }
  1596. static inline uint64_t
  1597. par

Large files files are truncated, but you can click here to view the full file