-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathqb_storage.c
1751 lines (1663 loc) · 76.5 KB
/
qb_storage.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
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2012 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Chung Leong <cleong@cal.berkeley.edu> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#include "qb.h"
static intptr_t qb_relocate_segment_memory(qb_memory_segment *segment, int8_t *new_location) {
if(segment->memory != new_location) {
// adjust references in code
uint32_t i;
intptr_t diff = new_location - segment->memory;
for(i = 0; i < segment->reference_count; i++) {
uintptr_t *p_ref = segment->references[i];
*p_ref += diff;
}
segment->memory = new_location;
// relocate other segments that point to the same location
if(segment->next_dependent) {
qb_relocate_segment_memory(segment->next_dependent, new_location);
}
// return the shift in location
return diff;
}
return 0;
}
void qb_import_segment(qb_memory_segment *segment, qb_memory_segment *other_segment) {
if(other_segment->imported_segment) {
// if the target segment has imported another segment, import the same one instead
qb_import_segment(segment, other_segment->imported_segment);
} else {
qb_relocate_segment_memory(segment, other_segment->memory);
segment->flags |= QB_SEGMENT_IMPORTED;
segment->imported_segment = other_segment;
segment->next_dependent = other_segment->next_dependent;
other_segment->next_dependent = segment;
}
}
static void qb_allocate_segment_memory_in_main_thread(void *param1, void *param2, int param3) {
qb_allocate_segment_memory(param1, param3);
}
void qb_allocate_segment_memory(qb_memory_segment *segment, uint32_t byte_count) {
if(segment->flags & QB_SEGMENT_IMPORTED) {
qb_allocate_segment_memory(segment->imported_segment, byte_count);
} else {
if(byte_count > segment->current_allocation) {
if(qb_in_main_thread()) {
uint32_t new_allocation = ALIGN_TO(byte_count, 1024);
uint32_t extra = new_allocation - byte_count;
int8_t *memory = (segment->current_allocation > 0) ? erealloc(segment->memory, new_allocation): emalloc(new_allocation);
int8_t *data_end = memory + byte_count;
segment->current_allocation = new_allocation;
memset(data_end, 0, extra);
qb_relocate_segment_memory(segment, memory);
segment->byte_count = byte_count;
} else {
qb_run_in_main_thread(qb_allocate_segment_memory_in_main_thread, segment, NULL, byte_count);
}
}
}
}
static int32_t qb_connect_segment_to_memory(qb_memory_segment *segment, int8_t *memory, uint32_t byte_count, uint32_t bytes_available, int32_t ownership) {
if(segment->flags & QB_SEGMENT_IMPORTED) {
return qb_connect_segment_to_memory(segment->imported_segment, memory, byte_count, bytes_available, ownership);
} else {
if(byte_count <= bytes_available) {
if(segment->current_allocation > 0) {
qb_release_segment(segment);
}
segment->byte_count = byte_count;
segment->current_allocation = bytes_available;
if(!ownership) {
segment->flags |= QB_SEGMENT_BORROWED;
}
if(bytes_available) {
qb_relocate_segment_memory(segment, memory);
}
return TRUE;
} else {
return FALSE;
}
}
}
static void * qb_map_file_to_memory(php_stream *stream, uint32_t byte_count, int32_t write_access TSRMLS_DC);
static void qb_unmap_file_from_memory(php_stream *stream TSRMLS_DC);
static int32_t qb_connect_segment_to_file(qb_memory_segment *segment, php_stream *stream, uint32_t byte_count, int32_t write_access) {
if(segment->flags & QB_SEGMENT_IMPORTED) {
return qb_connect_segment_to_file(segment->imported_segment, stream, byte_count, write_access);
} else {
uint32_t bytes_available = byte_count;
int8_t *memory;
TSRMLS_FETCH();
if(!bytes_available) {
bytes_available = 1024;
}
memory = qb_map_file_to_memory(stream, bytes_available, write_access TSRMLS_CC);
if(memory) {
if(!qb_connect_segment_to_memory(segment, memory, byte_count, bytes_available, FALSE)) {
qb_unmap_file_from_memory(stream TSRMLS_CC);
return FALSE;
}
segment->flags |= QB_SEGMENT_MAPPED;
segment->stream = stream;
return TRUE;
} else {
return FALSE;
}
}
}
static void qb_release_segment_in_main_thread(void *param1, void *param2, int param3) {
qb_release_segment(param1);
}
void qb_release_segment(qb_memory_segment *segment) {
if(segment->flags & QB_SEGMENT_IMPORTED) {
qb_memory_segment *other_segment = segment->imported_segment;
segment->flags &= ~QB_SEGMENT_IMPORTED;
other_segment->next_dependent = segment->next_dependent;
segment->next_dependent = NULL;
segment->imported_segment = NULL;
} else if((segment->flags & QB_SEGMENT_BORROWED) && !(segment->flags & QB_SEGMENT_MAPPED)) {
// the memory was borrowed--nothing needs to be done
segment->flags &= ~QB_SEGMENT_BORROWED;
} else {
if(qb_in_main_thread()) {
if(segment->flags & QB_SEGMENT_MAPPED) {
TSRMLS_FETCH();
qb_unmap_file_from_memory(segment->stream TSRMLS_CC);
// set the file to the actual size if more bytes were allocated than needed
if(segment->current_allocation != segment->byte_count) {
php_stream_truncate_set_size(segment->stream, segment->byte_count);
}
segment->flags &= ~(QB_SEGMENT_BORROWED | QB_SEGMENT_MAPPED);
segment->stream = NULL;
} else {
if(segment->current_allocation > 0) {
efree(segment->memory);
}
}
} else {
if((segment->flags & QB_SEGMENT_MAPPED) || segment->current_allocation > 0) {
qb_run_in_main_thread(qb_release_segment_in_main_thread, segment, NULL, 0);
return;
}
}
}
// note: segment->memory is not set to NULL
// because we need the value for relocation
segment->current_allocation = 0;
}
void qb_resize_segment_in_main_thread(void *param1, void *param2, int param3) {
intptr_t *p_offset = param2;
*p_offset = qb_resize_segment(param1, param3);
}
intptr_t qb_resize_segment(qb_memory_segment *segment, uint32_t new_size) {
if(segment->flags & QB_SEGMENT_IMPORTED) {
return qb_resize_segment(segment->imported_segment, new_size);
}
if(new_size > segment->current_allocation) {
if(qb_in_main_thread()) {
int8_t *current_data_end;
int8_t *memory;
uint32_t new_allocation = ALIGN_TO(new_size, 1024);
uint32_t addition = new_allocation - segment->current_allocation;
if(segment->flags & QB_SEGMENT_MAPPED) {
// unmap the file, enlarge it, then map it again
TSRMLS_FETCH();
qb_unmap_file_from_memory(segment->stream TSRMLS_CC);
php_stream_truncate_set_size(segment->stream, new_allocation);
memory = qb_map_file_to_memory(segment->stream, new_allocation, TRUE TSRMLS_CC);
if(!memory) {
// shouldn't really happen--we had managed to map it successfully after all
qb_report_memory_map_exception(0, segment->stream->orig_path);
addition = 0;
new_size = 0;
new_allocation = 0;
}
} else {
// segment->memory is valid only when current_allocation > 0
if(segment->current_allocation > 0) {
memory = erealloc(segment->memory, new_allocation);
} else {
memory = emalloc(new_allocation);
}
}
// clear the newly allcoated bytes
current_data_end = memory + segment->byte_count;
memset(current_data_end, 0, addition);
segment->byte_count = new_size;
segment->current_allocation = new_allocation;
return qb_relocate_segment_memory(segment, memory);
} else {
intptr_t offset;
qb_run_in_main_thread(qb_resize_segment_in_main_thread, segment, &offset, new_size);
return offset;
}
} else {
segment->byte_count = new_size;
}
return 0;
}
qb_storage * qb_create_storage_copy(qb_storage *base, intptr_t instruction_shift, int32_t reentrance) {
qb_storage *storage;
intptr_t shift;
uint32_t i, j;
storage = emalloc(base->size);
memcpy(storage, base, base->size);
// adjust pointers
shift = (uintptr_t) storage - (uintptr_t) base;
SHIFT_POINTER(storage->segments, shift);
for(i = 0; i < storage->segment_count; i++) {
qb_memory_segment *src = &base->segments[i];
qb_memory_segment *dst = &storage->segments[i];
int separation;
if(dst->flags & QB_SEGMENT_SEPARATE_ON_FORK && !reentrance) {
separation = TRUE;
} else if((dst->flags & QB_SEGMENT_SEPARATE_ON_REENTRY) && reentrance) {
separation = TRUE;
} else {
separation = FALSE;
}
if(dst->flags & QB_SEGMENT_PREALLOCATED) {
if(separation) {
SHIFT_POINTER(dst->memory, shift);
}
} else {
if(dst->references) {
// shift the point of pointers
SHIFT_POINTER(dst->references, shift);
// shift the relocations themselves
for(j = 0; j < dst->reference_count; j++) {
SHIFT_POINTER(dst->references[j], instruction_shift);
}
}
if(separation) {
if(dst->flags & QB_SEGMENT_IMPORTED) {
// unattach it
dst->flags &= ~QB_SEGMENT_IMPORTED;
dst->imported_segment = NULL;
dst->next_dependent = NULL;
} else {
if((dst->flags & QB_SEGMENT_EMPTY_ON_RETURN) && reentrance) {
dst->byte_count = 0;
}
if((dst->flags & QB_SEGMENT_REALLOCATE_ON_CALL) && reentrance) {
// the memory will be alloc'ed when the copy is called
dst->current_allocation = 0;
} else {
if(dst->byte_count) {
// allocate new memory for the segment
int8_t *new_memory = emalloc(dst->byte_count);
if(!reentrance) {
// forking--need to copy the contents over
memcpy(new_memory, dst->memory, dst->byte_count);
}
qb_relocate_segment_memory(dst, new_memory);
}
dst->current_allocation = dst->byte_count;
}
}
} else {
dst->byte_count = 0;
dst->current_allocation = 0;
qb_import_segment(dst, src);
}
}
}
return storage;
}
void qb_copy_storage_contents(qb_storage *src_storage, qb_storage *dst_storage) {
qb_memory_segment *src_segment_start, *src_segment_end, *dst_segment_start;
uint32_t i, byte_count;
// copy the preallocated segments
src_segment_start = &src_storage->segments[QB_SELECTOR_LOCAL_SCALAR];
src_segment_end = &src_storage->segments[QB_SELECTOR_LOCAL_ARRAY];
dst_segment_start = &dst_storage->segments[QB_SELECTOR_LOCAL_SCALAR];
byte_count = (uint32_t) ((src_segment_end->memory + src_segment_end->byte_count) - src_segment_start->memory);
memcpy(dst_segment_start->memory, src_segment_start->memory, byte_count);
for(i = QB_SELECTOR_ARRAY_START; i < src_storage->segment_count; i++) {
qb_memory_segment *src = &src_storage->segments[i];
qb_memory_segment *dst = &dst_storage->segments[i];
if(dst->memory != src->memory) {
qb_resize_segment(dst, src->byte_count);
memcpy(dst->memory, src->memory, src->byte_count);
}
}
}
static int32_t qb_capture_dimensions_from_byte_count(uint32_t byte_count, qb_dimension_mappings *m, uint32_t dimension_index) {
uint32_t element_count = ELEMENT_COUNT(byte_count, m->dst_element_type);
uint32_t correct_byte_count = BYTE_COUNT(element_count, m->dst_element_type);
if(dimension_index + 1 > MAX_DIMENSION) {
qb_report_too_man_dimension_exception(0);
return FALSE;
}
// make sure the number of bytes is a multiple of the element size
if(byte_count != correct_byte_count) {
qb_report_binary_string_size_mismatch_exception(0, byte_count, m->dst_element_type);
}
if(m->src_dimensions[dimension_index] < element_count) {
m->src_dimensions[dimension_index] = element_count;
}
m->src_dimension_count = dimension_index + 1;
return TRUE;
}
static int32_t qb_capture_dimensions_from_utf8_string(const char *text, uint32_t byte_count, qb_dimension_mappings *m, uint32_t dimension_index) {
const unsigned char *bytes = (const unsigned char *) text;
uint32_t element_count = 0;
uint32_t codepoint, state = 0, i;
if(dimension_index + 1 > MAX_DIMENSION) {
qb_report_too_man_dimension_exception(0);
return FALSE;
}
for(i = 0; i < byte_count; i++) {
if(!decode(&state, &codepoint, bytes[i])) {
element_count++;
}
}
if(m->src_dimensions[dimension_index] < element_count) {
m->src_dimensions[dimension_index] = element_count;
}
m->src_dimension_count = dimension_index + 1;
return TRUE;
}
static int32_t qb_capture_dimensions_from_string(zval *zvalue, qb_dimension_mappings *m, uint32_t dimension_index) {
int32_t need_utf8_decoding = FALSE;
if(m->dst_address_flags & QB_ADDRESS_STRING) {
if(m->dst_element_type >= QB_TYPE_I16) {
need_utf8_decoding = TRUE;
}
}
if(need_utf8_decoding) {
return qb_capture_dimensions_from_utf8_string(Z_STRVAL_P(zvalue), Z_STRLEN_P(zvalue), m, dimension_index);
} else {
return qb_capture_dimensions_from_byte_count(Z_STRLEN_P(zvalue), m, dimension_index);
}
}
static int32_t qb_capture_dimensions_from_zval(zval *zvalue, qb_dimension_mappings *m, uint32_t dimension_index);
static int32_t qb_capture_dimensions_from_array(zval *zarray, qb_dimension_mappings *m, uint32_t dimension_index) {
HashTable *ht = Z_ARRVAL_P(zarray);
Bucket *p;
uint32_t dimension = ht->nNextFreeElement;
if(dimension_index + 1 > MAX_DIMENSION) {
qb_report_too_man_dimension_exception(0);
return FALSE;
}
if(m->src_dimensions[dimension_index] < dimension) {
m->src_dimensions[dimension_index] = dimension;
}
if(m->src_dimension_count < dimension_index + 1) {
m->src_dimension_count = dimension_index + 1;
}
for(p = ht->pListHead; p; p = p->pListNext) {
if((long) p->h >= 0 && !p->nKeyLength) {
zval **p_element = p->pData;
qb_capture_dimensions_from_zval(*p_element, m, dimension_index + 1);
}
}
return TRUE;
}
static int32_t qb_capture_dimensions_from_object(zval *zobject, qb_dimension_mappings *m, uint32_t dimension_index) {
qb_index_alias_scheme *scheme = m->dst_index_alias_schemes[dimension_index];
uint32_t i;
TSRMLS_FETCH();
if(dimension_index + 1 > MAX_DIMENSION) {
qb_report_too_man_dimension_exception(0);
return FALSE;
}
if(!scheme) {
qb_report_illegal_conversion_to_array_exception(0, "object");
}
if(m->src_dimensions[dimension_index] < scheme->dimension) {
m->src_dimensions[dimension_index] = scheme->dimension;
}
m->src_dimension_count = dimension_index + 1;
for(i = 0; i < scheme->dimension; i++) {
zval **p_element, *element = NULL;
zval *alias = qb_cstring_to_zval(scheme->aliases[i] TSRMLS_CC);
p_element = Z_OBJ_GET_PROP_PTR_PTR(zobject, alias);
if(p_element) {
element = *p_element;
} else if(Z_OBJ_HT_P(zobject)->read_property) {
element = Z_OBJ_READ_PROP(zobject, alias);
}
if(element) {
qb_capture_dimensions_from_zval(element, m, dimension_index + 1);
}
if(!p_element && element) {
// value from __get()
Z_ADDREF_P(element);
zval_ptr_dtor(&element);
}
}
return TRUE;
}
void qb_copy_wrap_around(int8_t *memory, uint32_t filled_byte_count, uint32_t required_byte_count) {
if(filled_byte_count) {
while(filled_byte_count < required_byte_count) {
uint32_t gap = (required_byte_count - filled_byte_count);
uint32_t copy_count = (gap > filled_byte_count) ? filled_byte_count : gap;
memcpy(memory + filled_byte_count, memory, copy_count);
filled_byte_count += copy_count;
}
} else {
memset(memory, 0, required_byte_count);
}
}
#include "qb_storage_file.c"
#include "qb_storage_gd_image.c"
static int32_t qb_capture_dimensions_from_zval(zval *zvalue, qb_dimension_mappings *m, uint32_t dimension_index) {
switch(Z_TYPE_P(zvalue)) {
#ifdef IS_CONSTANT_ARRAY
case IS_CONSTANT_ARRAY:
#endif
case IS_ARRAY: {
return qb_capture_dimensions_from_array(zvalue, m, dimension_index);
}
case IS_OBJECT: {
return qb_capture_dimensions_from_object(zvalue, m, dimension_index);
}
case IS_STRING: {
return qb_capture_dimensions_from_string(zvalue, m, dimension_index);
}
case IS_NULL:
case IS_LONG:
case IS_BOOL:
case IS_DOUBLE: {
return TRUE;
}
case IS_RESOURCE: {
gdImagePtr image;
php_stream *stream;
if((image = qb_get_gd_image(zvalue))) {
return qb_capture_dimensions_from_image(image, m, dimension_index);
} else if((stream = qb_get_file_stream(zvalue))) {
return qb_capture_dimensions_from_file(stream, m, dimension_index);
} else {
qb_report_illegal_conversion_to_array_exception(0, "resource");
return FALSE;
}
} break;
default: {
return FALSE;
}
}
}
static int32_t qb_add_destination_dimensions(qb_storage *storage, qb_address *address, qb_dimension_mappings *m) {
uint32_t i;
m->dst_dimension_count = address->dimension_count;
m->dst_element_type = address->type;
m->dst_address_flags = address->flags;
for(i = 0; i < address->dimension_count; i++) {
qb_address *dimension_address = address->dimension_addresses[i];
qb_address *array_size_address = address->array_size_addresses[i];
if(IS_IMMUTABLE(dimension_address)) {
m->dst_dimensions[i] = VALUE_IN(storage, U32, dimension_address);
} else {
m->dst_dimensions[i] = 0;
}
if(IS_IMMUTABLE(array_size_address)) {
m->dst_array_sizes[i] = VALUE_IN(storage, U32, array_size_address);
} else {
m->dst_array_sizes[i] = 0;
}
if(address->index_alias_schemes) {
m->dst_index_alias_schemes[i] = address->index_alias_schemes[i];
} else {
m->dst_index_alias_schemes[i] = NULL;
}
}
return TRUE;
}
static int32_t qb_add_source_dimensions(qb_storage *storage, qb_address *address, qb_dimension_mappings *m) {
uint32_t i;
m->src_dimension_count = address->dimension_count;
m->src_element_type = address->type;
m->src_address_flags = address->flags;
for(i = 0; i < address->dimension_count; i++) {
qb_address *dimension_address = address->dimension_addresses[i];
qb_address *array_size_address = address->array_size_addresses[i];
m->src_dimensions[i] = VALUE_IN(storage, U32, dimension_address);
m->src_array_sizes[i] = VALUE_IN(storage, U32, array_size_address);
if(address->index_alias_schemes) {
m->src_index_alias_schemes[i] = address->index_alias_schemes[i];
} else {
m->src_index_alias_schemes[i] = NULL;
}
}
return TRUE;
}
static int32_t qb_add_source_dimensions_from_zval(zval *zvalue, qb_dimension_mappings *m) {
uint32_t i;
m->src_dimension_count = 0;
m->src_address_flags = 0;
m->src_element_type = QB_TYPE_UNKNOWN;
for(i = 0; i < MAX_DIMENSION; i++) {
m->src_dimensions[i] = 0;
}
if(qb_capture_dimensions_from_zval(zvalue, m, 0)) {
uint32_t element_size = 1;
if(STORAGE_TYPE_MATCH(m->dst_element_type, QB_TYPE_I64)) {
if(m->dst_dimension_count + 1 == m->src_dimension_count) {
if(m->src_dimensions[m->src_dimension_count - 1] == 2) {
// forming 64-bit integers from two-element array
m->src_dimension_count--;
}
}
}
for(i = m->src_dimension_count - 1; (int32_t) i >= 0; i--) {
uint32_t array_size = element_size * m->src_dimensions[i];
m->src_array_sizes[i] = array_size;
m->src_index_alias_schemes[i] = NULL;
element_size = array_size;
}
return TRUE;
}
return FALSE;
}
static int32_t qb_apply_dimension_mappings(qb_storage *storage, qb_address *address, qb_dimension_mappings *m, int32_t autovivification) {
uint32_t src_array_size = (m->src_dimension_count > 0) ? m->src_array_sizes[0] : 1;
if(m->dst_dimension_count > 0) {
uint32_t dst_array_size = m->dst_array_sizes[0];
if(src_array_size != dst_array_size) {
if(!dst_array_size) {
// some dimensions aren't defined
uint32_t unknown_dimension_count = 0, known_dimension_count = m->src_dimension_count, derived_dimension_count = 0;
uint32_t i;
for(i = 0; i < m->dst_dimension_count; i++) {
if(m->dst_dimensions[i] != 0) {
if(i < m->src_dimension_count) {
// the subarray size of the destination needs to match the source
if(m->dst_array_sizes[i] == m->src_array_sizes[i]) {
known_dimension_count = i;
} else {
known_dimension_count = 0;
}
} else {
// see if the source array at one level higher contains a multiple of the sub-array size
if(i > 0 && i - 1 < m->src_dimension_count && m->src_array_sizes[i - 1] > 0 && (m->src_array_sizes[i - 1] % m->dst_array_sizes[i]) == 0) {
// we can derive what the dimension should be
m->dst_dimensions[i - 1] = m->src_array_sizes[i - 1] / m->dst_array_sizes[i];
m->dst_array_sizes[i - 1] = m->dst_dimensions[i - 1] * m->dst_array_sizes[i];
unknown_dimension_count--;
derived_dimension_count++;
known_dimension_count = i;
} else {
known_dimension_count = 0;
}
}
break;
} else {
unknown_dimension_count++;
}
}
if(unknown_dimension_count <= known_dimension_count) {
// copy the dimensions over
for(i = 0; i < unknown_dimension_count; i++) {
m->dst_dimensions[i] = m->src_dimensions[i];
m->dst_array_sizes[i] = m->src_array_sizes[i];
}
dst_array_size = m->dst_array_sizes[0];
} else if(unknown_dimension_count == 1) {
if(m->dst_dimension_count == 1) {
// a multidimensional array copied to a linear array
m->dst_dimensions[0] = src_array_size;
m->dst_array_sizes[0] = src_array_size;
dst_array_size = src_array_size;
} else {
uint32_t dst_sub_array_size = m->dst_array_sizes[1];
uint32_t dst_dimension = src_array_size / dst_sub_array_size;
dst_array_size = dst_dimension * dst_sub_array_size;
m->dst_dimensions[0] = dst_dimension;
m->dst_array_sizes[0] = dst_array_size;
}
} else {
if(autovivification) {
src_array_size = 0;
} else {
qb_report_incompatible_array_structure_exception(0, m->src_element_type, m->src_dimensions, m->src_dimension_count, m->dst_element_type, m->dst_dimensions, m->dst_dimension_count);
return FALSE;
}
}
for(i = 0; i < unknown_dimension_count + derived_dimension_count; i++) {
qb_address *dimension_address = address->dimension_addresses[i];
qb_address *array_size_address = address->array_size_addresses[i];
VALUE_IN(storage, U32, dimension_address) = m->dst_dimensions[i];
VALUE_IN(storage, U32, array_size_address) = m->dst_array_sizes[i];
}
}
if(dst_array_size < src_array_size) {
// the source is too large
qb_report_incompatible_array_structure_exception(0, m->src_element_type, m->src_dimensions, m->src_dimension_count, m->dst_element_type, m->dst_dimensions, m->dst_dimension_count);
return FALSE;
}
}
} else {
// scalar
if(src_array_size != 1) {
qb_report_incompatible_array_structure_exception(0, m->src_element_type, m->src_dimensions, m->src_dimension_count, m->dst_element_type, m->dst_dimensions, m->dst_dimension_count);
return FALSE;
}
}
return TRUE;
}
static int32_t qb_copy_elements_from_zval(zval *zvalue, int8_t *dst_memory, qb_dimension_mappings *m, uint32_t dimension_index);
static int32_t qb_copy_elements_from_array(zval *zarray, int8_t *dst_memory, qb_dimension_mappings *m, uint32_t dimension_index) {
int8_t *dst_pointer = dst_memory;
uint32_t dst_element_count = (dimension_index < m->dst_dimension_count) ? m->dst_array_sizes[dimension_index] : 1;
uint32_t dst_byte_count = BYTE_COUNT(dst_element_count, m->dst_element_type);
uint32_t src_dimension = (dimension_index < m->src_dimension_count) ? m->src_dimensions[dimension_index] : 1;
uint32_t src_element_element_count = (dimension_index + 1 < m->src_dimension_count) ? m->src_array_sizes[dimension_index + 1] : 1;
uint32_t src_element_byte_count = BYTE_COUNT(src_element_element_count, m->dst_element_type);
uint32_t src_byte_count = src_dimension * src_element_byte_count;
uint32_t src_index = 0;
HashTable *ht = Z_ARRVAL_P(zarray);
Bucket *p;
if(STORAGE_TYPE_MATCH(m->dst_element_type, QB_TYPE_I64) && dimension_index == m->src_dimension_count) {
if(qb_zval_array_to_int64(zarray, (int64_t *) dst_memory)) {
return TRUE;
}
}
// assume the elements are stored in order in the array
for(p = ht->pListHead; p && src_index < src_dimension; p = p->pListNext) {
if((long) p->h == src_index && !p->nKeyLength) {
zval **p_element = p->pData;
qb_copy_elements_from_zval(*p_element, dst_pointer, m, dimension_index + 1);
src_index++;
dst_pointer += src_element_byte_count;
} else {
break;
}
}
if(src_index < src_dimension) {
// the elements are not sequential: copy the rest
while(src_index < src_dimension) {
zval **p_element;
if(zend_hash_index_find(ht, src_index, (void **) &p_element) == SUCCESS) {
qb_copy_elements_from_zval(*p_element, dst_pointer, m, dimension_index + 1);
} else {
// there's a gap
memset(dst_pointer, 0, src_element_byte_count);
}
src_index++;
dst_pointer += src_element_byte_count;
}
}
if(src_byte_count < dst_byte_count) {
if(dimension_index == 0) {
qb_copy_wrap_around(dst_memory, src_byte_count, dst_byte_count);
} else {
memset(dst_memory + src_byte_count, 0, dst_byte_count - src_byte_count);
}
}
return TRUE;
}
static int32_t qb_copy_elements_from_object(zval *zobject, int8_t *dst_memory, qb_dimension_mappings *m, uint32_t dimension_index) {
int8_t *dst_pointer = dst_memory;
uint32_t dst_element_count = (dimension_index < m->dst_dimension_count) ? m->dst_array_sizes[dimension_index] : 1;
uint32_t dst_byte_count = BYTE_COUNT(dst_element_count, m->dst_element_type);
uint32_t src_dimension = (dimension_index < m->src_dimension_count) ? m->src_dimensions[dimension_index] : 1;
uint32_t src_element_element_count = (dimension_index + 1 < m->src_dimension_count) ? m->src_array_sizes[dimension_index + 1] : 1;
uint32_t src_element_byte_count = BYTE_COUNT(src_element_element_count, m->dst_element_type);
uint32_t src_byte_count = src_dimension * src_element_byte_count;
uint32_t src_index = 0;
qb_index_alias_scheme *scheme = m->dst_index_alias_schemes[dimension_index];
TSRMLS_FETCH();
while(src_index < scheme->dimension) {
zval **p_element, *element = NULL;
zval *alias = qb_cstring_to_zval(scheme->aliases[src_index] TSRMLS_CC);
p_element = Z_OBJ_GET_PROP_PTR_PTR(zobject, alias);
if(p_element) {
element = *p_element;
} else if(Z_OBJ_HT_P(zobject)->read_property) {
element = Z_OBJ_READ_PROP(zobject, alias);
}
if(element && Z_TYPE_P(element) != IS_NULL) {
qb_copy_elements_from_zval(element, dst_pointer, m, dimension_index + 1);
} else {
memset(dst_pointer, 0, src_element_byte_count);
}
if(!p_element && element) {
Z_ADDREF_P(element);
zval_ptr_dtor(&element);
}
src_index++;
dst_pointer += src_element_byte_count;
}
if(src_byte_count < dst_byte_count) {
if(dimension_index == 0) {
qb_copy_wrap_around(dst_memory, src_byte_count, dst_byte_count);
} else {
memset(dst_memory + src_byte_count, 0, dst_byte_count - src_byte_count);
}
}
return TRUE;
}
static int32_t qb_copy_elements_from_string(zval *zstring, int8_t *dst_memory, qb_dimension_mappings *m, uint32_t dimension_index) {
int8_t *src_memory = (int8_t *) Z_STRVAL_P(zstring);
uint32_t src_byte_count = Z_STRLEN_P(zstring);
uint32_t dst_element_count = (dimension_index < m->dst_dimension_count) ? m->dst_array_sizes[dimension_index] : 1;
uint32_t dst_byte_count = BYTE_COUNT(dst_element_count, m->dst_element_type);
int32_t need_utf8_decoding = FALSE;
if(m->dst_address_flags & QB_ADDRESS_STRING) {
if(m->dst_element_type >= QB_TYPE_I16) {
need_utf8_decoding = TRUE;
}
}
if(need_utf8_decoding) {
const unsigned char *bytes = (const unsigned char *) src_memory;
uint32_t codepoint, state = 0, i, j;
if(STORAGE_TYPE_MATCH(m->dst_element_type, QB_TYPE_I16)) {
uint16_t *dst_elements = (uint16_t *) dst_memory;
for(i = 0, j = 0; j < dst_element_count && i < src_byte_count; i++) {
if(!decode(&state, &codepoint, bytes[i])) {
dst_elements[j] = codepoint;
j++;
}
}
src_byte_count = j * sizeof(uint16_t);
} else if(STORAGE_TYPE_MATCH(m->dst_element_type, QB_TYPE_I32)) {
uint32_t *dst_elements = (uint32_t *) dst_memory;
for(i = 0, j = 0; j < dst_element_count && i < src_byte_count; i++) {
if(!decode(&state, &codepoint, bytes[i])) {
dst_elements[j] = codepoint;
j++;
}
}
src_byte_count = j * sizeof(uint32_t);
}
} else {
memcpy(dst_memory, src_memory, src_byte_count);
}
if(src_byte_count < dst_byte_count) {
if(dimension_index == 0) {
qb_copy_wrap_around(dst_memory, src_byte_count, dst_byte_count);
} else {
memset(dst_memory + src_byte_count, 0, dst_byte_count - src_byte_count);
}
}
return TRUE;
}
static int32_t qb_copy_elements_from_long(zval *zlong, int8_t *dst_memory, qb_dimension_mappings *m, uint32_t dimension_index) {
long src_value = Z_LVAL_P(zlong);
#if SIZEOF_LONG == 8
qb_copy_element(QB_TYPE_S64, (int8_t *) &src_value, m->dst_element_type, dst_memory);
#else
qb_copy_element(QB_TYPE_S32, (int8_t *) &src_value, m->dst_element_type, dst_memory);
#endif
if(dimension_index == 0) {
if(m->dst_dimension_count > 0) {
uint32_t dst_element_count = m->dst_array_sizes[dimension_index];
uint32_t dst_byte_count = BYTE_COUNT(dst_element_count, m->dst_element_type);
uint32_t src_byte_count = BYTE_COUNT(1, m->dst_element_type);
qb_copy_wrap_around(dst_memory, src_byte_count, dst_byte_count);
}
} else if(dimension_index < m->src_dimension_count) {
uint32_t dst_element_count = m->src_array_sizes[dimension_index];
uint32_t dst_byte_count = BYTE_COUNT(dst_element_count, m->dst_element_type);
uint32_t src_byte_count = BYTE_COUNT(1, m->dst_element_type);
if(dst_byte_count < src_byte_count) {
memset(dst_memory + src_byte_count, 0, dst_byte_count - src_byte_count);
}
}
return TRUE;
}
static int32_t qb_copy_elements_from_double(zval *zdouble, int8_t *dst_memory, qb_dimension_mappings *m, uint32_t dimension_index) {
double src_value = Z_DVAL_P(zdouble);
qb_copy_element(QB_TYPE_F64, (int8_t *) &src_value, m->dst_element_type, dst_memory);
if(dimension_index == 0) {
if(m->dst_dimension_count > 0) {
uint32_t dst_element_count = m->dst_array_sizes[dimension_index];
uint32_t dst_byte_count = BYTE_COUNT(dst_element_count, m->dst_element_type);
uint32_t src_byte_count = BYTE_COUNT(1, m->dst_element_type);
qb_copy_wrap_around(dst_memory, src_byte_count, dst_byte_count);
}
} else if(dimension_index < m->src_dimension_count) {
uint32_t dst_element_count = m->src_array_sizes[dimension_index];
uint32_t dst_byte_count = BYTE_COUNT(dst_element_count, m->dst_element_type);
uint32_t src_byte_count = BYTE_COUNT(1, m->dst_element_type);
if(dst_byte_count < src_byte_count) {
memset(dst_memory + src_byte_count, 0, dst_byte_count - src_byte_count);
}
}
return TRUE;
}
static int32_t qb_copy_elements_from_null(zval *znull, int8_t *dst_memory, qb_dimension_mappings *m, uint32_t dimension_index) {
if(dimension_index == 0) {
uint32_t dst_element_count = (m->dst_dimension_count > 0) ? m->dst_array_sizes[dimension_index] : 1;
uint32_t dst_byte_count = BYTE_COUNT(dst_element_count, m->dst_element_type);
memset(dst_memory, 0, dst_byte_count);
} else if(dimension_index < m->src_dimension_count) {
uint32_t dst_element_count = m->src_array_sizes[dimension_index];
uint32_t dst_byte_count = BYTE_COUNT(dst_element_count, m->dst_element_type);
memset(dst_memory, 0, dst_byte_count);
}
return TRUE;
}
static int32_t qb_copy_elements_from_zval(zval *zvalue, int8_t *dst_memory, qb_dimension_mappings *m, uint32_t dimension_index) {
switch(Z_TYPE_P(zvalue)) {
#ifdef IS_CONSTANT_ARRAY
case IS_CONSTANT_ARRAY:
#endif
case IS_ARRAY: return qb_copy_elements_from_array(zvalue, dst_memory, m, dimension_index);
case IS_OBJECT: return qb_copy_elements_from_object(zvalue, dst_memory, m, dimension_index);
case IS_STRING: return qb_copy_elements_from_string(zvalue, dst_memory, m, dimension_index);
case IS_NULL: return qb_copy_elements_from_null(zvalue, dst_memory, m, dimension_index);
case IS_BOOL:
case IS_LONG: return qb_copy_elements_from_long(zvalue, dst_memory, m, dimension_index);
case IS_DOUBLE: return qb_copy_elements_from_double(zvalue, dst_memory, m, dimension_index);
case IS_RESOURCE: {
gdImagePtr image;
php_stream *stream;
if((image = qb_get_gd_image(zvalue))) {
// copy the content from the image
return qb_copy_elements_from_gd_image(image, dst_memory, m, dimension_index);
} else if((stream = qb_get_file_stream(zvalue))) {
// copy the content from the file
return qb_copy_elements_from_file(stream, dst_memory, m, dimension_index);
}
} break;
}
return FALSE;
}
static int32_t qb_copy_element_to_scalar(int8_t *src_memory, zval *zvalue, qb_dimension_mappings *m, uint32_t dimension_index) {
zval_dtor(zvalue);
switch(m->src_element_type) {
case QB_TYPE_S08: {
Z_TYPE_P(zvalue) = IS_LONG;
Z_LVAL_P(zvalue) = *((CTYPE(S08) *) src_memory);
} break;
case QB_TYPE_U08: {
Z_TYPE_P(zvalue) = IS_LONG;
Z_LVAL_P(zvalue) = *((CTYPE(U08) *) src_memory);
} break;
case QB_TYPE_S16: {
Z_TYPE_P(zvalue) = IS_LONG;
Z_LVAL_P(zvalue) = *((CTYPE(S16) *) src_memory);
} break;
case QB_TYPE_U16: {
Z_TYPE_P(zvalue) = IS_LONG;
Z_LVAL_P(zvalue) = *((CTYPE(U16) *) src_memory);
} break;
case QB_TYPE_S32: {
Z_TYPE_P(zvalue) = IS_LONG;
Z_LVAL_P(zvalue) = *((CTYPE(S32) *) src_memory);
} break;
case QB_TYPE_U32: {
#if SIZEOF_LONG == 8
Z_TYPE_P(zvalue) = IS_LONG;
Z_LVAL_P(zvalue) = *((CTYPE(U32) *) src_memory);
#else
CTYPE(U32) value = *((CTYPE(U32) *) src_memory);
if(value & 0x80000000) {
// the sign bit is used--save it to a double
Z_TYPE_P(zvalue) = IS_DOUBLE;
Z_DVAL_P(zvalue) = (double) value;
} else {
// we can safely put it in a 32-bit long
Z_TYPE_P(zvalue) = IS_LONG;
Z_LVAL_P(zvalue) = (long) value;
}
#endif
} break;
case QB_TYPE_S64: {
#if SIZEOF_LONG == 8
Z_TYPE_P(zvalue) = IS_LONG;
Z_LVAL_P(zvalue) = *((CTYPE(S64) *) src_memory);
#else
CTYPE(S64) value = *((CTYPE(S64) *) src_memory);
if(value & 0xFFFFFFFF00000000LL) {
// the upper bits are used--save it to a double to preserve as much info as we can
Z_TYPE_P(zvalue) = IS_DOUBLE;
Z_DVAL_P(zvalue) = (double) value;
} else {
// we can safely put it in a 32-bit long
Z_TYPE_P(zvalue) = IS_LONG;
Z_LVAL_P(zvalue) = (long) value;
}
#endif
} break;
case QB_TYPE_U64: {
#if SIZEOF_LONG == 8
Z_TYPE_P(zvalue) = IS_LONG;
Z_LVAL_P(zvalue) = *((CTYPE(U64) *) src_memory);
#else
CTYPE(U64) value = *((CTYPE(U64) *) src_memory);
if(value & 0xFFFFFFFF80000000LL) {
// the upper bits are used--save it to a double to preserve as much info as we can
Z_TYPE_P(zvalue) = IS_DOUBLE;
Z_DVAL_P(zvalue) = (double) value;
} else {
// we can safely put it in a 32-bit long
Z_TYPE_P(zvalue) = IS_LONG;
Z_LVAL_P(zvalue) = (long) value;
}
#endif
} break;
case QB_TYPE_F32: {
Z_TYPE_P(zvalue) = IS_DOUBLE;
Z_DVAL_P(zvalue) = *((CTYPE(F32) *) src_memory);
} break;
case QB_TYPE_F64: {
Z_TYPE_P(zvalue) = IS_DOUBLE;
Z_DVAL_P(zvalue) = *((CTYPE(F64) *) src_memory);
} break;
}
return TRUE;
}
static int32_t qb_initialize_zval_array(zval *zvalue, zval *container, qb_dimension_mappings *m, uint32_t dimension_index) {
zend_class_entry *ce = NULL;
qb_index_alias_scheme *scheme = m->src_index_alias_schemes[dimension_index];
TSRMLS_FETCH();
if(scheme) {
if(scheme->class_name) {
if(!scheme->zend_class) {
zend_class_entry **p_ce;
if(zend_lookup_class(scheme->class_name, scheme->class_name_length, &p_ce TSRMLS_CC) == FAILURE) {
qb_report_missing_class_exception(0, scheme->class_name);
return FALSE;
} else if((*p_ce)->ce_flags & (ZEND_ACC_INTERFACE|ZEND_ACC_IMPLICIT_ABSTRACT_CLASS|ZEND_ACC_EXPLICIT_ABSTRACT_CLASS)) {
qb_report_abstract_class_exception(0, scheme->class_name);
return FALSE;
}
scheme->zend_class = *p_ce;
}
}
ce = scheme->zend_class;