xref: /PHP-5.5/main/streams/cast.c (revision 73c1be26)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 5                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2015 The PHP Group                                |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 3.01 of the PHP license,      |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.php.net/license/3_01.txt                                  |
11    | If you did not receive a copy of the PHP license and are unable to   |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@php.net so we can mail you a copy immediately.               |
14    +----------------------------------------------------------------------+
15    | Authors: Wez Furlong <wez@thebrainroom.com>                          |
16    +----------------------------------------------------------------------+
17  */
18 
19 /* $Id$ */
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 /* Under BSD, emulate fopencookie using funopen */
33 #if defined(HAVE_FUNOPEN) && !defined(HAVE_FOPENCOOKIE)
34 
35 /* NetBSD 6.0+ uses off_t instead of fpos_t in funopen */
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 
fopencookie(void * cookie,const char * mode,COOKIE_IO_FUNCTIONS_T * funcs)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 /* {{{ STDIO with fopencookie */
61 #if defined(PHP_EMULATE_FOPENCOOKIE)
62 /* use our fopencookie emulation */
stream_cookie_reader(void * cookie,char * buffer,int size)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 
stream_cookie_writer(void * cookie,const char * buffer,int size)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 
stream_cookie_seeker(void * cookie,off_t position,int whence)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 
stream_cookie_closer(void * cookie)86 static int stream_cookie_closer(void *cookie)
87 {
88 	php_stream *stream = (php_stream*)cookie;
89 	TSRMLS_FETCH();
90 
91 	/* prevent recursion */
92 	stream->fclose_stdiocast = PHP_STREAM_FCLOSE_NONE;
93 	return php_stream_close(stream);
94 }
95 #elif defined(HAVE_FOPENCOOKIE)
stream_cookie_reader(void * cookie,char * buffer,size_t size)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 
stream_cookie_writer(void * cookie,const char * buffer,size_t size)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
stream_cookie_seeker(void * cookie,__off64_t * position,int whence)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
stream_cookie_seeker(void * cookie,off_t position,int whence)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 
stream_cookie_closer(void * cookie)133 static int stream_cookie_closer(void *cookie)
134 {
135 	php_stream *stream = (php_stream*)cookie;
136 	TSRMLS_FETCH();
137 
138 	/* prevent recursion */
139 	stream->fclose_stdiocast = PHP_STREAM_FCLOSE_NONE;
140 	return php_stream_close(stream);
141 }
142 #endif /* elif defined(HAVE_FOPENCOOKIE) */
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 /* TODO: use socketpair() to emulate fopencookie, as suggested by Hartmut ? */
152 #endif
153 /* }}} */
154 
155 /* {{{ php_stream_mode_sanitize_fdopen_fopencookie
156  * Result should have at least size 5, e.g. to write wbx+\0 */
php_stream_mode_sanitize_fdopen_fopencookie(php_stream * stream,char * result)157 void php_stream_mode_sanitize_fdopen_fopencookie(php_stream *stream, char *result)
158 {
159 	/* replace modes not supported by fdopen and fopencookie, but supported
160 	 * by PHP's fread(), so that their calls won't fail */
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 		/* assume cur_mode[0] is 'c' or 'x'; substitute by 'w', which should not
171 		 * truncate anything in fdopen/fopencookie */
172 		result[res_curs++] = 'w';
173 
174 		/* x is allowed (at least by glibc & compat), but not as the 1st mode
175 		 * as in PHP and in any case is (at best) ignored by fdopen and fopencookie */
176 	}
177 
178 	/* assume current mode has at most length 4 (e.g. wbn+) */
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 		/* ignore 'n', 't' or other stuff */
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 /* {{{ php_stream_cast */
_php_stream_cast(php_stream * stream,int castas,void ** ret,int show_err TSRMLS_DC)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 	/* synchronize our buffer (if possible) */
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 	/* filtered streams can only be cast as stdio, and only when fopencookie is present */
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 		/* if the stream is a stdio stream let's give it a chance to respond
227 		 * first, to avoid doubling up the layers of stdio with an fopencookie */
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 		/* if just checking, say yes we can be a FILE*, but don't actually create it yet */
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 			/* If the stream position is not at the start, we need to force
254 			 * the stdio layer to believe it's real location. */
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 		/* must be either:
264 			a) programmer error
265 			b) no memory
266 			-> lets bail
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 					/* do some specialized cleanup */
294 					if ((flags & PHP_STREAM_CAST_RELEASE)) {
295 						php_stream_free(stream, PHP_STREAM_FREE_CLOSE_CASTED);
296 					}
297 
298 					/* TODO: we probably should be setting .stdiocast and .fclose_stdiocast or
299 					 * we may be leaking the FILE*. Needs investigation, though. */
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 		/* these names depend on the values of the PHP_STREAM_AS_XXX defines in php_streams.h */
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 		/* the data we have buffered will be lost to the third party library that
334 		 * will be accessing the stream.  Emit a warning so that the end-user will
335 		 * know that they should try something else */
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 /* {{{ php_stream_open_wrapper_as_file */
_php_stream_open_wrapper_as_file(char * path,char * mode,int options,char ** opened_path STREAMS_DC TSRMLS_DC)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 /* {{{ php_stream_make_seekable */
_php_stream_make_seekable(php_stream * origstream,php_stream ** newstream,int flags STREAMS_DC TSRMLS_DC)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 	/* Use a tmpfile and copy the old streams contents into it */
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  * Local variables:
421  * tab-width: 4
422  * c-basic-offset: 4
423  * End:
424  * vim600: noet sw=4 ts=4 fdm=marker
425  * vim<600: noet sw=4 ts=4
426  */
427