Coverage Report

Created: 2026-06-02 06:36

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
0
{
143
0
  unsigned int loop;
144
0
  unsigned long int result = 0;
145
146
0
  for (loop = pos; loop < pos + count; loop++)
147
0
  {
148
0
    result = result +
149
0
      ((((buffer[loop / 8]) >> (7 - (loop % 8))) & 0x01) << (count - (loop - pos) - 1));
150
0
  }
151
0
  return result;
152
0
}
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
0
{
245
0
  struct php_gfxinfo *result = NULL;
246
0
  long bits;
247
0
  unsigned char a[32];
248
249
0
  if (php_stream_seek(stream, 5, SEEK_CUR))
250
0
    return NULL;
251
252
0
  if (php_stream_read(stream, (char*)a, sizeof(a)) != sizeof(a))
253
0
    return NULL;
254
255
0
  result = (struct php_gfxinfo *) ecalloc (1, sizeof (struct php_gfxinfo));
256
0
  bits = php_swf_get_bits (a, 0, 5);
257
0
  result->width = (php_swf_get_bits (a, 5 + bits, bits) -
258
0
    php_swf_get_bits (a, 5, bits)) / 20;
259
0
  result->height = (php_swf_get_bits (a, 5 + (3 * bits), bits) -
260
0
    php_swf_get_bits (a, 5 + (2 * bits), bits)) / 20;
261
0
  result->bits     = 0;
262
0
  result->channels = 0;
263
0
  return result;
264
0
}
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
0
#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
0
#define M_APP7  0xe7
323
0
#define M_APP8  0xe8
324
0
#define M_APP9  0xe9
325
0
#define M_APP10 0xea
326
0
#define M_APP11 0xeb
327
0
#define M_APP12 0xec
328
0
#define M_APP13 0xed
329
0
#define M_APP14 0xee
330
0
#define M_APP15 0xef
331
#define M_COM   0xFE            /* COMment                                  */
332
333
0
#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
0
{
338
0
  unsigned char a[2];
339
340
  /* return 0 if we couldn't read enough data */
341
0
  if((php_stream_read(stream, (char *) a, sizeof(a))) < sizeof(a)) return 0;
342
343
0
  return (((unsigned short)a[0]) << 8) + ((unsigned short)a[1]);
344
0
}
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
0
{
351
0
  int a=0, marker;
352
353
  /* get marker byte, swallowing possible padding                           */
354
0
  if (!ff_read) {
355
0
    size_t extraneous = 0;
356
357
0
    while ((marker = php_stream_getc(stream)) != 0xff) {
358
0
      if (marker == EOF) {
359
0
        return M_EOI;/* we hit EOF */
360
0
  }
361
0
      extraneous++;
362
0
  }
363
0
    if (extraneous) {
364
0
      php_error_docref(NULL, E_WARNING, "Corrupt JPEG data: %zu extraneous bytes before marker", extraneous);
365
0
    }
366
0
  }
367
0
  a = 1;
368
0
  do {
369
0
    if ((marker = php_stream_getc(stream)) == EOF)
370
0
    {
371
0
      return M_EOI;/* we hit EOF */
372
0
    }
373
0
    a++;
374
0
  } while (marker == 0xff);
375
0
  if (a < 2)
376
0
  {
377
0
    return M_EOI; /* at least one 0xff is needed before marker code */
378
0
  }
379
0
  return (unsigned int)marker;
380
0
}
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
0
{
387
0
  zend_off_t length = ((unsigned int)php_read2(stream));
388
389
0
  if (length < 2) {
390
0
    return 0;
391
0
  }
392
0
  length = length - 2;
393
0
  php_stream_seek(stream, (zend_long)length, SEEK_CUR);
394
0
  return 1;
395
0
}
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
0
{
450
0
  struct php_gfxinfo *result = NULL;
451
0
  unsigned int marker = M_PSEUDO;
452
0
  unsigned short length, ff_read=1;
453
454
0
  for (;;) {
455
0
    marker = php_next_marker(stream, marker, ff_read);
456
0
    ff_read = 0;
457
0
    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
0
      case M_APP7:
500
0
      case M_APP8:
501
0
      case M_APP9:
502
0
      case M_APP10:
503
0
      case M_APP11:
504
0
      case M_APP12:
505
0
      case M_APP13:
506
0
      case M_APP14:
507
0
      case M_APP15:
508
0
        if (info) {
509
0
          if (!php_read_APP(stream, marker, info)) { /* read all the app marks... */
510
0
            return result;
511
0
          }
512
0
        } else {
513
0
          if (!php_skip_variable(stream)) {
514
0
            return result;
515
0
          }
516
0
        }
517
0
        break;
518
519
0
      case M_SOS:
520
0
      case M_EOI:
521
0
        return result;  /* we're about to hit image data, or are at EOF. stop processing. */
522
523
0
      default:
524
0
        if (!php_skip_variable(stream)) { /* anything else isn't interesting */
525
0
          return result;
526
0
        }
527
0
        break;
528
0
    }
529
0
  }
530
531
0
  return result; /* perhaps image broken -> no info but size */
