Coverage Report

Created: 2026-10-28 06:20

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