/src/php-src/main/streams/streams.c
Line | Count | Source |
1 | | /* |
2 | | +----------------------------------------------------------------------+ |
3 | | | Copyright © The PHP Group and Contributors. | |
4 | | +----------------------------------------------------------------------+ |
5 | | | This source file is subject to the Modified BSD License that is | |
6 | | | bundled with this package in the file LICENSE, and is available | |
7 | | | through the World Wide Web at <https://www.php.net/license/>. | |
8 | | | | |
9 | | | SPDX-License-Identifier: BSD-3-Clause | |
10 | | +----------------------------------------------------------------------+ |
11 | | | Authors: Wez Furlong <wez@thebrainroom.com> | |
12 | | | Borrowed code from: | |
13 | | | Rasmus Lerdorf <rasmus@lerdorf.on.ca> | |
14 | | | Jim Winstead <jimw@php.net> | |
15 | | +----------------------------------------------------------------------+ |
16 | | */ |
17 | | |
18 | | #ifndef _GNU_SOURCE |
19 | | # define _GNU_SOURCE |
20 | | #endif |
21 | | #include "php.h" |
22 | | #include "php_globals.h" |
23 | | #include "php_memory_streams.h" |
24 | | #include "php_network.h" |
25 | | #include "php_open_temporary_file.h" |
26 | | #include "ext/standard/file.h" |
27 | | #include "ext/standard/basic_functions.h" /* for BG(CurrentStatFile) */ |
28 | | #include "ext/standard/php_string.h" /* for php_memnstr, used by php_stream_get_record() */ |
29 | | #include "ext/uri/php_uri.h" |
30 | | #include <stddef.h> |
31 | | #include <fcntl.h> |
32 | | #include "php_streams_int.h" |
33 | | |
34 | | /* {{{ resource and registration code */ |
35 | | /* Global wrapper hash, copied to FG(stream_wrappers) on registration of volatile wrapper */ |
36 | | static HashTable url_stream_wrappers_hash; |
37 | | static int le_stream = -1; /* true global */ |
38 | | static int le_pstream = -1; /* true global */ |
39 | | static int le_stream_filter = -1; /* true global */ |
40 | | |
41 | | PHPAPI int php_file_le_stream(void) |
42 | 1.99k | { |
43 | 1.99k | return le_stream; |
44 | 1.99k | } |
45 | | |
46 | | PHPAPI int php_file_le_pstream(void) |
47 | 1.99k | { |
48 | 1.99k | return le_pstream; |
49 | 1.99k | } |
50 | | |
51 | | PHPAPI int php_file_le_stream_filter(void) |
52 | 0 | { |
53 | 0 | return le_stream_filter; |
54 | 0 | } |
55 | | |
56 | | PHPAPI HashTable *_php_stream_get_url_stream_wrappers_hash(void) |
57 | 0 | { |
58 | 0 | return (FG(stream_wrappers) ? FG(stream_wrappers) : &url_stream_wrappers_hash); |
59 | 0 | } |
60 | | |
61 | | PHPAPI HashTable *php_stream_get_url_stream_wrappers_hash_global(void) |
62 | 2 | { |
63 | 2 | return &url_stream_wrappers_hash; |
64 | 2 | } |
65 | | |
66 | | static int forget_persistent_resource_id_numbers(zval *el) |
67 | 0 | { |
68 | 0 | php_stream *stream; |
69 | 0 | zend_resource *rsrc = Z_RES_P(el); |
70 | |
|
71 | 0 | if (rsrc->type != le_pstream) { |
72 | 0 | return 0; |
73 | 0 | } |
74 | | |
75 | 0 | stream = (php_stream*)rsrc->ptr; |
76 | |
|
77 | | #if STREAM_DEBUG |
78 | | fprintf(stderr, "forget_persistent: %s:%p\n", stream->ops->label, stream); |
79 | | #endif |
80 | |
|
81 | 0 | stream->res = NULL; |
82 | |
|
83 | 0 | if (stream->ctx) { |
84 | 0 | zend_list_delete(stream->ctx); |
85 | 0 | stream->ctx = NULL; |
86 | 0 | } |
87 | |
|
88 | 0 | return 0; |
89 | 0 | } |
90 | | |
91 | | PHP_RSHUTDOWN_FUNCTION(streams) |
92 | 1.99k | { |
93 | 1.99k | zval *el; |
94 | | |
95 | 1.99k | ZEND_HASH_FOREACH_VAL(&EG(persistent_list), el) { |
96 | 1.99k | forget_persistent_resource_id_numbers(el); |
97 | 1.99k | } ZEND_HASH_FOREACH_END(); |
98 | 1.99k | return SUCCESS; |
99 | 1.99k | } |
100 | | |
101 | | PHPAPI php_stream *php_stream_encloses(php_stream *enclosing, php_stream *enclosed) |
102 | 0 | { |
103 | 0 | php_stream *orig = enclosed->enclosing_stream; |
104 | |
|
105 | 0 | php_stream_auto_cleanup(enclosed); |
106 | 0 | enclosed->flags |= PHP_STREAM_FLAG_NO_FCLOSE; |
107 | 0 | enclosed->enclosing_stream = enclosing; |
108 | 0 | return orig; |
109 | 0 | } |
110 | | |
111 | | PHPAPI int php_stream_from_persistent_id(const char *persistent_id, php_stream **stream) |
112 | 0 | { |
113 | 0 | zend_resource *le; |
114 | |
|
115 | 0 | if ((le = zend_hash_str_find_ptr(&EG(persistent_list), persistent_id, strlen(persistent_id))) != NULL) { |
116 | 0 | if (le->type == le_pstream) { |
117 | 0 | if (stream) { |
118 | 0 | zend_resource *regentry = NULL; |
119 | | |
120 | | /* see if this persistent resource already has been loaded to the |
121 | | * regular list; allowing the same resource in several entries in the |
122 | | * regular list causes trouble (see bug #54623) */ |
123 | 0 | *stream = (php_stream*)le->ptr; |
124 | 0 | ZEND_HASH_FOREACH_PTR(&EG(regular_list), regentry) { |
125 | 0 | if (regentry->ptr == le->ptr) { |
126 | 0 | GC_ADDREF(regentry); |
127 | 0 | (*stream)->res = regentry; |
128 | 0 | return PHP_STREAM_PERSISTENT_SUCCESS; |
129 | 0 | } |
130 | 0 | } ZEND_HASH_FOREACH_END(); |
131 | 0 | GC_ADDREF(le); |
132 | 0 | (*stream)->res = zend_register_resource(*stream, le_pstream); |
133 | 0 | } |
134 | 0 | return PHP_STREAM_PERSISTENT_SUCCESS; |
135 | 0 | } |
136 | 0 | return PHP_STREAM_PERSISTENT_FAILURE; |
137 | 0 | } |
138 | 0 | return PHP_STREAM_PERSISTENT_NOT_EXIST; |
139 | 0 | } |
140 | | |
141 | | /* }}} */ |
142 | | |
143 | | /* allocate a new stream for a particular ops */ |
144 | | PHPAPI php_stream *_php_stream_alloc(const php_stream_ops *ops, void *abstract, const char *persistent_id, const char *mode STREAMS_DC) /* {{{ */ |
145 | 1.99k | { |
146 | 1.99k | php_stream *ret; |
147 | | |
148 | 1.99k | ret = (php_stream*) pemalloc_rel_orig(sizeof(php_stream), persistent_id ? 1 : 0); |
149 | | |
150 | 1.99k | memset(ret, 0, sizeof(php_stream)); |
151 | | |
152 | 1.99k | ret->readfilters.stream = ret; |
153 | 1.99k | ret->writefilters.stream = ret; |
154 | | |
155 | | #if STREAM_DEBUG |
156 | | fprintf(stderr, "stream_alloc: %s:%p persistent=%s\n", ops->label, ret, persistent_id); |
157 | | #endif |
158 | | |
159 | 1.99k | ret->ops = ops; |
160 | 1.99k | ret->abstract = abstract; |
161 | 1.99k | ret->is_persistent = persistent_id ? 1 : 0; |
162 | 1.99k | ret->chunk_size = FG(def_chunk_size); |
163 | | |
164 | 1.99k | #if ZEND_DEBUG |
165 | 1.99k | ret->open_filename = __zend_orig_filename ? __zend_orig_filename : __zend_filename; |
166 | 1.99k | ret->open_lineno = __zend_orig_lineno ? __zend_orig_lineno : __zend_lineno; |
167 | 1.99k | #endif |
168 | | |
169 | 1.99k | if (FG(auto_detect_line_endings)) { |
170 | 0 | ret->flags |= PHP_STREAM_FLAG_DETECT_EOL; |
171 | 0 | } |
172 | | |
173 | 1.99k | if (persistent_id) { |
174 | 0 | if (NULL == zend_register_persistent_resource(persistent_id, strlen(persistent_id), ret, le_pstream)) { |
175 | 0 | pefree(ret, 1); |
176 | 0 | return NULL; |
177 | 0 | } |
178 | 0 | } |
179 | | |
180 | 1.99k | ret->res = zend_register_resource(ret, persistent_id ? le_pstream : le_stream); |
181 | 1.99k | strlcpy(ret->mode, mode, sizeof(ret->mode)); |
182 | | |
183 | 1.99k | ret->wrapper = NULL; |
184 | 1.99k | ret->wrapperthis = NULL; |
185 | 1.99k | ZVAL_UNDEF(&ret->wrapperdata); |
186 | 1.99k | ret->stdiocast = NULL; |
187 | 1.99k | ret->orig_path = NULL; |
188 | 1.99k | ret->ctx = NULL; |
189 | 1.99k | ret->readbuf = NULL; |
190 | 1.99k | ret->enclosing_stream = NULL; |
191 | | |
192 | 1.99k | return ret; |
193 | 1.99k | } |
194 | | /* }}} */ |
195 | | |
196 | | PHPAPI int _php_stream_free_enclosed(php_stream *stream_enclosed, int close_options) /* {{{ */ |
197 | 0 | { |
198 | 0 | return php_stream_free(stream_enclosed, |
199 | 0 | close_options | PHP_STREAM_FREE_IGNORE_ENCLOSING); |
200 | 0 | } |
201 | | /* }}} */ |
202 | | |
203 | | #if STREAM_DEBUG |
204 | | static const char *_php_stream_pretty_free_options(int close_options, char *out) |
205 | | { |
206 | | if (close_options & PHP_STREAM_FREE_CALL_DTOR) |
207 | | strcat(out, "CALL_DTOR, "); |
208 | | if (close_options & PHP_STREAM_FREE_RELEASE_STREAM) |
209 | | strcat(out, "RELEASE_STREAM, "); |
210 | | if (close_options & PHP_STREAM_FREE_PRESERVE_HANDLE) |
211 | | strcat(out, "PRESERVE_HANDLE, "); |
212 | | if (close_options & PHP_STREAM_FREE_RSRC_DTOR) |
213 | | strcat(out, "RSRC_DTOR, "); |
214 | | if (close_options & PHP_STREAM_FREE_PERSISTENT) |
215 | | strcat(out, "PERSISTENT, "); |
216 | | if (close_options & PHP_STREAM_FREE_IGNORE_ENCLOSING) |
217 | | strcat(out, "IGNORE_ENCLOSING, "); |
218 | | if (out[0] != '\0') |
219 | | out[strlen(out) - 2] = '\0'; |
220 | | return out; |
221 | | } |
222 | | #endif |
223 | | |
224 | | static int _php_stream_free_persistent(zval *zv, void *pStream) |
225 | 0 | { |
226 | 0 | zend_resource *le = Z_RES_P(zv); |
227 | 0 | return le->ptr == pStream; |
228 | 0 | } |
229 | | |
230 | | |
231 | | PHPAPI int _php_stream_free(php_stream *stream, int close_options) /* {{{ */ |
232 | 1.99k | { |
233 | 1.99k | int ret = 1; |
234 | 1.99k | int preserve_handle = close_options & PHP_STREAM_FREE_PRESERVE_HANDLE ? 1 : 0; |
235 | 1.99k | int release_cast = 1; |
236 | 1.99k | php_stream_context *context; |
237 | | |
238 | | /* During shutdown resources may be released before other resources still holding them. |
239 | | * When only resources are referenced this is not a problem, because they are refcounted |
240 | | * and will only be fully freed once the refcount drops to zero. However, if php_stream* |
241 | | * is held directly, we don't have this guarantee. To avoid use-after-free we ignore all |
242 | | * stream free operations in shutdown unless they come from the resource list destruction, |
243 | | * or by freeing an enclosed stream (in which case resource list destruction will not have |
244 | | * freed it). */ |
245 | 1.99k | if ((EG(flags) & EG_FLAGS_IN_RESOURCE_SHUTDOWN) && |
246 | 0 | !(close_options & (PHP_STREAM_FREE_RSRC_DTOR|PHP_STREAM_FREE_IGNORE_ENCLOSING))) { |
247 | 0 | return 1; |
248 | 0 | } |
249 | | |
250 | 1.99k | context = PHP_STREAM_CONTEXT(stream); |
251 | | |
252 | 1.99k | if ((stream->flags & PHP_STREAM_FLAG_NO_CLOSE) || |
253 | 1.99k | ((stream->flags & PHP_STREAM_FLAG_NO_RSCR_DTOR_CLOSE) && (close_options & PHP_STREAM_FREE_RSRC_DTOR))) { |
254 | 0 | preserve_handle = 1; |
255 | 0 | } |
256 | | |
257 | | #if STREAM_DEBUG |
258 | | { |
259 | | char out[200] = ""; |
260 | | fprintf(stderr, "stream_free: %s:%p[%s] in_free=%d opts=%s\n", |
261 | | stream->ops->label, stream, stream->orig_path, stream->in_free, _php_stream_pretty_free_options(close_options, out)); |
262 | | } |
263 | | |
264 | | #endif |
265 | | |
266 | 1.99k | if (stream->in_free) { |
267 | | /* hopefully called recursively from the enclosing stream; the pointer was NULLed below */ |
268 | 0 | if ((stream->in_free == 1) && (close_options & PHP_STREAM_FREE_IGNORE_ENCLOSING) && (stream->enclosing_stream == NULL)) { |
269 | 0 | close_options |= PHP_STREAM_FREE_RSRC_DTOR; /* restore flag */ |
270 | 0 | } else { |
271 | 0 | return 1; /* recursion protection */ |
272 | 0 | } |
273 | 0 | } |
274 | | |
275 | 1.99k | stream->in_free++; |
276 | | |
277 | | /* force correct order on enclosing/enclosed stream destruction (only from resource |
278 | | * destructor as in when reverse destroying the resource list) */ |
279 | 1.99k | if ((close_options & PHP_STREAM_FREE_RSRC_DTOR) && |
280 | 1.99k | !(close_options & PHP_STREAM_FREE_IGNORE_ENCLOSING) && |
281 | 1.99k | (close_options & (PHP_STREAM_FREE_CALL_DTOR | PHP_STREAM_FREE_RELEASE_STREAM)) && /* always? */ |
282 | 1.99k | (stream->enclosing_stream != NULL)) { |
283 | 0 | php_stream *enclosing_stream = stream->enclosing_stream; |
284 | 0 | stream->enclosing_stream = NULL; |
285 | | /* we force PHP_STREAM_CALL_DTOR because that's from where the |
286 | | * enclosing stream can free this stream. */ |
287 | 0 | return php_stream_free(enclosing_stream, |
288 | 0 | (close_options | PHP_STREAM_FREE_CALL_DTOR | PHP_STREAM_FREE_KEEP_RSRC) & ~PHP_STREAM_FREE_RSRC_DTOR); |
289 | 0 | } |
290 | | |
291 | | /* if we are releasing the stream only (and preserving the underlying handle), |
292 | | * we need to do things a little differently. |
293 | | * We are only ever called like this when the stream is cast to a FILE* |
294 | | * for include (or other similar) purposes. |
295 | | * */ |
296 | 1.99k | if (preserve_handle) { |
297 | 0 | if (stream->fclose_stdiocast == PHP_STREAM_FCLOSE_FOPENCOOKIE) { |
298 | | /* If the stream was fopencookied, we must NOT touch anything |
299 | | * here, as the cookied stream relies on it all. |
300 | | * Instead, mark the stream as OK to auto-clean */ |
301 | 0 | php_stream_auto_cleanup(stream); |
302 | 0 | stream->in_free--; |
303 | 0 | return 0; |
304 | 0 | } |
305 | | /* otherwise, make sure that we don't close the FILE* from a cast */ |
306 | 0 | release_cast = 0; |
307 | 0 | } |
308 | | |
309 | | #if STREAM_DEBUG |
310 | | fprintf(stderr, "stream_free: %s:%p[%s] preserve_handle=%d release_cast=%d remove_rsrc=%d\n", |
311 | | stream->ops->label, stream, stream->orig_path, preserve_handle, release_cast, |
312 | | (close_options & PHP_STREAM_FREE_RSRC_DTOR) == 0); |
313 | | #endif |
314 | | |
315 | 1.99k | if (stream->flags & PHP_STREAM_FLAG_WAS_WRITTEN || stream->writefilters.head) { |
316 | | /* make sure everything is saved */ |
317 | 1.99k | _php_stream_flush(stream, 1); |
318 | 1.99k | } |
319 | | |
320 | | /* If not called from the resource dtor, remove the stream from the resource list. */ |
321 | 1.99k | if ((close_options & PHP_STREAM_FREE_RSRC_DTOR) == 0 && stream->res) { |
322 | | /* Close resource, but keep it in resource list */ |
323 | 0 | zend_list_close(stream->res); |
324 | 0 | if ((close_options & PHP_STREAM_FREE_KEEP_RSRC) == 0) { |
325 | | /* Completely delete zend_resource, if not referenced */ |
326 | 0 | zend_list_delete(stream->res); |
327 | 0 | stream->res = NULL; |
328 | 0 | } |
329 | 0 | } |
330 | | |
331 | 1.99k | if (close_options & PHP_STREAM_FREE_CALL_DTOR) { |
332 | 1.99k | if (release_cast && stream->fclose_stdiocast == PHP_STREAM_FCLOSE_FOPENCOOKIE) { |
333 | | /* calling fclose on an fopencookied stream will ultimately |
334 | | call this very same function. If we were called via fclose, |
335 | | the cookie_closer unsets the fclose_stdiocast flags, so |
336 | | we can be sure that we only reach here when PHP code calls |
337 | | php_stream_free. |
338 | | Let's let the cookie code clean it all up. |
339 | | */ |
340 | 0 | stream->in_free = 0; |
341 | 0 | return fclose(stream->stdiocast); |
342 | 0 | } |
343 | | |
344 | 1.99k | ret = stream->ops->close(stream, preserve_handle ? 0 : 1); |
345 | 1.99k | stream->abstract = NULL; |
346 | | |
347 | | /* tidy up any FILE* that might have been fdopened */ |
348 | 1.99k | if (release_cast && stream->fclose_stdiocast == PHP_STREAM_FCLOSE_FDOPEN && stream->stdiocast) { |
349 | 0 | fclose(stream->stdiocast); |
350 | 0 | stream->stdiocast = NULL; |
351 | 0 | stream->fclose_stdiocast = PHP_STREAM_FCLOSE_NONE; |
352 | 0 | } |
353 | 1.99k | } |
354 | | |
355 | 1.99k | if (close_options & PHP_STREAM_FREE_RELEASE_STREAM) { |
356 | 1.99k | while (stream->readfilters.head) { |
357 | 0 | if (stream->readfilters.head->res != NULL) { |
358 | 0 | zend_list_close(stream->readfilters.head->res); |
359 | 0 | } |
360 | 0 | php_stream_filter_remove(stream->readfilters.head, 1); |
361 | 0 | } |
362 | 1.99k | while (stream->writefilters.head) { |
363 | 0 | if (stream->writefilters.head->res != NULL) { |
364 | 0 | zend_list_close(stream->writefilters.head->res); |
365 | 0 | } |
366 | 0 | php_stream_filter_remove(stream->writefilters.head, 1); |
367 | 0 | } |
368 | | |
369 | 1.99k | if (stream->wrapper && stream->wrapper->wops && stream->wrapper->wops->stream_closer) { |
370 | 0 | stream->wrapper->wops->stream_closer(stream->wrapper, stream); |
371 | 0 | stream->wrapper = NULL; |
372 | 0 | } |
373 | | |
374 | 1.99k | if (Z_TYPE(stream->wrapperdata) != IS_UNDEF) { |
375 | 0 | zval_ptr_dtor(&stream->wrapperdata); |
376 | 0 | ZVAL_UNDEF(&stream->wrapperdata); |
377 | 0 | } |
378 | | |
379 | 1.99k | if (stream->error_list) { |
380 | 0 | zend_llist_destroy(stream->error_list); |
381 | 0 | pefree(stream->error_list, stream->is_persistent); |
382 | 0 | } |
383 | | |
384 | 1.99k | if (stream->readbuf) { |
385 | 0 | pefree(stream->readbuf, stream->is_persistent); |
386 | 0 | stream->readbuf = NULL; |
387 | 0 | } |
388 | | |
389 | 1.99k | if (stream->is_persistent && (close_options & PHP_STREAM_FREE_PERSISTENT)) { |
390 | | /* we don't work with *stream but need its value for comparison */ |
391 | 0 | zend_hash_apply_with_argument(&EG(persistent_list), _php_stream_free_persistent, stream); |
392 | 0 | } |
393 | | |
394 | 1.99k | if (stream->orig_path) { |
395 | 0 | pefree(stream->orig_path, stream->is_persistent); |
396 | 0 | stream->orig_path = NULL; |
397 | 0 | } |
398 | | |
399 | 1.99k | pefree(stream, stream->is_persistent); |
400 | 1.99k | } |
401 | | |
402 | 1.99k | if (context) { |
403 | 0 | zend_list_delete(context->res); |
404 | 0 | } |
405 | | |
406 | 1.99k | return ret; |
407 | 1.99k | } |
408 | | /* }}} */ |
409 | | |
410 | | /* {{{ generic stream operations */ |
411 | | |
412 | | PHPAPI zend_result _php_stream_fill_read_buffer(php_stream *stream, size_t size) |
413 | 0 | { |
414 | | /* allocate/fill the buffer */ |
415 | |
|
416 | 0 | zend_result retval; |
417 | 0 | bool old_eof = stream->eof; |
418 | |
|
419 | 0 | if (stream->readfilters.head) { |
420 | 0 | size_t to_read_now = MIN(size, stream->chunk_size); |
421 | 0 | char *chunk_buf; |
422 | 0 | php_stream_bucket_brigade brig_in = { NULL, NULL }, brig_out = { NULL, NULL }; |
423 | 0 | php_stream_bucket_brigade *brig_inp = &brig_in, *brig_outp = &brig_out, *brig_swap; |
424 | | |
425 | | /* allocate a buffer for reading chunks */ |
426 | 0 | chunk_buf = emalloc(stream->chunk_size); |
427 | |
|
428 | 0 | while (!stream->eof && (stream->writepos - stream->readpos < (zend_off_t)to_read_now)) { |
429 | 0 | ssize_t justread = 0; |
430 | 0 | int flags; |
431 | 0 | php_stream_bucket *bucket; |
432 | 0 | php_stream_filter_status_t status = PSFS_ERR_FATAL; |
433 | 0 | php_stream_filter *filter; |
434 | | |
435 | | /* read a chunk into a bucket */ |
436 | 0 | justread = stream->ops->read(stream, chunk_buf, stream->chunk_size); |
437 | 0 | if (justread < 0 && stream->writepos == stream->readpos) { |
438 | 0 | efree(chunk_buf); |
439 | 0 | retval = FAILURE; |
440 | 0 | goto out_check_eof; |
441 | 0 | } else if (justread > 0) { |
442 | 0 | bucket = php_stream_bucket_new(stream, chunk_buf, justread, 0, 0); |
443 | | |
444 | | /* after this call, bucket is owned by the brigade */ |
445 | 0 | php_stream_bucket_append(brig_inp, bucket); |
446 | |
|
447 | 0 | flags = stream->eof ? PSFS_FLAG_FLUSH_CLOSE : PSFS_FLAG_NORMAL; |
448 | 0 | } else { |
449 | 0 | flags = stream->eof ? PSFS_FLAG_FLUSH_CLOSE : PSFS_FLAG_FLUSH_INC; |
450 | 0 | } |
451 | | |
452 | | /* wind the handle... */ |
453 | 0 | for (filter = stream->readfilters.head; filter; filter = filter->next) { |
454 | 0 | status = filter->fops->filter(stream, filter, brig_inp, brig_outp, NULL, flags); |
455 | |
|
456 | 0 | if (status != PSFS_PASS_ON) { |
457 | 0 | break; |
458 | 0 | } |
459 | | |
460 | | /* brig_out becomes brig_in. |
461 | | * brig_in will always be empty here, as the filter MUST attach any un-consumed buckets |
462 | | * to its own brigade */ |
463 | 0 | brig_swap = brig_inp; |
464 | 0 | brig_inp = brig_outp; |
465 | 0 | brig_outp = brig_swap; |
466 | 0 | memset(brig_outp, 0, sizeof(*brig_outp)); |
467 | 0 | } |
468 | |
|
469 | 0 | switch (status) { |
470 | 0 | case PSFS_PASS_ON: |
471 | | /* we get here when the last filter in the chain has data to pass on. |
472 | | * in this situation, we are passing the brig_in brigade into the |
473 | | * stream read buffer */ |
474 | 0 | while (brig_inp->head) { |
475 | 0 | bucket = brig_inp->head; |
476 | | /* reduce buffer memory consumption if possible, to avoid a realloc */ |
477 | 0 | if (stream->readbuf && stream->readbuflen - stream->writepos < bucket->buflen) { |
478 | 0 | if (stream->writepos > stream->readpos) { |
479 | 0 | memmove(stream->readbuf, stream->readbuf + stream->readpos, stream->writepos - stream->readpos); |
480 | 0 | } |
481 | 0 | stream->writepos -= stream->readpos; |
482 | 0 | stream->readpos = 0; |
483 | 0 | } |
484 | | /* grow buffer to hold this bucket */ |
485 | 0 | if (stream->readbuflen - stream->writepos < bucket->buflen) { |
486 | 0 | stream->readbuflen += bucket->buflen; |
487 | 0 | stream->readbuf = perealloc(stream->readbuf, stream->readbuflen, |
488 | 0 | stream->is_persistent); |
489 | 0 | } |
490 | 0 | if (bucket->buflen) { |
491 | 0 | memcpy(stream->readbuf + stream->writepos, bucket->buf, bucket->buflen); |
492 | 0 | } |
493 | 0 | stream->writepos += bucket->buflen; |
494 | |
|
495 | 0 | php_stream_bucket_unlink(bucket); |
496 | 0 | php_stream_bucket_delref(bucket); |
497 | 0 | } |
498 | 0 | break; |
499 | | |
500 | 0 | case PSFS_FEED_ME: |
501 | | /* when a filter needs feeding, there is no brig_out to deal with. |
502 | | * we simply continue the loop; if the caller needs more data, |
503 | | * we will read again, otherwise out job is done here */ |
504 | | |
505 | | /* Filter could have added buckets anyway, but signalled that it did not return any. Discard them. */ |
506 | 0 | while ((bucket = brig_outp->head)) { |
507 | 0 | php_stream_bucket_unlink(bucket); |
508 | 0 | php_stream_bucket_delref(bucket); |
509 | 0 | } |
510 | 0 | break; |
511 | | |
512 | 0 | case PSFS_ERR_FATAL: |
513 | | /* some fatal error. Theoretically, the stream is borked, so all |
514 | | * further reads should fail. */ |
515 | 0 | stream->eof = 1; |
516 | 0 | stream->fatal_error = 1; |
517 | | /* free all data left in brigades */ |
518 | 0 | while ((bucket = brig_inp->head)) { |
519 | | /* Remove unconsumed buckets from the input brigade */ |
520 | 0 | php_stream_bucket_unlink(bucket); |
521 | 0 | php_stream_bucket_delref(bucket); |
522 | 0 | } |
523 | 0 | while ((bucket = brig_outp->head)) { |
524 | | /* Remove unconsumed buckets from the output brigade */ |
525 | 0 | php_stream_bucket_unlink(bucket); |
526 | 0 | php_stream_bucket_delref(bucket); |
527 | 0 | } |
528 | 0 | efree(chunk_buf); |
529 | 0 | retval = FAILURE; |
530 | 0 | goto out_is_eof; |
531 | 0 | } |
532 | | |
533 | 0 | if (justread <= 0) { |
534 | 0 | break; |
535 | 0 | } |
536 | 0 | } |
537 | | |
538 | 0 | efree(chunk_buf); |
539 | 0 | return SUCCESS; |
540 | 0 | } else { |
541 | | /* is there enough data in the buffer ? */ |
542 | 0 | if (stream->writepos - stream->readpos < (zend_off_t)size) { |
543 | 0 | ssize_t justread = 0; |
544 | | |
545 | | /* reduce buffer memory consumption if possible, to avoid a realloc */ |
546 | 0 | if (stream->readbuf && stream->readbuflen - stream->writepos < stream->chunk_size) { |
547 | 0 | if (stream->writepos > stream->readpos) { |
548 | 0 | memmove(stream->readbuf, stream->readbuf + stream->readpos, stream->writepos - stream->readpos); |
549 | 0 | } |
550 | 0 | stream->writepos -= stream->readpos; |
551 | 0 | stream->readpos = 0; |
552 | 0 | } |
553 | | |
554 | | /* grow the buffer if required |
555 | | * TODO: this can fail for persistent streams */ |
556 | 0 | if (stream->readbuflen - stream->writepos < stream->chunk_size) { |
557 | 0 | stream->readbuflen += stream->chunk_size; |
558 | 0 | stream->readbuf = perealloc(stream->readbuf, stream->readbuflen, |
559 | 0 | stream->is_persistent); |
560 | 0 | } |
561 | |
|
562 | 0 | justread = stream->ops->read(stream, (char*)stream->readbuf + stream->writepos, |
563 | 0 | stream->readbuflen - stream->writepos |
564 | 0 | ); |
565 | 0 | if (justread < 0) { |
566 | 0 | retval = FAILURE; |
567 | 0 | goto out_check_eof; |
568 | 0 | } |
569 | 0 | stream->writepos += justread; |
570 | 0 | retval = SUCCESS; |
571 | 0 | goto out_check_eof; |
572 | 0 | } |
573 | 0 | return SUCCESS; |
574 | 0 | } |
575 | | |
576 | 0 | out_check_eof: |
577 | 0 | if (old_eof != stream->eof) { |
578 | 0 | out_is_eof: |
579 | 0 | php_stream_notify_completed(PHP_STREAM_CONTEXT(stream)); |
580 | 0 | } |
581 | 0 | return retval; |
582 | 0 | } |
583 | | |
584 | | PHPAPI ssize_t _php_stream_read(php_stream *stream, char *buf, size_t size) |
585 | 1.73M | { |
586 | 1.73M | ssize_t toread = 0, didread = 0; |
587 | | |
588 | 3.47M | while (size > 0) { |
589 | | |
590 | | /* take from the read buffer first. |
591 | | * It is possible that a buffered stream was switched to non-buffered, so we |
592 | | * drain the remainder of the buffer before using the "raw" read mode for |
593 | | * the excess */ |
594 | 1.73M | if (stream->writepos > stream->readpos) { |
595 | |
|
596 | 0 | toread = stream->writepos - stream->readpos; |
597 | 0 | if (toread > size) { |
598 | 0 | toread = size; |
599 | 0 | } |
600 | |
|
601 | 0 | memcpy(buf, stream->readbuf + stream->readpos, toread); |
602 | 0 | stream->readpos += toread; |
603 | 0 | size -= toread; |
604 | 0 | buf += toread; |
605 | 0 | didread += toread; |
606 | 0 | stream->has_buffered_data = 1; |
607 | 0 | } |
608 | | |
609 | | /* ignore eof here; the underlying state might have changed */ |
610 | 1.73M | if (size == 0) { |
611 | 0 | break; |
612 | 0 | } |
613 | | |
614 | 1.73M | if (!stream->readfilters.head && ((stream->flags & PHP_STREAM_FLAG_NO_BUFFER) || stream->chunk_size == 1)) { |
615 | 1.73M | toread = stream->ops->read(stream, buf, size); |
616 | 1.73M | if (toread < 0) { |
617 | | /* Report an error if the read failed and we did not read any data |
618 | | * before that. Otherwise return the data we did read. */ |
619 | 0 | if (didread == 0) { |
620 | 0 | return toread; |
621 | 0 | } |
622 | 0 | break; |
623 | 0 | } |
624 | 1.73M | } else { |
625 | 0 | if (php_stream_fill_read_buffer(stream, size) != SUCCESS) { |
626 | 0 | if (didread == 0) { |
627 | 0 | return -1; |
628 | 0 | } |
629 | 0 | break; |
630 | 0 | } |
631 | | |
632 | 0 | toread = stream->writepos - stream->readpos; |
633 | 0 | if ((size_t) toread > size) { |
634 | 0 | toread = size; |
635 | 0 | } |
636 | |
|
637 | 0 | if (toread > 0) { |
638 | 0 | memcpy(buf, stream->readbuf + stream->readpos, toread); |
639 | 0 | stream->readpos += toread; |
640 | 0 | } |
641 | 0 | } |
642 | 1.73M | if (toread > 0) { |
643 | 1.73M | didread += toread; |
644 | 1.73M | buf += toread; |
645 | 1.73M | size -= toread; |
646 | 1.73M | stream->has_buffered_data = 1; |
647 | 1.73M | } else { |
648 | | /* EOF, or temporary end of data (for non-blocking mode). */ |
649 | 577 | break; |
650 | 577 | } |
651 | | |
652 | | /* just break anyway, to avoid greedy read for file://, php://memory, and php://temp */ |
653 | 1.73M | if ((stream->wrapper != &php_plain_files_wrapper) && |
654 | 1.73M | (stream->ops != &php_stream_memory_ops) && |
655 | 0 | (stream->ops != &php_stream_temp_ops)) { |
656 | 0 | break; |
657 | 0 | } |
658 | 1.73M | } |
659 | | |
660 | 1.73M | if (didread > 0) { |
661 | 1.73M | stream->position += didread; |
662 | 1.73M | stream->has_buffered_data = 0; |
663 | 1.73M | } |
664 | | |
665 | 1.73M | return didread; |
666 | 1.73M | } |
667 | | |
668 | | /* Like php_stream_read(), but reading into a zend_string buffer. This has some similarity |
669 | | * to the copy_to_mem() operation, but only performs a single direct read. */ |
670 | | PHPAPI zend_string *php_stream_read_to_str(php_stream *stream, size_t len) |
671 | 0 | { |
672 | 0 | zend_string *str = zend_string_alloc(len, 0); |
673 | 0 | ssize_t read = php_stream_read(stream, ZSTR_VAL(str), len); |
674 | 0 | if (read < 0) { |
675 | 0 | zend_string_efree(str); |
676 | 0 | return NULL; |
677 | 0 | } |
678 | | |
679 | 0 | ZSTR_LEN(str) = read; |
680 | 0 | ZSTR_VAL(str)[read] = 0; |
681 | |
|
682 | 0 | if ((size_t) read < len / 2) { |
683 | 0 | return zend_string_truncate(str, read, 0); |
684 | 0 | } |
685 | 0 | return str; |
686 | 0 | } |
687 | | |
688 | | PHPAPI bool _php_stream_eof(php_stream *stream) |
689 | 0 | { |
690 | | /* if there is data in the buffer, it's not EOF */ |
691 | 0 | if (stream->writepos - stream->readpos > 0) { |
692 | 0 | return 0; |
693 | 0 | } |
694 | | |
695 | | /* use the configured timeout when checking eof */ |
696 | 0 | if (!stream->eof && PHP_STREAM_OPTION_RETURN_ERR == |
697 | 0 | php_stream_set_option(stream, PHP_STREAM_OPTION_CHECK_LIVENESS, |
698 | 0 | 0, NULL)) { |
699 | 0 | stream->eof = 1; |
700 | 0 | } |
701 | |
|
702 | 0 | return stream->eof; |
703 | 0 | } |
704 | | |
705 | | PHPAPI int _php_stream_putc(php_stream *stream, int c) |
706 | 0 | { |
707 | 0 | unsigned char buf = c; |
708 | |
|
709 | 0 | if (php_stream_write(stream, (char*)&buf, 1) > 0) { |
710 | 0 | return 1; |
711 | 0 | } |
712 | 0 | return EOF; |
713 | 0 | } |
714 | | |
715 | | PHPAPI int _php_stream_getc(php_stream *stream) |
716 | 1.34M | { |
717 | 1.34M | char buf; |
718 | | |
719 | 1.34M | if (php_stream_read(stream, &buf, 1) > 0) { |
720 | 1.34M | return buf & 0xff; |
721 | 1.34M | } |
722 | 197 | return EOF; |
723 | 1.34M | } |
724 | | |
725 | | PHPAPI bool _php_stream_puts(php_stream *stream, const char *buf) |
726 | 0 | { |
727 | 0 | size_t len; |
728 | 0 | char newline[2] = "\n"; /* is this OK for Win? */ |
729 | 0 | len = strlen(buf); |
730 | |
|
731 | 0 | if (len > 0 && php_stream_write(stream, buf, len) > 0 && php_stream_write(stream, newline, 1) > 0) { |
732 | 0 | return 1; |
733 | 0 | } |
734 | 0 | return 0; |
735 | 0 | } |
736 | | |
737 | | PHPAPI int _php_stream_stat(php_stream *stream, php_stream_statbuf *ssb) |
738 | 0 | { |
739 | 0 | memset(ssb, 0, sizeof(*ssb)); |
740 | | |
741 | | /* if the stream was wrapped, allow the wrapper to stat it */ |
742 | 0 | if (stream->wrapper && stream->wrapper->wops->stream_stat != NULL) { |
743 | 0 | return stream->wrapper->wops->stream_stat(stream->wrapper, stream, ssb); |
744 | 0 | } |
745 | | |
746 | | /* if the stream doesn't directly support stat-ing, return with failure. |
747 | | * We could try and emulate this by casting to an FD and fstat-ing it, |
748 | | * but since the fd might not represent the actual underlying content |
749 | | * this would give bogus results. */ |
750 | 0 | if (stream->ops->stat == NULL) { |
751 | 0 | return -1; |
752 | 0 | } |
753 | | |
754 | 0 | return (stream->ops->stat)(stream, ssb); |
755 | 0 | } |
756 | | |
757 | | PHPAPI const char *php_stream_locate_eol(php_stream *stream, zend_string *buf) |
758 | 0 | { |
759 | 0 | size_t avail; |
760 | 0 | const char *eol = NULL; |
761 | 0 | const char *readptr; |
762 | |
|
763 | 0 | if (!buf) { |
764 | 0 | readptr = (char*)stream->readbuf + stream->readpos; |
765 | 0 | avail = stream->writepos - stream->readpos; |
766 | 0 | } else { |
767 | 0 | readptr = ZSTR_VAL(buf); |
768 | 0 | avail = ZSTR_LEN(buf); |
769 | 0 | } |
770 | | |
771 | | /* Look for EOL */ |
772 | 0 | if (stream->flags & PHP_STREAM_FLAG_DETECT_EOL) { |
773 | 0 | const char *cr = memchr(readptr, '\r', avail); |
774 | 0 | const char *lf = memchr(readptr, '\n', avail); |
775 | |
|
776 | 0 | if (cr && lf != cr + 1 && !(lf && lf < cr)) { |
777 | | /* mac */ |
778 | 0 | stream->flags ^= PHP_STREAM_FLAG_DETECT_EOL; |
779 | 0 | stream->flags |= PHP_STREAM_FLAG_EOL_MAC; |
780 | 0 | eol = cr; |
781 | 0 | } else if ((cr && lf && cr == lf - 1) || (lf)) { |
782 | | /* dos or unix endings */ |
783 | 0 | stream->flags ^= PHP_STREAM_FLAG_DETECT_EOL; |
784 | 0 | eol = lf; |
785 | 0 | } |
786 | 0 | } else if (stream->flags & PHP_STREAM_FLAG_EOL_MAC) { |
787 | 0 | eol = memchr(readptr, '\r', avail); |
788 | 0 | } else { |
789 | | /* unix (and dos) line endings */ |
790 | 0 | eol = memchr(readptr, '\n', avail); |
791 | 0 | } |
792 | |
|
793 | 0 | return eol; |
794 | 0 | } |
795 | | |
796 | | /* If buf == NULL, the buffer will be allocated automatically and will be of an |
797 | | * appropriate length to hold the line, regardless of the line length, memory |
798 | | * permitting */ |
799 | | PHPAPI char *_php_stream_get_line(php_stream *stream, char *buf, size_t maxlen, |
800 | | size_t *returned_len) |
801 | 0 | { |
802 | 0 | size_t avail = 0; |
803 | 0 | size_t current_buf_size = 0; |
804 | 0 | size_t total_copied = 0; |
805 | 0 | int grow_mode = 0; |
806 | 0 | char *bufstart = buf; |
807 | |
|
808 | 0 | if (buf == NULL) { |
809 | 0 | grow_mode = 1; |
810 | 0 | } else if (maxlen == 0) { |
811 | 0 | return NULL; |
812 | 0 | } |
813 | | |
814 | | /* |
815 | | * If the underlying stream operations block when no new data is readable, |
816 | | * we need to take extra precautions. |
817 | | * |
818 | | * If there is buffered data available, we check for a EOL. If it exists, |
819 | | * we pass the data immediately back to the caller. This saves a call |
820 | | * to the read implementation and will not block where blocking |
821 | | * is not necessary at all. |
822 | | * |
823 | | * If the stream buffer contains more data than the caller requested, |
824 | | * we can also avoid that costly step and simply return that data. |
825 | | */ |
826 | | |
827 | 0 | for (;;) { |
828 | 0 | avail = stream->writepos - stream->readpos; |
829 | |
|
830 | 0 | if (avail > 0) { |
831 | 0 | size_t cpysz = 0; |
832 | 0 | char *readptr; |
833 | 0 | const char *eol; |
834 | 0 | int done = 0; |
835 | |
|
836 | 0 | readptr = (char*)stream->readbuf + stream->readpos; |
837 | 0 | eol = php_stream_locate_eol(stream, NULL); |
838 | |
|
839 | 0 | if (eol) { |
840 | 0 | cpysz = eol - readptr + 1; |
841 | 0 | done = 1; |
842 | 0 | } else { |
843 | 0 | cpysz = avail; |
844 | 0 | } |
845 | |
|
846 | 0 | if (grow_mode) { |
847 | | /* allow room for a NUL. If this realloc is really a realloc |
848 | | * (ie: second time around), we get an extra byte. In most |
849 | | * cases, with the default chunk size of 8K, we will only |
850 | | * incur that overhead once. When people have lines longer |
851 | | * than 8K, we waste 1 byte per additional 8K or so. |
852 | | * That seems acceptable to me, to avoid making this code |
853 | | * hard to follow */ |
854 | 0 | bufstart = erealloc(bufstart, current_buf_size + cpysz + 1); |
855 | 0 | current_buf_size += cpysz + 1; |
856 | 0 | buf = bufstart + total_copied; |
857 | 0 | } else { |
858 | 0 | if (cpysz >= maxlen - 1) { |
859 | 0 | cpysz = maxlen - 1; |
860 | 0 | done = 1; |
861 | 0 | } |
862 | 0 | } |
863 | |
|
864 | 0 | memcpy(buf, readptr, cpysz); |
865 | |
|
866 | 0 | stream->position += cpysz; |
867 | 0 | stream->readpos += cpysz; |
868 | 0 | buf += cpysz; |
869 | 0 | maxlen -= cpysz; |
870 | 0 | total_copied += cpysz; |
871 | |
|
872 | 0 | if (done) { |
873 | 0 | break; |
874 | 0 | } |
875 | 0 | } else if (stream->eof) { |
876 | 0 | break; |
877 | 0 | } else { |
878 | | /* XXX: Should be fine to always read chunk_size */ |
879 | 0 | size_t toread; |
880 | |
|
881 | 0 | if (grow_mode) { |
882 | 0 | toread = stream->chunk_size; |
883 | 0 | } else { |
884 | 0 | toread = maxlen - 1; |
885 | 0 | if (toread > stream->chunk_size) { |
886 | 0 | toread = stream->chunk_size; |
887 | 0 | } |
888 | 0 | } |
889 | |
|
890 | 0 | if (php_stream_fill_read_buffer(stream, toread) == FAILURE && stream->fatal_error) { |
891 | 0 | if (grow_mode) { |
892 | 0 | efree(bufstart); |
893 | 0 | } |
894 | 0 | return NULL; |
895 | 0 | } |
896 | | |
897 | 0 | if (stream->writepos - stream->readpos == 0) { |
898 | 0 | break; |
899 | 0 | } |
900 | 0 | } |
901 | 0 | } |
902 | | |
903 | 0 | if (total_copied == 0) { |
904 | 0 | if (grow_mode) { |
905 | 0 | assert(bufstart == NULL); |
906 | 0 | } |
907 | 0 | return NULL; |
908 | 0 | } |
909 | | |
910 | 0 | buf[0] = '\0'; |
911 | 0 | if (returned_len) { |
912 | 0 | *returned_len = total_copied; |
913 | 0 | } |
914 | |
|
915 | 0 | return bufstart; |
916 | 0 | } |
917 | | |
918 | | #define STREAM_BUFFERED_AMOUNT(stream) \ |
919 | 0 | ((size_t)(((stream)->writepos) - (stream)->readpos)) |
920 | | |
921 | | static const char *_php_stream_search_delim( |
922 | | const php_stream *stream, |
923 | | size_t maxlen, |
924 | | size_t skiplen, |
925 | | const char *delim, /* non-empty! */ |
926 | | size_t delim_len |
927 | 0 | ) { |
928 | 0 | size_t seek_len; |
929 | | |
930 | | /* set the maximum number of bytes we're allowed to read from buffer */ |
931 | 0 | seek_len = MIN(STREAM_BUFFERED_AMOUNT(stream), maxlen); |
932 | 0 | if (seek_len <= skiplen) { |
933 | 0 | return NULL; |
934 | 0 | } |
935 | | |
936 | 0 | if (delim_len == 1) { |
937 | 0 | return memchr(&stream->readbuf[stream->readpos + skiplen], |
938 | 0 | delim[0], seek_len - skiplen); |
939 | 0 | } else { |
940 | 0 | return php_memnstr((char*)&stream->readbuf[stream->readpos + skiplen], |
941 | 0 | delim, delim_len, |
942 | 0 | (char*)&stream->readbuf[stream->readpos + seek_len]); |
943 | 0 | } |
944 | 0 | } |
945 | | |
946 | | PHPAPI zend_string *php_stream_get_record(php_stream *stream, size_t maxlen, const char *delim, size_t delim_len) |
947 | 0 | { |
948 | 0 | zend_string *ret_buf; /* returned buffer */ |
949 | 0 | const char *found_delim = NULL; |
950 | 0 | size_t buffered_len, |
951 | 0 | tent_ret_len; /* tentative returned length */ |
952 | 0 | bool has_delim = delim_len > 0; |
953 | |
|
954 | 0 | if (maxlen == 0) { |
955 | 0 | return NULL; |
956 | 0 | } |
957 | | |
958 | 0 | if (has_delim) { |
959 | 0 | found_delim = _php_stream_search_delim( |
960 | 0 | stream, maxlen, 0, delim, delim_len); |
961 | 0 | } |
962 | |
|
963 | 0 | buffered_len = STREAM_BUFFERED_AMOUNT(stream); |
964 | | /* try to read up to maxlen length bytes while we don't find the delim */ |
965 | 0 | while (!found_delim && buffered_len < maxlen) { |
966 | 0 | size_t just_read, |
967 | 0 | to_read_now; |
968 | |
|
969 | 0 | to_read_now = MIN(maxlen - buffered_len, stream->chunk_size); |
970 | |
|
971 | 0 | if (php_stream_fill_read_buffer(stream, buffered_len + to_read_now) == FAILURE && stream->fatal_error) { |
972 | 0 | return NULL; |
973 | 0 | } |
974 | | |
975 | 0 | just_read = STREAM_BUFFERED_AMOUNT(stream) - buffered_len; |
976 | | |
977 | | /* Assume the stream is temporarily or permanently out of data */ |
978 | 0 | if (just_read == 0) { |
979 | 0 | break; |
980 | 0 | } |
981 | | |
982 | 0 | if (has_delim) { |
983 | | /* search for delimiter, but skip buffered_len (the number of bytes |
984 | | * buffered before this loop iteration), as they have already been |
985 | | * searched for the delimiter. |
986 | | * The left part of the delimiter may still remain in the buffer, |
987 | | * so subtract up to <delim_len - 1> from buffered_len, which is |
988 | | * the amount of data we skip on this search as an optimization |
989 | | */ |
990 | 0 | found_delim = _php_stream_search_delim( |
991 | 0 | stream, maxlen, |
992 | 0 | buffered_len >= (delim_len - 1) |
993 | 0 | ? buffered_len - (delim_len - 1) |
994 | 0 | : 0, |
995 | 0 | delim, delim_len); |
996 | 0 | if (found_delim) { |
997 | 0 | break; |
998 | 0 | } |
999 | 0 | } |
1000 | 0 | buffered_len += just_read; |
1001 | 0 | } |
1002 | | |
1003 | 0 | if (has_delim && found_delim) { |
1004 | 0 | tent_ret_len = found_delim - (char*)&stream->readbuf[stream->readpos]; |
1005 | 0 | } else if (!has_delim && STREAM_BUFFERED_AMOUNT(stream) >= maxlen) { |
1006 | 0 | tent_ret_len = maxlen; |
1007 | 0 | } else { |
1008 | | /* return with error if the delimiter string (if any) was not found, we |
1009 | | * could not completely fill the read buffer with maxlen bytes and we |
1010 | | * don't know we've reached end of file. Added with non-blocking streams |
1011 | | * in mind, where this situation is frequent */ |
1012 | 0 | if (STREAM_BUFFERED_AMOUNT(stream) < maxlen && !stream->eof) { |
1013 | 0 | return NULL; |
1014 | 0 | } else if (STREAM_BUFFERED_AMOUNT(stream) == 0 && stream->eof) { |
1015 | | /* refuse to return an empty string just because by accident |
1016 | | * we knew of EOF in a read that returned no data */ |
1017 | 0 | return NULL; |
1018 | 0 | } else { |
1019 | 0 | tent_ret_len = MIN(STREAM_BUFFERED_AMOUNT(stream), maxlen); |
1020 | 0 | } |
1021 | 0 | } |
1022 | | |
1023 | 0 | ret_buf = zend_string_alloc(tent_ret_len, 0); |
1024 | | /* php_stream_read will not call ops->read here because the necessary |
1025 | | * data is guaranteed to be buffered */ |
1026 | 0 | ZSTR_LEN(ret_buf) = php_stream_read(stream, ZSTR_VAL(ret_buf), tent_ret_len); |
1027 | |
|
1028 | 0 | if (found_delim) { |
1029 | 0 | stream->readpos += delim_len; |
1030 | 0 | stream->position += delim_len; |
1031 | 0 | } |
1032 | 0 | ZSTR_VAL(ret_buf)[ZSTR_LEN(ret_buf)] = '\0'; |
1033 | 0 | return ret_buf; |
1034 | 0 | } |
1035 | | |
1036 | | /* Writes a buffer directly to a stream, using multiple of the chunk size */ |
1037 | | static ssize_t _php_stream_write_buffer(php_stream *stream, const char *buf, size_t count) |
1038 | 1.99k | { |
1039 | 1.99k | ssize_t didwrite = 0; |
1040 | 1.99k | ssize_t retval; |
1041 | | |
1042 | | /* if we have a seekable stream we need to ensure that data is written at the |
1043 | | * current stream->position. This means invalidating the read buffer and then |
1044 | | * performing a low-level seek */ |
1045 | 1.99k | if (stream->ops->seek && (stream->flags & PHP_STREAM_FLAG_NO_SEEK) == 0 && stream->readpos != stream->writepos) { |
1046 | 0 | stream->readpos = stream->writepos = 0; |
1047 | |
|
1048 | 0 | stream->ops->seek(stream, stream->position, SEEK_SET, &stream->position); |
1049 | 0 | } |
1050 | | |
1051 | 1.99k | bool old_eof = stream->eof; |
1052 | | |
1053 | | /* See GH-13071: userspace stream is subject to the memory limit. */ |
1054 | 1.99k | size_t chunk_size = count; |
1055 | 1.99k | if (php_stream_is(stream, PHP_STREAM_IS_USERSPACE)) { |
1056 | | /* If the stream is unbuffered, we can only write one byte at a time. */ |
1057 | 0 | chunk_size = stream->chunk_size; |
1058 | 0 | } |
1059 | | |
1060 | 3.98k | while (count > 0) { |
1061 | 1.99k | ssize_t justwrote = stream->ops->write(stream, buf, MIN(chunk_size, count)); |
1062 | 1.99k | if (justwrote <= 0) { |
1063 | | /* If we already successfully wrote some bytes and a write error occurred |
1064 | | * later, report the successfully written bytes. */ |
1065 | 0 | if (didwrite == 0) { |
1066 | 0 | retval = justwrote; |
1067 | 0 | goto out; |
1068 | 0 | } |
1069 | 0 | retval = didwrite; |
1070 | 0 | goto out; |
1071 | 0 | } |
1072 | | |
1073 | 1.99k | buf += justwrote; |
1074 | 1.99k | count -= justwrote; |
1075 | 1.99k | didwrite += justwrote; |
1076 | 1.99k | stream->position += justwrote; |
1077 | 1.99k | } |
1078 | | |
1079 | 1.99k | retval = didwrite; |
1080 | | |
1081 | 1.99k | out: |
1082 | 1.99k | if (old_eof != stream->eof) { |
1083 | 0 | php_stream_notify_completed(PHP_STREAM_CONTEXT(stream)); |
1084 | 0 | } |
1085 | 1.99k | return retval; |
1086 | 1.99k | } |
1087 | | |
1088 | | /* push some data through the write filter chain. |
1089 | | * buf may be NULL, if flags are set to indicate a flush. |
1090 | | * This may trigger a real write to the stream. |
1091 | | * Returns the number of bytes consumed from buf by the first filter in the chain. |
1092 | | * */ |
1093 | | static ssize_t _php_stream_write_filtered(php_stream *stream, const char *buf, size_t count, int flags) |
1094 | 0 | { |
1095 | 0 | size_t consumed = 0; |
1096 | 0 | php_stream_bucket *bucket; |
1097 | 0 | php_stream_bucket_brigade brig_in = { NULL, NULL }, brig_out = { NULL, NULL }; |
1098 | 0 | php_stream_bucket_brigade *brig_inp = &brig_in, *brig_outp = &brig_out; |
1099 | 0 | php_stream_filter_status_t status = PSFS_ERR_FATAL; |
1100 | |
|
1101 | 0 | if (buf) { |
1102 | 0 | bucket = php_stream_bucket_new(stream, (char *)buf, count, 0, 0); |
1103 | 0 | php_stream_bucket_append(&brig_in, bucket); |
1104 | 0 | } |
1105 | |
|
1106 | 0 | for (php_stream_filter *filter = stream->writefilters.head; filter; filter = filter->next) { |
1107 | | /* for our return value, we are interested in the number of bytes consumed from |
1108 | | * the first filter in the chain */ |
1109 | 0 | status = filter->fops->filter(stream, filter, brig_inp, brig_outp, |
1110 | 0 | filter == stream->writefilters.head ? &consumed : NULL, flags); |
1111 | |
|
1112 | 0 | if (status != PSFS_PASS_ON) { |
1113 | 0 | break; |
1114 | 0 | } |
1115 | | /* brig_out becomes brig_in. |
1116 | | * brig_in will always be empty here, as the filter MUST attach any un-consumed buckets |
1117 | | * to its own brigade */ |
1118 | 0 | php_stream_bucket_brigade *brig_swap = brig_inp; |
1119 | 0 | brig_inp = brig_outp; |
1120 | 0 | brig_outp = brig_swap; |
1121 | 0 | memset(brig_outp, 0, sizeof(*brig_outp)); |
1122 | 0 | } |
1123 | |
|
1124 | 0 | switch (status) { |
1125 | 0 | case PSFS_PASS_ON: |
1126 | | /* filter chain generated some output; push it through to the |
1127 | | * underlying stream */ |
1128 | 0 | while (brig_inp->head) { |
1129 | 0 | bucket = brig_inp->head; |
1130 | 0 | if (_php_stream_write_buffer(stream, bucket->buf, bucket->buflen) < 0) { |
1131 | 0 | consumed = (ssize_t) -1; |
1132 | 0 | } |
1133 | | |
1134 | | /* Potential error situation - eg: no space on device. Perhaps we should keep this brigade |
1135 | | * hanging around and try to write it later. |
1136 | | * At the moment, we just drop it on the floor |
1137 | | * */ |
1138 | |
|
1139 | 0 | php_stream_bucket_unlink(bucket); |
1140 | 0 | php_stream_bucket_delref(bucket); |
1141 | 0 | } |
1142 | 0 | break; |
1143 | | |
1144 | 0 | case PSFS_ERR_FATAL: |
1145 | | /* some fatal error. Theoretically, the stream is borked, so all |
1146 | | * further writes should fail. */ |
1147 | 0 | consumed = (ssize_t) -1; |
1148 | 0 | ZEND_FALLTHROUGH; |
1149 | |
|
1150 | 0 | case PSFS_FEED_ME: |
1151 | | /* need more data before we can push data through to the stream */ |
1152 | | /* Filter could have added buckets anyway, but signalled that it did not return any. Discard them. */ |
1153 | 0 | while (brig_inp->head) { |
1154 | 0 | bucket = brig_inp->head; |
1155 | 0 | php_stream_bucket_unlink(bucket); |
1156 | 0 | php_stream_bucket_delref(bucket); |
1157 | 0 | } |
1158 | 0 | break; |
1159 | 0 | } |
1160 | | |
1161 | 0 | return consumed; |
1162 | 0 | } |
1163 | | |
1164 | | PHPAPI int _php_stream_flush(php_stream *stream, int closing) |
1165 | 1.99k | { |
1166 | 1.99k | int ret = 0; |
1167 | | |
1168 | 1.99k | if (stream->writefilters.head && stream->ops->write) { |
1169 | 0 | _php_stream_write_filtered(stream, NULL, 0, closing ? PSFS_FLAG_FLUSH_CLOSE : PSFS_FLAG_FLUSH_INC ); |
1170 | 0 | } |
1171 | | |
1172 | 1.99k | stream->flags &= ~PHP_STREAM_FLAG_WAS_WRITTEN; |
1173 | | |
1174 | 1.99k | if (stream->ops->flush) { |
1175 | 1.99k | ret = stream->ops->flush(stream); |
1176 | 1.99k | } |
1177 | | |
1178 | 1.99k | return ret; |
1179 | 1.99k | } |
1180 | | |
1181 | | PHPAPI ssize_t _php_stream_write(php_stream *stream, const char *buf, size_t count) |
1182 | 1.99k | { |
1183 | 1.99k | ssize_t bytes; |
1184 | | |
1185 | 1.99k | if (count == 0) { |
1186 | 0 | return 0; |
1187 | 0 | } |
1188 | | |
1189 | 1.99k | ZEND_ASSERT(buf != NULL); |
1190 | 1.99k | if (stream->ops->write == NULL) { |
1191 | 0 | php_stream_notice(stream, NotWritable, "Stream is not writable"); |
1192 | 0 | return (ssize_t) -1; |
1193 | 0 | } |
1194 | | |
1195 | 1.99k | if (stream->writefilters.head) { |
1196 | 0 | bytes = _php_stream_write_filtered(stream, buf, count, PSFS_FLAG_NORMAL); |
1197 | 1.99k | } else { |
1198 | 1.99k | bytes = _php_stream_write_buffer(stream, buf, count); |
1199 | 1.99k | } |
1200 | | |
1201 | 1.99k | if (bytes) { |
1202 | 1.99k | stream->flags |= PHP_STREAM_FLAG_WAS_WRITTEN; |
1203 | 1.99k | } |
1204 | | |
1205 | 1.99k | return bytes; |
1206 | 1.99k | } |
1207 | | |
1208 | | PHPAPI ssize_t _php_stream_printf(php_stream *stream, const char *fmt, ...) |
1209 | 0 | { |
1210 | 0 | ssize_t count; |
1211 | 0 | char *buf; |
1212 | 0 | va_list ap; |
1213 | |
|
1214 | 0 | va_start(ap, fmt); |
1215 | 0 | count = vspprintf(&buf, 0, fmt, ap); |
1216 | 0 | va_end(ap); |
1217 | |
|
1218 | 0 | if (!buf) { |
1219 | 0 | return -1; /* error condition */ |
1220 | 0 | } |
1221 | | |
1222 | 0 | count = php_stream_write(stream, buf, count); |
1223 | 0 | efree(buf); |
1224 | |
|
1225 | 0 | return count; |
1226 | 0 | } |
1227 | | |
1228 | | PHPAPI zend_off_t _php_stream_tell(const php_stream *stream) |
1229 | 504k | { |
1230 | 504k | return stream->position; |
1231 | 504k | } |
1232 | | |
1233 | | static bool php_stream_are_filters_seekable(php_stream_filter *filter, bool is_start_seeking, int chain_type) |
1234 | 0 | { |
1235 | 0 | while (filter) { |
1236 | 0 | php_stream_filter_seekable_t seekable = (chain_type == PHP_STREAM_FILTER_READ) ? |
1237 | 0 | filter->read_seekable : filter->write_seekable; |
1238 | 0 | if (seekable == PSFS_SEEKABLE_NEVER) { |
1239 | 0 | php_error_docref(NULL, E_WARNING, "Stream filter %s is never seekable", filter->fops->label); |
1240 | 0 | return false; |
1241 | 0 | } |
1242 | 0 | if (!is_start_seeking && seekable == PSFS_SEEKABLE_START) { |
1243 | 0 | php_error_docref(NULL, E_WARNING, "Stream filter %s is seekable only to start position", filter->fops->label); |
1244 | 0 | return false; |
1245 | 0 | } |
1246 | 0 | filter = filter->next; |
1247 | 0 | } |
1248 | 0 | return true; |
1249 | 0 | } |
1250 | | |
1251 | | static zend_result php_stream_filters_seek(php_stream *stream, php_stream_filter *filter, |
1252 | | bool is_start_seeking, zend_off_t offset, int whence, int chain_type) |
1253 | 226k | { |
1254 | 226k | while (filter) { |
1255 | 0 | php_stream_filter_seekable_t seekable = (chain_type == PHP_STREAM_FILTER_READ) ? |
1256 | 0 | filter->read_seekable : filter->write_seekable; |
1257 | 0 | if (((seekable == PSFS_SEEKABLE_START && is_start_seeking) || |
1258 | 0 | seekable == PSFS_SEEKABLE_CHECK) && |
1259 | 0 | filter->fops->seek(stream, filter, offset, whence) == FAILURE) { |
1260 | 0 | php_error_docref(NULL, E_WARNING, "Stream filter seeking for %s failed", filter->fops->label); |
1261 | 0 | return FAILURE; |
1262 | 0 | } |
1263 | 0 | filter = filter->next; |
1264 | 0 | } |
1265 | 226k | return SUCCESS; |
1266 | 226k | } |
1267 | | |
1268 | | static zend_result php_stream_filters_seek_all(php_stream *stream, bool is_start_seeking, |
1269 | | zend_off_t offset, int whence) |
1270 | 113k | { |
1271 | 113k | if (php_stream_filters_seek(stream, stream->writefilters.head, is_start_seeking, |
1272 | 113k | offset, whence, PHP_STREAM_FILTER_WRITE) == FAILURE) { |
1273 | 0 | return FAILURE; |
1274 | 0 | } |
1275 | 113k | if (php_stream_filters_seek(stream, stream->readfilters.head, is_start_seeking, |
1276 | 113k | offset, whence, PHP_STREAM_FILTER_READ) == FAILURE) { |
1277 | 0 | return FAILURE; |
1278 | 0 | } |
1279 | | |
1280 | 113k | return SUCCESS; |
1281 | 113k | } |
1282 | | |
1283 | | |
1284 | | |
1285 | | PHPAPI int _php_stream_seek(php_stream *stream, zend_off_t offset, int whence) |
1286 | 113k | { |
1287 | 113k | if (stream->fclose_stdiocast == PHP_STREAM_FCLOSE_FOPENCOOKIE) { |
1288 | | /* flush can call seek internally so we need to prevent an infinite loop */ |
1289 | 0 | if (!stream->fclose_stdiocast_flush_in_progress) { |
1290 | 0 | stream->fclose_stdiocast_flush_in_progress = 1; |
1291 | | /* flush to commit data written to the fopencookie FILE* */ |
1292 | 0 | fflush(stream->stdiocast); |
1293 | 0 | stream->fclose_stdiocast_flush_in_progress = 0; |
1294 | 0 | } |
1295 | 0 | } |
1296 | | |
1297 | 113k | bool is_start_seeking = whence == SEEK_SET && offset == 0; |
1298 | | |
1299 | 113k | if (stream->writefilters.head) { |
1300 | 0 | _php_stream_flush(stream, 0); |
1301 | 0 | if (!php_stream_are_filters_seekable(stream->writefilters.head, is_start_seeking, |
1302 | 0 | PHP_STREAM_FILTER_WRITE)) { |
1303 | 0 | return -1; |
1304 | 0 | } |
1305 | 0 | } |
1306 | 113k | if (stream->readfilters.head && !php_stream_are_filters_seekable( |
1307 | 0 | stream->readfilters.head, is_start_seeking, PHP_STREAM_FILTER_READ)) { |
1308 | 0 | return -1; |
1309 | 0 | } |
1310 | | |
1311 | | /* handle the case where we are in the buffer */ |
1312 | 113k | if ((stream->flags & PHP_STREAM_FLAG_NO_BUFFER) == 0) { |
1313 | 0 | switch(whence) { |
1314 | 0 | case SEEK_CUR: |
1315 | 0 | if (offset > 0 && offset <= stream->writepos - stream->readpos) { |
1316 | 0 | stream->readpos += offset; /* if offset = ..., then readpos = writepos */ |
1317 | 0 | stream->position += offset; |
1318 | 0 | stream->eof = 0; |
1319 | 0 | stream->fatal_error = 0; |
1320 | 0 | return php_stream_filters_seek_all(stream, is_start_seeking, offset, whence) == SUCCESS ? 0 : -1; |
1321 | 0 | } |
1322 | 0 | break; |
1323 | 0 | case SEEK_SET: |
1324 | 0 | if (offset > stream->position && |
1325 | 0 | offset <= stream->position + stream->writepos - stream->readpos) { |
1326 | 0 | stream->readpos += offset - stream->position; |
1327 | 0 | stream->position = offset; |
1328 | 0 | stream->eof = 0; |
1329 | 0 | stream->fatal_error = 0; |
1330 | 0 | return php_stream_filters_seek_all(stream, is_start_seeking, offset, whence) == SUCCESS ? 0 : -1; |
1331 | 0 | } |
1332 | 0 | break; |
1333 | 0 | } |
1334 | 0 | } |
1335 | | |
1336 | | |
1337 | 113k | if (stream->ops->seek && (stream->flags & PHP_STREAM_FLAG_NO_SEEK) == 0) { |
1338 | 113k | int ret; |
1339 | 113k | switch(whence) { |
1340 | 0 | case SEEK_CUR: |
1341 | 0 | ZEND_ASSERT(stream->position >= 0); |
1342 | 0 | if (UNEXPECTED(offset > ZEND_LONG_MAX - stream->position)) { |
1343 | 0 | offset = ZEND_LONG_MAX; |
1344 | 0 | } else { |
1345 | 0 | offset = stream->position + offset; |
1346 | 0 | } |
1347 | 0 | whence = SEEK_SET; |
1348 | 0 | break; |
1349 | 111k | case SEEK_SET: |
1350 | 111k | if (offset < 0) { |
1351 | 0 | return -1; |
1352 | 0 | } |
1353 | 113k | } |
1354 | 113k | ret = stream->ops->seek(stream, offset, whence, &stream->position); |
1355 | | |
1356 | 113k | if (((stream->flags & PHP_STREAM_FLAG_NO_SEEK) == 0) || ret == 0) { |
1357 | 113k | if (ret == 0) { |
1358 | 113k | stream->eof = 0; |
1359 | 113k | stream->fatal_error = 0; |
1360 | 113k | } |
1361 | | |
1362 | | /* invalidate the buffer contents */ |
1363 | 113k | stream->readpos = stream->writepos = 0; |
1364 | | |
1365 | 113k | return php_stream_filters_seek_all(stream, is_start_seeking, offset, whence) == SUCCESS ? ret : -1; |
1366 | 113k | } |
1367 | | /* else the stream has decided that it can't support seeking after all; |
1368 | | * fall through to attempt emulation */ |
1369 | 113k | } |
1370 | | |
1371 | | /* emulate forward moving seeks with reads */ |
1372 | 0 | if (whence == SEEK_CUR && offset >= 0) { |
1373 | 0 | char tmp[1024]; |
1374 | 0 | ssize_t didread; |
1375 | 0 | while (offset > 0) { |
1376 | 0 | if ((didread = php_stream_read(stream, tmp, MIN(offset, sizeof(tmp)))) <= 0) { |
1377 | 0 | return -1; |
1378 | 0 | } |
1379 | 0 | offset -= didread; |
1380 | 0 | } |
1381 | 0 | stream->eof = 0; |
1382 | 0 | stream->fatal_error = 0; |
1383 | 0 | return 0; |
1384 | 0 | } |
1385 | | |
1386 | 0 | php_stream_warn(stream, SeekNotSupported, |
1387 | 0 | "Stream does not support seeking"); |
1388 | |
|
1389 | 0 | return -1; |
1390 | 0 | } |
1391 | | |
1392 | | PHPAPI int _php_stream_set_option(php_stream *stream, int option, int value, void *ptrparam) |
1393 | 0 | { |
1394 | 0 | int ret = PHP_STREAM_OPTION_RETURN_NOTIMPL; |
1395 | |
|
1396 | 0 | if (stream->ops->set_option) { |
1397 | 0 | ret = stream->ops->set_option(stream, option, value, ptrparam); |
1398 | 0 | } |
1399 | |
|
1400 | 0 | if (ret == PHP_STREAM_OPTION_RETURN_NOTIMPL) { |
1401 | 0 | switch(option) { |
1402 | 0 | case PHP_STREAM_OPTION_SET_CHUNK_SIZE: |
1403 | | /* XXX chunk size itself is of size_t, that might be ok or not for a particular case*/ |
1404 | 0 | ret = stream->chunk_size > INT_MAX ? INT_MAX : (int)stream->chunk_size; |
1405 | 0 | stream->chunk_size = value; |
1406 | 0 | return ret; |
1407 | | |
1408 | 0 | case PHP_STREAM_OPTION_READ_BUFFER: |
1409 | | /* try to match the buffer mode as best we can */ |
1410 | 0 | if (value == PHP_STREAM_BUFFER_NONE) { |
1411 | 0 | stream->flags |= PHP_STREAM_FLAG_NO_BUFFER; |
1412 | 0 | } else if (stream->flags & PHP_STREAM_FLAG_NO_BUFFER) { |
1413 | 0 | stream->flags ^= PHP_STREAM_FLAG_NO_BUFFER; |
1414 | 0 | } |
1415 | 0 | ret = PHP_STREAM_OPTION_RETURN_OK; |
1416 | 0 | break; |
1417 | | |
1418 | 0 | default: |
1419 | 0 | ; |
1420 | 0 | } |
1421 | 0 | } |
1422 | | |
1423 | 0 | return ret; |
1424 | 0 | } |
1425 | | |
1426 | | PHPAPI int _php_stream_sync(php_stream *stream, bool data_only) |
1427 | 0 | { |
1428 | 0 | int op = PHP_STREAM_SYNC_FSYNC; |
1429 | 0 | if (data_only) { |
1430 | 0 | op = PHP_STREAM_SYNC_FDSYNC; |
1431 | 0 | } |
1432 | 0 | return php_stream_set_option(stream, PHP_STREAM_OPTION_SYNC_API, op, NULL); |
1433 | 0 | } |
1434 | | |
1435 | | PHPAPI int _php_stream_truncate_set_size(php_stream *stream, size_t newsize) |
1436 | 0 | { |
1437 | 0 | return php_stream_set_option(stream, PHP_STREAM_OPTION_TRUNCATE_API, PHP_STREAM_TRUNCATE_SET_SIZE, &newsize); |
1438 | 0 | } |
1439 | | |
1440 | | PHPAPI ssize_t _php_stream_passthru(php_stream * stream STREAMS_DC) |
1441 | 0 | { |
1442 | 0 | size_t bcount = 0; |
1443 | 0 | char buf[8192]; |
1444 | 0 | ssize_t b; |
1445 | |
|
1446 | 0 | if (php_stream_mmap_possible(stream)) { |
1447 | 0 | char *p; |
1448 | 0 | size_t mapped; |
1449 | |
|
1450 | 0 | p = php_stream_mmap_range(stream, php_stream_tell(stream), PHP_STREAM_MMAP_ALL, PHP_STREAM_MAP_MODE_SHARED_READONLY, &mapped); |
1451 | |
|
1452 | 0 | if (p) { |
1453 | 0 | do { |
1454 | | /* output functions return int, so pass in int max */ |
1455 | 0 | if (0 < (b = PHPWRITE(p + bcount, MIN(mapped - bcount, INT_MAX)))) { |
1456 | 0 | bcount += b; |
1457 | 0 | } |
1458 | 0 | } while (b > 0 && mapped > bcount); |
1459 | |
|
1460 | 0 | php_stream_mmap_unmap_ex(stream, mapped); |
1461 | |
|
1462 | 0 | return bcount; |
1463 | 0 | } |
1464 | 0 | } |
1465 | | |
1466 | 0 | while ((b = php_stream_read(stream, buf, sizeof(buf))) > 0) { |
1467 | 0 | PHPWRITE(buf, b); |
1468 | 0 | bcount += b; |
1469 | 0 | } |
1470 | |
|
1471 | 0 | if (b < 0 && bcount == 0) { |
1472 | 0 | return b; |
1473 | 0 | } |
1474 | | |
1475 | 0 | return bcount; |
1476 | 0 | } |
1477 | | |
1478 | | |
1479 | | PHPAPI zend_string *_php_stream_copy_to_mem(php_stream *src, size_t maxlen, bool persistent STREAMS_DC) |
1480 | 0 | { |
1481 | 0 | ssize_t ret = 0; |
1482 | 0 | char *ptr; |
1483 | 0 | size_t len = 0, buflen; |
1484 | 0 | php_stream_statbuf ssbuf; |
1485 | 0 | zend_string *result; |
1486 | |
|
1487 | 0 | if (maxlen == 0) { |
1488 | 0 | return ZSTR_EMPTY_ALLOC(); |
1489 | 0 | } |
1490 | | |
1491 | 0 | if (maxlen == PHP_STREAM_COPY_ALL) { |
1492 | 0 | maxlen = 0; |
1493 | 0 | } |
1494 | |
|
1495 | 0 | if (maxlen > 0 && maxlen < 4 * CHUNK_SIZE) { |
1496 | 0 | result = zend_string_alloc(maxlen, persistent); |
1497 | 0 | ptr = ZSTR_VAL(result); |
1498 | 0 | while ((len < maxlen) && !php_stream_eof(src)) { |
1499 | 0 | ret = php_stream_read(src, ptr, maxlen - len); |
1500 | 0 | if (ret <= 0) { |
1501 | | // TODO: Propagate error? |
1502 | 0 | break; |
1503 | 0 | } |
1504 | 0 | len += ret; |
1505 | 0 | ptr += ret; |
1506 | 0 | } |
1507 | 0 | if (len) { |
1508 | 0 | ZSTR_LEN(result) = len; |
1509 | 0 | ZSTR_VAL(result)[len] = '\0'; |
1510 | | |
1511 | | /* Only truncate if the savings are large enough */ |
1512 | 0 | if (len < maxlen / 2) { |
1513 | 0 | result = zend_string_truncate(result, len, persistent); |
1514 | 0 | } |
1515 | 0 | } else { |
1516 | 0 | zend_string_free(result); |
1517 | 0 | result = NULL; |
1518 | 0 | } |
1519 | 0 | return result; |
1520 | 0 | } |
1521 | | |
1522 | | /* avoid many reallocs by allocating a good-sized chunk to begin with, if |
1523 | | * we can. Note that the stream may be filtered, in which case the stat |
1524 | | * result may be inaccurate, as the filter may inflate or deflate the |
1525 | | * number of bytes that we can read. In order to avoid an upsize followed |
1526 | | * by a downsize of the buffer, overestimate by the CHUNK_SIZE size (which is |
1527 | | * 8K). */ |
1528 | 0 | if (php_stream_stat(src, &ssbuf) == 0 && ssbuf.sb.st_size > 0) { |
1529 | 0 | buflen = MAX(ssbuf.sb.st_size - src->position, 0) + CHUNK_SIZE; |
1530 | 0 | if (maxlen > 0 && buflen > maxlen) { |
1531 | 0 | buflen = maxlen; |
1532 | 0 | } |
1533 | 0 | } else { |
1534 | 0 | buflen = CHUNK_SIZE; |
1535 | 0 | } |
1536 | |
|
1537 | 0 | result = zend_string_alloc(buflen, persistent); |
1538 | 0 | ptr = ZSTR_VAL(result); |
1539 | |
|
1540 | 0 | const int min_room = CHUNK_SIZE / 4; |
1541 | | // TODO: Propagate error? |
1542 | 0 | while ((ret = php_stream_read(src, ptr, buflen - len)) > 0) { |
1543 | 0 | len += ret; |
1544 | 0 | if (len + min_room >= buflen) { |
1545 | 0 | if (maxlen == len) { |
1546 | 0 | break; |
1547 | 0 | } |
1548 | 0 | if (maxlen > 0 && buflen + CHUNK_SIZE > maxlen) { |
1549 | 0 | buflen = maxlen; |
1550 | 0 | } else { |
1551 | 0 | buflen += CHUNK_SIZE; |
1552 | 0 | } |
1553 | 0 | result = zend_string_extend(result, buflen, persistent); |
1554 | 0 | ptr = ZSTR_VAL(result) + len; |
1555 | 0 | } else { |
1556 | 0 | ptr += ret; |
1557 | 0 | } |
1558 | 0 | } |
1559 | 0 | if (len) { |
1560 | 0 | result = zend_string_truncate(result, len, persistent); |
1561 | 0 | ZSTR_VAL(result)[len] = '\0'; |
1562 | 0 | } else { |
1563 | 0 | zend_string_free(result); |
1564 | 0 | result = NULL; |
1565 | 0 | } |
1566 | |
|
1567 | 0 | return result; |
1568 | 0 | } |
1569 | | |
1570 | | /* Returns SUCCESS/FAILURE and sets *len to the number of bytes moved */ |
1571 | | PHPAPI zend_result _php_stream_copy_to_stream_ex(php_stream *src, php_stream *dest, size_t maxlen, size_t *len STREAMS_DC) |
1572 | 0 | { |
1573 | 0 | char buf[CHUNK_SIZE]; |
1574 | 0 | size_t haveread = 0; |
1575 | 0 | size_t towrite; |
1576 | 0 | size_t dummy; |
1577 | |
|
1578 | 0 | if (!len) { |
1579 | 0 | len = &dummy; |
1580 | 0 | } |
1581 | |
|
1582 | 0 | if (maxlen == 0) { |
1583 | 0 | *len = 0; |
1584 | 0 | return SUCCESS; |
1585 | 0 | } |
1586 | | |
1587 | 0 | #ifdef HAVE_COPY_FILE_RANGE |
1588 | 0 | if (php_stream_is(src, PHP_STREAM_IS_STDIO) && |
1589 | 0 | php_stream_is(dest, PHP_STREAM_IS_STDIO) && |
1590 | 0 | src->writepos == src->readpos) { |
1591 | | /* both php_stream instances are backed by a file descriptor, are not filtered and the |
1592 | | * read buffer is empty: we can use copy_file_range() */ |
1593 | 0 | int src_fd, dest_fd, dest_open_flags = 0; |
1594 | | |
1595 | | /* copy_file_range does not work with O_APPEND */ |
1596 | 0 | if (php_stream_cast(src, PHP_STREAM_AS_FD, (void*)&src_fd, 0) == SUCCESS && |
1597 | 0 | php_stream_cast(dest, PHP_STREAM_AS_FD, (void*)&dest_fd, 0) == SUCCESS && |
1598 | | /* get dest open flags to check if the stream is open in append mode */ |
1599 | 0 | php_stream_parse_fopen_modes(dest->mode, &dest_open_flags) == SUCCESS && |
1600 | 0 | !(dest_open_flags & O_APPEND)) { |
1601 | | |
1602 | | /* clamp to INT_MAX to avoid EOVERFLOW */ |
1603 | 0 | const size_t cfr_max = MIN(maxlen, (size_t)SSIZE_MAX); |
1604 | | |
1605 | | /* copy_file_range() is a Linux-specific system call which allows efficient copying |
1606 | | * between two file descriptors, eliminating the need to transfer data from the kernel |
1607 | | * to userspace and back. For networking file systems like NFS and Ceph, it even |
1608 | | * eliminates copying data to the client, and local filesystems like Btrfs and XFS can |
1609 | | * create shared extents. */ |
1610 | 0 | ssize_t result = copy_file_range(src_fd, NULL, dest_fd, NULL, cfr_max, 0); |
1611 | 0 | if (result > 0) { |
1612 | 0 | size_t nbytes = (size_t)result; |
1613 | 0 | haveread += nbytes; |
1614 | |
|
1615 | 0 | src->position += nbytes; |
1616 | 0 | dest->position += nbytes; |
1617 | |
|
1618 | 0 | if ((maxlen != PHP_STREAM_COPY_ALL && nbytes == maxlen) || php_stream_eof(src)) { |
1619 | | /* the whole request was satisfied or end-of-file reached - done */ |
1620 | 0 | *len = haveread; |
1621 | 0 | return SUCCESS; |
1622 | 0 | } |
1623 | | |
1624 | | /* there may be more data; continue copying using the fallback code below */ |
1625 | 0 | } else if (result == 0) { |
1626 | | /* end of file */ |
1627 | 0 | *len = haveread; |
1628 | 0 | return SUCCESS; |
1629 | 0 | } else if (result < 0) { |
1630 | 0 | switch (errno) { |
1631 | 0 | case EINVAL: |
1632 | | /* some formal error, e.g. overlapping file ranges */ |
1633 | 0 | break; |
1634 | | |
1635 | 0 | case EXDEV: |
1636 | | /* pre Linux 5.3 error */ |
1637 | 0 | break; |
1638 | | |
1639 | 0 | case ENOSYS: |
1640 | | /* not implemented by this Linux kernel */ |
1641 | 0 | break; |
1642 | | |
1643 | 0 | case EIO: |
1644 | | /* Some filesystems will cause failures if the max length is greater than the file length |
1645 | | * in certain circumstances and configuration. In those cases the errno is EIO and we will |
1646 | | * fall back to other methods. We cannot use stat to determine the file length upfront because |
1647 | | * that is prone to races and outdated caching. */ |
1648 | 0 | break; |
1649 | | |
1650 | 0 | default: |
1651 | | /* unexpected I/O error - give up, no fallback */ |
1652 | 0 | *len = haveread; |
1653 | 0 | return FAILURE; |
1654 | 0 | } |
1655 | | |
1656 | | /* fall back to classic copying */ |
1657 | 0 | } |
1658 | 0 | } |
1659 | 0 | } |
1660 | 0 | #endif // HAVE_COPY_FILE_RANGE |
1661 | | |
1662 | 0 | if (maxlen == PHP_STREAM_COPY_ALL) { |
1663 | 0 | maxlen = 0; |
1664 | 0 | } |
1665 | |
|
1666 | 0 | if (php_stream_mmap_possible(src)) { |
1667 | 0 | char *p; |
1668 | |
|
1669 | 0 | do { |
1670 | | /* We must not modify maxlen here, because otherwise the file copy fallback below can fail */ |
1671 | 0 | size_t chunk_size, must_read, mapped; |
1672 | 0 | if (maxlen == 0) { |
1673 | | /* Unlimited read */ |
1674 | 0 | must_read = chunk_size = PHP_STREAM_MMAP_MAX; |
1675 | 0 | } else { |
1676 | 0 | must_read = maxlen - haveread; |
1677 | 0 | if (must_read >= PHP_STREAM_MMAP_MAX) { |
1678 | 0 | chunk_size = PHP_STREAM_MMAP_MAX; |
1679 | 0 | } else { |
1680 | | /* In case the length we still have to read from the file could be smaller than the file size, |
1681 | | * chunk_size must not get bigger the size we're trying to read. */ |
1682 | 0 | chunk_size = must_read; |
1683 | 0 | } |
1684 | 0 | } |
1685 | |
|
1686 | 0 | p = php_stream_mmap_range(src, php_stream_tell(src), chunk_size, PHP_STREAM_MAP_MODE_SHARED_READONLY, &mapped); |
1687 | |
|
1688 | 0 | if (p) { |
1689 | 0 | ssize_t didwrite; |
1690 | |
|
1691 | 0 | if (php_stream_seek(src, mapped, SEEK_CUR) != 0) { |
1692 | 0 | php_stream_mmap_unmap(src); |
1693 | 0 | break; |
1694 | 0 | } |
1695 | | |
1696 | 0 | didwrite = php_stream_write(dest, p, mapped); |
1697 | 0 | if (didwrite < 0) { |
1698 | 0 | *len = haveread; |
1699 | 0 | php_stream_mmap_unmap(src); |
1700 | 0 | return FAILURE; |
1701 | 0 | } |
1702 | | |
1703 | 0 | php_stream_mmap_unmap(src); |
1704 | |
|
1705 | 0 | *len = haveread += didwrite; |
1706 | | |
1707 | | /* we've got at least 1 byte to read |
1708 | | * less than 1 is an error |
1709 | | * AND read bytes match written */ |
1710 | 0 | if (mapped == 0 || mapped != didwrite) { |
1711 | 0 | return FAILURE; |
1712 | 0 | } |
1713 | 0 | if (mapped < chunk_size) { |
1714 | 0 | return SUCCESS; |
1715 | 0 | } |
1716 | | /* If we're not reading as much as possible, so a bounded read */ |
1717 | 0 | if (maxlen != 0) { |
1718 | 0 | must_read -= mapped; |
1719 | 0 | if (must_read == 0) { |
1720 | 0 | return SUCCESS; |
1721 | 0 | } |
1722 | 0 | } |
1723 | 0 | } |
1724 | 0 | } while (p); |
1725 | 0 | } |
1726 | | |
1727 | 0 | while(1) { |
1728 | 0 | size_t readchunk = sizeof(buf); |
1729 | 0 | ssize_t didread; |
1730 | 0 | char *writeptr; |
1731 | |
|
1732 | 0 | if (maxlen && (maxlen - haveread) < readchunk) { |
1733 | 0 | readchunk = maxlen - haveread; |
1734 | 0 | } |
1735 | |
|
1736 | 0 | didread = php_stream_read(src, buf, readchunk); |
1737 | 0 | if (didread <= 0) { |
1738 | 0 | *len = haveread; |
1739 | 0 | return didread < 0 ? FAILURE : SUCCESS; |
1740 | 0 | } |
1741 | | |
1742 | 0 | towrite = didread; |
1743 | 0 | writeptr = buf; |
1744 | 0 | haveread += didread; |
1745 | |
|
1746 | 0 | while (towrite) { |
1747 | 0 | ssize_t didwrite = php_stream_write(dest, writeptr, towrite); |
1748 | 0 | if (didwrite <= 0) { |
1749 | 0 | *len = haveread - (didread - towrite); |
1750 | 0 | return FAILURE; |
1751 | 0 | } |
1752 | | |
1753 | 0 | towrite -= didwrite; |
1754 | 0 | writeptr += didwrite; |
1755 | 0 | } |
1756 | | |
1757 | 0 | if (maxlen && maxlen == haveread) { |
1758 | 0 | break; |
1759 | 0 | } |
1760 | 0 | } |
1761 | | |
1762 | 0 | *len = haveread; |
1763 | 0 | return SUCCESS; |
1764 | 0 | } |
1765 | | |
1766 | | /* Returns the number of bytes moved. |
1767 | | * Returns 1 when source len is 0. |
1768 | | * Deprecated in favor of php_stream_copy_to_stream_ex() */ |
1769 | | ZEND_ATTRIBUTE_DEPRECATED |
1770 | | PHPAPI size_t _php_stream_copy_to_stream(php_stream *src, php_stream *dest, size_t maxlen STREAMS_DC) |
1771 | 0 | { |
1772 | 0 | size_t len; |
1773 | 0 | zend_result ret = _php_stream_copy_to_stream_ex(src, dest, maxlen, &len STREAMS_REL_CC); |
1774 | 0 | if (ret == SUCCESS && len == 0 && maxlen != 0) { |
1775 | 0 | return 1; |
1776 | 0 | } |
1777 | 0 | return len; |
1778 | 0 | } |
1779 | | /* }}} */ |
1780 | | |
1781 | | /* {{{ wrapper init and registration */ |
1782 | | |
1783 | | static void stream_resource_regular_dtor(zend_resource *rsrc) |
1784 | 1.99k | { |
1785 | 1.99k | php_stream *stream = (php_stream*)rsrc->ptr; |
1786 | | /* set the return value for pclose */ |
1787 | 1.99k | FG(pclose_ret) = php_stream_free(stream, PHP_STREAM_FREE_CLOSE | PHP_STREAM_FREE_RSRC_DTOR); |
1788 | 1.99k | } |
1789 | | |
1790 | | static void stream_resource_persistent_dtor(zend_resource *rsrc) |
1791 | 0 | { |
1792 | 0 | php_stream *stream = (php_stream*)rsrc->ptr; |
1793 | 0 | FG(pclose_ret) = php_stream_free(stream, PHP_STREAM_FREE_CLOSE | PHP_STREAM_FREE_RSRC_DTOR); |
1794 | 0 | } |
1795 | | |
1796 | | void php_shutdown_stream_hashes(void) |
1797 | 1.99k | { |
1798 | 1.99k | FG(user_stream_current_filename) = NULL; |
1799 | 1.99k | if (FG(stream_wrappers)) { |
1800 | 0 | zend_hash_destroy(FG(stream_wrappers)); |
1801 | 0 | efree(FG(stream_wrappers)); |
1802 | 0 | FG(stream_wrappers) = NULL; |
1803 | 0 | } |
1804 | | |
1805 | 1.99k | if (FG(stream_filters)) { |
1806 | 0 | zend_hash_destroy(FG(stream_filters)); |
1807 | 0 | efree(FG(stream_filters)); |
1808 | 0 | FG(stream_filters) = NULL; |
1809 | 0 | } |
1810 | | |
1811 | 1.99k | if (FG(wrapper_logged_errors)) { |
1812 | 0 | zend_hash_destroy(FG(wrapper_logged_errors)); |
1813 | 0 | efree(FG(wrapper_logged_errors)); |
1814 | 0 | FG(wrapper_logged_errors) = NULL; |
1815 | 0 | } |
1816 | | |
1817 | 1.99k | php_stream_error_state_cleanup(); |
1818 | 1.99k | } |
1819 | | |
1820 | | zend_result php_init_stream_wrappers(int module_number) |
1821 | 2 | { |
1822 | 2 | le_stream = zend_register_list_destructors_ex(stream_resource_regular_dtor, NULL, "stream", module_number); |
1823 | 2 | le_pstream = zend_register_list_destructors_ex(NULL, stream_resource_persistent_dtor, "persistent stream", module_number); |
1824 | | |
1825 | | /* Filters are cleaned up by the streams they're attached to */ |
1826 | 2 | le_stream_filter = zend_register_list_destructors_ex(NULL, NULL, "stream filter", module_number); |
1827 | | |
1828 | 2 | zend_hash_init(&url_stream_wrappers_hash, 8, NULL, NULL, 1); |
1829 | 2 | zend_hash_init(php_get_stream_filters_hash_global(), 8, NULL, NULL, 1); |
1830 | 2 | zend_hash_init(php_stream_xport_get_hash(), 8, NULL, NULL, 1); |
1831 | | |
1832 | 2 | return (php_stream_xport_register("tcp", php_stream_generic_socket_factory) == SUCCESS |
1833 | 2 | && |
1834 | 2 | php_stream_xport_register("udp", php_stream_generic_socket_factory) == SUCCESS |
1835 | 2 | #if defined(AF_UNIX) && !(defined(PHP_WIN32) || defined(__riscos__)) |
1836 | 2 | && |
1837 | 2 | php_stream_xport_register("unix", php_stream_generic_socket_factory) == SUCCESS |
1838 | 2 | && |
1839 | 2 | php_stream_xport_register("udg", php_stream_generic_socket_factory) == SUCCESS |
1840 | 2 | #endif |
1841 | 2 | ) ? SUCCESS : FAILURE; |
1842 | 2 | } |
1843 | | |
1844 | | void php_shutdown_stream_wrappers(int module_number) |
1845 | 0 | { |
1846 | 0 | zend_hash_destroy(&url_stream_wrappers_hash); |
1847 | 0 | zend_hash_destroy(php_get_stream_filters_hash_global()); |
1848 | 0 | zend_hash_destroy(php_stream_xport_get_hash()); |
1849 | 0 | } |
1850 | | |
1851 | | /* Validate protocol scheme names during registration |
1852 | | * Must conform to /^[a-zA-Z0-9+.-]+$/ |
1853 | | */ |
1854 | | static inline zend_result php_stream_wrapper_scheme_validate(const char *protocol, size_t protocol_len) |
1855 | 12 | { |
1856 | 56 | for (size_t i = 0; i < protocol_len; i++) { |
1857 | 44 | if (!isalnum((unsigned char)protocol[i]) && |
1858 | 0 | protocol[i] != '+' && |
1859 | 0 | protocol[i] != '-' && |
1860 | 0 | protocol[i] != '.') { |
1861 | 0 | return FAILURE; |
1862 | 0 | } |
1863 | 44 | } |
1864 | | |
1865 | 12 | return SUCCESS; |
1866 | 12 | } |
1867 | | |
1868 | | /* API for registering GLOBAL wrappers */ |
1869 | | PHPAPI zend_result php_register_url_stream_wrapper(const char *protocol, const php_stream_wrapper *wrapper) |
1870 | 12 | { |
1871 | 12 | size_t protocol_len = strlen(protocol); |
1872 | 12 | zend_result ret; |
1873 | 12 | zend_string *str; |
1874 | | |
1875 | 12 | if (php_stream_wrapper_scheme_validate(protocol, protocol_len) == FAILURE) { |
1876 | 0 | return FAILURE; |
1877 | 0 | } |
1878 | | |
1879 | 12 | str = zend_string_init_interned(protocol, protocol_len, 1); |
1880 | 12 | ret = zend_hash_add_ptr(&url_stream_wrappers_hash, str, (void*)wrapper) ? SUCCESS : FAILURE; |
1881 | 12 | zend_string_release_ex(str, 1); |
1882 | 12 | return ret; |
1883 | 12 | } |
1884 | | |
1885 | | PHPAPI zend_result php_unregister_url_stream_wrapper(const char *protocol) |
1886 | 0 | { |
1887 | 0 | return zend_hash_str_del(&url_stream_wrappers_hash, protocol, strlen(protocol)); |
1888 | 0 | } |
1889 | | |
1890 | | static void clone_wrapper_hash(void) |
1891 | 0 | { |
1892 | 0 | ALLOC_HASHTABLE(FG(stream_wrappers)); |
1893 | 0 | zend_hash_init(FG(stream_wrappers), zend_hash_num_elements(&url_stream_wrappers_hash), NULL, NULL, 0); |
1894 | 0 | zend_hash_copy(FG(stream_wrappers), &url_stream_wrappers_hash, NULL); |
1895 | 0 | } |
1896 | | |
1897 | | /* API for registering VOLATILE wrappers */ |
1898 | | PHPAPI zend_result php_register_url_stream_wrapper_volatile(zend_string *protocol, php_stream_wrapper *wrapper) |
1899 | 0 | { |
1900 | 0 | if (php_stream_wrapper_scheme_validate(ZSTR_VAL(protocol), ZSTR_LEN(protocol)) == FAILURE) { |
1901 | 0 | return FAILURE; |
1902 | 0 | } |
1903 | | |
1904 | 0 | if (!FG(stream_wrappers)) { |
1905 | 0 | clone_wrapper_hash(); |
1906 | 0 | } |
1907 | |
|
1908 | 0 | return zend_hash_add_ptr(FG(stream_wrappers), protocol, wrapper) ? SUCCESS : FAILURE; |
1909 | 0 | } |
1910 | | |
1911 | | PHPAPI zend_result php_unregister_url_stream_wrapper_volatile(zend_string *protocol) |
1912 | 0 | { |
1913 | 0 | if (!FG(stream_wrappers)) { |
1914 | 0 | clone_wrapper_hash(); |
1915 | 0 | } |
1916 | |
|
1917 | 0 | return zend_hash_del(FG(stream_wrappers), protocol); |
1918 | 0 | } |
1919 | | /* }}} */ |
1920 | | |
1921 | | /* {{{ php_stream_locate_url_wrapper */ |
1922 | | PHPAPI php_stream_wrapper *php_stream_locate_url_wrapper(const char *path, const char **path_for_open, int options) |
1923 | 0 | { |
1924 | 0 | const HashTable *wrapper_hash = (FG(stream_wrappers) ? FG(stream_wrappers) : &url_stream_wrappers_hash); |
1925 | 0 | php_stream_wrapper *wrapper = NULL; |
1926 | 0 | const char *p, *protocol = NULL; |
1927 | 0 | size_t n = 0; |
1928 | |
|
1929 | 0 | if (path_for_open) { |
1930 | 0 | *path_for_open = (char*)path; |
1931 | 0 | } |
1932 | |
|
1933 | 0 | if (options & IGNORE_URL) { |
1934 | 0 | return (php_stream_wrapper*)((options & STREAM_LOCATE_WRAPPERS_ONLY) ? NULL : &php_plain_files_wrapper); |
1935 | 0 | } |
1936 | | |
1937 | 0 | for (p = path; isalnum((unsigned char)*p) || *p == '+' || *p == '-' || *p == '.'; p++) { |
1938 | 0 | n++; |
1939 | 0 | } |
1940 | |
|
1941 | 0 | if ((*p == ':') && (n > 1) && (!strncmp("//", p+1, 2) || (n == 4 && !memcmp("data:", path, 5)))) { |
1942 | 0 | protocol = path; |
1943 | 0 | } |
1944 | |
|
1945 | 0 | if (protocol) { |
1946 | 0 | if (NULL == (wrapper = zend_hash_str_find_ptr(wrapper_hash, protocol, n))) { |
1947 | 0 | char *tmp = estrndup(protocol, n); |
1948 | |
|
1949 | 0 | zend_str_tolower(tmp, n); |
1950 | 0 | if (NULL == (wrapper = zend_hash_str_find_ptr(wrapper_hash, tmp, n))) { |
1951 | 0 | char wrapper_name[32]; |
1952 | |
|
1953 | 0 | if (n >= sizeof(wrapper_name)) { |
1954 | 0 | n = sizeof(wrapper_name) - 1; |
1955 | 0 | } |
1956 | 0 | PHP_STRLCPY(wrapper_name, protocol, sizeof(wrapper_name), n); |
1957 | |
|
1958 | 0 | php_error_docref(NULL, E_WARNING, "Unable to find the wrapper \"%s\" - did you forget to enable it when you configured PHP?", wrapper_name); |
1959 | |
|
1960 | 0 | wrapper = NULL; |
1961 | 0 | protocol = NULL; |
1962 | 0 | } |
1963 | 0 | efree(tmp); |
1964 | 0 | } |
1965 | 0 | } |
1966 | | /* TODO: curl based streams probably support file:// properly */ |
1967 | 0 | if (!protocol || !strncasecmp(protocol, "file", n)) { |
1968 | | /* fall back on regular file access */ |
1969 | 0 | php_stream_wrapper *plain_files_wrapper = (php_stream_wrapper*)&php_plain_files_wrapper; |
1970 | |
|
1971 | 0 | if (protocol) { |
1972 | 0 | bool localhost = false; |
1973 | |
|
1974 | 0 | if (!strncasecmp(path, "file://localhost/", 17)) { |
1975 | 0 | localhost = true; |
1976 | 0 | } |
1977 | |
|
1978 | | #ifdef PHP_WIN32 |
1979 | | if (!localhost && path[n+3] != '\0' && path[n+3] != '/' && path[n+4] != ':') { |
1980 | | #else |
1981 | 0 | if (!localhost && path[n+3] != '\0' && path[n+3] != '/') { |
1982 | 0 | #endif |
1983 | 0 | if (options & REPORT_ERRORS) { |
1984 | 0 | php_stream_wrapper_warn(plain_files_wrapper, NULL, options, |
1985 | 0 | ProtocolUnsupported, |
1986 | 0 | "Remote host file access not supported, %s", path); |
1987 | 0 | } |
1988 | 0 | return NULL; |
1989 | 0 | } |
1990 | | |
1991 | 0 | if (path_for_open) { |
1992 | | /* skip past protocol and :/, but handle windows correctly */ |
1993 | 0 | *path_for_open = (char*)path + n + 1; |
1994 | 0 | if (localhost) { |
1995 | 0 | (*path_for_open) += 11; |
1996 | 0 | } |
1997 | 0 | while (*(++*path_for_open)=='/') { |
1998 | | /* intentionally empty */ |
1999 | 0 | } |
2000 | | #ifdef PHP_WIN32 |
2001 | | if (*(*path_for_open + 1) != ':') |
2002 | | #endif |
2003 | 0 | (*path_for_open)--; |
2004 | 0 | } |
2005 | 0 | } |
2006 | | |
2007 | 0 | if (options & STREAM_LOCATE_WRAPPERS_ONLY) { |
2008 | 0 | return NULL; |
2009 | 0 | } |
2010 | | |
2011 | 0 | if (FG(stream_wrappers)) { |
2012 | | /* The file:// wrapper may have been disabled/overridden */ |
2013 | |
|
2014 | 0 | if (wrapper) { |
2015 | | /* It was found so go ahead and provide it */ |
2016 | 0 | return wrapper; |
2017 | 0 | } |
2018 | | |
2019 | | /* Check again, the original check might have not known the protocol name */ |
2020 | 0 | if ((wrapper = zend_hash_find_ex_ptr(wrapper_hash, ZSTR_KNOWN(ZEND_STR_FILE), 1)) != NULL) { |
2021 | 0 | return wrapper; |
2022 | 0 | } |
2023 | | |
2024 | 0 | if (options & REPORT_ERRORS) { |
2025 | 0 | php_stream_wrapper_warn(plain_files_wrapper, NULL, options, |
2026 | 0 | Disabled, |
2027 | 0 | "file:// wrapper is disabled in the server configuration"); |
2028 | 0 | } |
2029 | 0 | return NULL; |
2030 | 0 | } |
2031 | | |
2032 | 0 | return plain_files_wrapper; |
2033 | 0 | } |
2034 | | |
2035 | 0 | if (wrapper && wrapper->is_url && |
2036 | 0 | (options & STREAM_DISABLE_URL_PROTECTION) == 0 && |
2037 | 0 | (!PG(allow_url_fopen) || |
2038 | 0 | (((options & STREAM_OPEN_FOR_INCLUDE) || |
2039 | 0 | PG(in_user_include)) && !PG(allow_url_include)))) { |
2040 | 0 | if (options & REPORT_ERRORS) { |
2041 | | /* protocol[n] probably isn't '\0' */ |
2042 | 0 | if (!PG(allow_url_fopen)) { |
2043 | 0 | php_error_docref(NULL, E_WARNING, "%.*s:// wrapper is disabled in the server configuration by allow_url_fopen=0", (int)n, protocol); |
2044 | 0 | } else { |
2045 | 0 | php_error_docref(NULL, E_WARNING, "%.*s:// wrapper is disabled in the server configuration by allow_url_include=0", (int)n, protocol); |
2046 | 0 | } |
2047 | 0 | } |
2048 | 0 | return NULL; |
2049 | 0 | } |
2050 | | |
2051 | 0 | return wrapper; |
2052 | 0 | } |
2053 | | /* }}} */ |
2054 | | |
2055 | | /* {{{ _php_stream_mkdir */ |
2056 | | PHPAPI int _php_stream_mkdir(const char *path, int mode, int options, php_stream_context *context) |
2057 | 0 | { |
2058 | 0 | php_stream_wrapper *wrapper = NULL; |
2059 | |
|
2060 | 0 | wrapper = php_stream_locate_url_wrapper(path, NULL, 0); |
2061 | 0 | if (!wrapper || !wrapper->wops || !wrapper->wops->stream_mkdir) { |
2062 | 0 | return 0; |
2063 | 0 | } |
2064 | | |
2065 | 0 | return wrapper->wops->stream_mkdir(wrapper, path, mode, options, context); |
2066 | 0 | } |
2067 | | /* }}} */ |
2068 | | |
2069 | | /* {{{ _php_stream_rmdir */ |
2070 | | PHPAPI int _php_stream_rmdir(const char *path, int options, php_stream_context *context) |
2071 | 0 | { |
2072 | 0 | php_stream_wrapper *wrapper = NULL; |
2073 | |
|
2074 | 0 | wrapper = php_stream_locate_url_wrapper(path, NULL, 0); |
2075 | 0 | if (!wrapper || !wrapper->wops || !wrapper->wops->stream_rmdir) { |
2076 | 0 | return 0; |
2077 | 0 | } |
2078 | | |
2079 | 0 | return wrapper->wops->stream_rmdir(wrapper, path, options, context); |
2080 | 0 | } |
2081 | | /* }}} */ |
2082 | | |
2083 | | /* {{{ _php_stream_stat_path */ |
2084 | | PHPAPI int _php_stream_stat_path(const char *path, int flags, php_stream_statbuf *ssb, php_stream_context *context) |
2085 | 0 | { |
2086 | 0 | php_stream_wrapper *wrapper = NULL; |
2087 | 0 | const char *path_to_open = path; |
2088 | |
|
2089 | 0 | memset(ssb, 0, sizeof(*ssb)); |
2090 | |
|
2091 | 0 | wrapper = php_stream_locate_url_wrapper(path, &path_to_open, 0); |
2092 | 0 | if (wrapper && wrapper->wops->url_stat) { |
2093 | 0 | return wrapper->wops->url_stat(wrapper, path_to_open, flags, ssb, context); |
2094 | 0 | } |
2095 | 0 | return -1; |
2096 | 0 | } |
2097 | | /* }}} */ |
2098 | | |
2099 | | /* {{{ php_stream_opendir */ |
2100 | | PHPAPI php_stream *_php_stream_opendir(const char *path, int options, |
2101 | | php_stream_context *context STREAMS_DC) |
2102 | 0 | { |
2103 | 0 | php_stream *stream = NULL; |
2104 | 0 | php_stream_wrapper *wrapper = NULL; |
2105 | 0 | const char *path_to_open; |
2106 | |
|
2107 | 0 | if (!path || !*path) { |
2108 | 0 | return NULL; |
2109 | 0 | } |
2110 | | |
2111 | 0 | path_to_open = path; |
2112 | |
|
2113 | 0 | wrapper = php_stream_locate_url_wrapper(path, &path_to_open, options); |
2114 | |
|
2115 | 0 | if (wrapper && wrapper->wops->dir_opener) { |
2116 | 0 | stream = wrapper->wops->dir_opener(wrapper, |
2117 | 0 | path_to_open, "r", options & ~REPORT_ERRORS, NULL, |
2118 | 0 | context STREAMS_REL_CC); |
2119 | |
|
2120 | 0 | if (stream) { |
2121 | 0 | stream->wrapper = wrapper; |
2122 | 0 | stream->flags |= PHP_STREAM_FLAG_NO_BUFFER | PHP_STREAM_FLAG_IS_DIR; |
2123 | 0 | } |
2124 | 0 | } else if (wrapper) { |
2125 | 0 | php_stream_wrapper_log_warn(wrapper, context, options & ~REPORT_ERRORS, |
2126 | 0 | NoOpener, "not implemented"); |
2127 | 0 | } |
2128 | 0 | if (stream == NULL && (options & REPORT_ERRORS)) { |
2129 | 0 | php_stream_display_wrapper_errors(wrapper, context, PHP_STREAM_EC(OpenFailed), |
2130 | 0 | path, "Failed to open directory"); |
2131 | 0 | } |
2132 | 0 | php_stream_tidy_wrapper_error_log(wrapper); |
2133 | |
|
2134 | 0 | return stream; |
2135 | 0 | } |
2136 | | /* }}} */ |
2137 | | |
2138 | | /* {{{ _php_stream_readdir */ |
2139 | | PHPAPI php_stream_dirent *_php_stream_readdir(php_stream *dirstream, php_stream_dirent *ent) |
2140 | 0 | { |
2141 | |
|
2142 | 0 | if (sizeof(php_stream_dirent) == php_stream_read(dirstream, (char*)ent, sizeof(php_stream_dirent))) { |
2143 | 0 | return ent; |
2144 | 0 | } |
2145 | | |
2146 | 0 | return NULL; |
2147 | 0 | } |
2148 | | /* }}} */ |
2149 | | |
2150 | | /* {{{ php_stream_open_wrapper_ex */ |
2151 | | PHPAPI php_stream *_php_stream_open_wrapper_ex(const char *path, const char *mode, int options, |
2152 | | zend_string **opened_path, php_stream_context *context STREAMS_DC) |
2153 | 0 | { |
2154 | 0 | php_stream *stream = NULL; |
2155 | 0 | php_stream_wrapper *wrapper = NULL; |
2156 | 0 | const char *path_to_open; |
2157 | 0 | int persistent = options & STREAM_OPEN_PERSISTENT; |
2158 | 0 | zend_string *path_str = NULL; |
2159 | 0 | zend_string *resolved_path = NULL; |
2160 | |
|
2161 | 0 | if (opened_path) { |
2162 | 0 | if (options & STREAM_OPEN_FOR_ZEND_STREAM) { |
2163 | 0 | path_str = *opened_path; |
2164 | 0 | } |
2165 | 0 | *opened_path = NULL; |
2166 | 0 | } |
2167 | |
|
2168 | 0 | if (!path || !*path) { |
2169 | 0 | zend_value_error("Path must not be empty"); |
2170 | 0 | return NULL; |
2171 | 0 | } |
2172 | | |
2173 | 0 | if (options & USE_PATH) { |
2174 | 0 | if (path_str) { |
2175 | 0 | resolved_path = zend_resolve_path(path_str); |
2176 | 0 | } else { |
2177 | 0 | resolved_path = php_resolve_path(path, strlen(path), PG(include_path)); |
2178 | 0 | } |
2179 | 0 | if (resolved_path) { |
2180 | 0 | path = ZSTR_VAL(resolved_path); |
2181 | | /* we've found this file, don't re-check include_path or run realpath */ |
2182 | 0 | options |= STREAM_ASSUME_REALPATH; |
2183 | 0 | options &= ~USE_PATH; |
2184 | 0 | } |
2185 | 0 | if (EG(exception)) { |
2186 | 0 | if (resolved_path) { |
2187 | 0 | zend_string_release_ex(resolved_path, false); |
2188 | 0 | } |
2189 | 0 | return NULL; |
2190 | 0 | } |
2191 | 0 | } |
2192 | | |
2193 | 0 | path_to_open = path; |
2194 | |
|
2195 | 0 | wrapper = php_stream_locate_url_wrapper(path, &path_to_open, options); |
2196 | 0 | if ((options & STREAM_USE_URL) && (!wrapper || !wrapper->is_url)) { |
2197 | 0 | if (wrapper) { |
2198 | 0 | php_stream_wrapper_warn(wrapper, context, options, |
2199 | 0 | ProtocolUnsupported, |
2200 | 0 | "This function may only be used against URLs"); |
2201 | 0 | } else { |
2202 | 0 | php_error_docref(NULL, E_WARNING, "This function may only be used against URLs"); |
2203 | 0 | } |
2204 | 0 | if (resolved_path) { |
2205 | 0 | zend_string_release_ex(resolved_path, 0); |
2206 | 0 | } |
2207 | 0 | return NULL; |
2208 | 0 | } |
2209 | | |
2210 | | /* wrapper name needs to be stored as wrapper can be removed in opener (user stream) */ |
2211 | 0 | char *wrapper_name = pestrdup(PHP_STREAM_ERROR_WRAPPER_NAME(wrapper), persistent); |
2212 | 0 | if (wrapper) { |
2213 | 0 | if (!wrapper->wops->stream_opener) { |
2214 | 0 | php_stream_wrapper_log_warn(wrapper, context, options & ~REPORT_ERRORS, |
2215 | 0 | NoOpener, |
2216 | 0 | "wrapper does not support stream open"); |
2217 | 0 | } else { |
2218 | 0 | stream = wrapper->wops->stream_opener(wrapper, |
2219 | 0 | path_to_open, mode, options & ~REPORT_ERRORS, |
2220 | 0 | opened_path, context STREAMS_REL_CC); |
2221 | 0 | } |
2222 | | |
2223 | | /* if the caller asked for a persistent stream but the wrapper did not |
2224 | | * return one, force an error here */ |
2225 | 0 | if (stream && persistent && !stream->is_persistent) { |
2226 | 0 | php_stream_wrapper_log_warn(wrapper, context, options & ~REPORT_ERRORS, |
2227 | 0 | PersistentNotSupported, |
2228 | 0 | "wrapper does not support persistent streams"); |
2229 | 0 | php_stream_close(stream); |
2230 | 0 | stream = NULL; |
2231 | 0 | } |
2232 | |
|
2233 | 0 | if (stream) { |
2234 | 0 | stream->wrapper = wrapper; |
2235 | 0 | } |
2236 | 0 | } |
2237 | |
|
2238 | 0 | if (stream) { |
2239 | 0 | if (opened_path && !*opened_path && resolved_path) { |
2240 | 0 | *opened_path = resolved_path; |
2241 | 0 | resolved_path = NULL; |
2242 | 0 | } |
2243 | 0 | if (stream->orig_path) { |
2244 | 0 | pefree(stream->orig_path, persistent); |
2245 | 0 | } |
2246 | 0 | stream->orig_path = pestrdup(path, persistent); |
2247 | 0 | #if ZEND_DEBUG |
2248 | 0 | stream->open_filename = __zend_orig_filename ? __zend_orig_filename : __zend_filename; |
2249 | 0 | stream->open_lineno = __zend_orig_lineno ? __zend_orig_lineno : __zend_lineno; |
2250 | 0 | #endif |
2251 | 0 | if (stream->ctx == NULL && context != NULL && !persistent) { |
2252 | 0 | php_stream_context_set(stream, context); |
2253 | 0 | } |
2254 | 0 | } |
2255 | |
|
2256 | 0 | if (stream != NULL && (options & STREAM_MUST_SEEK)) { |
2257 | 0 | php_stream *newstream; |
2258 | |
|
2259 | 0 | switch(php_stream_make_seekable_rel(stream, &newstream, |
2260 | 0 | (options & STREAM_WILL_CAST) |
2261 | 0 | ? PHP_STREAM_PREFER_STDIO : PHP_STREAM_NO_PREFERENCE)) { |
2262 | 0 | case PHP_STREAM_UNCHANGED: |
2263 | 0 | if (resolved_path) { |
2264 | 0 | zend_string_release_ex(resolved_path, 0); |
2265 | 0 | } |
2266 | 0 | pefree(wrapper_name, persistent); |
2267 | 0 | return stream; |
2268 | 0 | case PHP_STREAM_RELEASED: |
2269 | 0 | if (newstream->orig_path) { |
2270 | 0 | pefree(newstream->orig_path, persistent); |
2271 | 0 | } |
2272 | 0 | newstream->orig_path = pestrdup(path, persistent); |
2273 | 0 | if (resolved_path) { |
2274 | 0 | zend_string_release_ex(resolved_path, 0); |
2275 | 0 | } |
2276 | 0 | pefree(wrapper_name, persistent); |
2277 | 0 | return newstream; |
2278 | 0 | default: |
2279 | 0 | php_stream_close(stream); |
2280 | 0 | stream = NULL; |
2281 | 0 | if (options & REPORT_ERRORS) { |
2282 | 0 | char *tmp = estrdup(path); |
2283 | 0 | php_strip_url_passwd(tmp); |
2284 | 0 | php_stream_wrapper_warn_param(wrapper, context, options, |
2285 | 0 | SeekNotSupported, tmp, |
2286 | 0 | "could not make seekable - %s", tmp); |
2287 | 0 | efree(tmp); |
2288 | |
|
2289 | 0 | options &= ~REPORT_ERRORS; |
2290 | 0 | } |
2291 | 0 | } |
2292 | 0 | } |
2293 | | |
2294 | 0 | if (stream && stream->ops->seek && (stream->flags & PHP_STREAM_FLAG_NO_SEEK) == 0 && strchr(mode, 'a') && stream->position == 0) { |
2295 | 0 | zend_off_t newpos = 0; |
2296 | | |
2297 | | /* if opened for append, we need to revise our idea of the initial file position */ |
2298 | 0 | if (0 == stream->ops->seek(stream, 0, SEEK_CUR, &newpos)) { |
2299 | 0 | stream->position = newpos; |
2300 | 0 | } |
2301 | 0 | } |
2302 | |
|
2303 | 0 | if (stream == NULL && (options & REPORT_ERRORS)) { |
2304 | 0 | php_stream_display_wrapper_name_errors(wrapper_name, context, PHP_STREAM_EC(OpenFailed), |
2305 | 0 | path, "Failed to open stream"); |
2306 | 0 | if (opened_path && *opened_path) { |
2307 | 0 | zend_string_release_ex(*opened_path, 0); |
2308 | 0 | *opened_path = NULL; |
2309 | 0 | } |
2310 | 0 | } |
2311 | 0 | php_stream_tidy_wrapper_name_error_log(wrapper_name); |
2312 | 0 | pefree(wrapper_name, persistent); |
2313 | 0 | if (resolved_path) { |
2314 | 0 | zend_string_release_ex(resolved_path, 0); |
2315 | 0 | } |
2316 | 0 | return stream; |
2317 | 0 | } |
2318 | | /* }}} */ |
2319 | | |
2320 | | /* {{{ context API */ |
2321 | | PHPAPI php_stream_context *php_stream_context_set(php_stream *stream, php_stream_context *context) |
2322 | 0 | { |
2323 | 0 | php_stream_context *oldcontext = PHP_STREAM_CONTEXT(stream); |
2324 | |
|
2325 | 0 | if (context) { |
2326 | 0 | stream->ctx = context->res; |
2327 | 0 | GC_ADDREF(context->res); |
2328 | 0 | } else { |
2329 | 0 | stream->ctx = NULL; |
2330 | 0 | } |
2331 | 0 | if (oldcontext) { |
2332 | 0 | zend_list_delete(oldcontext->res); |
2333 | 0 | } |
2334 | |
|
2335 | 0 | return oldcontext; |
2336 | 0 | } |
2337 | | |
2338 | | PHPAPI void php_stream_notification_notify(php_stream_context *context, int notifycode, int severity, |
2339 | | char *xmsg, int xcode, size_t bytes_sofar, size_t bytes_max, void * ptr) |
2340 | 0 | { |
2341 | 0 | if (context && context->notifier) |
2342 | 0 | context->notifier->func(context, notifycode, severity, xmsg, xcode, bytes_sofar, bytes_max, ptr); |
2343 | 0 | } |
2344 | | |
2345 | | PHPAPI void php_stream_context_free(php_stream_context *context) |
2346 | 0 | { |
2347 | 0 | if (Z_TYPE(context->options) != IS_UNDEF) { |
2348 | 0 | zval_ptr_dtor(&context->options); |
2349 | 0 | ZVAL_UNDEF(&context->options); |
2350 | 0 | } |
2351 | 0 | if (context->notifier) { |
2352 | 0 | php_stream_notification_free(context->notifier); |
2353 | 0 | context->notifier = NULL; |
2354 | 0 | } |
2355 | 0 | efree(context); |
2356 | 0 | } |
2357 | | |
2358 | | PHPAPI php_stream_context *php_stream_context_alloc(void) |
2359 | 0 | { |
2360 | 0 | php_stream_context *context; |
2361 | |
|
2362 | 0 | context = ecalloc(1, sizeof(php_stream_context)); |
2363 | 0 | array_init(&context->options); |
2364 | |
|
2365 | 0 | context->res = zend_register_resource(context, php_le_stream_context()); |
2366 | 0 | return context; |
2367 | 0 | } |
2368 | | |
2369 | | PHPAPI php_stream_notifier *php_stream_notification_alloc(void) |
2370 | 0 | { |
2371 | 0 | return ecalloc(1, sizeof(php_stream_notifier)); |
2372 | 0 | } |
2373 | | |
2374 | | PHPAPI void php_stream_notification_free(php_stream_notifier *notifier) |
2375 | 0 | { |
2376 | 0 | if (notifier->dtor) { |
2377 | 0 | notifier->dtor(notifier); |
2378 | 0 | } |
2379 | 0 | efree(notifier); |
2380 | 0 | } |
2381 | | |
2382 | | PHPAPI zval *php_stream_context_get_option(const php_stream_context *context, |
2383 | | const char *wrappername, const char *optionname) |
2384 | 0 | { |
2385 | 0 | zval *wrapperhash; |
2386 | |
|
2387 | 0 | if (NULL == (wrapperhash = zend_hash_str_find(Z_ARRVAL(context->options), wrappername, strlen(wrappername)))) { |
2388 | 0 | return NULL; |
2389 | 0 | } |
2390 | 0 | return zend_hash_str_find(Z_ARRVAL_P(wrapperhash), optionname, strlen(optionname)); |
2391 | 0 | } |
2392 | | |
2393 | | PHPAPI void php_stream_context_set_option(php_stream_context *context, |
2394 | | const char *wrappername, const char *optionname, zval *optionvalue) |
2395 | 0 | { |
2396 | 0 | zval *wrapperhash; |
2397 | 0 | zval category; |
2398 | |
|
2399 | 0 | SEPARATE_ARRAY(&context->options); |
2400 | 0 | wrapperhash = zend_hash_str_find(Z_ARRVAL(context->options), wrappername, strlen(wrappername)); |
2401 | 0 | if (NULL == wrapperhash) { |
2402 | 0 | array_init(&category); |
2403 | 0 | wrapperhash = zend_hash_str_update(Z_ARRVAL(context->options), (char*)wrappername, strlen(wrappername), &category); |
2404 | 0 | } |
2405 | 0 | ZVAL_DEREF(optionvalue); |
2406 | 0 | Z_TRY_ADDREF_P(optionvalue); |
2407 | 0 | SEPARATE_ARRAY(wrapperhash); |
2408 | 0 | zend_hash_str_update(Z_ARRVAL_P(wrapperhash), optionname, strlen(optionname), optionvalue); |
2409 | 0 | } |
2410 | | |
2411 | | void php_stream_context_unset_option(php_stream_context *context, |
2412 | | const char *wrappername, const char *optionname) |
2413 | 0 | { |
2414 | 0 | zval *wrapperhash; |
2415 | |
|
2416 | 0 | wrapperhash = zend_hash_str_find(Z_ARRVAL(context->options), wrappername, strlen(wrappername)); |
2417 | 0 | if (NULL == wrapperhash) { |
2418 | 0 | return; |
2419 | 0 | } |
2420 | 0 | SEPARATE_ARRAY(&context->options); |
2421 | 0 | SEPARATE_ARRAY(wrapperhash); |
2422 | 0 | zend_hash_str_del(Z_ARRVAL_P(wrapperhash), optionname, strlen(optionname)); |
2423 | 0 | } |
2424 | | /* }}} */ |
2425 | | |
2426 | | PHPAPI const struct php_uri_parser *php_stream_context_get_uri_parser(const char *wrappername, php_stream_context *context) |
2427 | 0 | { |
2428 | 0 | if (context == NULL) { |
2429 | 0 | return php_uri_get_parser(NULL); |
2430 | 0 | } |
2431 | | |
2432 | 0 | zval *uri_parser_name = php_stream_context_get_option(context, wrappername, "uri_parser_class"); |
2433 | 0 | if (uri_parser_name == NULL || Z_TYPE_P(uri_parser_name) == IS_NULL) { |
2434 | 0 | return php_uri_get_parser(NULL); |
2435 | 0 | } |
2436 | | |
2437 | 0 | if (Z_TYPE_P(uri_parser_name) != IS_STRING) { |
2438 | 0 | return NULL; |
2439 | 0 | } |
2440 | | |
2441 | 0 | return php_uri_get_parser(Z_STR_P(uri_parser_name)); |
2442 | 0 | } |
2443 | | |
2444 | | /* {{{ php_stream_dirent_alphasort */ |
2445 | | PHPAPI int php_stream_dirent_alphasort(const zend_string **a, const zend_string **b) |
2446 | 0 | { |
2447 | 0 | return strcoll(ZSTR_VAL(*a), ZSTR_VAL(*b)); |
2448 | 0 | } |
2449 | | /* }}} */ |
2450 | | |
2451 | | /* {{{ php_stream_dirent_alphasortr */ |
2452 | | PHPAPI int php_stream_dirent_alphasortr(const zend_string **a, const zend_string **b) |
2453 | 0 | { |
2454 | 0 | return strcoll(ZSTR_VAL(*b), ZSTR_VAL(*a)); |
2455 | 0 | } |
2456 | | /* }}} */ |
2457 | | |
2458 | | /* {{{ php_stream_scandir */ |
2459 | | PHPAPI int _php_stream_scandir(const char *dirname, zend_string **namelist[], int flags, php_stream_context *context, |
2460 | | int (*compare) (const zend_string **a, const zend_string **b)) |
2461 | 0 | { |
2462 | 0 | php_stream *stream; |
2463 | 0 | php_stream_dirent sdp; |
2464 | 0 | zend_string **vector = NULL; |
2465 | 0 | unsigned int vector_size = 0; |
2466 | 0 | unsigned int nfiles = 0; |
2467 | |
|
2468 | 0 | if (!namelist) { |
2469 | 0 | return -1; |
2470 | 0 | } |
2471 | | |
2472 | 0 | stream = php_stream_opendir(dirname, REPORT_ERRORS, context); |
2473 | 0 | if (!stream) { |
2474 | 0 | return -1; |
2475 | 0 | } |
2476 | | |
2477 | 0 | while (php_stream_readdir(stream, &sdp)) { |
2478 | 0 | if (nfiles == vector_size) { |
2479 | 0 | if (vector_size == 0) { |
2480 | 0 | vector_size = 10; |
2481 | 0 | } else { |
2482 | 0 | if(vector_size*2 < vector_size) { |
2483 | 0 | goto overflow; |
2484 | 0 | } |
2485 | 0 | vector_size *= 2; |
2486 | 0 | } |
2487 | 0 | vector = (zend_string **) safe_erealloc(vector, vector_size, sizeof(zend_string *), 0); |
2488 | 0 | } |
2489 | | |
2490 | 0 | vector[nfiles] = zend_string_init(sdp.d_name, strlen(sdp.d_name), 0); |
2491 | |
|
2492 | 0 | if(vector_size < 10 || nfiles + 1 == 0) { |
2493 | 0 | goto overflow; |
2494 | 0 | } |
2495 | 0 | nfiles++; |
2496 | 0 | } |
2497 | 0 | php_stream_closedir(stream); |
2498 | |
|
2499 | 0 | *namelist = vector; |
2500 | |
|
2501 | 0 | if (nfiles > 0 && compare) { |
2502 | 0 | qsort(*namelist, nfiles, sizeof(zend_string *), (int(*)(const void *, const void *))compare); |
2503 | 0 | } |
2504 | 0 | return nfiles; |
2505 | | |
2506 | 0 | overflow: |
2507 | 0 | php_stream_closedir(stream); |
2508 | 0 | for (unsigned int i = 0; i < nfiles; i++) { |
2509 | 0 | zend_string_efree(vector[i]); |
2510 | 0 | } |
2511 | | efree(vector); |
2512 | 0 | return -1; |
2513 | 0 | } |
2514 | | /* }}} */ |