532
0
}
533
/* }}} */
534
535
/* {{{ php_read4 */
536
static unsigned int php_read4(php_stream * stream)
537
0
{
538
0
  unsigned char a[4];
539
540
  /* just return 0 if we hit the end-of-file */
541
0
  if ((php_stream_read(stream, (char*)a, sizeof(a))) != sizeof(a)) return 0;
542
543
0
  return (((unsigned int)a[0]) << 24)
544
0
       + (((unsigned int)a[1]) << 16)
545
0
       + (((unsigned int)a[2]) <<  8)
546
0
       + (((unsigned int)a[3]));
547
0
}
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
0
{
649
0
  struct php_gfxinfo *result = NULL;
650
0
  unsigned int box_length;
651
0
  unsigned int box_type;
652
0
  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
0
  for (;;)
664
0
  {
665
0
    box_length = php_read4(stream); /* LBox */
666
    /* TBox */
667
0
    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
0
      break;
670
0
    }
671
672
0
    if (box_length == 1) {
673
      /* We won't handle XLBoxes */
674
0
      return NULL;
675
0
    }
676
677
0
    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
0
    if ((int)box_length <= 0) {
688
0
      break;
689
0
    }
690
691
    /* Skip over LBox (Which includes both TBox and LBox itself */
692
0
    if (php_stream_seek(stream, box_length - 8, SEEK_CUR)) {
693
0
      break;
694
0
    }
695
0
  }
696
697
0
  if (result == NULL) {
698
0
    php_error_docref(NULL, E_WARNING, "JP2 file has no codestreams at root level");
699
0
  }
700
701
0
  return result;
702
0
}
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
0
#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
0
{
733
0
  if (motorola_intel) {
734
0
    return (((unsigned char *)Short)[0] << 8) | ((unsigned char *)Short)[1];
735
0
  } else {
736
0
    return (((unsigned char *)Short)[1] << 8) | ((unsigned char *)Short)[0];
737
0
  }
738
0
}
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
0
{
753
0
  if (motorola_intel) {
754
0
    return  ((unsigned)(((unsigned char *)Long)[0]) << 24) | (((unsigned char *)Long)[1] << 16)
755
0
          | (((unsigned char *)Long)[2] << 8 ) | (((unsigned char *)Long)[3] << 0 );
756
0
  } else {
757
0
    return  ((unsigned)(((unsigned char *)Long)[3]) << 24) | (((unsigned char *)Long)[2] << 16)
758
0
          | (((unsigned char *)Long)[1] << 8 ) | (((unsigned char *)Long)[0] << 0 );
759
0
  }
760
0
}
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
0
{
767
0
  return (unsigned)php_ifd_get32s(Long, motorola_intel) & 0xffffffff;
768
0
}
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
0
{
775
0
  struct php_gfxinfo *result = NULL;
776
0
  int i, num_entries;
777
0
  unsigned char *dir_entry;
778
0
  size_t ifd_size, dir_size, entry_value, width=0, height=0, ifd_addr;
779
0
  int entry_tag , entry_type;
780
0
  char *ifd_data, ifd_ptr[4];
781
782
0
  if (php_stream_read(stream, ifd_ptr, 4) != 4)
783
0
    return NULL;
784
0
  ifd_addr = php_ifd_get32u(ifd_ptr, motorola_intel);
785
0
  if (php_stream_seek(stream, ifd_addr-8, SEEK_CUR))
786
0
    return NULL;
787
0
  ifd_size = 2;
788
0
  ifd_data = emalloc(ifd_size);
789
0
  if (php_stream_read(stream, ifd_data, 2) != 2) {
790
0
    efree(ifd_data);
791
0
    return NULL;
792
0
  }
793
0
  num_entries = php_ifd_get16u(ifd_data, motorola_intel);
794
0
  dir_size = 2/*num dir entries*/ +12/*length of entry*/*num_entries +4/* offset to next ifd (points to thumbnail or NULL)*/;
795
0
  ifd_size = dir_size;
796
0
  ifd_data = erealloc(ifd_data,ifd_size);
797
0
  if (php_stream_read(stream, ifd_data+2, dir_size-2) != dir_size-2) {
798
0
    efree(ifd_data);
799
0
    return NULL;
800
0
  }
801
  /* now we have the directory we can look how long it should be */
802
0
  ifd_size = dir_size;
803
0
  for(i=0;i<num_entries;i++) {
804
0
    dir_entry    = (unsigned char *) ifd_data+2+i*12;
805
0
    entry_tag    = php_ifd_get16u(dir_entry+0, motorola_intel);
806
0
    entry_type   = php_ifd_get16u(dir_entry+2, motorola_intel);
807
0
    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
0
      case TAG_FMT_USHORT:
813
0
        entry_value  = php_ifd_get16u(dir_entry+8, motorola_intel);
814
0
        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
0
      default:
825
0
        continue;
826
0
    }
827
0
    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
0
    }
837
0
  }
838
0
  efree(ifd_data);
839
0
  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
0
  return NULL;
849
0
}
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
      size++;
880
0
    }
881
0
    if (chunkId == 0x424d4844) { /* BMHD chunk */
882
0
      if (size < 9 || php_stream_read(stream, (char*)a, 9) != 9) {
883
0
        return NULL;
884
0
      }
885
0
      width  = php_ifd_get16s(a+0, 1);
886
0
      height = php_ifd_get16s(a+2, 1);
887
0
      bits   = a[8] & 0xff;
888
0
      if (width > 0 && height > 0 && bits > 0 && bits < 33) {
889
0
        result = (struct php_gfxinfo *) ecalloc(1, sizeof(struct php_gfxinfo));
890
0
        result->width    = width;
891
0
        result->height   = height;
892
0
        result->bits     = bits;
893
0
        result->channels = 0;
894
0
        return result;
895
0
      }
896
0
    } else {
897
0
      if (php_stream_seek(stream, size, SEEK_CUR)) {
898
0
        return NULL;
899
0
      }
900
0
    }
901
0
  } while (1);
902
0
}
903
/* }}} */
904
905
/* {{{ php_get_wbmp
906
 * int WBMP file format type
907
 * byte Header Type
908
 *  byte Extended Header
909
 *    byte Header Data (type 00 = multibyte)
910
 *    byte Header Data (type 11 = name/pairs)
911
 * int Number of columns
912
 * int Number of rows
913
 */
