Coverage Report

Created: 2026-09-14 06:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/php-src/ext/standard/image.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
   | Authors: Rasmus Lerdorf <rasmus@php.net>                             |
12
   |          Marcus Boerger <helly@php.net>                              |
13
   +----------------------------------------------------------------------+
14
 */
15
16
#include "php.h"
17
#include <stdio.h>
18
#ifdef HAVE_FCNTL_H
19
#include <fcntl.h>
20
#endif
21
#include "fopen_wrappers.h"
22
#include "ext/standard/fsock.h"
23
#include "libavifinfo/avifinfo.h"
24
#ifdef HAVE_UNISTD_H
25
#include <unistd.h>
26
#endif
27
#include "php_image.h"
28
29
#if defined(HAVE_ZLIB) && !defined(COMPILE_DL_ZLIB)
30
#include "zlib.h"
31
#endif
32
33
/* file type markers */
34
PHPAPI const char php_sig_gif[3] = {'G', 'I', 'F'};
35
PHPAPI const char php_sig_psd[4] = {'8', 'B', 'P', 'S'};
36
PHPAPI const char php_sig_bmp[2] = {'B', 'M'};
37
PHPAPI const char php_sig_swf[3] = {'F', 'W', 'S'};
38
PHPAPI const char php_sig_swc[3] = {'C', 'W', 'S'};
39
PHPAPI const char php_sig_jpg[3] = {(char) 0xff, (char) 0xd8, (char) 0xff};
40
PHPAPI const char php_sig_png[8] = {(char) 0x89, (char) 0x50, (char) 0x4e, (char) 0x47,
41
                                    (char) 0x0d, (char) 0x0a, (char) 0x1a, (char) 0x0a};
42
PHPAPI const char php_sig_tif_ii[4] = {'I','I', (char)0x2A, (char)0x00};
43
PHPAPI const char php_sig_tif_mm[4] = {'M','M', (char)0x00, (char)0x2A};
44
PHPAPI const char php_sig_jpc[3]  = {(char)0xff, (char)0x4f, (char)0xff};
45
PHPAPI const char php_sig_jp2[12] = {(char)0x00, (char)0x00, (char)0x00, (char)0x0c,
46
                                     (char)0x6a, (char)0x50, (char)0x20, (char)0x20,
47
                                     (char)0x0d, (char)0x0a, (char)0x87, (char)0x0a};
48
PHPAPI const char php_sig_iff[4] = {'F','O','R','M'};
49
PHPAPI const char php_sig_ico[4] = {(char)0x00, (char)0x00, (char)0x01, (char)0x00};
50
PHPAPI const char php_sig_riff[4] = {'R', 'I', 'F', 'F'};
51
PHPAPI const char php_sig_webp[4] = {'W', 'E', 'B', 'P'};
52
PHPAPI const char php_sig_ftyp[4] = {'f', 't', 'y', 'p'};
53
PHPAPI const char php_sig_mif1[4] = {'m', 'i', 'f', '1'};
54
PHPAPI const char php_sig_heic[4] = {'h', 'e', 'i', 'c'};
55
PHPAPI const char php_sig_heix[4] = {'h', 'e', 'i', 'x'};
56
57
static zend_array php_image_handlers;
58
static int php_image_handler_next_id = IMAGE_FILETYPE_FIXED_COUNT;
59
60
/* REMEMBER TO ADD MIME-TYPE TO FUNCTION php_image_type_to_mime_type */
61
/* PCX must check first 64bytes and byte 0=0x0a and byte2 < 0x06 */
62
63
/* {{{ php_handle_gif
64
 * routine to handle GIF files. If only everything were that easy... ;} */
65
static struct php_gfxinfo *php_handle_gif (php_stream * stream)
66
0
{
67
0
  struct php_gfxinfo *result = NULL;
68
0
  unsigned char dim[5];
69
70
0
  if (php_stream_seek(stream, 3, SEEK_CUR))
71
0
    return NULL;
72
73
0
  if (php_stream_read(stream, (char*)dim, sizeof(dim)) != sizeof(dim))
74
0
    return NULL;
75
76
0
  result = (struct php_gfxinfo *) ecalloc(1, sizeof(struct php_gfxinfo));
77
0
  result->width    = (unsigned int)dim[0] | (((unsigned int)dim[1])<<8);
78
0
  result->height   = (unsigned int)dim[2] | (((unsigned int)dim[3])<<8);
79
0
  result->bits     = dim[4]&0x80 ? ((((unsigned int)dim[4])&0x07) + 1) : 0;
80
0
  result->channels = 3; /* always */
81
82
0
  return result;
83
0
}
84
/* }}} */
85
86
/* {{{ php_handle_psd */
87
static struct php_gfxinfo *php_handle_psd (php_stream * stream)
88
0
{
89
0
  struct php_gfxinfo *result = NULL;
90
0
  unsigned char dim[8];
91
92
0
  if (php_stream_seek(stream, 11, SEEK_CUR))
93
0
    return NULL;
94
95
0
  if (php_stream_read(stream, (char*)dim, sizeof(dim)) != sizeof(dim))
96
0
    return NULL;
97
98
0
  result = (struct php_gfxinfo *) ecalloc(1, sizeof(struct php_gfxinfo));
99
0
  result->height   =  (((unsigned int)dim[0]) << 24) + (((unsigned int)dim[1]) << 16) + (((unsigned int)dim[2]) << 8) + ((unsigned int)dim[3]);
100
0
  result->width    =  (((unsigned int)dim[4]) << 24) + (((unsigned int)dim[5]) << 16) + (((unsigned int)dim[6]) << 8) + ((unsigned int)dim[7]);
101
102
0
  return result;
103
0
}
104
/* }}} */
105
106
/* {{{ php_handle_bmp */
107
static struct php_gfxinfo *php_handle_bmp (php_stream * stream)
108
0
{
109
0
  struct php_gfxinfo *result = NULL;
110
0
  unsigned char dim[16];
111
0
  int size;
112
113
0
  if (php_stream_seek(stream, 11, SEEK_CUR))
114
0
    return NULL;
115
116
0
  if (php_stream_read(stream, (char*)dim, sizeof(dim)) != sizeof(dim))
117
0
    return NULL;
118
119
0
  size   = (((unsigned int)dim[ 3]) << 24) + (((unsigned int)dim[ 2]) << 16) + (((unsigned int)dim[ 1]) << 8) + ((unsigned int) dim[ 0]);
120
0
  if (size == 12) {
121
0
    result = (struct php_gfxinfo *) ecalloc (1, sizeof(struct php_gfxinfo));
122
0
    result->width    =  (((unsigned int)dim[ 5]) << 8) + ((unsigned int) dim[ 4]);
123
0
    result->height   =  (((unsigned int)dim[ 7]) << 8) + ((unsigned int) dim[ 6]);
124
0
    result->bits     =  ((unsigned int)dim[11]);
125
0
  } else if (size > 12 && (size <= 64 || size == 108 || size == 124)) {
126
0
    result = (struct php_gfxinfo *) ecalloc (1, sizeof(struct php_gfxinfo));
127
0
    result->width    =  (((unsigned int)dim[ 7]) << 24) + (((unsigned int)dim[ 6]) << 16) + (((unsigned int)dim[ 5]) << 8) + ((unsigned int) dim[ 4]);
128
0
    result->height   =  (((unsigned int)dim[11]) << 24) + (((unsigned int)dim[10]) << 16) + (((unsigned int)dim[ 9]) << 8) + ((unsigned int) dim[ 8]);
129
0
    result->height   =  abs((int32_t)result->height);
130
0
    result->bits     =  (((unsigned int)dim[15]) <<  8) +  ((unsigned int)dim[14]);
131
0
  } else {
132
0
    return NULL;
133
0
  }
134
135
0
  return result;
136
0
}
137
/* }}} */
138
139
/* {{{ php_swf_get_bits
140
 * routines to handle SWF files. */
