-
Notifications
You must be signed in to change notification settings - Fork 20
/
hprose_writer.h
737 lines (658 loc) · 29 KB
/
hprose_writer.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
731
732
733
734
735
736
/**********************************************************\
| |
| hprose |
| |
| Official WebSite: http://www.hprose.com/ |
| http://www.hprose.org/ |
| |
\**********************************************************/
/**********************************************************\
* *
* hprose_writer.h *
* *
* hprose writer for pecl header file. *
* *
* LastModified: Jan 5, 2016 *
* Author: Ma Bingyao <[email protected]> *
* *
\**********************************************************/
#ifndef HPROSE_WRITER_H
#define HPROSE_WRITER_H
#include "hprose_common.h"
#include "hprose_tags.h"
#include "hprose_bytes_io.h"
#include "hprose_class_manager.h"
#include "zend_interfaces.h"
#include "ext/spl/spl_observer.h"
#if PHP_API_VERSION >= 20090626
#include "ext/date/php_date.h"
#endif
BEGIN_EXTERN_C()
zend_class_entry *get_hprose_writer_ce();
HPROSE_STARTUP_FUNCTION(writer);
typedef struct {
zend_llist *ref;
zval *sref;
zval *oref;
int32_t refcount;
} hprose_writer_refer;
static zend_always_inline void hprose_writer_refer_write_ref(hprose_bytes_io *stream, int32_t index) {
hprose_bytes_io_putc(stream, HPROSE_TAG_REF);
hprose_bytes_io_write_int(stream, index);
hprose_bytes_io_putc(stream, HPROSE_TAG_SEMICOLON);
}
static void hprose_writer_refer_set(hprose_writer_refer *refer, zval *val) {
switch (Z_TYPE_P(val)) {
case IS_STRING:
add_assoc_long_ex(refer->sref, Z_STRVAL_P(val), Z_STRLEN_P(val), refer->refcount);
break;
case IS_OBJECT: {
zend_ulong h;
#if PHP_MAJOR_VERSION < 7
Z_ADDREF_P(val);
SEPARATE_ZVAL(&val);
zend_llist_add_element(refer->ref, &val);
h = (zend_ulong)Z_OBJ_HANDLE_P(val);
#else
Z_ADDREF_P(val);
zend_llist_add_element(refer->ref, &(Z_OBJ_P(val)));
h = (zend_ulong)Z_OBJ_P(val);
#endif
add_index_long(refer->oref, h, refer->refcount);
break;
}
}
++(refer->refcount);
}
static zend_bool hprose_writer_refer_write(hprose_writer_refer *refer, hprose_bytes_io *stream, zval *val) {
long index;
switch (Z_TYPE_P(val)) {
case IS_STRING:
if (php_assoc_array_get_long(refer->sref, Z_STRVAL_P(val), Z_STRLEN_P(val), &index)) {
hprose_writer_refer_write_ref(stream, (int32_t)index);
return 1;
}
break;
case IS_OBJECT: {
#if PHP_MAJOR_VERSION < 7
zend_ulong h = (zend_ulong)Z_OBJ_HANDLE_P(val);
#else
zend_ulong h = (zend_ulong)Z_OBJ_P(val);
#endif
if (php_array_get_long(refer->oref, h, &index)) {
hprose_writer_refer_write_ref(stream, (int32_t)index);
return 1;
}
break;
}
}
return 0;
}
static zend_always_inline void hprose_writer_refer_reset(hprose_writer_refer *refer) {
zend_llist_clean(refer->ref);
zend_hash_clean(Z_ARRVAL_P(refer->sref));
zend_hash_clean(Z_ARRVAL_P(refer->oref));
refer->refcount = 0;
}
static void hprose_writer_refer_free(hprose_writer_refer *refer) {
zend_llist_destroy(refer->ref);
efree(refer->ref);
refer->ref = NULL;
hprose_zval_free(refer->sref);
hprose_zval_free(refer->oref);
refer->sref = NULL;
refer->oref = NULL;
efree(refer);
}
static void __hprose_writer_refer_dtor(void *data) {
#if PHP_MAJOR_VERSION < 7
zval **val = (zval **)data;
if (data) {
zval_ptr_dtor(val);
}
#else
zend_object **obj = (zend_object **)data;
if (data) {
OBJ_RELEASE(*obj);
}
#endif
}
static hprose_writer_refer * hprose_writer_refer_new() {
hprose_writer_refer *_this = emalloc(sizeof(hprose_writer_refer));
_this->ref = emalloc(sizeof(zend_llist));
#if PHP_MAJOR_VERSION < 7
zend_llist_init(_this->ref, sizeof(zval *), (llist_dtor_func_t)__hprose_writer_refer_dtor, 0);
#else
zend_llist_init(_this->ref, sizeof(zend_object *), (llist_dtor_func_t)__hprose_writer_refer_dtor, 0);
#endif
hprose_zval_new(_this->sref);
hprose_zval_new(_this->oref);
array_init(_this->sref);
array_init(_this->oref);
_this->refcount = 0;
return _this;
}
typedef struct {
hprose_bytes_io *stream;
zval *classref;
zval *propsref;
hprose_writer_refer *refer;
} hprose_writer;
PHP_HPROSE_API void _hprose_writer_serialize(hprose_writer *_this, hprose_writer_refer *refer, hprose_bytes_io *stream, zval *val TSRMLS_DC);
#define hprose_writer_serialize(_this, val) _hprose_writer_serialize((_this), (_this)->refer, (_this)->stream, (val) TSRMLS_CC)
static zend_always_inline void hprose_writer_init(hprose_writer *_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);
_this->refer = simple ? NULL : hprose_writer_refer_new();
}
static zend_always_inline hprose_writer * hprose_writer_create(hprose_bytes_io *stream, zend_bool simple) {
hprose_writer *_this = emalloc(sizeof(hprose_writer));
hprose_writer_init(_this, stream, simple);
return _this;
}
static zend_always_inline void hprose_writer_destroy(hprose_writer *_this) {
_this->stream = NULL;
hprose_zval_free(_this->classref);
hprose_zval_free(_this->propsref);
_this->classref = NULL;
_this->propsref = NULL;
if (_this->refer) {
hprose_writer_refer_free(_this->refer);
_this->refer = NULL;
}
}
static zend_always_inline void hprose_writer_free(hprose_writer *_this) {
hprose_writer_destroy(_this);
efree(_this);
}
static zend_always_inline void hprose_writer_reset(hprose_writer *_this) {
zend_hash_clean(Z_ARRVAL_P(_this->classref));
zend_hash_clean(Z_ARRVAL_P(_this->propsref));
if (_this->refer) {
hprose_writer_refer_reset(_this->refer);
}
}
#define _hprose_writer_write_nan(stream) hprose_bytes_io_putc((stream), HPROSE_TAG_NAN)
#define _hprose_writer_write_null(stream) hprose_bytes_io_putc((stream), HPROSE_TAG_NULL)
#define _hprose_writer_write_true(stream) hprose_bytes_io_putc((stream), HPROSE_TAG_TRUE)
#define _hprose_writer_write_false(stream) hprose_bytes_io_putc((stream), HPROSE_TAG_FALSE)
#define _hprose_writer_write_bool(stream, val) ((val) ? _hprose_writer_write_true(stream) : _hprose_writer_write_false(stream))
#define _hprose_writer_write_empty(stream) hprose_bytes_io_putc((stream), HPROSE_TAG_EMPTY)
#define hprose_writer_write_nan(_this) _hprose_writer_write_nan((_this)->stream)
#define hprose_writer_write_null(_this) _hprose_writer_write_null((_this)->stream)
#define hprose_writer_write_true(_this) _hprose_writer_write_true((_this)->stream)
#define hprose_writer_write_false(_this) _hprose_writer_write_false((_this)->stream)
#define hprose_writer_write_bool(_this, val) _hprose_writer_write_bool((_this)->stream, (val))
#define hprose_writer_write_empty(_this) _hprose_writer_write_empty((_this)->stream)
static zend_always_inline void _hprose_writer_write_ulong(hprose_bytes_io *stream, uint64_t i) {
if (i <= 9) {
hprose_bytes_io_putc(stream, (char)('0' + i));
}
else {
hprose_bytes_io_putc(stream, (i > INT32_MAX) ? HPROSE_TAG_LONG : HPROSE_TAG_INTEGER);
hprose_bytes_io_write_ulong(stream, i);
hprose_bytes_io_putc(stream, HPROSE_TAG_SEMICOLON);
}
}
#define hprose_writer_write_ulong(_this, i) _hprose_writer_write_ulong((_this)->stream, (i))
static zend_always_inline void _hprose_writer_write_long(hprose_bytes_io *stream, int64_t i) {
if (i >= 0 && i <= 9) {
hprose_bytes_io_putc(stream, (char)('0' + i));
}
else {
hprose_bytes_io_putc(stream, (i > INT32_MAX || i < INT32_MIN) ? HPROSE_TAG_LONG : HPROSE_TAG_INTEGER);
hprose_bytes_io_write_long(stream, i);
hprose_bytes_io_putc(stream, HPROSE_TAG_SEMICOLON);
}
}
#define hprose_writer_write_long(_this, i) _hprose_writer_write_long((_this)->stream, (i))
static zend_always_inline void _hprose_writer_write_infinity(hprose_bytes_io *stream, zend_bool positive) {
hprose_bytes_io_putc(stream, HPROSE_TAG_INFINITY);
hprose_bytes_io_putc(stream, (positive ? HPROSE_TAG_POS : HPROSE_TAG_NEG));
}
#define hprose_writer_write_infinity(_this, positive) _hprose_writer_write_infinity((_this)->stream, (positive))
static zend_always_inline void _hprose_writer_write_double(hprose_bytes_io *stream, double d) {
if (isnan(d)) {
_hprose_writer_write_nan(stream);
}
else if (isinf(d)) {
_hprose_writer_write_infinity(stream, d > 0);
}
else if (floor(d) == d && d <= INT64_MAX && d >= INT64_MIN) {
_hprose_writer_write_long(stream, (int64_t)d);
}
else {
hprose_bytes_io_putc(stream, HPROSE_TAG_DOUBLE);
hprose_bytes_io_write_double(stream, d);
hprose_bytes_io_putc(stream, HPROSE_TAG_SEMICOLON);
}
}
#define hprose_writer_write_double(_this, d) _hprose_writer_write_double((_this)->stream, (d))
static zend_always_inline void _hprose_writer_write_utf8char(hprose_bytes_io *stream, zval *val) {
hprose_bytes_io_putc(stream, HPROSE_TAG_UTF8CHAR);
hprose_bytes_io_write(stream, Z_STRVAL_P(val), Z_STRLEN_P(val));
}
#define hprose_writer_write_utf8char(_this, val) _hprose_writer_write_utf8char((_this)->stream, (val))
static zend_always_inline void _hprose_writer_write_string(hprose_writer_refer *refer, hprose_bytes_io *stream, zval *val) {
int32_t len = utf16_length(Z_STRVAL_P(val), Z_STRLEN_P(val));
if (refer) {
hprose_writer_refer_set(refer, val);
}
if (len >= 0) {
hprose_bytes_io_putc(stream, HPROSE_TAG_STRING);
if (len) {
hprose_bytes_io_write_int(stream, len);
}
} else {
hprose_bytes_io_putc(stream, HPROSE_TAG_BYTES);
hprose_bytes_io_write_int(stream, Z_STRLEN_P(val));
}
hprose_bytes_io_putc(stream, HPROSE_TAG_QUOTE);
hprose_bytes_io_write(stream, Z_STRVAL_P(val), Z_STRLEN_P(val));
hprose_bytes_io_putc(stream, HPROSE_TAG_QUOTE);
}
#define hprose_writer_write_string(_this, val) _hprose_writer_write_string((_this)->refer, (_this)->stream, (val))
static zend_always_inline void _hprose_writer_write_string_with_ref(hprose_writer_refer *refer, hprose_bytes_io *stream, zval *val) {
if (refer == NULL || !hprose_writer_refer_write(refer, stream, val)) _hprose_writer_write_string(refer, stream, val);
}
#define hprose_writer_write_string_with_ref(_this, val) _hprose_writer_write_string_with_ref((_this)->refer, (_this)->stream, (val))
static zend_always_inline void _hprose_writer_write_bytes(hprose_writer_refer *refer, hprose_bytes_io *stream, zval *val) {
int32_t len = Z_STRLEN_P(val);
if (refer) {
hprose_writer_refer_set(refer, val);
}
hprose_bytes_io_putc(stream, HPROSE_TAG_BYTES);
if (len) {
hprose_bytes_io_write_int(stream, len);
}
hprose_bytes_io_putc(stream, HPROSE_TAG_QUOTE);
hprose_bytes_io_write(stream, Z_STRVAL_P(val), len);
hprose_bytes_io_putc(stream, HPROSE_TAG_QUOTE);
}
#define hprose_writer_write_bytes(_this, val) _hprose_writer_write_bytes((_this)->refer, (_this)->stream, (val))
static zend_always_inline void _hprose_writer_write_bytes_with_ref(hprose_writer_refer *refer, hprose_bytes_io *stream, zval *val) {
if (refer == NULL || !hprose_writer_refer_write(refer, stream, val)) _hprose_writer_write_bytes(refer, stream, val);
}
#define hprose_writer_write_bytes_with_ref(_this, val) _hprose_writer_write_bytes_with_ref((_this)->refer, (_this)->stream, (val))
static zend_always_inline void _hprose_writer_write_bytes_io(hprose_writer_refer *refer, hprose_bytes_io *stream, zval *val TSRMLS_DC) {
hprose_bytes_io *instream = HPROSE_GET_OBJECT_P(bytes_io, val)->_this;
int32_t len = 0;
if (HB_INITED_P(instream)) {
len = HB_LEN_P(instream);
}
if (refer) {
hprose_writer_refer_set(refer, val);
}
hprose_bytes_io_putc(stream, HPROSE_TAG_BYTES);
if (len) {
int32_t pos = HB_POS_P(instream);
HB_POS_P(instream) = 0;
hprose_bytes_io_write_int(stream, len);
hprose_bytes_io_putc(stream, HPROSE_TAG_QUOTE);
hprose_bytes_io_read_to(instream, stream, len);
HB_POS_P(instream) = pos;
}
else {
hprose_bytes_io_putc(stream, HPROSE_TAG_QUOTE);
}
hprose_bytes_io_putc(stream, HPROSE_TAG_QUOTE);
}
#define hprose_writer_write_bytes_io(_this, val) _hprose_writer_write_bytes_io((_this)->refer, (_this)->stream, (val) TSRMLS_CC)
static zend_always_inline void _hprose_writer_write_bytes_io_with_ref(hprose_writer_refer *refer, hprose_bytes_io *stream, zval *val TSRMLS_DC) {
if (refer == NULL || !hprose_writer_refer_write(refer, stream, val)) _hprose_writer_write_bytes_io(refer, stream, val TSRMLS_CC);
}
#define hprose_writer_write_bytes_io_with_ref(_this, val) _hprose_writer_write_bytes_io_with_ref((_this)->refer, (_this)->stream, (val) TSRMLS_CC)
static zend_always_inline void _hprose_writer_write_datetime(hprose_writer_refer *refer, hprose_bytes_io *stream, zval *val TSRMLS_DC) {
zval result;
if (refer) {
hprose_writer_refer_set(refer, val);
}
method_invoke_no_args(val, getOffset, &result);
if (Z_LVAL(result) == 0) {
method_invoke(val, format, &result, "s", ZEND_STRL("\\DYmd\\THis.u\\Z"));
}
else {
method_invoke(val, format, &result, "s", ZEND_STRL("\\DYmd\\THis.u;"));
}
hprose_bytes_io_write(stream, Z_STRVAL(result), Z_STRLEN(result));
#if PHP_MAJOR_VERSION < 7
zval_dtor(&result);
#else
zval_ptr_dtor(&result);
#endif
}
#define hprose_writer_write_datetime(_this, val) _hprose_writer_write_datetime((_this)->refer, (_this)->stream, (val) TSRMLS_CC)
static zend_always_inline void _hprose_writer_write_datetime_with_ref(hprose_writer_refer *refer, hprose_bytes_io *stream, zval *val TSRMLS_DC) {
if (refer == NULL || !hprose_writer_refer_write(refer, stream, val)) _hprose_writer_write_datetime(refer, stream, val TSRMLS_CC);
}
#define hprose_writer_write_datetime_with_ref(_this, val) _hprose_writer_write_datetime_with_ref((_this)->refer, (_this)->stream, (val) TSRMLS_CC)
static void _hprose_writer_write_array(hprose_writer *_this, hprose_writer_refer *refer, hprose_bytes_io *stream, zval *val TSRMLS_DC) {
HashTable *ht = Z_ARRVAL_P(val);
int32_t i = zend_hash_num_elements(ht);
if (refer) {
hprose_writer_refer_set(refer, val);
}
hprose_bytes_io_putc(stream, HPROSE_TAG_LIST);
if (i) {
hprose_bytes_io_write_int(stream, i);
}
hprose_bytes_io_putc(stream, HPROSE_TAG_OPENBRACE);
if (i) {
zend_hash_internal_pointer_reset(ht);
for (; i > 0; --i) {
#if PHP_MAJOR_VERSION < 7
zval **e;
zend_hash_get_current_data(ht, (void **)&e);
_hprose_writer_serialize(_this, refer, stream, *e TSRMLS_CC);
#else
zval *e = zend_hash_get_current_data(ht);
_hprose_writer_serialize(_this, refer, stream, e TSRMLS_CC);
#endif
zend_hash_move_forward(ht);
}
}
hprose_bytes_io_putc(stream, HPROSE_TAG_CLOSEBRACE);
}
#define hprose_writer_write_array(_this, val) _hprose_writer_write_array((_this), (_this)->refer, (_this)->stream, (val) TSRMLS_CC)
static void _hprose_writer_write_hashtable(hprose_writer *_this, hprose_writer_refer *refer, hprose_bytes_io *stream, HashTable *ht TSRMLS_DC) {
int32_t i = zend_hash_num_elements(ht);
hprose_bytes_io_putc(stream, HPROSE_TAG_MAP);
if (i) {
hprose_bytes_io_write_int(stream, i);
}
hprose_bytes_io_putc(stream, HPROSE_TAG_OPENBRACE);
if (i) {
HashPosition *position = emalloc(sizeof(HashPosition));
memset(position, 0, sizeof(HashPosition));
zend_hash_internal_pointer_reset_ex(ht, position);
for (; i > 0; --i) {
#if PHP_MAJOR_VERSION < 7
char *str;
uint32_t len;
zend_ulong index;
zval **value;
if (zend_hash_get_current_key_ex(ht, &str, &len, &index, 0, position) == HASH_KEY_IS_STRING) {
zval key;
ZVAL_STRINGL(&key, str, len - 1, 0);
_hprose_writer_serialize(_this, refer, stream, &key TSRMLS_CC);
}
else {
_hprose_writer_write_ulong(stream, (uint64_t)index);
}
zend_hash_get_current_data_ex(ht, (void **)&value, position);
_hprose_writer_serialize(_this, refer, stream, *value TSRMLS_CC);
#else
zval key, *value;
zend_hash_get_current_key_zval_ex(ht, &key, position);
value = zend_hash_get_current_data_ex(ht, position);
_hprose_writer_serialize(_this, refer, stream, &key TSRMLS_CC);
_hprose_writer_serialize(_this, refer, stream, value TSRMLS_CC);
zval_ptr_dtor(&key);
#endif
zend_hash_move_forward_ex(ht, position);
}
efree(position);
}
hprose_bytes_io_putc(stream, HPROSE_TAG_CLOSEBRACE);
}
static zend_always_inline void _hprose_writer_write_assoc_array(hprose_writer *_this, hprose_writer_refer *refer, hprose_bytes_io *stream, zval *val TSRMLS_DC) {
if (refer) {
hprose_writer_refer_set(refer, val);
}
_hprose_writer_write_hashtable(_this, refer, stream, Z_ARRVAL_P(val) TSRMLS_CC);
}
#define hprose_writer_write_assoc_array(_this, val) _hprose_writer_write_assoc_array((_this), (_this)->refer, (_this)->stream, (val) TSRMLS_CC)
static void _hprose_writer_write_map(hprose_writer *_this, hprose_writer_refer *refer, hprose_bytes_io *stream, zval *val TSRMLS_DC) {
zval count;
int32_t i;
if (refer) {
hprose_writer_refer_set(refer, val);
}
method_invoke_no_args(val, count, &count);
i = Z_LVAL(count);
hprose_bytes_io_putc(stream, HPROSE_TAG_MAP);
if (i) {
hprose_bytes_io_write_int(stream, i);
}
hprose_bytes_io_putc(stream, HPROSE_TAG_OPENBRACE);
if (i) {
method_invoke_no_args(val, rewind, NULL);
for (; i > 0; --i) {
zval key, value;
method_invoke_no_args(val, current, &key);
method_invoke(val, offsetGet, &value, "z", &key);
_hprose_writer_serialize(_this, refer, stream, &key TSRMLS_CC);
_hprose_writer_serialize(_this, refer, stream, &value TSRMLS_CC);
#if PHP_MAJOR_VERSION < 7
zval_dtor(&key);
zval_dtor(&value);
#else
zval_ptr_dtor(&key);
zval_ptr_dtor(&value);
#endif
method_invoke_no_args(val, next, NULL);
}
}
hprose_bytes_io_putc(stream, HPROSE_TAG_CLOSEBRACE);
}
#define hprose_writer_write_map(_this, val) _hprose_writer_write_map((_this), (_this)->refer, (_this)->stream, (val) TSRMLS_CC)
static zend_always_inline void _hprose_writer_write_map_with_ref(hprose_writer *_this, hprose_writer_refer *refer, hprose_bytes_io *stream, zval *val TSRMLS_DC) {
if (refer == NULL || !hprose_writer_refer_write(refer, stream, val)) _hprose_writer_write_map(_this, refer, stream, val TSRMLS_CC);
}
#define hprose_writer_write_map_with_ref(_this, val) _hprose_writer_write_map_with_ref((_this), (_this)->refer, (_this)->stream, (val) TSRMLS_CC)
static void _hprose_writer_write_iterator(hprose_writer *_this, hprose_writer_refer *refer, hprose_bytes_io *stream, zval *val TSRMLS_DC) {
zval count;
int32_t i;
if (refer) {
hprose_writer_refer_set(refer, val);
}
method_invoke_no_args(val, count, &count);
i = Z_LVAL(count);
hprose_bytes_io_putc(stream, HPROSE_TAG_LIST);
if (i) {
hprose_bytes_io_write_int(stream, i);
}
hprose_bytes_io_putc(stream, HPROSE_TAG_OPENBRACE);
if (i) {
method_invoke_no_args(val, rewind, NULL);
for (; i > 0; --i) {
zval e;
method_invoke_no_args(val, current, &e);
_hprose_writer_serialize(_this, refer, stream, &e TSRMLS_CC);
#if PHP_MAJOR_VERSION < 7
zval_dtor(&e);
#else
zval_ptr_dtor(&e);
#endif
method_invoke_no_args(val, next, NULL);
}
}
hprose_bytes_io_putc(stream, HPROSE_TAG_CLOSEBRACE);
}
#define hprose_writer_write_iterator(_this, val) _hprose_writer_write_iterator((_this), (_this)->refer, (_this)->stream, (val) TSRMLS_CC)
static zend_always_inline void _hprose_writer_write_iterator_with_ref(hprose_writer *_this, hprose_writer_refer *refer, hprose_bytes_io *stream, zval *val TSRMLS_DC) {
if (refer == NULL || !hprose_writer_refer_write(refer, stream, val)) _hprose_writer_write_iterator(_this, refer, stream, val TSRMLS_CC);
}
#define hprose_writer_write_iterator_with_ref(_this, val) _hprose_writer_write_iterator_with_ref((_this), (_this)->refer, (_this)->stream, (val) TSRMLS_CC)
static void _hprose_writer_write_list(hprose_writer *_this, hprose_writer_refer *refer, hprose_bytes_io *stream, zval *val TSRMLS_DC) {
zend_class_entry *ce = Z_OBJCE_P(val);
if (instanceof_function(ce, zend_ce_aggregate TSRMLS_CC)) {
zval iterator;
method_invoke_no_args(val, getIterator, &iterator);
_hprose_writer_write_list(_this, refer, stream, &iterator TSRMLS_CC);
}
else {
_hprose_writer_write_iterator(_this, refer, stream, val TSRMLS_CC);
}
}
#define hprose_writer_write_list(_this, val) _hprose_writer_write_list((_this), (_this)->refer, (_this)->stream, (val) TSRMLS_CC)
static void _hprose_writer_write_list_with_ref(hprose_writer *_this, hprose_writer_refer *refer, hprose_bytes_io *stream, zval *val TSRMLS_DC) {
zend_class_entry *ce = Z_OBJCE_P(val);
if (instanceof_function(ce, zend_ce_aggregate TSRMLS_CC)) {
zval iterator;
method_invoke_no_args(val, getIterator, &iterator);
_hprose_writer_write_list_with_ref(_this, refer, stream, &iterator TSRMLS_CC);
#if PHP_MAJOR_VERSION < 7
zval_dtor(&iterator);
#else
zval_ptr_dtor(&iterator);
#endif
}
else {
_hprose_writer_write_iterator_with_ref(_this, refer, stream, val TSRMLS_CC);
}
}
#define hprose_writer_write_list_with_ref(_this, val) _hprose_writer_write_list_with_ref((_this), (_this)->refer, (_this)->stream, (val) TSRMLS_CC)
static zend_always_inline void _hprose_writer_write_stdclass(hprose_writer *_this, hprose_writer_refer *refer, hprose_bytes_io *stream, zval *val TSRMLS_DC) {
if (refer) {
hprose_writer_refer_set(refer, val);
}
_hprose_writer_write_hashtable(_this, refer, stream, Z_OBJPROP_P(val) TSRMLS_CC);
}
#define hprose_writer_write_stdclass(_this, val) _hprose_writer_write_stdclass((_this), (_this)->refer, (_this)->stream, (val) TSRMLS_CC)
static zend_always_inline void _hprose_writer_write_stdclass_with_ref(hprose_writer *_this, hprose_writer_refer *refer, hprose_bytes_io *stream, zval *val TSRMLS_DC) {
if (refer == NULL || !hprose_writer_refer_write(refer, stream, val)) _hprose_writer_write_stdclass(_this, refer, stream, val TSRMLS_CC);
}
#define hprose_writer_write_stdclass_with_ref(_this, val) _hprose_writer_write_stdclass_with_ref((_this), (_this)->refer, (_this)->stream, (val) TSRMLS_CC)
static int32_t _hprose_writer_write_class(hprose_writer *_this, hprose_writer_refer *refer, hprose_bytes_io *stream, char *alias, int32_t len, HashTable *ht) {
int32_t i = zend_hash_num_elements(ht);
int32_t index = Z_ARRLEN_P(_this->propsref);
#if PHP_MAJOR_VERSION < 7
zval *props;
hprose_zval_new(props);
array_init(props);
#else
zval props;
array_init(&props);
#endif
hprose_bytes_io_putc(stream, HPROSE_TAG_CLASS);
hprose_bytes_io_write_int(stream, len);
hprose_bytes_io_putc(stream, HPROSE_TAG_QUOTE);
hprose_bytes_io_write(stream, alias, len);
hprose_bytes_io_putc(stream, HPROSE_TAG_QUOTE);
if (i) {
HashPosition *position = emalloc(sizeof(HashPosition));
memset(position, 0, sizeof(HashPosition));
zend_hash_internal_pointer_reset_ex(ht, position);
hprose_bytes_io_write_int(stream, i);
hprose_bytes_io_putc(stream, HPROSE_TAG_OPENBRACE);
for (; i > 0; --i) {
#if PHP_MAJOR_VERSION < 7
char *str;
uint32_t len;
zend_ulong index;
if (zend_hash_get_current_key_ex(ht, &str, &len, &index, 0, position) == HASH_KEY_IS_STRING) {
zval prop;
size_t pos = 0;
add_next_index_stringl(props, str, len, 1);
if (str[0] == 0) {
pos = strlen(str + 1) + 2;
}
ZVAL_STRINGL(&prop, str + pos, len - pos - 1, 1);
str = Z_STRVAL(prop);
if ((str[0] >= 'A') && (str[0] <= 'Z')) {
str[0] = str[0] + 32;
}
_hprose_writer_write_string_with_ref(refer, stream, &prop);
zval_dtor(&prop);
}
#else
zval prop;
char *str;
uint32_t len;
size_t pos = 0;
zend_hash_get_current_key_zval_ex(ht, &prop, position);
assert(Z_TYPE(prop) == IS_STRING);
add_next_index_zval(&props, &prop);
str = Z_STRVAL(prop);
len = Z_STRLEN(prop);
if (str[0] == 0) {
pos = strlen(str + 1) + 2;
}
ZVAL_STRINGL(&prop, str + pos, len - pos);
str = Z_STRVAL(prop);
if ((str[0] >= 'A') && (str[0] <= 'Z')) {
str[0] = str[0] + 32;
}
_hprose_writer_write_string_with_ref(refer, stream, &prop);
zval_ptr_dtor(&prop);
#endif
zend_hash_move_forward_ex(ht, position);
}
efree(position);
hprose_bytes_io_putc(stream, HPROSE_TAG_CLOSEBRACE);
}
else {
hprose_bytes_io_putc(stream, HPROSE_TAG_OPENBRACE);
hprose_bytes_io_putc(stream, HPROSE_TAG_CLOSEBRACE);
}
#if PHP_MAJOR_VERSION < 7
add_next_index_zval(_this->propsref, props);
#else
add_next_index_zval(_this->propsref, &props);
#endif
add_assoc_long_ex(_this->classref, alias, len, index);
return index;
}
static void _hprose_writer_write_object(hprose_writer *_this, hprose_writer_refer *refer, hprose_bytes_io *stream, zval *val TSRMLS_DC) {
HashTable *ht = Z_OBJPROP_P(val), *props_ht;
zval *props;
long index;
int32_t i;
#if PHP_MAJOR_VERSION < 7
char *classname = (char *)Z_OBJCE_P(val)->name;
int32_t nlen = strlen(classname);
int32_t alen;
char *alias = hprose_class_manager_get_alias(classname, nlen, &alen);
if (!php_assoc_array_get_long(_this->classref, alias, alen, &index)) {
index = _hprose_writer_write_class(_this, refer, stream, alias, alen, ht);
}
efree(alias);
#else
zend_string *_classname = Z_OBJ_HT_P(val)->get_class_name(Z_OBJ_P(val));
zend_string *alias = hprose_class_manager_get_alias(_classname->val, _classname->len);
if (!php_assoc_array_get_long(_this->classref, alias->val, alias->len, &index)) {
index = _hprose_writer_write_class(_this, refer, stream, alias->val, alias->len, ht);
}
zend_string_release(_classname);
zend_string_release(alias);
#endif
props = php_array_get(_this->propsref, index);
props_ht = Z_ARRVAL_P(props);
i = zend_hash_num_elements(props_ht);
if (refer) {
hprose_writer_refer_set(refer, val);
}
hprose_bytes_io_putc(stream, HPROSE_TAG_OBJECT);
hprose_bytes_io_write_int(stream, index);
hprose_bytes_io_putc(stream, HPROSE_TAG_OPENBRACE);
if (i) {
zend_hash_internal_pointer_reset(props_ht);
for (; i > 0; --i) {
#if PHP_MAJOR_VERSION < 7
zval **e, *value;
zend_hash_get_current_data(props_ht, (void **)&e);
value = zend_hash_str_find_ptr(ht, Z_STRVAL_PP(e), Z_STRLEN_PP(e));
_hprose_writer_serialize(_this, refer, stream, value TSRMLS_CC);
#else
zval *e = zend_hash_get_current_data(props_ht);
zval *value = zend_hash_str_find_ptr(ht, Z_STRVAL_P(e), Z_STRLEN_P(e));
_hprose_writer_serialize(_this, refer, stream, value TSRMLS_CC);
#endif
zend_hash_move_forward(props_ht);
}
}
hprose_bytes_io_putc(stream, HPROSE_TAG_CLOSEBRACE);
}
#define hprose_writer_write_object(_this, val) _hprose_writer_write_object((_this), (_this)->refer, (_this)->stream, (val) TSRMLS_CC)
static zend_always_inline void _hprose_writer_write_object_with_ref(hprose_writer *_this, hprose_writer_refer *refer, hprose_bytes_io *stream, zval *val TSRMLS_DC) {
if (refer == NULL || !hprose_writer_refer_write(refer, stream, val)) _hprose_writer_write_object(_this, refer, stream, val TSRMLS_CC);
}
#define hprose_writer_write_object_with_ref(_this, val) _hprose_writer_write_object_with_ref((_this), (_this)->refer, (_this)->stream, (val) TSRMLS_CC)
HPROSE_CLASS_BEGIN(writer)
HPROSE_CLASS_END(writer)
END_EXTERN_C()
#endif /* HPROSE_WRITER_H */