914
static int php_get_wbmp(php_stream *stream, struct php_gfxinfo **result, int check)
915
0
{
916
0
  int i, width = 0, height = 0;
917
918
0
  if (php_stream_rewind(stream)) {
919
0
    return 0;
920
0
  }
921
922
  /* get type */
923
0
  if (php_stream_getc(stream) != 0) {
924
0
    return 0;
925
0
  }
926
927
  /* skip header */
928
0
  do {
929
0
    i = php_stream_getc(stream);
930
0
    if (i < 0) {
931
0
      return 0;
932
0
    }
933
0
  } while (i & 0x80);
934
935
  /* get width */
936
0
  do {
937
0
    i = php_stream_getc(stream);
938
0
    if (i < 0) {
939
0
      return 0;
940
0
    }
941
0
    width = (width << 7) | (i & 0x7f);
942
    /* maximum valid width for wbmp (although 127 may be a more accurate one) */
943
0
    if (width > 2048) {
944
0
      return 0;
945
0
    }
946
0
  } while (i & 0x80);
947
948
  /* get height */
949
0
  do {
950
0
    i = php_stream_getc(stream);
951
0
    if (i < 0) {
952
0
      return 0;
953
0
    }
954
0
    height = (height << 7) | (i & 0x7f);
955
    /* maximum valid height for wbmp (although 127 may be a more accurate one) */
956
0
    if (height > 2048) {
957
0
      return 0;
958
0
    }
959
0
  } while (i & 0x80);
960
961
0
  if (!height || !width) {
962
0
    return 0;
963
0
  }
964
965
0
  if (!check) {
966
0
    (*result)->width = width;
967
0
    (*result)->height = height;
968
0
  }
969
970
0
  return IMAGE_FILETYPE_WBMP;
971
0
}
972
/* }}} */
973
974
/* {{{ php_handle_wbmp */
975
static struct php_gfxinfo *php_handle_wbmp(php_stream * stream)
976
0
{
977
0
  struct php_gfxinfo *result = (struct php_gfxinfo *) ecalloc(1, sizeof(struct php_gfxinfo));
978
979
0
  if (!php_get_wbmp(stream, &result, 0)) {
980
0
    efree(result);
981
0
    return NULL;
982
0
  }
983
984
0
  return result;
985
0
}
986
/* }}} */
987
988
/* {{{ php_get_xbm */
989
static int php_get_xbm(php_stream *stream, struct php_gfxinfo **result)
990
0
{
991
0
  char *fline;
992
0
  char *iname;
993
0
  char *type;
994
0
  int value;
995
0
  unsigned int width = 0, height = 0;
996
997
0
  if (result) {
998
0
    *result = NULL;
999
0
  }
1000
0
  if (php_stream_rewind(stream)) {
1001
0
    return 0;
1002
0
  }
1003
0
  while ((fline=php_stream_gets(stream, NULL, 0)) != NULL) {
1004
0
    iname = estrdup(fline); /* simple way to get necessary buffer of required size */
1005
0
    if (sscanf(fline, "#define %s %d", iname, &value) == 2) {
1006
0
      if (!(type = strrchr(iname, '_'))) {
1007
0
        type = iname;
1008
0
      } else {
1009
0
        type++;
1010
0
      }
1011
1012
0
      if (!strcmp("width", type)) {
1013
0
        width = (unsigned int) value;
1014
0
        if (height) {
1015
0
          efree(iname);
1016
0
          break;
1017
0
        }
1018
0
      }
1019
0
      if (!strcmp("height", type)) {
1020
0
        height = (unsigned int) value;
1021
0
        if (width) {
1022
0
          efree(iname);
1023
0
          break;
1024
0
        }
1025
0
      }
1026
0
    }
1027
0
    efree(fline);
1028
0
    efree(iname);
1029
0
  }
1030
0
  if (fline) {
1031
0
    efree(fline);
1032
0
  }
1033
1034
0
  if (width && height) {
1035
0
    if (result) {
1036
0
      *result = (struct php_gfxinfo *) ecalloc(1, sizeof(struct php_gfxinfo));
1037
0
      (*result)->width = width;
1038
0
      (*result)->height = height;
1039
0
    }
1040
0
    return IMAGE_FILETYPE_XBM;
1041
0
  }
1042
1043
0
  return 0;
1044
0
}
1045
/* }}} */
1046
1047
/* {{{ php_handle_xbm */
1048
static struct php_gfxinfo *php_handle_xbm(php_stream * stream)
1049
0
{
1050
0
  struct php_gfxinfo *result;
1051
0
  php_get_xbm(stream, &result);
1052
0
  return result;
1053
0
}
1054
/* }}} */
1055
1056
/* {{{ php_handle_ico */
1057
static struct php_gfxinfo *php_handle_ico(php_stream * stream)
1058
0
{
1059
0
  struct php_gfxinfo *result = NULL;
1060
0
  unsigned char dim[16];
1061
0
  int num_icons = 0;
1062
1063
0
  if (php_stream_read(stream, (char *) dim, 2) != 2)
1064
0
    return NULL;
1065
1066
0
  num_icons = (((unsigned int)dim[1]) << 8) + ((unsigned int) dim[0]);
1067
1068
0
  if (num_icons < 1 || num_icons > 255)
1069
0
    return NULL;
1070
1071
0
  result = (struct php_gfxinfo *) ecalloc(1, sizeof(struct php_gfxinfo));
1072
1073
0
  while (num_icons > 0)
1074
0
  {
1075
0
    if (php_stream_read(stream, (char *) dim, sizeof(dim)) != sizeof(dim))
1076
0
      break;
1077
1078
0
    if ((((unsigned int)dim[7]) <<  8) +  ((unsigned int)dim[6]) >= result->bits)
1079
0
    {
1080
0
      result->width    =  (unsigned int)dim[0];
1081
0
      result->height   =  (unsigned int)dim[1];
1082
0
      result->bits     =  (((unsigned int)dim[7]) <<  8) +  ((unsigned int)dim[6]);
1083
0
    }
1084
0
    num_icons--;
1085
0
  }
1086
1087
0
  if (0 == result->width)
1088
0
    result->width = 256;
1089
1090
0
  if (0 == result->height)
1091
0
    result->height = 256;
1092
1093
0
  return result;
1094
0
}
1095
/* }}} */
1096
1097
/* {{{ php_handle_webp */
1098
static struct php_gfxinfo *php_handle_webp(php_stream * stream)
1099
0
{
1100
0
  struct php_gfxinfo *result = NULL;
1101
0
  const char sig[3] = {'V', 'P', '8'};
1102
0
  unsigned char buf[18];
1103
0
  char format;
1104
1105
0
  if (php_stream_read(stream, (char *) buf, 18) != 18)
1106
0
    return NULL;
1107
1108
0
  if (memcmp(buf, sig, 3)) {
1109
0
    return NULL;
1110
0
  }
1111
0
  switch (buf[3]) {
1112
0
    case ' ':
1113
0
    case 'L':
1114
0
    case 'X':
1115
0
      format = buf[3];
1116
0
      break;
1117
0
    default:
1118
0
      return NULL;
1119
0
  }
1120
1121
0
  result = (struct php_gfxinfo *) ecalloc(1, sizeof(struct php_gfxinfo));
1122
1123
0
  switch (format) {
1124
0
    case ' ':
1125
0
      result->width = buf[14] + ((buf[15] & 0x3F) << 8);
1126
0
      result->height = buf[16] + ((buf[17] & 0x3F) << 8);
1127
0
      break;
1128
0
    case 'L':
1129
0
      result->width = buf[9] + ((buf[10] & 0x3F) << 8) + 1;
1130
0
      result->height = (buf[10] >> 6) + (buf[11] << 2) + ((buf[12] & 0xF) << 10) + 1;
1131
0
      break;
1132
0
    case 'X':
1133
0
      result->width = buf[12] + (buf[13] << 8) + (buf[14] << 16) + 1;
1134
0
      result->height = buf[15] + (buf[16] << 8) + (buf[17] << 16) + 1;
1135
0
      break;
1136
0
  }
1137
0
  result->bits = 8; /* always 1 byte */
1138
1139
0
  return result;
1140
0
}
1141
/* }}} */
1142
1143
/* {{{ User struct and stream read/skip implementations for libavifinfo API */
1144
struct php_avif_stream {
1145
  php_stream* stream;
1146
  uint8_t buffer[AVIFINFO_MAX_NUM_READ_BYTES];
1147
};
1148
1149
0
static const uint8_t* php_avif_stream_read(void* stream, size_t num_bytes) {
1150
0
  struct php_avif_stream* avif_stream = (struct php_avif_stream*)stream;
1151
1152
0
  if (avif_stream == NULL || avif_stream->stream == NULL) {
1153
0
    return NULL;
1154
0
  }
1155
0
  if (php_stream_read(avif_stream->stream, (char*)avif_stream->buffer, num_bytes) != num_bytes) {
1156
0
    avif_stream->stream = NULL; /* fail further calls */
1157
0
    return NULL;
1158
0
  }
1159
0
  return avif_stream->buffer;
1160
0
}
1161
1162
0
static void php_avif_stream_skip(void* stream, size_t num_bytes) {
1163
0
  struct php_avif_stream* avif_stream = (struct php_avif_stream*)stream;
1164
1165
0
  if (avif_stream == NULL || avif_stream->stream == NULL) {
1166
0
    return;
1167
0
  }
1168
0
  if (php_stream_seek(avif_stream->stream, num_bytes, SEEK_CUR)) {
1169
0
    avif_stream->stream = NULL; /* fail further calls */
1170
0
  }
1171
0
}
1172
/* }}} */
1173
1174
/* {{{ php_handle_avif
1175
 * Parse AVIF features
1176
 *
1177
 * The stream must be positioned at the beginning of a box, so it does not
1178
 * matter whether the "ftyp" box was already read by php_is_image_avif() or not.
1179
 * It will read bytes from the stream until features are found or the file is
1180
 * declared as invalid. Around 450 bytes are usually enough.
1181
 * Transforms such as mirror and rotation are not applied on width and height.
1182
 */
