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: 1125 1456 77.3 %
Date: 2018-08-17 Functions: 59 61 96.7 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Fri, 17 Aug 2018 22:18:39 +0000 (2 days ago)

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