PageRenderTime 77ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/ext/exif/exif.c

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