PageRenderTime 66ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 1ms

/client/c/src/protobuf-c.c

https://github.com/sapo/sapo-broker
C | 2619 lines | 2297 code | 139 blank | 183 comment | 451 complexity | 79b8b5f62ad3ec3f4fa1d445bfbec88d MD5 | raw file
Possible License(s): GPL-3.0, BSD-3-Clause, CC-BY-SA-3.0, Apache-2.0

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

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