141
static unsigned long int php_swf_get_bits (unsigned char* buffer, unsigned int pos, unsigned int count)
142
10
{
143
10
  unsigned int loop;
144
10
  unsigned long int result = 0;
145
146
164
  for (loop = pos; loop < pos + count; loop++)
147
154
  {
148
154
    result = result +
149
154
      ((((buffer[loop / 8]) >> (7 - (loop % 8))) & 0x01) << (count - (loop - pos) - 1));
150
154
  }
151
10
  return result;
152
10
}
153
/* }}} */
154
155
#if defined(HAVE_ZLIB) && !defined(COMPILE_DL_ZLIB)
156
/* {{{ php_handle_swc */
157
static struct php_gfxinfo *php_handle_swc(php_stream * stream)
158
{
159
  struct php_gfxinfo *result = NULL;
160
161
  long bits;
162
  unsigned char a[64];
163
  unsigned long len=64, szlength;
164
  int factor = 1,maxfactor = 1 << 15;
165
  int status = 0;
166
  unsigned char *b, *buf = NULL;
167
  zend_string *bufz;
168
169
  if (php_stream_seek(stream, 5, SEEK_CUR)) {
170
    return NULL;
171
  }
172
173
  if (php_stream_read(stream, (char *) a, sizeof(a)) != sizeof(a)) {
174
    return NULL;
175
  }
176
177
  b = ecalloc(1, len + 1);
178
179
  if (uncompress(b, &len, a, sizeof(a)) != Z_OK) {
180
    /* failed to decompress the file, will try reading the rest of the file */
181
    if (php_stream_seek(stream, 8, SEEK_SET)) {
182
      efree(b);
183
      return NULL;
184
    }
185
186
    bufz = php_stream_copy_to_mem(stream, PHP_STREAM_COPY_ALL, 0);
187
188
    if (!bufz) {
189
      efree(b);
190
      return NULL;
191
    }
192
193
    /*
194
     * zlib::uncompress() wants to know the output data length
195
     * if none was given as a parameter
196
     * we try from input length * 2 up to input length * 2^15
197
     * doubling it whenever it wasn't big enough
198
     * that should be enough for all real life cases
199
    */
200
201
    do {
202
      factor <<= 1;
203
      if (ZSTR_LEN(bufz) > ULONG_MAX / factor) {
204
        status = Z_MEM_ERROR;
205
        break;
206
      }
207
      szlength = (unsigned long) (ZSTR_LEN(bufz) * factor);
208
      buf = erealloc(buf, szlength);
209
      status = uncompress(buf, &szlength, (unsigned char *) ZSTR_VAL(bufz), (unsigned long) ZSTR_LEN(bufz));
210
    } while ((status==Z_BUF_ERROR)&&(factor<maxfactor));
211
212
    if (bufz) {
213
      zend_string_release_ex(bufz, 0);
214
    }
215
216
    if (status == Z_OK) {
217
       memcpy(b, buf, len);
218
    }
219
220
    if (buf) {
221
      efree(buf);
222
    }
223
  }
224
225
  if (!status) {
226
    result = (struct php_gfxinfo *) ecalloc (1, sizeof (struct php_gfxinfo));
227
    bits = php_swf_get_bits (b, 0, 5);
228
    result->width = (php_swf_get_bits (b, 5 + bits, bits) -
229
      php_swf_get_bits (b, 5, bits)) / 20;
230
    result->height = (php_swf_get_bits (b, 5 + (3 * bits), bits) -
231
      php_swf_get_bits (b, 5 + (2 * bits), bits)) / 20;
232
  } else {
233
    result = NULL;
234
  }
235
236
  efree (b);
237
  return result;
238
}
239
/* }}} */
240
#endif
241
242
/* {{{ php_handle_swf */
243
static struct php_gfxinfo *php_handle_swf (php_stream * stream)
244
3
{
245
3
  struct php_gfxinfo *result = NULL;
246
3
  long bits;
247
3
  unsigned char a[32];
248
249
3
  if (php_stream_seek(stream, 5, SEEK_CUR))
250
0
    return NULL;
251
252
3
  if (php_stream_read(stream, (char*)a, sizeof(a)) != sizeof(a))
253
1
    return NULL;
254
255
2
  result = (struct php_gfxinfo *) ecalloc (1, sizeof (struct php_gfxinfo));
256
2
  bits = php_swf_get_bits (a, 0, 5);
257
2
  result->width = (php_swf_get_bits (a, 5 + bits, bits) -
258
2
    php_swf_get_bits (a, 5, bits)) / 20;
259
2
  result->height = (php_swf_get_bits (a, 5 + (3 * bits), bits) -
260
2
    php_swf_get_bits (a, 5 + (2 * bits), bits)) / 20;
261
2
  result->bits     = 0;
262
2
  result->channels = 0;
263
2
  return result;
264
3
}
265
/* }}} */
266
267
/* {{{ php_handle_png
268
 * routine to handle PNG files */
269
static struct php_gfxinfo *php_handle_png (php_stream * stream)
270
0
{
271
0
  struct php_gfxinfo *result = NULL;
272
0
  unsigned char dim[9];
273
/* Width:              4 bytes
274
 * Height:             4 bytes
275
 * Bit depth:          1 byte
276
 * Color type:         1 byte
277
 * Compression method: 1 byte
278
 * Filter method:      1 byte
279
 * Interlace method:   1 byte
280
 */
281
282
0
  if (php_stream_seek(stream, 8, SEEK_CUR))
283
0
    return NULL;
284
285
0
  if((php_stream_read(stream, (char*)dim, sizeof(dim))) < sizeof(dim))
286
0
    return NULL;
287
288
0
  result = (struct php_gfxinfo *) ecalloc(1, sizeof(struct php_gfxinfo));
289
0
  result->width  = (((unsigned int)dim[0]) << 24) + (((unsigned int)dim[1]) << 16) + (((unsigned int)dim[2]) << 8) + ((unsigned int)dim[3]);
290
0
  result->height = (((unsigned int)dim[4]) << 24) + (((unsigned int)dim[5]) << 16) + (((unsigned int)dim[6]) << 8) + ((unsigned int)dim[7]);
291
0
  result->bits   = (unsigned int)dim[8];
292
0
  return result;
293
0
}
294
/* }}} */
295
296
/* routines to handle JPEG data */
297
298
/* some defines for the different JPEG block types */
299
0
#define M_SOF0  0xC0      /* Start Of Frame N */
300
0
#define M_SOF1  0xC1      /* N indicates which compression process */
301
0
#define M_SOF2  0xC2      /* Only SOF0-SOF2 are now in common use */
302
0
#define M_SOF3  0xC3
303
0
#define M_SOF5  0xC5      /* NB: codes C4 and CC are NOT SOF markers */
304
0
#define M_SOF6  0xC6
305
0
#define M_SOF7  0xC7
306
0
#define M_SOF9  0xC9
307
0
#define M_SOF10 0xCA
308
0
#define M_SOF11 0xCB
309
0
#define M_SOF13 0xCD
310
0
#define M_SOF14 0xCE
311
0
#define M_SOF15 0xCF
312
#define M_SOI   0xD8
313
2
#define M_EOI   0xD9      /* End Of Image (end of datastream) */
314
0
#define M_SOS   0xDA      /* Start Of Scan (begins compressed data) */
315
0
#define M_APP0  0xe0
316
0
#define M_APP1  0xe1
317
0
#define M_APP2  0xe2
318
0
#define M_APP3  0xe3
319
0
#define M_APP4  0xe4
320
0
#define M_APP5  0xe5
321
0
#define M_APP6  0xe6
322
1
#define M_APP7  0xe7
323
1
#define M_APP8  0xe8
324
1
#define M_APP9  0xe9
325
1
#define M_APP10 0xea
326
1
#define M_APP11 0xeb
327
1
#define M_APP12 0xec
328
1
#define M_APP13 0xed
329
1
#define M_APP14 0xee
330
1
#define M_APP15 0xef
331
#define M_COM   0xFE            /* COMment                                  */
332
333
4
#define M_PSEUDO 0xFFD8      /* pseudo marker for start of image(byte 0) */
334
335
/* {{{ php_read2 */
336
static unsigned short php_read2(php_stream * stream)
337
7
{
338
7
  unsigned char a[2];
339
340
  /* return 0 if we couldn't read enough data */
341
7
  if((php_stream_read(stream, (char *) a, sizeof(a))) < sizeof(a)) return 0;
342
343
7
  return (((unsigned short)a[0]) << 8) + ((unsigned short)a[1]);
344
7
}
345
/* }}} */
346
347
/* {{{ php_next_marker
348
 * get next marker byte from file */
349
static unsigned int php_next_marker(php_stream * stream, int last_marker, int ff_read)
350
8
{
351
8
  int a=0, marker;
352
353
  /* get marker byte, swallowing possible padding                           */
354
8
  if (!ff_read) {
355
4
    size_t extraneous = 0;
356
357
199
    while ((marker = php_stream_getc(stream)) != 0xff) {
358
196
      if (marker == EOF) {
359
1
        return M_EOI;/* we hit EOF */
360
1
  }
361
195
      extraneous++;
362
195
  }
363
3
    if (extraneous) {
364
3
      php_error_docref(NULL, E_WARNING, "Corrupt JPEG data: %zu extraneous bytes before marker", extraneous);
365
3
    }
366
3
  }
367
7
  a = 1;
368
13
  do {
369
13
    if ((marker = php_stream_getc(stream)) == EOF)
370
0
    {
371
0
      return M_EOI;/* we hit EOF */
372
0
    }
373
13
    a++;
374
13
  } while (marker == 0xff);
375
7
  if (a < 2)
376
0
  {
377
0
    return M_EOI; /* at least one 0xff is needed before marker code */
378
0
  }
379
7
  return (unsigned int)marker;
380
7
}
381
/* }}} */
382
383
/* {{{ php_skip_variable
384
 * skip over a variable-length block; assumes proper length marker */
