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