1183
0
static struct php_gfxinfo *php_handle_avif(php_stream * stream) {
1184
0
  struct php_gfxinfo* result = NULL;
1185
0
  AvifInfoFeatures features;
1186
0
  struct php_avif_stream avif_stream;
1187
0
  avif_stream.stream = stream;
1188
1189
0
  if (AvifInfoGetFeaturesStream(&avif_stream, php_avif_stream_read, php_avif_stream_skip, &features) == kAvifInfoOk) {
1190
0
    result = (struct php_gfxinfo*)ecalloc(1, sizeof(struct php_gfxinfo));
1191
0
    result->width = features.width;
1192
0
    result->height = features.height;
1193
0
    result->bits = features.bit_depth;
1194
0
    result->channels = features.num_channels;
1195
0
  }
1196
0
  return result;
1197
0
}
1198
/* }}} */
1199
1200
/*
1201
 * Detect whether an image is of type AVIF
1202
 *
1203
 * Only the first "ftyp" box is read.
1204
 * For a valid file, 12 bytes are usually read, but more might be necessary.
1205
 */
1206
0
bool php_is_image_avif(php_stream* stream) {
1207
0
  struct php_avif_stream avif_stream;
1208
0
  avif_stream.stream = stream;
1209
1210
0
  return AvifInfoIdentifyStream(&avif_stream, php_avif_stream_read, php_avif_stream_skip) == kAvifInfoOk;
1211
0
}
1212
1213
/* {{{ php_image_type_to_mime_type
1214
 * Convert internal image_type to mime type */
