PageRenderTime 72ms CodeModel.GetById 39ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://github.com/sapo/sapo-broker
C Header | 486 lines | 291 code | 59 blank | 136 comment | 8 complexity | 4c1c629b28b4f131106a390d0016b9e9 MD5 | raw file
Possible License(s): GPL-3.0, BSD-3-Clause, CC-BY-SA-3.0, Apache-2.0
  1. /* --- protobuf-c.h: public protobuf c runtime api --- */
  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. #ifndef __PROTOBUF_C_RUNTIME_H_
  17. #define __PROTOBUF_C_RUNTIME_H_
  18. #include <stddef.h>
  19. #include <assert.h>
  20. #ifdef __cplusplus
  21. # define PROTOBUF_C_BEGIN_DECLS extern "C" {
  22. # define PROTOBUF_C_END_DECLS }
  23. #else
  24. # define PROTOBUF_C_BEGIN_DECLS
  25. # define PROTOBUF_C_END_DECLS
  26. #endif
  27. #if !defined(PROTOBUF_C_NO_DEPRECATED) && (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1))
  28. #define PROTOBUF_C_DEPRECATED __attribute__((__deprecated__))
  29. #else
  30. #define PROTOBUF_C_DEPRECATED
  31. #endif
  32. /* Define int32_t, int64_t, uint32_t, uint64_t, uint8_t.
  33. Usually, just include <inttypes.h> to do the work.
  34. XXX: should we use stdint.h?
  35. */
  36. #ifndef PROTOBUF_C_SKIP_INTTYPES_H
  37. # if defined(_MSC_VER)
  38. /* On windows, in ms visual studio, define the types ourselves */
  39. # define int32_t signed __int32
  40. # define INT32_MIN _I32_MIN
  41. # define INT32_MAX _I32_MAX
  42. # define uint32_t unsigned __int32
  43. # define UINT32_MIN _UI32_MIN
  44. # define UINT32_MAX _UI32_MAX
  45. # define int64_t signed __int64
  46. # define INT64_MIN _I64_MIN
  47. # define INT64_MAX _I64_MAX
  48. # define uint64_t unsigned __int64
  49. # define UINT64_MIN _UI64_MIN
  50. # define UINT64_MAX _UI64_MAX
  51. # define uint8_t unsigned char
  52. # else
  53. /* Use the system inttypes.h */
  54. # include <inttypes.h>
  55. # endif
  56. #endif
  57. #if defined(_WIN32) && defined(PROTOBUF_C_USE_SHARED_LIB)
  58. # ifdef PROTOBUF_C_EXPORT
  59. # define PROTOBUF_C_API __declspec(dllexport)
  60. # else
  61. # define PROTOBUF_C_API __declspec(dllimport)
  62. #endif
  63. #else
  64. # define PROTOBUF_C_API
  65. #endif
  66. PROTOBUF_C_BEGIN_DECLS
  67. typedef enum
  68. {
  69. PROTOBUF_C_LABEL_REQUIRED,
  70. PROTOBUF_C_LABEL_OPTIONAL,
  71. PROTOBUF_C_LABEL_REPEATED
  72. } ProtobufCLabel;
  73. typedef enum
  74. {
  75. PROTOBUF_C_TYPE_INT32,
  76. PROTOBUF_C_TYPE_SINT32,
  77. PROTOBUF_C_TYPE_SFIXED32,
  78. PROTOBUF_C_TYPE_INT64,
  79. PROTOBUF_C_TYPE_SINT64,
  80. PROTOBUF_C_TYPE_SFIXED64,
  81. PROTOBUF_C_TYPE_UINT32,
  82. PROTOBUF_C_TYPE_FIXED32,
  83. PROTOBUF_C_TYPE_UINT64,
  84. PROTOBUF_C_TYPE_FIXED64,
  85. PROTOBUF_C_TYPE_FLOAT,
  86. PROTOBUF_C_TYPE_DOUBLE,
  87. PROTOBUF_C_TYPE_BOOL,
  88. PROTOBUF_C_TYPE_ENUM,
  89. PROTOBUF_C_TYPE_STRING,
  90. PROTOBUF_C_TYPE_BYTES,
  91. //PROTOBUF_C_TYPE_GROUP, // NOT SUPPORTED
  92. PROTOBUF_C_TYPE_MESSAGE,
  93. } ProtobufCType;
  94. typedef int protobuf_c_boolean;
  95. #define PROTOBUF_C_OFFSETOF(struct, member) offsetof(struct, member)
  96. #define PROTOBUF_C_ASSERT(condition) assert(condition)
  97. #define PROTOBUF_C_ASSERT_NOT_REACHED() assert(0)
  98. typedef struct _ProtobufCBinaryData ProtobufCBinaryData;
  99. struct _ProtobufCBinaryData
  100. {
  101. size_t len;
  102. uint8_t *data;
  103. };
  104. typedef struct _ProtobufCIntRange ProtobufCIntRange; /* private */
  105. /* --- memory management --- */
  106. typedef struct _ProtobufCAllocator ProtobufCAllocator;
  107. struct _ProtobufCAllocator
  108. {
  109. void *(*alloc)(void *allocator_data, size_t size);
  110. void (*free)(void *allocator_data, void *pointer);
  111. void *(*tmp_alloc)(void *allocator_data, size_t size);
  112. unsigned max_alloca;
  113. void *allocator_data;
  114. };
  115. /* This is a configurable allocator.
  116. * By default, it uses the system allocator (meaning malloc() and free()).
  117. * This is typically changed to adapt to frameworks that provide
  118. * some nonstandard allocation functions.
  119. *
  120. * NOTE: you may modify this allocator.
  121. */
  122. extern PROTOBUF_C_API ProtobufCAllocator protobuf_c_default_allocator; /* settable */
  123. /* This is the system allocator, meaning it uses malloc() and free().
  124. *
  125. * NOTE: please do NOT modify this allocator.
  126. */
  127. extern PROTOBUF_C_API ProtobufCAllocator protobuf_c_system_allocator; /* use malloc, free etc */
  128. /* This is the function that our default allocators call when they
  129. run out-of-memory. The default behavior of this function is to
  130. terminate your program. */
  131. extern PROTOBUF_C_API void (*protobuf_c_out_of_memory) (void);
  132. /* --- append-only data buffer --- */
  133. typedef struct _ProtobufCBuffer ProtobufCBuffer;
  134. struct _ProtobufCBuffer
  135. {
  136. void (*append)(ProtobufCBuffer *buffer,
  137. size_t len,
  138. const uint8_t *data);
  139. };
  140. /* --- enums --- */
  141. typedef struct _ProtobufCEnumValue ProtobufCEnumValue;
  142. typedef struct _ProtobufCEnumValueIndex ProtobufCEnumValueIndex;
  143. typedef struct _ProtobufCEnumDescriptor ProtobufCEnumDescriptor;
  144. /* ProtobufCEnumValue: this represents a single value of
  145. * an enumeration.
  146. * 'name' is the string identifying this value, as given in the .proto file.
  147. * 'c_name' is the full name of the C enumeration value.
  148. * 'value' is the number assigned to this value, as given in the .proto file.
  149. */
  150. struct _ProtobufCEnumValue
  151. {
  152. const char *name;
  153. const char *c_name;
  154. int value;
  155. };
  156. /* ProtobufCEnumDescriptor: the represents the enum as a whole,
  157. * with all its values.
  158. * 'magic' is a code we check to ensure that the api is used correctly.
  159. * 'name' is the qualified name (e.g. "namespace.Type").
  160. * 'short_name' is the unqualified name ("Type"), as given in the .proto file.
  161. * 'package_name' is the '.'-separated namespace
  162. * 'n_values' is the number of distinct values.
  163. * 'values' is the array of distinct values.
  164. * 'n_value_names' number of named values (including aliases).
  165. * 'value_names' are the named values (including aliases).
  166. *
  167. * The rest of the values are private essentially.
  168. *
  169. * see also: Use protobuf_c_enum_descriptor_get_value_by_name()
  170. * and protobuf_c_enum_descriptor_get_value() to efficiently
  171. * lookup values in the descriptor.
  172. */
  173. struct _ProtobufCEnumDescriptor
  174. {
  175. uint32_t magic;
  176. const char *name;
  177. const char *short_name;
  178. const char *c_name;
  179. const char *package_name;
  180. /* sorted by value */
  181. unsigned n_values;
  182. const ProtobufCEnumValue *values;
  183. /* sorted by name */
  184. unsigned n_value_names;
  185. const ProtobufCEnumValueIndex *values_by_name;
  186. /* value-ranges, for faster lookups by number */
  187. unsigned n_value_ranges;
  188. const ProtobufCIntRange *value_ranges;
  189. void *reserved1;
  190. void *reserved2;
  191. void *reserved3;
  192. void *reserved4;
  193. };
  194. /* --- messages --- */
  195. typedef struct _ProtobufCMessageDescriptor ProtobufCMessageDescriptor;
  196. typedef struct _ProtobufCFieldDescriptor ProtobufCFieldDescriptor;
  197. typedef struct _ProtobufCMessage ProtobufCMessage;
  198. typedef void (*ProtobufCMessageInit)(ProtobufCMessage *);
  199. /* ProtobufCFieldDescriptor: description of a single field
  200. * in a message.
  201. * 'name' is the name of the field, as given in the .proto file.
  202. * 'id' is the code representing the field, as given in the .proto file.
  203. * 'label' is one of PROTOBUF_C_LABEL_{REQUIRED,OPTIONAL,REPEATED}
  204. * 'type' is the type of field.
  205. * 'quantifier_offset' is the offset in bytes into the message's C structure
  206. * for this member's "has_MEMBER" field (for optional members) or
  207. * "n_MEMBER" field (for repeated members).
  208. * 'offset' is the offset in bytes into the message's C structure
  209. * for the member itself.
  210. * 'descriptor' is a pointer to a ProtobufC{Enum,Message}Descriptor
  211. * if type is PROTOBUF_C_TYPE_{ENUM,MESSAGE} respectively,
  212. * otherwise NULL.
  213. * 'default_value' is a pointer to a default value for this field,
  214. * where allowed.
  215. */
  216. struct _ProtobufCFieldDescriptor
  217. {
  218. const char *name;
  219. uint32_t id;
  220. ProtobufCLabel label;
  221. ProtobufCType type;
  222. unsigned quantifier_offset;
  223. unsigned offset;
  224. const void *descriptor; /* for MESSAGE and ENUM types */
  225. const void *default_value; /* or NULL if no default-value */
  226. protobuf_c_boolean packed;
  227. unsigned reserved_flags;
  228. void *reserved2;
  229. void *reserved3;
  230. };
  231. /* ProtobufCMessageDescriptor: description of a message.
  232. *
  233. * 'magic' is a code we check to ensure that the api is used correctly.
  234. * 'name' is the qualified name (e.g. "namespace.Type").
  235. * 'short_name' is the unqualified name ("Type"), as given in the .proto file.
  236. * 'c_name' is the c-formatted name of the structure
  237. * 'package_name' is the '.'-separated namespace
  238. * 'sizeof_message' is the size in bytes of the C structure
  239. * representing an instance of this type of message.
  240. * 'n_fields' is the number of known fields in this message.
  241. * 'fields' is the fields sorted by id number.
  242. * 'fields_sorted_by_name', 'n_field_ranges' and 'field_ranges'
  243. * are used for looking up fields by name and id. (private)
  244. */
  245. struct _ProtobufCMessageDescriptor
  246. {
  247. uint32_t magic;
  248. const char *name;
  249. const char *short_name;
  250. const char *c_name;
  251. const char *package_name;
  252. size_t sizeof_message;
  253. /* sorted by field-id */
  254. unsigned n_fields;
  255. const ProtobufCFieldDescriptor *fields;
  256. const unsigned *fields_sorted_by_name;
  257. /* ranges, optimization for looking up fields */
  258. unsigned n_field_ranges;
  259. const ProtobufCIntRange *field_ranges;
  260. ProtobufCMessageInit message_init;
  261. void *reserved1;
  262. void *reserved2;
  263. void *reserved3;
  264. };
  265. /* ProtobufCMessage: an instance of a message.
  266. *
  267. * ProtobufCMessage is sort-of a lightweight
  268. * base-class for all messages.
  269. *
  270. * In particular, ProtobufCMessage doesn't have
  271. * any allocation policy associated with it.
  272. * That's because it is common to create ProtobufCMessage's
  273. * on the stack. In fact, we that's what we recommend
  274. * for sending messages (because if you just allocate from the
  275. * stack, then you can't really have a memory leak).
  276. *
  277. * This means that functions like protobuf_c_message_unpack()
  278. * which return a ProtobufCMessage must be paired
  279. * with a free function, like protobuf_c_message_free_unpacked().
  280. *
  281. * 'descriptor' gives the locations and types of the members of message
  282. * 'n_unknown_fields' is the number of fields we didn't recognize.
  283. * 'unknown_fields' are fields we didn't recognize.
  284. */
  285. typedef struct _ProtobufCMessageUnknownField ProtobufCMessageUnknownField;
  286. struct _ProtobufCMessage
  287. {
  288. const ProtobufCMessageDescriptor *descriptor;
  289. unsigned n_unknown_fields;
  290. ProtobufCMessageUnknownField *unknown_fields;
  291. };
  292. #define PROTOBUF_C_MESSAGE_INIT(descriptor) { descriptor, 0, NULL }
  293. /* To pack a message: you have two options:
  294. (1) you can compute the size of the message
  295. using protobuf_c_message_get_packed_size()
  296. then pass protobuf_c_message_pack() a buffer of
  297. that length.
  298. (2) Provide a virtual buffer (a ProtobufCBuffer) to
  299. accept data as we scan through it.
  300. */
  301. PROTOBUF_C_API size_t protobuf_c_message_get_packed_size(const ProtobufCMessage *message);
  302. PROTOBUF_C_API size_t protobuf_c_message_pack (const ProtobufCMessage *message,
  303. uint8_t *out);
  304. PROTOBUF_C_API size_t protobuf_c_message_pack_to_buffer (const ProtobufCMessage *message,
  305. ProtobufCBuffer *buffer);
  306. PROTOBUF_C_API ProtobufCMessage *
  307. protobuf_c_message_unpack (const ProtobufCMessageDescriptor *,
  308. ProtobufCAllocator *allocator,
  309. size_t len,
  310. const uint8_t *data);
  311. PROTOBUF_C_API void protobuf_c_message_free_unpacked (ProtobufCMessage *message,
  312. ProtobufCAllocator *allocator);
  313. /* WARNING: 'message' must be a block of memory
  314. of size descriptor->sizeof_message. */
  315. PROTOBUF_C_API void protobuf_c_message_init (const ProtobufCMessageDescriptor *,
  316. void *message);
  317. /* --- services --- */
  318. typedef struct _ProtobufCMethodDescriptor ProtobufCMethodDescriptor;
  319. typedef struct _ProtobufCServiceDescriptor ProtobufCServiceDescriptor;
  320. struct _ProtobufCMethodDescriptor
  321. {
  322. const char *name;
  323. const ProtobufCMessageDescriptor *input;
  324. const ProtobufCMessageDescriptor *output;
  325. };
  326. struct _ProtobufCServiceDescriptor
  327. {
  328. uint32_t magic;
  329. const char *name;
  330. const char *short_name;
  331. const char *c_name;
  332. const char *package;
  333. unsigned n_methods;
  334. const ProtobufCMethodDescriptor *methods; /* in order from .proto file */
  335. const unsigned *method_indices_by_name;
  336. };
  337. typedef struct _ProtobufCService ProtobufCService;
  338. typedef void (*ProtobufCClosure)(const ProtobufCMessage *message,
  339. void *closure_data);
  340. struct _ProtobufCService
  341. {
  342. const ProtobufCServiceDescriptor *descriptor;
  343. void (*invoke)(ProtobufCService *service,
  344. unsigned method_index,
  345. const ProtobufCMessage *input,
  346. ProtobufCClosure closure,
  347. void *closure_data);
  348. void (*destroy) (ProtobufCService *service);
  349. };
  350. void protobuf_c_service_destroy (ProtobufCService *);
  351. /* --- querying the descriptors --- */
  352. PROTOBUF_C_API const ProtobufCEnumValue *
  353. protobuf_c_enum_descriptor_get_value_by_name
  354. (const ProtobufCEnumDescriptor *desc,
  355. const char *name);
  356. PROTOBUF_C_API const ProtobufCEnumValue *
  357. protobuf_c_enum_descriptor_get_value
  358. (const ProtobufCEnumDescriptor *desc,
  359. int value);
  360. PROTOBUF_C_API const ProtobufCFieldDescriptor *
  361. protobuf_c_message_descriptor_get_field_by_name
  362. (const ProtobufCMessageDescriptor *desc,
  363. const char *name);
  364. PROTOBUF_C_API const ProtobufCFieldDescriptor *
  365. protobuf_c_message_descriptor_get_field
  366. (const ProtobufCMessageDescriptor *desc,
  367. unsigned value);
  368. PROTOBUF_C_API const ProtobufCMethodDescriptor *
  369. protobuf_c_service_descriptor_get_method_by_name
  370. (const ProtobufCServiceDescriptor *desc,
  371. const char *name);
  372. /* --- wire format enums --- */
  373. typedef enum
  374. {
  375. PROTOBUF_C_WIRE_TYPE_VARINT,
  376. PROTOBUF_C_WIRE_TYPE_64BIT,
  377. PROTOBUF_C_WIRE_TYPE_LENGTH_PREFIXED,
  378. PROTOBUF_C_WIRE_TYPE_START_GROUP, /* unsupported */
  379. PROTOBUF_C_WIRE_TYPE_END_GROUP, /* unsupported */
  380. PROTOBUF_C_WIRE_TYPE_32BIT
  381. } ProtobufCWireType;
  382. /* --- unknown message fields --- */
  383. struct _ProtobufCMessageUnknownField
  384. {
  385. uint32_t tag;
  386. ProtobufCWireType wire_type;
  387. size_t len;
  388. uint8_t *data;
  389. };
  390. /* --- extra (superfluous) api: trivial buffer --- */
  391. typedef struct _ProtobufCBufferSimple ProtobufCBufferSimple;
  392. struct _ProtobufCBufferSimple
  393. {
  394. ProtobufCBuffer base;
  395. size_t alloced;
  396. size_t len;
  397. uint8_t *data;
  398. protobuf_c_boolean must_free_data;
  399. };
  400. #define PROTOBUF_C_BUFFER_SIMPLE_INIT(array_of_bytes) \
  401. { { protobuf_c_buffer_simple_append }, \
  402. sizeof(array_of_bytes), 0, (array_of_bytes), 0 }
  403. #define PROTOBUF_C_BUFFER_SIMPLE_CLEAR(simp_buf) \
  404. do { if ((simp_buf)->must_free_data) \
  405. protobuf_c_default_allocator.free (&protobuf_c_default_allocator.allocator_data, (simp_buf)->data); } while (0)
  406. typedef enum
  407. {
  408. PROTOBUF_C_CTYPE_INT32,
  409. PROTOBUF_C_CTYPE_UINT32,
  410. PROTOBUF_C_CTYPE_INT64,
  411. PROTOBUF_C_CTYPE_UINT64,
  412. PROTOBUF_C_CTYPE_FLOAT,
  413. PROTOBUF_C_CTYPE_DOUBLE,
  414. PROTOBUF_C_CTYPE_BOOL,
  415. PROTOBUF_C_CTYPE_ENUM,
  416. PROTOBUF_C_CTYPE_STRING,
  417. PROTOBUF_C_CTYPE_BYTES,
  418. PROTOBUF_C_CTYPE_MESSAGE,
  419. } ProtobufCCType;
  420. extern ProtobufCCType protobuf_c_type_to_ctype (ProtobufCType type);
  421. #define protobuf_c_type_to_ctype(type) \
  422. ((ProtobufCCType)(protobuf_c_type_to_ctype_array[(type)]))
  423. /* ====== private ====== */
  424. #include "protobuf-c-private.h"
  425. PROTOBUF_C_END_DECLS
  426. #endif /* __PROTOBUF_C_RUNTIME_H_ */