385
static int php_skip_variable(php_stream * stream)
386
7
{
387
7
  zend_off_t length = ((unsigned int)php_read2(stream));
388
389
7
  if (length < 2)  {
390
3
    return 0;
391
3
  }
392
4
  length = length - 2;
393
4
  php_stream_seek(stream, (zend_long)length, SEEK_CUR);
394
4
  return 1;
395
7
}
396
/* }}} */
397
398
static size_t php_read_stream_all_chunks(php_stream *stream, char *buffer, size_t length)
399
0
{
400
0
  size_t read_total = 0;
401
0
  do {
402
0
    ssize_t read_now = php_stream_read(stream, buffer, length - read_total);
403
0
    read_total += read_now;
404
0
    if (read_now < stream->chunk_size && read_total != length) {
405
0
      return 0;
406
0
    }
407
0
    buffer += read_now;
408
0
  } while (read_total < length);
409
410
0
  return read_total;
411
0
}
412
413
/* {{{ php_read_APP */
414
static int php_read_APP(php_stream * stream, unsigned int marker, zval *info)
415
0
{
416
0
  size_t length;
417
0
  char *buffer;
418
0
  char markername[16];
419
0
  zval *tmp;
420
421
0
  length = php_read2(stream);
422
0
  if (length < 2) {
423
0
    return 0;
424
0
  }
425
0
  length -= 2;        /* length includes itself */
426
427
0
  buffer = emalloc(length);
428
429
0
  if (php_read_stream_all_chunks(stream, buffer, length) != length) {
430
0
    efree(buffer);
431
0
    return 0;
432
0
  }
433
434
0
  snprintf(markername, sizeof(markername), "APP%d", marker - M_APP0);
435
436
0
  if ((tmp = zend_hash_str_find(Z_ARRVAL_P(info), markername, strlen(markername))) == NULL) {
437
    /* XXX we only catch the 1st tag of it's kind! */
438
0
    add_assoc_stringl(info, markername, buffer, length);
439
0
  }
440
441
0
  efree(buffer);
442
0
  return 1;
443
0
}
444
/* }}} */
445
446
/* {{{ php_handle_jpeg
447
   main loop to parse JPEG structure */
448
static struct php_gfxinfo *php_handle_jpeg (php_stream * stream, zval *info)
449
4
{
450
4
  struct php_gfxinfo *result = NULL;
451
4
  unsigned int marker = M_PSEUDO;
452
4
  unsigned short length, ff_read=1;
453
454
8
  for (;;) {
455
8
    marker = php_next_marker(stream, marker, ff_read);
456
8
    ff_read = 0;
457
8
    switch (marker) {
458
0
      case M_SOF0:
459
0
      case M_SOF1:
460
0
      case M_SOF2:
461
0
      case M_SOF3:
462
0
      case M_SOF5:
463
0
      case M_SOF6:
464
0
      case M_SOF7:
465
0
      case M_SOF9:
466
0
      case M_SOF10:
467
0
      case M_SOF11:
468
0
      case M_SOF13:
469
0
      case M_SOF14:
470
0
      case M_SOF15:
471
0
        if (result == NULL) {
472
          /* handle SOFn block */
473
0
          result = (struct php_gfxinfo *) ecalloc(1, sizeof(struct php_gfxinfo));
474
0
          length = php_read2(stream);
475
0
          result->bits     = php_stream_getc(stream);
476
0
          result->height   = php_read2(stream);
477
0
          result->width    = php_read2(stream);
478
0
          result->channels = php_stream_getc(stream);
479
0
          if (!info || length < 8) { /* if we don't want an extanded info -> return */
480
0
            return result;
481
0
          }
482
0
          if (php_stream_seek(stream, length - 8, SEEK_CUR)) { /* file error after info */
483
0
            return result;
484
0
          }
485
0
        } else {
486
0
          if (!php_skip_variable(stream)) {
487
0
            return result;
488
0
          }
489
0
        }
490
0
        break;
491
492
0
      case M_APP0:
493
0
      case M_APP1:
494
0
      case M_APP2:
495
0
      case M_APP3:
496
0
      case M_APP4:
497
0
      case M_APP5:
498
0
      case M_APP6:
499
1
      case M_APP7:
500
1
      case M_APP8:
501
1
      case M_APP9:
502
1
      case M_APP10:
503
1
      case M_APP11:
504
1
      case M_APP12:
505
1
      case M_APP13:
506
1
      case M_APP14:
507
1
      case M_APP15:
508
1
        if (info) {
509
0
          if (!php_read_APP(stream, marker, info)) { /* read all the app marks... */
510
0
            return result;
511
0
          }
512
1
        } else {
513
1
          if (!php_skip_variable(stream)) {
514
1
            return result;
515
1
          }
516
1
        }
517
0
        break;
518
519
0
      case M_SOS:
520
1
      case M_EOI:
521
1
        return result; /* we're about to hit image data, or are at EOF. stop processing. */
522
523
6
      default:
524
6
        if (!php_skip_variable(stream)) { /* anything else isn't interesting */
525
2
          return result;
526
2
        }
527
4
        break;
528
8
    }
529
8
  }
530
531
0
  return result; /* perhaps image broken -> no info but size */
532
4
}
533
/* }}} */
534
535
/* {{{ php_read4 */
536
static unsigned int php_read4(php_stream * stream)
537
5
{
538
5
  unsigned char a[4];
539
540
  /* just return 0 if we hit the end-of-file */
541
5
  if ((php_stream_read(stream, (char*)a, sizeof(a))) != sizeof(a)) return 0;
542
543
3
  return (((unsigned int)a[0]) << 24)
544
3
       + (((unsigned int)a[1]) << 16)
545
3
       + (((unsigned int)a[2]) <<  8)
546
3
       + (((unsigned int)a[3]));
547
5
}
548
/* }}} */
549
550
/* {{{ JPEG 2000 Marker Codes */
551
#define JPEG2000_MARKER_PREFIX 0xFF /* All marker codes start with this */
552
#define JPEG2000_MARKER_SOC 0x4F /* Start of Codestream */
553
#define JPEG2000_MARKER_SOT 0x90 /* Start of Tile part */
554
#define JPEG2000_MARKER_SOD 0x93 /* Start of Data */
555
#define JPEG2000_MARKER_EOC 0xD9 /* End of Codestream */
556
0
#define JPEG2000_MARKER_SIZ 0x51 /* Image and tile size */
557
#define JPEG2000_MARKER_COD 0x52 /* Coding style default */
558
#define JPEG2000_MARKER_COC 0x53 /* Coding style component */
559
#define JPEG2000_MARKER_RGN 0x5E /* Region of interest */
560
#define JPEG2000_MARKER_QCD 0x5C /* Quantization default */
561
#define JPEG2000_MARKER_QCC 0x5D /* Quantization component */
562
#define JPEG2000_MARKER_POC 0x5F /* Progression order change */
563
#define JPEG2000_MARKER_TLM 0x55 /* Tile-part lengths */
564
#define JPEG2000_MARKER_PLM 0x57 /* Packet length, main header */
565
#define JPEG2000_MARKER_PLT 0x58 /* Packet length, tile-part header */
566
#define JPEG2000_MARKER_PPM 0x60 /* Packed packet headers, main header */
567
#define JPEG2000_MARKER_PPT 0x61 /* Packed packet headers, tile part header */
568
#define JPEG2000_MARKER_SOP 0x91 /* Start of packet */
569
#define JPEG2000_MARKER_EPH 0x92 /* End of packet header */
570
#define JPEG2000_MARKER_CRG 0x63 /* Component registration */
571
#define JPEG2000_MARKER_COM 0x64 /* Comment */
572
/* }}} */
573
574
/* {{{ php_handle_jpc
575
   Main loop to parse JPEG2000 raw codestream structure */
576
static struct php_gfxinfo *php_handle_jpc(php_stream * stream)
577
0
{
578
0
  struct php_gfxinfo *result = NULL;
579
0
  int highest_bit_depth, bit_depth;
580
0
  unsigned char first_marker_id;
581
0
  unsigned int i;
582
583
  /* JPEG 2000 components can be vastly different from one another.
584
     Each component can be sampled at a different resolution, use
585
     a different colour space, have a separate colour depth, and
586
     be compressed totally differently! This makes giving a single
587
     "bit depth" answer somewhat problematic. For this implementation
588
     we'll use the highest depth encountered. */
589
590
  /* Get the single byte that remains after the file type identification */
591
0
  first_marker_id = php_stream_getc(stream);
592
593
  /* Ensure that this marker is SIZ (as is mandated by the standard) */
594
0
  if (first_marker_id != JPEG2000_MARKER_SIZ) {
595
0
    php_error_docref(NULL, E_WARNING, "JPEG2000 codestream corrupt(Expected SIZ marker not found after SOC)");
596
0
    return NULL;
597
0
  }
598
599
0
  result = (struct php_gfxinfo *)ecalloc(1, sizeof(struct php_gfxinfo));
600
601
0
  php_read2(stream); /* Lsiz */
602
0
  php_read2(stream); /* Rsiz */
603
0
  result->width = php_read4(stream); /* Xsiz */
604
0
  result->height = php_read4(stream); /* Ysiz */
605
606
#ifdef MBO_0
607
  php_read4(stream); /* XOsiz */
608
  php_read4(stream); /* YOsiz */
609
  php_read4(stream); /* XTsiz */
610
  php_read4(stream); /* YTsiz */
611
  php_read4(stream); /* XTOsiz */
612
  php_read4(stream); /* YTOsiz */
613
#else
614
0
  if (php_stream_seek(stream, 24, SEEK_CUR)) {
615
0
    efree(result);
616
0
    return NULL;
617
0
  }
618
0
#endif
619
620
0
  result->channels = php_read2(stream); /* Csiz */
621
0
  if ((result->channels == 0 && php_stream_eof(stream)) || result->channels > 256) {
622
0
    efree(result);
623
0
    return NULL;
624
0
  }
625
626
  /* Collect bit depth info */
627
0
  highest_bit_depth = 0;
628
0
  for (i = 0; i < result->channels; i++) {
629
0
    bit_depth = php_stream_getc(stream); /* Ssiz[i] */
630
0
    bit_depth++;
631
0
    if (bit_depth > highest_bit_depth) {
632
0
      highest_bit_depth = bit_depth;
633
0
    }
634
635
0
    php_stream_getc(stream); /* XRsiz[i] */
636
0
    php_stream_getc(stream); /* YRsiz[i] */
637
0
  }
638
639
0
  result->bits = highest_bit_depth;
640
641
0
  return result;
642
0
}
643
/* }}} */
644
645
/* {{{ php_handle_jp2
646
   main loop to parse JPEG 2000 JP2 wrapper format structure */
