protobuf-c.h 32.3 KB
Newer Older
L
liuruilong 已提交
1
/*
H
hjchen2 已提交
2
 * Copyright (c) 2008-2017, Dave Benson and the protobuf-c authors.
L
liuruilong 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *     * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *
 *     * Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following disclaimer
 * in the documentation and/or other materials provided with the
 * distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*! \file
 * \mainpage Introduction
 *
 * This is [protobuf-c], a C implementation of [Protocol Buffers].
 *
 * This file defines the public API for the `libprotobuf-c` support library.
 * This API includes interfaces that can be used directly by client code as well
 * as the interfaces used by the code generated by the `protoc-c` compiler.
 *
 * The `libprotobuf-c` support library performs the actual serialization and
 * deserialization of Protocol Buffers messages. It interacts with structures,
 * definitions, and metadata generated by the `protoc-c` compiler from .proto
 * files.
 *
 * \authors Dave Benson and the `protobuf-c` authors.
 *
 * \copyright 2008-2014. Licensed under the terms of the [BSD-2-Clause] license.
 *
 * [protobuf-c]:       https://github.com/protobuf-c/protobuf-c
 * [Protocol Buffers]: https://developers.google.com/protocol-buffers/
 * [BSD-2-Clause]:     http://opensource.org/licenses/BSD-2-Clause
 *
 * \page gencode Generated Code
 *
 * For each enum, we generate a C enum. For each message, we generate a C
55
 * structure which can be cast to a `PaddleMobile__Framework__ProtobufCMessage`.
L
liuruilong 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
 *
 * For each enum and message, we generate a descriptor object that allows us to
 * implement a kind of reflection on the structures.
 *
 * First, some naming conventions:
 *
 * - The name of the type for enums and messages and services is camel case
 *   (meaning WordsAreCrammedTogether) except that double underscores are used
 *   to delimit scopes. For example, the following `.proto` file:
 *
~~~{.proto}
        package foo.bar;
        message BazBah {
            optional int32 val = 1;
        }
~~~
 *
 * would generate a C type `Foo__Bar__BazBah`.
 *
 * - Identifiers for functions and globals are all lowercase, with camel case
 *   words separated by single underscores. For example, one of the function
 *   prototypes generated by `protoc-c` for the above example:
 *
~~~{.c}
Foo__Bar__BazBah *
       foo__bar__baz_bah__unpack
82
                     (PaddleMobile__Framework__ProtobufCAllocator  *allocator,
L
liuruilong 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
                      size_t               len,
                      const uint8_t       *data);
~~~
 *
 * - Identifiers for enum values contain an uppercase prefix which embeds the
 *   package name and the enum type name.
 *
 * - A double underscore is used to separate further components of identifier
 *   names.
 *
 * For example, in the name of the unpack function above, the package name
 * `foo.bar` has become `foo__bar`, the message name BazBah has become
 * `baz_bah`, and the method name is `unpack`. These are all joined with double
 * underscores to form the C identifier `foo__bar__baz_bah__unpack`.
 *
 * We also generate descriptor objects for messages and enums. These are
 * declared in the `.pb-c.h` files:
 *
~~~{.c}
102 103
extern const PaddleMobile__Framework__ProtobufCMessageDescriptor
foo__bar__baz_bah__descriptor;
L
liuruilong 已提交
104 105
~~~
 *
106 107 108 109
 * The message structures all begin with
`PaddleMobile__Framework__ProtobufCMessageDescriptor *` which is
 * sufficient to allow them to be cast to
`PaddleMobile__Framework__ProtobufCMessage`.
L
liuruilong 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
 *
 * For each message defined in a `.proto` file, we generate a number of
 * functions and macros. Each function name contains a prefix based on the
 * package name and message name in order to make it a unique C identifier.
 *
 * - `INIT`. Statically initializes a message object, initializing its
 *   descriptor and setting its fields to default values. Uninitialized
 *   messages cannot be processed by the protobuf-c library.
 *
~~~{.c}
#define FOO__BAR__BAZ_BAH__INIT \
 { PROTOBUF_C_MESSAGE_INIT (&foo__bar__baz_bah__descriptor), 0 }
~~~
 * - `init()`. Initializes a message object, initializing its descriptor and
 *   setting its fields to default values. Uninitialized messages cannot be
 *   processed by the protobuf-c library.
 *
~~~{.c}
void foo__bar__baz_bah__init
                     (Foo__Bar__BazBah *message);
~~~
 * - `unpack()`. Unpacks data for a particular message format. Note that the
 *   `allocator` parameter is usually `NULL` to indicate that the system's
 *   `malloc()` and `free()` functions should be used for dynamically allocating
 *   memory.
 *
~~~{.c}
Foo__Bar__BazBah *
       foo__bar__baz_bah__unpack
139
                     (PaddleMobile__Framework__ProtobufCAllocator  *allocator,
L
liuruilong 已提交
140 141 142 143 144 145 146 147 148 149
                      size_t               len,
                      const uint8_t       *data);
~~~
 *
 * - `free_unpacked()`. Frees a message object obtained with the `unpack()`
 *   method. Freeing `NULL` is allowed (the same as with `free()`).
 *
~~~{.c}
void   foo__bar__baz_bah__free_unpacked
                     (Foo__Bar__BazBah *message,
150
                      PaddleMobile__Framework__ProtobufCAllocator *allocator);
L
liuruilong 已提交
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
~~~
 *
 * - `get_packed_size()`. Calculates the length in bytes of the serialized
 *   representation of the message object.
 *
~~~{.c}
size_t foo__bar__baz_bah__get_packed_size
                     (const Foo__Bar__BazBah   *message);
~~~
 *
 * - `pack()`. Pack a message object into a preallocated buffer. Assumes that
 *   the buffer is large enough. (Use `get_packed_size()` first.)
 *
~~~{.c}
size_t foo__bar__baz_bah__pack
                     (const Foo__Bar__BazBah   *message,
                      uint8_t             *out);
~~~
 *
 * - `pack_to_buffer()`. Packs a message into a "virtual buffer". This is an
 *   object which defines an "append bytes" callback to consume data as it is
 *   serialized.
 *
~~~{.c}
size_t foo__bar__baz_bah__pack_to_buffer
                     (const Foo__Bar__BazBah   *message,
177
                      PaddleMobile__Framework__ProtobufCBuffer     *buffer);
L
liuruilong 已提交
178 179 180 181 182
~~~
 *
 * \page pack Packing and unpacking messages
 *
 * To pack a message, first compute the packed size of the message with
183 184
 * PaddleMobile__Framework__protobuf_c_message_get_packed_size(), then allocate
a buffer of at least
L
liuruilong 已提交
185 186 187 188
 * that size, then call protobuf_c_message_pack().
 *
 * Alternatively, a message can be serialized without calculating the final size
 * first. Use the protobuf_c_message_pack_to_buffer() function and provide a
189 190
 * PaddleMobile__Framework__ProtobufCBuffer object which implements an "append"
method that consumes
L
liuruilong 已提交
191 192
 * data.
 *
193 194
 * To unpack a message, call the
PaddleMobile__Framework__protobuf_c_message_unpack() function. The
L
liuruilong 已提交
195 196 197 198
 * result can be cast to an object of the type that matches the descriptor for
 * the message.
 *
 * The result of unpacking a message should be freed with
199
 * PaddleMobile__Framework__protobuf_c_message_free_unpacked().
L
liuruilong 已提交
200 201 202 203 204 205 206 207 208 209 210
 */

