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