libsignal-protocol-c  master
protobuf-c.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008-2014, Dave Benson and the protobuf-c authors.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above
13  * copyright notice, this list of conditions and the following disclaimer
14  * in the documentation and/or other materials provided with the
15  * distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
196 #ifndef PROTOBUF_C_H
197 #define PROTOBUF_C_H
198 
199 #include <assert.h>
200 #include <limits.h>
201 #include <stddef.h>
202 #include <stdint.h>
203 
204 #ifdef __cplusplus
205 # define PROTOBUF_C__BEGIN_DECLS extern "C" {
206 # define PROTOBUF_C__END_DECLS }
207 #else
208 # define PROTOBUF_C__BEGIN_DECLS
209 # define PROTOBUF_C__END_DECLS
210 #endif
211 
212 PROTOBUF_C__BEGIN_DECLS
213 
214 #if defined(_WIN32) && defined(PROTOBUF_C_USE_SHARED_LIB)
215 # ifdef PROTOBUF_C_EXPORT
216 # define PROTOBUF_C__API __declspec(dllexport)
217 # else
218 # define PROTOBUF_C__API __declspec(dllimport)
219 # endif
220 #else
221 # define PROTOBUF_C__API
222 #endif
223 
224 #if !defined(PROTOBUF_C__NO_DEPRECATED) && \
225  ((__GNUC__ > 3) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1))
226 # define PROTOBUF_C__DEPRECATED __attribute__((__deprecated__))
227 #else
228 # define PROTOBUF_C__DEPRECATED
229 #endif
230 
231 #ifndef PROTOBUF_C__FORCE_ENUM_TO_BE_INT_SIZE
232  #define PROTOBUF_C__FORCE_ENUM_TO_BE_INT_SIZE(enum_name) \
233  , _##enum_name##_IS_INT_SIZE = INT_MAX
234 #endif
235 
236 #define PROTOBUF_C__SERVICE_DESCRIPTOR_MAGIC 0x14159bc3
237 #define PROTOBUF_C__MESSAGE_DESCRIPTOR_MAGIC 0x28aaeef9
238 #define PROTOBUF_C__ENUM_DESCRIPTOR_MAGIC 0x114315af
239 
252 typedef enum {
255 
258 
262 
271 typedef enum {
274 
280 
288 
297 typedef enum {
315 } ProtobufCType;
316 
325 typedef enum {
326  PROTOBUF_C_WIRE_TYPE_VARINT = 0,
327  PROTOBUF_C_WIRE_TYPE_64BIT = 1,
328  PROTOBUF_C_WIRE_TYPE_LENGTH_PREFIXED = 2,
329  /* "Start group" and "end group" wire types are unsupported. */
330  PROTOBUF_C_WIRE_TYPE_32BIT = 5,
332 
333 struct ProtobufCAllocator;
334 struct ProtobufCBinaryData;
335 struct ProtobufCBuffer;
336 struct ProtobufCBufferSimple;
338 struct ProtobufCEnumValue;
341 struct ProtobufCIntRange;
342 struct ProtobufCMessage;
346 struct ProtobufCService;
348 
349 typedef struct ProtobufCAllocator ProtobufCAllocator;
350 typedef struct ProtobufCBinaryData ProtobufCBinaryData;
351 typedef struct ProtobufCBuffer ProtobufCBuffer;
352 typedef struct ProtobufCBufferSimple ProtobufCBufferSimple;
353 typedef struct ProtobufCEnumDescriptor ProtobufCEnumDescriptor;
354 typedef struct ProtobufCEnumValue ProtobufCEnumValue;
355 typedef struct ProtobufCEnumValueIndex ProtobufCEnumValueIndex;
356 typedef struct ProtobufCFieldDescriptor ProtobufCFieldDescriptor;
357 typedef struct ProtobufCIntRange ProtobufCIntRange;
358 typedef struct ProtobufCMessage ProtobufCMessage;
359 typedef struct ProtobufCMessageDescriptor ProtobufCMessageDescriptor;
360 typedef struct ProtobufCMessageUnknownField ProtobufCMessageUnknownField;
361 typedef struct ProtobufCMethodDescriptor ProtobufCMethodDescriptor;
362 typedef struct ProtobufCService ProtobufCService;
363 typedef struct ProtobufCServiceDescriptor ProtobufCServiceDescriptor;
364 
366 typedef int protobuf_c_boolean;
367 
368 typedef void (*ProtobufCClosure)(const ProtobufCMessage *, void *closure_data);
369 typedef void (*ProtobufCMessageInit)(ProtobufCMessage *);
370 typedef void (*ProtobufCServiceDestroy)(ProtobufCService *);
371 
375 struct ProtobufCAllocator {
377  void *(*alloc)(void *allocator_data, size_t size);
378 
380  void (*free)(void *allocator_data, void *pointer);
381 
384 };
385 
393 struct ProtobufCBinaryData {
394  size_t len;
395  uint8_t *data;
396 };
397 
433 struct ProtobufCBuffer {
435  void (*append)(ProtobufCBuffer *buffer,
436  size_t len,
437  const uint8_t *data);
438 };
439 
468 struct ProtobufCBufferSimple {
470  ProtobufCBuffer base;
472  size_t alloced;
474  size_t len;
476  uint8_t *data;
478  protobuf_c_boolean must_free_data;
480  ProtobufCAllocator *allocator;
481 };
482 
486 struct ProtobufCEnumDescriptor {
488  uint32_t magic;
489 
491  const char *name;
493  const char *short_name;
495  const char *c_name;
497  const char *package_name;
498 
500  unsigned n_values;
502  const ProtobufCEnumValue *values;
503 
505  unsigned n_value_names;
507  const ProtobufCEnumValueIndex *values_by_name;
508 
510  unsigned n_value_ranges;
512  const ProtobufCIntRange *value_ranges;
513 
515  void *reserved1;
517  void *reserved2;
519  void *reserved3;
521  void *reserved4;
522 };
523 
527 struct ProtobufCEnumValue {
529  const char *name;
530 
532  const char *c_name;
533 
535  int value;
536 };
537 
541 struct ProtobufCEnumValueIndex {
543  const char *name;
545  unsigned index;
546 };
547 
551 struct ProtobufCFieldDescriptor {
553  const char *name;
554 
556  uint32_t id;
557 
559  ProtobufCLabel label;
560 
562  ProtobufCType type;
563 
570 
575  unsigned offset;
576 
588  const void *descriptor; /* for MESSAGE and ENUM types */
589 
591  const void *default_value;
592 
597  uint32_t flags;
598 
600  unsigned reserved_flags;
602  void *reserved2;
604  void *reserved3;
605 };
606 
615 struct ProtobufCIntRange {
616  int start_value;
617  unsigned orig_index;
618  /*
619  * NOTE: the number of values in the range can be inferred by looking
620  * at the next element's orig_index. A dummy element is added to make
621  * this simple.
622  */
623 };
624 
640 struct ProtobufCMessage {
642  const ProtobufCMessageDescriptor *descriptor;
646  ProtobufCMessageUnknownField *unknown_fields;
647 };
648 
652 struct ProtobufCMessageDescriptor {
654  uint32_t magic;
655 
657  const char *name;
659  const char *short_name;
661  const char *c_name;
663  const char *package_name;
664 
670 
672  unsigned n_fields;
674  const ProtobufCFieldDescriptor *fields;
676  const unsigned *fields_sorted_by_name;
677 
679  unsigned n_field_ranges;
681  const ProtobufCIntRange *field_ranges;
682 
684  ProtobufCMessageInit message_init;
685 
687  void *reserved1;
689  void *reserved2;
691  void *reserved3;
692 };
693 
697 struct ProtobufCMessageUnknownField {
699  uint32_t tag;
703  size_t len;
705  uint8_t *data;
706 };
707 
711 struct ProtobufCMethodDescriptor {
713  const char *name;
715  const ProtobufCMessageDescriptor *input;
717  const ProtobufCMessageDescriptor *output;
718 };
719 
723 struct ProtobufCService {
725  const ProtobufCServiceDescriptor *descriptor;
727  void (*invoke)(ProtobufCService *service,
728  unsigned method_index,
729  const ProtobufCMessage *input,
730  ProtobufCClosure closure,
731  void *closure_data);
733  void (*destroy)(ProtobufCService *service);
734 };
735 
739 struct ProtobufCServiceDescriptor {
741  uint32_t magic;
742 
744  const char *name;
746  const char *short_name;
748  const char *c_name;
750  const char *package;
752  unsigned n_methods;
754  const ProtobufCMethodDescriptor *methods;
756  const unsigned *method_indices_by_name;
757 };
758 
765 PROTOBUF_C__API
766 const char *
767 protobuf_c_version(void);
768 
776 PROTOBUF_C__API
777 uint32_t
779 
784 #define PROTOBUF_C_VERSION "1.1.1"
785 
790 #define PROTOBUF_C_VERSION_NUMBER 1001001
791 
796 #define PROTOBUF_C_MIN_COMPILER_VERSION 1000000
797 
811 PROTOBUF_C__API
812 const ProtobufCEnumValue *
814  const ProtobufCEnumDescriptor *desc,
815  const char *name);
816 
832 PROTOBUF_C__API
833 const ProtobufCEnumValue *
835  const ProtobufCEnumDescriptor *desc,
836  int value);
837 
851 PROTOBUF_C__API
852 const ProtobufCFieldDescriptor *
854  const ProtobufCMessageDescriptor *desc,
855  const char *name);
856 
870 PROTOBUF_C__API
871 const ProtobufCFieldDescriptor *
873  const ProtobufCMessageDescriptor *desc,
874  unsigned value);
875 
884 PROTOBUF_C__API
885 size_t
886 protobuf_c_message_get_packed_size(const ProtobufCMessage *message);
887 
904 PROTOBUF_C__API
905 size_t
906 protobuf_c_message_pack(const ProtobufCMessage *message, uint8_t *out);
907 
921 PROTOBUF_C__API
922 size_t
924  const ProtobufCMessage *message,
925  ProtobufCBuffer *buffer);
926 
944 PROTOBUF_C__API
945 ProtobufCMessage *
947  const ProtobufCMessageDescriptor *descriptor,
948  ProtobufCAllocator *allocator,
949  size_t len,
950  const uint8_t *data);
951 
964 PROTOBUF_C__API
965 void
967  ProtobufCMessage *message,
968  ProtobufCAllocator *allocator);
969 
981 PROTOBUF_C__API
982 protobuf_c_boolean
983 protobuf_c_message_check(const ProtobufCMessage *);
984 
986 #define PROTOBUF_C_MESSAGE_INIT(descriptor) { descriptor, 0, NULL }
987 
996 PROTOBUF_C__API
997 void
999  const ProtobufCMessageDescriptor *descriptor,
1000  void *message);
1001 
1008 PROTOBUF_C__API
1009 void
1010 protobuf_c_service_destroy(ProtobufCService *service);
1011 
1025 PROTOBUF_C__API
1026 const ProtobufCMethodDescriptor *
1028  const ProtobufCServiceDescriptor *desc,
1029  const char *name);
1030 
1034 #define PROTOBUF_C_BUFFER_SIMPLE_INIT(array_of_bytes) \
1035 { \
1036  { protobuf_c_buffer_simple_append }, \
1037  sizeof(array_of_bytes), \
1038  0, \
1039  (array_of_bytes), \
1040  0, \
1041  NULL \
1042 }
1043 
1047 #define PROTOBUF_C_BUFFER_SIMPLE_CLEAR(simp_buf) \
1048 do { \
1049  if ((simp_buf)->must_free_data) { \
1050  if ((simp_buf)->allocator != NULL) \
1051  (simp_buf)->allocator->free( \
1052  (simp_buf)->allocator, \
1053  (simp_buf)->data); \
1054  else \
1055  free((simp_buf)->data); \
1056  } \
1057 } while (0)
1058 
1070 PROTOBUF_C__API
1071 void
1073  ProtobufCBuffer *buffer,
1074  size_t len,
1075  const unsigned char *data);
1076 
1077 PROTOBUF_C__API
1078 void
1079 protobuf_c_service_generated_init(
1080  ProtobufCService *service,
1081  const ProtobufCServiceDescriptor *descriptor,
1082  ProtobufCServiceDestroy destroy);
1083 
1084 PROTOBUF_C__API
1085 void
1086 protobuf_c_service_invoke_internal(
1087  ProtobufCService *service,
1088  unsigned method_index,
1089  const ProtobufCMessage *input,
1090  ProtobufCClosure closure,
1091  void *closure_data);
1092 
1095 PROTOBUF_C__END_DECLS
1096 
1097 #endif /* PROTOBUF_C_H */
void * reserved3
Definition: protobuf-c.h:691
PROTOBUF_C__API void protobuf_c_message_init(const ProtobufCMessageDescriptor *descriptor, void *message)
Definition: protobuf-c.c:3254
void * reserved1
Definition: protobuf-c.h:687
const char * c_name
Definition: protobuf-c.h:495
void * reserved3
Definition: protobuf-c.h:604
ProtobufCLabel label
Definition: protobuf-c.h:559
void * allocator_data
Definition: protobuf-c.h:383
Definition: protobuf-c.h:302
Definition: protobuf-c.h:307
Definition: protobuf-c.h:711
PROTOBUF_C__API ProtobufCMessage * protobuf_c_message_unpack(const ProtobufCMessageDescriptor *descriptor, ProtobufCAllocator *allocator, size_t len, const uint8_t *data)
Definition: protobuf-c.c:2871
unsigned n_unknown_fields
Definition: protobuf-c.h:644
PROTOBUF_C__API const ProtobufCFieldDescriptor * protobuf_c_message_descriptor_get_field(const ProtobufCMessageDescriptor *desc, unsigned value)
Definition: protobuf-c.c:3454
const char * c_name
Definition: protobuf-c.h:532
Definition: protobuf-c.h:279
Definition: protobuf-c.h:468
PROTOBUF_C__API uint32_t protobuf_c_version_number(void)
Definition: protobuf-c.c:139
protobuf_c_boolean must_free_data
Definition: protobuf-c.h:478
Definition: protobuf-c.h:393
Definition: protobuf-c.h:314
const ProtobufCServiceDescriptor * descriptor
Definition: protobuf-c.h:725
Definition: protobuf-c.h:312
size_t alloced
Definition: protobuf-c.h:472
const char * name
Definition: protobuf-c.h:553
unsigned offset
Definition: protobuf-c.h:575
ProtobufCWireType
Definition: protobuf-c.h:325
const void * default_value
Definition: protobuf-c.h:591
ProtobufCLabel
Definition: protobuf-c.h:271
Definition: protobuf-c.h:723
uint32_t id
Definition: protobuf-c.h:556
Definition: protobuf-c.h:551
void * reserved2
Definition: protobuf-c.h:602
Definition: protobuf-c.h:305
Definition: protobuf-c.h:286
const char * name
Definition: protobuf-c.h:657
PROTOBUF_C__API void protobuf_c_message_free_unpacked(ProtobufCMessage *message, ProtobufCAllocator *allocator)
Definition: protobuf-c.c:3165
const unsigned * method_indices_by_name
Definition: protobuf-c.h:756
const ProtobufCIntRange * field_ranges
Definition: protobuf-c.h:681
const char * short_name
Definition: protobuf-c.h:746
Definition: protobuf-c.h:303
PROTOBUF_C__API void protobuf_c_buffer_simple_append(ProtobufCBuffer *buffer, size_t len, const unsigned char *data)
Definition: protobuf-c.c:185
Definition: protobuf-c.h:311
uint8_t * data
Definition: protobuf-c.h:395
const ProtobufCIntRange * value_ranges
Definition: protobuf-c.h:512
Definition: protobuf-c.h:298
Definition: protobuf-c.h:301
uint32_t tag
Definition: protobuf-c.h:699
PROTOBUF_C__API const char * protobuf_c_version(void)
Definition: protobuf-c.c:133
const char * c_name
Definition: protobuf-c.h:661
const ProtobufCMessageDescriptor * descriptor
Definition: protobuf-c.h:642
size_t len
Definition: protobuf-c.h:474
Definition: protobuf-c.h:306
ProtobufCType
Definition: protobuf-c.h:297
const ProtobufCFieldDescriptor * fields
Definition: protobuf-c.h:674
ProtobufCWireType wire_type
Definition: protobuf-c.h:701
void(* free)(void *allocator_data, void *pointer)
Definition: protobuf-c.h:380
const char * name
Definition: protobuf-c.h:744
Definition: protobuf-c.h:300
const char * name
Definition: protobuf-c.h:713
Definition: protobuf-c.h:309
Definition: protobuf-c.h:299
const ProtobufCMethodDescriptor * methods
Definition: protobuf-c.h:754
const char * package_name
Definition: protobuf-c.h:663
const ProtobufCEnumValue * values
Definition: protobuf-c.h:502
Definition: protobuf-c.h:310
Definition: protobuf-c.h:375
unsigned reserved_flags
Definition: protobuf-c.h:600
Definition: protobuf-c.h:254
PROTOBUF_C__API size_t protobuf_c_message_get_packed_size(const ProtobufCMessage *message)
Definition: protobuf-c.c:640
size_t len
Definition: protobuf-c.h:394
PROTOBUF_C__API size_t protobuf_c_message_pack_to_buffer(const ProtobufCMessage *message, ProtobufCBuffer *buffer)
Definition: protobuf-c.c:1823
PROTOBUF_C__API size_t protobuf_c_message_pack(const ProtobufCMessage *message, uint8_t *out)
Definition: protobuf-c.c:1369
Definition: protobuf-c.h:527
const ProtobufCEnumValueIndex * values_by_name
Definition: protobuf-c.h:507
PROTOBUF_C__API protobuf_c_boolean protobuf_c_message_check(const ProtobufCMessage *)
Definition: protobuf-c.c:3261
Definition: protobuf-c.h:615
Definition: protobuf-c.h:486
Definition: protobuf-c.h:640
uint32_t flags
Definition: protobuf-c.h:597
Definition: protobuf-c.h:304
const char * short_name
Definition: protobuf-c.h:659
const ProtobufCMessageDescriptor * output
Definition: protobuf-c.h:717
Definition: protobuf-c.h:739
const ProtobufCMessageDescriptor * input
Definition: protobuf-c.h:715
const char * package
Definition: protobuf-c.h:750
ProtobufCMessageUnknownField * unknown_fields
Definition: protobuf-c.h:646
unsigned n_value_ranges
Definition: protobuf-c.h:510
size_t sizeof_message
Definition: protobuf-c.h:669
unsigned quantifier_offset
Definition: protobuf-c.h:569
const char * c_name
Definition: protobuf-c.h:748
Definition: protobuf-c.h:313
ProtobufCFieldFlag
Definition: protobuf-c.h:252
Definition: protobuf-c.h:652
Definition: protobuf-c.h:308
const unsigned * fields_sorted_by_name
Definition: protobuf-c.h:676
Definition: protobuf-c.h:260
size_t len
Definition: protobuf-c.h:703
ProtobufCBuffer base
Definition: protobuf-c.h:470
unsigned n_value_names
Definition: protobuf-c.h:505
PROTOBUF_C__API const ProtobufCEnumValue * protobuf_c_enum_descriptor_get_value_by_name(const ProtobufCEnumDescriptor *desc, const char *name)
Definition: protobuf-c.c:3390
const void * descriptor
Definition: protobuf-c.h:588
unsigned n_methods
Definition: protobuf-c.h:752
unsigned n_fields
Definition: protobuf-c.h:672
uint32_t magic
Definition: protobuf-c.h:654
void * reserved1
Definition: protobuf-c.h:515
ProtobufCAllocator * allocator
Definition: protobuf-c.h:480
uint32_t magic
Definition: protobuf-c.h:488
const char * package_name
Definition: protobuf-c.h:497
uint8_t * data
Definition: protobuf-c.h:476
const char * name
Definition: protobuf-c.h:491
uint32_t magic
Definition: protobuf-c.h:741
ProtobufCType type
Definition: protobuf-c.h:562
Definition: protobuf-c.h:433
Definition: protobuf-c.h:697
Definition: protobuf-c.h:257
void * reserved2
Definition: protobuf-c.h:517
PROTOBUF_C__API const ProtobufCEnumValue * protobuf_c_enum_descriptor_get_value(const ProtobufCEnumDescriptor *desc, int value)
Definition: protobuf-c.c:3415
PROTOBUF_C__API const ProtobufCFieldDescriptor * protobuf_c_message_descriptor_get_field_by_name(const ProtobufCMessageDescriptor *desc, const char *name)
Definition: protobuf-c.c:3425
Definition: protobuf-c.h:541
PROTOBUF_C__API void protobuf_c_service_destroy(ProtobufCService *service)
Definition: protobuf-c.c:3382
unsigned n_values
Definition: protobuf-c.h:500
Definition: protobuf-c.h:273
void * reserved2
Definition: protobuf-c.h:689
PROTOBUF_C__API const ProtobufCMethodDescriptor * protobuf_c_service_descriptor_get_method_by_name(const ProtobufCServiceDescriptor *desc, const char *name)
Definition: protobuf-c.c:3464
const char * name
Definition: protobuf-c.h:543
void * reserved4
Definition: protobuf-c.h:521
unsigned n_field_ranges
Definition: protobuf-c.h:679
uint8_t * data
Definition: protobuf-c.h:705
void * reserved3
Definition: protobuf-c.h:519
const char * name
Definition: protobuf-c.h:529
unsigned index
Definition: protobuf-c.h:545
int protobuf_c_boolean
Definition: protobuf-c.h:366
int value
Definition: protobuf-c.h:535
const char * short_name
Definition: protobuf-c.h:493