1 /*
2 +----------------------------------------------------------------------+
3 | PHP Version 7 |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 1997-2018 The PHP Group |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
15 | Author: Marcus Boerger <helly@php.net> |
16 +----------------------------------------------------------------------+
17 */
18
19 #define _GNU_SOURCE
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 char *data;
39 size_t fpos;
40 size_t fsize;
41 size_t smax;
42 int mode;
43 } php_stream_memory_data;
44
45
46 /* {{{ */
php_stream_memory_write(php_stream * stream,const char * buf,size_t count)47 static size_t php_stream_memory_write(php_stream *stream, const char *buf, size_t count)
48 {
49 php_stream_memory_data *ms = (php_stream_memory_data*)stream->abstract;
50 assert(ms != NULL);
51
52 if (ms->mode & TEMP_STREAM_READONLY) {
53 return 0;
54 } else if (ms->mode & TEMP_STREAM_APPEND) {
55 ms->fpos = ms->fsize;
56 }
57 if (ms->fpos + count > ms->fsize) {
58 char *tmp;
59 if (!ms->data) {
60 tmp = emalloc(ms->fpos + count);
61 } else {
62 tmp = erealloc(ms->data, ms->fpos + count);
63 }
64 ms->data = tmp;
65 ms->fsize = ms->fpos + count;
66 }
67 if (!ms->data)
68 count = 0;
69 if (count) {
70 assert(buf!= NULL);
71 memcpy(ms->data+ms->fpos, (char*)buf, count);
72 ms->fpos += count;
73 }
74 return count;
75 }
76 /* }}} */
77
78
79 /* {{{ */
php_stream_memory_read(php_stream * stream,char * buf,size_t count)80 static size_t php_stream_memory_read(php_stream *stream, char *buf, size_t count)
81 {
82 php_stream_memory_data *ms = (php_stream_memory_data*)stream->abstract;
83 assert(ms != NULL);
84
85 if (ms->fpos == ms->fsize) {
86 stream->eof = 1;
87 count = 0;
88 } else {
89 if (ms->fpos + count >= ms->fsize) {
90 count = ms->fsize - ms->fpos;
91 }
92 if (count) {
93 assert(ms->data!= NULL);
94 assert(buf!= NULL);
95 memcpy(buf, ms->data+ms->fpos, count);
96 ms->fpos += count;
97 }
98 }
99 return count;
100 }
101 /* }}} */
102
103
104 /* {{{ */
php_stream_memory_close(php_stream * stream,int close_handle)105 static int php_stream_memory_close(php_stream *stream, int close_handle)
106 {
107 php_stream_memory_data *ms = (php_stream_memory_data*)stream->abstract;
108 assert(ms != NULL);
109
110 if (ms->data && close_handle && ms->mode != TEMP_STREAM_READONLY) {
111 efree(ms->data);
112 }
113 efree(ms);
114 return 0;
115 }
116 /* }}} */
117
118
119 /* {{{ */
php_stream_memory_flush(php_stream * stream)120 static int php_stream_memory_flush(php_stream *stream)
121 {
122 /* nothing to do here */
123 return 0;
124 }
125 /* }}} */
126
127
128 /* {{{ */
php_stream_memory_seek(php_stream * stream,zend_off_t offset,int whence,zend_off_t * newoffs)129 static int php_stream_memory_seek(php_stream *stream, zend_off_t offset, int whence, zend_off_t *newoffs)
130 {
131 php_stream_memory_data *ms = (php_stream_memory_data*)stream->abstract;
132 assert(ms != NULL);
133
134 switch(whence) {
135 case SEEK_CUR:
136 if (offset < 0) {
137 if (ms->fpos < (size_t)(-offset)) {
138 ms->fpos = 0;
139 *newoffs = -1;
140 return -1;
141 } else {
142 ms->fpos = ms->fpos + offset;
143 *newoffs = ms->fpos;
144 stream->eof = 0;
145 return 0;
146 }
147 } else {
148 if (ms->fpos + (size_t)(offset) > ms->fsize) {
149 ms->fpos = ms->fsize;
150 *newoffs = -1;
151 return -1;
152 } else {
153 ms->fpos = ms->fpos + offset;
154 *newoffs = ms->fpos;
155 stream->eof = 0;
156 return 0;
157 }
158 }
159 case SEEK_SET:
160 if (ms->fsize < (size_t)(offset)) {
161 ms->fpos = ms->fsize;
162 *newoffs = -1;
163 return -1;
164 } else {
165 ms->fpos = offset;
166 *newoffs = ms->fpos;
167 stream->eof = 0;
168 return 0;
169 }
170 case SEEK_END:
171 if (offset > 0) {
172 ms->fpos = ms->fsize;
173 *newoffs = -1;
174 return -1;
175 } else if (ms->fsize < (size_t)(-offset)) {
176 ms->fpos = 0;
177 *newoffs = -1;
178 return -1;
179 } else {
180 ms->fpos = ms->fsize + offset;
181 *newoffs = ms->fpos;
182 stream->eof = 0;
183 return 0;
184 }
185 default:
186 *newoffs = ms->fpos;
187 return -1;
188 }
189 }
190 /* }}} */
191
192 /* {{{ */
php_stream_memory_cast(php_stream * stream,int castas,void ** ret)193 static int php_stream_memory_cast(php_stream *stream, int castas, void **ret)
194 {
195 return FAILURE;
196 }
197 /* }}} */
198
php_stream_memory_stat(php_stream * stream,php_stream_statbuf * ssb)199 static int php_stream_memory_stat(php_stream *stream, php_stream_statbuf *ssb) /* {{{ */
200 {
201 time_t timestamp = 0;
202 php_stream_memory_data *ms = (php_stream_memory_data*)stream->abstract;
203 assert(ms != NULL);
204
205 memset(ssb, 0, sizeof(php_stream_statbuf));
206 /* read-only across the board */
207
208 ssb->sb.st_mode = ms->mode & TEMP_STREAM_READONLY ? 0444 : 0666;
209
210 ssb->sb.st_size = ms->fsize;
211 ssb->sb.st_mode |= S_IFREG; /* regular file */
212 ssb->sb.st_mtime = timestamp;
213 ssb->sb.st_atime = timestamp;
214 ssb->sb.st_ctime = timestamp;
215 ssb->sb.st_nlink = 1;
216 ssb->sb.st_rdev = -1;
217 /* this is only for APC, so use /dev/null device - no chance of conflict there! */
218 ssb->sb.st_dev = 0xC;
219 /* generate unique inode number for alias/filename, so no phars will conflict */
220 ssb->sb.st_ino = 0;
221
222 #ifndef PHP_WIN32
223 ssb->sb.st_blksize = -1;
224 ssb->sb.st_blocks = -1;
225 #endif
226
227 return 0;
228 }
229 /* }}} */
230
php_stream_memory_set_option(php_stream * stream,int option,int value,void * ptrparam)231 static int php_stream_memory_set_option(php_stream *stream, int option, int value, void *ptrparam) /* {{{ */
232 {
233 php_stream_memory_data *ms = (php_stream_memory_data*)stream->abstract;
234 size_t newsize;
235
236 switch(option) {
237 case PHP_STREAM_OPTION_TRUNCATE_API:
238 switch (value) {
239 case PHP_STREAM_TRUNCATE_SUPPORTED:
240 return PHP_STREAM_OPTION_RETURN_OK;
241
242 case PHP_STREAM_TRUNCATE_SET_SIZE:
243 if (ms->mode & TEMP_STREAM_READONLY) {
244 return PHP_STREAM_OPTION_RETURN_ERR;
245 }
246 newsize = *(size_t*)ptrparam;
247 if (newsize <= ms->fsize) {
248 if (newsize < ms->fpos) {
249 ms->fpos = newsize;
250 }
251 } else {
252 ms->data = erealloc(ms->data, newsize);
253 memset(ms->data+ms->fsize, 0, newsize - ms->fsize);
254 ms->fsize = newsize;
255 }
256 ms->fsize = newsize;
257 return PHP_STREAM_OPTION_RETURN_OK;
258 }
259 default:
260 return PHP_STREAM_OPTION_RETURN_NOTIMPL;
261 }
262 }
263 /* }}} */
264
265 PHPAPI const php_stream_ops php_stream_memory_ops = {
266 php_stream_memory_write, php_stream_memory_read,
267 php_stream_memory_close, php_stream_memory_flush,
268 "MEMORY",
269 php_stream_memory_seek,
270 php_stream_memory_cast,
271 php_stream_memory_stat,
272 php_stream_memory_set_option
273 };
274
275 /* {{{ */
php_stream_mode_from_str(const char * mode)276 PHPAPI int php_stream_mode_from_str(const char *mode)
277 {
278 if (strpbrk(mode, "a")) {
279 return TEMP_STREAM_APPEND;
280 } else if (strpbrk(mode, "w+")) {
281 return TEMP_STREAM_DEFAULT;
282 }
283 return TEMP_STREAM_READONLY;
284 }
285 /* }}} */
286
287 /* {{{ */
_php_stream_mode_to_str(int mode)288 PHPAPI const char *_php_stream_mode_to_str(int mode)
289 {
290 if (mode == TEMP_STREAM_READONLY) {
291 return "rb";
292 } else if (mode == TEMP_STREAM_APPEND) {
293 return "a+b";
294 }
295 return "w+b";
296 }
297 /* }}} */
298
299 /* {{{ */
_php_stream_memory_create(int mode STREAMS_DC)300 PHPAPI php_stream *_php_stream_memory_create(int mode STREAMS_DC)
301 {
302 php_stream_memory_data *self;
303 php_stream *stream;
304
305 self = emalloc(sizeof(*self));
306 self->data = NULL;
307 self->fpos = 0;
308 self->fsize = 0;
309 self->smax = ~0u;
310 self->mode = mode;
311
312 stream = php_stream_alloc_rel(&php_stream_memory_ops, self, 0, _php_stream_mode_to_str(mode));
313 stream->flags |= PHP_STREAM_FLAG_NO_BUFFER;
314 return stream;
315 }
316 /* }}} */
317
318
319 /* {{{ */
_php_stream_memory_open(int mode,char * buf,size_t length STREAMS_DC)320 PHPAPI php_stream *_php_stream_memory_open(int mode, char *buf, size_t length STREAMS_DC)
321 {
322 php_stream *stream;
323 php_stream_memory_data *ms;
324
325 if ((stream = php_stream_memory_create_rel(mode)) != NULL) {
326 ms = (php_stream_memory_data*)stream->abstract;
327
328 if (mode == TEMP_STREAM_READONLY || mode == TEMP_STREAM_TAKE_BUFFER) {
329 /* use the buffer directly */
330 ms->data = buf;
331 ms->fsize = length;
332 } else {
333 if (length) {
334 assert(buf != NULL);
335 php_stream_write(stream, buf, length);
336 }
337 }
338 }
339 return stream;
340 }
341 /* }}} */
342
343
344 /* {{{ */
_php_stream_memory_get_buffer(php_stream * stream,size_t * length STREAMS_DC)345 PHPAPI char *_php_stream_memory_get_buffer(php_stream *stream, size_t *length STREAMS_DC)
346 {
347 php_stream_memory_data *ms = (php_stream_memory_data*)stream->abstract;
348
349 assert(ms != NULL);
350 assert(length != 0);
351
352 *length = ms->fsize;
353 return ms->data;
354 }
355 /* }}} */
356
357 /* }}} */
358
359 /* {{{ ------- TEMP stream implementation -------*/
360
361 typedef struct {
362 php_stream *innerstream;
363 size_t smax;
364 int mode;
365 zval meta;
366 char* tmpdir;
367 } php_stream_temp_data;
368
369
370 /* {{{ */
php_stream_temp_write(php_stream * stream,const char * buf,size_t count)371 static size_t php_stream_temp_write(php_stream *stream, const char *buf, size_t count)
372 {
373 php_stream_temp_data *ts = (php_stream_temp_data*)stream->abstract;
374 assert(ts != NULL);
375
376 if (!ts->innerstream) {
377 return -1;
378 }
379 if (php_stream_is(ts->innerstream, PHP_STREAM_IS_MEMORY)) {
380 size_t memsize;
381 char *membuf = php_stream_memory_get_buffer(ts->innerstream, &memsize);
382
383 if (memsize + count >= ts->smax) {
384 php_stream *file = php_stream_fopen_temporary_file(ts->tmpdir, "php", NULL);
385 if (file == NULL) {
386 php_error_docref(NULL, E_WARNING, "Unable to create temporary file, Check permissions in temporary files directory.");
387 return 0;
388 }
389 php_stream_write(file, membuf, memsize);
390 php_stream_free_enclosed(ts->innerstream, PHP_STREAM_FREE_CLOSE);
391 ts->innerstream = file;
392 php_stream_encloses(stream, ts->innerstream);
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 size_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,char * buf,size_t length STREAMS_DC)603 PHPAPI php_stream *_php_stream_temp_open(int mode, size_t max_memory_usage, 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 php_stream_temp_write, 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
793 /*
794 * Local variables:
795 * tab-width: 4
796 * c-basic-offset: 4
797 * End:
798 * vim600: noet sw=4 ts=4 fdm=marker
799 * vim<600: noet sw=4 ts=4
800 */
801