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