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