#ifndef PROTOBUF_C_H
#define PROTOBUF_C_H

#include <assert.h>
#include <limits.h>
#include <stddef.h>
#include <stdint.h>

#ifdef __cplusplus
211 212
#define PROTOBUF_C__BEGIN_DECLS extern "C" {
#define PROTOBUF_C__END_DECLS }
L
liuruilong 已提交
213
#else
214 215
#define PROTOBUF_C__BEGIN_DECLS
#define PROTOBUF_C__END_DECLS
L
liuruilong 已提交
216 217 218 219 220
#endif

PROTOBUF_C__BEGIN_DECLS

#if defined(_WIN32) && defined(PROTOBUF_C_USE_SHARED_LIB)
221 222
#ifdef PROTOBUF_C_EXPORT
#define PROTOBUF_C__API __declspec(dllexport)
L
liuruilong 已提交
223
#else
224 225 226 227
#define PROTOBUF_C__API __declspec(dllimport)
#endif
#else
#define PROTOBUF_C__API
L
liuruilong 已提交
228 229 230
#endif

#if !defined(PROTOBUF_C__NO_DEPRECATED) && \
231 232
    ((__GNUC__ > 3) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1))
#define PROTOBUF_C__DEPRECATED __attribute__((__deprecated__))
L
liuruilong 已提交
233
#else
234
#define PROTOBUF_C__DEPRECATED
L
liuruilong 已提交
235 236 237
#endif

#ifndef PROTOBUF_C__FORCE_ENUM_TO_BE_INT_SIZE
238
#define PROTOBUF_C__FORCE_ENUM_TO_BE_INT_SIZE(enum_name) \
L
liuruilong 已提交
239 240 241
  , _##enum_name##_IS_INT_SIZE = INT_MAX
#endif

242 243 244
#define PROTOBUF_C__SERVICE_DESCRIPTOR_MAGIC 0x14159bc3
#define PROTOBUF_C__MESSAGE_DESCRIPTOR_MAGIC 0x28aaeef9
#define PROTOBUF_C__ENUM_DESCRIPTOR_MAGIC 0x114315af
L
liuruilong 已提交
245 246

/* Empty string used for initializers */
247
extern const char PaddleMobile__Framework__protobuf_c_empty_string[];
L
liuruilong 已提交
248 249 250 251 252 253 254 255 256 257 258

/**
 * \defgroup api Public API
 *
 * This is the public API for `libprotobuf-c`. These interfaces are stable and
 * subject to Semantic Versioning guarantees.
 *
 * @{
 */

/**
259 260
 * Values for the `flags` word in
 * `PaddleMobile__Framework__ProtobufCFieldDescriptor`.
L
liuruilong 已提交
261 262
 */
