Coverage Report

Created: 2025-09-27 06:26

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