/*1* Copyright (c) 2008-2025, Dave Benson and the protobuf-c authors.2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions are6* met:7*8* * Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10*11* * Redistributions in binary form must reproduce the above12* copyright notice, this list of conditions and the following disclaimer13* in the documentation and/or other materials provided with the14* distribution.15*16* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS17* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT18* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR19* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT20* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,21* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT22* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,23* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY24* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT25* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE26* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.27*/2829/*! \file30* \mainpage Introduction31*32* This is [protobuf-c], a C implementation of [Protocol Buffers].33*34* This file defines the public API for the `libprotobuf-c` support library.35* This API includes interfaces that can be used directly by client code as well36* as the interfaces used by the code generated by the `protoc-gen-c` compiler37* plugin.38*39* The `libprotobuf-c` support library performs the actual serialization and40* deserialization of Protocol Buffers messages. It interacts with structures,41* definitions, and metadata generated by the `protoc-gen-c` compiler plugin42* from .proto files.43*44* \authors Dave Benson and the `protobuf-c` authors.45*46* \copyright 2008-2025. Licensed under the terms of the [BSD-2-Clause] license.47*48* [protobuf-c]: https://github.com/protobuf-c/protobuf-c49* [Protocol Buffers]: https://developers.google.com/protocol-buffers/50* [BSD-2-Clause]: http://opensource.org/licenses/BSD-2-Clause51*52* \page gencode Generated Code53*54* For each enum, we generate a C enum. For each message, we generate a C55* structure which can be cast to a `ProtobufCMessage`.56*57* For each enum and message, we generate a descriptor object that allows us to58* implement a kind of reflection on the structures.59*60* First, some naming conventions:61*62* - The name of the type for enums and messages and services is camel case63* (meaning WordsAreCrammedTogether) except that double underscores are used64* to delimit scopes. For example, the following `.proto` file:65*66~~~{.proto}67package foo.bar;68message BazBah {69optional int32 val = 1;70}71~~~72*73* would generate a C type `Foo__Bar__BazBah`.74*75* - Identifiers for functions and globals are all lowercase, with camel case76* words separated by single underscores. For example, one of the function77* prototypes generated by `protoc-gen-c` for the above example:78*79~~~{.c}80Foo__Bar__BazBah *81foo__bar__baz_bah__unpack82(ProtobufCAllocator *allocator,83size_t len,84const uint8_t *data);85~~~86*87* - Identifiers for enum values contain an uppercase prefix which embeds the88* package name and the enum type name.89*90* - A double underscore is used to separate further components of identifier91* names.92*93* For example, in the name of the unpack function above, the package name94* `foo.bar` has become `foo__bar`, the message name BazBah has become95* `baz_bah`, and the method name is `unpack`. These are all joined with double96* underscores to form the C identifier `foo__bar__baz_bah__unpack`.97*98* We also generate descriptor objects for messages and enums. These are99* declared in the `.pb-c.h` files:100*101~~~{.c}102extern const ProtobufCMessageDescriptor foo__bar__baz_bah__descriptor;103~~~104*105* The message structures all begin with `ProtobufCMessageDescriptor *` which is106* sufficient to allow them to be cast to `ProtobufCMessage`.107*108* For each message defined in a `.proto` file, we generate a number of109* functions and macros. Each function name contains a prefix based on the110* package name and message name in order to make it a unique C identifier.111*112* - `INIT`. Statically initializes a message object, initializing its113* descriptor and setting its fields to default values. Uninitialized114* messages cannot be processed by the protobuf-c library.115*116~~~{.c}117#define FOO__BAR__BAZ_BAH__INIT \118{ PROTOBUF_C_MESSAGE_INIT (&foo__bar__baz_bah__descriptor), 0 }119~~~120* - `init()`. Initializes a message object, initializing its descriptor and121* setting its fields to default values. Uninitialized messages cannot be122* processed by the protobuf-c library.123*124~~~{.c}125void foo__bar__baz_bah__init126(Foo__Bar__BazBah *message);127~~~128* - `unpack()`. Unpacks data for a particular message format. Note that the129* `allocator` parameter is usually `NULL` to indicate that the system's130* `malloc()` and `free()` functions should be used for dynamically allocating131* memory.132*133~~~{.c}134Foo__Bar__BazBah *135foo__bar__baz_bah__unpack136(ProtobufCAllocator *allocator,137size_t len,138const uint8_t *data);139~~~140*141* - `free_unpacked()`. Frees a message object obtained with the `unpack()`142* method. Freeing `NULL` is allowed (the same as with `free()`).143*144~~~{.c}145void foo__bar__baz_bah__free_unpacked146(Foo__Bar__BazBah *message,147ProtobufCAllocator *allocator);148~~~149*150* - `get_packed_size()`. Calculates the length in bytes of the serialized151* representation of the message object.152*153~~~{.c}154size_t foo__bar__baz_bah__get_packed_size155(const Foo__Bar__BazBah *message);156~~~157*158* - `pack()`. Pack a message object into a preallocated buffer. Assumes that159* the buffer is large enough. (Use `get_packed_size()` first.)160*161~~~{.c}162size_t foo__bar__baz_bah__pack163(const Foo__Bar__BazBah *message,164uint8_t *out);165~~~166*167* - `pack_to_buffer()`. Packs a message into a "virtual buffer". This is an168* object which defines an "append bytes" callback to consume data as it is169* serialized.170*171~~~{.c}172size_t foo__bar__baz_bah__pack_to_buffer173(const Foo__Bar__BazBah *message,174ProtobufCBuffer *buffer);175~~~176*177* \page pack Packing and unpacking messages178*179* To pack a message, first compute the packed size of the message with180* protobuf_c_message_get_packed_size(), then allocate a buffer of at least181* that size, then call protobuf_c_message_pack().182*183* Alternatively, a message can be serialized without calculating the final size184* first. Use the protobuf_c_message_pack_to_buffer() function and provide a185* ProtobufCBuffer object which implements an "append" method that consumes186* data.187*188* To unpack a message, call the protobuf_c_message_unpack() function. The189* result can be cast to an object of the type that matches the descriptor for190* the message.191*192* The result of unpacking a message should be freed with193* protobuf_c_message_free_unpacked().194*/195196#ifndef PROTOBUF_C_H197#define PROTOBUF_C_H198199#include <assert.h>200#include <limits.h>201#include <stddef.h>202#include <inttypes.h> /* stdint.h not present on older systems */203204#ifdef __cplusplus205# define PROTOBUF_C__BEGIN_DECLS extern "C" {206# define PROTOBUF_C__END_DECLS }207#else208# define PROTOBUF_C__BEGIN_DECLS209# define PROTOBUF_C__END_DECLS210#endif211212PROTOBUF_C__BEGIN_DECLS213214#if defined(_WIN32) && defined(PROTOBUF_C_USE_SHARED_LIB)215# ifdef PROTOBUF_C_EXPORT216# define PROTOBUF_C__API __declspec(dllexport)217# else218# define PROTOBUF_C__API __declspec(dllimport)219# endif220#else221# define PROTOBUF_C__API222#endif223224#if !defined(PROTOBUF_C__NO_DEPRECATED) && \225((__GNUC__ > 3) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1))226# define PROTOBUF_C__DEPRECATED __attribute__((__deprecated__))227#else228# define PROTOBUF_C__DEPRECATED229#endif230231#ifndef PROTOBUF_C__FORCE_ENUM_TO_BE_INT_SIZE232#define PROTOBUF_C__FORCE_ENUM_TO_BE_INT_SIZE(enum_name) \233, _##enum_name##_IS_INT_SIZE = INT_MAX234#endif235236#define PROTOBUF_C__SERVICE_DESCRIPTOR_MAGIC 0x14159bc3237#define PROTOBUF_C__MESSAGE_DESCRIPTOR_MAGIC 0x28aaeef9238#define PROTOBUF_C__ENUM_DESCRIPTOR_MAGIC 0x114315af239240/* Empty string used for initializers */241#if defined(_WIN32) && defined(PROTOBUF_C_USE_SHARED_LIB)242static const char protobuf_c_empty_string[] = "";243#else244extern const char protobuf_c_empty_string[];245#endif246247/**248* \defgroup api Public API249*250* This is the public API for `libprotobuf-c`. These interfaces are stable and251* subject to Semantic Versioning guarantees.252*253* @{254*/255256/**257* Values for the `flags` word in `ProtobufCFieldDescriptor`.258*/259typedef enum {260/** Set if the field is repeated and marked with the `packed` option. */261PROTOBUF_C_FIELD_FLAG_PACKED = (1 << 0),262263/** Set if the field is marked with the `deprecated` option. */264PROTOBUF_C_FIELD_FLAG_DEPRECATED = (1 << 1),265266/** Set if the field is a member of a oneof (union). */267PROTOBUF_C_FIELD_FLAG_ONEOF = (1 << 2),268} ProtobufCFieldFlag;269270/**271* Message field rules.272*273* \see [Defining A Message Type] in the Protocol Buffers documentation.274*275* [Defining A Message Type]:276* https://developers.google.com/protocol-buffers/docs/proto#simple277*/278typedef enum {279/** A well-formed message must have exactly one of this field. */280PROTOBUF_C_LABEL_REQUIRED,281282/**283* A well-formed message can have zero or one of this field (but not284* more than one).285*/286PROTOBUF_C_LABEL_OPTIONAL,287288/**289* This field can be repeated any number of times (including zero) in a290* well-formed message. The order of the repeated values will be291* preserved.292*/293PROTOBUF_C_LABEL_REPEATED,294295/**296* This field has no label. This is valid only in proto3 and is297* equivalent to OPTIONAL but no "has" quantifier will be consulted.298*/299PROTOBUF_C_LABEL_NONE,300} ProtobufCLabel;301302/**303* Field value types.304*305* \see [Scalar Value Types] in the Protocol Buffers documentation.306*307* [Scalar Value Types]:308* https://developers.google.com/protocol-buffers/docs/proto#scalar309*/310typedef enum {311PROTOBUF_C_TYPE_INT32, /**< int32 */312PROTOBUF_C_TYPE_SINT32, /**< signed int32 */313PROTOBUF_C_TYPE_SFIXED32, /**< signed int32 (4 bytes) */314PROTOBUF_C_TYPE_INT64, /**< int64 */315PROTOBUF_C_TYPE_SINT64, /**< signed int64 */316PROTOBUF_C_TYPE_SFIXED64, /**< signed int64 (8 bytes) */317PROTOBUF_C_TYPE_UINT32, /**< unsigned int32 */318PROTOBUF_C_TYPE_FIXED32, /**< unsigned int32 (4 bytes) */319PROTOBUF_C_TYPE_UINT64, /**< unsigned int64 */320PROTOBUF_C_TYPE_FIXED64, /**< unsigned int64 (8 bytes) */321PROTOBUF_C_TYPE_FLOAT, /**< float */322PROTOBUF_C_TYPE_DOUBLE, /**< double */323PROTOBUF_C_TYPE_BOOL, /**< boolean */324PROTOBUF_C_TYPE_ENUM, /**< enumerated type */325PROTOBUF_C_TYPE_STRING, /**< UTF-8 or ASCII string */326PROTOBUF_C_TYPE_BYTES, /**< arbitrary byte sequence */327PROTOBUF_C_TYPE_MESSAGE, /**< nested message */328} ProtobufCType;329330/**331* Field wire types.332*333* \see [Message Structure] in the Protocol Buffers documentation.334*335* [Message Structure]:336* https://developers.google.com/protocol-buffers/docs/encoding#structure337*/338typedef enum {339PROTOBUF_C_WIRE_TYPE_VARINT = 0,340PROTOBUF_C_WIRE_TYPE_64BIT = 1,341PROTOBUF_C_WIRE_TYPE_LENGTH_PREFIXED = 2,342/* "Start group" and "end group" wire types are unsupported. */343PROTOBUF_C_WIRE_TYPE_32BIT = 5,344} ProtobufCWireType;345346struct ProtobufCAllocator;347struct ProtobufCBinaryData;348struct ProtobufCBuffer;349struct ProtobufCBufferSimple;350struct ProtobufCEnumDescriptor;351struct ProtobufCEnumValue;352struct ProtobufCEnumValueIndex;353struct ProtobufCFieldDescriptor;354struct ProtobufCIntRange;355struct ProtobufCMessage;356struct ProtobufCMessageDescriptor;357struct ProtobufCMessageUnknownField;358struct ProtobufCMethodDescriptor;359struct ProtobufCService;360struct ProtobufCServiceDescriptor;361362typedef struct ProtobufCAllocator ProtobufCAllocator;363typedef struct ProtobufCBinaryData ProtobufCBinaryData;364typedef struct ProtobufCBuffer ProtobufCBuffer;365typedef struct ProtobufCBufferSimple ProtobufCBufferSimple;366typedef struct ProtobufCEnumDescriptor ProtobufCEnumDescriptor;367typedef struct ProtobufCEnumValue ProtobufCEnumValue;368typedef struct ProtobufCEnumValueIndex ProtobufCEnumValueIndex;369typedef struct ProtobufCFieldDescriptor ProtobufCFieldDescriptor;370typedef struct ProtobufCIntRange ProtobufCIntRange;371typedef struct ProtobufCMessage ProtobufCMessage;372typedef struct ProtobufCMessageDescriptor ProtobufCMessageDescriptor;373typedef struct ProtobufCMessageUnknownField ProtobufCMessageUnknownField;374typedef struct ProtobufCMethodDescriptor ProtobufCMethodDescriptor;375typedef struct ProtobufCService ProtobufCService;376typedef struct ProtobufCServiceDescriptor ProtobufCServiceDescriptor;377378/** Boolean type. */379typedef int protobuf_c_boolean;380381typedef void (*ProtobufCClosure)(const ProtobufCMessage *, void *closure_data);382typedef void (*ProtobufCMessageInit)(ProtobufCMessage *);383typedef void (*ProtobufCServiceDestroy)(ProtobufCService *);384385/**386* Structure for defining a custom memory allocator.387*/388struct ProtobufCAllocator {389/** Function to allocate memory. */390void *(*alloc)(void *allocator_data, size_t size);391392/** Function to free memory. */393void (*free)(void *allocator_data, void *pointer);394395/** Opaque pointer passed to `alloc` and `free` functions. */396void *allocator_data;397};398399/**400* Structure for the protobuf `bytes` scalar type.401*402* The data contained in a `ProtobufCBinaryData` is an arbitrary sequence of403* bytes. It may contain embedded `NUL` characters and is not required to be404* `NUL`-terminated.405*/406struct ProtobufCBinaryData {407size_t len; /**< Number of bytes in the `data` field. */408uint8_t *data; /**< Data bytes. */409};410411/**412* Structure for defining a virtual append-only buffer. Used by413* protobuf_c_message_pack_to_buffer() to abstract the consumption of serialized414* bytes.415*416* `ProtobufCBuffer` "subclasses" may be defined on the stack. For example, to417* write to a `FILE` object:418*419~~~{.c}420typedef struct {421ProtobufCBuffer base;422FILE *fp;423} BufferAppendToFile;424425static void426my_buffer_file_append(ProtobufCBuffer *buffer,427size_t len,428const uint8_t *data)429{430BufferAppendToFile *file_buf = (BufferAppendToFile *) buffer;431fwrite(data, len, 1, file_buf->fp); // XXX: No error handling!432}433~~~434*435* To use this new type of ProtobufCBuffer, it could be called as follows:436*437~~~{.c}438...439BufferAppendToFile tmp = {0};440tmp.base.append = my_buffer_file_append;441tmp.fp = fp;442protobuf_c_message_pack_to_buffer(&message, &tmp);443...444~~~445*/446struct ProtobufCBuffer {447/** Append function. Consumes the `len` bytes stored at `data`. */448void (*append)(ProtobufCBuffer *buffer,449size_t len,450const uint8_t *data);451};452453/**454* Simple buffer "subclass" of `ProtobufCBuffer`.455*456* A `ProtobufCBufferSimple` object is declared on the stack and uses a457* scratch buffer provided by the user for the initial allocation. It performs458* exponential resizing, using dynamically allocated memory. A459* `ProtobufCBufferSimple` object can be created and used as follows:460*461~~~{.c}462uint8_t pad[128];463ProtobufCBufferSimple simple = PROTOBUF_C_BUFFER_SIMPLE_INIT(pad);464ProtobufCBuffer *buffer = (ProtobufCBuffer *) &simple;465~~~466*467* `buffer` can now be used with `protobuf_c_message_pack_to_buffer()`. Once a468* message has been serialized to a `ProtobufCBufferSimple` object, the469* serialized data bytes can be accessed from the `.data` field.470*471* To free the memory allocated by a `ProtobufCBufferSimple` object, if any,472* call PROTOBUF_C_BUFFER_SIMPLE_CLEAR() on the object, for example:473*474~~~{.c}475PROTOBUF_C_BUFFER_SIMPLE_CLEAR(&simple);476~~~477*478* \see PROTOBUF_C_BUFFER_SIMPLE_INIT479* \see PROTOBUF_C_BUFFER_SIMPLE_CLEAR480*/481struct ProtobufCBufferSimple {482/** "Base class". */483ProtobufCBuffer base;484/** Number of bytes allocated in `data`. */485size_t alloced;486/** Number of bytes currently stored in `data`. */487size_t len;488/** Data bytes. */489uint8_t *data;490/** Whether `data` must be freed. */491protobuf_c_boolean must_free_data;492/** Allocator to use. May be NULL to indicate the system allocator. */493ProtobufCAllocator *allocator;494};495496/**497* Describes an enumeration as a whole, with all of its values.498*/499struct ProtobufCEnumDescriptor {500/** Magic value checked to ensure that the API is used correctly. */501uint32_t magic;502503/** The qualified name (e.g., "namespace.Type"). */504const char *name;505/** The unqualified name as given in the .proto file (e.g., "Type"). */506const char *short_name;507/** Identifier used in generated C code. */508const char *c_name;509/** The dot-separated namespace. */510const char *package_name;511512/** Number elements in `values`. */513unsigned n_values;514/** Array of distinct values, sorted by numeric value. */515const ProtobufCEnumValue *values;516517/** Number of elements in `values_by_name`. */518unsigned n_value_names;519/** Array of named values, including aliases, sorted by name. */520const ProtobufCEnumValueIndex *values_by_name;521522/** Number of elements in `value_ranges`. */523unsigned n_value_ranges;524/** Value ranges, for faster lookups by numeric value. */525const ProtobufCIntRange *value_ranges;526527/** Reserved for future use. */528void *reserved1;529/** Reserved for future use. */530void *reserved2;531/** Reserved for future use. */532void *reserved3;533/** Reserved for future use. */534void *reserved4;535};536537/**538* Represents a single value of an enumeration.539*/540struct ProtobufCEnumValue {541/** The string identifying this value in the .proto file. */542const char *name;543544/** The string identifying this value in generated C code. */545const char *c_name;546547/** The numeric value assigned in the .proto file. */548int value;549};550551/**552* Used by `ProtobufCEnumDescriptor` to look up enum values.553*/554struct ProtobufCEnumValueIndex {555/** Name of the enum value. */556const char *name;557/** Index into values[] array. */558unsigned index;559};560561/**562* Describes a single field in a message.563*/564struct ProtobufCFieldDescriptor {565/** Name of the field as given in the .proto file. */566const char *name;567568/** Tag value of the field as given in the .proto file. */569uint32_t id;570571/** Whether the field is `REQUIRED`, `OPTIONAL`, or `REPEATED`. */572ProtobufCLabel label;573574/** The type of the field. */575ProtobufCType type;576577/**578* The offset in bytes of the message's C structure's quantifier field579* (the `has_MEMBER` field for optional members or the `n_MEMBER` field580* for repeated members or the case enum for oneofs).581*/582unsigned quantifier_offset;583584/**585* The offset in bytes into the message's C structure for the member586* itself.587*/588unsigned offset;589590/**591* A type-specific descriptor.592*593* If `type` is `PROTOBUF_C_TYPE_ENUM`, then `descriptor` points to the594* corresponding `ProtobufCEnumDescriptor`.595*596* If `type` is `PROTOBUF_C_TYPE_MESSAGE`, then `descriptor` points to597* the corresponding `ProtobufCMessageDescriptor`.598*599* Otherwise this field is NULL.600*/601const void *descriptor; /* for MESSAGE and ENUM types */602603/** The default value for this field, if defined. May be NULL. */604const void *default_value;605606/**607* A flag word. Zero or more of the bits defined in the608* `ProtobufCFieldFlag` enum may be set.609*/610uint32_t flags;611612/** Reserved for future use. */613unsigned reserved_flags;614/** Reserved for future use. */615void *reserved2;616/** Reserved for future use. */617void *reserved3;618};619620/**621* Helper structure for optimizing int => index lookups in the case622* where the keys are mostly consecutive values, as they presumably are for623* enums and fields.624*625* The data structures requires that the values in the original array are626* sorted.627*/628struct ProtobufCIntRange {629int start_value;630unsigned orig_index;631/*632* NOTE: the number of values in the range can be inferred by looking633* at the next element's orig_index. A dummy element is added to make634* this simple.635*/636};637638/**639* An instance of a message.640*641* `ProtobufCMessage` is a light-weight "base class" for all messages.642*643* In particular, `ProtobufCMessage` doesn't have any allocation policy644* associated with it. That's because it's common to create `ProtobufCMessage`645* objects on the stack. In fact, that's what we recommend for sending messages.646* If the object is allocated from the stack, you can't really have a memory647* leak.648*649* This means that calls to functions like protobuf_c_message_unpack() which650* return a `ProtobufCMessage` must be paired with a call to a free function,651* like protobuf_c_message_free_unpacked().652*/653struct ProtobufCMessage {654/** The descriptor for this message type. */655const ProtobufCMessageDescriptor *descriptor;656/** The number of elements in `unknown_fields`. */657unsigned n_unknown_fields;658/** The fields that weren't recognized by the parser. */659ProtobufCMessageUnknownField *unknown_fields;660};661662/**663* Describes a message.664*/665struct ProtobufCMessageDescriptor {666/** Magic value checked to ensure that the API is used correctly. */667uint32_t magic;668669/** The qualified name (e.g., "namespace.Type"). */670const char *name;671/** The unqualified name as given in the .proto file (e.g., "Type"). */672const char *short_name;673/** Identifier used in generated C code. */674const char *c_name;675/** The dot-separated namespace. */676const char *package_name;677678/**679* Size in bytes of the C structure representing an instance of this680* type of message.681*/682size_t sizeof_message;683684/** Number of elements in `fields`. */685unsigned n_fields;686/** Field descriptors, sorted by tag number. */687const ProtobufCFieldDescriptor *fields;688/** Used for looking up fields by name. */689const unsigned *fields_sorted_by_name;690691/** Number of elements in `field_ranges`. */692unsigned n_field_ranges;693/** Used for looking up fields by id. */694const ProtobufCIntRange *field_ranges;695696/** Message initialisation function. */697ProtobufCMessageInit message_init;698699/** Reserved for future use. */700void *reserved1;701/** Reserved for future use. */702void *reserved2;703/** Reserved for future use. */704void *reserved3;705};706707/**708* An unknown message field.709*/710struct ProtobufCMessageUnknownField {711/** The tag number. */712uint32_t tag;713/** The wire type of the field. */714ProtobufCWireType wire_type;715/** Number of bytes in `data`. */716size_t len;717/** Field data. */718uint8_t *data;719};720721/**722* Method descriptor.723*/724struct ProtobufCMethodDescriptor {725/** Method name. */726const char *name;727/** Input message descriptor. */728const ProtobufCMessageDescriptor *input;729/** Output message descriptor. */730const ProtobufCMessageDescriptor *output;731};732733/**734* Service.735*/736struct ProtobufCService {737/** Service descriptor. */738const ProtobufCServiceDescriptor *descriptor;739/** Function to invoke the service. */740void (*invoke)(ProtobufCService *service,741unsigned method_index,742const ProtobufCMessage *input,743ProtobufCClosure closure,744void *closure_data);745/** Function to destroy the service. */746void (*destroy)(ProtobufCService *service);747};748749/**750* Service descriptor.751*/752struct ProtobufCServiceDescriptor {753/** Magic value checked to ensure that the API is used correctly. */754uint32_t magic;755756/** Service name. */757const char *name;758/** Short version of service name. */759const char *short_name;760/** C identifier for the service name. */761const char *c_name;762/** Package name. */763const char *package;764/** Number of elements in `methods`. */765unsigned n_methods;766/** Method descriptors, in the order defined in the .proto file. */767const ProtobufCMethodDescriptor *methods;768/** Sort index of methods. */769const unsigned *method_indices_by_name;770};771772/**773* Get the version of the protobuf-c library. Note that this is the version of774* the library linked against, not the version of the headers compiled against.775*776* \return A string containing the version number of protobuf-c.777*/778PROTOBUF_C__API779const char *780protobuf_c_version(void);781782/**783* Get the version of the protobuf-c library. Note that this is the version of784* the library linked against, not the version of the headers compiled against.785*786* \return A 32 bit unsigned integer containing the version number of787* protobuf-c, represented in base-10 as (MAJOR*1E6) + (MINOR*1E3) + PATCH.788*/789PROTOBUF_C__API790uint32_t791protobuf_c_version_number(void);792793/**794* The version of the protobuf-c headers, represented as a string using the same795* format as protobuf_c_version().796*/797#define PROTOBUF_C_VERSION "1.5.2"798799/**800* The version of the protobuf-c headers, represented as an integer using the801* same format as protobuf_c_version_number().802*/803#define PROTOBUF_C_VERSION_NUMBER 1005002804805/**806* The minimum protoc-gen-c version which works with the current version of the807* protobuf-c headers.808*/809#define PROTOBUF_C_MIN_COMPILER_VERSION 1000000810811/**812* Look up a `ProtobufCEnumValue` from a `ProtobufCEnumDescriptor` by name.813*814* \param desc815* The `ProtobufCEnumDescriptor` object.816* \param name817* The `name` field from the corresponding `ProtobufCEnumValue` object to818* match.819* \return820* A `ProtobufCEnumValue` object.821* \retval NULL822* If not found or if the optimize_for = CODE_SIZE option was set.823*/824PROTOBUF_C__API825const ProtobufCEnumValue *826protobuf_c_enum_descriptor_get_value_by_name(827const ProtobufCEnumDescriptor *desc,828const char *name);829830/**831* Look up a `ProtobufCEnumValue` from a `ProtobufCEnumDescriptor` by numeric832* value.833*834* \param desc835* The `ProtobufCEnumDescriptor` object.836* \param value837* The `value` field from the corresponding `ProtobufCEnumValue` object to838* match.839*840* \return841* A `ProtobufCEnumValue` object.842* \retval NULL843* If not found.844*/845PROTOBUF_C__API846const ProtobufCEnumValue *847protobuf_c_enum_descriptor_get_value(848const ProtobufCEnumDescriptor *desc,849int value);850851/**852* Look up a `ProtobufCFieldDescriptor` from a `ProtobufCMessageDescriptor` by853* the name of the field.854*855* \param desc856* The `ProtobufCMessageDescriptor` object.857* \param name858* The name of the field.859* \return860* A `ProtobufCFieldDescriptor` object.861* \retval NULL862* If not found or if the optimize_for = CODE_SIZE option was set.863*/864PROTOBUF_C__API865const ProtobufCFieldDescriptor *866protobuf_c_message_descriptor_get_field_by_name(867const ProtobufCMessageDescriptor *desc,868const char *name);869870/**871* Look up a `ProtobufCFieldDescriptor` from a `ProtobufCMessageDescriptor` by872* the tag value of the field.873*874* \param desc875* The `ProtobufCMessageDescriptor` object.876* \param value877* The tag value of the field.878* \return879* A `ProtobufCFieldDescriptor` object.880* \retval NULL881* If not found.882*/883PROTOBUF_C__API884const ProtobufCFieldDescriptor *885protobuf_c_message_descriptor_get_field(886const ProtobufCMessageDescriptor *desc,887unsigned value);888889/**890* Determine the number of bytes required to store the serialised message.891*892* \param message893* The message object to serialise.894* \return895* Number of bytes.896*/897PROTOBUF_C__API898size_t899protobuf_c_message_get_packed_size(const ProtobufCMessage *message);900901/**902* Serialise a message from its in-memory representation.903*904* This function stores the serialised bytes of the message in a pre-allocated905* buffer.906*907* \param message908* The message object to serialise.909* \param[out] out910* Buffer to store the bytes of the serialised message. This buffer must911* have enough space to store the packed message. Use912* protobuf_c_message_get_packed_size() to determine the number of bytes913* required.914* \return915* Number of bytes stored in `out`.916*/917PROTOBUF_C__API918size_t919protobuf_c_message_pack(const ProtobufCMessage *message, uint8_t *out);920921/**922* Serialise a message from its in-memory representation to a virtual buffer.923*924* This function calls the `append` method of a `ProtobufCBuffer` object to925* consume the bytes generated by the serialiser.926*927* \param message928* The message object to serialise.929* \param buffer930* The virtual buffer object.931* \return932* Number of bytes passed to the virtual buffer.933*/934PROTOBUF_C__API935size_t936protobuf_c_message_pack_to_buffer(937const ProtobufCMessage *message,938ProtobufCBuffer *buffer);939940/**941* Unpack a serialised message into an in-memory representation.942*943* \param descriptor944* The message descriptor.945* \param allocator946* `ProtobufCAllocator` to use for memory allocation. May be NULL to947* specify the default allocator.948* \param len949* Length in bytes of the serialised message.950* \param data951* Pointer to the serialised message.952* \return953* An unpacked message object.954* \retval NULL955* If an error occurred during unpacking.956*/957PROTOBUF_C__API958ProtobufCMessage *959protobuf_c_message_unpack(960const ProtobufCMessageDescriptor *descriptor,961ProtobufCAllocator *allocator,962size_t len,963const uint8_t *data);964965/**966* Free an unpacked message object.967*968* This function should be used to deallocate the memory used by a call to969* protobuf_c_message_unpack().970*971* \param message972* The message object to free. May be NULL.973* \param allocator974* `ProtobufCAllocator` to use for memory deallocation. May be NULL to975* specify the default allocator.976*/977PROTOBUF_C__API978void979protobuf_c_message_free_unpacked(980ProtobufCMessage *message,981ProtobufCAllocator *allocator);982983/**984* Check the validity of a message object.985*986* Makes sure all required fields (`PROTOBUF_C_LABEL_REQUIRED`) are present.987* Recursively checks nested messages.988*989* \retval TRUE990* Message is valid.991* \retval FALSE992* Message is invalid.993*/994PROTOBUF_C__API995protobuf_c_boolean996protobuf_c_message_check(const ProtobufCMessage *);997998/** Message initialiser. */999#define PROTOBUF_C_MESSAGE_INIT(descriptor) { descriptor, 0, NULL }10001001/**1002* Initialise a message object from a message descriptor.1003*1004* \param descriptor1005* Message descriptor.1006* \param message1007* Allocated block of memory of size `descriptor->sizeof_message`.1008*/1009PROTOBUF_C__API1010void1011protobuf_c_message_init(1012const ProtobufCMessageDescriptor *descriptor,1013void *message);10141015/**1016* Free a service.1017*1018* \param service1019* The service object to free.1020*/1021PROTOBUF_C__API1022void1023protobuf_c_service_destroy(ProtobufCService *service);10241025/**1026* Look up a `ProtobufCMethodDescriptor` by name.1027*1028* \param desc1029* Service descriptor.1030* \param name1031* Name of the method.1032*1033* \return1034* A `ProtobufCMethodDescriptor` object.1035* \retval NULL1036* If not found or if the optimize_for = CODE_SIZE option was set.1037*/1038PROTOBUF_C__API1039const ProtobufCMethodDescriptor *1040protobuf_c_service_descriptor_get_method_by_name(1041const ProtobufCServiceDescriptor *desc,1042const char *name);10431044/**1045* Initialise a `ProtobufCBufferSimple` object.1046*/1047#define PROTOBUF_C_BUFFER_SIMPLE_INIT(array_of_bytes) \1048{ \1049{ protobuf_c_buffer_simple_append }, \1050sizeof(array_of_bytes), \10510, \1052(array_of_bytes), \10530, \1054NULL \1055}10561057/**1058* Clear a `ProtobufCBufferSimple` object, freeing any allocated memory.1059*/1060#define PROTOBUF_C_BUFFER_SIMPLE_CLEAR(simp_buf) \1061do { \1062if ((simp_buf)->must_free_data) { \1063if ((simp_buf)->allocator != NULL) \1064(simp_buf)->allocator->free( \1065(simp_buf)->allocator, \1066(simp_buf)->data); \1067else \1068free((simp_buf)->data); \1069} \1070} while (0)10711072/**1073* The `append` method for `ProtobufCBufferSimple`.1074*1075* \param buffer1076* The buffer object to append to. Must actually be a1077* `ProtobufCBufferSimple` object.1078* \param len1079* Number of bytes in `data`.1080* \param data1081* Data to append.1082*/1083PROTOBUF_C__API1084void1085protobuf_c_buffer_simple_append(1086ProtobufCBuffer *buffer,1087size_t len,1088const unsigned char *data);10891090PROTOBUF_C__API1091void1092protobuf_c_service_generated_init(1093ProtobufCService *service,1094const ProtobufCServiceDescriptor *descriptor,1095ProtobufCServiceDestroy destroy);10961097PROTOBUF_C__API1098void1099protobuf_c_service_invoke_internal(1100ProtobufCService *service,1101unsigned method_index,1102const ProtobufCMessage *input,1103ProtobufCClosure closure,1104void *closure_data);11051106/**@}*/11071108PROTOBUF_C__END_DECLS11091110#endif /* PROTOBUF_C_H */111111121113