aboutsummaryrefslogtreecommitdiff
path: root/php/ext/google/protobuf/protobuf.c
blob: da00302f2cb4039d303abeb9f732593d51b5de71 (plain) (blame)
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
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc.  All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// 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.
//     * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// 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 "protobuf.h"

#include <zend_hash.h>

ZEND_DECLARE_MODULE_GLOBALS(protobuf)
static PHP_GINIT_FUNCTION(protobuf);
static PHP_GSHUTDOWN_FUNCTION(protobuf);
static PHP_RINIT_FUNCTION(protobuf);
static PHP_RSHUTDOWN_FUNCTION(protobuf);
static PHP_MINIT_FUNCTION(protobuf);
static PHP_MSHUTDOWN_FUNCTION(protobuf);

// Global map from upb {msg,enum}defs to wrapper Descriptor/EnumDescriptor
// instances.
static HashTable* upb_def_to_php_obj_map;
// Global map from message/enum's php class entry to corresponding wrapper
// Descriptor/EnumDescriptor instances.
static HashTable* ce_to_php_obj_map;
// Global map from message/enum's proto fully-qualified name to corresponding
// wrapper Descriptor/EnumDescriptor instances.
static HashTable* proto_to_php_obj_map;
static HashTable* reserved_names;

// -----------------------------------------------------------------------------
// Global maps.
// -----------------------------------------------------------------------------

static void add_to_table(HashTable* t, const void* def, void* value) {
  uint nIndex = (ulong)def & t->nTableMask;

  zval* pDest = NULL;
  php_proto_zend_hash_index_update_mem(t, (zend_ulong)def, &value,
                                       sizeof(zval*), (void**)&pDest);
}

static void* get_from_table(const HashTable* t, const void* def) {
  void** value;
  if (php_proto_zend_hash_index_find_mem(t, (zend_ulong)def, (void**)&value) ==
      FAILURE) {
    return NULL;
  }
  return *value;
}

static bool exist_in_table(const HashTable* t, const void* def) {
  void** value;
  return (php_proto_zend_hash_index_find_mem(t, (zend_ulong)def,
                                             (void**)&value) == SUCCESS);
}

static void add_to_list(HashTable* t, void* value) {
  zval* pDest = NULL;
  php_proto_zend_hash_next_index_insert_mem(t, &value, sizeof(void*),
                                        (void**)&pDest);
}

static void add_to_strtable(HashTable* t, const char* key, int key_size,
                            void* value) {
  zval* pDest = NULL;
  php_proto_zend_hash_update_mem(t, key, key_size, &value, sizeof(void*),
                                 (void**)&pDest);
}

static void* get_from_strtable(const HashTable* t, const char* key, int key_size) {
  void** value;
  if (php_proto_zend_hash_find_mem(t, key, key_size, (void**)&value) ==
      FAILURE) {
    return NULL;
  }
  return *value;
}

void add_def_obj(const void* def, PHP_PROTO_HASHTABLE_VALUE value) {
#if PHP_MAJOR_VERSION < 7
  Z_ADDREF_P(value);
#else
  ++GC_REFCOUNT(value);
#endif
  add_to_table(upb_def_to_php_obj_map, def, value);
}

PHP_PROTO_HASHTABLE_VALUE get_def_obj(const void* def) {
  return (PHP_PROTO_HASHTABLE_VALUE)get_from_table(upb_def_to_php_obj_map, def);
}

void add_ce_obj(const void* ce, PHP_PROTO_HASHTABLE_VALUE value) {
#if PHP_MAJOR_VERSION < 7
  Z_ADDREF_P(value);
#else
  ++GC_REFCOUNT(value);
#endif
  add_to_table(ce_to_php_obj_map, ce, value);
}

PHP_PROTO_HASHTABLE_VALUE get_ce_obj(const void* ce) {
  return (PHP_PROTO_HASHTABLE_VALUE)get_from_table(ce_to_php_obj_map, ce);
}

bool class_added(const void* ce) {
  return exist_in_table(ce_to_php_obj_map, ce);
}

void add_proto_obj(const char* proto, PHP_PROTO_HASHTABLE_VALUE value) {
#if PHP_MAJOR_VERSION < 7
  Z_ADDREF_P(value);
#else
  ++GC_REFCOUNT(value);
#endif
  add_to_strtable(proto_to_php_obj_map, proto, strlen(proto), value);
}

PHP_PROTO_HASHTABLE_VALUE get_proto_obj(const char* proto) {
  return (PHP_PROTO_HASHTABLE_VALUE)get_from_strtable(proto_to_php_obj_map,
                                                      proto, strlen(proto));
}

// -----------------------------------------------------------------------------
// Well Known Types.
// -----------------------------------------------------------------------------

