This source file includes following definitions.
- fopencookie
- stream_cookie_reader
- stream_cookie_writer
- stream_cookie_seeker
- stream_cookie_closer
- stream_cookie_reader
- stream_cookie_writer
- stream_cookie_seeker
- stream_cookie_seeker
- stream_cookie_closer
- php_stream_mode_sanitize_fdopen_fopencookie
- _php_stream_cast
- _php_stream_open_wrapper_as_file
- _php_stream_make_seekable
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 #define _GNU_SOURCE
22 #include "php.h"
23 #include "php_globals.h"
24 #include "php_network.h"
25 #include "php_open_temporary_file.h"
26 #include "ext/standard/file.h"
27 #include <stddef.h>
28 #include <fcntl.h>
29
30 #include "php_streams_int.h"
31
32
33 #if defined(HAVE_FUNOPEN) && !defined(HAVE_FOPENCOOKIE)
34
35
36 # if defined(__NetBSD__) && (__NetBSD_Version__ >= 600000000)
37 # define PHP_FPOS_T off_t
38 # else
39 # define PHP_FPOS_T fpos_t
40 # endif
41
42 typedef struct {
43 int (*reader)(void *, char *, int);
44 int (*writer)(void *, const char *, int);
45 PHP_FPOS_T (*seeker)(void *, PHP_FPOS_T, int);
46 int (*closer)(void *);
47 } COOKIE_IO_FUNCTIONS_T;
48
49 FILE *fopencookie(void *cookie, const char *mode, COOKIE_IO_FUNCTIONS_T *funcs)
50 {
51 return funopen(cookie, funcs->reader, funcs->writer, funcs->seeker, funcs->closer);
52 }
53 # define HAVE_FOPENCOOKIE 1
54 # define PHP_EMULATE_FOPENCOOKIE 1
55 # define PHP_STREAM_COOKIE_FUNCTIONS &stream_cookie_functions
56 #elif defined(HAVE_FOPENCOOKIE)
57 # define PHP_STREAM_COOKIE_FUNCTIONS stream_cookie_functions
58 #endif
59
60
61 #if defined(PHP_EMULATE_FOPENCOOKIE)
62
63 static int stream_cookie_reader(void *cookie, char *buffer, int size)
64 {
65 int ret;
66 TSRMLS_FETCH();
67
68 ret = php_stream_read((php_stream*)cookie, buffer, size);
69 return ret;
70 }
71
72 static int stream_cookie_writer(void *cookie, const char *buffer, int size)
73 {
74 TSRMLS_FETCH();
75
76 return php_stream_write((php_stream *)cookie, (char *)buffer, size);
77 }
78
79 static PHP_FPOS_T stream_cookie_seeker(void *cookie, off_t position, int whence)
80 {
81 TSRMLS_FETCH();
82
83 return (PHP_FPOS_T)php_stream_seek((php_stream *)cookie, position, whence);
84 }
85
86 static int stream_cookie_closer(void *cookie)
87 {
88 php_stream *stream = (php_stream*)cookie;
89 TSRMLS_FETCH();
90
91
92 stream->fclose_stdiocast = PHP_STREAM_FCLOSE_NONE;
93 return php_stream_close(stream);
94 }
95 #elif defined(HAVE_FOPENCOOKIE)
96 static ssize_t stream_cookie_reader(void *cookie, char *buffer, size_t size)
97 {
98 ssize_t ret;
99 TSRMLS_FETCH();
100
101 ret = php_stream_read(((php_stream *)cookie), buffer, size);
102 return ret;
103 }
104
105 static ssize_t stream_cookie_writer(void *cookie, const char *buffer, size_t size)
106 {
107 TSRMLS_FETCH();
108
109 return php_stream_write(((php_stream *)cookie), (char *)buffer, size);
110 }
111
112 # ifdef COOKIE_SEEKER_USES_OFF64_T
113 static int stream_cookie_seeker(void *cookie, __off64_t *position, int whence)
114 {
115 TSRMLS_FETCH();
116
117 *position = php_stream_seek((php_stream *)cookie, (off_t)*position, whence);
118
119 if (*position == -1) {
120 return -1;
121 }
122 return 0;
123 }
124 # else
125 static int stream_cookie_seeker(void *cookie, off_t position, int whence)
126 {
127 TSRMLS_FETCH();
128
129 return php_stream_seek((php_stream *)cookie, position, whence);
130 }
131 # endif
132
133 static int stream_cookie_closer(void *cookie)
134 {
135 php_stream *stream = (php_stream*)cookie;
136 TSRMLS_FETCH();
137
138
139 stream->fclose_stdiocast = PHP_STREAM_FCLOSE_NONE;
140 return php_stream_close(stream);
141 }
142 #endif
143
144 #if HAVE_FOPENCOOKIE
145 static COOKIE_IO_FUNCTIONS_T stream_cookie_functions =
146 {
147 stream_cookie_reader, stream_cookie_writer,
148 stream_cookie_seeker, stream_cookie_closer
149 };
150 #else
151
152 #endif
153
154
155
156
157 void php_stream_mode_sanitize_fdopen_fopencookie(php_stream *stream, char *result)
158 {
159
160
161 const char *cur_mode = stream->mode;
162 int has_plus = 0,
163 has_bin = 0,
164 i,
165 res_curs = 0;
166
167 if (cur_mode[0] == 'r' || cur_mode[0] == 'w' || cur_mode[0] == 'a') {
168 result[res_curs++] = cur_mode[0];
169 } else {
170
171
172 result[res_curs++] = 'w';
173
174
175
176 }
177
178
179 for (i = 1; i < 4 && cur_mode[i] != '\0'; i++) {
180 if (cur_mode[i] == 'b') {
181 has_bin = 1;
182 } else if (cur_mode[i] == '+') {
183 has_plus = 1;
184 }
185
186 }
187
188 if (has_bin) {
189 result[res_curs++] = 'b';
190 }
191 if (has_plus) {
192 result[res_curs++] = '+';
193 }
194
195 result[res_curs] = '\0';
196 }
197
198
199
200 PHPAPI int _php_stream_cast(php_stream *stream, int castas, void **ret, int show_err TSRMLS_DC)
201 {
202 int flags = castas & PHP_STREAM_CAST_MASK;
203 castas &= ~PHP_STREAM_CAST_MASK;
204
205
206 if (ret && castas != PHP_STREAM_AS_FD_FOR_SELECT) {
207 php_stream_flush(stream);
208 if (stream->ops->seek && (stream->flags & PHP_STREAM_FLAG_NO_SEEK) == 0) {
209 off_t dummy;
210
211 stream->ops->seek(stream, stream->position, SEEK_SET, &dummy TSRMLS_CC);
212 stream->readpos = stream->writepos = 0;
213 }
214 }
215
216
217
218 if (castas == PHP_STREAM_AS_STDIO) {
219 if (stream->stdiocast) {
220 if (ret) {
221 *(FILE**)ret = stream->stdiocast;
222 }
223 goto exit_success;
224 }
225
226
227
228 if (php_stream_is(stream, PHP_STREAM_IS_STDIO) &&
229 stream->ops->cast &&
230 !php_stream_is_filtered(stream) &&
231 stream->ops->cast(stream, castas, ret TSRMLS_CC) == SUCCESS
232 ) {
233 goto exit_success;
234 }
235
236 #if HAVE_FOPENCOOKIE
237
238 if (ret == NULL) {
239 goto exit_success;
240 }
241
242 {
243 char fixed_mode[5];
244 php_stream_mode_sanitize_fdopen_fopencookie(stream, fixed_mode);
245 *(FILE**)ret = fopencookie(stream, fixed_mode, PHP_STREAM_COOKIE_FUNCTIONS);
246 }
247
248 if (*ret != NULL) {
249 off_t pos;
250
251 stream->fclose_stdiocast = PHP_STREAM_FCLOSE_FOPENCOOKIE;
252
253
254
255 pos = php_stream_tell(stream);
256 if (pos > 0) {
257 fseek(*ret, pos, SEEK_SET);
258 }
259
260 goto exit_success;
261 }
262
263
264
265
266
267
268 php_error_docref(NULL TSRMLS_CC, E_ERROR, "fopencookie failed");
269 return FAILURE;
270 #endif
271
272 if (!php_stream_is_filtered(stream) && stream->ops->cast && stream->ops->cast(stream, castas, NULL TSRMLS_CC) == SUCCESS) {
273 if (FAILURE == stream->ops->cast(stream, castas, ret TSRMLS_CC)) {
274 return FAILURE;
275 }
276 goto exit_success;
277 } else if (flags & PHP_STREAM_CAST_TRY_HARD) {
278 php_stream *newstream;
279
280 newstream = php_stream_fopen_tmpfile();
281 if (newstream) {
282 int retcopy = php_stream_copy_to_stream_ex(stream, newstream, PHP_STREAM_COPY_ALL, NULL);
283
284 if (retcopy != SUCCESS) {
285 php_stream_close(newstream);
286 } else {
287 int retcast = php_stream_cast(newstream, castas | flags, (void **)ret, show_err);
288
289 if (retcast == SUCCESS) {
290 rewind(*(FILE**)ret);
291 }
292
293
294 if ((flags & PHP_STREAM_CAST_RELEASE)) {
295 php_stream_free(stream, PHP_STREAM_FREE_CLOSE_CASTED);
296 }
297
298
299
300 return retcast;
301 }
302 }
303 }
304 }
305
306 if (php_stream_is_filtered(stream)) {
307 php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot cast a filtered stream on this system");
308 return FAILURE;
309 } else if (stream->ops->cast && stream->ops->cast(stream, castas, ret TSRMLS_CC) == SUCCESS) {
310 goto exit_success;
311 }
312
313 if (show_err) {
314
315 static const char *cast_names[4] = {
316 "STDIO FILE*",
317 "File Descriptor",
318 "Socket Descriptor",
319 "select()able descriptor"
320 };
321
322 php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot represent a stream of type %s as a %s", stream->ops->label, cast_names[castas]);
323 }
324
325 return FAILURE;
326
327 exit_success:
328
329 if ((stream->writepos - stream->readpos) > 0 &&
330 stream->fclose_stdiocast != PHP_STREAM_FCLOSE_FOPENCOOKIE &&
331 (flags & PHP_STREAM_CAST_INTERNAL) == 0
332 ) {
333
334
335
336
337 php_error_docref(NULL TSRMLS_CC, E_WARNING, "%ld bytes of buffered data lost during stream conversion!", (long)(stream->writepos - stream->readpos));
338 }
339
340 if (castas == PHP_STREAM_AS_STDIO && ret) {
341 stream->stdiocast = *(FILE**)ret;
342 }
343
344 if (flags & PHP_STREAM_CAST_RELEASE) {
345 php_stream_free(stream, PHP_STREAM_FREE_CLOSE_CASTED);
346 }
347
348 return SUCCESS;
349
350 }
351
352
353
354 PHPAPI FILE * _php_stream_open_wrapper_as_file(char *path, char *mode, int options, char **opened_path STREAMS_DC TSRMLS_DC)
355 {
356 FILE *fp = NULL;
357 php_stream *stream = NULL;
358
359 stream = php_stream_open_wrapper_rel(path, mode, options|STREAM_WILL_CAST, opened_path);
360
361 if (stream == NULL) {
362 return NULL;
363 }
364
365 if (php_stream_cast(stream, PHP_STREAM_AS_STDIO|PHP_STREAM_CAST_TRY_HARD|PHP_STREAM_CAST_RELEASE, (void**)&fp, REPORT_ERRORS) == FAILURE) {
366 php_stream_close(stream);
367 if (opened_path && *opened_path) {
368 efree(*opened_path);
369 }
370 return NULL;
371 }
372 return fp;
373 }
374
375
376
377 PHPAPI int _php_stream_make_seekable(php_stream *origstream, php_stream **newstream, int flags STREAMS_DC TSRMLS_DC)
378 {
379 if (newstream == NULL) {
380 return PHP_STREAM_FAILED;
381 }
382 *newstream = NULL;
383
384 if (((flags & PHP_STREAM_FORCE_CONVERSION) == 0) && origstream->ops->seek != NULL) {
385 *newstream = origstream;
386 return PHP_STREAM_UNCHANGED;
387 }
388
389
390
391 if (flags & PHP_STREAM_PREFER_STDIO) {
392 *newstream = php_stream_fopen_tmpfile();
393 } else {
394 *newstream = php_stream_temp_new();
395 }
396
397 if (*newstream == NULL) {
398 return PHP_STREAM_FAILED;
399 }
400
401 #if ZEND_DEBUG
402 (*newstream)->open_filename = origstream->open_filename;
403 (*newstream)->open_lineno = origstream->open_lineno;
404 #endif
405
406 if (php_stream_copy_to_stream_ex(origstream, *newstream, PHP_STREAM_COPY_ALL, NULL) != SUCCESS) {
407 php_stream_close(*newstream);
408 *newstream = NULL;
409 return PHP_STREAM_CRITICAL;
410 }
411
412 php_stream_close(origstream);
413 php_stream_seek(*newstream, 0, SEEK_SET);
414
415 return PHP_STREAM_RELEASED;
416 }
417
418
419
420
421
422
423
424
425
426