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