This repository has been archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathres.c
377 lines (313 loc) · 11.7 KB
/
res.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
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
/*
+----------------------------------------------------------------------+
| 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 |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Eric COLINET <[email protected]> |
+----------------------------------------------------------------------+
*/
#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_rsrc_list_entry *rsrc TSRMLS_DC) {
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;
long 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 )
RETURN_LONG(-1);
// 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
}
}
ZEND_REGISTER_RESOURCE( return_value, 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 )
RETURN_FALSE
ZEND_FETCH_RESOURCE(h, HMODULE, &res_rc, -1, le_res_resource_name, le_res_resource);
zend_list_delete(Z_RESVAL_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;
long name_len, type_len;
long lang= MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT);
char buffer[WIN32_STRERROR_BUFFER_LEN];
zval *res_rc;
if( zend_parse_parameters( ZEND_NUM_ARGS() TSRMLS_CC, "rss|l", &res_rc, &type, &type_len, &name, &name_len, &lang ) == FAILURE )
RETURN_NULL();
if( !name_len || !type_len ) {
zend_error(E_WARNING, "res_get: name or type can't be empty");
RETURN_NULL();
}
ZEND_FETCH_RESOURCE(h_module, HMODULE, &res_rc, -1, le_res_resource_name, le_res_resource);
// Convert name and type to uppercase since lowercase don't work
strupr(name);
strupr(type);
if( ZEND_NUM_ARGS()>3 )
hr= FindResourceEx( h_module, name, type, (WORD)lang );
else
hr= FindResource( h_module, name, type );
if( hr==NULL ) {
zend_error(E_WARNING, "res_get: find '%s/%s' failed: %s", type, name, win32_strerror(buffer, WIN32_STRERROR_BUFFER_LEN));
RETURN_NULL( );
}
rc= LoadResource( h_module, hr );
if( rc==NULL ) {
zend_error(E_WARNING, "res_get: load '%s/%s' failed: %s", type, name, win32_strerror(buffer, WIN32_STRERROR_BUFFER_LEN));
RETURN_NULL( );
}
size= SizeofResource( h_module, hr );
RETVAL_STRINGL((char*)rc, size, 1);
}
/* }}} */
/* {{{ 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;
int module_len, type_len, name_len, data_len;
char buffer[WIN32_STRERROR_BUFFER_LEN];
long lang= MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL);
if( zend_parse_parameters( ZEND_NUM_ARGS() TSRMLS_CC, "ssss|l",
&module, &module_len,
&type, &type_len,
&name, &name_len,
&data, &data_len, &lang ) == FAILURE )
RETURN_FALSE;
//zend_error( E_WARNING, "res_set modifie res://%s/%s/%s avec %d octets",
// module, type, name, data_len );
if( !module_len || !type_len || !name_len ) {
zend_error(E_WARNING, "res_set: module file, type or name can't be empty" );
RETURN_FALSE;
}
// Convert name and type to uppercase since lowercase don't work
strupr(name);
strupr(type);
h_module= BeginUpdateResource( module, FALSE );
if( h_module==NULL ) {
zend_error(E_WARNING, "res_set: error opening module: %s", win32_strerror(buffer, WIN32_STRERROR_BUFFER_LEN) );
RETURN_FALSE;
}
if( UpdateResource( h_module, type, name, (WORD)lang, data, data_len )==0 ) {
zend_error(E_WARNING, "res_set: error updating module: %s", win32_strerror(buffer, WIN32_STRERROR_BUFFER_LEN) );
EndUpdateResource(h_module, TRUE);
RETURN_FALSE;
}
EndUpdateResource(h_module, FALSE);
RETURN_TRUE;
}
/* }}} */
long php_res_list_get_int_type( const char * string_type )
{
if( !strcmp( string_type, "RT_CURSOR" ) ) return (long)RT_CURSOR;
if( !strcmp( string_type, "RT_BITMAP" ) ) return (long)RT_BITMAP;
if( !strcmp( string_type, "RT_ICON" ) ) return (long)RT_ICON;
if( !strcmp( string_type, "RT_MENU" ) ) return (long)RT_MENU;
if( !strcmp( string_type, "RT_DIALOG" ) ) return (long)RT_DIALOG;
if( !strcmp( string_type, "RT_STRING" ) ) return (long)RT_STRING;
if( !strcmp( string_type, "RT_FONTDIR" ) ) return (long)RT_FONTDIR;
if( !strcmp( string_type, "RT_FONT" ) ) return (long)RT_FONT;
if( !strcmp( string_type, "RT_ACCELERATOR" ) ) return (long)RT_ACCELERATOR;
if( !strcmp( string_type, "RT_RCDATA" ) ) return (long)RT_RCDATA;
if( !strcmp( string_type, "RT_MESSAGETABLE" ) ) return (long)RT_MESSAGETABLE;
if( !strcmp( string_type, "RT_GROUP_CURSOR" ) ) return (long)RT_GROUP_CURSOR;
if( !strcmp( string_type, "RT_GROUP_ICON" ) ) return (long)RT_GROUP_ICON;
if( !strcmp( string_type, "RT_VERSION" ) ) return (long)RT_VERSION;
if( !strcmp( string_type, "RT_DLGINCLUDE" ) ) return (long)RT_DLGINCLUDE;
if( !strcmp( string_type, "RT_PLUGPLAY" ) ) return (long)RT_PLUGPLAY;
if( !strcmp( string_type, "RT_VXD" ) ) return (long)RT_VXD;
if( !strcmp( string_type, "RT_ANICURSOR" ) ) return (long)RT_ANICURSOR;
if( !strcmp( string_type, "RT_ANIICON" ) ) return (long)RT_ANIICON;
if( !strcmp( string_type, "RT_HTML" ) ) return (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
LPCTSTR lpszType, // pointer to resource type
LPTSTR lpszName, // pointer to resource name
LONG lParam // application-defined parameter
)
{
char buffer[8];
zval * array= (zval*) lParam;
if( !IS_INTRESOURCE(lpszName) )
add_next_index_string(array, lpszName, 1);
else {
sprintf( buffer, "#%d", lpszName );
add_next_index_string(array, buffer, 1);
}
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;
long 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 )
RETURN_FALSE;
ZEND_FETCH_RESOURCE(h_module, HMODULE, &res_rc, -1, le_res_resource_name, le_res_resource);
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 );
if( !array_init( return_value ) ) {
if( int_type==-1 )
ret= EnumResourceNames( h_module, type, php_res_list_callback, (LONG)return_value );
else
ret= EnumResourceNames( h_module, MAKEINTRESOURCE(int_type), php_res_list_callback, (LONG)return_value );
if( !ret ) {
if( GetLastError()!=1813 ) // No resource of this type
{ zend_error(E_WARNING, "res_list_type %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 lParam // application-defined parameter
)
{
zval * array;
char buffer[8];
array= (zval*) lParam;
if( !IS_INTRESOURCE(lpszType) )
add_next_index_string(array, lpszType, 1);
else {
sprintf( buffer, "#%d", lpszType );
add_next_index_string(array, buffer, 1);
}
return TRUE;
}
BOOL CALLBACK php_res_list_type_string_callback(
HMODULE hModule, // resource-module handle
LPTSTR lpszType, // pointer to resource type
LONG lParam // application-defined parameter
)
{
zval * array;
char buffer[8];
array= (zval*) lParam;
if( !IS_INTRESOURCE(lpszType) ) {
add_next_index_string(array, lpszType, 1);
return TRUE;
}
#define RES_LIST_TYPE_STRING(type) case type: add_next_index_string(array, #type, 1); break;
switch( (DWORD)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:
sprintf( buffer, "#%d", lpszType );
add_next_index_string(array, buffer, 1);
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 )
RETURN_FALSE;
ZEND_FETCH_RESOURCE(h_module, HMODULE, &res_rc, -1, le_res_resource_name, le_res_resource);
if( !array_init( return_value ) ) {
if( !as_string )
ret= EnumResourceTypes( h_module, php_res_list_type_callback, (LONG)return_value );
else
ret= EnumResourceTypes( h_module, php_res_list_type_string_callback, (LONG)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;
}
/* }}} */