PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/standard - image.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 493 603 81.8 %
Date: 2016-09-27 Functions: 34 35 97.1 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Tue, 27 Sep 2016 10:26:07 +0000 (2 days ago)

Copyright © 2005-2016 The PHP Group
All rights reserved.