Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 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 <stdint.h> |
| 32 | #include <protobuf.h> |
| 33 | #include <Zend/zend.h> |
| 34 | |
| 35 | #include "utf8.h" |
| 36 | |
| 37 | // ----------------------------------------------------------------------------- |
| 38 | // Native slot storage. |
| 39 | // ----------------------------------------------------------------------------- |
| 40 | |
| 41 | #define DEREF(memory, type) *(type*)(memory) |
| 42 | |
| 43 | size_t native_slot_size(upb_fieldtype_t type) { |
| 44 | switch (type) { |
| 45 | case UPB_TYPE_FLOAT: return 4; |
| 46 | case UPB_TYPE_DOUBLE: return 8; |
| 47 | case UPB_TYPE_BOOL: return 1; |
| 48 | case UPB_TYPE_STRING: return sizeof(void*); |
| 49 | case UPB_TYPE_BYTES: return sizeof(void*); |
| 50 | case UPB_TYPE_MESSAGE: return sizeof(void*); |
| 51 | case UPB_TYPE_ENUM: return 4; |
| 52 | case UPB_TYPE_INT32: return 4; |
| 53 | case UPB_TYPE_INT64: return 8; |
| 54 | case UPB_TYPE_UINT32: return 4; |
| 55 | case UPB_TYPE_UINT64: return 8; |
| 56 | default: return 0; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | static bool native_slot_is_default(upb_fieldtype_t type, const void* memory) { |
| 61 | switch (type) { |
| 62 | #define CASE_TYPE(upb_type, c_type) \ |
| 63 | case UPB_TYPE_##upb_type: { \ |
| 64 | return DEREF(memory, c_type) == 0; \ |
| 65 | } |
| 66 | CASE_TYPE(INT32, int32_t ) |
| 67 | CASE_TYPE(UINT32, uint32_t) |
| 68 | CASE_TYPE(ENUM, int32_t ) |
| 69 | CASE_TYPE(INT64, int64_t ) |
| 70 | CASE_TYPE(UINT64, uint64_t) |
| 71 | CASE_TYPE(FLOAT, float ) |
| 72 | CASE_TYPE(DOUBLE, double ) |
| 73 | CASE_TYPE(BOOL, int8_t ) |
| 74 | |
| 75 | #undef CASE_TYPE |
| 76 | case UPB_TYPE_STRING: |
| 77 | case UPB_TYPE_BYTES: |
| 78 | return Z_STRLEN_P(CACHED_PTR_TO_ZVAL_PTR(DEREF(memory, CACHED_VALUE*))) == |
| 79 | 0; |
| 80 | case UPB_TYPE_MESSAGE: |
| 81 | return Z_TYPE_P(CACHED_PTR_TO_ZVAL_PTR(DEREF(memory, CACHED_VALUE*))) == |
| 82 | IS_NULL; |
| 83 | default: return false; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | bool native_slot_set(upb_fieldtype_t type, const zend_class_entry* klass, |
| 88 | void* memory, zval* value PHP_PROTO_TSRMLS_DC) { |
| 89 | switch (type) { |
| 90 | case UPB_TYPE_STRING: |
| 91 | case UPB_TYPE_BYTES: { |
| 92 | if (!protobuf_convert_to_string(value)) { |
| 93 | return false; |
| 94 | } |
| 95 | if (type == UPB_TYPE_STRING && |
| 96 | !is_structurally_valid_utf8(Z_STRVAL_P(value), Z_STRLEN_P(value))) { |
| 97 | zend_error(E_USER_ERROR, "Given string is not UTF8 encoded."); |
| 98 | return false; |
| 99 | } |
| 100 | |
| 101 | zval* cached_zval = CACHED_PTR_TO_ZVAL_PTR((CACHED_VALUE*)memory); |
| 102 | if (EXPECTED(cached_zval != NULL)) { |
| 103 | #if PHP_MAJOR_VERSION < 7 |
| 104 | REPLACE_ZVAL_VALUE((zval**)memory, value, 1); |
| 105 | #else |
| 106 | zend_assign_to_variable(cached_zval, value, IS_CV); |
| 107 | #endif |
| 108 | } |
| 109 | break; |
| 110 | } |
| 111 | case UPB_TYPE_MESSAGE: { |
| 112 | if (Z_TYPE_P(value) != IS_OBJECT && Z_TYPE_P(value) != IS_NULL) { |
| 113 | zend_error(E_USER_ERROR, "Given value is not message."); |
| 114 | return false; |
| 115 | } |
| 116 | if (Z_TYPE_P(value) == IS_OBJECT && klass != Z_OBJCE_P(value)) { |
| 117 | zend_error(E_USER_ERROR, "Given message does not have correct class."); |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | zval* property_ptr = CACHED_PTR_TO_ZVAL_PTR((CACHED_VALUE*)memory); |
| 122 | if (EXPECTED(property_ptr != value)) { |
| 123 | php_proto_zval_ptr_dtor(property_ptr); |
| 124 | } |
| 125 | |
| 126 | #if PHP_MAJOR_VERSION < 7 |
| 127 | DEREF(memory, zval*) = value; |
| 128 | Z_ADDREF_P(value); |
| 129 | #else |
| 130 | ZVAL_ZVAL(property_ptr, value, 1, 0); |
| 131 | #endif |
| 132 | break; |
| 133 | } |
| 134 | |
| 135 | #define CASE_TYPE(upb_type, type, c_type, php_type) \ |
| 136 | case UPB_TYPE_##upb_type: { \ |
| 137 | c_type type##_value; \ |
| 138 | if (protobuf_convert_to_##type(value, &type##_value)) { \ |
| 139 | DEREF(memory, c_type) = type##_value; \ |
| 140 | } \ |
| 141 | break; \ |
| 142 | } |
| 143 | CASE_TYPE(INT32, int32, int32_t, LONG) |
| 144 | CASE_TYPE(UINT32, uint32, uint32_t, LONG) |
| 145 | CASE_TYPE(ENUM, int32, int32_t, LONG) |
| 146 | CASE_TYPE(INT64, int64, int64_t, LONG) |
| 147 | CASE_TYPE(UINT64, uint64, uint64_t, LONG) |
| 148 | CASE_TYPE(FLOAT, float, float, DOUBLE) |
| 149 | CASE_TYPE(DOUBLE, double, double, DOUBLE) |
| 150 | CASE_TYPE(BOOL, bool, int8_t, BOOL) |
| 151 | |
| 152 | #undef CASE_TYPE |
| 153 | |
| 154 | default: |
| 155 | break; |
| 156 | } |
| 157 | |
| 158 | return true; |
| 159 | } |
| 160 | |
| 161 | bool native_slot_set_by_array(upb_fieldtype_t type, |
| 162 | const zend_class_entry* klass, void* memory, |
| 163 | zval* value TSRMLS_DC) { |
| 164 | switch (type) { |
| 165 | case UPB_TYPE_STRING: |
| 166 | case UPB_TYPE_BYTES: { |
| 167 | if (!protobuf_convert_to_string(value)) { |
| 168 | return false; |
| 169 | } |
| 170 | if (type == UPB_TYPE_STRING && |
| 171 | !is_structurally_valid_utf8(Z_STRVAL_P(value), Z_STRLEN_P(value))) { |
| 172 | zend_error(E_USER_ERROR, "Given string is not UTF8 encoded."); |
| 173 | return false; |
| 174 | } |
| 175 | |
| 176 | // Handles repeated/map string field. Memory provided by |
| 177 | // RepeatedField/Map is not initialized. |
| 178 | #if PHP_MAJOR_VERSION < 7 |
| 179 | MAKE_STD_ZVAL(DEREF(memory, zval*)); |
| 180 | PHP_PROTO_ZVAL_STRINGL(DEREF(memory, zval*), Z_STRVAL_P(value), |
| 181 | Z_STRLEN_P(value), 1); |
| 182 | #else |
| 183 | *(zend_string**)memory = zend_string_dup(Z_STR_P(value), 0); |
| 184 | #endif |
| 185 | break; |
| 186 | } |
| 187 | case UPB_TYPE_MESSAGE: { |
| 188 | if (Z_TYPE_P(value) != IS_OBJECT) { |
| 189 | zend_error(E_USER_ERROR, "Given value is not message."); |
| 190 | return false; |
| 191 | } |
| 192 | if (Z_TYPE_P(value) == IS_OBJECT && klass != Z_OBJCE_P(value)) { |
| 193 | zend_error(E_USER_ERROR, "Given message does not have correct class."); |
| 194 | return false; |
| 195 | } |
| 196 | #if PHP_MAJOR_VERSION < 7 |
| 197 | if (EXPECTED(DEREF(memory, zval*) != value)) { |
| 198 | DEREF(memory, zval*) = value; |
| 199 | Z_ADDREF_P(value); |
| 200 | } |
| 201 | #else |
| 202 | DEREF(memory, zval*) = value; |
| 203 | GC_ADDREF(Z_OBJ_P(value)); |
| 204 | #endif |
| 205 | break; |
| 206 | } |
| 207 | default: |
| 208 | return native_slot_set(type, klass, memory, value TSRMLS_CC); |
| 209 | } |
| 210 | return true; |
| 211 | } |
| 212 | |
| 213 | bool native_slot_set_by_map(upb_fieldtype_t type, const zend_class_entry* klass, |
| 214 | void* memory, zval* value TSRMLS_DC) { |
| 215 | switch (type) { |
| 216 | case UPB_TYPE_STRING: |
| 217 | case UPB_TYPE_BYTES: { |
| 218 | if (!protobuf_convert_to_string(value)) { |
| 219 | return false; |
| 220 | } |
| 221 | if (type == UPB_TYPE_STRING && |
| 222 | !is_structurally_valid_utf8(Z_STRVAL_P(value), Z_STRLEN_P(value))) { |
| 223 | zend_error(E_USER_ERROR, "Given string is not UTF8 encoded."); |
| 224 | return false; |
| 225 | } |
| 226 | |
| 227 | // Handles repeated/map string field. Memory provided by |
| 228 | // RepeatedField/Map is not initialized. |
| 229 | #if PHP_MAJOR_VERSION < 7 |
| 230 | MAKE_STD_ZVAL(DEREF(memory, zval*)); |
| 231 | PHP_PROTO_ZVAL_STRINGL(DEREF(memory, zval*), Z_STRVAL_P(value), |
| 232 | Z_STRLEN_P(value), 1); |
| 233 | #else |
| 234 | *(zend_string**)memory = zend_string_dup(Z_STR_P(value), 0); |
| 235 | #endif |
| 236 | break; |
| 237 | } |
| 238 | case UPB_TYPE_MESSAGE: { |
| 239 | if (Z_TYPE_P(value) != IS_OBJECT) { |
| 240 | zend_error(E_USER_ERROR, "Given value is not message."); |
| 241 | return false; |
| 242 | } |
| 243 | if (Z_TYPE_P(value) == IS_OBJECT && klass != Z_OBJCE_P(value)) { |
| 244 | zend_error(E_USER_ERROR, "Given message does not have correct class."); |
| 245 | return false; |
| 246 | } |
| 247 | #if PHP_MAJOR_VERSION < 7 |
| 248 | if (EXPECTED(DEREF(memory, zval*) != value)) { |
| 249 | DEREF(memory, zval*) = value; |
| 250 | Z_ADDREF_P(value); |
| 251 | } |
| 252 | #else |
| 253 | DEREF(memory, zend_object*) = Z_OBJ_P(value); |
| 254 | GC_ADDREF(Z_OBJ_P(value)); |
| 255 | #endif |
| 256 | break; |
| 257 | } |
| 258 | default: |
| 259 | return native_slot_set(type, klass, memory, value TSRMLS_CC); |
| 260 | } |
| 261 | return true; |
| 262 | } |
| 263 | |
| 264 | void native_slot_init(upb_fieldtype_t type, void* memory, CACHED_VALUE* cache) { |
| 265 | zval* tmp = NULL; |
| 266 | switch (type) { |
| 267 | case UPB_TYPE_FLOAT: |
| 268 | DEREF(memory, float) = 0.0; |
| 269 | break; |
| 270 | case UPB_TYPE_DOUBLE: |
| 271 | DEREF(memory, double) = 0.0; |
| 272 | break; |
| 273 | case UPB_TYPE_BOOL: |
| 274 | DEREF(memory, int8_t) = 0; |
| 275 | break; |
| 276 | case UPB_TYPE_STRING: |
| 277 | case UPB_TYPE_BYTES: |
| 278 | case UPB_TYPE_MESSAGE: |
| 279 | DEREF(memory, CACHED_VALUE*) = cache; |
| 280 | break; |
| 281 | case UPB_TYPE_ENUM: |
| 282 | case UPB_TYPE_INT32: |
| 283 | DEREF(memory, int32_t) = 0; |
| 284 | break; |
| 285 | case UPB_TYPE_INT64: |
| 286 | DEREF(memory, int64_t) = 0; |
| 287 | break; |
| 288 | case UPB_TYPE_UINT32: |
| 289 | DEREF(memory, uint32_t) = 0; |
| 290 | break; |
| 291 | case UPB_TYPE_UINT64: |
| 292 | DEREF(memory, uint64_t) = 0; |
| 293 | break; |
| 294 | default: |
| 295 | break; |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | void native_slot_get(upb_fieldtype_t type, const void* memory, |
| 300 | CACHED_VALUE* cache TSRMLS_DC) { |
| 301 | switch (type) { |
| 302 | #define CASE(upb_type, php_type, c_type) \ |
| 303 | case UPB_TYPE_##upb_type: \ |
| 304 | PHP_PROTO_SEPARATE_ZVAL_IF_NOT_REF(cache); \ |
| 305 | ZVAL_##php_type(CACHED_PTR_TO_ZVAL_PTR(cache), DEREF(memory, c_type)); \ |
| 306 | return; |
| 307 | |
| 308 | CASE(FLOAT, DOUBLE, float) |
| 309 | CASE(DOUBLE, DOUBLE, double) |
| 310 | CASE(BOOL, BOOL, int8_t) |
| 311 | CASE(INT32, LONG, int32_t) |
| 312 | CASE(ENUM, LONG, uint32_t) |
| 313 | |
| 314 | #undef CASE |
| 315 | |
| 316 | #if SIZEOF_LONG == 4 |
| 317 | #define CASE(upb_type, c_type) \ |
| 318 | case UPB_TYPE_##upb_type: { \ |
| 319 | PHP_PROTO_SEPARATE_ZVAL_IF_NOT_REF(cache); \ |
| 320 | char buffer[MAX_LENGTH_OF_INT64]; \ |
| 321 | sprintf(buffer, "%lld", DEREF(memory, c_type)); \ |
| 322 | PHP_PROTO_ZVAL_STRING(CACHED_PTR_TO_ZVAL_PTR(cache), buffer, 1); \ |
| 323 | return; \ |
| 324 | } |
| 325 | #else |
| 326 | #define CASE(upb_type, c_type) \ |
| 327 | case UPB_TYPE_##upb_type: { \ |
| 328 | PHP_PROTO_SEPARATE_ZVAL_IF_NOT_REF(cache); \ |
| 329 | ZVAL_LONG(CACHED_PTR_TO_ZVAL_PTR(cache), DEREF(memory, c_type)); \ |
| 330 | return; \ |
| 331 | } |
| 332 | #endif |
| 333 | CASE(UINT64, uint64_t) |
| 334 | CASE(INT64, int64_t) |
| 335 | #undef CASE |
| 336 | |
| 337 | case UPB_TYPE_UINT32: { |
| 338 | // Prepend bit-1 for negative numbers, so that uint32 value will be |
| 339 | // consistent on both 32-bit and 64-bit architectures. |
| 340 | PHP_PROTO_SEPARATE_ZVAL_IF_NOT_REF(cache); |
| 341 | int value = DEREF(memory, int32_t); |
| 342 | if (sizeof(int) == 8) { |
| 343 | value |= (-((value >> 31) & 0x1) & 0xFFFFFFFF00000000); |
| 344 | } |
| 345 | ZVAL_LONG(CACHED_PTR_TO_ZVAL_PTR(cache), value); |
| 346 | return; |
| 347 | } |
| 348 | |
| 349 | case UPB_TYPE_STRING: |
| 350 | case UPB_TYPE_BYTES: { |
| 351 | // For optional string/bytes/message fields, the cache is owned by the |
| 352 | // containing message and should have been updated during |
| 353 | // setting/decoding. However, oneof accessor call this function by |
| 354 | // providing the return value directly, which is not the same as the cache |
| 355 | // value. |
| 356 | zval* value = CACHED_PTR_TO_ZVAL_PTR((CACHED_VALUE*)memory); |
| 357 | if (CACHED_PTR_TO_ZVAL_PTR(cache) != value) { |
| 358 | PHP_PROTO_ZVAL_STRINGL(CACHED_PTR_TO_ZVAL_PTR(cache), Z_STRVAL_P(value), |
| 359 | Z_STRLEN_P(value), 1); |
| 360 | } |
| 361 | break; |
| 362 | } |
| 363 | case UPB_TYPE_MESSAGE: { |
| 364 | // Same as above for string/bytes fields. |
| 365 | zval* value = CACHED_PTR_TO_ZVAL_PTR((CACHED_VALUE*)memory); |
| 366 | if (CACHED_PTR_TO_ZVAL_PTR(cache) != value) { |
| 367 | ZVAL_ZVAL(CACHED_PTR_TO_ZVAL_PTR(cache), value, 1, 0); |
| 368 | } |
| 369 | return; |
| 370 | } |
| 371 | default: |
| 372 | return; |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | void native_slot_get_by_array(upb_fieldtype_t type, const void* memory, |
| 377 | CACHED_VALUE* cache TSRMLS_DC) { |
| 378 | switch (type) { |
| 379 | case UPB_TYPE_STRING: |
| 380 | case UPB_TYPE_BYTES: { |
| 381 | #if PHP_MAJOR_VERSION < 7 |
| 382 | zval* value = CACHED_PTR_TO_ZVAL_PTR((CACHED_VALUE*)memory); |
| 383 | if (EXPECTED(CACHED_PTR_TO_ZVAL_PTR(cache) != value)) { |
| 384 | PHP_PROTO_ZVAL_STRINGL(CACHED_PTR_TO_ZVAL_PTR(cache), |
| 385 | Z_STRVAL_P(value), Z_STRLEN_P(value), 1); |
| 386 | } |
| 387 | #else |
| 388 | ZVAL_NEW_STR(cache, zend_string_dup(*(zend_string**)memory, 0)); |
| 389 | #endif |
| 390 | return; |
| 391 | } |
| 392 | case UPB_TYPE_MESSAGE: { |
| 393 | #if PHP_MAJOR_VERSION < 7 |
| 394 | zval* value = CACHED_PTR_TO_ZVAL_PTR((CACHED_VALUE*)memory); |
| 395 | if (EXPECTED(CACHED_PTR_TO_ZVAL_PTR(cache) != value)) { |
| 396 | ZVAL_ZVAL(CACHED_PTR_TO_ZVAL_PTR(cache), value, 1, 0); |
| 397 | } |
| 398 | #else |
| 399 | ZVAL_COPY(CACHED_PTR_TO_ZVAL_PTR(cache), memory); |
| 400 | #endif |
| 401 | return; |
| 402 | } |
| 403 | default: |
| 404 | native_slot_get(type, memory, cache TSRMLS_CC); |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | void native_slot_get_by_map_key(upb_fieldtype_t type, const void* memory, |
| 409 | int length, CACHED_VALUE* cache TSRMLS_DC) { |
| 410 | switch (type) { |
| 411 | case UPB_TYPE_STRING: |
| 412 | case UPB_TYPE_BYTES: { |
| 413 | PHP_PROTO_ZVAL_STRINGL(CACHED_PTR_TO_ZVAL_PTR(cache), memory, length, 1); |
| 414 | return; |
| 415 | } |
| 416 | default: |
| 417 | native_slot_get(type, memory, cache TSRMLS_CC); |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | void native_slot_get_by_map_value(upb_fieldtype_t type, const void* memory, |
| 422 | CACHED_VALUE* cache TSRMLS_DC) { |
| 423 | switch (type) { |
| 424 | case UPB_TYPE_MESSAGE: { |
| 425 | #if PHP_MAJOR_VERSION < 7 |
| 426 | zval* value = CACHED_PTR_TO_ZVAL_PTR((CACHED_VALUE*)memory); |
| 427 | if (EXPECTED(CACHED_PTR_TO_ZVAL_PTR(cache) != value)) { |
| 428 | ZVAL_ZVAL(CACHED_PTR_TO_ZVAL_PTR(cache), value, 1, 0); |
| 429 | } |
| 430 | #else |
| 431 | GC_ADDREF(*(zend_object**)memory); |
| 432 | ZVAL_OBJ(cache, *(zend_object**)memory); |
| 433 | #endif |
| 434 | return; |
| 435 | } |
| 436 | default: |
| 437 | native_slot_get_by_array(type, memory, cache TSRMLS_CC); |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | void native_slot_get_default(upb_fieldtype_t type, |
| 442 | CACHED_VALUE* cache TSRMLS_DC) { |
| 443 | switch (type) { |
| 444 | #define CASE(upb_type, php_type) \ |
| 445 | case UPB_TYPE_##upb_type: \ |
| 446 | PHP_PROTO_SEPARATE_ZVAL_IF_NOT_REF(cache); \ |
| 447 | ZVAL_##php_type(CACHED_PTR_TO_ZVAL_PTR(cache), 0); \ |
| 448 | return; |
| 449 | |
| 450 | CASE(FLOAT, DOUBLE) |
| 451 | CASE(DOUBLE, DOUBLE) |
| 452 | CASE(BOOL, BOOL) |
| 453 | CASE(INT32, LONG) |
| 454 | CASE(UINT32, LONG) |
| 455 | CASE(ENUM, LONG) |
| 456 | |
| 457 | #undef CASE |
| 458 | |
| 459 | #if SIZEOF_LONG == 4 |
| 460 | #define CASE(upb_type) \ |
| 461 | case UPB_TYPE_##upb_type: { \ |
| 462 | PHP_PROTO_SEPARATE_ZVAL_IF_NOT_REF(cache); \ |
| 463 | PHP_PROTO_ZVAL_STRING(CACHED_PTR_TO_ZVAL_PTR(cache), "0", 1); \ |
| 464 | return; \ |
| 465 | } |
| 466 | #else |
| 467 | #define CASE(upb_type) \ |
| 468 | case UPB_TYPE_##upb_type: { \ |
| 469 | PHP_PROTO_SEPARATE_ZVAL_IF_NOT_REF(cache); \ |
| 470 | ZVAL_LONG(CACHED_PTR_TO_ZVAL_PTR(cache), 0); \ |
| 471 | return; \ |
| 472 | } |
| 473 | #endif |
| 474 | CASE(UINT64) |
| 475 | CASE(INT64) |
| 476 | #undef CASE |
| 477 | |
| 478 | case UPB_TYPE_STRING: |
| 479 | case UPB_TYPE_BYTES: { |
| 480 | PHP_PROTO_SEPARATE_ZVAL_IF_NOT_REF(cache); |
| 481 | PHP_PROTO_ZVAL_STRINGL(CACHED_PTR_TO_ZVAL_PTR(cache), "", 0, 1); |
| 482 | break; |
| 483 | } |
| 484 | case UPB_TYPE_MESSAGE: { |
| 485 | PHP_PROTO_SEPARATE_ZVAL_IF_NOT_REF(cache); |
| 486 | ZVAL_NULL(CACHED_PTR_TO_ZVAL_PTR(cache)); |
| 487 | return; |
| 488 | } |
| 489 | default: |
| 490 | return; |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | // ----------------------------------------------------------------------------- |
| 495 | // Map field utilities. |
| 496 | // ---------------------------------------------------------------------------- |
| 497 | |
| 498 | const upb_msgdef* tryget_map_entry_msgdef(const upb_fielddef* field) { |
| 499 | const upb_msgdef* subdef; |
| 500 | if (upb_fielddef_label(field) != UPB_LABEL_REPEATED || |
| 501 | upb_fielddef_type(field) != UPB_TYPE_MESSAGE) { |
| 502 | return NULL; |
| 503 | } |
| 504 | subdef = upb_fielddef_msgsubdef(field); |
| 505 | return upb_msgdef_mapentry(subdef) ? subdef : NULL; |
| 506 | } |
| 507 | |
| 508 | const upb_msgdef* map_entry_msgdef(const upb_fielddef* field) { |
| 509 | const upb_msgdef* subdef = tryget_map_entry_msgdef(field); |
| 510 | assert(subdef); |
| 511 | return subdef; |
| 512 | } |
| 513 | |
| 514 | bool is_map_field(const upb_fielddef* field) { |
| 515 | return tryget_map_entry_msgdef(field) != NULL; |
| 516 | } |
| 517 | |
| 518 | const upb_fielddef* map_field_key(const upb_fielddef* field) { |
| 519 | const upb_msgdef* subdef = map_entry_msgdef(field); |
| 520 | return map_entry_key(subdef); |
| 521 | } |
| 522 | |
| 523 | const upb_fielddef* map_field_value(const upb_fielddef* field) { |
| 524 | const upb_msgdef* subdef = map_entry_msgdef(field); |
| 525 | return map_entry_value(subdef); |
| 526 | } |
| 527 | |
| 528 | const upb_fielddef* map_entry_key(const upb_msgdef* msgdef) { |
| 529 | const upb_fielddef* key_field = upb_msgdef_itof(msgdef, MAP_KEY_FIELD); |
| 530 | assert(key_field != NULL); |
| 531 | return key_field; |
| 532 | } |
| 533 | |
| 534 | const upb_fielddef* map_entry_value(const upb_msgdef* msgdef) { |
| 535 | const upb_fielddef* value_field = upb_msgdef_itof(msgdef, MAP_VALUE_FIELD); |
| 536 | assert(value_field != NULL); |
| 537 | return value_field; |
| 538 | } |
| 539 | |
| 540 | const zend_class_entry* field_type_class( |
| 541 | const upb_fielddef* field PHP_PROTO_TSRMLS_DC) { |
| 542 | if (upb_fielddef_type(field) == UPB_TYPE_MESSAGE) { |
| 543 | Descriptor* desc = UNBOX_HASHTABLE_VALUE( |
| 544 | Descriptor, get_def_obj(upb_fielddef_subdef(field))); |
| 545 | return desc->klass; |
| 546 | } else if (upb_fielddef_type(field) == UPB_TYPE_ENUM) { |
| 547 | EnumDescriptor* desc = UNBOX_HASHTABLE_VALUE( |
| 548 | EnumDescriptor, get_def_obj(upb_fielddef_subdef(field))); |
| 549 | return desc->klass; |
| 550 | } |
| 551 | return NULL; |
| 552 | } |
| 553 | |
| 554 | // ----------------------------------------------------------------------------- |
| 555 | // Memory layout management. |
| 556 | // ----------------------------------------------------------------------------- |
| 557 | |
| 558 | static size_t align_up_to(size_t offset, size_t granularity) { |
| 559 | // Granularity must be a power of two. |
| 560 | return (offset + granularity - 1) & ~(granularity - 1); |
| 561 | } |
| 562 | |
| 563 | static uint32_t* slot_oneof_case(MessageLayout* layout, const void* storage, |
| 564 | const upb_fielddef* field) { |
| 565 | return (uint32_t*)(((uint8_t*)storage) + |
| 566 | layout->fields[upb_fielddef_index(field)].case_offset); |
| 567 | } |
| 568 | |
| 569 | static int slot_property_cache(MessageLayout* layout, const void* storage, |
| 570 | const upb_fielddef* field) { |
| 571 | return layout->fields[upb_fielddef_index(field)].cache_index; |
| 572 | } |
| 573 | |
| 574 | void* slot_memory(MessageLayout* layout, const void* storage, |
| 575 | const upb_fielddef* field) { |
| 576 | return ((uint8_t*)storage) + layout->fields[upb_fielddef_index(field)].offset; |
| 577 | } |
| 578 | |
| 579 | MessageLayout* create_layout(const upb_msgdef* msgdef) { |
| 580 | MessageLayout* layout = ALLOC(MessageLayout); |
| 581 | int nfields = upb_msgdef_numfields(msgdef); |
| 582 | upb_msg_field_iter it; |
| 583 | upb_msg_oneof_iter oit; |
| 584 | size_t off = 0; |
| 585 | int i = 0; |
| 586 | |
| 587 | // Reserve space for unknown fields. |
| 588 | off += sizeof(void*); |
| 589 | |
| 590 | TSRMLS_FETCH(); |
| 591 | Descriptor* desc = UNBOX_HASHTABLE_VALUE(Descriptor, get_def_obj(msgdef)); |
| 592 | layout->fields = ALLOC_N(MessageField, nfields); |
| 593 | |
| 594 | for (upb_msg_field_begin(&it, msgdef); !upb_msg_field_done(&it); |
| 595 | upb_msg_field_next(&it)) { |
| 596 | const upb_fielddef* field = upb_msg_iter_field(&it); |
| 597 | size_t field_size; |
| 598 | |
| 599 | if (upb_fielddef_containingoneof(field)) { |
| 600 | // Oneofs are handled separately below. |
| 601 | continue; |
| 602 | } |
| 603 | |
| 604 | // Allocate |field_size| bytes for this field in the layout. |
| 605 | field_size = 0; |
| 606 | if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) { |
| 607 | field_size = sizeof(zval*); |
| 608 | } else { |
| 609 | field_size = native_slot_size(upb_fielddef_type(field)); |
| 610 | } |
| 611 | |
| 612 | // Align current offset up to | size | granularity. |
| 613 | off = align_up_to(off, field_size); |
| 614 | layout->fields[upb_fielddef_index(field)].offset = off; |
| 615 | layout->fields[upb_fielddef_index(field)].case_offset = |
| 616 | MESSAGE_FIELD_NO_CASE; |
| 617 | |
| 618 | const char* fieldname = upb_fielddef_name(field); |
| 619 | |
| 620 | #if PHP_MAJOR_VERSION < 7 || (PHP_MAJOR_VERSION == 7 && PHP_MINOR_VERSION == 0) |
| 621 | zend_class_entry* old_scope = EG(scope); |
| 622 | EG(scope) = desc->klass; |
| 623 | #else |
| 624 | zend_class_entry* old_scope = EG(fake_scope); |
| 625 | EG(fake_scope) = desc->klass; |
| 626 | #endif |
| 627 | |
| 628 | #if PHP_MAJOR_VERSION < 7 |
| 629 | zval member; |
| 630 | ZVAL_STRINGL(&member, fieldname, strlen(fieldname), 0); |
| 631 | zend_property_info* property_info = |
| 632 | zend_get_property_info(desc->klass, &member, true TSRMLS_CC); |
| 633 | #else |
| 634 | zend_string* member = zend_string_init(fieldname, strlen(fieldname), 1); |
| 635 | zend_property_info* property_info = |
| 636 | zend_get_property_info(desc->klass, member, true); |
| 637 | zend_string_release(member); |
| 638 | #endif |
| 639 | |
| 640 | #if PHP_MAJOR_VERSION < 7 || (PHP_MAJOR_VERSION == 7 && PHP_MINOR_VERSION == 0) |
| 641 | EG(scope) = old_scope; |
| 642 | #else |
| 643 | EG(fake_scope) = old_scope; |
| 644 | #endif |
| 645 | |
| 646 | layout->fields[upb_fielddef_index(field)].cache_index = |
| 647 | property_info->offset; |
| 648 | off += field_size; |
| 649 | } |
| 650 | |
| 651 | // Handle oneofs now -- we iterate over oneofs specifically and allocate only |
| 652 | // one slot per oneof. |
| 653 | // |
| 654 | // We assign all value slots first, then pack the 'case' fields at the end, |
| 655 | // since in the common case (modern 64-bit platform) these are 8 bytes and 4 |
| 656 | // bytes respectively and we want to avoid alignment overhead. |
| 657 | // |
| 658 | // Note that we reserve 4 bytes (a uint32) per 'case' slot because the value |
| 659 | // space for oneof cases is conceptually as wide as field tag numbers. In |
| 660 | // practice, it's unlikely that a oneof would have more than e.g. 256 or 64K |
| 661 | // members (8 or 16 bits respectively), so conceivably we could assign |
| 662 | // consecutive case numbers and then pick a smaller oneof case slot size, but |
| 663 | // the complexity to implement this indirection is probably not worthwhile. |
| 664 | for (upb_msg_oneof_begin(&oit, msgdef); !upb_msg_oneof_done(&oit); |
| 665 | upb_msg_oneof_next(&oit)) { |
| 666 | const upb_oneofdef* oneof = upb_msg_iter_oneof(&oit); |
| 667 | upb_oneof_iter fit; |
| 668 | |
| 669 | // Always allocate NATIVE_SLOT_MAX_SIZE bytes, but share the slot between |
| 670 | // all fields. |
| 671 | size_t field_size = NATIVE_SLOT_MAX_SIZE; |
| 672 | // Align the offset . |
| 673 | off = align_up_to( off, field_size); |
| 674 | // Assign all fields in the oneof this same offset. |
| 675 | const char* oneofname = upb_oneofdef_name(oneof); |
| 676 | for (upb_oneof_begin(&fit, oneof); !upb_oneof_done(&fit); |
| 677 | upb_oneof_next(&fit)) { |
| 678 | const upb_fielddef* field = upb_oneof_iter_field(&fit); |
| 679 | layout->fields[upb_fielddef_index(field)].offset = off; |
| 680 | |
| 681 | #if PHP_MAJOR_VERSION < 7 || (PHP_MAJOR_VERSION == 7 && PHP_MINOR_VERSION == 0) |
| 682 | zend_class_entry* old_scope = EG(scope); |
| 683 | EG(scope) = desc->klass; |
| 684 | #else |
| 685 | zend_class_entry* old_scope = EG(fake_scope); |
| 686 | EG(fake_scope) = desc->klass; |
| 687 | #endif |
| 688 | |
| 689 | #if PHP_MAJOR_VERSION < 7 |
| 690 | zval member; |
| 691 | ZVAL_STRINGL(&member, oneofname, strlen(oneofname), 0); |
| 692 | zend_property_info* property_info = |
| 693 | zend_get_property_info(desc->klass, &member, true TSRMLS_CC); |
| 694 | #else |
| 695 | zend_string* member = zend_string_init(oneofname, strlen(oneofname), 1); |
| 696 | zend_property_info* property_info = |
| 697 | zend_get_property_info(desc->klass, member, true); |
| 698 | zend_string_release(member); |
| 699 | #endif |
| 700 | |
| 701 | #if PHP_MAJOR_VERSION < 7 || (PHP_MAJOR_VERSION == 7 && PHP_MINOR_VERSION == 0) |
| 702 | EG(scope) = old_scope; |
| 703 | #else |
| 704 | EG(fake_scope) = old_scope; |
| 705 | #endif |
| 706 | |
| 707 | layout->fields[upb_fielddef_index(field)].cache_index = |
| 708 | property_info->offset; |
| 709 | } |
| 710 | i++; |
| 711 | off += field_size; |
| 712 | } |
| 713 | |
| 714 | // Now the case offset. |
| 715 | for (upb_msg_oneof_begin(&oit, msgdef); !upb_msg_oneof_done(&oit); |
| 716 | upb_msg_oneof_next(&oit)) { |
| 717 | const upb_oneofdef* oneof = upb_msg_iter_oneof(&oit); |
| 718 | upb_oneof_iter fit; |
| 719 | |
| 720 | size_t field_size = sizeof(uint32_t); |
| 721 | // Align the offset . |
| 722 | off = (off + field_size - 1) & ~(field_size - 1); |
| 723 | // Assign all fields in the oneof this same offset. |
| 724 | for (upb_oneof_begin(&fit, oneof); !upb_oneof_done(&fit); |
| 725 | upb_oneof_next(&fit)) { |
| 726 | const upb_fielddef* field = upb_oneof_iter_field(&fit); |
| 727 | layout->fields[upb_fielddef_index(field)].case_offset = off; |
| 728 | } |
| 729 | off += field_size; |
| 730 | } |
| 731 | |
| 732 | layout->size = off; |
| 733 | |
| 734 | layout->msgdef = msgdef; |
| 735 | upb_msgdef_ref(layout->msgdef, &layout->msgdef); |
| 736 | |
| 737 | return layout; |
| 738 | } |
| 739 | |
| 740 | void free_layout(MessageLayout* layout) { |
| 741 | FREE(layout->fields); |
| 742 | upb_msgdef_unref(layout->msgdef, &layout->msgdef); |
| 743 | FREE(layout); |
| 744 | } |
| 745 | |
| 746 | void layout_init(MessageLayout* layout, void* storage, |
| 747 | zend_object* object PHP_PROTO_TSRMLS_DC) { |
| 748 | int i; |
| 749 | upb_msg_field_iter it; |
| 750 | |
| 751 | // Init unknown fields |
| 752 | memset(storage, 0, sizeof(void*)); |
| 753 | |
| 754 | for (upb_msg_field_begin(&it, layout->msgdef), i = 0; !upb_msg_field_done(&it); |
| 755 | upb_msg_field_next(&it), i++) { |
| 756 | const upb_fielddef* field = upb_msg_iter_field(&it); |
| 757 | void* memory = slot_memory(layout, storage, field); |
| 758 | uint32_t* oneof_case = slot_oneof_case(layout, storage, field); |
| 759 | int cache_index = slot_property_cache(layout, storage, field); |
| 760 | CACHED_VALUE* property_ptr = OBJ_PROP(object, cache_index); |
| 761 | |
| 762 | if (upb_fielddef_containingoneof(field)) { |
| 763 | memset(memory, 0, NATIVE_SLOT_MAX_SIZE); |
| 764 | *oneof_case = ONEOF_CASE_NONE; |
| 765 | } else if (is_map_field(field)) { |
| 766 | zval_ptr_dtor(property_ptr); |
| 767 | #if PHP_MAJOR_VERSION < 7 |
| 768 | MAKE_STD_ZVAL(*property_ptr); |
| 769 | #endif |
| 770 | map_field_create_with_field(map_field_type, field, |
| 771 | property_ptr PHP_PROTO_TSRMLS_CC); |
| 772 | DEREF(memory, CACHED_VALUE*) = property_ptr; |
| 773 | } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) { |
| 774 | zval_ptr_dtor(property_ptr); |
| 775 | #if PHP_MAJOR_VERSION < 7 |
| 776 | MAKE_STD_ZVAL(*property_ptr); |
| 777 | #endif |
| 778 | repeated_field_create_with_field(repeated_field_type, field, |
| 779 | property_ptr PHP_PROTO_TSRMLS_CC); |
| 780 | DEREF(memory, CACHED_VALUE*) = property_ptr; |
| 781 | } else { |
| 782 | native_slot_init(upb_fielddef_type(field), memory, property_ptr); |
| 783 | } |
| 784 | } |
| 785 | } |
| 786 | |
| 787 | // For non-singular fields, the related memory needs to point to the actual |
| 788 | // zval in properties table first. |
| 789 | static void* value_memory(const upb_fielddef* field, void* memory) { |
| 790 | switch (upb_fielddef_type(field)) { |
| 791 | case UPB_TYPE_STRING: |
| 792 | case UPB_TYPE_BYTES: |
| 793 | case UPB_TYPE_MESSAGE: |
| 794 | memory = DEREF(memory, CACHED_VALUE*); |
| 795 | break; |
| 796 | default: |
| 797 | // No operation |
| 798 | break; |
| 799 | } |
| 800 | return memory; |
| 801 | } |
| 802 | |
| 803 | zval* layout_get(MessageLayout* layout, const void* storage, |
| 804 | const upb_fielddef* field, CACHED_VALUE* cache TSRMLS_DC) { |
| 805 | void* memory = slot_memory(layout, storage, field); |
| 806 | uint32_t* oneof_case = slot_oneof_case(layout, storage, field); |
| 807 | |
| 808 | if (upb_fielddef_containingoneof(field)) { |
| 809 | if (*oneof_case != upb_fielddef_number(field)) { |
| 810 | native_slot_get_default(upb_fielddef_type(field), cache TSRMLS_CC); |
| 811 | } else { |
| 812 | native_slot_get(upb_fielddef_type(field), value_memory(field, memory), |
| 813 | cache TSRMLS_CC); |
| 814 | } |
| 815 | return CACHED_PTR_TO_ZVAL_PTR(cache); |
| 816 | } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) { |
| 817 | return CACHED_PTR_TO_ZVAL_PTR(cache); |
| 818 | } else { |
| 819 | native_slot_get(upb_fielddef_type(field), value_memory(field, memory), |
| 820 | cache TSRMLS_CC); |
| 821 | return CACHED_PTR_TO_ZVAL_PTR(cache); |
| 822 | } |
| 823 | } |
| 824 | |
| 825 | void layout_set(MessageLayout* layout, MessageHeader* header, |
| 826 | const upb_fielddef* field, zval* val TSRMLS_DC) { |
| 827 | void* storage = message_data(header); |
| 828 | void* memory = slot_memory(layout, storage, field); |
| 829 | uint32_t* oneof_case = slot_oneof_case(layout, storage, field); |
| 830 | |
| 831 | if (upb_fielddef_containingoneof(field)) { |
| 832 | upb_fieldtype_t type = upb_fielddef_type(field); |
| 833 | zend_class_entry *ce = NULL; |
| 834 | |
| 835 | // For non-singular fields, the related memory needs to point to the actual |
| 836 | // zval in properties table first. |
| 837 | switch (type) { |
| 838 | case UPB_TYPE_MESSAGE: { |
| 839 | const upb_msgdef* msg = upb_fielddef_msgsubdef(field); |
| 840 | Descriptor* desc = UNBOX_HASHTABLE_VALUE(Descriptor, get_def_obj(msg)); |
| 841 | ce = desc->klass; |
| 842 | // Intentionally fall through. |
| 843 | } |
| 844 | case UPB_TYPE_STRING: |
| 845 | case UPB_TYPE_BYTES: { |
| 846 | int property_cache_index = |
| 847 | header->descriptor->layout->fields[upb_fielddef_index(field)] |
| 848 | .cache_index; |
| 849 | DEREF(memory, CACHED_VALUE*) = |
| 850 | OBJ_PROP(&header->std, property_cache_index); |
| 851 | memory = DEREF(memory, CACHED_VALUE*); |
| 852 | break; |
| 853 | } |
| 854 | default: |
| 855 | break; |
| 856 | } |
| 857 | |
| 858 | native_slot_set(type, ce, memory, val TSRMLS_CC); |
| 859 | *oneof_case = upb_fielddef_number(field); |
| 860 | } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) { |
| 861 | // Works for both repeated and map fields |
| 862 | memory = DEREF(memory, void**); |
| 863 | zval* property_ptr = CACHED_PTR_TO_ZVAL_PTR((CACHED_VALUE*)memory); |
| 864 | |
| 865 | if (EXPECTED(property_ptr != val)) { |
| 866 | zend_class_entry *subce = NULL; |
| 867 | zval converted_value; |
| 868 | |
| 869 | if (upb_fielddef_ismap(field)) { |
| 870 | const upb_msgdef* mapmsg = upb_fielddef_msgsubdef(field); |
| 871 | const upb_fielddef* keyfield = upb_msgdef_ntof(mapmsg, "key", 3); |
| 872 | const upb_fielddef* valuefield = upb_msgdef_ntof(mapmsg, "value", 5); |
| 873 | if (upb_fielddef_descriptortype(valuefield) == |
| 874 | UPB_DESCRIPTOR_TYPE_MESSAGE) { |
| 875 | const upb_msgdef* submsg = upb_fielddef_msgsubdef(valuefield); |
| 876 | Descriptor* subdesc = |
| 877 | UNBOX_HASHTABLE_VALUE(Descriptor, get_def_obj(submsg)); |
| 878 | subce = subdesc->klass; |
| 879 | } |
| 880 | check_map_field(subce, upb_fielddef_descriptortype(keyfield), |
| 881 | upb_fielddef_descriptortype(valuefield), val, |
| 882 | &converted_value); |
| 883 | } else { |
| 884 | if (upb_fielddef_type(field) == UPB_TYPE_MESSAGE) { |
| 885 | const upb_msgdef* submsg = upb_fielddef_msgsubdef(field); |
| 886 | Descriptor* subdesc = |
| 887 | UNBOX_HASHTABLE_VALUE(Descriptor, get_def_obj(submsg)); |
| 888 | subce = subdesc->klass; |
| 889 | } |
| 890 | |
| 891 | check_repeated_field(subce, upb_fielddef_descriptortype(field), val, |
| 892 | &converted_value); |
| 893 | } |
| 894 | #if PHP_MAJOR_VERSION < 7 |
| 895 | REPLACE_ZVAL_VALUE((zval**)memory, &converted_value, 1); |
| 896 | #else |
| 897 | php_proto_zval_ptr_dtor(property_ptr); |
| 898 | ZVAL_ZVAL(property_ptr, &converted_value, 1, 0); |
| 899 | #endif |
| 900 | zval_dtor(&converted_value); |
| 901 | } |
| 902 | } else { |
| 903 | upb_fieldtype_t type = upb_fielddef_type(field); |
| 904 | zend_class_entry *ce = NULL; |
| 905 | if (type == UPB_TYPE_MESSAGE) { |
| 906 | const upb_msgdef* msg = upb_fielddef_msgsubdef(field); |
| 907 | Descriptor* desc = UNBOX_HASHTABLE_VALUE(Descriptor, get_def_obj(msg)); |
| 908 | ce = desc->klass; |
| 909 | } |
| 910 | native_slot_set(type, ce, value_memory(field, memory), val TSRMLS_CC); |
| 911 | } |
| 912 | } |
| 913 | |
| 914 | static void native_slot_merge(const upb_fielddef* field, const void* from_memory, |
| 915 | void* to_memory PHP_PROTO_TSRMLS_DC) { |
| 916 | upb_fieldtype_t type = upb_fielddef_type(field); |
| 917 | zend_class_entry* ce = NULL; |
| 918 | if (!native_slot_is_default(type, from_memory)) { |
| 919 | switch (type) { |
| 920 | #define CASE_TYPE(upb_type, c_type) \ |
| 921 | case UPB_TYPE_##upb_type: { \ |
| 922 | DEREF(to_memory, c_type) = DEREF(from_memory, c_type); \ |
| 923 | break; \ |
| 924 | } |
| 925 | CASE_TYPE(INT32, int32_t) |
| 926 | CASE_TYPE(UINT32, uint32_t) |
| 927 | CASE_TYPE(ENUM, int32_t) |
| 928 | CASE_TYPE(INT64, int64_t) |
| 929 | CASE_TYPE(UINT64, uint64_t) |
| 930 | CASE_TYPE(FLOAT, float) |
| 931 | CASE_TYPE(DOUBLE, double) |
| 932 | CASE_TYPE(BOOL, int8_t) |
| 933 | |
| 934 | #undef CASE_TYPE |
| 935 | case UPB_TYPE_STRING: |
| 936 | case UPB_TYPE_BYTES: |
| 937 | native_slot_set(type, NULL, value_memory(field, to_memory), |
| 938 | CACHED_PTR_TO_ZVAL_PTR(DEREF( |
| 939 | from_memory, CACHED_VALUE*)) PHP_PROTO_TSRMLS_CC); |
| 940 | break; |
| 941 | case UPB_TYPE_MESSAGE: { |
| 942 | const upb_msgdef* msg = upb_fielddef_msgsubdef(field); |
| 943 | Descriptor* desc = UNBOX_HASHTABLE_VALUE(Descriptor, get_def_obj(msg)); |
| 944 | ce = desc->klass; |
| 945 | if (native_slot_is_default(type, to_memory)) { |
| 946 | #if PHP_MAJOR_VERSION < 7 |
| 947 | SEPARATE_ZVAL_IF_NOT_REF((zval**)value_memory(field, to_memory)); |
| 948 | #endif |
| 949 | CREATE_OBJ_ON_ALLOCATED_ZVAL_PTR( |
| 950 | CACHED_PTR_TO_ZVAL_PTR(DEREF(to_memory, CACHED_VALUE*)), ce); |
| 951 | MessageHeader* submsg = |
| 952 | UNBOX(MessageHeader, |
| 953 | CACHED_PTR_TO_ZVAL_PTR(DEREF(to_memory, CACHED_VALUE*))); |
| 954 | custom_data_init(ce, submsg PHP_PROTO_TSRMLS_CC); |
| 955 | } |
| 956 | |
| 957 | MessageHeader* sub_from = |
| 958 | UNBOX(MessageHeader, |
| 959 | CACHED_PTR_TO_ZVAL_PTR(DEREF(from_memory, CACHED_VALUE*))); |
| 960 | MessageHeader* sub_to = |
| 961 | UNBOX(MessageHeader, |
| 962 | CACHED_PTR_TO_ZVAL_PTR(DEREF(to_memory, CACHED_VALUE*))); |
| 963 | |
| 964 | layout_merge(desc->layout, sub_from, sub_to PHP_PROTO_TSRMLS_CC); |
| 965 | break; |
| 966 | } |
| 967 | } |
| 968 | } |
| 969 | } |
| 970 | |
| 971 | static void native_slot_merge_by_array(const upb_fielddef* field, const void* from_memory, |
| 972 | void* to_memory PHP_PROTO_TSRMLS_DC) { |
| 973 | upb_fieldtype_t type = upb_fielddef_type(field); |
| 974 | switch (type) { |
| 975 | case UPB_TYPE_STRING: |
| 976 | case UPB_TYPE_BYTES: { |
| 977 | #if PHP_MAJOR_VERSION < 7 |
| 978 | MAKE_STD_ZVAL(DEREF(to_memory, zval*)); |
| 979 | PHP_PROTO_ZVAL_STRINGL(DEREF(to_memory, zval*), |
| 980 | Z_STRVAL_P(*(zval**)from_memory), |
| 981 | Z_STRLEN_P(*(zval**)from_memory), 1); |
| 982 | #else |
| 983 | DEREF(to_memory, zend_string*) = |
| 984 | zend_string_dup(*(zend_string**)from_memory, 0); |
| 985 | #endif |
| 986 | break; |
| 987 | } |
| 988 | case UPB_TYPE_MESSAGE: { |
| 989 | const upb_msgdef* msg = upb_fielddef_msgsubdef(field); |
| 990 | Descriptor* desc = UNBOX_HASHTABLE_VALUE(Descriptor, get_def_obj(msg)); |
| 991 | zend_class_entry* ce = desc->klass; |
| 992 | #if PHP_MAJOR_VERSION < 7 |
| 993 | MAKE_STD_ZVAL(DEREF(to_memory, zval*)); |
| 994 | CREATE_OBJ_ON_ALLOCATED_ZVAL_PTR(DEREF(to_memory, zval*), ce); |
| 995 | #else |
| 996 | DEREF(to_memory, zend_object*) = ce->create_object(ce TSRMLS_CC); |
| 997 | #endif |
| 998 | MessageHeader* sub_from = UNBOX_HASHTABLE_VALUE( |
| 999 | MessageHeader, DEREF(from_memory, PHP_PROTO_HASHTABLE_VALUE)); |
| 1000 | MessageHeader* sub_to = UNBOX_HASHTABLE_VALUE( |
| 1001 | MessageHeader, DEREF(to_memory, PHP_PROTO_HASHTABLE_VALUE)); |
| 1002 | custom_data_init(ce, sub_to PHP_PROTO_TSRMLS_CC); |
| 1003 | |
| 1004 | layout_merge(desc->layout, sub_from, sub_to PHP_PROTO_TSRMLS_CC); |
| 1005 | break; |
| 1006 | } |
| 1007 | default: |
| 1008 | native_slot_merge(field, from_memory, to_memory PHP_PROTO_TSRMLS_CC); |
| 1009 | break; |
| 1010 | } |
| 1011 | } |
| 1012 | |
| 1013 | void layout_merge(MessageLayout* layout, MessageHeader* from, |
| 1014 | MessageHeader* to PHP_PROTO_TSRMLS_DC) { |
| 1015 | int i, j; |
| 1016 | upb_msg_field_iter it; |
| 1017 | |
| 1018 | for (upb_msg_field_begin(&it, layout->msgdef), i = 0; !upb_msg_field_done(&it); |
| 1019 | upb_msg_field_next(&it), i++) { |
| 1020 | const upb_fielddef* field = upb_msg_iter_field(&it); |
| 1021 | |
| 1022 | void* to_memory = slot_memory(layout, message_data(to), field); |
| 1023 | void* from_memory = slot_memory(layout, message_data(from), field); |
| 1024 | |
| 1025 | if (upb_fielddef_containingoneof(field)) { |
| 1026 | uint32_t oneof_case_offset = |
| 1027 | layout->fields[upb_fielddef_index(field)].case_offset; |
| 1028 | // For a oneof, check that this field is actually present -- skip all the |
| 1029 | // below if not. |
| 1030 | if (DEREF((message_data(from) + oneof_case_offset), uint32_t) != |
| 1031 | upb_fielddef_number(field)) { |
| 1032 | continue; |
| 1033 | } |
| 1034 | uint32_t* from_oneof_case = slot_oneof_case(layout, message_data(from), field); |
| 1035 | uint32_t* to_oneof_case = slot_oneof_case(layout, message_data(to), field); |
| 1036 | |
| 1037 | // For non-singular fields, the related memory needs to point to the |
| 1038 | // actual zval in properties table first. |
| 1039 | switch (upb_fielddef_type(field)) { |
| 1040 | case UPB_TYPE_MESSAGE: |
| 1041 | case UPB_TYPE_STRING: |
| 1042 | case UPB_TYPE_BYTES: { |
| 1043 | int property_cache_index = |
| 1044 | layout->fields[upb_fielddef_index(field)].cache_index; |
| 1045 | DEREF(to_memory, CACHED_VALUE*) = |
| 1046 | OBJ_PROP(&to->std, property_cache_index); |
| 1047 | break; |
| 1048 | } |
| 1049 | default: |
| 1050 | break; |
| 1051 | } |
| 1052 | |
| 1053 | *to_oneof_case = *from_oneof_case; |
| 1054 | |
| 1055 | // Otherwise, fall through to the appropriate singular-field handler |
| 1056 | // below. |
| 1057 | } |
| 1058 | |
| 1059 | if (is_map_field(field)) { |
| 1060 | int size, key_length, value_length; |
| 1061 | MapIter map_it; |
| 1062 | |
| 1063 | zval* to_map_php = |
| 1064 | CACHED_PTR_TO_ZVAL_PTR(DEREF(to_memory, CACHED_VALUE*)); |
| 1065 | zval* from_map_php = |
| 1066 | CACHED_PTR_TO_ZVAL_PTR(DEREF(from_memory, CACHED_VALUE*)); |
| 1067 | Map* to_map = UNBOX(Map, to_map_php); |
| 1068 | Map* from_map = UNBOX(Map, from_map_php); |
| 1069 | |
| 1070 | size = upb_strtable_count(&from_map->table); |
| 1071 | if (size == 0) continue; |
| 1072 | |
| 1073 | const upb_msgdef *mapentry_def = upb_fielddef_msgsubdef(field); |
| 1074 | const upb_fielddef *value_field = upb_msgdef_itof(mapentry_def, 2); |
| 1075 | |
| 1076 | for (map_begin(from_map_php, &map_it TSRMLS_CC); !map_done(&map_it); |
| 1077 | map_next(&map_it)) { |
| 1078 | const char* key = map_iter_key(&map_it, &key_length); |
| 1079 | upb_value from_value = map_iter_value(&map_it, &value_length); |
| 1080 | upb_value to_value; |
| 1081 | void* from_mem = upb_value_memory(&from_value); |
| 1082 | void* to_mem = upb_value_memory(&to_value); |
| 1083 | memset(to_mem, 0, native_slot_size(to_map->value_type)); |
| 1084 | |
| 1085 | native_slot_merge_by_array(value_field, from_mem, |
| 1086 | to_mem PHP_PROTO_TSRMLS_CC); |
| 1087 | |
| 1088 | map_index_set(to_map, key, key_length, to_value); |
| 1089 | } |
| 1090 | |
| 1091 | } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) { |
| 1092 | zval* to_array_php = CACHED_PTR_TO_ZVAL_PTR(DEREF(to_memory, CACHED_VALUE*)); |
| 1093 | zval* from_array_php = CACHED_PTR_TO_ZVAL_PTR(DEREF(from_memory, CACHED_VALUE*)); |
| 1094 | RepeatedField* to_array = UNBOX(RepeatedField, to_array_php); |
| 1095 | RepeatedField* from_array = UNBOX(RepeatedField, from_array_php); |
| 1096 | |
| 1097 | int size = zend_hash_num_elements(PHP_PROTO_HASH_OF(from_array->array)); |
| 1098 | if (size > 0) { |
| 1099 | for (j = 0; j < size; j++) { |
| 1100 | void* from_memory = NULL; |
| 1101 | void* to_memory = |
| 1102 | ALLOC_N(char, native_slot_size(upb_fielddef_type(field))); |
| 1103 | memset(to_memory, 0, native_slot_size(upb_fielddef_type(field))); |
| 1104 | |
| 1105 | if (to_array->type == UPB_TYPE_MESSAGE) { |
| 1106 | php_proto_zend_hash_index_find_zval( |
| 1107 | PHP_PROTO_HASH_OF(from_array->array), j, (void**)&from_memory); |
| 1108 | #if PHP_MAJOR_VERSION >= 7 |
| 1109 | from_memory = &Z_OBJ_P((zval*)from_memory); |
| 1110 | #endif |
| 1111 | } else { |
| 1112 | php_proto_zend_hash_index_find_mem( |
| 1113 | PHP_PROTO_HASH_OF(from_array->array), j, (void**)&from_memory); |
| 1114 | } |
| 1115 | |
| 1116 | native_slot_merge_by_array(field, from_memory, |
| 1117 | to_memory PHP_PROTO_TSRMLS_CC); |
| 1118 | repeated_field_push_native(to_array, to_memory); |
| 1119 | FREE(to_memory); |
| 1120 | } |
| 1121 | } |
| 1122 | } else { |
| 1123 | native_slot_merge(field, from_memory, to_memory PHP_PROTO_TSRMLS_CC); |
| 1124 | } |
| 1125 | } |
| 1126 | } |
| 1127 | |
| 1128 | const char* layout_get_oneof_case(MessageLayout* layout, const void* storage, |
| 1129 | const upb_oneofdef* oneof TSRMLS_DC) { |
| 1130 | upb_oneof_iter i; |
| 1131 | const upb_fielddef* first_field; |
| 1132 | |
| 1133 | // Oneof is guaranteed to have at least one field. Get the first field. |
| 1134 | for(upb_oneof_begin(&i, oneof); !upb_oneof_done(&i); upb_oneof_next(&i)) { |
| 1135 | first_field = upb_oneof_iter_field(&i); |
| 1136 | break; |
| 1137 | } |
| 1138 | |
| 1139 | uint32_t* oneof_case = slot_oneof_case(layout, storage, first_field); |
| 1140 | if (*oneof_case == 0) { |
| 1141 | return ""; |
| 1142 | } |
| 1143 | const upb_fielddef* field = upb_oneofdef_itof(oneof, *oneof_case); |
| 1144 | return upb_fielddef_name(field); |
| 1145 | } |