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