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: 1003 1319 76.0 %
Date: 2016-09-18 Functions: 57 59 96.6 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Sun, 18 Sep 2016 08:20:05 +0000 (8 days ago)

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