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