647
static struct php_gfxinfo *php_handle_jp2(php_stream *stream)
648
3
{
649
3
  struct php_gfxinfo *result = NULL;
650
3
  unsigned int box_length;
651
3
  unsigned int box_type;
652
3
  char jp2c_box_id[] = {(char)0x6a, (char)0x70, (char)0x32, (char)0x63};
653
654
  /* JP2 is a wrapper format for JPEG 2000. Data is contained within "boxes".
655
     Boxes themselves can be contained within "super-boxes". Super-Boxes can
656
     contain super-boxes which provides us with a hierarchical storage system.
657
658
     It is valid for a JP2 file to contain multiple individual codestreams.
659
     We'll just look for the first codestream at the root of the box structure
660
     and handle that.
661
  */
662
663
3
  for (;;)
664
5
  {
665
5
    box_length = php_read4(stream); /* LBox */
666
    /* TBox */
667
5
    if (php_stream_read(stream, (void *)&box_type, sizeof(box_type)) != sizeof(box_type)) {
668
      /* Use this as a general "out of stream" error */
669
2
      break;
670
2
    }
671
672
3
    if (box_length == 1) {
673
      /* We won't handle XLBoxes */
674
0
      return NULL;
675
0
    }
676
677
3
    if (!memcmp(&box_type, jp2c_box_id, 4))
678
0
    {
679
      /* Skip the first 3 bytes to emulate the file type examination */
680
0
      php_stream_seek(stream, 3, SEEK_CUR);
681
682
0
      result = php_handle_jpc(stream);
683
0
      break;
684
0
    }
685
686
    /* Stop if this was the last box */
687
3
    if ((int)box_length <= 0) {
688
1
      break;
689
1
    }
690
691
    /* Skip over LBox (Which includes both TBox and LBox itself */
692
2
    if (php_stream_seek(stream, box_length - 8, SEEK_CUR)) {
693
0
      break;
694
0
    }
695
2
  }
696
697
3
  if (result == NULL) {
698
3
    php_error_docref(NULL, E_WARNING, "JP2 file has no codestreams at root level");
699
3
  }
700
701
3
  return result;
702
3
}
703
/* }}} */
704
705
/* {{{ tiff constants */
706
PHPAPI const int php_tiff_bytes_per_format[] = {0, 1, 1, 2, 4, 8, 1, 1, 2, 4, 8, 4, 8};
707
708
/* uncompressed only */
709
0
#define TAG_IMAGEWIDTH              0x0100
710
0
#define TAG_IMAGEHEIGHT             0x0101
711
/* compressed images only */
712
0
#define TAG_COMP_IMAGEWIDTH         0xA002
713
0
#define TAG_COMP_IMAGEHEIGHT        0xA003
714
715
0
#define TAG_FMT_BYTE       1
716
#define TAG_FMT_STRING     2
717
1
#define TAG_FMT_USHORT     3
718
0
#define TAG_FMT_ULONG      4
719
#define TAG_FMT_URATIONAL  5
720
0
#define TAG_FMT_SBYTE      6
721
#define TAG_FMT_UNDEFINED  7
722
0
#define TAG_FMT_SSHORT     8
723
0
#define TAG_FMT_SLONG      9
724
#define TAG_FMT_SRATIONAL 10
725
#define TAG_FMT_SINGLE    11
726
#define TAG_FMT_DOUBLE    12
727
/* }}} */
728
729
/* {{{ php_ifd_get16u
730
 * Convert a 16 bit unsigned value from file's native byte order */
731
static int php_ifd_get16u(void *Short, int motorola_intel)
732
10
{
733
10
  if (motorola_intel) {
734
0
    return (((unsigned char *)Short)[0] << 8) | ((unsigned char *)Short)[1];
735
10
  } else {
736
10
    return (((unsigned char *)Short)[1] << 8) | ((unsigned char *)Short)[0];
737
10
  }
738
10
}
739
/* }}} */
740
741
/* {{{ php_ifd_get16s
742
 * Convert a 16 bit signed value from file's native byte order */
743
static signed short php_ifd_get16s(void *Short, int motorola_intel)
744
0
{
745
0
  return (signed short)php_ifd_get16u(Short, motorola_intel);
746
0
}
747
/* }}} */
748
749
/* {{{ php_ifd_get32s
750
 * Convert a 32 bit signed value from file's native byte order */
751
static int php_ifd_get32s(void *Long, int motorola_intel)
752
8
{
753
8
  if (motorola_intel) {
754
2
    return  ((unsigned)(((unsigned char *)Long)[0]) << 24) | (((unsigned char *)Long)[1] << 16)
755
2
          | (((unsigned char *)Long)[2] << 8 ) | (((unsigned char *)Long)[3] << 0 );
756
6
  } else {
757
6
    return  ((unsigned)(((unsigned char *)Long)[3]) << 24) | (((unsigned char *)Long)[2] << 16)
758
6
          | (((unsigned char *)Long)[1] << 8 ) | (((unsigned char *)Long)[0] << 0 );
759
6
  }
760
8
}
761
/* }}} */
762
763
/* {{{ php_ifd_get32u
764
 * Convert a 32 bit unsigned value from file's native byte order */
765
static unsigned php_ifd_get32u(void *Long, int motorola_intel)
766
8
{
767
8
  return (unsigned)php_ifd_get32s(Long, motorola_intel) & 0xffffffff;
768
8
}
769
/* }}} */
770
771
/* {{{ php_handle_tiff
772
   main loop to parse TIFF structure */
773
static struct php_gfxinfo *php_handle_tiff (php_stream * stream, zval *info, int motorola_intel)
774
8
{
775
8
  struct php_gfxinfo *result = NULL;
776
8
  int i, num_entries;
777
8
  unsigned char *dir_entry;
778
8
  size_t ifd_size, dir_size, entry_value, width=0, height=0, ifd_addr;
779
8
  int entry_tag , entry_type;
780
8
  char *ifd_data, ifd_ptr[4];
781
782
8
  if (php_stream_read(stream, ifd_ptr, 4) != 4)
783
0
    return NULL;
784
8
  ifd_addr = php_ifd_get32u(ifd_ptr, motorola_intel);
785
8
  if (php_stream_seek(stream, ifd_addr-8, SEEK_CUR))
786
0
    return NULL;
787
8
  ifd_size = 2;
788
8
  ifd_data = emalloc(ifd_size);
789
8
  if (php_stream_read(stream, ifd_data, 2) != 2) {
790
5
    efree(ifd_data);
791
5
    return NULL;
792
5
  }
793
3
  num_entries = php_ifd_get16u(ifd_data, motorola_intel);
794
3
  dir_size = 2/*num dir entries*/ +12/*length of entry*/*num_entries +4/* offset to next ifd (points to thumbnail or NULL)*/;
795
3
  ifd_size = dir_size;
796
3
  ifd_data = erealloc(ifd_data,ifd_size);
797
3
  if (php_stream_read(stream, ifd_data+2, dir_size-2) != dir_size-2) {
798
1
    efree(ifd_data);
799
1
    return NULL;
800
1
  }
801
  /* now we have the directory we can look how long it should be */
802
2
  ifd_size = dir_size;
803
5
  for(i=0;i<num_entries;i++) {
804
3
    dir_entry    = (unsigned char *) ifd_data+2+i*12;
805
3
    entry_tag    = php_ifd_get16u(dir_entry+0, motorola_intel);
806
3
    entry_type   = php_ifd_get16u(dir_entry+2, motorola_intel);
807
3
    switch(entry_type) {
808
0
      case TAG_FMT_BYTE:
809
0
      case TAG_FMT_SBYTE:
810
0
        entry_value  = (size_t)(dir_entry[8]);
811
0
        break;
812
1
      case TAG_FMT_USHORT:
813
1
        entry_value  = php_ifd_get16u(dir_entry+8, motorola_intel);
814
1
        break;
815
0
      case TAG_FMT_SSHORT:
816
0
        entry_value  = php_ifd_get16s(dir_entry+8, motorola_intel);
817
0
        break;
818
0
      case TAG_FMT_ULONG:
819
0
        entry_value  = php_ifd_get32u(dir_entry+8, motorola_intel);
820
0
        break;
821
0
      case TAG_FMT_SLONG:
822
0
        entry_value  = php_ifd_get32s(dir_entry+8, motorola_intel);
823
0
        break;
824
2
      default:
825
2
        continue;
826
3
    }
827
1
    switch(entry_tag) {
828
0
      case TAG_IMAGEWIDTH:
829
0
      case TAG_COMP_IMAGEWIDTH:
830
0
        width  = entry_value;
831
0
        break;
832
0
      case TAG_IMAGEHEIGHT:
833
0
      case TAG_COMP_IMAGEHEIGHT:
834
0
        height = entry_value;
835
0
        break;
836
1
    }
837
1
  }
838
2
  efree(ifd_data);
839
2
  if ( width && height) {
840
    /* not the same when in for-loop */
841
0
    result = (struct php_gfxinfo *) ecalloc(1, sizeof(struct php_gfxinfo));
842
0
    result->height   = height;
843
0
    result->width    = width;
844
0
    result->bits     = 0;
845
0
    result->channels = 0;
846
0
    return result;
847
0
  }
848
2
  return NULL;
849
2
}
850
/* }}} */
851
852
/* {{{ php_handle_psd */
853
static struct php_gfxinfo *php_handle_iff(php_stream * stream)
854
0
{
855
0
  struct php_gfxinfo * result;
856
0
  unsigned char a[10];
857
0
  int chunkId;
858
0
  int size;
859
0
  short width, height, bits;
860
861
0
  if (php_stream_read(stream, (char *) a, 8) != 8) {
862
0
    return NULL;
863
0
  }
864
0
  if (strncmp((char *) a+4, "ILBM", 4) && strncmp((char *) a+4, "PBM ", 4)) {
865
0
    return NULL;
866
0
  }
867
868
  /* loop chunks to find BMHD chunk */
869
0
  do {
870
0
    if (php_stream_read(stream, (char*)a, 8) != 8) {
871
0
      return NULL;
872
0
    }
873
0
    chunkId = php_ifd_get32s(a+0, 1);
874
0
    size    = php_ifd_get32s(a+4, 1);
875
0
    if (size < 0) {
876
0
      return NULL;
877
0
    }
878
0
    if ((size & 1) == 1) {
879
0
      if (size == INT_MAX) {
880
0
        return NULL;
881
0
      }
882
0
      size++;
883
0
    }
884
0
    if (chunkId == 0x424d4844) { /* BMHD chunk */
885
0
      if (size < 9 || php_stream_read(stream, (char*)a, 9) != 9) {
886
0
        return NULL;
887
0
      }
888
0
      width  = php_ifd_get16s(a+0, 1);
889
0
      height = php_ifd_get16s(a+2, 1);
890
0
      bits   = a[8] & 0xff;
891
0
      if (width > 0 && height > 0 && bits > 0 && bits < 33) {
892
0
        result = (struct php_gfxinfo *) ecalloc(1, sizeof(struct php_gfxinfo));
893
0
        result->width    = width;
894
0
        result->height   = height;
895
0
        result->bits     = bits;
896
0
        result->channels = 0;
897
0
        return result;
898
0
      }
899
0
    } else {
900
0
      if (php_stream_seek(stream, size, SEEK_CUR)) {
901
0
        return NULL;
902
0
      }
903
0
    }
904
0
  } while (1);
905
0
}
906
/* }}} */
907
908
/* {{{ php_get_wbmp
909
 * int WBMP file format type
910
 * byte Header Type
911
 *  byte Extended Header
912
 *    byte Header Data (type 00 = multibyte)
913
 *    byte Header Data (type 11 = name/pairs)
914
 * int Number of columns
915
 * int Number of rows
916
 */
