-
Notifications
You must be signed in to change notification settings - Fork 38
/
protocolbuffers.c
450 lines (369 loc) · 15.3 KB
/
protocolbuffers.c
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
344
345
346
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/*
* 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.
*/
#include "protocolbuffers.h"
#include "descriptor.h"
#include "field_descriptor.h"
#include "message.h"
#include "message_options.h"
#include "enum.h"
#include "php_message_options.h"
#include "descriptor_builder.h"
#include "unknown_field.h"
#include "unknown_field_set.h"
#include "extension_registry.h"
#include "helper.h"
#include "enum_descriptor.h"
#include "enum_value_descriptor.h"
#include "enum_descriptor_builder.h"
zend_class_entry *protocol_buffers_class_entry;
zend_class_entry *php_protocol_buffers_descriptor_class_entry;
zend_class_entry *php_protocol_buffers_field_descriptor_class_entry;
zend_class_entry *php_protocol_buffers_serializable_class_entry;
zend_class_entry *php_protocol_buffers_enum_describable_class_entry;
zend_class_entry *php_protocol_buffers_message_class_entry;
zend_class_entry *php_protocol_buffers_message_options_class_entry;
zend_class_entry *php_protocol_buffers_enum_class_entry;
zend_class_entry *php_protocol_buffers_enum_descriptor_class_entry;
zend_class_entry *php_protocol_buffers_enum_value_descriptor_class_entry;
zend_class_entry *php_protocol_buffers_enum_descriptor_builder_class_entry;
zend_class_entry *php_protocol_buffers_descriptor_builder_class_entry;
zend_class_entry *php_protocol_buffers_unknown_field_class_entry;
zend_class_entry *php_protocol_buffers_unknown_field_set_class_entry;
zend_class_entry *php_protocol_buffers_helper_class_entry;
zend_class_entry *php_protocol_buffers_extension_registry_class_entry;
zend_class_entry *php_protocol_buffers_php_message_options_class_entry;
zend_class_entry *php_protocol_buffers_invalid_byte_sequence_class_entry;
zend_class_entry *php_protocol_buffers_invalid_protocolbuffers_exception_class_entry;
zend_class_entry *php_protocol_buffers_uninitialized_message_exception_class_entry;
ZEND_DECLARE_MODULE_GLOBALS(protocolbuffers);
zend_class_entry *php_protocolbuffers_get_exception_base(TSRMLS_D)
{
#if (PHP_MAJOR_VERSION == 5) && (PHP_MINOR_VERSION < 2)
return zend_exception_get_default();
#else
return zend_exception_get_default(TSRMLS_C);
#endif
}
ZEND_BEGIN_ARG_INFO_EX(arginfo_protocolbuffers_decode, 0, 0, 1)
ZEND_ARG_INFO(0, class_name)
ZEND_ARG_INFO(0, bytes)
ZEND_ARG_INFO(0, descriptor)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_protocolbuffers_encode, 0, 0, 1)
ZEND_ARG_INFO(0, object)
ZEND_ARG_INFO(0, descriptor)
ZEND_END_ARG_INFO()
/* {{{ proto mixed ProtocolBuffers::decode($class_name, $bytes)
*/
PHP_METHOD(protocolbuffers, decode)
{
char *klass;
const char *data;
long klass_len = 0, data_len = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"ss", &klass, &klass_len, &data, &data_len) == FAILURE) {
return;
}
if (klass[0] == '\\') {
int x;
for (x = 0; x < klass_len-1; x++) {
klass[x] = klass[x+1];
}
klass[klass_len-1] = '\0';
klass_len--;
}
php_protocolbuffers_decode(INTERNAL_FUNCTION_PARAM_PASSTHRU, data, data_len, klass, klass_len);
}
/* }}} */
/* {{{ proto string ProtocolBuffers::encode($class)
*/
PHP_METHOD(protocolbuffers, encode)
{
zval *klass = NULL;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"o", &klass) == FAILURE) {
return;
}
php_protocolbuffers_encode(INTERNAL_FUNCTION_PARAM_PASSTHRU, Z_OBJCE_P(klass), klass);
}
/* }}} */
static zend_function_entry php_protocolbuffers_methods[] = {
PHP_ME(protocolbuffers, decode, arginfo_protocolbuffers_decode, ZEND_ACC_STATIC | ZEND_ACC_PUBLIC)
PHP_ME(protocolbuffers, encode, NULL, ZEND_ACC_STATIC | ZEND_ACC_PUBLIC)
PHP_FE_END
};
static void php_protocol_buffers_invalid_byte_sequence_exception(TSRMLS_D)
{
zend_class_entry ce;
INIT_CLASS_ENTRY(ce, "ProtocolBuffersInvalidByteSequenceException", 0);
php_protocol_buffers_invalid_byte_sequence_class_entry = zend_register_internal_class_ex(&ce, php_protocolbuffers_get_exception_base(TSRMLS_C), NULL TSRMLS_CC);
PHP_PROTOCOLBUFFERS_REGISTER_NS_CLASS_ALIAS(PHP_PROTOCOLBUFFERS_NAMESPACE, "InvalidByteSequenceException", php_protocol_buffers_invalid_byte_sequence_class_entry);
}
static void php_protocol_buffers_invalid_exception(TSRMLS_D)
{
zend_class_entry ce;
INIT_CLASS_ENTRY(ce, "ProtocolBuffersInvalidProtocolBufferException", 0);
php_protocol_buffers_invalid_protocolbuffers_exception_class_entry = zend_register_internal_class_ex(&ce, php_protocolbuffers_get_exception_base(TSRMLS_C), NULL TSRMLS_CC);
PHP_PROTOCOLBUFFERS_REGISTER_NS_CLASS_ALIAS(PHP_PROTOCOLBUFFERS_NAMESPACE, "InvalidProtocolBufferException", php_protocol_buffers_invalid_protocolbuffers_exception_class_entry);
}
static void php_protocolbuffers_uninitialized_message_exception(TSRMLS_D)
{
zend_class_entry ce;
INIT_CLASS_ENTRY(ce, "ProtocolBuffersUninitializedMessageException", 0);
php_protocol_buffers_uninitialized_message_exception_class_entry = zend_register_internal_class_ex(&ce, php_protocolbuffers_get_exception_base(TSRMLS_C), NULL TSRMLS_CC);
PHP_PROTOCOLBUFFERS_REGISTER_NS_CLASS_ALIAS(PHP_PROTOCOLBUFFERS_NAMESPACE, "UninitializedMessageException", php_protocol_buffers_uninitialized_message_exception_class_entry);
}
static zend_function_entry php_protocolbuffers_describable_methods[] = {
ZEND_FENTRY(getDescriptor, NULL, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC | ZEND_ACC_ABSTRACT)
PHP_FE_END
};
static void php_protocol_buffers_serializable(TSRMLS_D)
{
zend_class_entry ce;
INIT_CLASS_ENTRY(ce, "ProtocolBuffersDescribable", php_protocolbuffers_describable_methods);
php_protocol_buffers_serializable_class_entry = zend_register_internal_interface(&ce TSRMLS_CC);
PHP_PROTOCOLBUFFERS_REGISTER_NS_CLASS_ALIAS(PHP_PROTOCOLBUFFERS_NAMESPACE, "Describable", php_protocol_buffers_serializable_class_entry);
}
static zend_function_entry php_protocolbuffers_enum_describable_methods[] = {
ZEND_FENTRY(getEnumDescriptor, NULL, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC | ZEND_ACC_ABSTRACT)
PHP_FE_END
};
static void php_protocol_buffers_enum_describable(TSRMLS_D)
{
zend_class_entry ce;
INIT_CLASS_ENTRY(ce, "ProtocolBuffersEnumDescribable", php_protocolbuffers_enum_describable_methods);
php_protocol_buffers_enum_describable_class_entry = zend_register_internal_interface(&ce TSRMLS_CC);
PHP_PROTOCOLBUFFERS_REGISTER_NS_CLASS_ALIAS(PHP_PROTOCOLBUFFERS_NAMESPACE, "EnumDescribable", php_protocol_buffers_serializable_class_entry);
}
static void php_protocolbuffers_init(TSRMLS_D)
{
zend_class_entry ce;
INIT_CLASS_ENTRY(ce, "ProtocolBuffers", php_protocolbuffers_methods);
protocol_buffers_class_entry = zend_register_internal_class(&ce TSRMLS_CC);
php_protocol_buffers_invalid_byte_sequence_exception(TSRMLS_C);
php_protocol_buffers_invalid_exception(TSRMLS_C);
php_protocolbuffers_uninitialized_message_exception(TSRMLS_C);
php_protocolbuffers_descriptor_class(TSRMLS_C);
php_protocolbuffers_filed_descriptor_class(TSRMLS_C);
php_protocol_buffers_serializable(TSRMLS_C);
php_protocol_buffers_enum_describable(TSRMLS_C);
php_protocolbuffers_message_class(TSRMLS_C);
php_protocolbuffers_message_options_class(TSRMLS_C);
php_protocolbuffers_enum_class(TSRMLS_C);
php_protocolbuffers_enum_descriptor_class(TSRMLS_C);
php_protocolbuffers_enum_value_descriptor_class(TSRMLS_C);
php_protocolbuffers_enum_descriptor_builder_class(TSRMLS_C);
php_protocolbuffers_php_message_options_class(TSRMLS_C);
php_protocolbuffers_descriptor_builder_class(TSRMLS_C);
php_protocolbuffers_unknown_field_class(TSRMLS_C);
php_protocolbuffers_unknown_field_set_class(TSRMLS_C);
php_protocolbuffers_helper_class(TSRMLS_C);
php_protocolbuffers_extension_registry_class(TSRMLS_C);
#define PB_DECLARE_CONST_STRING(name, value) \
zend_declare_class_constant_string(protocol_buffers_class_entry, ZEND_STRS(#name)-1, "php" TSRMLS_CC);
#define PB_DECLARE_CONST_LONG(name, value) \
zend_declare_class_constant_long(protocol_buffers_class_entry, ZEND_STRS(#name)-1, value TSRMLS_CC);
PB_DECLARE_CONST_LONG(WIRETYPE_VARINT, WIRETYPE_VARINT);
PB_DECLARE_CONST_LONG(WIRETYPE_FIXED64, WIRETYPE_FIXED64);
PB_DECLARE_CONST_LONG(WIRETYPE_LENGTH_DELIMITED, WIRETYPE_LENGTH_DELIMITED);
PB_DECLARE_CONST_LONG(WIRETYPE_START_GROUP, WIRETYPE_START_GROUP);
PB_DECLARE_CONST_LONG(WIRETYPE_END_GROUP, WIRETYPE_END_GROUP);
PB_DECLARE_CONST_LONG(WIRETYPE_FIXED32, WIRETYPE_FIXED32);
PB_DECLARE_CONST_LONG(TYPE_DOUBLE, TYPE_DOUBLE);
PB_DECLARE_CONST_LONG(TYPE_FLOAT, TYPE_FLOAT);
PB_DECLARE_CONST_LONG(TYPE_INT64, TYPE_INT64);
PB_DECLARE_CONST_LONG(TYPE_UINT64, TYPE_UINT64);
PB_DECLARE_CONST_LONG(TYPE_INT32, TYPE_INT32);
PB_DECLARE_CONST_LONG(TYPE_FIXED64, TYPE_FIXED64);
PB_DECLARE_CONST_LONG(TYPE_FIXED32, TYPE_FIXED32);
PB_DECLARE_CONST_LONG(TYPE_BOOL, TYPE_BOOL);
PB_DECLARE_CONST_LONG(TYPE_STRING, TYPE_STRING);
PB_DECLARE_CONST_LONG(TYPE_GROUP, TYPE_GROUP);
PB_DECLARE_CONST_LONG(TYPE_MESSAGE, TYPE_MESSAGE);
PB_DECLARE_CONST_LONG(TYPE_BYTES, TYPE_BYTES);
PB_DECLARE_CONST_LONG(TYPE_UINT32, TYPE_UINT32);
PB_DECLARE_CONST_LONG(TYPE_ENUM, TYPE_ENUM);
PB_DECLARE_CONST_LONG(TYPE_SFIXED32, TYPE_SFIXED32);
PB_DECLARE_CONST_LONG(TYPE_SFIXED64, TYPE_SFIXED64);
PB_DECLARE_CONST_LONG(TYPE_SINT32, TYPE_SINT32);
PB_DECLARE_CONST_LONG(TYPE_SINT64, TYPE_SINT64);
PB_DECLARE_CONST_LONG(MAX_FIELD_TYPE, MAX_FIELD_TYPE);
PB_DECLARE_CONST_STRING(PHP_MESSAGE_OPTION, "php");
#undef PB_DECLARE_CONST_LONG
}
PHP_MINFO_FUNCTION(protocolbuffers)
{
php_printf("PHP Protocol Buffers Extension\n");
php_info_print_table_start();
php_info_print_table_header(2, "Protocol Buffers Support", "enabled");
php_info_print_table_row(2, "Version", PHP_PROTOCOLBUFFERS_VERSION);
php_info_print_table_end();
}
PHP_INI_BEGIN()
STD_PHP_INI_BOOLEAN("protocolbuffers.strict_mode", "1", PHP_INI_ALL, OnUpdateLong, strict_mode, zend_protocolbuffers_globals, protocolbuffers_globals)
STD_PHP_INI_BOOLEAN("protocolbuffers.validate_string", "1", PHP_INI_ALL, OnUpdateLong, validate_string, zend_protocolbuffers_globals, protocolbuffers_globals)
STD_PHP_INI_BOOLEAN("protocolbuffers.native_scalars", "0", PHP_INI_ALL, OnUpdateLong, native_scalars, zend_protocolbuffers_globals, protocolbuffers_globals)
PHP_INI_END()
static PHP_GINIT_FUNCTION(protocolbuffers)
{
memset(protocolbuffers_globals, 0, sizeof(zend_protocolbuffers_globals));
}
static void php_protocolbuffers_cleanup_global_handles(TSRMLS_D)
{
}
static PHP_GSHUTDOWN_FUNCTION(protocolbuffers)
{
#if (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION < 2) || (PHP_MAJOR_VERSION < 5)
#ifndef ZTS
php_protocolbuffers_cleanup_global_handles(TSRMLS_C);
#endif
#endif
}
static void php_protocolbuffers_init_globals() {
}
PHP_MINIT_FUNCTION(protocolbuffers)
{
//ZEND_INIT_MODULE_GLOBALS(protocolbuffers, php_protocolbuffers_init_globals, NULL);
REGISTER_INI_ENTRIES();
php_protocolbuffers_init(TSRMLS_C);
return SUCCESS;
}
static int implemented = 0;
PHP_RINIT_FUNCTION(protocolbuffers)
{
zend_class_entry **json;
PBG(messages) = NULL;
PBG(classes) = NULL;
PBG(extension_registry) = NULL;
PBG(strict_mode) = 1;
// NOTE(chobie): prevent segmentaiton fault on CentOS box (CentOS uses json shared modules.)
if (implemented == 0 && zend_lookup_class("JsonSerializable", sizeof("JsonSerializable")-1, &json TSRMLS_CC) != FAILURE) {
zend_class_implements(php_protocol_buffers_message_class_entry TSRMLS_CC, 1, *json);
implemented = 1;
}
if (!PBG(messages)) {
ALLOC_HASHTABLE(PBG(messages));
zend_hash_init(PBG(messages), 0, NULL, NULL, 0);
}
if (!PBG(classes)) {
ALLOC_HASHTABLE(PBG(classes));
zend_hash_init(PBG(classes), 0, NULL, NULL, 0);
}
PBG(validate_string) = 1;
PBG(native_scalars) = 0;
return SUCCESS;
}
PHP_MSHUTDOWN_FUNCTION(protocolbuffers)
{
UNREGISTER_INI_ENTRIES();
return SUCCESS;
}
PHP_RSHUTDOWN_FUNCTION(protocolbuffers)
{
if (PBG(messages)) {
zend_try {
int i = 0;
HashPosition pos;
php_protocolbuffers_scheme_container **element;
for(zend_hash_internal_pointer_reset_ex(PBG(messages), &pos);
zend_hash_get_current_data_ex(PBG(messages), (void **)&element, &pos) == SUCCESS;
zend_hash_move_forward_ex(PBG(messages), &pos)
) {
for (i = 0; i < (*element)->size; i++) {
if ((*element)->scheme[i].original_name != NULL) {
efree((*element)->scheme[i].original_name);
}
if ((*element)->scheme[i].name != NULL) {
efree((*element)->scheme[i].name);
}
if ((*element)->scheme[i].mangled_name != NULL) {
efree((*element)->scheme[i].mangled_name);
}
if ((*element)->scheme[i].default_value != NULL) {
zval_ptr_dtor(&((*element)->scheme[i].default_value));
}
}
if ((*element)->single_property_name != NULL) {
efree((*element)->single_property_name);
}
if ((*element)->orig_single_property_name != NULL &&
memcmp((*element)->orig_single_property_name, php_protocolbuffers_get_default_single_property_name(), php_protocolbuffers_get_default_single_property_name_len()) != 0) {
efree((*element)->orig_single_property_name);
}
if ((*element)->scheme != NULL) {
efree((*element)->scheme);
}
if ((*element)->extensions != NULL) {
efree((*element)->extensions);
}
efree(*element);
}
zend_hash_destroy(PBG(messages));
FREE_HASHTABLE(PBG(messages));
PBG(messages) = NULL;
}
zend_end_try();
}
if (PBG(classes)) {
zend_try {
zend_hash_destroy(PBG(classes));
FREE_HASHTABLE(PBG(classes));
PBG(classes) = NULL;
}
zend_end_try();
}
if (PBG(extension_registry)) {
zval_ptr_dtor(&PBG(extension_registry));
}
return SUCCESS;
}
zend_module_entry protocolbuffers_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
"protocolbuffers",
NULL, /* Functions */
PHP_MINIT(protocolbuffers), /* MINIT */
PHP_MSHUTDOWN(protocolbuffers), /* MSHUTDOWN */
PHP_RINIT(protocolbuffers), /* RINIT */
PHP_RSHUTDOWN(protocolbuffers), /* RSHUTDOWN */
PHP_MINFO(protocolbuffers), /* MINFO */
#if ZEND_MODULE_API_NO >= 20010901
PHP_PROTOCOLBUFFERS_VERSION,
#endif
PHP_MODULE_GLOBALS(protocolbuffers),
PHP_GINIT(protocolbuffers),
PHP_GSHUTDOWN(protocolbuffers),
NULL,
STANDARD_MODULE_PROPERTIES_EX
};
#ifdef COMPILE_DL_PROTOCOLBUFFERS
ZEND_GET_MODULE(protocolbuffers)
#endif