PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

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

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