PageRenderTime 42ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/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
  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*)member = parse_uint64 (len, data);
  1624. return 1;
  1625. case PROTOBUF_C_TYPE_SINT64:
  1626. if (wire_type != PROTOBUF_C_WIRE_TYPE_VARINT)
  1627. return 0;
  1628. *(int64_t*)member = unzigzag64 (parse_uint64 (len, data));
  1629. return 1;
  1630. case PROTOBUF_C_TYPE_SFIXED64:
  1631. case PROTOBUF_C_TYPE_FIXED64:
  1632. case PROTOBUF_C_TYPE_DOUBLE:
  1633. if (wire_type != PROTOBUF_C_WIRE_TYPE_64BIT)
  1634. return 0;
  1635. *(uint64_t*)member = parse_fixed_uint64 (data);
  1636. return 1;
  1637. case PROTOBUF_C_TYPE_BOOL:
  1638. *(protobuf_c_boolean*)member = parse_boolean (len, data);
  1639. return 1;
  1640. case PROTOBUF_C_TYPE_ENUM:
  1641. if (wire_type != PROTOBUF_C_WIRE_TYPE_VARINT)
  1642. return 0;
  1643. *(uint32_t*)member = parse_uint32 (len, data);
  1644. return 1;
  1645. case PROTOBUF_C_TYPE_STRING:
  1646. if (wire_type != PROTOBUF_C_WIRE_TYPE_LENGTH_PREFIXED)
  1647. return 0;
  1648. {
  1649. char **pstr = member;
  1650. unsigned pref_len = scanned_member->length_prefix_len;
  1651. if (maybe_clear && *pstr != NULL)
  1652. {
  1653. const char *def = scanned_member->field->default_value;
  1654. if (*pstr != NULL && *pstr != def)
  1655. FREE (allocator, *pstr);
  1656. }
  1657. DO_ALLOC (*pstr, allocator, len - pref_len + 1, return 0);
  1658. memcpy (*pstr, data + pref_len, len - pref_len);
  1659. (*pstr)[len-pref_len] = 0;
  1660. return 1;
  1661. }
  1662. case PROTOBUF_C_TYPE_BYTES:
  1663. if (wire_type != PROTOBUF_C_WIRE_TYPE_LENGTH_PREFIXED)
  1664. return 0;
  1665. {
  1666. ProtobufCBinaryData *bd = member;
  1667. const ProtobufCBinaryData *def_bd;
  1668. unsigned pref_len = scanned_member->length_prefix_len;
  1669. def_bd = scanned_member->field->default_value;
  1670. if (maybe_clear
  1671. && bd->data != NULL
  1672. && (def_bd == NULL || bd->data != def_bd->data))
  1673. FREE (allocator, bd->data);
  1674. DO_ALLOC (bd->data, allocator, len - pref_len, return 0);
  1675. memcpy (bd->data, data + pref_len, len - pref_len);
  1676. bd->len = len - pref_len;
  1677. return 1;
  1678. }
  1679. //case PROTOBUF_C_TYPE_GROUP, // NOT SUPPORTED
  1680. case PROTOBUF_C_TYPE_MESSAGE:
  1681. if (wire_type != PROTOBUF_C_WIRE_TYPE_LENGTH_PREFIXED)
  1682. return 0;
  1683. {
  1684. ProtobufCMessage **pmessage = member;
  1685. ProtobufCMessage *subm;
  1686. const ProtobufCMessage *def_mess;
  1687. unsigned pref_len = scanned_member->length_prefix_len;
  1688. def_mess = scanned_member->field->default_value;
  1689. if (maybe_clear && *pmessage != NULL && *pmessage != def_mess)
  1690. protobuf_c_message_free_unpacked (*pmessage, allocator);
  1691. subm = protobuf_c_message_unpack (scanned_member->field->descriptor,
  1692. allocator,
  1693. len - pref_len, data + pref_len);
  1694. *pmessage = subm; /* since we freed the message we must clear the field, even if NULL */
  1695. if (subm == NULL)
  1696. return 0;
  1697. return 1;
  1698. }
  1699. }
  1700. return 0;
  1701. }
  1702. static protobuf_c_boolean
  1703. parse_optional_member (ScannedMember *scanned_member,
  1704. void *member,
  1705. ProtobufCMessage *message,
  1706. ProtobufCAllocator *allocator)
  1707. {
  1708. if (!parse_required_member (scanned_member, member, allocator, TRUE))
  1709. return 0;
  1710. if (scanned_member->field->quantifier_offset != 0)
  1711. STRUCT_MEMBER (protobuf_c_boolean,
  1712. message,
  1713. scanned_member->field->quantifier_offset) = 1;
  1714. return 1;
  1715. }
  1716. static protobuf_c_boolean
  1717. parse_repeated_member (ScannedMember *scanned_member,
  1718. void *member,
  1719. ProtobufCMessage *message,
  1720. ProtobufCAllocator *allocator)
  1721. {
  1722. const ProtobufCFieldDescriptor *field = scanned_member->field;
  1723. size_t *p_n = STRUCT_MEMBER_PTR(size_t, message, field->quantifier_offset);
  1724. size_t siz = sizeof_elt_in_repeated_array (field->type);
  1725. char *array = *(char**)member;
  1726. if (!parse_required_member (scanned_member,
  1727. array + siz * (*p_n),
  1728. allocator,
  1729. FALSE))
  1730. return 0;
  1731. *p_n += 1;
  1732. return 1;
  1733. }
  1734. static unsigned scan_varint (unsigned len, const uint8_t *data)
  1735. {
  1736. unsigned i;
  1737. if (len > 10)
  1738. len = 10;
  1739. for (i = 0; i < len; i++)
  1740. if ((data[i] & 0x80) == 0)
  1741. break;
  1742. if (i == len)
  1743. return 0;
  1744. return i + 1;
  1745. }
  1746. static protobuf_c_boolean
  1747. parse_packed_repeated_member (ScannedMember *scanned_member,
  1748. void *member,
  1749. ProtobufCMessage *message)
  1750. {
  1751. const ProtobufCFieldDescriptor *field = scanned_member->field;
  1752. size_t *p_n = STRUCT_MEMBER_PTR(size_t, message, field->quantifier_offset);
  1753. size_t siz = sizeof_elt_in_repeated_array (field->type);
  1754. char *array = *(char**)member + siz * (*p_n);
  1755. const uint8_t *at = scanned_member->data + scanned_member->length_prefix_len;
  1756. size_t rem = scanned_member->len - scanned_member->length_prefix_len;
  1757. size_t count = 0;
  1758. unsigned i;
  1759. switch (field->type)
  1760. {
  1761. case PROTOBUF_C_TYPE_SFIXED32:
  1762. case PROTOBUF_C_TYPE_FIXED32:
  1763. case PROTOBUF_C_TYPE_FLOAT:
  1764. count = (scanned_member->len - scanned_member->length_prefix_len) / 4;
  1765. #if IS_LITTLE_ENDIAN
  1766. goto no_unpacking_needed;
  1767. #else
  1768. for (i = 0; i < count; i++)
  1769. {
  1770. ((uint32_t*)array)[i] = parse_fixed_uint32 (at);
  1771. at += 4;
  1772. }
  1773. #endif
  1774. break;
  1775. case PROTOBUF_C_TYPE_SFIXED64:
  1776. case PROTOBUF_C_TYPE_FIXED64:
  1777. case PROTOBUF_C_TYPE_DOUBLE:
  1778. count = (scanned_member->len - scanned_member->length_prefix_len) / 8;
  1779. #if IS_LITTLE_ENDIAN
  1780. goto no_unpacking_needed;
  1781. #else
  1782. for (i = 0; i < count; i++)
  1783. {
  1784. ((uint64_t*)array)[i] = parse_fixed_uint64 (at);
  1785. at += 8;
  1786. }
  1787. break;
  1788. #endif
  1789. case PROTOBUF_C_TYPE_INT32:
  1790. while (rem > 0)
  1791. {
  1792. unsigned s = scan_varint (rem, at);
  1793. if (s == 0)
  1794. {
  1795. UNPACK_ERROR (("bad packed-repeated int32 value"));
  1796. return FALSE;
  1797. }
  1798. ((int32_t*)array)[count++] = parse_int32 (s, at);
  1799. at += s;
  1800. rem -= s;
  1801. }
  1802. break;
  1803. case PROTOBUF_C_TYPE_SINT32:
  1804. while (rem > 0)
  1805. {
  1806. unsigned s = scan_varint (rem, at);
  1807. if (s == 0)
  1808. {
  1809. UNPACK_ERROR (("bad packed-repeated sint32 value"));
  1810. return FALSE;
  1811. }
  1812. ((int32_t*)array)[count++] = unzigzag32 (parse_uint32 (s, at));
  1813. at += s;
  1814. rem -= s;
  1815. }
  1816. break;
  1817. case PROTOBUF_C_TYPE_ENUM:
  1818. case PROTOBUF_C_TYPE_UINT32:
  1819. while (rem > 0)
  1820. {
  1821. unsigned s = scan_varint (rem, at);
  1822. if (s == 0)
  1823. {
  1824. UNPACK_ERROR (("bad packed-repeated enum or uint32 value"));
  1825. return FALSE;
  1826. }
  1827. ((uint32_t*)array)[count++] = parse_uint32 (s, at);
  1828. at += s;
  1829. rem -= s;
  1830. }
  1831. break;
  1832. case PROTOBUF_C_TYPE_SINT64:
  1833. while (rem > 0)
  1834. {
  1835. unsigned s = scan_varint (rem, at);
  1836. if (s == 0)
  1837. {
  1838. UNPACK_ERROR (("bad packed-repeated sint64 value"));
  1839. return FALSE;
  1840. }
  1841. ((int64_t*)array)[count++] = unzigzag64 (parse_uint64 (s, at));
  1842. at += s;
  1843. rem -= s;
  1844. }
  1845. break;
  1846. case PROTOBUF_C_TYPE_INT64:
  1847. case PROTOBUF_C_TYPE_UINT64:
  1848. while (rem > 0)
  1849. {
  1850. unsigned s = scan_varint (rem, at);
  1851. if (s == 0)
  1852. {
  1853. UNPACK_ERROR (("bad packed-repeated int64/uint64 value"));
  1854. return FALSE;
  1855. }
  1856. ((int64_t*)array)[count++] = parse_uint64 (s, at);
  1857. at += s;
  1858. rem -= s;
  1859. }
  1860. break;
  1861. case PROTOBUF_C_TYPE_BOOL:
  1862. count = rem;
  1863. for (i = 0; i < count; i++)
  1864. {
  1865. if (at[i] > 1)
  1866. {
  1867. UNPACK_ERROR (("bad packed-repeated boolean value"));
  1868. return FALSE;
  1869. }
  1870. ((protobuf_c_boolean*)array)[i] = at[i];
  1871. }
  1872. break;
  1873. default:
  1874. assert(0);
  1875. }
  1876. *p_n += count;
  1877. return TRUE;
  1878. #ifdef IS_LITTLE_ENDIAN
  1879. no_unpacking_needed:
  1880. #endif
  1881. memcpy (array, at, count * siz);
  1882. *p_n += count;
  1883. return TRUE;
  1884. }
  1885. static protobuf_c_boolean
  1886. parse_member (ScannedMember *scanned_member,
  1887. ProtobufCMessage *message,
  1888. ProtobufCAllocator *allocator)
  1889. {
  1890. const ProtobufCFieldDescriptor *field = scanned_member->field;
  1891. void *member;
  1892. if (field == NULL)
  1893. {
  1894. ProtobufCMessageUnknownField *ufield = message->unknown_fields + (message->n_unknown_fields++);
  1895. ufield->tag = scanned_member->tag;
  1896. ufield->wire_type = scanned_member->wire_type;
  1897. ufield->len = scanned_member->len;
  1898. DO_UNALIGNED_ALLOC (ufield->data, allocator, scanned_member->len, return 0);
  1899. memcpy (ufield->data, scanned_member->data, ufield->len);
  1900. return 1;
  1901. }
  1902. member = (char*)message + field->offset;
  1903. switch (field->label)
  1904. {
  1905. case PROTOBUF_C_LABEL_REQUIRED:
  1906. return parse_required_member (scanned_member, member, allocator, TRUE);
  1907. case PROTOBUF_C_LABEL_OPTIONAL:
  1908. return parse_optional_member (scanned_member, member, message, allocator);
  1909. case PROTOBUF_C_LABEL_REPEATED:
  1910. if (field->packed
  1911. && scanned_member->wire_type == PROTOBUF_C_WIRE_TYPE_LENGTH_PREFIXED)
  1912. return parse_packed_repeated_member (scanned_member, member, message);
  1913. else
  1914. return parse_repeated_member (scanned_member, member, message, allocator);
  1915. }
  1916. PROTOBUF_C_ASSERT_NOT_REACHED ();
  1917. return 0;
  1918. }
  1919. /* TODO: expose/use this function if desc->message_init==NULL
  1920. (which occurs for old code, and may be useful for certain
  1921. programatic techniques for generating descriptors). */
  1922. void
  1923. protobuf_c_message_init_generic (const ProtobufCMessageDescriptor *desc,
  1924. ProtobufCMessage *message)
  1925. {
  1926. unsigned i;
  1927. memset (message, 0, desc->sizeof_message);
  1928. message->descriptor = desc;
  1929. for (i = 0; i < desc->n_fields; i++)
  1930. if (desc->fields[i].default_value != NULL
  1931. && desc->fields[i].label != PROTOBUF_C_LABEL_REPEATED)
  1932. {
  1933. void *field = STRUCT_MEMBER_P (message, desc->fields[i].offset);
  1934. const void *dv = desc->fields[i].default_value;
  1935. switch (desc->fields[i].type)
  1936. {
  1937. case PROTOBUF_C_TYPE_INT32:
  1938. case PROTOBUF_C_TYPE_SINT32:
  1939. case PROTOBUF_C_TYPE_SFIXED32:
  1940. case PROTOBUF_C_TYPE_UINT32:
  1941. case PROTOBUF_C_TYPE_FIXED32:
  1942. case PROTOBUF_C_TYPE_FLOAT:
  1943. case PROTOBUF_C_TYPE_ENUM:
  1944. memcpy (field, dv, 4);
  1945. break;
  1946. case PROTOBUF_C_TYPE_INT64:
  1947. case PROTOBUF_C_TYPE_SINT64:
  1948. case PROTOBUF_C_TYPE_SFIXED64:
  1949. case PROTOBUF_C_TYPE_UINT64:
  1950. case PROTOBUF_C_TYPE_FIXED64:
  1951. case PROTOBUF_C_TYPE_DOUBLE:
  1952. memcpy (field, dv, 8);
  1953. break;
  1954. case PROTOBUF_C_TYPE_BOOL:
  1955. memcpy (field, dv, sizeof (protobuf_c_boolean));
  1956. break;
  1957. case PROTOBUF_C_TYPE_BYTES:
  1958. memcpy (field, dv, sizeof (ProtobufCBinaryData));
  1959. break;
  1960. case PROTOBUF_C_TYPE_STRING:
  1961. case PROTOBUF_C_TYPE_MESSAGE:
  1962. /* the next line essentially implements a cast from const,
  1963. which is totally unavoidable. */
  1964. *(const void**)field = dv;
  1965. break;
  1966. }
  1967. }
  1968. }
  1969. /* ScannedMember slabs (an unpacking implementation detail).
  1970. Before doing real unpacking, we first scan through the
  1971. elements to see how many there are (for repeated fields),
  1972. and which field to use (for non-repeated fields given twice).
  1973. * In order to avoid allocations for small messages,
  1974. we keep a stack-allocated slab of ScannedMembers of
  1975. size FIRST_SCANNED_MEMBER_SLAB_SIZE (16).
  1976. After we fill that up, we allocate each slab twice
  1977. as large as the previous one. */
  1978. #define FIRST_SCANNED_MEMBER_SLAB_SIZE_LOG2 4
  1979. /* The number of slabs, including the stack-allocated ones;
  1980. choose the number so that we would overflow if we needed
  1981. a slab larger than provided. */
  1982. #define MAX_SCANNED_MEMBER_SLAB \
  1983. (sizeof(void*)*8 - 1 \
  1984. - BOUND_SIZEOF_SCANNED_MEMBER_LOG2 \
  1985. - FIRST_SCANNED_MEMBER_SLAB_SIZE_LOG2)
  1986. ProtobufCMessage *
  1987. protobuf_c_message_unpack (const ProtobufCMessageDescriptor *desc,
  1988. ProtobufCAllocator *allocator,
  1989. size_t len,
  1990. const uint8_t *data)
  1991. {
  1992. ProtobufCMessage *rv;
  1993. size_t rem = len;
  1994. const uint8_t *at = data;
  1995. const ProtobufCFieldDescriptor *last_field = desc->fields + 0;
  1996. ScannedMember first_member_slab[1<<FIRST_SCANNED_MEMBER_SLAB_SIZE_LOG2];
  1997. /* scanned_member_slabs[i] is an array of arrays of ScannedMember.
  1998. The first slab (scanned_member_slabs[0] is just a pointer to
  1999. first_member_slab), above. All subsequent slabs will be allocated
  2000. using the allocator. */
  2001. ScannedMember *scanned_member_slabs[MAX_SCANNED_MEMBER_SLAB+1];
  2002. unsigned which_slab = 0; /* the slab we are currently populating */
  2003. unsigned in_slab_index = 0; /* number of members in the slab */
  2004. size_t n_unknown = 0;
  2005. unsigned f;
  2006. unsigned i_slab;
  2007. unsigned last_field_index = 0;
  2008. unsigned long *required_fields_bitmap;
  2009. unsigned required_fields_bitmap_len;
  2010. static const unsigned word_bits = sizeof(long) * 8;
  2011. ASSERT_IS_MESSAGE_DESCRIPTOR (desc);
  2012. if (allocator == NULL)
  2013. allocator = &protobuf_c_default_allocator;
  2014. required_fields_bitmap_len = (desc->n_fields + word_bits - 1) / word_bits;
  2015. required_fields_bitmap = alloca(required_fields_bitmap_len * sizeof(long));
  2016. memset(required_fields_bitmap, 0, required_fields_bitmap_len * sizeof(long));
  2017. DO_ALLOC (rv, allocator, desc->sizeof_message, return NULL);
  2018. scanned_member_slabs[0] = first_member_slab;
  2019. /* Generated code always defines "message_init".
  2020. However, we provide a fallback for (1) users of old protobuf-c
  2021. generated-code that do not provide the function,
  2022. and (2) descriptors constructed from some other source
  2023. (most likely, direct construction from the .proto file) */
  2024. if (desc->message_init != NULL)
  2025. protobuf_c_message_init (desc, rv);
  2026. else
  2027. protobuf_c_message_init_generic (desc, rv);
  2028. while (rem > 0)
  2029. {
  2030. uint32_t tag;
  2031. ProtobufCWireType wire_type;
  2032. size_t used = parse_tag_and_wiretype (rem, at, &tag, &wire_type);
  2033. const ProtobufCFieldDescriptor *field;
  2034. ScannedMember tmp;
  2035. if (used == 0)
  2036. {
  2037. UNPACK_ERROR (("error parsing tag/wiretype at offset %u",
  2038. (unsigned)(at-data)));
  2039. goto error_cleanup_during_scan;
  2040. }
  2041. /* XXX: consider optimizing for field[1].id == tag, if field[1] exists! */
  2042. if (last_field == NULL || last_field->id != tag)
  2043. {
  2044. /* lookup field */
  2045. int field_index = int_range_lookup (desc->n_field_ranges,
  2046. desc->field_ranges,
  2047. tag);
  2048. if (field_index < 0)
  2049. {
  2050. field = NULL;
  2051. n_unknown++;
  2052. }
  2053. else
  2054. {
  2055. field = desc->fields + field_index;
  2056. last_field = field;
  2057. last_field_index = field_index;
  2058. }
  2059. }
  2060. else
  2061. field = last_field;
  2062. if (field != NULL && field->label == PROTOBUF_C_LABEL_REQUIRED)
  2063. required_fields_bitmap[last_field_index / word_bits] |= (1UL << (last_field_index % word_bits));
  2064. at += used;
  2065. rem -= used;
  2066. tmp.tag = tag;
  2067. tmp.wire_type = wire_type;
  2068. tmp.field = field;
  2069. tmp.data = at;
  2070. switch (wire_type)
  2071. {
  2072. case PROTOBUF_C_WIRE_TYPE_VARINT:
  2073. {
  2074. unsigned max_len = rem < 10 ? rem : 10;
  2075. unsigned i;
  2076. for (i = 0; i < max_len; i++)
  2077. if ((at[i] & 0x80) == 0)
  2078. break;
  2079. if (i == max_len)
  2080. {
  2081. UNPACK_ERROR (("unterminated varint at offset %u",
  2082. (unsigned)(at-data)));
  2083. goto error_cleanup_during_scan;
  2084. }
  2085. tmp.len = i + 1;
  2086. }
  2087. break;
  2088. case PROTOBUF_C_WIRE_TYPE_64BIT:
  2089. if (rem < 8)
  2090. {
  2091. UNPACK_ERROR (("too short after 64bit wiretype at offset %u",
  2092. (unsigned)(at-data)));
  2093. goto error_cleanup_during_scan;
  2094. }
  2095. tmp.len = 8;
  2096. break;
  2097. case PROTOBUF_C_WIRE_TYPE_LENGTH_PREFIXED:
  2098. {
  2099. size_t pref_len;
  2100. tmp.len = scan_length_prefixed_data (rem, at, &pref_len);
  2101. if (tmp.len == 0)
  2102. {
  2103. /* NOTE: scan_length_prefixed_data calls UNPACK_ERROR */
  2104. goto error_cleanup_during_scan;
  2105. }
  2106. tmp.length_prefix_len = pref_len;
  2107. break;
  2108. }
  2109. case PROTOBUF_C_WIRE_TYPE_32BIT:
  2110. if (rem < 4)
  2111. {
  2112. UNPACK_ERROR (("too short after 32bit wiretype at offset %u",
  2113. (unsigned)(at-data)));
  2114. goto error_cleanup_during_scan;
  2115. }
  2116. tmp.len = 4;
  2117. break;
  2118. default:
  2119. UNPACK_ERROR (("unsupported tag %u at offset %u",
  2120. wire_type, (unsigned)(at-data)));
  2121. goto error_cleanup_during_scan;
  2122. }
  2123. if (in_slab_index == (1U<<(which_slab+FIRST_SCANNED_MEMBER_SLAB_SIZE_LOG2)))
  2124. {
  2125. size_t size;
  2126. in_slab_index = 0;
  2127. if (which_slab == MAX_SCANNED_MEMBER_SLAB)
  2128. {
  2129. UNPACK_ERROR (("too many fields"));
  2130. goto error_cleanup_during_scan;
  2131. }
  2132. which_slab++;
  2133. size = sizeof(ScannedMember) << (which_slab+FIRST_SCANNED_MEMBER_SLAB_SIZE_LOG2);
  2134. /* TODO: consider using alloca() ! */
  2135. if (allocator->tmp_alloc != NULL)
  2136. scanned_member_slabs[which_slab] = TMPALLOC(allocator, size);
  2137. else
  2138. DO_ALLOC (scanned_member_slabs[which_slab], allocator, size, goto error_cleanup_during_scan);
  2139. }
  2140. scanned_member_slabs[which_slab][in_slab_index++] = tmp;
  2141. if (field != NULL && field->label == PROTOBUF_C_LABEL_REPEATED)
  2142. {
  2143. size_t *n = STRUCT_MEMBER_PTR (size_t, rv, field->quantifier_offset);
  2144. if (field->packed
  2145. && wire_type == PROTOBUF_C_WIRE_TYPE_LENGTH_PREFIXED)
  2146. {
  2147. size_t count;
  2148. if (!count_packed_elements (field->type,
  2149. tmp.len - tmp.length_prefix_len,
  2150. tmp.data + tmp.length_prefix_len,
  2151. &count))
  2152. {
  2153. UNPACK_ERROR (("counting packed elements"));
  2154. goto error_cleanup_during_scan;
  2155. }
  2156. *n += count;
  2157. }
  2158. else
  2159. *n += 1;
  2160. }
  2161. at += tmp.len;
  2162. rem -= tmp.len;
  2163. }
  2164. /* allocate space for repeated fields, also check that all required fields have been set */
  2165. for (f = 0; f < desc->n_fields; f++)
  2166. {
  2167. const ProtobufCFieldDescriptor *field = desc->fields + f;
  2168. if (field->label == PROTOBUF_C_LABEL_REPEATED)
  2169. {
  2170. size_t siz = sizeof_elt_in_repeated_array (field->type);
  2171. size_t *n_ptr = STRUCT_MEMBER_PTR (size_t, rv, field->quantifier_offset);
  2172. if (*n_ptr != 0)
  2173. {
  2174. unsigned n = *n_ptr;
  2175. *n_ptr = 0;
  2176. assert(rv->descriptor != NULL);
  2177. #define CLEAR_REMAINING_N_PTRS() \
  2178. for(f++;f < desc->n_fields; f++) \
  2179. { \
  2180. field = desc->fields + f; \
  2181. if (field->label == PROTOBUF_C_LABEL_REPEATED) \
  2182. STRUCT_MEMBER (size_t, rv, field->quantifier_offset) = 0; \
  2183. }
  2184. DO_ALLOC (STRUCT_MEMBER (void *, rv, field->offset),
  2185. allocator, siz * n,
  2186. CLEAR_REMAINING_N_PTRS (); goto error_cleanup);
  2187. #undef CLEAR_REMAINING_N_PTRS
  2188. }
  2189. }
  2190. else if (field->label == PROTOBUF_C_LABEL_REQUIRED)
  2191. {
  2192. if (field->default_value == NULL && 0 == (required_fields_bitmap[f / word_bits] & (1UL << (f % word_bits))))
  2193. {
  2194. UNPACK_ERROR (("message '%s': missing required field '%s'", desc->name, field->name));
  2195. goto error_cleanup;
  2196. }
  2197. }
  2198. }
  2199. /* allocate space for unknown fields */
  2200. if (n_unknown)
  2201. {
  2202. DO_ALLOC (rv->unknown_fields,
  2203. allocator, n_unknown * sizeof (ProtobufCMessageUnknownField),
  2204. goto error_cleanup);
  2205. }
  2206. /* do real parsing */
  2207. for (i_slab = 0; i_slab <= which_slab; i_slab++)
  2208. {
  2209. unsigned max = (i_slab == which_slab) ? in_slab_index : (1U<<(i_slab+4));
  2210. ScannedMember *slab = scanned_member_slabs[i_slab];
  2211. unsigned j;
  2212. for (j = 0; j < max; j++)
  2213. {
  2214. if (!parse_member (slab + j, rv, allocator))
  2215. {
  2216. UNPACK_ERROR (("error parsing member %s of %s",
  2217. slab->field ? slab->field->name : "*unknown-field*", desc->name));
  2218. goto error_cleanup;
  2219. }
  2220. }
  2221. }
  2222. /* cleanup */
  2223. if (allocator->tmp_alloc == NULL)
  2224. {
  2225. unsigned j;
  2226. for (j = 1; j <= which_slab; j++)
  2227. FREE (allocator, scanned_member_slabs[j]);
  2228. }
  2229. return rv;
  2230. error_cleanup:
  2231. protobuf_c_message_free_unpacked (rv, allocator);
  2232. if (allocator->tmp_alloc == NULL)
  2233. {
  2234. unsigned j;
  2235. for (j = 1; j <= which_slab; j++)
  2236. FREE (allocator, scanned_member_slabs[j]);
  2237. }
  2238. return NULL;
  2239. error_cleanup_during_scan:
  2240. FREE (allocator, rv);
  2241. if (allocator->tmp_alloc == NULL)
  2242. {
  2243. unsigned j;
  2244. for (j = 1; j <= which_slab; j++)
  2245. FREE (allocator, scanned_member_slabs[j]);
  2246. }
  2247. return NULL;
  2248. }
  2249. /* === free_unpacked === */
  2250. void
  2251. protobuf_c_message_free_unpacked (ProtobufCMessage *message,
  2252. ProtobufCAllocator *allocator)
  2253. {
  2254. const ProtobufCMessageDescriptor *desc = message->descriptor;
  2255. unsigned f;
  2256. ASSERT_IS_MESSAGE (message);
  2257. if (allocator == NULL)
  2258. allocator = &protobuf_c_default_allocator;
  2259. message->descriptor = NULL;
  2260. for (f = 0; f < desc->n_fields; f++)
  2261. {
  2262. if (desc->fields[f].label == PROTOBUF_C_LABEL_REPEATED)
  2263. {
  2264. size_t n = STRUCT_MEMBER (size_t, message, desc->fields[f].quantifier_offset);
  2265. void * arr = STRUCT_MEMBER (void *, message, desc->fields[f].offset);
  2266. if (desc->fields[f].type == PROTOBUF_C_TYPE_STRING)
  2267. {
  2268. unsigned i;
  2269. for (i = 0; i < n; i++)
  2270. FREE (allocator, ((char**)arr)[i]);
  2271. }
  2272. else if (desc->fields[f].type == PROTOBUF_C_TYPE_BYTES)
  2273. {
  2274. unsigned i;
  2275. for (i = 0; i < n; i++)
  2276. FREE (allocator, ((ProtobufCBinaryData*)arr)[i].data);
  2277. }
  2278. else if (desc->fields[f].type == PROTOBUF_C_TYPE_MESSAGE)
  2279. {
  2280. unsigned i;
  2281. for (i = 0; i < n; i++)
  2282. protobuf_c_message_free_unpacked (((ProtobufCMessage**)arr)[i], allocator);
  2283. }
  2284. if (arr != NULL)
  2285. FREE (allocator, arr);
  2286. }
  2287. else if (desc->fields[f].type == PROTOBUF_C_TYPE_STRING)
  2288. {
  2289. char *str = STRUCT_MEMBER (char *, message, desc->fields[f].offset);
  2290. if (str && str != desc->fields[f].default_value)
  2291. FREE (allocator, str);
  2292. }
  2293. else if (desc->fields[f].type == PROTOBUF_C_TYPE_BYTES)
  2294. {
  2295. void *data = STRUCT_MEMBER (ProtobufCBinaryData, message, desc->fields[f].offset).data;
  2296. const ProtobufCBinaryData *default_bd;
  2297. default_bd = desc->fields[f].default_value;
  2298. if (data != NULL
  2299. && (default_bd == NULL || default_bd->data != data))
  2300. FREE (allocator, data);
  2301. }
  2302. else if (desc->fields[f].type == PROTOBUF_C_TYPE_MESSAGE)
  2303. {
  2304. ProtobufCMessage *sm;
  2305. sm = STRUCT_MEMBER (ProtobufCMessage *, message,desc->fields[f].offset);
  2306. if (sm && sm != desc->fields[f].default_value)
  2307. protobuf_c_message_free_unpacked (sm, allocator);
  2308. }
  2309. }
  2310. for (f = 0; f < message->n_unknown_fields; f++)
  2311. FREE (allocator, message->unknown_fields[f].data);
  2312. if (message->unknown_fields != NULL)
  2313. FREE (allocator, message->unknown_fields);
  2314. FREE (allocator, message);
  2315. }
  2316. void
  2317. protobuf_c_message_init (const ProtobufCMessageDescriptor *descriptor,
  2318. void *message)
  2319. {
  2320. descriptor->message_init((ProtobufCMessage*) (message));
  2321. }
  2322. /* === services === */
  2323. typedef void (*GenericHandler)(void *service,
  2324. const ProtobufCMessage *input,
  2325. ProtobufCClosure closure,
  2326. void *closure_data);
  2327. void
  2328. protobuf_c_service_invoke_internal(ProtobufCService *service,
  2329. unsigned method_index,
  2330. const ProtobufCMessage *input,
  2331. ProtobufCClosure closure,
  2332. void *closure_data)
  2333. {
  2334. GenericHandler *handlers;
  2335. GenericHandler handler;
  2336. /* Verify that method_index is within range.
  2337. If this fails, you are likely invoking a newly added
  2338. method on an old service. (Although other memory corruption
  2339. bugs can cause this assertion too) */
  2340. PROTOBUF_C_ASSERT (method_index < service->descriptor->n_methods);
  2341. /* Get the array of virtual methods (which are enumerated by
  2342. the generated code) */
  2343. handlers = (GenericHandler *) (service + 1);
  2344. /* get our method and invoke it */
  2345. /* TODO: seems like handler==NULL is a situation that
  2346. needs handling */
  2347. handler = handlers[method_index];
  2348. (*handler) (service, input, closure, closure_data);
  2349. }
  2350. void
  2351. protobuf_c_service_generated_init (ProtobufCService *service,
  2352. const ProtobufCServiceDescriptor *descriptor,
  2353. ProtobufCServiceDestroy destroy)
  2354. {
  2355. ASSERT_IS_SERVICE_DESCRIPTOR(descriptor);
  2356. service->descriptor = descriptor;
  2357. service->destroy = destroy;
  2358. service->invoke = protobuf_c_service_invoke_internal;
  2359. memset (service + 1, 0, descriptor->n_methods * sizeof (GenericHandler));
  2360. }
  2361. void protobuf_c_service_destroy (ProtobufCService *service)
  2362. {
  2363. service->destroy (service);
  2364. }
  2365. /* --- querying the descriptors --- */
  2366. const ProtobufCEnumValue *
  2367. protobuf_c_enum_descriptor_get_value_by_name
  2368. (const ProtobufCEnumDescriptor *desc,
  2369. const char *name)
  2370. {
  2371. unsigned start = 0, count = desc->n_value_names;
  2372. while (count > 1)
  2373. {
  2374. unsigned mid = start + count / 2;
  2375. int rv = strcmp (desc->values_by_name[mid].name, name);
  2376. if (rv == 0)
  2377. return desc->values + desc->values_by_name[mid].index;
  2378. else if (rv < 0)
  2379. {
  2380. count = start + count - (mid + 1);
  2381. start = mid + 1;
  2382. }
  2383. else
  2384. count = mid - start;
  2385. }
  2386. if (count == 0)
  2387. return NULL;
  2388. if (strcmp (desc->values_by_name[start].name, name) == 0)
  2389. return desc->values + desc->values_by_name[start].index;
  2390. return NULL;
  2391. }
  2392. const ProtobufCEnumValue *
  2393. protobuf_c_enum_descriptor_get_value
  2394. (const ProtobufCEnumDescriptor *desc,
  2395. int value)
  2396. {
  2397. int rv = int_range_lookup (desc->n_value_ranges, desc->value_ranges, value);
  2398. if (rv < 0)
  2399. return NULL;
  2400. return desc->values + rv;
  2401. }
  2402. const ProtobufCFieldDescriptor *
  2403. protobuf_c_message_descriptor_get_field_by_name
  2404. (const ProtobufCMessageDescriptor *desc,
  2405. const char *name)
  2406. {
  2407. unsigned start = 0, count = desc->n_fields;
  2408. const ProtobufCFieldDescriptor *field;
  2409. while (count > 1)
  2410. {
  2411. unsigned mid = start + count / 2;
  2412. int rv;
  2413. field = desc->fields + desc->fields_sorted_by_name[mid];
  2414. rv = strcmp (field->name, name);
  2415. if (rv == 0)
  2416. return field;
  2417. else if (rv < 0)
  2418. {
  2419. count = start + count - (mid + 1);
  2420. start = mid + 1;
  2421. }
  2422. else
  2423. count = mid - start;
  2424. }
  2425. if (count == 0)
  2426. return NULL;
  2427. field = desc->fields + desc->fields_sorted_by_name[start];
  2428. if (strcmp (field->name, name) == 0)
  2429. return field;
  2430. return NULL;
  2431. }
  2432. const ProtobufCFieldDescriptor *
  2433. protobuf_c_message_descriptor_get_field
  2434. (const ProtobufCMessageDescriptor *desc,
  2435. unsigned value)
  2436. {
  2437. int rv = int_range_lookup (desc->n_field_ranges,
  2438. desc->field_ranges,
  2439. value);
  2440. if (rv < 0)
  2441. return NULL;
  2442. return desc->fields + rv;
  2443. }
  2444. const ProtobufCMethodDescriptor *
  2445. protobuf_c_service_descriptor_get_method_by_name
  2446. (const ProtobufCServiceDescriptor *desc,
  2447. const char *name)
  2448. {
  2449. unsigned start = 0, count = desc->n_methods;
  2450. while (count > 1)
  2451. {
  2452. unsigned mid = start + count / 2;
  2453. unsigned mid_index = desc->method_indices_by_name[mid];
  2454. const char *mid_name = desc->methods[mid_index].name;
  2455. int rv = strcmp (mid_name, name);
  2456. if (rv == 0)
  2457. return desc->methods + desc->method_indices_by_name[mid];
  2458. if (rv < 0)
  2459. {
  2460. count = start + count - (mid + 1);
  2461. start = mid + 1;
  2462. }
  2463. else
  2464. {
  2465. count = mid - start;
  2466. }
  2467. }
  2468. if (count == 0)
  2469. return NULL;
  2470. if (strcmp (desc->methods[desc->method_indices_by_name[start]].name, name) == 0)
  2471. return desc->methods + desc->method_indices_by_name[start];
  2472. return NULL;
  2473. }