/src/php-src/main/streams/memory.c
Line | Count | Source |
1 | | /* |
2 | | +----------------------------------------------------------------------+ |
3 | | | Copyright © The PHP Group and Contributors. | |
4 | | +----------------------------------------------------------------------+ |
5 | | | This source file is subject to the Modified BSD License that is | |
6 | | | bundled with this package in the file LICENSE, and is available | |
7 | | | through the World Wide Web at <https://www.php.net/license/>. | |
8 | | | | |
9 | | | SPDX-License-Identifier: BSD-3-Clause | |
10 | | +----------------------------------------------------------------------+ |
11 | | | Author: Marcus Boerger <helly@php.net> | |
12 | | +----------------------------------------------------------------------+ |
13 | | */ |
14 | | |
15 | | #ifndef _GNU_SOURCE |
16 | | # define _GNU_SOURCE |
17 | | #endif |
18 | | #include "php.h" |
19 | | #include "ext/standard/base64.h" |
20 | | |
21 | | PHPAPI size_t php_url_decode(char *str, size_t len); |
22 | | |
23 | | /* Memory streams use a dynamic memory buffer to emulate a stream. |
24 | | * You can use php_stream_memory_open to create a readonly stream |
25 | | * from an existing memory buffer. |
26 | | */ |
27 | | |
28 | | /* Temp streams are streams that uses memory streams as long their |
29 | | * size is less than a given memory amount. When a write operation |
30 | | * exceeds that limit the content is written to a temporary file. |
31 | | */ |
32 | | |
33 | | /* {{{ ------- MEMORY stream implementation -------*/ |
34 | | |
35 | | typedef struct { |
36 | | zend_string *data; |
37 | | size_t fpos; |
38 | | int mode; |
39 | | } php_stream_memory_data; |
40 | | |
41 | | |
42 | | /* {{{ */ |
43 | | static ssize_t php_stream_memory_write(php_stream *stream, const char *buf, size_t count) |
44 | 0 | { |
45 | 0 | php_stream_memory_data *ms = (php_stream_memory_data*)stream->abstract; |
46 | 0 | assert(ms != NULL); |
47 | |
|
48 | 0 | if (ms->mode & TEMP_STREAM_READONLY) { |
49 | 0 | return (ssize_t) -1; |
50 | 0 | } |
51 | 0 | size_t data_len = ZSTR_LEN(ms->data); |
52 | 0 | if (ms->mode & TEMP_STREAM_APPEND) { |
53 | 0 | ms->fpos = data_len; |
54 | 0 | } |
55 | 0 | if (ms->fpos + count > data_len) { |
56 | 0 | ms->data = zend_string_realloc(ms->data, ms->fpos + count, 0); |
57 | 0 | if (ms->fpos > data_len) { |
58 | | /* zero the bytes added due to seek past end position */ |
59 | 0 | memset(ZSTR_VAL(ms->data) + data_len, 0, ms->fpos - data_len); |
60 | 0 | } |
61 | 0 | } else { |
62 | 0 | ms->data = zend_string_separate(ms->data, 0); |
63 | 0 | } |
64 | 0 | if (count) { |
65 | 0 | ZEND_ASSERT(buf != NULL); |
66 | 0 | memcpy(ZSTR_VAL(ms->data) + ms->fpos, (char*) buf, count); |
67 | 0 | ZSTR_VAL(ms->data)[ZSTR_LEN(ms->data)] = '\0'; |
68 | 0 | ms->fpos += count; |
69 | 0 | } |
70 | 0 | return count; |
71 | 0 | } |
72 | | /* }}} */ |
73 | | |
74 | | |
75 | | /* {{{ */ |
76 | | static ssize_t php_stream_memory_read(php_stream *stream, char *buf, size_t count) |
77 | 368 | { |
78 | 368 | php_stream_memory_data *ms = (php_stream_memory_data*)stream->abstract; |
79 | 368 | assert(ms != NULL); |
80 | | |
81 | 368 | if (ms->fpos >= ZSTR_LEN(ms->data)) { |
82 | 21 | stream->eof = 1; |
83 | 21 | count = 0; |
84 | 347 | } else { |
85 | 347 | if (ms->fpos + count > ZSTR_LEN(ms->data)) { |
86 | 17 | count = ZSTR_LEN(ms->data) - ms->fpos; |
87 | 17 | } |
88 | 347 | if (count) { |
89 | 347 | ZEND_ASSERT(buf != NULL); |
90 | 347 | memcpy(buf, ZSTR_VAL(ms->data) + ms->fpos, count); |
91 | 347 | ms->fpos += count; |
92 | 347 | } |
93 | 347 | } |
94 | 368 | return count; |
95 | 368 | } |
96 | | /* }}} */ |
97 | | |
98 | | |
99 | | /* {{{ */ |
100 | | static int php_stream_memory_close(php_stream *stream, int close_handle) |
101 | 22 | { |
102 | 22 | php_stream_memory_data *ms = (php_stream_memory_data*)stream->abstract; |
103 | 22 | ZEND_ASSERT(ms != NULL); |
104 | 22 | zend_string_release(ms->data); |
105 | 22 | efree(ms); |
106 | 22 | return 0; |
107 | 22 | } |
108 | | /* }}} */ |
109 | | |
110 | | |
111 | | /* {{{ */ |
112 | | static int php_stream_memory_flush(php_stream *stream) |
113 | 0 | { |
114 | | /* nothing to do here */ |
115 | 0 | return 0; |
116 | 0 | } |
117 | | /* }}} */ |
118 | | |
119 | | |
120 | | /* {{{ */ |
121 | | static int php_stream_memory_seek(php_stream *stream, zend_off_t offset, int whence, zend_off_t *newoffs) |
122 | 53 | { |
123 | 53 | php_stream_memory_data *ms = (php_stream_memory_data*)stream->abstract; |
124 | 53 | assert(ms != NULL); |
125 | | |
126 | 53 | switch(whence) { |
127 | 0 | case SEEK_CUR: |
128 | 0 | if (offset < 0) { |
129 | 0 | if (ms->fpos < (size_t)(-offset)) { |
130 | 0 | ms->fpos = 0; |
131 | 0 | *newoffs = -1; |
132 | 0 | return -1; |
133 | 0 | } else { |
134 | 0 | ms->fpos = ms->fpos + offset; |
135 | 0 | *newoffs = ms->fpos; |
136 | 0 | stream->eof = 0; |
137 | 0 | stream->fatal_error = 0; |
138 | 0 | return 0; |
139 | 0 | } |
140 | 0 | } else { |
141 | 0 | stream->eof = 0; |
142 | 0 | stream->fatal_error = 0; |
143 | 0 | ms->fpos = ms->fpos + offset; |
144 | 0 | *newoffs = ms->fpos; |
145 | 0 | return 0; |
146 | 0 | } |
147 | 53 | case SEEK_SET: |
148 | 53 | if (offset < 0) { |
149 | 0 | ms->fpos = 0; |
150 | 0 | *newoffs = -1; |
151 | 0 | return -1; |
152 | 53 | } else { |
153 | 53 | ms->fpos = offset; |
154 | 53 | *newoffs = ms->fpos; |
155 | 53 | stream->eof = 0; |
156 | 53 | stream->fatal_error = 0; |
157 | 53 | return 0; |
158 | 53 | } |
159 | 0 | case SEEK_END: |
160 | 0 | if (offset > 0) { |
161 | 0 | ms->fpos = ZSTR_LEN(ms->data) + offset; |
162 | 0 | *newoffs = ms->fpos; |
163 | 0 | stream->eof = 0; |
164 | 0 | stream->fatal_error = 0; |
165 | 0 | return 0; |
166 | 0 | } else if (ZSTR_LEN(ms->data) < (size_t)(-offset)) { |
167 | 0 | ms->fpos = 0; |
168 | 0 | *newoffs = -1; |
169 | 0 | return -1; |
170 | 0 | } else { |
171 | 0 | ms->fpos = ZSTR_LEN(ms->data) + offset; |
172 | 0 | *newoffs = ms->fpos; |
173 | 0 | stream->eof = 0; |
174 | 0 | stream->fatal_error = 0; |
175 | 0 | return 0; |
176 | 0 | } |
177 | 0 | default: |
178 | 0 | *newoffs = ms->fpos; |
179 | 0 | return -1; |
180 | 53 | } |
181 | 53 | } |
182 | | /* }}} */ |
183 | | |
184 | | /* {{{ */ |
185 | | static int php_stream_memory_cast(php_stream *stream, int castas, void **ret) |
186 | 0 | { |
187 | 0 | return FAILURE; |
188 | 0 | } |
189 | | /* }}} */ |
190 | | |
191 | | static int php_stream_memory_stat(php_stream *stream, php_stream_statbuf *ssb) /* {{{ */ |
192 | 0 | { |
193 | 0 | time_t timestamp = 0; |
194 | 0 | php_stream_memory_data *ms = (php_stream_memory_data*)stream->abstract; |
195 | 0 | assert(ms != NULL); |
196 | |
|
197 | 0 | memset(ssb, 0, sizeof(php_stream_statbuf)); |
198 | | /* read-only across the board */ |
199 | |
|
200 | 0 | ssb->sb.st_mode = ms->mode & TEMP_STREAM_READONLY ? 0444 : 0666; |
201 | |
|
202 | 0 | ssb->sb.st_size = ZSTR_LEN(ms->data); |
203 | 0 | ssb->sb.st_mode |= S_IFREG; /* regular file */ |
204 | 0 | ssb->sb.st_mtime = timestamp; |
205 | 0 | ssb->sb.st_atime = timestamp; |
206 | 0 | ssb->sb.st_ctime = timestamp; |
207 | 0 | ssb->sb.st_nlink = 1; |
208 | 0 | ssb->sb.st_rdev = -1; |
209 | | /* this is only for APC, so use /dev/null device - no chance of conflict there! */ |
210 | 0 | ssb->sb.st_dev = 0xC; |
211 | | /* generate unique inode number for alias/filename, so no phars will conflict */ |
212 | 0 | ssb->sb.st_ino = 0; |
213 | |
|
214 | 0 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
215 | 0 | ssb->sb.st_blksize = -1; |
216 | 0 | #endif |
217 | 0 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
218 | 0 | ssb->sb.st_blocks = -1; |
219 | 0 | #endif |
220 | |
|
221 | 0 | return 0; |
222 | 0 | } |
223 | | /* }}} */ |
224 | | |
225 | | static int php_stream_memory_set_option(php_stream *stream, int option, int value, void *ptrparam) /* {{{ */ |
226 | 0 | { |
227 | 0 | php_stream_memory_data *ms = (php_stream_memory_data*)stream->abstract; |
228 | 0 | size_t newsize; |
229 | |
|
230 | 0 | switch(option) { |
231 | 0 | case PHP_STREAM_OPTION_TRUNCATE_API: |
232 | 0 | switch (value) { |
233 | 0 | case PHP_STREAM_TRUNCATE_SUPPORTED: |
234 | 0 | return PHP_STREAM_OPTION_RETURN_OK; |
235 | | |
236 | 0 | case PHP_STREAM_TRUNCATE_SET_SIZE: |
237 | 0 | if (ms->mode & TEMP_STREAM_READONLY) { |
238 | 0 | return PHP_STREAM_OPTION_RETURN_ERR; |
239 | 0 | } |
240 | 0 | newsize = *(size_t*)ptrparam; |
241 | 0 | if (newsize <= ZSTR_LEN(ms->data)) { |
242 | 0 | ms->data = zend_string_truncate(ms->data, newsize, 0); |
243 | 0 | if (newsize < ms->fpos) { |
244 | 0 | ms->fpos = newsize; |
245 | 0 | } |
246 | 0 | } else { |
247 | 0 | size_t old_size = ZSTR_LEN(ms->data); |
248 | 0 | ms->data = zend_string_realloc(ms->data, newsize, 0); |
249 | 0 | memset(ZSTR_VAL(ms->data) + old_size, 0, newsize - old_size); |
250 | 0 | } |
251 | 0 | ZSTR_VAL(ms->data)[ZSTR_LEN(ms->data)] = '\0'; |
252 | 0 | return PHP_STREAM_OPTION_RETURN_OK; |
253 | 0 | } |
254 | 0 | } |
255 | | |
256 | 0 | return PHP_STREAM_OPTION_RETURN_NOTIMPL; |
257 | 0 | } |
258 | | /* }}} */ |
259 | | |
260 | | PHPAPI const php_stream_ops php_stream_memory_ops = { |
261 | | php_stream_memory_write, php_stream_memory_read, |
262 | | php_stream_memory_close, php_stream_memory_flush, |
263 | | "MEMORY", |
264 | | php_stream_memory_seek, |
265 | | php_stream_memory_cast, |
266 | | php_stream_memory_stat, |
267 | | php_stream_memory_set_option |
268 | | }; |
269 | | |
270 | | /* {{{ */ |
271 | | PHPAPI int php_stream_mode_from_str(const char *mode) |
272 | 0 | { |
273 | 0 | if (strpbrk(mode, "a")) { |
274 | 0 | return TEMP_STREAM_APPEND; |
275 | 0 | } else if (strpbrk(mode, "w+")) { |
276 | 0 | return TEMP_STREAM_DEFAULT; |
277 | 0 | } |
278 | 0 | return TEMP_STREAM_READONLY; |
279 | 0 | } |
280 | | /* }}} */ |
281 | | |
282 | | /* {{{ */ |
283 | | PHPAPI const char *_php_stream_mode_to_str(int mode) |
284 | 22 | { |
285 | 22 | if (mode == TEMP_STREAM_READONLY) { |
286 | 22 | return "rb"; |
287 | 22 | } else if (mode == TEMP_STREAM_APPEND) { |
288 | 0 | return "a+b"; |
289 | 0 | } |
290 | 0 | return "w+b"; |
291 | 22 | } |
292 | | /* }}} */ |
293 | | |
294 | | /* {{{ */ |
295 | | PHPAPI php_stream *_php_stream_memory_create(int mode STREAMS_DC) |
296 | 22 | { |
297 | 22 | php_stream_memory_data *self; |
298 | 22 | php_stream *stream; |
299 | | |
300 | 22 | self = emalloc(sizeof(*self)); |
301 | 22 | self->data = ZSTR_EMPTY_ALLOC(); |
302 | 22 | self->fpos = 0; |
303 | 22 | self->mode = mode; |
304 | | |
305 | 22 | stream = php_stream_alloc_rel(&php_stream_memory_ops, self, 0, _php_stream_mode_to_str(mode)); |
306 | 22 | stream->flags |= PHP_STREAM_FLAG_NO_BUFFER; |
307 | 22 | return stream; |
308 | 22 | } |
309 | | /* }}} */ |
310 | | |
311 | | |
312 | | /* {{{ */ |
313 | | PHPAPI php_stream *_php_stream_memory_open(int mode, zend_string *buf STREAMS_DC) |
314 | 22 | { |
315 | 22 | php_stream *stream; |
316 | 22 | php_stream_memory_data *ms; |
317 | | |
318 | 22 | if ((stream = php_stream_memory_create_rel(mode)) != NULL) { |
319 | 22 | ms = (php_stream_memory_data*)stream->abstract; |
320 | 22 | ms->data = zend_string_copy(buf); |
321 | 22 | } |
322 | 22 | return stream; |
323 | 22 | } |
324 | | /* }}} */ |
325 | | |
326 | | |
327 | | /* {{{ */ |
328 | | PHPAPI zend_string *_php_stream_memory_get_buffer(php_stream *stream STREAMS_DC) |
329 | 0 | { |
330 | 0 | php_stream_memory_data *ms = (php_stream_memory_data*)stream->abstract; |
331 | 0 | ZEND_ASSERT(ms != NULL); |
332 | 0 | return ms->data; |
333 | 0 | } |
334 | | /* }}} */ |
335 | | |
336 | | /* }}} */ |
337 | | |
338 | | /* {{{ ------- TEMP stream implementation -------*/ |
339 | | |
340 | | typedef struct { |
341 | | php_stream *innerstream; |
342 | | size_t smax; |
343 | | int mode; |
344 | | zval meta; |
345 | | char* tmpdir; |
346 | | } php_stream_temp_data; |
347 | | |
348 | | |
349 | | /* {{{ */ |
350 | | static ssize_t php_stream_temp_write(php_stream *stream, const char *buf, size_t count) |
351 | 0 | { |
352 | 0 | php_stream_temp_data *ts = (php_stream_temp_data*)stream->abstract; |
353 | 0 | assert(ts != NULL); |
354 | |
|
355 | 0 | if (!ts->innerstream) { |
356 | 0 | return -1; |
357 | 0 | } |
358 | 0 | if (php_stream_is(ts->innerstream, PHP_STREAM_IS_MEMORY)) { |
359 | 0 | zend_off_t pos = php_stream_tell(ts->innerstream); |
360 | |
|
361 | 0 | if (pos + count >= ts->smax) { |
362 | 0 | zend_string *membuf = php_stream_memory_get_buffer(ts->innerstream); |
363 | 0 | php_stream *file = php_stream_fopen_temporary_file(ts->tmpdir, "php", NULL); |
364 | 0 | if (file == NULL) { |
365 | 0 | php_stream_wrapper_warn(NULL, PHP_STREAM_CONTEXT(stream), REPORT_ERRORS, |
366 | 0 | PermissionDenied, |
367 | 0 | "Unable to create temporary file, Check permissions in temporary files directory."); |
368 | 0 | return 0; |
369 | 0 | } |
370 | 0 | php_stream_write(file, ZSTR_VAL(membuf), ZSTR_LEN(membuf)); |
371 | 0 | php_stream_free_enclosed(ts->innerstream, PHP_STREAM_FREE_CLOSE); |
372 | 0 | ts->innerstream = file; |
373 | 0 | php_stream_encloses(stream, ts->innerstream); |
374 | 0 | php_stream_seek(ts->innerstream, pos, SEEK_SET); |
375 | 0 | } |
376 | 0 | } |
377 | 0 | return php_stream_write(ts->innerstream, buf, count); |
378 | 0 | } |
379 | | /* }}} */ |
380 | | |
381 | | |
382 | | /* {{{ */ |
383 | | static ssize_t php_stream_temp_read(php_stream *stream, char *buf, size_t count) |
384 | 0 | { |
385 | 0 | php_stream_temp_data *ts = (php_stream_temp_data*)stream->abstract; |
386 | 0 | size_t got; |
387 | |
|
388 | 0 | assert(ts != NULL); |
389 | |
|
390 | 0 | if (!ts->innerstream) { |
391 | 0 | return -1; |
392 | 0 | } |
393 | | |
394 | 0 | got = php_stream_read(ts->innerstream, buf, count); |
395 | |
|
396 | 0 | stream->eof = ts->innerstream->eof; |
397 | |
|
398 | 0 | return got; |
399 | 0 | } |
400 | | /* }}} */ |
401 | | |
402 | | |
403 | | /* {{{ */ |
404 | | static int php_stream_temp_close(php_stream *stream, int close_handle) |
405 | 0 | { |
406 | 0 | php_stream_temp_data *ts = (php_stream_temp_data*)stream->abstract; |
407 | 0 | int ret; |
408 | |
|
409 | 0 | assert(ts != NULL); |
410 | |
|
411 | 0 | if (ts->innerstream) { |
412 | 0 | ret = php_stream_free_enclosed(ts->innerstream, PHP_STREAM_FREE_CLOSE | (close_handle ? 0 : PHP_STREAM_FREE_PRESERVE_HANDLE)); |
413 | 0 | } else { |
414 | 0 | ret = 0; |
415 | 0 | } |
416 | |
|
417 | 0 | zval_ptr_dtor(&ts->meta); |
418 | |
|
419 | 0 | if (ts->tmpdir) { |
420 | 0 | efree(ts->tmpdir); |
421 | 0 | } |
422 | |
|
423 | 0 | efree(ts); |
424 | |
|
425 | 0 | return ret; |
426 | 0 | } |
427 | | /* }}} */ |
428 | | |
429 | | |
430 | | /* {{{ */ |
431 | | static int php_stream_temp_flush(php_stream *stream) |
432 | 0 | { |
433 | 0 | php_stream_temp_data *ts = (php_stream_temp_data*)stream->abstract; |
434 | 0 | assert(ts != NULL); |
435 | |
|
436 | 0 | return ts->innerstream ? php_stream_flush(ts->innerstream) : -1; |
437 | 0 | } |
438 | | /* }}} */ |
439 | | |
440 | | |
441 | | /* {{{ */ |
442 | | static int php_stream_temp_seek(php_stream *stream, zend_off_t offset, int whence, zend_off_t *newoffs) |
443 | 0 | { |
444 | 0 | php_stream_temp_data *ts = (php_stream_temp_data*)stream->abstract; |
445 | 0 | int ret; |
446 | |
|
447 | 0 | assert(ts != NULL); |
448 | |
|
449 | 0 | if (!ts->innerstream) { |
450 | 0 | *newoffs = -1; |
451 | 0 | return -1; |
452 | 0 | } |
453 | 0 | ret = php_stream_seek(ts->innerstream, offset, whence); |
454 | 0 | *newoffs = php_stream_tell(ts->innerstream); |
455 | 0 | stream->eof = ts->innerstream->eof; |
456 | |
|
457 | 0 | return ret; |
458 | 0 | } |
459 | | /* }}} */ |
460 | | |
461 | | /* {{{ */ |
462 | | static int php_stream_temp_cast(php_stream *stream, int castas, void **ret) |
463 | 0 | { |
464 | 0 | php_stream_temp_data *ts = (php_stream_temp_data*)stream->abstract; |
465 | 0 | php_stream *file; |
466 | 0 | zend_string *membuf; |
467 | 0 | zend_off_t pos; |
468 | |
|
469 | 0 | assert(ts != NULL); |
470 | |
|
471 | 0 | if (!ts->innerstream) { |
472 | 0 | return FAILURE; |
473 | 0 | } |
474 | 0 | if (php_stream_is(ts->innerstream, PHP_STREAM_IS_STDIO)) { |
475 | 0 | return php_stream_cast(ts->innerstream, castas, ret, 0); |
476 | 0 | } |
477 | | |
478 | | /* we are still using a memory based backing. If they are if we can be |
479 | | * a FILE*, say yes because we can perform the conversion. |
480 | | * If they actually want to perform the conversion, we need to switch |
481 | | * the memory stream to a tmpfile stream */ |
482 | | |
483 | 0 | if (ret == NULL && castas == PHP_STREAM_AS_STDIO) { |
484 | 0 | return SUCCESS; |
485 | 0 | } |
486 | | |
487 | | /* say "no" to other stream forms */ |
488 | 0 | if (ret == NULL) { |
489 | 0 | return FAILURE; |
490 | 0 | } |
491 | | |
492 | 0 | file = php_stream_fopen_tmpfile(); |
493 | 0 | if (file == NULL) { |
494 | 0 | php_stream_wrapper_warn(NULL, PHP_STREAM_CONTEXT(stream), REPORT_ERRORS, |
495 | 0 | PermissionDenied, "Unable to create temporary file."); |
496 | 0 | return FAILURE; |
497 | 0 | } |
498 | | |
499 | | /* perform the conversion and then pass the request on to the innerstream */ |
500 | 0 | membuf = php_stream_memory_get_buffer(ts->innerstream); |
501 | 0 | php_stream_write(file, ZSTR_VAL(membuf), ZSTR_LEN(membuf)); |
502 | 0 | pos = php_stream_tell(ts->innerstream); |
503 | |
|
504 | 0 | php_stream_free_enclosed(ts->innerstream, PHP_STREAM_FREE_CLOSE); |
505 | 0 | ts->innerstream = file; |
506 | 0 | php_stream_encloses(stream, ts->innerstream); |
507 | 0 | php_stream_seek(ts->innerstream, pos, SEEK_SET); |
508 | |
|
509 | 0 | return php_stream_cast(ts->innerstream, castas, ret, 1); |
510 | 0 | } |
511 | | /* }}} */ |
512 | | |
513 | | static int php_stream_temp_stat(php_stream *stream, php_stream_statbuf *ssb) /* {{{ */ |
514 | 0 | { |
515 | 0 | php_stream_temp_data *ts = (php_stream_temp_data*)stream->abstract; |
516 | |
|
517 | 0 | if (!ts || !ts->innerstream) { |
518 | 0 | return -1; |
519 | 0 | } |
520 | 0 | return php_stream_stat(ts->innerstream, ssb); |
521 | 0 | } |
522 | | /* }}} */ |
523 | | |
524 | | static int php_stream_temp_set_option(php_stream *stream, int option, int value, void *ptrparam) /* {{{ */ |
525 | 0 | { |
526 | 0 | php_stream_temp_data *ts = (php_stream_temp_data*)stream->abstract; |
527 | |
|
528 | 0 | switch(option) { |
529 | 0 | case PHP_STREAM_OPTION_META_DATA_API: |
530 | 0 | if (Z_TYPE(ts->meta) != IS_UNDEF) { |
531 | 0 | zend_hash_copy(Z_ARRVAL_P((zval*)ptrparam), Z_ARRVAL(ts->meta), zval_add_ref); |
532 | 0 | } |
533 | 0 | return PHP_STREAM_OPTION_RETURN_OK; |
534 | 0 | default: |
535 | 0 | if (ts->innerstream) { |
536 | 0 | return php_stream_set_option(ts->innerstream, option, value, ptrparam); |
537 | 0 | } |
538 | 0 | return PHP_STREAM_OPTION_RETURN_NOTIMPL; |
539 | 0 | } |
540 | 0 | } |
541 | | /* }}} */ |
542 | | |
543 | | PHPAPI const php_stream_ops php_stream_temp_ops = { |
544 | | php_stream_temp_write, php_stream_temp_read, |
545 | | php_stream_temp_close, php_stream_temp_flush, |
546 | | "TEMP", |
547 | | php_stream_temp_seek, |
548 | | php_stream_temp_cast, |
549 | | php_stream_temp_stat, |
550 | | php_stream_temp_set_option |
551 | | }; |
552 | | |
553 | | /* }}} */ |
554 | | |
555 | | /* {{{ _php_stream_temp_create_ex */ |
556 | | PHPAPI php_stream *_php_stream_temp_create_ex(int mode, size_t max_memory_usage, const char *tmpdir STREAMS_DC) |
557 | 0 | { |
558 | 0 | php_stream_temp_data *self; |
559 | 0 | php_stream *stream; |
560 | |
|
561 | 0 | self = ecalloc(1, sizeof(*self)); |
562 | 0 | self->smax = max_memory_usage; |
563 | 0 | self->mode = mode; |
564 | 0 | ZVAL_UNDEF(&self->meta); |
565 | 0 | if (tmpdir) { |
566 | 0 | self->tmpdir = estrdup(tmpdir); |
567 | 0 | } |
568 | 0 | stream = php_stream_alloc_rel(&php_stream_temp_ops, self, 0, _php_stream_mode_to_str(mode)); |
569 | 0 | stream->flags |= PHP_STREAM_FLAG_NO_BUFFER; |
570 | 0 | self->innerstream = php_stream_memory_create_rel(mode); |
571 | 0 | php_stream_encloses(stream, self->innerstream); |
572 | |
|
573 | 0 | return stream; |
574 | 0 | } |
575 | | /* }}} */ |
576 | | |
577 | | /* {{{ _php_stream_temp_create */ |
578 | | PHPAPI php_stream *_php_stream_temp_create(int mode, size_t max_memory_usage STREAMS_DC) |
579 | 0 | { |
580 | 0 | return php_stream_temp_create_ex(mode, max_memory_usage, NULL); |
581 | 0 | } |
582 | | /* }}} */ |
583 | | |
584 | | /* {{{ _php_stream_temp_open */ |
585 | | PHPAPI php_stream *_php_stream_temp_open(int mode, size_t max_memory_usage, const char *buf, size_t length STREAMS_DC) |
586 | 0 | { |
587 | 0 | php_stream *stream; |
588 | 0 | php_stream_temp_data *ts; |
589 | 0 | zend_off_t newoffs; |
590 | |
|
591 | 0 | if ((stream = php_stream_temp_create_rel(mode, max_memory_usage)) != NULL) { |
592 | 0 | if (length) { |
593 | 0 | assert(buf != NULL); |
594 | 0 | php_stream_temp_write(stream, buf, length); |
595 | 0 | php_stream_temp_seek(stream, 0, SEEK_SET, &newoffs); |
596 | 0 | } |
597 | 0 | ts = (php_stream_temp_data*)stream->abstract; |
598 | 0 | assert(ts != NULL); |
599 | 0 | ts->mode = mode; |
600 | 0 | } |
601 | 0 | return stream; |
602 | 0 | } |
603 | | /* }}} */ |
604 | | |
605 | | PHPAPI const php_stream_ops php_stream_rfc2397_ops = { |
606 | | NULL, php_stream_temp_read, |
607 | | php_stream_temp_close, php_stream_temp_flush, |
608 | | "RFC2397", |
609 | | php_stream_temp_seek, |
610 | | php_stream_temp_cast, |
611 | | php_stream_temp_stat, |
612 | | php_stream_temp_set_option |
613 | | }; |
614 | | |
615 | | static php_stream * php_stream_url_wrap_rfc2397(php_stream_wrapper *wrapper, const char *path, |
616 | | const char *mode, int options, zend_string **opened_path, |
617 | | php_stream_context *context STREAMS_DC) /* {{{ */ |
618 | 0 | { |
619 | 0 | php_stream *stream; |
620 | 0 | php_stream_temp_data *ts; |
621 | 0 | char *comma; |
622 | 0 | const char *semi, *sep; |
623 | 0 | size_t mlen, dlen, plen, vlen, ilen; |
624 | 0 | zend_off_t newoffs; |
625 | 0 | zval meta; |
626 | 0 | int base64 = 0; |
627 | 0 | zend_string *base64_comma = NULL; |
628 | |
|
629 | 0 | ZEND_ASSERT(mode); |
630 | |
|
631 | 0 | ZVAL_NULL(&meta); |
632 | 0 | if (memcmp(path, "data:", 5)) { |
633 | 0 | return NULL; |
634 | 0 | } |
635 | | |
636 | 0 | path += 5; |
637 | 0 | dlen = strlen(path); |
638 | |
|
639 | 0 | if (dlen >= 2 && path[0] == '/' && path[1] == '/') { |
640 | 0 | dlen -= 2; |
641 | 0 | path += 2; |
642 | 0 | } |
643 | |
|
644 | 0 | if ((comma = (char *) memchr(path, ',', dlen)) == NULL) { |
645 | 0 | php_stream_wrapper_log_warn(wrapper, context, options, |
646 | 0 | InvalidUrl, "rfc2397: no comma in URL"); |
647 | 0 | return NULL; |
648 | 0 | } |
649 | | |
650 | 0 | if (comma != path) { |
651 | | /* meta info */ |
652 | 0 | mlen = comma - path; |
653 | 0 | dlen -= mlen; |
654 | 0 | semi = memchr(path, ';', mlen); |
655 | 0 | sep = memchr(path, '/', mlen); |
656 | |
|
657 | 0 | if (!semi && !sep) { |
658 | 0 | php_stream_wrapper_log_warn(wrapper, context, options, |
659 | 0 | InvalidUrl, "rfc2397: illegal media type"); |
660 | 0 | return NULL; |
661 | 0 | } |
662 | | |
663 | 0 | array_init(&meta); |
664 | 0 | if (!semi) { /* there is only a mime type */ |
665 | 0 | add_assoc_stringl(&meta, "mediatype", (char *) path, mlen); |
666 | 0 | mlen = 0; |
667 | 0 | } else if (sep && sep < semi) { /* there is a mime type */ |
668 | 0 | plen = semi - path; |
669 | 0 | add_assoc_stringl(&meta, "mediatype", (char *) path, plen); |
670 | 0 | mlen -= plen; |
671 | 0 | path += plen; |
672 | 0 | } else if (semi != path || mlen != sizeof(";base64")-1 || memcmp(path, ";base64", sizeof(";base64")-1)) { /* must be error since parameters are only allowed after mediatype */ |
673 | 0 | zval_ptr_dtor(&meta); |
674 | 0 | php_stream_wrapper_log_warn(wrapper, context, options, |
675 | 0 | InvalidUrl, "rfc2397: illegal media type"); |
676 | 0 | return NULL; |
677 | 0 | } |
678 | | /* get parameters and potentially ';base64' */ |
679 | 0 | while(semi && (semi == path)) { |
680 | 0 | path++; |
681 | 0 | mlen--; |
682 | 0 | sep = memchr(path, '=', mlen); |
683 | 0 | semi = memchr(path, ';', mlen); |
684 | 0 | if (!sep || (semi && semi < sep)) { /* must be ';base64' or failure */ |
685 | 0 | if (mlen != sizeof("base64")-1 || memcmp(path, "base64", sizeof("base64")-1)) { |
686 | | /* must be error since parameters are only allowed after mediatype and we have no '=' sign */ |
687 | 0 | zval_ptr_dtor(&meta); |
688 | 0 | php_stream_wrapper_log_warn(wrapper, context, options, |
689 | 0 | InvalidParam, "rfc2397: illegal parameter"); |
690 | 0 | return NULL; |
691 | 0 | } |
692 | 0 | base64 = 1; |
693 | 0 | mlen -= sizeof("base64") - 1; |
694 | 0 | path += sizeof("base64") - 1; |
695 | 0 | break; |
696 | 0 | } |
697 | | /* found parameter ... the heart of cs ppl lies in +1/-1 or was it +2 this time? */ |
698 | 0 | plen = sep - path; |
699 | 0 | vlen = (semi ? (size_t)(semi - sep) : (mlen - plen)) - 1 /* '=' */; |
700 | 0 | if (plen != sizeof("mediatype")-1 || memcmp(path, "mediatype", sizeof("mediatype")-1)) { |
701 | 0 | add_assoc_stringl_ex(&meta, path, plen, sep + 1, vlen); |
702 | 0 | } |
703 | 0 | plen += vlen + 1; |
704 | 0 | mlen -= plen; |
705 | 0 | path += plen; |
706 | 0 | } |
707 | 0 | if (mlen) { |
708 | 0 | zval_ptr_dtor(&meta); |
709 | 0 | php_stream_wrapper_log_warn(wrapper, context, options, |
710 | 0 | InvalidUrl, "rfc2397: illegal URL"); |
711 | 0 | return NULL; |
712 | 0 | } |
713 | 0 | } else { |
714 | 0 | array_init(&meta); |
715 | 0 | } |
716 | 0 | add_assoc_bool(&meta, "base64", base64); |
717 | | |
718 | | /* skip ',' */ |
719 | 0 | comma++; |
720 | 0 | dlen--; |
721 | |
|
722 | 0 | if (base64) { |
723 | 0 | base64_comma = php_base64_decode_ex((const unsigned char *)comma, dlen, 1); |
724 | 0 | if (!base64_comma) { |
725 | 0 | zval_ptr_dtor(&meta); |
726 | 0 | php_stream_wrapper_log_warn(wrapper, context, options, |
727 | 0 | DecodingFailed, "rfc2397: unable to decode"); |
728 | 0 | return NULL; |
729 | 0 | } |
730 | 0 | comma = ZSTR_VAL(base64_comma); |
731 | 0 | ilen = ZSTR_LEN(base64_comma); |
732 | 0 | } else { |
733 | 0 | comma = estrndup(comma, dlen); |
734 | 0 | dlen = php_url_decode(comma, dlen); |
735 | 0 | ilen = dlen; |
736 | 0 | } |
737 | | |
738 | 0 | if ((stream = php_stream_temp_create_rel(0, ~0u)) != NULL) { |
739 | | /* store data */ |
740 | 0 | php_stream_temp_write(stream, comma, ilen); |
741 | 0 | php_stream_temp_seek(stream, 0, SEEK_SET, &newoffs); |
742 | | /* set special stream stuff (enforce exact mode) */ |
743 | 0 | vlen = strlen(mode); |
744 | 0 | if (vlen >= sizeof(stream->mode)) { |
745 | 0 | vlen = sizeof(stream->mode) - 1; |
746 | 0 | } |
747 | 0 | memcpy(stream->mode, mode, vlen); |
748 | 0 | stream->mode[vlen] = '\0'; |
749 | 0 | stream->ops = &php_stream_rfc2397_ops; |
750 | 0 | ts = (php_stream_temp_data*)stream->abstract; |
751 | 0 | assert(ts != NULL); |
752 | 0 | ts->mode = mode[0] == 'r' && mode[1] != '+' ? TEMP_STREAM_READONLY : 0; |
753 | 0 | ZVAL_COPY_VALUE(&ts->meta, &meta); |
754 | 0 | } |
755 | 0 | if (base64_comma) { |
756 | 0 | zend_string_free(base64_comma); |
757 | 0 | } else { |
758 | 0 | efree(comma); |
759 | 0 | } |
760 | |
|
761 | 0 | return stream; |
762 | 0 | } |
763 | | |
764 | | PHPAPI const php_stream_wrapper_ops php_stream_rfc2397_wops = { |
765 | | php_stream_url_wrap_rfc2397, |
766 | | NULL, /* close */ |
767 | | NULL, /* fstat */ |
768 | | NULL, /* stat */ |
769 | | NULL, /* opendir */ |
770 | | "RFC2397", |
771 | | NULL, /* unlink */ |
772 | | NULL, /* rename */ |
773 | | NULL, /* mkdir */ |
774 | | NULL, /* rmdir */ |
775 | | NULL, /* stream_metadata */ |
776 | | }; |
777 | | |
778 | | PHPAPI const php_stream_wrapper php_stream_rfc2397_wrapper = { |
779 | | &php_stream_rfc2397_wops, |
780 | | NULL, |
781 | | 1, /* is_url */ |
782 | | }; |