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