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 1453 77.4 %
Date: 2019-02-04 Functions: 59 61 96.7 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Tue, 05 Feb 2019 02:37:58 +0000 (16 days ago)

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