1215
PHPAPI const char * php_image_type_to_mime_type(int image_type)
1216
0
{
1217
0
  switch( image_type) {
1218
0
    case IMAGE_FILETYPE_GIF:
1219
0
      return "image/gif";
1220
0
    case IMAGE_FILETYPE_JPEG:
1221
0
      return "image/jpeg";
1222
0
    case IMAGE_FILETYPE_PNG:
1223
0
      return "image/png";
1224
0
    case IMAGE_FILETYPE_SWF:
1225
0
    case IMAGE_FILETYPE_SWC:
1226
0
      return "application/x-shockwave-flash";
1227
0
    case IMAGE_FILETYPE_PSD:
1228
0
      return "image/psd";
1229
0
    case IMAGE_FILETYPE_BMP:
1230
0
      return "image/bmp";
1231
0
    case IMAGE_FILETYPE_TIFF_II:
1232
0
    case IMAGE_FILETYPE_TIFF_MM:
1233
0
      return "image/tiff";
1234
0
    case IMAGE_FILETYPE_IFF:
1235
0
      return "image/iff";
1236
0
    case IMAGE_FILETYPE_WBMP:
1237
0
      return "image/vnd.wap.wbmp";
1238
0
    case IMAGE_FILETYPE_JPC:
1239
0
      return "application/octet-stream";
1240
0
    case IMAGE_FILETYPE_JP2:
1241
0
      return "image/jp2";
1242
0
    case IMAGE_FILETYPE_XBM:
1243
0
      return "image/xbm";
1244
0
    case IMAGE_FILETYPE_ICO:
1245
0
      return "image/vnd.microsoft.icon";
1246
0
    case IMAGE_FILETYPE_WEBP:
1247
0
      return "image/webp";
1248
0
    case IMAGE_FILETYPE_AVIF:
1249
0
      return "image/avif";
1250
0
    case IMAGE_FILETYPE_HEIF:
1251
0
      return "image/heif";
1252
0
    default: {
1253
0
      const struct php_image_handler *handler = zend_hash_index_find_ptr(&php_image_handlers, (zend_ulong) image_type);
1254
0
      if (handler) {
1255
0
        return handler->mime_type;
1256
0
      }
1257
0
      ZEND_FALLTHROUGH;
1258
0
    }
1259
0
    case IMAGE_FILETYPE_UNKNOWN:
1260
0
      return "application/octet-stream"; /* suppose binary format */
1261
0
  }
1262
0
}
1263
/* }}} */
1264
1265
/* {{{ Get Mime-Type for image-type returned by getimagesize, exif_read_data, exif_thumbnail, exif_imagetype */
1266
PHP_FUNCTION(image_type_to_mime_type)
1267
0
{
1268
0
  zend_long p_image_type;
1269
1270
0
  ZEND_PARSE_PARAMETERS_START(1, 1)
1271
0
    Z_PARAM_LONG(p_image_type)
1272
0
  ZEND_PARSE_PARAMETERS_END();
1273
1274
0
  ZVAL_STRING(return_value, php_image_type_to_mime_type(p_image_type));
1275
0
}
1276
/* }}} */
1277
1278
/* {{{ Get file extension for image-type returned by getimagesize, exif_read_data, exif_thumbnail, exif_imagetype */
1279
PHP_FUNCTION(image_type_to_extension)
1280
0
{
1281
0
  zend_long image_type;
1282
0
  bool inc_dot=1;
1283
0
  const char *imgext = NULL;
1284
1285
0
  ZEND_PARSE_PARAMETERS_START(1, 2)
1286
0
    Z_PARAM_LONG(image_type)
1287
0
    Z_PARAM_OPTIONAL
1288
0
    Z_PARAM_BOOL(inc_dot)
1289
0
  ZEND_PARSE_PARAMETERS_END();
1290
1291
0
  switch (image_type) {
1292
0
    case IMAGE_FILETYPE_GIF:
1293
0
      imgext = ".gif";
1294
0
      break;
1295
0
    case IMAGE_FILETYPE_JPEG:
1296
0
      imgext = ".jpeg";
1297
0
      break;
1298
0
    case IMAGE_FILETYPE_PNG:
1299
0
      imgext = ".png";
1300
0
      break;
1301
0
    case IMAGE_FILETYPE_SWF:
1302
0
    case IMAGE_FILETYPE_SWC:
1303
0
      imgext = ".swf";
1304
0
      break;
1305
0
    case IMAGE_FILETYPE_PSD:
1306
0
      imgext = ".psd";
1307
0
      break;
1308
0
    case IMAGE_FILETYPE_BMP:
1309
0
    case IMAGE_FILETYPE_WBMP:
1310
0
      imgext = ".bmp";
1311
0
      break;
1312
0
    case IMAGE_FILETYPE_TIFF_II:
1313
0
    case IMAGE_FILETYPE_TIFF_MM:
1314
0
      imgext = ".tiff";
1315
0
      break;
1316
0
    case IMAGE_FILETYPE_IFF:
1317
0
      imgext = ".iff";
1318
0
      break;
1319
0
    case IMAGE_FILETYPE_JPC:
1320
0
      imgext = ".jpc";
1321
0
      break;
1322
0
    case IMAGE_FILETYPE_JP2:
1323
0
      imgext = ".jp2";
1324
0
      break;
1325
0
    case IMAGE_FILETYPE_JPX:
1326
0
      imgext = ".jpx";
1327
0
      break;
1328
0
    case IMAGE_FILETYPE_JB2:
1329
0
      imgext = ".jb2";
1330
0
      break;
1331
0
    case IMAGE_FILETYPE_XBM:
1332
0
      imgext = ".xbm";
1333
0
      break;
1334
0
    case IMAGE_FILETYPE_ICO:
1335
0
      imgext = ".ico";
1336
0
      break;
1337
0
    case IMAGE_FILETYPE_WEBP:
1338
0
      imgext = ".webp";
1339
0
      break;
1340
0
    case IMAGE_FILETYPE_AVIF:
1341
0
      imgext = ".avif";
1342
0
      break;
1343
0
    case IMAGE_FILETYPE_HEIF:
1344
0
      imgext = ".heif";
1345
0
      break;
1346
0
    default: {
1347
0
      const struct php_image_handler *handler = zend_hash_index_find_ptr(&php_image_handlers, (zend_ulong) image_type);
1348
0
      if (handler) {
1349
0
        imgext = handler->extension;
1350
0
      }
1351
0
      break;
1352
0
    }
1353
0
  }
1354
1355
0
  if (imgext) {
1356
0
    RETURN_STRING(&imgext[!inc_dot]);
1357
0
  }
1358
1359
0
  RETURN_FALSE;
1360
0
}
1361
/* }}} */
1362
1363
/* {{{ php_imagetype
1364
   detect filetype from first bytes */
