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

LCOV - code coverage report
Current view: top level - ext/exif - exif.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 930 1279 72.7 %
Date: 2015-03-28 Functions: 55 58 94.8 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Sun, 29 Mar 2015 03:45:49 +0000 (43 hours ago)

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