typedef enum {
263 264
  /** Set if the field is repeated and marked with the `packed` option. */
  PROTOBUF_C_FIELD_FLAG_PACKED = (1 << 0),
L
liuruilong 已提交
265

266 267
  /** Set if the field is marked with the `deprecated` option. */
  PROTOBUF_C_FIELD_FLAG_DEPRECATED = (1 << 1),
L
liuruilong 已提交
268

269 270
  /** Set if the field is a member of a oneof (union). */
  PROTOBUF_C_FIELD_FLAG_ONEOF = (1 << 2),
271
} PaddleMobile__Framework__ProtobufCFieldFlag;
L
liuruilong 已提交
272 273 274 275 276 277 278 279 280 281

/**
 * Message field rules.
 *
 * \see [Defining A Message Type] in the Protocol Buffers documentation.
 *
 * [Defining A Message Type]:
 *      https://developers.google.com/protocol-buffers/docs/proto#simple
 */
typedef enum {
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
  /** A well-formed message must have exactly one of this field. */
  PROTOBUF_C_LABEL_REQUIRED,

  /**
   * A well-formed message can have zero or one of this field (but not
   * more than one).
   */
  PROTOBUF_C_LABEL_OPTIONAL,

  /**
   * This field can be repeated any number of times (including zero) in a
   * well-formed message. The order of the repeated values will be
   * preserved.
   */
  PROTOBUF_C_LABEL_REPEATED,

  /**
   * This field has no label. This is valid only in proto3 and is
   * equivalent to OPTIONAL but no "has" quantifier will be consulted.
   */
  PROTOBUF_C_LABEL_NONE,
303
} PaddleMobile__Framework__ProtobufCLabel;
L
liuruilong 已提交
304 305 306 307 308 309 310 311 312 313

/**
 * Field value types.
 *
 * \see [Scalar Value Types] in the Protocol Buffers documentation.
 *
 * [Scalar Value Types]:
 *      https://developers.google.com/protocol-buffers/docs/proto#scalar
 */
typedef enum {
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
  PROTOBUF_C_TYPE_INT32,    /**< int32 */
  PROTOBUF_C_TYPE_SINT32,   /**< signed int32 */
  PROTOBUF_C_TYPE_SFIXED32, /**< signed int32 (4 bytes) */
  PROTOBUF_C_TYPE_INT64,    /**< int64 */
  PROTOBUF_C_TYPE_SINT64,   /**< signed int64 */
  PROTOBUF_C_TYPE_SFIXED64, /**< signed int64 (8 bytes) */
  PROTOBUF_C_TYPE_UINT32,   /**< unsigned int32 */
  PROTOBUF_C_TYPE_FIXED32,  /**< unsigned int32 (4 bytes) */
  PROTOBUF_C_TYPE_UINT64,   /**< unsigned int64 */
  PROTOBUF_C_TYPE_FIXED64,  /**< unsigned int64 (8 bytes) */
  PROTOBUF_C_TYPE_FLOAT,    /**< float */
  PROTOBUF_C_TYPE_DOUBLE,   /**< double */
  PROTOBUF_C_TYPE_BOOL,     /**< boolean */
  PROTOBUF_C_TYPE_ENUM,     /**< enumerated type */
  PROTOBUF_C_TYPE_STRING,   /**< UTF-8 or ASCII string */
  PROTOBUF_C_TYPE_BYTES,    /**< arbitrary byte sequence */
  PROTOBUF_C_TYPE_MESSAGE,  /**< nested message */
331
} PaddleMobile__Framework__ProtobufCType;
L
liuruilong 已提交
332 333 334 335 336 337 338 339 340 341

/**
 * Field wire types.
 *
 * \see [Message Structure] in the Protocol Buffers documentation.
 *
 * [Message Structure]:
 *      https://developers.google.com/protocol-buffers/docs/encoding#structure
 */
typedef enum {
342 343 344 345 346
  PROTOBUF_C_WIRE_TYPE_VARINT = 0,
  PROTOBUF_C_WIRE_TYPE_64BIT = 1,
  PROTOBUF_C_WIRE_TYPE_LENGTH_PREFIXED = 2,
  /* "Start group" and "end group" wire types are unsupported. */
  PROTOBUF_C_WIRE_TYPE_32BIT = 5,
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
} PaddleMobile__Framework__ProtobufCWireType;

struct PaddleMobile__Framework__ProtobufCAllocator;
struct PaddleMobile__Framework__ProtobufCBinaryData;
struct PaddleMobile__Framework__ProtobufCBuffer;
struct PaddleMobile__Framework__ProtobufCBufferSimple;
struct PaddleMobile__Framework__ProtobufCEnumDescriptor;
struct PaddleMobile__Framework__ProtobufCEnumValue;
struct PaddleMobile__Framework__ProtobufCEnumValueIndex;
struct PaddleMobile__Framework__ProtobufCFieldDescriptor;
struct PaddleMobile__Framework__ProtobufCIntRange;
struct PaddleMobile__Framework__ProtobufCMessage;
struct PaddleMobile__Framework__ProtobufCMessageDescriptor;
struct PaddleMobile__Framework__ProtobufCMessageUnknownField;
struct PaddleMobile__Framework__ProtobufCMethodDescriptor;
struct PaddleMobile__Framework__ProtobufCService;
struct PaddleMobile__Framework__ProtobufCServiceDescriptor;