1365
PHPAPI int php_getimagetype(php_stream *stream, const char *input, char *filetype)
1366
0
{
1367
0
  char tmp[12];
1368
0
  int twelve_bytes_read;
1369
1370
0
  if ( !filetype) filetype = tmp;
1371
0
  if((php_stream_read(stream, filetype, 3)) != 3) {
1372
0
    php_error_docref(NULL, E_NOTICE, "Error reading from %s!", input);
1373
0
    return IMAGE_FILETYPE_UNKNOWN;
1374
0
  }
1375
1376
/* BYTES READ: 3 */
1377
0
  if (!memcmp(filetype, php_sig_gif, 3)) {
1378
0
    return IMAGE_FILETYPE_GIF;
1379
0
  } else if (!memcmp(filetype, php_sig_jpg, 3)) {
1380
0
    return IMAGE_FILETYPE_JPEG;
1381
0
  } else if (!memcmp(filetype, php_sig_png, 3)) {
1382
0
    if (php_stream_read(stream, filetype+3, 5) != 5) {
1383
0
      php_error_docref(NULL, E_NOTICE, "Error reading from %s!", input);
1384
0
      return IMAGE_FILETYPE_UNKNOWN;
1385
0
    }
1386
0
    if (!memcmp(filetype, php_sig_png, 8)) {
1387
0
      return IMAGE_FILETYPE_PNG;
1388
0
    } else {
1389
0
      php_error_docref(NULL, E_WARNING, "PNG file corrupted by ASCII conversion");
1390
0
      return IMAGE_FILETYPE_UNKNOWN;
1391
0
    }
1392
0
  } else if (!memcmp(filetype, php_sig_swf, 3)) {
1393
0
    return IMAGE_FILETYPE_SWF;
1394
0
  } else if (!memcmp(filetype, php_sig_swc, 3)) {
1395
0
    return IMAGE_FILETYPE_SWC;
1396
0
  } else if (!memcmp(filetype, php_sig_psd, 3)) {
1397
0
    return IMAGE_FILETYPE_PSD;
1398
0
  } else if (!memcmp(filetype, php_sig_bmp, 2)) {
1399
0
    return IMAGE_FILETYPE_BMP;
1400
0
  } else if (!memcmp(filetype, php_sig_jpc, 3)) {
1401
0
    return IMAGE_FILETYPE_JPC;
1402
0
  } else if (!memcmp(filetype, php_sig_riff, 3)) {
1403
0
    if (php_stream_read(stream, filetype+3, 9) != 9) {
1404
0
      php_error_docref(NULL, E_NOTICE, "Error reading from %s!", input);
1405
0
      return IMAGE_FILETYPE_UNKNOWN;
1406
0
    }
1407
0
    if (!memcmp(filetype+8, php_sig_webp, 4)) {
1408
0
      return IMAGE_FILETYPE_WEBP;
1409
0
    } else {
1410
0
      return IMAGE_FILETYPE_UNKNOWN;
1411
0
    }
1412
0
  }
1413
1414
0
  if (php_stream_read(stream, filetype+3, 1) != 1) {
1415
0
    php_error_docref(NULL, E_NOTICE, "Error reading from %s!", input);
1416
0
    return IMAGE_FILETYPE_UNKNOWN;
1417
0
  }
1418
/* BYTES READ: 4 */
1419
0
  if (!memcmp(filetype, php_sig_tif_ii, 4)) {
1420
0
    return IMAGE_FILETYPE_TIFF_II;
1421
0
  } else if (!memcmp(filetype, php_sig_tif_mm, 4)) {
1422
0
    return IMAGE_FILETYPE_TIFF_MM;
1423
0
  } else if (!memcmp(filetype, php_sig_iff, 4)) {
1424
0
    return IMAGE_FILETYPE_IFF;
1425
0
  } else if (!memcmp(filetype, php_sig_ico, 4)) {
1426
0
    return IMAGE_FILETYPE_ICO;
1427
0
  }
1428
1429
  /* WBMP may be smaller than 12 bytes, so delay error */
1430
0
  twelve_bytes_read = (php_stream_read(stream, filetype+4, 8) == 8);
1431
1432
/* BYTES READ: 12 */
1433
0
  if (twelve_bytes_read && !memcmp(filetype, php_sig_jp2, 12)) {
1434
0
    return IMAGE_FILETYPE_JP2;
1435
0
  }
1436
1437
0
  if (!php_stream_rewind(stream) && php_is_image_avif(stream)) {
1438
0
    return IMAGE_FILETYPE_AVIF;
1439
0
  }
1440
1441
  /* See GH-20201: this needs to be after avif checks to avoid identifying avif as heif. */
1442
0
  if (twelve_bytes_read && !memcmp(filetype + 4, php_sig_ftyp, 4) &&
1443
0
    (!memcmp(filetype + 8, php_sig_mif1, 4) || !memcmp(filetype + 8, php_sig_heic, 4) || !memcmp(filetype + 8, php_sig_heix, 4))) {
1444
0
    return IMAGE_FILETYPE_HEIF;
1445
0
  }
1446
1447
/* AFTER ALL ABOVE FAILED */
1448
0
  if (php_get_wbmp(stream, NULL, 1)) {
1449
0
    return IMAGE_FILETYPE_WBMP;
1450
0
  }
1451
1452
0
  if (!twelve_bytes_read) {
1453
0
    php_error_docref(NULL, E_NOTICE, "Error reading from %s!", input);
1454
0
    return IMAGE_FILETYPE_UNKNOWN;
1455
0
  }
1456
1457
0
  if (php_get_xbm(stream, NULL)) {
1458
0
    return IMAGE_FILETYPE_XBM;
1459
0
  }
1460
1461
0
  zend_ulong h;
1462
0
  zval *zv;
1463
0
  ZEND_HASH_FOREACH_NUM_KEY_VAL(&php_image_handlers, h, zv) {
1464
0
    const struct php_image_handler *handler = Z_PTR_P(zv);
1465
0
    if (handler->identify(stream) == SUCCESS) {
1466
0
      return (int) h;
1467
0
    }
1468
0
  } ZEND_HASH_FOREACH_END();
1469
1470
0
  return IMAGE_FILETYPE_UNKNOWN;
1471
0
}
1472
/* }}} */
1473
1474
static void php_getimagesize_from_stream(php_stream *stream, char *input, zval *info, INTERNAL_FUNCTION_PARAMETERS) /* {{{ */
1475
0
{
1476
0
  int itype = 0;
1477
0
  struct php_gfxinfo *result = NULL;
1478
0
  const char *mime_type = NULL;
1479
1480
0
  if (!stream) {
1481
0
    RETURN_FALSE;
1482
0
  }
1483
1484
0
  itype = php_getimagetype(stream, input, NULL);
1485
0
  switch( itype) {
1486
0
    case IMAGE_FILETYPE_GIF:
1487
0
      result = php_handle_gif(stream);
1488
0
      break;
1489
0
    case IMAGE_FILETYPE_JPEG:
1490
0
      if (info) {
1491
0
        result = php_handle_jpeg(stream, info);
1492
0
      } else {
1493
0
        result = php_handle_jpeg(stream, NULL);
1494
0
      }
1495
0
      break;
1496
0
    case IMAGE_FILETYPE_PNG:
1497
0
      result = php_handle_png(stream);
1498
0
      break;
1499
0
    case IMAGE_FILETYPE_SWF:
1500
0
      result = php_handle_swf(stream);
1501
0
      break;
1502
0
    case IMAGE_FILETYPE_SWC:
1503
      /* TODO: with the new php_image_register_handler() APIs, this restriction could be solved */
1504
#if defined(HAVE_ZLIB) && !defined(COMPILE_DL_ZLIB)
1505
      result = php_handle_swc(stream);
1506
#else
1507
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");
1508
0
#endif
1509
0
      break;
1510
0
    case IMAGE_FILETYPE_PSD:
1511
0
      result = php_handle_psd(stream);
1512
0
      break;
1513
0
    case IMAGE_FILETYPE_BMP:
1514
0
      result = php_handle_bmp(stream);
1515
0
      break;
1516
0
    case IMAGE_FILETYPE_TIFF_II:
1517
0
      result = php_handle_tiff(stream, NULL, 0);
1518
0
      break;
1519
0
    case IMAGE_FILETYPE_TIFF_MM:
1520
0
      result = php_handle_tiff(stream, NULL, 1);
1521
0
      break;
1522
0
    case IMAGE_FILETYPE_JPC:
1523
0
      result = php_handle_jpc(stream);
1524
0
      break;
1525
0
    case IMAGE_FILETYPE_JP2:
1526
0
      result = php_handle_jp2(stream);
1527
0
      break;
1528
0
    case IMAGE_FILETYPE_IFF:
1529
0
      result = php_handle_iff(stream);
1530
0
      break;
1531
0
    case IMAGE_FILETYPE_WBMP:
1532
0
      result = php_handle_wbmp(stream);
1533
0
      break;
1534
0
    case IMAGE_FILETYPE_XBM:
1535
0
      result = php_handle_xbm(stream);
1536
0
      break;
1537
0
    case IMAGE_FILETYPE_ICO:
1538
0
      result = php_handle_ico(stream);
1539
0
      break;
1540
0
    case IMAGE_FILETYPE_WEBP:
1541
0
      result = php_handle_webp(stream);
1542
0
      break;
1543
0
    case IMAGE_FILETYPE_AVIF:
1544
0
      result = php_handle_avif(stream);
1545
0
      break;
1546
0
    case IMAGE_FILETYPE_HEIF:
1547
0
      if (!php_stream_rewind(stream)) {
1548
0
        result = php_handle_avif(stream);
1549
0
      }
1550
0
      break;
1551
0
    default: {
1552
0
      struct php_image_handler* handler = zend_hash_index_find_ptr(&php_image_handlers, (zend_ulong) itype);
1553
0
      if (handler) {
1554
0
        result = handler->get_info(stream);
1555
0
        mime_type = handler->mime_type;
1556
0
        break;
1557
0
      }
1558
0
      ZEND_FALLTHROUGH;
1559
0
    }
1560
0
    case IMAGE_FILETYPE_UNKNOWN:
1561
0
      break;
1562
0
  }
1563
1564
0
  if (result) {
1565
0
    array_init(return_value);
1566
0
    add_index_long(return_value, 0, result->width);
1567
0
    add_index_long(return_value, 1, result->height);
1568
0
    add_index_long(return_value, 2, itype);
1569
0
    if ((!result->width_unit || zend_string_equals_literal(result->width_unit, "px"))
1570
0
      && (!result->height_unit || zend_string_equals_literal(result->height_unit, "px"))) {
1571
0
      char temp[MAX_LENGTH_OF_LONG * 2 + sizeof("width=\"\" height=\"\"")];
1572
0
      snprintf(temp, sizeof(temp), "width=\"%d\" height=\"%d\"", result->width, result->height);
1573
0
      add_index_string(return_value, 3, temp);
1574
0
    }
1575
1576
0
    if (result->bits != 0) {
1577
0
      add_assoc_long(return_value, "bits", result->bits);
1578
0
    }
1579
0
    if (result->channels != 0) {
1580
0
      add_assoc_long(return_value, "channels", result->channels);
1581
0
    }
1582
0
    add_assoc_string(return_value, "mime", mime_type ? mime_type : php_image_type_to_mime_type(itype));
1583
1584
0
    if (result->width_unit) {
1585
0
      add_assoc_str(return_value, "width_unit", result->width_unit);
1586
0
    } else {
1587
0
      add_assoc_string(return_value, "width_unit", "px");
1588
0
    }
1589
0
    if (result->height_unit) {
1590
0
      add_assoc_str(return_value, "height_unit", result->height_unit);
1591
0
    } else {
1592
0
      add_assoc_string(return_value, "height_unit", "px");
1593
0
    }
1594
1595
0
    efree(result);
1596
0
  } else {
1597
0
    RETURN_FALSE;
1598
0
  }
1599
0
}
1600
/* }}} */
1601
1602
0
#define FROM_DATA 0
1603
0
#define FROM_PATH 1
1604
1605
0
static void php_getimagesize_from_any(INTERNAL_FUNCTION_PARAMETERS, int mode) {  /* {{{ */
1606
0
  zval *info = NULL;
1607
0
  php_stream *stream = NULL;
1608
0
  zend_string *input;
1609
0
  const int argc = ZEND_NUM_ARGS();
1610
1611
0
  ZEND_PARSE_PARAMETERS_START(1, 2)
1612
0
    Z_PARAM_STR(input)
1613
0
    Z_PARAM_OPTIONAL
1614
0
    Z_PARAM_ZVAL(info)
1615
0
  ZEND_PARSE_PARAMETERS_END();
1616
1617
0
  if (mode == FROM_PATH && zend_str_has_nul_byte(input)) {
1618
0
    zend_argument_value_error(1, "must not contain any null bytes");
1619
0
    RETURN_THROWS();
1620
0
  }
1621
1622
0
  if (argc == 2) {
1623
0
    info = zend_try_array_init(info);
1624
0
    if (!info) {
1625
0
      RETURN_THROWS();
1626
0
    }
1627
0
  }
1628
1629
0
  if (mode == FROM_PATH) {
1630
0
    stream = php_stream_open_wrapper(ZSTR_VAL(input), "rb", STREAM_MUST_SEEK|REPORT_ERRORS|IGNORE_PATH, NULL);
1631
0
  } else {
1632
0
    stream = php_stream_memory_open(TEMP_STREAM_READONLY, input);
1633
0
  }
1634
1635
0
  if (!stream) {
1636
0
    RETURN_FALSE;
1637
0
  }
1638
1639
0
  php_getimagesize_from_stream(stream, ZSTR_VAL(input), info, INTERNAL_FUNCTION_PARAM_PASSTHRU);
1640
0
  php_stream_close(stream);
1641
0
}
1642
/* }}} */
1643
1644
/* {{{ Get the size of an image as 4-element array */
1645
PHP_FUNCTION(getimagesize)
1646
0
{
1647
0
  php_getimagesize_from_any(INTERNAL_FUNCTION_PARAM_PASSTHRU, FROM_PATH);
1648
0
}
1649
/* }}} */
1650
1651
/* {{{ Get the size of an image as 4-element array */
1652
PHP_FUNCTION(getimagesizefromstring)
1653
0
{
1654
0
  php_getimagesize_from_any(INTERNAL_FUNCTION_PARAM_PASSTHRU, FROM_DATA);
1655
0
}
1656
/* }}} */
1657
1658
PHP_MINIT_FUNCTION(image)
1659
2
{
1660
2
  zend_hash_init(&php_image_handlers, 4, NULL, NULL, true);
1661
2
  return SUCCESS;
1662
2
}
1663
1664
PHP_MSHUTDOWN_FUNCTION(image)
1665
0
{
1666
#ifdef ZTS
1667
  if (!tsrm_is_main_thread()) {
1668
    return SUCCESS;
1669
  }
1670
#endif
1671
0
  zend_hash_destroy(&php_image_handlers);
1672
0
  return SUCCESS;
1673
0
}
1674
1675
extern zend_module_entry basic_functions_module;
1676
1677
int php_image_register_handler(const struct php_image_handler *handler)
1678
0
{
1679
0
  zend_hash_index_add_ptr(&php_image_handlers, (zend_ulong) php_image_handler_next_id, (void *) handler);
1680
0
  zend_register_long_constant(handler->const_name, strlen(handler->const_name), php_image_handler_next_id, CONST_PERSISTENT, basic_functions_module.module_number);
1681
0
  Z_LVAL_P(zend_get_constant_str(ZEND_STRL("IMAGETYPE_COUNT")))++;
1682
0
  return php_image_handler_next_id++;
1683
0
}
1684
1685
zend_result php_image_unregister_handler(int image_type)
1686
0
{
1687
0
  ZEND_ASSERT(image_type >= IMAGE_FILETYPE_FIXED_COUNT);
1688
0
  return zend_hash_index_del(&php_image_handlers, (zend_ulong) image_type);
1689
0
}