forked from wagy/WinBinder
-
Notifications
You must be signed in to change notification settings - Fork 1
/
phpwb_generic.c
265 lines (190 loc) · 5.25 KB
/
phpwb_generic.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
/*******************************************************************************
WINBINDER - The native Windows binding for PHP for PHP
Copyright © Hypervisual - see LICENSE.TXT for details
Author: Rubem Pechansky (http://winbinder.org/contact.php)
General-purpose functions (not exported to the ZEND engine)
*******************************************************************************/
//----------------------------------------------------------------- DEPENDENCIES
#include "phpwb.h"
//------------------------------------------------------------- GLOBAL VARIABLES
const char *pszWbobjName = "WinBinder Object";
//------------------------------------------------------------- PUBLIC FUNCTIONS
/* Accepts a limited subset of the parameters accepted by zend_parse_parameters() */
int parse_array(zval *array, const char *fmt, ...)
{
int i, nelem;
void *arg;
zval *entry = NULL;
va_list ap;
HashTable *target_hash;
#if (PHP_MAJOR_VERSION >= 5)
TSRMLS_FETCH();
#endif
va_start(ap, fmt);
target_hash = HASH_OF(array);
if(!target_hash)
return 0;
nelem = zend_hash_num_elements(target_hash);
zend_hash_internal_pointer_reset(target_hash);
// Parse loop
for(i = 0; i < (int)strlen(fmt); i++) {
arg = va_arg(ap, void *);
if(!arg)
break;
// Requested items past the length of the array must return NULL
if(i >= nelem ) {
switch(fmt[i]) {
case 'l':
*((long *)arg) = 0;
break;
case 'd':
*((double *)arg) = 0;
break;
case 's':
*((long *)arg) = (long)NULL;
break;
default:
zend_error(E_WARNING, "Invalid format character '%c' in function %s()",
fmt[i], __FUNCTION__);
continue;
}
continue;
} else if((entry = zend_hash_get_current_data(target_hash)) == NULL) {
zend_error(E_WARNING, "Could not retrieve element %d from array in function %s()",
i, __FUNCTION__);
continue;
} else {
if(!entry)
break;
switch(fmt[i]) {
case 'l':
if(Z_TYPE_P(entry) == IS_NULL) {
*((long *)arg) = (long)NULL;
} else
*((long *)arg) = Z_LVAL_P(entry);
break;
case 'd':
if(Z_TYPE_P(entry) == IS_NULL) {
*((long *)arg) = (long)NULL;
} else
*((double *)arg) = Z_DVAL_P(entry);
break;
case 's':
if(Z_TYPE_P(entry) == IS_STRING) {
*((long *)arg) = (long)(Z_STRVAL_P(entry));
} else if(Z_TYPE_P(entry) == IS_NULL) {
*((long *)arg) = (long)NULL;
} else
*((long *)arg) = (long)NULL;
break;
default:
zend_error(E_WARNING, "Invalid format string '%s' in function %s()",
fmt, __FUNCTION__);
continue;
} // switch
} // else
if(i < nelem - 1)
zend_hash_move_forward(target_hash);
}
va_end(ap);
return nelem;
}
/* Function to process arrays. Returns a pointer to a zval element from array zitems or
NULL if end of array is reached */
zval *process_array(zval *zitems)
{
static int nelem = 0;
static int nelems = 0;
static HashTable *target_hash = NULL;
zval *entry = NULL;
if(Z_TYPE_P(zitems) != IS_ARRAY)
return FALSE;
// Prepare to read items from zitem array
if(!nelems && !nelem) {
target_hash = HASH_OF(zitems);
if(!target_hash)
return FALSE;
nelems = zend_hash_num_elements(target_hash);
if(!nelems) {
// Array is empty: reset everything
target_hash = NULL;
nelems = 0;
nelem = 0;
return NULL;
} else {
// Start array
zend_hash_internal_pointer_reset(target_hash);
nelem = 0;
}
} else {
if(nelem < nelems - 1) {
zend_hash_move_forward(target_hash);
nelem++;
} else if(nelem == nelems - 1) {
// End of array: reset everything
target_hash = NULL;
nelems = 0;
nelem = 0;
return NULL;
}
}
// Get zval data
if((entry = zend_hash_get_current_data(target_hash)) == NULL)
zend_error(E_WARNING, "Could not retrieve element %d from array in function %s()",
nelem, get_active_function_name());
return entry;
}
TCHAR * Utf82WideChar(const char *str, int len)
{
TCHAR *wstr = NULL;
int wlen = 0;
if (!str)
return NULL;
if (len <= 0)
len = -1; //len = strlen(str);
wlen = MultiByteToWideChar(CP_UTF8, 0, str, len, NULL, 0);
wstr = wbMalloc(sizeof(TCHAR)*(wlen+1));
wstr[wlen] = '\0';
MultiByteToWideChar(CP_UTF8, 0, str, len, wstr, wstr + 1);
return wstr;
}
void Utf82WideCharCopy(const char *str, int str_len, TCHAR *wcs, int wcs_len)
{
if (!str || !wcs)
return NULL;
MultiByteToWideChar(CP_UTF8, 0, str, str_len, wcs, wcs_len);
}
char *WideChar2Utf8(LPCTSTR wcs, int *plen)
{
char *str = 0;
int str_len = 0;
if (!wcs)
return NULL;
str_len = WideCharToMultiByte(CP_UTF8, 0, wcs, -1, str, 0, NULL, NULL);
if (str_len == 0)
return NULL;
str = wbMalloc(str_len+1);
str[str_len] = '\0';
str[str_len-1] = '\0';
if (plen)
*plen = WideCharToMultiByte(CP_UTF8, 0, wcs, -1, str, str_len, NULL, NULL);
else
WideCharToMultiByte(CP_UTF8, 0, wcs, -1, str, str_len, NULL, NULL);
return str;
}
void WideCharCopy(LPCTSTR wcs, char *s, int len)
{
if (wcs && s)
WideCharToMultiByte(CP_UTF8, 0, wcs, -1, s, len, NULL, NULL);
}
void dumptcs(TCHAR *str)
{
int i;
int len = wcslen(str);
printf("Dump String (%d): ", len);
for (i = 0; i < len; i++) {
printf("%x,", str[i]);
}
printf("\n");
}
//------------------------------------------------------------------ END OF FILE