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