PageRenderTime 61ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

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

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