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

LTP GCOV extension - code coverage report
Current view: directory - exif - exif.c
Test: PHP Code Coverage
Date: 2009-11-21 Instrumented lines: 1239
Code covered: 61.0 % Executed lines: 756
Legend: not executed executed

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

Generated by: LTP GCOV extension version 1.5

Generated at Sat, 21 Nov 2009 12:26:59 +0000 (3 days ago)

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