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