xref: /PHP-8.4/ext/com_dotnet/com_misc.c (revision 11accb5c)
1 /*
2    +----------------------------------------------------------------------+
3    | Copyright (c) The PHP Group                                          |
4    +----------------------------------------------------------------------+
5    | This source file is subject to version 3.01 of the PHP license,      |
6    | that is bundled with this package in the file LICENSE, and is        |
7    | available through the world-wide-web at the following url:           |
8    | https://www.php.net/license/3_01.txt                                 |
9    | If you did not receive a copy of the PHP license and are unable to   |
10    | obtain it through the world-wide-web, please send a note to          |
11    | license@php.net so we can mail you a copy immediately.               |
12    +----------------------------------------------------------------------+
13    | Author: Wez Furlong  <wez@thebrainroom.com>                          |
14    +----------------------------------------------------------------------+
15  */
16 
17 #ifdef HAVE_CONFIG_H
18 #include <config.h>
19 #endif
20 
21 #include "php.h"
22 #include "php_ini.h"
23 #include "ext/standard/info.h"
24 #include "php_com_dotnet.h"
25 #include "php_com_dotnet_internal.h"
26 #include "Zend/zend_exceptions.h"
27 
php_com_throw_exception(HRESULT code,char * message)28 void php_com_throw_exception(HRESULT code, char *message)
29 {
30 	int free_msg = 0;
31 	if (message == NULL) {
32 		message = php_win32_error_to_msg(code);
33 		free_msg = 1;
34 	}
35 #if SIZEOF_ZEND_LONG == 8
36 	zend_throw_exception(php_com_exception_class_entry, message, (zend_long)(uint32_t)code);
37 #else
38 	zend_throw_exception(php_com_exception_class_entry, message, (zend_long)code);
39 #endif
40 	if (free_msg) {
41 		php_win32_error_msg_free(message);
42 	}
43 }
44 
php_com_wrap_dispatch(zval * z,IDispatch * disp,int codepage)45 PHP_COM_DOTNET_API void php_com_wrap_dispatch(zval *z, IDispatch *disp,
46 		int codepage)
47 {
48 	php_com_dotnet_object *obj;
49 
50 	obj = emalloc(sizeof(*obj));
51 	memset(obj, 0, sizeof(*obj));
52 	obj->code_page = codepage;
53 	obj->ce = php_com_variant_class_entry;
54 	obj->zo.ce = php_com_variant_class_entry;
55 
56 	VariantInit(&obj->v);
57 	V_VT(&obj->v) = VT_DISPATCH;
58 	V_DISPATCH(&obj->v) = disp;
59 
60 	IDispatch_AddRef(V_DISPATCH(&obj->v));
61 	IDispatch_GetTypeInfo(V_DISPATCH(&obj->v), 0, LANG_NEUTRAL, &obj->typeinfo);
62 
63 	zend_object_std_init(&obj->zo, php_com_variant_class_entry);
64 	ZVAL_OBJ(z, &obj->zo);
65 }
66 
php_com_wrap_variant(zval * z,VARIANT * v,int codepage)67 PHP_COM_DOTNET_API void php_com_wrap_variant(zval *z, VARIANT *v,
68 		int codepage)
69 {
70 	php_com_dotnet_object *obj;
71 
72 	obj = emalloc(sizeof(*obj));
73 	memset(obj, 0, sizeof(*obj));
74 	obj->code_page = codepage;
75 	obj->ce = php_com_variant_class_entry;
76 	obj->zo.ce = php_com_variant_class_entry;
77 
78 	VariantInit(&obj->v);
79 	VariantCopyInd(&obj->v, v);
80 	obj->modified = 0;
81 
82 	if ((V_VT(&obj->v) == VT_DISPATCH) && (V_DISPATCH(&obj->v) != NULL)) {
83 		IDispatch_GetTypeInfo(V_DISPATCH(&obj->v), 0, LANG_NEUTRAL, &obj->typeinfo);
84 	}
85 
86 	zend_object_std_init(&obj->zo, php_com_variant_class_entry);
87 	ZVAL_OBJ(z, &obj->zo);
88 }
89 
90 /* this is a convenience function for fetching a particular
91  * element from a (possibly multi-dimensional) safe array */
php_com_safearray_get_elem(VARIANT * array,VARIANT * dest,LONG dim1)92 PHP_COM_DOTNET_API bool php_com_safearray_get_elem(VARIANT *array, VARIANT *dest, LONG dim1)
93 {
94 	UINT dims;
95 	LONG lbound, ubound;
96 	LONG indices[1];
97 	VARTYPE vt;
98 
99 	if (!V_ISARRAY(array)) {
100 		return 0;
101 	}
102 
103 	dims = SafeArrayGetDim(V_ARRAY(array));
104 
105 	if (dims != 1) {
106 		/* TODO Promote to ValueError? */
107 		php_error_docref(NULL, E_WARNING,
108 			   "Can only handle single dimension variant arrays (this array has %d)", dims);
109 		return 0;
110 	}
111 
112 	if (FAILED(SafeArrayGetVartype(V_ARRAY(array), &vt)) || vt == VT_EMPTY) {
113 		vt = V_VT(array) & ~VT_ARRAY;
114 	}
115 
116 	/* determine the bounds */
117 	SafeArrayGetLBound(V_ARRAY(array), 1, &lbound);
118 	SafeArrayGetUBound(V_ARRAY(array), 1, &ubound);
119 
120 	/* check bounds */
121 	if (dim1 < lbound || dim1 > ubound) {
122 		php_com_throw_exception(DISP_E_BADINDEX, "index out of bounds");
123 		return 0;
124 	}
125 
126 	/* now fetch that element */
127 	VariantInit(dest);
128 
129 	indices[0] = dim1;
130 
131 	if (vt == VT_VARIANT) {
132 		SafeArrayGetElement(V_ARRAY(array), indices, dest);
133 	} else {
134 		V_VT(dest) = vt;
135 		/* store the value into "lVal" member of the variant.
136 		 * This works because it is a union; since we know the variant
137 		 * type, we end up with a working variant */
138 		SafeArrayGetElement(V_ARRAY(array), indices, &dest->lVal);
139 	}
140 
141 	return 1;
142 }
143