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