PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/ext/exif/exif.c

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