• 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 PHP_PROTOBUF_H_
32 #define PHP_PROTOBUF_H_
33 
34 #include <php.h>
35 #include <stdbool.h>
36 
37 #include "php-upb.h"
38 
39 upb_DefPool *get_global_symtab();
40 
41 #if PHP_VERSION_ID < 70300
42 #define GC_ADDREF(h) ++GC_REFCOUNT(h)
43 #define GC_DELREF(h) --GC_REFCOUNT(h)
44 #endif
45 
46 // Since php 7.4, the write_property() object handler now returns the assigned
47 // value (after possible type coercions) rather than void.
48 // https://github.com/php/php-src/blob/PHP-7.4.0/UPGRADING.INTERNALS#L171-L173
49 #if PHP_VERSION_ID < 70400
50 #define PROTO_RETURN_VAL void
51 #else
52 #define PROTO_RETURN_VAL zval*
53 #endif
54 
55 // Since php 8.0, the Object Handlers API was changed to receive zend_object*
56 // instead of zval* and zend_string* instead of zval* for property names.
57 // https://github.com/php/php-src/blob/php-8.0.0beta1/UPGRADING.INTERNALS#L37-L39
58 #if PHP_VERSION_ID < 80000
59 #define PROTO_VAL zval
60 #define PROTO_STR zval
61 #define PROTO_VAL_P(obj) (void*)Z_OBJ_P(obj)
62 #define PROTO_STRVAL_P(obj) Z_STRVAL_P(obj)
63 #define PROTO_STRLEN_P(obj) Z_STRLEN_P(obj)
64 #define ZVAL_OBJ_COPY(z, o) do { ZVAL_OBJ(z, o); GC_ADDREF(o); } while (0)
65 #define RETVAL_OBJ_COPY(r) ZVAL_OBJ_COPY(return_value, r)
66 #define RETURN_OBJ_COPY(r) do { RETVAL_OBJ_COPY(r); return; } while (0)
67 #define RETURN_COPY(zv) do { ZVAL_COPY(return_value, zv); return; } while (0)
68 #define RETURN_COPY_VALUE(zv) do { ZVAL_COPY_VALUE(return_value, zv); return; } while (0)
69 #else
70 #define PROTO_VAL zend_object
71 #define PROTO_STR zend_string
72 #define PROTO_VAL_P(obj) (void*)(obj)
73 #define PROTO_STRVAL_P(obj) ZSTR_VAL(obj)
74 #define PROTO_STRLEN_P(obj) ZSTR_LEN(obj)
75 #endif
76 
77 // In PHP 8.1, several old interfaces are removed:
78 // https://github.com/php/php-src/blob/14f599ea7def7c7a59c40aff763ce8b105573e7a/UPGRADING.INTERNALS#L27-L31
79 //
80 // We now use the new interfaces (zend_ce_arrayaccess and zend_ce_countable).
81 // However we have to polyfill zend_ce_countable, which was only introduced in
82 // PHP 7.2.0.
83 #if PHP_VERSION_ID < 70200
84 #define zend_ce_countable spl_ce_Countable
85 #define ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(name, return_reference, required_num_args, class_name, allow_null) \
86         ZEND_BEGIN_ARG_INFO_EX(name, return_reference, required_num_args, allow_null)
87 #endif
88 
89 // polyfill for ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX, which changes between 7.1 and 7.2
90 #if PHP_VERSION_ID < 70200
91 #define PROTOBUF_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(name, return_reference, required_num_args, type, allow_null) \
92         ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(name, return_reference, required_num_args, type, /*class_name*/ 0, allow_null)
93 #else
94 #define PROTOBUF_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(name, return_reference, required_num_args, type, allow_null) \
95         ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(name, return_reference, required_num_args, type, allow_null)
96 #endif
97 
98 // In PHP 8.1, mismatched tentative return types emit a deprecation notice.
99 // https://wiki.php.net/rfc/internal_method_return_types
100 //
101 // When compiling for earlier php versions, the return type is dropped.
102 #if PHP_VERSION_ID < 80100
103 #define ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_INFO_EX(name, return_reference, required_num_args, type, allow_null) \
104         ZEND_BEGIN_ARG_INFO_EX(name, return_reference, required_num_args, allow_null)
105 #endif
106 
107 #ifndef IS_VOID
108 #define IS_VOID 99
109 #endif
110 
111 #ifndef IS_MIXED
112 #define IS_MIXED 99
113 #endif
114 
115 #ifndef _IS_BOOL
116 #define _IS_BOOL 99
117 #endif
118 
119 #ifndef IS_LONG
120 #define IS_LONG 99
121 #endif
122 
123 ZEND_BEGIN_ARG_INFO(arginfo_void, 0)
124 ZEND_END_ARG_INFO()
125 
126 ZEND_BEGIN_ARG_INFO_EX(arginfo_setter, 0, 0, 1)
127   ZEND_ARG_INFO(0, value)
128 ZEND_END_ARG_INFO()
129 
130 #define PHP_PROTOBUF_VERSION "3.21.7"
131 
132 // ptr -> PHP object cache. This is a weak map that caches lazily-created
133 // wrapper objects around upb types:
134 //  * upb_Message* -> Message
135 //  * upb_Array* -> RepeatedField
136 //  * upb_Map*, -> MapField
137 //  * upb_MessageDef* -> Descriptor
138 //  * upb_EnumDef* -> EnumDescriptor
139 //  * upb_MessageDef* -> Descriptor
140 //
141 // Each wrapped object should add itself to the map when it is constructed, and
142 // remove itself from the map when it is destroyed. This is how we ensure that
143 // the map only contains live objects. The map is weak so it does not actually
144 // take references to the cached objects.
145 void ObjCache_Add(const void *key, zend_object *php_obj);
146 void ObjCache_Delete(const void *key);
147 bool ObjCache_Get(const void *key, zval *val);
148 
149 // PHP class name map. This is necessary because the pb_name->php_class_name
150 // transformation is non-reversible, so when we need to look up a msgdef or
151 // enumdef by PHP class, we can't turn the class name into a pb_name.
152 //  * php_class_name -> upb_MessageDef*
153 //  * php_class_name -> upb_EnumDef*
154 void NameMap_AddMessage(const upb_MessageDef *m);
155 void NameMap_AddEnum(const upb_EnumDef *m);
156 const upb_MessageDef *NameMap_GetMessage(zend_class_entry *ce);
157 const upb_EnumDef *NameMap_GetEnum(zend_class_entry *ce);
158 void NameMap_EnterConstructor(zend_class_entry* ce);
159 void NameMap_ExitConstructor(zend_class_entry* ce);
160 
161 // Add this descriptor object to the global list of descriptors that will be
162 // kept alive for the duration of the request but destroyed when the request
163 // is ending.
164 void Descriptors_Add(zend_object *desc);
165 
166 // We need our own assert() because PHP takes control of NDEBUG in its headers.
167 #ifdef PBPHP_ENABLE_ASSERTS
168 #define PBPHP_ASSERT(x)                                                    \
169   do {                                                                     \
170     if (!(x)) {                                                            \
171       fprintf(stderr, "Assertion failure at %s:%d %s", __FILE__, __LINE__, \
172               #x);                                                         \
173       abort();                                                             \
174     }                                                                      \
175   } while (false)
176 #else
177 #define PBPHP_ASSERT(x) \
178   do {                  \
179   } while (false && (x))
180 #endif
181 
182 #endif  // PHP_PROTOBUF_H_
183