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
|
/*
+----------------------------------------------------------------------+
| PHP version 4.0 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2001 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 2.02 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available at through the world-wide-web at |
| http://www.php.net/license/2_02.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: Harald Radi <h.radi@nme.at> |
| Alan Brown <abrown@pobox.com> |
| Paul Shortis <pshortis@dataworx.com.au> |
+----------------------------------------------------------------------+
*/
/*
* 03.6.2001
* Added SafeArray ==> Hash support
*/
/*
* Paul Shortis June 7, 2001 <pshortis@dataworx.com.au> - Added code to support SafeArray passing
* to COM objects. Support includes passing arrays of variants as well
* as typed arrays.
*/
#ifdef PHP_WIN32
#include "php.h"
#include "php_COM.h"
#include "php_VARIANT.h"
/* prototypes */
PHPAPI void php_pval_to_variant(pval *pval_arg, VARIANT *var_arg, int codepage TSRMLS_DC);
PHPAPI void php_pval_to_variant_ex(pval *pval_arg, VARIANT *var_arg, pval *pval_type, int codepage TSRMLS_DC);
PHPAPI int php_variant_to_pval(VARIANT *var_arg, pval *pval_arg, int persistent, int codepage);
PHPAPI OLECHAR *php_char_to_OLECHAR(char *C_str, uint strlen, int codepage);
PHPAPI char *php_OLECHAR_to_char(OLECHAR *unicode_str, uint *out_length, int persistent, int codepage);
static void pval_to_variant_ex(pval *pval_arg, VARIANT *var_arg, int type, int codepage TSRMLS_DC);
static void comval_to_variant(pval *pval_arg, VARIANT *var_arg TSRMLS_DC);
/* implementations */
PHPAPI void php_pval_to_variant(pval *pval_arg, VARIANT *var_arg, int codepage TSRMLS_DC)
{
int type = VT_EMPTY; /* default variant type */
switch(Z_TYPE_P(pval_arg))
{
case IS_NULL:
type = VT_NULL;
break;
case IS_BOOL:
type = VT_BOOL;
break;
case IS_OBJECT:
if(!strcmp(Z_OBJCE_P(pval_arg)->name, "VARIANT"))
{
type = VT_VARIANT;
}
else if(!strcmp(Z_OBJCE_P(pval_arg)->name, "COM"))
{
type = VT_DISPATCH;
}
break;
case IS_ARRAY:
type = VT_ARRAY;
break;
case IS_RESOURCE:
case IS_CONSTANT:
case IS_CONSTANT_ARRAY:
/* ?? */
break;
case IS_LONG:
type = VT_I4; /* assuming 32-bit platform */
break;
case IS_DOUBLE:
type = VT_R8; /* assuming 64-bit double precision */
break;
case IS_STRING:
type = VT_BSTR;
break;
}
if(pval_arg->is_ref) /* deprecated, implemented for downwards compatiblity */
{
type |= VT_BYREF;
}
pval_to_variant_ex(pval_arg, var_arg, type, codepage TSRMLS_CC);
}
PHPAPI void php_pval_to_variant_ex(pval *pval_arg, VARIANT *var_arg, pval *pval_type, int codepage TSRMLS_DC)
{
pval_to_variant_ex(pval_arg, var_arg, Z_LVAL_P(pval_type), codepage TSRMLS_CC);
}
static void pval_to_variant_ex(pval *pval_arg, VARIANT *var_arg, int type, int codepage TSRMLS_DC)
{
OLECHAR *unicode_str;
VariantInit(var_arg);
V_VT(var_arg) = type;
if(V_VT(var_arg) & VT_ARRAY)
{
/* For now we'll just handle single dimension arrays, we'll use the data type of the first element for the
output data type */
HashTable *ht = Z_ARRVAL(*pval_arg);
int numberOfElements = zend_hash_num_elements(ht);
SAFEARRAY *safeArray;
SAFEARRAYBOUND bounds[1];
VARIANT *v;
zval **entry; /* An entry in the input array */
type &= ~VT_ARRAY;
if(V_VT(var_arg) == (VT_ARRAY|VT_BYREF)) /* == is intended, because VT_*|VT_BYREF|VT_ARRAY means something diffrent */
{
type &= ~VT_BYREF;
ALLOC_VARIANT(V_VARIANTREF(var_arg));
var_arg = V_VARIANTREF(var_arg); /* put the array in that VARIANT */
}
bounds[0].lLbound = 0;
bounds[0].cElements = numberOfElements;
safeArray = SafeArrayCreate(VT_VARIANT, 1, bounds);
if(NULL == safeArray)
{
php_error( E_WARNING, "Unable to convert php array to VARIANT array - %s", numberOfElements ? "" : "(Empty input array)");
ZVAL_FALSE(pval_arg);
}
else
{
V_ARRAY(var_arg) = safeArray;
V_VT(var_arg) = VT_ARRAY|VT_VARIANT; /* Now have a valid safe array allocated */
if(SUCCEEDED(SafeArrayLock( safeArray)))
{
ulong i;
zend_hash_internal_pointer_reset(ht);
for( i = 0; i < (ulong)numberOfElements; ++i)
{
if((zend_hash_get_current_data(ht, (void **)&entry) == SUCCESS) && (entry != NULL)) /* Get a pointer to the php array element */
{
/* Add another value to the safe array */
if(SUCCEEDED(SafeArrayPtrOfIndex( safeArray, &i, &v)))/* Pointer to output element entry retrieved successfully */
{
if(type) /* explicit type */
{
pval_to_variant_ex(*entry, v, type, codepage TSRMLS_CC); /* Do the required conversion */
}
else
{
php_pval_to_variant(*entry, v, codepage TSRMLS_CC); /* Do the required conversion */
}
}
else
{
php_error( E_WARNING, "phpArrayToSafeArray() - Unable to retrieve pointer to output element number (%d)", i);
}
}
zend_hash_move_forward(ht);
}
SafeArrayUnlock( safeArray);
}
else
{
php_error( E_WARNING, "phpArrayToSafeArray() - Unable to lock safeArray");
}
}
}
else
{
switch(V_VT(var_arg))
{
case VT_UI1:
convert_to_long_ex(&pval_arg);
V_UI1(var_arg) = (unsigned char)Z_LVAL_P(pval_arg);
break;
case VT_I2:
convert_to_long_ex(&pval_arg);
V_I2(var_arg) = (short)Z_LVAL_P(pval_arg);
break;
case VT_I4:
convert_to_long_ex(&pval_arg);
V_I4(var_arg) = Z_LVAL_P(pval_arg);
break;
case VT_R4:
convert_to_double_ex(&pval_arg);
V_R4(var_arg) = (float)Z_DVAL_P(pval_arg);
break;
case VT_R8:
convert_to_double_ex(&pval_arg);
V_R8(var_arg) = Z_DVAL_P(pval_arg);
break;
case VT_BOOL:
convert_to_boolean_ex(&pval_arg);
V_BOOL(var_arg) = (short)Z_LVAL_P(pval_arg);
break;
case VT_ERROR:
convert_to_long_ex(&pval_arg);
V_ERROR(var_arg) = Z_LVAL_P(pval_arg);
break;
case VT_CY:
convert_to_double_ex(&pval_arg);
VarCyFromR8(Z_DVAL_P(pval_arg), &V_CY(var_arg));
break;
case VT_DATE:
{
SYSTEMTIME wintime;
struct tm *phptime;
phptime = gmtime(&(pval_arg->value.lval));
memset(&wintime, 0, sizeof(wintime));
wintime.wYear = phptime->tm_year + 1900;
wintime.wMonth = phptime->tm_mon + 1;
wintime.wDay = phptime->tm_mday;
wintime.wHour = phptime->tm_hour;
wintime.wMinute = phptime->tm_min;
wintime.wSecond = phptime->tm_sec;
SystemTimeToVariantTime(&wintime, &V_DATE(var_arg));
}
break;
case VT_BSTR:
convert_to_string_ex(&pval_arg);
unicode_str = php_char_to_OLECHAR(Z_STRVAL_P(pval_arg), Z_STRLEN_P(pval_arg), codepage);
V_BSTR(var_arg) = SysAllocString(unicode_str);
efree(unicode_str);
break;
case VT_DECIMAL:
convert_to_string_ex(&pval_arg);
unicode_str = php_char_to_OLECHAR(Z_STRVAL_P(pval_arg), Z_STRLEN_P(pval_arg), codepage);
VarDecFromStr(unicode_str, LOCALE_SYSTEM_DEFAULT, 0, &V_DECIMAL(var_arg));
break;
case VT_DECIMAL|VT_BYREF:
convert_to_string_ex(&pval_arg);
unicode_str = php_char_to_OLECHAR(Z_STRVAL_P(pval_arg), Z_STRLEN_P(pval_arg), codepage);
VarDecFromStr(unicode_str, LOCALE_SYSTEM_DEFAULT, 0, V_DECIMALREF(var_arg));
break;
case VT_UNKNOWN:
comval_to_variant(pval_arg, var_arg TSRMLS_CC);
if(V_VT(var_arg) != VT_DISPATCH)
{
VariantInit(var_arg);
}
else
{
V_VT(var_arg) = VT_UNKNOWN;
V_UNKNOWN(var_arg) = (IUnknown *) V_DISPATCH(var_arg);
}
break;
case VT_DISPATCH:
comval_to_variant(pval_arg, var_arg TSRMLS_CC);
if(V_VT(var_arg) != VT_DISPATCH)
{
VariantInit(var_arg);
}
break;
case VT_UI1|VT_BYREF:
convert_to_long(pval_arg);
V_UI1REF(var_arg) = (unsigned char FAR*) &Z_LVAL_P(pval_arg);
break;
case VT_I2|VT_BYREF:
convert_to_long(pval_arg);
V_I2REF(var_arg) = (short FAR*) &Z_LVAL_P(pval_arg);
break;
case VT_I4|VT_BYREF:
convert_to_long(pval_arg);
V_I4REF(var_arg) = (long FAR*) &Z_LVAL_P(pval_arg);
break;
case VT_R4|VT_BYREF:
convert_to_double(pval_arg);
V_R4REF(var_arg) = (float FAR*) &Z_LVAL_P(pval_arg);
break;
case VT_R8|VT_BYREF:
convert_to_double(pval_arg);
V_R8REF(var_arg) = (double FAR*) &Z_LVAL_P(pval_arg);
break;
case VT_BOOL|VT_BYREF:
convert_to_boolean(pval_arg);
V_BOOLREF(var_arg) = (short FAR*) &Z_LVAL_P(pval_arg);
break;
case VT_ERROR|VT_BYREF:
convert_to_long(pval_arg);
V_ERRORREF(var_arg) = (long FAR*) &Z_LVAL_P(pval_arg);
break;
case VT_CY|VT_BYREF:
convert_to_double_ex(&pval_arg);
VarCyFromR8(pval_arg->value.dval, var_arg->pcyVal);
break;
case VT_DATE|VT_BYREF:
{
SYSTEMTIME wintime;
struct tm *phptime;
phptime = gmtime(&(pval_arg->value.lval));
memset(&wintime, 0, sizeof(wintime));
wintime.wYear = phptime->tm_year + 1900;
wintime.wMonth = phptime->tm_mon + 1;
wintime.wDay = phptime->tm_mday;
wintime.wHour = phptime->tm_hour;
wintime.wMinute = phptime->tm_min;
wintime.wSecond = phptime->tm_sec;
SystemTimeToVariantTime(&wintime, var_arg->pdate);
}
break;
case VT_BSTR|VT_BYREF:
convert_to_string(pval_arg);
V_BSTRREF(var_arg) = (BSTR FAR*) emalloc(sizeof(BSTR FAR*));
unicode_str = php_char_to_OLECHAR(Z_STRVAL_P(pval_arg), Z_STRLEN_P(pval_arg), codepage);
*V_BSTRREF(var_arg) = SysAllocString(unicode_str);
efree(unicode_str);
break;
case VT_UNKNOWN|VT_BYREF:
comval_to_variant(pval_arg, var_arg TSRMLS_CC);
if(V_VT(var_arg) != VT_DISPATCH)
{
VariantInit(var_arg);
}
else
{
V_VT(var_arg) = VT_UNKNOWN|VT_BYREF;
V_UNKNOWNREF(var_arg) = (IUnknown **) &V_DISPATCH(var_arg);
}
break;
case VT_DISPATCH|VT_BYREF:
comval_to_variant(pval_arg, var_arg TSRMLS_CC);
if(V_VT(var_arg) != VT_DISPATCH)
{
VariantInit(var_arg);
}
else
{
V_VT(var_arg) = VT_DISPATCH|VT_BYREF;
V_DISPATCHREF(var_arg) = &V_DISPATCH(var_arg);
}
break;
case VT_VARIANT|VT_BYREF:
{
int tp;
pval **var_handle;
/* fetch the VARIANT structure */
zend_hash_index_find(Z_OBJPROP_P(pval_arg), 0, (void **) &var_handle);
V_VT(var_arg) = VT_VARIANT|VT_BYREF;
V_VARIANTREF(var_arg) = (VARIANT FAR*) zend_list_find(Z_LVAL_P(*var_handle), &tp);
if(!V_VARIANTREF(var_arg) && (tp != IS_VARIANT))
{
VariantInit(var_arg);
}
}
/*
should be, but isn't :)
if(V_VT(var_arg) != (VT_VARIANT|VT_BYREF))
{
VariantInit(var_arg);
}
*/
break;
case VT_I1:
convert_to_long_ex(&pval_arg);
V_I1(var_arg) = (char)Z_LVAL_P(pval_arg);
break;
case VT_UI2:
convert_to_long_ex(&pval_arg);
V_UI2(var_arg) = (unsigned short)Z_LVAL_P(pval_arg);
break;
case VT_UI4:
convert_to_long_ex(&pval_arg);
V_UI4(var_arg) = (unsigned long)Z_LVAL_P(pval_arg);
break;
case VT_INT:
convert_to_long_ex(&pval_arg);
V_INT(var_arg) = (int)Z_LVAL_P(pval_arg);
break;
case VT_UINT:
convert_to_long_ex(&pval_arg);
V_UINT(var_arg) = (unsigned int)Z_LVAL_P(pval_arg);
break;
case VT_I1|VT_BYREF:
convert_to_long(pval_arg);
V_I1REF(var_arg) = (char FAR*) &Z_LVAL_P(pval_arg);
break;
case VT_UI2|VT_BYREF:
convert_to_long(pval_arg);
V_UI2REF(var_arg) = (unsigned short FAR*) &Z_LVAL_P(pval_arg);
break;
case VT_UI4|VT_BYREF:
convert_to_long(pval_arg);
V_UI4REF(var_arg) = (unsigned long FAR*) &Z_LVAL_P(pval_arg);
break;
case VT_INT|VT_BYREF:
convert_to_long(pval_arg);
V_INTREF(var_arg) = (int FAR*) &Z_LVAL_P(pval_arg);
break;
case VT_UINT|VT_BYREF:
convert_to_long(pval_arg);
V_UINTREF(var_arg) = (unsigned int FAR*) &Z_LVAL_P(pval_arg);
break;
default:
php_error(E_WARNING, "Type not supported or not yet implemented.");
}
}
}
PHPAPI int php_variant_to_pval(VARIANT *var_arg, pval *pval_arg, int persistent, int codepage)
{
/* Changed the function to return a value for recursive error testing */
/* Existing calls will be unaffected by the change - so it */
/* seemed like the smallest impact on unfamiliar code */
int ret = SUCCESS;
INIT_PZVAL(pval_arg);
/* Add SafeArray support */
if (V_ISARRAY(var_arg))
{
SAFEARRAY *array = V_ARRAY(var_arg);
LONG indices[1];
LONG lbound=0, ubound;
VARTYPE vartype;
register int ii;
UINT Dims;
VARIANT vv;
pval *element;
HRESULT hr;
/* TODO: Add support for multi-dimensional SafeArrays */
/* For now just validate that the SafeArray has one dimension */
if (1 != (Dims = SafeArrayGetDim(array)))
{
php_error(E_WARNING, "Unsupported: multi-dimensional (%d) SafeArrays", Dims);
ZVAL_NULL(pval_arg);
return FAILURE;
}
SafeArrayLock( array);
/* This call has failed for everything I have tried */
/* But best leave it to be on the safe side */
if (FAILED(SafeArrayGetVartype(array, &vartype)))
{
/* Fall back to what we do know */
/* Mask off the array bit and assume */
/* what is left is the type of the array */
/* elements */
vartype = V_VT(var_arg) & ~VT_ARRAY;
}
SafeArrayGetUBound(array, 1, &ubound);
SafeArrayGetLBound(array, 1, &lbound);
/* Since COM returned an array we set up the php */
/* return value to be an array */
array_init(pval_arg);
/* Walk the safe array */
for (ii=lbound;ii<=ubound;ii++)
{
indices[0] = ii;
VariantInit(&vv); /* Docs say this just set the vt field, but you never know */
/* Set up a variant to pass to a recursive call */
/* So that we do not need to have two copies */
/* of the code */
if (VT_VARIANT == vartype)
{
hr = SafeArrayGetElement(array, indices, (VOID *) &(vv));
}
else
{
V_VT(&vv) = vartype;
hr = SafeArrayGetElement(array, indices, (VOID *) &(vv.lVal));
}
if (FAILED(hr))
{
/* Failure to retieve an element probably means the array is sparse */
/* So leave the php array sparse too */
continue;
}
/* Create an element to be added to the array */
ALLOC_ZVAL(element);
/* Call ourself again to handle the base type conversion */
/* If SafeArrayGetElement proclaims to allocate */
/* memory for a BSTR, so the recursive call frees */
/* the string correctly */
if (FAILURE == php_variant_to_pval(&vv, element, persistent, codepage))
{
/* Error occurred setting up array element */
/* Error was displayed by the recursive call */
FREE_ZVAL(element);
/* TODO: Do we stop here, or go on and */
/* try to make sense of the rest of the array */
/* Going on leads to multiple errors displayed */
/* for the same conversion. For large arrays that */
/* could be very annoying */
/* And if we don't go on - what to do about */
/* the parts of the array that are OK? */
/* break; */
}
else
{
/* Just insert the element into our return array */
add_index_zval(pval_arg, ii, element);
}
}
SafeArrayUnlock(array);
/* Clean up the SafeArray since that is our responsibility */
SafeArrayDestroyData(array);
SafeArrayDestroyDescriptor(array);
}
else switch(var_arg->vt & ~VT_BYREF)
{
case VT_EMPTY:
ZVAL_NULL(pval_arg);
break;
case VT_UI1:
if(V_ISBYREF(var_arg))
{
ZVAL_LONG(pval_arg, (long)*V_UI4REF(var_arg));
}
else
{
ZVAL_LONG(pval_arg, (long)V_UI4(var_arg));
}
break;
case VT_I2:
if(V_ISBYREF(var_arg))
{
ZVAL_LONG(pval_arg, (long )*V_I2REF(var_arg));
}
else
{
ZVAL_LONG(pval_arg, (long)V_I2(var_arg));
}
break;
case VT_I4:
if(V_ISBYREF(var_arg))
{
ZVAL_LONG(pval_arg, *V_I4REF(var_arg));
}
else
{
ZVAL_LONG(pval_arg, V_I4(var_arg));
}
break;
case VT_R4:
if(V_ISBYREF(var_arg))
{
ZVAL_DOUBLE(pval_arg, (double)*V_R4REF(var_arg));
}
else
{
ZVAL_DOUBLE(pval_arg, (double)V_R4(var_arg));
}
break;
case VT_R8:
if(V_ISBYREF(var_arg))
{
ZVAL_DOUBLE(pval_arg, *V_R8REF(var_arg));
}
else
{
ZVAL_DOUBLE(pval_arg, V_R8(var_arg));
}
break;
/* 96bit uint */
case VT_DECIMAL:
{
OLECHAR *unicode_str;
switch(VarBstrFromDec(&V_DECIMAL(var_arg), LOCALE_SYSTEM_DEFAULT, 0, &unicode_str))
{
case S_OK:
Z_STRVAL_P(pval_arg) = php_OLECHAR_to_char(unicode_str, &Z_STRLEN_P(pval_arg), persistent, codepage);
Z_TYPE_P(pval_arg) = IS_STRING;
break;
default:
ZVAL_NULL(pval_arg);
ret = FAILURE;
php_error(E_WARNING, "Error converting DECIMAL value to PHP string");
break;
}
}
break;
/* Currency */
case VT_CY:
if(V_ISBYREF(var_arg))
{
VarR8FromCy(*V_CYREF(var_arg), &Z_DVAL_P(pval_arg));
}
else
{
VarR8FromCy(V_CY(var_arg), &Z_DVAL_P(pval_arg));
}
Z_TYPE_P(pval_arg) = IS_DOUBLE;
break;
case VT_BOOL:
if(V_ISBYREF(var_arg))
{
ZVAL_BOOL(pval_arg, *V_BOOLREF(var_arg));
}
else
{
ZVAL_BOOL(pval_arg, V_BOOL(var_arg));
}
break;
case VT_NULL:
case VT_VOID:
ZVAL_NULL(pval_arg);
break;
case VT_VARIANT:
php_variant_to_pval(V_VARIANTREF(var_arg), pval_arg, persistent, codepage);
break;
case VT_BSTR:
if(V_ISBYREF(var_arg))
{
Z_STRVAL_P(pval_arg) = php_OLECHAR_to_char(*V_BSTRREF(var_arg), &Z_STRLEN_P(pval_arg), persistent, codepage);
SysFreeString(*V_BSTRREF(var_arg));
efree(V_BSTRREF(var_arg));
}
else
{
Z_STRVAL_P(pval_arg) = php_OLECHAR_to_char(V_BSTR(var_arg), &Z_STRLEN_P(pval_arg), persistent, codepage);
SysFreeString(V_BSTR(var_arg));
}
Z_TYPE_P(pval_arg) = IS_STRING;
break;
case VT_DATE:
{
SYSTEMTIME wintime;
struct tm phptime;
if(V_ISBYREF(var_arg))
{
VariantTimeToSystemTime(*V_DATEREF(var_arg), &wintime);
}
else
{
VariantTimeToSystemTime(V_DATE(var_arg), &wintime);
}
memset(&phptime, 0, sizeof(phptime));
phptime.tm_year = wintime.wYear - 1900;
phptime.tm_mon = wintime.wMonth - 1;
phptime.tm_mday = wintime.wDay;
phptime.tm_hour = wintime.wHour;
phptime.tm_min = wintime.wMinute;
phptime.tm_sec = wintime.wSecond;
phptime.tm_isdst = -1;
tzset();
ZVAL_LONG(pval_arg, mktime(&phptime));
}
break;
case VT_UNKNOWN:
if(V_UNKNOWN(var_arg) == NULL)
{
V_DISPATCH(var_arg) = NULL;
}
else
{
HRESULT hr;
hr = V_UNKNOWN(var_arg)->lpVtbl->QueryInterface(var_arg->punkVal, &IID_IDispatch, &V_DISPATCH(var_arg));
if(FAILED(hr))
{
char *error_message;
error_message = php_COM_error_message(hr);
php_error(E_WARNING, "Unable to obtain IDispatch interface: %s", error_message);
LocalFree(error_message);
V_DISPATCH(var_arg) = NULL;
}
}
/* break missing intentionaly */
case VT_DISPATCH:
{
comval *obj;
if(V_DISPATCH(var_arg) == NULL)
{
ret = FAILURE;
ZVAL_NULL(pval_arg);
}
else
{
ALLOC_COM(obj);
php_COM_set(obj, V_DISPATCH(var_arg), TRUE);
ZVAL_COM(pval_arg, obj);
}
}
break;
case VT_I1:
if(V_ISBYREF(var_arg))
{
ZVAL_LONG(pval_arg, (long)*V_I1REF(var_arg));
}
else
{
ZVAL_LONG(pval_arg, (long)V_I1(var_arg));
}
break;
case VT_UI2:
if(V_ISBYREF(var_arg))
{
ZVAL_LONG(pval_arg, (long)*V_UI2REF(var_arg));
}
else
{
ZVAL_LONG(pval_arg, (long)V_UI2(var_arg));
}
break;
case VT_UI4:
if(V_ISBYREF(var_arg))
{
ZVAL_LONG(pval_arg, (long)*V_UI4REF(var_arg));
}
else
{
ZVAL_LONG(pval_arg, (long)V_UI4(var_arg));
}
break;
case VT_INT:
if(V_ISBYREF(var_arg))
{
ZVAL_LONG(pval_arg, (long)*V_INTREF(var_arg));
}
else
{
ZVAL_LONG(pval_arg, (long)V_INT(var_arg));
}
break;
case VT_UINT:
if(V_ISBYREF(var_arg))
{
ZVAL_LONG(pval_arg, (long)*V_UINTREF(var_arg));
}
else
{
ZVAL_LONG(pval_arg, (long)V_UINT(var_arg));
}
break;
default:
php_error(E_WARNING, "Unsupported variant type: %d (0x%X)", V_VT(var_arg), V_VT(var_arg));
ZVAL_NULL(pval_arg);
ret = FAILURE;
break;
}
return ret;
}
PHPAPI OLECHAR *php_char_to_OLECHAR(char *C_str, uint strlen, int codepage)
{
OLECHAR *unicode_str;
/* request needed buffersize */
uint reqSize = MultiByteToWideChar(codepage, MB_PRECOMPOSED | MB_ERR_INVALID_CHARS, C_str, -1, NULL, 0);
if(reqSize)
{
unicode_str = (OLECHAR *) emalloc(sizeof(OLECHAR) * reqSize);
/* convert string */
MultiByteToWideChar(codepage, MB_PRECOMPOSED | MB_ERR_INVALID_CHARS, C_str, -1, unicode_str, reqSize);
}
else
{
unicode_str = (OLECHAR *) emalloc(sizeof(OLECHAR));
*unicode_str = 0;
switch(GetLastError())
{
case ERROR_NO_UNICODE_TRANSLATION:
php_error(E_WARNING, "No unicode translation available for the specified string");
break;
default:
php_error(E_WARNING, "Error in php_char_to_OLECHAR()");
}
}
return unicode_str;
}
PHPAPI char *php_OLECHAR_to_char(OLECHAR *unicode_str, uint *out_length, int persistent, int codepage)
{
char *C_str;
uint length = 0;
/* request needed buffersize */
uint reqSize = WideCharToMultiByte(codepage, WC_COMPOSITECHECK, unicode_str, -1, NULL, 0, NULL, NULL);
if(reqSize)
{
C_str = (char *) pemalloc(sizeof(char) * reqSize, persistent);
/* convert string */
length = WideCharToMultiByte(codepage, WC_COMPOSITECHECK, unicode_str, -1, C_str, reqSize, NULL, NULL) - 1;
}
else
{
C_str = (char *) pemalloc(sizeof(char), persistent);
*C_str = 0;
php_error(E_WARNING, "Error in php_OLECHAR_to_char()");
}
if(out_length)
{
*out_length = length;
}
return C_str;
}
static void comval_to_variant(pval *pval_arg, VARIANT *var_arg TSRMLS_DC)
{
pval **comval_handle;
comval *obj;
int type;
/* fetch the comval structure */
zend_hash_index_find(Z_OBJPROP_P(pval_arg), 0, (void **) &comval_handle);
obj = (comval *) zend_list_find(Z_LVAL_P(*comval_handle), &type);
if(!obj || (type != IS_COM) || !C_ISREFD(obj))
{
VariantInit(var_arg);
}
else
{
V_VT(var_arg) = VT_DISPATCH;
V_DISPATCH(var_arg) = C_DISPATCH(obj);
}
}
#endif /* PHP_WIN32 */
|