Coverage Report

Created: 2025-07-23 06:33

/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
5.80k
{
47
5.80k
  php_stream_memory_data *ms = (php_stream_memory_data*)stream->abstract;
48
5.80k
  assert(ms != NULL);
49
50
5.80k
  if (ms->mode & TEMP_STREAM_READONLY) {
51
0
    return (ssize_t) -1;
52
0
  }
53
5.80k
  size_t data_len = ZSTR_LEN(ms->data);
54
5.80k
  if (ms->mode & TEMP_STREAM_APPEND) {
55
0
    ms->fpos = data_len;
56
0
  }
57
5.80k
  if (ms->fpos + count > data_len) {
58
5.80k
    ms->data = zend_string_realloc(ms->data, ms->fpos + count, 0);
59
5.80k
    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
5.80k
  } else {
64
0
    ms->data = zend_string_separate(ms->data, 0);
65
0
  }
66
5.80k
  if (count) {
67
5.80k
    ZEND_ASSERT(buf != NULL);
68
5.80k
    memcpy(ZSTR_VAL(ms->data) + ms->fpos, (char*) buf, count);
69
5.80k
    ZSTR_VAL(ms->data)[ZSTR_LEN(ms->data)] = '\0';
70
5.80k
    ms->fpos += count;
71
5.80k
  }
72
5.80k
  return count;
73
5.80k
}
74
/* }}} */
75
76
77
/* {{{ */
78
static ssize_t php_stream_memory_read(php_stream *stream, char *buf, size_t count)
79
1.89M
{
80
1.89M
  php_stream_memory_data *ms = (php_stream_memory_data*)stream->abstract;
81
1.89M
  assert(ms != NULL);
82
83
1.89M
  if (ms->fpos >= ZSTR_LEN(ms->data)) {
84
1.01k
    stream->eof = 1;
85
1.01k
    count = 0;
86
1.89M
  } else {
87
1.89M
    if (ms->fpos + count > ZSTR_LEN(ms->data)) {
88
119
      count = ZSTR_LEN(ms->data) - ms->fpos;
89
119
    }
90
1.89M
    if (count) {
91
1.89M
      ZEND_ASSERT(buf != NULL);
92
1.89M
      memcpy(buf, ZSTR_VAL(ms->data) + ms->fpos, count);
93
1.89M
      ms->fpos += count;
94
1.89M
    }
95
1.89M
  }
96
1.89M
  return count;
97
1.89M
}
98
/* }}} */
99
100
101
/* {{{ */
102
static int php_stream_memory_close(php_stream *stream, int close_handle)
103
5.81k
{
104
5.81k
  php_stream_memory_data *ms = (php_stream_memory_data*)stream->abstract;
105
5.81k
  ZEND_ASSERT(ms != NULL);
106
5.81k
  zend_string_release(ms->data);
107
5.81k
  efree(ms);
108
5.81k
  return 0;
109
5.81k
}
110
/* }}} */
111
112
113
/* {{{ */
114
static int php_stream_memory_flush(php_stream *stream)
115
5.80k
{
116
  /* nothing to do here */
117
5.80k
  return 0;
118
5.80k
}
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
259k
{
125
259k
  php_stream_memory_data *ms = (php_stream_memory_data*)stream->abstract;
126
259k
  assert(ms != NULL);
127
128
259k
  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
253k
    case SEEK_SET:
150
253k
      if (offset < 0) {
151
0
        ms->fpos = 0;
152
0
        *newoffs = -1;
153
0
        return -1;
154
253k
      } else {
155
253k
        ms->fpos = offset;
156
253k
        *newoffs = ms->fpos;
157
253k
        stream->eof = 0;
158
253k
        stream->fatal_error = 0;
159
253k
        return 0;
160
253k
      }
161
5.80k
    case SEEK_END:
162
5.80k
      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
5.80k
      } else if (ZSTR_LEN(ms->data) < (size_t)(-offset)) {
169
0
        ms->fpos = 0;
170
0
        *newoffs = -1;
171
0
        return -1;
172
5.80k
      } else {
173
5.80k
        ms->fpos = ZSTR_LEN(ms->data) + offset;
174
5.80k
        *newoffs = ms->fpos;
175
5.80k
        stream->eof = 0;
176
5.80k
        stream->fatal_error = 0;
177
5.80k
        return 0;
178
5.80k
      }
