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: 955 1304 73.2 %
Date: 2016-08-28 Functions: 55 58 94.8 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Sun, 28 Aug 2016 17:10:00 +0000 (35 hours ago)

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