typedef struct PaddleMobile__Framework__ProtobufCAllocator
    PaddleMobile__Framework__ProtobufCAllocator;
typedef struct PaddleMobile__Framework__ProtobufCBinaryData
    PaddleMobile__Framework__ProtobufCBinaryData;
typedef struct PaddleMobile__Framework__ProtobufCBuffer
    PaddleMobile__Framework__ProtobufCBuffer;
typedef struct PaddleMobile__Framework__ProtobufCBufferSimple
    PaddleMobile__Framework__ProtobufCBufferSimple;
typedef struct PaddleMobile__Framework__ProtobufCEnumDescriptor
    PaddleMobile__Framework__ProtobufCEnumDescriptor;
typedef struct PaddleMobile__Framework__ProtobufCEnumValue
    PaddleMobile__Framework__ProtobufCEnumValue;
typedef struct PaddleMobile__Framework__ProtobufCEnumValueIndex
    PaddleMobile__Framework__ProtobufCEnumValueIndex;
typedef struct PaddleMobile__Framework__ProtobufCFieldDescriptor
    PaddleMobile__Framework__ProtobufCFieldDescriptor;
typedef struct PaddleMobile__Framework__ProtobufCIntRange
    PaddleMobile__Framework__ProtobufCIntRange;
typedef struct PaddleMobile__Framework__ProtobufCMessage
    PaddleMobile__Framework__ProtobufCMessage;
typedef struct PaddleMobile__Framework__ProtobufCMessageDescriptor
    PaddleMobile__Framework__ProtobufCMessageDescriptor;
typedef struct PaddleMobile__Framework__ProtobufCMessageUnknownField
    PaddleMobile__Framework__ProtobufCMessageUnknownField;
typedef struct PaddleMobile__Framework__ProtobufCMethodDescriptor
    PaddleMobile__Framework__ProtobufCMethodDescriptor;
typedef struct PaddleMobile__Framework__ProtobufCService
    PaddleMobile__Framework__ProtobufCService;
typedef struct PaddleMobile__Framework__ProtobufCServiceDescriptor
    PaddleMobile__Framework__ProtobufCServiceDescriptor;
L
liuruilong 已提交
395 396 397 398

/** Boolean type. */
typedef int protobuf_c_boolean;

399 400 401 402 403 404
typedef void (*ProtobufCClosure)(
    const PaddleMobile__Framework__ProtobufCMessage *, void *closure_data);
typedef void (*ProtobufCMessageInit)(
    PaddleMobile__Framework__ProtobufCMessage *);
typedef void (*ProtobufCServiceDestroy)(
    PaddleMobile__Framework__ProtobufCService *);
L
liuruilong 已提交
405 406 407 408

/**
 * Structure for defining a custom memory allocator.
 */
409
struct PaddleMobile__Framework__ProtobufCAllocator {
410 411
  /** Function to allocate memory. */
  void *(*alloc)(void *allocator_data, size_t size);
L
liuruilong 已提交
412

413 414
  /** Function to free memory. */
  void (*free)(void *allocator_data, void *pointer);
L
liuruilong 已提交
415

416 417
  /** Opaque pointer passed to `alloc` and `free` functions. */
  void *allocator_data;
L
liuruilong 已提交
418 419 420 421 422
};

/**
 * Structure for the protobuf `bytes` scalar type.
 *
423 424 425
 * The data contained in a `PaddleMobile__Framework__ProtobufCBinaryData` is an
 * arbitrary sequence of bytes. It may contain embedded `NUL` characters and is
 * not required to be `NUL`-terminated.
L
liuruilong 已提交
426
 */
427
struct PaddleMobile__Framework__ProtobufCBinaryData {
428 429
  size_t len;    /**< Number of bytes in the `data` field. */
  uint8_t *data; /**< Data bytes. */
L
liuruilong 已提交
430 431 432 433 434 435 436
};

/**
 * Structure for defining a virtual append-only buffer. Used by
 * protobuf_c_message_pack_to_buffer() to abstract the consumption of serialized
 * bytes.
 *
437 438
 * `PaddleMobile__Framework__ProtobufCBuffer` "subclasses" may be defined on the
stack. For example, to
L
liuruilong 已提交
439 440 441 442
 * write to a `FILE` object:
 *
~~~{.c}
typedef struct {
443
        PaddleMobile__Framework__ProtobufCBuffer base;
L
liuruilong 已提交
444 445 446 447
        FILE *fp;
} BufferAppendToFile;

static void
448
my_buffer_file_append(PaddleMobile__Framework__ProtobufCBuffer *buffer,
L
liuruilong 已提交
449 450 451 452 453 454 455 456
                      size_t len,
                      const uint8_t *data)
{
        BufferAppendToFile *file_buf = (BufferAppendToFile *) buffer;
        fwrite(data, len, 1, file_buf->fp); // XXX: No error handling!
}
~~~
 *
457 458
 * To use this new type of PaddleMobile__Framework__ProtobufCBuffer, it could be
called as follows:
L
liuruilong 已提交
459 460 461 462 463 464 465 466 467 468
 *
~~~{.c}
...
BufferAppendToFile tmp = {0};
tmp.base.append = my_buffer_file_append;
tmp.fp = fp;
protobuf_c_message_pack_to_buffer(&message, &tmp);
...
~~~
 */