179
0
    default:
180
0
      *newoffs = ms->fpos;
181
0
      return -1;
182
259k
  }
183
259k
}
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
#ifndef PHP_WIN32
217
0
  ssb->sb.st_blksize = -1;
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
            ZSTR_VAL(ms->data)[ZSTR_LEN(ms->data)] = '\0';
251
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
5
{
273
5
  if (strpbrk(mode, "a")) {
274
0
    return TEMP_STREAM_APPEND;
275
5
  } else if (strpbrk(mode, "w+")) {
276
5
    return TEMP_STREAM_DEFAULT;
277
5
  }
278
0
  return TEMP_STREAM_READONLY;
279
5
}
280
/* }}} */
281
282
/* {{{ */
283
PHPAPI const char *_php_stream_mode_to_str(int mode)
284
5.81k
{
285
5.81k
  if (mode == TEMP_STREAM_READONLY) {
286
0
    return "rb";
287
5.81k
  } else if (mode == TEMP_STREAM_APPEND) {
288
0
    return "a+b";
289
0
  }
290
5.81k
  return "w+b";
291
5.81k
}
292
/* }}} */
293
294
/* {{{ */
295
PHPAPI php_stream *_php_stream_memory_create(int mode STREAMS_DC)
296
5.81k
{
297
5.81k
  php_stream_memory_data *self;
298
5.81k
  php_stream *stream;
299
300
5.81k
  self = emalloc(sizeof(*self));
301
5.81k
  self->data = ZSTR_EMPTY_ALLOC();
302
5.81k
  self->fpos = 0;
303
5.81k
  self->mode = mode;
304
305
5.81k
  stream = php_stream_alloc_rel(&php_stream_memory_ops, self, 0, _php_stream_mode_to_str(mode));
306
5.81k
  stream->flags |= PHP_STREAM_FLAG_NO_BUFFER;
307
5.81k
  return stream;
308
5.81k
}
309
/* }}} */
310
311
312
/* {{{ */
313
PHPAPI php_stream *_php_stream_memory_open(int mode, zend_string *buf STREAMS_DC)
314
0
{
315
0
  php_stream *stream;
316
0
  php_stream_memory_data *ms;
317
318
0
  if ((stream = php_stream_memory_create_rel(mode)) != NULL) {
319
0
    ms = (php_stream_memory_data*)stream->abstract;
320
0
    ms->data = zend_string_copy(buf);
321
0
  }
322
0
  return stream;
323
0
}
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_error_docref(NULL, E_WARNING, "Unable to create temporary file, Check permissions in temporary files directory.");
366
0
        return 0;
367
0
      }
368
0
      php_stream_write(file, ZSTR_VAL(membuf), ZSTR_LEN(membuf));
369
0
      php_stream_free_enclosed(ts->innerstream, PHP_STREAM_FREE_CLOSE);
370
0
      ts->innerstream = file;
371
0
      php_stream_encloses(stream, ts->innerstream);
372
0
      php_stream_seek(ts->innerstream, pos, SEEK_SET);
373
0
    }
374
0
  }
375
0
  return php_stream_write(ts->innerstream, buf, count);
