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: 930 1279 72.7 %
Date: 2015-09-02 Functions: 55 58 94.8 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Wed, 02 Sep 2015 17:19:09 +0000 (1 hour ago)

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