Annotation of embedaddon/php/ext/exif/exif.c, revision 1.1.1.4

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>