469
struct PaddleMobile__Framework__ProtobufCBuffer {
470
  /** Append function. Consumes the `len` bytes stored at `data`. */
471 472
  void (*append)(PaddleMobile__Framework__ProtobufCBuffer *buffer, size_t len,
                 const uint8_t *data);
L
liuruilong 已提交
473 474 475
};

/**
476
 * Simple buffer "subclass" of `PaddleMobile__Framework__ProtobufCBuffer`.
L
liuruilong 已提交
477
 *
478 479
 * A `PaddleMobile__Framework__ProtobufCBufferSimple` object is declared on the
stack and uses a
L
liuruilong 已提交
480 481
 * scratch buffer provided by the user for the initial allocation. It performs
 * exponential resizing, using dynamically allocated memory. A
482 483
 * `PaddleMobile__Framework__ProtobufCBufferSimple` object can be created and
used as follows:
L
liuruilong 已提交
484 485 486
 *
~~~{.c}
uint8_t pad[128];
487 488 489
PaddleMobile__Framework__ProtobufCBufferSimple simple =
PROTOBUF_C_BUFFER_SIMPLE_INIT(pad); PaddleMobile__Framework__ProtobufCBuffer
*buffer = (PaddleMobile__Framework__ProtobufCBuffer *) &simple;
L
liuruilong 已提交
490 491 492
~~~
 *
 * `buffer` can now be used with `protobuf_c_message_pack_to_buffer()`. Once a
493 494
 * message has been serialized to a
`PaddleMobile__Framework__ProtobufCBufferSimple` object, the
L
liuruilong 已提交
495 496
 * serialized data bytes can be accessed from the `.data` field.
 *
497 498
 * To free the memory allocated by a
`PaddleMobile__Framework__ProtobufCBufferSimple` object, if any,
L
liuruilong 已提交
499 500 501 502 503 504 505 506 507
 * call PROTOBUF_C_BUFFER_SIMPLE_CLEAR() on the object, for example:
 *
~~~{.c}
PROTOBUF_C_BUFFER_SIMPLE_CLEAR(&simple);
~~~
 *
 * \see PROTOBUF_C_BUFFER_SIMPLE_INIT
 * \see PROTOBUF_C_BUFFER_SIMPLE_CLEAR
 */
508
struct PaddleMobile__Framework__ProtobufCBufferSimple {
509
  /** "Base class". */
510
  PaddleMobile__Framework__ProtobufCBuffer base;
511 512 513 514 515 516 517 518 519
  /** Number of bytes allocated in `data`. */
  size_t alloced;
  /** Number of bytes currently stored in `data`. */
  size_t len;
  /** Data bytes. */
  uint8_t *data;
  /** Whether `data` must be freed. */
  protobuf_c_boolean must_free_data;
  /** Allocator to use. May be NULL to indicate the system allocator. */
520
  PaddleMobile__Framework__ProtobufCAllocator *allocator;
L
liuruilong 已提交
521 522 523 524 525
};

/**
 * Describes an enumeration as a whole, with all of its values.
 */
526
struct PaddleMobile__Framework__ProtobufCEnumDescriptor {
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
  /** Magic value checked to ensure that the API is used correctly. */
  uint32_t magic;

  /** The qualified name (e.g., "namespace.Type"). */
  const char *name;
  /** The unqualified name as given in the .proto file (e.g., "Type"). */
  const char *short_name;
  /** Identifier used in generated C code. */
  const char *c_name;
  /** The dot-separated namespace. */
  const char *package_name;

  /** Number elements in `values`. */
  unsigned n_values;
  /** Array of distinct values, sorted by numeric value. */
542
  const PaddleMobile__Framework__ProtobufCEnumValue *values;
543 544 545 546

  /** Number of elements in `values_by_name`. */
  unsigned n_value_names;
  /** Array of named values, including aliases, sorted by name. */
547
  const PaddleMobile__Framework__ProtobufCEnumValueIndex *values_by_name;
548 549 550 551

  /** Number of elements in `value_ranges`. */
  unsigned n_value_ranges;
  /** Value ranges, for faster lookups by numeric value. */
552
  const PaddleMobile__Framework__ProtobufCIntRange *value_ranges;
553 554 555 556 557 558 559 560 561

  /** Reserved for future use. */
  void *reserved1;
  /** Reserved for future use. */
  void *reserved2;
  /** Reserved for future use. */
  void *reserved3;
  /** Reserved for future use. */
  void *reserved4;
L
liuruilong 已提交
562 563 564 565 566
};

/**
 * Represents a single value of an enumeration.
 */
567
struct PaddleMobile__Framework__ProtobufCEnumValue {
568 569
  /** The string identifying this value in the .proto file. */
  const char *name;
L
liuruilong 已提交
570

571 572
  /** The string identifying this value in generated C code. */
  const char *c_name;
L
liuruilong 已提交
573

574 575
  /** The numeric value assigned in the .proto file. */
  int value;
L
liuruilong 已提交
576 577 578
};

/**
579 580
 * Used by `PaddleMobile__Framework__ProtobufCEnumDescriptor` to look up enum
 * values.
L
liuruilong 已提交
581
 */
