forked from grommunio/gromox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtype_conversion.cpp
1687 lines (1638 loc) · 49.7 KB
/
type_conversion.cpp
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
// SPDX-License-Identifier: GPL-2.0-only WITH linking exception
// SPDX-FileCopyrightText: 2020–2021 grommunio GmbH
// This file is part of Gromox.
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <string>
#include <fmt/core.h>
#include <libHX/defs.h>
#include <gromox/defs.h>
#include <gromox/mapidefs.h>
#include "type_conversion.hpp"
#include "ext.hpp"
#define TIME_FIXUP_CONSTANT_INT 11644473600LL
using namespace gromox;
uint64_t unix_to_nttime(time_t unix_time)
{
return (static_cast<uint64_t>(unix_time) + TIME_FIXUP_CONSTANT_INT) * 10000000;
}
time_t nttime_to_unix(uint64_t nt_time)
{
return nt_time / 10000000 - TIME_FIXUP_CONSTANT_INT;
}
/* In PHP-MAPI, PT_STRING8 means UTF-8
* string. We do not use PT_UNICODE,
there's no definition for ansi string */
uint32_t proptag_to_phptag(uint32_t proptag)
{
switch (PROP_TYPE(proptag)) {
case PT_UNICODE:
return CHANGE_PROP_TYPE(proptag, PT_STRING8);
case PT_MV_UNICODE:
return CHANGE_PROP_TYPE(proptag, PT_MV_STRING8);
default:
return proptag;
}
}
uint32_t phptag_to_proptag(uint32_t proptag)
{
switch (PROP_TYPE(proptag)) {
case PT_STRING8:
return CHANGE_PROP_TYPE(proptag, PT_UNICODE);
case PT_MV_STRING8:
return CHANGE_PROP_TYPE(proptag, PT_MV_UNICODE);
default:
return proptag;
}
}
ec_error_t php_to_binary_array(zval *pzval, BINARY_ARRAY *pbins)
{
HashTable *ptarget_hash;
if (pzval == nullptr)
return ecInvalidParam;
ZVAL_DEREF(pzval);
ptarget_hash = HASH_OF(pzval);
if (ptarget_hash == nullptr)
return ecInvalidParam;
pbins->count = zend_hash_num_elements(Z_ARRVAL_P(pzval));
if (0 == pbins->count) {
pbins->pbin = NULL;
return ecSuccess;
}
pbins->pbin = sta_malloc<BINARY>(pbins->count);
if (NULL == pbins->pbin) {
pbins->count = 0;
return ecMAPIOOM;
}
size_t i = 0;
zval *entry;
ZEND_HASH_FOREACH_VAL(ptarget_hash, entry) {
zstrplus str(zval_get_string(entry));
pbins->pbin[i].cb = str->len;
if (str->len == 0) {
pbins->pbin[i].pb = NULL;
} else {
pbins->pbin[i].pb = sta_malloc<uint8_t>(pbins->pbin[i].cb);
if (NULL == pbins->pbin[i].pb) {
pbins->pbin[i].cb = 0;
return ecMAPIOOM;
}
memcpy(pbins->pbin[i].pb, str->val, str->len);
}
++i;
} ZEND_HASH_FOREACH_END();
return ecSuccess;
}
ec_error_t binary_array_to_php(const BINARY_ARRAY *pbins, zval *pzval)
{
zarray_init(pzval);
for (size_t i = 0; i < pbins->count; ++i)
add_next_index_stringl(
pzval, reinterpret_cast<const char *>(pbins->pbin[i].pb),
pbins->pbin[i].cb);
return ecSuccess;
}
ec_error_t fb_array_to_php(const std::vector<freebusy_event> &fbs, zval *pzval)
{
zval pzvalfbevent;
zarray_init(pzval);
for (const auto &e : fbs) {
zarray_init(&pzvalfbevent);
add_assoc_long(&pzvalfbevent, "start", e.start_time);
add_assoc_long(&pzvalfbevent, "end", e.end_time);
add_assoc_long(&pzvalfbevent, "busystatus", e.busy_status);
if (!e.has_details) {
add_next_index_zval(pzval, &pzvalfbevent);
continue;
}
if (e.id != nullptr)
add_assoc_string(&pzvalfbevent, "id", e.id);
if (e.subject != nullptr)
add_assoc_string(&pzvalfbevent, "subject", e.subject);
if (e.location != nullptr)
add_assoc_string(&pzvalfbevent, "location", e.location);
add_assoc_bool(&pzvalfbevent, "meeting", e.is_meeting);
add_assoc_bool(&pzvalfbevent, "recurring", e.is_recurring);
add_assoc_bool(&pzvalfbevent, "exception", e.is_exception);
add_assoc_bool(&pzvalfbevent, "reminderset", e.is_reminderset);
add_assoc_bool(&pzvalfbevent, "private", e.is_private);
add_next_index_zval(pzval, &pzvalfbevent);
}
return ecSuccess;
}
ec_error_t php_to_sortorder_set(zval *pzval, SORTORDER_SET *pset)
{
unsigned long idx;
HashTable *ptarget_hash;
if (pzval == nullptr)
return ecInvalidParam;
ZVAL_DEREF(pzval);
ptarget_hash = HASH_OF(pzval);
if (ptarget_hash == nullptr)
return ecInvalidParam;
pset->count = zend_hash_num_elements(Z_ARRVAL_P(pzval));
pset->ccategories = 0;
pset->cexpanded = 0;
if (0 == pset->count) {
pset->psort = NULL;
return ecSuccess;
}
pset->psort = sta_malloc<SORT_ORDER>(pset->count);
if (NULL == pset->psort) {
pset->count = 0;
return ecMAPIOOM;
}
zend_string *key;
zval *entry;
size_t i = 0;
ZEND_HASH_FOREACH_KEY_VAL(ptarget_hash, idx, key, entry) {
uint32_t proptag = phptag_to_proptag(key != nullptr ? strtol(key->val, nullptr, 0) : idx);
pset->psort[i].propid = PROP_ID(proptag);
pset->psort[i].type = PROP_TYPE(proptag);
pset->psort[i].table_sort = zval_get_long(entry);
++i;
} ZEND_HASH_FOREACH_END();
return ecSuccess;
}
ec_error_t php_to_proptag_array(zval *pzval, PROPTAG_ARRAY *pproptags)
{
HashTable *ptarget_hash;
if (pzval == nullptr)
return ecInvalidParam;
ZVAL_DEREF(pzval);
ptarget_hash = HASH_OF(pzval);
if (ptarget_hash == nullptr)
return ecInvalidParam;
pproptags->count = zend_hash_num_elements(ptarget_hash);
if (0 == pproptags->count) {
pproptags->pproptag = NULL;
return ecSuccess;
}
pproptags->pproptag = sta_malloc<uint32_t>(pproptags->count);
if (pproptags->pproptag == nullptr) {
pproptags->count = 0;
return ecMAPIOOM;
}
size_t i = 0;
zval *entry;
ZEND_HASH_FOREACH_VAL(ptarget_hash, entry) {
pproptags->pproptag[i++] = phptag_to_proptag(zval_get_long(entry));
} ZEND_HASH_FOREACH_END();
return ecSuccess;
}
static void *php_to_propval(zval *entry, uint16_t proptype)
{
int j = 0;
void *pvalue;
char *pstring;
zval *data_entry;
ACTION_BLOCK *pblock;
HashTable *pdata_hash;
HashTable *paction_hash;
HashTable *precipient_hash;
TPROPVAL_ARRAY tmp_propvals;
RECIPIENT_BLOCK *prcpt_block;
zstrplus str_action(zend_string_init("action", sizeof("action") - 1, 0));
zstrplus str_flags(zend_string_init("flags", sizeof("flags") - 1, 0));
zstrplus str_flavor(zend_string_init("flavor", sizeof("flavor") - 1, 0));
zstrplus str_storeentryid(zend_string_init("storeentryid", sizeof("storeentryid") - 1, 0));
zstrplus str_folderentryid(zend_string_init("folderentryid", sizeof("folderentryid") - 1, 0));
zstrplus str_replyentryid(zend_string_init("replyentryid", sizeof("replyentryid") - 1, 0));
zstrplus str_replyguid(zend_string_init("replyguid", sizeof("replyguid") - 1, 0));
zstrplus str_dam(zend_string_init("dam", sizeof("dam") - 1, 0));
zstrplus str_code(zend_string_init("code", sizeof("code") - 1, 0));
zstrplus str_adrlist(zend_string_init("adrlist", sizeof("adrlist") - 1, 0));
zstrplus str_proptag(zend_string_init("proptag", sizeof("proptag") - 1, 0));
if (entry == nullptr)
return nullptr;
switch(proptype) {
case PT_SHORT:
pvalue = emalloc(sizeof(uint16_t));
if (pvalue == nullptr)
return NULL;
*static_cast<uint16_t *>(pvalue) = zval_get_long(entry);
break;
case PT_LONG:
case PT_ERROR:
pvalue = emalloc(sizeof(uint32_t));
if (pvalue == nullptr)
return NULL;
*static_cast<uint32_t *>(pvalue) = zval_get_long(entry);
break;
case PT_FLOAT:
pvalue = emalloc(sizeof(float));
if (pvalue == nullptr)
return NULL;
*static_cast<float *>(pvalue) = zval_get_double(entry);
break;
case PT_DOUBLE:
case PT_APPTIME:
pvalue = emalloc(sizeof(double));
if (pvalue == nullptr)
return NULL;
*static_cast<double *>(pvalue) = zval_get_double(entry);
break;
case PT_CURRENCY:
case PT_I8:
pvalue = emalloc(sizeof(uint64_t));
if (pvalue == nullptr)
return NULL;
*static_cast<uint64_t *>(pvalue) = zval_get_double(entry);
break;
case PT_BOOLEAN:
pvalue = emalloc(sizeof(uint8_t));
if (pvalue == nullptr)
return NULL;
*static_cast<uint8_t *>(pvalue) = zval_is_true(entry);
break;
case PT_SYSTIME:
/* convert unix timestamp to nt timestamp */
pvalue = emalloc(sizeof(uint64_t));
if (pvalue == nullptr)
return NULL;
*static_cast<uint64_t *>(pvalue) = unix_to_nttime(zval_get_long(entry));
break;
case PT_BINARY: {
zstrplus str(zval_get_string(entry));
pvalue = emalloc(sizeof(BINARY));
auto bin = static_cast<BINARY *>(pvalue);
if (bin == nullptr)
return NULL;
bin->cb = str->len;
if (str->len == 0) {
bin->pb = nullptr;
} else {
bin->pb = sta_malloc<uint8_t>(str->len);
if (bin->pb == nullptr) {
bin->cb = 0;
return NULL;
}
memcpy(bin->pb, str->val, str->len);
}
break;
}
case PT_STRING8:
case PT_UNICODE: {
zstrplus str(zval_get_string(entry));
pvalue = emalloc(str->len + 1);
if (pvalue == nullptr)
return NULL;
memcpy(pvalue, str->val, str->len);
static_cast<char *>(pvalue)[str->len] = '\0';
break;
}
case PT_CLSID: {
zstrplus str(zval_get_string(entry));
if (str->len != sizeof(GUID))
return NULL;
pvalue = emalloc(sizeof(GUID));
if (pvalue == nullptr)
return NULL;
memcpy(pvalue, str->val, sizeof(GUID));
break;
}
case PT_MV_SHORT: {
ZVAL_DEREF(entry);
pdata_hash = HASH_OF(entry);
if (pdata_hash == nullptr)
return NULL;
pvalue = emalloc(sizeof(SHORT_ARRAY));
auto xs = static_cast<SHORT_ARRAY *>(pvalue);
if (xs == nullptr)
return NULL;
xs->count = zend_hash_num_elements(pdata_hash);
if (xs->count == 0) {
xs->ps = nullptr;
break;
}
xs->ps = sta_malloc<uint16_t>(xs->count);
if (xs->ps == nullptr) {
xs->count = 0;
return NULL;
}
ZEND_HASH_FOREACH_VAL(pdata_hash, data_entry) {
xs->ps[j++] = zval_get_long(data_entry);
} ZEND_HASH_FOREACH_END();
break;
}
case PT_MV_LONG: {
ZVAL_DEREF(entry);
pdata_hash = HASH_OF(entry);
if (pdata_hash == nullptr)
return NULL;
pvalue = emalloc(sizeof(LONG_ARRAY));
auto xl = static_cast<LONG_ARRAY *>(pvalue);
if (xl == nullptr)
return NULL;
xl->count = zend_hash_num_elements(pdata_hash);
if (xl->count == 0) {
xl->pl = nullptr;
break;
}
xl->pl = sta_malloc<uint32_t>(xl->count);
if (xl->pl == nullptr) {
xl->count = 0;
return NULL;
}
ZEND_HASH_FOREACH_VAL(pdata_hash, data_entry) {
xl->pl[j++] = zval_get_long(data_entry);
} ZEND_HASH_FOREACH_END();
break;
}
case PT_MV_CURRENCY:
case PT_MV_I8: {
ZVAL_DEREF(entry);
pdata_hash = HASH_OF(entry);
if (pdata_hash == nullptr)
return NULL;
pvalue = emalloc(sizeof(LONGLONG_ARRAY));
auto xl = static_cast<LONGLONG_ARRAY *>(pvalue);
if (xl == nullptr)
return NULL;
xl->count = zend_hash_num_elements(pdata_hash);
if (xl->count == 0) {
xl->pll = nullptr;
break;
}
xl->pll = sta_malloc<uint64_t>(xl->count);
if (xl->pll == nullptr) {
xl->count = 0;
return NULL;
}
ZEND_HASH_FOREACH_VAL(pdata_hash, data_entry) {
xl->pll[j++] = zval_get_double(data_entry);
} ZEND_HASH_FOREACH_END();
break;
}
case PT_MV_FLOAT: {
ZVAL_DEREF(entry);
pdata_hash = HASH_OF(entry);
if (pdata_hash == nullptr)
return NULL;
pvalue = emalloc(sizeof(FLOAT_ARRAY));
auto xl = static_cast<FLOAT_ARRAY *>(pvalue);
if (xl == nullptr)
return NULL;
xl->count = zend_hash_num_elements(pdata_hash);
if (xl->count == 0) {
xl->mval = nullptr;
break;
}
xl->mval = sta_malloc<float>(xl->count);
if (xl->mval == nullptr) {
xl->count = 0;
return NULL;
}
ZEND_HASH_FOREACH_VAL(pdata_hash, data_entry) {
xl->mval[j++] = zval_get_double(data_entry);
} ZEND_HASH_FOREACH_END();
break;
}
case PT_MV_DOUBLE:
case PT_MV_APPTIME: {
ZVAL_DEREF(entry);
pdata_hash = HASH_OF(entry);
if (pdata_hash == nullptr)
return NULL;
pvalue = emalloc(sizeof(DOUBLE_ARRAY));
auto xl = static_cast<DOUBLE_ARRAY *>(pvalue);
if (xl == nullptr)
return NULL;
xl->count = zend_hash_num_elements(pdata_hash);
if (xl->count == 0) {
xl->mval = nullptr;
break;
}
xl->mval = sta_malloc<double>(xl->count);
if (xl->mval == nullptr) {
xl->count = 0;
return NULL;
}
ZEND_HASH_FOREACH_VAL(pdata_hash, data_entry) {
xl->mval[j++] = zval_get_double(data_entry);
} ZEND_HASH_FOREACH_END();
break;
}
case PT_MV_STRING8:
case PT_MV_UNICODE: {
ZVAL_DEREF(entry);
pdata_hash = HASH_OF(entry);
if (pdata_hash == nullptr)
return NULL;
pvalue = emalloc(sizeof(STRING_ARRAY));
auto xs = static_cast<STRING_ARRAY *>(pvalue);
if (xs == nullptr)
return NULL;
xs->count = zend_hash_num_elements(pdata_hash);
if (xs->count == 0) {
xs->ppstr = nullptr;
break;
}
xs->ppstr = sta_malloc<char *>(xs->count);
if (xs->ppstr == nullptr) {
xs->count = 0;
return NULL;
}
ZEND_HASH_FOREACH_VAL(pdata_hash, data_entry) {
zstrplus str(zval_get_string(data_entry));
pstring = sta_malloc<char>(str->len + 1);
if (pstring == nullptr)
return NULL;
xs->ppstr[j++] = pstring;
memcpy(pstring, str->val, str->len);
pstring[str->len] = '\0';
} ZEND_HASH_FOREACH_END();
break;
}
case PT_MV_SYSTIME: {
ZVAL_DEREF(entry);
pdata_hash = HASH_OF(entry);
if (pdata_hash == nullptr)
return nullptr;
pvalue = emalloc(sizeof(LONGLONG_ARRAY));
auto xl = static_cast<LONGLONG_ARRAY *>(pvalue);
if (xl == nullptr)
return nullptr;
xl->count = zend_hash_num_elements(pdata_hash);
if (xl->count == 0) {
xl->pll = nullptr;
break;
}
xl->pll = sta_malloc<uint64_t>(xl->count);
if (xl->pll == nullptr) {
xl->count = 0;
return NULL;
}
ZEND_HASH_FOREACH_VAL(pdata_hash, data_entry) {
xl->pll[j++] = unix_to_nttime(zval_get_long(data_entry));
} ZEND_HASH_FOREACH_END();
break;
}
case PT_MV_BINARY: {
ZVAL_DEREF(entry);
pdata_hash = HASH_OF(entry);
if (pdata_hash == nullptr)
return NULL;
pvalue = emalloc(sizeof(BINARY_ARRAY));
auto xb = static_cast<BINARY_ARRAY *>(pvalue);
if (xb == nullptr)
return NULL;
xb->count = zend_hash_num_elements(pdata_hash);
if (xb->count == 0) {
xb->pbin = nullptr;
break;
}
xb->pbin = sta_malloc<BINARY>(xb->count);
if (xb->pbin == nullptr) {
xb->count = 0;
return NULL;
}
ZEND_HASH_FOREACH_VAL(pdata_hash, data_entry) {
zstrplus str(zval_get_string(data_entry));
xb->pbin[j].cb = str->len;
if (str->len == 0) {
xb->pbin[j].pb = NULL;
} else {
xb->pbin[j].pb = sta_malloc<uint8_t>(str->len);
if (xb->pbin[j].pb == nullptr)
return NULL;
memcpy(xb->pbin[j].pb, str->val, str->len);
}
++j;
} ZEND_HASH_FOREACH_END();
break;
}
case PT_MV_CLSID: {
ZVAL_DEREF(entry);
pdata_hash = HASH_OF(entry);
if (pdata_hash == nullptr)
return NULL;
pvalue = emalloc(sizeof(GUID_ARRAY));
auto xb = static_cast<GUID_ARRAY *>(pvalue);
if (xb == nullptr)
return NULL;
xb->count = zend_hash_num_elements(pdata_hash);
if (xb->count == 0) {
xb->pguid = nullptr;
break;
}
xb->pguid = sta_malloc<GUID>(xb->count);
if (xb->pguid == nullptr) {
xb->count = 0;
return NULL;
}
ZEND_HASH_FOREACH_VAL(pdata_hash, data_entry) {
zstrplus str(zval_get_string(data_entry));
if (str->len != sizeof(GUID))
return NULL;
memcpy(&xb->pguid[j], Z_STRVAL_P(data_entry), sizeof(GUID));
} ZEND_HASH_FOREACH_END();
break;
}
case PT_ACTIONS: {
pvalue = emalloc(sizeof(RULE_ACTIONS));
auto xr = static_cast<RULE_ACTIONS *>(pvalue);
if (xr == nullptr)
return NULL;
ZVAL_DEREF(entry);
pdata_hash = HASH_OF(entry);
if (NULL == pdata_hash) {
xr->count = 0;
xr->pblock = NULL;
break;
}
xr->count = zend_hash_num_elements(pdata_hash);
if (xr->count == 0) {
xr->pblock = nullptr;
break;
}
xr->pblock = sta_malloc<ACTION_BLOCK>(xr->count);
if (xr->pblock == nullptr) {
xr->count = 0;
return NULL;
}
ZEND_HASH_FOREACH_VAL(pdata_hash, data_entry) {
ZVAL_DEREF(data_entry);
paction_hash = HASH_OF(data_entry);
if (paction_hash == nullptr)
return NULL;
data_entry = zend_hash_find(paction_hash, str_action.get());
if (data_entry == nullptr)
return NULL;
pblock = &xr->pblock[j];
pblock->type = zval_get_long(data_entry);
/* option field user defined flags, default 0 */
data_entry = zend_hash_find(paction_hash, str_flags.get());
pblock->flags = data_entry != nullptr ? zval_get_long(data_entry) : 0;
/* option field used with OP_REPLAY and OP_FORWARD, default 0 */
data_entry = zend_hash_find(paction_hash, str_flavor.get());
pblock->flavor = data_entry != nullptr ? zval_get_long(data_entry) : 0;
switch (pblock->type) {
case OP_MOVE:
case OP_COPY: {
pblock->pdata = emalloc(sizeof(ZMOVECOPY_ACTION));
auto xq = static_cast<ZMOVECOPY_ACTION *>(pblock->pdata);
if (xq == nullptr)
return NULL;
data_entry = zend_hash_find(paction_hash, str_storeentryid.get());
if (data_entry == nullptr)
return NULL;
zstrplus str1(zval_get_string(data_entry));
xq->store_eid.cb = str1->len;
xq->store_eid.pb = sta_malloc<uint8_t>(str1->len);
if (xq->store_eid.pb == nullptr) {
xq->store_eid.cb = 0;
return NULL;
}
memcpy(xq->store_eid.pb, str1->val, str1->len);
data_entry = zend_hash_find(paction_hash, str_folderentryid.get());
if (data_entry == nullptr)
return NULL;
zstrplus str2(zval_get_string(data_entry));
xq->folder_eid.cb = str2->len;
xq->folder_eid.pb = sta_malloc<uint8_t>(str2->len);
if (xq->folder_eid.pb == nullptr)
return NULL;
memcpy(xq->folder_eid.pb, str2->val, str2->len);
break;
}
case OP_REPLY:
case OP_OOF_REPLY: {
data_entry = zend_hash_find(paction_hash, str_replyentryid.get());
if (data_entry == nullptr)
return NULL;
zstrplus str1(zval_get_string(data_entry));
pblock->pdata = emalloc(sizeof(ZREPLY_ACTION));
auto xq = static_cast<ZREPLY_ACTION *>(pblock->pdata);
if (xq == nullptr)
return NULL;
xq->message_eid.cb = str1->len;
xq->message_eid.pb = sta_malloc<uint8_t>(str1->len);
if (xq->message_eid.pb == nullptr) {
xq->message_eid.cb = 0;
return NULL;
}
memcpy(xq->message_eid.pb, str1->val, str1->len);
data_entry = zend_hash_find(paction_hash, str_replyguid.get());
if (data_entry != nullptr) {
zstrplus str2(zval_get_string(data_entry));
if (str2->len != sizeof(GUID))
return NULL;
memcpy(&xq->template_guid, str2->val, sizeof(GUID));
} else {
memset(&xq->template_guid, 0, sizeof(GUID));
}
break;
}
case OP_DEFER_ACTION: {
data_entry = zend_hash_find(paction_hash, str_dam.get());
if (data_entry == nullptr)
return NULL;
zstrplus str1(zval_get_string(data_entry));
if (str1->len == 0)
return NULL;
pblock->length = str1->len + sizeof(uint8_t) + 2 * sizeof(uint32_t);
pblock->pdata = emalloc(str1->len);
if (pblock->pdata == nullptr)
return NULL;
memcpy(pblock->pdata, str1->val, str1->len);
break;
}
case OP_BOUNCE:
data_entry = zend_hash_find(paction_hash, str_code.get());
if (data_entry == nullptr)
return NULL;
pblock->pdata = emalloc(sizeof(uint32_t));
if (pblock->pdata == nullptr)
return NULL;
*static_cast<uint32_t *>(pblock->pdata) = zval_get_long(data_entry);
break;
case OP_FORWARD:
case OP_DELEGATE: {
data_entry = zend_hash_find(paction_hash, str_adrlist.get());
if (data_entry == nullptr || Z_TYPE_P(data_entry) != IS_ARRAY)
return NULL;
pblock->pdata = emalloc(sizeof(FORWARDDELEGATE_ACTION));
auto xq = static_cast<FORWARDDELEGATE_ACTION *>(pblock->pdata);
if (xq == nullptr)
return NULL;
ZVAL_DEREF(data_entry);
precipient_hash = HASH_OF(data_entry);
xq->count = zend_hash_num_elements(precipient_hash);
if (xq->count == 0)
return NULL;
xq->pblock = sta_malloc<RECIPIENT_BLOCK>(xq->count);
if (xq->pblock == nullptr) {
xq->count = 0;
return NULL;
}
int k = 0;
ZEND_HASH_FOREACH_VAL(precipient_hash, data_entry) {
auto err = php_to_tpropval_array(data_entry, &tmp_propvals);
if (err != ecSuccess)
return NULL;
prcpt_block = &xq->pblock[k];
prcpt_block->reserved = 0;
prcpt_block->count = tmp_propvals.count;
prcpt_block->ppropval = tmp_propvals.ppropval;
++k;
} ZEND_HASH_FOREACH_END();
break;
}
case OP_TAG: {
data_entry = zend_hash_find(paction_hash, str_proptag.get());
if (data_entry == nullptr)
return NULL;
auto err = php_to_tpropval_array(data_entry, &tmp_propvals);
if (err != ecSuccess)
return NULL;
if (tmp_propvals.count != 1)
return NULL;
pblock->pdata = tmp_propvals.ppropval;
break;
}
case OP_DELETE:
case OP_MARK_AS_READ:
pblock->pdata = NULL;
break;
default:
return NULL;
}
++j;
} ZEND_HASH_FOREACH_END();
break;
}
case PT_SRESTRICTION: {
pvalue = emalloc(sizeof(RESTRICTION));
if (pvalue == nullptr)
return NULL;
auto err = php_to_restriction(entry, static_cast<RESTRICTION *>(pvalue));
if (err != ecSuccess)
return NULL;
break;
}
default:
return NULL;
}
return pvalue;
}
ec_error_t php_to_tpropval_array(zval *pzval, TPROPVAL_ARRAY *ppropvals)
{
zend_string *pstring;
unsigned long idx;
HashTable *ptarget_hash;
if (pzval == nullptr)
return ecInvalidParam;
ZVAL_DEREF(pzval);
ptarget_hash = HASH_OF(pzval);
if (ptarget_hash == nullptr)
return ecInvalidParam;
ppropvals->count = zend_hash_num_elements(ptarget_hash);
if (0 == ppropvals->count) {
ppropvals->ppropval = NULL;
return ecSuccess;
}
ppropvals->ppropval = sta_malloc<TAGGED_PROPVAL>(ppropvals->count);
if (NULL == ppropvals->ppropval) {
ppropvals->count = 0;
return ecMAPIOOM;
}
zval *entry;
size_t i = 0;
ZEND_HASH_FOREACH_KEY_VAL(ptarget_hash, idx, pstring, entry) {
static_cast<void>(pstring);
ppropvals->ppropval[i].proptag = phptag_to_proptag(idx);
ppropvals->ppropval[i].pvalue = php_to_propval(entry, PROP_TYPE(idx));
if (ppropvals->ppropval[i].pvalue == nullptr)
return ecError;
++i;
} ZEND_HASH_FOREACH_END();
return ecSuccess;
}
ec_error_t php_to_tarray_set(zval *pzval, TARRAY_SET *pset)
{
HashTable *ptarget_hash;
if (pzval == nullptr)
return ecInvalidParam;
ZVAL_DEREF(pzval);
if (Z_TYPE_P(pzval) != IS_ARRAY)
return ecInvalidParam;
ptarget_hash = HASH_OF(pzval);
if (ptarget_hash == nullptr)
return ecInvalidParam;
pset->count = zend_hash_num_elements(ptarget_hash);
if (0 == pset->count) {
pset->pparray = NULL;
return ecSuccess;
}
pset->pparray = sta_malloc<TPROPVAL_ARRAY *>(pset->count);
if (NULL == pset->pparray) {
pset->count = 0;
return ecMAPIOOM;
}
zval *entry;
size_t i = 0;
ZEND_HASH_FOREACH_VAL(ptarget_hash, entry) {
if (Z_TYPE_P(entry) != IS_ARRAY)
return ecInvalidParam;
pset->pparray[i] = st_malloc<TPROPVAL_ARRAY>();
if (pset->pparray[i] == nullptr)
return ecMAPIOOM;
auto err = php_to_tpropval_array(entry, pset->pparray[i]);
if (err != ecSuccess)
return err;
++i;
} ZEND_HASH_FOREACH_END();
return ecSuccess;
}
ec_error_t php_to_rule_list(zval *pzval, RULE_LIST *plist)
{
zstrplus str_properties(zend_string_init("properties", sizeof("properties") - 1, 0));
zstrplus str_rowflags(zend_string_init("rowflags", sizeof("rowflags") - 1, 0));
HashTable *ptarget_hash;
if (pzval == nullptr)
return ecInvalidParam;
ZVAL_DEREF(pzval);
if (Z_TYPE_P(pzval) != IS_ARRAY)
return ecInvalidParam;
ptarget_hash = HASH_OF(pzval);
if (ptarget_hash == nullptr)
return ecInvalidParam;
plist->count = zend_hash_num_elements(ptarget_hash);
if (0 == plist->count) {
plist->prule = NULL;
return ecSuccess;
}
plist->prule = sta_malloc<RULE_DATA>(plist->count);
if (NULL == plist->prule) {
plist->count = 0;
return ecMAPIOOM;
}
zval *entry;
size_t i = 0;
ZEND_HASH_FOREACH_VAL(ptarget_hash, entry) {
ZVAL_DEREF(entry);
if (Z_TYPE_P(entry) != IS_ARRAY)
return ecInvalidParam;
auto data = zend_hash_find(HASH_OF(entry), str_properties.get());
if (data == nullptr)
return ecInvalidParam;
auto err = php_to_tpropval_array(data, &plist->prule[i].propvals);
if (err != ecSuccess)
return err;
data = zend_hash_find(HASH_OF(entry), str_rowflags.get());
if (data == nullptr)
return ecInvalidParam;
plist->prule[i].flags = zval_get_long(data);
++i;
} ZEND_HASH_FOREACH_END();
return ecSuccess;
}
#define IDX_VALUE 0
#define IDX_RELOP 1
#define IDX_FUZZYLEVEL 2
#define IDX_SIZE 3
#define IDX_TYPE 4
#define IDX_MASK 5
#define IDX_PROPTAG 6
#define IDX_PROPTAG1 7
#define IDX_PROPTAG2 8
#define IDX_PROPVALS 9
#define IDX_RESTRICTION 10
ec_error_t php_to_restriction(zval *pzval, RESTRICTION *pres)
{
int i;
HashTable *pres_hash;
HashTable *pdata_hash;
TPROPVAL_ARRAY tmp_propvals;
if (pzval == nullptr)
return ecInvalidParam;
ZVAL_DEREF(pzval);
pres_hash = HASH_OF(pzval);
if (pres_hash == nullptr || zend_hash_num_elements(pres_hash) != 2)
return ecInvalidParam;
HashPosition hpos;
zend_hash_internal_pointer_reset_ex(pres_hash, &hpos);
/* 0=>type, 1=>value array */
auto type_entry = zend_hash_get_current_data_ex(pres_hash, &hpos);
zend_hash_move_forward_ex(pres_hash, &hpos);
auto value_entry = zend_hash_get_current_data_ex(pres_hash, &hpos);
pres->rt = static_cast<mapi_rtype>(zval_get_long(type_entry));
ZVAL_DEREF(value_entry);
pdata_hash = HASH_OF(value_entry);
if (pdata_hash == nullptr)
return ecInvalidParam;
switch(pres->rt) {
case RES_AND:
case RES_OR: {
pres->pres = emalloc(sizeof(RESTRICTION_AND_OR));
auto andor = pres->andor;
if (andor == nullptr)
return ecMAPIOOM;
andor->count = zend_hash_num_elements(pdata_hash);
andor->pres = sta_malloc<RESTRICTION>(andor->count);
if (andor->pres == nullptr) {
andor->count = 0;
return ecMAPIOOM;
}
i = 0;
ZEND_HASH_FOREACH_VAL(pdata_hash, value_entry) {
auto err = php_to_restriction(value_entry, &andor->pres[i++]);
if (err != ecSuccess)
return err;
} ZEND_HASH_FOREACH_END();
break;
}
case RES_NOT: {
pres->pres = emalloc(sizeof(RESTRICTION_NOT));
auto rnot = pres->xnot;
if (rnot == nullptr)
return ecMAPIOOM;
HashPosition hpos2;
zend_hash_internal_pointer_reset_ex(pdata_hash, &hpos2);
value_entry = zend_hash_get_current_data_ex(pdata_hash, &hpos2);
auto err = php_to_restriction(value_entry, &rnot->res);
if (err != ecSuccess)
return err;
break;
}
case RES_SUBRESTRICTION: {
pres->pres = emalloc(sizeof(RESTRICTION_SUBOBJ));
auto rsub = pres->sub;
if (rsub == nullptr)
return ecMAPIOOM;
value_entry = zend_hash_index_find(pdata_hash, IDX_PROPTAG);
if (value_entry == nullptr)
return ecInvalidParam;
rsub->subobject = phptag_to_proptag(zval_get_long(value_entry));
value_entry = zend_hash_index_find(pdata_hash, IDX_RESTRICTION);
if (value_entry == nullptr)
return ecInvalidParam;
auto err = php_to_restriction(value_entry, &rsub->res);
if (err != ecSuccess)
return err;
break;
}
case RES_COMMENT:
case RES_ANNOTATION: {
pres->pres = emalloc(sizeof(RESTRICTION_COMMENT));
auto rcom = pres->comment;
if (rcom == nullptr)
return ecMAPIOOM;
rcom->pres = st_malloc<RESTRICTION>();
if (rcom->pres == nullptr)
/* memory leak */
return ecMAPIOOM;
value_entry = zend_hash_index_find(pdata_hash, IDX_RESTRICTION);
if (value_entry == nullptr)
return ecInvalidParam;
auto err = php_to_restriction(value_entry, rcom->pres);
if (err != ecSuccess)
return err;
value_entry = zend_hash_index_find(pdata_hash, IDX_PROPVALS);
if (value_entry == nullptr)
return ecInvalidParam;
err = php_to_tpropval_array(value_entry, &tmp_propvals);
if (err != ecSuccess)
return err;
rcom->count = tmp_propvals.count;
rcom->ppropval = tmp_propvals.ppropval;
break;
}
case RES_CONTENT: {
pres->pres = emalloc(sizeof(RESTRICTION_CONTENT));
auto rcon = pres->cont;
if (rcon == nullptr)
return ecMAPIOOM;
value_entry = zend_hash_index_find(pdata_hash, IDX_PROPTAG);
if (value_entry == nullptr)
return ecInvalidParam;
rcon->proptag = phptag_to_proptag(zval_get_long(value_entry));
value_entry = zend_hash_index_find(pdata_hash, IDX_FUZZYLEVEL);
if (value_entry == nullptr)
return ecInvalidParam;
rcon->fuzzy_level = zval_get_long(value_entry);
value_entry = zend_hash_index_find(pdata_hash, IDX_VALUE);
if (value_entry == nullptr)
return ecInvalidParam;
if (Z_TYPE_P(value_entry) == IS_ARRAY) {
auto err = php_to_tpropval_array(value_entry, &tmp_propvals);
if (err != ecSuccess)
return err;
if (tmp_propvals.count != 1)
return ecInvalidParam;
rcon->propval = *tmp_propvals.ppropval;
} else {
rcon->propval.proptag = rcon->proptag;