-
Notifications
You must be signed in to change notification settings - Fork 20
/
hprose_reader.h
731 lines (671 loc) · 27 KB
/
hprose_reader.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
/**********************************************************\
| |
| hprose |
| |
| Official WebSite: http://www.hprose.com/ |
| http://www.hprose.org/ |
| |
\**********************************************************/
/**********************************************************\
* *
* hprose_reader.h *
* *
* hprose reader for pecl header file. *
* *
* LastModified: Mar 19, 2017 *
* Author: Ma Bingyao <andot@hprose.com> *
* *
\**********************************************************/
#ifndef HPROSE_READER_H
#define HPROSE_READER_H
#include "hprose_common.h"
#include "hprose_tags.h"
#include "hprose_bytes_io.h"
#include "hprose_class_manager.h"
#include "hprose_raw_reader.h"
#if PHP_VERSION_ID >= 50311
#include "ext/date/php_date.h"
#endif
/* Workaround for old gcc. */
#ifndef NAN
#define NAN (HUGE_VAL/HUGE_VAL)
#endif
#ifndef INFINITY
#define INFINITY HUGE_VAL
#endif
#ifndef Z_PHPDATE_P
#define Z_PHPDATE_P(zv) zend_object_store_get_object(zv TSRMLS_CC)
#endif
BEGIN_EXTERN_C()
zend_class_entry *get_hprose_reader_ce();
HPROSE_STARTUP_FUNCTION(reader);
typedef struct {
hprose_bytes_io *stream;
zval *classref;
zval *propsref;
zval *refer;
} hprose_reader;
static zend_always_inline void hprose_reader_refer_set(zval *refer, zval *val) {
if (refer) {
#ifdef Z_TRY_ADDREF_P
Z_TRY_ADDREF_P(val);
#else
Z_ADDREF_P(val);
#endif
add_next_index_zval(refer, val);
}
}
PHP_HPROSE_API void hprose_reader_unserialize(hprose_reader *_this, zval *return_value TSRMLS_DC);
static zend_always_inline void hprose_reader_init(hprose_reader *_this, hprose_bytes_io *stream, zend_bool simple) {
_this->stream = stream;
hprose_zval_new(_this->classref);
hprose_zval_new(_this->propsref);
array_init(_this->classref);
array_init(_this->propsref);
if (simple) {
_this->refer = NULL;
}
else {
hprose_zval_new(_this->refer);
array_init(_this->refer);
}
}
static zend_always_inline hprose_reader * hprose_reader_create(hprose_bytes_io *stream, zend_bool simple) {
hprose_reader *_this = emalloc(sizeof(hprose_reader));
hprose_reader_init(_this, stream, simple);
return _this;
}
static zend_always_inline void hprose_reader_destroy(hprose_reader *_this) {
_this->stream = NULL;
hprose_zval_free(_this->classref);
hprose_zval_free(_this->propsref);
_this->classref = NULL;
_this->propsref = NULL;
if (_this->refer) {
hprose_zval_free(_this->refer);
_this->refer = NULL;
}
}
static zend_always_inline void hprose_reader_free(hprose_reader *_this) {
hprose_reader_destroy(_this);
efree(_this);
}
static zend_always_inline void hprose_reader_reset(hprose_reader *_this) {
zend_hash_clean(Z_ARRVAL_P(_this->classref));
zend_hash_clean(Z_ARRVAL_P(_this->propsref));
if (_this->refer) {
zend_hash_clean(Z_ARRVAL_P(_this->refer));
}
}
static zend_always_inline long _hprose_reader_read_integer_without_tag(hprose_bytes_io *stream) {
return hprose_bytes_io_read_int(stream, HPROSE_TAG_SEMICOLON);
}
#define hprose_reader_read_integer_without_tag(_this) _hprose_reader_read_integer_without_tag((_this)->stream)
static zend_always_inline long hprose_reader_read_integer(hprose_reader *_this TSRMLS_DC) {
char expected_tags[] = {'0', '1', '2', '3', '4',
'5', '6', '7', '8', '9',
HPROSE_TAG_INTEGER, '\0'};
char tag = hprose_bytes_io_getc(_this->stream);
switch (tag) {
case '0': return 0;
case '1': return 1;
case '2': return 2;
case '3': return 3;
case '4': return 4;
case '5': return 5;
case '6': return 6;
case '7': return 7;
case '8': return 8;
case '9': return 9;
case HPROSE_TAG_INTEGER:
return hprose_reader_read_integer_without_tag(_this);
default: unexpected_tag(tag, expected_tags TSRMLS_CC); return 0;
}
}
#if PHP_MAJOR_VERSION < 7
static zend_always_inline char * _hprose_reader_read_long_without_tag(hprose_bytes_io *stream, int32_t *len_ptr) {
return hprose_bytes_io_readuntil(stream, HPROSE_TAG_SEMICOLON, len_ptr);
}
#define hprose_reader_read_long_without_tag(_this, len_ptr) _hprose_reader_read_long_without_tag((_this)->stream, len_ptr)
static zend_always_inline char * hprose_reader_read_long(hprose_reader *_this, int32_t *len_ptr TSRMLS_DC) {
char expected_tags[] = {'0', '1', '2', '3', '4',
'5', '6', '7', '8', '9',
HPROSE_TAG_INTEGER,
HPROSE_TAG_LONG, '\0'};
char tag = hprose_bytes_io_getc(_this->stream);
switch (tag) {
case '0': *len_ptr = 1; return estrndup("0", 1);
case '1': *len_ptr = 1; return estrndup("1", 1);
case '2': *len_ptr = 1; return estrndup("2", 1);
case '3': *len_ptr = 1; return estrndup("3", 1);
case '4': *len_ptr = 1; return estrndup("4", 1);
case '5': *len_ptr = 1; return estrndup("5", 1);
case '6': *len_ptr = 1; return estrndup("6", 1);
case '7': *len_ptr = 1; return estrndup("7", 1);
case '8': *len_ptr = 1; return estrndup("8", 1);
case '9': *len_ptr = 1; return estrndup("9", 1);
case HPROSE_TAG_INTEGER:
case HPROSE_TAG_LONG:
return hprose_reader_read_long_without_tag(_this, len_ptr);
default: unexpected_tag(tag, expected_tags TSRMLS_CC); return 0;
}
}
#else
static zend_always_inline zend_string *_hprose_reader_read_long_without_tag(hprose_bytes_io *stream) {
return hprose_bytes_io_readuntil(stream, HPROSE_TAG_SEMICOLON);
}
#define hprose_reader_read_long_without_tag(_this) _hprose_reader_read_long_without_tag((_this)->stream)
static zend_always_inline zend_string *hprose_reader_read_long(hprose_reader *_this TSRMLS_DC) {
char expected_tags[] = {'0', '1', '2', '3', '4',
'5', '6', '7', '8', '9',
HPROSE_TAG_INTEGER,
HPROSE_TAG_LONG, '\0'};
char tag = hprose_bytes_io_getc(_this->stream);
switch (tag) {
case '0': return zend_string_init("0", 1, 0);
case '1': return zend_string_init("1", 1, 0);
case '2': return zend_string_init("2", 1, 0);
case '3': return zend_string_init("3", 1, 0);
case '4': return zend_string_init("4", 1, 0);
case '5': return zend_string_init("5", 1, 0);
case '6': return zend_string_init("6", 1, 0);
case '7': return zend_string_init("7", 1, 0);
case '8': return zend_string_init("8", 1, 0);
case '9': return zend_string_init("9", 1, 0);
case HPROSE_TAG_INTEGER:
case HPROSE_TAG_LONG:
return hprose_reader_read_long_without_tag(_this);
default: unexpected_tag(tag, expected_tags TSRMLS_CC); return 0;
}
}
#endif
static zend_always_inline double _hprose_reader_read_double_without_tag(hprose_bytes_io *stream) {
#if PHP_MAJOR_VERSION < 7
int32_t l;
char *s = hprose_bytes_io_readuntil(stream, HPROSE_TAG_SEMICOLON, &l);
double d = atof(s);
efree(s);
#else
zend_string *s = hprose_bytes_io_readuntil(stream, HPROSE_TAG_SEMICOLON);
double d = atof(s->val);
zend_string_release(s);
#endif
return d;
}
#define hprose_reader_read_double_without_tag(_this) _hprose_reader_read_double_without_tag((_this)->stream)
static zend_always_inline double _hprose_reader_read_infinity_without_tag(hprose_bytes_io *stream) {
return (hprose_bytes_io_getc(stream) == HPROSE_TAG_NEG) ? -INFINITY : INFINITY;
}
#define hprose_reader_read_infinity_without_tag(_this) _hprose_reader_read_infinity_without_tag((_this)->stream)
static zend_always_inline double hprose_reader_read_double(hprose_reader *_this TSRMLS_DC) {
char expected_tags[] = {'0', '1', '2', '3', '4',
'5', '6', '7', '8', '9',
HPROSE_TAG_INTEGER,
HPROSE_TAG_LONG,
HPROSE_TAG_DOUBLE,
HPROSE_TAG_NAN,
HPROSE_TAG_INFINITY, '\0'};
char tag = hprose_bytes_io_getc(_this->stream);
switch (tag) {
case '0': return 0;
case '1': return 1;
case '2': return 2;
case '3': return 3;
case '4': return 4;
case '5': return 5;
case '6': return 6;
case '7': return 7;
case '8': return 8;
case '9': return 9;
case HPROSE_TAG_INTEGER:
return hprose_reader_read_integer_without_tag(_this);
case HPROSE_TAG_LONG:
case HPROSE_TAG_DOUBLE:
return hprose_reader_read_double_without_tag(_this);
case HPROSE_TAG_NAN:
return NAN;
case HPROSE_TAG_INFINITY:
return hprose_reader_read_infinity_without_tag(_this);
default: unexpected_tag(tag, expected_tags TSRMLS_CC); return 0;
}
}
static zend_always_inline zend_bool hprose_reader_read_boolean(hprose_reader *_this TSRMLS_DC) {
char expected_tags[] = {HPROSE_TAG_TRUE,
HPROSE_TAG_FALSE, '\0'};
char tag = hprose_bytes_io_getc(_this->stream);
switch (tag) {
case HPROSE_TAG_TRUE: return 1;
case HPROSE_TAG_FALSE: return 0;
default: unexpected_tag(tag, expected_tags TSRMLS_CC); return 0;
}
}
static zend_always_inline void hprose_reader_read_ref(hprose_reader *_this, zval *return_value TSRMLS_DC) {
if (_this->refer) {
uint32_t index = hprose_bytes_io_read_int(_this->stream, HPROSE_TAG_SEMICOLON);
zval *result = php_array_get(_this->refer, index);
RETURN_ZVAL(result, 1, 0);
return;
}
zend_throw_exception_ex(NULL, 0 TSRMLS_CC,
"Unexpected serialize tag '%c' in stream", HPROSE_TAG_REF);
}
static zend_always_inline void _hprose_reader_read_datetime_without_tag(hprose_bytes_io *stream, zval *return_value TSRMLS_DC) {
char tag;
hprose_bytes_io *tmp = hprose_bytes_io_new();
hprose_bytes_io_read_to(stream, tmp, 4);
hprose_bytes_io_putc(tmp, '-');
hprose_bytes_io_read_to(stream, tmp, 2);
hprose_bytes_io_putc(tmp, '-');
hprose_bytes_io_read_to(stream, tmp, 2);
tag = hprose_bytes_io_getc(stream);
if (tag == HPROSE_TAG_TIME) {
hprose_bytes_io_putc(tmp, tag);
hprose_bytes_io_read_to(stream, tmp, 2);
hprose_bytes_io_putc(tmp, ':');
hprose_bytes_io_read_to(stream, tmp, 2);
hprose_bytes_io_putc(tmp, ':');
hprose_bytes_io_read_to(stream, tmp, 2);
tag = hprose_bytes_io_getc(stream);
if (tag == HPROSE_TAG_POINT) {
hprose_bytes_io_putc(tmp, tag);
hprose_bytes_io_read_to(stream, tmp, 3);
tag = hprose_bytes_io_getc(stream);
if ((tag >= '0') && (tag <= '9')) {
hprose_bytes_io_putc(tmp, tag);
hprose_bytes_io_read_to(stream, tmp, 2);
tag = hprose_bytes_io_getc(stream);
if ((tag >= '0') && (tag <= '9')) {
hprose_bytes_io_skip(stream, 2);
tag = hprose_bytes_io_getc(stream);
}
}
}
}
#if PHP_VERSION_ID < 50311
if (tag == HPROSE_TAG_UTC) {
zval timezone;
function_invoke(timezone_open, &timezone, "s", ZEND_STRL("UTC"));
function_invoke(date_create, return_value, "sz", HB_BUF_P(tmp), HB_LEN_P(tmp), &timezone);
}
else {
function_invoke(date_create, return_value, "s", HB_BUF_P(tmp), HB_LEN_P(tmp));
}
#else
if (tag == HPROSE_TAG_UTC) {
zval timezone;
function_invoke(timezone_open, &timezone, "s", ZEND_STRL("UTC"));
php_date_instantiate(php_date_get_date_ce(), return_value TSRMLS_CC);
php_date_initialize(Z_PHPDATE_P(return_value), HB_BUF_P(tmp), HB_LEN_P(tmp), NULL, &timezone, 0 TSRMLS_CC);
#if PHP_MAJOR_VERSION < 7
zval_dtor(&timezone);
#else
zval_ptr_dtor(&timezone);
#endif
}
else {
php_date_instantiate(php_date_get_date_ce(), return_value TSRMLS_CC);
php_date_initialize(Z_PHPDATE_P(return_value), HB_BUF_P(tmp), HB_LEN_P(tmp), NULL, NULL, 0 TSRMLS_CC);
}
#endif
hprose_bytes_io_free(tmp);
}
static zend_always_inline void hprose_reader_read_datetime_without_tag(hprose_reader *_this, zval *return_value TSRMLS_DC) {
_hprose_reader_read_datetime_without_tag(_this->stream, return_value TSRMLS_CC);
hprose_reader_refer_set(_this->refer, return_value);
}
static zend_always_inline void _hprose_reader_read_time_without_tag(hprose_bytes_io *stream, zval *return_value TSRMLS_DC) {
char tag;
hprose_bytes_io *tmp = hprose_bytes_io_new();
hprose_bytes_io_write(tmp, "1970-01-01", 10);
hprose_bytes_io_putc(tmp, HPROSE_TAG_TIME);
hprose_bytes_io_read_to(stream, tmp, 2);
hprose_bytes_io_putc(tmp, ':');
hprose_bytes_io_read_to(stream, tmp, 2);
hprose_bytes_io_putc(tmp, ':');
hprose_bytes_io_read_to(stream, tmp, 2);
tag = hprose_bytes_io_getc(stream);
if (tag == HPROSE_TAG_POINT) {
hprose_bytes_io_putc(tmp, tag);
hprose_bytes_io_read_to(stream, tmp, 3);
tag = hprose_bytes_io_getc(stream);
if ((tag >= '0') && (tag <= '9')) {
hprose_bytes_io_putc(tmp, tag);
hprose_bytes_io_read_to(stream, tmp, 2);
tag = hprose_bytes_io_getc(stream);
if ((tag >= '0') && (tag <= '9')) {
hprose_bytes_io_skip(stream, 2);
tag = hprose_bytes_io_getc(stream);
}
}
}
#if PHP_VERSION_ID < 50311
if (tag == HPROSE_TAG_UTC) {
zval timezone;
function_invoke(timezone_open, &timezone, "s", ZEND_STRL("UTC"));
function_invoke(date_create, return_value, "sz", HB_BUF_P(tmp), HB_LEN_P(tmp), &timezone);
}
else {
function_invoke(date_create, return_value, "s", HB_BUF_P(tmp), HB_LEN_P(tmp));
}
#else
if (tag == HPROSE_TAG_UTC) {
zval timezone;
function_invoke(timezone_open, &timezone, "s", ZEND_STRL("UTC"));
php_date_instantiate(php_date_get_date_ce(), return_value TSRMLS_CC);
php_date_initialize(Z_PHPDATE_P(return_value), HB_BUF_P(tmp), HB_LEN_P(tmp), NULL, &timezone, 0 TSRMLS_CC);
#if PHP_MAJOR_VERSION < 7
zval_dtor(&timezone);
#else
zval_ptr_dtor(&timezone);
#endif
}
else {
php_date_instantiate(php_date_get_date_ce(), return_value TSRMLS_CC);
php_date_initialize(Z_PHPDATE_P(return_value), HB_BUF_P(tmp), HB_LEN_P(tmp), NULL, NULL, 0 TSRMLS_CC);
}
#endif
hprose_bytes_io_free(tmp);
}
static zend_always_inline void hprose_reader_read_time_without_tag(hprose_reader *_this, zval *return_value TSRMLS_DC) {
_hprose_reader_read_time_without_tag(_this->stream, return_value TSRMLS_CC);
hprose_reader_refer_set(_this->refer, return_value);
}
static zend_always_inline void _hprose_reader_read_bytes_without_tag(hprose_bytes_io *stream, zval *return_value) {
int32_t count = hprose_bytes_io_read_int(stream, HPROSE_TAG_QUOTE);
#if PHP_MAJOR_VERSION < 7
RETVAL_STRINGL_0(hprose_bytes_io_read(stream, count), count);
#else
RETVAL_STR(hprose_bytes_io_read(stream, count));
#endif
hprose_bytes_io_skip(stream, 1);
}
static zend_always_inline void hprose_reader_read_bytes_without_tag(hprose_reader *_this, zval *return_value) {
_hprose_reader_read_bytes_without_tag(_this->stream, return_value);
hprose_reader_refer_set(_this->refer, return_value);
}
static zend_always_inline void _hprose_reader_read_utf8char_without_tag(hprose_bytes_io *stream, zval *return_value TSRMLS_DC) {
#if PHP_MAJOR_VERSION < 7
int32_t len;
char *uc = hprose_bytes_io_read_string(stream, 1, &len);
RETVAL_STRINGL_0(uc, len);
#else
RETVAL_STR(hprose_bytes_io_read_string(stream, 1));
#endif
}
#define hprose_reader_read_utf8char_without_tag(_this, return_value) _hprose_reader_read_utf8char_without_tag((_this)->stream, return_value TSRMLS_CC)
static zend_always_inline void _hprose_reader_read_string_without_tag(hprose_bytes_io *stream, zval *return_value TSRMLS_DC) {
int32_t count = hprose_bytes_io_read_int(stream, HPROSE_TAG_QUOTE);
#if PHP_MAJOR_VERSION < 7
int32_t len;
char *str = hprose_bytes_io_read_string(stream, count, &len);
RETVAL_STRINGL_0(str, len);
#else
RETVAL_STR(hprose_bytes_io_read_string(stream, count));
#endif
hprose_bytes_io_skip(stream, 1);
}
static zend_always_inline void hprose_reader_read_string_without_tag(hprose_reader *_this, zval *return_value TSRMLS_DC) {
_hprose_reader_read_string_without_tag(_this->stream, return_value TSRMLS_CC);
hprose_reader_refer_set(_this->refer, return_value);
}
static zend_always_inline void _hprose_reader_read_string(hprose_reader *_this, zval *return_value TSRMLS_DC) {
char expected_tags[] = {HPROSE_TAG_UTF8CHAR,
HPROSE_TAG_STRING,
HPROSE_TAG_REF, '\0'};
char tag = hprose_bytes_io_getc(_this->stream);
switch (tag) {
case HPROSE_TAG_UTF8CHAR: {
hprose_reader_read_utf8char_without_tag(_this, return_value);
return;
}
case HPROSE_TAG_STRING: {
hprose_reader_read_string_without_tag(_this, return_value TSRMLS_CC);
return;
}
case HPROSE_TAG_REF: {
hprose_reader_read_ref(_this, return_value TSRMLS_CC);
convert_to_string(return_value);
return;
}
default: unexpected_tag(tag, expected_tags TSRMLS_CC);
}
}
static zend_always_inline void hprose_reader_read_string(hprose_reader *_this, zval *return_value TSRMLS_DC) {
char expected_tags[] = {HPROSE_TAG_NULL,
HPROSE_TAG_EMPTY,
HPROSE_TAG_UTF8CHAR,
HPROSE_TAG_STRING,
HPROSE_TAG_REF, '\0'};
char tag = hprose_bytes_io_getc(_this->stream);
switch (tag) {
case HPROSE_TAG_NULL: RETURN_NULL();
case HPROSE_TAG_EMPTY: RETURN_EMPTY_STRING();
case HPROSE_TAG_UTF8CHAR: {
hprose_reader_read_utf8char_without_tag(_this, return_value);
return;
}
case HPROSE_TAG_STRING: {
hprose_reader_read_string_without_tag(_this, return_value TSRMLS_CC);
return;
}
case HPROSE_TAG_REF: {
hprose_reader_read_ref(_this, return_value TSRMLS_CC);
convert_to_string(return_value);
return;
}
default: unexpected_tag(tag, expected_tags TSRMLS_CC);
}
}
static zend_always_inline void _hprose_reader_read_guid_without_tag(hprose_bytes_io *stream, zval *return_value) {
hprose_bytes_io_skip(stream, 1);
#if PHP_MAJOR_VERSION < 7
RETVAL_STRINGL_0(hprose_bytes_io_read(stream, 36), 36);
#else
RETVAL_STR(hprose_bytes_io_read(stream, 36));
#endif
hprose_bytes_io_skip(stream, 1);
}
static zend_always_inline void hprose_reader_read_guid_without_tag(hprose_reader *_this, zval *return_value) {
_hprose_reader_read_guid_without_tag(_this->stream, return_value);
hprose_reader_refer_set(_this->refer, return_value);
}
static zend_always_inline void hprose_reader_read_list_without_tag(hprose_reader *_this, zval *return_value TSRMLS_DC) {
int32_t i = hprose_bytes_io_read_int(_this->stream, HPROSE_TAG_OPENBRACE);
array_init_size(return_value, i);
hprose_reader_refer_set(_this->refer, return_value);
for (; i > 0; --i) {
#if PHP_MAJOR_VERSION < 7
zval *val;
MAKE_STD_ZVAL(val);
hprose_reader_unserialize(_this, val TSRMLS_CC);
add_next_index_zval(return_value, val);
#else
zval val;
hprose_reader_unserialize(_this, &val TSRMLS_CC);
add_next_index_zval(return_value, &val);
#endif
}
hprose_bytes_io_skip(_this->stream, 1);
}
static zend_always_inline void hprose_reader_read_list(hprose_reader *_this, zval *return_value TSRMLS_DC) {
char expected_tags[] = {HPROSE_TAG_NULL,
HPROSE_TAG_LIST,
HPROSE_TAG_REF, '\0'};
char tag = hprose_bytes_io_getc(_this->stream);
switch (tag) {
case HPROSE_TAG_NULL: RETURN_NULL();
case HPROSE_TAG_LIST: {
hprose_reader_read_list_without_tag(_this, return_value TSRMLS_CC);
return;
}
case HPROSE_TAG_REF: {
hprose_reader_read_ref(_this, return_value TSRMLS_CC);
return;
}
default: unexpected_tag(tag, expected_tags TSRMLS_CC);
}
}
static zend_always_inline void hprose_reader_read_map_without_tag(hprose_reader *_this, zval *return_value TSRMLS_DC) {
int32_t i = hprose_bytes_io_read_int(_this->stream, HPROSE_TAG_OPENBRACE);
array_init_size(return_value, i);
hprose_reader_refer_set(_this->refer, return_value);
for (; i > 0; --i) {
#if PHP_MAJOR_VERSION < 7
zval *key, *value;
MAKE_STD_ZVAL(key);
MAKE_STD_ZVAL(value);
hprose_reader_unserialize(_this, key TSRMLS_CC);
hprose_reader_unserialize(_this, value TSRMLS_CC);
if (Z_TYPE_P(key) == IS_LONG) {
add_index_zval(return_value, Z_LVAL_P(key), value);
}
else {
convert_to_string(key);
add_assoc_zval_ex(return_value, Z_STRVAL_P(key), Z_STRLEN_P(key) + 1, value);
}
zval_ptr_dtor(&key);
#else
zval key, value;
hprose_reader_unserialize(_this, &key TSRMLS_CC);
hprose_reader_unserialize(_this, &value TSRMLS_CC);
array_set_zval_key(Z_ARRVAL_P(return_value), &key, &value);
zval_ptr_dtor(&key);
zval_ptr_dtor(&value);
#endif
}
hprose_bytes_io_skip(_this->stream, 1);
}
static zend_always_inline void hprose_reader_read_map(hprose_reader *_this, zval *return_value TSRMLS_DC) {
char expected_tags[] = {HPROSE_TAG_NULL,
HPROSE_TAG_MAP,
HPROSE_TAG_REF, '\0'};
char tag = hprose_bytes_io_getc(_this->stream);
switch (tag) {
case HPROSE_TAG_NULL: RETURN_NULL();
case HPROSE_TAG_MAP: {
hprose_reader_read_map_without_tag(_this, return_value TSRMLS_CC);
return;
}
case HPROSE_TAG_REF: {
hprose_reader_read_ref(_this, return_value TSRMLS_CC);
return;
}
default: unexpected_tag(tag, expected_tags TSRMLS_CC);
}
}
static zend_always_inline void hprose_reader_read_class(hprose_reader *_this TSRMLS_DC) {
int32_t i = hprose_bytes_io_read_int(_this->stream, HPROSE_TAG_QUOTE);
#if PHP_MAJOR_VERSION < 7
int32_t alen, nlen;
char *alias = hprose_bytes_io_read_string(_this->stream, i, &alen);
char *name = hprose_class_manager_get_class(alias, alen, &nlen);
efree(alias);
#else
zend_string *alias = hprose_bytes_io_read_string(_this->stream, i);
zend_string *name = hprose_class_manager_get_class(alias->val, alias->len);
zend_string_release(alias);
#endif
hprose_bytes_io_skip(_this->stream, 1);
i = hprose_bytes_io_read_int(_this->stream, HPROSE_TAG_OPENBRACE);
do {
#if PHP_MAJOR_VERSION < 7
zval *props;
MAKE_STD_ZVAL(props);
array_init_size(props, i);
for (; i > 0; --i) {
zval *prop;
MAKE_STD_ZVAL(prop);
_hprose_reader_read_string(_this, prop TSRMLS_CC);
add_next_index_zval(props, prop);
}
add_next_index_stringl(_this->classref, name, nlen, 0);
add_next_index_zval(_this->propsref, props);
#else
zval props;
array_init_size(&props, i);
for (; i > 0; --i) {
zval prop;
_hprose_reader_read_string(_this, &prop TSRMLS_CC);
add_next_index_zval(&props, &prop);
}
add_next_index_str(_this->classref, name);
add_next_index_zval(_this->propsref, &props);
#endif
} while(0);
hprose_bytes_io_skip(_this->stream, 1);
}
static zend_always_inline void hprose_reader_read_object_without_tag(hprose_reader *_this, zval *return_value TSRMLS_DC) {
int32_t index = hprose_bytes_io_read_int(_this->stream, HPROSE_TAG_OPENBRACE);
zval *class_name = php_array_get(_this->classref, index);
zval *props = php_array_get(_this->propsref, index);
HashTable *props_ht = Z_ARRVAL_P(props);
int32_t i = zend_hash_num_elements(props_ht);
zend_class_entry *scope = __create_php_object(Z_STRVAL_P(class_name), Z_STRLEN_P(class_name), return_value TSRMLS_CC, "");
hprose_reader_refer_set(_this->refer, return_value);
if (i) {
#if PHP_VERSION_ID < 70100
zend_class_entry *old_scope = EG(scope);
EG(scope) = scope;
#else
zend_class_entry *old_scope = EG(fake_scope);
EG(fake_scope) = scope;
#endif
zend_hash_internal_pointer_reset(props_ht);
if (!Z_OBJ_HT_P(return_value)->write_property) {
zend_error(E_CORE_ERROR, "Properties of class %s cannot be updated", Z_STRVAL_P(class_name));
}
for (; i > 0; --i) {
char *name;
#if PHP_MAJOR_VERSION < 7
zval **e, *val;
zend_hash_get_current_data(props_ht, (void **)&e);
MAKE_STD_ZVAL(val);
hprose_reader_unserialize(_this, val TSRMLS_CC);
name = Z_STRVAL_PP(e);
name[0] = name[0] - 32;
if (has_property(scope, return_value, *e)) {
#if PHP_API_VERSION < 20100412
Z_OBJ_HT_P(return_value)->write_property(return_value, *e, val TSRMLS_CC);
#else
Z_OBJ_HT_P(return_value)->write_property(return_value, *e, val, NULL TSRMLS_CC);
#endif
name[0] = name[0] + 32;
}
else {
name[0] = name[0] + 32;
#if PHP_API_VERSION < 20100412
Z_OBJ_HT_P(return_value)->write_property(return_value, *e, val TSRMLS_CC);
#else
Z_OBJ_HT_P(return_value)->write_property(return_value, *e, val, NULL TSRMLS_CC);
#endif
}
#else
zval *e = zend_hash_get_current_data(props_ht), val, prop;
hprose_reader_unserialize(_this, &val TSRMLS_CC);
name = Z_STRVAL_P(e);
name[0] = name[0] - 32;
ZVAL_STRINGL(&prop, name, Z_STRLEN_P(e));
name[0] = name[0] + 32;
if (has_property(scope, return_value, &prop)) {
Z_OBJ_HT_P(return_value)->write_property(return_value, &prop, &val, NULL);
}
else {
Z_OBJ_HT_P(return_value)->write_property(return_value, e, &val, NULL);
}
zval_ptr_dtor(&prop);
#endif
zval_ptr_dtor(&val);
zend_hash_move_forward(props_ht);
}
#if PHP_VERSION_ID < 70100
EG(scope) = old_scope;
#else
EG(fake_scope) = old_scope;
#endif
}
hprose_bytes_io_skip(_this->stream, 1);
}
HPROSE_CLASS_BEGIN(reader)
HPROSE_CLASS_END(reader)
END_EXTERN_C()
#endif /* HPROSE_READER_H */