582
struct PaddleMobile__Framework__ProtobufCEnumValueIndex {
583 584 585 586
  /** Name of the enum value. */
  const char *name;
  /** Index into values[] array. */
  unsigned index;
L
liuruilong 已提交
587 588 589 590 591
};

/**
 * Describes a single field in a message.
 */
592
struct PaddleMobile__Framework__ProtobufCFieldDescriptor {
593 594 595 596 597 598 599
  /** Name of the field as given in the .proto file. */
  const char *name;

  /** Tag value of the field as given in the .proto file. */
  uint32_t id;

  /** Whether the field is `REQUIRED`, `OPTIONAL`, or `REPEATED`. */
600
  PaddleMobile__Framework__ProtobufCLabel label;
601 602

  /** The type of the field. */
603
  PaddleMobile__Framework__ProtobufCType type;
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621

  /**
   * The offset in bytes of the message's C structure's quantifier field
   * (the `has_MEMBER` field for optional members or the `n_MEMBER` field
   * for repeated members or the case enum for oneofs).
   */
  unsigned quantifier_offset;

  /**
   * The offset in bytes into the message's C structure for the member
   * itself.
   */
  unsigned offset;

  /**
   * A type-specific descriptor.
   *
   * If `type` is `PROTOBUF_C_TYPE_ENUM`, then `descriptor` points to the
622
   * corresponding `PaddleMobile__Framework__ProtobufCEnumDescriptor`.
623 624
   *
   * If `type` is `PROTOBUF_C_TYPE_MESSAGE`, then `descriptor` points to
625
   * the corresponding `PaddleMobile__Framework__ProtobufCMessageDescriptor`.
626 627 628 629 630 631 632 633 634 635
   *
   * Otherwise this field is NULL.
   */
  const void *descriptor; /* for MESSAGE and ENUM types */

  /** The default value for this field, if defined. May be NULL. */
  const void *default_value;

  /**
   * A flag word. Zero or more of the bits defined in the
636
   * `PaddleMobile__Framework__ProtobufCFieldFlag` enum may be set.
637 638 639 640 641 642 643 644 645
   */
  uint32_t flags;

  /** Reserved for future use. */
  unsigned reserved_flags;
  /** Reserved for future use. */
  void *reserved2;
  /** Reserved for future use. */
  void *reserved3;
L
liuruilong 已提交
646 647 648 649 650 651 652 653 654 655
};

/**
 * Helper structure for optimizing int => index lookups in the case
 * where the keys are mostly consecutive values, as they presumably are for
 * enums and fields.
 *
 * The data structures requires that the values in the original array are
 * sorted.
 */
656
struct PaddleMobile__Framework__ProtobufCIntRange {
657 658 659 660 661 662 663
  int start_value;
  unsigned orig_index;
  /*
   * NOTE: the number of values in the range can be inferred by looking
   * at the next element's orig_index. A dummy element is added to make
   * this simple.
   */
L
liuruilong 已提交
664 665 666 667 668
};

/**
 * An instance of a message.
 *
669 670
 * `PaddleMobile__Framework__ProtobufCMessage` is a light-weight "base class"
 * for all messages.
L
liuruilong 已提交
671
 *
672 673 674 675 676
 * In particular, `PaddleMobile__Framework__ProtobufCMessage` doesn't have any
 * allocation policy associated with it. That's because it's common to create
 * `PaddleMobile__Framework__ProtobufCMessage` objects on the stack. In fact,
 * that's what we recommend for sending messages. If the object is allocated
 * from the stack, you can't really have a memory leak.
L
liuruilong 已提交
677
 *
678 679 680 681 682
 * This means that calls to functions like
 * PaddleMobile__Framework__protobuf_c_message_unpack() which return a
 * `PaddleMobile__Framework__ProtobufCMessage` must be paired with a call to a
 * free function, like
 * PaddleMobile__Framework__protobuf_c_message_free_unpacked().
L
liuruilong 已提交
683
 */
684
struct PaddleMobile__Framework__ProtobufCMessage {
685
  /** The descriptor for this message type. */
686
  const PaddleMobile__Framework__ProtobufCMessageDescriptor *descriptor;
687 688 689
  /** The number of elements in `unknown_fields`. */
  unsigned n_unknown_fields;
  /** The fields that weren't recognized by the parser. */
690
  PaddleMobile__Framework__ProtobufCMessageUnknownField *unknown_fields;
L
liuruilong 已提交
691 692 693 694 695
};

/**
 * Describes a message.
 */
696
struct PaddleMobile__Framework__ProtobufCMessageDescriptor {
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
  /** Magic value checked to ensure that the API is used correctly. */
  uint32_t magic;

  /** The qualified name (e.g., "namespace.Type"). */
  const char *name;
  /** The unqualified name as given in the .proto file (e.g., "Type"). */
  const char *short_name;
  /** Identifier used in generated C code. */
  const char *c_name;
  /** The dot-separated namespace. */
  const char *package_name;

  /**
   * Size in bytes of the C structure representing an instance of this
   * type of message.
   */
  size_t sizeof_message;

  /** Number of elements in `fields`. */
  unsigned n_fields;
  /** Field descriptors, sorted by tag number. */
718
  const PaddleMobile__Framework__ProtobufCFieldDescriptor *fields;
719 720 721 722 723 724
  /** Used for looking up fields by name. */
  const unsigned *fields_sorted_by_name;

