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/exif - exif.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 929 1278 72.7 %
Date: 2014-11-10 Functions: 55 58 94.8 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 7                                                        |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1997-2014 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: 06e1dae41ae678068272db10cb6092dce68fd27c $ */
      21             : 
      22             : /*  ToDos
      23             :  *
      24             :  *      See if example images from http://www.exif.org have illegal
      25             :  *              thumbnail sizes or if code is corrupt.
      26             :  *      Create/Update exif headers.
      27             :  *      Create/Remove/Update image thumbnails.
      28             :  */
      29             : 
      30             : /*  Security
      31             :  *
      32             :  *  At current time i do not see any security problems but a potential
      33             :  *  attacker could generate an image with recursive ifd pointers...(Marcus)
      34             :  */
      35             : 
      36             : #ifdef HAVE_CONFIG_H
      37             : #include "config.h"
      38             : #endif
      39             : 
      40             : #include "php.h"
      41             : #include "ext/standard/file.h"
      42             : 
      43             : #if HAVE_EXIF
      44             : 
      45             : /* When EXIF_DEBUG is defined the module generates a lot of debug messages
      46             :  * that help understanding what is going on. This can and should be used
      47             :  * while extending the module as it shows if you are at the right position.
      48             :  * You are always considered to have a copy of TIFF6.0 and EXIF2.10 standard.
      49             :  */
      50             : #undef EXIF_DEBUG
      51             : 
      52             : #ifdef EXIF_DEBUG
      53             : #define EXIFERR_DC , const char *_file, size_t _line TSRMLS_DC
      54             : #define EXIFERR_CC , __FILE__, __LINE__ TSRMLS_CC
      55             : #else
      56             : #define EXIFERR_DC TSRMLS_DC
      57             : #define EXIFERR_CC TSRMLS_CC
      58             : #endif
      59             : 
      60             : #undef EXIF_JPEG2000
      61             : 
      62             : #include "php_exif.h"
      63             : #include <math.h>
      64             : #include "php_ini.h"
      65             : #include "ext/standard/php_string.h"
      66             : #include "ext/standard/php_image.h"
      67             : #include "ext/standard/info.h" 
      68             : 
      69             : /* needed for ssize_t definition */
      70             : #include <sys/types.h>
      71             : 
      72             : typedef unsigned char uchar;
      73             : 
      74             : #ifndef safe_emalloc
      75             : # define safe_emalloc(a,b,c) emalloc((a)*(b)+(c))
      76             : #endif
      77             : #ifndef safe_erealloc
      78             : # define safe_erealloc(p,a,b,c) erealloc(p, (a)*(b)+(c))
      79             : #endif
      80             : 
      81             : #ifndef TRUE
      82             : #       define TRUE 1
      83             : #       define FALSE 0
      84             : #endif
      85             : 
      86             : #ifndef max
      87             : #       define max(a,b) ((a)>(b) ? (a) : (b))
      88             : #endif
      89             : 
      90             : #define EFREE_IF(ptr)   if (ptr) efree(ptr)
      91             : 
      92             : #define MAX_IFD_NESTING_LEVEL 100
      93             : 
      94             : /* {{{ arginfo */
      95             : ZEND_BEGIN_ARG_INFO(arginfo_exif_tagname, 0)
      96             :         ZEND_ARG_INFO(0, index)
      97             : ZEND_END_ARG_INFO()
      98             : 
      99             : ZEND_BEGIN_ARG_INFO_EX(arginfo_exif_read_data, 0, 0, 1)
     100             :         ZEND_ARG_INFO(0, filename)
     101             :         ZEND_ARG_INFO(0, sections_needed)
     102             :         ZEND_ARG_INFO(0, sub_arrays)
     103             :         ZEND_ARG_INFO(0, read_thumbnail)
     104             : ZEND_END_ARG_INFO()
     105             : 
     106             : ZEND_BEGIN_ARG_INFO_EX(arginfo_exif_thumbnail, 0, 0, 1)
     107             :         ZEND_ARG_INFO(0, filename)
     108             :         ZEND_ARG_INFO(1, width)
     109             :         ZEND_ARG_INFO(1, height)
     110             :         ZEND_ARG_INFO(1, imagetype)
     111             : ZEND_END_ARG_INFO()
     112             : 
     113             : ZEND_BEGIN_ARG_INFO(arginfo_exif_imagetype, 0)
     114             :         ZEND_ARG_INFO(0, imagefile)
     115             : ZEND_END_ARG_INFO()
     116             : 
     117             : /* }}} */
     118             : 
     119             : /* {{{ exif_functions[]
     120             :  */
     121             : const zend_function_entry exif_functions[] = {
     122             :         PHP_FE(exif_read_data, arginfo_exif_read_data)
     123             :         PHP_FALIAS(read_exif_data, exif_read_data, arginfo_exif_read_data)
     124             :         PHP_FE(exif_tagname, arginfo_exif_tagname)
     125             :         PHP_FE(exif_thumbnail, arginfo_exif_thumbnail)
     126             :         PHP_FE(exif_imagetype, arginfo_exif_imagetype)
     127             :         PHP_FE_END
     128             : };
     129             : /* }}} */
     130             : 
     131             : #define EXIF_VERSION "1.4 $Id: 06e1dae41ae678068272db10cb6092dce68fd27c $"
     132             : 
     133             : /* {{{ PHP_MINFO_FUNCTION
     134             :  */
     135         143 : PHP_MINFO_FUNCTION(exif)
     136             : {
     137         143 :         php_info_print_table_start();
     138         143 :         php_info_print_table_row(2, "EXIF Support", "enabled");
     139         143 :         php_info_print_table_row(2, "EXIF Version", EXIF_VERSION);
     140         143 :         php_info_print_table_row(2, "Supported EXIF Version", "0220");
     141         143 :         php_info_print_table_row(2, "Supported filetypes", "JPEG,TIFF");
     142         143 :         php_info_print_table_end();
     143         143 :         DISPLAY_INI_ENTRIES();
     144         143 : }
     145             : /* }}} */
     146             : 
     147             : ZEND_BEGIN_MODULE_GLOBALS(exif)
     148             :         char * encode_unicode;
     149             :         char * decode_unicode_be;
     150             :         char * decode_unicode_le;
     151             :         char * encode_jis;
     152             :         char * decode_jis_be;
     153             :         char * decode_jis_le;
     154             : ZEND_END_MODULE_GLOBALS(exif) 
     155             : 
     156             : ZEND_DECLARE_MODULE_GLOBALS(exif)
     157             : 
     158             : #ifdef ZTS
     159             : #define EXIF_G(v) TSRMG(exif_globals_id, zend_exif_globals *, v)
     160             : #else
     161             : #define EXIF_G(v) (exif_globals.v)
     162             : #endif
     163             :  
     164             : /* {{{ PHP_INI
     165             :  */
     166             : 
     167       40946 : ZEND_INI_MH(OnUpdateEncode)
     168             : {
     169       40946 :         if (new_value && new_value->len) {
     170             :                 const zend_encoding **return_list;
     171             :                 size_t return_size;
     172       20473 :                 if (FAILURE == zend_multibyte_parse_encoding_list(new_value->val, new_value->len,
     173             :         &return_list, &return_size, 0 TSRMLS_CC)) {
     174           0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Illegal encoding ignored: '%s'", new_value->val);
     175           0 :                         return FAILURE;
     176             :                 }
     177       20473 :                 efree(return_list);
     178             :         }
     179       40946 :         return OnUpdateString(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
     180             : }
     181             : 
     182       81892 : ZEND_INI_MH(OnUpdateDecode)
     183             : {
     184       81892 :         if (new_value) {
     185             :                 const zend_encoding **return_list;
     186             :                 size_t return_size;
     187       81892 :                 if (FAILURE == zend_multibyte_parse_encoding_list(new_value->val, new_value->len,
     188             :         &return_list, &return_size, 0 TSRMLS_CC)) {
     189           0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Illegal encoding ignored: '%s'", new_value->val);
     190           0 :                         return FAILURE;
     191             :                 }
     192       81892 :                 efree(return_list);
     193             :         }
     194       81892 :         return OnUpdateString(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
     195             : }
     196             : 
     197             : PHP_INI_BEGIN()
     198             :     STD_PHP_INI_ENTRY("exif.encode_unicode",          "ISO-8859-15", PHP_INI_ALL, OnUpdateEncode, encode_unicode,    zend_exif_globals, exif_globals)
     199             :     STD_PHP_INI_ENTRY("exif.decode_unicode_motorola", "UCS-2BE",     PHP_INI_ALL, OnUpdateDecode, decode_unicode_be, zend_exif_globals, exif_globals)
     200             :     STD_PHP_INI_ENTRY("exif.decode_unicode_intel",    "UCS-2LE",     PHP_INI_ALL, OnUpdateDecode, decode_unicode_le, zend_exif_globals, exif_globals)
     201             :     STD_PHP_INI_ENTRY("exif.encode_jis",              "",            PHP_INI_ALL, OnUpdateEncode, encode_jis,        zend_exif_globals, exif_globals)
     202             :     STD_PHP_INI_ENTRY("exif.decode_jis_motorola",     "JIS",         PHP_INI_ALL, OnUpdateDecode, decode_jis_be,     zend_exif_globals, exif_globals)
     203             :     STD_PHP_INI_ENTRY("exif.decode_jis_intel",        "JIS",         PHP_INI_ALL, OnUpdateDecode, decode_jis_le,     zend_exif_globals, exif_globals)
     204             : PHP_INI_END()
     205             : /* }}} */
     206             :  
     207             : /* {{{ PHP_GINIT_FUNCTION
     208             :  */
     209       20473 : static PHP_GINIT_FUNCTION(exif)
     210             : {
     211       20473 :         exif_globals->encode_unicode    = NULL;
     212       20473 :         exif_globals->decode_unicode_be = NULL;
     213       20473 :         exif_globals->decode_unicode_le = NULL;
     214       20473 :         exif_globals->encode_jis        = NULL;
     215       20473 :         exif_globals->decode_jis_be     = NULL;
     216       20473 :         exif_globals->decode_jis_le     = NULL;
     217       20473 : }
     218             : /* }}} */
     219             : 
     220             : /* {{{ PHP_MINIT_FUNCTION(exif)
     221             :    Get the size of an image as 4-element array */
     222       20473 : PHP_MINIT_FUNCTION(exif)
     223             : {
     224       20473 :         REGISTER_INI_ENTRIES();
     225       20473 :         if (zend_hash_str_exists(&module_registry, "mbstring", sizeof("mbstring")-1)) {
     226       20473 :                 REGISTER_LONG_CONSTANT("EXIF_USE_MBSTRING", 1, CONST_CS | CONST_PERSISTENT); 
     227             :         } else {
     228           0 :                 REGISTER_LONG_CONSTANT("EXIF_USE_MBSTRING", 0, CONST_CS | CONST_PERSISTENT); 
     229             :         }
     230       20473 :         return SUCCESS;
     231             : }
     232             : /* }}} */
     233             : 
     234             : /* {{{ PHP_MSHUTDOWN_FUNCTION
     235             :  */
     236       20507 : PHP_MSHUTDOWN_FUNCTION(exif)
     237             : {
     238       20507 :         UNREGISTER_INI_ENTRIES();
     239       20507 :         return SUCCESS;
     240             : }
     241             : /* }}} */
     242             : 
     243             : /* {{{ exif dependencies */
     244             : static const zend_module_dep exif_module_deps[] = {
     245             :         ZEND_MOD_REQUIRED("standard")
     246             :         ZEND_MOD_OPTIONAL("mbstring")
     247             :         ZEND_MOD_END
     248             : };
     249             : /* }}} */
     250             : 
     251             : /* {{{ exif_module_entry
     252             :  */
     253             : zend_module_entry exif_module_entry = {
     254             :         STANDARD_MODULE_HEADER_EX, NULL,
     255             :         exif_module_deps,
     256             :         "exif",
     257             :         exif_functions,
     258             :         PHP_MINIT(exif), 
     259             :         PHP_MSHUTDOWN(exif),
     260             :         NULL, NULL,
     261             :         PHP_MINFO(exif),
     262             : #if ZEND_MODULE_API_NO >= 20010901
     263             :         EXIF_VERSION,
     264             : #endif
     265             : #if ZEND_MODULE_API_NO >= 20060613
     266             :         PHP_MODULE_GLOBALS(exif),
     267             :         PHP_GINIT(exif),
     268             :         NULL,
     269             :         NULL,
     270             :         STANDARD_MODULE_PROPERTIES_EX
     271             : #else   
     272             :         STANDARD_MODULE_PROPERTIES
     273             : #endif
     274             : };
     275             : /* }}} */
     276             : 
     277             : #ifdef COMPILE_DL_EXIF
     278             : ZEND_GET_MODULE(exif)
     279             : #endif
     280             : 
     281             : /* {{{ php_strnlen
     282             :  * get length of string if buffer if less than buffer size or buffer size */
     283         227 : static size_t php_strnlen(char* str, size_t maxlen) {
     284         227 :         size_t len = 0;
     285             : 
     286         227 :         if (str && maxlen && *str) {
     287             :                 do {
     288        2789 :                         len++;
     289        2789 :                 } while (--maxlen && *(++str));
     290             :         }
     291         227 :         return len;
     292             : }
     293             : /* }}} */
     294             : 
     295             : /* {{{ error messages
     296             : */
     297             : static const char * EXIF_ERROR_FILEEOF   = "Unexpected end of file reached";
     298             : static const char * EXIF_ERROR_CORRUPT   = "File structure corrupted";
     299             : static const char * EXIF_ERROR_THUMBEOF  = "Thumbnail goes IFD boundary or end of file reached";
     300             : static const char * EXIF_ERROR_FSREALLOC = "Illegal reallocating of undefined file section";
     301             : 
     302             : #define EXIF_ERRLOG_FILEEOF(ImageInfo)    exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "%s", EXIF_ERROR_FILEEOF);
     303             : #define EXIF_ERRLOG_CORRUPT(ImageInfo)    exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "%s", EXIF_ERROR_CORRUPT);
     304             : #define EXIF_ERRLOG_THUMBEOF(ImageInfo)   exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "%s", EXIF_ERROR_THUMBEOF);
     305             : #define EXIF_ERRLOG_FSREALLOC(ImageInfo)  exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "%s", EXIF_ERROR_FSREALLOC);
     306             : /* }}} */
     307             : 
     308             : /* {{{ format description defines
     309             :    Describes format descriptor
     310             : */
     311             : static int php_tiff_bytes_per_format[] = {0, 1, 1, 2, 4, 8, 1, 1, 2, 4, 8, 4, 8, 1};
     312             : #define NUM_FORMATS 13
     313             : 
     314             : #define TAG_FMT_BYTE       1
     315             : #define TAG_FMT_STRING     2
     316             : #define TAG_FMT_USHORT     3
     317             : #define TAG_FMT_ULONG      4
     318             : #define TAG_FMT_URATIONAL  5
     319             : #define TAG_FMT_SBYTE      6
     320             : #define TAG_FMT_UNDEFINED  7
     321             : #define TAG_FMT_SSHORT     8
     322             : #define TAG_FMT_SLONG      9
     323             : #define TAG_FMT_SRATIONAL 10
     324             : #define TAG_FMT_SINGLE    11
     325             : #define TAG_FMT_DOUBLE    12
     326             : #define TAG_FMT_IFD       13
     327             : 
     328             : #ifdef EXIF_DEBUG
     329             : static char *exif_get_tagformat(int format)
     330             : {
     331             :         switch(format) {
     332             :                 case TAG_FMT_BYTE:      return "BYTE";
     333             :                 case TAG_FMT_STRING:    return "STRING";
     334             :                 case TAG_FMT_USHORT:    return "USHORT";
     335             :                 case TAG_FMT_ULONG:     return "ULONG";
     336             :                 case TAG_FMT_URATIONAL: return "URATIONAL";
     337             :                 case TAG_FMT_SBYTE:     return "SBYTE";
     338             :                 case TAG_FMT_UNDEFINED: return "UNDEFINED";
     339             :                 case TAG_FMT_SSHORT:    return "SSHORT";
     340             :                 case TAG_FMT_SLONG:     return "SLONG";
     341             :                 case TAG_FMT_SRATIONAL: return "SRATIONAL";
     342             :                 case TAG_FMT_SINGLE:    return "SINGLE";
     343             :                 case TAG_FMT_DOUBLE:    return "DOUBLE";
     344             :                 case TAG_FMT_IFD:       return "IFD";
     345             :         }
     346             :         return "*Illegal";
     347             : }
     348             : #endif
     349             : 
     350             : /* Describes tag values */
     351             : #define TAG_GPS_VERSION_ID              0x0000
     352             : #define TAG_GPS_LATITUDE_REF            0x0001
     353             : #define TAG_GPS_LATITUDE                0x0002
     354             : #define TAG_GPS_LONGITUDE_REF           0x0003
     355             : #define TAG_GPS_LONGITUDE               0x0004
     356             : #define TAG_GPS_ALTITUDE_REF            0x0005
     357             : #define TAG_GPS_ALTITUDE                0x0006
     358             : #define TAG_GPS_TIME_STAMP              0x0007
     359             : #define TAG_GPS_SATELLITES              0x0008
     360             : #define TAG_GPS_STATUS                  0x0009
     361             : #define TAG_GPS_MEASURE_MODE            0x000A
     362             : #define TAG_GPS_DOP                     0x000B
     363             : #define TAG_GPS_SPEED_REF               0x000C
     364             : #define TAG_GPS_SPEED                   0x000D
     365             : #define TAG_GPS_TRACK_REF               0x000E
     366             : #define TAG_GPS_TRACK                   0x000F
     367             : #define TAG_GPS_IMG_DIRECTION_REF       0x0010
     368             : #define TAG_GPS_IMG_DIRECTION           0x0011
     369             : #define TAG_GPS_MAP_DATUM               0x0012
     370             : #define TAG_GPS_DEST_LATITUDE_REF       0x0013
     371             : #define TAG_GPS_DEST_LATITUDE           0x0014
     372             : #define TAG_GPS_DEST_LONGITUDE_REF      0x0015
     373             : #define TAG_GPS_DEST_LONGITUDE          0x0016
     374             : #define TAG_GPS_DEST_BEARING_REF        0x0017
     375             : #define TAG_GPS_DEST_BEARING            0x0018
     376             : #define TAG_GPS_DEST_DISTANCE_REF       0x0019
     377             : #define TAG_GPS_DEST_DISTANCE           0x001A
     378             : #define TAG_GPS_PROCESSING_METHOD       0x001B
     379             : #define TAG_GPS_AREA_INFORMATION        0x001C
     380             : #define TAG_GPS_DATE_STAMP              0x001D
     381             : #define TAG_GPS_DIFFERENTIAL            0x001E
     382             : #define TAG_TIFF_COMMENT                0x00FE /* SHOUDLNT HAPPEN */
     383             : #define TAG_NEW_SUBFILE                 0x00FE /* New version of subfile tag */
     384             : #define TAG_SUBFILE_TYPE                0x00FF /* Old version of subfile tag */
     385             : #define TAG_IMAGEWIDTH                  0x0100
     386             : #define TAG_IMAGEHEIGHT                 0x0101
     387             : #define TAG_BITS_PER_SAMPLE             0x0102
     388             : #define TAG_COMPRESSION                 0x0103
     389             : #define TAG_PHOTOMETRIC_INTERPRETATION  0x0106
     390             : #define TAG_TRESHHOLDING                0x0107
     391             : #define TAG_CELL_WIDTH                  0x0108
     392             : #define TAG_CELL_HEIGHT                 0x0109
     393             : #define TAG_FILL_ORDER                  0x010A
     394             : #define TAG_DOCUMENT_NAME               0x010D
     395             : #define TAG_IMAGE_DESCRIPTION           0x010E
     396             : #define TAG_MAKE                        0x010F
     397             : #define TAG_MODEL                       0x0110
     398             : #define TAG_STRIP_OFFSETS               0x0111
     399             : #define TAG_ORIENTATION                 0x0112
     400             : #define TAG_SAMPLES_PER_PIXEL           0x0115
     401             : #define TAG_ROWS_PER_STRIP              0x0116
     402             : #define TAG_STRIP_BYTE_COUNTS           0x0117
     403             : #define TAG_MIN_SAMPPLE_VALUE           0x0118
     404             : #define TAG_MAX_SAMPLE_VALUE            0x0119
     405             : #define TAG_X_RESOLUTION                0x011A
     406             : #define TAG_Y_RESOLUTION                0x011B
     407             : #define TAG_PLANAR_CONFIGURATION        0x011C
     408             : #define TAG_PAGE_NAME                   0x011D
     409             : #define TAG_X_POSITION                  0x011E
     410             : #define TAG_Y_POSITION                  0x011F
     411             : #define TAG_FREE_OFFSETS                0x0120
     412             : #define TAG_FREE_BYTE_COUNTS            0x0121
     413             : #define TAG_GRAY_RESPONSE_UNIT          0x0122
     414             : #define TAG_GRAY_RESPONSE_CURVE         0x0123
     415             : #define TAG_RESOLUTION_UNIT             0x0128
     416             : #define TAG_PAGE_NUMBER                 0x0129
     417             : #define TAG_TRANSFER_FUNCTION           0x012D
     418             : #define TAG_SOFTWARE                    0x0131
     419             : #define TAG_DATETIME                    0x0132
     420             : #define TAG_ARTIST                      0x013B
     421             : #define TAG_HOST_COMPUTER               0x013C
     422             : #define TAG_PREDICTOR                   0x013D
     423             : #define TAG_WHITE_POINT                 0x013E
     424             : #define TAG_PRIMARY_CHROMATICITIES      0x013F
     425             : #define TAG_COLOR_MAP                   0x0140
     426             : #define TAG_HALFTONE_HINTS              0x0141
     427             : #define TAG_TILE_WIDTH                  0x0142
     428             : #define TAG_TILE_LENGTH                 0x0143
     429             : #define TAG_TILE_OFFSETS                0x0144
     430             : #define TAG_TILE_BYTE_COUNTS            0x0145
     431             : #define TAG_SUB_IFD                     0x014A
     432             : #define TAG_INK_SETMPUTER               0x014C
     433             : #define TAG_INK_NAMES                   0x014D
     434             : #define TAG_NUMBER_OF_INKS              0x014E
     435             : #define TAG_DOT_RANGE                   0x0150
     436             : #define TAG_TARGET_PRINTER              0x0151
     437             : #define TAG_EXTRA_SAMPLE                0x0152
     438             : #define TAG_SAMPLE_FORMAT               0x0153
     439             : #define TAG_S_MIN_SAMPLE_VALUE          0x0154
     440             : #define TAG_S_MAX_SAMPLE_VALUE          0x0155
     441             : #define TAG_TRANSFER_RANGE              0x0156
     442             : #define TAG_JPEG_TABLES                 0x015B
     443             : #define TAG_JPEG_PROC                   0x0200
     444             : #define TAG_JPEG_INTERCHANGE_FORMAT     0x0201
     445             : #define TAG_JPEG_INTERCHANGE_FORMAT_LEN 0x0202
     446             : #define TAG_JPEG_RESTART_INTERVAL       0x0203
     447             : #define TAG_JPEG_LOSSLESS_PREDICTOR     0x0205
     448             : #define TAG_JPEG_POINT_TRANSFORMS       0x0206
     449             : #define TAG_JPEG_Q_TABLES               0x0207
     450             : #define TAG_JPEG_DC_TABLES              0x0208
     451             : #define TAG_JPEG_AC_TABLES              0x0209
     452             : #define TAG_YCC_COEFFICIENTS            0x0211
     453             : #define TAG_YCC_SUB_SAMPLING            0x0212
     454             : #define TAG_YCC_POSITIONING             0x0213
     455             : #define TAG_REFERENCE_BLACK_WHITE       0x0214
     456             : /* 0x0301 - 0x0302 */
     457             : /* 0x0320 */
     458             : /* 0x0343 */
     459             : /* 0x5001 - 0x501B */
     460             : /* 0x5021 - 0x503B */
     461             : /* 0x5090 - 0x5091 */
     462             : /* 0x5100 - 0x5101 */
     463             : /* 0x5110 - 0x5113 */
     464             : /* 0x80E3 - 0x80E6 */
     465             : /* 0x828d - 0x828F */
     466             : #define TAG_COPYRIGHT                   0x8298
     467             : #define TAG_EXPOSURETIME                0x829A
     468             : #define TAG_FNUMBER                     0x829D
     469             : #define TAG_EXIF_IFD_POINTER            0x8769
     470             : #define TAG_ICC_PROFILE                 0x8773
     471             : #define TAG_EXPOSURE_PROGRAM            0x8822
     472             : #define TAG_SPECTRAL_SENSITY            0x8824
     473             : #define TAG_GPS_IFD_POINTER             0x8825
     474             : #define TAG_ISOSPEED                    0x8827
     475             : #define TAG_OPTOELECTRIC_CONVERSION_F   0x8828
     476             : /* 0x8829 - 0x882b */
     477             : #define TAG_EXIFVERSION                 0x9000
     478             : #define TAG_DATE_TIME_ORIGINAL          0x9003
     479             : #define TAG_DATE_TIME_DIGITIZED         0x9004
     480             : #define TAG_COMPONENT_CONFIG            0x9101
     481             : #define TAG_COMPRESSED_BITS_PER_PIXEL   0x9102
     482             : #define TAG_SHUTTERSPEED                0x9201
     483             : #define TAG_APERTURE                    0x9202
     484             : #define TAG_BRIGHTNESS_VALUE            0x9203
     485             : #define TAG_EXPOSURE_BIAS_VALUE         0x9204
     486             : #define TAG_MAX_APERTURE                0x9205
     487             : #define TAG_SUBJECT_DISTANCE            0x9206
     488             : #define TAG_METRIC_MODULE               0x9207
     489             : #define TAG_LIGHT_SOURCE                0x9208
     490             : #define TAG_FLASH                       0x9209
     491             : #define TAG_FOCAL_LENGTH                0x920A
     492             : /* 0x920B - 0x920D */
     493             : /* 0x9211 - 0x9216 */
     494             : #define TAG_SUBJECT_AREA                0x9214
     495             : #define TAG_MAKER_NOTE                  0x927C
     496             : #define TAG_USERCOMMENT                 0x9286
     497             : #define TAG_SUB_SEC_TIME                0x9290
     498             : #define TAG_SUB_SEC_TIME_ORIGINAL       0x9291
     499             : #define TAG_SUB_SEC_TIME_DIGITIZED      0x9292
     500             : /* 0x923F */
     501             : /* 0x935C */
     502             : #define TAG_XP_TITLE                    0x9C9B
     503             : #define TAG_XP_COMMENTS                 0x9C9C
     504             : #define TAG_XP_AUTHOR                   0x9C9D
     505             : #define TAG_XP_KEYWORDS                 0x9C9E
     506             : #define TAG_XP_SUBJECT                  0x9C9F
     507             : #define TAG_FLASH_PIX_VERSION           0xA000
     508             : #define TAG_COLOR_SPACE                 0xA001
     509             : #define TAG_COMP_IMAGE_WIDTH            0xA002 /* compressed images only */
     510             : #define TAG_COMP_IMAGE_HEIGHT           0xA003
     511             : #define TAG_RELATED_SOUND_FILE          0xA004
     512             : #define TAG_INTEROP_IFD_POINTER         0xA005 /* IFD pointer */
     513             : #define TAG_FLASH_ENERGY                0xA20B
     514             : #define TAG_SPATIAL_FREQUENCY_RESPONSE  0xA20C
     515             : #define TAG_FOCALPLANE_X_RES            0xA20E
     516             : #define TAG_FOCALPLANE_Y_RES            0xA20F
     517             : #define TAG_FOCALPLANE_RESOLUTION_UNIT  0xA210
     518             : #define TAG_SUBJECT_LOCATION            0xA214
     519             : #define TAG_EXPOSURE_INDEX              0xA215
     520             : #define TAG_SENSING_METHOD              0xA217
     521             : #define TAG_FILE_SOURCE                 0xA300
     522             : #define TAG_SCENE_TYPE                  0xA301
     523             : #define TAG_CFA_PATTERN                 0xA302
     524             : #define TAG_CUSTOM_RENDERED             0xA401
     525             : #define TAG_EXPOSURE_MODE               0xA402
     526             : #define TAG_WHITE_BALANCE               0xA403
     527             : #define TAG_DIGITAL_ZOOM_RATIO          0xA404
     528             : #define TAG_FOCAL_LENGTH_IN_35_MM_FILM  0xA405
     529             : #define TAG_SCENE_CAPTURE_TYPE          0xA406
     530             : #define TAG_GAIN_CONTROL                0xA407
     531             : #define TAG_CONTRAST                    0xA408
     532             : #define TAG_SATURATION                  0xA409
     533             : #define TAG_SHARPNESS                   0xA40A
     534             : #define TAG_DEVICE_SETTING_DESCRIPTION  0xA40B
     535             : #define TAG_SUBJECT_DISTANCE_RANGE      0xA40C
     536             : #define TAG_IMAGE_UNIQUE_ID             0xA420
     537             : 
     538             : /* Olympus specific tags */
     539             : #define TAG_OLYMPUS_SPECIALMODE         0x0200
     540             : #define TAG_OLYMPUS_JPEGQUAL            0x0201
     541             : #define TAG_OLYMPUS_MACRO               0x0202
     542             : #define TAG_OLYMPUS_DIGIZOOM            0x0204
     543             : #define TAG_OLYMPUS_SOFTWARERELEASE     0x0207
     544             : #define TAG_OLYMPUS_PICTINFO            0x0208
     545             : #define TAG_OLYMPUS_CAMERAID            0x0209
     546             : /* end Olympus specific tags */
     547             : 
     548             : /* Internal */
     549             : #define TAG_NONE                                -1 /* note that -1 <> 0xFFFF */
     550             : #define TAG_COMPUTED_VALUE                      -2
     551             : #define TAG_END_OF_LIST                 0xFFFD
     552             : 
     553             : /* Values for TAG_PHOTOMETRIC_INTERPRETATION */
     554             : #define PMI_BLACK_IS_ZERO       0
     555             : #define PMI_WHITE_IS_ZERO       1
     556             : #define PMI_RGB                     2
     557             : #define PMI_PALETTE_COLOR       3
     558             : #define PMI_TRANSPARENCY_MASK   4
     559             : #define PMI_SEPARATED           5
     560             : #define PMI_YCBCR               6
     561             : #define PMI_CIELAB              8
     562             : 
     563             : /* }}} */
     564             : 
     565             : /* {{{ TabTable[]
     566             :  */
     567             : typedef const struct {
     568             :         unsigned short Tag;
     569             :         char *Desc;
     570             : } tag_info_type;
     571             : 
     572             : typedef tag_info_type  tag_info_array[];
     573             : typedef tag_info_type  *tag_table_type;
     574             : 
     575             : #define TAG_TABLE_END \
     576             :   {TAG_NONE,           "No tag value"},\
     577             :   {TAG_COMPUTED_VALUE, "Computed value"},\
     578             :   {TAG_END_OF_LIST,    ""}  /* Important for exif_get_tagname() IF value != "" function result is != false */
     579             : 
     580             : static tag_info_array tag_table_IFD = {
     581             :   { 0x000B, "ACDComment"},
     582             :   { 0x00FE, "NewSubFile"}, /* better name it 'ImageType' ? */
     583             :   { 0x00FF, "SubFile"},
     584             :   { 0x0100, "ImageWidth"},
     585             :   { 0x0101, "ImageLength"},
     586             :   { 0x0102, "BitsPerSample"},
     587             :   { 0x0103, "Compression"},
     588             :   { 0x0106, "PhotometricInterpretation"},
     589             :   { 0x010A, "FillOrder"},
     590             :   { 0x010D, "DocumentName"},
     591             :   { 0x010E, "ImageDescription"},
     592             :   { 0x010F, "Make"},
     593             :   { 0x0110, "Model"},
     594             :   { 0x0111, "StripOffsets"},
     595             :   { 0x0112, "Orientation"},
     596             :   { 0x0115, "SamplesPerPixel"},
     597             :   { 0x0116, "RowsPerStrip"},
     598             :   { 0x0117, "StripByteCounts"},
     599             :   { 0x0118, "MinSampleValue"},
     600             :   { 0x0119, "MaxSampleValue"},
     601             :   { 0x011A, "XResolution"},
     602             :   { 0x011B, "YResolution"},
     603             :   { 0x011C, "PlanarConfiguration"},
     604             :   { 0x011D, "PageName"},
     605             :   { 0x011E, "XPosition"},
     606             :   { 0x011F, "YPosition"},
     607             :   { 0x0120, "FreeOffsets"},
     608             :   { 0x0121, "FreeByteCounts"},
     609             :   { 0x0122, "GrayResponseUnit"},
     610             :   { 0x0123, "GrayResponseCurve"},
     611             :   { 0x0124, "T4Options"},
     612             :   { 0x0125, "T6Options"},
     613             :   { 0x0128, "ResolutionUnit"},
     614             :   { 0x0129, "PageNumber"},
     615             :   { 0x012D, "TransferFunction"},
     616             :   { 0x0131, "Software"},
     617             :   { 0x0132, "DateTime"},
     618             :   { 0x013B, "Artist"},
     619             :   { 0x013C, "HostComputer"},
     620             :   { 0x013D, "Predictor"},
     621             :   { 0x013E, "WhitePoint"},
     622             :   { 0x013F, "PrimaryChromaticities"},
     623             :   { 0x0140, "ColorMap"},
     624             :   { 0x0141, "HalfToneHints"},
     625             :   { 0x0142, "TileWidth"},
     626             :   { 0x0143, "TileLength"},
     627             :   { 0x0144, "TileOffsets"},
     628             :   { 0x0145, "TileByteCounts"},
     629             :   { 0x014A, "SubIFD"},
     630             :   { 0x014C, "InkSet"},
     631             :   { 0x014D, "InkNames"},
     632             :   { 0x014E, "NumberOfInks"},
     633             :   { 0x0150, "DotRange"},
     634             :   { 0x0151, "TargetPrinter"},
     635             :   { 0x0152, "ExtraSample"},
     636             :   { 0x0153, "SampleFormat"},
     637             :   { 0x0154, "SMinSampleValue"},
     638             :   { 0x0155, "SMaxSampleValue"},
     639             :   { 0x0156, "TransferRange"},
     640             :   { 0x0157, "ClipPath"},
     641             :   { 0x0158, "XClipPathUnits"},
     642             :   { 0x0159, "YClipPathUnits"},
     643             :   { 0x015A, "Indexed"},
     644             :   { 0x015B, "JPEGTables"},
     645             :   { 0x015F, "OPIProxy"},
     646             :   { 0x0200, "JPEGProc"},
     647             :   { 0x0201, "JPEGInterchangeFormat"},
     648             :   { 0x0202, "JPEGInterchangeFormatLength"},
     649             :   { 0x0203, "JPEGRestartInterval"},
     650             :   { 0x0205, "JPEGLosslessPredictors"},
     651             :   { 0x0206, "JPEGPointTransforms"},
     652             :   { 0x0207, "JPEGQTables"},
     653             :   { 0x0208, "JPEGDCTables"},
     654             :   { 0x0209, "JPEGACTables"},
     655             :   { 0x0211, "YCbCrCoefficients"},
     656             :   { 0x0212, "YCbCrSubSampling"},
     657             :   { 0x0213, "YCbCrPositioning"},
     658             :   { 0x0214, "ReferenceBlackWhite"},
     659             :   { 0x02BC, "ExtensibleMetadataPlatform"}, /* XAP: Extensible Authoring Publishing, obsoleted by XMP: Extensible Metadata Platform */
     660             :   { 0x0301, "Gamma"}, 
     661             :   { 0x0302, "ICCProfileDescriptor"}, 
     662             :   { 0x0303, "SRGBRenderingIntent"}, 
     663             :   { 0x0320, "ImageTitle"}, 
     664             :   { 0x5001, "ResolutionXUnit"}, 
     665             :   { 0x5002, "ResolutionYUnit"}, 
     666             :   { 0x5003, "ResolutionXLengthUnit"}, 
     667             :   { 0x5004, "ResolutionYLengthUnit"}, 
     668             :   { 0x5005, "PrintFlags"}, 
     669             :   { 0x5006, "PrintFlagsVersion"}, 
     670             :   { 0x5007, "PrintFlagsCrop"}, 
     671             :   { 0x5008, "PrintFlagsBleedWidth"}, 
     672             :   { 0x5009, "PrintFlagsBleedWidthScale"}, 
     673             :   { 0x500A, "HalftoneLPI"}, 
     674             :   { 0x500B, "HalftoneLPIUnit"}, 
     675             :   { 0x500C, "HalftoneDegree"}, 
     676             :   { 0x500D, "HalftoneShape"}, 
     677             :   { 0x500E, "HalftoneMisc"}, 
     678             :   { 0x500F, "HalftoneScreen"}, 
     679             :   { 0x5010, "JPEGQuality"}, 
     680             :   { 0x5011, "GridSize"}, 
     681             :   { 0x5012, "ThumbnailFormat"}, 
     682             :   { 0x5013, "ThumbnailWidth"}, 
     683             :   { 0x5014, "ThumbnailHeight"}, 
     684             :   { 0x5015, "ThumbnailColorDepth"}, 
     685             :   { 0x5016, "ThumbnailPlanes"}, 
     686             :   { 0x5017, "ThumbnailRawBytes"}, 
     687             :   { 0x5018, "ThumbnailSize"}, 
     688             :   { 0x5019, "ThumbnailCompressedSize"}, 
     689             :   { 0x501A, "ColorTransferFunction"}, 
     690             :   { 0x501B, "ThumbnailData"}, 
     691             :   { 0x5020, "ThumbnailImageWidth"}, 
     692             :   { 0x5021, "ThumbnailImageHeight"}, 
     693             :   { 0x5022, "ThumbnailBitsPerSample"}, 
     694             :   { 0x5023, "ThumbnailCompression"}, 
     695             :   { 0x5024, "ThumbnailPhotometricInterp"}, 
     696             :   { 0x5025, "ThumbnailImageDescription"}, 
     697             :   { 0x5026, "ThumbnailEquipMake"}, 
     698             :   { 0x5027, "ThumbnailEquipModel"}, 
     699             :   { 0x5028, "ThumbnailStripOffsets"}, 
     700             :   { 0x5029, "ThumbnailOrientation"}, 
     701             :   { 0x502A, "ThumbnailSamplesPerPixel"}, 
     702             :   { 0x502B, "ThumbnailRowsPerStrip"}, 
     703             :   { 0x502C, "ThumbnailStripBytesCount"}, 
     704             :   { 0x502D, "ThumbnailResolutionX"}, 
     705             :   { 0x502E, "ThumbnailResolutionY"}, 
     706             :   { 0x502F, "ThumbnailPlanarConfig"}, 
     707             :   { 0x5030, "ThumbnailResolutionUnit"}, 
     708             :   { 0x5031, "ThumbnailTransferFunction"}, 
     709             :   { 0x5032, "ThumbnailSoftwareUsed"}, 
     710             :   { 0x5033, "ThumbnailDateTime"}, 
     711             :   { 0x5034, "ThumbnailArtist"}, 
     712             :   { 0x5035, "ThumbnailWhitePoint"}, 
     713             :   { 0x5036, "ThumbnailPrimaryChromaticities"}, 
     714             :   { 0x5037, "ThumbnailYCbCrCoefficients"}, 
     715             :   { 0x5038, "ThumbnailYCbCrSubsampling"}, 
     716             :   { 0x5039, "ThumbnailYCbCrPositioning"}, 
     717             :   { 0x503A, "ThumbnailRefBlackWhite"}, 
     718             :   { 0x503B, "ThumbnailCopyRight"}, 
     719             :   { 0x5090, "LuminanceTable"}, 
     720             :   { 0x5091, "ChrominanceTable"}, 
     721             :   { 0x5100, "FrameDelay"}, 
     722             :   { 0x5101, "LoopCount"}, 
     723             :   { 0x5110, "PixelUnit"}, 
     724             :   { 0x5111, "PixelPerUnitX"}, 
     725             :   { 0x5112, "PixelPerUnitY"}, 
     726             :   { 0x5113, "PaletteHistogram"}, 
     727             :   { 0x1000, "RelatedImageFileFormat"},
     728             :   { 0x800D, "ImageID"},
     729             :   { 0x80E3, "Matteing"},   /* obsoleted by ExtraSamples */
     730             :   { 0x80E4, "DataType"},   /* obsoleted by SampleFormat */
     731             :   { 0x80E5, "ImageDepth"},
     732             :   { 0x80E6, "TileDepth"},
     733             :   { 0x828D, "CFARepeatPatternDim"},
     734             :   { 0x828E, "CFAPattern"},
     735             :   { 0x828F, "BatteryLevel"},
     736             :   { 0x8298, "Copyright"},
     737             :   { 0x829A, "ExposureTime"},
     738             :   { 0x829D, "FNumber"},
     739             :   { 0x83BB, "IPTC/NAA"},
     740             :   { 0x84E3, "IT8RasterPadding"},
     741             :   { 0x84E5, "IT8ColorTable"},
     742             :   { 0x8649, "ImageResourceInformation"}, /* PhotoShop */
     743             :   { 0x8769, "Exif_IFD_Pointer"},
     744             :   { 0x8773, "ICC_Profile"},
     745             :   { 0x8822, "ExposureProgram"},
     746             :   { 0x8824, "SpectralSensity"},
     747             :   { 0x8828, "OECF"},
     748             :   { 0x8825, "GPS_IFD_Pointer"},
     749             :   { 0x8827, "ISOSpeedRatings"},
     750             :   { 0x8828, "OECF"},
     751             :   { 0x9000, "ExifVersion"},
     752             :   { 0x9003, "DateTimeOriginal"},
     753             :   { 0x9004, "DateTimeDigitized"},
     754             :   { 0x9101, "ComponentsConfiguration"},
     755             :   { 0x9102, "CompressedBitsPerPixel"},
     756             :   { 0x9201, "ShutterSpeedValue"},
     757             :   { 0x9202, "ApertureValue"},
     758             :   { 0x9203, "BrightnessValue"},
     759             :   { 0x9204, "ExposureBiasValue"},
     760             :   { 0x9205, "MaxApertureValue"},
     761             :   { 0x9206, "SubjectDistance"},
     762             :   { 0x9207, "MeteringMode"},
     763             :   { 0x9208, "LightSource"},
     764             :   { 0x9209, "Flash"},
     765             :   { 0x920A, "FocalLength"},
     766             :   { 0x920B, "FlashEnergy"},                 /* 0xA20B  in JPEG   */
     767             :   { 0x920C, "SpatialFrequencyResponse"},    /* 0xA20C    -  -    */
     768             :   { 0x920D, "Noise"},
     769             :   { 0x920E, "FocalPlaneXResolution"},       /* 0xA20E    -  -    */
     770             :   { 0x920F, "FocalPlaneYResolution"},       /* 0xA20F    -  -    */
     771             :   { 0x9210, "FocalPlaneResolutionUnit"},    /* 0xA210    -  -    */
     772             :   { 0x9211, "ImageNumber"},
     773             :   { 0x9212, "SecurityClassification"},
     774             :   { 0x9213, "ImageHistory"},
     775             :   { 0x9214, "SubjectLocation"},             /* 0xA214    -  -    */
     776             :   { 0x9215, "ExposureIndex"},               /* 0xA215    -  -    */
     777             :   { 0x9216, "TIFF/EPStandardID"},
     778             :   { 0x9217, "SensingMethod"},               /* 0xA217    -  -    */
     779             :   { 0x923F, "StoNits"},
     780             :   { 0x927C, "MakerNote"},
     781             :   { 0x9286, "UserComment"},
     782             :   { 0x9290, "SubSecTime"},
     783             :   { 0x9291, "SubSecTimeOriginal"},
     784             :   { 0x9292, "SubSecTimeDigitized"},
     785             :   { 0x935C, "ImageSourceData"},             /* "Adobe Photoshop Document Data Block": 8BIM... */
     786             :   { 0x9c9b, "Title" },                      /* Win XP specific, Unicode  */
     787             :   { 0x9c9c, "Comments" },                   /* Win XP specific, Unicode  */
     788             :   { 0x9c9d, "Author" },                     /* Win XP specific, Unicode  */
     789             :   { 0x9c9e, "Keywords" },                   /* Win XP specific, Unicode  */
     790             :   { 0x9c9f, "Subject" },                    /* Win XP specific, Unicode, not to be confused with SubjectDistance and SubjectLocation */
     791             :   { 0xA000, "FlashPixVersion"},
     792             :   { 0xA001, "ColorSpace"},
     793             :   { 0xA002, "ExifImageWidth"},
     794             :   { 0xA003, "ExifImageLength"},
     795             :   { 0xA004, "RelatedSoundFile"},
     796             :   { 0xA005, "InteroperabilityOffset"},
     797             :   { 0xA20B, "FlashEnergy"},                 /* 0x920B in TIFF/EP */
     798             :   { 0xA20C, "SpatialFrequencyResponse"},    /* 0x920C    -  -    */
     799             :   { 0xA20D, "Noise"},
     800             :   { 0xA20E, "FocalPlaneXResolution"},         /* 0x920E    -  -    */
     801             :   { 0xA20F, "FocalPlaneYResolution"},       /* 0x920F    -  -    */
     802             :   { 0xA210, "FocalPlaneResolutionUnit"},    /* 0x9210    -  -    */
     803             :   { 0xA211, "ImageNumber"},
     804             :   { 0xA212, "SecurityClassification"},
     805             :   { 0xA213, "ImageHistory"},
     806             :   { 0xA214, "SubjectLocation"},             /* 0x9214    -  -    */
     807             :   { 0xA215, "ExposureIndex"},               /* 0x9215    -  -    */
     808             :   { 0xA216, "TIFF/EPStandardID"},
     809             :   { 0xA217, "SensingMethod"},               /* 0x9217    -  -    */
     810             :   { 0xA300, "FileSource"},
     811             :   { 0xA301, "SceneType"},
     812             :   { 0xA302, "CFAPattern"},
     813             :   { 0xA401, "CustomRendered"},
     814             :   { 0xA402, "ExposureMode"},
     815             :   { 0xA403, "WhiteBalance"},
     816             :   { 0xA404, "DigitalZoomRatio"},
     817             :   { 0xA405, "FocalLengthIn35mmFilm"},
     818             :   { 0xA406, "SceneCaptureType"},
     819             :   { 0xA407, "GainControl"},
     820             :   { 0xA408, "Contrast"},
     821             :   { 0xA409, "Saturation"},
     822             :   { 0xA40A, "Sharpness"},
     823             :   { 0xA40B, "DeviceSettingDescription"},
     824             :   { 0xA40C, "SubjectDistanceRange"},
     825             :   { 0xA420, "ImageUniqueID"},
     826             :   TAG_TABLE_END
     827             : } ;
     828             : 
     829             : static tag_info_array tag_table_GPS = {
     830             :   { 0x0000, "GPSVersion"},
     831             :   { 0x0001, "GPSLatitudeRef"},
     832             :   { 0x0002, "GPSLatitude"},
     833             :   { 0x0003, "GPSLongitudeRef"},
     834             :   { 0x0004, "GPSLongitude"},
     835             :   { 0x0005, "GPSAltitudeRef"},
     836             :   { 0x0006, "GPSAltitude"},
     837             :   { 0x0007, "GPSTimeStamp"},
     838             :   { 0x0008, "GPSSatellites"},
     839             :   { 0x0009, "GPSStatus"},
     840             :   { 0x000A, "GPSMeasureMode"},
     841             :   { 0x000B, "GPSDOP"},
     842             :   { 0x000C, "GPSSpeedRef"},
     843             :   { 0x000D, "GPSSpeed"},
     844             :   { 0x000E, "GPSTrackRef"},
     845             :   { 0x000F, "GPSTrack"},
     846             :   { 0x0010, "GPSImgDirectionRef"},
     847             :   { 0x0011, "GPSImgDirection"},
     848             :   { 0x0012, "GPSMapDatum"},
     849             :   { 0x0013, "GPSDestLatitudeRef"},
     850             :   { 0x0014, "GPSDestLatitude"},
     851             :   { 0x0015, "GPSDestLongitudeRef"},
     852             :   { 0x0016, "GPSDestLongitude"},
     853             :   { 0x0017, "GPSDestBearingRef"},
     854             :   { 0x0018, "GPSDestBearing"},
     855             :   { 0x0019, "GPSDestDistanceRef"},
     856             :   { 0x001A, "GPSDestDistance"},
     857             :   { 0x001B, "GPSProcessingMode"},
     858             :   { 0x001C, "GPSAreaInformation"},
     859             :   { 0x001D, "GPSDateStamp"},
     860             :   { 0x001E, "GPSDifferential"},
     861             :   TAG_TABLE_END
     862             : };
     863             : 
     864             : static tag_info_array tag_table_IOP = {
     865             :   { 0x0001, "InterOperabilityIndex"}, /* should be 'R98' or 'THM' */
     866             :   { 0x0002, "InterOperabilityVersion"},
     867             :   { 0x1000, "RelatedFileFormat"},
     868             :   { 0x1001, "RelatedImageWidth"},
     869             :   { 0x1002, "RelatedImageHeight"},
     870             :   TAG_TABLE_END
     871             : };
     872             : 
     873             : static tag_info_array tag_table_VND_CANON = {
     874             :   { 0x0001, "ModeArray"}, /* guess */
     875             :   { 0x0004, "ImageInfo"}, /* guess */
     876             :   { 0x0006, "ImageType"},
     877             :   { 0x0007, "FirmwareVersion"},
     878             :   { 0x0008, "ImageNumber"},
     879             :   { 0x0009, "OwnerName"},
     880             :   { 0x000C, "Camera"},
     881             :   { 0x000F, "CustomFunctions"},
     882             :   TAG_TABLE_END
     883             : };
     884             : 
     885             : static tag_info_array tag_table_VND_CASIO = {
     886             :   { 0x0001, "RecordingMode"},
     887             :   { 0x0002, "Quality"},
     888             :   { 0x0003, "FocusingMode"},
     889             :   { 0x0004, "FlashMode"},
     890             :   { 0x0005, "FlashIntensity"},
     891             :   { 0x0006, "ObjectDistance"},
     892             :   { 0x0007, "WhiteBalance"},
     893             :   { 0x000A, "DigitalZoom"},
     894             :   { 0x000B, "Sharpness"},
     895             :   { 0x000C, "Contrast"},
     896             :   { 0x000D, "Saturation"},
     897             :   { 0x0014, "CCDSensitivity"},
     898             :   TAG_TABLE_END
     899             : };
     900             : 
     901             : static tag_info_array tag_table_VND_FUJI = {
     902             :   { 0x0000, "Version"},
     903             :   { 0x1000, "Quality"},
     904             :   { 0x1001, "Sharpness"},
     905             :   { 0x1002, "WhiteBalance"},
     906             :   { 0x1003, "Color"},
     907             :   { 0x1004, "Tone"},
     908             :   { 0x1010, "FlashMode"},
     909             :   { 0x1011, "FlashStrength"},
     910             :   { 0x1020, "Macro"},
     911             :   { 0x1021, "FocusMode"},
     912             :   { 0x1030, "SlowSync"},
     913             :   { 0x1031, "PictureMode"},
     914             :   { 0x1100, "ContTake"},
     915             :   { 0x1300, "BlurWarning"},
     916             :   { 0x1301, "FocusWarning"},
     917             :   { 0x1302, "AEWarning "},
     918             :   TAG_TABLE_END
     919             : };
     920             : 
     921             : static tag_info_array tag_table_VND_NIKON = {
     922             :   { 0x0003, "Quality"},
     923             :   { 0x0004, "ColorMode"},
     924             :   { 0x0005, "ImageAdjustment"},
     925             :   { 0x0006, "CCDSensitivity"},
     926             :   { 0x0007, "WhiteBalance"},
     927             :   { 0x0008, "Focus"},
     928             :   { 0x000a, "DigitalZoom"},
     929             :   { 0x000b, "Converter"},
     930             :   TAG_TABLE_END
     931             : };
     932             :   
     933             : static tag_info_array tag_table_VND_NIKON_990 = {
     934             :   { 0x0001, "Version"},
     935             :   { 0x0002, "ISOSetting"},
     936             :   { 0x0003, "ColorMode"},
     937             :   { 0x0004, "Quality"},
     938             :   { 0x0005, "WhiteBalance"},
     939             :   { 0x0006, "ImageSharpening"},
     940             :   { 0x0007, "FocusMode"},
     941             :   { 0x0008, "FlashSetting"},
     942             :   { 0x000F, "ISOSelection"},
     943             :   { 0x0080, "ImageAdjustment"},
     944             :   { 0x0082, "AuxiliaryLens"},
     945             :   { 0x0085, "ManualFocusDistance"},
     946             :   { 0x0086, "DigitalZoom"},
     947             :   { 0x0088, "AFFocusPosition"},
     948             :   { 0x0010, "DataDump"},
     949             :   TAG_TABLE_END
     950             : };
     951             :   
     952             : static tag_info_array tag_table_VND_OLYMPUS = {
     953             :   { 0x0200, "SpecialMode"},
     954             :   { 0x0201, "JPEGQuality"},
     955             :   { 0x0202, "Macro"},
     956             :   { 0x0204, "DigitalZoom"},
     957             :   { 0x0207, "SoftwareRelease"},
     958             :   { 0x0208, "PictureInfo"},
     959             :   { 0x0209, "CameraId"},
     960             :   { 0x0F00, "DataDump"},
     961             :   TAG_TABLE_END
     962             : };
     963             : 
     964             : typedef enum mn_byte_order_t {
     965             :         MN_ORDER_INTEL    = 0,
     966             :         MN_ORDER_MOTOROLA = 1,
     967             :         MN_ORDER_NORMAL
     968             : } mn_byte_order_t;
     969             : 
     970             : typedef enum mn_offset_mode_t {
     971             :         MN_OFFSET_NORMAL,
     972             :         MN_OFFSET_MAKER,
     973             :         MN_OFFSET_GUESS
     974             : } mn_offset_mode_t;
     975             : 
     976             : typedef struct {
     977             :         tag_table_type   tag_table;
     978             :         char *           make;
     979             :         char *           model;
     980             :         char *           id_string;
     981             :         int              id_string_len;
     982             :         int              offset;
     983             :         mn_byte_order_t  byte_order;
     984             :         mn_offset_mode_t offset_mode;
     985             : } maker_note_type;
     986             : 
     987             : static const maker_note_type maker_note_array[] = {
     988             :   { tag_table_VND_CANON,     "Canon",                   NULL,  NULL,                       0,  0,  MN_ORDER_INTEL,    MN_OFFSET_GUESS},
     989             : /*  { tag_table_VND_CANON,     "Canon",                   NULL,  NULL,                       0,  0,  MN_ORDER_NORMAL,   MN_OFFSET_NORMAL},*/
     990             :   { tag_table_VND_CASIO,     "CASIO",                   NULL,  NULL,                       0,  0,  MN_ORDER_MOTOROLA, MN_OFFSET_NORMAL},
     991             :   { tag_table_VND_FUJI,      "FUJIFILM",                NULL,  "FUJIFILM\x0C\x00\x00\x00", 12, 12, MN_ORDER_INTEL,    MN_OFFSET_MAKER},
     992             :   { tag_table_VND_NIKON,     "NIKON",                   NULL,  "Nikon\x00\x01\x00",        8,  8,  MN_ORDER_NORMAL,   MN_OFFSET_NORMAL},
     993             :   { tag_table_VND_NIKON_990, "NIKON",                   NULL,  NULL,                       0,  0,  MN_ORDER_NORMAL,   MN_OFFSET_NORMAL},
     994             :   { tag_table_VND_OLYMPUS,   "OLYMPUS OPTICAL CO.,LTD", NULL,  "OLYMP\x00\x01\x00",        8,  8,  MN_ORDER_NORMAL,   MN_OFFSET_NORMAL},
     995             : };
     996             : /* }}} */
     997             : 
     998             : /* {{{ exif_get_tagname
     999             :         Get headername for tag_num or NULL if not defined */
    1000         678 : static char * exif_get_tagname(int tag_num, char *ret, int len, tag_table_type tag_table TSRMLS_DC)
    1001             : {
    1002             :         int i, t;
    1003             :         char tmp[32];
    1004             : 
    1005       56848 :         for (i = 0; (t = tag_table[i].Tag) != TAG_END_OF_LIST; i++) {
    1006       56801 :                 if (t == tag_num) {
    1007         631 :                         if (ret && len)  {
    1008         622 :                                 strlcpy(ret, tag_table[i].Desc, abs(len));
    1009         622 :                                 if (len < 0) {
    1010           3 :                                         memset(ret + strlen(ret), ' ', -len - strlen(ret) - 1);
    1011           3 :                                         ret[-len - 1] = '\0';
    1012             :                                 }
    1013         622 :                                 return ret;
    1014             :                         }
    1015           9 :                         return tag_table[i].Desc;
    1016             :                 }
    1017             :         }
    1018             : 
    1019          47 :         if (ret && len) {
    1020          30 :                 snprintf(tmp, sizeof(tmp), "UndefinedTag:0x%04X", tag_num);
    1021          30 :                 strlcpy(ret, tmp, abs(len));
    1022          30 :                 if (len < 0) {
    1023           2 :                         memset(ret + strlen(ret), ' ', -len - strlen(ret) - 1);
    1024           2 :                         ret[-len - 1] = '\0';
    1025             :                 }
    1026          30 :                 return ret;
    1027             :         }
    1028          17 :         return "";
    1029             : }
    1030             : /* }}} */
    1031             : 
    1032             : /* {{{ exif_char_dump
    1033             :  * Do not use! This is a debug function... */
    1034             : #ifdef EXIF_DEBUG
    1035             : static unsigned char* exif_char_dump(unsigned char * addr, int len, int offset)
    1036             : {
    1037             :         static unsigned char buf[4096+1];
    1038             :         static unsigned char tmp[20];
    1039             :         int c, i, p=0, n = 5+31;
    1040             : 
    1041             :         p += slprintf(buf+p, sizeof(buf)-p, "\nDump Len: %08X (%d)", len, len);
    1042             :         if (len) {
    1043             :                 for(i=0; i<len+15 && p+n<=sizeof(buf); i++) {
    1044             :                         if (i%16==0) {
    1045             :                                 p += slprintf(buf+p, sizeof(buf)-p, "\n%08X: ", i+offset);
    1046             :                         }
    1047             :                         if (i<len) {
    1048             :                                 c = *addr++;
    1049             :                                 p += slprintf(buf+p, sizeof(buf)-p, "%02X ", c);
    1050             :                                 tmp[i%16] = c>=32 ? c : '.';
    1051             :                                 tmp[(i%16)+1] = '\0';
    1052             :                         } else {
    1053             :                                 p += slprintf(buf+p, sizeof(buf)-p, "   ");
    1054             :                         }
    1055             :                         if (i%16==15) {
    1056             :                                 p += slprintf(buf+p, sizeof(buf)-p, "    %s", tmp);
    1057             :                                 if (i>=len) {
    1058             :                                         break;
    1059             :                                 }
    1060             :                         }
    1061             :                 }
    1062             :         }
    1063             :         buf[sizeof(buf)-1] = '\0';
    1064             :         return buf;
    1065             : }
    1066             : #endif
    1067             : /* }}} */
    1068             : 
    1069             : /* {{{ php_jpg_get16
    1070             :    Get 16 bits motorola order (always) for jpeg header stuff.
    1071             : */
    1072          48 : static int php_jpg_get16(void *value)
    1073             : {
    1074          48 :         return (((uchar *)value)[0] << 8) | ((uchar *)value)[1];
    1075             : }
    1076             : /* }}} */
    1077             : 
    1078             : /* {{{ php_ifd_get16u
    1079             :  * Convert a 16 bit unsigned value from file's native byte order */
    1080       10554 : static int php_ifd_get16u(void *value, int motorola_intel)
    1081             : {
    1082       10554 :         if (motorola_intel) {
    1083        4222 :                 return (((uchar *)value)[0] << 8) | ((uchar *)value)[1];
    1084             :         } else {
    1085        6332 :                 return (((uchar *)value)[1] << 8) | ((uchar *)value)[0];
    1086             :         }
    1087             : }
    1088             : /* }}} */
    1089             : 
    1090             : /* {{{ php_ifd_get16s
    1091             :  * Convert a 16 bit signed value from file's native byte order */
    1092           0 : static signed short php_ifd_get16s(void *value, int motorola_intel)
    1093             : {
    1094           0 :         return (signed short)php_ifd_get16u(value, motorola_intel);
    1095             : }
    1096             : /* }}} */
    1097             : 
    1098             : /* {{{ php_ifd_get32s
    1099             :  * Convert a 32 bit signed value from file's native byte order */
    1100        1966 : static int php_ifd_get32s(void *value, int motorola_intel)
    1101             : {
    1102        1966 :         if (motorola_intel) {
    1103        1638 :                 return  (((char  *)value)[0] << 24)
    1104         819 :                           | (((uchar *)value)[1] << 16)
    1105         819 :                           | (((uchar *)value)[2] << 8 )
    1106         819 :                           | (((uchar *)value)[3]      );
    1107             :         } else {
    1108        2294 :                 return  (((char  *)value)[3] << 24)
    1109        1147 :                           | (((uchar *)value)[2] << 16)
    1110        1147 :                           | (((uchar *)value)[1] << 8 )
    1111        1147 :                           | (((uchar *)value)[0]      );
    1112             :         }
    1113             : }
    1114             : /* }}} */
    1115             : 
    1116             : /* {{{ php_ifd_get32u
    1117             :  * Write 32 bit unsigned value to data */
    1118        1965 : static unsigned php_ifd_get32u(void *value, int motorola_intel)
    1119             : {
    1120        1965 :         return (unsigned)php_ifd_get32s(value, motorola_intel) & 0xffffffff;
    1121             : }
    1122             : /* }}} */
    1123             : 
    1124             : /* {{{ php_ifd_set16u
    1125             :  * Write 16 bit unsigned value to data */
    1126           7 : static void php_ifd_set16u(char *data, unsigned int value, int motorola_intel)
    1127             : {
    1128           7 :         if (motorola_intel) {
    1129           0 :                 data[0] = (value & 0xFF00) >> 8;
    1130           0 :                 data[1] = (value & 0x00FF);
    1131             :         } else {
    1132           7 :                 data[1] = (value & 0xFF00) >> 8;
    1133           7 :                 data[0] = (value & 0x00FF);
    1134             :         }
    1135           7 : }
    1136             : /* }}} */
    1137             : 
    1138             : /* {{{ php_ifd_set32u
    1139             :  * Convert a 32 bit unsigned value from file's native byte order */
    1140           6 : static void php_ifd_set32u(char *data, size_t value, int motorola_intel)
    1141             : {
    1142           6 :         if (motorola_intel) {
    1143           0 :                 data[0] = (value & 0xFF000000) >> 24;
    1144           0 :                 data[1] = (value & 0x00FF0000) >> 16;
    1145           0 :                 data[2] = (value & 0x0000FF00) >>  8;
    1146           0 :                 data[3] = (value & 0x000000FF);
    1147             :         } else {
    1148           6 :                 data[3] = (value & 0xFF000000) >> 24;
    1149           6 :                 data[2] = (value & 0x00FF0000) >> 16;
    1150           6 :                 data[1] = (value & 0x0000FF00) >>  8;
    1151           6 :                 data[0] = (value & 0x000000FF);
    1152             :         }
    1153           6 : }
    1154             : /* }}} */
    1155             : 
    1156             : #ifdef EXIF_DEBUG
    1157             : char * exif_dump_data(int *dump_free, int format, int components, int length, int motorola_intel, char *value_ptr TSRMLS_DC) /* {{{ */
    1158             : {
    1159             :         char *dump;
    1160             :         int len;
    1161             : 
    1162             :         *dump_free = 0;
    1163             :         if (format == TAG_FMT_STRING) {
    1164             :                 return value_ptr ? value_ptr : "<no data>";
    1165             :         }
    1166             :         if (format == TAG_FMT_UNDEFINED) {
    1167             :                 return "<undefined>\n";
    1168             :         }
    1169             :         if (format == TAG_FMT_IFD) {
    1170             :                 return "";
    1171             :         }
    1172             :         if (format == TAG_FMT_SINGLE || format == TAG_FMT_DOUBLE) {
    1173             :                 return "<not implemented>";
    1174             :         }
    1175             :         *dump_free = 1;
    1176             :         if (components > 1) {
    1177             :                 len = spprintf(&dump, 0, "(%d,%d) {", components, length);
    1178             :         } else {
    1179             :                 len = spprintf(&dump, 0, "{");
    1180             :         }
    1181             :         while(components > 0) {
    1182             :                 switch(format) {
    1183             :                         case TAG_FMT_BYTE:
    1184             :                         case TAG_FMT_UNDEFINED:
    1185             :                         case TAG_FMT_STRING:
    1186             :                         case TAG_FMT_SBYTE:
    1187             :                                 dump = erealloc(dump, len + 4 + 1);
    1188             :                                 snprintf(dump + len, 4 + 1, "0x%02X", *value_ptr);
    1189             :                                 len += 4;
    1190             :                                 value_ptr++;
    1191             :                                 break;
    1192             :                         case TAG_FMT_USHORT:
    1193             :                         case TAG_FMT_SSHORT:
    1194             :                                 dump = erealloc(dump, len + 6 + 1);
    1195             :                                 snprintf(dump + len, 6 + 1, "0x%04X", php_ifd_get16s(value_ptr, motorola_intel));
    1196             :                                 len += 6;
    1197             :                                 value_ptr += 2;
    1198             :                                 break;
    1199             :                         case TAG_FMT_ULONG:
    1200             :                         case TAG_FMT_SLONG:
    1201             :                                 dump = erealloc(dump, len + 6 + 1);
    1202             :                                 snprintf(dump + len, 6 + 1, "0x%04X", php_ifd_get32s(value_ptr, motorola_intel));
    1203             :                                 len += 6;
    1204             :                                 value_ptr += 4;
    1205             :                                 break;
    1206             :                         case TAG_FMT_URATIONAL:
    1207             :                         case TAG_FMT_SRATIONAL:
    1208             :                                 dump = erealloc(dump, len + 13 + 1);
    1209             :                                 snprintf(dump + len, 13 + 1, "0x%04X/0x%04X", php_ifd_get32s(value_ptr, motorola_intel), php_ifd_get32s(value_ptr+4, motorola_intel));
    1210             :                                 len += 13;
    1211             :                                 value_ptr += 8;
    1212             :                                 break;
    1213             :                 }
    1214             :                 if (components > 0) {
    1215             :                         dump = erealloc(dump, len + 2 + 1);
    1216             :                         snprintf(dump + len, 2 + 1, ", ");
    1217             :                         len += 2;                       
    1218             :                         components--;
    1219             :                 } else{
    1220             :                         break;
    1221             :                 }
    1222             :         }
    1223             :         dump = erealloc(dump, len + 1 + 1);
    1224             :         snprintf(dump + len, 1 + 1, "}");
    1225             :         return dump;
    1226             : }
    1227             : /* }}} */
    1228             : #endif
    1229             : 
    1230             : /* {{{ exif_convert_any_format
    1231             :  * Evaluate number, be it int, rational, or float from directory. */
    1232          14 : static double exif_convert_any_format(void *value, int format, int motorola_intel TSRMLS_DC)
    1233             : {
    1234             :         int             s_den;
    1235             :         unsigned        u_den;
    1236             : 
    1237          14 :         switch(format) {
    1238           0 :                 case TAG_FMT_SBYTE:     return *(signed char *)value;
    1239           0 :                 case TAG_FMT_BYTE:      return *(uchar *)value;
    1240             : 
    1241           1 :                 case TAG_FMT_USHORT:    return php_ifd_get16u(value, motorola_intel);
    1242           0 :                 case TAG_FMT_ULONG:     return php_ifd_get32u(value, motorola_intel);
    1243             : 
    1244             :                 case TAG_FMT_URATIONAL:
    1245          13 :                         u_den = php_ifd_get32u(4+(char *)value, motorola_intel);
    1246          13 :                         if (u_den == 0) {
    1247           0 :                                 return 0;
    1248             :                         } else {
    1249          13 :                                 return (double)php_ifd_get32u(value, motorola_intel) / u_den;
    1250             :                         }
    1251             : 
    1252             :                 case TAG_FMT_SRATIONAL:
    1253           0 :                         s_den = php_ifd_get32s(4+(char *)value, motorola_intel);
    1254           0 :                         if (s_den == 0) {
    1255           0 :                                 return 0;
    1256             :                         } else {
    1257           0 :                                 return (double)php_ifd_get32s(value, motorola_intel) / s_den;
    1258             :                         }
    1259             : 
    1260           0 :                 case TAG_FMT_SSHORT:    return (signed short)php_ifd_get16u(value, motorola_intel);
    1261           0 :                 case TAG_FMT_SLONG:     return php_ifd_get32s(value, motorola_intel);
    1262             : 
    1263             :                 /* Not sure if this is correct (never seen float used in Exif format) */
    1264             :                 case TAG_FMT_SINGLE:
    1265             : #ifdef EXIF_DEBUG
    1266             :                         php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Found value of type single");
    1267             : #endif
    1268           0 :                         return (double)*(float *)value;
    1269             :                 case TAG_FMT_DOUBLE:
    1270             : #ifdef EXIF_DEBUG
    1271             :                         php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Found value of type double");
    1272             : #endif
    1273           0 :                         return *(double *)value;
    1274             :         }
    1275           0 :         return 0;
    1276             : }
    1277             : /* }}} */
    1278             : 
    1279             : /* {{{ exif_convert_any_to_int
    1280             :  * Evaluate number, be it int, rational, or float from directory. */
    1281          23 : static size_t exif_convert_any_to_int(void *value, int format, int motorola_intel TSRMLS_DC)
    1282             : {
    1283             :         int             s_den;
    1284             :         unsigned        u_den;
    1285             : 
    1286          23 :         switch(format) {
    1287           0 :                 case TAG_FMT_SBYTE:     return *(signed char *)value;
    1288           0 :                 case TAG_FMT_BYTE:      return *(uchar *)value;
    1289             : 
    1290           8 :                 case TAG_FMT_USHORT:    return php_ifd_get16u(value, motorola_intel);
    1291          15 :                 case TAG_FMT_ULONG:     return php_ifd_get32u(value, motorola_intel);
    1292             : 
    1293             :                 case TAG_FMT_URATIONAL:
    1294           0 :                         u_den = php_ifd_get32u(4+(char *)value, motorola_intel);
    1295           0 :                         if (u_den == 0) {
    1296           0 :                                 return 0;
    1297             :                         } else {
    1298           0 :                                 return php_ifd_get32u(value, motorola_intel) / u_den;
    1299             :                         }
    1300             : 
    1301             :                 case TAG_FMT_SRATIONAL:
    1302           0 :                         s_den = php_ifd_get32s(4+(char *)value, motorola_intel);
    1303           0 :                         if (s_den == 0) {
    1304           0 :                                 return 0;
    1305             :                         } else {
    1306           0 :                                 return php_ifd_get32s(value, motorola_intel) / s_den;
    1307             :                         }
    1308             : 
    1309           0 :                 case TAG_FMT_SSHORT:    return php_ifd_get16u(value, motorola_intel);
    1310           0 :                 case TAG_FMT_SLONG:     return php_ifd_get32s(value, motorola_intel);
    1311             : 
    1312             :                 /* Not sure if this is correct (never seen float used in Exif format) */
    1313             :                 case TAG_FMT_SINGLE:
    1314             : #ifdef EXIF_DEBUG
    1315             :                         php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Found value of type single");
    1316             : #endif
    1317           0 :                         return (size_t)*(float *)value;
    1318             :                 case TAG_FMT_DOUBLE:
    1319             : #ifdef EXIF_DEBUG
    1320             :                         php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Found value of type double");
    1321             : #endif
    1322           0 :                         return (size_t)*(double *)value;
    1323             :         }
    1324           0 :         return 0;
    1325             : }
    1326             : /* }}} */
    1327             : 
    1328             : /* {{{ struct image_info_value, image_info_list
    1329             : */
    1330             : #ifndef WORD
    1331             : #define WORD unsigned short
    1332             : #endif
    1333             : #ifndef DWORD
    1334             : #define DWORD unsigned int
    1335             : #endif
    1336             : 
    1337             : typedef struct {
    1338             :         int             num;
    1339             :         int             den;
    1340             : } signed_rational;
    1341             : 
    1342             : typedef struct {
    1343             :         unsigned int    num;
    1344             :         unsigned int    den;
    1345             : } unsigned_rational;
    1346             : 
    1347             : typedef union _image_info_value {
    1348             :         char                            *s;
    1349             :         unsigned            u;
    1350             :         int                             i;
    1351             :         float               f;
    1352             :         double              d;
    1353             :         signed_rational         sr;
    1354             :         unsigned_rational       ur;
    1355             :         union _image_info_value   *list;
    1356             : } image_info_value;
    1357             : 
    1358             : typedef struct {
    1359             :         WORD                tag;
    1360             :         WORD                format;
    1361             :         DWORD               length;
    1362             :         DWORD               dummy;  /* value ptr of tiff directory entry */
    1363             :         char                            *name;
    1364             :         image_info_value    value;
    1365             : } image_info_data;
    1366             : 
    1367             : typedef struct {
    1368             :         int                 count;
    1369             :         image_info_data         *list;
    1370             : } image_info_list;
    1371             : /* }}} */
    1372             : 
    1373             : /* {{{ exif_get_sectionname
    1374             :  Returns the name of a section
    1375             : */
    1376             : #define SECTION_FILE        0
    1377             : #define SECTION_COMPUTED    1
    1378             : #define SECTION_ANY_TAG     2
    1379             : #define SECTION_IFD0        3
    1380             : #define SECTION_THUMBNAIL   4
    1381             : #define SECTION_COMMENT     5
    1382             : #define SECTION_APP0        6
    1383             : #define SECTION_EXIF        7
    1384             : #define SECTION_FPIX        8
    1385             : #define SECTION_GPS         9
    1386             : #define SECTION_INTEROP     10
    1387             : #define SECTION_APP12       11
    1388             : #define SECTION_WINXP       12
    1389             : #define SECTION_MAKERNOTE   13
    1390             : #define SECTION_COUNT       14
    1391             : 
    1392             : #define FOUND_FILE          (1<<SECTION_FILE)
    1393             : #define FOUND_COMPUTED      (1<<SECTION_COMPUTED)
    1394             : #define FOUND_ANY_TAG       (1<<SECTION_ANY_TAG)
    1395             : #define FOUND_IFD0          (1<<SECTION_IFD0)
    1396             : #define FOUND_THUMBNAIL     (1<<SECTION_THUMBNAIL)
    1397             : #define FOUND_COMMENT       (1<<SECTION_COMMENT)
    1398             : #define FOUND_APP0          (1<<SECTION_APP0)
    1399             : #define FOUND_EXIF          (1<<SECTION_EXIF)
    1400             : #define FOUND_FPIX          (1<<SECTION_FPIX)
    1401             : #define FOUND_GPS           (1<<SECTION_GPS)
    1402             : #define FOUND_INTEROP       (1<<SECTION_INTEROP)
    1403             : #define FOUND_APP12         (1<<SECTION_APP12)
    1404             : #define FOUND_WINXP         (1<<SECTION_WINXP)
    1405             : #define FOUND_MAKERNOTE     (1<<SECTION_MAKERNOTE)
    1406             : 
    1407         762 : static char *exif_get_sectionname(int section)
    1408             : {
    1409         762 :         switch(section) {
    1410          48 :                 case SECTION_FILE:      return "FILE";
    1411         102 :                 case SECTION_COMPUTED:  return "COMPUTED";
    1412          70 :                 case SECTION_ANY_TAG:   return "ANY_TAG";
    1413          79 :                 case SECTION_IFD0:      return "IFD0";
    1414          55 :                 case SECTION_THUMBNAIL: return "THUMBNAIL";
    1415          52 :                 case SECTION_COMMENT:   return "COMMENT";
    1416          40 :                 case SECTION_APP0:      return "APP0";
    1417          56 :                 case SECTION_EXIF:      return "EXIF";
    1418          40 :                 case SECTION_FPIX:      return "FPIX";
    1419          48 :                 case SECTION_GPS:       return "GPS";
    1420          45 :                 case SECTION_INTEROP:   return "INTEROP";
    1421          40 :                 case SECTION_APP12:     return "APP12";
    1422          44 :                 case SECTION_WINXP:     return "WINXP";
    1423          43 :                 case SECTION_MAKERNOTE: return "MAKERNOTE";
    1424             :         }
    1425           0 :         return "";
    1426             : }
    1427             : 
    1428         402 : static tag_table_type exif_get_tag_table(int section)
    1429             : {
    1430         402 :         switch(section) {
    1431           0 :                 case SECTION_FILE:      return &tag_table_IFD[0];
    1432           0 :                 case SECTION_COMPUTED:  return &tag_table_IFD[0];
    1433           0 :                 case SECTION_ANY_TAG:   return &tag_table_IFD[0];
    1434         163 :                 case SECTION_IFD0:      return &tag_table_IFD[0];
    1435          19 :                 case SECTION_THUMBNAIL: return &tag_table_IFD[0];
    1436           0 :                 case SECTION_COMMENT:   return &tag_table_IFD[0];
    1437           0 :                 case SECTION_APP0:      return &tag_table_IFD[0];
    1438         162 :                 case SECTION_EXIF:      return &tag_table_IFD[0];
    1439           0 :                 case SECTION_FPIX:      return &tag_table_IFD[0];
    1440          36 :                 case SECTION_GPS:       return &tag_table_GPS[0];
    1441          16 :                 case SECTION_INTEROP:   return &tag_table_IOP[0];
    1442           0 :                 case SECTION_APP12:     return &tag_table_IFD[0];
    1443           6 :                 case SECTION_WINXP:     return &tag_table_IFD[0];
    1444             :         }
    1445           0 :         return &tag_table_IFD[0];
    1446             : }
    1447             : /* }}} */
    1448             : 
    1449             : /* {{{ exif_get_sectionlist
    1450             :    Return list of sectionnames specified by sectionlist. Return value must be freed
    1451             : */
    1452          35 : static char *exif_get_sectionlist(int sectionlist TSRMLS_DC)
    1453             : {
    1454          35 :         int i, len, ml = 0;
    1455             :         char *sections;
    1456             : 
    1457         525 :         for(i=0; i<SECTION_COUNT; i++) {
    1458         490 :                 ml += strlen(exif_get_sectionname(i))+2;
    1459             :         }
    1460          35 :         sections = safe_emalloc(ml, 1, 1);
    1461          35 :         sections[0] = '\0';
    1462          35 :         len = 0;
    1463         525 :         for(i=0; i<SECTION_COUNT; i++) {
    1464         490 :                 if (sectionlist&(1<<i)) {
    1465         104 :                         snprintf(sections+len, ml-len, "%s, ", exif_get_sectionname(i));
    1466         104 :                         len = strlen(sections);
    1467             :                 }
    1468             :         }
    1469          35 :         if (len>2)
    1470          31 :                 sections[len-2] = '\0';
    1471          35 :         return sections;
    1472             : }
    1473             : /* }}} */
    1474             : 
    1475             : /* {{{ struct image_info_type
    1476             :    This structure stores Exif header image elements in a simple manner
    1477             :    Used to store camera data as extracted from the various ways that it can be
    1478             :    stored in a nexif header
    1479             : */
    1480             : 
    1481             : typedef struct {
    1482             :         int     type;
    1483             :         size_t  size;
    1484             :         uchar   *data;
    1485             : } file_section;
    1486             : 
    1487             : typedef struct {
    1488             :         int             count;
    1489             :         file_section    *list;
    1490             : } file_section_list;
    1491             : 
    1492             : typedef struct {
    1493             :         image_filetype  filetype;
    1494             :         size_t          width, height;
    1495             :         size_t          size;
    1496             :         size_t          offset;
    1497             :         char            *data;
    1498             : } thumbnail_data;
    1499             : 
    1500             : typedef struct {
    1501             :         char                    *value;
    1502             :         size_t                  size;
    1503             :         int                             tag;
    1504             : } xp_field_type;
    1505             : 
    1506             : typedef struct {
    1507             :         int             count;
    1508             :         xp_field_type   *list;
    1509             : } xp_field_list;
    1510             : 
    1511             : /* This structure is used to store a section of a Jpeg file. */
    1512             : typedef struct {
    1513             :         php_stream      *infile;
    1514             :         char            *FileName;
    1515             :         time_t          FileDateTime;
    1516             :         size_t          FileSize;
    1517             :         image_filetype  FileType;
    1518             :         int             Height, Width;
    1519             :         int             IsColor;
    1520             : 
    1521             :         char            *make;
    1522             :         char            *model;
    1523             : 
    1524             :         float           ApertureFNumber;
    1525             :         float           ExposureTime;
    1526             :         double          FocalplaneUnits;
    1527             :         float           CCDWidth;
    1528             :         double          FocalplaneXRes;
    1529             :         size_t          ExifImageWidth;
    1530             :         float           FocalLength;
    1531             :         float           Distance;
    1532             : 
    1533             :         int             motorola_intel; /* 1 Motorola; 0 Intel */
    1534             : 
    1535             :         char            *UserComment;
    1536             :         int             UserCommentLength;
    1537             :         char            *UserCommentEncoding;
    1538             :         char            *encode_unicode;
    1539             :         char            *decode_unicode_be;
    1540             :         char            *decode_unicode_le;
    1541             :         char            *encode_jis;
    1542             :         char            *decode_jis_be;
    1543             :         char            *decode_jis_le;
    1544             :         char            *Copyright;/* EXIF standard defines Copyright as "<Photographer> [ '\0' <Editor> ] ['\0']" */
    1545             :         char            *CopyrightPhotographer;
    1546             :         char            *CopyrightEditor;
    1547             : 
    1548             :         xp_field_list   xp_fields;
    1549             : 
    1550             :         thumbnail_data  Thumbnail;
    1551             :         /* other */
    1552             :         int             sections_found; /* FOUND_<marker> */
    1553             :         image_info_list info_list[SECTION_COUNT];
    1554             :         /* for parsing */
    1555             :         int             read_thumbnail;
    1556             :         int             read_all;
    1557             :         int             ifd_nesting_level;
    1558             :         /* internal */
    1559             :         file_section_list       file;
    1560             : } image_info_type;
    1561             : /* }}} */
    1562             : 
    1563             : /* {{{ exif_error_docref */
    1564          14 : static void exif_error_docref(const char *docref EXIFERR_DC, const image_info_type *ImageInfo, int type, const char *format, ...)
    1565             : {
    1566             :         va_list args;
    1567             :         
    1568          14 :         va_start(args, format);
    1569             : #ifdef EXIF_DEBUG
    1570             :         {
    1571             :                 char *buf;
    1572             : 
    1573             :                 spprintf(&buf, 0, "%s(%d): %s", _file, _line, format);
    1574             :                 php_verror(docref, ImageInfo->FileName?ImageInfo->FileName:"", type, buf, args TSRMLS_CC);
    1575             :                 efree(buf);
    1576             :         }
    1577             : #else
    1578          14 :         php_verror(docref, ImageInfo->FileName?ImageInfo->FileName:"", type, format, args TSRMLS_CC);
    1579             : #endif
    1580          14 :         va_end(args);
    1581          14 : }
    1582             : /* }}} */
    1583             : 
    1584             : /* {{{ jpeg_sof_info
    1585             :  */
    1586             : typedef struct {
    1587             :         int     bits_per_sample;
    1588             :         size_t  width;
    1589             :         size_t  height;
    1590             :         int     num_components;
    1591             : } jpeg_sof_info;
    1592             : /* }}} */
    1593             : 
    1594             : /* {{{ exif_file_sections_add
    1595             :  Add a file_section to image_info
    1596             :  returns the used block or -1. if size>0 and data == NULL buffer of size is allocated
    1597             : */
    1598         256 : static int exif_file_sections_add(image_info_type *ImageInfo, int type, size_t size, uchar *data)
    1599             : {
    1600             :         file_section    *tmp;
    1601         256 :         int             count = ImageInfo->file.count;
    1602             : 
    1603         256 :         tmp = safe_erealloc(ImageInfo->file.list, (count+1), sizeof(file_section), 0);
    1604         256 :         ImageInfo->file.list = tmp;
    1605         256 :         ImageInfo->file.list[count].type = 0xFFFF;
    1606         256 :         ImageInfo->file.list[count].data = NULL;
    1607         256 :         ImageInfo->file.list[count].size = 0;
    1608         256 :         ImageInfo->file.count = count+1;
    1609         256 :         if (!size) {
    1610           0 :                 data = NULL;
    1611         256 :         } else if (data == NULL) {
    1612         256 :                 data = safe_emalloc(size, 1, 0);
    1613             :         }
    1614         256 :         ImageInfo->file.list[count].type = type;
    1615         256 :         ImageInfo->file.list[count].data = data;
    1616         256 :         ImageInfo->file.list[count].size = size;
    1617         256 :         return count;
    1618             : }
    1619             : /* }}} */
    1620             : 
    1621             : /* {{{ exif_file_sections_realloc
    1622             :  Reallocate a file section returns 0 on success and -1 on failure
    1623             : */
    1624          38 : static int exif_file_sections_realloc(image_info_type *ImageInfo, int section_index, size_t size TSRMLS_DC)
    1625             : {
    1626             :         void *tmp;
    1627             : 
    1628             :         /* This is not a malloc/realloc check. It is a plausibility check for the
    1629             :          * function parameters (requirements engineering).
    1630             :          */
    1631          38 :         if (section_index >= ImageInfo->file.count) {
    1632           0 :                 EXIF_ERRLOG_FSREALLOC(ImageInfo)
    1633           0 :                 return -1;
    1634             :         }
    1635          38 :         tmp = safe_erealloc(ImageInfo->file.list[section_index].data, 1, size, 0);
    1636          38 :         ImageInfo->file.list[section_index].data = tmp;
    1637          38 :         ImageInfo->file.list[section_index].size = size;
    1638          38 :         return 0;
    1639             : }
    1640             : /* }}} */
    1641             : 
    1642             : /* {{{ exif_file_section_free
    1643             :    Discard all file_sections in ImageInfo
    1644             : */
    1645          37 : static int exif_file_sections_free(image_info_type *ImageInfo)
    1646             : {
    1647             :         int i;
    1648             : 
    1649          37 :         if (ImageInfo->file.count) {
    1650         291 :                 for (i=0; i<ImageInfo->file.count; i++) {
    1651         256 :                         EFREE_IF(ImageInfo->file.list[i].data);
    1652             :                 }
    1653             :         }
    1654          37 :         EFREE_IF(ImageInfo->file.list);
    1655          37 :         ImageInfo->file.count = 0;
    1656          37 :         return TRUE;
    1657             : }
    1658             : /* }}} */
    1659             : 
    1660             : /* {{{ exif_iif_add_value
    1661             :  Add a value to image_info
    1662             : */
    1663         662 : static void exif_iif_add_value(image_info_type *image_info, int section_index, char *name, int tag, int format, int length, void* value, int motorola_intel TSRMLS_DC)
    1664             : {
    1665             :         size_t idex;
    1666             :         void *vptr;
    1667             :         image_info_value *info_value;
    1668             :         image_info_data  *info_data;
    1669             :         image_info_data  *list;
    1670             : 
    1671         662 :         if (length < 0) {
    1672           0 :                 return;
    1673             :         }
    1674             : 
    1675         662 :         list = safe_erealloc(image_info->info_list[section_index].list, (image_info->info_list[section_index].count+1), sizeof(image_info_data), 0);
    1676         662 :         image_info->info_list[section_index].list = list;
    1677             : 
    1678         662 :         info_data  = &image_info->info_list[section_index].list[image_info->info_list[section_index].count];
    1679         662 :         memset(info_data, 0, sizeof(image_info_data));
    1680         662 :         info_data->tag    = tag;
    1681         662 :         info_data->format = format;
    1682         662 :         info_data->length = length;
    1683         662 :         info_data->name   = estrdup(name);
    1684         662 :         info_value        = &info_data->value;
    1685             : 
    1686         662 :         switch (format) {
    1687             :                 case TAG_FMT_STRING:
    1688         198 :                         if (value) {
    1689         198 :                                 length = php_strnlen(value, length);
    1690         198 :                                 info_value->s = estrndup(value, length);
    1691         198 :                                 info_data->length = length;
    1692             :                         } else {
    1693           0 :                                 info_data->length = 0;
    1694           0 :                                 info_value->s = estrdup("");
    1695             :                         }
    1696         198 :                         break;
    1697             : 
    1698             :                 default:
    1699             :                         /* Standard says more types possible but skip them...
    1700             :                          * but allow users to handle data if they know how to
    1701             :                          * So not return but use type UNDEFINED
    1702             :                          * return;
    1703             :                          */
    1704           0 :                         info_data->tag = TAG_FMT_UNDEFINED;/* otherwise not freed from memory */
    1705             :                 case TAG_FMT_SBYTE:
    1706             :                 case TAG_FMT_BYTE:
    1707             :                 /* in contrast to strings bytes do not need to allocate buffer for NULL if length==0 */
    1708          22 :                         if (!length)
    1709           0 :                                 break;
    1710             :                 case TAG_FMT_UNDEFINED:
    1711          52 :                         if (value) {
    1712             :                                 /* do not recompute length here */
    1713          52 :                                 info_value->s = estrndup(value, length);
    1714          52 :                                 info_data->length = length;
    1715             :                         } else {
    1716           0 :                                 info_data->length = 0;
    1717           0 :                                 info_value->s = estrdup("");
    1718             :                         }
    1719          52 :                         break;
    1720             : 
    1721             :                 case TAG_FMT_USHORT:
    1722             :                 case TAG_FMT_ULONG:
    1723             :                 case TAG_FMT_URATIONAL:
    1724             :                 case TAG_FMT_SSHORT:
    1725             :                 case TAG_FMT_SLONG:
    1726             :                 case TAG_FMT_SRATIONAL:
    1727             :                 case TAG_FMT_SINGLE:
    1728             :                 case TAG_FMT_DOUBLE:
    1729         412 :                         if (length==0) {
    1730           0 :                                 break;
    1731             :                         } else
    1732         412 :                         if (length>1) {
    1733          45 :                                 info_value->list = safe_emalloc(length, sizeof(image_info_value), 0);
    1734             :                         } else {
    1735         367 :                                 info_value = &info_data->value;
    1736             :                         }
    1737        8702 :                         for (idex=0,vptr=value; idex<(size_t)length; idex++,vptr=(char *) vptr + php_tiff_bytes_per_format[format]) {
    1738        8290 :                                 if (length>1) {
    1739        7923 :                                         info_value = &info_data->value.list[idex];
    1740             :                                 }
    1741        8290 :                                 switch (format) {
    1742             :                                         case TAG_FMT_USHORT:
    1743        8010 :                                                 info_value->u = php_ifd_get16u(vptr, motorola_intel);
    1744        8010 :                                                 break;
    1745             : 
    1746             :                                         case TAG_FMT_ULONG:
    1747          64 :                                                 info_value->u = php_ifd_get32u(vptr, motorola_intel);
    1748          64 :                                                 break;
    1749             : 
    1750             :                                         case TAG_FMT_URATIONAL:
    1751         173 :                                                 info_value->ur.num = php_ifd_get32u(vptr, motorola_intel);
    1752         173 :                                                 info_value->ur.den = php_ifd_get32u(4+(char *)vptr, motorola_intel);
    1753         173 :                                                 break;
    1754             : 
    1755             :                                         case TAG_FMT_SSHORT:
    1756           0 :                                                 info_value->i = php_ifd_get16s(vptr, motorola_intel);
    1757           0 :                                                 break;
    1758             : 
    1759             :                                         case TAG_FMT_SLONG:
    1760           1 :                                                 info_value->i = php_ifd_get32s(vptr, motorola_intel);
    1761           1 :                                                 break;
    1762             : 
    1763             :                                         case TAG_FMT_SRATIONAL:
    1764           6 :                                                 info_value->sr.num = php_ifd_get32u(vptr, motorola_intel);
    1765           6 :                                                 info_value->sr.den = php_ifd_get32u(4+(char *)vptr, motorola_intel);
    1766           6 :                                                 break;
    1767             : 
    1768             :                                         case TAG_FMT_SINGLE:
    1769             : #ifdef EXIF_DEBUG
    1770             :                                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Found value of type single");
    1771             : #endif
    1772           0 :                                                 info_value->f = *(float *)value;
    1773             : 
    1774             :                                         case TAG_FMT_DOUBLE:
    1775             : #ifdef EXIF_DEBUG
    1776             :                                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Found value of type double");
    1777             : #endif
    1778          36 :                                                 info_value->d = *(double *)value;
    1779             :                                                 break;
    1780             :                                 }
    1781             :                         }
    1782             :         }
    1783         662 :         image_info->sections_found |= 1<<section_index;
    1784         662 :         image_info->info_list[section_index].count++;
    1785             : }
    1786             : /* }}} */
    1787             : 
    1788             : /* {{{ exif_iif_add_tag
    1789             :  Add a tag from IFD to image_info
    1790             : */
    1791         662 : static void exif_iif_add_tag(image_info_type *image_info, int section_index, char *name, int tag, int format, size_t length, void* value TSRMLS_DC)
    1792             : {
    1793         662 :         exif_iif_add_value(image_info, section_index, name, tag, format, (int)length, value, image_info->motorola_intel TSRMLS_CC);
    1794         662 : }
    1795             : /* }}} */
    1796             : 
    1797             : /* {{{ exif_iif_add_int
    1798             :  Add an int value to image_info
    1799             : */
    1800         221 : static void exif_iif_add_int(image_info_type *image_info, int section_index, char *name, int value TSRMLS_DC)
    1801             : {
    1802             :         image_info_data  *info_data;
    1803             :         image_info_data  *list;
    1804             : 
    1805         221 :         list = safe_erealloc(image_info->info_list[section_index].list, (image_info->info_list[section_index].count+1), sizeof(image_info_data), 0);
    1806         221 :         image_info->info_list[section_index].list = list;
    1807             : 
    1808         221 :         info_data  = &image_info->info_list[section_index].list[image_info->info_list[section_index].count];
    1809         221 :         info_data->tag    = TAG_NONE;
    1810         221 :         info_data->format = TAG_FMT_SLONG;
    1811         221 :         info_data->length = 1;
    1812         221 :         info_data->name   = estrdup(name);
    1813         221 :         info_data->value.i = value;
    1814         221 :         image_info->sections_found |= 1<<section_index;
    1815         221 :         image_info->info_list[section_index].count++;
    1816         221 : }
    1817             : /* }}} */
    1818             : 
    1819             : /* {{{ exif_iif_add_str
    1820             :  Add a string value to image_info MUST BE NUL TERMINATED
    1821             : */
    1822         247 : static void exif_iif_add_str(image_info_type *image_info, int section_index, char *name, char *value TSRMLS_DC)
    1823             : {
    1824             :         image_info_data  *info_data;
    1825             :         image_info_data  *list;
    1826             : 
    1827         247 :         if (value) {
    1828         217 :                 list = safe_erealloc(image_info->info_list[section_index].list, (image_info->info_list[section_index].count+1), sizeof(image_info_data), 0);
    1829         217 :                 image_info->info_list[section_index].list = list;
    1830         217 :                 info_data  = &image_info->info_list[section_index].list[image_info->info_list[section_index].count];
    1831         217 :                 info_data->tag    = TAG_NONE;
    1832         217 :                 info_data->format = TAG_FMT_STRING;
    1833         217 :                 info_data->length = 1;
    1834         217 :                 info_data->name   = estrdup(name);
    1835         217 :                 info_data->value.s = estrdup(value);
    1836         217 :                 image_info->sections_found |= 1<<section_index;
    1837         217 :                 image_info->info_list[section_index].count++;
    1838             :         }
    1839         247 : }
    1840             : /* }}} */
    1841             : 
    1842             : /* {{{ exif_iif_add_fmt
    1843             :  Add a format string value to image_info MUST BE NUL TERMINATED
    1844             : */
    1845          43 : static void exif_iif_add_fmt(image_info_type *image_info, int section_index, char *name TSRMLS_DC, char *value, ...)
    1846             : {
    1847             :         char             *tmp;
    1848             :         va_list                  arglist;
    1849             : 
    1850          43 :         va_start(arglist, value);
    1851          43 :         if (value) {
    1852          43 :                 vspprintf(&tmp, 0, value, arglist);
    1853          43 :                 exif_iif_add_str(image_info, section_index, name, tmp TSRMLS_CC);
    1854          43 :                 efree(tmp);
    1855             :         }
    1856          43 :         va_end(arglist);
    1857          43 : }
    1858             : /* }}} */
    1859             : 
    1860             : /* {{{ exif_iif_add_str
    1861             :  Add a string value to image_info MUST BE NUL TERMINATED
    1862             : */
    1863           7 : static void exif_iif_add_buffer(image_info_type *image_info, int section_index, char *name, int length, char *value TSRMLS_DC)
    1864             : {
    1865             :         image_info_data  *info_data;
    1866             :         image_info_data  *list;
    1867             : 
    1868           7 :         if (value) {
    1869           7 :                 list = safe_erealloc(image_info->info_list[section_index].list, (image_info->info_list[section_index].count+1), sizeof(image_info_data), 0);
    1870           7 :                 image_info->info_list[section_index].list = list;
    1871           7 :                 info_data  = &image_info->info_list[section_index].list[image_info->info_list[section_index].count];
    1872           7 :                 info_data->tag    = TAG_NONE;
    1873           7 :                 info_data->format = TAG_FMT_UNDEFINED;
    1874           7 :                 info_data->length = length;
    1875           7 :                 info_data->name   = estrdup(name);
    1876           7 :                 info_data->value.s = safe_emalloc(length, 1, 1);
    1877           7 :                 memcpy(info_data->value.s, value, length);
    1878           7 :                 info_data->value.s[length] = 0;
    1879           7 :                 image_info->sections_found |= 1<<section_index;
    1880           7 :                 image_info->info_list[section_index].count++;
    1881             :         }
    1882           7 : }
    1883             : /* }}} */
    1884             : 
    1885             : /* {{{ exif_iif_free
    1886             :  Free memory allocated for image_info
    1887             : */
    1888         518 : static void exif_iif_free(image_info_type *image_info, int section_index) {
    1889             :         int  i;
    1890             :         void *f; /* faster */
    1891             : 
    1892         518 :         if (image_info->info_list[section_index].count) {
    1893        1244 :                 for (i=0; i < image_info->info_list[section_index].count; i++) {
    1894        1107 :                         if ((f=image_info->info_list[section_index].list[i].name) != NULL) {
    1895        1107 :                                 efree(f);
    1896             :                         }
    1897        1107 :                         switch(image_info->info_list[section_index].list[i].format) {
    1898             :                                 case TAG_FMT_SBYTE:
    1899             :                                 case TAG_FMT_BYTE:
    1900             :                                         /* in contrast to strings bytes do not need to allocate buffer for NULL if length==0 */
    1901          22 :                                         if (image_info->info_list[section_index].list[i].length<1)
    1902           0 :                                                 break;
    1903             :                                 default:
    1904             :                                 case TAG_FMT_UNDEFINED:
    1905             :                                 case TAG_FMT_STRING:
    1906         474 :                                         if ((f=image_info->info_list[section_index].list[i].value.s) != NULL) {
    1907         474 :                                                 efree(f);
    1908             :                                         }
    1909         474 :                                         break;
    1910             : 
    1911             :                                 case TAG_FMT_USHORT:
    1912             :                                 case TAG_FMT_ULONG:
    1913             :                                 case TAG_FMT_URATIONAL:
    1914             :                                 case TAG_FMT_SSHORT:
    1915             :                                 case TAG_FMT_SLONG:
    1916             :                                 case TAG_FMT_SRATIONAL:
    1917             :                                 case TAG_FMT_SINGLE:
    1918             :                                 case TAG_FMT_DOUBLE:
    1919             :                                         /* nothing to do here */
    1920         633 :                                         if (image_info->info_list[section_index].list[i].length > 1) {
    1921          45 :                                                 if ((f=image_info->info_list[section_index].list[i].value.list) != NULL) {
    1922          45 :                                                         efree(f);
    1923             :                                                 }
    1924             :                                         }
    1925             :                                         break;
    1926             :                         }
    1927             :                 }
    1928             :         }
    1929         518 :         EFREE_IF(image_info->info_list[section_index].list);
    1930         518 : }
    1931             : /* }}} */
    1932             : 
    1933             : /* {{{ add_assoc_image_info
    1934             :  * Add image_info to associative array value. */
    1935         403 : static void add_assoc_image_info(zval *value, int sub_array, image_info_type *image_info, int section_index TSRMLS_DC)
    1936             : {
    1937             :         char    buffer[64], *val, *name, uname[64];
    1938         403 :         int     i, ap, l, b, idx=0, unknown=0;
    1939             : #ifdef EXIF_DEBUG
    1940             :         int     info_tag;
    1941             : #endif
    1942             :         image_info_value *info_value;
    1943             :         image_info_data  *info_data;
    1944             :         zval                     tmpi, array;
    1945             : 
    1946             : #ifdef EXIF_DEBUG
    1947             : /*              php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Adding %d infos from section %s", image_info->info_list[section_index].count, exif_get_sectionname(section_index));*/
    1948             : #endif
    1949         403 :         if (image_info->info_list[section_index].count) {
    1950         131 :                 if (sub_array) {
    1951          49 :                         array_init(&tmpi);
    1952             :                 } else {
    1953          82 :                         ZVAL_COPY_VALUE(&tmpi, value);
    1954             :                 }
    1955             : 
    1956        1213 :                 for(i=0; i<image_info->info_list[section_index].count; i++) {
    1957        1082 :                         info_data  = &image_info->info_list[section_index].list[i];
    1958             : #ifdef EXIF_DEBUG
    1959             :                         info_tag   = info_data->tag; /* conversion */
    1960             : #endif
    1961        1082 :                         info_value = &info_data->value;
    1962        1082 :                         if (!(name = info_data->name)) {
    1963           0 :                                 snprintf(uname, sizeof(uname), "%d", unknown++);
    1964           0 :                                 name = uname;
    1965             :                         }
    1966             : #ifdef EXIF_DEBUG
    1967             : /*              php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Adding infos: tag(0x%04X,%12s,L=0x%04X): %s", info_tag, exif_get_tagname(info_tag, buffer, -12, exif_get_tag_table(section_index) TSRMLS_CC), info_data->length, info_data->format==TAG_FMT_STRING?(info_value&&info_value->s?info_value->s:"<no data>"):exif_get_tagformat(info_data->format));*/
    1968             : #endif
    1969        1082 :                         if (info_data->length==0) {
    1970           6 :                                 add_assoc_null(&tmpi, name);
    1971             :                         } else {
    1972        1076 :                                 switch (info_data->format) {
    1973             :                                         default:
    1974             :                                                 /* Standard says more types possible but skip them...
    1975             :                                                  * but allow users to handle data if they know how to
    1976             :                                                  * So not return but use type UNDEFINED
    1977             :                                                  * return;
    1978             :                                                  */
    1979             :                                         case TAG_FMT_BYTE:
    1980             :                                         case TAG_FMT_SBYTE:
    1981             :                                         case TAG_FMT_UNDEFINED:
    1982          55 :                                                 if (!info_value->s) {
    1983           0 :                                                         add_assoc_stringl(&tmpi, name, "", 0);
    1984             :                                                 } else {
    1985          55 :                                                         add_assoc_stringl(&tmpi, name, info_value->s, info_data->length);
    1986             :                                                 }
    1987          55 :                                                 break;
    1988             : 
    1989             :                                         case TAG_FMT_STRING:
    1990         402 :                                                 if (!(val = info_value->s)) {
    1991           0 :                                                         val = "";
    1992             :                                                 }
    1993         402 :                                                 if (section_index==SECTION_COMMENT) {
    1994          12 :                                                         add_index_string(&tmpi, idx++, val);
    1995             :                                                 } else {
    1996         390 :                                                         add_assoc_string(&tmpi, name, val);
    1997             :                                                 }
    1998         402 :                                                 break;
    1999             : 
    2000             :                                         case TAG_FMT_URATIONAL:
    2001             :                                         case TAG_FMT_SRATIONAL:
    2002             :                                         /*case TAG_FMT_BYTE:
    2003             :                                         case TAG_FMT_SBYTE:*/
    2004             :                                         case TAG_FMT_USHORT:
    2005             :                                         case TAG_FMT_SSHORT:
    2006             :                                         case TAG_FMT_SINGLE:
    2007             :                                         case TAG_FMT_DOUBLE:
    2008             :                                         case TAG_FMT_ULONG:
    2009             :                                         case TAG_FMT_SLONG:
    2010             :                                                 /* now the rest, first see if it becomes an array */
    2011         619 :                                                 if ((l = info_data->length) > 1) {
    2012          44 :                                                         array_init(&array);
    2013             :                                                 }
    2014        9081 :                                                 for(ap=0; ap<l; ap++) {
    2015        8462 :                                                         if (l>1) {
    2016        7887 :                                                                 info_value = &info_data->value.list[ap];
    2017             :                                                         }
    2018        8462 :                                                         switch (info_data->format) {
    2019             :                                                                 case TAG_FMT_BYTE:
    2020           0 :                                                                         if (l>1) {
    2021           0 :                                                                                 info_value = &info_data->value;
    2022           0 :                                                                                 for (b=0;b<l;b++) {
    2023           0 :                                                                                         add_index_long(&array, b, (int)(info_value->s[b]));
    2024             :                                                                                 }
    2025           0 :                                                                                 break;
    2026             :                                                                         }
    2027             :                                                                 case TAG_FMT_USHORT:
    2028             :                                                                 case TAG_FMT_ULONG:
    2029        8065 :                                                                         if (l==1) {
    2030         250 :                                                                                 add_assoc_long(&tmpi, name, (int)info_value->u);
    2031             :                                                                         } else {
    2032        7815 :                                                                                 add_index_long(&array, ap, (int)info_value->u);
    2033             :                                                                         }
    2034        8065 :                                                                         break;
    2035             : 
    2036             :                                                                 case TAG_FMT_URATIONAL:
    2037         169 :                                                                         snprintf(buffer, sizeof(buffer), "%i/%i", info_value->ur.num, info_value->ur.den);
    2038         169 :                                                                         if (l==1) {
    2039          97 :                                                                                 add_assoc_string(&tmpi, name, buffer);
    2040             :                                                                         } else {
    2041          72 :                                                                                 add_index_string(&array, ap, buffer);
    2042             :                                                                         }
    2043         169 :                                                                         break;
    2044             : 
    2045             :                                                                 case TAG_FMT_SBYTE:
    2046           0 :                                                                         if (l>1) {
    2047           0 :                                                                                 info_value = &info_data->value;
    2048           0 :                                                                                 for (b=0;b<l;b++) {
    2049           0 :                                                                                         add_index_long(&array, ap, (int)info_value->s[b]);
    2050             :                                                                                 }
    2051           0 :                                                                                 break;
    2052             :                                                                         }
    2053             :                                                                 case TAG_FMT_SSHORT:
    2054             :                                                                 case TAG_FMT_SLONG:
    2055         222 :                                                                         if (l==1) {
    2056         222 :                                                                                 add_assoc_long(&tmpi, name, info_value->i);
    2057             :                                                                         } else {
    2058           0 :                                                                                 add_index_long(&array, ap, info_value->i);
    2059             :                                                                         }
    2060         222 :                                                                         break;
    2061             : 
    2062             :                                                                 case TAG_FMT_SRATIONAL:
    2063           6 :                                                                         snprintf(buffer, sizeof(buffer), "%i/%i", info_value->sr.num, info_value->sr.den);
    2064           6 :                                                                         if (l==1) {
    2065           6 :                                                                                 add_assoc_string(&tmpi, name, buffer);
    2066             :                                                                         } else {
    2067           0 :                                                                                 add_index_string(&array, ap, buffer);
    2068             :                                                                         }
    2069           6 :                                                                         break;
    2070             : 
    2071             :                                                                 case TAG_FMT_SINGLE:
    2072           0 :                                                                         if (l==1) {
    2073           0 :                                                                                 add_assoc_double(&tmpi, name, info_value->f);
    2074             :                                                                         } else {
    2075           0 :                                                                                 add_index_double(&array, ap, info_value->f);
    2076             :                                                                         }
    2077           0 :                                                                         break;
    2078             : 
    2079             :                                                                 case TAG_FMT_DOUBLE:
    2080           0 :                                                                         if (l==1) {
    2081           0 :                                                                                 add_assoc_double(&tmpi, name, info_value->d);
    2082             :                                                                         } else {
    2083           0 :                                                                                 add_index_double(&array, ap, info_value->d);
    2084             :                                                                         }
    2085             :                                                                         break;
    2086             :                                                         }
    2087        8462 :                                                         info_value = &info_data->value.list[ap];
    2088             :                                                 }
    2089         619 :                                                 if (l>1) {
    2090          44 :                                                         add_assoc_zval(&tmpi, name, &array);
    2091             :                                                 }
    2092             :                                                 break;
    2093             :                                 }
    2094             :                         }
    2095             :                 }
    2096         131 :                 if (sub_array) {
    2097          49 :                         add_assoc_zval(value, exif_get_sectionname(section_index), &tmpi);
    2098             :                 }
    2099             :         }
    2100         403 : }
    2101             : /* }}} */
    2102             : 
    2103             : /* {{{ Markers
    2104             :    JPEG markers consist of one or more 0xFF bytes, followed by a marker
    2105             :    code byte (which is not an FF).  Here are the marker codes of interest
    2106             :    in this program.  (See jdmarker.c for a more complete list.)
    2107             : */
    2108             : 
    2109             : #define M_TEM   0x01    /* temp for arithmetic coding              */
    2110             : #define M_RES   0x02    /* reserved                                */
    2111             : #define M_SOF0  0xC0    /* Start Of Frame N                        */
    2112             : #define M_SOF1  0xC1    /* N indicates which compression process   */
    2113             : #define M_SOF2  0xC2    /* Only SOF0-SOF2 are now in common use    */
    2114             : #define M_SOF3  0xC3
    2115             : #define M_DHT   0xC4
    2116             : #define M_SOF5  0xC5    /* NB: codes C4 and CC are NOT SOF markers */
    2117             : #define M_SOF6  0xC6
    2118             : #define M_SOF7  0xC7
    2119             : #define M_JPEG  0x08    /* reserved for extensions                 */
    2120             : #define M_SOF9  0xC9
    2121             : #define M_SOF10 0xCA
    2122             : #define M_SOF11 0xCB
    2123             : #define M_DAC   0xCC    /* arithmetic table                         */
    2124             : #define M_SOF13 0xCD
    2125             : #define M_SOF14 0xCE
    2126             : #define M_SOF15 0xCF
    2127             : #define M_RST0  0xD0    /* restart segment                          */
    2128             : #define M_RST1  0xD1
    2129             : #define M_RST2  0xD2
    2130             : #define M_RST3  0xD3
    2131             : #define M_RST4  0xD4
    2132             : #define M_RST5  0xD5
    2133             : #define M_RST6  0xD6
    2134             : #define M_RST7  0xD7
    2135             : #define M_SOI   0xD8    /* Start Of Image (beginning of datastream) */
    2136             : #define M_EOI   0xD9    /* End Of Image (end of datastream)         */
    2137             : #define M_SOS   0xDA    /* Start Of Scan (begins compressed data)   */
    2138             : #define M_DQT   0xDB
    2139             : #define M_DNL   0xDC
    2140             : #define M_DRI   0xDD
    2141             : #define M_DHP   0xDE
    2142             : #define M_EXP   0xDF
    2143             : #define M_APP0  0xE0    /* JPEG: 'JFIFF' AND (additional 'JFXX')    */
    2144             : #define M_EXIF  0xE1    /* Exif Attribute Information               */
    2145             : #define M_APP2  0xE2    /* Flash Pix Extension Data?                */
    2146             : #define M_APP3  0xE3
    2147             : #define M_APP4  0xE4
    2148             : #define M_APP5  0xE5
    2149             : #define M_APP6  0xE6
    2150             : #define M_APP7  0xE7
    2151             : #define M_APP8  0xE8
    2152             : #define M_APP9  0xE9
    2153             : #define M_APP10 0xEA
    2154             : #define M_APP11 0xEB
    2155             : #define M_APP12 0xEC
    2156             : #define M_APP13 0xED    /* IPTC International Press Telecommunications Council */
    2157             : #define M_APP14 0xEE    /* Software, Copyright?                     */
    2158             : #define M_APP15 0xEF
    2159             : #define M_JPG0  0xF0
    2160             : #define M_JPG1  0xF1
    2161             : #define M_JPG2  0xF2
    2162             : #define M_JPG3  0xF3
    2163             : #define M_JPG4  0xF4
    2164             : #define M_JPG5  0xF5
    2165             : #define M_JPG6  0xF6
    2166             : #define M_JPG7  0xF7
    2167             : #define M_JPG8  0xF8
    2168             : #define M_JPG9  0xF9
    2169             : #define M_JPG10 0xFA
    2170             : #define M_JPG11 0xFB
    2171             : #define M_JPG12 0xFC
    2172             : #define M_JPG13 0xFD
    2173             : #define M_COM   0xFE    /* COMment                                  */
    2174             : 
    2175             : #define M_PSEUDO 0x123  /* Extra value.                             */
    2176             : 
    2177             : /* }}} */
    2178             : 
    2179             : /* {{{ jpeg2000 markers
    2180             :  */
    2181             : /* Markers x30 - x3F do not have a segment */
    2182             : /* Markers x00, x01, xFE, xC0 - xDF ISO/IEC 10918-1 -> M_<xx> */
    2183             : /* Markers xF0 - xF7 ISO/IEC 10918-3 */
    2184             : /* Markers xF7 - xF8 ISO/IEC 14495-1 */
    2185             : /* XY=Main/Tile-header:(R:required, N:not_allowed, O:optional, L:last_marker) */
    2186             : #define JC_SOC   0x4F   /* NN, Start of codestream                          */
    2187             : #define JC_SIZ   0x51   /* RN, Image and tile size                          */
    2188             : #define JC_COD   0x52   /* RO, Codeing style defaulte                       */
    2189             : #define JC_COC   0x53   /* OO, Coding style component                       */
    2190             : #define JC_TLM   0x55   /* ON, Tile part length main header                 */
    2191             : #define JC_PLM   0x57   /* ON, Packet length main header                    */
    2192             : #define JC_PLT   0x58   /* NO, Packet length tile part header               */
    2193             : #define JC_QCD   0x5C   /* RO, Quantization default                         */
    2194             : #define JC_QCC   0x5D   /* OO, Quantization component                       */
    2195             : #define JC_RGN   0x5E   /* OO, Region of interest                           */
    2196             : #define JC_POD   0x5F   /* OO, Progression order default                    */
    2197             : #define JC_PPM   0x60   /* ON, Packed packet headers main header            */
    2198             : #define JC_PPT   0x61   /* NO, Packet packet headers tile part header       */
    2199             : #define JC_CME   0x64   /* OO, Comment: "LL E <text>" E=0:binary, E=1:ascii */
    2200             : #define JC_SOT   0x90   /* NR, Start of tile                                */
    2201             : #define JC_SOP   0x91   /* NO, Start of packeter default                    */
    2202             : #define JC_EPH   0x92   /* NO, End of packet header                         */
    2203             : #define JC_SOD   0x93   /* NL, Start of data                                */
    2204             : #define JC_EOC   0xD9   /* NN, End of codestream                            */
    2205             : /* }}} */
    2206             : 
    2207             : /* {{{ exif_process_COM
    2208             :    Process a COM marker.
    2209             :    We want to print out the marker contents as legible text;
    2210             :    we must guard against random junk and varying newline representations.
    2211             : */
    2212          15 : static void exif_process_COM (image_info_type *image_info, char *value, size_t length TSRMLS_DC)
    2213             : {
    2214          15 :         exif_iif_add_tag(image_info, SECTION_COMMENT, "Comment", TAG_COMPUTED_VALUE, TAG_FMT_STRING, length-2, value+2 TSRMLS_CC);
    2215          15 : }
    2216             : /* }}} */
    2217             : 
    2218             : /* {{{ exif_process_CME
    2219             :    Process a CME marker.
    2220             :    We want to print out the marker contents as legible text;
    2221             :    we must guard against random junk and varying newline representations.
    2222             : */
    2223             : #ifdef EXIF_JPEG2000
    2224             : static void exif_process_CME (image_info_type *image_info, char *value, size_t length TSRMLS_DC)
    2225             : {
    2226             :         if (length>3) {
    2227             :                 switch(value[2]) {
    2228             :                         case 0:
    2229             :                                 exif_iif_add_tag(image_info, SECTION_COMMENT, "Comment", TAG_COMPUTED_VALUE, TAG_FMT_UNDEFINED, length, value TSRMLS_CC);
    2230             :                                 break;
    2231             :                         case 1:
    2232             :                                 exif_iif_add_tag(image_info, SECTION_COMMENT, "Comment", TAG_COMPUTED_VALUE, TAG_FMT_STRING, length, value);
    2233             :                                 break;
    2234             :                         default:
    2235             :                                 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Undefined JPEG2000 comment encoding");
    2236             :                                 break;
    2237             :                 }
    2238             :         } else {
    2239             :                 exif_iif_add_tag(image_info, SECTION_COMMENT, "Comment", TAG_COMPUTED_VALUE, TAG_FMT_UNDEFINED, 0, NULL);
    2240             :                 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "JPEG2000 comment section too small");
    2241             :         }
    2242             : }
    2243             : #endif
    2244             : /* }}} */
    2245             : 
    2246             : /* {{{ exif_process_SOFn
    2247             :  * Process a SOFn marker.  This is useful for the image dimensions */
    2248          24 : static void exif_process_SOFn (uchar *Data, int marker, jpeg_sof_info *result)
    2249             : {
    2250             : /* 0xFF SOSn SectLen(2) Bits(1) Height(2) Width(2) Channels(1)  3*Channels (1)  */
    2251          24 :         result->bits_per_sample = Data[2];
    2252          24 :         result->height          = php_jpg_get16(Data+3);
    2253          24 :         result->width           = php_jpg_get16(Data+5);
    2254          24 :         result->num_components  = Data[7];
    2255             : 
    2256             : /*      switch (marker) {
    2257             :                 case M_SOF0:  process = "Baseline";  break;
    2258             :                 case M_SOF1:  process = "Extended sequential";  break;
    2259             :                 case M_SOF2:  process = "Progressive";  break;
    2260             :                 case M_SOF3:  process = "Lossless";  break;
    2261             :                 case M_SOF5:  process = "Differential sequential";  break;
    2262             :                 case M_SOF6:  process = "Differential progressive";  break;
    2263             :                 case M_SOF7:  process = "Differential lossless";  break;
    2264             :                 case M_SOF9:  process = "Extended sequential, arithmetic coding";  break;
    2265             :                 case M_SOF10: process = "Progressive, arithmetic coding";  break;
    2266             :                 case M_SOF11: process = "Lossless, arithmetic coding";  break;
    2267             :                 case M_SOF13: process = "Differential sequential, arithmetic coding";  break;
    2268             :                 case M_SOF14: process = "Differential progressive, arithmetic coding"; break;
    2269             :                 case M_SOF15: process = "Differential lossless, arithmetic coding";  break;
    2270             :                 default:      process = "Unknown";  break;
    2271             :         }*/
    2272          24 : }
    2273             : /* }}} */
    2274             : 
    2275             : /* forward declarations */
    2276             : static int exif_process_IFD_in_JPEG(image_info_type *ImageInfo, char *dir_start, char *offset_base, size_t IFDlength, size_t displacement, int section_index TSRMLS_DC);
    2277             : static int exif_process_IFD_TAG(    image_info_type *ImageInfo, char *dir_entry, char *offset_base, size_t IFDlength, size_t displacement, int section_index, int ReadNextIFD, tag_table_type tag_table TSRMLS_DC);
    2278             : 
    2279             : /* {{{ exif_get_markername
    2280             :         Get name of marker */
    2281             : #ifdef EXIF_DEBUG
    2282             : static char * exif_get_markername(int marker)
    2283             : {
    2284             :         switch(marker) {
    2285             :                 case 0xC0: return "SOF0";
    2286             :                 case 0xC1: return "SOF1";
    2287             :                 case 0xC2: return "SOF2";
    2288             :                 case 0xC3: return "SOF3";
    2289             :                 case 0xC4: return "DHT";
    2290             :                 case 0xC5: return "SOF5";
    2291             :                 case 0xC6: return "SOF6";
    2292             :                 case 0xC7: return "SOF7";
    2293             :                 case 0xC9: return "SOF9";
    2294             :                 case 0xCA: return "SOF10";
    2295             :                 case 0xCB: return "SOF11";
    2296             :                 case 0xCD: return "SOF13";
    2297             :                 case 0xCE: return "SOF14";
    2298             :                 case 0xCF: return "SOF15";
    2299             :                 case 0xD8: return "SOI";
    2300             :                 case 0xD9: return "EOI";
    2301             :                 case 0xDA: return "SOS";
    2302             :                 case 0xDB: return "DQT";
    2303             :                 case 0xDC: return "DNL";
    2304             :                 case 0xDD: return "DRI";
    2305             :                 case 0xDE: return "DHP";
    2306             :                 case 0xDF: return "EXP";
    2307             :                 case 0xE0: return "APP0";
    2308             :                 case 0xE1: return "EXIF";
    2309             :                 case 0xE2: return "FPIX";
    2310             :                 case 0xE3: return "APP3";
    2311             :                 case 0xE4: return "APP4";
    2312             :                 case 0xE5: return "APP5";
    2313             :                 case 0xE6: return "APP6";
    2314             :                 case 0xE7: return "APP7";
    2315             :                 case 0xE8: return "APP8";
    2316             :                 case 0xE9: return "APP9";
    2317             :                 case 0xEA: return "APP10";
    2318             :                 case 0xEB: return "APP11";
    2319             :                 case 0xEC: return "APP12";
    2320             :                 case 0xED: return "APP13";
    2321             :                 case 0xEE: return "APP14";
    2322             :                 case 0xEF: return "APP15";
    2323             :                 case 0xF0: return "JPG0";
    2324             :                 case 0xFD: return "JPG13";
    2325             :                 case 0xFE: return "COM";
    2326             :                 case 0x01: return "TEM";
    2327             :         }
    2328             :         return "Unknown";
    2329             : }
    2330             : #endif
    2331             : /* }}} */
    2332             : 
    2333             : /* {{{ proto string exif_tagname(index)
    2334             :         Get headername for index or false if not defined */
    2335          31 : PHP_FUNCTION(exif_tagname)
    2336             : {
    2337             :         zend_long tag;
    2338             :         char *szTemp;
    2339             : 
    2340          31 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &tag) == FAILURE) {
    2341          11 :                 return;
    2342             :         }
    2343             : 
    2344          20 :         szTemp = exif_get_tagname(tag, NULL, 0, tag_table_IFD TSRMLS_CC);
    2345             : 
    2346          20 :         if (tag < 0 || !szTemp || !szTemp[0]) {
    2347          17 :                 RETURN_FALSE;
    2348             :         }
    2349             : 
    2350           6 :         RETURN_STRING(szTemp)
    2351             : }
    2352             : /* }}} */
    2353             : 
    2354             : /* {{{ exif_ifd_make_value
    2355             :  * Create a value for an ifd from an info_data pointer */
    2356           2 : static void* exif_ifd_make_value(image_info_data *info_data, int motorola_intel TSRMLS_DC) {
    2357             :         size_t  byte_count;
    2358             :         char    *value_ptr, *data_ptr;
    2359             :         size_t  i;
    2360             : 
    2361             :         image_info_value  *info_value;
    2362             : 
    2363           2 :         byte_count = php_tiff_bytes_per_format[info_data->format] * info_data->length;
    2364           2 :         value_ptr = safe_emalloc(max(byte_count, 4), 1, 0);
    2365           2 :         memset(value_ptr, 0, 4);
    2366           2 :         if (!info_data->length) {
    2367           0 :                 return value_ptr;
    2368             :         }
    2369           8 :         if (info_data->format == TAG_FMT_UNDEFINED || info_data->format == TAG_FMT_STRING
    2370           6 :           || (byte_count>1 && (info_data->format == TAG_FMT_BYTE || info_data->format == TAG_FMT_SBYTE))
    2371             :         ) {
    2372           0 :                 memmove(value_ptr, info_data->value.s, byte_count);
    2373           0 :                 return value_ptr;
    2374           2 :         } else if (info_data->format == TAG_FMT_BYTE) {
    2375           0 :                 *value_ptr = info_data->value.u;
    2376           0 :                 return value_ptr;
    2377           2 :         } else if (info_data->format == TAG_FMT_SBYTE) {
    2378           0 :                 *value_ptr = info_data->value.i;
    2379           0 :                 return value_ptr;
    2380             :         } else {
    2381           2 :                 data_ptr = value_ptr;
    2382          39 :                 for(i=0; i<info_data->length; i++) {
    2383          37 :                         if (info_data->length==1) {
    2384           1 :                                 info_value = &info_data->value;
    2385             :                         } else {
    2386          36 :                                 info_value = &info_data->value.list[i];
    2387             :                         }
    2388          37 :                         switch(info_data->format) {
    2389             :                                 case TAG_FMT_USHORT:
    2390           0 :                                         php_ifd_set16u(data_ptr, info_value->u, motorola_intel);
    2391           0 :                                         data_ptr += 2;
    2392           0 :                                         break;
    2393             :                                 case TAG_FMT_ULONG:
    2394           1 :                                         php_ifd_set32u(data_ptr, info_value->u, motorola_intel);
    2395           1 :                                         data_ptr += 4;
    2396           1 :                                         break;
    2397             :                                 case TAG_FMT_SSHORT:
    2398           0 :                                         php_ifd_set16u(data_ptr, info_value->i, motorola_intel);
    2399           0 :                                         data_ptr += 2;
    2400           0 :                                         break;
    2401             :                                 case TAG_FMT_SLONG:
    2402           0 :                                         php_ifd_set32u(data_ptr, info_value->i, motorola_intel);
    2403           0 :                                         data_ptr += 4;
    2404           0 :                                         break;
    2405             :                                 case TAG_FMT_URATIONAL:
    2406           0 :                                         php_ifd_set32u(data_ptr,   info_value->sr.num, motorola_intel);
    2407           0 :                                         php_ifd_set32u(data_ptr+4, info_value->sr.den, motorola_intel);
    2408           0 :                                         data_ptr += 8;
    2409           0 :                                         break;
    2410             :                                 case TAG_FMT_SRATIONAL:
    2411           0 :                                         php_ifd_set32u(data_ptr,   info_value->ur.num, motorola_intel);
    2412           0 :                                         php_ifd_set32u(data_ptr+4, info_value->ur.den, motorola_intel);
    2413           0 :                                         data_ptr += 8;
    2414           0 :                                         break;
    2415             :                                 case TAG_FMT_SINGLE:
    2416           0 :                                         memmove(data_ptr, &info_value->f, 4);
    2417           0 :                                         data_ptr += 4;
    2418           0 :                                         break;
    2419             :                                 case TAG_FMT_DOUBLE:
    2420          36 :                                         memmove(data_ptr, &info_value->d, 8);
    2421          36 :                                         data_ptr += 8;
    2422             :                                         break;
    2423             :                         }
    2424             :                 }
    2425             :         }
    2426           2 :         return value_ptr;
    2427             : }
    2428             : /* }}} */
    2429             : 
    2430             : /* {{{ exif_thumbnail_build
    2431             :  * Check and build thumbnail */
    2432           2 : static void exif_thumbnail_build(image_info_type *ImageInfo TSRMLS_DC) {
    2433             :         size_t            new_size, new_move, new_value;
    2434             :         char              *new_data;
    2435             :         void              *value_ptr;
    2436             :         int               i, byte_count;
    2437             :         image_info_list   *info_list;
    2438             :         image_info_data   *info_data;
    2439             : #ifdef EXIF_DEBUG
    2440             :         char              tagname[64];
    2441             : #endif
    2442             : 
    2443           2 :         if (!ImageInfo->read_thumbnail || !ImageInfo->Thumbnail.offset || !ImageInfo->Thumbnail.size) {
    2444           0 :                 return; /* ignore this call */
    2445             :         }
    2446             : #ifdef EXIF_DEBUG
    2447             :         exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Thumbnail: filetype = %d", ImageInfo->Thumbnail.filetype);
    2448             : #endif
    2449           2 :         switch(ImageInfo->Thumbnail.filetype) {
    2450             :                 default:
    2451             :                 case IMAGE_FILETYPE_JPEG:
    2452             :                         /* done */
    2453           1 :                         break;
    2454             :                 case IMAGE_FILETYPE_TIFF_II:
    2455             :                 case IMAGE_FILETYPE_TIFF_MM:
    2456           1 :                         info_list = &ImageInfo->info_list[SECTION_THUMBNAIL];
    2457           1 :                         new_size  = 8 + 2 + info_list->count * 12 + 4;
    2458             : #ifdef EXIF_DEBUG
    2459             :                         exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Thumbnail: size of signature + directory(%d): 0x%02X", info_list->count, new_size);
    2460             : #endif
    2461           1 :                         new_value= new_size; /* offset for ifd values outside ifd directory */
    2462           4 :                         for (i=0; i<info_list->count; i++) {
    2463           3 :                                 info_data  = &info_list->list[i];
    2464           3 :                                 byte_count = php_tiff_bytes_per_format[info_data->format] * info_data->length;
    2465           3 :                                 if (byte_count > 4) {
    2466           1 :                                         new_size += byte_count;
    2467             :                                 }
    2468             :                         }
    2469           1 :                         new_move = new_size;
    2470           1 :                         new_data = safe_erealloc(ImageInfo->Thumbnail.data, 1, ImageInfo->Thumbnail.size, new_size);
    2471           1 :                         ImageInfo->Thumbnail.data = new_data;
    2472           1 :                         memmove(ImageInfo->Thumbnail.data + new_move, ImageInfo->Thumbnail.data, ImageInfo->Thumbnail.size);
    2473           1 :                         ImageInfo->Thumbnail.size += new_size;
    2474             :                         /* fill in data */
    2475           1 :                         if (ImageInfo->motorola_intel) {
    2476           0 :                                 memmove(new_data, "MM\x00\x2a\x00\x00\x00\x08", 8);
    2477             :                         } else {
    2478           1 :                                 memmove(new_data, "II\x2a\x00\x08\x00\x00\x00", 8);
    2479             :                         }
    2480           1 :                         new_data += 8;
    2481           1 :                         php_ifd_set16u(new_data, info_list->count, ImageInfo->motorola_intel);
    2482           1 :                         new_data += 2;
    2483           4 :                         for (i=0; i<info_list->count; i++) {
    2484           3 :                                 info_data  = &info_list->list[i];
    2485           3 :                                 byte_count = php_tiff_bytes_per_format[info_data->format] * info_data->length;
    2486             : #ifdef EXIF_DEBUG
    2487             :                                 exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Thumbnail: process tag(x%04X=%s): %s%s (%d bytes)", info_data->tag, exif_get_tagname(info_data->tag, tagname, -12, tag_table_IFD TSRMLS_CC), (info_data->length>1)&&info_data->format!=TAG_FMT_UNDEFINED&&info_data->format!=TAG_FMT_STRING?"ARRAY OF ":"", exif_get_tagformat(info_data->format), byte_count);
    2488             : #endif
    2489           4 :                                 if (info_data->tag==TAG_STRIP_OFFSETS || info_data->tag==TAG_JPEG_INTERCHANGE_FORMAT) {
    2490           1 :                                         php_ifd_set16u(new_data + 0, info_data->tag,    ImageInfo->motorola_intel);
    2491           1 :                                         php_ifd_set16u(new_data + 2, TAG_FMT_ULONG,     ImageInfo->motorola_intel);
    2492           1 :                                         php_ifd_set32u(new_data + 4, 1,                 ImageInfo->motorola_intel);
    2493           1 :                                         php_ifd_set32u(new_data + 8, new_move,          ImageInfo->motorola_intel);
    2494             :                                 } else {
    2495           2 :                                         php_ifd_set16u(new_data + 0, info_data->tag,    ImageInfo->motorola_intel);
    2496           2 :                                         php_ifd_set16u(new_data + 2, info_data->format, ImageInfo->motorola_intel);
    2497           2 :                                         php_ifd_set32u(new_data + 4, info_data->length, ImageInfo->motorola_intel);
    2498           2 :                                         value_ptr  = exif_ifd_make_value(info_data, ImageInfo->motorola_intel TSRMLS_CC);
    2499           2 :                                         if (byte_count <= 4) {
    2500           1 :                                                 memmove(new_data+8, value_ptr, 4);
    2501             :                                         } else {
    2502           1 :                                                 php_ifd_set32u(new_data+8, new_value, ImageInfo->motorola_intel);
    2503             : #ifdef EXIF_DEBUG
    2504             :                                                 exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Thumbnail: writing with value offset: 0x%04X + 0x%02X", new_value, byte_count);
    2505             : #endif
    2506           1 :                                                 memmove(ImageInfo->Thumbnail.data+new_value, value_ptr, byte_count);
    2507           1 :                                                 new_value += byte_count;
    2508             :                                         }
    2509           2 :                                         efree(value_ptr);
    2510             :                                 }
    2511           3 :                                 new_data += 12;
    2512             :                         }
    2513           1 :                         memset(new_data, 0, 4); /* next ifd pointer */
    2514             : #ifdef EXIF_DEBUG
    2515             :                         exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Thumbnail: created");
    2516             : #endif
    2517             :                         break;
    2518             :         }
    2519             : }
    2520             : /* }}} */
    2521             : 
    2522             : /* {{{ exif_thumbnail_extract
    2523             :  * Grab the thumbnail, corrected */
    2524           2 : static void exif_thumbnail_extract(image_info_type *ImageInfo, char *offset, size_t length TSRMLS_DC) {
    2525           2 :         if (ImageInfo->Thumbnail.data) {
    2526           0 :                 exif_error_docref("exif_read_data#error_mult_thumb" EXIFERR_CC, ImageInfo, E_WARNING, "Multiple possible thumbnails");
    2527           0 :                 return; /* Should not happen */
    2528             :         }
    2529           2 :         if (!ImageInfo->read_thumbnail)      {
    2530           0 :                 return; /* ignore this call */
    2531             :         }
    2532             :         /* according to exif2.1, the thumbnail is not supposed to be greater than 64K */
    2533           6 :         if (ImageInfo->Thumbnail.size >= 65536
    2534           2 :          || ImageInfo->Thumbnail.size <= 0
    2535           4 :          || ImageInfo->Thumbnail.offset <= 0
    2536             :         ) {
    2537           0 :                 exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Illegal thumbnail size/offset");
    2538           0 :                 return;
    2539             :         }
    2540             :         /* Check to make sure we are not going to go past the ExifLength */
    2541           2 :         if ((ImageInfo->Thumbnail.offset + ImageInfo->Thumbnail.size) > length) {
    2542           0 :                 EXIF_ERRLOG_THUMBEOF(ImageInfo)
    2543           0 :                 return;
    2544             :         }
    2545           2 :         ImageInfo->Thumbnail.data = estrndup(offset + ImageInfo->Thumbnail.offset, ImageInfo->Thumbnail.size);
    2546           2 :         exif_thumbnail_build(ImageInfo TSRMLS_CC);
    2547             : }
    2548             : /* }}} */
    2549             : 
    2550             : /* {{{ exif_process_undefined
    2551             :  * Copy a string/buffer in Exif header to a character string and return length of allocated buffer if any. */
    2552           4 : static int exif_process_undefined(char **result, char *value, size_t byte_count TSRMLS_DC) {
    2553             :         /* we cannot use strlcpy - here the problem is that we have to copy NUL
    2554             :          * chars up to byte_count, we also have to add a single NUL character to
    2555             :          * force end of string.
    2556             :          * estrndup does not return length
    2557             :          */
    2558           4 :         if (byte_count) {
    2559           4 :                 (*result) = estrndup(value, byte_count); /* NULL @ byte_count!!! */
    2560           4 :                 return byte_count+1;
    2561             :         }
    2562           0 :         return 0;
    2563             : }
    2564             : /* }}} */
    2565             : 
    2566             : /* {{{ exif_process_string_raw
    2567             :  * Copy a string in Exif header to a character string returns length of allocated buffer if any. */
    2568           0 : static int exif_process_string_raw(char **result, char *value, size_t byte_count) {
    2569             :         /* we cannot use strlcpy - here the problem is that we have to copy NUL
    2570             :          * chars up to byte_count, we also have to add a single NUL character to
    2571             :          * force end of string.
    2572             :          */
    2573           0 :         if (byte_count) {
    2574           0 :                 (*result) = safe_emalloc(byte_count, 1, 1);
    2575           0 :                 memcpy(*result, value, byte_count);
    2576           0 :                 (*result)[byte_count] = '\0';
    2577           0 :                 return byte_count+1;
    2578             :         }
    2579           0 :         return 0;
    2580             : }
    2581             : /* }}} */
    2582             : 
    2583             : /* {{{ exif_process_string
    2584             :  * Copy a string in Exif header to a character string and return length of allocated buffer if any.
    2585             :  * In contrast to exif_process_string this function does always return a string buffer */
    2586           7 : static int exif_process_string(char **result, char *value, size_t byte_count TSRMLS_DC) {
    2587             :         /* we cannot use strlcpy - here the problem is that we cannot use strlen to
    2588             :          * determin length of string and we cannot use strlcpy with len=byte_count+1
    2589             :          * because then we might get into an EXCEPTION if we exceed an allocated
    2590             :          * memory page...so we use php_strnlen in conjunction with memcpy and add the NUL
    2591             :          * char.
    2592             :          * estrdup would sometimes allocate more memory and does not return length
    2593             :          */
    2594           7 :         if ((byte_count=php_strnlen(value, byte_count)) > 0) {
    2595           4 :                 return exif_process_undefined(result, value, byte_count TSRMLS_CC);
    2596             :         }
    2597           3 :         (*result) = estrndup("", 1); /* force empty string */
    2598           3 :         return byte_count+1;
    2599             : }
    2600             : /* }}} */
    2601             : 
    2602             : /* {{{ exif_process_user_comment
    2603             :  * Process UserComment in IFD. */
    2604           8 : static int exif_process_user_comment(image_info_type *ImageInfo, char **pszInfoPtr, char **pszEncoding, char *szValuePtr, int ByteCount TSRMLS_DC)
    2605             : {
    2606             :         int   a;
    2607             :         char  *decode;
    2608             :         size_t len;;
    2609             : 
    2610           8 :         *pszEncoding = NULL;
    2611             :         /* Copy the comment */
    2612           8 :         if (ByteCount>=8) {
    2613           8 :                 if (!memcmp(szValuePtr, "UNICODE\0", 8)) {
    2614           1 :                         *pszEncoding = estrdup((const char*)szValuePtr);
    2615           1 :                         szValuePtr = szValuePtr+8;
    2616           1 :                         ByteCount -= 8;
    2617             :                         /* First try to detect BOM: ZERO WIDTH NOBREAK SPACE (FEFF 16) 
    2618             :                          * since we have no encoding support for the BOM yet we skip that.
    2619             :                          */
    2620           1 :                         if (!memcmp(szValuePtr, "\xFE\xFF", 2)) {
    2621           0 :                                 decode = "UCS-2BE";
    2622           0 :                                 szValuePtr = szValuePtr+2;
    2623           0 :                                 ByteCount -= 2;
    2624           1 :                         } else if (!memcmp(szValuePtr, "\xFF\xFE", 2)) {
    2625           0 :                                 decode = "UCS-2LE";
    2626           0 :                                 szValuePtr = szValuePtr+2;
    2627           0 :                                 ByteCount -= 2;
    2628           1 :                         } else if (ImageInfo->motorola_intel) {
    2629           1 :                                 decode = ImageInfo->decode_unicode_be;
    2630             :                         } else {
    2631           0 :                                 decode = ImageInfo->decode_unicode_le;
    2632             :                         }
    2633             :                         /* XXX this will fail again if encoding_converter returns on error something different than SIZE_MAX   */
    2634           2 :                         if (zend_multibyte_encoding_converter(
    2635             :                                         (unsigned char**)pszInfoPtr, 
    2636             :                                         &len, 
    2637             :                                         (unsigned char*)szValuePtr,
    2638             :                                         ByteCount,
    2639           1 :                                         zend_multibyte_fetch_encoding(ImageInfo->encode_unicode TSRMLS_CC),
    2640             :                                         zend_multibyte_fetch_encoding(decode TSRMLS_CC)
    2641             :                                         TSRMLS_CC) == (size_t)-1) {
    2642           0 :                                 len = exif_process_string_raw(pszInfoPtr, szValuePtr, ByteCount);
    2643             :                         }
    2644           1 :                         return len;
    2645           7 :                 } else if (!memcmp(szValuePtr, "ASCII\0\0\0", 8)) {
    2646           4 :                         *pszEncoding = estrdup((const char*)szValuePtr);
    2647           4 :                         szValuePtr = szValuePtr+8;
    2648           4 :                         ByteCount -= 8;
    2649           3 :                 } else if (!memcmp(szValuePtr, "JIS\0\0\0\0\0", 8)) {
    2650             :                         /* JIS should be tanslated to MB or we leave it to the user - leave it to the user */
    2651           0 :                         *pszEncoding = estrdup((const char*)szValuePtr);
    2652           0 :                         szValuePtr = szValuePtr+8;
    2653           0 :                         ByteCount -= 8;
    2654             :                         /* XXX this will fail again if encoding_converter returns on error something different than SIZE_MAX   */
    2655           0 :                         if (zend_multibyte_encoding_converter(
    2656             :                                         (unsigned char**)pszInfoPtr, 
    2657             :                                         &len, 
    2658             :                                         (unsigned char*)szValuePtr,
    2659             :                                         ByteCount,
    2660           0 :                                         zend_multibyte_fetch_encoding(ImageInfo->encode_jis TSRMLS_CC),
    2661           0 :                                         zend_multibyte_fetch_encoding(ImageInfo->motorola_intel ? ImageInfo->decode_jis_be : ImageInfo->decode_jis_le TSRMLS_CC)
    2662             :                                         TSRMLS_CC) == (size_t)-1) {
    2663           0 :                                 len = exif_process_string_raw(pszInfoPtr, szValuePtr, ByteCount);
    2664             :                         }
    2665           0 :                         return len;
    2666           3 :                 } else if (!memcmp(szValuePtr, "\0\0\0\0\0\0\0\0", 8)) {
    2667             :                         /* 8 NULL means undefined and should be ASCII... */
    2668           3 :                         *pszEncoding = estrdup("UNDEFINED");
    2669           3 :                         szValuePtr = szValuePtr+8;
    2670           3 :                         ByteCount -= 8;
    2671             :                 }
    2672             :         }
    2673             : 
    2674             :         /* Olympus has this padded with trailing spaces.  Remove these first. */
    2675           7 :         if (ByteCount>0) {
    2676         239 :                 for (a=ByteCount-1;a && szValuePtr[a]==' ';a--) {
    2677         232 :                         (szValuePtr)[a] = '\0';
    2678             :                 }
    2679             :         }
    2680             : 
    2681             :         /* normal text without encoding */
    2682           7 :         exif_process_string(pszInfoPtr, szValuePtr, ByteCount TSRMLS_CC);
    2683           7 :         return strlen(*pszInfoPtr);
    2684             : }
    2685             : /* }}} */
    2686             : 
    2687             : /* {{{ exif_process_unicode
    2688             :  * Process unicode field in IFD. */
    2689           6 : static int exif_process_unicode(image_info_type *ImageInfo, xp_field_type *xp_field, int tag, char *szValuePtr, int ByteCount TSRMLS_DC)
    2690             : {
    2691           6 :         xp_field->tag = tag; 
    2692             :         
    2693             :         /* XXX this will fail again if encoding_converter returns on error something different than SIZE_MAX   */
    2694          18 :         if (zend_multibyte_encoding_converter(
    2695           6 :                         (unsigned char**)&xp_field->value, 
    2696             :                         &xp_field->size, 
    2697             :                         (unsigned char*)szValuePtr,
    2698             :                         ByteCount,
    2699           6 :                         zend_multibyte_fetch_encoding(ImageInfo->encode_unicode TSRMLS_CC),
    2700           6 :                         zend_multibyte_fetch_encoding(ImageInfo->motorola_intel ? ImageInfo->decode_unicode_be : ImageInfo->decode_unicode_le TSRMLS_CC)
    2701             :                         TSRMLS_CC) == (size_t)-1) {
    2702           0 :                 xp_field->size = exif_process_string_raw(&xp_field->value, szValuePtr, ByteCount);
    2703             :         }
    2704           6 :         return xp_field->size;
    2705             : }
    2706             : /* }}} */
    2707             : 
    2708             : /* {{{ exif_process_IFD_in_MAKERNOTE
    2709             :  * Process nested IFDs directories in Maker Note. */
    2710           3 : static int exif_process_IFD_in_MAKERNOTE(image_info_type *ImageInfo, char * value_ptr, int value_len, char *offset_base, size_t IFDlength, size_t displacement TSRMLS_DC)
    2711             : {
    2712           3 :         int de, i=0, section_index = SECTION_MAKERNOTE;
    2713             :         int NumDirEntries, old_motorola_intel, offset_diff;
    2714             :         const maker_note_type *maker_note;
    2715             :         char *dir_start;
    2716             : 
    2717          13 :         for (i=0; i<=sizeof(maker_note_array)/sizeof(maker_note_type); i++) {
    2718          13 :                 if (i==sizeof(maker_note_array)/sizeof(maker_note_type))
    2719           0 :                         return FALSE;
    2720          13 :                 maker_note = maker_note_array+i;
    2721             :                 
    2722             :                 /*exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "check (%s,%s)", maker_note->make?maker_note->make:"", maker_note->model?maker_note->model:"");*/
    2723          13 :                 if (maker_note->make && (!ImageInfo->make || strcmp(maker_note->make, ImageInfo->make)))
    2724          10 :                         continue;
    2725           3 :                 if (maker_note->model && (!ImageInfo->model || strcmp(maker_note->model, ImageInfo->model)))
    2726           0 :                         continue;
    2727           3 :                 if (maker_note->id_string && strncmp(maker_note->id_string, value_ptr, maker_note->id_string_len))
    2728           0 :                         continue;
    2729           3 :                 break;
    2730             :         }
    2731             : 
    2732           3 :         dir_start = value_ptr + maker_note->offset;
    2733             : 
    2734             : #ifdef EXIF_DEBUG
    2735             :         exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Process %s @x%04X + 0x%04X=%d: %s", exif_get_sectionname(section_index), (int)dir_start-(int)offset_base+maker_note->offset+displacement, value_len, value_len, exif_char_dump(value_ptr, value_len, (int)dir_start-(int)offset_base+maker_note->offset+displacement));
    2736             : #endif
    2737             : 
    2738           3 :         ImageInfo->sections_found |= FOUND_MAKERNOTE;
    2739             : 
    2740           3 :         old_motorola_intel = ImageInfo->motorola_intel;
    2741           3 :         switch (maker_note->byte_order) {
    2742             :                 case MN_ORDER_INTEL:
    2743           1 :                         ImageInfo->motorola_intel = 0;
    2744           1 :                         break;
    2745             :                 case MN_ORDER_MOTOROLA:
    2746           0 :                         ImageInfo->motorola_intel = 1;
    2747             :                         break;
    2748             :                 default:
    2749             :                 case MN_ORDER_NORMAL:
    2750             :                         break;
    2751             :         }
    2752             : 
    2753           3 :         NumDirEntries = php_ifd_get16u(dir_start, ImageInfo->motorola_intel);
    2754             : 
    2755           3 :         switch (maker_note->offset_mode) {
    2756             :                 case MN_OFFSET_MAKER:
    2757           0 :                         offset_base = value_ptr;
    2758           0 :                         break;
    2759             :                 case MN_OFFSET_GUESS:
    2760           1 :                         offset_diff = 2 + NumDirEntries*12 + 4 - php_ifd_get32u(dir_start+10, ImageInfo->motorola_intel);
    2761             : #ifdef EXIF_DEBUG
    2762             :                         exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Using automatic offset correction: 0x%04X", ((int)dir_start-(int)offset_base+maker_note->offset+displacement) + offset_diff);
    2763             : #endif
    2764           1 :                         offset_base = value_ptr + offset_diff;
    2765             :                         break;
    2766             :                 default:
    2767             :                 case MN_OFFSET_NORMAL:
    2768             :                         break;
    2769             :         }
    2770             : 
    2771           3 :         if ((2+NumDirEntries*12) > value_len) {
    2772           0 :                 exif_error_docref("exif_read_data#error_ifd" EXIFERR_CC, ImageInfo, E_WARNING, "Illegal IFD size: 2 + x%04X*12 = x%04X > x%04X", NumDirEntries, 2+NumDirEntries*12, value_len);
    2773           0 :                 return FALSE;
    2774             :         }
    2775             : 
    2776          34 :         for (de=0;de<NumDirEntries;de++) {
    2777          33 :                 if (!exif_process_IFD_TAG(ImageInfo, dir_start + 2 + 12 * de,
    2778             :                                                                   offset_base, IFDlength, displacement, section_index, 0, maker_note->tag_table TSRMLS_CC)) {
    2779           2 :                         return FALSE;
    2780             :                 }
    2781             :         }
    2782           1 :         ImageInfo->motorola_intel = old_motorola_intel;
    2783             : /*      NextDirOffset (must be NULL) = php_ifd_get32u(dir_start+2+12*de, ImageInfo->motorola_intel);*/
    2784             : #ifdef EXIF_DEBUG
    2785             :         exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Subsection %s done", exif_get_sectionname(SECTION_MAKERNOTE));
    2786             : #endif
    2787           1 :         return TRUE;
    2788             : }
    2789             : /* }}} */
    2790             : 
    2791             : /* {{{ exif_process_IFD_TAG
    2792             :  * Process one of the nested IFDs directories. */
    2793         655 : static int exif_process_IFD_TAG(image_info_type *ImageInfo, char *dir_entry, char *offset_base, size_t IFDlength, size_t displacement, int section_index, int ReadNextIFD, tag_table_type tag_table TSRMLS_DC)
    2794             : {
    2795             :         size_t length;
    2796             :         int tag, format, components;
    2797         655 :         char *value_ptr, tagname[64], cbuf[32], *outside=NULL;
    2798             :         size_t byte_count, offset_val, fpos, fgot;
    2799             :         int64_t byte_count_signed;
    2800             :         xp_field_type *tmp_xp;
    2801             : #ifdef EXIF_DEBUG
    2802             :         char *dump_data;
    2803             :         int dump_free;
    2804             : #endif /* EXIF_DEBUG */
    2805             : 
    2806             :         /* Protect against corrupt headers */
    2807         655 :         if (ImageInfo->ifd_nesting_level > MAX_IFD_NESTING_LEVEL) {
    2808           0 :                 exif_error_docref("exif_read_data#error_ifd" EXIFERR_CC, ImageInfo, E_WARNING, "corrupt EXIF header: maximum directory nesting level reached");
    2809           0 :                 return FALSE;
    2810             :         }
    2811         655 :         ImageInfo->ifd_nesting_level++;
    2812             : 
    2813         655 :         tag = php_ifd_get16u(dir_entry, ImageInfo->motorola_intel);
    2814         655 :         format = php_ifd_get16u(dir_entry+2, ImageInfo->motorola_intel);
    2815         655 :         components = php_ifd_get32u(dir_entry+4, ImageInfo->motorola_intel);
    2816             : 
    2817         655 :         if (!format || format > NUM_FORMATS) {
    2818             :                 /* (-1) catches illegal zero case as unsigned underflows to positive large. */
    2819           0 :                 exif_error_docref("exif_read_data#error_ifd" EXIFERR_CC, ImageInfo, E_WARNING, "Process tag(x%04X=%s): Illegal format code 0x%04X, suppose BYTE", tag, exif_get_tagname(tag, tagname, -12, tag_table TSRMLS_CC), format);
    2820           0 :                 format = TAG_FMT_BYTE;
    2821             :                 /*return TRUE;*/
    2822             :         }
    2823             : 
    2824         655 :         if (components < 0) {
    2825           0 :                 exif_error_docref("exif_read_data#error_ifd" EXIFERR_CC, ImageInfo, E_WARNING, "Process tag(x%04X=%s): Illegal components(%ld)", tag, exif_get_tagname(tag, tagname, -12, tag_table TSRMLS_CC), components);
    2826           0 :                 return FALSE;
    2827             :         }
    2828             : 
    2829         655 :         byte_count_signed = (int64_t)components * php_tiff_bytes_per_format[format];
    2830             : 
    2831         655 :         if (byte_count_signed < 0 || (byte_count_signed > INT32_MAX)) {
    2832           2 :                 exif_error_docref("exif_read_data#error_ifd" EXIFERR_CC, ImageInfo, E_WARNING, "Process tag(x%04X=%s): Illegal byte_count", tag, exif_get_tagname(tag, tagname, -12, tag_table TSRMLS_CC));
    2833           2 :                 return FALSE;
    2834             :         }
    2835             : 
    2836         653 :         byte_count = (size_t)byte_count_signed;
    2837             : 
    2838         653 :         if (byte_count > 4) {
    2839         332 :                 offset_val = php_ifd_get32u(dir_entry+8, ImageInfo->motorola_intel);
    2840             :                 /* If its bigger than 4 bytes, the dir entry contains an offset. */
    2841         332 :                 value_ptr = offset_base+offset_val;
    2842             :         /* 
    2843             :             dir_entry is ImageInfo->file.list[sn].data+2+i*12
    2844             :             offset_base is ImageInfo->file.list[sn].data-dir_offset 
    2845             :             dir_entry - offset_base is dir_offset+2+i*12
    2846             :         */
    2847         332 :                 if (byte_count > IFDlength || offset_val > IFDlength-byte_count || value_ptr < dir_entry || offset_val < (size_t)(dir_entry-offset_base)) {
    2848             :                         /* It is important to check for IMAGE_FILETYPE_TIFF
    2849             :                          * JPEG does not use absolute pointers instead its pointers are
    2850             :                          * relative to the start of the TIFF header in APP1 section. */
    2851         108 :                         if (byte_count > ImageInfo->FileSize || offset_val>ImageInfo->FileSize-byte_count || (ImageInfo->FileType!=IMAGE_FILETYPE_TIFF_II && ImageInfo->FileType!=IMAGE_FILETYPE_TIFF_MM && ImageInfo->FileType!=IMAGE_FILETYPE_JPEG)) {
    2852           3 :                                 if (value_ptr < dir_entry) {
    2853             :                                         /* we can read this if offset_val > 0 */
    2854             :                                         /* some files have their values in other parts of the file */
    2855           0 :                                         exif_error_docref("exif_read_data#error_ifd" EXIFERR_CC, ImageInfo, E_WARNING, "Process tag(x%04X=%s): Illegal pointer offset(x%04X < x%04X)", tag, exif_get_tagname(tag, tagname, -12, tag_table TSRMLS_CC), offset_val, dir_entry);
    2856             :                                 } else {
    2857             :                                         /* this is for sure not allowed */
    2858             :                                         /* exception are IFD pointers */
    2859           3 :                                         exif_error_docref("exif_read_data#error_ifd" EXIFERR_CC, ImageInfo, E_WARNING, "Process tag(x%04X=%s): Illegal pointer offset(x%04X + x%04X = x%04X > x%04X)", tag, exif_get_tagname(tag, tagname, -12, tag_table TSRMLS_CC), offset_val, byte_count, offset_val+byte_count, IFDlength);
    2860             :                                 }
    2861           3 :                                 return FALSE;
    2862             :                         }
    2863         105 :                         if (byte_count>sizeof(cbuf)) {
    2864             :                                 /* mark as outside range and get buffer */
    2865          17 :                                 value_ptr = safe_emalloc(byte_count, 1, 0);
    2866          17 :                                 outside = value_ptr;
    2867             :                         } else {
    2868             :                                 /* In most cases we only access a small range so
    2869             :                                  * it is faster to use a static buffer there
    2870             :                                  * BUT it offers also the possibility to have
    2871             :                                  * pointers read without the need to free them
    2872             :                                  * explicitley before returning. */
    2873          88 :                                 memset(&cbuf, 0, sizeof(cbuf));
    2874          88 :                                 value_ptr = cbuf;
    2875             :                         }
    2876             : 
    2877         105 :                         fpos = php_stream_tell(ImageInfo->infile);
    2878         105 :                         php_stream_seek(ImageInfo->infile, offset_val, SEEK_SET);
    2879         105 :                         fgot = php_stream_tell(ImageInfo->infile);
    2880         105 :                         if (fgot!=offset_val) {
    2881           0 :                                 EFREE_IF(outside);
    2882           0 :                                 exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Wrong file pointer: 0x%08X != 0x%08X", fgot, offset_val);
    2883           0 :                                 return FALSE;
    2884             :                         }
    2885         105 :                         fgot = php_stream_read(ImageInfo->infile, value_ptr, byte_count);
    2886         105 :                         php_stream_seek(ImageInfo->infile, fpos, SEEK_SET);
    2887         105 :                         if (fgot<byte_count) {
    2888           0 :                                 EFREE_IF(outside);
    2889           0 :                                 EXIF_ERRLOG_FILEEOF(ImageInfo)
    2890           0 :                                 return FALSE;
    2891             :                         }
    2892             :                 }
    2893             :         } else {
    2894             :                 /* 4 bytes or less and value is in the dir entry itself */
    2895         321 :                 value_ptr = dir_entry+8;
    2896         321 :                 offset_val= value_ptr-offset_base;
    2897             :         }
    2898             : 
    2899         650 :         ImageInfo->sections_found |= FOUND_ANY_TAG;
    2900             : #ifdef EXIF_DEBUG
    2901             :         dump_data = exif_dump_data(&dump_free, format, components, length, ImageInfo->motorola_intel, value_ptr TSRMLS_CC);
    2902             :         exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Process tag(x%04X=%s,@x%04X + x%04X(=%d)): %s%s %s", tag, exif_get_tagname(tag, tagname, -12, tag_table TSRMLS_CC), offset_val+displacement, byte_count, byte_count, (components>1)&&format!=TAG_FMT_UNDEFINED&&format!=TAG_FMT_STRING?"ARRAY OF ":"", exif_get_tagformat(format), dump_data);
    2903             :         if (dump_free) {
    2904             :                 efree(dump_data);
    2905             :         }
    2906             : #endif
    2907             : 
    2908         650 :         if (section_index==SECTION_THUMBNAIL) {
    2909          19 :                 if (!ImageInfo->Thumbnail.data) {
    2910          19 :                         switch(tag) {
    2911             :                                 case TAG_IMAGEWIDTH:
    2912             :                                 case TAG_COMP_IMAGE_WIDTH:
    2913           0 :                                         ImageInfo->Thumbnail.width = exif_convert_any_to_int(value_ptr, format, ImageInfo->motorola_intel TSRMLS_CC);
    2914           0 :                                         break;
    2915             :         
    2916             :                                 case TAG_IMAGEHEIGHT:
    2917             :                                 case TAG_COMP_IMAGE_HEIGHT:
    2918           0 :                                         ImageInfo->Thumbnail.height = exif_convert_any_to_int(value_ptr, format, ImageInfo->motorola_intel TSRMLS_CC);
    2919           0 :                                         break;
    2920             :         
    2921             :                                 case TAG_STRIP_OFFSETS:
    2922             :                                 case TAG_JPEG_INTERCHANGE_FORMAT:
    2923             :                                         /* accept both formats */
    2924           7 :                                         ImageInfo->Thumbnail.offset = exif_convert_any_to_int(value_ptr, format, ImageInfo->motorola_intel TSRMLS_CC);
    2925           7 :                                         break;
    2926             :         
    2927             :                                 case TAG_STRIP_BYTE_COUNTS:
    2928           1 :                                         if (ImageInfo->FileType == IMAGE_FILETYPE_TIFF_II || ImageInfo->FileType == IMAGE_FILETYPE_TIFF_MM) {
    2929           0 :                                                 ImageInfo->Thumbnail.filetype = ImageInfo->FileType;
    2930             :                                         } else {
    2931             :                                                 /* motorola is easier to read */
    2932           1 :                                                 ImageInfo->Thumbnail.filetype = IMAGE_FILETYPE_TIFF_MM;
    2933             :                                         }
    2934           1 :                                         ImageInfo->Thumbnail.size = exif_convert_any_to_int(value_ptr, format, ImageInfo->motorola_intel TSRMLS_CC);
    2935           1 :                                         break;
    2936             :         
    2937             :                                 case TAG_JPEG_INTERCHANGE_FORMAT_LEN:
    2938           6 :                                         if (ImageInfo->Thumbnail.filetype == IMAGE_FILETYPE_UNKNOWN) {
    2939           6 :                                                 ImageInfo->Thumbnail.filetype = IMAGE_FILETYPE_JPEG;
    2940           6 :                                                 ImageInfo->Thumbnail.size = exif_convert_any_to_int(value_ptr, format, ImageInfo->motorola_intel TSRMLS_CC);
    2941             :                                         }
    2942             :                                         break;
    2943             :                         }
    2944             :                 }
    2945             :         } else {
    2946         631 :                 if (section_index==SECTION_IFD0 || section_index==SECTION_EXIF)
    2947         512 :                 switch(tag) {
    2948             :                         case TAG_COPYRIGHT:
    2949             :                                 /* check for "<photographer> NUL <editor> NUL" */
    2950          22 :                                 if (byte_count>1 && (length=php_strnlen(value_ptr, byte_count)) > 0) {
    2951          22 :                                         if (length<byte_count-1) {
    2952             :                                                 /* When there are any characters after the first NUL */
    2953          22 :                                                 ImageInfo->CopyrightPhotographer  = estrdup(value_ptr);
    2954          22 :                                                 ImageInfo->CopyrightEditor        = estrdup(value_ptr+length+1);
    2955          22 :                                                 spprintf(&ImageInfo->Copyright, 0, "%s, %s", value_ptr, value_ptr+length+1);
    2956             :                                                 /* format = TAG_FMT_UNDEFINED; this musn't be ASCII         */
    2957             :                                                 /* but we are not supposed to change this                   */
    2958             :                                                 /* keep in mind that image_info does not store editor value */
    2959             :                                         } else {
    2960           0 :                                                 ImageInfo->Copyright = estrdup(value_ptr);
    2961             :                                         }
    2962             :                                 }
    2963          22 :                                 break;   
    2964             : 
    2965             :                         case TAG_USERCOMMENT:
    2966           8 :                                 ImageInfo->UserCommentLength = exif_process_user_comment(ImageInfo, &(ImageInfo->UserComment), &(ImageInfo->UserCommentEncoding), value_ptr, byte_count TSRMLS_CC);
    2967           8 :                                 break;
    2968             : 
    2969             :                         case TAG_XP_TITLE:
    2970             :                         case TAG_XP_COMMENTS:
    2971             :                         case TAG_XP_AUTHOR:
    2972             :                         case TAG_XP_KEYWORDS:
    2973             :                         case TAG_XP_SUBJECT:
    2974           6 :                                 tmp_xp = (xp_field_type*)safe_erealloc(ImageInfo->xp_fields.list, (ImageInfo->xp_fields.count+1), sizeof(xp_field_type), 0);
    2975           6 :                                 ImageInfo->sections_found |= FOUND_WINXP;
    2976           6 :                                 ImageInfo->xp_fields.list = tmp_xp;
    2977           6 :                                 ImageInfo->xp_fields.count++;
    2978           6 :                                 exif_process_unicode(ImageInfo, &(ImageInfo->xp_fields.list[ImageInfo->xp_fields.count-1]), tag, value_ptr, byte_count TSRMLS_CC);
    2979           6 :                                 break;
    2980             : 
    2981             :                         case TAG_FNUMBER:
    2982             :                                 /* Simplest way of expressing aperture, so I trust it the most.
    2983             :                                    (overwrite previously computed value if there is one) */
    2984          12 :                                 ImageInfo->ApertureFNumber = (float)exif_convert_any_format(value_ptr, format, ImageInfo->motorola_intel TSRMLS_CC);
    2985          12 :                                 break;
    2986             : 
    2987             :                         case TAG_APERTURE:
    2988             :                         case TAG_MAX_APERTURE:
    2989             :                                 /* More relevant info always comes earlier, so only use this field if we don't
    2990             :                                    have appropriate aperture information yet. */
    2991           5 :                                 if (ImageInfo->ApertureFNumber == 0) {
    2992           0 :                                         ImageInfo->ApertureFNumber
    2993           0 :                                                 = (float)exp(exif_convert_any_format(value_ptr, format, ImageInfo->motorola_intel TSRMLS_CC)*log(2)*0.5);
    2994             :                                 }
    2995           5 :                                 break;
    2996             : 
    2997             :                         case TAG_SHUTTERSPEED:
    2998             :                                 /* More complicated way of expressing exposure time, so only use
    2999             :                                    this value if we don't already have it from somewhere else.
    3000             :                                    SHUTTERSPEED comes after EXPOSURE TIME
    3001             :                                   */
    3002           2 :                                 if (ImageInfo->ExposureTime == 0) {
    3003           0 :                                         ImageInfo->ExposureTime
    3004           0 :                                                 = (float)(1/exp(exif_convert_any_format(value_ptr, format, ImageInfo->motorola_intel TSRMLS_CC)*log(2)));
    3005             :                                 }
    3006           2 :                                 break;
    3007             :                         case TAG_EXPOSURETIME:
    3008          12 :                                 ImageInfo->ExposureTime = -1;
    3009          12 :                                 break;
    3010             : 
    3011             :                         case TAG_COMP_IMAGE_WIDTH:
    3012           9 :                                 ImageInfo->ExifImageWidth = exif_convert_any_to_int(value_ptr, format, ImageInfo->motorola_intel TSRMLS_CC);
    3013           9 :                                 break;
    3014             : 
    3015             :                         case TAG_FOCALPLANE_X_RES:
    3016           1 :                                 ImageInfo->FocalplaneXRes = exif_convert_any_format(value_ptr, format, ImageInfo->motorola_intel TSRMLS_CC);
    3017           1 :                                 break;
    3018             : 
    3019             :                         case TAG_SUBJECT_DISTANCE:
    3020             :                                 /* Inidcates the distacne the autofocus camera is focused to.
    3021             :                                    Tends to be less accurate as distance increases. */
    3022           0 :                                 ImageInfo->Distance = (float)exif_convert_any_format(value_ptr, format, ImageInfo->motorola_intel TSRMLS_CC);
    3023           0 :                                 break;
    3024             : 
    3025             :                         case TAG_FOCALPLANE_RESOLUTION_UNIT:
    3026           1 :                                 switch((int)exif_convert_any_format(value_ptr, format, ImageInfo->motorola_intel TSRMLS_CC)) {
    3027           0 :                                         case 1: ImageInfo->FocalplaneUnits = 25.4; break; /* inch */
    3028             :                                         case 2:
    3029             :                                                 /* According to the information I was using, 2 measn meters.
    3030             :                                                    But looking at the Cannon powershot's files, inches is the only
    3031             :                                                    sensible value. */
    3032           1 :                                                 ImageInfo->FocalplaneUnits = 25.4;
    3033           1 :                                                 break;
    3034             : 
    3035           0 :                                         case 3: ImageInfo->FocalplaneUnits = 10;   break;  /* centimeter */
    3036           0 :                                         case 4: ImageInfo->FocalplaneUnits = 1;    break;  /* milimeter  */
    3037           0 :                                         case 5: ImageInfo->FocalplaneUnits = .001; break;  /* micrometer */
    3038             :                                 }
    3039           1 :                                 break;
    3040             : 
    3041             :                         case TAG_SUB_IFD:
    3042           0 :                                 if (format==TAG_FMT_IFD) {
    3043             :                                         /* If this is called we are either in a TIFFs thumbnail or a JPEG where we cannot handle it */
    3044             :                                         /* TIFF thumbnail: our data structure cannot store a thumbnail of a thumbnail */
    3045             :                                         /* JPEG do we have the data area and what to do with it */
    3046           0 :                                         exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Skip SUB IFD");
    3047             :                                 }
    3048           0 :                                 break;
    3049             : 
    3050             :                         case TAG_MAKE:
    3051          22 :                                 ImageInfo->make = estrdup(value_ptr);
    3052          22 :                                 break;
    3053             :                         case TAG_MODEL:
    3054          22 :                                 ImageInfo->model = estrdup(value_ptr);
    3055          22 :                                 break;
    3056             : 
    3057             :                         case TAG_MAKER_NOTE:
    3058           3 :                                 exif_process_IFD_in_MAKERNOTE(ImageInfo, value_ptr, byte_count, offset_base, IFDlength, displacement TSRMLS_CC);
    3059           3 :                                 break;
    3060             : 
    3061             :                         case TAG_EXIF_IFD_POINTER:
    3062             :                         case TAG_GPS_IFD_POINTER:
    3063             :                         case TAG_INTEROP_IFD_POINTER:
    3064          18 :                                 if (ReadNextIFD) {
    3065             :                                         char *Subdir_start;
    3066          18 :                                         int sub_section_index = 0;
    3067          18 :                                         switch(tag) {
    3068             :                                                 case TAG_EXIF_IFD_POINTER:
    3069             : #ifdef EXIF_DEBUG
    3070             :                                                         exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Found EXIF");
    3071             : #endif
    3072          11 :                                                         ImageInfo->sections_found |= FOUND_EXIF;
    3073          11 :                                                         sub_section_index = SECTION_EXIF;
    3074          11 :                                                         break;
    3075             :                                                 case TAG_GPS_IFD_POINTER:
    3076             : #ifdef EXIF_DEBUG
    3077             :                                                         exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Found GPS");
    3078             : #endif
    3079           4 :                                                         ImageInfo->sections_found |= FOUND_GPS;
    3080           4 :                                                         sub_section_index = SECTION_GPS;
    3081           4 :                                                         break;
    3082             :                                                 case TAG_INTEROP_IFD_POINTER:
    3083             : #ifdef EXIF_DEBUG
    3084             :                                                         exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Found INTEROPERABILITY");
    3085             : #endif
    3086           3 :                                                         ImageInfo->sections_found |= FOUND_INTEROP;
    3087           3 :                                                         sub_section_index = SECTION_INTEROP;
    3088             :                                                         break;
    3089             :                                         }
    3090          18 :                                         Subdir_start = offset_base + php_ifd_get32u(value_ptr, ImageInfo->motorola_intel);
    3091          18 :                                         if (Subdir_start < offset_base || Subdir_start > offset_base+IFDlength) {
    3092           0 :                                                 exif_error_docref("exif_read_data#error_ifd" EXIFERR_CC, ImageInfo, E_WARNING, "Illegal IFD Pointer");
    3093           0 :                                                 return FALSE;
    3094             :                                         }
    3095          18 :                                         if (!exif_process_IFD_in_JPEG(ImageInfo, Subdir_start, offset_base, IFDlength, displacement, sub_section_index TSRMLS_CC)) {
    3096           3 :                                                 return FALSE;
    3097             :                                         }
    3098             : #ifdef EXIF_DEBUG
    3099             :                                         exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Subsection %s done", exif_get_sectionname(sub_section_index));
    3100             : #endif
    3101             :                                 }
    3102             :                 }
    3103             :         }
    3104         647 :         exif_iif_add_tag(ImageInfo, section_index, exif_get_tagname(tag, tagname, sizeof(tagname), tag_table TSRMLS_CC), tag, format, components, value_ptr TSRMLS_CC);
    3105         647 :         EFREE_IF(outside);
    3106         647 :         return TRUE;
    3107             : }
    3108             : /* }}} */
    3109             : 
    3110             : /* {{{ exif_process_IFD_in_JPEG
    3111             :  * Process one of the nested IFDs directories. */
    3112          50 : static int exif_process_IFD_in_JPEG(image_info_type *ImageInfo, char *dir_start, char *offset_base, size_t IFDlength, size_t displacement, int section_index TSRMLS_DC)
    3113             : {
    3114             :         int de;
    3115             :         int NumDirEntries;
    3116             :         int NextDirOffset;
    3117             : 
    3118             : #ifdef EXIF_DEBUG
    3119             :         exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Process %s (x%04X(=%d))", exif_get_sectionname(section_index), IFDlength, IFDlength);
    3120             : #endif
    3121             : 
    3122          50 :         ImageInfo->sections_found |= FOUND_IFD0;
    3123             : 
    3124          50 :         NumDirEntries = php_ifd_get16u(dir_start, ImageInfo->motorola_intel);
    3125             : 
    3126          50 :         if ((dir_start+2+NumDirEntries*12) > (offset_base+IFDlength)) {
    3127           0 :                 exif_error_docref("exif_read_data#error_ifd" EXIFERR_CC, ImageInfo, E_WARNING, "Illegal IFD size: x%04X + 2 + x%04X*12 = x%04X > x%04X", (int)((size_t)dir_start+2-(size_t)offset_base), NumDirEntries, (int)((size_t)dir_start+2+NumDirEntries*12-(size_t)offset_base), IFDlength);
    3128           0 :                 return FALSE;
    3129             :         }
    3130             : 
    3131         420 :         for (de=0;de<NumDirEntries;de++) {
    3132         376 :                 if (!exif_process_IFD_TAG(ImageInfo, dir_start + 2 + 12 * de,
    3133             :                                                                   offset_base, IFDlength, displacement, section_index, 1, exif_get_tag_table(section_index) TSRMLS_CC)) {
    3134           6 :                         return FALSE;
    3135             :                 }
    3136             :         }
    3137             :         /*
    3138             :          * Ignore IFD2 if it purportedly exists
    3139             :          */
    3140          44 :         if (section_index == SECTION_THUMBNAIL) {
    3141           8 :                 return TRUE;
    3142             :         }
    3143             :         /*
    3144             :          * Hack to make it process IDF1 I hope
    3145             :          * There are 2 IDFs, the second one holds the keys (0x0201 and 0x0202) to the thumbnail
    3146             :          */
    3147          36 :         NextDirOffset = php_ifd_get32u(dir_start+2+12*de, ImageInfo->motorola_intel);
    3148          36 :         if (NextDirOffset) {
    3149             :                 /* the next line seems false but here IFDlength means length of all IFDs */
    3150           8 :                 if (offset_base + NextDirOffset < offset_base || offset_base + NextDirOffset > offset_base+IFDlength) {
    3151           0 :                         exif_error_docref("exif_read_data#error_ifd" EXIFERR_CC, ImageInfo, E_WARNING, "Illegal IFD offset");
    3152           0 :                         return FALSE;
    3153             :                 }
    3154             :                 /* That is the IFD for the first thumbnail */
    3155             : #ifdef EXIF_DEBUG
    3156             :                 exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Expect next IFD to be thumbnail");
    3157             : #endif
    3158           8 :                 if (exif_process_IFD_in_JPEG(ImageInfo, offset_base + NextDirOffset, offset_base, IFDlength, displacement, SECTION_THUMBNAIL TSRMLS_CC)) {
    3159             : #ifdef EXIF_DEBUG
    3160             :                         exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Thumbnail size: 0x%04X", ImageInfo->Thumbnail.size);
    3161             : #endif
    3162          29 :                         if (ImageInfo->Thumbnail.filetype != IMAGE_FILETYPE_UNKNOWN
    3163           8 :                         &&  ImageInfo->Thumbnail.size
    3164           7 :                         &&  ImageInfo->Thumbnail.offset
    3165           7 :                         &&  ImageInfo->read_thumbnail
    3166           7 :                         ) {
    3167           2 :                                 exif_thumbnail_extract(ImageInfo, offset_base, IFDlength TSRMLS_CC);
    3168             :                         }
    3169           8 :                         return TRUE;
    3170             :                 } else {
    3171           0 :                         return FALSE;
    3172             :                 }
    3173             :         }
    3174          28 :         return TRUE;
    3175             : }
    3176             : /* }}} */
    3177             : 
    3178             : /* {{{ exif_process_TIFF_in_JPEG
    3179             :    Process a TIFF header in a JPEG file
    3180             : */
    3181          25 : static void exif_process_TIFF_in_JPEG(image_info_type *ImageInfo, char *CharBuf, size_t length, size_t displacement TSRMLS_DC)
    3182             : {
    3183             :         unsigned exif_value_2a, offset_of_ifd;
    3184             : 
    3185             :         /* set the thumbnail stuff to nothing so we can test to see if they get set up */
    3186          25 :         if (memcmp(CharBuf, "II", 2) == 0) {
    3187          13 :                 ImageInfo->motorola_intel = 0;
    3188          12 :         } else if (memcmp(CharBuf, "MM", 2) == 0) {
    3189          12 :                 ImageInfo->motorola_intel = 1;
    3190             :         } else {
    3191           0 :                 exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Invalid TIFF alignment marker");
    3192           0 :                 return;
    3193             :         }
    3194             : 
    3195             :         /* Check the next two values for correctness. */
    3196          25 :         exif_value_2a = php_ifd_get16u(CharBuf+2, ImageInfo->motorola_intel);
    3197          25 :         offset_of_ifd = php_ifd_get32u(CharBuf+4, ImageInfo->motorola_intel);
    3198          25 :         if ( exif_value_2a != 0x2a || offset_of_ifd < 0x08) {
    3199           0 :                 exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Invalid TIFF start (1)");
    3200           0 :                 return;
    3201             :         }
    3202          25 :         if (offset_of_ifd > length) {
    3203           1 :                 exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Invalid IFD start");
    3204           1 :                 return;
    3205             :         }
    3206             : 
    3207          24 :         ImageInfo->sections_found |= FOUND_IFD0;
    3208             :         /* First directory starts at offset 8. Offsets starts at 0. */
    3209          24 :         exif_process_IFD_in_JPEG(ImageInfo, CharBuf+offset_of_ifd, CharBuf, length/*-14*/, displacement, SECTION_IFD0 TSRMLS_CC);
    3210             : 
    3211             : #ifdef EXIF_DEBUG
    3212             :         exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Process TIFF in JPEG done");
    3213             : #endif
    3214             : 
    3215             :         /* Compute the CCD width, in milimeters. */
    3216          24 :         if (ImageInfo->FocalplaneXRes != 0) {
    3217           1 :                 ImageInfo->CCDWidth = (float)(ImageInfo->ExifImageWidth * ImageInfo->FocalplaneUnits / ImageInfo->FocalplaneXRes);
    3218             :         }
    3219             : }
    3220             : /* }}} */
    3221             : 
    3222             : /* {{{ exif_process_APP1
    3223             :    Process an JPEG APP1 block marker
    3224             :    Describes all the drivel that most digital cameras include...
    3225             : */
    3226          25 : static void exif_process_APP1(image_info_type *ImageInfo, char *CharBuf, size_t length, size_t displacement TSRMLS_DC)
    3227             : {
    3228             :         /* Check the APP1 for Exif Identifier Code */
    3229             :         static const uchar ExifHeader[] = {0x45, 0x78, 0x69, 0x66, 0x00, 0x00};
    3230          25 :         if (length <= 8 || memcmp(CharBuf+2, ExifHeader, 6)) {
    3231           0 :                 exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Incorrect APP1 Exif Identifier Code");
    3232           0 :                 return;
    3233             :         }
    3234          25 :         exif_process_TIFF_in_JPEG(ImageInfo, CharBuf + 8, length - 8, displacement+8 TSRMLS_CC);
    3235             : #ifdef EXIF_DEBUG
    3236             :         exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Process APP1/EXIF done");
    3237             : #endif
    3238             : }
    3239             : /* }}} */
    3240             : 
    3241             : /* {{{ exif_process_APP12
    3242             :    Process an JPEG APP12 block marker used by OLYMPUS
    3243             : */
    3244           0 : static void exif_process_APP12(image_info_type *ImageInfo, char *buffer, size_t length TSRMLS_DC)
    3245             : {
    3246           0 :         size_t l1, l2=0;
    3247             : 
    3248           0 :         if ((l1 = php_strnlen(buffer+2, length-2)) > 0) {
    3249           0 :                 exif_iif_add_tag(ImageInfo, SECTION_APP12, "Company", TAG_NONE, TAG_FMT_STRING, l1, buffer+2 TSRMLS_CC);
    3250           0 :                 if (length > 2+l1+1) {
    3251           0 :                         l2 = php_strnlen(buffer+2+l1+1, length-2-l1-1);
    3252           0 :                         exif_iif_add_tag(ImageInfo, SECTION_APP12, "Info", TAG_NONE, TAG_FMT_STRING, l2, buffer+2+l1+1 TSRMLS_CC);
    3253             :                 }
    3254             :         }
    3255             : #ifdef EXIF_DEBUG
    3256             :         exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Process section APP12 with l1=%d, l2=%d done", l1, l2);
    3257             : #endif
    3258           0 : }
    3259             : /* }}} */
    3260             : 
    3261             : /* {{{ exif_scan_JPEG_header
    3262             :  * Parse the marker stream until SOS or EOI is seen; */
    3263          26 : static int exif_scan_JPEG_header(image_info_type *ImageInfo TSRMLS_DC)
    3264             : {
    3265             :         int section, sn;
    3266          26 :         int marker = 0, last_marker = M_PSEUDO, comment_correction=1;
    3267             :         unsigned int ll, lh;
    3268             :         uchar *Data;
    3269             :         size_t fpos, size, got, itemlen;
    3270             :         jpeg_sof_info  sof_info;
    3271             : 
    3272         237 :         for(section=0;;section++) {
    3273             : #ifdef EXIF_DEBUG
    3274             :                 fpos = php_stream_tell(ImageInfo->infile);
    3275             :                 exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Needing section %d @ 0x%08X", ImageInfo->file.count, fpos);
    3276             : #endif
    3277             : 
    3278             :                 /* get marker byte, swallowing possible padding                           */
    3279             :                 /* some software does not count the length bytes of COM section           */
    3280             :                 /* one company doing so is very much envolved in JPEG... so we accept too */
    3281         237 :                 if (last_marker==M_COM && comment_correction) {
    3282          15 :                         comment_correction = 2;
    3283             :                 }
    3284             :                 do {
    3285         467 :                         if ((marker = php_stream_getc(ImageInfo->infile)) == EOF) {
    3286           0 :                                 EXIF_ERRLOG_CORRUPT(ImageInfo)
    3287           0 :                                 return FALSE;
    3288             :                         }
    3289         467 :                         if (last_marker==M_COM && comment_correction>0) {
    3290          15 :                                 if (marker!=0xFF) {
    3291           0 :                                         marker = 0xff;
    3292           0 :                                         comment_correction--;
    3293             :                                 } else  {
    3294          15 :                                         last_marker = M_PSEUDO; /* stop skipping 0 for M_COM */
    3295             :                                 }
    3296             :                         }
    3297         467 :                 } while (marker == 0xff);
    3298         237 :                 if (last_marker==M_COM && !comment_correction) {
    3299           0 :                         exif_error_docref("exif_read_data#error_mcom" EXIFERR_CC, ImageInfo, E_NOTICE, "Image has corrupt COM section: some software set wrong length information");
    3300             :                 }
    3301         237 :                 if (last_marker==M_COM && comment_correction)
    3302           0 :                         return M_EOI; /* ah illegal: char after COM section not 0xFF */
    3303             : 
    3304         237 :                 fpos = php_stream_tell(ImageInfo->infile);
    3305             : 
    3306         237 :                 if (marker == 0xff) {
    3307             :                         /* 0xff is legal padding, but if we get that many, something's wrong. */
    3308           0 :                         exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "To many padding bytes");
    3309           0 :                         return FALSE;
    3310             :                 }
    3311             : 
    3312             :                 /* Read the length of the section. */
    3313         237 :                 if ((lh = php_stream_getc(ImageInfo->infile)) == EOF) {
    3314           0 :                         EXIF_ERRLOG_CORRUPT(ImageInfo)
    3315           0 :                         return FALSE;
    3316             :                 }
    3317         237 :                 if ((ll = php_stream_getc(ImageInfo->infile)) == EOF) {
    3318           0 :                         EXIF_ERRLOG_CORRUPT(ImageInfo)
    3319           0 :                         return FALSE;
    3320             :                 }
    3321             : 
    3322         237 :                 itemlen = (lh << 8) | ll;
    3323             : 
    3324         237 :                 if (itemlen < 2) {
    3325             : #ifdef EXIF_DEBUG
    3326             :                         exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "%s, Section length: 0x%02X%02X", EXIF_ERROR_CORRUPT, lh, ll);
    3327             : #else
    3328           1 :                         EXIF_ERRLOG_CORRUPT(ImageInfo)
    3329             : #endif
    3330           1 :                         return FALSE;
    3331             :                 }
    3332             : 
    3333         236 :                 sn = exif_file_sections_add(ImageInfo, marker, itemlen+1, NULL);
    3334         236 :                 Data = ImageInfo->file.list[sn].data;
    3335             : 
    3336             :                 /* Store first two pre-read bytes. */
    3337         236 :                 Data[0] = (uchar)lh;
    3338         236 :                 Data[1] = (uchar)ll;
    3339             : 
    3340         236 :                 got = php_stream_read(ImageInfo->infile, (char*)(Data+2), itemlen-2); /* Read the whole section. */
    3341         236 :                 if (got != itemlen-2) {
    3342           2 :                         exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Error reading from file: got=x%04X(=%d) != itemlen-2=x%04X(=%d)", got, got, itemlen-2, itemlen-2);
    3343           2 :                         return FALSE;
    3344             :                 }
    3345             : 
    3346             : #ifdef EXIF_DEBUG
    3347             :                 exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Process section(x%02X=%s) @ x%04X + x%04X(=%d)", marker, exif_get_markername(marker), fpos, itemlen, itemlen);
    3348             : #endif
    3349         234 :                 switch(marker) {
    3350             :                         case M_SOS:   /* stop before hitting compressed data  */
    3351             :                                 /* If reading entire image is requested, read the rest of the data. */
    3352          23 :                                 if (ImageInfo->read_all) {
    3353             :                                         /* Determine how much file is left. */
    3354           0 :                                         fpos = php_stream_tell(ImageInfo->infile);
    3355           0 :                                         size = ImageInfo->FileSize - fpos;
    3356           0 :                                         sn = exif_file_sections_add(ImageInfo, M_PSEUDO, size, NULL);
    3357           0 :                                         Data = ImageInfo->file.list[sn].data;
    3358           0 :                                         got = php_stream_read(ImageInfo->infile, (char*)Data, size);
    3359           0 :                                         if (got != size) {
    3360           0 :                                                 EXIF_ERRLOG_FILEEOF(ImageInfo)
    3361           0 :                                                 return FALSE;
    3362             :                                         }
    3363             :                                 }
    3364          23 :                                 return TRUE;
    3365             : 
    3366             :                         case M_EOI:   /* in case it's a tables-only JPEG stream */
    3367           0 :                                 exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "No image in jpeg!");
    3368           0 :                                 return (ImageInfo->sections_found&(~FOUND_COMPUTED)) ? TRUE : FALSE;
    3369             : 
    3370             :                         case M_COM: /* Comment section */
    3371          15 :                                 exif_process_COM(ImageInfo, (char *)Data, itemlen TSRMLS_CC);
    3372          15 :                                 break;
    3373             : 
    3374             :                         case M_EXIF:
    3375          27 :                                 if (!(ImageInfo->sections_found&FOUND_IFD0)) {
    3376             :                                         /*ImageInfo->sections_found |= FOUND_EXIF;*/
    3377             :                                         /* Seen files from some 'U-lead' software with Vivitar scanner
    3378             :                                            that uses marker 31 later in the file (no clue what for!) */
    3379          25 :                                         exif_process_APP1(ImageInfo, (char *)Data, itemlen, fpos TSRMLS_CC);
    3380             :                                 }
    3381          27 :                                 break;
    3382             : 
    3383             :                         case M_APP12:
    3384           0 :                                 exif_process_APP12(ImageInfo, (char *)Data, itemlen TSRMLS_CC);
    3385           0 :                                 break;
    3386             : 
    3387             : 
    3388             :                         case M_SOF0:
    3389             :                         case M_SOF1:
    3390             :                         case M_SOF2:
    3391             :                         case M_SOF3:
    3392             :                         case M_SOF5:
    3393             :                         case M_SOF6:
    3394             :                         case M_SOF7:
    3395             :                         case M_SOF9:
    3396             :                         case M_SOF10:
    3397             :                         case M_SOF11:
    3398             :                         case M_SOF13:
    3399             :                         case M_SOF14:
    3400             :                         case M_SOF15:
    3401          24 :                                 if ((itemlen - 2) < 6) {
    3402           0 :                                         return FALSE;
    3403             :                                 }
    3404             :                 
    3405          24 :                                 exif_process_SOFn(Data, marker, &sof_info);
    3406          24 :                                 ImageInfo->Width  = sof_info.width;
    3407          24 :                                 ImageInfo->Height = sof_info.height;
    3408          24 :                                 if (sof_info.num_components == 3) {
    3409          24 :                                         ImageInfo->IsColor = 1;
    3410             :                                 } else {
    3411           0 :                                         ImageInfo->IsColor = 0;
    3412             :                                 }
    3413             :                                 break;
    3414             :                         default:
    3415             :                                 /* skip any other marker silently. */
    3416             :                                 break;
    3417             :                 }
    3418             : 
    3419             :                 /* keep track of last marker */
    3420         211 :                 last_marker = marker;
    3421         211 :         }
    3422             : #ifdef EXIF_DEBUG
    3423             :         exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Done");
    3424             : #endif
    3425             :         return TRUE;
    3426             : }
    3427             : /* }}} */
    3428             : 
    3429             : /* {{{ exif_scan_thumbnail
    3430             :  * scan JPEG in thumbnail (memory) */
    3431           5 : static int exif_scan_thumbnail(image_info_type *ImageInfo TSRMLS_DC)
    3432             : {
    3433           5 :         uchar           c, *data = (uchar*)ImageInfo->Thumbnail.data;
    3434             :         int             n, marker;
    3435           5 :         size_t          length=2, pos=0;
    3436             :         jpeg_sof_info   sof_info;
    3437             : 
    3438           5 :         if (!data) {
    3439           5 :                 return FALSE; /* nothing to do here */
    3440             :         }
    3441           0 :         if (memcmp(data, "\xFF\xD8\xFF", 3)) {
    3442           0 :                 if (!ImageInfo->Thumbnail.width && !ImageInfo->Thumbnail.height) {
    3443           0 :                         exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Thumbnail is not a JPEG image");
    3444             :                 }
    3445           0 :                 return FALSE;
    3446             :         }
    3447             :         for (;;) {
    3448           0 :                 pos += length;
    3449           0 :                 if (pos>=ImageInfo->Thumbnail.size) 
    3450           0 :                         return FALSE;
    3451           0 :                 c = data[pos++];
    3452           0 :                 if (pos>=ImageInfo->Thumbnail.size) 
    3453           0 :                         return FALSE;
    3454           0 :                 if (c != 0xFF) {
    3455           0 :                         return FALSE;
    3456             :                 }
    3457           0 :                 n = 8;
    3458           0 :                 while ((c = data[pos++]) == 0xFF && n--) {
    3459           0 :                         if (pos+3>=ImageInfo->Thumbnail.size) 
    3460           0 :                                 return FALSE;
    3461             :                         /* +3 = pos++ of next check when reaching marker + 2 bytes for length */
    3462             :                 }
    3463           0 :                 if (c == 0xFF) 
    3464           0 :                         return FALSE;
    3465           0 :                 marker = c;
    3466           0 :                 length = php_jpg_get16(data+pos);
    3467           0 :                 if (pos+length>=ImageInfo->Thumbnail.size) {
    3468           0 :                         return FALSE;
    3469             :                 }
    3470             : #ifdef EXIF_DEBUG
    3471             :                 exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Thumbnail: process section(x%02X=%s) @ x%04X + x%04X", marker, exif_get_markername(marker), pos, length);
    3472             : #endif
    3473           0 :                 switch (marker) {
    3474             :                         case M_SOF0:
    3475             :                         case M_SOF1:
    3476             :                         case M_SOF2:
    3477             :                         case M_SOF3:
    3478             :                         case M_SOF5:
    3479             :                         case M_SOF6:
    3480             :                         case M_SOF7:
    3481             :                         case M_SOF9:
    3482             :                         case M_SOF10:
    3483             :                         case M_SOF11:
    3484             :                         case M_SOF13:
    3485             :                         case M_SOF14:
    3486             :                         case M_SOF15:
    3487             :                                 /* handle SOFn block */
    3488           0 :                                 exif_process_SOFn(data+pos, marker, &sof_info);
    3489           0 :                                 ImageInfo->Thumbnail.height   = sof_info.height;
    3490           0 :                                 ImageInfo->Thumbnail.width    = sof_info.width;
    3491             : #ifdef EXIF_DEBUG
    3492             :                                 exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Thumbnail: size: %d * %d", sof_info.width, sof_info.height);
    3493             : #endif
    3494           0 :                                 return TRUE;
    3495             : 
    3496             :                         case M_SOS:
    3497             :                         case M_EOI:
    3498           0 :                                 exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Could not compute size of thumbnail");
    3499           0 :                                 return FALSE;
    3500             :                                 break;
    3501             : 
    3502             :                         default:
    3503             :                                 /* just skip */
    3504             :                                 break;
    3505             :                 }
    3506           0 :         }
    3507             : 
    3508             :         exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Could not compute size of thumbnail");
    3509             :         return FALSE;
    3510             : }
    3511             : /* }}} */
    3512             : 
    3513             : /* {{{ exif_process_IFD_in_TIFF
    3514             :  * Parse the TIFF header; */
    3515          20 : static int exif_process_IFD_in_TIFF(image_info_type *ImageInfo, size_t dir_offset, int section_index TSRMLS_DC)
    3516             : {
    3517          20 :         int i, sn, num_entries, sub_section_index = 0;
    3518             :         unsigned char *dir_entry;
    3519             :         char tagname[64];
    3520          20 :         size_t ifd_size, dir_size, entry_offset, next_offset, entry_length, entry_value=0, fgot;
    3521             :         int entry_tag , entry_type;
    3522          20 :         tag_table_type tag_table = exif_get_tag_table(section_index);
    3523             : 
    3524          20 :         if (ImageInfo->ifd_nesting_level > MAX_IFD_NESTING_LEVEL) {
    3525           0 :                 return FALSE;
    3526             :         }
    3527             : 
    3528          20 :         if (ImageInfo->FileSize >= dir_offset+2) {
    3529          20 :                 sn = exif_file_sections_add(ImageInfo, M_PSEUDO, 2, NULL);
    3530             : #ifdef EXIF_DEBUG
    3531             :                 exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Read from TIFF: filesize(x%04X), IFD dir(x%04X + x%04X)", ImageInfo->FileSize, dir_offset, 2);
    3532             : #endif
    3533          20 :                 php_stream_seek(ImageInfo->infile, dir_offset, SEEK_SET); /* we do not know the order of sections */
    3534          20 :                 php_stream_read(ImageInfo->infile, (char*)ImageInfo->file.list[sn].data, 2);
    3535          20 :                 num_entries = php_ifd_get16u(ImageInfo->file.list[sn].data, ImageInfo->motorola_intel);
    3536          20 :                 dir_size = 2/*num dir entries*/ +12/*length of entry*/*num_entries +4/* offset to next ifd (points to thumbnail or NULL)*/;
    3537          20 :                 if (ImageInfo->FileSize >= dir_offset+dir_size) {
    3538             : #ifdef EXIF_DEBUG
    3539             :                         exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Read from TIFF: filesize(x%04X), IFD dir(x%04X + x%04X), IFD entries(%d)", ImageInfo->FileSize, dir_offset+2, dir_size-2, num_entries);
    3540             : #endif
    3541          20 :                         if (exif_file_sections_realloc(ImageInfo, sn, dir_size TSRMLS_CC)) {
    3542           0 :                                 return FALSE;
    3543             :                         }
    3544          20 :                         php_stream_read(ImageInfo->infile, (char*)(ImageInfo->file.list[sn].data+2), dir_size-2);
    3545             :                         /*exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Dump: %s", exif_char_dump(ImageInfo->file.list[sn].data, dir_size, 0));*/
    3546          20 :                         next_offset = php_ifd_get32u(ImageInfo->file.list[sn].data + dir_size - 4, ImageInfo->motorola_intel);
    3547             : #ifdef EXIF_DEBUG
    3548             :                         exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Read from TIFF done, next offset x%04X", next_offset);
    3549             : #endif
    3550             :                         /* now we have the directory we can look how long it should be */
    3551          20 :                         ifd_size = dir_size;
    3552         277 :                         for(i=0;i<num_entries;i++) {
    3553         257 :                                 dir_entry        = ImageInfo->file.list[sn].data+2+i*12;
    3554         257 :                                 entry_tag    = php_ifd_get16u(dir_entry+0, ImageInfo->motorola_intel);
    3555         257 :                                 entry_type   = php_ifd_get16u(dir_entry+2, ImageInfo->motorola_intel);
    3556         257 :                                 if (entry_type > NUM_FORMATS) {
    3557           0 :                                         exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Read from TIFF: tag(0x%04X,%12s): Illegal format code 0x%04X, switching to BYTE", entry_tag, exif_get_tagname(entry_tag, tagname, -12, tag_table TSRMLS_CC), entry_type);
    3558             :                                         /* Since this is repeated in exif_process_IFD_TAG make it a notice here */
    3559             :                                         /* and make it a warning in the exif_process_IFD_TAG which is called    */
    3560             :                                         /* elsewhere. */
    3561           0 :                                         entry_type = TAG_FMT_BYTE;
    3562             :                                         /*The next line would break the image on writeback: */
    3563             :                                         /* php_ifd_set16u(dir_entry+2, entry_type, ImageInfo->motorola_intel);*/
    3564             :                                 }
    3565         257 :                                 entry_length = php_ifd_get32u(dir_entry+4, ImageInfo->motorola_intel) * php_tiff_bytes_per_format[entry_type];
    3566         257 :                                 if (entry_length <= 4) {
    3567         152 :                                         switch(entry_type) {
    3568             :                                                 case TAG_FMT_USHORT:
    3569          99 :                                                         entry_value  = php_ifd_get16u(dir_entry+8, ImageInfo->motorola_intel);
    3570          99 :                                                         break;
    3571             :                                                 case TAG_FMT_SSHORT:
    3572           0 :                                                         entry_value  = php_ifd_get16s(dir_entry+8, ImageInfo->motorola_intel);
    3573           0 :                                                         break;
    3574             :                                                 case TAG_FMT_ULONG:
    3575          33 :                                                         entry_value  = php_ifd_get32u(dir_entry+8, ImageInfo->motorola_intel);
    3576          33 :                                                         break;
    3577             :                                                 case TAG_FMT_SLONG:
    3578           0 :                                                         entry_value  = php_ifd_get32s(dir_entry+8, ImageInfo->motorola_intel);
    3579             :                                                         break;
    3580             :                                         }
    3581         152 :                                         switch(entry_tag) {
    3582             :                                                 case TAG_IMAGEWIDTH:
    3583             :                                                 case TAG_COMP_IMAGE_WIDTH:
    3584          12 :                                                         ImageInfo->Width  = entry_value;
    3585          12 :                                                         break;
    3586             :                                                 case TAG_IMAGEHEIGHT:
    3587             :                                                 case TAG_COMP_IMAGE_HEIGHT:
    3588          12 :                                                         ImageInfo->Height = entry_value;
    3589          12 :                                                         break;
    3590             :                                                 case TAG_PHOTOMETRIC_INTERPRETATION:
    3591           9 :                                                         switch (entry_value) {
    3592             :                                                                 case PMI_BLACK_IS_ZERO:
    3593             :                                                                 case PMI_WHITE_IS_ZERO:
    3594             :                                                                 case PMI_TRANSPARENCY_MASK:
    3595           0 :                                                                         ImageInfo->IsColor = 0;
    3596           0 :                                                                         break;
    3597             :                                                                 case PMI_RGB:
    3598             :                                                                 case PMI_PALETTE_COLOR:
    3599             :                                                                 case PMI_SEPARATED:
    3600             :                                                                 case PMI_YCBCR:
    3601             :                                                                 case PMI_CIELAB:
    3602           9 :                                                                         ImageInfo->IsColor = 1;
    3603             :                                                                         break;
    3604             :                                                         }
    3605             :                                                         break;
    3606             :                                         }
    3607             :                                 } else {
    3608         105 :                                         entry_offset = php_ifd_get32u(dir_entry+8, ImageInfo->motorola_intel);
    3609             :                                         /* if entry needs expading ifd cache and entry is at end of current ifd cache. */
    3610             :                                         /* otherwise there may be huge holes between two entries */
    3611         315 :                                         if (entry_offset + entry_length > dir_offset + ifd_size
    3612         210 :                                           && entry_offset == dir_offset + ifd_size) {
    3613          60 :                                                 ifd_size = entry_offset + entry_length - dir_offset;
    3614             : #ifdef EXIF_DEBUG
    3615             :                                                 exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Resize struct: x%04X + x%04X - x%04X = x%04X", entry_offset, entry_length, dir_offset, ifd_size);
    3616             : #endif
    3617             :                                         }
    3618             :                                 }
    3619             :                         }
    3620          20 :                         if (ImageInfo->FileSize >= dir_offset + ImageInfo->file.list[sn].size) {
    3621          20 :                                 if (ifd_size > dir_size) {
    3622          18 :                                         if (dir_offset + ifd_size > ImageInfo->FileSize) {
    3623           0 :                                                 exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Error in TIFF: filesize(x%04X) less than size of IFD(x%04X + x%04X)", ImageInfo->FileSize, dir_offset, ifd_size);
    3624           0 :                                                 return FALSE;
    3625             :                                         }
    3626          18 :                                         if (exif_file_sections_realloc(ImageInfo, sn, ifd_size TSRMLS_CC)) {
    3627           0 :                                                 return FALSE;
    3628             :                                         }
    3629             :                                         /* read values not stored in directory itself */
    3630             : #ifdef EXIF_DEBUG
    3631             :                                         exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Read from TIFF: filesize(x%04X), IFD(x%04X + x%04X)", ImageInfo->FileSize, dir_offset, ifd_size);
    3632             : #endif
    3633          18 :                                         php_stream_read(ImageInfo->infile, (char*)(ImageInfo->file.list[sn].data+dir_size), ifd_size-dir_size);
    3634             : #ifdef EXIF_DEBUG
    3635             :                                         exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Read from TIFF, done");
    3636             : #endif
    3637             :                                 }
    3638             :                                 /* now process the tags */
    3639         277 :                                 for(i=0;i<num_entries;i++) {
    3640         257 :                                         dir_entry        = ImageInfo->file.list[sn].data+2+i*12;
    3641         257 :                                         entry_tag    = php_ifd_get16u(dir_entry+0, ImageInfo->motorola_intel);
    3642         257 :                                         entry_type   = php_ifd_get16u(dir_entry+2, ImageInfo->motorola_intel);
    3643             :                                         /*entry_length = php_ifd_get32u(dir_entry+4, ImageInfo->motorola_intel);*/
    3644         268 :                                         if (entry_tag == TAG_EXIF_IFD_POINTER ||
    3645             :                                                 entry_tag == TAG_INTEROP_IFD_POINTER ||
    3646             :                                                 entry_tag == TAG_GPS_IFD_POINTER ||
    3647             :                                                 entry_tag == TAG_SUB_IFD
    3648             :                                         ) {
    3649          11 :                                                 switch(entry_tag) {
    3650             :                                                         case TAG_EXIF_IFD_POINTER:
    3651           5 :                                                                 ImageInfo->sections_found |= FOUND_EXIF;
    3652           5 :                                                                 sub_section_index = SECTION_EXIF;
    3653           5 :                                                                 break;
    3654             :                                                         case TAG_GPS_IFD_POINTER:
    3655           4 :                                                                 ImageInfo->sections_found |= FOUND_GPS;
    3656           4 :                                                                 sub_section_index = SECTION_GPS;
    3657           4 :                                                                 break;
    3658             :                                                         case TAG_INTEROP_IFD_POINTER:
    3659           2 :                                                                 ImageInfo->sections_found |= FOUND_INTEROP;
    3660           2 :                                                                 sub_section_index = SECTION_INTEROP;
    3661           2 :                                                                 break;
    3662             :                                                         case TAG_SUB_IFD:
    3663           0 :                                                                 ImageInfo->sections_found |= FOUND_THUMBNAIL;
    3664           0 :                                                                 sub_section_index = SECTION_THUMBNAIL;
    3665             :                                                                 break;
    3666             :                                                 }
    3667          11 :                                                 entry_offset = php_ifd_get32u(dir_entry+8, ImageInfo->motorola_intel);
    3668             : #ifdef EXIF_DEBUG
    3669             :                                                 exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Next IFD: %s @x%04X", exif_get_sectionname(sub_section_index), entry_offset);
    3670             : #endif
    3671          11 :                                                 ImageInfo->ifd_nesting_level++;
    3672          11 :                                                 exif_process_IFD_in_TIFF(ImageInfo, entry_offset, sub_section_index TSRMLS_CC);
    3673          11 :                                                 if (section_index!=SECTION_THUMBNAIL && entry_tag==TAG_SUB_IFD) {
    3674           0 :                                                         if (ImageInfo->Thumbnail.filetype != IMAGE_FILETYPE_UNKNOWN
    3675           0 :                                                         &&  ImageInfo->Thumbnail.size
    3676           0 :                                                         &&  ImageInfo->Thumbnail.offset
    3677           0 :                                                         &&  ImageInfo->read_thumbnail
    3678           0 :                                                         ) {
    3679             : #ifdef EXIF_DEBUG
    3680             :                                                                 exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "%s THUMBNAIL @0x%04X + 0x%04X", ImageInfo->Thumbnail.data ? "Ignore" : "Read", ImageInfo->Thumbnail.offset, ImageInfo->Thumbnail.size);
    3681             : #endif
    3682           0 :                                                                 if (!ImageInfo->Thumbnail.data) {
    3683           0 :                                                                         ImageInfo->Thumbnail.data = safe_emalloc(ImageInfo->Thumbnail.size, 1, 0);
    3684           0 :                                                                         php_stream_seek(ImageInfo->infile, ImageInfo->Thumbnail.offset, SEEK_SET);
    3685           0 :                                                                         fgot = php_stream_read(ImageInfo->infile, ImageInfo->Thumbnail.data, ImageInfo->Thumbnail.size);
    3686           0 :                                                                         if (fgot < ImageInfo->Thumbnail.size) {
    3687           0 :                                                                                 EXIF_ERRLOG_THUMBEOF(ImageInfo)
    3688             :                                                                         }
    3689           0 :                                                                         exif_thumbnail_build(ImageInfo TSRMLS_CC);
    3690             :                                                                 }
    3691             :                                                         }
    3692             :                                                 }
    3693             : #ifdef EXIF_DEBUG
    3694             :                                                 exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Next IFD: %s done", exif_get_sectionname(sub_section_index));
    3695             : #endif
    3696             :                                         } else {
    3697         246 :                                                 if (!exif_process_IFD_TAG(ImageInfo, (char*)dir_entry,
    3698         246 :                                                                                                   (char*)(ImageInfo->file.list[sn].data-dir_offset),
    3699             :                                                                                                   ifd_size, 0, section_index, 0, tag_table TSRMLS_CC)) {
    3700           0 :                                                         return FALSE;
    3701             :                                                 }
    3702             :                                         }
    3703             :                                 }
    3704             :                                 /* If we had a thumbnail in a SUB_IFD we have ANOTHER image in NEXT IFD */
    3705          20 :                                 if (next_offset && section_index != SECTION_THUMBNAIL) {
    3706             :                                         /* this should be a thumbnail IFD */
    3707             :                                         /* the thumbnail itself is stored at Tag=StripOffsets */
    3708             : #ifdef EXIF_DEBUG
    3709             :                                         exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Read next IFD (THUMBNAIL) at x%04X", next_offset);
    3710             : #endif
    3711           0 :                                         ImageInfo->ifd_nesting_level++;
    3712           0 :                                         exif_process_IFD_in_TIFF(ImageInfo, next_offset, SECTION_THUMBNAIL TSRMLS_CC);
    3713             : #ifdef EXIF_DEBUG
    3714             :                                         exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "%s THUMBNAIL @0x%04X + 0x%04X", ImageInfo->Thumbnail.data ? "Ignore" : "Read", ImageInfo->Thumbnail.offset, ImageInfo->Thumbnail.size);
    3715             : #endif
    3716           0 :                                         if (!ImageInfo->Thumbnail.data && ImageInfo->Thumbnail.offset && ImageInfo->Thumbnail.size && ImageInfo->read_thumbnail) {
    3717           0 :                                                 ImageInfo->Thumbnail.data = safe_emalloc(ImageInfo->Thumbnail.size, 1, 0);
    3718           0 :                                                 php_stream_seek(ImageInfo->infile, ImageInfo->Thumbnail.offset, SEEK_SET);
    3719           0 :                                                 fgot = php_stream_read(ImageInfo->infile, ImageInfo->Thumbnail.data, ImageInfo->Thumbnail.size);
    3720           0 :                                                 if (fgot < ImageInfo->Thumbnail.size) {
    3721           0 :                                                         EXIF_ERRLOG_THUMBEOF(ImageInfo)
    3722             :                                                 }
    3723           0 :                                                 exif_thumbnail_build(ImageInfo TSRMLS_CC);
    3724             :                                         }
    3725             : #ifdef EXIF_DEBUG
    3726             :                                         exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Read next IFD (THUMBNAIL) done");
    3727             : #endif
    3728             :                                 }
    3729          20 :                                 return TRUE;
    3730             :                         } else {
    3731           0 :                                 exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Error in TIFF: filesize(x%04X) less than size of IFD(x%04X)", ImageInfo->FileSize, dir_offset+ImageInfo->file.list[sn].size);
    3732           0 :                                 return FALSE;
    3733             :                         }
    3734             :                 } else {
    3735           0 :                         exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Error in TIFF: filesize(x%04X) less than size of IFD dir(x%04X)", ImageInfo->FileSize, dir_offset+dir_size);
    3736           0 :                         return FALSE;
    3737             :                 }
    3738             :         } else {
    3739           0 :                 exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Error in TIFF: filesize(x%04X) less than start of IFD dir(x%04X)", ImageInfo->FileSize, dir_offset+2);
    3740           0 :                 return FALSE;
    3741             :         }
    3742             : }
    3743             : /* }}} */
    3744             : 
    3745             : /* {{{ exif_scan_FILE_header
    3746             :  * Parse the marker stream until SOS or EOI is seen; */
    3747          37 : static int exif_scan_FILE_header(image_info_type *ImageInfo TSRMLS_DC)
    3748             : {
    3749             :         unsigned char file_header[8];
    3750          37 :         int ret = FALSE;
    3751             : 
    3752          37 :         ImageInfo->FileType = IMAGE_FILETYPE_UNKNOWN;
    3753             : 
    3754          37 :         if (ImageInfo->FileSize >= 2) {
    3755          37 :                 php_stream_seek(ImageInfo->infile, 0, SEEK_SET);
    3756          37 :                 if (php_stream_read(ImageInfo->infile, (char*)file_header, 2) != 2) {
    3757           0 :                         return FALSE;
    3758             :                 }
    3759          63 :                 if ((file_header[0]==0xff) && (file_header[1]==M_SOI)) {
    3760          26 :                         ImageInfo->FileType = IMAGE_FILETYPE_JPEG;
    3761          26 :                         if (exif_scan_JPEG_header(ImageInfo TSRMLS_CC)) {
    3762          23 :                                 ret = TRUE;
    3763             :                         } else {
    3764           3 :                                 exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Invalid JPEG file");
    3765             :                         }
    3766          11 :                 } else if (ImageInfo->FileSize >= 8) {
    3767          11 :                         if (php_stream_read(ImageInfo->infile, (char*)(file_header+2), 6) != 6) {
    3768           0 :                                 return FALSE;
    3769             :                         }
    3770          11 :                         if (!memcmp(file_header, "II\x2A\x00", 4)) {
    3771           5 :                                 ImageInfo->FileType = IMAGE_FILETYPE_TIFF_II;
    3772           5 :                                 ImageInfo->motorola_intel = 0;
    3773             : #ifdef EXIF_DEBUG
    3774             :                                 exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "File has TIFF/II format");
    3775             : #endif
    3776           5 :                                 ImageInfo->sections_found |= FOUND_IFD0;
    3777           5 :                                 if (exif_process_IFD_in_TIFF(ImageInfo, 
    3778           5 :                                                                                          php_ifd_get32u(file_header + 4, ImageInfo->motorola_intel),
    3779             :                                                                                          SECTION_IFD0 TSRMLS_CC)) {
    3780           5 :                                         ret = TRUE;
    3781             :                                 } else {
    3782           0 :                                         exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Invalid TIFF file");
    3783             :                                 }
    3784           6 :                         } else if (!memcmp(file_header, "MM\x00\x2a", 4)) {
    3785           4 :                                 ImageInfo->FileType = IMAGE_FILETYPE_TIFF_MM;
    3786           4 :                                 ImageInfo->motorola_intel = 1;
    3787             : #ifdef EXIF_DEBUG
    3788             :                                 exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "File has TIFF/MM format");
    3789             : #endif
    3790           4 :                                 ImageInfo->sections_found |= FOUND_IFD0;
    3791           4 :                                 if (exif_process_IFD_in_TIFF(ImageInfo,
    3792           4 :                                                                                          php_ifd_get32u(file_header + 4, ImageInfo->motorola_intel),
    3793             :                                                                                          SECTION_IFD0 TSRMLS_CC)) {
    3794           4 :                                         ret = TRUE;
    3795             :                                 } else {
    3796           0 :                                         exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Invalid TIFF file");
    3797             :                                 }
    3798             :                         } else {
    3799           2 :                                 exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "File not supported");
    3800           2 :                                 return FALSE;
    3801             :                         }
    3802             :                 }
    3803             :         } else {
    3804           0 :                 exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "File too small (%d)", ImageInfo->FileSize);
    3805             :         }
    3806          35 :         return ret;
    3807             : }
    3808             : /* }}} */
    3809             : 
    3810             : /* {{{ exif_discard_imageinfo
    3811             :    Discard data scanned by exif_read_file.
    3812             : */
    3813          37 : static int exif_discard_imageinfo(image_info_type *ImageInfo)
    3814             : {
    3815             :         int i;
    3816             : 
    3817          37 :         EFREE_IF(ImageInfo->FileName);
    3818          37 :         EFREE_IF(ImageInfo->UserComment);
    3819          37 :         EFREE_IF(ImageInfo->UserCommentEncoding);
    3820          37 :         EFREE_IF(ImageInfo->Copyright);
    3821          37 :         EFREE_IF(ImageInfo->CopyrightPhotographer);
    3822          37 :         EFREE_IF(ImageInfo->CopyrightEditor);
    3823          37 :         EFREE_IF(ImageInfo->Thumbnail.data);
    3824          37 :         EFREE_IF(ImageInfo->encode_unicode);
    3825          37 :         EFREE_IF(ImageInfo->decode_unicode_be);
    3826          37 :         EFREE_IF(ImageInfo->decode_unicode_le);
    3827          37 :         EFREE_IF(ImageInfo->encode_jis);
    3828          37 :         EFREE_IF(ImageInfo->decode_jis_be);
    3829          37 :         EFREE_IF(ImageInfo->decode_jis_le);
    3830          37 :         EFREE_IF(ImageInfo->make);
    3831          37 :         EFREE_IF(ImageInfo->model);
    3832          43 :         for (i=0; i<ImageInfo->xp_fields.count; i++) {
    3833           6 :                 EFREE_IF(ImageInfo->xp_fields.list[i].value);
    3834             :         }
    3835          37 :         EFREE_IF(ImageInfo->xp_fields.list);
    3836         555 :         for (i=0; i<SECTION_COUNT; i++) {
    3837         518 :                 exif_iif_free(ImageInfo, i);
    3838             :         }
    3839          37 :         exif_file_sections_free(ImageInfo);
    3840          37 :         memset(ImageInfo, 0, sizeof(*ImageInfo));
    3841          37 :         return TRUE;
    3842             : }
    3843             : /* }}} */
    3844             : 
    3845             : /* {{{ exif_read_file
    3846             :  */
    3847          37 : static int exif_read_file(image_info_type *ImageInfo, char *FileName, int read_thumbnail, int read_all TSRMLS_DC)
    3848             : {
    3849             :         int ret;
    3850             :         zend_stat_t st;
    3851             :         zend_string *base;
    3852             :         
    3853             :         /* Start with an empty image information structure. */
    3854          37 :         memset(ImageInfo, 0, sizeof(*ImageInfo));
    3855             : 
    3856          37 :         ImageInfo->motorola_intel = -1; /* flag as unknown */
    3857             : 
    3858          37 :         ImageInfo->infile = php_stream_open_wrapper(FileName, "rb", STREAM_MUST_SEEK|IGNORE_PATH, NULL);
    3859          37 :         if (!ImageInfo->infile) {
    3860           0 :                 exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Unable to open file");
    3861           0 :                 return FALSE;
    3862             :         }
    3863             : 
    3864          37 :         if (php_stream_is(ImageInfo->infile, PHP_STREAM_IS_STDIO)) {
    3865          37 :                 if (VCWD_STAT(FileName, &st) >= 0) {
    3866          37 :                         if ((st.st_mode & S_IFMT) != S_IFREG) {
    3867           0 :                                 exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Not a file");
    3868           0 :                                 php_stream_close(ImageInfo->infile);
    3869           0 :                                 return FALSE;
    3870             :                         }
    3871             : 
    3872             :                         /* Store file date/time. */
    3873          37 :                         ImageInfo->FileDateTime = st.st_mtime;
    3874          37 :                         ImageInfo->FileSize = st.st_size;
    3875             :                         /*exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Opened stream is file: %d", ImageInfo->FileSize);*/
    3876             :                 }
    3877             :         } else {
    3878           0 :                 if (!ImageInfo->FileSize) {
    3879           0 :                         php_stream_seek(ImageInfo->infile, 0, SEEK_END);
    3880           0 :                         ImageInfo->FileSize = php_stream_tell(ImageInfo->infile);
    3881           0 :                         php_stream_seek(ImageInfo->infile, 0, SEEK_SET);
    3882             :                 }
    3883             :         }
    3884             : 
    3885          37 :         base = php_basename(FileName, strlen(FileName), NULL, 0 TSRMLS_CC);
    3886          37 :         ImageInfo->FileName          = estrndup(base->val, base->len);
    3887             :         zend_string_release(base);
    3888          37 :         ImageInfo->read_thumbnail = read_thumbnail;
    3889          37 :         ImageInfo->read_all = read_all;
    3890          37 :         ImageInfo->Thumbnail.filetype = IMAGE_FILETYPE_UNKNOWN;
    3891             : 
    3892          37 :         ImageInfo->encode_unicode    = estrdup(EXIF_G(encode_unicode));
    3893          37 :         ImageInfo->decode_unicode_be = estrdup(EXIF_G(decode_unicode_be));
    3894          37 :         ImageInfo->decode_unicode_le = estrdup(EXIF_G(decode_unicode_le));
    3895          37 :         ImageInfo->encode_jis        = estrdup(EXIF_G(encode_jis));
    3896          37 :         ImageInfo->decode_jis_be     = estrdup(EXIF_G(decode_jis_be));
    3897          37 :         ImageInfo->decode_jis_le     = estrdup(EXIF_G(decode_jis_le));
    3898             : 
    3899             : 
    3900          37 :         ImageInfo->ifd_nesting_level = 0;
    3901             : 
    3902             :         /* Scan the JPEG headers. */
    3903          37 :         ret = exif_scan_FILE_header(ImageInfo TSRMLS_CC);
    3904             : 
    3905          37 :         php_stream_close(ImageInfo->infile);
    3906          37 :         return ret;
    3907             : }
    3908             : /* }}} */
    3909             : 
    3910             : /* {{{ proto array exif_read_data(string filename [, sections_needed [, sub_arrays[, read_thumbnail]]])
    3911             :    Reads header data from the JPEG/TIFF image filename and optionally reads the internal thumbnails */
    3912          35 : PHP_FUNCTION(exif_read_data)
    3913             : {
    3914          35 :         char *p_name, *p_sections_needed = NULL;
    3915          35 :         size_t p_name_len, p_sections_needed_len = 0;
    3916          35 :         zend_bool sub_arrays=0, read_thumbnail=0, read_all=0;
    3917             : 
    3918          35 :         int i, ret, sections_needed=0;
    3919             :         image_info_type ImageInfo;
    3920             :         char tmp[64], *sections_str, *s;
    3921             : 
    3922          35 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "p|sbb", &p_name, &p_name_len, &p_sections_needed, &p_sections_needed_len, &sub_arrays, &read_thumbnail) == FAILURE) {
    3923           0 :                 return;
    3924             :         }
    3925             : 
    3926          35 :         memset(&ImageInfo, 0, sizeof(ImageInfo));
    3927             : 
    3928          35 :         if (p_sections_needed) {
    3929           5 :                 spprintf(&sections_str, 0, ",%s,", p_sections_needed);
    3930             :                 /* sections_str DOES start with , and SPACES are NOT allowed in names */
    3931           5 :                 s = sections_str;
    3932          36 :                 while (*++s) {
    3933          26 :                         if (*s == ' ') {
    3934           0 :                                 *s = ',';
    3935             :                         }
    3936             :                 }
    3937             : 
    3938          75 :                 for (i = 0; i < SECTION_COUNT; i++) {
    3939          70 :                         snprintf(tmp, sizeof(tmp), ",%s,", exif_get_sectionname(i));
    3940          70 :                         if (strstr(sections_str, tmp)) {
    3941           3 :                                 sections_needed |= 1<<i;
    3942             :                         }
    3943             :                 }
    3944           5 :                 EFREE_IF(sections_str);
    3945             :                 /* now see what we need */
    3946             : #ifdef EXIF_DEBUG
    3947             :                 sections_str = exif_get_sectionlist(sections_needed TSRMLS_CC);
    3948             :                 if (!sections_str) {
    3949             :                         RETURN_FALSE;
    3950             :                 }
    3951             :                 exif_error_docref(NULL EXIFERR_CC, &ImageInfo, E_NOTICE, "Sections needed: %s", sections_str[0] ? sections_str : "None");
    3952             :                 EFREE_IF(sections_str);
    3953             : #endif
    3954             :         }
    3955             : 
    3956          35 :         ret = exif_read_file(&ImageInfo, p_name, read_thumbnail, read_all TSRMLS_CC);
    3957          35 :         sections_str = exif_get_sectionlist(ImageInfo.sections_found TSRMLS_CC);
    3958             : 
    3959             : #ifdef EXIF_DEBUG
    3960             :         if (sections_str) 
    3961             :                 exif_error_docref(NULL EXIFERR_CC, &ImageInfo, E_NOTICE, "Sections found: %s", sections_str[0] ? sections_str : "None");
    3962             : #endif
    3963             : 
    3964          35 :         ImageInfo.sections_found |= FOUND_COMPUTED|FOUND_FILE;/* do not inform about in debug*/
    3965             : 
    3966          35 :         if (ret == FALSE || (sections_needed && !(sections_needed&ImageInfo.sections_found))) {
    3967             :                 /* array_init must be checked at last! otherwise the array must be freed if a later test fails. */
    3968           4 :                 exif_discard_imageinfo(&ImageInfo);
    3969           4 :                 EFREE_IF(sections_str);
    3970           4 :                 RETURN_FALSE;
    3971             :         }
    3972             : 
    3973          31 :         array_init(return_value);
    3974             : 
    3975             : #ifdef EXIF_DEBUG
    3976             :         exif_error_docref(NULL EXIFERR_CC, &ImageInfo, E_NOTICE, "Generate section FILE");
    3977             : #endif
    3978             : 
    3979             :         /* now we can add our information */
    3980          31 :         exif_iif_add_str(&ImageInfo, SECTION_FILE, "FileName",      ImageInfo.FileName TSRMLS_CC);
    3981          31 :         exif_iif_add_int(&ImageInfo, SECTION_FILE, "FileDateTime",  ImageInfo.FileDateTime TSRMLS_CC);
    3982          31 :         exif_iif_add_int(&ImageInfo, SECTION_FILE, "FileSize",      ImageInfo.FileSize TSRMLS_CC);
    3983          31 :         exif_iif_add_int(&ImageInfo, SECTION_FILE, "FileType",      ImageInfo.FileType TSRMLS_CC);
    3984          31 :         exif_iif_add_str(&ImageInfo, SECTION_FILE, "MimeType",      (char*)php_image_type_to_mime_type(ImageInfo.FileType) TSRMLS_CC);
    3985          31 :         exif_iif_add_str(&ImageInfo, SECTION_FILE, "SectionsFound", sections_str ? sections_str : "NONE" TSRMLS_CC);
    3986             : 
    3987             : #ifdef EXIF_DEBUG
    3988             :         exif_error_docref(NULL EXIFERR_CC, &ImageInfo, E_NOTICE, "Generate section COMPUTED");
    3989             : #endif
    3990             : 
    3991          31 :         if (ImageInfo.Width>0 &&  ImageInfo.Height>0) {
    3992          31 :                 exif_iif_add_fmt(&ImageInfo, SECTION_COMPUTED, "html"    TSRMLS_CC, "width=\"%d\" height=\"%d\"", ImageInfo.Width, ImageInfo.Height);
    3993          31 :                 exif_iif_add_int(&ImageInfo, SECTION_COMPUTED, "Height", ImageInfo.Height TSRMLS_CC);
    3994          31 :                 exif_iif_add_int(&ImageInfo, SECTION_COMPUTED, "Width",  ImageInfo.Width TSRMLS_CC);
    3995             :         }
    3996          31 :         exif_iif_add_int(&ImageInfo, SECTION_COMPUTED, "IsColor", ImageInfo.IsColor TSRMLS_CC);
    3997          31 :         if (ImageInfo.motorola_intel != -1) {
    3998          30 :                 exif_iif_add_int(&ImageInfo, SECTION_COMPUTED, "ByteOrderMotorola", ImageInfo.motorola_intel TSRMLS_CC);
    3999             :         }
    4000          31 :         if (ImageInfo.FocalLength) {
    4001           0 :                 exif_iif_add_fmt(&ImageInfo, SECTION_COMPUTED, "FocalLength" TSRMLS_CC, "%4.1Fmm", ImageInfo.FocalLength);
    4002           0 :                 if(ImageInfo.CCDWidth) {
    4003           0 :                         exif_iif_add_fmt(&ImageInfo, SECTION_COMPUTED, "35mmFocalLength" TSRMLS_CC, "%dmm", (int)(ImageInfo.FocalLength/ImageInfo.CCDWidth*35+0.5));
    4004             :                 }
    4005             :         }
    4006          31 :         if(ImageInfo.CCDWidth) {
    4007           1 :                 exif_iif_add_fmt(&ImageInfo, SECTION_COMPUTED, "CCDWidth" TSRMLS_CC, "%dmm", (int)ImageInfo.CCDWidth);
    4008             :         }
    4009          31 :         if(ImageInfo.ExposureTime>0) {
    4010           0 :                 if(ImageInfo.ExposureTime <= 0.5) {
    4011           0 :                         exif_iif_add_fmt(&ImageInfo, SECTION_COMPUTED, "ExposureTime" TSRMLS_CC, "%0.3F s (1/%d)", ImageInfo.ExposureTime, (int)(0.5 + 1/ImageInfo.ExposureTime));
    4012             :                 } else {
    4013           0 :                         exif_iif_add_fmt(&ImageInfo, SECTION_COMPUTED, "ExposureTime" TSRMLS_CC, "%0.3F s", ImageInfo.ExposureTime);
    4014             :                 }
    4015             :         }
    4016          31 :         if(ImageInfo.ApertureFNumber) {
    4017          11 :                 exif_iif_add_fmt(&ImageInfo, SECTION_COMPUTED, "ApertureFNumber" TSRMLS_CC, "f/%.1F", ImageInfo.ApertureFNumber);
    4018             :         }
    4019          31 :         if(ImageInfo.Distance) {
    4020           0 :                 if(ImageInfo.Distance<0) {
    4021           0 :                         exif_iif_add_str(&ImageInfo, SECTION_COMPUTED, "FocusDistance", "Infinite" TSRMLS_CC);
    4022             :                 } else {
    4023           0 :                         exif_iif_add_fmt(&ImageInfo, SECTION_COMPUTED, "FocusDistance" TSRMLS_CC, "%0.2Fm", ImageInfo.Distance);
    4024             :                 }
    4025             :         }
    4026          31 :         if (ImageInfo.UserComment) {
    4027           7 :                 exif_iif_add_buffer(&ImageInfo, SECTION_COMPUTED, "UserComment", ImageInfo.UserCommentLength, ImageInfo.UserComment TSRMLS_CC);
    4028           7 :                 if (ImageInfo.UserCommentEncoding && strlen(ImageInfo.UserCommentEncoding)) {
    4029           7 :                         exif_iif_add_str(&ImageInfo, SECTION_COMPUTED, "UserCommentEncoding", ImageInfo.UserCommentEncoding TSRMLS_CC);
    4030             :                 }
    4031             :         }
    4032             : 
    4033          31 :         exif_iif_add_str(&ImageInfo, SECTION_COMPUTED, "Copyright",              ImageInfo.Copyright TSRMLS_CC);
    4034          31 :         exif_iif_add_str(&ImageInfo, SECTION_COMPUTED, "Copyright.Photographer", ImageInfo.CopyrightPhotographer TSRMLS_CC);
    4035          31 :         exif_iif_add_str(&ImageInfo, SECTION_COMPUTED, "Copyright.Editor",       ImageInfo.CopyrightEditor TSRMLS_CC);
    4036             : 
    4037          37 :         for (i=0; i<ImageInfo.xp_fields.count; i++) {
    4038           6 :                 exif_iif_add_str(&ImageInfo, SECTION_WINXP, exif_get_tagname(ImageInfo.xp_fields.list[i].tag, NULL, 0, exif_get_tag_table(SECTION_WINXP) TSRMLS_CC), ImageInfo.xp_fields.list[i].value TSRMLS_CC);
    4039             :         }
    4040          31 :         if (ImageInfo.Thumbnail.size) {
    4041           5 :                 if (read_thumbnail) {
    4042             :                         /* not exif_iif_add_str : this is a buffer */
    4043           0 :                         exif_iif_add_tag(&ImageInfo, SECTION_THUMBNAIL, "THUMBNAIL", TAG_NONE, TAG_FMT_UNDEFINED, ImageInfo.Thumbnail.size, ImageInfo.Thumbnail.data TSRMLS_CC);
    4044             :                 }
    4045           5 :                 if (!ImageInfo.Thumbnail.width || !ImageInfo.Thumbnail.height) {
    4046             :                         /* try to evaluate if thumbnail data is present */
    4047           5 :                         exif_scan_thumbnail(&ImageInfo TSRMLS_CC);
    4048             :                 }
    4049           5 :                 exif_iif_add_int(&ImageInfo, SECTION_COMPUTED, "Thumbnail.FileType", ImageInfo.Thumbnail.filetype TSRMLS_CC);
    4050           5 :                 exif_iif_add_str(&ImageInfo, SECTION_COMPUTED, "Thumbnail.MimeType", (char*)php_image_type_to_mime_type(ImageInfo.Thumbnail.filetype) TSRMLS_CC);
    4051             :         }
    4052          31 :         if (ImageInfo.Thumbnail.width && ImageInfo.Thumbnail.height) {
    4053           0 :                 exif_iif_add_int(&ImageInfo, SECTION_COMPUTED, "Thumbnail.Height", ImageInfo.Thumbnail.height TSRMLS_CC);
    4054           0 :                 exif_iif_add_int(&ImageInfo, SECTION_COMPUTED, "Thumbnail.Width",  ImageInfo.Thumbnail.width TSRMLS_CC);
    4055             :         }
    4056          31 :         EFREE_IF(sections_str);
    4057             : 
    4058             : #ifdef EXIF_DEBUG
    4059             :         exif_error_docref(NULL EXIFERR_CC, &ImageInfo, E_NOTICE, "Adding image infos");
    4060             : #endif
    4061             : 
    4062          31 :         add_assoc_image_info(return_value, sub_arrays, &ImageInfo, SECTION_FILE       TSRMLS_CC);
    4063          31 :         add_assoc_image_info(return_value, 1,          &ImageInfo, SECTION_COMPUTED   TSRMLS_CC);
    4064          31 :         add_assoc_image_info(return_value, sub_arrays, &ImageInfo, SECTION_ANY_TAG    TSRMLS_CC);
    4065          31 :         add_assoc_image_info(return_value, sub_arrays, &ImageInfo, SECTION_IFD0       TSRMLS_CC);
    4066          31 :         add_assoc_image_info(return_value, 1,          &ImageInfo, SECTION_THUMBNAIL  TSRMLS_CC);
    4067          31 :         add_assoc_image_info(return_value, 1,          &ImageInfo, SECTION_COMMENT    TSRMLS_CC);
    4068          31 :         add_assoc_image_info(return_value, sub_arrays, &ImageInfo, SECTION_EXIF       TSRMLS_CC);
    4069          31 :         add_assoc_image_info(return_value, sub_arrays, &ImageInfo, SECTION_GPS        TSRMLS_CC);
    4070          31 :         add_assoc_image_info(return_value, sub_arrays, &ImageInfo, SECTION_INTEROP    TSRMLS_CC);
    4071          31 :         add_assoc_image_info(return_value, sub_arrays, &ImageInfo, SECTION_FPIX       TSRMLS_CC);
    4072          31 :         add_assoc_image_info(return_value, sub_arrays, &ImageInfo, SECTION_APP12      TSRMLS_CC);
    4073          31 :         add_assoc_image_info(return_value, sub_arrays, &ImageInfo, SECTION_WINXP      TSRMLS_CC);
    4074          31 :         add_assoc_image_info(return_value, sub_arrays, &ImageInfo, SECTION_MAKERNOTE  TSRMLS_CC);
    4075             : 
    4076             : #ifdef EXIF_DEBUG
    4077             :         exif_error_docref(NULL EXIFERR_CC, &ImageInfo, E_NOTICE, "Discarding info");
    4078             : #endif
    4079             : 
    4080          31 :         exif_discard_imageinfo(&ImageInfo);
    4081             : 
    4082             : #ifdef EXIF_DEBUG
    4083             :         php_error_docref1(NULL TSRMLS_CC, Z_STRVAL_PP(p_name), E_NOTICE, "done");
    4084             : #endif
    4085             : }
    4086             : /* }}} */
    4087             : 
    4088             : /* {{{ proto string exif_thumbnail(string filename [, &width, &height [, &imagetype]])
    4089             :    Reads the embedded thumbnail */
    4090           2 : PHP_FUNCTION(exif_thumbnail)
    4091             : {
    4092           2 :         zval *p_width = 0, *p_height = 0, *p_imagetype = 0;
    4093             :         char *p_name;
    4094             :         size_t p_name_len;
    4095           2 :         int ret, arg_c = ZEND_NUM_ARGS();
    4096             :         image_info_type ImageInfo;
    4097             : 
    4098           2 :         memset(&ImageInfo, 0, sizeof(ImageInfo));
    4099             : 
    4100           2 :         if (arg_c!=1 && arg_c!=3 && arg_c!=4) {
    4101           0 :                 WRONG_PARAM_COUNT;
    4102             :         }
    4103             : 
    4104           2 :         if (zend_parse_parameters(arg_c TSRMLS_CC, "p|z/z/z/", &p_name, &p_name_len, &p_width, &p_height, &p_imagetype) == FAILURE) {
    4105           0 :                 return;
    4106             :         }
    4107             : 
    4108           2 :         ret = exif_read_file(&ImageInfo, p_name, 1, 0 TSRMLS_CC);
    4109           2 :         if (ret==FALSE) {
    4110           1 :                 exif_discard_imageinfo(&ImageInfo);
    4111           1 :                 RETURN_FALSE;
    4112             :         }
    4113             : 
    4114             : #ifdef EXIF_DEBUG
    4115             :         exif_error_docref(NULL EXIFERR_CC, &ImageInfo, E_NOTICE, "Thumbnail data %d %d %d, %d x %d", ImageInfo.Thumbnail.data, ImageInfo.Thumbnail.size, ImageInfo.Thumbnail.filetype, ImageInfo.Thumbnail.width, ImageInfo.Thumbnail.height);
    4116             : #endif
    4117           1 :         if (!ImageInfo.Thumbnail.data || !ImageInfo.Thumbnail.size) {
    4118           0 :                 exif_discard_imageinfo(&ImageInfo);
    4119           0 :                 RETURN_FALSE;
    4120             :         }
    4121             : 
    4122             : #ifdef EXIF_DEBUG
    4123             :         exif_error_docref(NULL EXIFERR_CC, &ImageInfo, E_NOTICE, "Returning thumbnail(%d)", ImageInfo.Thumbnail.size);
    4124             : #endif
    4125             : 
    4126           2 :         ZVAL_STRINGL(return_value, ImageInfo.Thumbnail.data, ImageInfo.Thumbnail.size);
    4127           1 :         if (arg_c >= 3) {
    4128           0 :                 if (!ImageInfo.Thumbnail.width || !ImageInfo.Thumbnail.height) {
    4129           0 :                         exif_scan_thumbnail(&ImageInfo TSRMLS_CC);
    4130             :                 }
    4131           0 :                 zval_dtor(p_width);
    4132           0 :                 zval_dtor(p_height);
    4133           0 :                 ZVAL_LONG(p_width,  ImageInfo.Thumbnail.width);
    4134           0 :                 ZVAL_LONG(p_height, ImageInfo.Thumbnail.height);
    4135             :         }
    4136           1 :         if (arg_c >= 4)      {
    4137           0 :                 zval_dtor(p_imagetype);
    4138           0 :                 ZVAL_LONG(p_imagetype, ImageInfo.Thumbnail.filetype);
    4139             :         }
    4140             : 
    4141             : #ifdef EXIF_DEBUG
    4142             :         exif_error_docref(NULL EXIFERR_CC, &ImageInfo, E_NOTICE, "Discarding info");
    4143             : #endif
    4144             : 
    4145           1 :         exif_discard_imageinfo(&ImageInfo);
    4146             : 
    4147             : #ifdef EXIF_DEBUG
    4148             :         php_error_docref1(NULL TSRMLS_CC, p_name, E_NOTICE, "Done");
    4149             : #endif
    4150             : }
    4151             : /* }}} */
    4152             : 
    4153             : /* {{{ proto int exif_imagetype(string imagefile)
    4154             :    Get the type of an image */
    4155          30 : PHP_FUNCTION(exif_imagetype)
    4156             : {
    4157             :         char *imagefile;
    4158             :         size_t imagefile_len;
    4159             :         php_stream * stream;
    4160          30 :         int itype = 0;
    4161             : 
    4162          30 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &imagefile, &imagefile_len) == FAILURE) {
    4163           8 :                 return;
    4164             :         }
    4165             : 
    4166          22 :         stream = php_stream_open_wrapper(imagefile, "rb", IGNORE_PATH|REPORT_ERRORS, NULL);
    4167             : 
    4168          22 :         if (stream == NULL) {
    4169          21 :                 RETURN_FALSE;
    4170             :         }
    4171             : 
    4172           1 :         itype = php_getimagetype(stream, NULL TSRMLS_CC);
    4173             : 
    4174           1 :         php_stream_close(stream);
    4175             : 
    4176           1 :         if (itype == IMAGE_FILETYPE_UNKNOWN) {
    4177           0 :                 RETURN_FALSE;
    4178             :         } else {
    4179           1 :                 ZVAL_LONG(return_value, itype);
    4180             :         }
    4181             : }
    4182             : /* }}} */
    4183             : 
    4184             : #endif
    4185             : 
    4186             : /*
    4187             :  * Local variables:
    4188             :  * tab-width: 4
    4189             :  * c-basic-offset: 4
    4190             :  * End:
    4191             :  * vim600: sw=4 ts=4 tw=78 fdm=marker
    4192             :  * vim<600: sw=4 ts=4 tw=78
    4193             :  */

Generated by: LCOV version 1.10

Generated at Mon, 10 Nov 2014 22:46:38 +0000 (10 days ago)

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