PageRenderTime 71ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/ext/exif/exif.c

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