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