  /** Number of elements in `field_ranges`. */
  unsigned n_field_ranges;
  /** Used for looking up fields by id. */
725
  const PaddleMobile__Framework__ProtobufCIntRange *field_ranges;
726 727 728 729 730 731 732 733 734 735

  /** Message initialisation function. */
  ProtobufCMessageInit message_init;

  /** Reserved for future use. */
  void *reserved1;
  /** Reserved for future use. */
  void *reserved2;
  /** Reserved for future use. */
  void *reserved3;
L
liuruilong 已提交
736 737 738 739 740
};

/**
 * An unknown message field.
 */
741
struct PaddleMobile__Framework__ProtobufCMessageUnknownField {
742 743 744
  /** The tag number. */
  uint32_t tag;
  /** The wire type of the field. */
745
  PaddleMobile__Framework__ProtobufCWireType wire_type;
746 747 748 749
  /** Number of bytes in `data`. */
  size_t len;
  /** Field data. */
  uint8_t *data;
L
liuruilong 已提交
750 751 752 753 754
};

/**
 * Method descriptor.
 */
755
struct PaddleMobile__Framework__ProtobufCMethodDescriptor {
756 757 758
  /** Method name. */
  const char *name;
  /** Input message descriptor. */
759
  const PaddleMobile__Framework__ProtobufCMessageDescriptor *input;
760
  /** Output message descriptor. */
761
  const PaddleMobile__Framework__ProtobufCMessageDescriptor *output;
L
liuruilong 已提交
762 763 764 765 766
};

/**
 * Service.
 */
767
struct PaddleMobile__Framework__ProtobufCService {
768
  /** Service descriptor. */
769
  const PaddleMobile__Framework__ProtobufCServiceDescriptor *descriptor;
770
  /** Function to invoke the service. */
771 772 773 774
  void (*invoke)(PaddleMobile__Framework__ProtobufCService *service,
                 unsigned method_index,
                 const PaddleMobile__Framework__ProtobufCMessage *input,
                 ProtobufCClosure closure, void *closure_data);
775
  /** Function to destroy the service. */
776
  void (*destroy)(PaddleMobile__Framework__ProtobufCService *service);
L
liuruilong 已提交
777 778 779 780 781
};

/**
 * Service descriptor.
 */
782
struct PaddleMobile__Framework__ProtobufCServiceDescriptor {
783 784 785 786 787 788 789 790 791 792 793 794 795 796
  /** Magic value checked to ensure that the API is used correctly. */
  uint32_t magic;

  /** Service name. */
  const char *name;
  /** Short version of service name. */
  const char *short_name;
  /** C identifier for the service name. */
  const char *c_name;
  /** Package name. */
  const char *package;
  /** Number of elements in `methods`. */
  unsigned n_methods;
  /** Method descriptors, in the order defined in the .proto file. */
797
  const PaddleMobile__Framework__ProtobufCMethodDescriptor *methods;
798 799
  /** Sort index of methods. */
  const unsigned *method_indices_by_name;
L
liuruilong 已提交
800 801 802 803 804 805 806 807 808
};

/**
 * Get the version of the protobuf-c library. Note that this is the version of
 * the library linked against, not the version of the headers compiled against.
 *
 * \return A string containing the version number of protobuf-c.
 */
PROTOBUF_C__API
809
const char *PaddleMobile__Framework__protobuf_c_version(void);
L
liuruilong 已提交
810 811 812 813 814 815 816 817 818

/**
 * Get the version of the protobuf-c library. Note that this is the version of
 * the library linked against, not the version of the headers compiled against.
 *
 * \return A 32 bit unsigned integer containing the version number of
 *      protobuf-c, represented in base-10 as (MAJOR*1E6) + (MINOR*1E3) + PATCH.
 */
PROTOBUF_C__API
819
uint32_t PaddleMobile__Framework__protobuf_c_version_number(void);
L
liuruilong 已提交
820 821 822

/**
 * The version of the protobuf-c headers, represented as a string using the same
823
 * format as PaddleMobile__Framework__protobuf_c_version().
L
liuruilong 已提交
824
 */
H
hjchen2 已提交
825
#define PROTOBUF_C_VERSION "1.3.0"
L
liuruilong 已提交
826 827 828

/**
 * The version of the protobuf-c headers, represented as an integer using the
829
 * same format as PaddleMobile__Framework__protobuf_c_version_number().
L
liuruilong 已提交
830
 */
H
hjchen2 已提交
831
#define PROTOBUF_C_VERSION_NUMBER 1003000
L
liuruilong 已提交
832 833 834 835 836

/**
 * The minimum protoc-c version which works with the current version of the
 * protobuf-c headers.
 */
837
#define PROTOBUF_C_MIN_COMPILER_VERSION 1000000
838

L
liuruilong 已提交
839 840 841 842 843 844 845 846 847
/**
 * Determine the number of bytes required to store the serialised message.
 *
 * \param message
 *      The message object to serialise.
 * \return
 *      Number of bytes.
 */
PROTOBUF_C__API
848 849
size_t PaddleMobile__Framework__protobuf_c_message_get_packed_size(
    const PaddleMobile__Framework__ProtobufCMessage *message);
