Coverage Report

Created: 2025-07-23 06:33

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