376
0
}
377
/* }}} */
378
379
380
/* {{{ */
381
static ssize_t php_stream_temp_read(php_stream *stream, char *buf, size_t count)
382
0
{
383
0
  php_stream_temp_data *ts = (php_stream_temp_data*)stream->abstract;
384
0
  size_t got;
385
386
0
  assert(ts != NULL);
387
388
0
  if (!ts->innerstream) {
389
0
    return -1;
390
0
  }
391
392
0
  got = php_stream_read(ts->innerstream, buf, count);
393
394
0
  stream->eof = ts->innerstream->eof;
395
396
0
  return got;
397
0
}
398
/* }}} */
399
400
401
/* {{{ */
402
static int php_stream_temp_close(php_stream *stream, int close_handle)
403
5
{
404
5
  php_stream_temp_data *ts = (php_stream_temp_data*)stream->abstract;
405
5
  int ret;
406
407
5
  assert(ts != NULL);
408
409
5
  if (ts->innerstream) {
410
5
    ret = php_stream_free_enclosed(ts->innerstream, PHP_STREAM_FREE_CLOSE | (close_handle ? 0 : PHP_STREAM_FREE_PRESERVE_HANDLE));
411
5
  } else {
412
0
    ret = 0;
413
0
  }
414
415
5
  zval_ptr_dtor(&ts->meta);
416
417
5
  if (ts->tmpdir) {
418
0
    efree(ts->tmpdir);
419
0
  }
420
421
5
  efree(ts);
422
423
5
  return ret;
424
5
}
425
/* }}} */
426
427
428
/* {{{ */
429
static int php_stream_temp_flush(php_stream *stream)
430
0
{
431
0
  php_stream_temp_data *ts = (php_stream_temp_data*)stream->abstract;
432
0
  assert(ts != NULL);
433
434
0
  return ts->innerstream ? php_stream_flush(ts->innerstream) : -1;
435
0
}
436
/* }}} */
437
438
439
/* {{{ */
440
static int php_stream_temp_seek(php_stream *stream, zend_off_t offset, int whence, zend_off_t *newoffs)
441
0
{
442
0
  php_stream_temp_data *ts = (php_stream_temp_data*)stream->abstract;
443
0
  int ret;
444
445
0
  assert(ts != NULL);
446
447
0
  if (!ts->innerstream) {
448
0
    *newoffs = -1;
449
0
    return -1;
450
0
  }
451
0
  ret = php_stream_seek(ts->innerstream, offset, whence);
452
0
  *newoffs = php_stream_tell(ts->innerstream);
453
0
  stream->eof = ts->innerstream->eof;
454
455
0
  return ret;
456
0
}
457
/* }}} */
458
459
/* {{{ */
460
static int php_stream_temp_cast(php_stream *stream, int castas, void **ret)
461
0
{
462
0
  php_stream_temp_data *ts = (php_stream_temp_data*)stream->abstract;
463
0
  php_stream *file;
464
0
  zend_string *membuf;
465
0
  zend_off_t pos;
466
467
0
  assert(ts != NULL);
468
469
0
  if (!ts->innerstream) {
470
0
    return FAILURE;
471
0
  }
472
0
  if (php_stream_is(ts->innerstream, PHP_STREAM_IS_STDIO)) {
473
0
    return php_stream_cast(ts->innerstream, castas, ret, 0);
474
0
  }
475
476
  /* we are still using a memory based backing. If they are if we can be
477
   * a FILE*, say yes because we can perform the conversion.
478
   * If they actually want to perform the conversion, we need to switch
479
   * the memory stream to a tmpfile stream */
480
481
0
  if (ret == NULL && castas == PHP_STREAM_AS_STDIO) {
482
0
    return SUCCESS;
483
0
  }
484
485
  /* say "no" to other stream forms */
486
0
  if (ret == NULL) {
487
0
    return FAILURE;
488
0
  }
489
490
0
  file = php_stream_fopen_tmpfile();
491
0
  if (file == NULL) {
492
0
    php_error_docref(NULL, E_WARNING, "Unable to create temporary file.");
493
0
    return FAILURE;
494
0
  }
495
496
  /* perform the conversion and then pass the request on to the innerstream */
497
0
  membuf = php_stream_memory_get_buffer(ts->innerstream);
498
0
  php_stream_write(file, ZSTR_VAL(membuf), ZSTR_LEN(membuf));
499
0
  pos = php_stream_tell(ts->innerstream);
500
501
0
  php_stream_free_enclosed(ts->innerstream, PHP_STREAM_FREE_CLOSE);
502
0
  ts->innerstream = file;
503
0
  php_stream_encloses(stream, ts->innerstream);
504
0
  php_stream_seek(ts->innerstream, pos, SEEK_SET);
505
506
0
  return php_stream_cast(ts->innerstream, castas, ret, 1);
507
0
}
508
/* }}} */
509
510
static int php_stream_temp_stat(php_stream *stream, php_stream_statbuf *ssb) /* {{{ */
511
0
{
512
0
  php_stream_temp_data *ts = (php_stream_temp_data*)stream->abstract;
513
514
0
  if (!ts || !ts->innerstream) {
515
0
    return -1;
516
0
  }
517
0
  return php_stream_stat(ts->innerstream, ssb);
518
0
}
519
/* }}} */
520
521
static int php_stream_temp_set_option(php_stream *stream, int option, int value, void *ptrparam) /* {{{ */
522
0
{
523
0
  php_stream_temp_data *ts = (php_stream_temp_data*)stream->abstract;
524
525
0
  switch(option) {
526
0
    case PHP_STREAM_OPTION_META_DATA_API:
527
0
      if (Z_TYPE(ts->meta) != IS_UNDEF) {
528
0
        zend_hash_copy(Z_ARRVAL_P((zval*)ptrparam), Z_ARRVAL(ts->meta), zval_add_ref);
529
0
      }
530
0
      return PHP_STREAM_OPTION_RETURN_OK;
531
0
    default:
532
0
      if (ts->innerstream) {
533
0
        return php_stream_set_option(ts->innerstream, option, value, ptrparam);
534
0
      }
535
0
      return PHP_STREAM_OPTION_RETURN_NOTIMPL;
536
0
  }
537
0
}
538
/* }}} */
539
540
PHPAPI const php_stream_ops php_stream_temp_ops = {
541
  php_stream_temp_write, php_stream_temp_read,
542
  php_stream_temp_close, php_stream_temp_flush,
543
  "TEMP",
544
  php_stream_temp_seek,
545
  php_stream_temp_cast,
546
  php_stream_temp_stat,
547
  php_stream_temp_set_option
548
};
549
550
/* }}} */
551
552
/* {{{ _php_stream_temp_create_ex */
553
PHPAPI php_stream *_php_stream_temp_create_ex(int mode, size_t max_memory_usage, const char *tmpdir STREAMS_DC)
554
5
{
555
5
  php_stream_temp_data *self;
556
5
  php_stream *stream;
557
558
5
  self = ecalloc(1, sizeof(*self));
559
5
  self->smax = max_memory_usage;
560
5
  self->mode = mode;
561
5
  ZVAL_UNDEF(&self->meta);
562
5
  if (tmpdir) {
563
0
    self->tmpdir = estrdup(tmpdir);
564
0
  }
565
5
  stream = php_stream_alloc_rel(&php_stream_temp_ops, self, 0, _php_stream_mode_to_str(mode));
566
5
  stream->flags |= PHP_STREAM_FLAG_NO_BUFFER;
567
5
  self->innerstream = php_stream_memory_create_rel(mode);
568
5
  php_stream_encloses(stream, self->innerstream);
569
570
5
  return stream;
571
5
}
572
/* }}} */
573
574
/* {{{ _php_stream_temp_create */
575
PHPAPI php_stream *_php_stream_temp_create(int mode, size_t max_memory_usage STREAMS_DC)
576
5
{
577
5
  return php_stream_temp_create_ex(mode, max_memory_usage, NULL);
578
5
}
579
/* }}} */
580
581
/* {{{ _php_stream_temp_open */
582
PHPAPI php_stream *_php_stream_temp_open(int mode, size_t max_memory_usage, const char *buf, size_t length STREAMS_DC)
583
0
{
584
0
  php_stream *stream;
585
0
  php_stream_temp_data *ts;
586
0
  zend_off_t newoffs;
587
588
0
  if ((stream = php_stream_temp_create_rel(mode, max_memory_usage)) != NULL) {
589
0
    if (length) {
590
0
      assert(buf != NULL);
591
0
      php_stream_temp_write(stream, buf, length);
592
0
      php_stream_temp_seek(stream, 0, SEEK_SET, &newoffs);
593
0
    }
594
0
    ts = (php_stream_temp_data*)stream->abstract;
595
0
    assert(ts != NULL);
596
0
    ts->mode = mode;
597
0
  }
598
0
  return stream;
599
0
}
600
/* }}} */
601
602
PHPAPI const php_stream_ops php_stream_rfc2397_ops = {
603
  NULL, php_stream_temp_read,
604
  php_stream_temp_close, php_stream_temp_flush,
605
  "RFC2397",
606
  php_stream_temp_seek,
607
  php_stream_temp_cast,
608
  php_stream_temp_stat,
609
  php_stream_temp_set_option
610
};
611
612
static php_stream * php_stream_url_wrap_rfc2397(php_stream_wrapper *wrapper, const char *path,
613
                        const char *mode, int options, zend_string **opened_path,
614
                        php_stream_context *context STREAMS_DC) /* {{{ */
