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