forked from RDashINC/win32std
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathres.c
More file actions
479 lines (404 loc) · 14.8 KB
/
res.c
File metadata and controls
479 lines (404 loc) · 14.8 KB
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
/*
+----------------------------------------------------------------------+
| PHP Version 4 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2002 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3 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/3_0.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: Eric COLINET <e.colinet@laposte.net> |
+----------------------------------------------------------------------+
*/
#include "php_win32std.h"
#ifndef IS_INTRESOURCE
#define IS_INTRESOURCE(_r) (((DWORD)(_r) >> 16) == 0)
#endif
/* static */ int le_res_resource;
void _php_res_destruction_handler(zend_resource *rsrc) {
HMODULE module = (HMODULE) rsrc->ptr;
if( module )
FreeLibrary(module);
}
/* {{{ proto resource res_open( module_name )
Return a (PHP)resource that identify the (WIN)resource module handle
*/
PHP_FUNCTION(res_open)
{
HMODULE h_module= NULL;
char *module= NULL;
size_t module_len;
char buffer[WIN32_STRERROR_BUFFER_LEN];
//res_resource * res_rc;
// if( zend_parse_parameters( ZEND_NUM_ARGS() TSRMLS_CC, "s", &module, &module_len ) == FAILURE )
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_STRING(module, module_len)
ZEND_PARSE_PARAMETERS_END();
// if module=="" module is the current executable
if( !module_len ) {
h_module= NULL;
} else {
h_module= LoadLibrary(module);
if( !h_module ) {
zend_error(E_WARNING, "res_open '%s' failed: %s", module, win32_strerror(buffer, WIN32_STRERROR_BUFFER_LEN));
RETURN_FALSE;
}
}
ZVAL_RES(return_value, zend_register_resource(h_module, le_res_resource));
}
/* }}} */
/* {{{ proto bool res_close( resouce module )
Close a module handle
*/
PHP_FUNCTION(res_close)
{
zval *res_rc;
HMODULE h;
//if( zend_parse_parameters( ZEND_NUM_ARGS() TSRMLS_CC, "r", &res_rc ) == FAILURE )
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_RESOURCE(res_rc)
ZEND_PARSE_PARAMETERS_END();
if((h = (HMODULE) zend_fetch_resource(Z_RES_P(res_rc), le_res_resource_name, le_res_resource)) == NULL)
RETURN_FALSE;
zend_list_delete(Z_RES_P(res_rc));
RETURN_TRUE;
}
/* }}} */
/* {{{ proto res_get( resource module, string type, string name[, int lang] )
Get a resource.
lang is experimental: 0 is neutral, 1 is user default, 2 is system default (see winnt.h LANG_* & SUBLANG_*).
*/
PHP_FUNCTION(res_get)
{
DWORD size;
HGLOBAL rc;
HMODULE h_module= NULL;
HRSRC hr;
char *module= NULL, *name= NULL, *type= NULL;
char *name_upper = NULL, *type_upper = NULL;
size_t name_len, type_len;
size_t lang= MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT);
char buffer[WIN32_STRERROR_BUFFER_LEN];
zval *res_rc;
ZEND_PARSE_PARAMETERS_START(3, 4)
Z_PARAM_RESOURCE(res_rc)
Z_PARAM_STRING(type, type_len)
Z_PARAM_STRING(name, name_len)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(lang)
ZEND_PARSE_PARAMETERS_END();
if( !name_len || !type_len ) {
zend_error(E_WARNING, "res_get: name or type can't be empty");
RETURN_FALSE;
}
if((h_module = (HMODULE)zend_fetch_resource(Z_RES_P(res_rc), le_res_resource_name, le_res_resource)) == NULL)
RETURN_FALSE;
name_upper = estrndup(name, name_len);
type_upper = estrndup(type, type_len);
strupr(name_upper);
strupr(type_upper);
if( ZEND_NUM_ARGS()>3 )
hr= FindResourceEx( h_module, type_upper, name_upper, (WORD)lang );
else
hr= FindResource( h_module, type_upper, name_upper );
if( hr==NULL ) {
efree(name_upper);
efree(type_upper);
zend_error(E_WARNING, "res_get: find '%s/%s' failed: %s", type, name, win32_strerror(buffer, WIN32_STRERROR_BUFFER_LEN));
RETURN_FALSE;
}
rc= LoadResource( h_module, hr );
if( rc==NULL ) {
efree(name_upper);
efree(type_upper);
zend_error(E_WARNING, "res_get: load '%s/%s' failed: %s", type, name, win32_strerror(buffer, WIN32_STRERROR_BUFFER_LEN));
RETURN_FALSE;
}
size= SizeofResource( h_module, hr );
efree(name_upper);
efree(type_upper);
RETVAL_STRINGL((char*)rc, size);
}
/* }}} */
/* {{{ proto bool res_set( string file, string type, string name, string data[, int lang] )
Add or modify a resource in 'file' (dll or exe)
lang is experimental: 0 is neutral, 1 is user default, 2 is system default (see winnt.h LANG_* & SUBLANG_*).
*/
PHP_FUNCTION(res_set)
{
HANDLE h_module;
char *module, *type, *name, *data;
char *name_upper = NULL, *type_upper = NULL;
size_t module_len, type_len, name_len, data_len;
char buffer[WIN32_STRERROR_BUFFER_LEN];
size_t lang = MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL);
zend_bool lang_isnull = 1;
ZEND_PARSE_PARAMETERS_START(4, 5)
Z_PARAM_STRING(module, module_len)
Z_PARAM_STRING(type, type_len)
Z_PARAM_STRING(name, name_len)
Z_PARAM_STRING_OR_NULL(data, data_len)
Z_PARAM_OPTIONAL
Z_PARAM_LONG_OR_NULL(lang, lang_isnull)
ZEND_PARSE_PARAMETERS_END();
if( !module_len || !type_len || !name_len ) {
zend_error(E_WARNING, "res_set: module file, type or name can't be empty" );
RETURN_FALSE;
}
name_upper = estrndup(name, name_len);
type_upper = estrndup(type, type_len);
strupr(name_upper);
strupr(type_upper);
h_module= BeginUpdateResource( module, FALSE );
if( h_module==NULL ) {
efree(name_upper);
efree(type_upper);
zend_error(E_WARNING, "res_set: error opening module: %s", win32_strerror(buffer, WIN32_STRERROR_BUFFER_LEN) );
RETURN_FALSE;
}
if(data == NULL || data_len == 0) {
if( UpdateResource( h_module, type_upper, name_upper, (WORD)lang, NULL, 0)==0 ) {
efree(name_upper);
efree(type_upper);
zend_error(E_WARNING, "res_set: error updating module(1): %s", win32_strerror(buffer, WIN32_STRERROR_BUFFER_LEN) );
EndUpdateResource(h_module, TRUE);
RETURN_FALSE;
}
} else {
if( UpdateResource( h_module, type_upper, name_upper, (WORD)lang, data, data_len )==0 ) {
efree(name_upper);
efree(type_upper);
zend_error(E_WARNING, "res_set: error updating module(2): %s", win32_strerror(buffer, WIN32_STRERROR_BUFFER_LEN) );
EndUpdateResource(h_module, TRUE);
RETURN_FALSE;
}
}
EndUpdateResource(h_module, FALSE);
efree(name_upper);
efree(type_upper);
RETURN_TRUE;
}
/* }}} */
long php_res_list_get_int_type( const char * string_type )
{
if( !strcmp( string_type, "RT_CURSOR" ) ) return (long long)RT_CURSOR;
if( !strcmp( string_type, "RT_BITMAP" ) ) return (long long)RT_BITMAP;
if( !strcmp( string_type, "RT_ICON" ) ) return (long long)RT_ICON;
if( !strcmp( string_type, "RT_MENU" ) ) return (long long)RT_MENU;
if( !strcmp( string_type, "RT_DIALOG" ) ) return (long long)RT_DIALOG;
if( !strcmp( string_type, "RT_STRING" ) ) return (long long)RT_STRING;
if( !strcmp( string_type, "RT_FONTDIR" ) ) return (long long)RT_FONTDIR;
if( !strcmp( string_type, "RT_FONT" ) ) return (long long)RT_FONT;
if( !strcmp( string_type, "RT_ACCELERATOR" ) ) return (long long)RT_ACCELERATOR;
if( !strcmp( string_type, "RT_RCDATA" ) ) return (long long)RT_RCDATA;
if( !strcmp( string_type, "RT_MESSAGETABLE" ) ) return (long long)RT_MESSAGETABLE;
if( !strcmp( string_type, "RT_GROUP_CURSOR" ) ) return (long long)RT_GROUP_CURSOR;
if( !strcmp( string_type, "RT_GROUP_ICON" ) ) return (long long)RT_GROUP_ICON;
if( !strcmp( string_type, "RT_VERSION" ) ) return (long long)RT_VERSION;
if( !strcmp( string_type, "RT_DLGINCLUDE" ) ) return (long long)RT_DLGINCLUDE;
if( !strcmp( string_type, "RT_PLUGPLAY" ) ) return (long long)RT_PLUGPLAY;
if( !strcmp( string_type, "RT_VXD" ) ) return (long long)RT_VXD;
if( !strcmp( string_type, "RT_ANICURSOR" ) ) return (long long)RT_ANICURSOR;
if( !strcmp( string_type, "RT_ANIICON" ) ) return (long long)RT_ANIICON;
if( !strcmp( string_type, "RT_HTML" ) ) return (long long)RT_HTML;
//if( !strcmp( string_type, "RT_NOT_DOCUMENTED" ) ) return 241;
return -1;
}
BOOL CALLBACK php_res_list_callback(
HMODULE h_module, // module handle
LPCSTR lpszType, // pointer to resource type
LPSTR lpszName, // pointer to resource name
LONG_PTR lParam // application-defined parameter
)
{
char buffer[32] = {0};
zval * array= (zval*) lParam;
if( !IS_INTRESOURCE(lpszName) )
add_next_index_string(array, lpszName);
else {
snprintf(buffer, sizeof(buffer), "#%llu", (unsigned long long)(ULONG_PTR)lpszName);
add_next_index_string(array, buffer);
}
return TRUE;
}
/* {{{ proto array res_list( resource module, string type )
return the resource list for a given type
*/
PHP_FUNCTION(res_list)
{
char *module= NULL, *type= NULL;
size_t type_len;
HMODULE h_module= NULL;
BOOL ret= FALSE;
long int_type=-1;
char buffer[WIN32_STRERROR_BUFFER_LEN];
zval * res_rc;
// if( zend_parse_parameters( ZEND_NUM_ARGS() TSRMLS_CC, "rs", &res_rc, &type, &type_len ) == FAILURE )
ZEND_PARSE_PARAMETERS_START(2, 2)
Z_PARAM_RESOURCE(res_rc)
Z_PARAM_STRING(type, type_len)
ZEND_PARSE_PARAMETERS_END();
if(( h_module = (HMODULE)zend_fetch_resource(Z_RES_P(res_rc), le_res_resource_name, le_res_resource)) == NULL)
RETURN_FALSE;
if( !type || !type[0] ) RETURN_FALSE;
if( *type=='#' )
sscanf( type, "#%x", &int_type );
if( !strncmp( type, "RT_", 3 ) )
int_type= php_res_list_get_int_type( type );
// dont check return value of array init()?
array_init( return_value );
if( int_type==-1 ){
ret= EnumResourceNamesA( h_module, type, php_res_list_callback, (LONG_PTR)return_value );
} else {
ret= EnumResourceNamesA( h_module, MAKEINTRESOURCE(int_type), php_res_list_callback, (LONG_PTR)return_value );
}
if( !ret ) {
// No resource of this type
if( GetLastError()!=1813 ){
zend_error(E_WARNING, "res_list %s", win32_strerror(buffer, WIN32_STRERROR_BUFFER_LEN)); RETURN_FALSE;
} else {
ret= TRUE;
}
}
if( !ret ){
RETURN_FALSE;
}
}
/* }}} */
BOOL CALLBACK php_res_list_type_callback(
HMODULE hModule, // resource-module handle
LPTSTR lpszType, // pointer to resource type
LONG_PTR lParam // application-defined parameter
)
{
zval * array;
char buffer[32] = {0};
array= (zval*) lParam;
if( !IS_INTRESOURCE(lpszType) )
add_next_index_stringl(array, lpszType, strlen(lpszType));
else {
snprintf(buffer, sizeof(buffer), "#%llu", (unsigned long long)(ULONG_PTR)lpszType);
add_next_index_stringl(array, buffer, strlen(buffer));
}
return TRUE;
}
BOOL CALLBACK php_res_list_type_string_callback(
HMODULE hModule, // resource-module handle
LPTSTR lpszType, // pointer to resource type
LONG_PTR lParam // application-defined parameter
)
{
zval * array;
char buffer[32] = {0};
array= (zval*) lParam;
if( !IS_INTRESOURCE(lpszType) ) {
add_next_index_stringl(array, lpszType, strlen(lpszType));
return TRUE;
}
#define RES_LIST_TYPE_STRING(type) case type: add_next_index_stringl(array, #type, strlen(#type)); break;
switch( (DWORD64)lpszType ) {
RES_LIST_TYPE_STRING(RT_CURSOR)
RES_LIST_TYPE_STRING(RT_BITMAP)
RES_LIST_TYPE_STRING(RT_ICON)
RES_LIST_TYPE_STRING(RT_MENU)
RES_LIST_TYPE_STRING(RT_DIALOG)
RES_LIST_TYPE_STRING(RT_STRING)
RES_LIST_TYPE_STRING(RT_FONTDIR)
RES_LIST_TYPE_STRING(RT_FONT)
RES_LIST_TYPE_STRING(RT_ACCELERATOR)
RES_LIST_TYPE_STRING(RT_RCDATA)
RES_LIST_TYPE_STRING(RT_MESSAGETABLE)
RES_LIST_TYPE_STRING(RT_GROUP_CURSOR)
RES_LIST_TYPE_STRING(RT_GROUP_ICON)
RES_LIST_TYPE_STRING(RT_VERSION)
RES_LIST_TYPE_STRING(RT_DLGINCLUDE)
RES_LIST_TYPE_STRING(RT_PLUGPLAY)
RES_LIST_TYPE_STRING(RT_VXD)
RES_LIST_TYPE_STRING(RT_ANICURSOR)
RES_LIST_TYPE_STRING(RT_ANIICON)
RES_LIST_TYPE_STRING(RT_HTML)
default:
snprintf(buffer, sizeof(buffer), "#%llu", (unsigned long long)(ULONG_PTR)lpszType);
add_next_index_stringl(array, buffer, strlen(buffer));
break;
}
#undef RES_LIST_TYPE_STRING
return TRUE;
}
/* {{{ proto array res_list_type( resource module [, bool as_string=true] )
return the resource type list for a given module
as_string specify if known type should be translated to string (but such string can't be used in res_get)
*/
PHP_FUNCTION(res_list_type)
{
char *module= NULL;
HMODULE h_module= NULL;
BOOL ret= FALSE;
zend_bool as_string = 1;
char buffer[WIN32_STRERROR_BUFFER_LEN];
zval *res_rc;
//if( zend_parse_parameters( ZEND_NUM_ARGS() TSRMLS_CC, "r|b", &res_rc, &as_string ) == FAILURE )
ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_RESOURCE(res_rc)
Z_PARAM_OPTIONAL
Z_PARAM_BOOL(as_string)
ZEND_PARSE_PARAMETERS_END();
if((h_module = (HMODULE) zend_fetch_resource(Z_RES_P(res_rc), le_res_resource_name, le_res_resource)) == NULL){
RETURN_FALSE;
}
array_init( return_value );
if( !as_string ){
ret= EnumResourceTypes( h_module, php_res_list_type_callback, (LONG_PTR)return_value );
} else {
ret= EnumResourceTypes( h_module, php_res_list_type_string_callback, (LONG_PTR)return_value );
}
if( !ret ) {
zend_error(E_WARNING, "res_list_type %d - %s", GetLastError(), win32_strerror(buffer, WIN32_STRERROR_BUFFER_LEN)); RETURN_FALSE;
}
if( !ret ){
RETURN_FALSE;
}
}
/* }}} */
/* {{{ proto bool res_exists(resource module, string type, string name[, int lang] )
Check if a resource exists in an opened module
lang is experimental: 0 is neutral, 1 is user default, 2 is system default (see winnt.h LANG_* & SUBLANG_*).
*/
PHP_FUNCTION(res_exists)
{
char *name = NULL, *type = NULL;
char *name_upper = NULL, *type_upper = NULL;
size_t name_len, type_len;
size_t lang = MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT);
zend_bool lang_isnull = 1;
BOOL ret;
zval *res_rc;
HMODULE h_module = NULL;
HRSRC hr;
ZEND_PARSE_PARAMETERS_START(3, 4)
Z_PARAM_RESOURCE(res_rc)
Z_PARAM_STRING(type, type_len)
Z_PARAM_STRING(name, name_len)
Z_PARAM_OPTIONAL
Z_PARAM_LONG_OR_NULL(lang, lang_isnull)
ZEND_PARSE_PARAMETERS_END();
if((h_module = (HMODULE)zend_fetch_resource(Z_RES_P(res_rc), le_res_resource_name, le_res_resource)) == NULL)
RETURN_FALSE;
name_upper = estrndup(name, name_len);
type_upper = estrndup(type, type_len);
strupr(name_upper);
strupr(type_upper);
if(lang_isnull) hr = FindResource(h_module, type_upper, name_upper);
else hr = FindResourceEx(h_module, type_upper, name_upper, (WORD)lang);
if(hr) ret = TRUE;
else ret = FALSE;
efree(name_upper);
efree(type_upper);
RETURN_BOOL(ret);
}
/* }}} */