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