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: 852 1281 66.5 %
Date: 2014-12-15 Functions: 52 58 89.7 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Mon, 15 Dec 2014 17:02:41 +0000 (5 days ago)

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