850

L
liuruilong 已提交
851 852 853 854 855 856
/**
 * Unpack a serialised message into an in-memory representation.
 *
 * \param descriptor
 *      The message descriptor.
 * \param allocator
857 858 859 860 861
 *      `PaddleMobile__Framework__ProtobufCAllocator` to use for memory
 * allocation. May be NULL to specify the default allocator. \param len Length
 * in bytes of the serialised message. \param data Pointer to the
 * serialised message. \return An unpacked message object. \retval NULL If
 * an error occurred during unpacking.
L
liuruilong 已提交
862 863
 */
PROTOBUF_C__API
864 865 866 867 868
PaddleMobile__Framework__ProtobufCMessage *
PaddleMobile__Framework__protobuf_c_message_unpack(
    const PaddleMobile__Framework__ProtobufCMessageDescriptor *descriptor,
    PaddleMobile__Framework__ProtobufCAllocator *allocator, size_t len,
    const uint8_t *data);
L
liuruilong 已提交
869 870 871 872 873

/**
 * Free an unpacked message object.
 *
 * This function should be used to deallocate the memory used by a call to
874
 * PaddleMobile__Framework__protobuf_c_message_unpack().
L
liuruilong 已提交
875 876 877 878
 *
 * \param message
 *      The message object to free. May be NULL.
 * \param allocator
879 880
 *      `PaddleMobile__Framework__ProtobufCAllocator` to use for memory
 * deallocation. May be NULL to specify the default allocator.
L
liuruilong 已提交
881 882
 */
PROTOBUF_C__API
883 884 885
void PaddleMobile__Framework__protobuf_c_message_free_unpacked(
    PaddleMobile__Framework__ProtobufCMessage *message,
    PaddleMobile__Framework__ProtobufCAllocator *allocator);
L
liuruilong 已提交
886 887 888 889 890 891 892 893 894 895 896 897 898

/**
 * Check the validity of a message object.
 *
 * Makes sure all required fields (`PROTOBUF_C_LABEL_REQUIRED`) are present.
 * Recursively checks nested messages.
 *
 * \retval TRUE
 *      Message is valid.
 * \retval FALSE
 *      Message is invalid.
 */
PROTOBUF_C__API
899 900
protobuf_c_boolean PaddleMobile__Framework__protobuf_c_message_check(
    const PaddleMobile__Framework__ProtobufCMessage *);
L
liuruilong 已提交
901 902

/** Message initialiser. */
903 904
#define PROTOBUF_C_MESSAGE_INIT(descriptor) \
  { descriptor, 0, NULL }
L
liuruilong 已提交
905 906 907 908 909 910 911 912 913 914

/**
 * Initialise a message object from a message descriptor.
 *
 * \param descriptor
 *      Message descriptor.
 * \param message
 *      Allocated block of memory of size `descriptor->sizeof_message`.
 */
PROTOBUF_C__API
915 916 917
void PaddleMobile__Framework__protobuf_c_message_init(
    const PaddleMobile__Framework__ProtobufCMessageDescriptor *descriptor,
    void *message);
918

L
liuruilong 已提交
919
/**
920
 * Initialise a `PaddleMobile__Framework__ProtobufCBufferSimple` object.
L
liuruilong 已提交
921
 */
922 923 924 925
#define PROTOBUF_C_BUFFER_SIMPLE_INIT(array_of_bytes)           \
  {                                                             \
    {PaddleMobile__Framework__protobuf_c_buffer_simple_append}, \
        sizeof(array_of_bytes), 0, (array_of_bytes), 0, NULL    \
926
  }
L
liuruilong 已提交
927 928

/**
929 930
 * Clear a `PaddleMobile__Framework__ProtobufCBufferSimple` object, freeing any
 * allocated memory.
L
liuruilong 已提交
931
 */
932 933 934 935 936 937 938 939 940
#define PROTOBUF_C_BUFFER_SIMPLE_CLEAR(simp_buf)                              \
  do {                                                                        \
    if ((simp_buf)->must_free_data) {                                         \
      if ((simp_buf)->allocator != NULL)                                      \
        (simp_buf)->allocator->free((simp_buf)->allocator, (simp_buf)->data); \
      else                                                                    \
        free((simp_buf)->data);                                               \
    }                                                                         \
  } while (0)
L
liuruilong 已提交
941 942

/**
943
 * The `append` method for `PaddleMobile__Framework__ProtobufCBufferSimple`.
L
liuruilong 已提交
944 945 946
 *
 * \param buffer
 *      The buffer object to append to. Must actually be a
947
 *      `PaddleMobile__Framework__ProtobufCBufferSimple` object.
L
liuruilong 已提交
948 949 950 951 952 953
 * \param len
 *      Number of bytes in `data`.
 * \param data
 *      Data to append.
 */
PROTOBUF_C__API
954 955 956
void PaddleMobile__Framework__protobuf_c_buffer_simple_append(
    PaddleMobile__Framework__ProtobufCBuffer *buffer, size_t len,
    const unsigned char *data);
957

L
liuruilong 已提交
958 959 960 961 962
/**@}*/

PROTOBUF_C__END_DECLS

#endif /* PROTOBUF_C_H */