forked from RDashINC/win32std
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathres_stream.c
229 lines (188 loc) · 6.05 KB
/
res_stream.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
/*
+----------------------------------------------------------------------+
| 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"
typedef struct _res_stream_data{
HMODULE module;
char *data;
size_t size;
size_t pos;
} res_stream_data;
static size_t php_res_read(php_stream *stream, char *buf, size_t count)
{
res_stream_data *self = (res_stream_data *)stream->abstract;
int read;
read= count<self->size-self->pos?count:self->size-self->pos;
memcpy(buf, self->data+self->pos, read);
self->pos+= read;
if( self->pos==self->size )
stream->eof= 1;
return read;
}
static int php_res_seek(php_stream *stream, off_t offset, int whence, off_t *newoffs)
{
res_stream_data *self = (res_stream_data *)stream->abstract;
*newoffs= self->pos;
assert(self!=NULL);
switch( whence ) {
case SEEK_SET:
*newoffs= offset;
break;
case SEEK_CUR:
*newoffs+= offset;
break;
case SEEK_END:
*newoffs= self->size+offset;
break;
default:
return -1;
}
if( *newoffs<0 || *newoffs>(off_t)self->size ) {
*newoffs= self->pos;
return -1;
}
self->pos= *newoffs;
return 0;
}
static int php_res_close(php_stream *stream, int close_handle)
{
res_stream_data *self = (res_stream_data *)stream->abstract;
if( self->module ) FreeLibrary(self->module);
efree(self);
return 1;
}
/* Stream operation entry */
php_stream_ops php_res_stream_ops = {
NULL,php_res_read, /* write, read */
php_res_close,NULL, /* close, flush */
"RES",
php_res_seek, /* seek */
NULL, /* cast */
NULL, /* stat */
NULL /* set_option */
};
php_stream *php_res_stream_open(php_stream_wrapper *wrapper, char *path, char *mode,
int options, char **opened_path, php_stream_context *context STREAMS_DC);
/* Wrapper operation entry */
static php_stream_wrapper_ops php_res_wrapper_ops = {
php_res_stream_open,
NULL, /* close */
NULL, /* stat */
NULL, /* stat_url */
NULL, /* opendir */
"RES"
};
/* Wrapper entry */
php_stream_wrapper php_res_stream_wrapper = {
&php_res_wrapper_ops,
NULL,
0, /* is_url */
};
/* Can handle
"res://c:\adir\another\file.dll/#145/#12"
"res://c:\adir\another\file.dll/#145/#12/"
"res://c:/adir/another/file.dll/#145/#12"
"res://c:\adir\another\file.dll/TYPE/NAME"
*/
php_stream *php_res_stream_open(php_stream_wrapper *wrapper, char *path, char *mode,
int options, char **opened_path, php_stream_context *context STREAMS_DC)
{
res_stream_data *self;
php_stream *stream = NULL;
char *ptr, *type, *name, buffer[WIN32_STRERROR_BUFFER_LEN], *path_copy;
int path_len;
HRSRC hr;
/* check: read only */
if( strchr(mode, '+') || strchr(mode, 'a') || strchr(mode, 'w')) {
//if (options & REPORT_ERRORS) {
zend_error(E_WARNING, "cannot open a res stream for writing");
//}
return NULL;
}
self = emalloc(sizeof(res_stream_data));
memset(self, 0, sizeof(res_stream_data));
if( strncasecmp("res://", path, 6)==0 )
path+= 6;
path_copy= estrdup(path);
ptr= path_copy;
// can't end with /
path_len= strlen(path_copy);
if( path_copy[path_len-1]=='/' ) path_copy[path_len]= 0;
// search name
ptr= path_copy;
ptr= strrchr(ptr, '/');
if( !ptr ) {
zend_error(E_WARNING, "bad res string" );
efree(self);
efree(path_copy);
return NULL;
}
name= ptr+1;
*ptr= 0;
// search type
ptr= path_copy;
ptr= strrchr(ptr, '/');
if( !ptr ) {
zend_error(E_WARNING, "bad res string" );
efree(self);
efree(path_copy);
return NULL;
}
type= ptr+1;
*ptr= 0;
if( *path_copy ) {
self->module= LoadLibrary(path_copy);
if( !self->module ) {
zend_error(E_WARNING, "open '%s' failed: %s", path_copy, win32_strerror(buffer, WIN32_STRERROR_BUFFER_LEN));
efree(self);
efree(path_copy);
return NULL;
}
}
else
self->module= NULL; // Current exe
// Convert name and type to uppercase since lowercase don't work
strupr(name);
strupr(type);
hr= FindResource( self->module, name, type );
if( hr==NULL ) {
zend_error(E_WARNING, "find '%s/%s' failed: %s", type, name, win32_strerror(buffer, WIN32_STRERROR_BUFFER_LEN));
if( self->module ) FreeLibrary(self->module);
efree(self);
efree(path_copy);
return NULL;
}
self->data= LoadResource( self->module, hr );
if( self->data==NULL ) {
zend_error(E_WARNING, "load '%s/%s' failed: %s", type, name, win32_strerror(buffer, WIN32_STRERROR_BUFFER_LEN));
if( self->module ) FreeLibrary(self->module);
efree(self);
efree(path_copy);
return NULL;
}
self->size= SizeofResource( self->module, hr );
efree(path_copy);
// Allocate stream and return it
stream = php_stream_alloc_rel(&php_res_stream_ops, self, 0, mode);
if (stream) {
stream->flags |= PHP_STREAM_FLAG_NO_BUFFER;
//stream->flags |= PHP_STREAM_FLAG_NO_SEEK;
return stream;
}
efree( self );
return NULL;
}