forked from chobie/php-protocolbuffers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
protocolbuffers.h
343 lines (302 loc) · 9.83 KB
/
protocolbuffers.h
1
2
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
55
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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
139
140
141
142
143
144
145
146
147
148
149
150
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
* Protocol Buffers for PHP
* Copyright 2013 Shuhei Tanuma. All rights reserved.
*
* https://github.com/chobie/php-protocolbuffers
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. 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.
*/
#ifndef PHP_PROTOCOLBUFFERS_PROTOCOL_BUFFERS_H
#define PHP_PROTOCOLBUFFERS_PROTOCOL_BUFFERS_H
#include "php_protocolbuffers.h"
#if defined(__osf__)
#include <inttypes.h>
#elif !defined(_MSC_VER)
#include <stdint.h>
#endif
#if defined(_MSC_VER) && !defined(strcasecmp)
#define strcasecmp stricmp
#endif
#if defined(_MSC_VER) && !defined(strncasecmp)
#define strncasecmp strnicmp
#endif
#if defined(_MSC_VER) && !defined(strtoull)
#define strtoull _strtoui64
#endif
#if defined(_MSC_VER) && !defined(atoll)
#define atoll _atoi64
#endif
#ifdef _MSC_VER
#if defined(_M_IX86) && \
!defined(PROTOBUF_DISABLE_LITTLE_ENDIAN_OPT_FOR_TEST)
#define PROTOBUF_LITTLE_ENDIAN 1
#endif
#if _MSC_VER >= 1300
// If MSVC has "/RTCc" set, it will complain about truncating casts at
// runtime. This file contains some intentional truncating casts.
#pragma runtime_checks("c", off)
#endif
#else
#include <sys/param.h> // __BYTE_ORDER
#if defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN && \
!defined(PROTOBUF_DISABLE_LITTLE_ENDIAN_OPT_FOR_TEST)
#define PROTOBUF_LITTLE_ENDIAN 1
#endif
#endif
#ifndef PHP_PROTOCOLBUFFERS_DEBUG
#define PHP_PROTOCOLBUFFERS_DEBUG 0
#endif
#define PHP_PROTOCOLBUFFERS_NAMESPACE "ProtocolBuffers"
#if PHP_VERSION_ID < 50300
/* NOTE: do nothing */
#define PHP_PROTOCOLBUFFERS_REGISTER_NS_CLASS_ALIAS(ns, name, ce)
#else
/* NOTE: zend_register_ns_class_alias had TSRMLS bug under 5.4. we have to define it for compatibility */
#define PHP_PROTOCOLBUFFERS_REGISTER_NS_CLASS_ALIAS(ns, name, ce) \
zend_register_class_alias_ex(ZEND_NS_NAME(ns, name), sizeof(ZEND_NS_NAME(ns, name))-1, ce TSRMLS_CC)
#endif
#if PHP_VERSION_ID < 50300
#define PHP_PROTOCOLBUFFERS_EXCEPTION_ERROR(exception) zend_exception_error(exception TSRMLS_CC)
#else
#define PHP_PROTOCOLBUFFERS_EXCEPTION_ERROR(exception) zend_exception_error(exception, E_ERROR TSRMLS_CC)
#endif
#if PHP_VERSION_ID < 50307
#define PHP_FE_END { NULL, NULL, NULL, 0, 0 }
#endif
// long long macros to be used because gcc and vc++ use different suffixes,
// and different size specifiers in format strings
#undef GOOGLE_LONGLONG
#undef GOOGLE_ULONGLONG
#undef GOOGLE_LL_FORMAT
#ifdef _MSC_VER
#define GOOGLE_LONGLONG(x) x##I64
#define GOOGLE_ULONGLONG(x) x##UI64
#define GOOGLE_LL_FORMAT "I64" // As in printf("%I64d", ...)
#else
#define GOOGLE_LONGLONG(x) x##LL
#define GOOGLE_ULONGLONG(x) x##ULL
#define GOOGLE_LL_FORMAT "ll" // As in "%lld". Note that "q" is poor form also.
#endif
static const int32_t ktagmax = 0x1FFFFFFF;
static const int32_t kint32max = 0x7FFFFFFF;
static const int32_t kint32min = -0x7FFFFFFF - 1;
static const int64_t kint64max = GOOGLE_LONGLONG(0x7FFFFFFFFFFFFFFF);
static const int64_t kint64min = -GOOGLE_LONGLONG(0x7FFFFFFFFFFFFFFF) - 1;
static const uint32_t kuint32max = 0xFFFFFFFFu;
static const uint64_t kuint64max = GOOGLE_ULONGLONG(0xFFFFFFFFFFFFFFFF);
#ifdef _MSC_VER
#define kMaxVarintBytes 10
#define kMaxVarint32Bytes 5
#else
static const int kMaxVarintBytes = 10;
static const int kMaxVarint32Bytes = 5;
#endif
static const int64_t kLongMax = LONG_MAX;
#define PHP_PROTOCOL_BUFFERS_LONG_SIGN_MASK (1L << (8*sizeof(long)-1))
#ifndef GOOGLE_PREDICT_TRUE
#ifdef __GNUC__
// Provided at least since GCC 3.0.
#define GOOGLE_PREDICT_TRUE(x) (__builtin_expect(!!(x), 1))
#else
#define GOOGLE_PREDICT_TRUE
#endif
#endif
#define php_protocolbuffers_raise_error_or_exception(ce, warning_level, use_exception, ...) \
do {\
if (use_exception) { \
zend_throw_exception_ex(ce, 0 TSRMLS_CC, __VA_ARGS__); \
} else { \
php_error_docref(NULL TSRMLS_CC, warning_level, __VA_ARGS__); \
} \
} while(0);
zend_class_entry *php_protocolbuffers_get_exception_base(TSRMLS_D);
typedef struct php_protocolbuffers_scheme
{
int tag;
char *name;
int name_len;
ulong name_h;
char *original_name;
int original_name_len;
char *mangled_name;
int mangled_name_len;
ulong mangled_name_h;
int magic_type;
/* TODO: use flags */
int type;
int flags;
int required;
int optional;
int scheme_type;
int repeated;
int packed;
int skip;
int is_extension;
char *message;
char *enum_msg;
zval *default_value;
zend_class_entry *ce;
} php_protocolbuffers_scheme;
typedef struct {
int begin;
int end;
} php_protocolbuffers_extension_range;
typedef struct {
char *name;
int name_len;
char *filename;
int filename_len;
int is_extendable;
php_protocolbuffers_scheme *scheme;
int use_single_property;
char *orig_single_property_name;
int orig_single_property_name_len;
char *single_property_name;
int single_property_name_len;
ulong single_property_h;
int size;
int process_unknown_fields;
int use_wakeup_and_sleep;
int extension_cnt;
php_protocolbuffers_extension_range *extensions;
} php_protocolbuffers_scheme_container;
enum WireType {
WIRETYPE_VARINT = 0,
WIRETYPE_FIXED64 = 1,
WIRETYPE_LENGTH_DELIMITED = 2,
WIRETYPE_START_GROUP = 3,
WIRETYPE_END_GROUP = 4,
WIRETYPE_FIXED32 = 5,
};
// Lite alternative to FieldDescriptor::Type. Must be kept in sync.
enum FieldType {
TYPE_DOUBLE = 1,
TYPE_FLOAT = 2,
TYPE_INT64 = 3,
TYPE_UINT64 = 4,
TYPE_INT32 = 5,
TYPE_FIXED64 = 6,
TYPE_FIXED32 = 7,
TYPE_BOOL = 8,
TYPE_STRING = 9,
TYPE_GROUP = 10,
TYPE_MESSAGE = 11,
TYPE_BYTES = 12,
TYPE_UINT32 = 13,
TYPE_ENUM = 14,
TYPE_SFIXED32 = 15,
TYPE_SFIXED64 = 16,
TYPE_SINT32 = 17,
TYPE_SINT64 = 18,
MAX_FIELD_TYPE = 18,
};
typedef struct php_protocolbuffers_serializer
{
uint8_t *buffer;
size_t buffer_size;
size_t buffer_capacity;
size_t buffer_offset;
} php_protocolbuffers_serializer;
typedef struct{
zend_object zo;
char *name;
size_t name_len;
int free_container;
php_protocolbuffers_scheme_container *container;
} php_protocolbuffers_descriptor;
typedef struct{
zend_object zo;
zval *container;
int max;
int offset;
} php_protocolbuffers_message;
typedef struct{
zend_object zo;
int max;
int offset;
} php_protocolbuffers_unknown_field_set;
typedef struct{
zend_object zo;
HashTable *registry;
} php_protocolbuffers_extension_registry;
typedef union {
uint64_t varint;
struct {
uint8_t *val;
size_t len;
} buffer;
} unknown_value;
typedef struct{
zend_object zo;
int number;
int type;
HashTable *ht;
} php_protocolbuffers_unknown_field;
# if ZEND_MODULE_API_NO >= 20100525
# define PHP_PROTOCOLBUFFERS_STD_CREATE_OBJECT(STRUCT_NAME) \
STRUCT_NAME *object;\
\
object = (STRUCT_NAME*)ecalloc(1, sizeof(*object));\
zend_object_std_init(&object->zo, ce TSRMLS_CC);\
object_properties_init(&object->zo, ce);\
\
retval.handle = zend_objects_store_put(object,\
(zend_objects_store_dtor_t)zend_objects_destroy_object,\
(zend_objects_free_object_storage_t) STRUCT_NAME##_free_storage ,\
NULL TSRMLS_CC);\
retval.handlers = zend_get_std_object_handlers();
# else
# define PHP_PROTOCOLBUFFERS_STD_CREATE_OBJECT(STRUCT_NAME) \
STRUCT_NAME *object;\
zval *tmp = NULL;\
\
object = (STRUCT_NAME*)ecalloc(1, sizeof(*object));\
zend_object_std_init(&object->zo, ce TSRMLS_CC);\
zend_hash_copy(object->zo.properties, &ce->default_properties, (copy_ctor_func_t)zval_add_ref, (void *)&tmp, sizeof(zval *)); \
\
retval.handle = zend_objects_store_put(object,\
(zend_objects_store_dtor_t)zend_objects_destroy_object,\
(zend_objects_free_object_storage_t) STRUCT_NAME##_free_storage ,\
NULL TSRMLS_CC);\
retval.handlers = zend_get_std_object_handlers();
# endif
#define PHP_PROTOCOLBUFFERS_GET_OBJECT(STRUCT_NAME, OBJECT) (STRUCT_NAME *)zend_object_store_get_object(OBJECT TSRMLS_CC);
#if PHP_VERSION_ID < 50300
# ifndef Z_ADDREF_P
# define Z_ADDREF_P(pz) (pz)->refcount++
# define Z_ADDREF_PP(ppz) Z_ADDREF_P(*(ppz))
# define Z_ADDREF(z) Z_ADDREF_P(&(z))
# endif
# ifndef Z_SET_REFCOUNT_P
# define Z_SET_REFCOUNT_P(pz,rc) (pz)->refcount = (rc)
# endif
# ifndef Z_REFCOUNT_P
# define Z_REFCOUNT_P(pz) ((pz)->refcount)
# endif
# ifndef Z_REFCOUNT_PP
# define Z_REFCOUNT_PP(pz) ((*pz)->refcount)
# endif
#endif
#include "core.h"
#endif