• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.  All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 //     * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 //     * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 //     * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 #ifndef __GOOGLE_PROTOBUF_PHP_PROTOBUF_H__
32 #define __GOOGLE_PROTOBUF_PHP_PROTOBUF_H__
33 
34 #include <php.h>
35 
36 // ubp.h has to be placed after php.h. Othwise, php.h will introduce NDEBUG.
37 #include "upb.h"
38 
39 #define PHP_PROTOBUF_EXTNAME "protobuf"
40 #define PHP_PROTOBUF_VERSION "3.9.1"
41 
42 #define MAX_LENGTH_OF_INT64 20
43 #define SIZEOF_INT64 8
44 
45 /* From Chromium. */
46 #define ARRAY_SIZE(x) \
47     ((sizeof(x)/sizeof(0[x])) / ((size_t)(!(sizeof(x) % sizeof(0[x])))))
48 
49 // -----------------------------------------------------------------------------
50 // PHP7 Wrappers
51 // ----------------------------------------------------------------------------
52 
53 #if PHP_VERSION_ID < 70300
54 #define GC_ADDREF(h) ++GC_REFCOUNT(h)
55 #define GC_DELREF(h) --GC_REFCOUNT(h)
56 #endif
57 
58 #if PHP_MAJOR_VERSION < 7
59 
60 #define php_proto_zend_literal const zend_literal*
61 #define PHP_PROTO_CASE_IS_BOOL IS_BOOL
62 #define PHP_PROTO_SIZE int
63 #define PHP_PROTO_LONG long
64 #define PHP_PROTO_TSRMLS_DC TSRMLS_DC
65 #define PHP_PROTO_TSRMLS_CC TSRMLS_CC
66 
67 // PHP String
68 
69 #define PHP_PROTO_ZVAL_STRING(zval_ptr, s, copy) \
70   ZVAL_STRING(zval_ptr, s, copy)
71 #define PHP_PROTO_ZVAL_STRINGL(zval_ptr, s, len, copy) \
72   ZVAL_STRINGL(zval_ptr, s, len, copy)
73 #define PHP_PROTO_RETURN_STRING(s, copy) RETURN_STRING(s, copy)
74 #define PHP_PROTO_RETURN_STRINGL(s, len, copy) RETURN_STRINGL(s, len, copy)
75 #define PHP_PROTO_RETVAL_STRINGL(s, len, copy) RETVAL_STRINGL(s, len, copy)
76 #define php_proto_zend_make_printable_zval(from, to) \
77   {                                                  \
78     int use_copy;                                    \
79     zend_make_printable_zval(from, to, &use_copy);   \
80   }
81 
82 // PHP Array
83 
84 #define PHP_PROTO_HASH_OF(array) Z_ARRVAL_P(array)
85 
86 #define php_proto_zend_hash_index_update_zval(ht, h, pData) \
87   zend_hash_index_update(ht, h, &(pData), sizeof(void*), NULL)
88 
89 #define php_proto_zend_hash_update_zval(ht, key, key_len, value) \
90   zend_hash_update(ht, key, key_len, value, sizeof(void*), NULL)
91 
92 #define php_proto_zend_hash_update(ht, key, key_len) \
93   zend_hash_update(ht, key, key_len, 0, 0, NULL)
94 
95 #define php_proto_zend_hash_index_update_mem(ht, h, pData, nDataSize, pDest) \
96   zend_hash_index_update(ht, h, pData, nDataSize, pDest)
97 
98 #define php_proto_zend_hash_update_mem(ht, key, key_len, pData, nDataSize, \
99                                        pDest)                              \
100   zend_hash_update(ht, key, key_len, pData, nDataSize, pDest)
101 
102 #define php_proto_zend_hash_index_find_zval(ht, h, pDest) \
103   zend_hash_index_find(ht, h, pDest)
104 
105 #define php_proto_zend_hash_find(ht, key, key_len, pDest) \
106   zend_hash_find(ht, key, key_len, pDest)
107 
108 #define php_proto_zend_hash_index_find_mem(ht, h, pDest) \
109   zend_hash_index_find(ht, h, pDest)
110 
111 #define php_proto_zend_hash_find_zval(ht, key, key_len, pDest) \
112   zend_hash_find(ht, key, key_len, pDest)
113 
114 #define php_proto_zend_hash_find_mem(ht, key, key_len, pDest) \
115   zend_hash_find(ht, key, key_len, pDest)
116 
117 #define php_proto_zend_hash_next_index_insert_zval(ht, pData) \
118   zend_hash_next_index_insert(ht, pData, sizeof(void*), NULL)
119 
120 #define php_proto_zend_hash_next_index_insert_mem(ht, pData, nDataSize, pDest) \
121   zend_hash_next_index_insert(ht, pData, nDataSize, pDest)
122 
123 #define php_proto_zend_hash_get_current_data_ex(ht, pDest, pos) \
124   zend_hash_get_current_data_ex(ht, pDest, pos)
125 
126 // PHP Object
127 
128 #define PHP_PROTO_WRAP_OBJECT_START(name) \
129   struct name {                           \
130     zend_object std;
131 #define PHP_PROTO_WRAP_OBJECT_END \
132   };
133 
134 #define PHP_PROTO_INIT_SUBMSGCLASS_START(CLASSNAME, CAMELNAME, LOWWERNAME)   \
135   void LOWWERNAME##_init(TSRMLS_D) {                                         \
136     zend_class_entry class_type;                                             \
137     const char* class_name = CLASSNAME;                                      \
138     INIT_CLASS_ENTRY_EX(class_type, CLASSNAME, strlen(CLASSNAME),            \
139                         LOWWERNAME##_methods);                               \
140     LOWWERNAME##_type = zend_register_internal_class_ex(                     \
141         &class_type, message_type, NULL TSRMLS_CC);                          \
142     LOWWERNAME##_type->create_object = message_create;                       \
143     zend_do_inheritance(LOWWERNAME##_type, message_type TSRMLS_CC);
144 #define PHP_PROTO_INIT_SUBMSGCLASS_END \
145   }
146 
147 #define PHP_PROTO_INIT_ENUMCLASS_START(CLASSNAME, CAMELNAME, LOWWERNAME)     \
148   void LOWWERNAME##_init(TSRMLS_D) {                                         \
149     zend_class_entry class_type;                                             \
150     const char* class_name = CLASSNAME;                                      \
151     INIT_CLASS_ENTRY_EX(class_type, CLASSNAME, strlen(CLASSNAME),            \
152                         LOWWERNAME##_methods);                               \
153     LOWWERNAME##_type = zend_register_internal_class(&class_type TSRMLS_CC);
154 #define PHP_PROTO_INIT_ENUMCLASS_END \
155   }
156 
157 #define PHP_PROTO_INIT_CLASS_START(CLASSNAME, CAMELNAME, LOWWERNAME)         \
158   void LOWWERNAME##_init(TSRMLS_D) {                                         \
159     zend_class_entry class_type;                                             \
160     const char* class_name = CLASSNAME;                                      \
161     INIT_CLASS_ENTRY_EX(class_type, CLASSNAME, strlen(CLASSNAME),            \
162                         LOWWERNAME##_methods);                               \
163     LOWWERNAME##_type = zend_register_internal_class(&class_type TSRMLS_CC); \
164     LOWWERNAME##_type->create_object = LOWWERNAME##_create;                  \
165     LOWWERNAME##_handlers = PEMALLOC(zend_object_handlers);                  \
166     memcpy(LOWWERNAME##_handlers, zend_get_std_object_handlers(),            \
167            sizeof(zend_object_handlers));
168 #define PHP_PROTO_INIT_CLASS_END \
169   }
170 
171 #define PHP_PROTO_OBJECT_CREATE_START(NAME, LOWWERNAME) \
172   static zend_object_value LOWWERNAME##_create(         \
173       zend_class_entry* ce TSRMLS_DC) {                 \
174     PHP_PROTO_ALLOC_CLASS_OBJECT(NAME, ce);             \
175     zend_object_std_init(&intern->std, ce TSRMLS_CC);   \
176     object_properties_init(&intern->std, ce);
177 #define PHP_PROTO_OBJECT_CREATE_END(NAME, LOWWERNAME)                          \
178   PHP_PROTO_FREE_CLASS_OBJECT(NAME, LOWWERNAME##_free, LOWWERNAME##_handlers); \
179   }
180 
181 #define PHP_PROTO_OBJECT_FREE_START(classname, lowername) \
182   void lowername##_free(void* object TSRMLS_DC) {         \
183     classname* intern = object;
184 #define PHP_PROTO_OBJECT_FREE_END                 \
185     zend_object_std_dtor(&intern->std TSRMLS_CC); \
186     efree(intern);                                \
187   }
188 
189 #define PHP_PROTO_OBJECT_DTOR_START(classname, lowername)
190 #define PHP_PROTO_OBJECT_DTOR_END
191 
192 #define CACHED_VALUE zval*
193 #define CACHED_TO_ZVAL_PTR(VALUE) (VALUE)
194 #define CACHED_PTR_TO_ZVAL_PTR(VALUE) (*VALUE)
195 #define ZVAL_PTR_TO_CACHED_PTR(VALUE) (&VALUE)
196 #define ZVAL_PTR_TO_CACHED_VALUE(VALUE) (VALUE)
197 #define ZVAL_TO_CACHED_VALUE(VALUE) (&VALUE)
198 
199 #define CREATE_OBJ_ON_ALLOCATED_ZVAL_PTR(zval_ptr, class_type) \
200   ZVAL_OBJ(zval_ptr, class_type->create_object(class_type TSRMLS_CC));
201 
202 #define PHP_PROTO_SEPARATE_ZVAL_IF_NOT_REF(value) \
203   SEPARATE_ZVAL_IF_NOT_REF(value)
204 
205 #define PHP_PROTO_GLOBAL_UNINITIALIZED_ZVAL EG(uninitialized_zval_ptr)
206 
207 #define OBJ_PROP(OBJECT, OFFSET) &((OBJECT)->properties_table[OFFSET])
208 
209 #define php_proto_zval_ptr_dtor(zval_ptr) \
210   zval_ptr_dtor(&(zval_ptr))
211 
212 #define PHP_PROTO_ALLOC_CLASS_OBJECT(class_object, class_type) \
213   class_object* intern;                                        \
214   intern = (class_object*)emalloc(sizeof(class_object));       \
215   memset(intern, 0, sizeof(class_object));
216 
217 #define PHP_PROTO_FREE_CLASS_OBJECT(class_object, class_object_free, handler) \
218   zend_object_value retval = {0};                                             \
219   retval.handle = zend_objects_store_put(                                     \
220       intern, (zend_objects_store_dtor_t)zend_objects_destroy_object,         \
221       class_object_free, NULL TSRMLS_CC);                                     \
222   retval.handlers = handler;                                                  \
223   return retval;
224 
225 #define PHP_PROTO_ALLOC_ARRAY(zval_ptr)  \
226   ALLOC_HASHTABLE(Z_ARRVAL_P(zval_ptr)); \
227   Z_TYPE_P(zval_ptr) = IS_ARRAY;
228 
229 #define ZVAL_OBJ(zval_ptr, call_create) \
230   Z_TYPE_P(zval_ptr) = IS_OBJECT;       \
231   Z_OBJVAL_P(zval_ptr) = call_create;
232 
233 #define UNBOX(class_name, val) \
234   (class_name*)zend_object_store_get_object(val TSRMLS_CC);
235 
236 #define UNBOX_HASHTABLE_VALUE(class_name, val) UNBOX(class_name, val)
237 
238 #define HASHTABLE_VALUE_DTOR ZVAL_PTR_DTOR
239 
240 #define PHP_PROTO_HASHTABLE_VALUE zval*
241 #define HASHTABLE_VALUE_CE(val) Z_OBJCE_P(val)
242 
243 #define CREATE_HASHTABLE_VALUE(OBJ, WRAPPED_OBJ, OBJ_TYPE, OBJ_CLASS_ENTRY) \
244   OBJ_TYPE* OBJ;                                                            \
245   PHP_PROTO_HASHTABLE_VALUE WRAPPED_OBJ;                                    \
246   MAKE_STD_ZVAL(WRAPPED_OBJ);                                               \
247   ZVAL_OBJ(WRAPPED_OBJ,                                                     \
248            OBJ_CLASS_ENTRY->create_object(OBJ_CLASS_ENTRY TSRMLS_CC));      \
249   OBJ = UNBOX_HASHTABLE_VALUE(OBJ_TYPE, WRAPPED_OBJ);                       \
250   Z_DELREF_P(desc_php);
251 
252 #define PHP_PROTO_CE_DECLARE zend_class_entry**
253 #define PHP_PROTO_CE_UNREF(ce) (*ce)
254 
255 #define php_proto_zend_lookup_class(name, name_length, ce) \
256   zend_lookup_class(name, name_length, ce TSRMLS_CC)
257 
258 #define PHP_PROTO_RETVAL_ZVAL(value) ZVAL_ZVAL(return_value, value, 1, 0)
259 
260 #else  // PHP_MAJOR_VERSION >= 7
261 
262 #define php_proto_zend_literal void**
263 #define PHP_PROTO_CASE_IS_BOOL IS_TRUE: case IS_FALSE
264 #define PHP_PROTO_SIZE size_t
265 #define PHP_PROTO_LONG zend_long
266 #define PHP_PROTO_TSRMLS_DC
267 #define PHP_PROTO_TSRMLS_CC
268 
269 // PHP String
270 
271 #define PHP_PROTO_ZVAL_STRING(zval_ptr, s, copy) \
272   ZVAL_STRING(zval_ptr, s)
273 #define PHP_PROTO_ZVAL_STRINGL(zval_ptr, s, len, copy) \
274   ZVAL_STRINGL(zval_ptr, s, len)
275 #define PHP_PROTO_RETURN_STRING(s, copy) RETURN_STRING(s)
276 #define PHP_PROTO_RETURN_STRINGL(s, len, copy) RETURN_STRINGL(s, len)
277 #define PHP_PROTO_RETVAL_STRINGL(s, len, copy) RETVAL_STRINGL(s, len)
278 #define php_proto_zend_make_printable_zval(from, to) \
279   zend_make_printable_zval(from, to)
280 
281 // PHP Array
282 
283 #define PHP_PROTO_HASH_OF(array) Z_ARRVAL_P(&array)
284 
php_proto_zend_hash_index_update_zval(HashTable * ht,ulong h,zval * pData)285 static inline int php_proto_zend_hash_index_update_zval(HashTable* ht, ulong h,
286                                                         zval* pData) {
287   void* result = NULL;
288   result = zend_hash_index_update(ht, h, pData);
289   return result != NULL ? SUCCESS : FAILURE;
290 }
291 
php_proto_zend_hash_update(HashTable * ht,const char * key,size_t key_len)292 static inline int php_proto_zend_hash_update(HashTable* ht, const char* key,
293                                              size_t key_len) {
294   void* result = NULL;
295   zval temp;
296   ZVAL_LONG(&temp, 0);
297   result = zend_hash_str_update(ht, key, key_len, &temp);
298   return result != NULL ? SUCCESS : FAILURE;
299 }
300 
php_proto_zend_hash_index_update_mem(HashTable * ht,ulong h,void * pData,uint nDataSize,void ** pDest)301 static inline int php_proto_zend_hash_index_update_mem(HashTable* ht, ulong h,
302                                                    void* pData, uint nDataSize,
303                                                    void** pDest) {
304   void* result = NULL;
305   result = zend_hash_index_update_mem(ht, h, pData, nDataSize);
306   if (pDest != NULL) *pDest = result;
307   return result != NULL ? SUCCESS : FAILURE;
308 }
309 
php_proto_zend_hash_update_zval(HashTable * ht,const char * key,uint key_len,zval * pData)310 static inline int php_proto_zend_hash_update_zval(HashTable* ht,
311                                                   const char* key, uint key_len,
312                                                   zval* pData) {
313   void* result = NULL;
314   zend_string* internal_key = zend_string_init(key, key_len, 0);
315   result = zend_hash_update(ht, internal_key, pData);
316   return result != NULL ? SUCCESS : FAILURE;
317 }
318 
php_proto_zend_hash_update_mem(HashTable * ht,const char * key,uint key_len,void * pData,uint nDataSize,void ** pDest)319 static inline int php_proto_zend_hash_update_mem(HashTable* ht, const char* key,
320                                                  uint key_len, void* pData,
321                                                  uint nDataSize, void** pDest) {
322   zend_string* internal_key = zend_string_init(key, key_len, 0);
323   void* result = zend_hash_update_mem(ht, internal_key, pData, nDataSize);
324   zend_string_release(internal_key);
325   if (pDest != NULL) *pDest = result;
326   return result != NULL ? SUCCESS : FAILURE;
327 }
328 
php_proto_zend_hash_index_find_zval(const HashTable * ht,ulong h,void ** pDest)329 static inline int php_proto_zend_hash_index_find_zval(const HashTable* ht,
330                                                       ulong h, void** pDest) {
331   zval* result = zend_hash_index_find(ht, h);
332   if (pDest != NULL) *pDest = result;
333   return result != NULL ? SUCCESS : FAILURE;
334 }
335 
php_proto_zend_hash_find(const HashTable * ht,const char * key,size_t key_len,void ** pDest)336 static inline int php_proto_zend_hash_find(const HashTable* ht, const char* key,
337                                            size_t key_len, void** pDest) {
338   void* result = NULL;
339   result = zend_hash_str_find(ht, key, key_len);
340   return result != NULL ? SUCCESS : FAILURE;
341 }
342 
php_proto_zend_hash_index_find_mem(const HashTable * ht,ulong h,void ** pDest)343 static inline int php_proto_zend_hash_index_find_mem(const HashTable* ht,
344                                                      ulong h, void** pDest) {
345   void* result = NULL;
346   result = zend_hash_index_find_ptr(ht, h);
347   if (pDest != NULL) *pDest = result;
348   return result != NULL ? SUCCESS : FAILURE;
349 }
350 
php_proto_zend_hash_find_zval(const HashTable * ht,const char * key,uint key_len,void ** pDest)351 static inline int php_proto_zend_hash_find_zval(const HashTable* ht,
352                                                 const char* key, uint key_len,
353                                                 void** pDest) {
354   zend_string* internal_key = zend_string_init(key, key_len, 1);
355   zval* result = zend_hash_find(ht, internal_key);
356   if (pDest != NULL) *pDest = result;
357   return result != NULL ? SUCCESS : FAILURE;
358 }
359 
php_proto_zend_hash_find_mem(const HashTable * ht,const char * key,uint key_len,void ** pDest)360 static inline int php_proto_zend_hash_find_mem(const HashTable* ht,
361                                                 const char* key, uint key_len,
362                                                 void** pDest) {
363   zend_string* internal_key = zend_string_init(key, key_len, 1);
364   void* result = zend_hash_find_ptr(ht, internal_key);
365   zend_string_release(internal_key);
366   if (pDest != NULL) *pDest = result;
367   return result != NULL ? SUCCESS : FAILURE;
368 }
369 
php_proto_zend_hash_next_index_insert_zval(HashTable * ht,void * pData)370 static inline int php_proto_zend_hash_next_index_insert_zval(HashTable* ht,
371                                                              void* pData) {
372   zval tmp;
373   ZVAL_OBJ(&tmp, *(zend_object**)pData);
374   zval* result = zend_hash_next_index_insert(ht, &tmp);
375   return result != NULL ? SUCCESS : FAILURE;
376 }
377 
php_proto_zend_hash_next_index_insert_mem(HashTable * ht,void * pData,uint nDataSize,void ** pDest)378 static inline int php_proto_zend_hash_next_index_insert_mem(HashTable* ht,
379                                                             void* pData,
380                                                             uint nDataSize,
381                                                             void** pDest) {
382   void* result = NULL;
383   result = zend_hash_next_index_insert_mem(ht, pData, nDataSize);
384   if (pDest != NULL) *pDest = result;
385   return result != NULL ? SUCCESS : FAILURE;
386 }
387 
php_proto_zend_hash_get_current_data_ex(HashTable * ht,void ** pDest,HashPosition * pos)388 static inline int php_proto_zend_hash_get_current_data_ex(HashTable* ht,
389                                                           void** pDest,
390                                                           HashPosition* pos) {
391   void* result = NULL;
392   result = zend_hash_get_current_data_ex(ht, pos);
393   if (pDest != NULL) *pDest = result;
394   return result != NULL ? SUCCESS : FAILURE;
395 }
396 
397 // PHP Object
398 
399 #define PHP_PROTO_WRAP_OBJECT_START(name) struct name {
400 #define PHP_PROTO_WRAP_OBJECT_END \
401   zend_object std;                \
402   };
403 
404 #define PHP_PROTO_INIT_SUBMSGCLASS_START(CLASSNAME, CAMELNAME, LOWWERNAME)   \
405   void LOWWERNAME##_init(TSRMLS_D) {                                         \
406     zend_class_entry class_type;                                             \
407     const char* class_name = CLASSNAME;                                      \
408     INIT_CLASS_ENTRY_EX(class_type, CLASSNAME, strlen(CLASSNAME),            \
409                         LOWWERNAME##_methods);                               \
410     LOWWERNAME##_type = zend_register_internal_class_ex(                     \
411         &class_type, message_type TSRMLS_CC);                                \
412     zend_do_inheritance(LOWWERNAME##_type, message_type TSRMLS_CC);
413 #define PHP_PROTO_INIT_SUBMSGCLASS_END \
414   }
415 
416 #define PHP_PROTO_INIT_ENUMCLASS_START(CLASSNAME, CAMELNAME, LOWWERNAME)     \
417   void LOWWERNAME##_init(TSRMLS_D) {                                         \
418     zend_class_entry class_type;                                             \
419     const char* class_name = CLASSNAME;                                      \
420     INIT_CLASS_ENTRY_EX(class_type, CLASSNAME, strlen(CLASSNAME),            \
421                         LOWWERNAME##_methods);                               \
422     LOWWERNAME##_type = zend_register_internal_class(&class_type TSRMLS_CC);
423 #define PHP_PROTO_INIT_ENUMCLASS_END \
424   }
425 
426 #define PHP_PROTO_INIT_CLASS_START(CLASSNAME, CAMELNAME, LOWWERNAME)         \
427   void LOWWERNAME##_init(TSRMLS_D) {                                         \
428     zend_class_entry class_type;                                             \
429     const char* class_name = CLASSNAME;                                      \
430     INIT_CLASS_ENTRY_EX(class_type, CLASSNAME, strlen(CLASSNAME),            \
431                         LOWWERNAME##_methods);                               \
432     LOWWERNAME##_type = zend_register_internal_class(&class_type TSRMLS_CC); \
433     LOWWERNAME##_type->create_object = LOWWERNAME##_create;                  \
434     LOWWERNAME##_handlers = PEMALLOC(zend_object_handlers);                  \
435     memcpy(LOWWERNAME##_handlers, zend_get_std_object_handlers(),            \
436            sizeof(zend_object_handlers));                                    \
437     LOWWERNAME##_handlers->free_obj = LOWWERNAME##_free;                     \
438     LOWWERNAME##_handlers->dtor_obj = LOWWERNAME##_dtor;                     \
439     LOWWERNAME##_handlers->offset = XtOffsetOf(CAMELNAME, std);
440 #define PHP_PROTO_INIT_CLASS_END \
441   }
442 
443 #define PHP_PROTO_OBJECT_FREE_START(classname, lowername) \
444   void lowername##_free(zend_object* object) {            \
445     classname* intern =                                   \
446         (classname*)((char*)object - XtOffsetOf(classname, std));
447 #define PHP_PROTO_OBJECT_FREE_END           \
448   }
449 
450 #define PHP_PROTO_OBJECT_DTOR_START(classname, lowername) \
451   void lowername##_dtor(zend_object* object) {            \
452     classname* intern =                                   \
453         (classname*)((char*)object - XtOffsetOf(classname, std));
454 #define PHP_PROTO_OBJECT_DTOR_END           \
455     zend_object_std_dtor(object TSRMLS_CC); \
456   }
457 
458 #define PHP_PROTO_OBJECT_CREATE_START(NAME, LOWWERNAME)                     \
459   static zend_object* LOWWERNAME##_create(zend_class_entry* ce TSRMLS_DC) { \
460     PHP_PROTO_ALLOC_CLASS_OBJECT(NAME, ce);                                 \
461     zend_object_std_init(&intern->std, ce TSRMLS_CC);                       \
462     object_properties_init(&intern->std, ce);
463 #define PHP_PROTO_OBJECT_CREATE_END(NAME, LOWWERNAME)                          \
464   PHP_PROTO_FREE_CLASS_OBJECT(NAME, LOWWERNAME##_free, LOWWERNAME##_handlers); \
465   }
466 
467 #define CACHED_VALUE zval
468 #define CACHED_TO_ZVAL_PTR(VALUE) (&VALUE)
469 #define CACHED_PTR_TO_ZVAL_PTR(VALUE) (VALUE)
470 #define ZVAL_PTR_TO_CACHED_PTR(VALUE) (VALUE)
471 #define ZVAL_PTR_TO_CACHED_VALUE(VALUE) (*VALUE)
472 #define ZVAL_TO_CACHED_VALUE(VALUE) (VALUE)
473 
474 #define CREATE_OBJ_ON_ALLOCATED_ZVAL_PTR(zval_ptr, class_type) \
475   ZVAL_OBJ(zval_ptr, class_type->create_object(class_type));
476 
477 #define PHP_PROTO_SEPARATE_ZVAL_IF_NOT_REF(value) ;
478 
479 #define PHP_PROTO_GLOBAL_UNINITIALIZED_ZVAL &EG(uninitialized_zval)
480 
481 #define php_proto_zval_ptr_dtor(zval_ptr) \
482   zval_ptr_dtor(zval_ptr)
483 
484 #define PHP_PROTO_ALLOC_CLASS_OBJECT(class_object, class_type)               \
485   class_object* intern;                                                      \
486   int size = sizeof(class_object) + zend_object_properties_size(class_type); \
487   intern = ecalloc(1, size);                                                 \
488   memset(intern, 0, size);
489 
490 #define PHP_PROTO_FREE_CLASS_OBJECT(class_object, class_object_free, handler) \
491   intern->std.handlers = handler;                                             \
492   return &intern->std;
493 
494 #define PHP_PROTO_ALLOC_ARRAY(zval_ptr) \
495   ZVAL_NEW_ARR(zval_ptr)
496 
497 #define UNBOX(class_name, val) \
498   (class_name*)((char*)Z_OBJ_P(val) - XtOffsetOf(class_name, std));
499 
500 #define UNBOX_HASHTABLE_VALUE(class_name, val) \
501   (class_name*)((char*)val - XtOffsetOf(class_name, std))
502 
503 #define HASHTABLE_VALUE_DTOR php_proto_hashtable_descriptor_release
504 
505 #define PHP_PROTO_HASHTABLE_VALUE zend_object*
506 #define HASHTABLE_VALUE_CE(val) val->ce
507 
508 #define CREATE_HASHTABLE_VALUE(OBJ, WRAPPED_OBJ, OBJ_TYPE, OBJ_CLASS_ENTRY) \
509   OBJ_TYPE* OBJ;                                                            \
510   PHP_PROTO_HASHTABLE_VALUE WRAPPED_OBJ;                                    \
511   WRAPPED_OBJ = OBJ_CLASS_ENTRY->create_object(OBJ_CLASS_ENTRY);            \
512   OBJ = UNBOX_HASHTABLE_VALUE(OBJ_TYPE, WRAPPED_OBJ);                       \
513   GC_DELREF(WRAPPED_OBJ);
514 
515 #define PHP_PROTO_CE_DECLARE zend_class_entry*
516 #define PHP_PROTO_CE_UNREF(ce) (ce)
517 
php_proto_zend_lookup_class(const char * name,int name_length,zend_class_entry ** ce TSRMLS_DC)518 static inline int php_proto_zend_lookup_class(
519     const char* name, int name_length, zend_class_entry** ce TSRMLS_DC) {
520   zend_string *zstr_name = zend_string_init(name, name_length, 0);
521   *ce = zend_lookup_class(zstr_name);
522   zend_string_release(zstr_name);
523   return *ce != NULL ? SUCCESS : FAILURE;
524 }
525 
526 #define PHP_PROTO_RETVAL_ZVAL(value) ZVAL_COPY(return_value, value)
527 
528 #endif  // PHP_MAJOR_VERSION >= 7
529 
530 #if PHP_MAJOR_VERSION < 7 || (PHP_MAJOR_VERSION == 7 && PHP_MINOR_VERSION == 0)
531 #define PHP_PROTO_FAKE_SCOPE_BEGIN(klass)  \
532   zend_class_entry* old_scope = EG(scope); \
533   EG(scope) = klass;
534 #define PHP_PROTO_FAKE_SCOPE_RESTART(klass) \
535   old_scope = EG(scope);                    \
536   EG(scope) = klass;
537 #define PHP_PROTO_FAKE_SCOPE_END EG(scope) = old_scope;
538 #else
539 #define PHP_PROTO_FAKE_SCOPE_BEGIN(klass)       \
540   zend_class_entry* old_scope = EG(fake_scope); \
541   EG(fake_scope) = klass;
542 #define PHP_PROTO_FAKE_SCOPE_RESTART(klass) \
543   old_scope = EG(fake_scope);               \
544   EG(fake_scope) = klass;
545 #define PHP_PROTO_FAKE_SCOPE_END EG(fake_scope) = old_scope;
546 #endif
547 
548 // Define PHP class
549 #define DEFINE_PROTOBUF_INIT_CLASS(CLASSNAME, CAMELNAME, LOWERNAME) \
550   PHP_PROTO_INIT_CLASS_START(CLASSNAME, CAMELNAME, LOWERNAME)       \
551   PHP_PROTO_INIT_CLASS_END
552 
553 #define DEFINE_PROTOBUF_CREATE(NAME, LOWERNAME)  \
554   PHP_PROTO_OBJECT_CREATE_START(NAME, LOWERNAME) \
555   LOWERNAME##_init_c_instance(intern TSRMLS_CC); \
556   PHP_PROTO_OBJECT_CREATE_END(NAME, LOWERNAME)
557 
558 #define DEFINE_PROTOBUF_FREE(CAMELNAME, LOWERNAME)  \
559   PHP_PROTO_OBJECT_FREE_START(CAMELNAME, LOWERNAME) \
560   LOWERNAME##_free_c(intern TSRMLS_CC);             \
561   PHP_PROTO_OBJECT_FREE_END
562 
563 #define DEFINE_PROTOBUF_DTOR(CAMELNAME, LOWERNAME)  \
564   PHP_PROTO_OBJECT_DTOR_START(CAMELNAME, LOWERNAME) \
565   PHP_PROTO_OBJECT_DTOR_END
566 
567 #define DEFINE_CLASS(NAME, LOWERNAME, string_name) \
568   zend_class_entry *LOWERNAME##_type;              \
569   zend_object_handlers *LOWERNAME##_handlers;      \
570   DEFINE_PROTOBUF_FREE(NAME, LOWERNAME)            \
571   DEFINE_PROTOBUF_DTOR(NAME, LOWERNAME)            \
572   DEFINE_PROTOBUF_CREATE(NAME, LOWERNAME)          \
573   DEFINE_PROTOBUF_INIT_CLASS(string_name, NAME, LOWERNAME)
574 
575 // -----------------------------------------------------------------------------
576 // Forward Declaration
577 // ----------------------------------------------------------------------------
578 
579 struct Any;
580 struct Api;
581 struct BoolValue;
582 struct BytesValue;
583 struct Descriptor;
584 struct DescriptorPool;
585 struct DoubleValue;
586 struct Duration;
587 struct Enum;
588 struct EnumDescriptor;
589 struct EnumValue;
590 struct EnumValueDescriptor;
591 struct Field;
592 struct FieldDescriptor;
593 struct FieldMask;
594 struct Field_Cardinality;
595 struct Field_Kind;
596 struct FloatValue;
597 struct GPBEmpty;
598 struct Int32Value;
599 struct Int64Value;
600 struct InternalDescriptorPool;
601 struct ListValue;
602 struct Map;
603 struct MapIter;
604 struct MessageField;
605 struct MessageHeader;
606 struct MessageLayout;
607 struct Method;
608 struct Mixin;
609 struct NullValue;
610 struct Oneof;
611 struct Option;
612 struct RepeatedField;
613 struct RepeatedFieldIter;
614 struct SourceContext;
615 struct StringValue;
616 struct Struct;
617 struct Syntax;
618 struct Timestamp;
619 struct Type;
620 struct UInt32Value;
621 struct UInt64Value;
622 struct Value;
623 
624 typedef struct Any Any;
625 typedef struct Api Api;
626 typedef struct BoolValue BoolValue;
627 typedef struct BytesValue BytesValue;
628 typedef struct Descriptor Descriptor;
629 typedef struct DescriptorPool DescriptorPool;
630 typedef struct DoubleValue DoubleValue;
631 typedef struct Duration Duration;
632 typedef struct EnumDescriptor EnumDescriptor;
633 typedef struct Enum Enum;
634 typedef struct EnumValueDescriptor EnumValueDescriptor;
635 typedef struct EnumValue EnumValue;
636 typedef struct Field_Cardinality Field_Cardinality;
637 typedef struct FieldDescriptor FieldDescriptor;
638 typedef struct Field Field;
639 typedef struct Field_Kind Field_Kind;
640 typedef struct FieldMask FieldMask;
641 typedef struct FloatValue FloatValue;
642 typedef struct GPBEmpty GPBEmpty;
643 typedef struct Int32Value Int32Value;
644 typedef struct Int64Value Int64Value;
645 typedef struct InternalDescriptorPool InternalDescriptorPool;
646 typedef struct ListValue ListValue;
647 typedef struct MapIter MapIter;
648 typedef struct Map Map;
649 typedef struct MessageField MessageField;
650 typedef struct MessageHeader MessageHeader;
651 typedef struct MessageLayout MessageLayout;
652 typedef struct Method Method;
653 typedef struct Mixin Mixin;
654 typedef struct NullValue NullValue;
655 typedef struct Oneof Oneof;
656 typedef struct Option Option;
657 typedef struct RepeatedFieldIter RepeatedFieldIter;
658 typedef struct RepeatedField RepeatedField;
659 typedef struct SourceContext SourceContext;
660 typedef struct StringValue StringValue;
661 typedef struct Struct Struct;
662 typedef struct Syntax Syntax;
663 typedef struct Timestamp Timestamp;
664 typedef struct Type Type;
665 typedef struct UInt32Value UInt32Value;
666 typedef struct UInt64Value UInt64Value;
667 typedef struct Value Value;
668 
669 // -----------------------------------------------------------------------------
670 // Globals.
671 // -----------------------------------------------------------------------------
672 
673 ZEND_BEGIN_MODULE_GLOBALS(protobuf)
674 ZEND_END_MODULE_GLOBALS(protobuf)
675 
676 // Init module and PHP classes.
677 void any_init(TSRMLS_D);
678 void api_init(TSRMLS_D);
679 void bool_value_init(TSRMLS_D);
680 void bytes_value_init(TSRMLS_D);
681 void descriptor_init(TSRMLS_D);
682 void descriptor_pool_init(TSRMLS_D);
683 void double_value_init(TSRMLS_D);
684 void duration_init(TSRMLS_D);
685 void empty_init(TSRMLS_D);
686 void enum_descriptor_init(TSRMLS_D);
687 void enum_value_descriptor_init(TSRMLS_D);
688 void enum_init(TSRMLS_D);
689 void enum_value_init(TSRMLS_D);
690 void field_cardinality_init(TSRMLS_D);
691 void field_descriptor_init(TSRMLS_D);
692 void field_init(TSRMLS_D);
693 void field_kind_init(TSRMLS_D);
694 void field_mask_init(TSRMLS_D);
695 void float_value_init(TSRMLS_D);
696 void gpb_type_init(TSRMLS_D);
697 void int32_value_init(TSRMLS_D);
698 void int64_value_init(TSRMLS_D);
699 void internal_descriptor_pool_init(TSRMLS_D);
700 void list_value_init(TSRMLS_D);
701 void map_field_init(TSRMLS_D);
702 void map_field_iter_init(TSRMLS_D);
703 void message_init(TSRMLS_D);
704 void method_init(TSRMLS_D);
705 void mixin_init(TSRMLS_D);
706 void null_value_init(TSRMLS_D);
707 void oneof_descriptor_init(TSRMLS_D);
708 void option_init(TSRMLS_D);
709 void repeated_field_init(TSRMLS_D);
710 void repeated_field_iter_init(TSRMLS_D);
711 void source_context_init(TSRMLS_D);
712 void string_value_init(TSRMLS_D);
713 void struct_init(TSRMLS_D);
714 void syntax_init(TSRMLS_D);
715 void timestamp_init(TSRMLS_D);
716 void type_init(TSRMLS_D);
717 void u_int32_value_init(TSRMLS_D);
718 void u_int64_value_init(TSRMLS_D);
719 void util_init(TSRMLS_D);
720 void value_init(TSRMLS_D);
721 
722 void gpb_metadata_any_init(TSRMLS_D);
723 void gpb_metadata_api_init(TSRMLS_D);
724 void gpb_metadata_duration_init(TSRMLS_D);
725 void gpb_metadata_field_mask_init(TSRMLS_D);
726 void gpb_metadata_empty_init(TSRMLS_D);
727 void gpb_metadata_source_context_init(TSRMLS_D);
728 void gpb_metadata_struct_init(TSRMLS_D);
729 void gpb_metadata_timestamp_init(TSRMLS_D);
730 void gpb_metadata_type_init(TSRMLS_D);
731 void gpb_metadata_wrappers_init(TSRMLS_D);
732 
733 // Global map from upb {msg,enum}defs to wrapper Descriptor/EnumDescriptor
734 // instances.
735 void add_def_obj(const void* def, PHP_PROTO_HASHTABLE_VALUE value);
736 PHP_PROTO_HASHTABLE_VALUE get_def_obj(const void* def);
737 
738 // Global map from PHP class entries to wrapper Descriptor/EnumDescriptor
739 // instances.
740 void add_ce_obj(const void* ce, PHP_PROTO_HASHTABLE_VALUE value);
741 PHP_PROTO_HASHTABLE_VALUE get_ce_obj(const void* ce);
742 bool class_added(const void* ce);
743 
744 // Global map from message/enum's proto fully-qualified name to corresponding
745 // wrapper Descriptor/EnumDescriptor instances.
746 void add_proto_obj(const char* proto, PHP_PROTO_HASHTABLE_VALUE value);
747 PHP_PROTO_HASHTABLE_VALUE get_proto_obj(const char* proto);
748 
749 extern zend_class_entry* map_field_type;
750 extern zend_class_entry* repeated_field_type;
751 
752 // -----------------------------------------------------------------------------
753 // Descriptor.
754 // -----------------------------------------------------------------------------
755 
756 PHP_PROTO_WRAP_OBJECT_START(DescriptorPool)
757   InternalDescriptorPool* intern;
758 PHP_PROTO_WRAP_OBJECT_END
759 
760 PHP_METHOD(DescriptorPool, getGeneratedPool);
761 PHP_METHOD(DescriptorPool, getDescriptorByClassName);
762 PHP_METHOD(DescriptorPool, getEnumDescriptorByClassName);
763 
764 PHP_PROTO_WRAP_OBJECT_START(InternalDescriptorPool)
765   upb_symtab* symtab;
766   upb_handlercache* fill_handler_cache;
767   upb_handlercache* pb_serialize_handler_cache;
768   upb_handlercache* json_serialize_handler_cache;
769   upb_handlercache* json_serialize_handler_preserve_cache;
770   upb_pbcodecache* fill_method_cache;
771   upb_json_codecache* json_fill_method_cache;
772 PHP_PROTO_WRAP_OBJECT_END
773 
774 PHP_METHOD(InternalDescriptorPool, getGeneratedPool);
775 PHP_METHOD(InternalDescriptorPool, internalAddGeneratedFile);
776 
777 void internal_add_generated_file(const char* data, PHP_PROTO_SIZE data_len,
778                                  InternalDescriptorPool* pool,
779                                  bool use_nested_submsg TSRMLS_DC);
780 void init_generated_pool_once(TSRMLS_D);
781 void add_handlers_for_message(const void* closure, upb_handlers* h);
782 
783 // wrapper of generated pool
784 #if PHP_MAJOR_VERSION < 7
785 extern zval* generated_pool_php;
786 extern zval* internal_generated_pool_php;
787 void descriptor_pool_free(void* object TSRMLS_DC);
788 void internal_descriptor_pool_free(void* object TSRMLS_DC);
789 #else
790 extern zend_object *generated_pool_php;
791 extern zend_object *internal_generated_pool_php;
792 void descriptor_pool_free(zend_object* object);
793 void internal_descriptor_pool_free(zend_object* object);
794 #endif
795 extern InternalDescriptorPool* generated_pool;  // The actual generated pool
796 
797 PHP_PROTO_WRAP_OBJECT_START(Descriptor)
798   InternalDescriptorPool* pool;
799   const upb_msgdef* msgdef;
800   MessageLayout* layout;
801   zend_class_entry* klass;  // begins as NULL
802 PHP_PROTO_WRAP_OBJECT_END
803 
804 PHP_METHOD(Descriptor, getClass);
805 PHP_METHOD(Descriptor, getFullName);
806 PHP_METHOD(Descriptor, getField);
807 PHP_METHOD(Descriptor, getFieldCount);
808 PHP_METHOD(Descriptor, getOneofDecl);
809 PHP_METHOD(Descriptor, getOneofDeclCount);
810 
811 extern zend_class_entry* descriptor_type;
812 
813 void descriptor_name_set(Descriptor *desc, const char *name);
814 
815 PHP_PROTO_WRAP_OBJECT_START(FieldDescriptor)
816   const upb_fielddef* fielddef;
817 PHP_PROTO_WRAP_OBJECT_END
818 
819 PHP_METHOD(FieldDescriptor, getName);
820 PHP_METHOD(FieldDescriptor, getNumber);
821 PHP_METHOD(FieldDescriptor, getLabel);
822 PHP_METHOD(FieldDescriptor, getType);
823 PHP_METHOD(FieldDescriptor, isMap);
824 PHP_METHOD(FieldDescriptor, getEnumType);
825 PHP_METHOD(FieldDescriptor, getMessageType);
826 
827 extern zend_class_entry* field_descriptor_type;
828 
829 PHP_PROTO_WRAP_OBJECT_START(EnumDescriptor)
830   const upb_enumdef* enumdef;
831   zend_class_entry* klass;  // begins as NULL
832 PHP_PROTO_WRAP_OBJECT_END
833 
834 PHP_METHOD(EnumDescriptor, getValue);
835 PHP_METHOD(EnumDescriptor, getValueCount);
836 
837 extern zend_class_entry* enum_descriptor_type;
838 
839 PHP_PROTO_WRAP_OBJECT_START(EnumValueDescriptor)
840   const char* name;
841   int32_t number;
842 PHP_PROTO_WRAP_OBJECT_END
843 
844 PHP_METHOD(EnumValueDescriptor, getName);
845 PHP_METHOD(EnumValueDescriptor, getNumber);
846 
847 extern zend_class_entry* enum_value_descriptor_type;
848 
849 // -----------------------------------------------------------------------------
850 // Message class creation.
851 // -----------------------------------------------------------------------------
852 
853 void* message_data(MessageHeader* msg);
854 void custom_data_init(const zend_class_entry* ce,
855                       MessageHeader* msg PHP_PROTO_TSRMLS_DC);
856 
857 // Build PHP class for given descriptor. Instead of building from scratch, this
858 // function modifies existing class which has been partially defined in PHP
859 // code.
860 void build_class_from_descriptor(
861     PHP_PROTO_HASHTABLE_VALUE php_descriptor TSRMLS_DC);
862 
863 extern zend_class_entry* message_type;
864 extern zend_object_handlers* message_handlers;
865 
866 // -----------------------------------------------------------------------------
867 // Message layout / storage.
868 // -----------------------------------------------------------------------------
869 
870 /*
871  * In c extension, each protobuf message is a zval instance. The zval instance
872  * is like union, which can be used to store int, string, zend_object_value and
873  * etc. For protobuf message, the zval instance is used to store the
874  * zend_object_value.
875  *
876  * The zend_object_value is composed of handlers and a handle to look up the
877  * actual stored data. The handlers are pointers to functions, e.g., read,
878  * write, and etc, to access properties.
879  *
880  * The actual data of protobuf messages is stored as MessageHeader in zend
881  * engine's central repository. Each MessageHeader instance is composed of a
882  * zend_object, a Descriptor instance and the real message data.
883  *
884  * For the reason that PHP's native types may not be large enough to store
885  * protobuf message's field (e.g., int64), all message's data is stored in
886  * custom memory layout and is indexed by the Descriptor instance.
887  *
888  * The zend_object contains the zend class entry and the properties table. The
889  * zend class entry contains all information about protobuf message's
890  * corresponding PHP class. The most useful information is the offset table of
891  * properties. Because read access to properties requires returning zval
892  * instance, we need to convert data from the custom layout to zval instance.
893  * Instead of creating zval instance for every read access, we use the zval
894  * instances in the properties table in the zend_object as cache.  When
895  * accessing properties, the offset is needed to find the zval property in
896  * zend_object's properties table. These properties will be updated using the
897  * data from custom memory layout only when reading these properties.
898  *
899  * zval
900  * |-zend_object_value obj
901  *   |-zend_object_handlers* handlers -> |-read_property_handler
902  *   |                                   |-write_property_handler
903  *   |                              ++++++++++++++++++++++
904  *   |-zend_object_handle handle -> + central repository +
905  *                                  ++++++++++++++++++++++
906  *  MessageHeader <-----------------|
907  *  |-zend_object std
908  *  | |-class_entry* ce -> class_entry
909  *  | |                    |-HashTable properties_table (name->offset)
910  *  | |-zval** properties_table <------------------------------|
911  *  |                         |------> zval* property(cache)
912  *  |-Descriptor* desc (name->offset)
913  *  |-void** data <-----------|
914  *           |-----------------------> void* property(data)
915  *
916  */
917 
918 #define MESSAGE_FIELD_NO_CASE ((size_t)-1)
919 
920 struct MessageField {
921   size_t offset;
922   int cache_index;  // Each field except oneof field has a zval cache to avoid
923                     // multiple creation when being accessed.
924   size_t case_offset;   // for oneofs, a uint32. Else, MESSAGE_FIELD_NO_CASE.
925 };
926 
927 struct MessageLayout {
928   const upb_msgdef* msgdef;
929   MessageField* fields;
930   size_t size;
931 };
932 
933 PHP_PROTO_WRAP_OBJECT_START(MessageHeader)
934   void* data;  // Point to the real message data.
935                // Place needs to be consistent with map_parse_frame_data_t.
936   Descriptor* descriptor;  // Kept alive by self.class.descriptor reference.
937 PHP_PROTO_WRAP_OBJECT_END
938 
939 MessageLayout* create_layout(const upb_msgdef* msgdef);
940 void layout_init(MessageLayout* layout, void* storage,
941                  zend_object* object PHP_PROTO_TSRMLS_DC);
942 zval* layout_get(MessageLayout* layout, const void* storage,
943                  const upb_fielddef* field, CACHED_VALUE* cache TSRMLS_DC);
944 void layout_set(MessageLayout* layout, MessageHeader* header,
945                 const upb_fielddef* field, zval* val TSRMLS_DC);
946 void layout_merge(MessageLayout* layout, MessageHeader* from,
947                   MessageHeader* to TSRMLS_DC);
948 const char* layout_get_oneof_case(MessageLayout* layout, const void* storage,
949                                   const upb_oneofdef* oneof TSRMLS_DC);
950 void free_layout(MessageLayout* layout);
951 uint32_t* slot_oneof_case(MessageLayout* layout, const void* storage,
952                           const upb_fielddef* field);
953 void* slot_memory(MessageLayout* layout, const void* storage,
954                   const upb_fielddef* field);
955 
956 PHP_METHOD(Message, clear);
957 PHP_METHOD(Message, mergeFrom);
958 PHP_METHOD(Message, readOneof);
959 PHP_METHOD(Message, writeOneof);
960 PHP_METHOD(Message, whichOneof);
961 PHP_METHOD(Message, __construct);
962 
963 // -----------------------------------------------------------------------------
964 // Encode / Decode.
965 // -----------------------------------------------------------------------------
966 
967 // Maximum depth allowed during encoding, to avoid stack overflows due to
968 // cycles.
969 #define ENCODE_MAX_NESTING 63
970 
971 // Constructs the upb decoder method for parsing messages of this type.
972 // This is called from the message class creation code.
973 const upb_pbdecodermethod *new_fillmsg_decodermethod(Descriptor *desc,
974                                                      const void *owner);
975 void serialize_to_string(zval* val, zval* return_value TSRMLS_DC);
976 void merge_from_string(const char* data, int data_len, Descriptor* desc,
977                        MessageHeader* msg);
978 
979 PHP_METHOD(Message, serializeToString);
980 PHP_METHOD(Message, mergeFromString);
981 PHP_METHOD(Message, serializeToJsonString);
982 PHP_METHOD(Message, mergeFromJsonString);
983 PHP_METHOD(Message, discardUnknownFields);
984 
985 // -----------------------------------------------------------------------------
986 // Type check / conversion.
987 // -----------------------------------------------------------------------------
988 
989 bool protobuf_convert_to_int32(zval* from, int32_t* to);
990 bool protobuf_convert_to_uint32(zval* from, uint32_t* to);
991 bool protobuf_convert_to_int64(zval* from, int64_t* to);
992 bool protobuf_convert_to_uint64(zval* from, uint64_t* to);
993 bool protobuf_convert_to_float(zval* from, float* to);
994 bool protobuf_convert_to_double(zval* from, double* to);
995 bool protobuf_convert_to_bool(zval* from, int8_t* to);
996 bool protobuf_convert_to_string(zval* from);
997 
998 void check_repeated_field(const zend_class_entry* klass, PHP_PROTO_LONG type,
999                           zval* val, zval* return_value);
1000 void check_map_field(const zend_class_entry* klass, PHP_PROTO_LONG key_type,
1001                      PHP_PROTO_LONG value_type, zval* val, zval* return_value);
1002 
1003 PHP_METHOD(Util, checkInt32);
1004 PHP_METHOD(Util, checkUint32);
1005 PHP_METHOD(Util, checkInt64);
1006 PHP_METHOD(Util, checkUint64);
1007 PHP_METHOD(Util, checkEnum);
1008 PHP_METHOD(Util, checkFloat);
1009 PHP_METHOD(Util, checkDouble);
1010 PHP_METHOD(Util, checkBool);
1011 PHP_METHOD(Util, checkString);
1012 PHP_METHOD(Util, checkBytes);
1013 PHP_METHOD(Util, checkMessage);
1014 PHP_METHOD(Util, checkMapField);
1015 PHP_METHOD(Util, checkRepeatedField);
1016 
1017 // -----------------------------------------------------------------------------
1018 // Native slot storage abstraction.
1019 // -----------------------------------------------------------------------------
1020 
1021 #define NATIVE_SLOT_MAX_SIZE sizeof(uint64_t)
1022 
1023 size_t native_slot_size(upb_fieldtype_t type);
1024 bool native_slot_set(upb_fieldtype_t type, const zend_class_entry* klass,
1025                      void* memory, zval* value TSRMLS_DC);
1026 // String/Message is stored differently in array/map from normal message fields.
1027 // So we need to make a special method to handle that.
1028 bool native_slot_set_by_array(upb_fieldtype_t type,
1029                               const zend_class_entry* klass, void* memory,
1030                               zval* value TSRMLS_DC);
1031 bool native_slot_set_by_map(upb_fieldtype_t type, const zend_class_entry* klass,
1032                             void* memory, zval* value TSRMLS_DC);
1033 void native_slot_init(upb_fieldtype_t type, void* memory, CACHED_VALUE* cache);
1034 // For each property, in order to avoid conversion between the zval object and
1035 // the actual data type during parsing/serialization, the containing message
1036 // object use the custom memory layout to store the actual data type for each
1037 // property inside of it.  To access a property from php code, the property
1038 // needs to be converted to a zval object. The message object is not responsible
1039 // for providing such a zval object. Instead the caller needs to provide one
1040 // (cache) and update it with the actual data (memory).
1041 void native_slot_get(upb_fieldtype_t type, const void* memory,
1042                      CACHED_VALUE* cache TSRMLS_DC);
1043 // String/Message is stored differently in array/map from normal message fields.
1044 // So we need to make a special method to handle that.
1045 void native_slot_get_by_array(upb_fieldtype_t type, const void* memory,
1046                      CACHED_VALUE* cache TSRMLS_DC);
1047 void native_slot_get_by_map_key(upb_fieldtype_t type, const void* memory,
1048                                 int length, CACHED_VALUE* cache TSRMLS_DC);
1049 void native_slot_get_by_map_value(upb_fieldtype_t type, const void* memory,
1050                                   CACHED_VALUE* cache TSRMLS_DC);
1051 void native_slot_get_default(upb_fieldtype_t type,
1052                              CACHED_VALUE* cache TSRMLS_DC);
1053 
1054 // -----------------------------------------------------------------------------
1055 // Map Field.
1056 // -----------------------------------------------------------------------------
1057 
1058 extern zend_object_handlers* map_field_handlers;
1059 extern zend_object_handlers* map_field_iter_handlers;
1060 
1061 PHP_PROTO_WRAP_OBJECT_START(Map)
1062   upb_fieldtype_t key_type;
1063   upb_fieldtype_t value_type;
1064   const zend_class_entry* msg_ce;  // class entry for value message
1065   upb_strtable table;
1066 PHP_PROTO_WRAP_OBJECT_END
1067 
1068 PHP_PROTO_WRAP_OBJECT_START(MapIter)
1069   Map* self;
1070   upb_strtable_iter it;
1071 PHP_PROTO_WRAP_OBJECT_END
1072 
1073 void map_begin(zval* self, MapIter* iter TSRMLS_DC);
1074 void map_next(MapIter* iter);
1075 bool map_done(MapIter* iter);
1076 const char* map_iter_key(MapIter* iter, int* len);
1077 upb_value map_iter_value(MapIter* iter, int* len);
1078 
1079 // These operate on a map-entry msgdef.
1080 const upb_fielddef* map_entry_key(const upb_msgdef* msgdef);
1081 const upb_fielddef* map_entry_value(const upb_msgdef* msgdef);
1082 
1083 void map_field_create_with_field(const zend_class_entry* ce,
1084                                  const upb_fielddef* field,
1085                                  CACHED_VALUE* map_field PHP_PROTO_TSRMLS_DC);
1086 void map_field_create_with_type(const zend_class_entry* ce,
1087                                 upb_fieldtype_t key_type,
1088                                 upb_fieldtype_t value_type,
1089                                 const zend_class_entry* msg_ce,
1090                                 CACHED_VALUE* map_field PHP_PROTO_TSRMLS_DC);
1091 void* upb_value_memory(upb_value* v);
1092 
1093 #define MAP_KEY_FIELD 1
1094 #define MAP_VALUE_FIELD 2
1095 
1096 // These operate on a map field (i.e., a repeated field of submessages whose
1097 // submessage type is a map-entry msgdef).
1098 bool is_map_field(const upb_fielddef* field);
1099 const upb_fielddef* map_field_key(const upb_fielddef* field);
1100 const upb_fielddef* map_field_value(const upb_fielddef* field);
1101 
1102 bool map_index_set(Map *intern, const char* keyval, int length, upb_value v);
1103 
1104 PHP_METHOD(MapField, __construct);
1105 PHP_METHOD(MapField, offsetExists);
1106 PHP_METHOD(MapField, offsetGet);
1107 PHP_METHOD(MapField, offsetSet);
1108 PHP_METHOD(MapField, offsetUnset);
1109 PHP_METHOD(MapField, count);
1110 PHP_METHOD(MapField, getIterator);
1111 
1112 PHP_METHOD(MapFieldIter, rewind);
1113 PHP_METHOD(MapFieldIter, current);
1114 PHP_METHOD(MapFieldIter, key);
1115 PHP_METHOD(MapFieldIter, next);
1116 PHP_METHOD(MapFieldIter, valid);
1117 
1118 // -----------------------------------------------------------------------------
1119 // Repeated Field.
1120 // -----------------------------------------------------------------------------
1121 
1122 extern zend_object_handlers* repeated_field_handlers;
1123 extern zend_object_handlers* repeated_field_iter_handlers;
1124 
1125 PHP_PROTO_WRAP_OBJECT_START(RepeatedField)
1126 #if PHP_MAJOR_VERSION < 7
1127   zval* array;
1128 #else
1129   zval array;
1130 #endif
1131   upb_fieldtype_t type;
1132   const zend_class_entry* msg_ce;  // class entry for containing message
1133                                    // (for message field only).
1134 PHP_PROTO_WRAP_OBJECT_END
1135 
1136 PHP_PROTO_WRAP_OBJECT_START(RepeatedFieldIter)
1137   RepeatedField* repeated_field;
1138   long position;
1139 PHP_PROTO_WRAP_OBJECT_END
1140 
1141 void repeated_field_create_with_field(
1142     zend_class_entry* ce, const upb_fielddef* field,
1143     CACHED_VALUE* repeated_field PHP_PROTO_TSRMLS_DC);
1144 void repeated_field_create_with_type(
1145     zend_class_entry* ce, upb_fieldtype_t type, const zend_class_entry* msg_ce,
1146     CACHED_VALUE* repeated_field PHP_PROTO_TSRMLS_DC);
1147 // Return the element at the index position from the repeated field. There is
1148 // not restriction on the type of stored elements.
1149 void *repeated_field_index_native(RepeatedField *intern, int index TSRMLS_DC);
1150 // Add the element to the end of the repeated field. There is not restriction on
1151 // the type of stored elements.
1152 void repeated_field_push_native(RepeatedField *intern, void *value);
1153 
1154 PHP_METHOD(RepeatedField, __construct);
1155 PHP_METHOD(RepeatedField, append);
1156 PHP_METHOD(RepeatedField, offsetExists);
1157 PHP_METHOD(RepeatedField, offsetGet);
1158 PHP_METHOD(RepeatedField, offsetSet);
1159 PHP_METHOD(RepeatedField, offsetUnset);
1160 PHP_METHOD(RepeatedField, count);
1161 PHP_METHOD(RepeatedField, getIterator);
1162 
1163 PHP_METHOD(RepeatedFieldIter, rewind);
1164 PHP_METHOD(RepeatedFieldIter, current);
1165 PHP_METHOD(RepeatedFieldIter, key);
1166 PHP_METHOD(RepeatedFieldIter, next);
1167 PHP_METHOD(RepeatedFieldIter, valid);
1168 
1169 // -----------------------------------------------------------------------------
1170 // Oneof Field.
1171 // -----------------------------------------------------------------------------
1172 
1173 PHP_PROTO_WRAP_OBJECT_START(Oneof)
1174   const upb_oneofdef* oneofdef;
1175   int index;    // Index of field in oneof. -1 if not set.
1176   char value[NATIVE_SLOT_MAX_SIZE];
1177 PHP_PROTO_WRAP_OBJECT_END
1178 
1179 PHP_METHOD(Oneof, getName);
1180 PHP_METHOD(Oneof, getField);
1181 PHP_METHOD(Oneof, getFieldCount);
1182 
1183 extern zend_class_entry* oneof_descriptor_type;
1184 
1185 // Oneof case slot value to indicate that no oneof case is set. The value `0` is
1186 // safe because field numbers are used as case identifiers, and no field can
1187 // have a number of 0.
1188 #define ONEOF_CASE_NONE 0
1189 
1190 // -----------------------------------------------------------------------------
1191 // Well Known Type.
1192 // -----------------------------------------------------------------------------
1193 
1194 extern bool is_inited_file_any;
1195 extern bool is_inited_file_api;
1196 extern bool is_inited_file_duration;
1197 extern bool is_inited_file_field_mask;
1198 extern bool is_inited_file_empty;
1199 extern bool is_inited_file_source_context;
1200 extern bool is_inited_file_struct;
1201 extern bool is_inited_file_timestamp;
1202 extern bool is_inited_file_type;
1203 extern bool is_inited_file_wrappers;
1204 
1205 PHP_METHOD(GPBMetadata_Any, initOnce);
1206 PHP_METHOD(GPBMetadata_Api, initOnce);
1207 PHP_METHOD(GPBMetadata_Duration, initOnce);
1208 PHP_METHOD(GPBMetadata_FieldMask, initOnce);
1209 PHP_METHOD(GPBMetadata_Empty, initOnce);
1210 PHP_METHOD(GPBMetadata_SourceContext, initOnce);
1211 PHP_METHOD(GPBMetadata_Struct, initOnce);
1212 PHP_METHOD(GPBMetadata_Timestamp, initOnce);
1213 PHP_METHOD(GPBMetadata_Type, initOnce);
1214 PHP_METHOD(GPBMetadata_Wrappers, initOnce);
1215 
1216 PHP_METHOD(Any, __construct);
1217 PHP_METHOD(Any, getTypeUrl);
1218 PHP_METHOD(Any, setTypeUrl);
1219 PHP_METHOD(Any, getValue);
1220 PHP_METHOD(Any, setValue);
1221 PHP_METHOD(Any, unpack);
1222 PHP_METHOD(Any, pack);
1223 PHP_METHOD(Any, is);
1224 
1225 PHP_METHOD(Duration, __construct);
1226 PHP_METHOD(Duration, getSeconds);
1227 PHP_METHOD(Duration, setSeconds);
1228 PHP_METHOD(Duration, getNanos);
1229 PHP_METHOD(Duration, setNanos);
1230 
1231 PHP_METHOD(Timestamp, __construct);
1232 PHP_METHOD(Timestamp, fromDateTime);
1233 PHP_METHOD(Timestamp, toDateTime);
1234 PHP_METHOD(Timestamp, getSeconds);
1235 PHP_METHOD(Timestamp, setSeconds);
1236 PHP_METHOD(Timestamp, getNanos);
1237 PHP_METHOD(Timestamp, setNanos);
1238 
1239 PHP_METHOD(Api, __construct);
1240 PHP_METHOD(Api, getName);
1241 PHP_METHOD(Api, setName);
1242 PHP_METHOD(Api, getMethods);
1243 PHP_METHOD(Api, setMethods);
1244 PHP_METHOD(Api, getOptions);
1245 PHP_METHOD(Api, setOptions);
1246 PHP_METHOD(Api, getVersion);
1247 PHP_METHOD(Api, setVersion);
1248 PHP_METHOD(Api, getSourceContext);
1249 PHP_METHOD(Api, setSourceContext);
1250 PHP_METHOD(Api, getMixins);
1251 PHP_METHOD(Api, setMixins);
1252 PHP_METHOD(Api, getSyntax);
1253 PHP_METHOD(Api, setSyntax);
1254 
1255 PHP_METHOD(BoolValue, __construct);
1256 PHP_METHOD(BoolValue, getValue);
1257 PHP_METHOD(BoolValue, setValue);
1258 
1259 PHP_METHOD(BytesValue, __construct);
1260 PHP_METHOD(BytesValue, getValue);
1261 PHP_METHOD(BytesValue, setValue);
1262 
1263 PHP_METHOD(DoubleValue, __construct);
1264 PHP_METHOD(DoubleValue, getValue);
1265 PHP_METHOD(DoubleValue, setValue);
1266 
1267 PHP_METHOD(Enum, __construct);
1268 PHP_METHOD(Enum, getName);
1269 PHP_METHOD(Enum, setName);
1270 PHP_METHOD(Enum, getEnumvalue);
1271 PHP_METHOD(Enum, setEnumvalue);
1272 PHP_METHOD(Enum, getOptions);
1273 PHP_METHOD(Enum, setOptions);
1274 PHP_METHOD(Enum, getSourceContext);
1275 PHP_METHOD(Enum, setSourceContext);
1276 PHP_METHOD(Enum, getSyntax);
1277 PHP_METHOD(Enum, setSyntax);
1278 
1279 PHP_METHOD(EnumValue, __construct);
1280 PHP_METHOD(EnumValue, getName);
1281 PHP_METHOD(EnumValue, setName);
1282 PHP_METHOD(EnumValue, getNumber);
1283 PHP_METHOD(EnumValue, setNumber);
1284 PHP_METHOD(EnumValue, getOptions);
1285 PHP_METHOD(EnumValue, setOptions);
1286 
1287 PHP_METHOD(FieldMask, __construct);
1288 PHP_METHOD(FieldMask, getPaths);
1289 PHP_METHOD(FieldMask, setPaths);
1290 
1291 PHP_METHOD(Field, __construct);
1292 PHP_METHOD(Field, getKind);
1293 PHP_METHOD(Field, setKind);
1294 PHP_METHOD(Field, getCardinality);
1295 PHP_METHOD(Field, setCardinality);
1296 PHP_METHOD(Field, getNumber);
1297 PHP_METHOD(Field, setNumber);
1298 PHP_METHOD(Field, getName);
1299 PHP_METHOD(Field, setName);
1300 PHP_METHOD(Field, getTypeUrl);
1301 PHP_METHOD(Field, setTypeUrl);
1302 PHP_METHOD(Field, getOneofIndex);
1303 PHP_METHOD(Field, setOneofIndex);
1304 PHP_METHOD(Field, getPacked);
1305 PHP_METHOD(Field, setPacked);
1306 PHP_METHOD(Field, getOptions);
1307 PHP_METHOD(Field, setOptions);
1308 PHP_METHOD(Field, getJsonName);
1309 PHP_METHOD(Field, setJsonName);
1310 PHP_METHOD(Field, getDefaultValue);
1311 PHP_METHOD(Field, setDefaultValue);
1312 
1313 PHP_METHOD(Field_Cardinality, name);
1314 PHP_METHOD(Field_Cardinality, value);
1315 
1316 PHP_METHOD(Field_Kind, name);
1317 PHP_METHOD(Field_Kind, value);
1318 
1319 PHP_METHOD(FloatValue, __construct);
1320 PHP_METHOD(FloatValue, getValue);
1321 PHP_METHOD(FloatValue, setValue);
1322 
1323 PHP_METHOD(GPBEmpty, __construct);
1324 
1325 PHP_METHOD(Int32Value, __construct);
1326 PHP_METHOD(Int32Value, getValue);
1327 PHP_METHOD(Int32Value, setValue);
1328 
1329 PHP_METHOD(Int64Value, __construct);
1330 PHP_METHOD(Int64Value, getValue);
1331 PHP_METHOD(Int64Value, setValue);
1332 
1333 PHP_METHOD(ListValue, __construct);
1334 PHP_METHOD(ListValue, getValues);
1335 PHP_METHOD(ListValue, setValues);
1336 
1337 PHP_METHOD(Method, __construct);
1338 PHP_METHOD(Method, getName);
1339 PHP_METHOD(Method, setName);
1340 PHP_METHOD(Method, getRequestTypeUrl);
1341 PHP_METHOD(Method, setRequestTypeUrl);
1342 PHP_METHOD(Method, getRequestStreaming);
1343 PHP_METHOD(Method, setRequestStreaming);
1344 PHP_METHOD(Method, getResponseTypeUrl);
1345 PHP_METHOD(Method, setResponseTypeUrl);
1346 PHP_METHOD(Method, getResponseStreaming);
1347 PHP_METHOD(Method, setResponseStreaming);
1348 PHP_METHOD(Method, getOptions);
1349 PHP_METHOD(Method, setOptions);
1350 PHP_METHOD(Method, getSyntax);
1351 PHP_METHOD(Method, setSyntax);
1352 
1353 PHP_METHOD(Mixin, __construct);
1354 PHP_METHOD(Mixin, getName);
1355 PHP_METHOD(Mixin, setName);
1356 PHP_METHOD(Mixin, getRoot);
1357 PHP_METHOD(Mixin, setRoot);
1358 
1359 PHP_METHOD(NullValue, name);
1360 PHP_METHOD(NullValue, value);
1361 
1362 PHP_METHOD(Option, __construct);
1363 PHP_METHOD(Option, getName);
1364 PHP_METHOD(Option, setName);
1365 PHP_METHOD(Option, getValue);
1366 PHP_METHOD(Option, setValue);
1367 
1368 PHP_METHOD(SourceContext, __construct);
1369 PHP_METHOD(SourceContext, getFileName);
1370 PHP_METHOD(SourceContext, setFileName);
1371 
1372 PHP_METHOD(StringValue, __construct);
1373 PHP_METHOD(StringValue, getValue);
1374 PHP_METHOD(StringValue, setValue);
1375 
1376 PHP_METHOD(Struct, __construct);
1377 PHP_METHOD(Struct, getFields);
1378 PHP_METHOD(Struct, setFields);
1379 
1380 PHP_METHOD(Syntax, name);
1381 PHP_METHOD(Syntax, value);
1382 
1383 PHP_METHOD(Type, __construct);
1384 PHP_METHOD(Type, getName);
1385 PHP_METHOD(Type, setName);
1386 PHP_METHOD(Type, getFields);
1387 PHP_METHOD(Type, setFields);
1388 PHP_METHOD(Type, getOneofs);
1389 PHP_METHOD(Type, setOneofs);
1390 PHP_METHOD(Type, getOptions);
1391 PHP_METHOD(Type, setOptions);
1392 PHP_METHOD(Type, getSourceContext);
1393 PHP_METHOD(Type, setSourceContext);
1394 PHP_METHOD(Type, getSyntax);
1395 PHP_METHOD(Type, setSyntax);
1396 
1397 PHP_METHOD(UInt32Value, __construct);
1398 PHP_METHOD(UInt32Value, getValue);
1399 PHP_METHOD(UInt32Value, setValue);
1400 
1401 PHP_METHOD(UInt64Value, __construct);
1402 PHP_METHOD(UInt64Value, getValue);
1403 PHP_METHOD(UInt64Value, setValue);
1404 
1405 PHP_METHOD(Value, __construct);
1406 PHP_METHOD(Value, getNullValue);
1407 PHP_METHOD(Value, setNullValue);
1408 PHP_METHOD(Value, getNumberValue);
1409 PHP_METHOD(Value, setNumberValue);
1410 PHP_METHOD(Value, getStringValue);
1411 PHP_METHOD(Value, setStringValue);
1412 PHP_METHOD(Value, getBoolValue);
1413 PHP_METHOD(Value, setBoolValue);
1414 PHP_METHOD(Value, getStructValue);
1415 PHP_METHOD(Value, setStructValue);
1416 PHP_METHOD(Value, getListValue);
1417 PHP_METHOD(Value, setListValue);
1418 PHP_METHOD(Value, getKind);
1419 
1420 extern zend_class_entry* any_type;
1421 extern zend_class_entry* api_type;
1422 extern zend_class_entry* bool_value_type;
1423 extern zend_class_entry* bytes_value_type;
1424 extern zend_class_entry* double_value_type;
1425 extern zend_class_entry* duration_type;
1426 extern zend_class_entry* empty_type;
1427 extern zend_class_entry* enum_type;
1428 extern zend_class_entry* enum_value_type;
1429 extern zend_class_entry* field_cardinality_type;
1430 extern zend_class_entry* field_kind_type;
1431 extern zend_class_entry* field_mask_type;
1432 extern zend_class_entry* field_type;
1433 extern zend_class_entry* float_value_type;
1434 extern zend_class_entry* int32_value_type;
1435 extern zend_class_entry* int64_value_type;
1436 extern zend_class_entry* list_value_type;
1437 extern zend_class_entry* method_type;
1438 extern zend_class_entry* mixin_type;
1439 extern zend_class_entry* null_value_type;
1440 extern zend_class_entry* option_type;
1441 extern zend_class_entry* source_context_type;
1442 extern zend_class_entry* string_value_type;
1443 extern zend_class_entry* struct_type;
1444 extern zend_class_entry* syntax_type;
1445 extern zend_class_entry* timestamp_type;
1446 extern zend_class_entry* type_type;
1447 extern zend_class_entry* uint32_value_type;
1448 extern zend_class_entry* uint64_value_type;
1449 extern zend_class_entry* value_type;
1450 
1451 // -----------------------------------------------------------------------------
1452 // Upb.
1453 // -----------------------------------------------------------------------------
1454 
1455 upb_fieldtype_t to_fieldtype(upb_descriptortype_t type);
1456 const zend_class_entry* field_type_class(
1457     const upb_fielddef* field PHP_PROTO_TSRMLS_DC);
1458 void stringsink_uninit_opaque(void *sink);
1459 
1460 typedef struct {
1461   upb_byteshandler handler;
1462   upb_bytessink sink;
1463   char *ptr;
1464   size_t len, size;
1465 } stringsink;
1466 
1467 void stringsink_init(stringsink *sink);
1468 void stringsink_uninit(stringsink *sink);
1469 size_t stringsink_string(void *_sink, const void *hd, const char *ptr,
1470                          size_t len, const upb_bufhandle *handle);
1471 
1472 // -----------------------------------------------------------------------------
1473 // Utilities.
1474 // -----------------------------------------------------------------------------
1475 
1476 // Memory management
1477 #define ALLOC(class_name) (class_name*) emalloc(sizeof(class_name))
1478 #define PEMALLOC(class_name) (class_name*) pemalloc(sizeof(class_name), 1)
1479 #define ALLOC_N(class_name, n) (class_name*) emalloc(sizeof(class_name) * n)
1480 #define FREE(object) efree(object)
1481 #define PEFREE(object) pefree(object, 1)
1482 
1483 // String argument.
1484 #define STR(str) (str), strlen(str)
1485 
1486 // Zend Value
1487 #if PHP_MAJOR_VERSION < 7
1488 #define Z_OBJ_P(zval_p)                                       \
1489   ((zend_object*)(EG(objects_store)                           \
1490                       .object_buckets[Z_OBJ_HANDLE_P(zval_p)] \
1491                       .bucket.obj.object))
1492 #endif
1493 
1494 // Message handler
php_proto_message_read_property(zval * msg,zval * member PHP_PROTO_TSRMLS_DC)1495 static inline zval* php_proto_message_read_property(
1496     zval* msg, zval* member PHP_PROTO_TSRMLS_DC) {
1497 #if PHP_MAJOR_VERSION < 7
1498   return message_handlers->read_property(msg, member, BP_VAR_R,
1499                                          NULL PHP_PROTO_TSRMLS_CC);
1500 #else
1501   return message_handlers->read_property(msg, member, BP_VAR_R, NULL,
1502                                          NULL PHP_PROTO_TSRMLS_CC);
1503 #endif
1504 }
1505 
1506 // Reserved name
1507 bool is_reserved_name(const char* name);
1508 bool is_valid_constant_name(const char* name);
1509 
1510 #endif  // __GOOGLE_PROTOBUF_PHP_PROTOBUF_H__
1511