|
13 | 13 | * * |
14 | 14 | * hprose formatter for pecl source file. * |
15 | 15 | * * |
16 | | - * LastModified: May 10, 2015 * |
| 16 | + * LastModified: Sep 1, 2015 * |
17 | 17 | * Author: Ma Bingyao <andot@hprose.com> * |
18 | 18 | * * |
19 | 19 | \**********************************************************/ |
@@ -64,69 +64,62 @@ static void hprose_serialize(hprose_bytes_io *stream, zval *val, zend_bool simpl |
64 | 64 | hprose_writer_destroy(&writer); |
65 | 65 | } |
66 | 66 |
|
67 | | -static zend_always_inline void hprose_fast_serialize(zval *val, zend_bool simple, zval *return_value TSRMLS_DC) { |
68 | | - hprose_bytes_io stream; |
69 | | - hprose_bytes_io_init(&stream, NULL, 0); |
| 67 | +static zend_always_inline void hprose_fast_serialize(hprose_bytes_io *stream, zval *val, zend_bool simple TSRMLS_DC) { |
70 | 68 | if (!val) { |
71 | | - _hprose_writer_write_null(&stream); return; |
| 69 | + _hprose_writer_write_null(stream); return; |
72 | 70 | } |
73 | 71 | switch (Z_TYPE_P(val)) { |
74 | 72 | case IS_NULL: |
75 | | - _hprose_writer_write_null(&stream); break; |
| 73 | + _hprose_writer_write_null(stream); break; |
76 | 74 | case IS_LONG: |
77 | | - _hprose_writer_write_long(&stream, Z_LVAL_P(val)); break; |
| 75 | + _hprose_writer_write_long(stream, Z_LVAL_P(val)); break; |
78 | 76 | case IS_DOUBLE: |
79 | | - _hprose_writer_write_double(&stream, Z_DVAL_P(val)); break; |
| 77 | + _hprose_writer_write_double(stream, Z_DVAL_P(val)); break; |
80 | 78 | #if PHP_MAJOR_VERSION < 7 |
81 | 79 | case IS_BOOL: |
82 | | - _hprose_writer_write_bool(&stream, Z_BVAL_P(val)); break; |
| 80 | + _hprose_writer_write_bool(stream, Z_BVAL_P(val)); break; |
83 | 81 | #else /* PHP_MAJOR_VERSION < 7 */ |
84 | 82 | case IS_UNDEF: |
85 | | - _hprose_writer_write_null(&stream); break; |
| 83 | + _hprose_writer_write_null(stream); break; |
86 | 84 | case IS_TRUE: |
87 | | - _hprose_writer_write_true(&stream); break; |
| 85 | + _hprose_writer_write_true(stream); break; |
88 | 86 | case IS_FALSE: |
89 | | - _hprose_writer_write_false(&stream); break; |
| 87 | + _hprose_writer_write_false(stream); break; |
90 | 88 | #endif |
91 | 89 | case IS_STRING: { |
92 | 90 | char * s = Z_STRVAL_P(val); |
93 | 91 | int32_t l = Z_STRLEN_P(val); |
94 | 92 | if (l == 0) { |
95 | | - _hprose_writer_write_empty(&stream); |
| 93 | + _hprose_writer_write_empty(stream); |
96 | 94 | } |
97 | 95 | else if (is_utf8(s, l)) { |
98 | 96 | if (l < 4 && ustrlen(s, l) == 1) { |
99 | | - _hprose_writer_write_utf8char(&stream, val); |
| 97 | + _hprose_writer_write_utf8char(stream, val); |
100 | 98 | } |
101 | 99 | else { |
102 | | - _hprose_writer_write_string_with_ref(NULL, &stream, val); |
| 100 | + _hprose_writer_write_string_with_ref(NULL, stream, val); |
103 | 101 | } |
104 | 102 | } |
105 | 103 | else { |
106 | | - _hprose_writer_write_bytes_with_ref(NULL, &stream, val); |
| 104 | + _hprose_writer_write_bytes_with_ref(NULL, stream, val); |
107 | 105 | } |
108 | 106 | break; |
109 | 107 | } |
110 | 108 | case IS_OBJECT: { |
111 | 109 | zend_class_entry *ce = Z_OBJCE_P(val); |
112 | 110 | if (instanceof_function(ce, get_hprose_bytes_io_ce() TSRMLS_CC)) { |
113 | | - _hprose_writer_write_bytes_io_with_ref(NULL, &stream, val TSRMLS_CC); |
| 111 | + _hprose_writer_write_bytes_io_with_ref(NULL, stream, val TSRMLS_CC); |
114 | 112 | } |
115 | 113 | else if (instanceof_function(ce, php_date_get_date_ce() TSRMLS_CC)) { |
116 | | - _hprose_writer_write_datetime_with_ref(NULL, &stream, val TSRMLS_CC); |
| 114 | + _hprose_writer_write_datetime_with_ref(NULL, stream, val TSRMLS_CC); |
117 | 115 | } |
118 | 116 | else { |
119 | | - hprose_serialize(&stream, val, simple TSRMLS_CC); break; |
| 117 | + hprose_serialize(stream, val, simple TSRMLS_CC); break; |
120 | 118 | } |
121 | 119 | } |
122 | 120 | default: |
123 | | - hprose_serialize(&stream, val, simple TSRMLS_CC); break; |
| 121 | + hprose_serialize(stream, val, simple TSRMLS_CC); break; |
124 | 122 | } |
125 | | -#if PHP_MAJOR_VERSION < 7 |
126 | | - RETVAL_STRINGL_0(HB_BUF(stream), HB_LEN(stream)); |
127 | | -#else |
128 | | - RETVAL_STR(HB_STR(stream)); |
129 | | -#endif |
130 | 123 | } |
131 | 124 |
|
132 | 125 | static void hprose_reader_fast_unserialize(hprose_reader *_this, char tag, zval *return_value TSRMLS_DC) { |
@@ -235,15 +228,108 @@ static zend_always_inline void hprose_fast_unserialize(hprose_bytes_io *stream, |
235 | 228 | } |
236 | 229 | } |
237 | 230 |
|
| 231 | +#if HAVE_PHP_SESSION |
| 232 | +PS_SERIALIZER_ENCODE_FUNC(hprose) { |
| 233 | + hprose_bytes_io stream; |
| 234 | + hprose_bytes_io_init(&stream, NULL, 0); |
| 235 | +#if PHP_MAJOR_VERSION < 7 |
| 236 | + hprose_fast_serialize(&stream, PS(http_session_vars), 0 TSRMLS_CC); |
| 237 | + if (newlen) { |
| 238 | + *newlen = HB_LEN(stream); |
| 239 | + } |
| 240 | + *newstr = HB_BUF(stream); |
| 241 | + return SUCCESS; |
| 242 | +#else |
| 243 | + hprose_fast_serialize(&stream, &PS(http_session_vars), 0 TSRMLS_CC); |
| 244 | + return stream.s; |
| 245 | +#endif |
| 246 | +} |
| 247 | + |
| 248 | +PS_SERIALIZER_DECODE_FUNC(hprose) { |
| 249 | +#if PHP_MAJOR_VERSION < 7 |
| 250 | + int ret; |
| 251 | + HashPosition tmp_hash_pos; |
| 252 | + char *key_str; |
| 253 | + ulong key_long; |
| 254 | + uint key_len; |
| 255 | + zval *tmp; |
| 256 | + zval **value; |
| 257 | +#else |
| 258 | + zend_string *str, *key_str; |
| 259 | + zval tmp, *value; |
| 260 | +#endif |
| 261 | + HashTable *tmp_hash; |
| 262 | + hprose_bytes_io stream; |
| 263 | + if (vallen > 0) { |
| 264 | +#if PHP_MAJOR_VERSION < 7 |
| 265 | + hprose_bytes_io_init_readonly(&stream, val, vallen); |
| 266 | + MAKE_STD_ZVAL(tmp); |
| 267 | + hprose_fast_unserialize(&stream, 0, tmp TSRMLS_CC); |
| 268 | + tmp_hash = HASH_OF(tmp); |
| 269 | + if (tmp_hash) { |
| 270 | + zend_hash_internal_pointer_reset_ex(tmp_hash, &tmp_hash_pos); |
| 271 | + while (zend_hash_get_current_data_ex( |
| 272 | + tmp_hash, (void *)&value, &tmp_hash_pos) == SUCCESS) { |
| 273 | + ret = zend_hash_get_current_key_ex( |
| 274 | + tmp_hash, &key_str, &key_len, &key_long, 0, &tmp_hash_pos); |
| 275 | + switch (ret) { |
| 276 | + case HASH_KEY_IS_LONG: |
| 277 | + /* ??? */ |
| 278 | + break; |
| 279 | + case HASH_KEY_IS_STRING: |
| 280 | + php_set_session_var( |
| 281 | + key_str, key_len - 1, *value, NULL TSRMLS_CC); |
| 282 | + php_add_session_var(key_str, key_len - 1 TSRMLS_CC); |
| 283 | + break; |
| 284 | + } |
| 285 | + zend_hash_move_forward_ex(tmp_hash, &tmp_hash_pos); |
| 286 | + } |
| 287 | + } |
| 288 | +#else |
| 289 | + str = zend_string_init(val, vallen, 0); |
| 290 | + hprose_bytes_io_init_readonly(&stream, str); |
| 291 | + hprose_fast_unserialize(&stream, 0, &tmp TSRMLS_CC); |
| 292 | + tmp_hash = HASH_OF(&tmp); |
| 293 | + if (tmp_hash) { |
| 294 | + ZEND_HASH_FOREACH_STR_KEY_VAL(tmp_hash, key_str, value) { |
| 295 | + if (key_str) { |
| 296 | + php_set_session_var(key_str, value, NULL); |
| 297 | + php_add_session_var(key_str); |
| 298 | + ZVAL_UNDEF(value); |
| 299 | + } |
| 300 | + } ZEND_HASH_FOREACH_END(); |
| 301 | + } |
| 302 | + zend_string_release(str); |
| 303 | +#endif |
| 304 | + zval_ptr_dtor(&tmp); |
| 305 | + } |
| 306 | + if (EG(exception)) { |
| 307 | +#if PHP_MAJOR_VERSION < 7 |
| 308 | + zend_clear_exception(TSRMLS_C); |
| 309 | +#else |
| 310 | + zend_clear_exception(); |
| 311 | +#endif |
| 312 | + } |
| 313 | + return SUCCESS; |
| 314 | +} |
| 315 | +#endif |
| 316 | + |
238 | 317 | /* {{{ proto string hprose_serialize(mixed $val, bool $simple = false) |
239 | 318 | serialize php value to hprose format data */ |
240 | 319 | ZEND_FUNCTION(hprose_serialize) { |
241 | 320 | zval *val; |
242 | 321 | zend_bool simple = 0; |
| 322 | + hprose_bytes_io stream; |
243 | 323 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z!|b", &val, &simple) == FAILURE) { |
244 | 324 | return; |
245 | 325 | } |
246 | | - hprose_fast_serialize(val, simple, return_value TSRMLS_CC); |
| 326 | + hprose_bytes_io_init(&stream, NULL, 0); |
| 327 | + hprose_fast_serialize(&stream, val, simple TSRMLS_CC); |
| 328 | +#if PHP_MAJOR_VERSION < 7 |
| 329 | + RETVAL_STRINGL_0(HB_BUF(stream), HB_LEN(stream)); |
| 330 | +#else |
| 331 | + RETVAL_STR(HB_STR(stream)); |
| 332 | +#endif |
247 | 333 | } |
248 | 334 | /* }}} */ |
249 | 335 |
|
|
0 commit comments