This repository has been archived by the owner on Oct 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
igbinary.c
2575 lines (2220 loc) · 76.6 KB
/
igbinary.c
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
/*
+----------------------------------------------------------------------+
| See COPYING file for further copyright information |
+----------------------------------------------------------------------+
| Author: Oleg Grenrus <[email protected]> |
| See CREDITS for contributors |
+----------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef PHP_WIN32
# include "ig_win32.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "zend_alloc.h"
#include "ext/standard/info.h"
#include "ext/standard/php_var.h"
#if PHP_MAJOR_VERSION >= 7
/* FIXME: still fix sessions and the APC-thingy */
# undef HAVE_PHP_SESSION
# undef HAVE_APC_SUPPORT
# undef HAVE_APCU_SUPPORT
#endif
#if HAVE_PHP_SESSION
# include "ext/session/php_session.h"
#endif /* HAVE_PHP_SESSION */
#include "ext/standard/php_incomplete_class.h"
#if defined(HAVE_APCU_SUPPORT)
# include "ext/apcu/apc_serializer.h"
#elif defined(HAVE_APC_SUPPORT)
# if USE_BUNDLED_APC
# include "apc_serializer.h"
# else
# include "ext/apc/apc_serializer.h"
# endif
#endif /* HAVE_APCU_SUPPORT || HAVE_APC_SUPPORT */
#include "php_igbinary.h"
#include "igbinary.h"
#include <assert.h>
#ifndef PHP_WIN32
# include <inttypes.h>
# include <stdbool.h>
# include <stdint.h>
#endif
#include <stddef.h>
#include "hash.h"
#include "hash_ptr.h"
#if HAVE_PHP_SESSION
/** Session serializer function prototypes. */
PS_SERIALIZER_FUNCS(igbinary);
#endif /* HAVE_PHP_SESSION */
#if defined(HAVE_APC_SUPPORT) || defined(HAVE_APCU_SUPPORT)
/** Apc serializer function prototypes */
static int APC_SERIALIZER_NAME(igbinary) (APC_SERIALIZER_ARGS);
static int APC_UNSERIALIZER_NAME(igbinary) (APC_UNSERIALIZER_ARGS);
#endif
/* {{{ Types */
enum igbinary_type {
/* 00 */ igbinary_type_null, /**< Null. */
/* 01 */ igbinary_type_ref8, /**< Array reference. */
/* 02 */ igbinary_type_ref16, /**< Array reference. */
/* 03 */ igbinary_type_ref32, /**< Array reference. */
/* 04 */ igbinary_type_bool_false, /**< Boolean true. */
/* 05 */ igbinary_type_bool_true, /**< Boolean false. */
/* 06 */ igbinary_type_long8p, /**< Long 8bit positive. */
/* 07 */ igbinary_type_long8n, /**< Long 8bit negative. */
/* 08 */ igbinary_type_long16p, /**< Long 16bit positive. */
/* 09 */ igbinary_type_long16n, /**< Long 16bit negative. */
/* 0a */ igbinary_type_long32p, /**< Long 32bit positive. */
/* 0b */ igbinary_type_long32n, /**< Long 32bit negative. */
/* 0c */ igbinary_type_double, /**< Double. */
/* 0d */ igbinary_type_string_empty, /**< Empty string. */
/* 0e */ igbinary_type_string_id8, /**< String id. */
/* 0f */ igbinary_type_string_id16, /**< String id. */
/* 10 */ igbinary_type_string_id32, /**< String id. */
/* 11 */ igbinary_type_string8, /**< String. */
/* 12 */ igbinary_type_string16, /**< String. */
/* 13 */ igbinary_type_string32, /**< String. */
/* 14 */ igbinary_type_array8, /**< Array. */
/* 15 */ igbinary_type_array16, /**< Array. */
/* 16 */ igbinary_type_array32, /**< Array. */
/* 17 */ igbinary_type_object8, /**< Object. */
/* 18 */ igbinary_type_object16, /**< Object. */
/* 19 */ igbinary_type_object32, /**< Object. */
/* 1a */ igbinary_type_object_id8, /**< Object string id. */
/* 1b */ igbinary_type_object_id16, /**< Object string id. */
/* 1c */ igbinary_type_object_id32, /**< Object string id. */
/* 1d */ igbinary_type_object_ser8, /**< Object serialized data. */
/* 1e */ igbinary_type_object_ser16, /**< Object serialized data. */
/* 1f */ igbinary_type_object_ser32, /**< Object serialized data. */
/* 20 */ igbinary_type_long64p, /**< Long 64bit positive. */
/* 21 */ igbinary_type_long64n, /**< Long 64bit negative. */
/* 22 */ igbinary_type_objref8, /**< Object reference. */
/* 23 */ igbinary_type_objref16, /**< Object reference. */
/* 24 */ igbinary_type_objref32, /**< Object reference. */
/* 25 */ igbinary_type_ref, /**< Simple reference */
};
/** Serializer data.
* @author Oleg Grenrus <[email protected]>
*/
struct igbinary_serialize_data {
uint8_t *buffer; /**< Buffer. */
size_t buffer_size; /**< Buffer size. */
size_t buffer_capacity; /**< Buffer capacity. */
bool scalar; /**< Serializing scalar. */
bool compact_strings; /**< Check for duplicate strings. */
struct hash_si strings; /**< Hash of already serialized strings. */
struct hash_si_ptr references; /**< Hash of already serialized potential references. (non-NULL uintptr_t => int32_t) */
int references_id; /**< Number of things that the unserializer might think are references. >= length of references */
int string_count; /**< Serialized string count, used for back referencing */
int error; /**< Error number. Not used. */
struct igbinary_memory_manager mm; /**< Memory management functions. */
};
/** String/len pair for the igbinary_unserializer_data.
* @author Oleg Grenrus <[email protected]>
* @see igbinary_unserialize_data.
*/
struct igbinary_unserialize_string_pair {
char *data; /**< Data. */
size_t len; /**< Data length. */
};
/** Unserializer data.
* @author Oleg Grenrus <[email protected]>
*/
struct igbinary_unserialize_data {
uint8_t *buffer; /**< Buffer. */
size_t buffer_size; /**< Buffer size. */
size_t buffer_offset; /**< Current read offset. */
struct igbinary_unserialize_string_pair *strings; /**< Unserialized strings. */
size_t strings_count; /**< Unserialized string count. */
size_t strings_capacity; /**< Unserialized string array capacity. */
zval **references; /**< Unserialized Arrays/Objects. */
size_t references_count; /**< Unserialized array/objects count. */
size_t references_capacity; /**< Unserialized array/object array capacity. */
zval *wakeup; /**< zvals of type IS_OBJECT for calls to __wakeup. */
size_t wakeup_count; /**< count of objects in array for calls to __wakeup */
size_t wakeup_capacity; /**< capacity of objects in array for calls to __wakeup */
int error; /**< Error number. Not used. */
smart_string string0_buf; /**< Temporary buffer for strings */
};
#define IGB_REF_VAL(igsd, n) ((igsd)->references[(n)])
#define WANT_CLEAR (0)
#define WANT_OBJECT (1<<0)
#define WANT_REF (1<<1)
/* }}} */
/* {{{ Memory allocator wrapper prototypes */
static inline void *igbinary_mm_wrapper_malloc(size_t size, void *context);
static inline void *igbinary_mm_wrapper_realloc(void *ptr, size_t size, void *context);
static inline void igbinary_mm_wrapper_free(void *ptr, void *context);
/* }}} */
/* {{{ Serializing functions prototypes */
inline static int igbinary_serialize_data_init(struct igbinary_serialize_data *igsd, bool scalar, struct igbinary_memory_manager *memory_manager TSRMLS_DC);
inline static void igbinary_serialize_data_deinit(struct igbinary_serialize_data *igsd, int free_buffer TSRMLS_DC);
inline static int igbinary_serialize_header(struct igbinary_serialize_data *igsd TSRMLS_DC);
inline static int igbinary_serialize8(struct igbinary_serialize_data *igsd, uint8_t i TSRMLS_DC);
inline static int igbinary_serialize16(struct igbinary_serialize_data *igsd, uint16_t i TSRMLS_DC);
inline static int igbinary_serialize32(struct igbinary_serialize_data *igsd, uint32_t i TSRMLS_DC);
inline static int igbinary_serialize64(struct igbinary_serialize_data *igsd, uint64_t i TSRMLS_DC);
inline static int igbinary_serialize_null(struct igbinary_serialize_data *igsd TSRMLS_DC);
inline static int igbinary_serialize_bool(struct igbinary_serialize_data *igsd, int b TSRMLS_DC);
inline static int igbinary_serialize_long(struct igbinary_serialize_data *igsd, zend_long l TSRMLS_DC);
inline static int igbinary_serialize_double(struct igbinary_serialize_data *igsd, double d TSRMLS_DC);
inline static int igbinary_serialize_string(struct igbinary_serialize_data *igsd, char *s, size_t len TSRMLS_DC);
inline static int igbinary_serialize_chararray(struct igbinary_serialize_data *igsd, const char *s, size_t len TSRMLS_DC);
inline static int igbinary_serialize_array(struct igbinary_serialize_data *igsd, zval *z, bool object, bool incomplete_class TSRMLS_DC);
inline static int igbinary_serialize_array_ref(struct igbinary_serialize_data *igsd, zval *z, bool object TSRMLS_DC);
inline static int igbinary_serialize_array_sleep(struct igbinary_serialize_data *igsd, zval *z, HashTable *ht, zend_class_entry *ce, bool incomplete_class TSRMLS_DC);
inline static int igbinary_serialize_object_name(struct igbinary_serialize_data *igsd, const char *name, size_t name_len TSRMLS_DC);
inline static int igbinary_serialize_object(struct igbinary_serialize_data *igsd, zval *z TSRMLS_DC);
static int igbinary_serialize_zval(struct igbinary_serialize_data *igsd, zval *z TSRMLS_DC);
/* }}} */
/* {{{ Unserializing functions prototypes */
inline static int igbinary_unserialize_data_init(struct igbinary_unserialize_data *igsd TSRMLS_DC);
inline static void igbinary_unserialize_data_deinit(struct igbinary_unserialize_data *igsd TSRMLS_DC);
inline static int igbinary_unserialize_header(struct igbinary_unserialize_data *igsd TSRMLS_DC);
inline static uint8_t igbinary_unserialize8(struct igbinary_unserialize_data *igsd TSRMLS_DC);
inline static uint16_t igbinary_unserialize16(struct igbinary_unserialize_data *igsd TSRMLS_DC);
inline static uint32_t igbinary_unserialize32(struct igbinary_unserialize_data *igsd TSRMLS_DC);
inline static uint64_t igbinary_unserialize64(struct igbinary_unserialize_data *igsd TSRMLS_DC);
inline static int igbinary_unserialize_long(struct igbinary_unserialize_data *igsd, enum igbinary_type t, zend_long *ret TSRMLS_DC);
inline static int igbinary_unserialize_double(struct igbinary_unserialize_data *igsd, enum igbinary_type t, double *ret TSRMLS_DC);
inline static int igbinary_unserialize_string(struct igbinary_unserialize_data *igsd, enum igbinary_type t, char **s, size_t *len TSRMLS_DC);
inline static int igbinary_unserialize_chararray(struct igbinary_unserialize_data *igsd, enum igbinary_type t, char **s, size_t *len TSRMLS_DC);
inline static int igbinary_unserialize_array(struct igbinary_unserialize_data *igsd, enum igbinary_type t, zval *const z, int flags TSRMLS_DC);
inline static int igbinary_unserialize_object(struct igbinary_unserialize_data *igsd, enum igbinary_type t, zval *const z, int flags TSRMLS_DC);
inline static int igbinary_unserialize_object_ser(struct igbinary_unserialize_data *igsd, enum igbinary_type t, zval *const z, zend_class_entry *ce TSRMLS_DC);
inline static int igbinary_unserialize_ref(struct igbinary_unserialize_data *igsd, enum igbinary_type t, zval *const z, int flags TSRMLS_DC);
static int igbinary_unserialize_zval(struct igbinary_unserialize_data *igsd, zval *const z, int flags TSRMLS_DC);
/* }}} */
/* {{{ arginfo */
ZEND_BEGIN_ARG_INFO_EX(arginfo_igbinary_serialize, 0, 0, 1)
ZEND_ARG_INFO(0, value)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_igbinary_unserialize, 0, 0, 1)
ZEND_ARG_INFO(0, str)
ZEND_END_ARG_INFO()
/* }}} */
/* {{{ igbinary_functions[] */
/** Exported php functions. */
zend_function_entry igbinary_functions[] = {
PHP_FE(igbinary_serialize, arginfo_igbinary_serialize)
PHP_FE(igbinary_unserialize, arginfo_igbinary_unserialize)
PHP_FE_END
};
/* }}} */
/* {{{ igbinary dependencies */
#if ZEND_MODULE_API_NO >= 20050922
static const zend_module_dep igbinary_module_deps[] = {
ZEND_MOD_REQUIRED("standard")
#ifdef HAVE_PHP_SESSION
ZEND_MOD_REQUIRED("session")
#endif
#if defined(HAVE_APCU_SUPPORT)
ZEND_MOD_OPTIONAL("apcu")
#elif defined(HAVE_APC_SUPPORT)
ZEND_MOD_OPTIONAL("apc")
#endif
ZEND_MOD_END
};
#endif
/* }}} */
/* {{{ igbinary_module_entry */
zend_module_entry igbinary_module_entry = {
#if ZEND_MODULE_API_NO >= 20050922
STANDARD_MODULE_HEADER_EX, NULL,
igbinary_module_deps,
#elif ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
"igbinary",
igbinary_functions,
PHP_MINIT(igbinary),
PHP_MSHUTDOWN(igbinary),
NULL,
NULL,
PHP_MINFO(igbinary),
#if ZEND_MODULE_API_NO >= 20010901
PHP_IGBINARY_VERSION, /* Replace with version number for your extension */
#endif
STANDARD_MODULE_PROPERTIES
};
/* }}} */
ZEND_DECLARE_MODULE_GLOBALS(igbinary)
/* {{{ ZEND_GET_MODULE */
#ifdef COMPILE_DL_IGBINARY
ZEND_GET_MODULE(igbinary)
#endif
/* }}} */
/* {{{ INI entries */
PHP_INI_BEGIN()
STD_PHP_INI_BOOLEAN("igbinary.compact_strings", "1", PHP_INI_ALL, OnUpdateBool, compact_strings, zend_igbinary_globals, igbinary_globals)
PHP_INI_END()
/* }}} */
/* {{{ php_igbinary_init_globals */
static void php_igbinary_init_globals(zend_igbinary_globals *igbinary_globals) {
igbinary_globals->compact_strings = 1;
}
/* }}} */
/* {{{ PHP_MINIT_FUNCTION */
PHP_MINIT_FUNCTION(igbinary) {
(void) type;
(void) module_number;
ZEND_INIT_MODULE_GLOBALS(igbinary, php_igbinary_init_globals, NULL);
#if HAVE_PHP_SESSION
php_session_register_serializer("igbinary",
PS_SERIALIZER_ENCODE_NAME(igbinary),
PS_SERIALIZER_DECODE_NAME(igbinary));
#endif
#if defined(HAVE_APC_SUPPORT) || defined(HAVE_APCU_SUPPORT)
apc_register_serializer("igbinary",
APC_SERIALIZER_NAME(igbinary),
APC_UNSERIALIZER_NAME(igbinary),
NULL TSRMLS_CC);
#endif
REGISTER_INI_ENTRIES();
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MSHUTDOWN_FUNCTION */
PHP_MSHUTDOWN_FUNCTION(igbinary) {
(void) type;
(void) module_number;
#ifdef ZTS
ts_free_id(igbinary_globals_id);
#endif
/*
* unregister serializer?
*/
UNREGISTER_INI_ENTRIES();
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION */
PHP_MINFO_FUNCTION(igbinary) {
(void) zend_module;
php_info_print_table_start();
php_info_print_table_row(2, "igbinary support", "enabled");
php_info_print_table_row(2, "igbinary version", PHP_IGBINARY_VERSION);
#if defined(HAVE_APCU_SUPPORT)
php_info_print_table_row(2, "igbinary APCU serializer ABI", APC_SERIALIZER_ABI);
#elif defined(HAVE_APC_SUPPORT)
php_info_print_table_row(2, "igbinary APC serializer ABI", APC_SERIALIZER_ABI);
#else
php_info_print_table_row(2, "igbinary APC serializer ABI", "no");
#endif
#if HAVE_PHP_SESSION
php_info_print_table_row(2, "igbinary session support", "yes");
#else
php_info_print_table_row(2, "igbinary session support", "no");
#endif
php_info_print_table_end();
DISPLAY_INI_ENTRIES();
}
/* }}} */
/* {{{ igsd management */
/* Append to list of references to take out later. Returns SIZE_MAX on allocation error. */
static inline size_t igsd_append_ref(struct igbinary_unserialize_data *igsd, zval *z)
{
size_t ref_n;
if (igsd->references_count + 1 >= igsd->references_capacity) {
while (igsd->references_count + 1 >= igsd->references_capacity) {
igsd->references_capacity *= 2;
}
igsd->references = erealloc(igsd->references, sizeof(igsd->references[0]) * igsd->references_capacity);
if (igsd->references == NULL) {
return SIZE_MAX;
}
}
ref_n = igsd->references_count++;
IGB_REF_VAL(igsd, ref_n) = z;
return ref_n;
}
static inline int igsd_defer_wakeup(struct igbinary_unserialize_data *igsd, zval* z) {
if (igsd->wakeup_count >= igsd->wakeup_capacity) {
if (igsd->wakeup_capacity == 0) {
igsd->wakeup_capacity = 2;
igsd->wakeup = emalloc(sizeof(igsd->wakeup[0]) * igsd->wakeup_capacity);
} else {
igsd->wakeup_capacity *= 2;
igsd->wakeup = erealloc(igsd->wakeup, sizeof(igsd->wakeup[0]) * igsd->wakeup_capacity);
if (igsd->wakeup == NULL) {
return 1;
}
}
}
ZVAL_COPY(&igsd->wakeup[igsd->wakeup_count], z);
igsd->wakeup_count++;
return 0;
}
/* }}} */
/* {{{ igbinary_finish_wakeup }}} */
static int igbinary_finish_wakeup(struct igbinary_unserialize_data* igsd TSRMLS_DC) {
if (igsd->wakeup_count == 0) { /* nothing to do */
return 0;
}
zval h;
zval f;
size_t i;
ZVAL_STRINGL(&f, "__wakeup", sizeof("__wakeup") - 1);
for (i = 0; i < igsd->wakeup_count; i++) {
call_user_function_ex(CG(function_table), &(igsd->wakeup[i]), &f, &h, 0, 0, 1, NULL TSRMLS_CC);
zval_ptr_dtor(&h);
if (EG(exception)) {
zval_dtor(&f);
return 1;
}
}
zval_dtor(&f);
return 0;
}
/* {{{ Memory allocator wrappers */
static inline void *igbinary_mm_wrapper_malloc(size_t size, void *context)
{
return emalloc(size);
}
static inline void *igbinary_mm_wrapper_realloc(void *ptr, size_t size, void *context)
{
return erealloc(ptr, size);
}
static inline void igbinary_mm_wrapper_free(void *ptr, void *context)
{
return efree(ptr);
}
/* }}} */
/* {{{ int igbinary_serialize(uint8_t**, size_t*, zval*) */
IGBINARY_API int igbinary_serialize(uint8_t **ret, size_t *ret_len, zval *z TSRMLS_DC) {
return igbinary_serialize_ex(ret, ret_len, z, NULL TSRMLS_CC);
}
/* }}} */
/* {{{ int igbinary_serialize_ex(uint8_t**, size_t*, zval*, igbinary_memory_manager*) */
IGBINARY_API int igbinary_serialize_ex(uint8_t **ret, size_t *ret_len, zval *z, struct igbinary_memory_manager *memory_manager TSRMLS_DC) {
struct igbinary_serialize_data igsd;
uint8_t *tmpbuf;
if (igbinary_serialize_data_init(&igsd, Z_TYPE_P(z) != IS_OBJECT && Z_TYPE_P(z) != IS_ARRAY, memory_manager TSRMLS_CC)) {
zend_error(E_WARNING, "igbinary_serialize: cannot init igsd");
return 1;
}
if (igbinary_serialize_header(&igsd TSRMLS_CC) != 0) {
zend_error(E_WARNING, "igbinary_serialize: cannot write header");
igbinary_serialize_data_deinit(&igsd, 1 TSRMLS_CC);
return 1;
}
if (igbinary_serialize_zval(&igsd, z TSRMLS_CC) != 0) {
igbinary_serialize_data_deinit(&igsd, 1 TSRMLS_CC);
return 1;
}
/* Explicit nul termination */
if (igbinary_serialize8(&igsd, 0 TSRMLS_CC) != 0) {
igbinary_serialize_data_deinit(&igsd, 1 TSRMLS_CC);
return 1;
}
/* shrink buffer to the real length, ignore errors */
tmpbuf = (uint8_t *) igsd.mm.realloc(igsd.buffer, igsd.buffer_size, igsd.mm.context);
if (tmpbuf != NULL) {
igsd.buffer = tmpbuf;
}
/* Set return values */
*ret_len = igsd.buffer_size - 1;
*ret = igsd.buffer;
igbinary_serialize_data_deinit(&igsd, 0 TSRMLS_CC);
return 0;
}
/* }}} */
/* {{{ int igbinary_unserialize(const uint8_t *, size_t, zval **) */
IGBINARY_API int igbinary_unserialize(const uint8_t *buf, size_t buf_len, zval *z TSRMLS_DC) {
struct igbinary_unserialize_data igsd;
igbinary_unserialize_data_init(&igsd TSRMLS_CC);
igsd.buffer = (uint8_t *) buf;
igsd.buffer_size = buf_len;
if (igbinary_unserialize_header(&igsd TSRMLS_CC)) {
igbinary_unserialize_data_deinit(&igsd TSRMLS_CC);
return 1;
}
if (igbinary_unserialize_zval(&igsd, z, WANT_CLEAR TSRMLS_CC)) {
igbinary_unserialize_data_deinit(&igsd TSRMLS_CC);
return 1;
}
if (igbinary_finish_wakeup(&igsd TSRMLS_CC)) {
igbinary_unserialize_data_deinit(&igsd TSRMLS_CC);
return 1;
}
igbinary_unserialize_data_deinit(&igsd TSRMLS_CC);
return 0;
}
/* }}} */
/* {{{ proto string igbinary_unserialize(mixed value) */
PHP_FUNCTION(igbinary_unserialize) {
char *string = NULL;
size_t string_len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &string, &string_len) == FAILURE) {
RETURN_NULL();
}
if (string_len <= 0) {
RETURN_FALSE;
}
if (igbinary_unserialize((uint8_t *) string, string_len, return_value TSRMLS_CC) != 0) {
/* FIXME: is this a good place? a catch all */
zval_ptr_dtor(return_value);
RETURN_NULL();
}
}
/* }}} */
/* {{{ proto mixed igbinary_serialize(string value) */
PHP_FUNCTION(igbinary_serialize) {
zval *z;
uint8_t *string;
size_t string_len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &z) == FAILURE) {
RETURN_NULL();
}
if (igbinary_serialize(&string, &string_len, z TSRMLS_CC) != 0) {
RETURN_NULL();
}
RETVAL_STRINGL((char *)string, string_len);
efree(string);
}
/* }}} */
#ifdef HAVE_PHP_SESSION
/* {{{ Serializer encode function */
PS_SERIALIZER_ENCODE_FUNC(igbinary)
{
struct igbinary_serialize_data igsd;
uint8_t *tmpbuf;
if (igbinary_serialize_data_init(&igsd, false, NULL TSRMLS_CC)) {
zend_error(E_WARNING, "igbinary_serialize: cannot init igsd");
return FAILURE;
}
if (igbinary_serialize_header(&igsd TSRMLS_CC) != 0) {
zend_error(E_WARNING, "igbinary_serialize: cannot write header");
igbinary_serialize_data_deinit(&igsd, 1 TSRMLS_CC);
return FAILURE;
}
if (igbinary_serialize_array(&igsd, PS(http_session_vars), false, false TSRMLS_CC) != 0) {
igbinary_serialize_data_deinit(&igsd, 1 TSRMLS_CC);
return FAILURE;
}
if (igbinary_serialize8(&igsd, 0 TSRMLS_CC) != 0) {
igbinary_serialize_data_deinit(&igsd, 1 TSRMLS_CC);
return FAILURE;
}
/* shrink buffer to the real length, ignore errors */
tmpbuf = (uint8_t *)igsd.mm.realloc(igsd.buffer, igsd.buffer_size, igsd.mm.context);
if (tmpbuf != NULL) {
igsd.buffer = tmpbuf;
}
*newstr = (char *)igsd.buffer;
if (newlen) {
*newlen = igsd.buffer_size - 1;
}
igbinary_serialize_data_deinit(&igsd, 0 TSRMLS_CC);
return SUCCESS;
}
/* }}} */
/* {{{ Serializer decode function */
PS_SERIALIZER_DECODE_FUNC(igbinary) {
HashPosition tmp_hash_pos;
HashTable *tmp_hash;
char *key_str;
zend_ulong key_long;
int tmp_int;
uint key_len;
zval *z;
zval **d;
struct igbinary_unserialize_data igsd;
if (!val || vallen==0)
return SUCCESS;
if (igbinary_unserialize_data_init(&igsd TSRMLS_CC) != 0) {
return FAILURE;
}
igsd.buffer = (uint8_t *)val;
igsd.buffer_size = vallen;
if (igbinary_unserialize_header(&igsd TSRMLS_CC)) {
igbinary_unserialize_data_deinit(&igsd TSRMLS_CC);
return FAILURE;
}
ALLOC_INIT_ZVAL(z);
if (igbinary_unserialize_zval(&igsd, &z, WANT_CLEAR TSRMLS_CC)) {
igbinary_unserialize_data_deinit(&igsd TSRMLS_CC);
zval_dtor(z);
FREE_ZVAL(z);
return FAILURE;
}
igbinary_unserialize_data_deinit(&igsd TSRMLS_CC);
tmp_hash = HASH_OF(z);
zend_hash_internal_pointer_reset_ex(tmp_hash, &tmp_hash_pos);
while (zend_hash_get_current_data_ex(tmp_hash, (void *) &d, &tmp_hash_pos) == SUCCESS) {
tmp_int = zend_hash_get_current_key_ex(tmp_hash, &key_str, &key_len, &key_long, 0, &tmp_hash_pos);
switch (tmp_int) {
case HASH_KEY_IS_LONG:
/* ??? */
break;
case HASH_KEY_IS_STRING:
php_set_session_var(key_str, key_len-1, *d, NULL TSRMLS_CC);
php_add_session_var(key_str, key_len-1 TSRMLS_CC);
break;
}
zend_hash_move_forward_ex(tmp_hash, &tmp_hash_pos);
}
zval_dtor(z);
FREE_ZVAL(z);
return SUCCESS;
}
/* }}} */
#endif /* HAVE_PHP_SESSION */
#if defined(HAVE_APC_SUPPORT) || defined(HAVE_APCU_SUPPORT)
/* {{{ apc_serialize function */
static int APC_SERIALIZER_NAME(igbinary) ( APC_SERIALIZER_ARGS ) {
(void)config;
if (igbinary_serialize(buf, buf_len, (zval *)value TSRMLS_CC) == 0) {
/* flipped semantics */
return 1;
}
return 0;
}
/* }}} */
/* {{{ apc_unserialize function */
static int APC_UNSERIALIZER_NAME(igbinary) ( APC_UNSERIALIZER_ARGS ) {
(void)config;
if (igbinary_unserialize(buf, buf_len, value TSRMLS_CC) == 0) {
/* flipped semantics */
return 1;
}
zval_dtor(*value);
(*value)->type = IS_NULL;
return 0;
}
/* }}} */
#endif
/* {{{ igbinary_serialize_data_init */
/** Inits igbinary_serialize_data. */
inline static int igbinary_serialize_data_init(struct igbinary_serialize_data *igsd, bool scalar, struct igbinary_memory_manager *memory_manager TSRMLS_DC) {
int r = 0;
if (memory_manager == NULL) {
igsd->mm.alloc = igbinary_mm_wrapper_malloc;
igsd->mm.realloc = igbinary_mm_wrapper_realloc;
igsd->mm.free = igbinary_mm_wrapper_free;
igsd->mm.context = NULL;
} else {
igsd->mm = *memory_manager;
}
igsd->buffer = NULL;
igsd->buffer_size = 0;
igsd->buffer_capacity = 32;
igsd->string_count = 0;
igsd->error = 0;
igsd->buffer = (uint8_t *) igsd->mm.alloc(igsd->buffer_capacity, igsd->mm.context);
if (igsd->buffer == NULL) {
return 1;
}
igsd->scalar = scalar;
if (!igsd->scalar) {
hash_si_init(&igsd->strings, 16);
hash_si_ptr_init(&igsd->references, 16);
igsd->references_id = 0;
}
igsd->compact_strings = (bool)IGBINARY_G(compact_strings);
return r;
}
/* }}} */
/* {{{ igbinary_serialize_data_deinit */
/** Deinits igbinary_serialize_data. */
inline static void igbinary_serialize_data_deinit(struct igbinary_serialize_data *igsd, int free_buffer TSRMLS_DC) {
if (free_buffer && igsd->buffer) {
igsd->mm.free(igsd->buffer, igsd->mm.context);
}
if (!igsd->scalar) {
hash_si_deinit(&igsd->strings);
hash_si_ptr_deinit(&igsd->references);
}
}
/* }}} */
/* {{{ igbinary_serialize_header */
/** Serializes header. */
inline static int igbinary_serialize_header(struct igbinary_serialize_data *igsd TSRMLS_DC) {
return igbinary_serialize32(igsd, IGBINARY_FORMAT_VERSION TSRMLS_CC); /* version */
}
/* }}} */
/* {{{ igbinary_serialize_resize */
/** Expands igbinary_serialize_data. */
inline static int igbinary_serialize_resize(struct igbinary_serialize_data *igsd, size_t size TSRMLS_DC) {
if (igsd->buffer_size + size < igsd->buffer_capacity) {
return 0;
}
while (igsd->buffer_size + size >= igsd->buffer_capacity) {
igsd->buffer_capacity *= 2;
}
igsd->buffer = (uint8_t *) igsd->mm.realloc(igsd->buffer, igsd->buffer_capacity, igsd->mm.context);
if (igsd->buffer == NULL)
return 1;
return 0;
}
/* }}} */
/* {{{ igbinary_serialize8 */
/** Serialize 8bit value. */
inline static int igbinary_serialize8(struct igbinary_serialize_data *igsd, uint8_t i TSRMLS_DC) {
if (igbinary_serialize_resize(igsd, 1 TSRMLS_CC)) {
return 1;
}
igsd->buffer[igsd->buffer_size++] = i;
return 0;
}
/* }}} */
/* {{{ igbinary_serialize16 */
/** Serialize 16bit value. */
inline static int igbinary_serialize16(struct igbinary_serialize_data *igsd, uint16_t i TSRMLS_DC) {
if (igbinary_serialize_resize(igsd, 2 TSRMLS_CC)) {
return 1;
}
igsd->buffer[igsd->buffer_size++] = (uint8_t) (i >> 8 & 0xff);
igsd->buffer[igsd->buffer_size++] = (uint8_t) (i & 0xff);
return 0;
}
/* }}} */
/* {{{ igbinary_serialize32 */
/** Serialize 32bit value. */
inline static int igbinary_serialize32(struct igbinary_serialize_data *igsd, uint32_t i TSRMLS_DC) {
if (igbinary_serialize_resize(igsd, 4 TSRMLS_CC)) {
return 1;
}
igsd->buffer[igsd->buffer_size++] = (uint8_t) (i >> 24 & 0xff);
igsd->buffer[igsd->buffer_size++] = (uint8_t) (i >> 16 & 0xff);
igsd->buffer[igsd->buffer_size++] = (uint8_t) (i >> 8 & 0xff);
igsd->buffer[igsd->buffer_size++] = (uint8_t) (i & 0xff);
return 0;
}
/* }}} */
/* {{{ igbinary_serialize64 */
/** Serialize 64bit value. */
inline static int igbinary_serialize64(struct igbinary_serialize_data *igsd, uint64_t i TSRMLS_DC) {
if (igbinary_serialize_resize(igsd, 8 TSRMLS_CC)) {
return 1;
}
igsd->buffer[igsd->buffer_size++] = (uint8_t) (i >> 56 & 0xff);
igsd->buffer[igsd->buffer_size++] = (uint8_t) (i >> 48 & 0xff);
igsd->buffer[igsd->buffer_size++] = (uint8_t) (i >> 40 & 0xff);
igsd->buffer[igsd->buffer_size++] = (uint8_t) (i >> 32 & 0xff);
igsd->buffer[igsd->buffer_size++] = (uint8_t) (i >> 24 & 0xff);
igsd->buffer[igsd->buffer_size++] = (uint8_t) (i >> 16 & 0xff);
igsd->buffer[igsd->buffer_size++] = (uint8_t) (i >> 8 & 0xff);
igsd->buffer[igsd->buffer_size++] = (uint8_t) (i & 0xff);
return 0;
}
/* }}} */
/* {{{ igbinary_serialize_null */
/** Serializes null. */
inline static int igbinary_serialize_null(struct igbinary_serialize_data *igsd TSRMLS_DC) {
return igbinary_serialize8(igsd, igbinary_type_null TSRMLS_CC);
}
/* }}} */
/* {{{ igbinary_serialize_bool */
/** Serializes bool. */
inline static int igbinary_serialize_bool(struct igbinary_serialize_data *igsd, int b TSRMLS_DC) {
return igbinary_serialize8(igsd, (uint8_t) (b ? igbinary_type_bool_true : igbinary_type_bool_false) TSRMLS_CC);
}
/* }}} */
/* {{{ igbinary_serialize_long */
/** Serializes zend_long. */
inline static int igbinary_serialize_long(struct igbinary_serialize_data *igsd, zend_long l TSRMLS_DC) {
zend_long k = l >= 0 ? l : -l;
bool p = l >= 0 ? true : false;
/* -ZEND_LONG_MIN is 0 otherwise. */
if (l == ZEND_LONG_MIN) {
#if SIZEOF_ZEND_LONG == 8
if (igbinary_serialize8(igsd, (uint8_t) igbinary_type_long64n TSRMLS_CC) != 0) {
return 1;
}
if (igbinary_serialize64(igsd, (uint64_t) 0x8000000000000000 TSRMLS_CC) != 0) {
return 1;
}
#elif SIZEOF_ZEND_LONG == 4
if (igbinary_serialize8(igsd, (uint8_t) igbinary_type_long32n TSRMLS_CC) != 0) {
return 1;
}
if (igbinary_serialize32(igsd, (uint32_t) 0x80000000 TSRMLS_CC) != 0) {
return 1;
}
#else
#error "Strange sizeof(zend_long)."
#endif
return 0;
}
if (k <= 0xff) {
if (igbinary_serialize8(igsd, (uint8_t) (p ? igbinary_type_long8p : igbinary_type_long8n) TSRMLS_CC) != 0) {
return 1;
}
if (igbinary_serialize8(igsd, (uint8_t) k TSRMLS_CC) != 0) {
return 1;
}
} else if (k <= 0xffff) {
if (igbinary_serialize8(igsd, (uint8_t) (p ? igbinary_type_long16p : igbinary_type_long16n) TSRMLS_CC) != 0) {
return 1;
}
if (igbinary_serialize16(igsd, (uint16_t) k TSRMLS_CC) != 0) {
return 1;
}
#if SIZEOF_ZEND_LONG == 8
} else if (k <= 0xffffffff) {
if (igbinary_serialize8(igsd, (uint8_t) (p ? igbinary_type_long32p : igbinary_type_long32n) TSRMLS_CC) != 0) {
return 1;
}
if (igbinary_serialize32(igsd, (uint32_t) k TSRMLS_CC) != 0) {
return 1;
}
} else {
if (igbinary_serialize8(igsd, (uint8_t) (p ? igbinary_type_long64p : igbinary_type_long64n) TSRMLS_CC) != 0) {
return 1;
}
if (igbinary_serialize64(igsd, (uint64_t) k TSRMLS_CC) != 0) {
return 1;
}
}
#elif SIZEOF_ZEND_LONG == 4
} else {
if (igbinary_serialize8(igsd, (uint8_t) (p ? igbinary_type_long32p : igbinary_type_long32n) TSRMLS_CC) != 0) {
return 1;
}
if (igbinary_serialize32(igsd, (uint32_t) k TSRMLS_CC) != 0) {
return 1;
}
}
#else
#error "Strange sizeof(zend_long)."
#endif
return 0;
}
/* }}} */
/* {{{ igbinary_serialize_double */
/** Serializes double. */
inline static int igbinary_serialize_double(struct igbinary_serialize_data *igsd, double d TSRMLS_DC) {
union {
double d;
uint64_t u;
} u;
if (igbinary_serialize8(igsd, igbinary_type_double TSRMLS_CC) != 0) {
return 1;
}
u.d = d;
return igbinary_serialize64(igsd, u.u TSRMLS_CC);
}
/* }}} */
/* {{{ igbinary_serialize_string */
/** Serializes string.
* Serializes each string once, after first time uses pointers.
*/
inline static int igbinary_serialize_string(struct igbinary_serialize_data *igsd, char *s, size_t len TSRMLS_DC) {
uint32_t t;
uint32_t *i = &t;
if (len == 0) {
if (igbinary_serialize8(igsd, igbinary_type_string_empty TSRMLS_CC) != 0) {
return 1;
}
return 0;
}
if (igsd->scalar || !igsd->compact_strings || hash_si_find(&igsd->strings, s, len, i) == 1) {
if (!igsd->scalar && igsd->compact_strings) {
hash_si_insert(&igsd->strings, s, len, igsd->string_count);
}
igsd->string_count += 1;
if (igbinary_serialize_chararray(igsd, s, len TSRMLS_CC) != 0) {
return 1;
}
} else {
if (*i <= 0xff) {
if (igbinary_serialize8(igsd, (uint8_t) igbinary_type_string_id8 TSRMLS_CC) != 0) {
return 1;
}
if (igbinary_serialize8(igsd, (uint8_t) *i TSRMLS_CC) != 0) {
return 1;
}
} else if (*i <= 0xffff) {
if (igbinary_serialize8(igsd, (uint8_t) igbinary_type_string_id16 TSRMLS_CC) != 0) {
return 1;
}
if (igbinary_serialize16(igsd, (uint16_t) *i TSRMLS_CC) != 0) {
return 1;
}
} else {
if (igbinary_serialize8(igsd, (uint8_t) igbinary_type_string_id32 TSRMLS_CC) != 0) {
return 1;
}
if (igbinary_serialize32(igsd, (uint32_t) *i TSRMLS_CC) != 0) {
return 1;