Coverage Report

Created: 2026-02-14 06:52

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