615
0
{
616
0
  php_stream *stream;
617
0
  php_stream_temp_data *ts;
618
0
  char *comma, *semi, *sep;
619
0
  size_t mlen, dlen, plen, vlen, ilen;
620
0
  zend_off_t newoffs;
621
0
  zval meta;
622
0
  int base64 = 0;
623
0
  zend_string *base64_comma = NULL;
624
625
0
  ZEND_ASSERT(mode);
626
627
0
  ZVAL_NULL(&meta);
628
0
  if (memcmp(path, "data:", 5)) {
629
0
    return NULL;
630
0
  }
631
632
0
  path += 5;
633
0
  dlen = strlen(path);
634
635
0
  if (dlen >= 2 && path[0] == '/' && path[1] == '/') {
636
0
    dlen -= 2;
637
0
    path += 2;
638
0
  }
639
640
0
  if ((comma = memchr(path, ',', dlen)) == NULL) {
641
0
    php_stream_wrapper_log_error(wrapper, options, "rfc2397: no comma in URL");
642
0
    return NULL;
643
0
  }
644
645
0
  if (comma != path) {
646
    /* meta info */
647
0
    mlen = comma - path;
648
0
    dlen -= mlen;
649
0
    semi = memchr(path, ';', mlen);
650
0
    sep = memchr(path, '/', mlen);
651
652
0
    if (!semi && !sep) {
653
0
      php_stream_wrapper_log_error(wrapper, options, "rfc2397: illegal media type");
654
0
      return NULL;
655
0
    }
656
657
0
    array_init(&meta);
658
0
    if (!semi) { /* there is only a mime type */
659
0
      add_assoc_stringl(&meta, "mediatype", (char *) path, mlen);
660
0
      mlen = 0;
661
0
    } else if (sep && sep < semi) { /* there is a mime type */
662
0
      plen = semi - path;
663
0
      add_assoc_stringl(&meta, "mediatype", (char *) path, plen);
664
0
      mlen -= plen;
665
0
      path += plen;
666
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 */
667
0
      zval_ptr_dtor(&meta);
668
0
      php_stream_wrapper_log_error(wrapper, options, "rfc2397: illegal media type");
669
0
      return NULL;
670
0
    }
671
    /* get parameters and potentially ';base64' */
672
0
    while(semi && (semi == path)) {
673
0
      path++;
674
0
      mlen--;
675
0
      sep = memchr(path, '=', mlen);
676
0
      semi = memchr(path, ';', mlen);
677
0
      if (!sep || (semi && semi < sep)) { /* must be ';base64' or failure */
678
0
        if (mlen != sizeof("base64")-1 || memcmp(path, "base64", sizeof("base64")-1)) {
679
          /* must be error since parameters are only allowed after mediatype and we have no '=' sign */
680
0
          zval_ptr_dtor(&meta);
681
0
          php_stream_wrapper_log_error(wrapper, options, "rfc2397: illegal parameter");
682
0
          return NULL;
683
0
        }
684
0
        base64 = 1;
685
0
        mlen -= sizeof("base64") - 1;
686
0
        path += sizeof("base64") - 1;
687
0
        break;
688
0
      }
689
      /* found parameter ... the heart of cs ppl lies in +1/-1 or was it +2 this time? */
690
0
      plen = sep - path;
691
0
      vlen = (semi ? (size_t)(semi - sep) : (mlen - plen)) - 1 /* '=' */;
692
0
      if (plen != sizeof("mediatype")-1 || memcmp(path, "mediatype", sizeof("mediatype")-1)) {
693
0
        add_assoc_stringl_ex(&meta, path, plen, sep + 1, vlen);
694
0
      }
695
0
      plen += vlen + 1;
696
0
      mlen -= plen;
697
0
      path += plen;
698
0
    }
699
0
    if (mlen) {
700
0
      zval_ptr_dtor(&meta);
701
0
      php_stream_wrapper_log_error(wrapper, options, "rfc2397: illegal URL");
702
0
      return NULL;
703
0
    }
704
0
  } else {
705
0
    array_init(&meta);
706
0
  }
707
0
  add_assoc_bool(&meta, "base64", base64);
708
709
  /* skip ',' */
710
0
  comma++;
711
0
  dlen--;
712
713
0
  if (base64) {
714
0
    base64_comma = php_base64_decode_ex((const unsigned char *)comma, dlen, 1);
715
0
    if (!base64_comma) {
716
0
      zval_ptr_dtor(&meta);
717
0
      php_stream_wrapper_log_error(wrapper, options, "rfc2397: unable to decode");
718
0
      return NULL;
719
0
    }
720
0
    comma = ZSTR_VAL(base64_comma);
721
0
    ilen = ZSTR_LEN(base64_comma);
722
0
  } else {
723
0
    comma = estrndup(comma, dlen);
724
0
    dlen = php_url_decode(comma, dlen);
725
0
    ilen = dlen;
726
0
  }
727
728
0
  if ((stream = php_stream_temp_create_rel(0, ~0u)) != NULL) {
729
    /* store data */
730
0
    php_stream_temp_write(stream, comma, ilen);
731
0
    php_stream_temp_seek(stream, 0, SEEK_SET, &newoffs);
732
    /* set special stream stuff (enforce exact mode) */
733
0
    vlen = strlen(mode);
734
0
    if (vlen >= sizeof(stream->mode)) {
735
0
      vlen = sizeof(stream->mode) - 1;
736
0
    }
737
0
    memcpy(stream->mode, mode, vlen);
738
0
    stream->mode[vlen] = '\0';
739
0
    stream->ops = &php_stream_rfc2397_ops;
740
0
    ts = (php_stream_temp_data*)stream->abstract;
741
0
    assert(ts != NULL);
742
0
    ts->mode = mode[0] == 'r' && mode[1] != '+' ? TEMP_STREAM_READONLY : 0;
743
0
    ZVAL_COPY_VALUE(&ts->meta, &meta);
744
0
  }
745
0
  if (base64_comma) {
746
0
    zend_string_free(base64_comma);
747
0
  } else {
748
0
    efree(comma);
749
0
  }
750
751
0
  return stream;
752
0
}
753
754
PHPAPI const php_stream_wrapper_ops php_stream_rfc2397_wops = {
755
  php_stream_url_wrap_rfc2397,
756
  NULL, /* close */
757
  NULL, /* fstat */
758
  NULL, /* stat */
759
  NULL, /* opendir */
760
  "RFC2397",
761
  NULL, /* unlink */
762
  NULL, /* rename */
763
  NULL, /* mkdir */
764
  NULL, /* rmdir */
765
  NULL, /* stream_metadata */
766
};
767
768
PHPAPI const php_stream_wrapper php_stream_rfc2397_wrapper =  {
769
  &php_stream_rfc2397_wops,
770
  NULL,
771
  1, /* is_url */
772
};