bool is_inited_file_any;
bool is_inited_file_api;
bool is_inited_file_duration;
bool is_inited_file_field_mask;
bool is_inited_file_empty;
bool is_inited_file_source_context;
bool is_inited_file_struct;
bool is_inited_file_timestamp;
bool is_inited_file_type;
bool is_inited_file_wrappers;

// -----------------------------------------------------------------------------
// Reserved Name.
// -----------------------------------------------------------------------------

// Although we already have kReservedNames, we still add them to hash table to
// speed up look up.
const char *const kReservedNames[] = {
    "abstract",   "and",        "array",        "as",           "break",
    "callable",   "case",       "catch",        "class",        "clone",
    "const",      "continue",   "declare",      "default",      "die",
    "do",         "echo",       "else",         "elseif",       "empty",
    "enddeclare", "endfor",     "endforeach",   "endif",        "endswitch",
    "endwhile",   "eval",       "exit",         "extends",      "final",
    "for",        "foreach",    "function",     "global",       "goto",
    "if",         "implements", "include",      "include_once", "instanceof",
    "insteadof",  "interface",  "isset",        "list",         "namespace",
    "new",        "or",         "print",        "private",      "protected",
    "public",     "require",    "require_once", "return",       "static",
    "switch",     "throw",      "trait",        "try",          "unset",
    "use",        "var",        "while",        "xor",          "int",
    "float",      "bool",       "string",       "true",         "false",
    "null",       "void",       "iterable"};
const int kReservedNamesSize = 73;

bool is_reserved_name(const char* name) {
  void** value;
  return (php_proto_zend_hash_find(reserved_names, name, strlen(name),
                                   (void**)&value) == SUCCESS);
}

// -----------------------------------------------------------------------------
// Utilities.
// -----------------------------------------------------------------------------

zend_function_entry protobuf_functions[] = {
  ZEND_FE_END
};

static const zend_module_dep protobuf_deps[] = {
  ZEND_MOD_OPTIONAL("date")
  ZEND_MOD_END
};

zend_module_entry protobuf_module_entry = {
  STANDARD_MODULE_HEADER_EX,
  NULL,
  protobuf_deps,
  PHP_PROTOBUF_EXTNAME,     // extension name
  protobuf_functions,       // function list
  PHP_MINIT(protobuf),      // process startup
  PHP_MSHUTDOWN(protobuf),  // process shutdown
  PHP_RINIT(protobuf),      // request shutdown
  PHP_RSHUTDOWN(protobuf),  // request shutdown
  NULL,                 // extension info
  PHP_PROTOBUF_VERSION, // extension version
  PHP_MODULE_GLOBALS(protobuf),  // globals descriptor
  PHP_GINIT(protobuf),  // globals ctor
  PHP_GSHUTDOWN(protobuf),  // globals dtor
  NULL,  // post deactivate
  STANDARD_MODULE_PROPERTIES_EX
};

// install module
ZEND_GET_MODULE(protobuf)

// global variables
static PHP_GINIT_FUNCTION(protobuf) {
}

static PHP_GSHUTDOWN_FUNCTION(protobuf) {
}

#if PHP_MAJOR_VERSION >= 7
static void php_proto_hashtable_descriptor_release(zval* value) {
  void* ptr = Z_PTR_P(value);
  zend_object* object = *(zend_object**)ptr;
  if(--GC_REFCOUNT(object) == 0) {
    zend_objects_store_del(object);
  }
  efree(ptr);
}
#endif

static PHP_RINIT_FUNCTION(protobuf) {
  int i = 0;

  ALLOC_HASHTABLE(upb_def_to_php_obj_map);
  zend_hash_init(upb_def_to_php_obj_map, 16, NULL, HASHTABLE_VALUE_DTOR, 0);

  ALLOC_HASHTABLE(ce_to_php_obj_map);
  zend_hash_init(ce_to_php_obj_map, 16, NULL, HASHTABLE_VALUE_DTOR, 0);

  ALLOC_HASHTABLE(proto_to_php_obj_map);
  zend_hash_init(proto_to_php_obj_map, 16, NULL, HASHTABLE_VALUE_DTOR, 0);

  ALLOC_HASHTABLE(reserved_names);
  zend_hash_init(reserved_names, 16, NULL, NULL, 0);
  for (i = 0; i < kReservedNamesSize; i++) {
    php_proto_zend_hash_update(reserved_names, kReservedNames[i],
                               strlen(kReservedNames[i]));
  }

  generated_pool = NULL;
  generated_pool_php = NULL;
  internal_generated_pool_php = NULL;

  is_inited_file_any = false;
  is_inited_file_api = false;
  is_inited_file_duration = false;
  is_inited_file_field_mask = false;
  is_inited_file_empty = false;
  is_inited_file_source_context = false;
  is_inited_file_struct = false;
  is_inited_file_timestamp = false;
  is_inited_file_type = false;
  is_inited_file_wrappers = false;

  return 0;
}

