Coverage Report

Created: 2022-10-14 11:19

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