Protocol Buffers - Google's data interchange format (grpc依赖)
https://developers.google.com/protocol-buffers/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
239 lines
7.7 KiB
239 lines
7.7 KiB
16 years ago
|
/*
|
||
|
* upb - a minimalist implementation of protocol buffers.
|
||
15 years ago
|
*
|
||
14 years ago
|
* Copyright (c) 2009 Google Inc. See LICENSE for details.
|
||
|
* Author: Josh Haberman <jhaberman@gmail.com>
|
||
16 years ago
|
*
|
||
|
* This file contains shared definitions that are widely used across upb.
|
||
16 years ago
|
*/
|
||
|
|
||
|
#ifndef UPB_H_
|
||
|
#define UPB_H_
|
||
|
|
||
|
#include <stdbool.h>
|
||
|
#include <stdint.h>
|
||
15 years ago
|
#include <stdio.h> // only for size_t.
|
||
14 years ago
|
#include <assert.h>
|
||
15 years ago
|
#include "descriptor_const.h"
|
||
14 years ago
|
#include "atomic.h"
|
||
16 years ago
|
|
||
|
#ifdef __cplusplus
|
||
|
extern "C" {
|
||
|
#endif
|
||
|
|
||
15 years ago
|
// inline if possible, emit standalone code if required.
|
||
16 years ago
|
#ifndef INLINE
|
||
16 years ago
|
#define INLINE static inline
|
||
16 years ago
|
#endif
|
||
16 years ago
|
|
||
15 years ago
|
#define UPB_MAX(x, y) ((x) > (y) ? (x) : (y))
|
||
|
#define UPB_MIN(x, y) ((x) < (y) ? (x) : (y))
|
||
15 years ago
|
#define UPB_INDEX(base, i, m) (void*)((char*)(base) + ((i)*(m)))
|
||
15 years ago
|
|
||
14 years ago
|
INLINE void nop_printf(const char *fmt, ...) { (void)fmt; }
|
||
14 years ago
|
|
||
|
#ifdef NDEBUG
|
||
|
#define DEBUGPRINTF nop_printf
|
||
|
#else
|
||
|
#define DEBUGPRINTF printf
|
||
|
#endif
|
||
|
|
||
14 years ago
|
// Rounds val up to the next multiple of align.
|
||
|
INLINE size_t upb_align_up(size_t val, size_t align) {
|
||
|
return val % align == 0 ? val : val + align - (val % align);
|
||
|
}
|
||
|
|
||
15 years ago
|
// The maximum that any submessages can be nested. Matches proto2's limit.
|
||
14 years ago
|
// At the moment this specifies the size of several statically-sized arrays
|
||
|
// and therefore setting it high will cause more memory to be used. Will
|
||
|
// be replaced by a runtime-configurable limit and dynamically-resizing arrays.
|
||
14 years ago
|
// TODO: make this a runtime-settable property of upb_handlers.
|
||
16 years ago
|
#define UPB_MAX_NESTING 64
|
||
|
|
||
15 years ago
|
// The maximum number of fields that any one .proto type can have. Note that
|
||
|
// this is very different than the max field number. It is hard to imagine a
|
||
14 years ago
|
// scenario where more than 2k fields (each with its own name and field number)
|
||
|
// makes sense. The .proto file to describe it would be 2000 lines long and
|
||
|
// contain 2000 unique names.
|
||
|
//
|
||
|
// With this limit we can store a has-bit offset in 8 bits (2**8 * 8 = 2048)
|
||
|
// and we can store a value offset in 16 bits, since the maximum message
|
||
|
// size is 16,640 bytes (2**8 has-bits + 2048 * 8-byte value). Note that
|
||
|
// strings and arrays are not counted in this, only the *pointer* to them is.
|
||
|
// An individual string or array is unaffected by this 16k byte limit.
|
||
|
#define UPB_MAX_FIELDS (2048)
|
||
16 years ago
|
|
||
15 years ago
|
// Nested type names are separated by periods.
|
||
16 years ago
|
#define UPB_SYMBOL_SEPARATOR '.'
|
||
15 years ago
|
|
||
15 years ago
|
// The longest chain that mutually-recursive types are allowed to form. For
|
||
|
// example, this is a type cycle of length 2:
|
||
|
// message A {
|
||
|
// B b = 1;
|
||
|
// }
|
||
|
// message B {
|
||
|
// A a = 1;
|
||
|
// }
|
||
|
#define UPB_MAX_TYPE_CYCLE_LEN 16
|
||
15 years ago
|
|
||
15 years ago
|
// The maximum depth that the type graph can have. Note that this setting does
|
||
|
// not automatically constrain UPB_MAX_NESTING, because type cycles allow for
|
||
14 years ago
|
// unlimited nesting if we do not limit it. Many algorithms in upb call
|
||
|
// recursive functions that traverse the type graph, so we must limit this to
|
||
|
// avoid blowing the C stack.
|
||
15 years ago
|
#define UPB_MAX_TYPE_DEPTH 64
|
||
|
|
||
15 years ago
|
|
||
16 years ago
|
/* Fundamental types and type constants. **************************************/
|
||
|
|
||
15 years ago
|
// A list of types as they are encoded on-the-wire.
|
||
16 years ago
|
enum upb_wire_type {
|
||
|
UPB_WIRE_TYPE_VARINT = 0,
|
||
|
UPB_WIRE_TYPE_64BIT = 1,
|
||
|
UPB_WIRE_TYPE_DELIMITED = 2,
|
||
|
UPB_WIRE_TYPE_START_GROUP = 3,
|
||
|
UPB_WIRE_TYPE_END_GROUP = 4,
|
||
15 years ago
|
UPB_WIRE_TYPE_32BIT = 5,
|
||
16 years ago
|
};
|
||
15 years ago
|
|
||
15 years ago
|
// Type of a field as defined in a .proto file. eg. string, int32, etc. The
|
||
15 years ago
|
// integers that represent this are defined by descriptor.proto. Note that
|
||
|
// descriptor.proto reserves "0" for errors, and we use it to represent
|
||
|
// exceptional circumstances.
|
||
15 years ago
|
typedef uint8_t upb_fieldtype_t;
|
||
16 years ago
|
|
||
15 years ago
|
// For referencing the type constants tersely.
|
||
15 years ago
|
#define UPB_TYPE(type) GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_ ## type
|
||
|
#define UPB_LABEL(type) GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_LABEL_ ## type
|
||
15 years ago
|
|
||
15 years ago
|
// Info for a given field type.
|
||
|
typedef struct {
|
||
|
uint8_t align;
|
||
|
uint8_t size;
|
||
14 years ago
|
uint8_t native_wire_type;
|
||
14 years ago
|
uint8_t inmemory_type; // For example, INT32, SINT32, and SFIXED32 -> INT32
|
||
15 years ago
|
char *ctype;
|
||
|
} upb_type_info;
|
||
|
|
||
|
// A static array of info about all of the field types, indexed by type number.
|
||
14 years ago
|
extern const upb_type_info upb_types[];
|
||
15 years ago
|
|
||
16 years ago
|
|
||
14 years ago
|
/* upb_value ******************************************************************/
|
||
16 years ago
|
|
||
14 years ago
|
struct _upb_strref;
|
||
14 years ago
|
struct _upb_fielddef;
|
||
15 years ago
|
|
||
14 years ago
|
// Special constants for the upb_value.type field. These must not conflict
|
||
|
// with any members of FieldDescriptorProto.Type.
|
||
|
#define UPB_TYPE_ENDGROUP 0
|
||
|
#define UPB_VALUETYPE_FIELDDEF 32
|
||
|
#define UPB_VALUETYPE_PTR 33
|
||
14 years ago
|
|
||
15 years ago
|
// A single .proto value. The owner must have an out-of-band way of knowing
|
||
|
// the type, so that it knows which union member to use.
|
||
14 years ago
|
typedef struct {
|
||
|
union {
|
||
14 years ago
|
uint64_t uint64;
|
||
14 years ago
|
double _double;
|
||
|
float _float;
|
||
|
int32_t int32;
|
||
|
int64_t int64;
|
||
|
uint32_t uint32;
|
||
|
bool _bool;
|
||
14 years ago
|
struct _upb_strref *strref;
|
||
|
struct _upb_fielddef *fielddef;
|
||
14 years ago
|
void *_void;
|
||
|
} val;
|
||
|
|
||
14 years ago
|
#ifndef NDEBUG
|
||
14 years ago
|
// In debug mode we carry the value type around also so we can check accesses
|
||
|
// to be sure the right member is being read.
|
||
14 years ago
|
char type;
|
||
14 years ago
|
#endif
|
||
15 years ago
|
} upb_value;
|
||
16 years ago
|
|
||
14 years ago
|
#ifdef NDEBUG
|
||
|
#define SET_TYPE(dest, val)
|
||
|
#else
|
||
|
#define SET_TYPE(dest, val) dest = val
|
||
|
#endif
|
||
|
|
||
14 years ago
|
#define UPB_VALUE_ACCESSORS(name, membername, ctype, proto_type) \
|
||
14 years ago
|
INLINE ctype upb_value_get ## name(upb_value val) { \
|
||
14 years ago
|
assert(val.type == proto_type); \
|
||
14 years ago
|
return val.val.membername; \
|
||
14 years ago
|
} \
|
||
14 years ago
|
INLINE void upb_value_set ## name(upb_value *val, ctype cval) { \
|
||
14 years ago
|
SET_TYPE(val->type, proto_type); \
|
||
14 years ago
|
val->val.membername = cval; \
|
||
14 years ago
|
}
|
||
14 years ago
|
UPB_VALUE_ACCESSORS(double, _double, double, UPB_TYPE(DOUBLE));
|
||
|
UPB_VALUE_ACCESSORS(float, _float, float, UPB_TYPE(FLOAT));
|
||
|
UPB_VALUE_ACCESSORS(int32, int32, int32_t, UPB_TYPE(INT32));
|
||
|
UPB_VALUE_ACCESSORS(int64, int64, int64_t, UPB_TYPE(INT64));
|
||
|
UPB_VALUE_ACCESSORS(uint32, uint32, uint32_t, UPB_TYPE(UINT32));
|
||
|
UPB_VALUE_ACCESSORS(uint64, uint64, uint64_t, UPB_TYPE(UINT64));
|
||
|
UPB_VALUE_ACCESSORS(bool, _bool, bool, UPB_TYPE(BOOL));
|
||
14 years ago
|
UPB_VALUE_ACCESSORS(strref, strref, struct _upb_strref*, UPB_TYPE(STRING));
|
||
|
UPB_VALUE_ACCESSORS(fielddef, fielddef, struct _upb_fielddef*, UPB_VALUETYPE_FIELDDEF);
|
||
14 years ago
|
UPB_VALUE_ACCESSORS(ptr, _void, void*, UPB_VALUETYPE_PTR);
|
||
14 years ago
|
|
||
|
extern upb_value UPB_NO_VALUE;
|
||
14 years ago
|
|
||
14 years ago
|
|
||
|
/* upb_status *****************************************************************/
|
||
14 years ago
|
|
||
15 years ago
|
// Status codes used as a return value. Codes >0 are not fatal and can be
|
||
|
// resumed.
|
||
15 years ago
|
enum upb_status_code {
|
||
14 years ago
|
// The operation completed successfully.
|
||
14 years ago
|
UPB_OK = 0,
|
||
16 years ago
|
|
||
14 years ago
|
// The bytesrc is at EOF and all data was read successfully.
|
||
14 years ago
|
UPB_EOF = 1,
|
||
16 years ago
|
|
||
14 years ago
|
// A read or write from a streaming src/sink could not be completed right now.
|
||
14 years ago
|
UPB_TRYAGAIN = 2,
|
||
15 years ago
|
|
||
15 years ago
|
// An unrecoverable error occurred.
|
||
14 years ago
|
UPB_ERROR = -1,
|
||
15 years ago
|
};
|
||
16 years ago
|
|
||
14 years ago
|
// TODO: consider adding error space and code, to let ie. errno be stored
|
||
14 years ago
|
// as a proper code, or application-specific error codes.
|
||
14 years ago
|
typedef struct {
|
||
14 years ago
|
char code;
|
||
14 years ago
|
char *str; // NULL when no message is present. NULL-terminated.
|
||
|
char *buf; // Owned by the status.
|
||
|
size_t bufsize;
|
||
|
} upb_status;
|
||
16 years ago
|
|
||
14 years ago
|
#define UPB_STATUS_INIT {UPB_OK, NULL, NULL, 0}
|
||
15 years ago
|
|
||
14 years ago
|
void upb_status_init(upb_status *status);
|
||
14 years ago
|
void upb_status_uninit(upb_status *status);
|
||
|
|
||
14 years ago
|
INLINE bool upb_ok(upb_status *status) { return status->code == UPB_OK; }
|
||
|
INLINE bool upb_iseof(upb_status *status) { return status->code == UPB_EOF; }
|
||
|
|
||
|
void upb_status_fromerrno(upb_status *status);
|
||
|
void upb_status_print(upb_status *status, FILE *f);
|
||
|
void upb_status_clear(upb_status *status);
|
||
|
void upb_status_setf(upb_status *status, enum upb_status_code code,
|
||
|
const char *fmt, ...);
|
||
|
void upb_status_copy(upb_status *to, upb_status *from);
|
||
|
|
||
|
// Like vaprintf, but uses *buf (which can be NULL) as a starting point and
|
||
|
// reallocates it only if the new value will not fit. "size" is updated to
|
||
|
// reflect the allocated size of the buffer. Returns false on memory alloc
|
||
|
// failure.
|
||
|
int upb_vrprintf(char **buf, size_t *size, size_t ofs,
|
||
|
const char *fmt, va_list args);
|
||
16 years ago
|
|
||
16 years ago
|
#ifdef __cplusplus
|
||
|
} /* extern "C" */
|
||
|
#endif
|
||
|
|
||
|
#endif /* UPB_H_ */
|