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