917
static int php_get_wbmp(php_stream *stream, struct php_gfxinfo **result, int check)
918
43
{
919
43
  int i, width = 0, height = 0;
920
921
43
  if (php_stream_rewind(stream)) {
922
0
    return 0;
923
0
  }
924
925
  /* get type */
926
43
  if (php_stream_getc(stream) != 0) {
927
17
    return 0;
928
17
  }
929
930
  /* skip header */
931
85
  do {
932
85
    i = php_stream_getc(stream);
933
85
    if (i < 0) {
934
0
      return 0;
935
0
    }
936
85
  } while (i & 0x80);
937
938
  /* get width */
939
65
  do {
940
65
    i = php_stream_getc(stream);
941
65
    if (i < 0) {
942
0
      return 0;
943
0
    }
944
65
    width = (width << 7) | (i & 0x7f);
945
    /* maximum valid width for wbmp (although 127 may be a more accurate one) */
946
65
    if (width > 2048) {
947
1
      return 0;
948
1
    }
949
65
  } while (i & 0x80);
950
951
  /* get height */
952
119
  do {
953
119
    i = php_stream_getc(stream);
954
119
    if (i < 0) {
955
0
      return 0;
956
0
    }
957
119
    height = (height << 7) | (i & 0x7f);
958
    /* maximum valid height for wbmp (although 127 may be a more accurate one) */
959
119
    if (height > 2048) {
960
4
      return 0;
961
4
    }
962
119
  } while (i & 0x80);
963
964
21
  if (!height || !width) {
965
7
    return 0;
966
7
  }
967
968
14
  if (!check) {
969
7
    (*result)->width = width;
970
7
    (*result)->height = height;
971
7
  }
972
973
14
  return IMAGE_FILETYPE_WBMP;
974
21
}
975
/* }}} */
976
977
/* {{{ php_handle_wbmp */
978
static struct php_gfxinfo *php_handle_wbmp(php_stream * stream)
979
7
{
980
7
  struct php_gfxinfo *result = (struct php_gfxinfo *) ecalloc(1, sizeof(struct php_gfxinfo));
981
982
7
  if (!php_get_wbmp(stream, &result, 0)) {
983
0
    efree(result);
984
0
    return NULL;
985
0
  }
986
987
7
  return result;
988
7
}
989
/* }}} */
990
991
/* {{{ php_get_xbm */
992
static int php_get_xbm(php_stream *stream, struct php_gfxinfo **result)
993
27
{
994
27
  char *fline;
995
27
  char *iname;
996
27
  char *type;
997
27
  int value;
998
27
  unsigned int width = 0, height = 0;
999
1000
27
  if (result) {
1001
0
    *result = NULL;
1002
0
  }
1003
27
  if (php_stream_rewind(stream)) {
1004
0
    return 0;
1005
0
  }
1006
170
  while ((fline=php_stream_gets(stream, NULL, 0)) != NULL) {
1007
143
    iname = estrdup(fline); /* simple way to get necessary buffer of required size */
1008
143
    if (sscanf(fline, "#define %s %d", iname, &value) == 2) {
1009
0
      if (!(type = strrchr(iname, '_'))) {
1010
0
        type = iname;
1011
0
      } else {
1012
0
        type++;
1013
0
      }
1014
1015
0
      if (!strcmp("width", type)) {
1016
0
        width = (unsigned int) value;
1017
0
        if (height) {
1018
0
          efree(iname);
1019
0
          break;
1020
0
        }
1021
0
      }
1022
0
      if (!strcmp("height", type)) {
1023
0
        height = (unsigned int) value;
1024
0
        if (width) {
1025
0
          efree(iname);
1026
0
          break;
1027
0
        }
1028
0
      }
1029
0
    }
1030
143
    efree(fline);
1031
143
    efree(iname);
1032
143
  }
1033
27
  if (fline) {
1034
0
    efree(fline);
1035
0
  }
1036
1037
27
  if (width && height) {
1038
0
    if (result) {
1039
0
      *result = (struct php_gfxinfo *) ecalloc(1, sizeof(struct php_gfxinfo));
1040
0
      (*result)->width = width;
1041
0
      (*result)->height = height;
1042
0
    }
1043
0
    return IMAGE_FILETYPE_XBM;
1044
0
  }
1045
1046
27
  return 0;
1047
27
}
1048
/* }}} */
1049
1050
/* {{{ php_handle_xbm */
1051
static struct php_gfxinfo *php_handle_xbm(php_stream * stream)
1052
0
{
1053
0
  struct php_gfxinfo *result;
1054
0
  php_get_xbm(stream, &result);
1055
0
  return result;
1056
0
}
1057
/* }}} */
1058
1059
/* {{{ php_handle_ico */
1060
static struct php_gfxinfo *php_handle_ico(php_stream * stream)
1061
1
{
1062
1
  struct php_gfxinfo *result = NULL;
1063
1
  unsigned char dim[16];
1064
1
  int num_icons = 0;
1065
1066
1
  if (php_stream_read(stream, (char *) dim, 2) != 2)
1067
0
    return NULL;
1068
1069
1
  num_icons = (((unsigned int)dim[1]) << 8) + ((unsigned int) dim[0]);
1070
1071
1
  if (num_icons < 1 || num_icons > 255)
1072
0
    return NULL;
1073
1074
1
  result = (struct php_gfxinfo *) ecalloc(1, sizeof(struct php_gfxinfo));
1075
1076
14
  while (num_icons > 0)
1077
14
  {
1078
14
    if (php_stream_read(stream, (char *) dim, sizeof(dim)) != sizeof(dim))
1079
1
      break;
1080
1081
13
    if ((((unsigned int)dim[7]) <<  8) +  ((unsigned int)dim[6]) >= result->bits)
1082
2
    {
1083
2
      result->width    =  (unsigned int)dim[0];
1084
2
      result->height   =  (unsigned int)dim[1];
1085
2
      result->bits     =  (((unsigned int)dim[7]) <<  8) +  ((unsigned int)dim[6]);
1086
2
    }
1087
13
    num_icons--;
1088
13
  }
1089
1090
1
  if (0 == result->width)
1091
0
    result->width = 256;
1092
1093
1
  if (0 == result->height)
1094
0
    result->height = 256;
1095
1096
1
  return result;
1097
1
}
1098
/* }}} */
1099
1100
/* {{{ php_handle_webp */
1101
static struct php_gfxinfo *php_handle_webp(php_stream * stream)
1102
0
{
1103
0
  struct php_gfxinfo *result = NULL;
1104
0
  const char sig[3] = {'V', 'P', '8'};
1105
0
  unsigned char buf[18];
1106
0
  char format;
1107
1108
0
  if (php_stream_read(stream, (char *) buf, 18) != 18)
1109
0
    return NULL;
1110
1111
0
  if (memcmp(buf, sig, 3)) {
1112
0
    return NULL;
1113
0
  }
1114
0
  switch (buf[3]) {
1115
0
    case ' ':
1116
0
    case 'L':
1117
0
    case 'X':
1118
0
      format = buf[3];
1119
0
      break;
1120
0
    default:
1121
0
      return NULL;
1122
0
  }
1123
1124
0
  result = (struct php_gfxinfo *) ecalloc(1, sizeof(struct php_gfxinfo));
1125
1126
0
  switch (format) {
1127
0
    case ' ':
1128
0
      result->width = buf[14] + ((buf[15] & 0x3F) << 8);
1129
0
      result->height = buf[16] + ((buf[17] & 0x3F) << 8);
1130
0
      break;
1131
0
    case 'L':
1132
0
      result->width = buf[9] + ((buf[10] & 0x3F) << 8) + 1;
1133
0
      result->height = (buf[10] >> 6) + (buf[11] << 2) + ((buf[12] & 0xF) << 10) + 1;
1134
0
      break;
1135
0
    case 'X':
1136
0
      result->width = buf[12] + (buf[13] << 8) + (buf[14] << 16) + 1;
1137
0
      result->height = buf[15] + (buf[16] << 8) + (buf[17] << 16) + 1;
1138
0
      break;
1139
0
  }
1140
0
  result->bits = 8; /* always 1 byte */
1141
1142
0
  return result;
1143
0
}
1144
/* }}} */
1145
1146
/* {{{ User struct and stream read/skip implementations for libavifinfo API */
1147
struct php_avif_stream {
1148
  php_stream* stream;
1149
  uint8_t buffer[AVIFINFO_MAX_NUM_READ_BYTES];
1150
};
1151
1152
309
static const uint8_t* php_avif_stream_read(void* stream, size_t num_bytes) {
1153
309
  struct php_avif_stream* avif_stream = (struct php_avif_stream*)stream;
1154
1155
309
  if (avif_stream == NULL || avif_stream->stream == NULL) {
1156
0
    return NULL;
1157
0
  }
1158
309
  if (php_stream_read(avif_stream->stream, (char*)avif_stream->buffer, num_bytes) != num_bytes) {
1159
7
    avif_stream->stream = NULL; /* fail further calls */
1160
7
    return NULL;
1161
7
  }
1162
302
  return avif_stream->buffer;
1163
309
}
1164
1165
0
static void php_avif_stream_skip(void* stream, size_t num_bytes) {
1166
0
  struct php_avif_stream* avif_stream = (struct php_avif_stream*)stream;
1167
1168
0
  if (avif_stream == NULL || avif_stream->stream == NULL) {
1169
0
    return;
1170
0
  }
1171
0
  if (php_stream_seek(avif_stream->stream, num_bytes, SEEK_CUR)) {
1172
0
    avif_stream->stream = NULL; /* fail further calls */
1173
0
  }
1174
0
}
1175
/* }}} */
1176
1177
/* {{{ php_handle_avif
1178
 * Parse AVIF features
1179
 *
1180
 * The stream must be positioned at the beginning of a box, so it does not
1181
 * matter whether the "ftyp" box was already read by php_is_image_avif() or not.
1182
 * It will read bytes from the stream until features are found or the file is
1183
 * declared as invalid. Around 450 bytes are usually enough.
1184
 * Transforms such as mirror and rotation are not applied on width and height.
1185
 */