static PHP_RSHUTDOWN_FUNCTION(protobuf) {
  zend_hash_destroy(upb_def_to_php_obj_map);
  FREE_HASHTABLE(upb_def_to_php_obj_map);

  zend_hash_destroy(ce_to_php_obj_map);
  FREE_HASHTABLE(ce_to_php_obj_map);

  zend_hash_destroy(proto_to_php_obj_map);
  FREE_HASHTABLE(proto_to_php_obj_map);

  zend_hash_destroy(reserved_names);
  FREE_HASHTABLE(reserved_names);

#if PHP_MAJOR_VERSION < 7
  if (generated_pool_php != NULL) {
    zval_dtor(generated_pool_php);
    FREE_ZVAL(generated_pool_php);
  }
  if (internal_generated_pool_php != NULL) {
    zval_dtor(internal_generated_pool_php);
    FREE_ZVAL(internal_generated_pool_php);
  }
#else
  if (generated_pool_php != NULL) {
    zval tmp;
    ZVAL_OBJ(&tmp, generated_pool_php);
    zval_dtor(&tmp);
  }
  if (internal_generated_pool_php != NULL) {
    zval tmp;
    ZVAL_OBJ(&tmp, internal_generated_pool_php);
    zval_dtor(&tmp);
  }
#endif

  is_inited_file_any = true;
  is_inited_file_api = true;
  is_inited_file_duration = true;
  is_inited_file_field_mask = true;
  is_inited_file_empty = true;
  is_inited_file_source_context = true;
  is_inited_file_struct = true;
  is_inited_file_timestamp = true;
  is_inited_file_type = true;
  is_inited_file_wrappers = true;

  return 0;
}

static PHP_MINIT_FUNCTION(protobuf) {
  descriptor_pool_init(TSRMLS_C);
  descriptor_init(TSRMLS_C);
  enum_descriptor_init(TSRMLS_C);
  enum_value_descriptor_init(TSRMLS_C);
  field_descriptor_init(TSRMLS_C);
  gpb_type_init(TSRMLS_C);
  internal_descriptor_pool_init(TSRMLS_C);
  map_field_init(TSRMLS_C);
  map_field_iter_init(TSRMLS_C);
  message_init(TSRMLS_C);
  oneof_descriptor_init(TSRMLS_C);
  repeated_field_init(TSRMLS_C);
  repeated_field_iter_init(TSRMLS_C);
  util_init(TSRMLS_C);

  gpb_metadata_any_init(TSRMLS_C);
  gpb_metadata_api_init(TSRMLS_C);
  gpb_metadata_duration_init(TSRMLS_C);
  gpb_metadata_field_mask_init(TSRMLS_C);
  gpb_metadata_empty_init(TSRMLS_C);
  gpb_metadata_source_context_init(TSRMLS_C);
  gpb_metadata_struct_init(TSRMLS_C);
  gpb_metadata_timestamp_init(TSRMLS_C);
  gpb_metadata_type_init(TSRMLS_C);
  gpb_metadata_wrappers_init(TSRMLS_C);

  any_init(TSRMLS_C);
  api_init(TSRMLS_C);
  bool_value_init(TSRMLS_C);
  bytes_value_init(TSRMLS_C);
  double_value_init(TSRMLS_C);
  duration_init(TSRMLS_C);
  enum_init(TSRMLS_C);
  enum_value_init(TSRMLS_C);
  field_cardinality_init(TSRMLS_C);
  field_init(TSRMLS_C);
  field_kind_init(TSRMLS_C);
  field_mask_init(TSRMLS_C);
  float_value_init(TSRMLS_C);
  empty_init(TSRMLS_C);
  int32_value_init(TSRMLS_C);
  int64_value_init(TSRMLS_C);
  list_value_init(TSRMLS_C);
  method_init(TSRMLS_C);
  mixin_init(TSRMLS_C);
  null_value_init(TSRMLS_C);
  option_init(TSRMLS_C);
  source_context_init(TSRMLS_C);
  string_value_init(TSRMLS_C);
  struct_init(TSRMLS_C);
  syntax_init(TSRMLS_C);
  timestamp_init(TSRMLS_C);
  type_init(TSRMLS_C);
  u_int32_value_init(TSRMLS_C);
  u_int64_value_init(TSRMLS_C);
  value_init(TSRMLS_C);

  return 0;
}

static PHP_MSHUTDOWN_FUNCTION(protobuf) {
  PEFREE(message_handlers);
  PEFREE(repeated_field_handlers);
  PEFREE(repeated_field_iter_handlers);
  PEFREE(map_field_handlers);
  PEFREE(map_field_iter_handlers);

  return 0;
}