File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / php / ext / exif / exif.c
Revision 1.1.1.5 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Sun Jun 15 20:03:43 2014 UTC (10 years, 10 months ago) by misho
Branches: php, MAIN
CVS tags: v5_4_29, HEAD
php 5.4.29

    1: /*
    2:    +----------------------------------------------------------------------+
    3:    | PHP Version 5                                                        |
    4:    +----------------------------------------------------------------------+
    5:    | Copyright (c) 1997-2014 The PHP Group                                |
    6:    +----------------------------------------------------------------------+
    7:    | This source file is subject to version 3.01 of the PHP license,      |
    8:    | that is bundled with this package in the file LICENSE, and is        |
    9:    | available through the world-wide-web at the following url:           |
   10:    | http://www.php.net/license/3_01.txt                                  |
   11:    | If you did not receive a copy of the PHP license and are unable to   |
   12:    | obtain it through the world-wide-web, please send a note to          |
   13:    | license@php.net so we can mail you a copy immediately.               |
   14:    +----------------------------------------------------------------------+
   15:    | Authors: Rasmus Lerdorf <rasmus@php.net>                             |
   16:    |          Marcus Boerger <helly@php.net>                              |
   17:    +----------------------------------------------------------------------+
   18:  */
   19: 
   20: /* $Id: exif.c,v 1.1.1.5 2014/06/15 20:03:43 misho Exp $ */
   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: 
  141: #define EXIF_VERSION "1.4 $Id: exif.c,v 1.1.1.5 2014/06/15 20:03:43 misho Exp $"
  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: {
  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);
  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: {
  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);
  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();
  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: 	}
  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")
  256: 	ZEND_MOD_OPTIONAL("mbstring")
  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);
 1700: 				info_value->s = estrndup(value, length);
 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 */
 1723: 				info_value->s = estrndup(value, length);
 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);
 1845: 		info_data->value.s = estrdup(value);
 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);
 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;
 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.
 2598:  * In contrast to exif_process_string this function does always return a string buffer */
 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: 			}
 2646: 			/* XXX this will fail again if encoding_converter returns on error something different than SIZE_MAX   */
 2647: 			if (zend_multibyte_encoding_converter(
 2648: 					(unsigned char**)pszInfoPtr, 
 2649: 					&len, 
 2650: 					(unsigned char*)szValuePtr,
 2651: 					ByteCount,
 2652: 					zend_multibyte_fetch_encoding(ImageInfo->encode_unicode TSRMLS_CC),
 2653: 					zend_multibyte_fetch_encoding(decode TSRMLS_CC)
 2654: 					TSRMLS_CC) == (size_t)-1) {
 2655: 				len = exif_process_string_raw(pszInfoPtr, szValuePtr, ByteCount);
 2656: 			}
 2657: 			return len;
 2658: 		} else if (!memcmp(szValuePtr, "ASCII\0\0\0", 8)) {
 2659: 			*pszEncoding = estrdup((const char*)szValuePtr);
 2660: 			szValuePtr = szValuePtr+8;
 2661: 			ByteCount -= 8;
 2662: 		} else if (!memcmp(szValuePtr, "JIS\0\0\0\0\0", 8)) {
 2663: 			/* JIS should be tanslated to MB or we leave it to the user - leave it to the user */
 2664: 			*pszEncoding = estrdup((const char*)szValuePtr);
 2665: 			szValuePtr = szValuePtr+8;
 2666: 			ByteCount -= 8;
 2667: 			/* XXX this will fail again if encoding_converter returns on error something different than SIZE_MAX   */
 2668: 			if (zend_multibyte_encoding_converter(
 2669: 					(unsigned char**)pszInfoPtr, 
 2670: 					&len, 
 2671: 					(unsigned char*)szValuePtr,
 2672: 					ByteCount,
 2673: 					zend_multibyte_fetch_encoding(ImageInfo->encode_jis TSRMLS_CC),
 2674: 					zend_multibyte_fetch_encoding(ImageInfo->motorola_intel ? ImageInfo->decode_jis_be : ImageInfo->decode_jis_le TSRMLS_CC)
 2675: 					TSRMLS_CC) == (size_t)-1) {
 2676: 				len = exif_process_string_raw(pszInfoPtr, szValuePtr, ByteCount);
 2677: 			}
 2678: 			return len;
 2679: 		} else if (!memcmp(szValuePtr, "\0\0\0\0\0\0\0\0", 8)) {
 2680: 			/* 8 NULL means undefined and should be ASCII... */
 2681: 			*pszEncoding = estrdup("UNDEFINED");
 2682: 			szValuePtr = szValuePtr+8;
 2683: 			ByteCount -= 8;
 2684: 		}
 2685: 	}
 2686: 
 2687: 	/* Olympus has this padded with trailing spaces.  Remove these first. */
 2688: 	if (ByteCount>0) {
 2689: 		for (a=ByteCount-1;a && szValuePtr[a]==' ';a--) {
 2690: 			(szValuePtr)[a] = '\0';
 2691: 		}
 2692: 	}
 2693: 
 2694: 	/* normal text without encoding */
 2695: 	exif_process_string(pszInfoPtr, szValuePtr, ByteCount TSRMLS_CC);
 2696: 	return strlen(*pszInfoPtr);
 2697: }
 2698: /* }}} */
 2699: 
 2700: /* {{{ exif_process_unicode
 2701:  * Process unicode field in IFD. */
 2702: static int exif_process_unicode(image_info_type *ImageInfo, xp_field_type *xp_field, int tag, char *szValuePtr, int ByteCount TSRMLS_DC)
 2703: {
 2704: 	xp_field->tag = tag;	
 2705: 	
 2706: 	/* XXX this will fail again if encoding_converter returns on error something different than SIZE_MAX   */
 2707: 	if (zend_multibyte_encoding_converter(
 2708: 			(unsigned char**)&xp_field->value, 
 2709: 			&xp_field->size, 
 2710: 			(unsigned char*)szValuePtr,
 2711: 			ByteCount,
 2712: 			zend_multibyte_fetch_encoding(ImageInfo->encode_unicode TSRMLS_CC),
 2713: 			zend_multibyte_fetch_encoding(ImageInfo->motorola_intel ? ImageInfo->decode_unicode_be : ImageInfo->decode_unicode_le TSRMLS_CC)
 2714: 			TSRMLS_CC) == (size_t)-1) {
 2715: 		xp_field->size = exif_process_string_raw(&xp_field->value, szValuePtr, ByteCount);
 2716: 	}
 2717: 	return xp_field->size;
 2718: }
 2719: /* }}} */
 2720: 
 2721: /* {{{ exif_process_IFD_in_MAKERNOTE
 2722:  * Process nested IFDs directories in Maker Note. */
 2723: 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)
 2724: {
 2725: 	int de, i=0, section_index = SECTION_MAKERNOTE;
 2726: 	int NumDirEntries, old_motorola_intel, offset_diff;
 2727: 	const maker_note_type *maker_note;
 2728: 	char *dir_start;
 2729: 
 2730: 	for (i=0; i<=sizeof(maker_note_array)/sizeof(maker_note_type); i++) {
 2731: 		if (i==sizeof(maker_note_array)/sizeof(maker_note_type))
 2732: 			return FALSE;
 2733: 		maker_note = maker_note_array+i;
 2734: 		
 2735: 		/*exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "check (%s,%s)", maker_note->make?maker_note->make:"", maker_note->model?maker_note->model:"");*/
 2736: 		if (maker_note->make && (!ImageInfo->make || strcmp(maker_note->make, ImageInfo->make)))
 2737: 			continue;
 2738: 		if (maker_note->model && (!ImageInfo->model || strcmp(maker_note->model, ImageInfo->model)))
 2739: 			continue;
 2740: 		if (maker_note->id_string && strncmp(maker_note->id_string, value_ptr, maker_note->id_string_len))
 2741: 			continue;
 2742: 		break;
 2743: 	}
 2744: 
 2745: 	dir_start = value_ptr + maker_note->offset;
 2746: 
 2747: #ifdef EXIF_DEBUG
 2748: 	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));
 2749: #endif
 2750: 
 2751: 	ImageInfo->sections_found |= FOUND_MAKERNOTE;
 2752: 
 2753: 	old_motorola_intel = ImageInfo->motorola_intel;
 2754: 	switch (maker_note->byte_order) {
 2755: 		case MN_ORDER_INTEL:
 2756: 			ImageInfo->motorola_intel = 0;
 2757: 			break;
 2758: 		case MN_ORDER_MOTOROLA:
 2759: 			ImageInfo->motorola_intel = 1;
 2760: 			break;
 2761: 		default:
 2762: 		case MN_ORDER_NORMAL:
 2763: 			break;
 2764: 	}
 2765: 
 2766: 	NumDirEntries = php_ifd_get16u(dir_start, ImageInfo->motorola_intel);
 2767: 
 2768: 	switch (maker_note->offset_mode) {
 2769: 		case MN_OFFSET_MAKER:
 2770: 			offset_base = value_ptr;
 2771: 			break;
 2772: 		case MN_OFFSET_GUESS:
 2773: 			offset_diff = 2 + NumDirEntries*12 + 4 - php_ifd_get32u(dir_start+10, ImageInfo->motorola_intel);
 2774: #ifdef EXIF_DEBUG
 2775: 			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);
 2776: #endif
 2777: 			offset_base = value_ptr + offset_diff;
 2778: 			break;
 2779: 		default:
 2780: 		case MN_OFFSET_NORMAL:
 2781: 			break;
 2782: 	}
 2783: 
 2784: 	if ((2+NumDirEntries*12) > value_len) {
 2785: 		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);
 2786: 		return FALSE;
 2787: 	}
 2788: 
 2789: 	for (de=0;de<NumDirEntries;de++) {
 2790: 		if (!exif_process_IFD_TAG(ImageInfo, dir_start + 2 + 12 * de,
 2791: 								  offset_base, IFDlength, displacement, section_index, 0, maker_note->tag_table TSRMLS_CC)) {
 2792: 			return FALSE;
 2793: 		}
 2794: 	}
 2795: 	ImageInfo->motorola_intel = old_motorola_intel;
 2796: /*	NextDirOffset (must be NULL) = php_ifd_get32u(dir_start+2+12*de, ImageInfo->motorola_intel);*/
 2797: #ifdef EXIF_DEBUG
 2798: 	exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Subsection %s done", exif_get_sectionname(SECTION_MAKERNOTE));
 2799: #endif
 2800: 	return TRUE;
 2801: }
 2802: /* }}} */
 2803: 
 2804: /* {{{ exif_process_IFD_TAG
 2805:  * Process one of the nested IFDs directories. */
 2806: 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)
 2807: {
 2808: 	size_t length;
 2809: 	int tag, format, components;
 2810: 	char *value_ptr, tagname[64], cbuf[32], *outside=NULL;
 2811: 	size_t byte_count, offset_val, fpos, fgot;
 2812: 	int64_t byte_count_signed;
 2813: 	xp_field_type *tmp_xp;
 2814: #ifdef EXIF_DEBUG
 2815: 	char *dump_data;
 2816: 	int dump_free;
 2817: #endif /* EXIF_DEBUG */
 2818: 
 2819: 	/* Protect against corrupt headers */
 2820: 	if (ImageInfo->ifd_nesting_level > MAX_IFD_NESTING_LEVEL) {
 2821: 		exif_error_docref("exif_read_data#error_ifd" EXIFERR_CC, ImageInfo, E_WARNING, "corrupt EXIF header: maximum directory nesting level reached");
 2822: 		return FALSE;
 2823: 	}
 2824: 	ImageInfo->ifd_nesting_level++;
 2825: 
 2826: 	tag = php_ifd_get16u(dir_entry, ImageInfo->motorola_intel);
 2827: 	format = php_ifd_get16u(dir_entry+2, ImageInfo->motorola_intel);
 2828: 	components = php_ifd_get32u(dir_entry+4, ImageInfo->motorola_intel);
 2829: 
 2830: 	if (!format || format > NUM_FORMATS) {
 2831: 		/* (-1) catches illegal zero case as unsigned underflows to positive large. */
 2832: 		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);
 2833: 		format = TAG_FMT_BYTE;
 2834: 		/*return TRUE;*/
 2835: 	}
 2836: 
 2837: 	if (components < 0) {
 2838: 		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);
 2839: 		return FALSE;
 2840: 	}
 2841: 
 2842: 	byte_count_signed = (int64_t)components * php_tiff_bytes_per_format[format];
 2843: 
 2844: 	if (byte_count_signed < 0 || (byte_count_signed > INT32_MAX)) {
 2845: 		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));
 2846: 		return FALSE;
 2847: 	}
 2848: 
 2849: 	byte_count = (size_t)byte_count_signed;
 2850: 
 2851: 	if (byte_count > 4) {
 2852: 		offset_val = php_ifd_get32u(dir_entry+8, ImageInfo->motorola_intel);
 2853: 		/* If its bigger than 4 bytes, the dir entry contains an offset. */
 2854: 		value_ptr = offset_base+offset_val;
 2855:         /* 
 2856:             dir_entry is ImageInfo->file.list[sn].data+2+i*12
 2857:             offset_base is ImageInfo->file.list[sn].data-dir_offset 
 2858:             dir_entry - offset_base is dir_offset+2+i*12
 2859:         */
 2860: 		if (byte_count > IFDlength || offset_val > IFDlength-byte_count || value_ptr < dir_entry || offset_val < (size_t)(dir_entry-offset_base)) {
 2861: 			/* It is important to check for IMAGE_FILETYPE_TIFF
 2862: 			 * JPEG does not use absolute pointers instead its pointers are
 2863: 			 * relative to the start of the TIFF header in APP1 section. */
 2864: 			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)) {
 2865: 				if (value_ptr < dir_entry) {
 2866: 					/* we can read this if offset_val > 0 */
 2867: 					/* some files have their values in other parts of the file */
 2868: 					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);
 2869: 				} else {
 2870: 					/* this is for sure not allowed */
 2871: 					/* exception are IFD pointers */
 2872: 					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);
 2873: 				}
 2874: 				return FALSE;
 2875: 			}
 2876: 			if (byte_count>sizeof(cbuf)) {
 2877: 				/* mark as outside range and get buffer */
 2878: 				value_ptr = safe_emalloc(byte_count, 1, 0);
 2879: 				outside = value_ptr;
 2880: 			} else {
 2881: 				/* In most cases we only access a small range so
 2882: 				 * it is faster to use a static buffer there
 2883: 				 * BUT it offers also the possibility to have
 2884: 				 * pointers read without the need to free them
 2885: 				 * explicitley before returning. */
 2886: 				memset(&cbuf, 0, sizeof(cbuf));
 2887: 				value_ptr = cbuf;
 2888: 			}
 2889: 
 2890: 			fpos = php_stream_tell(ImageInfo->infile);
 2891: 			php_stream_seek(ImageInfo->infile, offset_val, SEEK_SET);
 2892: 			fgot = php_stream_tell(ImageInfo->infile);
 2893: 			if (fgot!=offset_val) {
 2894: 				EFREE_IF(outside);
 2895: 				exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Wrong file pointer: 0x%08X != 0x%08X", fgot, offset_val);
 2896: 				return FALSE;
 2897: 			}
 2898: 			fgot = php_stream_read(ImageInfo->infile, value_ptr, byte_count);
 2899: 			php_stream_seek(ImageInfo->infile, fpos, SEEK_SET);
 2900: 			if (fgot<byte_count) {
 2901: 				EFREE_IF(outside);
 2902: 				EXIF_ERRLOG_FILEEOF(ImageInfo)
 2903: 				return FALSE;
 2904: 			}
 2905: 		}
 2906: 	} else {
 2907: 		/* 4 bytes or less and value is in the dir entry itself */
 2908: 		value_ptr = dir_entry+8;
 2909: 		offset_val= value_ptr-offset_base;
 2910: 	}
 2911: 
 2912: 	ImageInfo->sections_found |= FOUND_ANY_TAG;
 2913: #ifdef EXIF_DEBUG
 2914: 	dump_data = exif_dump_data(&dump_free, format, components, length, ImageInfo->motorola_intel, value_ptr TSRMLS_CC);
 2915: 	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);
 2916: 	if (dump_free) {
 2917: 		efree(dump_data);
 2918: 	}
 2919: #endif
 2920: 
 2921: 	if (section_index==SECTION_THUMBNAIL) {
 2922: 		if (!ImageInfo->Thumbnail.data) {
 2923: 			switch(tag) {
 2924: 				case TAG_IMAGEWIDTH:
 2925: 				case TAG_COMP_IMAGE_WIDTH:
 2926: 					ImageInfo->Thumbnail.width = exif_convert_any_to_int(value_ptr, format, ImageInfo->motorola_intel TSRMLS_CC);
 2927: 					break;
 2928: 	
 2929: 				case TAG_IMAGEHEIGHT:
 2930: 				case TAG_COMP_IMAGE_HEIGHT:
 2931: 					ImageInfo->Thumbnail.height = exif_convert_any_to_int(value_ptr, format, ImageInfo->motorola_intel TSRMLS_CC);
 2932: 					break;
 2933: 	
 2934: 				case TAG_STRIP_OFFSETS:
 2935: 				case TAG_JPEG_INTERCHANGE_FORMAT:
 2936: 					/* accept both formats */
 2937: 					ImageInfo->Thumbnail.offset = exif_convert_any_to_int(value_ptr, format, ImageInfo->motorola_intel TSRMLS_CC);
 2938: 					break;
 2939: 	
 2940: 				case TAG_STRIP_BYTE_COUNTS:
 2941: 					if (ImageInfo->FileType == IMAGE_FILETYPE_TIFF_II || ImageInfo->FileType == IMAGE_FILETYPE_TIFF_MM) {
 2942: 						ImageInfo->Thumbnail.filetype = ImageInfo->FileType;
 2943: 					} else {
 2944: 						/* motorola is easier to read */
 2945: 						ImageInfo->Thumbnail.filetype = IMAGE_FILETYPE_TIFF_MM;
 2946: 					}
 2947: 					ImageInfo->Thumbnail.size = exif_convert_any_to_int(value_ptr, format, ImageInfo->motorola_intel TSRMLS_CC);
 2948: 					break;
 2949: 	
 2950: 				case TAG_JPEG_INTERCHANGE_FORMAT_LEN:
 2951: 					if (ImageInfo->Thumbnail.filetype == IMAGE_FILETYPE_UNKNOWN) {
 2952: 						ImageInfo->Thumbnail.filetype = IMAGE_FILETYPE_JPEG;
 2953: 						ImageInfo->Thumbnail.size = exif_convert_any_to_int(value_ptr, format, ImageInfo->motorola_intel TSRMLS_CC);
 2954: 					}
 2955: 					break;
 2956: 			}
 2957: 		}
 2958: 	} else {
 2959: 		if (section_index==SECTION_IFD0 || section_index==SECTION_EXIF)
 2960: 		switch(tag) {
 2961: 			case TAG_COPYRIGHT:
 2962: 				/* check for "<photographer> NUL <editor> NUL" */
 2963: 				if (byte_count>1 && (length=php_strnlen(value_ptr, byte_count)) > 0) {
 2964: 					if (length<byte_count-1) {
 2965: 						/* When there are any characters after the first NUL */
 2966: 						ImageInfo->CopyrightPhotographer  = estrdup(value_ptr);
 2967: 						ImageInfo->CopyrightEditor        = estrdup(value_ptr+length+1);
 2968: 						spprintf(&ImageInfo->Copyright, 0, "%s, %s", value_ptr, value_ptr+length+1);
 2969: 						/* format = TAG_FMT_UNDEFINED; this musn't be ASCII         */
 2970: 						/* but we are not supposed to change this                   */
 2971: 						/* keep in mind that image_info does not store editor value */
 2972: 					} else {
 2973: 						ImageInfo->Copyright = estrdup(value_ptr);
 2974: 					}
 2975: 				}
 2976: 				break;   
 2977: 
 2978: 			case TAG_USERCOMMENT:
 2979: 				ImageInfo->UserCommentLength = exif_process_user_comment(ImageInfo, &(ImageInfo->UserComment), &(ImageInfo->UserCommentEncoding), value_ptr, byte_count TSRMLS_CC);
 2980: 				break;
 2981: 
 2982: 			case TAG_XP_TITLE:
 2983: 			case TAG_XP_COMMENTS:
 2984: 			case TAG_XP_AUTHOR:
 2985: 			case TAG_XP_KEYWORDS:
 2986: 			case TAG_XP_SUBJECT:
 2987: 				tmp_xp = (xp_field_type*)safe_erealloc(ImageInfo->xp_fields.list, (ImageInfo->xp_fields.count+1), sizeof(xp_field_type), 0);
 2988: 				ImageInfo->sections_found |= FOUND_WINXP;
 2989: 				ImageInfo->xp_fields.list = tmp_xp;
 2990: 				ImageInfo->xp_fields.count++;
 2991: 				exif_process_unicode(ImageInfo, &(ImageInfo->xp_fields.list[ImageInfo->xp_fields.count-1]), tag, value_ptr, byte_count TSRMLS_CC);
 2992: 				break;
 2993: 
 2994: 			case TAG_FNUMBER:
 2995: 				/* Simplest way of expressing aperture, so I trust it the most.
 2996: 				   (overwrite previously computed value if there is one) */
 2997: 				ImageInfo->ApertureFNumber = (float)exif_convert_any_format(value_ptr, format, ImageInfo->motorola_intel TSRMLS_CC);
 2998: 				break;
 2999: 
 3000: 			case TAG_APERTURE:
 3001: 			case TAG_MAX_APERTURE:
 3002: 				/* More relevant info always comes earlier, so only use this field if we don't
 3003: 				   have appropriate aperture information yet. */
 3004: 				if (ImageInfo->ApertureFNumber == 0) {
 3005: 					ImageInfo->ApertureFNumber
 3006: 						= (float)exp(exif_convert_any_format(value_ptr, format, ImageInfo->motorola_intel TSRMLS_CC)*log(2)*0.5);
 3007: 				}
 3008: 				break;
 3009: 
 3010: 			case TAG_SHUTTERSPEED:
 3011: 				/* More complicated way of expressing exposure time, so only use
 3012: 				   this value if we don't already have it from somewhere else.
 3013: 				   SHUTTERSPEED comes after EXPOSURE TIME
 3014: 				  */
 3015: 				if (ImageInfo->ExposureTime == 0) {
 3016: 					ImageInfo->ExposureTime
 3017: 						= (float)(1/exp(exif_convert_any_format(value_ptr, format, ImageInfo->motorola_intel TSRMLS_CC)*log(2)));
 3018: 				}
 3019: 				break;
 3020: 			case TAG_EXPOSURETIME:
 3021: 				ImageInfo->ExposureTime = -1;
 3022: 				break;
 3023: 
 3024: 			case TAG_COMP_IMAGE_WIDTH:
 3025: 				ImageInfo->ExifImageWidth = exif_convert_any_to_int(value_ptr, format, ImageInfo->motorola_intel TSRMLS_CC);
 3026: 				break;
 3027: 
 3028: 			case TAG_FOCALPLANE_X_RES:
 3029: 				ImageInfo->FocalplaneXRes = exif_convert_any_format(value_ptr, format, ImageInfo->motorola_intel TSRMLS_CC);
 3030: 				break;
 3031: 
 3032: 			case TAG_SUBJECT_DISTANCE:
 3033: 				/* Inidcates the distacne the autofocus camera is focused to.
 3034: 				   Tends to be less accurate as distance increases. */
 3035: 				ImageInfo->Distance = (float)exif_convert_any_format(value_ptr, format, ImageInfo->motorola_intel TSRMLS_CC);
 3036: 				break;
 3037: 
 3038: 			case TAG_FOCALPLANE_RESOLUTION_UNIT:
 3039: 				switch((int)exif_convert_any_format(value_ptr, format, ImageInfo->motorola_intel TSRMLS_CC)) {
 3040: 					case 1: ImageInfo->FocalplaneUnits = 25.4; break; /* inch */
 3041: 					case 2:
 3042: 						/* According to the information I was using, 2 measn meters.
 3043: 						   But looking at the Cannon powershot's files, inches is the only
 3044: 						   sensible value. */
 3045: 						ImageInfo->FocalplaneUnits = 25.4;
 3046: 						break;
 3047: 
 3048: 					case 3: ImageInfo->FocalplaneUnits = 10;   break;  /* centimeter */
 3049: 					case 4: ImageInfo->FocalplaneUnits = 1;    break;  /* milimeter  */
 3050: 					case 5: ImageInfo->FocalplaneUnits = .001; break;  /* micrometer */
 3051: 				}
 3052: 				break;
 3053: 
 3054: 			case TAG_SUB_IFD:
 3055: 				if (format==TAG_FMT_IFD) {
 3056: 					/* If this is called we are either in a TIFFs thumbnail or a JPEG where we cannot handle it */
 3057: 					/* TIFF thumbnail: our data structure cannot store a thumbnail of a thumbnail */
 3058: 					/* JPEG do we have the data area and what to do with it */
 3059: 					exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Skip SUB IFD");
 3060: 				}
 3061: 				break;
 3062: 
 3063: 			case TAG_MAKE:
 3064: 				ImageInfo->make = estrdup(value_ptr);
 3065: 				break;
 3066: 			case TAG_MODEL:
 3067: 				ImageInfo->model = estrdup(value_ptr);
 3068: 				break;
 3069: 
 3070: 			case TAG_MAKER_NOTE:
 3071: 				exif_process_IFD_in_MAKERNOTE(ImageInfo, value_ptr, byte_count, offset_base, IFDlength, displacement TSRMLS_CC);
 3072: 				break;
 3073: 
 3074: 			case TAG_EXIF_IFD_POINTER:
 3075: 			case TAG_GPS_IFD_POINTER:
 3076: 			case TAG_INTEROP_IFD_POINTER:
 3077: 				if (ReadNextIFD) {
 3078: 					char *Subdir_start;
 3079: 					int sub_section_index = 0;
 3080: 					switch(tag) {
 3081: 						case TAG_EXIF_IFD_POINTER:
 3082: #ifdef EXIF_DEBUG
 3083: 							exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Found EXIF");
 3084: #endif
 3085: 							ImageInfo->sections_found |= FOUND_EXIF;
 3086: 							sub_section_index = SECTION_EXIF;
 3087: 							break;
 3088: 						case TAG_GPS_IFD_POINTER:
 3089: #ifdef EXIF_DEBUG
 3090: 							exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Found GPS");
 3091: #endif
 3092: 							ImageInfo->sections_found |= FOUND_GPS;
 3093: 							sub_section_index = SECTION_GPS;
 3094: 							break;
 3095: 						case TAG_INTEROP_IFD_POINTER:
 3096: #ifdef EXIF_DEBUG
 3097: 							exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Found INTEROPERABILITY");
 3098: #endif
 3099: 							ImageInfo->sections_found |= FOUND_INTEROP;
 3100: 							sub_section_index = SECTION_INTEROP;
 3101: 							break;
 3102: 					}
 3103: 					Subdir_start = offset_base + php_ifd_get32u(value_ptr, ImageInfo->motorola_intel);
 3104: 					if (Subdir_start < offset_base || Subdir_start > offset_base+IFDlength) {
 3105: 						exif_error_docref("exif_read_data#error_ifd" EXIFERR_CC, ImageInfo, E_WARNING, "Illegal IFD Pointer");
 3106: 						return FALSE;
 3107: 					}
 3108: 					if (!exif_process_IFD_in_JPEG(ImageInfo, Subdir_start, offset_base, IFDlength, displacement, sub_section_index TSRMLS_CC)) {
 3109: 						return FALSE;
 3110: 					}
 3111: #ifdef EXIF_DEBUG
 3112: 					exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Subsection %s done", exif_get_sectionname(sub_section_index));
 3113: #endif
 3114: 				}
 3115: 		}
 3116: 	}
 3117: 	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);
 3118: 	EFREE_IF(outside);
 3119: 	return TRUE;
 3120: }
 3121: /* }}} */
 3122: 
 3123: /* {{{ exif_process_IFD_in_JPEG
 3124:  * Process one of the nested IFDs directories. */
 3125: 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)
 3126: {
 3127: 	int de;
 3128: 	int NumDirEntries;
 3129: 	int NextDirOffset;
 3130: 
 3131: #ifdef EXIF_DEBUG
 3132: 	exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Process %s (x%04X(=%d))", exif_get_sectionname(section_index), IFDlength, IFDlength);
 3133: #endif
 3134: 
 3135: 	ImageInfo->sections_found |= FOUND_IFD0;
 3136: 
 3137: 	NumDirEntries = php_ifd_get16u(dir_start, ImageInfo->motorola_intel);
 3138: 
 3139: 	if ((dir_start+2+NumDirEntries*12) > (offset_base+IFDlength)) {
 3140: 		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);
 3141: 		return FALSE;
 3142: 	}
 3143: 
 3144: 	for (de=0;de<NumDirEntries;de++) {
 3145: 		if (!exif_process_IFD_TAG(ImageInfo, dir_start + 2 + 12 * de,
 3146: 								  offset_base, IFDlength, displacement, section_index, 1, exif_get_tag_table(section_index) TSRMLS_CC)) {
 3147: 			return FALSE;
 3148: 		}
 3149: 	}
 3150: 	/*
 3151: 	 * Ignore IFD2 if it purportedly exists
 3152: 	 */
 3153: 	if (section_index == SECTION_THUMBNAIL) {
 3154: 		return TRUE;
 3155: 	}
 3156: 	/*
 3157: 	 * Hack to make it process IDF1 I hope
 3158: 	 * There are 2 IDFs, the second one holds the keys (0x0201 and 0x0202) to the thumbnail
 3159: 	 */
 3160: 	NextDirOffset = php_ifd_get32u(dir_start+2+12*de, ImageInfo->motorola_intel);
 3161: 	if (NextDirOffset) {
 3162: 		/* the next line seems false but here IFDlength means length of all IFDs */
 3163: 		if (offset_base + NextDirOffset < offset_base || offset_base + NextDirOffset > offset_base+IFDlength) {
 3164: 			exif_error_docref("exif_read_data#error_ifd" EXIFERR_CC, ImageInfo, E_WARNING, "Illegal IFD offset");
 3165: 			return FALSE;
 3166: 		}
 3167: 		/* That is the IFD for the first thumbnail */
 3168: #ifdef EXIF_DEBUG
 3169: 		exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Expect next IFD to be thumbnail");
 3170: #endif
 3171: 		if (exif_process_IFD_in_JPEG(ImageInfo, offset_base + NextDirOffset, offset_base, IFDlength, displacement, SECTION_THUMBNAIL TSRMLS_CC)) {
 3172: #ifdef EXIF_DEBUG
 3173: 			exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Thumbnail size: 0x%04X", ImageInfo->Thumbnail.size);
 3174: #endif
 3175: 			if (ImageInfo->Thumbnail.filetype != IMAGE_FILETYPE_UNKNOWN
 3176: 			&&  ImageInfo->Thumbnail.size
 3177: 			&&  ImageInfo->Thumbnail.offset
 3178: 			&&  ImageInfo->read_thumbnail
 3179: 			) {
 3180: 				exif_thumbnail_extract(ImageInfo, offset_base, IFDlength TSRMLS_CC);
 3181: 			}
 3182: 			return TRUE;
 3183: 		} else {
 3184: 			return FALSE;
 3185: 		}
 3186: 	}
 3187: 	return TRUE;
 3188: }
 3189: /* }}} */
 3190: 
 3191: /* {{{ exif_process_TIFF_in_JPEG
 3192:    Process a TIFF header in a JPEG file
 3193: */
 3194: static void exif_process_TIFF_in_JPEG(image_info_type *ImageInfo, char *CharBuf, size_t length, size_t displacement TSRMLS_DC)
 3195: {
 3196: 	unsigned exif_value_2a, offset_of_ifd;
 3197: 
 3198: 	/* set the thumbnail stuff to nothing so we can test to see if they get set up */
 3199: 	if (memcmp(CharBuf, "II", 2) == 0) {
 3200: 		ImageInfo->motorola_intel = 0;
 3201: 	} else if (memcmp(CharBuf, "MM", 2) == 0) {
 3202: 		ImageInfo->motorola_intel = 1;
 3203: 	} else {
 3204: 		exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Invalid TIFF alignment marker");
 3205: 		return;
 3206: 	}
 3207: 
 3208: 	/* Check the next two values for correctness. */
 3209: 	exif_value_2a = php_ifd_get16u(CharBuf+2, ImageInfo->motorola_intel);
 3210: 	offset_of_ifd = php_ifd_get32u(CharBuf+4, ImageInfo->motorola_intel);
 3211: 	if ( exif_value_2a != 0x2a || offset_of_ifd < 0x08) {
 3212: 		exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Invalid TIFF start (1)");
 3213: 		return;
 3214: 	}
 3215: 	if (offset_of_ifd > length) {
 3216: 		exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Invalid IFD start");
 3217: 		return;
 3218: 	}
 3219: 
 3220: 	ImageInfo->sections_found |= FOUND_IFD0;
 3221: 	/* First directory starts at offset 8. Offsets starts at 0. */
 3222: 	exif_process_IFD_in_JPEG(ImageInfo, CharBuf+offset_of_ifd, CharBuf, length/*-14*/, displacement, SECTION_IFD0 TSRMLS_CC);
 3223: 
 3224: #ifdef EXIF_DEBUG
 3225: 	exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Process TIFF in JPEG done");
 3226: #endif
 3227: 
 3228: 	/* Compute the CCD width, in milimeters. */
 3229: 	if (ImageInfo->FocalplaneXRes != 0) {
 3230: 		ImageInfo->CCDWidth = (float)(ImageInfo->ExifImageWidth * ImageInfo->FocalplaneUnits / ImageInfo->FocalplaneXRes);
 3231: 	}
 3232: }
 3233: /* }}} */
 3234: 
 3235: /* {{{ exif_process_APP1
 3236:    Process an JPEG APP1 block marker
 3237:    Describes all the drivel that most digital cameras include...
 3238: */
 3239: static void exif_process_APP1(image_info_type *ImageInfo, char *CharBuf, size_t length, size_t displacement TSRMLS_DC)
 3240: {
 3241: 	/* Check the APP1 for Exif Identifier Code */
 3242: 	static const uchar ExifHeader[] = {0x45, 0x78, 0x69, 0x66, 0x00, 0x00};
 3243: 	if (length <= 8 || memcmp(CharBuf+2, ExifHeader, 6)) {
 3244: 		exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Incorrect APP1 Exif Identifier Code");
 3245: 		return;
 3246: 	}
 3247: 	exif_process_TIFF_in_JPEG(ImageInfo, CharBuf + 8, length - 8, displacement+8 TSRMLS_CC);
 3248: #ifdef EXIF_DEBUG
 3249: 	exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Process APP1/EXIF done");
 3250: #endif
 3251: }
 3252: /* }}} */
 3253: 
 3254: /* {{{ exif_process_APP12
 3255:    Process an JPEG APP12 block marker used by OLYMPUS
 3256: */
 3257: static void exif_process_APP12(image_info_type *ImageInfo, char *buffer, size_t length TSRMLS_DC)
 3258: {
 3259: 	size_t l1, l2=0;
 3260: 
 3261: 	if ((l1 = php_strnlen(buffer+2, length-2)) > 0) {
 3262: 		exif_iif_add_tag(ImageInfo, SECTION_APP12, "Company", TAG_NONE, TAG_FMT_STRING, l1, buffer+2 TSRMLS_CC);
 3263: 		if (length > 2+l1+1) {
 3264: 			l2 = php_strnlen(buffer+2+l1+1, length-2-l1-1);
 3265: 			exif_iif_add_tag(ImageInfo, SECTION_APP12, "Info", TAG_NONE, TAG_FMT_STRING, l2, buffer+2+l1+1 TSRMLS_CC);
 3266: 		}
 3267: 	}
 3268: #ifdef EXIF_DEBUG
 3269: 	exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Process section APP12 with l1=%d, l2=%d done", l1, l2);
 3270: #endif
 3271: }
 3272: /* }}} */
 3273: 
 3274: /* {{{ exif_scan_JPEG_header
 3275:  * Parse the marker stream until SOS or EOI is seen; */
 3276: static int exif_scan_JPEG_header(image_info_type *ImageInfo TSRMLS_DC)
 3277: {
 3278: 	int section, sn;
 3279: 	int marker = 0, last_marker = M_PSEUDO, comment_correction=1;
 3280: 	unsigned int ll, lh;
 3281: 	uchar *Data;
 3282: 	size_t fpos, size, got, itemlen;
 3283: 	jpeg_sof_info  sof_info;
 3284: 
 3285: 	for(section=0;;section++) {
 3286: #ifdef EXIF_DEBUG
 3287: 		fpos = php_stream_tell(ImageInfo->infile);
 3288: 		exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Needing section %d @ 0x%08X", ImageInfo->file.count, fpos);
 3289: #endif
 3290: 
 3291: 		/* get marker byte, swallowing possible padding                           */
 3292: 		/* some software does not count the length bytes of COM section           */
 3293: 		/* one company doing so is very much envolved in JPEG... so we accept too */
 3294: 		if (last_marker==M_COM && comment_correction) {
 3295: 			comment_correction = 2;
 3296: 		}
 3297: 		do {
 3298: 			if ((marker = php_stream_getc(ImageInfo->infile)) == EOF) {
 3299: 				EXIF_ERRLOG_CORRUPT(ImageInfo)
 3300: 				return FALSE;
 3301: 			}
 3302: 			if (last_marker==M_COM && comment_correction>0) {
 3303: 				if (marker!=0xFF) {
 3304: 					marker = 0xff;
 3305: 					comment_correction--;
 3306: 				} else  {
 3307: 					last_marker = M_PSEUDO; /* stop skipping 0 for M_COM */
 3308: 				}
 3309: 			}
 3310: 		} while (marker == 0xff);
 3311: 		if (last_marker==M_COM && !comment_correction) {
 3312: 			exif_error_docref("exif_read_data#error_mcom" EXIFERR_CC, ImageInfo, E_NOTICE, "Image has corrupt COM section: some software set wrong length information");
 3313: 		}
 3314: 		if (last_marker==M_COM && comment_correction)
 3315: 			return M_EOI; /* ah illegal: char after COM section not 0xFF */
 3316: 
 3317: 		fpos = php_stream_tell(ImageInfo->infile);
 3318: 
 3319: 		if (marker == 0xff) {
 3320: 			/* 0xff is legal padding, but if we get that many, something's wrong. */
 3321: 			exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "To many padding bytes");
 3322: 			return FALSE;
 3323: 		}
 3324: 
 3325: 		/* Read the length of the section. */
 3326: 		if ((lh = php_stream_getc(ImageInfo->infile)) == EOF) {
 3327: 			EXIF_ERRLOG_CORRUPT(ImageInfo)
 3328: 			return FALSE;
 3329: 		}
 3330: 		if ((ll = php_stream_getc(ImageInfo->infile)) == EOF) {
 3331: 			EXIF_ERRLOG_CORRUPT(ImageInfo)
 3332: 			return FALSE;
 3333: 		}
 3334: 
 3335: 		itemlen = (lh << 8) | ll;
 3336: 
 3337: 		if (itemlen < 2) {
 3338: #ifdef EXIF_DEBUG
 3339: 			exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "%s, Section length: 0x%02X%02X", EXIF_ERROR_CORRUPT, lh, ll);
 3340: #else
 3341: 			EXIF_ERRLOG_CORRUPT(ImageInfo)
 3342: #endif
 3343: 			return FALSE;
 3344: 		}
 3345: 
 3346: 		sn = exif_file_sections_add(ImageInfo, marker, itemlen+1, NULL);
 3347: 		Data = ImageInfo->file.list[sn].data;
 3348: 
 3349: 		/* Store first two pre-read bytes. */
 3350: 		Data[0] = (uchar)lh;
 3351: 		Data[1] = (uchar)ll;
 3352: 
 3353: 		got = php_stream_read(ImageInfo->infile, (char*)(Data+2), itemlen-2); /* Read the whole section. */
 3354: 		if (got != itemlen-2) {
 3355: 			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);
 3356: 			return FALSE;
 3357: 		}
 3358: 
 3359: #ifdef EXIF_DEBUG
 3360: 		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);
 3361: #endif
 3362: 		switch(marker) {
 3363: 			case M_SOS:   /* stop before hitting compressed data  */
 3364: 				/* If reading entire image is requested, read the rest of the data. */
 3365: 				if (ImageInfo->read_all) {
 3366: 					/* Determine how much file is left. */
 3367: 					fpos = php_stream_tell(ImageInfo->infile);
 3368: 					size = ImageInfo->FileSize - fpos;
 3369: 					sn = exif_file_sections_add(ImageInfo, M_PSEUDO, size, NULL);
 3370: 					Data = ImageInfo->file.list[sn].data;
 3371: 					got = php_stream_read(ImageInfo->infile, (char*)Data, size);
 3372: 					if (got != size) {
 3373: 						EXIF_ERRLOG_FILEEOF(ImageInfo)
 3374: 						return FALSE;
 3375: 					}
 3376: 				}
 3377: 				return TRUE;
 3378: 
 3379: 			case M_EOI:   /* in case it's a tables-only JPEG stream */
 3380: 				exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "No image in jpeg!");
 3381: 				return (ImageInfo->sections_found&(~FOUND_COMPUTED)) ? TRUE : FALSE;
 3382: 
 3383: 			case M_COM: /* Comment section */
 3384: 				exif_process_COM(ImageInfo, (char *)Data, itemlen TSRMLS_CC);
 3385: 				break;
 3386: 
 3387: 			case M_EXIF:
 3388: 				if (!(ImageInfo->sections_found&FOUND_IFD0)) {
 3389: 					/*ImageInfo->sections_found |= FOUND_EXIF;*/
 3390: 					/* Seen files from some 'U-lead' software with Vivitar scanner
 3391: 					   that uses marker 31 later in the file (no clue what for!) */
 3392: 					exif_process_APP1(ImageInfo, (char *)Data, itemlen, fpos TSRMLS_CC);
 3393: 				}
 3394: 				break;
 3395: 
 3396: 			case M_APP12:
 3397: 				exif_process_APP12(ImageInfo, (char *)Data, itemlen TSRMLS_CC);
 3398: 				break;
 3399: 
 3400: 
 3401: 			case M_SOF0:
 3402: 			case M_SOF1:
 3403: 			case M_SOF2:
 3404: 			case M_SOF3:
 3405: 			case M_SOF5:
 3406: 			case M_SOF6:
 3407: 			case M_SOF7:
 3408: 			case M_SOF9:
 3409: 			case M_SOF10:
 3410: 			case M_SOF11:
 3411: 			case M_SOF13:
 3412: 			case M_SOF14:
 3413: 			case M_SOF15:
 3414: 				if ((itemlen - 2) < 6) {
 3415: 					return FALSE;
 3416: 				}
 3417: 		
 3418: 				exif_process_SOFn(Data, marker, &sof_info);
 3419: 				ImageInfo->Width  = sof_info.width;
 3420: 				ImageInfo->Height = sof_info.height;
 3421: 				if (sof_info.num_components == 3) {
 3422: 					ImageInfo->IsColor = 1;
 3423: 				} else {
 3424: 					ImageInfo->IsColor = 0;
 3425: 				}
 3426: 				break;
 3427: 			default:
 3428: 				/* skip any other marker silently. */
 3429: 				break;
 3430: 		}
 3431: 
 3432: 		/* keep track of last marker */
 3433: 		last_marker = marker;
 3434: 	}
 3435: #ifdef EXIF_DEBUG
 3436: 	exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Done");
 3437: #endif
 3438: 	return TRUE;
 3439: }
 3440: /* }}} */
 3441: 
 3442: /* {{{ exif_scan_thumbnail
 3443:  * scan JPEG in thumbnail (memory) */
 3444: static int exif_scan_thumbnail(image_info_type *ImageInfo TSRMLS_DC)
 3445: {
 3446: 	uchar           c, *data = (uchar*)ImageInfo->Thumbnail.data;
 3447: 	int             n, marker;
 3448: 	size_t          length=2, pos=0;
 3449: 	jpeg_sof_info   sof_info;
 3450: 
 3451: 	if (!data) {
 3452: 		return FALSE; /* nothing to do here */
 3453: 	}
 3454: 	if (memcmp(data, "\xFF\xD8\xFF", 3)) {
 3455: 		if (!ImageInfo->Thumbnail.width && !ImageInfo->Thumbnail.height) {
 3456: 			exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Thumbnail is not a JPEG image");
 3457: 		}
 3458: 		return FALSE;
 3459: 	}
 3460: 	for (;;) {
 3461: 		pos += length;
 3462: 		if (pos>=ImageInfo->Thumbnail.size) 
 3463: 			return FALSE;
 3464: 		c = data[pos++];
 3465: 		if (pos>=ImageInfo->Thumbnail.size) 
 3466: 			return FALSE;
 3467: 		if (c != 0xFF) {
 3468: 			return FALSE;
 3469: 		}
 3470: 		n = 8;
 3471: 		while ((c = data[pos++]) == 0xFF && n--) {
 3472: 			if (pos+3>=ImageInfo->Thumbnail.size) 
 3473: 				return FALSE;
 3474: 			/* +3 = pos++ of next check when reaching marker + 2 bytes for length */
 3475: 		}
 3476: 		if (c == 0xFF) 
 3477: 			return FALSE;
 3478: 		marker = c;
 3479: 		length = php_jpg_get16(data+pos);
 3480: 		if (pos+length>=ImageInfo->Thumbnail.size) {
 3481: 			return FALSE;
 3482: 		}
 3483: #ifdef EXIF_DEBUG
 3484: 		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);
 3485: #endif
 3486: 		switch (marker) {
 3487: 			case M_SOF0:
 3488: 			case M_SOF1:
 3489: 			case M_SOF2:
 3490: 			case M_SOF3:
 3491: 			case M_SOF5:
 3492: 			case M_SOF6:
 3493: 			case M_SOF7:
 3494: 			case M_SOF9:
 3495: 			case M_SOF10:
 3496: 			case M_SOF11:
 3497: 			case M_SOF13:
 3498: 			case M_SOF14:
 3499: 			case M_SOF15:
 3500: 				/* handle SOFn block */
 3501: 				exif_process_SOFn(data+pos, marker, &sof_info);
 3502: 				ImageInfo->Thumbnail.height   = sof_info.height;
 3503: 				ImageInfo->Thumbnail.width    = sof_info.width;
 3504: #ifdef EXIF_DEBUG
 3505: 				exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Thumbnail: size: %d * %d", sof_info.width, sof_info.height);
 3506: #endif
 3507: 				return TRUE;
 3508: 
 3509: 			case M_SOS:
 3510: 			case M_EOI:
 3511: 				exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Could not compute size of thumbnail");
 3512: 				return FALSE;
 3513: 				break;
 3514: 
 3515: 			default:
 3516: 				/* just skip */
 3517: 				break;
 3518: 		}
 3519: 	}
 3520: 
 3521: 	exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Could not compute size of thumbnail");
 3522: 	return FALSE;
 3523: }
 3524: /* }}} */
 3525: 
 3526: /* {{{ exif_process_IFD_in_TIFF
 3527:  * Parse the TIFF header; */
 3528: static int exif_process_IFD_in_TIFF(image_info_type *ImageInfo, size_t dir_offset, int section_index TSRMLS_DC)
 3529: {
 3530: 	int i, sn, num_entries, sub_section_index = 0;
 3531: 	unsigned char *dir_entry;
 3532: 	char tagname[64];
 3533: 	size_t ifd_size, dir_size, entry_offset, next_offset, entry_length, entry_value=0, fgot;
 3534: 	int entry_tag , entry_type;
 3535: 	tag_table_type tag_table = exif_get_tag_table(section_index);
 3536: 
 3537: 	if (ImageInfo->ifd_nesting_level > MAX_IFD_NESTING_LEVEL) {
 3538:                 return FALSE;
 3539:         }
 3540: 
 3541: 	if (ImageInfo->FileSize >= dir_offset+2) {
 3542: 		sn = exif_file_sections_add(ImageInfo, M_PSEUDO, 2, NULL);
 3543: #ifdef EXIF_DEBUG
 3544: 		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);
 3545: #endif
 3546: 		php_stream_seek(ImageInfo->infile, dir_offset, SEEK_SET); /* we do not know the order of sections */
 3547: 		php_stream_read(ImageInfo->infile, (char*)ImageInfo->file.list[sn].data, 2);
 3548: 		num_entries = php_ifd_get16u(ImageInfo->file.list[sn].data, ImageInfo->motorola_intel);
 3549: 		dir_size = 2/*num dir entries*/ +12/*length of entry*/*num_entries +4/* offset to next ifd (points to thumbnail or NULL)*/;
 3550: 		if (ImageInfo->FileSize >= dir_offset+dir_size) {
 3551: #ifdef EXIF_DEBUG
 3552: 			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);
 3553: #endif
 3554: 			if (exif_file_sections_realloc(ImageInfo, sn, dir_size TSRMLS_CC)) {
 3555: 				return FALSE;
 3556: 			}
 3557: 			php_stream_read(ImageInfo->infile, (char*)(ImageInfo->file.list[sn].data+2), dir_size-2);
 3558: 			/*exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Dump: %s", exif_char_dump(ImageInfo->file.list[sn].data, dir_size, 0));*/
 3559: 			next_offset = php_ifd_get32u(ImageInfo->file.list[sn].data + dir_size - 4, ImageInfo->motorola_intel);
 3560: #ifdef EXIF_DEBUG
 3561: 			exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Read from TIFF done, next offset x%04X", next_offset);
 3562: #endif
 3563: 			/* now we have the directory we can look how long it should be */
 3564: 			ifd_size = dir_size;
 3565: 			for(i=0;i<num_entries;i++) {
 3566: 				dir_entry 	 = ImageInfo->file.list[sn].data+2+i*12;
 3567: 				entry_tag    = php_ifd_get16u(dir_entry+0, ImageInfo->motorola_intel);
 3568: 				entry_type   = php_ifd_get16u(dir_entry+2, ImageInfo->motorola_intel);
 3569: 				if (entry_type > NUM_FORMATS) {
 3570: 					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);
 3571: 					/* Since this is repeated in exif_process_IFD_TAG make it a notice here */
 3572: 					/* and make it a warning in the exif_process_IFD_TAG which is called    */
 3573: 					/* elsewhere. */
 3574: 					entry_type = TAG_FMT_BYTE;
 3575: 					/*The next line would break the image on writeback: */
 3576: 					/* php_ifd_set16u(dir_entry+2, entry_type, ImageInfo->motorola_intel);*/
 3577: 				}
 3578: 				entry_length = php_ifd_get32u(dir_entry+4, ImageInfo->motorola_intel) * php_tiff_bytes_per_format[entry_type];
 3579: 				if (entry_length <= 4) {
 3580: 					switch(entry_type) {
 3581: 						case TAG_FMT_USHORT:
 3582: 							entry_value  = php_ifd_get16u(dir_entry+8, ImageInfo->motorola_intel);
 3583: 							break;
 3584: 						case TAG_FMT_SSHORT:
 3585: 							entry_value  = php_ifd_get16s(dir_entry+8, ImageInfo->motorola_intel);
 3586: 							break;
 3587: 						case TAG_FMT_ULONG:
 3588: 							entry_value  = php_ifd_get32u(dir_entry+8, ImageInfo->motorola_intel);
 3589: 							break;
 3590: 						case TAG_FMT_SLONG:
 3591: 							entry_value  = php_ifd_get32s(dir_entry+8, ImageInfo->motorola_intel);
 3592: 							break;
 3593: 					}
 3594: 					switch(entry_tag) {
 3595: 						case TAG_IMAGEWIDTH:
 3596: 						case TAG_COMP_IMAGE_WIDTH:
 3597: 							ImageInfo->Width  = entry_value;
 3598: 							break;
 3599: 						case TAG_IMAGEHEIGHT:
 3600: 						case TAG_COMP_IMAGE_HEIGHT:
 3601: 							ImageInfo->Height = entry_value;
 3602: 							break;
 3603: 						case TAG_PHOTOMETRIC_INTERPRETATION:
 3604: 							switch (entry_value) {
 3605: 								case PMI_BLACK_IS_ZERO:
 3606: 								case PMI_WHITE_IS_ZERO:
 3607: 								case PMI_TRANSPARENCY_MASK:
 3608: 									ImageInfo->IsColor = 0;
 3609: 									break;
 3610: 								case PMI_RGB:
 3611: 								case PMI_PALETTE_COLOR:
 3612: 								case PMI_SEPARATED:
 3613: 								case PMI_YCBCR:
 3614: 								case PMI_CIELAB:
 3615: 									ImageInfo->IsColor = 1;
 3616: 									break;
 3617: 							}
 3618: 							break;
 3619: 					}
 3620: 				} else {
 3621: 					entry_offset = php_ifd_get32u(dir_entry+8, ImageInfo->motorola_intel);
 3622: 					/* if entry needs expading ifd cache and entry is at end of current ifd cache. */
 3623: 					/* otherwise there may be huge holes between two entries */
 3624: 					if (entry_offset + entry_length > dir_offset + ifd_size
 3625: 					  && entry_offset == dir_offset + ifd_size) {
 3626: 						ifd_size = entry_offset + entry_length - dir_offset;
 3627: #ifdef EXIF_DEBUG
 3628: 						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);
 3629: #endif
 3630: 					}
 3631: 				}
 3632: 			}
 3633: 			if (ImageInfo->FileSize >= dir_offset + ImageInfo->file.list[sn].size) {
 3634: 				if (ifd_size > dir_size) {
 3635: 					if (dir_offset + ifd_size > ImageInfo->FileSize) {
 3636: 						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);
 3637: 						return FALSE;
 3638: 					}
 3639: 					if (exif_file_sections_realloc(ImageInfo, sn, ifd_size TSRMLS_CC)) {
 3640: 						return FALSE;
 3641: 					}
 3642: 					/* read values not stored in directory itself */
 3643: #ifdef EXIF_DEBUG
 3644: 					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);
 3645: #endif
 3646: 					php_stream_read(ImageInfo->infile, (char*)(ImageInfo->file.list[sn].data+dir_size), ifd_size-dir_size);
 3647: #ifdef EXIF_DEBUG
 3648: 					exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Read from TIFF, done");
 3649: #endif
 3650: 				}
 3651: 				/* now process the tags */
 3652: 				for(i=0;i<num_entries;i++) {
 3653: 					dir_entry 	 = ImageInfo->file.list[sn].data+2+i*12;
 3654: 					entry_tag    = php_ifd_get16u(dir_entry+0, ImageInfo->motorola_intel);
 3655: 					entry_type   = php_ifd_get16u(dir_entry+2, ImageInfo->motorola_intel);
 3656: 					/*entry_length = php_ifd_get32u(dir_entry+4, ImageInfo->motorola_intel);*/
 3657: 					if (entry_tag == TAG_EXIF_IFD_POINTER ||
 3658: 						entry_tag == TAG_INTEROP_IFD_POINTER ||
 3659: 						entry_tag == TAG_GPS_IFD_POINTER ||
 3660: 						entry_tag == TAG_SUB_IFD
 3661: 					) {
 3662: 						switch(entry_tag) {
 3663: 							case TAG_EXIF_IFD_POINTER:
 3664: 								ImageInfo->sections_found |= FOUND_EXIF;
 3665: 								sub_section_index = SECTION_EXIF;
 3666: 								break;
 3667: 							case TAG_GPS_IFD_POINTER:
 3668: 								ImageInfo->sections_found |= FOUND_GPS;
 3669: 								sub_section_index = SECTION_GPS;
 3670: 								break;
 3671: 							case TAG_INTEROP_IFD_POINTER:
 3672: 								ImageInfo->sections_found |= FOUND_INTEROP;
 3673: 								sub_section_index = SECTION_INTEROP;
 3674: 								break;
 3675: 							case TAG_SUB_IFD:
 3676: 								ImageInfo->sections_found |= FOUND_THUMBNAIL;
 3677: 								sub_section_index = SECTION_THUMBNAIL;
 3678: 								break;
 3679: 						}
 3680: 						entry_offset = php_ifd_get32u(dir_entry+8, ImageInfo->motorola_intel);
 3681: #ifdef EXIF_DEBUG
 3682: 						exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Next IFD: %s @x%04X", exif_get_sectionname(sub_section_index), entry_offset);
 3683: #endif
 3684: 						ImageInfo->ifd_nesting_level++;
 3685: 						exif_process_IFD_in_TIFF(ImageInfo, entry_offset, sub_section_index TSRMLS_CC);
 3686: 						if (section_index!=SECTION_THUMBNAIL && entry_tag==TAG_SUB_IFD) {
 3687: 							if (ImageInfo->Thumbnail.filetype != IMAGE_FILETYPE_UNKNOWN
 3688: 							&&  ImageInfo->Thumbnail.size
 3689: 							&&  ImageInfo->Thumbnail.offset
 3690: 							&&  ImageInfo->read_thumbnail
 3691: 							) {
 3692: #ifdef EXIF_DEBUG
 3693: 								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);
 3694: #endif
 3695: 								if (!ImageInfo->Thumbnail.data) {
 3696: 									ImageInfo->Thumbnail.data = safe_emalloc(ImageInfo->Thumbnail.size, 1, 0);
 3697: 									php_stream_seek(ImageInfo->infile, ImageInfo->Thumbnail.offset, SEEK_SET);
 3698: 									fgot = php_stream_read(ImageInfo->infile, ImageInfo->Thumbnail.data, ImageInfo->Thumbnail.size);
 3699: 									if (fgot < ImageInfo->Thumbnail.size) {
 3700: 										EXIF_ERRLOG_THUMBEOF(ImageInfo)
 3701: 									}
 3702: 									exif_thumbnail_build(ImageInfo TSRMLS_CC);
 3703: 								}
 3704: 							}
 3705: 						}
 3706: #ifdef EXIF_DEBUG
 3707: 						exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Next IFD: %s done", exif_get_sectionname(sub_section_index));
 3708: #endif
 3709: 					} else {
 3710: 						if (!exif_process_IFD_TAG(ImageInfo, (char*)dir_entry,
 3711: 												  (char*)(ImageInfo->file.list[sn].data-dir_offset),
 3712: 												  ifd_size, 0, section_index, 0, tag_table TSRMLS_CC)) {
 3713: 							return FALSE;
 3714: 						}
 3715: 					}
 3716: 				}
 3717: 				/* If we had a thumbnail in a SUB_IFD we have ANOTHER image in NEXT IFD */
 3718: 				if (next_offset && section_index != SECTION_THUMBNAIL) {
 3719: 					/* this should be a thumbnail IFD */
 3720: 					/* the thumbnail itself is stored at Tag=StripOffsets */
 3721: #ifdef EXIF_DEBUG
 3722: 					exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Read next IFD (THUMBNAIL) at x%04X", next_offset);
 3723: #endif
 3724: 					ImageInfo->ifd_nesting_level++;
 3725: 					exif_process_IFD_in_TIFF(ImageInfo, next_offset, SECTION_THUMBNAIL TSRMLS_CC);
 3726: #ifdef EXIF_DEBUG
 3727: 					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);
 3728: #endif
 3729: 					if (!ImageInfo->Thumbnail.data && ImageInfo->Thumbnail.offset && ImageInfo->Thumbnail.size && ImageInfo->read_thumbnail) {
 3730: 						ImageInfo->Thumbnail.data = safe_emalloc(ImageInfo->Thumbnail.size, 1, 0);
 3731: 						php_stream_seek(ImageInfo->infile, ImageInfo->Thumbnail.offset, SEEK_SET);
 3732: 						fgot = php_stream_read(ImageInfo->infile, ImageInfo->Thumbnail.data, ImageInfo->Thumbnail.size);
 3733: 						if (fgot < ImageInfo->Thumbnail.size) {
 3734: 							EXIF_ERRLOG_THUMBEOF(ImageInfo)
 3735: 						}
 3736: 						exif_thumbnail_build(ImageInfo TSRMLS_CC);
 3737: 					}
 3738: #ifdef EXIF_DEBUG
 3739: 					exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Read next IFD (THUMBNAIL) done");
 3740: #endif
 3741: 				}
 3742: 				return TRUE;
 3743: 			} else {
 3744: 				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);
 3745: 				return FALSE;
 3746: 			}
 3747: 		} else {
 3748: 			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);
 3749: 			return FALSE;
 3750: 		}
 3751: 	} else {
 3752: 		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);
 3753: 		return FALSE;
 3754: 	}
 3755: }
 3756: /* }}} */
 3757: 
 3758: /* {{{ exif_scan_FILE_header
 3759:  * Parse the marker stream until SOS or EOI is seen; */
 3760: static int exif_scan_FILE_header(image_info_type *ImageInfo TSRMLS_DC)
 3761: {
 3762: 	unsigned char file_header[8];
 3763: 	int ret = FALSE;
 3764: 
 3765: 	ImageInfo->FileType = IMAGE_FILETYPE_UNKNOWN;
 3766: 
 3767: 	if (ImageInfo->FileSize >= 2) {
 3768: 		php_stream_seek(ImageInfo->infile, 0, SEEK_SET);
 3769: 		if (php_stream_read(ImageInfo->infile, (char*)file_header, 2) != 2) {
 3770: 			return FALSE;
 3771: 		}
 3772: 		if ((file_header[0]==0xff) && (file_header[1]==M_SOI)) {
 3773: 			ImageInfo->FileType = IMAGE_FILETYPE_JPEG;
 3774: 			if (exif_scan_JPEG_header(ImageInfo TSRMLS_CC)) {
 3775: 				ret = TRUE;
 3776: 			} else {
 3777: 				exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Invalid JPEG file");
 3778: 			}
 3779: 		} else if (ImageInfo->FileSize >= 8) {
 3780: 			if (php_stream_read(ImageInfo->infile, (char*)(file_header+2), 6) != 6) {
 3781: 				return FALSE;
 3782: 			}
 3783: 			if (!memcmp(file_header, "II\x2A\x00", 4)) {
 3784: 				ImageInfo->FileType = IMAGE_FILETYPE_TIFF_II;
 3785: 				ImageInfo->motorola_intel = 0;
 3786: #ifdef EXIF_DEBUG
 3787: 				exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "File has TIFF/II format");
 3788: #endif
 3789: 				ImageInfo->sections_found |= FOUND_IFD0;
 3790: 				if (exif_process_IFD_in_TIFF(ImageInfo, 
 3791: 											 php_ifd_get32u(file_header + 4, ImageInfo->motorola_intel),
 3792: 											 SECTION_IFD0 TSRMLS_CC)) {
 3793: 					ret = TRUE;
 3794: 				} else {
 3795: 					exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Invalid TIFF file");
 3796: 				}
 3797: 			} else if (!memcmp(file_header, "MM\x00\x2a", 4)) {
 3798: 				ImageInfo->FileType = IMAGE_FILETYPE_TIFF_MM;
 3799: 				ImageInfo->motorola_intel = 1;
 3800: #ifdef EXIF_DEBUG
 3801: 				exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "File has TIFF/MM format");
 3802: #endif
 3803: 				ImageInfo->sections_found |= FOUND_IFD0;
 3804: 				if (exif_process_IFD_in_TIFF(ImageInfo,
 3805: 											 php_ifd_get32u(file_header + 4, ImageInfo->motorola_intel),
 3806: 											 SECTION_IFD0 TSRMLS_CC)) {
 3807: 					ret = TRUE;
 3808: 				} else {
 3809: 					exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Invalid TIFF file");
 3810: 				}
 3811: 			} else {
 3812: 				exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "File not supported");
 3813: 				return FALSE;
 3814: 			}
 3815: 		}
 3816: 	} else {
 3817: 		exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "File too small (%d)", ImageInfo->FileSize);
 3818: 	}
 3819: 	return ret;
 3820: }
 3821: /* }}} */
 3822: 
 3823: /* {{{ exif_discard_imageinfo
 3824:    Discard data scanned by exif_read_file.
 3825: */
 3826: static int exif_discard_imageinfo(image_info_type *ImageInfo)
 3827: {
 3828: 	int i;
 3829: 
 3830: 	EFREE_IF(ImageInfo->FileName);
 3831: 	EFREE_IF(ImageInfo->UserComment);
 3832: 	EFREE_IF(ImageInfo->UserCommentEncoding);
 3833: 	EFREE_IF(ImageInfo->Copyright);
 3834: 	EFREE_IF(ImageInfo->CopyrightPhotographer);
 3835: 	EFREE_IF(ImageInfo->CopyrightEditor);
 3836: 	EFREE_IF(ImageInfo->Thumbnail.data);
 3837: 	EFREE_IF(ImageInfo->encode_unicode);
 3838: 	EFREE_IF(ImageInfo->decode_unicode_be);
 3839: 	EFREE_IF(ImageInfo->decode_unicode_le);
 3840: 	EFREE_IF(ImageInfo->encode_jis);
 3841: 	EFREE_IF(ImageInfo->decode_jis_be);
 3842: 	EFREE_IF(ImageInfo->decode_jis_le);
 3843: 	EFREE_IF(ImageInfo->make);
 3844: 	EFREE_IF(ImageInfo->model);
 3845: 	for (i=0; i<ImageInfo->xp_fields.count; i++) {
 3846: 		EFREE_IF(ImageInfo->xp_fields.list[i].value);
 3847: 	}
 3848: 	EFREE_IF(ImageInfo->xp_fields.list);
 3849: 	for (i=0; i<SECTION_COUNT; i++) {
 3850: 		exif_iif_free(ImageInfo, i);
 3851: 	}
 3852: 	exif_file_sections_free(ImageInfo);
 3853: 	memset(ImageInfo, 0, sizeof(*ImageInfo));
 3854: 	return TRUE;
 3855: }
 3856: /* }}} */
 3857: 
 3858: /* {{{ exif_read_file
 3859:  */
 3860: static int exif_read_file(image_info_type *ImageInfo, char *FileName, int read_thumbnail, int read_all TSRMLS_DC)
 3861: {
 3862: 	int ret;
 3863: 	struct stat st;
 3864: 
 3865: 	/* Start with an empty image information structure. */
 3866: 	memset(ImageInfo, 0, sizeof(*ImageInfo));
 3867: 
 3868: 	ImageInfo->motorola_intel = -1; /* flag as unknown */
 3869: 
 3870: 	ImageInfo->infile = php_stream_open_wrapper(FileName, "rb", STREAM_MUST_SEEK|IGNORE_PATH, NULL);
 3871: 	if (!ImageInfo->infile) {
 3872: 		exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Unable to open file");
 3873: 		return FALSE;
 3874: 	}
 3875: 
 3876: 	if (php_stream_is(ImageInfo->infile, PHP_STREAM_IS_STDIO)) {
 3877: 		if (VCWD_STAT(FileName, &st) >= 0) {
 3878: 			if ((st.st_mode & S_IFMT) != S_IFREG) {
 3879: 				exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Not a file");
 3880: 				php_stream_close(ImageInfo->infile);
 3881: 				return FALSE;
 3882: 			}
 3883: 
 3884: 			/* Store file date/time. */
 3885: 			ImageInfo->FileDateTime = st.st_mtime;
 3886: 			ImageInfo->FileSize = st.st_size;
 3887: 			/*exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Opened stream is file: %d", ImageInfo->FileSize);*/
 3888: 		}
 3889: 	} else {
 3890: 		if (!ImageInfo->FileSize) {
 3891: 			php_stream_seek(ImageInfo->infile, 0, SEEK_END);
 3892: 			ImageInfo->FileSize = php_stream_tell(ImageInfo->infile);
 3893: 			php_stream_seek(ImageInfo->infile, 0, SEEK_SET);
 3894: 		}
 3895: 	}
 3896: 
 3897: 	php_basename(FileName, strlen(FileName), NULL, 0, &(ImageInfo->FileName), NULL TSRMLS_CC);
 3898: 	ImageInfo->read_thumbnail = read_thumbnail;
 3899: 	ImageInfo->read_all = read_all;
 3900: 	ImageInfo->Thumbnail.filetype = IMAGE_FILETYPE_UNKNOWN;
 3901: 
 3902: 	ImageInfo->encode_unicode    = safe_estrdup(EXIF_G(encode_unicode));
 3903: 	ImageInfo->decode_unicode_be = safe_estrdup(EXIF_G(decode_unicode_be));
 3904: 	ImageInfo->decode_unicode_le = safe_estrdup(EXIF_G(decode_unicode_le));
 3905: 	ImageInfo->encode_jis        = safe_estrdup(EXIF_G(encode_jis));
 3906: 	ImageInfo->decode_jis_be     = safe_estrdup(EXIF_G(decode_jis_be));
 3907: 	ImageInfo->decode_jis_le     = safe_estrdup(EXIF_G(decode_jis_le));
 3908: 
 3909: 
 3910: 	ImageInfo->ifd_nesting_level = 0;
 3911: 
 3912: 	/* Scan the JPEG headers. */
 3913: 	ret = exif_scan_FILE_header(ImageInfo TSRMLS_CC);
 3914: 
 3915: 	php_stream_close(ImageInfo->infile);
 3916: 	return ret;
 3917: }
 3918: /* }}} */
 3919: 
 3920: /* {{{ proto array exif_read_data(string filename [, sections_needed [, sub_arrays[, read_thumbnail]]])
 3921:    Reads header data from the JPEG/TIFF image filename and optionally reads the internal thumbnails */
 3922: PHP_FUNCTION(exif_read_data)
 3923: {
 3924: 	char *p_name, *p_sections_needed = NULL;
 3925: 	int p_name_len, p_sections_needed_len = 0;
 3926: 	zend_bool sub_arrays=0, read_thumbnail=0, read_all=0;
 3927: 
 3928: 	int i, ret, sections_needed=0;
 3929: 	image_info_type ImageInfo;
 3930: 	char tmp[64], *sections_str, *s;
 3931: 
 3932: 	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) {
 3933: 		return;
 3934: 	}
 3935: 
 3936: 	memset(&ImageInfo, 0, sizeof(ImageInfo));
 3937: 
 3938: 	if (p_sections_needed) {
 3939: 		spprintf(&sections_str, 0, ",%s,", p_sections_needed);
 3940: 		/* sections_str DOES start with , and SPACES are NOT allowed in names */
 3941: 		s = sections_str;
 3942: 		while (*++s) {
 3943: 			if (*s == ' ') {
 3944: 				*s = ',';
 3945: 			}
 3946: 		}
 3947: 
 3948: 		for (i = 0; i < SECTION_COUNT; i++) {
 3949: 			snprintf(tmp, sizeof(tmp), ",%s,", exif_get_sectionname(i));
 3950: 			if (strstr(sections_str, tmp)) {
 3951: 				sections_needed |= 1<<i;
 3952: 			}
 3953: 		}
 3954: 		EFREE_IF(sections_str);
 3955: 		/* now see what we need */
 3956: #ifdef EXIF_DEBUG
 3957: 		sections_str = exif_get_sectionlist(sections_needed TSRMLS_CC);
 3958: 		if (!sections_str) {
 3959: 			RETURN_FALSE;
 3960: 		}
 3961: 		exif_error_docref(NULL EXIFERR_CC, &ImageInfo, E_NOTICE, "Sections needed: %s", sections_str[0] ? sections_str : "None");
 3962: 		EFREE_IF(sections_str);
 3963: #endif
 3964: 	}
 3965: 
 3966: 	ret = exif_read_file(&ImageInfo, p_name, read_thumbnail, read_all TSRMLS_CC);
 3967: 	sections_str = exif_get_sectionlist(ImageInfo.sections_found TSRMLS_CC);
 3968: 
 3969: #ifdef EXIF_DEBUG
 3970: 	if (sections_str) 
 3971: 		exif_error_docref(NULL EXIFERR_CC, &ImageInfo, E_NOTICE, "Sections found: %s", sections_str[0] ? sections_str : "None");
 3972: #endif
 3973: 
 3974: 	ImageInfo.sections_found |= FOUND_COMPUTED|FOUND_FILE;/* do not inform about in debug*/
 3975: 
 3976: 	if (ret == FALSE || (sections_needed && !(sections_needed&ImageInfo.sections_found))) {
 3977: 		/* array_init must be checked at last! otherwise the array must be freed if a later test fails. */
 3978: 		exif_discard_imageinfo(&ImageInfo);
 3979: 	   	EFREE_IF(sections_str);
 3980: 		RETURN_FALSE;
 3981: 	}
 3982: 
 3983: 	array_init(return_value);
 3984: 
 3985: #ifdef EXIF_DEBUG
 3986: 	exif_error_docref(NULL EXIFERR_CC, &ImageInfo, E_NOTICE, "Generate section FILE");
 3987: #endif
 3988: 
 3989: 	/* now we can add our information */
 3990: 	exif_iif_add_str(&ImageInfo, SECTION_FILE, "FileName",      ImageInfo.FileName TSRMLS_CC);
 3991: 	exif_iif_add_int(&ImageInfo, SECTION_FILE, "FileDateTime",  ImageInfo.FileDateTime TSRMLS_CC);
 3992: 	exif_iif_add_int(&ImageInfo, SECTION_FILE, "FileSize",      ImageInfo.FileSize TSRMLS_CC);
 3993: 	exif_iif_add_int(&ImageInfo, SECTION_FILE, "FileType",      ImageInfo.FileType TSRMLS_CC);
 3994: 	exif_iif_add_str(&ImageInfo, SECTION_FILE, "MimeType",      (char*)php_image_type_to_mime_type(ImageInfo.FileType) TSRMLS_CC);
 3995: 	exif_iif_add_str(&ImageInfo, SECTION_FILE, "SectionsFound", sections_str ? sections_str : "NONE" TSRMLS_CC);
 3996: 
 3997: #ifdef EXIF_DEBUG
 3998: 	exif_error_docref(NULL EXIFERR_CC, &ImageInfo, E_NOTICE, "Generate section COMPUTED");
 3999: #endif
 4000: 
 4001: 	if (ImageInfo.Width>0 &&  ImageInfo.Height>0) {
 4002: 		exif_iif_add_fmt(&ImageInfo, SECTION_COMPUTED, "html"    TSRMLS_CC, "width=\"%d\" height=\"%d\"", ImageInfo.Width, ImageInfo.Height);
 4003: 		exif_iif_add_int(&ImageInfo, SECTION_COMPUTED, "Height", ImageInfo.Height TSRMLS_CC);
 4004: 		exif_iif_add_int(&ImageInfo, SECTION_COMPUTED, "Width",  ImageInfo.Width TSRMLS_CC);
 4005: 	}
 4006: 	exif_iif_add_int(&ImageInfo, SECTION_COMPUTED, "IsColor", ImageInfo.IsColor TSRMLS_CC);
 4007: 	if (ImageInfo.motorola_intel != -1) {
 4008: 		exif_iif_add_int(&ImageInfo, SECTION_COMPUTED, "ByteOrderMotorola", ImageInfo.motorola_intel TSRMLS_CC);
 4009: 	}
 4010: 	if (ImageInfo.FocalLength) {
 4011: 		exif_iif_add_fmt(&ImageInfo, SECTION_COMPUTED, "FocalLength" TSRMLS_CC, "%4.1Fmm", ImageInfo.FocalLength);
 4012: 		if(ImageInfo.CCDWidth) {
 4013: 			exif_iif_add_fmt(&ImageInfo, SECTION_COMPUTED, "35mmFocalLength" TSRMLS_CC, "%dmm", (int)(ImageInfo.FocalLength/ImageInfo.CCDWidth*35+0.5));
 4014: 		}
 4015: 	}
 4016: 	if(ImageInfo.CCDWidth) {
 4017: 		exif_iif_add_fmt(&ImageInfo, SECTION_COMPUTED, "CCDWidth" TSRMLS_CC, "%dmm", (int)ImageInfo.CCDWidth);
 4018: 	}
 4019: 	if(ImageInfo.ExposureTime>0) {
 4020: 		if(ImageInfo.ExposureTime <= 0.5) {
 4021: 			exif_iif_add_fmt(&ImageInfo, SECTION_COMPUTED, "ExposureTime" TSRMLS_CC, "%0.3F s (1/%d)", ImageInfo.ExposureTime, (int)(0.5 + 1/ImageInfo.ExposureTime));
 4022: 		} else {
 4023: 			exif_iif_add_fmt(&ImageInfo, SECTION_COMPUTED, "ExposureTime" TSRMLS_CC, "%0.3F s", ImageInfo.ExposureTime);
 4024: 		}
 4025: 	}
 4026: 	if(ImageInfo.ApertureFNumber) {
 4027: 		exif_iif_add_fmt(&ImageInfo, SECTION_COMPUTED, "ApertureFNumber" TSRMLS_CC, "f/%.1F", ImageInfo.ApertureFNumber);
 4028: 	}
 4029: 	if(ImageInfo.Distance) {
 4030: 		if(ImageInfo.Distance<0) {
 4031: 			exif_iif_add_str(&ImageInfo, SECTION_COMPUTED, "FocusDistance", "Infinite" TSRMLS_CC);
 4032: 		} else {
 4033: 			exif_iif_add_fmt(&ImageInfo, SECTION_COMPUTED, "FocusDistance" TSRMLS_CC, "%0.2Fm", ImageInfo.Distance);
 4034: 		}
 4035: 	}
 4036: 	if (ImageInfo.UserComment) {
 4037: 		exif_iif_add_buffer(&ImageInfo, SECTION_COMPUTED, "UserComment", ImageInfo.UserCommentLength, ImageInfo.UserComment TSRMLS_CC);
 4038: 		if (ImageInfo.UserCommentEncoding && strlen(ImageInfo.UserCommentEncoding)) {
 4039: 			exif_iif_add_str(&ImageInfo, SECTION_COMPUTED, "UserCommentEncoding", ImageInfo.UserCommentEncoding TSRMLS_CC);
 4040: 		}
 4041: 	}
 4042: 
 4043: 	exif_iif_add_str(&ImageInfo, SECTION_COMPUTED, "Copyright",              ImageInfo.Copyright TSRMLS_CC);
 4044: 	exif_iif_add_str(&ImageInfo, SECTION_COMPUTED, "Copyright.Photographer", ImageInfo.CopyrightPhotographer TSRMLS_CC);
 4045: 	exif_iif_add_str(&ImageInfo, SECTION_COMPUTED, "Copyright.Editor",       ImageInfo.CopyrightEditor TSRMLS_CC);
 4046: 
 4047: 	for (i=0; i<ImageInfo.xp_fields.count; i++) {
 4048: 		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);
 4049: 	}
 4050: 	if (ImageInfo.Thumbnail.size) {
 4051: 		if (read_thumbnail) {
 4052: 			/* not exif_iif_add_str : this is a buffer */
 4053: 			exif_iif_add_tag(&ImageInfo, SECTION_THUMBNAIL, "THUMBNAIL", TAG_NONE, TAG_FMT_UNDEFINED, ImageInfo.Thumbnail.size, ImageInfo.Thumbnail.data TSRMLS_CC);
 4054: 		}
 4055: 		if (!ImageInfo.Thumbnail.width || !ImageInfo.Thumbnail.height) {
 4056: 			/* try to evaluate if thumbnail data is present */
 4057: 			exif_scan_thumbnail(&ImageInfo TSRMLS_CC);
 4058: 		}
 4059: 		exif_iif_add_int(&ImageInfo, SECTION_COMPUTED, "Thumbnail.FileType", ImageInfo.Thumbnail.filetype TSRMLS_CC);
 4060: 		exif_iif_add_str(&ImageInfo, SECTION_COMPUTED, "Thumbnail.MimeType", (char*)php_image_type_to_mime_type(ImageInfo.Thumbnail.filetype) TSRMLS_CC);
 4061: 	}
 4062: 	if (ImageInfo.Thumbnail.width && ImageInfo.Thumbnail.height) {
 4063: 		exif_iif_add_int(&ImageInfo, SECTION_COMPUTED, "Thumbnail.Height", ImageInfo.Thumbnail.height TSRMLS_CC);
 4064: 		exif_iif_add_int(&ImageInfo, SECTION_COMPUTED, "Thumbnail.Width",  ImageInfo.Thumbnail.width TSRMLS_CC);
 4065: 	}
 4066:    	EFREE_IF(sections_str);
 4067: 
 4068: #ifdef EXIF_DEBUG
 4069: 	exif_error_docref(NULL EXIFERR_CC, &ImageInfo, E_NOTICE, "Adding image infos");
 4070: #endif
 4071: 
 4072: 	add_assoc_image_info(return_value, sub_arrays, &ImageInfo, SECTION_FILE       TSRMLS_CC);
 4073: 	add_assoc_image_info(return_value, 1,          &ImageInfo, SECTION_COMPUTED   TSRMLS_CC);
 4074: 	add_assoc_image_info(return_value, sub_arrays, &ImageInfo, SECTION_ANY_TAG    TSRMLS_CC);
 4075: 	add_assoc_image_info(return_value, sub_arrays, &ImageInfo, SECTION_IFD0       TSRMLS_CC);
 4076: 	add_assoc_image_info(return_value, 1,          &ImageInfo, SECTION_THUMBNAIL  TSRMLS_CC);
 4077: 	add_assoc_image_info(return_value, 1,          &ImageInfo, SECTION_COMMENT    TSRMLS_CC);
 4078: 	add_assoc_image_info(return_value, sub_arrays, &ImageInfo, SECTION_EXIF       TSRMLS_CC);
 4079: 	add_assoc_image_info(return_value, sub_arrays, &ImageInfo, SECTION_GPS        TSRMLS_CC);
 4080: 	add_assoc_image_info(return_value, sub_arrays, &ImageInfo, SECTION_INTEROP    TSRMLS_CC);
 4081: 	add_assoc_image_info(return_value, sub_arrays, &ImageInfo, SECTION_FPIX       TSRMLS_CC);
 4082: 	add_assoc_image_info(return_value, sub_arrays, &ImageInfo, SECTION_APP12      TSRMLS_CC);
 4083: 	add_assoc_image_info(return_value, sub_arrays, &ImageInfo, SECTION_WINXP      TSRMLS_CC);
 4084: 	add_assoc_image_info(return_value, sub_arrays, &ImageInfo, SECTION_MAKERNOTE  TSRMLS_CC);
 4085: 
 4086: #ifdef EXIF_DEBUG
 4087: 	exif_error_docref(NULL EXIFERR_CC, &ImageInfo, E_NOTICE, "Discarding info");
 4088: #endif
 4089: 
 4090: 	exif_discard_imageinfo(&ImageInfo);
 4091: 
 4092: #ifdef EXIF_DEBUG
 4093: 	php_error_docref1(NULL TSRMLS_CC, Z_STRVAL_PP(p_name), E_NOTICE, "done");
 4094: #endif
 4095: }
 4096: /* }}} */
 4097: 
 4098: /* {{{ proto string exif_thumbnail(string filename [, &width, &height [, &imagetype]])
 4099:    Reads the embedded thumbnail */
 4100: PHP_FUNCTION(exif_thumbnail)
 4101: {
 4102: 	zval *p_width = 0, *p_height = 0, *p_imagetype = 0;
 4103: 	char *p_name;
 4104: 	int p_name_len, ret, arg_c = ZEND_NUM_ARGS();
 4105: 	image_info_type ImageInfo;
 4106: 
 4107: 	memset(&ImageInfo, 0, sizeof(ImageInfo));
 4108: 
 4109: 	if (arg_c!=1 && arg_c!=3 && arg_c!=4) {
 4110: 		WRONG_PARAM_COUNT;
 4111: 	}
 4112: 
 4113: 	if (zend_parse_parameters(arg_c TSRMLS_CC, "p|z/z/z/", &p_name, &p_name_len, &p_width, &p_height, &p_imagetype) == FAILURE) {
 4114: 		return;
 4115: 	}
 4116: 
 4117: 	ret = exif_read_file(&ImageInfo, p_name, 1, 0 TSRMLS_CC);
 4118: 	if (ret==FALSE) {
 4119: 		exif_discard_imageinfo(&ImageInfo);
 4120: 		RETURN_FALSE;
 4121: 	}
 4122: 
 4123: #ifdef EXIF_DEBUG
 4124: 	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);
 4125: #endif
 4126: 	if (!ImageInfo.Thumbnail.data || !ImageInfo.Thumbnail.size) {
 4127: 		exif_discard_imageinfo(&ImageInfo);
 4128: 		RETURN_FALSE;
 4129: 	}
 4130: 
 4131: #ifdef EXIF_DEBUG
 4132: 	exif_error_docref(NULL EXIFERR_CC, &ImageInfo, E_NOTICE, "Returning thumbnail(%d)", ImageInfo.Thumbnail.size);
 4133: #endif
 4134: 
 4135: 	ZVAL_STRINGL(return_value, ImageInfo.Thumbnail.data, ImageInfo.Thumbnail.size, 1);
 4136: 	if (arg_c >= 3) {
 4137: 		if (!ImageInfo.Thumbnail.width || !ImageInfo.Thumbnail.height) {
 4138: 			exif_scan_thumbnail(&ImageInfo TSRMLS_CC);
 4139: 		}
 4140: 		zval_dtor(p_width);
 4141: 		zval_dtor(p_height);
 4142: 		ZVAL_LONG(p_width,  ImageInfo.Thumbnail.width);
 4143: 		ZVAL_LONG(p_height, ImageInfo.Thumbnail.height);
 4144: 	}
 4145: 	if (arg_c >= 4)	{
 4146: 		zval_dtor(p_imagetype);
 4147: 		ZVAL_LONG(p_imagetype, ImageInfo.Thumbnail.filetype);
 4148: 	}
 4149: 
 4150: #ifdef EXIF_DEBUG
 4151: 	exif_error_docref(NULL EXIFERR_CC, &ImageInfo, E_NOTICE, "Discarding info");
 4152: #endif
 4153: 
 4154: 	exif_discard_imageinfo(&ImageInfo);
 4155: 
 4156: #ifdef EXIF_DEBUG
 4157: 	php_error_docref1(NULL TSRMLS_CC, p_name, E_NOTICE, "Done");
 4158: #endif
 4159: }
 4160: /* }}} */
 4161: 
 4162: /* {{{ proto int exif_imagetype(string imagefile)
 4163:    Get the type of an image */
 4164: PHP_FUNCTION(exif_imagetype)
 4165: {
 4166: 	char *imagefile;
 4167: 	int imagefile_len;
 4168: 	php_stream * stream;
 4169:  	int itype = 0;
 4170: 
 4171: 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &imagefile, &imagefile_len) == FAILURE) {
 4172: 		return;
 4173: 	}
 4174: 
 4175: 	stream = php_stream_open_wrapper(imagefile, "rb", IGNORE_PATH|REPORT_ERRORS, NULL);
 4176: 
 4177: 	if (stream == NULL) {
 4178: 		RETURN_FALSE;
 4179: 	}
 4180: 
 4181: 	itype = php_getimagetype(stream, NULL TSRMLS_CC);
 4182: 
 4183: 	php_stream_close(stream);
 4184: 
 4185: 	if (itype == IMAGE_FILETYPE_UNKNOWN) {
 4186: 		RETURN_FALSE;
 4187: 	} else {
 4188: 		ZVAL_LONG(return_value, itype);
 4189: 	}
 4190: }
 4191: /* }}} */
 4192: 
 4193: #endif
 4194: 
 4195: /*
 4196:  * Local variables:
 4197:  * tab-width: 4
 4198:  * c-basic-offset: 4
 4199:  * End:
 4200:  * vim600: sw=4 ts=4 tw=78 fdm=marker
 4201:  * vim<600: sw=4 ts=4 tw=78
 4202:  */

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