1186
0
static struct php_gfxinfo *php_handle_avif(php_stream * stream) {
1187
0
  struct php_gfxinfo* result = NULL;
1188
0
  AvifInfoFeatures features;
1189
0
  struct php_avif_stream avif_stream;
1190
0
  avif_stream.stream = stream;
1191
1192
0
  if (AvifInfoGetFeaturesStream(&avif_stream, php_avif_stream_read, php_avif_stream_skip, &features) == kAvifInfoOk) {
1193
0
    result = (struct php_gfxinfo*)ecalloc(1, sizeof(struct php_gfxinfo));
1194
0
    result->width = features.width;
1195
0
    result->height = features.height;
1196
0
    result->bits = features.bit_depth;
1197
0
    result->channels = features.num_channels;
1198
0
  }
1199
0
  return result;
1200
0
}
1201
/* }}} */
1202
1203
/*
1204
 * Detect whether an image is of type AVIF
1205
 *
1206
 * Only the first "ftyp" box is read.
1207
 * For a valid file, 12 bytes are usually read, but more might be necessary.
1208
 */
1209
36
bool php_is_image_avif(php_stream* stream) {
1210
36
  struct php_avif_stream avif_stream;
1211
36
  avif_stream.stream = stream;
1212
1213
36
  return AvifInfoIdentifyStream(&avif_stream, php_avif_stream_read, php_avif_stream_skip) == kAvifInfoOk;
1214
36
}
1215
1216
/* {{{ php_image_type_to_mime_type
1217
 * Convert internal image_type to mime type */
1218
PHPAPI const char * php_image_type_to_mime_type(int image_type)
1219
4.04k
{
1220
4.04k
  switch( image_type) {
1221
0
    case IMAGE_FILETYPE_GIF:
1222
0
      return "image/gif";
1223
265
    case IMAGE_FILETYPE_JPEG:
1224
265
      return "image/jpeg";
1225
0
    case IMAGE_FILETYPE_PNG:
1226
0
      return "image/png";
1227
2
    case IMAGE_FILETYPE_SWF:
1228
2
    case IMAGE_FILETYPE_SWC:
1229
2
      return "application/x-shockwave-flash";
1230
0
    case IMAGE_FILETYPE_PSD:
1231
0
      return "image/psd";
1232
0
    case IMAGE_FILETYPE_BMP:
1233
0
      return "image/bmp";
1234
2.73k
    case IMAGE_FILETYPE_TIFF_II:
1235
3.67k
    case IMAGE_FILETYPE_TIFF_MM:
1236
3.67k
      return "image/tiff";
1237
0
    case IMAGE_FILETYPE_IFF:
1238
0
      return "image/iff";
1239
7
    case IMAGE_FILETYPE_WBMP:
1240
7
      return "image/vnd.wap.wbmp";
1241
0
    case IMAGE_FILETYPE_JPC:
1242
0
      return "application/octet-stream";
1243
0
    case IMAGE_FILETYPE_JP2:
1244
0
      return "image/jp2";
1245
0
    case IMAGE_FILETYPE_XBM:
1246
0
      return "image/xbm";
1247
1
    case IMAGE_FILETYPE_ICO:
1248
1
      return "image/vnd.microsoft.icon";
1249
41
    case IMAGE_FILETYPE_WEBP:
1250
41
      return "image/webp";
1251
0
    case IMAGE_FILETYPE_AVIF:
1252
0
      return "image/avif";
1253
51
    case IMAGE_FILETYPE_HEIF:
1254
51
      return "image/heif";
1255
0
    default: {
1256
0
      const struct php_image_handler *handler = zend_hash_index_find_ptr(&php_image_handlers, (zend_ulong) image_type);
1257
0
      if (handler) {
1258
0
        return handler->mime_type;
1259
0
      }
1260
0
      ZEND_FALLTHROUGH;
1261
0
    }
1262
0
    case IMAGE_FILETYPE_UNKNOWN:
1263
0
      return "application/octet-stream"; /* suppose binary format */
1264
4.04k
  }
1265
4.04k
}
1266
/* }}} */
1267
1268
/* {{{ Get Mime-Type for image-type returned by getimagesize, exif_read_data, exif_thumbnail, exif_imagetype */
1269
PHP_FUNCTION(image_type_to_mime_type)
1270
0
{
1271
0
  zend_long p_image_type;
1272
1273
0
  ZEND_PARSE_PARAMETERS_START(1, 1)
1274
0
    Z_PARAM_LONG(p_image_type)
1275
0
  ZEND_PARSE_PARAMETERS_END();
1276
1277
0
  ZVAL_STRING(return_value, php_image_type_to_mime_type(p_image_type));
1278
0
}
1279
/* }}} */
1280
1281
/* {{{ Get file extension for image-type returned by getimagesize, exif_read_data, exif_thumbnail, exif_imagetype */
1282
PHP_FUNCTION(image_type_to_extension)
1283
0
{
1284
0
  zend_long image_type;
1285
0
  bool inc_dot=1;
1286
0
  const char *imgext = NULL;
1287
1288
0
  ZEND_PARSE_PARAMETERS_START(1, 2)
1289
0
    Z_PARAM_LONG(image_type)
1290
0
    Z_PARAM_OPTIONAL
1291
0
    Z_PARAM_BOOL(inc_dot)
1292
0
  ZEND_PARSE_PARAMETERS_END();
1293
1294
0
  switch (image_type) {
1295
0
    case IMAGE_FILETYPE_GIF:
1296
0
      imgext = ".gif";
1297
0
      break;
1298
0
    case IMAGE_FILETYPE_JPEG:
1299
0
      imgext = ".jpeg";
1300
0
      break;
1301
0
    case IMAGE_FILETYPE_PNG:
1302
0
      imgext = ".png";
1303
0
      break;
1304
0
    case IMAGE_FILETYPE_SWF:
1305
0
    case IMAGE_FILETYPE_SWC:
1306
0
      imgext = ".swf";
1307
0
      break;
1308
0
    case IMAGE_FILETYPE_PSD:
1309
0
      imgext = ".psd";
1310
0
      break;
1311
0
    case IMAGE_FILETYPE_BMP:
1312
0
    case IMAGE_FILETYPE_WBMP:
1313
0
      imgext = ".bmp";
1314
0
      break;
1315
0
    case IMAGE_FILETYPE_TIFF_II:
1316
0
    case IMAGE_FILETYPE_TIFF_MM:
1317
0
      imgext = ".tiff";
1318
0
      break;
1319
0
    case IMAGE_FILETYPE_IFF:
1320
0
      imgext = ".iff";
1321
0
      break;
1322
0
    case IMAGE_FILETYPE_JPC:
1323
0
      imgext = ".jpc";
1324
0
      break;
1325
0
    case IMAGE_FILETYPE_JP2:
1326
0
      imgext = ".jp2";
1327
0
      break;
1328
0
    case IMAGE_FILETYPE_JPX:
1329
0
      imgext = ".jpx";
1330
0
      break;
1331
0
    case IMAGE_FILETYPE_JB2:
1332
0
      imgext = ".jb2";
1333
0
      break;
1334
0
    case IMAGE_FILETYPE_XBM:
1335
0
      imgext = ".xbm";
1336
0
      break;
1337
0
    case IMAGE_FILETYPE_ICO:
1338
0
      imgext = ".ico";
1339
0
      break;
1340
0
    case IMAGE_FILETYPE_WEBP:
1341
0
      imgext = ".webp";
1342
0
      break;
1343
0
    case IMAGE_FILETYPE_AVIF:
1344
0
      imgext = ".avif";
1345
0
      break;
1346
0
    case IMAGE_FILETYPE_HEIF:
1347
0
      imgext = ".heif";
1348
0
      break;
1349
0
    default: {
1350
0
      const struct php_image_handler *handler = zend_hash_index_find_ptr(&php_image_handlers, (zend_ulong) image_type);
1351
0
      if (handler) {
1352
0
        imgext = handler->extension;
1353
0
      }
1354
0
      break;
1355
0
    }
1356
0
  }
1357
1358
0
  if (imgext) {
1359
0
    RETURN_STRING(&imgext[!inc_dot]);
1360
0
  }
1361
1362
0
  RETURN_FALSE;
1363
0
}
1364
/* }}} */
1365
1366
/* {{{ php_imagetype
1367
   detect filetype from first bytes */
