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