Coverage Report

Created: 2026-10-28 06:20

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