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