Coverage Report

Created: 2025-06-13 06:43

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