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 #include "convert.h"
32 
33 #include <php.h>
34 
35 // This is not self-contained: it must be after other Zend includes.
36 #include <Zend/zend_exceptions.h>
37 
38 #include "array.h"
39 #include "map.h"
40 #include "message.h"
41 #include "php-upb.h"
42 #include "protobuf.h"
43 
44 // -----------------------------------------------------------------------------
45 // GPBUtil
46 // -----------------------------------------------------------------------------
47 
48 static zend_class_entry* GPBUtil_class_entry;
49 
50 // The implementation of type checking for primitive fields is empty. This is
51 // because type checking is done when direct assigning message fields (e.g.,
52 // foo->a = 1). Functions defined here are place holders in generated code for
53 // pure PHP implementation (c extension and pure PHP share the same generated
54 // code).
55 
PHP_METHOD(Util,checkInt32)56 PHP_METHOD(Util, checkInt32) {}
PHP_METHOD(Util,checkUint32)57 PHP_METHOD(Util, checkUint32) {}
PHP_METHOD(Util,checkInt64)58 PHP_METHOD(Util, checkInt64) {}
PHP_METHOD(Util,checkUint64)59 PHP_METHOD(Util, checkUint64) {}
PHP_METHOD(Util,checkEnum)60 PHP_METHOD(Util, checkEnum) {}
PHP_METHOD(Util,checkFloat)61 PHP_METHOD(Util, checkFloat) {}
PHP_METHOD(Util,checkDouble)62 PHP_METHOD(Util, checkDouble) {}
PHP_METHOD(Util,checkBool)63 PHP_METHOD(Util, checkBool) {}
PHP_METHOD(Util,checkString)64 PHP_METHOD(Util, checkString) {}
PHP_METHOD(Util,checkBytes)65 PHP_METHOD(Util, checkBytes) {}
PHP_METHOD(Util,checkMessage)66 PHP_METHOD(Util, checkMessage) {}
67 
68 // The result of checkMapField() is assigned, so we need to return the first
69 // param:
70 //   $arr = GPBUtil::checkMapField($var,
71 //                                 \Google\Protobuf\Internal\GPBType::INT64,
72 //                                 \Google\Protobuf\Internal\GPBType::INT32);
PHP_METHOD(Util,checkMapField)73 PHP_METHOD(Util, checkMapField) {
74   zval *val, *key_type, *val_type, *klass;
75   if (zend_parse_parameters(ZEND_NUM_ARGS(), "zzz|z", &val, &key_type,
76                             &val_type, &klass) == FAILURE) {
77     return;
78   }
79   RETURN_COPY(val);
80 }
81 
82 // The result of checkRepeatedField() is assigned, so we need to return the
83 // first param:
84 // $arr = GPBUtil::checkRepeatedField(
85 //     $var, \Google\Protobuf\Internal\GPBType::STRING);
PHP_METHOD(Util,checkRepeatedField)86 PHP_METHOD(Util, checkRepeatedField) {
87   zval *val, *type, *klass;
88   if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz|z", &val, &type, &klass) ==
89       FAILURE) {
90     return;
91   }
92   RETURN_COPY(val);
93 }
94 
95 ZEND_BEGIN_ARG_INFO_EX(arginfo_checkPrimitive, 0, 0, 1)
96   ZEND_ARG_INFO(0, value)
97 ZEND_END_ARG_INFO()
98 
99 ZEND_BEGIN_ARG_INFO_EX(arginfo_checkString, 0, 0, 1)
100   ZEND_ARG_INFO(0, value)
101   ZEND_ARG_INFO(0, check_utf8)
102 ZEND_END_ARG_INFO()
103 
104 ZEND_BEGIN_ARG_INFO_EX(arginfo_checkMessage, 0, 0, 2)
105   ZEND_ARG_INFO(0, value)
106   ZEND_ARG_INFO(0, class)
107 ZEND_END_ARG_INFO()
108 
109 ZEND_BEGIN_ARG_INFO_EX(arginfo_checkMapField, 0, 0, 3)
110   ZEND_ARG_INFO(0, value)
111   ZEND_ARG_INFO(0, key_type)
112   ZEND_ARG_INFO(0, value_type)
113   ZEND_ARG_INFO(0, value_class)
114 ZEND_END_ARG_INFO()
115 
116 ZEND_BEGIN_ARG_INFO_EX(arginfo_checkRepeatedField, 0, 0, 2)
117   ZEND_ARG_INFO(0, value)
118   ZEND_ARG_INFO(0, type)
119   ZEND_ARG_INFO(0, class)
120 ZEND_END_ARG_INFO()
121 
122 static zend_function_entry util_methods[] = {
123   PHP_ME(Util, checkInt32,  arginfo_checkPrimitive,
124          ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
125   PHP_ME(Util, checkUint32, arginfo_checkPrimitive,
126          ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
127   PHP_ME(Util, checkInt64,  arginfo_checkPrimitive,
128          ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
129   PHP_ME(Util, checkUint64, arginfo_checkPrimitive,
130          ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
131   PHP_ME(Util, checkEnum,   arginfo_checkMessage,
132          ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
133   PHP_ME(Util, checkFloat,  arginfo_checkPrimitive,
134          ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
135   PHP_ME(Util, checkDouble, arginfo_checkPrimitive,
136          ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
137   PHP_ME(Util, checkBool,   arginfo_checkPrimitive,
138          ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
139   PHP_ME(Util, checkString, arginfo_checkString,
140          ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
141   PHP_ME(Util, checkBytes,  arginfo_checkPrimitive,
142          ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
143   PHP_ME(Util, checkMessage, arginfo_checkMessage,
144          ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
145   PHP_ME(Util, checkMapField, arginfo_checkMapField,
146          ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
147   PHP_ME(Util, checkRepeatedField, arginfo_checkRepeatedField,
148          ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
149   ZEND_FE_END
150 };
151 
152 // -----------------------------------------------------------------------------
153 // Conversion functions used from C
154 // -----------------------------------------------------------------------------
155 
pbphp_dtype_to_type(upb_descriptortype_t type)156 upb_fieldtype_t pbphp_dtype_to_type(upb_descriptortype_t type) {
157   switch (type) {
158 #define CASE(descriptor_type, type)           \
159   case UPB_DESCRIPTOR_TYPE_##descriptor_type: \
160     return UPB_TYPE_##type;
161 
162   CASE(FLOAT,    FLOAT);
163   CASE(DOUBLE,   DOUBLE);
164   CASE(BOOL,     BOOL);
165   CASE(STRING,   STRING);
166   CASE(BYTES,    BYTES);
167   CASE(MESSAGE,  MESSAGE);
168   CASE(GROUP,    MESSAGE);
169   CASE(ENUM,     ENUM);
170   CASE(INT32,    INT32);
171   CASE(INT64,    INT64);
172   CASE(UINT32,   UINT32);
173   CASE(UINT64,   UINT64);
174   CASE(SINT32,   INT32);
175   CASE(SINT64,   INT64);
176   CASE(FIXED32,  UINT32);
177   CASE(FIXED64,  UINT64);
178   CASE(SFIXED32, INT32);
179   CASE(SFIXED64, INT64);
180 
181 #undef CASE
182 
183   }
184 
185   zend_error(E_ERROR, "Unknown field type.");
186   return 0;
187 }
188 
buftouint64(const char * ptr,const char * end,uint64_t * val)189 static bool buftouint64(const char *ptr, const char *end, uint64_t *val) {
190   uint64_t u64 = 0;
191   while (ptr < end) {
192     unsigned ch = (unsigned)(*ptr - '0');
193     if (ch >= 10) break;
194     if (u64 > UINT64_MAX / 10 || u64 * 10 > UINT64_MAX - ch) {
195       return false;
196     }
197     u64 *= 10;
198     u64 += ch;
199     ptr++;
200   }
201 
202   if (ptr != end) {
203     // In PHP tradition, we allow truncation: "1.1" -> 1.
204     // But we don't allow 'e', eg. '1.1e2' or any other non-numeric chars.
205     if (*ptr++ != '.') return false;
206 
207     for (;ptr < end; ptr++) {
208       if (*ptr < '0' || *ptr > '9') {
209         return false;
210       }
211     }
212   }
213 
214   *val = u64;
215   return true;
216 }
217 
buftoint64(const char * ptr,const char * end,int64_t * val)218 static bool buftoint64(const char *ptr, const char *end, int64_t *val) {
219   bool neg = false;
220   uint64_t u64;
221 
222   if (ptr != end && *ptr == '-') {
223     ptr++;
224     neg = true;
225   }
226 
227   if (!buftouint64(ptr, end, &u64) ||
228       u64 > (uint64_t)INT64_MAX + neg) {
229     return false;
230   }
231 
232   *val = neg ? -u64 : u64;
233   return true;
234 }
235 
throw_conversion_exception(const char * to,const zval * zv)236 static void throw_conversion_exception(const char *to, const zval *zv) {
237   zval tmp;
238   ZVAL_COPY(&tmp, zv);
239   convert_to_string(&tmp);
240 
241   zend_throw_exception_ex(NULL, 0, "Cannot convert '%s' to %s",
242                           Z_STRVAL_P(&tmp), to);
243 
244   zval_ptr_dtor(&tmp);
245 }
246 
Convert_PhpToInt64(const zval * php_val,int64_t * i64)247 bool Convert_PhpToInt64(const zval *php_val, int64_t *i64) {
248   switch (Z_TYPE_P(php_val)) {
249     case IS_LONG:
250       *i64 = Z_LVAL_P(php_val);
251       return true;
252     case IS_DOUBLE: {
253       double dbl = Z_DVAL_P(php_val);
254       if (dbl > 9223372036854774784.0 || dbl < -9223372036854775808.0) {
255         zend_throw_exception_ex(NULL, 0, "Out of range");
256         return false;
257       }
258       *i64 = dbl; /* must be guarded, overflow here is UB */
259       return true;
260     }
261     case IS_STRING: {
262       const char *buf = Z_STRVAL_P(php_val);
263       // PHP would accept scientific notation here, but we're going to be a
264       // little more discerning and only accept pure integers.
265       bool ok = buftoint64(buf, buf + Z_STRLEN_P(php_val), i64);
266       if (!ok) {
267         throw_conversion_exception("integer", php_val);
268       }
269       return ok;
270     }
271     default:
272       throw_conversion_exception("integer", php_val);
273       return false;
274   }
275 }
276 
to_double(zval * php_val,double * dbl)277 static bool to_double(zval *php_val, double *dbl) {
278   switch (Z_TYPE_P(php_val)) {
279     case IS_LONG:
280       *dbl = Z_LVAL_P(php_val);
281       return true;
282     case IS_DOUBLE:
283       *dbl = Z_DVAL_P(php_val);
284       return true;
285     case IS_STRING: {
286       zend_long lval;
287       switch (is_numeric_string(Z_STRVAL_P(php_val), Z_STRLEN_P(php_val), &lval,
288                                 dbl, false)) {
289         case IS_LONG:
290           *dbl = lval;
291           return true;
292         case IS_DOUBLE:
293           return true;
294         default:
295           goto fail;
296       }
297     }
298     default:
299      fail:
300       throw_conversion_exception("double", php_val);
301       return false;
302   }
303 }
304 
to_bool(zval * from,bool * to)305 static bool to_bool(zval* from, bool* to) {
306   switch (Z_TYPE_P(from)) {
307     case IS_TRUE:
308       *to = true;
309       return true;
310     case IS_FALSE:
311       *to = false;
312       return true;
313     case IS_LONG:
314       *to = (Z_LVAL_P(from) != 0);
315       return true;
316     case IS_DOUBLE:
317       *to = (Z_LVAL_P(from) != 0);
318       return true;
319     case IS_STRING:
320       if (Z_STRLEN_P(from) == 0 ||
321           (Z_STRLEN_P(from) == 1 && Z_STRVAL_P(from)[0] == '0')) {
322         *to = false;
323       } else {
324         *to = true;
325       }
326       return true;
327     default:
328       throw_conversion_exception("bool", from);
329       return false;
330   }
331 }
332 
to_string(zval * from)333 static bool to_string(zval* from) {
334   if (Z_ISREF_P(from)) {
335     ZVAL_DEREF(from);
336   }
337 
338   switch (Z_TYPE_P(from)) {
339     case IS_STRING:
340       return true;
341     case IS_TRUE:
342     case IS_FALSE:
343     case IS_LONG:
344     case IS_DOUBLE: {
345       zval tmp;
346       zend_make_printable_zval(from, &tmp);
347       ZVAL_COPY_VALUE(from, &tmp);
348       return true;
349     }
350     default:
351       throw_conversion_exception("string", from);
352       return false;
353   }
354 }
355 
Convert_PhpToUpb(zval * php_val,upb_msgval * upb_val,TypeInfo type,upb_arena * arena)356 bool Convert_PhpToUpb(zval *php_val, upb_msgval *upb_val, TypeInfo type,
357                       upb_arena *arena) {
358   int64_t i64;
359 
360   if (Z_ISREF_P(php_val)) {
361     ZVAL_DEREF(php_val);
362   }
363 
364   switch (type.type) {
365     case UPB_TYPE_INT64:
366       return Convert_PhpToInt64(php_val, &upb_val->int64_val);
367     case UPB_TYPE_INT32:
368     case UPB_TYPE_ENUM:
369       if (!Convert_PhpToInt64(php_val, &i64)) {
370         return false;
371       }
372       upb_val->int32_val = i64;
373       return true;
374     case UPB_TYPE_UINT64:
375       if (!Convert_PhpToInt64(php_val, &i64)) {
376         return false;
377       }
378       upb_val->uint64_val = i64;
379       return true;
380     case UPB_TYPE_UINT32:
381       if (!Convert_PhpToInt64(php_val, &i64)) {
382         return false;
383       }
384       upb_val->uint32_val = i64;
385       return true;
386     case UPB_TYPE_DOUBLE:
387       return to_double(php_val, &upb_val->double_val);
388     case UPB_TYPE_FLOAT:
389       if (!to_double(php_val, &upb_val->double_val)) return false;
390       upb_val->float_val = upb_val->double_val;
391       return true;
392     case UPB_TYPE_BOOL:
393       return to_bool(php_val, &upb_val->bool_val);
394     case UPB_TYPE_STRING:
395     case UPB_TYPE_BYTES: {
396       char *ptr;
397       size_t size;
398 
399       if (!to_string(php_val)) return false;
400 
401       size = Z_STRLEN_P(php_val);
402 
403       // If arena is NULL we reference the input zval.
404       // The resulting upb_strview will only be value while the zval is alive.
405       if (arena) {
406         ptr = upb_arena_malloc(arena, size);
407         memcpy(ptr, Z_STRVAL_P(php_val), size);
408       } else {
409         ptr = Z_STRVAL_P(php_val);
410       }
411 
412       upb_val->str_val = upb_strview_make(ptr, size);
413       return true;
414     }
415     case UPB_TYPE_MESSAGE:
416       PBPHP_ASSERT(type.desc);
417       return Message_GetUpbMessage(php_val, type.desc, arena,
418                                    (upb_msg **)&upb_val->msg_val);
419   }
420 
421   return false;
422 }
423 
Convert_UpbToPhp(upb_msgval upb_val,zval * php_val,TypeInfo type,zval * arena)424 void Convert_UpbToPhp(upb_msgval upb_val, zval *php_val, TypeInfo type,
425                       zval *arena) {
426   switch (type.type) {
427     case UPB_TYPE_INT64:
428 #if SIZEOF_ZEND_LONG == 8
429       ZVAL_LONG(php_val, upb_val.int64_val);
430 #else
431       {
432         char buf[20];
433         int size = sprintf(buf, "%lld", upb_val.int64_val);
434         ZVAL_NEW_STR(php_val, zend_string_init(buf, size, 0));
435       }
436 #endif
437       break;
438     case UPB_TYPE_UINT64:
439 #if SIZEOF_ZEND_LONG == 8
440       ZVAL_LONG(php_val, upb_val.uint64_val);
441 #else
442       {
443         char buf[20];
444         int size = sprintf(buf, "%lld", (int64_t)upb_val.uint64_val);
445         ZVAL_NEW_STR(php_val, zend_string_init(buf, size, 0));
446       }
447 #endif
448       break;
449     case UPB_TYPE_INT32:
450     case UPB_TYPE_ENUM:
451       ZVAL_LONG(php_val, upb_val.int32_val);
452       break;
453     case UPB_TYPE_UINT32: {
454       // Sign-extend for consistency between 32/64-bit builds.
455       zend_long val = (int32_t)upb_val.uint32_val;
456       ZVAL_LONG(php_val, val);
457       break;
458     }
459     case UPB_TYPE_DOUBLE:
460       ZVAL_DOUBLE(php_val, upb_val.double_val);
461       break;
462     case UPB_TYPE_FLOAT:
463       ZVAL_DOUBLE(php_val, upb_val.float_val);
464       break;
465     case UPB_TYPE_BOOL:
466       ZVAL_BOOL(php_val, upb_val.bool_val);
467       break;
468     case UPB_TYPE_STRING:
469     case UPB_TYPE_BYTES: {
470       upb_strview str = upb_val.str_val;
471       ZVAL_NEW_STR(php_val, zend_string_init(str.data, str.size, 0));
472       break;
473     }
474     case UPB_TYPE_MESSAGE:
475       PBPHP_ASSERT(type.desc);
476       Message_GetPhpWrapper(php_val, type.desc, (upb_msg *)upb_val.msg_val,
477                             arena);
478       break;
479   }
480 }
481 
Convert_PhpToUpbAutoWrap(zval * val,upb_msgval * upb_val,TypeInfo type,upb_arena * arena)482 bool Convert_PhpToUpbAutoWrap(zval *val, upb_msgval *upb_val, TypeInfo type,
483                               upb_arena *arena) {
484   const upb_msgdef *subm = type.desc ? type.desc->msgdef : NULL;
485   if (subm && upb_msgdef_iswrapper(subm) && Z_TYPE_P(val) != IS_OBJECT) {
486     // Assigning a scalar to a wrapper-typed value. We will automatically wrap
487     // the value, so the user doesn't need to create a FooWrapper(['value': X])
488     // message manually.
489     upb_msg *wrapper = upb_msg_new(subm, arena);
490     const upb_fielddef *val_f = upb_msgdef_itof(subm, 1);
491     upb_msgval msgval;
492     if (!Convert_PhpToUpb(val, &msgval, TypeInfo_Get(val_f), arena)) return false;
493     upb_msg_set(wrapper, val_f, msgval, arena);
494     upb_val->msg_val = wrapper;
495     return true;
496   } else {
497     // Convert_PhpToUpb doesn't auto-construct messages. This means that we only
498     // allow:
499     //   ['foo_submsg': new Foo(['a' => 1])]
500     // not:
501     //   ['foo_submsg': ['a' => 1]]
502     return Convert_PhpToUpb(val, upb_val, type, arena);
503   }
504 }
505 
Convert_ModuleInit(void)506 void Convert_ModuleInit(void) {
507   const char *prefix_name = "TYPE_URL_PREFIX";
508   zend_class_entry class_type;
509 
510   INIT_CLASS_ENTRY(class_type, "Google\\Protobuf\\Internal\\GPBUtil",
511                    util_methods);
512   GPBUtil_class_entry = zend_register_internal_class(&class_type);
513 
514   zend_declare_class_constant_string(GPBUtil_class_entry, prefix_name,
515                                      strlen(prefix_name),
516                                      "type.googleapis.com/");
517 }
518