1368
PHPAPI int php_getimagetype(php_stream *stream, const char *input, char *filetype)
1369
56
{
1370
56
  char tmp[12];
1371
56
  int twelve_bytes_read;
1372
1373
56
  if ( !filetype) filetype = tmp;
1374
56
  if((php_stream_read(stream, filetype, 3)) != 3) {
1375
1
    php_error_docref(NULL, E_NOTICE, "Error reading from %s!", input);
1376
1
    return IMAGE_FILETYPE_UNKNOWN;
1377
1
  }
1378
1379
/* BYTES READ: 3 */
1380
55
  if (!memcmp(filetype, php_sig_gif, 3)) {
1381
0
    return IMAGE_FILETYPE_GIF;
1382
55
  } else if (!memcmp(filetype, php_sig_jpg, 3)) {
1383
4
    return IMAGE_FILETYPE_JPEG;
1384
51
  } else if (!memcmp(filetype, php_sig_png, 3)) {
1385
0
    if (php_stream_read(stream, filetype+3, 5) != 5) {
1386
0
      php_error_docref(NULL, E_NOTICE, "Error reading from %s!", input);
1387
0
      return IMAGE_FILETYPE_UNKNOWN;
1388
0
    }
1389
0
    if (!memcmp(filetype, php_sig_png, 8)) {
1390
0
      return IMAGE_FILETYPE_PNG;
1391
0
    } else {
1392
0
      php_error_docref(NULL, E_WARNING, "PNG file corrupted by ASCII conversion");
1393
0
      return IMAGE_FILETYPE_UNKNOWN;
1394
0
    }
1395
51
  } else if (!memcmp(filetype, php_sig_swf, 3)) {
1396
3
    return IMAGE_FILETYPE_SWF;
1397
48
  } else if (!memcmp(filetype, php_sig_swc, 3)) {
1398
0
    return IMAGE_FILETYPE_SWC;
1399
48
  } else if (!memcmp(filetype, php_sig_psd, 3)) {
1400
0
    return IMAGE_FILETYPE_PSD;
1401
48
  } else if (!memcmp(filetype, php_sig_bmp, 2)) {
1402
0
    return IMAGE_FILETYPE_BMP;
1403
48
  } else if (!memcmp(filetype, php_sig_jpc, 3)) {
1404
0
    return IMAGE_FILETYPE_JPC;
1405
48
  } else if (!memcmp(filetype, php_sig_riff, 3)) {
1406
0
    if (php_stream_read(stream, filetype+3, 9) != 9) {
1407
0
      php_error_docref(NULL, E_NOTICE, "Error reading from %s!", input);
1408
0
      return IMAGE_FILETYPE_UNKNOWN;
1409
0
    }
1410
0
    if (!memcmp(filetype+8, php_sig_webp, 4)) {
1411
0
      return IMAGE_FILETYPE_WEBP;
1412
0
    } else {
1413
0
      return IMAGE_FILETYPE_UNKNOWN;
1414
0
    }
1415
0
  }
1416
1417
48
  if (php_stream_read(stream, filetype+3, 1) != 1) {
1418
0
    php_error_docref(NULL, E_NOTICE, "Error reading from %s!", input);
1419
0
    return IMAGE_FILETYPE_UNKNOWN;
1420
0
  }
1421
/* BYTES READ: 4 */
1422
48
  if (!memcmp(filetype, php_sig_tif_ii, 4)) {
1423
6
    return IMAGE_FILETYPE_TIFF_II;
1424
42
  } else if (!memcmp(filetype, php_sig_tif_mm, 4)) {
1425
2
    return IMAGE_FILETYPE_TIFF_MM;
1426
40
  } else if (!memcmp(filetype, php_sig_iff, 4)) {
1427
0
    return IMAGE_FILETYPE_IFF;
1428
40
  } else if (!memcmp(filetype, php_sig_ico, 4)) {
1429
1
    return IMAGE_FILETYPE_ICO;
1430
1
  }
1431
1432
  /* WBMP may be smaller than 12 bytes, so delay error */
1433
39
  twelve_bytes_read = (php_stream_read(stream, filetype+4, 8) == 8);
1434
1435
/* BYTES READ: 12 */
1436
39
  if (twelve_bytes_read && !memcmp(filetype, php_sig_jp2, 12)) {
1437
3
    return IMAGE_FILETYPE_JP2;
1438
3
  }
1439
1440
36
  if (!php_stream_rewind(stream) && php_is_image_avif(stream)) {
1441
0
    return IMAGE_FILETYPE_AVIF;
1442
0
  }
1443
1444
  /* See GH-20201: this needs to be after avif checks to avoid identifying avif as heif. */
1445
36
  if (twelve_bytes_read && !memcmp(filetype + 4, php_sig_ftyp, 4) &&
1446
9
    (!memcmp(filetype + 8, php_sig_mif1, 4) || !memcmp(filetype + 8, php_sig_heic, 4) || !memcmp(filetype + 8, php_sig_heix, 4))) {
1447
0
    return IMAGE_FILETYPE_HEIF;
1448
0
  }
1449
1450
/* AFTER ALL ABOVE FAILED */
1451
36
  if (php_get_wbmp(stream, NULL, 1)) {
1452
7
    return IMAGE_FILETYPE_WBMP;
1453
7
  }
1454
1455
29
  if (!twelve_bytes_read) {
1456
2
    php_error_docref(NULL, E_NOTICE, "Error reading from %s!", input);
1457
2
    return IMAGE_FILETYPE_UNKNOWN;
1458
2
  }
1459
1460
27
  if (php_get_xbm(stream, NULL)) {
1461
0
    return IMAGE_FILETYPE_XBM;
1462
0
  }
1463
1464
27
  zend_ulong h;
1465
27
  zval *zv;
1466
27
  ZEND_HASH_FOREACH_NUM_KEY_VAL(&php_image_handlers, h, zv) {
1467
27
    const struct php_image_handler *handler = Z_PTR_P(zv);
1468
27
    if (handler->identify(stream) == SUCCESS) {
1469
0
      return (int) h;
1470
0
    }
1471
27
  } ZEND_HASH_FOREACH_END();
1472
1473
27
  return IMAGE_FILETYPE_UNKNOWN;
1474
27
}
1475
/* }}} */
1476
1477
static void php_getimagesize_from_stream(php_stream *stream, char *input, zval *info, INTERNAL_FUNCTION_PARAMETERS) /* {{{ */
1478
56
{
1479
56
  int itype = 0;
1480
56
  struct php_gfxinfo *result = NULL;
1481
56
  const char *mime_type = NULL;
1482
1483
56
  if (!stream) {
1484
0
    RETURN_FALSE;
1485
0
  }
1486
1487
56
  itype = php_getimagetype(stream, input, NULL);
1488
56
  switch( itype) {
1489
0
    case IMAGE_FILETYPE_GIF:
1490
0
      result = php_handle_gif(stream);
1491
0
      break;
1492
4
    case IMAGE_FILETYPE_JPEG:
1493
4
      if (info) {
1494
0
        result = php_handle_jpeg(stream, info);
1495
4
      } else {
1496
4
        result = php_handle_jpeg(stream, NULL);
1497
4
      }
1498
4
      break;
1499
0
    case IMAGE_FILETYPE_PNG:
1500
0
      result = php_handle_png(stream);
1501
0
      break;
1502
3
    case IMAGE_FILETYPE_SWF:
1503
3
      result = php_handle_swf(stream);
1504
3
      break;
1505
0
    case IMAGE_FILETYPE_SWC:
1506
      /* TODO: with the new php_image_register_handler() APIs, this restriction could be solved */
1507
#if defined(HAVE_ZLIB) && !defined(COMPILE_DL_ZLIB)
1508
      result = php_handle_swc(stream);
1509
#else
1510
0
      php_error_docref(NULL, E_NOTICE, "The image is a compressed SWF file, but you do not have a static version of the zlib extension enabled");
1511
0
#endif
1512
0
      break;
1513
0
    case IMAGE_FILETYPE_PSD:
1514
0
      result = php_handle_psd(stream);
1515
0
      break;
1516
0
    case IMAGE_FILETYPE_BMP:
1517
0
      result = php_handle_bmp(stream);
1518
0
      break;
1519
6
    case IMAGE_FILETYPE_TIFF_II:
1520
6
      result = php_handle_tiff(stream, NULL, 0);
1521
6
      break;
1522
2
    case IMAGE_FILETYPE_TIFF_MM:
1523
2
      result = php_handle_tiff(stream, NULL, 1);
1524
2
      break;
1525
0
    case IMAGE_FILETYPE_JPC:
1526
0
      result = php_handle_jpc(stream);
1527
0
      break;
1528
3
    case IMAGE_FILETYPE_JP2:
1529
3
      result = php_handle_jp2(stream);
1530
3
      break;
1531
0
    case IMAGE_FILETYPE_IFF:
1532
0
      result = php_handle_iff(stream);
1533
0
      break;
1534
7
    case IMAGE_FILETYPE_WBMP:
1535
7
      result = php_handle_wbmp(stream);
1536
7
      break;
1537
0
    case IMAGE_FILETYPE_XBM:
1538
0
      result = php_handle_xbm(stream);
1539
0
      break;
1540
1
    case IMAGE_FILETYPE_ICO:
1541
1
      result = php_handle_ico(stream);
1542
1
      break;
1543
0
    case IMAGE_FILETYPE_WEBP:
1544
0
      result = php_handle_webp(stream);
1545
0
      break;
1546
0
    case IMAGE_FILETYPE_AVIF:
1547
0
      result = php_handle_avif(stream);
1548
0
      break;
1549
0
    case IMAGE_FILETYPE_HEIF:
1550
0
      if (!php_stream_rewind(stream)) {
1551
0
        result = php_handle_avif(stream);
1552
0
      }
1553
0
      break;
1554
0
    default: {
1555
0
      struct php_image_handler* handler = zend_hash_index_find_ptr(&php_image_handlers, (zend_ulong) itype);
1556
0
      if (handler) {
1557
0
        result = handler->get_info(stream);
1558
0
        mime_type = handler->mime_type;
1559
0
        break;
1560
0
      }
1561
0
      ZEND_FALLTHROUGH;
1562
0
    }
1563
30
    case IMAGE_FILETYPE_UNKNOWN:
1564
30
      break;
1565
56
  }
1566
1567
56
  if (result) {
1568
10
    array_init(return_value);
1569
10
    add_index_long(return_value, 0, result->width);
1570
10
    add_index_long(return_value, 1, result->height);
1571
10
    add_index_long(return_value, 2, itype);
1572
10
    if ((!result->width_unit || zend_string_equals_literal(result->width_unit, "px"))
1573
10
      && (!result->height_unit || zend_string_equals_literal(result->height_unit, "px"))) {
1574
10
      char temp[MAX_LENGTH_OF_LONG * 2 + sizeof("width=\"\" height=\"\"")];
1575
10
      snprintf(temp, sizeof(temp), "width=\"%d\" height=\"%d\"", result->width, result->height);
1576
10
      add_index_string(return_value, 3, temp);
1577
10
    }
1578
1579
10
    if (result->bits != 0) {
1580
1
      add_assoc_long(return_value, "bits", result->bits);
1581
1
    }
1582
10
    if (result->channels != 0) {
1583
0
      add_assoc_long(return_value, "channels", result->channels);
1584
0
    }
1585
10
    add_assoc_string(return_value, "mime", mime_type ? mime_type : php_image_type_to_mime_type(itype));
1586
1587
10
    if (result->width_unit) {
1588
0
      add_assoc_str(return_value, "width_unit", result->width_unit);
1589
10
    } else {
1590
10
      add_assoc_string(return_value, "width_unit", "px");
1591
10
    }
1592
10
    if (result->height_unit) {
1593
0
      add_assoc_str(return_value, "height_unit", result->height_unit);
1594
10
    } else {
1595
10
      add_assoc_string(return_value, "height_unit", "px");
1596
10
    }
1597
1598
10
    efree(result);
1599
46
  } else {
1600
46
    RETURN_FALSE;
1601
46
  }
1602
56
}
1603
/* }}} */
1604
1605
56
#define FROM_DATA 0
1606
194
#define FROM_PATH 1
1607
1608
63
static void php_getimagesize_from_any(INTERNAL_FUNCTION_PARAMETERS, int mode) {  /* {{{ */
1609
63
  zval *info = NULL;
1610
63
  php_stream *stream = NULL;
1611
63
  zend_string *input;
1612
63
  const int argc = ZEND_NUM_ARGS();
1613
1614
189
  ZEND_PARSE_PARAMETERS_START(1, 2)
1615
252
    Z_PARAM_STR(input)
1616
63
    Z_PARAM_OPTIONAL
1617
140
    Z_PARAM_ZVAL(info)
1618
140
  ZEND_PARSE_PARAMETERS_END();
1619
1620
63
  if (mode == FROM_PATH && zend_str_has_nul_byte(input)) {
1621
2
    zend_argument_value_error(1, "must not contain any null bytes");
1622
2
    RETURN_THROWS();
1623
2
  }
1624
1625
61
  if (argc == 2) {
1626
5
    info = zend_try_array_init(info);
1627
5
    if (!info) {
1628
0
      RETURN_THROWS();
1629
0
    }
1630
5
  }
1631
1632
61
  if (mode == FROM_PATH) {
1633
5
    stream = php_stream_open_wrapper(ZSTR_VAL(input), "rb", STREAM_MUST_SEEK|REPORT_ERRORS|IGNORE_PATH, NULL);
1634
56
  } else {
1635
56
    stream = php_stream_memory_open(TEMP_STREAM_READONLY, input);
1636
56
  }
1637
1638
61
  if (!stream) {
1639
5
    RETURN_FALSE;
1640
5
  }
1641
1642
56
  php_getimagesize_from_stream(stream, ZSTR_VAL(input), info, INTERNAL_FUNCTION_PARAM_PASSTHRU);
1643
56
  php_stream_close(stream);
1644
56
}
1645
/* }}} */
1646
1647
/* {{{ Get the size of an image as 4-element array */
1648
PHP_FUNCTION(getimagesize)
1649
7
{
1650
7
  php_getimagesize_from_any(INTERNAL_FUNCTION_PARAM_PASSTHRU, FROM_PATH);
1651
7
}
1652
/* }}} */
1653
1654
/* {{{ Get the size of an image as 4-element array */
1655
PHP_FUNCTION(getimagesizefromstring)
1656
56
{
1657
56
  php_getimagesize_from_any(INTERNAL_FUNCTION_PARAM_PASSTHRU, FROM_DATA);
1658
56
}
1659
/* }}} */
1660
1661
PHP_MINIT_FUNCTION(image)
1662
16
{
1663
16
  zend_hash_init(&php_image_handlers, 4, NULL, NULL, true);
1664
16
  return SUCCESS;
1665
16
}
1666
1667
PHP_MSHUTDOWN_FUNCTION(image)
1668
0
{
1669
#ifdef ZTS
1670
  if (!tsrm_is_main_thread()) {
1671
    return SUCCESS;
1672
  }
1673
#endif
1674
0
  zend_hash_destroy(&php_image_handlers);
1675
0
  return SUCCESS;
1676
0
}
1677
1678
extern zend_module_entry basic_functions_module;
1679
1680
int php_image_register_handler(const struct php_image_handler *handler)
1681
0
{
1682
0
  zend_hash_index_add_ptr(&php_image_handlers, (zend_ulong) php_image_handler_next_id, (void *) handler);
1683
0
  zend_register_long_constant(handler->const_name, strlen(handler->const_name), php_image_handler_next_id, CONST_PERSISTENT, basic_functions_module.module_number);
1684
0
  Z_LVAL_P(zend_get_constant_str(ZEND_STRL("IMAGETYPE_COUNT")))++;
1685
0
  return php_image_handler_next_id++;
1686
0
}
1687
1688
zend_result php_image_unregister_handler(int image_type)
1689
0
{
1690
0
  ZEND_ASSERT(image_type >= IMAGE_FILETYPE_FIXED_COUNT);
1691
0
  return zend_hash_index_del(&php_image_handlers, (zend_ulong) image_type);
1692
0
}