1 /*
2 +----------------------------------------------------------------------+
3 | PHP Version 5 |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 1997-2015 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
24 PHPAPI int php_url_decode(char *str, int len);
25 PHPAPI unsigned char *php_base64_decode(const unsigned char *str, int length, int *ret_length);
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 TSRMLS_DC)49 static size_t php_stream_memory_write(php_stream *stream, const char *buf, size_t count TSRMLS_DC)
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 TSRMLS_DC)85 static size_t php_stream_memory_read(php_stream *stream, char *buf, size_t count TSRMLS_DC)
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 TSRMLS_DC)110 static int php_stream_memory_close(php_stream *stream, int close_handle TSRMLS_DC)
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 TSRMLS_DC)125 static int php_stream_memory_flush(php_stream *stream TSRMLS_DC)
126 {
127 /* nothing to do here */
128 return 0;
129 }
130 /* }}} */
131
132
133 /* {{{ */
php_stream_memory_seek(php_stream * stream,off_t offset,int whence,off_t * newoffs TSRMLS_DC)134 static int php_stream_memory_seek(php_stream *stream, off_t offset, int whence, off_t *newoffs TSRMLS_DC)
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 TSRMLS_DC)198 static int php_stream_memory_cast(php_stream *stream, int castas, void **ret TSRMLS_DC)
199 {
200 return FAILURE;
201 }
202 /* }}} */
203
php_stream_memory_stat(php_stream * stream,php_stream_statbuf * ssb TSRMLS_DC)204 static int php_stream_memory_stat(php_stream *stream, php_stream_statbuf *ssb TSRMLS_DC) /* {{{ */
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 TSRMLS_DC)247 static int php_stream_memory_set_option(php_stream *stream, int option, int value, void *ptrparam TSRMLS_DC) /* {{{ */
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 TSRMLS_DC)293 PHPAPI php_stream *_php_stream_memory_create(int mode STREAMS_DC TSRMLS_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 TSRMLS_DC)313 PHPAPI php_stream *_php_stream_memory_open(int mode, char *buf, size_t length STREAMS_DC TSRMLS_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 TSRMLS_DC)338 PHPAPI char *_php_stream_memory_get_buffer(php_stream *stream, size_t *length STREAMS_DC TSRMLS_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 } php_stream_temp_data;
360
361
362 /* {{{ */
php_stream_temp_write(php_stream * stream,const char * buf,size_t count TSRMLS_DC)363 static size_t php_stream_temp_write(php_stream *stream, const char *buf, size_t count TSRMLS_DC)
364 {
365 php_stream_temp_data *ts = (php_stream_temp_data*)stream->abstract;
366 assert(ts != NULL);
367
368 if (!ts->innerstream) {
369 return -1;
370 }
371 if (php_stream_is(ts->innerstream, PHP_STREAM_IS_MEMORY)) {
372 size_t memsize;
373 char *membuf = php_stream_memory_get_buffer(ts->innerstream, &memsize);
374
375 if (memsize + count >= ts->smax) {
376 php_stream *file = php_stream_fopen_tmpfile();
377 if (file == NULL) {
378 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to create temporary file, Check permissions in temporary files directory.");
379 return 0;
380 }
381 php_stream_write(file, membuf, memsize);
382 php_stream_free_enclosed(ts->innerstream, PHP_STREAM_FREE_CLOSE);
383 ts->innerstream = file;
384 php_stream_encloses(stream, ts->innerstream);
385 }
386 }
387 return php_stream_write(ts->innerstream, buf, count);
388 }
389 /* }}} */
390
391
392 /* {{{ */
php_stream_temp_read(php_stream * stream,char * buf,size_t count TSRMLS_DC)393 static size_t php_stream_temp_read(php_stream *stream, char *buf, size_t count TSRMLS_DC)
394 {
395 php_stream_temp_data *ts = (php_stream_temp_data*)stream->abstract;
396 size_t got;
397
398 assert(ts != NULL);
399
400 if (!ts->innerstream) {
401 return -1;
402 }
403
404 got = php_stream_read(ts->innerstream, buf, count);
405
406 stream->eof = ts->innerstream->eof;
407
408 return got;
409 }
410 /* }}} */
411
412
413 /* {{{ */
php_stream_temp_close(php_stream * stream,int close_handle TSRMLS_DC)414 static int php_stream_temp_close(php_stream *stream, int close_handle TSRMLS_DC)
415 {
416 php_stream_temp_data *ts = (php_stream_temp_data*)stream->abstract;
417 int ret;
418
419 assert(ts != NULL);
420
421 if (ts->innerstream) {
422 ret = php_stream_free_enclosed(ts->innerstream, PHP_STREAM_FREE_CLOSE | (close_handle ? 0 : PHP_STREAM_FREE_PRESERVE_HANDLE));
423 } else {
424 ret = 0;
425 }
426
427 if (ts->meta) {
428 zval_ptr_dtor(&ts->meta);
429 }
430
431 efree(ts);
432
433 return ret;
434 }
435 /* }}} */
436
437
438 /* {{{ */
php_stream_temp_flush(php_stream * stream TSRMLS_DC)439 static int php_stream_temp_flush(php_stream *stream TSRMLS_DC)
440 {
441 php_stream_temp_data *ts = (php_stream_temp_data*)stream->abstract;
442 assert(ts != NULL);
443
444 return ts->innerstream ? php_stream_flush(ts->innerstream) : -1;
445 }
446 /* }}} */
447
448
449 /* {{{ */
php_stream_temp_seek(php_stream * stream,off_t offset,int whence,off_t * newoffs TSRMLS_DC)450 static int php_stream_temp_seek(php_stream *stream, off_t offset, int whence, off_t *newoffs TSRMLS_DC)
451 {
452 php_stream_temp_data *ts = (php_stream_temp_data*)stream->abstract;
453 int ret;
454
455 assert(ts != NULL);
456
457 if (!ts->innerstream) {
458 *newoffs = -1;
459 return -1;
460 }
461 ret = php_stream_seek(ts->innerstream, offset, whence);
462 *newoffs = php_stream_tell(ts->innerstream);
463 stream->eof = ts->innerstream->eof;
464
465 return ret;
466 }
467 /* }}} */
468
469 /* {{{ */
php_stream_temp_cast(php_stream * stream,int castas,void ** ret TSRMLS_DC)470 static int php_stream_temp_cast(php_stream *stream, int castas, void **ret TSRMLS_DC)
471 {
472 php_stream_temp_data *ts = (php_stream_temp_data*)stream->abstract;
473 php_stream *file;
474 size_t memsize;
475 char *membuf;
476 off_t pos;
477
478 assert(ts != NULL);
479
480 if (!ts->innerstream) {
481 return FAILURE;
482 }
483 if (php_stream_is(ts->innerstream, PHP_STREAM_IS_STDIO)) {
484 return php_stream_cast(ts->innerstream, castas, ret, 0);
485 }
486
487 /* we are still using a memory based backing. If they are if we can be
488 * a FILE*, say yes because we can perform the conversion.
489 * If they actually want to perform the conversion, we need to switch
490 * the memory stream to a tmpfile stream */
491
492 if (ret == NULL && castas == PHP_STREAM_AS_STDIO) {
493 return SUCCESS;
494 }
495
496 /* say "no" to other stream forms */
497 if (ret == NULL) {
498 return FAILURE;
499 }
500
501 /* perform the conversion and then pass the request on to the innerstream */
502 membuf = php_stream_memory_get_buffer(ts->innerstream, &memsize);
503 file = php_stream_fopen_tmpfile();
504 php_stream_write(file, membuf, memsize);
505 pos = php_stream_tell(ts->innerstream);
506
507 php_stream_free_enclosed(ts->innerstream, PHP_STREAM_FREE_CLOSE);
508 ts->innerstream = file;
509 php_stream_encloses(stream, ts->innerstream);
510 php_stream_seek(ts->innerstream, pos, SEEK_SET);
511
512 return php_stream_cast(ts->innerstream, castas, ret, 1);
513 }
514 /* }}} */
515
php_stream_temp_stat(php_stream * stream,php_stream_statbuf * ssb TSRMLS_DC)516 static int php_stream_temp_stat(php_stream *stream, php_stream_statbuf *ssb TSRMLS_DC) /* {{{ */
517 {
518 php_stream_temp_data *ts = (php_stream_temp_data*)stream->abstract;
519
520 if (!ts || !ts->innerstream) {
521 return -1;
522 }
523 return php_stream_stat(ts->innerstream, ssb);
524 }
525 /* }}} */
526
php_stream_temp_set_option(php_stream * stream,int option,int value,void * ptrparam TSRMLS_DC)527 static int php_stream_temp_set_option(php_stream *stream, int option, int value, void *ptrparam TSRMLS_DC) /* {{{ */
528 {
529 php_stream_temp_data *ts = (php_stream_temp_data*)stream->abstract;
530
531 switch(option) {
532 case PHP_STREAM_OPTION_META_DATA_API:
533 if (ts->meta) {
534 zend_hash_copy(Z_ARRVAL_P((zval*)ptrparam), Z_ARRVAL_P(ts->meta), (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval*));
535 }
536 return PHP_STREAM_OPTION_RETURN_OK;
537 default:
538 if (ts->innerstream) {
539 return php_stream_set_option(ts->innerstream, option, value, ptrparam);
540 }
541 return PHP_STREAM_OPTION_RETURN_NOTIMPL;
542 }
543 }
544 /* }}} */
545
546 PHPAPI php_stream_ops php_stream_temp_ops = {
547 php_stream_temp_write, php_stream_temp_read,
548 php_stream_temp_close, php_stream_temp_flush,
549 "TEMP",
550 php_stream_temp_seek,
551 php_stream_temp_cast,
552 php_stream_temp_stat,
553 php_stream_temp_set_option
554 };
555
556 /* }}} */
557
558 /* {{{ _php_stream_temp_create */
_php_stream_temp_create(int mode,size_t max_memory_usage STREAMS_DC TSRMLS_DC)559 PHPAPI php_stream *_php_stream_temp_create(int mode, size_t max_memory_usage STREAMS_DC TSRMLS_DC)
560 {
561 php_stream_temp_data *self;
562 php_stream *stream;
563
564 self = ecalloc(1, sizeof(*self));
565 self->smax = max_memory_usage;
566 self->mode = mode;
567 self->meta = NULL;
568 stream = php_stream_alloc_rel(&php_stream_temp_ops, self, 0, mode & TEMP_STREAM_READONLY ? "rb" : "w+b");
569 stream->flags |= PHP_STREAM_FLAG_NO_BUFFER;
570 self->innerstream = php_stream_memory_create_rel(mode);
571 php_stream_encloses(stream, self->innerstream);
572
573 return stream;
574 }
575 /* }}} */
576
577
578 /* {{{ _php_stream_temp_open */
_php_stream_temp_open(int mode,size_t max_memory_usage,char * buf,size_t length STREAMS_DC TSRMLS_DC)579 PHPAPI php_stream *_php_stream_temp_open(int mode, size_t max_memory_usage, char *buf, size_t length STREAMS_DC TSRMLS_DC)
580 {
581 php_stream *stream;
582 php_stream_temp_data *ts;
583 off_t newoffs;
584
585 if ((stream = php_stream_temp_create_rel(mode, max_memory_usage)) != NULL) {
586 if (length) {
587 assert(buf != NULL);
588 php_stream_temp_write(stream, buf, length TSRMLS_CC);
589 php_stream_temp_seek(stream, 0, SEEK_SET, &newoffs TSRMLS_CC);
590 }
591 ts = (php_stream_temp_data*)stream->abstract;
592 assert(ts != NULL);
593 ts->mode = mode;
594 }
595 return stream;
596 }
597 /* }}} */
598
599 PHPAPI php_stream_ops php_stream_rfc2397_ops = {
600 php_stream_temp_write, php_stream_temp_read,
601 php_stream_temp_close, php_stream_temp_flush,
602 "RFC2397",
603 php_stream_temp_seek,
604 php_stream_temp_cast,
605 php_stream_temp_stat,
606 php_stream_temp_set_option
607 };
608
php_stream_url_wrap_rfc2397(php_stream_wrapper * wrapper,char * path,char * mode,int options,char ** opened_path,php_stream_context * context STREAMS_DC TSRMLS_DC)609 static php_stream * php_stream_url_wrap_rfc2397(php_stream_wrapper *wrapper, char *path, char *mode, int options, char **opened_path, php_stream_context *context STREAMS_DC TSRMLS_DC) /* {{{ */
610 {
611 php_stream *stream;
612 php_stream_temp_data *ts;
613 char *comma, *semi, *sep, *key;
614 size_t mlen, dlen, plen, vlen;
615 off_t newoffs;
616 zval *meta = NULL;
617 int base64 = 0, ilen;
618
619 if (memcmp(path, "data:", 5)) {
620 return NULL;
621 }
622
623 path += 5;
624 dlen = strlen(path);
625
626 if (dlen >= 2 && path[0] == '/' && path[1] == '/') {
627 dlen -= 2;
628 path += 2;
629 }
630
631 if ((comma = memchr(path, ',', dlen)) == NULL) {
632 php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "rfc2397: no comma in URL");
633 return NULL;
634 }
635
636 if (comma != path) {
637 /* meta info */
638 mlen = comma - path;
639 dlen -= mlen;
640 semi = memchr(path, ';', mlen);
641 sep = memchr(path, '/', mlen);
642
643 if (!semi && !sep) {
644 php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "rfc2397: illegal media type");
645 return NULL;
646 }
647
648 MAKE_STD_ZVAL(meta);
649 array_init(meta);
650 if (!semi) { /* there is only a mime type */
651 add_assoc_stringl(meta, "mediatype", path, mlen, 1);
652 mlen = 0;
653 } else if (sep && sep < semi) { /* there is a mime type */
654 plen = semi - path;
655 add_assoc_stringl(meta, "mediatype", path, plen, 1);
656 mlen -= plen;
657 path += plen;
658 } else if (semi != path || mlen != sizeof(";base64")-1 || memcmp(path, ";base64", sizeof(";base64")-1)) { /* must be error since parameters are only allowed after mediatype */
659 zval_ptr_dtor(&meta);
660 php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "rfc2397: illegal media type");
661 return NULL;
662 }
663 /* get parameters and potentially ';base64' */
664 while(semi && (semi == path)) {
665 path++;
666 mlen--;
667 sep = memchr(path, '=', mlen);
668 semi = memchr(path, ';', mlen);
669 if (!sep || (semi && semi < sep)) { /* must be ';base64' or failure */
670 if (mlen != sizeof("base64")-1 || memcmp(path, "base64", sizeof("base64")-1)) {
671 /* must be error since parameters are only allowed after mediatype and we have no '=' sign */
672 zval_ptr_dtor(&meta);
673 php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "rfc2397: illegal parameter");
674 return NULL;
675 }
676 base64 = 1;
677 mlen -= sizeof("base64") - 1;
678 path += sizeof("base64") - 1;
679 break;
680 }
681 /* found parameter ... the heart of cs ppl lies in +1/-1 or was it +2 this time? */
682 plen = sep - path;
683 vlen = (semi ? semi - sep : mlen - plen) - 1 /* '=' */;
684 key = estrndup(path, plen);
685 if (plen != sizeof("mediatype")-1 || memcmp(key, "mediatype", sizeof("mediatype")-1)) {
686 add_assoc_stringl_ex(meta, key, plen + 1, sep + 1, vlen, 1);
687 }
688 efree(key);
689 plen += vlen + 1;
690 mlen -= plen;
691 path += plen;
692 }
693 if (mlen) {
694 zval_ptr_dtor(&meta);
695 php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "rfc2397: illegal URL");
696 return NULL;
697 }
698 } else {
699 MAKE_STD_ZVAL(meta);
700 array_init(meta);
701 }
702 add_assoc_bool(meta, "base64", base64);
703
704 /* skip ',' */
705 comma++;
706 dlen--;
707
708 if (base64) {
709 comma = (char*)php_base64_decode((const unsigned char *)comma, dlen, &ilen);
710 if (!comma) {
711 zval_ptr_dtor(&meta);
712 php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "rfc2397: unable to decode");
713 return NULL;
714 }
715 } else {
716 comma = estrndup(comma, dlen);
717 ilen = dlen = php_url_decode(comma, dlen);
718 }
719
720 if ((stream = php_stream_temp_create_rel(0, ~0u)) != NULL) {
721 /* store data */
722 php_stream_temp_write(stream, comma, ilen TSRMLS_CC);
723 php_stream_temp_seek(stream, 0, SEEK_SET, &newoffs TSRMLS_CC);
724 /* set special stream stuff (enforce exact mode) */
725 vlen = strlen(mode);
726 if (vlen >= sizeof(stream->mode)) {
727 vlen = sizeof(stream->mode) - 1;
728 }
729 memcpy(stream->mode, mode, vlen);
730 stream->mode[vlen] = '\0';
731 stream->ops = &php_stream_rfc2397_ops;
732 ts = (php_stream_temp_data*)stream->abstract;
733 assert(ts != NULL);
734 ts->mode = mode && mode[0] == 'r' && mode[1] != '+' ? TEMP_STREAM_READONLY : 0;
735 ts->meta = meta;
736 }
737 efree(comma);
738
739 return stream;
740 }
741
742 PHPAPI php_stream_wrapper_ops php_stream_rfc2397_wops = {
743 php_stream_url_wrap_rfc2397,
744 NULL, /* close */
745 NULL, /* fstat */
746 NULL, /* stat */
747 NULL, /* opendir */
748 "RFC2397",
749 NULL, /* unlink */
750 NULL, /* rename */
751 NULL, /* mkdir */
752 NULL /* rmdir */
753 };
754
755 PHPAPI php_stream_wrapper php_stream_rfc2397_wrapper = {
756 &php_stream_rfc2397_wops,
757 NULL,
758 1, /* is_url */
759 };
760
761 /*
762 * Local variables:
763 * tab-width: 4
764 * c-basic-offset: 4
765 * End:
766 * vim600: noet sw=4 ts=4 fdm=marker
767 * vim<600: noet sw=4 ts=4
768 */
769