-
Notifications
You must be signed in to change notification settings - Fork 3
/
ion_private.h
3040 lines (2665 loc) · 87.4 KB
/
ion_private.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
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
+--------------------------------------------------------------------+
| PECL :: ion |
+--------------------------------------------------------------------+
| Redistribution and use in source and binary forms, with or without |
| modification, are permitted provided that the conditions mentioned |
| in the accompanying LICENSE file are met. |
+--------------------------------------------------------------------+
| Copyright (c) 2021, Michael Wallner <[email protected]> |
+--------------------------------------------------------------------+
*/
#include "php.h"
#include "ext/standard/php_var.h"
#include "ext/date/php_date.h"
#define DECNUMDIGITS 34 /* DECQUAD_Pmax */
#include "ionc/ion.h"
#define PHP_ION_SYMBOL_TABLE_VERSION 1
#define PHP_ION_SYMBOL(c, s) { \
{ \
0, \
{ sizeof(s)-1, (BYTE *) s }, \
{ { 0, NULL }, 0 }, \
0 \
}, \
{ sizeof(c)-1, (BYTE *) c } \
},
typedef struct php_ion_global_symbol {
ION_SYMBOL s;
ION_STRING e;
} php_ion_global_symbol;
static php_ion_global_symbol g_sym_tab_php_sym[] = {
#define PHP_ION_SYMBOL_PHP (g_sym_tab_php_sym[0]).s
PHP_ION_SYMBOL("PHP", "PHP")
#define PHP_ION_SYMBOL_REFERENCE (g_sym_tab_php_sym[1]).s
PHP_ION_SYMBOL("Reference", "R")
#define PHP_ION_SYMBOL_BACKREF (g_sym_tab_php_sym[2]).s
PHP_ION_SYMBOL("Backref", "r")
#define PHP_ION_SYMBOL_PROPERTY (g_sym_tab_php_sym[3]).s
PHP_ION_SYMBOL("Property", "p")
#define PHP_ION_SYMBOL_CLASS_OBJECT (g_sym_tab_php_sym[4]).s
PHP_ION_SYMBOL("ClassObject", "c")
#define PHP_ION_SYMBOL_CUSTOM_OBJECT (g_sym_tab_php_sym[5]).s
PHP_ION_SYMBOL("CustomObject", "C")
#define PHP_ION_SYMBOL_OBJECT (g_sym_tab_php_sym[6]).s
PHP_ION_SYMBOL("Object", "o")
#define PHP_ION_SYMBOL_MAGIC_OBJECT (g_sym_tab_php_sym[7]).s
PHP_ION_SYMBOL("MagicObject", "O")
#define PHP_ION_SYMBOL_SERIALIZEABLE (g_sym_tab_php_sym[8]).s
PHP_ION_SYMBOL("Serializable", "S")
#define PHP_ION_SYMBOL_ENUM (g_sym_tab_php_sym[9]).s
PHP_ION_SYMBOL("Enum", "E")
{{0}, {0}}
};
#undef PHP_ION_SYMBOL
static ION_SYMBOL_TABLE *g_sym_tab_php;
/* [SID => STRING, STRING => SID] */
static HashTable g_sym_hash;
/* [enum_case_name => SID] */
static HashTable g_sym_map;
#define ION_SYS_SYMBOL_SYMBOL_TABLE \
ION_SYS_SYMBOL_ION_SYMBOL_TABLE
#define g_sym_add(enum_name, c_name) do { \
g_sym_hash_add(ION_SYS_SID_ ## c_name, ION_SYS_SYMBOL_ ## c_name, ION_SYS_STRLEN_ ## c_name); \
g_sym_map_add(ION_SYS_SID_ ## c_name, enum_name, sizeof(enum_name)-1); \
} while (0)
static void g_sym_hash_add(int sid, const char *str, size_t len)
{
zval zl, zs;
ZVAL_LONG(&zl, sid);
ZVAL_STR(&zs, zend_string_init_interned(str, len ,1));
zend_hash_add(&g_sym_hash, Z_STR(zs), &zl);
zend_hash_index_add(&g_sym_hash, sid, &zs);
}
static void g_sym_map_add(int sid, const char *str, size_t len)
{
zval zv;
ZVAL_LONG(&zv, sid);
zend_hash_str_add(&g_sym_map, str, len, &zv);
}
static void g_sym_dtor(void)
{
ion_symbol_table_close(g_sym_tab_php);
zend_hash_destroy(&g_sym_map);
zend_hash_destroy(&g_sym_hash);
}
static int g_sym_init(void)
{
zend_hash_init(&g_sym_hash, 0, NULL, NULL, 1);
zend_hash_init(&g_sym_map, 0, NULL, NULL, 1);
g_sym_hash_add(0, ZEND_STRL(""));
g_sym_map_add(0, ZEND_STRL(""));
g_sym_add("Ion", ION);
g_sym_add("Ivm_1_0", IVM);
g_sym_add("IonSymbolTable", SYMBOL_TABLE);
g_sym_add("Name", NAME);
g_sym_add("Version", VERSION);
g_sym_add("Imports", IMPORTS);
g_sym_add("Symbols", SYMBOLS);
g_sym_add("MaxId", MAX_ID);
g_sym_add("SharedSymbolTable", SHARED_SYMBOL_TABLE);
int sys_max_id = ION_SYS_SID_SHARED_SYMBOL_TABLE;
if (IERR_OK != ion_symbol_table_open_with_type(&g_sym_tab_php, NULL, ist_SHARED)) {
return FAILURE;
}
php_ion_global_symbol *ptr = g_sym_tab_php_sym;
ion_symbol_table_set_version(g_sym_tab_php, PHP_ION_SYMBOL_TABLE_VERSION);
ion_symbol_table_set_name(g_sym_tab_php, &ptr->s.value);
while (ptr->e.value) {
ion_symbol_table_add_symbol(g_sym_tab_php, &ptr->s.value, &ptr->s.sid);
g_sym_hash_add(sys_max_id + ptr->s.sid, (const char *) ptr->s.value.value, ptr->s.value.length);
g_sym_map_add(sys_max_id + ptr->s.sid, (const char *) ptr->e.value, ptr->e.length);
++ptr;
}
ion_symbol_table_lock(g_sym_tab_php);
return SUCCESS;
}
static struct {
zend_string *Year, *Month, *Day, *Min, *Sec, *Frac, *MinTZ, *SecTZ, *FracTZ;
} g_intern_str;
static void g_intern_str_init()
{
#define NEW_INTERN_STR(s) \
g_intern_str.s = zend_string_init_interned(#s, sizeof(#s)-1, 1)
NEW_INTERN_STR(Year);
NEW_INTERN_STR(Month);
NEW_INTERN_STR(Day);
NEW_INTERN_STR(Min);
NEW_INTERN_STR(Sec);
NEW_INTERN_STR(Frac);
NEW_INTERN_STR(MinTZ);
NEW_INTERN_STR(SecTZ);
NEW_INTERN_STR(FracTZ);
#undef NEW_INTERN_STR
}
typedef struct php_ion_serializer {
zend_string *call_custom;
zend_bool call_magic;
zend_bool multi_seq;
uint32_t level;
HashTable *ids;
HashTable *tmp;
zend_object *wri, std;
} php_ion_serializer;
typedef int (*php_ion_serialize_zval_cb)(void *ctx, zval *zv);
typedef struct php_ion_annotations {
uint8_t shared_symtab:1;
uint8_t backref:1;
uint8_t makeref:1;
uint8_t object_prop:1;
uint8_t object_type;
zend_string *object_class;
zend_string *property_class;
} php_ion_annotations;
typedef struct php_ion_unserializer {
zend_string *call_custom;
zend_bool call_magic;
zend_bool multi_seq;
uint32_t level;
HashTable *ids;
HashTable *tmp;
HashTable *addref;
ION_TYPE type; // FIXME: there's already `php_ion_obj(reader, rdr)->state`
php_ion_annotations annotations;
zend_object *rdr, std;
} php_ion_unserializer;
ZEND_BEGIN_MODULE_GLOBALS(ion)
struct {
decContext ctx;
ION_DECIMAL zend_max;
ION_DECIMAL zend_min;
} decimal;
struct {
HashTable cache;
} symbol;
php_ion_serializer serializer;
php_ion_unserializer unserializer;
struct {
HashTable serializer[2];
HashTable unserializer[3];
} _ht;
ZEND_END_MODULE_GLOBALS(ion);
#ifdef ZTS
# define php_ion_globals (*((zend_ion_globals *) (*((void ***) tsrm_get_ls_cache()))[TSRM_UNSHUFFLE_RSRC_ID(ion_globals_id)]))
#else
# define php_ion_globals ion_globals
#endif
ZEND_DECLARE_MODULE_GLOBALS(ion);
static zend_class_entry
*ce_Catalog,
*ce_Decimal,
*ce_Decimal_Context,
*ce_Decimal_Context_Rounding,
*ce_Exception,
*ce_LOB,
*ce_Reader,
*ce_Reader_Reader,
*ce_Reader_Buffer,
*ce_Reader_Stream,
*ce_Reader_Buffer_Reader,
*ce_Reader_Stream_Reader,
*ce_Serializer,
*ce_Serializer_Serializer,
*ce_Symbol,
*ce_Symbol_ImportLocation,
*ce_Symbol_Enum,
*ce_Symbol_Table,
*ce_Symbol_Table_Local,
*ce_Symbol_Table_PHP,
*ce_Symbol_Table_Shared,
*ce_Symbol_Table_System,
*ce_Timestamp,
*ce_Timestamp_Format,
*ce_Timestamp_Precision,
*ce_Type,
*ce_Unserializer,
*ce_Unserializer_Unserializer,
*ce_Writer,
*ce_Writer_Buffer,
*ce_Writer_Buffer_Writer,
*ce_Writer_Stream,
*ce_Writer_Stream_Writer,
*ce_Writer_Writer
;
static void php_ion_globals_symbols_init(void)
{
zend_hash_init(&php_ion_globals.symbol.cache, 0, NULL, ZVAL_PTR_DTOR, 0);
}
static void php_ion_globals_symbols_dtor(void)
{
zend_hash_destroy(&php_ion_globals.symbol.cache);
}
static void php_ion_globals_serializer_init(void)
{
php_ion_serializer *s = &php_ion_globals.serializer;
HashTable *h = php_ion_globals._ht.serializer;
zend_hash_init(s->tmp = &h[0], 0, NULL, ZVAL_PTR_DTOR, 0);
zend_hash_init(s->ids = &h[1], 0, NULL, NULL, 0);
}
static uint32_t php_ion_globals_serializer_step(void)
{
php_ion_serializer *s = &php_ion_globals.serializer;
uint32_t level;
if (!(level = s->level++)) {
zend_hash_clean(s->ids);
zend_hash_clean(s->tmp);
}
return level;
}
static uint32_t php_ion_globals_serializer_exit(void)
{
php_ion_serializer *s = &php_ion_globals.serializer;
ZEND_ASSERT(s->level);
if (!--s->level) {
zend_hash_clean(s->ids);
zend_hash_clean(s->tmp);
}
return s->level;
}
static void php_ion_globals_serializer_dtor(void)
{
php_ion_serializer *s = &php_ion_globals.serializer;
zend_hash_destroy(s->tmp);
zend_hash_destroy(s->ids);
}
void ZVAL_ADDREF(zval *zv)
{
if (Z_ISREF_P(zv)) {
Z_TRY_ADDREF_P(Z_REFVAL_P(zv));
} else {
Z_TRY_ADDREF_P(zv);
}
}
static void php_ion_globals_unserializer_init(void)
{
php_ion_unserializer *s = &php_ion_globals.unserializer;
HashTable *h = php_ion_globals._ht.unserializer;
zend_hash_init(s->tmp = &h[0], 0, NULL, ZVAL_PTR_DTOR, 0);
zend_hash_init(s->ids = &h[1], 0, NULL, NULL, 0);
zend_hash_init(s->addref = &h[2], 0, NULL, ZVAL_ADDREF, 0);
}
static void php_ion_globals_unserializer_step(void)
{
php_ion_unserializer *s = &php_ion_globals.unserializer;
if (!s->level++) {
zend_hash_clean(s->addref);
zend_hash_clean(s->ids);
zend_hash_clean(s->tmp);
}
}
static void php_ion_globals_unserializer_exit(void)
{
php_ion_unserializer *s = &php_ion_globals.unserializer;
ZEND_ASSERT(s->level);
if (!--s->level) {
zend_hash_clean(s->addref);
zend_hash_clean(s->ids);
zend_hash_clean(s->tmp);
}
}
static void php_ion_globals_unserializer_dtor(void)
{
php_ion_unserializer *s = &php_ion_globals.unserializer;
zend_hash_destroy(s->addref);
zend_hash_destroy(s->ids);
zend_hash_destroy(s->tmp);
}
#define php_ion_obj(type, zo) \
((php_ion_ ##type *) php_ion_obj_ex(zo, XtOffsetOf(php_ion_ ## type, std)))
static void *php_ion_obj_ex(void *obj, ptrdiff_t offset) {
if (obj) {
return ((char *) obj) - offset;
}
return NULL;
}
#define php_ion_decl_noclone(type, cname) \
static zend_object_handlers oh_ ## cname; \
static zend_object *create_ion_ ## cname(zend_class_entry *ce) \
{ \
if (!ce) ce = ce_ ## cname; \
php_ion_ ## type *o = ecalloc(1, sizeof(*o) + zend_object_properties_size(ce)); \
zend_object_std_init(&o->std, ce); \
object_properties_init(&o->std, ce); \
o->std.handlers = &oh_ ## cname; \
return &o->std; \
} \
static void free_ion_ ## cname(zend_object *std) \
{ \
php_ion_ ## type *obj = php_ion_obj(type, std); \
php_ion_ ## type ## _dtor(obj); \
zend_object_std_dtor(std); \
}
#define php_ion_decl(type, cname) \
php_ion_decl_noclone(type, cname) \
static zend_object *clone_ion_ ## cname(zend_object *std) \
{ \
php_ion_ ## type *old_obj = php_ion_obj(type, std), \
*new_obj = php_ion_obj(type, create_ion_ ## cname(std->ce)); \
php_ion_ ## type ## _copy(new_obj, old_obj); \
(void) old_obj; \
return &new_obj->std; \
}
#define php_ion_register(type, cname, ...) do { \
ce_ ## cname = register_class_ion_ ## cname(__VA_ARGS__); \
ce_ ## cname ->create_object = create_ion_ ## cname; \
memcpy(&oh_ ## cname, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); \
oh_ ## cname .offset = offsetof(php_ion_ ## type, std); \
oh_ ## cname .free_obj = free_ion_ ## cname; \
oh_ ## cname .clone_obj = clone_ion_ ## cname; \
} while (0)
#define ION_CHECK_RETURN(r, err, ...) do { \
iERR __err = err; \
if (UNEXPECTED(__err)) { \
zend_throw_exception_ex(ce_Exception, __err, "%s: %s", ion_error_to_str(__err), #err); \
__VA_ARGS__; \
return r; \
} \
} while (0)
#define ION_CHECK(err, ...) \
ION_CHECK_RETURN(, err, __VA_ARGS__)
#define ION_CATCH_RETURN(ret, ...) do { \
if (UNEXPECTED(EG(exception))) { \
__VA_ARGS__; \
return ret; \
} \
} while (0)
#define ION_CATCH(...) \
ION_CATCH_RETURN(, __VA_ARGS__)
#define PTR_CHECK_RETURN(ret, ptr, ...) do { \
if (UNEXPECTED(!(ptr))) { \
zend_throw_error(NULL, "Uninitialized object"); \
__VA_ARGS__; \
return ret; \
} \
} while (0)
#define PTR_CHECK(ptr, ...) PTR_CHECK_RETURN(, ptr, __VA_ARGS__)
#define OBJ_CHECK_RETURN(ret, obj, ...) do { \
PTR_CHECK_RETURN(ret, obj, __VA_ARGS__); \
PTR_CHECK_RETURN(ret, *((void **)obj), __VA_ARGS__); \
} while (0)
#define OBJ_CHECK(obj, ...) OBJ_CHECK_RETURN(, obj, __VA_ARGS__)
static ION_STRING *ion_string_from_zend(ION_STRING *is, const zend_string *zs)
{
is->length = zs ? (SIZE) zs->len : 0;
is->value = (BYTE *) (zs ? zs->val : NULL);
return is;
}
static zend_string *zend_string_from_ion(const ION_STRING *s)
{
return zend_string_init((const char *) s->value, s->length, 0);
}
static void call_constructor(zend_object *zo, uint32_t argc, zval *argv, zend_array *args_named)
{
zend_call_known_function(zo->ce->constructor, zo, zo->ce, NULL, argc, argv, args_named);
}
static zend_object *object_construct(zend_class_entry *ce, uint32_t argc, zval *argv, zend_array *args_named)
{
zval z_obj;
object_init_ex(&z_obj, ce);
call_constructor(Z_OBJ(z_obj), argc, argv, args_named);
return Z_OBJ(z_obj);
}
static void update_property_obj_ex(zend_class_entry *scope, zend_object *obj, const char *n, size_t l, zend_object *p)
{
zval zobj;
ZVAL_OBJ(&zobj, p);
zend_update_property(scope, obj, n, l, &zobj);
}
static void update_property_obj(zend_object *obj, const char *n, size_t l, zend_object *p)
{
update_property_obj_ex(obj->ce, obj, n, l, p);
}
#define RETURN_IONTYPE(typ) do { \
zend_object *__zo = php_ion_type_fetch(typ); \
if (UNEXPECTED(!__zo)) { \
RETURN_THROWS(); \
} \
RETURN_OBJ_COPY(__zo); \
} while(0)
static zend_object *php_ion_type_fetch(ION_TYPE typ)
{
zend_long index = ION_TYPE_INT(typ);
zval *ztype = zend_hash_index_find(ce_Type->backed_enum_table, index);
if (UNEXPECTED(!ztype || Z_TYPE_P(ztype) != IS_STRING)) {
zend_value_error(ZEND_LONG_FMT " is not a valid backing value for enum \"%s\"", index, ZSTR_VAL(ce_Type->name));
return NULL;
}
return zend_enum_get_case(ce_Type, Z_STR_P(ztype));
}
static ION_TYPE ion_type_from_enum(zend_object *zo)
{
return (ION_TYPE) Z_LVAL_P(zend_enum_fetch_case_value(zo));
}
typedef struct php_ion_symbol_iloc {
ION_SYMBOL_IMPORT_LOCATION loc;
zend_string *name;
zend_object std;
} php_ion_symbol_iloc;
static void php_ion_symbol_iloc_ctor(php_ion_symbol_iloc *obj)
{
zend_update_property_long(ce_Symbol_ImportLocation, &obj->std, ZEND_STRL("location"), obj->loc.location);
zend_update_property_str(ce_Symbol_ImportLocation, &obj->std, ZEND_STRL("name"), obj->name);
ion_string_from_zend(&obj->loc.name, obj->name);
}
static void php_ion_symbol_iloc_dtor(php_ion_symbol_iloc *obj)
{
zend_string_release(obj->name);
}
static void php_ion_symbol_iloc_copy(php_ion_symbol_iloc *new_obj, php_ion_symbol_iloc *old_obj)
{
zend_objects_clone_members(&new_obj->std, &old_obj->std);
new_obj->name = zend_string_copy(old_obj->name);
ion_string_from_zend(&new_obj->loc.name, new_obj->name);
new_obj->loc.location = old_obj->loc.location;
}
php_ion_decl(symbol_iloc, Symbol_ImportLocation);
typedef struct php_ion_symbol {
ION_SYMBOL sym;
zend_string *value;
zend_object *iloc, std;
} php_ion_symbol;
static int php_ion_symbol_zval_compare(zval *zv1, zval *zv2) {
zend_string *zs1 = zval_get_string(zv1);
zend_string *zs2 = zval_get_string(zv2);
if (EG(exception)) {
return 0;
}
int result;
if (zs1->len > zs2->len) {
result = 1;
} else if (zs2->len > zs1->len) {
result = -1;
} else {
result = memcmp(zs1->val, zs2->val, zs1->len);
}
zend_string_release(zs1);
zend_string_release(zs2);
return result;
}
static void php_ion_symbol_ctor(php_ion_symbol *obj)
{
zend_update_property_long(ce_Symbol, &obj->std, ZEND_STRL("sid"),
obj->sym.sid);
if (obj->value) {
zend_update_property_str(ce_Symbol, &obj->std, ZEND_STRL("value"), obj->value);
} else{
zend_update_property_null(ce_Symbol, &obj->std, ZEND_STRL("value"));
}
ion_string_from_zend(&obj->sym.value, obj->value);
if (obj->iloc) {
update_property_obj(&obj->std, ZEND_STRL("importLocation"), obj->iloc);
obj->sym.import_location = php_ion_obj(symbol_iloc, obj->iloc)->loc;
} else {
zend_update_property_null(ce_Symbol, &obj->std, ZEND_STRL("importLocation"));
}
}
static void php_ion_symbol_dtor(php_ion_symbol *obj)
{
if (obj->value) {
zend_string_release(obj->value);
}
}
static void php_ion_symbol_copy(php_ion_symbol *new_obj, php_ion_symbol *old_obj)
{
zend_objects_clone_members(&new_obj->std, &old_obj->std);
new_obj->sym = old_obj->sym;
if (old_obj->value) {
new_obj->value = zend_string_copy(old_obj->value);
ion_string_from_zend(&new_obj->sym.value, new_obj->value);
}
if ((new_obj->iloc = old_obj->iloc)) {
new_obj->sym.import_location = php_ion_obj(symbol_iloc, new_obj->iloc)->loc;
}
}
static void php_ion_symbol_zval(ION_SYMBOL *sym_ptr, zval *return_value)
{
object_init_ex(return_value, ce_Symbol);
php_ion_symbol *sym = php_ion_obj(symbol, Z_OBJ_P(return_value));
sym->sym.sid = sym_ptr->sid;
sym->value = zend_string_from_ion(&sym_ptr->value);
if (!ION_SYMBOL_IMPORT_LOCATION_IS_NULL(sym_ptr)) {
zval ziloc;
object_init_ex(&ziloc, ce_Symbol_ImportLocation);
sym->iloc = Z_OBJ(ziloc);
php_ion_symbol_iloc *iloc = php_ion_obj(symbol_iloc, sym->iloc);
iloc->loc.location = sym_ptr->import_location.location;
iloc->name = zend_string_from_ion(&sym_ptr->import_location.name);
php_ion_symbol_iloc_ctor(iloc);
}
php_ion_symbol_ctor(sym);
if (!ION_SYMBOL_IMPORT_LOCATION_IS_NULL(sym_ptr)) {
GC_DELREF(sym->iloc);
}
}
static zval *php_ion_global_symbol_fetch_by_enum(zend_string *name)
{
zval *zgs = zend_hash_find(&php_ion_globals.symbol.cache, name);
if (!zgs) {
zval *zid = zend_hash_find(&g_sym_map, name);
if (zid) {
zval *zss = zend_hash_index_find(&g_sym_hash, Z_LVAL_P(zid));
if (zss) {
zval zsym;
object_init_ex(&zsym, ce_Symbol);
php_ion_symbol *sym = php_ion_obj(symbol, Z_OBJ(zsym));
sym->sym.sid = Z_LVAL_P(zid);
sym->value = zval_get_string(zss);
php_ion_symbol_ctor(sym);
zgs = zend_hash_add(&php_ion_globals.symbol.cache, name, &zsym);
}
}
}
return zgs;
}
php_ion_decl(symbol, Symbol);
typedef struct php_ion_symbol_table {
ION_SYMBOL_TABLE *tab;
int (*dtor)(ION_SYMBOL_TABLE *);
zend_object std;
} php_ion_symbol_table;
static void php_ion_symbol_table_ctor(php_ion_symbol_table *obj)
{
OBJ_CHECK(obj);
ION_SYMBOL_TABLE_TYPE typ = ist_EMPTY;
ion_symbol_table_get_type(obj->tab, &typ);
if (typ != ist_LOCAL) {
ION_STRING is;
if (IERR_OK == ion_symbol_table_get_name(obj->tab, &is)) {
zend_update_property_stringl(ce_Symbol_Table_Shared, &obj->std, ZEND_STRL("name"), (char *) is.value, is.length);
}
int32_t iv;
if (IERR_OK == ion_symbol_table_get_version(obj->tab, &iv)) {
zend_update_property_long(ce_Symbol_Table_Shared, &obj->std, ZEND_STRL("version"), iv);
}
}
}
static void php_ion_symbol_table_dtor(php_ion_symbol_table *obj)
{
if (obj->tab) {
if (obj->dtor) {
obj->dtor(obj->tab);
}
obj->tab = NULL;
}
}
static void php_ion_symbol_table_copy(php_ion_symbol_table *new_obj, php_ion_symbol_table *old_obj)
{
// do not clone cache members
// zend_objects_clone_members(...)
if ((new_obj->dtor = old_obj->dtor)) {
ION_CHECK(ion_symbol_table_clone_with_owner(old_obj->tab, &new_obj->tab, NULL));
} else {
new_obj->tab = old_obj->tab;
}
// update non-cache members
php_ion_symbol_table_ctor(new_obj);
}
static void php_ion_symbol_table_import(php_ion_symbol_table *obj, php_ion_symbol_table *import)
{
OBJ_CHECK(obj);
OBJ_CHECK(import);
zval tmp;
zval *zimports = zend_read_property(obj->std.ce, &obj->std, ZEND_STRL("imports"), 0, &tmp);
if (zimports) {
zend_ulong idx = (uintptr_t) &import->std.gc;
if (!zend_hash_index_exists(Z_ARRVAL_P(zimports), idx)) {
ION_CHECK(ion_symbol_table_import_symbol_table(obj->tab, import->tab));
SEPARATE_ARRAY(zimports);
GC_ADDREF(&import->std);
add_index_object(zimports, idx, &import->std);
zend_update_property(obj->std.ce, &obj->std, ZEND_STRL("imports"), zimports);
}
}
}
static void php_ion_symbol_table_symbol_zval(php_ion_symbol_table *obj, ION_SYMBOL *sym, zval *return_value)
{
zval tmp;
zval *zsyms = zend_read_property(obj->std.ce, &obj->std, ZEND_STRL("symbols"), 0, &tmp);
if (zsyms) {
zval *zsym = zend_hash_index_find(Z_ARRVAL_P(zsyms), sym->sid);
if (zsym) {
RETURN_COPY(zsym);
}
}
php_ion_symbol_zval(sym, return_value);
if (zsyms) {
SEPARATE_ARRAY(zsyms);
ZVAL_ADDREF(return_value);
add_index_zval(zsyms, sym->sid, return_value);
}
}
php_ion_decl(symbol_table, Symbol_Table);
typedef struct php_ion_decimal_ctx {
decContext ctx;
zend_object std;
} php_ion_decimal_ctx;
#define php_ion_decimal_ctx_init_max(c, rounding) \
php_ion_decimal_ctx_init((c), DEC_MAX_DIGITS, DEC_MAX_EMAX, DEC_MIN_EMIN, (rounding), false)
static void php_ion_decimal_ctx_init(decContext *ctx,
int digits, int emax, int emin, enum rounding round, zend_bool clamp)
{
memset(ctx, 0, sizeof(*ctx));
ctx->digits = digits;
ctx->emax = emax;
ctx->emin = emin;
ctx->round = round;
ctx->clamp = clamp;
}
static void php_ion_decimal_ctx_ctor(php_ion_decimal_ctx *obj, zend_object *o_round)
{
if (!obj->ctx.digits) {
php_ion_decimal_ctx_init_max(&obj->ctx, DEC_ROUND_HALF_EVEN);
}
if (o_round) {
update_property_obj(&obj->std, ZEND_STRL("round"), o_round);
} else {
zend_update_property_long(obj->std.ce, &obj->std, ZEND_STRL("round"), obj->ctx.round);
}
zend_update_property_long(ce_Decimal_Context, &obj->std, ZEND_STRL("digits"), obj->ctx.digits);
zend_update_property_long(ce_Decimal_Context, &obj->std, ZEND_STRL("eMax"), obj->ctx.emax);
zend_update_property_long(ce_Decimal_Context, &obj->std, ZEND_STRL("eMin"), obj->ctx.emin);
zend_update_property_bool(ce_Decimal_Context, &obj->std, ZEND_STRL("clamp"), obj->ctx.clamp);
}
static void php_ion_decimal_ctx_dtor(php_ion_decimal_ctx *obj)
{
}
static void php_ion_decimal_ctx_copy(php_ion_decimal_ctx *new_obj, php_ion_decimal_ctx *old_obj)
{
zend_objects_clone_members(&new_obj->std, &old_obj->std);
new_obj->ctx = old_obj->ctx;
}
php_ion_decl(decimal_ctx, Decimal_Context);
typedef struct php_ion_decimal {
ION_DECIMAL dec;
zend_object *ctx, std;
} php_ion_decimal;
static void php_ion_decimal_from_zend_long(ION_DECIMAL *dec, decContext *ctx, zend_long num)
{
if (num <= INT32_MAX && num >= INT32_MIN) {
ION_CHECK(ion_decimal_from_int32(dec, num));
} else if (num > 0 && num <= UINT32_MAX) {
ION_CHECK(ion_decimal_from_uint32(dec, num));
} else {
ION_INT *iint;
ION_CHECK(ion_int_alloc(NULL, &iint));
ION_CHECK(ion_int_from_long(iint, num),
ion_int_free(iint));
/* WATCH OUT: BS API */
dec->type = ION_DECIMAL_TYPE_QUAD;
ION_CHECK(ion_decimal_from_ion_int(dec, ctx, iint),
ion_int_free(iint));
ion_int_free(iint);
}
}
static zend_string *php_ion_decimal_to_string(ION_DECIMAL *dec)
{
zend_string *zstr = zend_string_alloc(ION_DECIMAL_STRLEN(dec), 0);
(void) ion_decimal_to_string(dec, zstr->val);
return zend_string_truncate(zstr, strlen(zstr->val), 0);
}
static void php_ion_decimal_to_zend_long(ION_DECIMAL *dec, decContext *ctx, zend_long *l)
{
ION_INT *ii = NULL;
ION_CHECK(ion_int_alloc(NULL, &ii));
ION_CHECK(ion_decimal_to_ion_int(dec, ctx, ii), ion_int_free(ii));
int64_t i64;
ION_CHECK(ion_int_to_int64(ii, &i64), ion_int_free(ii));
*l = i64;
ion_int_free(ii);
}
static bool php_ion_decimal_fits_zend_long(php_ion_decimal *obj)
{
int32_t result;
if (!ion_decimal_is_integer(&obj->dec)) {
return false;
}
result = 1;
ion_decimal_compare(&obj->dec, &php_ion_globals.decimal.zend_max, &php_ion_globals.decimal.ctx, &result);
if (result == 1) {
return false;
}
result = -1;
ion_decimal_compare(&obj->dec, &php_ion_globals.decimal.zend_min, &php_ion_globals.decimal.ctx, &result);
if (result == -1) {
return false;
}
return true;
}
static void php_ion_decimal_ctor(php_ion_decimal *obj)
{
if (!obj->ctx) {
zval zdc;
object_init_ex(&zdc, ce_Decimal_Context);
obj->ctx = Z_OBJ(zdc);
php_ion_decimal_ctx_ctor(php_ion_obj(decimal_ctx, obj->ctx), NULL);
GC_DELREF(obj->ctx);
}
update_property_obj(&obj->std, ZEND_STRL("context"), obj->ctx);
if (php_ion_decimal_fits_zend_long(obj)) {
zend_long l;
php_ion_decimal_to_zend_long(&obj->dec, &php_ion_obj(decimal_ctx, obj->ctx)->ctx, &l);
zend_update_property_long(ce_Decimal, &obj->std, ZEND_STRL("number"), l);
} else {
zend_string *zstr = php_ion_decimal_to_string(&obj->dec);
zend_update_property_str(ce_Decimal, &obj->std, ZEND_STRL("number"), zstr);
zend_string_release(zstr);
}
}
static void php_ion_decimal_dtor(php_ion_decimal *obj)
{
ion_decimal_free(&obj->dec);
}
static void php_ion_decimal_copy(php_ion_decimal *new_obj, php_ion_decimal *old_obj)
{
zend_objects_clone_members(&new_obj->std, &old_obj->std);
new_obj->ctx = old_obj->ctx;
ION_CHECK(ion_decimal_copy(&new_obj->dec, &old_obj->dec));
}
php_ion_decl(decimal, Decimal);
typedef php_date_obj php_ion_timestamp;
static zend_long php_usec_from_ion(const decQuad *frac, decContext *ctx)
{
if (!ctx) {
ctx = &php_ion_globals.decimal.ctx;
}
decQuad microsecs, result;
decQuadMultiply(&result, decQuadFromInt32(µsecs, 1000000), frac, ctx);
return (zend_long) decQuadToUInt32(&result, ctx, DEC_ROUND_HALF_EVEN);
}
static decQuad *ion_ts_frac_from_usec(decQuad *frac, int usec, decContext *ctx)
{
if (!ctx) {
ctx = &php_ion_globals.decimal.ctx;
}
decQuad microsecs, us;
return decQuadDivide(frac, decQuadFromInt32(&us, usec), decQuadFromInt32(µsecs, 1000000), ctx);
}
static zend_string *php_ion_timestamp_format_fetch(zend_string *fmt_case)
{
return Z_STR_P(zend_enum_fetch_case_value(zend_enum_get_case(ce_Timestamp_Format, fmt_case)));
}
static zend_string *php_dt_format_from_precision(uint8_t precision)
{
switch (precision) {
case ION_TS_FRAC | 0x80:
return php_ion_timestamp_format_fetch(g_intern_str.FracTZ);
case ION_TS_FRAC:
return php_ion_timestamp_format_fetch(g_intern_str.Frac);
case ION_TS_SEC | 0x80:
return php_ion_timestamp_format_fetch(g_intern_str.SecTZ);
case ION_TS_SEC:
return php_ion_timestamp_format_fetch(g_intern_str.Sec);
case ION_TS_MIN | 0x80:
return php_ion_timestamp_format_fetch(g_intern_str.MinTZ);
case ION_TS_MIN:
return php_ion_timestamp_format_fetch(g_intern_str.Min);
case ION_TS_DAY:
return php_ion_timestamp_format_fetch(g_intern_str.Day);
case ION_TS_MONTH:
return php_ion_timestamp_format_fetch(g_intern_str.Month);
case ION_TS_YEAR:
return php_ion_timestamp_format_fetch(g_intern_str.Year);
default:
return ZSTR_CHAR('c');
}
}
static timelib_time* php_time_from_ion(const ION_TIMESTAMP *ts, decContext *ctx, zend_string **fmt)
{
timelib_time *time = ecalloc(1, sizeof(*time));
/* defaults */
time->y = 1970;
time->m = 1;
time->d = 1;
switch (ts->precision & 0x7f) {
case ION_TS_FRAC:
time->us = php_usec_from_ion(&ts->fraction, ctx);
/* fallthrough */
case ION_TS_SEC:
time->s = ts->seconds;
/* fallthrough */
case ION_TS_MIN:
time->i = ts->minutes;
time->h = ts->hours;
/* fallthrough */
case ION_TS_DAY:
time->d = ts->day;
/* fallthrough */
case ION_TS_MONTH:
time->m = ts->month;
/* fallthrough */
case ION_TS_YEAR:
time->y = ts->year;
/* fallthrough */
default:
time->z = ts->tz_offset * 60;
if (time->z || ts->precision & 0x80) {
time->zone_type = TIMELIB_ZONETYPE_OFFSET;
} else {
time->zone_type = TIMELIB_ZONETYPE_ID;
time->tz_info = get_timezone_info();
}
}
if (fmt) {
*fmt = php_dt_format_from_precision(ts->precision);
}
return time;
}
static ION_TIMESTAMP *ion_timestamp_from_php(ION_TIMESTAMP *buf, php_ion_timestamp *ts, decContext *ctx)
{
memset(buf, 0, sizeof(*buf));
zval tmp;
int precision = Z_LVAL_P(zend_read_property(ts->std.ce, &ts->std, ZEND_STRL("precision"), 0, &tmp));
if (!precision || precision > (ION_TS_FRAC|0x80)) {
zend_throw_exception_ex(ce_Exception, IERR_INVALID_ARG,
"Invalid precision (%d) of ion\\Timestamp", precision);
} else switch ((buf->precision = precision) & 0x7f) {
case ION_TS_FRAC:
ion_ts_frac_from_usec(&buf->fraction, (int) ts->time->us, ctx);
/* fallthrough */
case ION_TS_SEC:
buf->seconds = ts->time->s;
/* fallthrough */
case ION_TS_MIN:
buf->minutes = ts->time->i;
/* fallthrough */