Coverage Report

Created: 2026-07-25 06:39

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