Coverage Report

Created: 2025-11-16 06:23

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
8
#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
904k
#define EFREE_IF(ptr) if (ptr) efree(ptr)
64
65
414k
#define MAX_IFD_NESTING_LEVEL 10
66
416k
#define MAX_IFD_TAGS 1000
67
68
/* {{{ PHP_MINFO_FUNCTION */
69
PHP_MINFO_FUNCTION(exif)
70
8
{
71
8
  php_info_print_table_start();
72
8
  php_info_print_table_row(2, "EXIF Support", "enabled");
73
8
  php_info_print_table_row(2, "Supported EXIF Version", "0220");
74
8
  php_info_print_table_row(2, "Supported filetypes", "JPEG, TIFF");
75
76
8
  if (USE_MBSTRING) {
77
0
    php_info_print_table_row(2, "Multibyte decoding support using mbstring", "enabled");
78
8
  } else {
79
8
    php_info_print_table_row(2, "Multibyte decoding support using mbstring", "disabled");
80
8
  }
81
82
8
  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
8
  php_info_print_table_end();
84
85
8
  DISPLAY_INI_ENTRIES();
86
8
}
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
19.2M
#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
761k
{
223
761k
  size_t total_read = 0;
224
1.39M
  while (total_read < count) {
225
636k
    ssize_t ret = php_stream_read(stream, buf + total_read, count - total_read);
226
636k
    if (ret == -1) {
227
0
      return -1;
228
0
    }
229
636k
    if (ret == 0) {
230
326
      break;
231
326
    }
232
635k
    total_read += ret;
233
635k
  }
234
761k
  return total_read;
235
761k
}
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
30
#define EXIF_ERRLOG_FILEEOF(ImageInfo)    exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "%s", EXIF_ERROR_FILEEOF);
246
591
#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
11.7M
#define NUM_FORMATS 13
256
257
10.0M
#define TAG_FMT_BYTE       1
258
1.39M
#define TAG_FMT_STRING     2
259
12.6M
#define TAG_FMT_USHORT     3
260
6.51M
#define TAG_FMT_ULONG      4
261
155k
#define TAG_FMT_URATIONAL  5
262
450k
#define TAG_FMT_SBYTE      6
263
94.9k
#define TAG_FMT_UNDEFINED  7
264
368k
#define TAG_FMT_SSHORT     8
265
1.06M
#define TAG_FMT_SLONG      9
266
461k
#define TAG_FMT_SRATIONAL 10
267
1.25M
#define TAG_FMT_SINGLE    11
268
464k
#define TAG_FMT_DOUBLE    12
269
152
#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
31.0k
#define TAG_IMAGEWIDTH                  0x0100
329
8.06k
#define TAG_IMAGEHEIGHT                 0x0101
330
#define TAG_BITS_PER_SAMPLE             0x0102
331
#define TAG_COMPRESSION                 0x0103
332
4.97k
#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.48k
#define TAG_MAKE                        0x010F
340
299
#define TAG_MODEL                       0x0110
341
2.39k
#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.14k
#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
333k
#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.48k
#define TAG_JPEG_INTERCHANGE_FORMAT     0x0201
388
1.55k
#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.77k
#define TAG_COPYRIGHT                   0x8298
410
279
#define TAG_EXPOSURETIME                0x829A
411
1.09k
#define TAG_FNUMBER                     0x829D
412
802k
#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
560k
#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.58k
#define TAG_SHUTTERSPEED                0x9201
429
668
#define TAG_APERTURE                    0x9202
430
#define TAG_BRIGHTNESS_VALUE            0x9203
431
#define TAG_EXPOSURE_BIAS_VALUE         0x9204
432
1.39k
#define TAG_MAX_APERTURE                0x9205
433
1.87k
#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
55.2k
#define TAG_MAKER_NOTE                  0x927C
442
4.42k
#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.47k
#define TAG_XP_TITLE                    0x9C9B
449
1.76k
#define TAG_XP_COMMENTS                 0x9C9C
450
2.04k
#define TAG_XP_AUTHOR                   0x9C9D
451
3.36k
#define TAG_XP_KEYWORDS                 0x9C9E
452
3.92k
#define TAG_XP_SUBJECT                  0x9C9F
453
#define TAG_FLASH_PIX_VERSION           0xA000
454
#define TAG_COLOR_SPACE                 0xA001
455
37.7k
#define TAG_COMP_IMAGE_WIDTH            0xA002 /* compressed images only */
456
10.0k
#define TAG_COMP_IMAGE_HEIGHT           0xA003
457
#define TAG_RELATED_SOUND_FILE          0xA004
458
606k
#define TAG_INTEROP_IFD_POINTER         0xA005 /* IFD pointer */
459
#define TAG_FLASH_ENERGY                0xA20B
460
#define TAG_SPATIAL_FREQUENCY_RESPONSE  0xA20C
461
3.52k
#define TAG_FOCALPLANE_X_RES            0xA20E
462
#define TAG_FOCALPLANE_Y_RES            0xA20F
463
6.27k
#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
31.5k
#define TAG_NONE                    -1 /* note that -1 <> 0xFFFF */
496
384k
#define TAG_COMPUTED_VALUE          -2
497
698
#define TAG_END_OF_LIST                 0xFFFD
498
499
/* Values for TAG_PHOTOMETRIC_INTERPRETATION */
500
1.16k
#define PMI_BLACK_IS_ZERO       0
501
2.14k
#define PMI_WHITE_IS_ZERO       1
502
565
#define PMI_RGB               2
503
818
#define PMI_PALETTE_COLOR       3
504
2.71k
#define PMI_TRANSPARENCY_MASK   4
505
1.14k
#define PMI_SEPARATED           5
506
1.70k
#define PMI_YCBCR               6
507
2.01k
#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
3.69k
#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
698
  while (tag_table->Tag != TAG_END_OF_LIST) {
1341
682
    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
682
    tag_table++;
1345
682
  }
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
9.60M
{
1358
9.60M
  HashTable *ht;
1359
1360
9.60M
  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
9.60M
  ht = zend_hash_index_find_ptr(EXIF_G(tag_table_cache), (uintptr_t) tag_table);
1366
9.60M
  if (ht) {
1367
9.60M
    return ht;
1368
9.60M
  }
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
9.60M
}
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
9.47M
{
1385
9.47M
  char *desc = zend_hash_index_find_ptr(exif_get_tag_ht(tag_table), tag_num);
1386
9.47M
  if (desc) {
1387
1.17M
    return desc;
1388
1.17M
  }
1389
8.29M
  return "UndefinedTag";
1390
9.47M
}
1391
1392
static char *exif_get_tagname_key(int tag_num, char *buf, size_t buf_size, tag_table_type tag_table)
1393
128k
{
1394
128k
  char *desc = zend_hash_index_find_ptr(exif_get_tag_ht(tag_table), tag_num);
1395
128k
  if (desc) {
1396
76.4k
    return desc;
1397
76.4k
  }
1398
51.8k
  snprintf(buf, buf_size, "UndefinedTag:0x%04X", tag_num);
1399
51.8k
  return buf;
1400
128k
}
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
12.6k
{
1444
12.6k
  return (((uchar *)value)[0] << 8) | ((uchar *)value)[1];
1445
12.6k
}
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
30.8M
{
1452
30.8M
  if (motorola_intel) {
1453
14.0M
    return (((uchar *)value)[0] << 8) | ((uchar *)value)[1];
1454
16.8M
  } else {
1455
16.8M
    return (((uchar *)value)[1] << 8) | ((uchar *)value)[0];
1456
16.8M
  }
1457
30.8M
}
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
168k
{
1464
168k
  return (signed short)php_ifd_get16u(value, motorola_intel);
1465
168k
}
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
23.1M
{
1472
23.1M
  uchar *value = (uchar *) void_value;
1473
23.1M
  if (motorola_intel) {
1474
13.2M
    return  ((unsigned)value[0] << 24)
1475
13.2M
        | ((unsigned)value[1] << 16)
1476
13.2M
        | ((unsigned)value[2] << 8 )
1477
13.2M
        | ((unsigned)value[3]      );
1478
13.2M
  } else {
1479
9.99M
    return  ((unsigned)value[3] << 24)
1480
9.99M
        | ((unsigned)value[2] << 16)
1481
9.99M
        | ((unsigned)value[1] << 8 )
1482
9.99M
        | ((unsigned)value[0]      );
1483
9.99M
  }
1484
23.1M
}
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
144k
{
1491
144k
  uchar *value = (uchar *) void_value;
1492
144k
  if (motorola_intel) {
1493
696
    return ((uint64_t)value[0] << 56)
1494
696
      | ((uint64_t)value[1] << 48)
1495
696
      | ((uint64_t)value[2] << 40)
1496
696
      | ((uint64_t)value[3] << 32)
1497
696
      | ((uint64_t)value[4] << 24)
1498
696
      | ((uint64_t)value[5] << 16)
1499
696
      | ((uint64_t)value[6] << 8 )
1500
696
      | ((uint64_t)value[7]      );
1501
143k
  } else {
1502
143k
    return ((uint64_t)value[7] << 56)
1503
143k
      | ((uint64_t)value[6] << 48)
1504
143k
      | ((uint64_t)value[5] << 40)
1505
143k
      | ((uint64_t)value[4] << 32)
1506
143k
      | ((uint64_t)value[3] << 24)
1507
143k
      | ((uint64_t)value[2] << 16)
1508
143k
      | ((uint64_t)value[1] << 8 )
1509
143k
      | ((uint64_t)value[0]      );
1510
143k
  }
1511
144k
}
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
383k
{
1518
383k
  return (int) php_ifd_get32u(value, motorola_intel);
1519
383k
}
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
550k
static float php_ifd_get_float(char *data) {
1555
550k
  union { uint32_t i; float f; } u;
1556
550k
  u.i = php_ifd_get32u(data, 0);
1557
550k
  return u.f;
1558
550k
}
1559
1560
143k
static double php_ifd_get_double(char *data) {
1561
143k
  union { uint64_t i; double f; } u;
1562
143k
  u.i = php_ifd_get64u(data, 0);
1563
143k
  return u.f;
1564
143k
}
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
6.94k
{
1644
6.94k
  int     s_den;
1645
6.94k
  unsigned  u_den;
1646
1647
6.94k
  switch(format) {
1648
341
    case TAG_FMT_SBYTE:     return *(signed char *)value;
1649
658
    case TAG_FMT_BYTE:      return *(uchar *)value;
1650
1651
498
    case TAG_FMT_USHORT:    return php_ifd_get16u(value, motorola_intel);
1652
521
    case TAG_FMT_ULONG:     return php_ifd_get32u(value, motorola_intel);
1653
1654
1.41k
    case TAG_FMT_URATIONAL:
1655
1.41k
      u_den = php_ifd_get32u(4+(char *)value, motorola_intel);
1656
1.41k
      if (u_den == 0) {
1657
329
        return 0;
1658
1.08k
      } else {
1659
1.08k
        return (double)php_ifd_get32u(value, motorola_intel) / u_den;
1660
1.08k
      }
1661
1662
929
    case TAG_FMT_SRATIONAL:
1663
929
      s_den = php_ifd_get32s(4+(char *)value, motorola_intel);
1664
929
      if (s_den == 0) {
1665
244
        return 0;
1666
685
      } else {
1667
685
        return (double)php_ifd_get32s(value, motorola_intel) / s_den;
1668
685
      }
1669
1670
625
    case TAG_FMT_SSHORT:    return (signed short)php_ifd_get16u(value, motorola_intel);
1671
532
    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
478
    case TAG_FMT_SINGLE:
1675
#ifdef EXIF_DEBUG
1676
      php_error_docref(NULL, E_NOTICE, "Found value of type single");
1677
#endif
1678
478
      return (double) php_ifd_get_float(value);
1679
597
    case TAG_FMT_DOUBLE:
1680
#ifdef EXIF_DEBUG
1681
      php_error_docref(NULL, E_NOTICE, "Found value of type double");
1682
#endif
1683
597
      return php_ifd_get_double(value);
1684
6.94k
  }
1685
345
  return 0;
1686
6.94k
}
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.9k
{
1693
14.9k
  switch(format) {
1694
1.00k
    case TAG_FMT_SBYTE: return TAG_FMT_BYTE;
1695
1.04k
    case TAG_FMT_SRATIONAL: return TAG_FMT_URATIONAL;
1696
1.73k
    case TAG_FMT_SSHORT: return TAG_FMT_USHORT;
1697
1.49k
    case TAG_FMT_SLONG: return TAG_FMT_ULONG;
1698
14.9k
  }
1699
9.67k
  return format;
1700
14.9k
}
1701
/* }}} */
1702
1703
/* Use saturation for out of bounds values to avoid UB */
1704
2.41k
static size_t float_to_size_t(float x) {
1705
2.41k
  if (x < 0.0f || zend_isnan(x)) {
1706
945
    return 0;
1707
1.47k
  } else if (x > (float) SIZE_MAX) {
1708
663
    return SIZE_MAX;
1709
809
  } else {
1710
809
    return (size_t) x;
1711
809
  }
1712
2.41k
}
1713
1714
3.19k
static size_t double_to_size_t(double x) {
1715
3.19k
  if (x < 0.0 || zend_isnan(x)) {
1716
634
    return 0;
1717
2.56k
  } else if (x > (double) SIZE_MAX) {
1718
442
    return SIZE_MAX;
1719
2.12k
  } else {
1720
2.12k
    return (size_t) x;
1721
2.12k
  }
1722
3.19k
}
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
20.9k
{
1728
20.9k
  switch (format) {
1729
216
    case TAG_FMT_SBYTE:     return *(signed char *)value;
1730
3.96k
    case TAG_FMT_BYTE:      return *(uchar *)value;
1731
1732
2.09k
    case TAG_FMT_USHORT:    return php_ifd_get16u(value, motorola_intel);
1733
2.30k
    case TAG_FMT_ULONG:     return php_ifd_get32u(value, motorola_intel);
1734
1735
2.33k
    case TAG_FMT_URATIONAL: {
1736
2.33k
      unsigned u_den = php_ifd_get32u(4+(char *)value, motorola_intel);
1737
2.33k
      if (u_den == 0) {
1738
274
        return 0;
1739
2.06k
      } else {
1740
2.06k
        return php_ifd_get32u(value, motorola_intel) / u_den;
1741
2.06k
      }
1742
2.33k
    }
1743
1744
1.78k
    case TAG_FMT_SRATIONAL: {
1745
1.78k
      int s_num = php_ifd_get32s(value, motorola_intel);
1746
1.78k
      int s_den = php_ifd_get32s(4+(char *)value, motorola_intel);
1747
1.78k
      if (s_den == 0) {
1748
244
        return 0;
1749
1.54k
      } else if (s_num == INT_MIN && s_den == -1) {
1750
217
        return INT_MAX;
1751
1.32k
      } else {
1752
1.32k
        return s_num / s_den;
1753
1.32k
      }
1754
1.78k
    }
1755
1756
477
    case TAG_FMT_SSHORT:    return php_ifd_get16u(value, motorola_intel);
1757
472
    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.41k
    case TAG_FMT_SINGLE:
1761
#ifdef EXIF_DEBUG
1762
      php_error_docref(NULL, E_NOTICE, "Found value of type single");
1763
#endif
1764
2.41k
      return float_to_size_t(php_ifd_get_float(value));
1765
3.19k
    case TAG_FMT_DOUBLE:
1766
#ifdef EXIF_DEBUG
1767
      php_error_docref(NULL, E_NOTICE, "Found value of type double");
1768
#endif
1769
3.19k
      return double_to_size_t(php_ifd_get_double(value));
1770
20.9k
  }
1771
1.72k
  return 0;
1772
20.9k
}
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
37.1k
#define SECTION_FILE        0
1824
39.0k
#define SECTION_COMPUTED    1
1825
147k
#define SECTION_ANY_TAG     2
1826
223k
#define SECTION_IFD0        3
1827
742k
#define SECTION_THUMBNAIL   4
1828
595k
#define SECTION_COMMENT     5
1829
6.02k
#define SECTION_APP0        6
1830
401k
#define SECTION_EXIF        7
1831
9.60k
#define SECTION_FPIX        8
1832
26.2k
#define SECTION_GPS         9
1833
79.3k
#define SECTION_INTEROP     10
1834
10.5k
#define SECTION_APP12       11
1835
25.1k
#define SECTION_WINXP       12
1836
17.0k
#define SECTION_MAKERNOTE   13
1837
271k
#define SECTION_COUNT       14
1838
1839
6.02k
#define FOUND_FILE          (1<<SECTION_FILE)
1840
6.09k
#define FOUND_COMPUTED      (1<<SECTION_COMPUTED)
1841
134k
#define FOUND_ANY_TAG       (1<<SECTION_ANY_TAG)
1842
9.53k
#define FOUND_IFD0          (1<<SECTION_IFD0)
1843
24.2k
#define FOUND_THUMBNAIL     (1<<SECTION_THUMBNAIL)
1844
#define FOUND_COMMENT       (1<<SECTION_COMMENT)
1845
#define FOUND_APP0          (1<<SECTION_APP0)
1846
110k
#define FOUND_EXIF          (1<<SECTION_EXIF)
1847
#define FOUND_FPIX          (1<<SECTION_FPIX)
1848
5.86k
#define FOUND_GPS           (1<<SECTION_GPS)
1849
25.8k
#define FOUND_INTEROP       (1<<SECTION_INTEROP)
1850
#define FOUND_APP12         (1<<SECTION_APP12)
1851
3.92k
#define FOUND_WINXP         (1<<SECTION_WINXP)
1852
2.45k
#define FOUND_MAKERNOTE     (1<<SECTION_MAKERNOTE)
1853
1854
static const char *exif_get_sectionname(int section)
1855
101k
{
1856
101k
  switch(section) {
1857
6.02k
    case SECTION_FILE:      return "FILE";
1858
9.60k
    case SECTION_COMPUTED:  return "COMPUTED";
1859
9.90k
    case SECTION_ANY_TAG:   return "ANY_TAG";
1860
10.4k
    case SECTION_IFD0:      return "IFD0";
1861
9.07k
    case SECTION_THUMBNAIL: return "THUMBNAIL";
1862
6.29k
    case SECTION_COMMENT:   return "COMMENT";
1863
6.02k
    case SECTION_APP0:      return "APP0";
1864
7.44k
    case SECTION_EXIF:      return "EXIF";
1865
6.02k
    case SECTION_FPIX:      return "FPIX";
1866
6.08k
    case SECTION_GPS:       return "GPS";
1867
6.28k
    case SECTION_INTEROP:   return "INTEROP";
1868
6.10k
    case SECTION_APP12:     return "APP12";
1869
6.09k
    case SECTION_WINXP:     return "WINXP";
1870
6.26k
    case SECTION_MAKERNOTE: return "MAKERNOTE";
1871
101k
  }
1872
0
  return "";
1873
101k
}
1874
1875
static tag_table_type exif_get_tag_table(int section)
1876
181k
{
1877
181k
  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
5.81k
    case SECTION_IFD0:      return &tag_table_IFD[0];
1882
68.8k
    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
80.8k
    case SECTION_EXIF:      return &tag_table_IFD[0];
1886
0
    case SECTION_FPIX:      return &tag_table_IFD[0];
1887
4.88k
    case SECTION_GPS:       return &tag_table_GPS[0];
1888
17.6k
    case SECTION_INTEROP:   return &tag_table_IOP[0];
1889
0
    case SECTION_APP12:     return &tag_table_IFD[0];
1890
3.83k
    case SECTION_WINXP:     return &tag_table_IFD[0];
1891
181k
  }
1892
0
  return &tag_table_IFD[0];
1893
181k
}
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.02k
{
1901
6.02k
  int i, len, ml = 0;
1902
6.02k
  char *sections;
1903
1904
90.3k
  for(i=0; i<SECTION_COUNT; i++) {
1905
84.3k
    ml += strlen(exif_get_sectionname(i))+2;
1906
84.3k
  }
1907
6.02k
  sections = safe_emalloc(ml, 1, 1);
1908
6.02k
  sections[0] = '\0';
1909
6.02k
  len = 0;
1910
90.3k
  for(i=0; i<SECTION_COUNT; i++) {
1911
84.3k
    if (sectionlist&(1<<i)) {
1912
12.2k
      snprintf(sections+len, ml-len, "%s, ", exif_get_sectionname(i));
1913
12.2k
      len = strlen(sections);
1914
12.2k
    }
1915
84.3k
  }
1916
6.02k
  if (len>2)
1917
4.63k
    sections[len-2] = '\0';
1918
6.02k
  return sections;
1919
6.02k
}
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
19.8M
#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
9.93M
{
2023
9.93M
  va_list args;
2024
2025
9.93M
  if (ImageInfo) {
2026
9.93M
    if (++ImageInfo->num_errors > EXIF_MAX_ERRORS) {
2027
9.90M
      if (ImageInfo->num_errors == EXIF_MAX_ERRORS+1) {
2028
1.66k
        php_error_docref(docref, type,
2029
1.66k
          "Further exif parsing errors have been suppressed");
2030
1.66k
      }
2031
9.90M
      return;
2032
9.90M
    }
2033
9.93M
  }
2034
2035
9.93M
  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
26.4k
  php_verror(docref, ImageInfo && ImageInfo->FileName ? ImageInfo->FileName:"", type, format, args);
2046
26.4k
#endif
2047
26.4k
  va_end(args);
2048
26.4k
}
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
335k
static zend_always_inline bool ptr_offset_overflows(const char *ptr, size_t offset) {
2069
335k
  return UINTPTR_MAX - (uintptr_t) ptr < offset;
2070
335k
}
2071
2072
static inline void exif_offset_info_init(
2073
179k
    exif_offset_info *info, char *offset_base, char *valid_start, size_t valid_length) {
2074
179k
  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
179k
  info->offset_base = offset_base;
2079
179k
  info->valid_start = valid_start;
2080
179k
  info->valid_end = valid_start + valid_length;
2081
179k
}
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
75.7k
    const exif_offset_info *info, size_t offset, size_t length) {
2086
75.7k
  char *start, *end;
2087
75.7k
  if (ptr_offset_overflows(info->offset_base, offset)) {
2088
34
    return NULL;
2089
34
  }
2090
2091
75.7k
  start = info->offset_base + offset;
2092
75.7k
  if (ptr_offset_overflows(start, length)) {
2093
0
    return NULL;
2094
0
  }
2095
2096
75.7k
  end = start + length;
2097
75.7k
  if (start < info->valid_start || end > info->valid_end) {
2098
38.2k
    return NULL;
2099
38.2k
  }
2100
2101
37.4k
  return start;
2102
75.7k
}
2103
2104
static inline bool exif_offset_info_contains(
2105
4.03k
    const exif_offset_info *info, const char *start, size_t length) {
2106
4.03k
  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.03k
  const char *end = start + length;
2113
4.03k
  return start >= info->valid_start && end <= info->valid_end;
2114
4.03k
}
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
554k
{
2129
554k
  int count = ImageInfo->file.count;
2130
554k
  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
554k
  ImageInfo->file.list[count].type = 0xFFFF;
2138
554k
  ImageInfo->file.list[count].data = NULL;
2139
554k
  ImageInfo->file.list[count].size = 0;
2140
554k
  ImageInfo->file.count = count+1;
2141
554k
  if (!size) {
2142
0
    data = NULL;
2143
554k
  } else if (data == NULL) {
2144
554k
    data = safe_emalloc(size, 1, 0);
2145
554k
  }
2146
554k
  ImageInfo->file.list[count].type = type;
2147
554k
  ImageInfo->file.list[count].data = data;
2148
554k
  ImageInfo->file.list[count].size = size;
2149
554k
  return count;
2150
554k
}
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.02k
{
2179
6.02k
  if (ImageInfo->file.count) {
2180
559k
    for (int i = 0; i<ImageInfo->file.count; i++) {
2181
554k
      EFREE_IF(ImageInfo->file.list[i].data);
2182
554k
    }
2183
5.06k
  }
2184
6.02k
  EFREE_IF(ImageInfo->file.list);
2185
6.02k
  ImageInfo->file.count = 0;
2186
6.02k
}
2187
/* }}} */
2188
2189
544k
static image_info_data *exif_alloc_image_info_data(image_info_list *info_list) {
2190
544k
  if (info_list->count == info_list->alloc_count) {
2191
42.1k
    int new_alloc_count = info_list->alloc_count ? info_list->alloc_count * 2 : 1;
2192
42.1k
    info_list->list = safe_erealloc(
2193
42.1k
      info_list->list, new_alloc_count, sizeof(image_info_data), 0);
2194
42.1k
    info_list->alloc_count = new_alloc_count;
2195
42.1k
  }
2196
544k
  return &info_list->list[info_list->count++];
2197
544k
}
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
513k
{
2204
513k
  size_t idex;
2205
513k
  void *vptr, *vptr_end;
2206
513k
  image_info_value *info_value;
2207
513k
  image_info_data  *info_data;
2208
2209
513k
  info_data = exif_alloc_image_info_data(&image_info->info_list[section_index]);
2210
513k
  memset(info_data, 0, sizeof(image_info_data));
2211
513k
  info_data->tag    = tag;
2212
513k
  info_data->format = format;
2213
513k
  info_data->length = length;
2214
513k
  info_data->name   = estrdup(name);
2215
513k
  info_value        = &info_data->value;
2216
2217
513k
  switch (format) {
2218
388k
    case TAG_FMT_STRING:
2219
388k
      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
388k
      if (value) {
2224
388k
        length = zend_strnlen(value, length);
2225
388k
        info_value->s = estrndup(value, length);
2226
388k
        info_data->length = length;
2227
388k
      } else {
2228
0
        info_data->length = 0;
2229
0
        info_value->s = estrdup("");
2230
0
      }
2231
388k
      break;
2232
2233
1.05k
    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
1.05k
      info_data->tag = TAG_FMT_UNDEFINED;/* otherwise not freed from memory */
2240
1.05k
      ZEND_FALLTHROUGH;
2241
4.08k
    case TAG_FMT_SBYTE:
2242
62.9k
    case TAG_FMT_BYTE:
2243
    /* in contrast to strings bytes do not need to allocate buffer for NULL if length==0 */
2244
62.9k
      if (!length) {
2245
15.8k
        break;
2246
15.8k
      }
2247
47.0k
      ZEND_FALLTHROUGH;
2248
50.4k
    case TAG_FMT_UNDEFINED:
2249
50.4k
      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
50.4k
      if (value) {
2254
50.4k
        if (tag == TAG_MAKER_NOTE) {
2255
3.90k
          length = zend_strnlen(value, length);
2256
3.90k
        }
2257
2258
        /* do not recompute length here */
2259
50.4k
        info_value->s = estrndup(value, length);
2260
50.4k
        info_data->length = length;
2261
50.4k
      } else {
2262
0
        info_data->length = 0;
2263
0
        info_value->s = estrdup("");
2264
0
      }
2265
50.4k
      break;
2266
2267
3.65k
    case TAG_FMT_USHORT:
2268
6.84k
    case TAG_FMT_ULONG:
2269
11.9k
    case TAG_FMT_URATIONAL:
2270
21.6k
    case TAG_FMT_SSHORT:
2271
27.5k
    case TAG_FMT_SLONG:
2272
33.6k
    case TAG_FMT_SRATIONAL:
2273
51.2k
    case TAG_FMT_SINGLE:
2274
58.4k
    case TAG_FMT_DOUBLE:
2275
58.4k
      if (length==0) {
2276
2.09k
        break;
2277
2.09k
      }
2278
56.4k
      if (length>1) {
2279
31.9k
        info_value->list = safe_emalloc(length, sizeof(image_info_value), 0);
2280
31.9k
      } else {
2281
24.4k
        info_value = &info_data->value;
2282
24.4k
      }
2283
56.4k
      vptr_end = (char *) value + value_len;
2284
7.91M
      for (idex=0,vptr=value; idex<length; idex++,vptr=(char *) vptr + php_tiff_bytes_per_format[format]) {
2285
7.85M
        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
7.85M
        if (length>1) {
2290
7.83M
          info_value = &info_data->value.list[idex];
2291
7.83M
        }
2292
7.85M
        switch (format) {
2293
6.32M
          case TAG_FMT_USHORT:
2294
6.32M
            info_value->u = php_ifd_get16u(vptr, motorola_intel);
2295
6.32M
            break;
2296
2297
78.0k
          case TAG_FMT_ULONG:
2298
78.0k
            info_value->u = php_ifd_get32u(vptr, motorola_intel);
2299
78.0k
            break;
2300
2301
62.5k
          case TAG_FMT_URATIONAL:
2302
62.5k
            info_value->ur.num = php_ifd_get32u(vptr, motorola_intel);
2303
62.5k
            info_value->ur.den = php_ifd_get32u(4+(char *)vptr, motorola_intel);
2304
62.5k
            break;
2305
2306
153k
          case TAG_FMT_SSHORT:
2307
153k
            info_value->i = php_ifd_get16s(vptr, motorola_intel);
2308
153k
            break;
2309
2310
367k
          case TAG_FMT_SLONG:
2311
367k
            info_value->i = php_ifd_get32s(vptr, motorola_intel);
2312
367k
            break;
2313
2314
181k
          case TAG_FMT_SRATIONAL:
2315
181k
            info_value->sr.num = php_ifd_get32u(vptr, motorola_intel);
2316
181k
            info_value->sr.den = php_ifd_get32u(4+(char *)vptr, motorola_intel);
2317
181k
            break;
2318
2319
547k
          case TAG_FMT_SINGLE:
2320
#ifdef EXIF_DEBUG
2321
            php_error_docref(NULL, E_WARNING, "Found value of type single");
2322
#endif
2323
547k
            info_value->f = php_ifd_get_float(value);
2324
547k
            break;
2325
139k
          case TAG_FMT_DOUBLE:
2326
#ifdef EXIF_DEBUG
2327
            php_error_docref(NULL, E_WARNING, "Found value of type double");
2328
#endif
2329
139k
            info_value->d = php_ifd_get_double(value);
2330
139k
            break;
2331
7.85M
        }
2332
7.85M
      }
2333
513k
  }
2334
513k
  image_info->sections_found |= 1<<section_index;
2335
513k
}
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
513k
{
2343
513k
  exif_iif_add_value(image_info, section_index, name, tag, format, length, value, value_len, image_info->motorola_intel);
2344
513k
}
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
18.5k
{
2352
18.5k
  image_info_data *info_data = exif_alloc_image_info_data(&image_info->info_list[section_index]);
2353
18.5k
  info_data->tag    = TAG_NONE;
2354
18.5k
  info_data->format = TAG_FMT_SLONG;
2355
18.5k
  info_data->length = 1;
2356
18.5k
  info_data->name   = estrdup(name);
2357
18.5k
  info_data->value.i = value;
2358
18.5k
  image_info->sections_found |= 1<<section_index;
2359
18.5k
}
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
26.3k
{
2367
26.3k
  if (value) {
2368
11.8k
    image_info_data *info_data =
2369
11.8k
      exif_alloc_image_info_data(&image_info->info_list[section_index]);
2370
11.8k
    info_data->tag    = TAG_NONE;
2371
11.8k
    info_data->format = TAG_FMT_STRING;
2372
11.8k
    info_data->length = 1;
2373
11.8k
    info_data->name   = estrdup(name);
2374
11.8k
    info_data->value.s = estrdup(value);
2375
11.8k
    image_info->sections_found |= 1<<section_index;
2376
11.8k
  }
2377
26.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
521
{
2385
521
  char             *tmp;
2386
521
  va_list      arglist;
2387
2388
521
  va_start(arglist, value);
2389
521
  if (value) {
2390
521
    vspprintf(&tmp, 0, value, arglist);
2391
521
    exif_iif_add_str(image_info, section_index, name, tmp);
2392
521
    efree(tmp);
2393
521
  }
2394
521
  va_end(arglist);
2395
521
}
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
258
{
2403
258
  if (value) {
2404
258
    image_info_data *info_data =
2405
258
      exif_alloc_image_info_data(&image_info->info_list[section_index]);
2406
258
    info_data->tag    = TAG_NONE;
2407
258
    info_data->format = TAG_FMT_UNDEFINED;
2408
258
    info_data->length = length;
2409
258
    info_data->name   = estrdup(name);
2410
258
    info_data->value.s = safe_emalloc(length, 1, 1);
2411
258
    memcpy(info_data->value.s, value, length);
2412
258
    info_data->value.s[length] = 0;
2413
258
    image_info->sections_found |= 1<<section_index;
2414
258
  }
2415
258
}
2416
/* }}} */
2417
2418
/* {{{ exif_iif_free
2419
 Free memory allocated for image_info
2420
*/
2421
84.3k
static void exif_iif_free(image_info_type *image_info, int section_index) {
2422
84.3k
  void *f; /* faster */
2423
2424
84.3k
  if (image_info->info_list[section_index].count) {
2425
557k
    for (int i = 0; i < image_info->info_list[section_index].count; i++) {
2426
544k
      if ((f=image_info->info_list[section_index].list[i].name) != NULL) {
2427
544k
        efree(f);
2428
544k
      }
2429
544k
      switch(image_info->info_list[section_index].list[i].format) {
2430
3.66k
        case TAG_FMT_UNDEFINED:
2431
404k
        case TAG_FMT_STRING:
2432
407k
        case TAG_FMT_SBYTE:
2433
466k
        case TAG_FMT_BYTE:
2434
467k
        default:
2435
467k
          if ((f=image_info->info_list[section_index].list[i].value.s) != NULL) {
2436
451k
            efree(f);
2437
451k
          }
2438
467k
          break;
2439
2440
3.65k
        case TAG_FMT_USHORT:
2441
6.84k
        case TAG_FMT_ULONG:
2442
11.9k
        case TAG_FMT_URATIONAL:
2443
21.6k
        case TAG_FMT_SSHORT:
2444
46.0k
        case TAG_FMT_SLONG:
2445
52.1k
        case TAG_FMT_SRATIONAL:
2446
69.7k
        case TAG_FMT_SINGLE:
2447
77.0k
        case TAG_FMT_DOUBLE:
2448
          /* nothing to do here */
2449
77.0k
          if (image_info->info_list[section_index].list[i].length > 1) {
2450
31.9k
            if ((f=image_info->info_list[section_index].list[i].value.list) != NULL) {
2451
31.9k
              efree(f);
2452
31.9k
            }
2453
31.9k
          }
2454
77.0k
          break;
2455
544k
      }
2456
544k
    }
2457
13.2k
  }
2458
84.3k
  EFREE_IF(image_info->info_list[section_index].list);
2459
84.3k
}
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
46.5k
{
2466
46.5k
  int idx = 0, unknown = 0;
2467
2468
46.5k
  if (!image_info->info_list[section_index].count) {
2469
34.2k
    return;
2470
34.2k
  }
2471
2472
12.3k
  zval tmpi;
2473
12.3k
  if (sub_array) {
2474
5.07k
    array_init(&tmpi);
2475
7.23k
  } else {
2476
7.23k
    ZVAL_COPY_VALUE(&tmpi, value);
2477
7.23k
  }
2478
2479
340k
  for (int i = 0; i<image_info->info_list[section_index].count; i++) {
2480
327k
    image_info_data *info_data = &image_info->info_list[section_index].list[i];
2481
327k
    image_info_value *info_value = &info_data->value;
2482
327k
    const char *name = info_data->name;
2483
327k
    char uname[64];
2484
2485
327k
    if (!name) {
2486
0
      snprintf(uname, sizeof(uname), "%d", unknown++);
2487
0
      name = uname;
2488
0
    }
2489
2490
327k
    if (info_data->length == 0) {
2491
14.8k
      add_assoc_null(&tmpi, name);
2492
312k
    } else {
2493
312k
      switch (info_data->format) {
2494
947
        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
34.5k
        case TAG_FMT_BYTE:
2501
36.9k
        case TAG_FMT_SBYTE:
2502
39.4k
        case TAG_FMT_UNDEFINED:
2503
39.4k
          if (!info_value->s) {
2504
0
            add_assoc_stringl(&tmpi, name, "", 0);
2505
39.4k
          } else {
2506
39.4k
            add_assoc_stringl(&tmpi, name, info_value->s, info_data->length);
2507
39.4k
          }
2508
39.4k
          break;
2509
2510
201k
        case TAG_FMT_STRING: {
2511
201k
          const char *val = info_value->s ? info_value->s : "";
2512
201k
          if (section_index==SECTION_COMMENT) {
2513
187k
            add_index_string(&tmpi, idx++, val);
2514
187k
          } else {
2515
13.8k
            add_assoc_string(&tmpi, name, val);
2516
13.8k
          }
2517
201k
          break;
2518
36.9k
        }
2519
2520
4.55k
        case TAG_FMT_URATIONAL:
2521
9.87k
        case TAG_FMT_SRATIONAL:
2522
13.1k
        case TAG_FMT_USHORT:
2523
21.1k
        case TAG_FMT_SSHORT:
2524
38.6k
        case TAG_FMT_SINGLE:
2525
45.4k
        case TAG_FMT_DOUBLE:
2526
48.0k
        case TAG_FMT_ULONG:
2527
72.0k
        case TAG_FMT_SLONG: {
2528
          /* now the rest, first see if it becomes an array */
2529
72.0k
          zval array;
2530
72.0k
          int l = info_data->length;
2531
72.0k
          if (l > 1) {
2532
31.4k
            array_init(&array);
2533
31.4k
          }
2534
7.88M
          for (int ap = 0; ap < l; ap++) {
2535
7.81M
            char buffer[64];
2536
7.81M
            if (l>1) {
2537
7.77M
              info_value = &info_data->value.list[ap];
2538
7.77M
            }
2539
7.81M
            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
6.29M
              case TAG_FMT_USHORT:
2550
6.36M
              case TAG_FMT_ULONG:
2551
6.36M
                if (l==1) {
2552
3.24k
                  add_assoc_long(&tmpi, name, (int)info_value->u);
2553
6.36M
                } else {
2554
6.36M
                  add_index_long(&array, ap, (int)info_value->u);
2555
6.36M
                }
2556
6.36M
                break;
2557
2558
60.1k
              case TAG_FMT_URATIONAL:
2559
60.1k
                snprintf(buffer, sizeof(buffer), "%u/%u", info_value->ur.num, info_value->ur.den);
2560
60.1k
                if (l==1) {
2561
2.10k
                  add_assoc_string(&tmpi, name, buffer);
2562
58.0k
                } else {
2563
58.0k
                  add_index_string(&array, ap, buffer);
2564
58.0k
                }
2565
60.1k
                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
133k
              case TAG_FMT_SSHORT:
2577
519k
              case TAG_FMT_SLONG:
2578
519k
                if (l==1) {
2579
25.3k
                  add_assoc_long(&tmpi, name, info_value->i);
2580
493k
                } else {
2581
493k
                  add_index_long(&array, ap, info_value->i);
2582
493k
                }
2583
519k
                break;
2584
2585
180k
              case TAG_FMT_SRATIONAL:
2586
180k
                snprintf(buffer, sizeof(buffer), "%i/%i", info_value->sr.num, info_value->sr.den);
2587
180k
                if (l==1) {
2588
2.75k
                  add_assoc_string(&tmpi, name, buffer);
2589
177k
                } else {
2590
177k
                  add_index_string(&array, ap, buffer);
2591
177k
                }
2592
180k
                break;
2593
2594
547k
              case TAG_FMT_SINGLE:
2595
547k
                if (l==1) {
2596
3.57k
                  add_assoc_double(&tmpi, name, info_value->f);
2597
543k
                } else {
2598
543k
                  add_index_double(&array, ap, info_value->f);
2599
543k
                }
2600
547k
                break;
2601
2602
139k
              case TAG_FMT_DOUBLE:
2603
139k
                if (l==1) {
2604
3.53k
                  add_assoc_double(&tmpi, name, info_value->d);
2605
135k
                } else {
2606
135k
                  add_index_double(&array, ap, info_value->d);
2607
135k
                }
2608
139k
                break;
2609
7.81M
            }
2610
7.81M
          }
2611
72.0k
          if (l > 1) {
2612
31.4k
            add_assoc_zval(&tmpi, name, &array);
2613
31.4k
          }
2614
72.0k
          break;
2615
72.0k
        }
2616
312k
      }
2617
312k
    }
2618
327k
  }
2619
12.3k
  if (sub_array) {
2620
5.07k
    add_assoc_zval(value, exif_get_sectionname(section_index), &tmpi);
2621
5.07k
  }
2622
12.3k
}
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
498
#define M_SOF1  0xC1    /* N indicates which compression process   */
2635
698
#define M_SOF2  0xC2    /* Only SOF0-SOF2 are now in common use    */
2636
940
#define M_SOF3  0xC3
2637
#define M_DHT   0xC4
2638
1.14k
#define M_SOF5  0xC5    /* NB: codes C4 and CC are NOT SOF markers */
2639
1.39k
#define M_SOF6  0xC6
2640
1.46k
#define M_SOF7  0xC7
2641
#define M_JPEG  0x08    /* reserved for extensions                 */
2642
1.66k
#define M_SOF9  0xC9
2643
2.93k
#define M_SOF10 0xCA
2644
5.80k
#define M_SOF11 0xCB
2645
#define M_DAC   0xCC    /* arithmetic table                         */
2646
5.86k
#define M_SOF13 0xCD
2647
6.16k
#define M_SOF14 0xCE
2648
6.37k
#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.00k
#define M_SOI   0xD8    /* Start Of Image (beginning of datastream) */
2658
75
#define M_EOI   0xD9    /* End Of Image (end of datastream)         */
2659
33
#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.41k
#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
775
#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
3.70M
#define M_COM   0xFE    /* COMment                                  */
2696
2697
159k
#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
384k
{
2707
384k
  exif_iif_add_tag(image_info, SECTION_COMMENT, "Comment", TAG_COMPUTED_VALUE, TAG_FMT_STRING, length-2, value+2, length-2);
2708
384k
}
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
6.31k
{
2715
  /* 0xFF SOSn SectLen(2) Bits(1) Height(2) Width(2) Channels(1)  3*Channels (1)  */
2716
6.31k
  result->bits_per_sample = Data[2];
2717
6.31k
  result->height          = php_jpg_get16(Data+3);
2718
6.31k
  result->width           = php_jpg_get16(Data+5);
2719
6.31k
  result->num_components  = Data[7];
2720
6.31k
}
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
2.21k
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
2.21k
  if (byte_count) {
3004
2.21k
    (*result) = estrndup(value, byte_count); /* NULL @ byte_count!!! */
3005
2.21k
    return byte_count+1;
3006
2.21k
  }
3007
0
  return 0;
3008
2.21k
}
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
5.68k
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
5.68k
  if (byte_count) {
3019
4.27k
    (*result) = safe_emalloc(byte_count, 1, 1);
3020
4.27k
    memcpy(*result, value, byte_count);
3021
4.27k
    (*result)[byte_count] = '\0';
3022
4.27k
    return byte_count+1;
3023
4.27k
  }
3024
1.40k
  return 0;
3025
5.68k
}
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.66k
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.66k
  if ((byte_count=zend_strnlen(value, byte_count)) > 0) {
3040
2.21k
    return exif_process_undefined(result, value, byte_count);
3041
2.21k
  }
3042
455
  (*result) = estrndup("", 1); /* force empty string */
3043
455
  return byte_count+1;
3044
2.66k
}
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.42k
{
3051
4.42k
  size_t len;
3052
3053
4.42k
  *pszEncoding = NULL;
3054
  /* Copy the comment */
3055
4.42k
  if (ByteCount>=8) {
3056
3.80k
    const zend_encoding *from, *to;
3057
3.80k
    if (!memcmp(szValuePtr, "UNICODE\0", 8)) {
3058
1.09k
      char  *decode;
3059
1.09k
      *pszEncoding = estrdup(szValuePtr);
3060
1.09k
      szValuePtr = szValuePtr+8;
3061
1.09k
      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.09k
      if (ByteCount >=2 && !memcmp(szValuePtr, "\xFE\xFF", 2)) {
3066
71
        decode = "UCS-2BE";
3067
71
        szValuePtr = szValuePtr+2;
3068
71
        ByteCount -= 2;
3069
1.02k
      } else if (ByteCount >=2 && !memcmp(szValuePtr, "\xFF\xFE", 2)) {
3070
235
        decode = "UCS-2LE";
3071
235
        szValuePtr = szValuePtr+2;
3072
235
        ByteCount -= 2;
3073
791
      } else if (ImageInfo->motorola_intel) {
3074
216
        decode = ImageInfo->decode_unicode_be;
3075
575
      } else {
3076
575
        decode = ImageInfo->decode_unicode_le;
3077
575
      }
3078
1.09k
      to = zend_multibyte_fetch_encoding(ImageInfo->encode_unicode);
3079
1.09k
      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.09k
      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.09k
          from) == (size_t)-1) {
3088
1.09k
        len = exif_process_string_raw(pszInfoPtr, szValuePtr, ByteCount);
3089
1.09k
      }
3090
1.09k
      return len;
3091
2.71k
    } else if (!memcmp(szValuePtr, "ASCII\0\0\0", 8)) {
3092
385
      *pszEncoding = estrdup(szValuePtr);
3093
385
      szValuePtr = szValuePtr+8;
3094
385
      ByteCount -= 8;
3095
2.32k
    } 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
663
      *pszEncoding = estrdup(szValuePtr);
3098
663
      szValuePtr = szValuePtr+8;
3099
663
      ByteCount -= 8;
3100
      /* XXX this will fail again if encoding_converter returns on error something different than SIZE_MAX   */
3101
663
      to = zend_multibyte_fetch_encoding(ImageInfo->encode_jis);
3102
663
      from = zend_multibyte_fetch_encoding(ImageInfo->motorola_intel ? ImageInfo->decode_jis_be : ImageInfo->decode_jis_le);
3103
663
      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
663
          from) == (size_t)-1) {
3110
663
        len = exif_process_string_raw(pszInfoPtr, szValuePtr, ByteCount);
3111
663
      }
3112
663
      return len;
3113
1.66k
    } else if (!memcmp(szValuePtr, "\0\0\0\0\0\0\0\0", 8)) {
3114
      /* 8 NULL means undefined and should be ASCII... */
3115
237
      *pszEncoding = estrdup("UNDEFINED");
3116
237
      szValuePtr = szValuePtr+8;
3117
237
      ByteCount -= 8;
3118
237
    }
3119
3.80k
  }
3120
3121
  /* Olympus has this padded with trailing spaces.  Remove these first. */
3122
2.66k
  if (ByteCount>0) {
3123
2.93k
    for (int a = ByteCount-1; a && szValuePtr[a]==' '; a--) {
3124
560
      (szValuePtr)[a] = '\0';
3125
560
    }
3126
2.37k
  }
3127
3128
  /* normal text without encoding */
3129
2.66k
  exif_process_string(pszInfoPtr, szValuePtr, ByteCount);
3130
2.66k
  return strlen(*pszInfoPtr);
3131
4.42k
}
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
3.92k
{
3138
3.92k
  xp_field->tag = tag;
3139
3.92k
  xp_field->value = NULL;
3140
  /* XXX this will fail again if encoding_converter returns on error something different than SIZE_MAX   */
3141
3.92k
  if (zend_multibyte_encoding_converter(
3142
3.92k
      (unsigned char**)&xp_field->value,
3143
3.92k
      &xp_field->size,
3144
3.92k
      (const unsigned char*)szValuePtr,
3145
3.92k
      ByteCount,
3146
3.92k
      zend_multibyte_fetch_encoding(ImageInfo->encode_unicode),
3147
3.92k
      zend_multibyte_fetch_encoding(ImageInfo->motorola_intel ? ImageInfo->decode_unicode_be : ImageInfo->decode_unicode_le)
3148
3.92k
      ) == (size_t)-1) {
3149
3.92k
    xp_field->size = exif_process_string_raw(&xp_field->value, szValuePtr, ByteCount);
3150
3.92k
  }
3151
3.92k
  return xp_field->size;
3152
3.92k
}
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.74k
{
3159
4.74k
  int section_index = SECTION_MAKERNOTE;
3160
4.74k
  int NumDirEntries, old_motorola_intel;
3161
4.74k
  const maker_note_type *maker_note;
3162
4.74k
  char *dir_start;
3163
4.74k
  exif_offset_info new_info;
3164
3165
60.3k
  for (size_t i = 0; i <= sizeof(maker_note_array)/sizeof(maker_note_type); i++) {
3166
60.3k
    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.48k
      return true;
3172
1.48k
    }
3173
3174
58.8k
    maker_note = maker_note_array+i;
3175
3176
58.8k
    if (maker_note->make && (!ImageInfo->make || strcmp(maker_note->make, ImageInfo->make)))
3177
54.2k
      continue;
3178
4.62k
    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.35k
      continue;
3181
3.26k
    break;
3182
4.62k
  }
3183
3184
3.26k
  if (value_len < 2 || maker_note->offset >= value_len - 1) {
3185
    /* Do not go past the value end */
3186
735
    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
735
    return true;
3188
735
  }
3189
3190
2.52k
  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.45k
  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.45k
  ImageInfo->sections_found |= FOUND_MAKERNOTE;
3203
3204
2.45k
  old_motorola_intel = ImageInfo->motorola_intel;
3205
2.45k
  switch (maker_note->byte_order) {
3206
715
    case MN_ORDER_INTEL:
3207
715
      ImageInfo->motorola_intel = 0;
3208
715
      break;
3209
371
    case MN_ORDER_MOTOROLA:
3210
371
      ImageInfo->motorola_intel = 1;
3211
371
      break;
3212
0
    default:
3213
1.36k
    case MN_ORDER_NORMAL:
3214
1.36k
      break;
3215
2.45k
  }
3216
3217
2.45k
  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.45k
  if ((2+NumDirEntries*12) > value_len) {
3221
653
    exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Potentially invalid endianness, trying again with different endianness before imminent failure.");
3222
3223
653
    ImageInfo->motorola_intel = ImageInfo->motorola_intel == 0 ? 1 : 0;
3224
653
    NumDirEntries = php_ifd_get16u(dir_start, ImageInfo->motorola_intel);
3225
653
  }
3226
3227
2.45k
  if ((2+NumDirEntries*12) > value_len) {
3228
347
    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
347
    return false;
3230
347
  }
3231
2.10k
  if ((dir_start - value_ptr) > value_len - (2+NumDirEntries*12)) {
3232
211
    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
211
    return false;
3234
211
  }
3235
3236
1.89k
  switch (maker_note->offset_mode) {
3237
429
    case MN_OFFSET_MAKER:
3238
429
      exif_offset_info_init(&new_info, value_ptr, value_ptr, value_len);
3239
429
      info = &new_info;
3240
429
      break;
3241
0
    default:
3242
1.46k
    case MN_OFFSET_NORMAL:
3243
1.46k
      break;
3244
1.89k
  }
3245
3246
4.20k
  for (int de = 0; de < NumDirEntries; de++) {
3247
2.87k
    size_t offset = 2 + 12 * de;
3248
2.87k
    if (!exif_process_IFD_TAG(ImageInfo, dir_start + offset,
3249
2.87k
                  info, displacement, section_index, 0, maker_note->tag_table)) {
3250
562
      return false;
3251
562
    }
3252
2.87k
  }
3253
1.33k
  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.33k
  return true;
3259
1.89k
}
3260
/* }}} */
3261
3262
56.6k
#define REQUIRE_NON_EMPTY() do { \
3263
56.6k
  if (byte_count == 0) { \
3264
4.45k
    EFREE_IF(outside); \
3265
4.45k
    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)); \
3266
4.45k
    return false; \
3267
4.45k
  } \
3268
56.6k
} while (0)
3269
3270
3271
/* {{{ exif_process_IFD_TAG
3272
 * Process one of the nested IFDs directories. */
3273
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)
3274
142k
{
3275
142k
  unsigned int tag, format, components;
3276
142k
  char *value_ptr, tagname[64], cbuf[32], *outside=NULL;
3277
142k
  size_t byte_count, offset_val;
3278
142k
  int64_t byte_count_signed;
3279
3280
142k
  tag = php_ifd_get16u(dir_entry, ImageInfo->motorola_intel);
3281
142k
  format = php_ifd_get16u(dir_entry+2, ImageInfo->motorola_intel);
3282
142k
  components = php_ifd_get32u(dir_entry+4, ImageInfo->motorola_intel);
3283
3284
142k
  if (!format || format > NUM_FORMATS) {
3285
    /* (-1) catches illegal zero case as unsigned underflows to positive large. */
3286
68.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);
3287
68.6k
    format = TAG_FMT_BYTE;
3288
68.6k
  }
3289
3290
142k
  byte_count_signed = (int64_t)components * php_tiff_bytes_per_format[format];
3291
3292
142k
  if (byte_count_signed < 0 || (byte_count_signed > INT32_MAX)) {
3293
1.96k
    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));
3294
1.96k
    return false;
3295
1.96k
  }
3296
3297
140k
  byte_count = (size_t)byte_count_signed;
3298
3299
140k
  if (byte_count > 4) {
3300
    /* If its bigger than 4 bytes, the dir entry contains an offset. */
3301
74.3k
    offset_val = php_ifd_get32u(dir_entry+8, ImageInfo->motorola_intel);
3302
74.3k
    value_ptr = exif_offset_info_try_get(info, offset_val, byte_count);
3303
74.3k
    if (!value_ptr) {
3304
      /* It is important to check for IMAGE_FILETYPE_TIFF
3305
       * JPEG does not use absolute pointers instead its pointers are
3306
       * relative to the start of the TIFF header in APP1 section. */
3307
      // TODO: Shouldn't we also be taking "displacement" into account here?
3308
38.2k
      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)) {
3309
5.89k
        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);
3310
5.89k
        return false;
3311
5.89k
      }
3312
32.3k
      if (byte_count>sizeof(cbuf)) {
3313
        /* mark as outside range and get buffer */
3314
18.9k
        value_ptr = safe_emalloc(byte_count, 1, 0);
3315
18.9k
        outside = value_ptr;
3316
18.9k
      } else {
3317
        /* In most cases we only access a small range so
3318
         * it is faster to use a static buffer there
3319
         * BUT it offers also the possibility to have
3320
         * pointers read without the need to free them
3321
         * explicitly before returning. */
3322
13.4k
        memset(&cbuf, 0, sizeof(cbuf));
3323
13.4k
        value_ptr = cbuf;
3324
13.4k
      }
3325
3326
32.3k
      size_t fpos = php_stream_tell(ImageInfo->infile);
3327
32.3k
      php_stream_seek(ImageInfo->infile, displacement+offset_val, SEEK_SET);
3328
32.3k
      size_t fgot = php_stream_tell(ImageInfo->infile);
3329
32.3k
      if (fgot!=displacement+offset_val) {
3330
0
        EFREE_IF(outside);
3331
0
        exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Wrong file pointer: 0x%08X != 0x%08X", fgot, displacement+offset_val);
3332
0
        return false;
3333
0
      }
3334
32.3k
      fgot = exif_read_from_stream_file_looped(ImageInfo->infile, value_ptr, byte_count);
3335
32.3k
      php_stream_seek(ImageInfo->infile, fpos, SEEK_SET);
3336
32.3k
      if (fgot != byte_count) {
3337
30
        EFREE_IF(outside);
3338
30
        EXIF_ERRLOG_FILEEOF(ImageInfo)
3339
30
        return false;
3340
30
      }
3341
32.3k
    }
3342
74.3k
  } else {
3343
    /* 4 bytes or less and value is in the dir entry itself */
3344
65.9k
    value_ptr = dir_entry+8;
3345
    // TODO: This is dubious, but the value is only used for debugging.
3346
65.9k
    offset_val = value_ptr-info->offset_base;
3347
65.9k
  }
3348
3349
134k
  ImageInfo->sections_found |= FOUND_ANY_TAG;
3350
#ifdef EXIF_DEBUG
3351
  int dump_free;
3352
  char *dump_data = exif_dump_data(&dump_free, format, components, ImageInfo->motorola_intel, value_ptr);
3353
  exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE,
3354
    "Process tag(x%04X=%s,@0x%04X + x%04X(=%d)): %s%s %s",
3355
    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);
3356
  if (dump_free) {
3357
    efree(dump_data);
3358
  }
3359
#endif
3360
3361
  /* NB: The following code may not assume that there is at least one component!
3362
   * byte_count may be zero! */
3363
3364
134k
  if (section_index==SECTION_THUMBNAIL) {
3365
39.8k
    if (!ImageInfo->Thumbnail.data) {
3366
39.8k
      REQUIRE_NON_EMPTY();
3367
37.0k
      switch(tag) {
3368
2.67k
        case TAG_IMAGEWIDTH:
3369
3.75k
        case TAG_COMP_IMAGE_WIDTH:
3370
3.75k
          ImageInfo->Thumbnail.width = exif_convert_any_to_int(value_ptr, exif_rewrite_tag_format_to_unsigned(format), ImageInfo->motorola_intel);
3371
3.75k
          break;
3372
3373
1.61k
        case TAG_IMAGEHEIGHT:
3374
2.07k
        case TAG_COMP_IMAGE_HEIGHT:
3375
2.07k
          ImageInfo->Thumbnail.height = exif_convert_any_to_int(value_ptr, exif_rewrite_tag_format_to_unsigned(format), ImageInfo->motorola_intel);
3376
2.07k
          break;
3377
3378
2.39k
        case TAG_STRIP_OFFSETS:
3379
3.48k
        case TAG_JPEG_INTERCHANGE_FORMAT:
3380
          /* accept both formats */
3381
3.48k
          ImageInfo->Thumbnail.offset = exif_convert_any_to_int(value_ptr, exif_rewrite_tag_format_to_unsigned(format), ImageInfo->motorola_intel);
3382
3.48k
          break;
3383
3384
4.14k
        case TAG_STRIP_BYTE_COUNTS:
3385
4.14k
          if (ImageInfo->FileType == IMAGE_FILETYPE_TIFF_II || ImageInfo->FileType == IMAGE_FILETYPE_TIFF_MM) {
3386
3.94k
            ImageInfo->Thumbnail.filetype = ImageInfo->FileType;
3387
3.94k
          } else {
3388
            /* motorola is easier to read */
3389
196
            ImageInfo->Thumbnail.filetype = IMAGE_FILETYPE_TIFF_MM;
3390
196
          }
3391
4.14k
          ImageInfo->Thumbnail.size = exif_convert_any_to_int(value_ptr, exif_rewrite_tag_format_to_unsigned(format), ImageInfo->motorola_intel);
3392
4.14k
          break;
3393
3394
1.55k
        case TAG_JPEG_INTERCHANGE_FORMAT_LEN:
3395
1.55k
          if (ImageInfo->Thumbnail.filetype == IMAGE_FILETYPE_UNKNOWN) {
3396
258
            ImageInfo->Thumbnail.filetype = IMAGE_FILETYPE_JPEG;
3397
258
            ImageInfo->Thumbnail.size = exif_convert_any_to_int(value_ptr, exif_rewrite_tag_format_to_unsigned(format), ImageInfo->motorola_intel);
3398
258
          }
3399
1.55k
          break;
3400
37.0k
      }
3401
37.0k
    }
3402
94.5k
  } else {
3403
94.5k
    if (section_index==SECTION_IFD0 || section_index==SECTION_EXIF)
3404
71.9k
    switch(tag) {
3405
2.77k
      case TAG_COPYRIGHT: {
3406
2.77k
        size_t length;
3407
        /* check for "<photographer> NUL <editor> NUL" */
3408
2.77k
        if (byte_count>1 && (length=zend_strnlen(value_ptr, byte_count)) > 0) {
3409
2.27k
          if (length<byte_count-1) {
3410
            /* When there are any characters after the first NUL */
3411
1.75k
            EFREE_IF(ImageInfo->CopyrightPhotographer);
3412
1.75k
            EFREE_IF(ImageInfo->CopyrightEditor);
3413
1.75k
            EFREE_IF(ImageInfo->Copyright);
3414
1.75k
            ImageInfo->CopyrightPhotographer  = estrdup(value_ptr);
3415
1.75k
            ImageInfo->CopyrightEditor        = estrndup(value_ptr+length+1, byte_count-length-1);
3416
1.75k
            spprintf(&ImageInfo->Copyright, 0, "%s, %s", ImageInfo->CopyrightPhotographer, ImageInfo->CopyrightEditor);
3417
            /* format = TAG_FMT_UNDEFINED; this mustn't be ASCII         */
3418
            /* but we are not supposed to change this                   */
3419
            /* keep in mind that image_info does not store editor value */
3420
1.75k
          } else {
3421
525
            EFREE_IF(ImageInfo->Copyright);
3422
525
            ImageInfo->Copyright = estrndup(value_ptr, byte_count);
3423
525
          }
3424
2.27k
        }
3425
2.77k
        break;
3426
0
      }
3427
3428
4.42k
      case TAG_USERCOMMENT:
3429
4.42k
        EFREE_IF(ImageInfo->UserComment);
3430
4.42k
        ImageInfo->UserComment = NULL;
3431
4.42k
        EFREE_IF(ImageInfo->UserCommentEncoding);
3432
4.42k
        ImageInfo->UserCommentEncoding = NULL;
3433
4.42k
        ImageInfo->UserCommentLength = exif_process_user_comment(ImageInfo, &(ImageInfo->UserComment), &(ImageInfo->UserCommentEncoding), value_ptr, byte_count);
3434
4.42k
        break;
3435
3436
1.47k
      case TAG_XP_TITLE:
3437
1.76k
      case TAG_XP_COMMENTS:
3438
2.04k
      case TAG_XP_AUTHOR:
3439
3.36k
      case TAG_XP_KEYWORDS:
3440
3.92k
      case TAG_XP_SUBJECT: {
3441
3.92k
        xp_field_type *tmp_xp = (xp_field_type*)safe_erealloc(ImageInfo->xp_fields.list, (ImageInfo->xp_fields.count+1), sizeof(xp_field_type), 0);
3442
3.92k
        ImageInfo->sections_found |= FOUND_WINXP;
3443
3.92k
        ImageInfo->xp_fields.list = tmp_xp;
3444
3.92k
        ImageInfo->xp_fields.count++;
3445
3.92k
        exif_process_unicode(ImageInfo, &(ImageInfo->xp_fields.list[ImageInfo->xp_fields.count-1]), tag, value_ptr, byte_count);
3446
3.92k
        break;
3447
3.36k
      }
3448
3449
1.09k
      case TAG_FNUMBER:
3450
        /* Simplest way of expressing aperture, so I trust it the most.
3451
           (overwrite previously computed value if there is one) */
3452
1.09k
        REQUIRE_NON_EMPTY();
3453
849
        ImageInfo->ApertureFNumber = (float)exif_convert_any_format(value_ptr, format, ImageInfo->motorola_intel);
3454
849
        break;
3455
3456
668
      case TAG_APERTURE:
3457
1.39k
      case TAG_MAX_APERTURE:
3458
        /* More relevant info always comes earlier, so only use this field if we don't
3459
           have appropriate aperture information yet. */
3460
1.39k
        if (ImageInfo->ApertureFNumber == 0) {
3461
673
          REQUIRE_NON_EMPTY();
3462
439
          ImageInfo->ApertureFNumber
3463
439
            = expf(exif_convert_any_format(value_ptr, format, ImageInfo->motorola_intel)*logf(2.0)*0.5);
3464
439
        }
3465
1.16k
        break;
3466
3467
1.58k
      case TAG_SHUTTERSPEED:
3468
        /* More complicated way of expressing exposure time, so only use
3469
           this value if we don't already have it from somewhere else.
3470
           SHUTTERSPEED comes after EXPOSURE TIME
3471
          */
3472
1.58k
        if (ImageInfo->ExposureTime == 0) {
3473
967
          REQUIRE_NON_EMPTY();
3474
729
          ImageInfo->ExposureTime
3475
729
            = expf(-exif_convert_any_format(value_ptr, format, ImageInfo->motorola_intel)*logf(2.0));
3476
729
        }
3477
1.35k
        break;
3478
1.35k
      case TAG_EXPOSURETIME:
3479
279
        ImageInfo->ExposureTime = -1;
3480
279
        break;
3481
3482
1.49k
      case TAG_COMP_IMAGE_WIDTH:
3483
1.49k
        REQUIRE_NON_EMPTY();
3484
1.24k
        ImageInfo->ExifImageWidth = exif_convert_any_to_int(value_ptr, exif_rewrite_tag_format_to_unsigned(format), ImageInfo->motorola_intel);
3485
1.24k
        break;
3486
3487
3.52k
      case TAG_FOCALPLANE_X_RES:
3488
3.52k
        REQUIRE_NON_EMPTY();
3489
3.28k
        ImageInfo->FocalplaneXRes = exif_convert_any_format(value_ptr, format, ImageInfo->motorola_intel);
3490
3.28k
        break;
3491
3492
1.87k
      case TAG_SUBJECT_DISTANCE:
3493
        /* Indicates the distance the autofocus camera is focused to.
3494
           Tends to be less accurate as distance increases. */
3495
1.87k
        REQUIRE_NON_EMPTY();
3496
1.63k
        ImageInfo->Distance = (float)exif_convert_any_format(value_ptr, format, ImageInfo->motorola_intel);
3497
1.63k
        break;
3498
3499
6.27k
      case TAG_FOCALPLANE_RESOLUTION_UNIT:
3500
6.27k
        REQUIRE_NON_EMPTY();
3501
6.03k
        switch (exif_convert_any_to_int(value_ptr, format, ImageInfo->motorola_intel)) {
3502
409
          case 1: ImageInfo->FocalplaneUnits = 25.4; break; /* inch */
3503
501
          case 2:
3504
            /* According to the information I was using, 2 means meters.
3505
               But looking at the Cannon powershot's files, inches is the only
3506
               sensible value. */
3507
501
            ImageInfo->FocalplaneUnits = 25.4;
3508
501
            break;
3509
3510
385
          case 3: ImageInfo->FocalplaneUnits = 10;   break;  /* centimeter */
3511
442
          case 4: ImageInfo->FocalplaneUnits = 1;    break;  /* millimeter */
3512
243
          case 5: ImageInfo->FocalplaneUnits = .001; break;  /* micrometer */
3513
6.03k
        }
3514
6.03k
        break;
3515
3516
6.03k
      case TAG_SUB_IFD:
3517
152
        if (format==TAG_FMT_IFD) {
3518
          /* If this is called we are either in a TIFFs thumbnail or a JPEG where we cannot handle it */
3519
          /* TIFF thumbnail: our data structure cannot store a thumbnail of a thumbnail */
3520
          /* JPEG do we have the data area and what to do with it */
3521
76
          exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Skip SUB IFD");
3522
76
        }
3523
152
        break;
3524
3525
3.48k
      case TAG_MAKE:
3526
3.48k
        EFREE_IF(ImageInfo->make);
3527
3.48k
        ImageInfo->make = estrndup(value_ptr, byte_count);
3528
3.48k
        break;
3529
299
      case TAG_MODEL:
3530
299
        EFREE_IF(ImageInfo->model);
3531
299
        ImageInfo->model = estrndup(value_ptr, byte_count);
3532
299
        break;
3533
3534
4.74k
      case TAG_MAKER_NOTE:
3535
4.74k
        if (!exif_process_IFD_in_MAKERNOTE(ImageInfo, value_ptr, byte_count, info, displacement)) {
3536
1.12k
          EFREE_IF(outside);
3537
1.12k
          return false;
3538
1.12k
        }
3539
3.62k
        break;
3540
3541
3.62k
      case TAG_EXIF_IFD_POINTER:
3542
617
      case TAG_GPS_IFD_POINTER:
3543
947
      case TAG_INTEROP_IFD_POINTER:
3544
947
        if (ReadNextIFD) {
3545
947
          REQUIRE_NON_EMPTY();
3546
946
          char *Subdir_start;
3547
946
          int sub_section_index = 0;
3548
946
          switch(tag) {
3549
500
            case TAG_EXIF_IFD_POINTER:
3550
#ifdef EXIF_DEBUG
3551
              exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Found EXIF");
3552
#endif
3553
500
              ImageInfo->sections_found |= FOUND_EXIF;
3554
500
              sub_section_index = SECTION_EXIF;
3555
500
              break;
3556
117
            case TAG_GPS_IFD_POINTER:
3557
#ifdef EXIF_DEBUG
3558
              exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Found GPS");
3559
#endif
3560
117
              ImageInfo->sections_found |= FOUND_GPS;
3561
117
              sub_section_index = SECTION_GPS;
3562
117
              break;
3563
329
            case TAG_INTEROP_IFD_POINTER:
3564
#ifdef EXIF_DEBUG
3565
              exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Found INTEROPERABILITY");
3566
#endif
3567
329
              ImageInfo->sections_found |= FOUND_INTEROP;
3568
329
              sub_section_index = SECTION_INTEROP;
3569
329
              break;
3570
946
          }
3571
946
          offset_val = php_ifd_get32u(value_ptr, ImageInfo->motorola_intel);
3572
946
          Subdir_start = exif_offset_info_try_get(info, offset_val, 0);
3573
946
          if (!Subdir_start) {
3574
11
            exif_error_docref("exif_read_data#error_ifd" EXIFERR_CC, ImageInfo, E_WARNING, "Illegal IFD Pointer");
3575
11
            EFREE_IF(outside);
3576
11
            return false;
3577
11
          }
3578
935
          if (!exif_process_IFD_in_JPEG(ImageInfo, Subdir_start, info, displacement, sub_section_index, tag)) {
3579
430
            EFREE_IF(outside);
3580
430
            return false;
3581
430
          }
3582
#ifdef EXIF_DEBUG
3583
          exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Subsection %s done", exif_get_sectionname(sub_section_index));
3584
#endif
3585
935
        }
3586
71.9k
    }
3587
94.5k
  }
3588
128k
  exif_iif_add_tag(ImageInfo, section_index, exif_get_tagname_key(tag, tagname, sizeof(tagname), tag_table), tag, format, components, value_ptr, byte_count);
3589
128k
  EFREE_IF(outside);
3590
128k
  return true;
3591
134k
}
3592
/* }}} */
3593
3594
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)
3595
190k
{
3596
190k
  bool result;
3597
  /* Protect against corrupt headers */
3598
190k
  if (ImageInfo->ifd_count++ > MAX_IFD_TAGS) {
3599
964
    exif_error_docref("exif_read_data#error_ifd" EXIFERR_CC, ImageInfo, E_WARNING, "corrupt EXIF header: maximum IFD tag count reached");
3600
964
    return false;
3601
964
  }
3602
189k
  if (ImageInfo->ifd_nesting_level > MAX_IFD_NESTING_LEVEL) {
3603
47.5k
    exif_error_docref("exif_read_data#error_ifd" EXIFERR_CC, ImageInfo, E_WARNING, "corrupt EXIF header: maximum directory nesting level reached");
3604
47.5k
    return false;
3605
47.5k
  }
3606
142k
  ImageInfo->ifd_nesting_level++;
3607
142k
  result = exif_process_IFD_TAG_impl(ImageInfo, dir_entry, info, displacement, section_index, ReadNextIFD, tag_table);
3608
142k
  ImageInfo->ifd_nesting_level--;
3609
142k
  return result;
3610
189k
}
3611
3612
/* {{{ exif_process_IFD_in_JPEG
3613
 * Process one of the nested IFDs directories. */
3614
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)
3615
1.66k
{
3616
1.66k
  int de;
3617
1.66k
  int NumDirEntries;
3618
1.66k
  int NextDirOffset = 0;
3619
3620
#ifdef EXIF_DEBUG
3621
  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));
3622
#endif
3623
3624
1.66k
  ImageInfo->sections_found |= FOUND_IFD0;
3625
3626
1.66k
  if (!exif_offset_info_contains(info, dir_start, 2)) {
3627
8
    exif_error_docref("exif_read_data#error_ifd" EXIFERR_CC, ImageInfo, E_WARNING, "Illegal IFD size");
3628
8
    return false;
3629
8
  }
3630
3631
1.65k
  NumDirEntries = php_ifd_get16u(dir_start, ImageInfo->motorola_intel);
3632
3633
1.65k
  if (!exif_offset_info_contains(info, dir_start+2, NumDirEntries*12)) {
3634
8
    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);
3635
8
    return false;
3636
8
  }
3637
3638
9.97k
  for (de=0;de<NumDirEntries;de++) {
3639
8.86k
    if (!exif_process_IFD_TAG(ImageInfo, dir_start + 2 + 12 * de,
3640
8.86k
                  info, displacement, section_index, 1, exif_get_tag_table(section_index))) {
3641
543
      return false;
3642
543
    }
3643
8.86k
  }
3644
  /*
3645
   * Ignore IFD2 if it purportedly exists
3646
   */
3647
1.10k
  if (section_index == SECTION_THUMBNAIL) {
3648
403
    return true;
3649
403
  }
3650
  /*
3651
   * Hack to make it process IDF1 I hope
3652
   * There are 2 IDFs, the second one holds the keys (0x0201 and 0x0202) to the thumbnail
3653
   */
3654
705
  if (!exif_offset_info_contains(info, dir_start+2+NumDirEntries*12, 4)) {
3655
22
    exif_error_docref("exif_read_data#error_ifd" EXIFERR_CC, ImageInfo, E_WARNING, "Illegal IFD size");
3656
22
    return false;
3657
22
  }
3658
3659
683
  if (tag != TAG_EXIF_IFD_POINTER && tag != TAG_GPS_IFD_POINTER) {
3660
481
    NextDirOffset = php_ifd_get32u(dir_start+2+12*de, ImageInfo->motorola_intel);
3661
481
  }
3662
3663
683
  if (NextDirOffset) {
3664
468
    char *next_dir_start = exif_offset_info_try_get(info, NextDirOffset, 0);
3665
468
    if (!next_dir_start) {
3666
51
      exif_error_docref("exif_read_data#error_ifd" EXIFERR_CC, ImageInfo, E_WARNING, "Illegal IFD offset");
3667
51
      return false;
3668
51
    }
3669
    /* That is the IFD for the first thumbnail */
3670
#ifdef EXIF_DEBUG
3671
    exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Expect next IFD to be thumbnail");
3672
#endif
3673
417
    if (exif_process_IFD_in_JPEG(ImageInfo, next_dir_start, info, displacement, SECTION_THUMBNAIL, 0)) {
3674
#ifdef EXIF_DEBUG
3675
      exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Thumbnail size: 0x%04X", ImageInfo->Thumbnail.size);
3676
#endif
3677
403
      if (ImageInfo->Thumbnail.filetype != IMAGE_FILETYPE_UNKNOWN
3678
282
      &&  ImageInfo->Thumbnail.size
3679
200
      &&  ImageInfo->Thumbnail.offset
3680
79
      &&  ImageInfo->read_thumbnail
3681
403
      ) {
3682
0
        exif_thumbnail_extract(ImageInfo, info);
3683
0
      }
3684
403
      return true;
3685
403
    } else {
3686
14
      return false;
3687
14
    }
3688
417
  }
3689
215
  return true;
3690
683
}
3691
/* }}} */
3692
3693
/* {{{ exif_process_TIFF_in_JPEG
3694
   Process a TIFF header in a JPEG file
3695
*/
3696
static void exif_process_TIFF_in_JPEG(image_info_type *ImageInfo, char *CharBuf, size_t length, size_t displacement)
3697
2.56k
{
3698
2.56k
  unsigned exif_value_2a, offset_of_ifd;
3699
2.56k
  exif_offset_info info;
3700
3701
2.56k
  if (length < 2) {
3702
194
    exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Missing TIFF alignment marker");
3703
194
    return;
3704
194
  }
3705
3706
  /* set the thumbnail stuff to nothing so we can test to see if they get set up */
3707
2.37k
  if (memcmp(CharBuf, "II", 2) == 0) {
3708
782
    ImageInfo->motorola_intel = 0;
3709
1.59k
  } else if (memcmp(CharBuf, "MM", 2) == 0) {
3710
1.30k
    ImageInfo->motorola_intel = 1;
3711
1.30k
  } else {
3712
288
    exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Invalid TIFF alignment marker");
3713
288
    return;
3714
288
  }
3715
3716
  /* Check the next two values for correctness. */
3717
2.08k
  if (length < 8) {
3718
401
    exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Invalid TIFF start (1)");
3719
401
    return;
3720
401
  }
3721
1.68k
  exif_value_2a = php_ifd_get16u(CharBuf+2, ImageInfo->motorola_intel);
3722
1.68k
  offset_of_ifd = php_ifd_get32u(CharBuf+4, ImageInfo->motorola_intel);
3723
1.68k
  if (exif_value_2a != 0x2a || offset_of_ifd < 0x08) {
3724
563
    exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Invalid TIFF start (1)");
3725
563
    return;
3726
563
  }
3727
1.12k
  if (offset_of_ifd > length) {
3728
805
    exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Invalid IFD start");
3729
805
    return;
3730
805
  }
3731
3732
315
  ImageInfo->sections_found |= FOUND_IFD0;
3733
  /* First directory starts at offset 8. Offsets starts at 0. */
3734
315
  exif_offset_info_init(&info, CharBuf, CharBuf, length/*-14*/);
3735
315
  exif_process_IFD_in_JPEG(ImageInfo, CharBuf+offset_of_ifd, &info, displacement, SECTION_IFD0, 0);
3736
3737
#ifdef EXIF_DEBUG
3738
  exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Process TIFF in JPEG done");
3739
#endif
3740
3741
  /* Compute the CCD width, in millimeters. */
3742
315
  if (ImageInfo->FocalplaneXRes != 0) {
3743
2
    ImageInfo->CCDWidth = (float)(ImageInfo->ExifImageWidth * ImageInfo->FocalplaneUnits / ImageInfo->FocalplaneXRes);
3744
2
  }
3745
315
}
3746
/* }}} */
3747
3748
/* {{{ exif_process_APP1
3749
   Process an JPEG APP1 block marker
3750
   Describes all the drivel that most digital cameras include...
3751
*/
3752
static void exif_process_APP1(image_info_type *ImageInfo, char *CharBuf, size_t length, size_t displacement)
3753
3.22k
{
3754
  /* Check the APP1 for Exif Identifier Code */
3755
3.22k
  static const uchar ExifHeader[] = {0x45, 0x78, 0x69, 0x66, 0x00, 0x00};
3756
3.22k
  if (length <= 8 || memcmp(CharBuf+2, ExifHeader, 6)) {
3757
662
    exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Incorrect APP1 Exif Identifier Code");
3758
662
    return;
3759
662
  }
3760
2.56k
  exif_process_TIFF_in_JPEG(ImageInfo, CharBuf + 8, length - 8, displacement+8);
3761
#ifdef EXIF_DEBUG
3762
  exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Process APP1/EXIF done");
3763
#endif
3764
2.56k
}
3765
/* }}} */
3766
3767
/* {{{ exif_process_APP12
3768
   Process an JPEG APP12 block marker used by OLYMPUS
3769
*/
3770
static void exif_process_APP12(image_info_type *ImageInfo, char *buffer, size_t length)
3771
775
{
3772
775
  size_t l1, l2=0;
3773
3774
775
  if ((l1 = zend_strnlen(buffer+2, length-2)) > 0) {
3775
558
    exif_iif_add_tag(ImageInfo, SECTION_APP12, "Company", TAG_NONE, TAG_FMT_STRING, l1, buffer+2, l1);
3776
558
    if (length > 2+l1+1) {
3777
352
      l2 = zend_strnlen(buffer+2+l1+1, length-2-l1-1);
3778
352
      exif_iif_add_tag(ImageInfo, SECTION_APP12, "Info", TAG_NONE, TAG_FMT_STRING, l2, buffer+2+l1+1, l2);
3779
352
    }
3780
558
  }
3781
#ifdef EXIF_DEBUG
3782
  exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Process section APP12 with l1=%d, l2=%d done", l1, l2);
3783
#endif
3784
775
}
3785
/* }}} */
3786
3787
/* {{{ exif_scan_JPEG_header
3788
 * Parse the marker stream until SOS or EOI is seen; */
3789
static bool exif_scan_JPEG_header(image_info_type *ImageInfo)
3790
978
{
3791
978
  int sn;
3792
978
  int marker = 0, last_marker = M_PSEUDO, comment_correction=1;
3793
978
  unsigned int ll, lh;
3794
978
  uchar *Data;
3795
978
  size_t fpos, size, got, itemlen;
3796
978
  jpeg_sof_info sof_info;
3797
3798
397k
  while (true) {
3799
#ifdef EXIF_DEBUG
3800
    fpos = php_stream_tell(ImageInfo->infile);
3801
    exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Needing section %d @ 0x%08X", ImageInfo->file.count, fpos);
3802
#endif
3803
3804
    /* get marker byte, swallowing possible padding                           */
3805
    /* some software does not count the length bytes of COM section           */
3806
    /* one company doing so is very much involved in JPEG... so we accept too */
3807
397k
    if (last_marker==M_COM && comment_correction) {
3808
893
      comment_correction = 2;
3809
893
    }
3810
468k
    do {
3811
468k
      if ((marker = php_stream_getc(ImageInfo->infile)) == EOF) {
3812
404
        EXIF_ERRLOG_CORRUPT(ImageInfo)
3813
404
        return false;
3814
404
      }
3815
468k
      if (last_marker==M_COM && comment_correction>0) {
3816
1.24k
        if (marker!=0xFF) {
3817
514
          marker = 0xff;
3818
514
          comment_correction--;
3819
732
        } else  {
3820
732
          last_marker = M_PSEUDO; /* stop skipping 0 for M_COM */
3821
732
        }
3822
1.24k
      }
3823
468k
    } while (marker == 0xff);
3824
397k
    if (last_marker==M_COM && !comment_correction) {
3825
383k
      exif_error_docref("exif_read_data#error_mcom" EXIFERR_CC, ImageInfo, E_NOTICE, "Image has corrupt COM section: some software set wrong length information");
3826
383k
    }
3827
397k
    if (last_marker==M_COM && comment_correction)
3828
0
      return M_EOI; /* ah illegal: char after COM section not 0xFF */
3829
3830
397k
    fpos = php_stream_tell(ImageInfo->infile);
3831
3832
    /* safety net in case the above algorithm change dramatically, should not trigger */
3833
397k
    ZEND_ASSERT(marker != 0xff);
3834
3835
    /* Read the length of the section. */
3836
397k
    if ((lh = php_stream_getc(ImageInfo->infile)) == (unsigned int)EOF) {
3837
64
      EXIF_ERRLOG_CORRUPT(ImageInfo)
3838
64
      return false;
3839
64
    }
3840
397k
    if ((ll = php_stream_getc(ImageInfo->infile)) == (unsigned int)EOF) {
3841
75
      EXIF_ERRLOG_CORRUPT(ImageInfo)
3842
75
      return false;
3843
75
    }
3844
3845
397k
    itemlen = (lh << 8) | ll;
3846
3847
397k
    if (itemlen < 2) {
3848
#ifdef EXIF_DEBUG
3849
      exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "%s, Section length: 0x%02X%02X", EXIF_ERROR_CORRUPT, lh, ll);
3850
#else
3851
48
      EXIF_ERRLOG_CORRUPT(ImageInfo)
3852
48
#endif
3853
48
      return false;
3854
48
    }
3855
3856
397k
    sn = exif_file_sections_add(ImageInfo, marker, itemlen, NULL);
3857
397k
    Data = ImageInfo->file.list[sn].data;
3858
3859
    /* Store first two pre-read bytes. */
3860
397k
    Data[0] = (uchar)lh;
3861
397k
    Data[1] = (uchar)ll;
3862
3863
397k
    got = exif_read_from_stream_file_looped(ImageInfo->infile, (char*)(Data+2), itemlen-2); /* Read the whole section. */
3864
397k
    if (got != itemlen-2) {
3865
218
      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);
3866
218
      return false;
3867
218
    }
3868
3869
#ifdef EXIF_DEBUG
3870
    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);
3871
#endif
3872
396k
    switch(marker) {
3873
33
      case M_SOS:   /* stop before hitting compressed data  */
3874
        /* If reading entire image is requested, read the rest of the data. */
3875
33
        if (ImageInfo->read_all) {
3876
          /* Determine how much file is left. */
3877
0
          fpos = php_stream_tell(ImageInfo->infile);
3878
0
          size = ImageInfo->FileSize - fpos;
3879
0
          sn = exif_file_sections_add(ImageInfo, M_PSEUDO, size, NULL);
3880
0
          Data = ImageInfo->file.list[sn].data;
3881
0
          got = exif_read_from_stream_file_looped(ImageInfo->infile, (char*)Data, size);
3882
0
          if (got != size) {
3883
0
            EXIF_ERRLOG_FILEEOF(ImageInfo)
3884
0
            return false;
3885
0
          }
3886
0
        }
3887
33
        return true;
3888
3889
75
      case M_EOI:   /* in case it's a tables-only JPEG stream */
3890
75
        exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "No image in jpeg!");
3891
75
        return (ImageInfo->sections_found&(~FOUND_COMPUTED)) ? true : false;
3892
3893
384k
      case M_COM: /* Comment section */
3894
384k
        exif_process_COM(ImageInfo, (char *)Data, itemlen);
3895
384k
        break;
3896
3897
3.41k
      case M_EXIF:
3898
3.41k
        if (!(ImageInfo->sections_found&FOUND_IFD0)) {
3899
          /*ImageInfo->sections_found |= FOUND_EXIF;*/
3900
          /* Seen files from some 'U-lead' software with Vivitar scanner
3901
             that uses marker 31 later in the file (no clue what for!) */
3902
3.19k
          exif_process_APP1(ImageInfo, (char *)Data, itemlen, fpos);
3903
3.19k
        }
3904
3.41k
        break;
3905
3906
775
      case M_APP12:
3907
775
        exif_process_APP12(ImageInfo, (char *)Data, itemlen);
3908
775
        break;
3909
3910
3911
205
      case M_SOF0:
3912
498
      case M_SOF1:
3913
698
      case M_SOF2:
3914
940
      case M_SOF3:
3915
1.14k
      case M_SOF5:
3916
1.39k
      case M_SOF6:
3917
1.46k
      case M_SOF7:
3918
1.66k
      case M_SOF9:
3919
2.93k
      case M_SOF10:
3920
5.80k
      case M_SOF11:
3921
5.86k
      case M_SOF13:
3922
6.16k
      case M_SOF14:
3923
6.37k
      case M_SOF15:
3924
6.37k
        if ((itemlen - 2) < 6) {
3925
61
          return false;
3926
61
        }
3927
3928
6.31k
        exif_process_SOFn(Data, marker, &sof_info);
3929
6.31k
        ImageInfo->Width  = sof_info.width;
3930
6.31k
        ImageInfo->Height = sof_info.height;
3931
6.31k
        if (sof_info.num_components == 3) {
3932
1.58k
          ImageInfo->IsColor = 1;
3933
4.72k
        } else {
3934
4.72k
          ImageInfo->IsColor = 0;
3935
4.72k
        }
3936
6.31k
        break;
3937
1.76k
      default:
3938
        /* skip any other marker silently. */
3939
1.76k
        break;
3940
396k
    }
3941
3942
    /* keep track of last marker */
3943
396k
    last_marker = marker;
3944
396k
  }
3945
#ifdef EXIF_DEBUG
3946
  exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Done");
3947
#endif
3948
0
  return true;
3949
978
}
3950
/* }}} */
3951
3952
/* {{{ exif_scan_thumbnail
3953
 * scan JPEG in thumbnail (memory) */
3954
static bool exif_scan_thumbnail(image_info_type *ImageInfo)
3955
311
{
3956
311
  uchar           c, *data = (uchar*)ImageInfo->Thumbnail.data;
3957
311
  int             n, marker;
3958
311
  size_t          length=2, pos=0;
3959
311
  jpeg_sof_info   sof_info;
3960
3961
311
  if (!data || ImageInfo->Thumbnail.size < 4) {
3962
311
    return false; /* nothing to do here */
3963
311
  }
3964
0
  if (memcmp(data, "\xFF\xD8\xFF", 3)) {
3965
0
    if (!ImageInfo->Thumbnail.width && !ImageInfo->Thumbnail.height) {
3966
0
      exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Thumbnail is not a JPEG image");
3967
0
    }
3968
0
    return false;
3969
0
  }
3970
0
  for (;;) {
3971
0
    pos += length;
3972
0
    if (pos>=ImageInfo->Thumbnail.size)
3973
0
      return false;
3974
0
    c = data[pos++];
3975
0
    if (pos>=ImageInfo->Thumbnail.size)
3976
0
      return false;
3977
0
    if (c != 0xFF) {
3978
0
      return false;
3979
0
    }
3980
0
    n = 8;
3981
0
    while ((c = data[pos++]) == 0xFF && n--) {
3982
0
      if (pos+3>=ImageInfo->Thumbnail.size)
3983
0
        return false;
3984
      /* +3 = pos++ of next check when reaching marker + 2 bytes for length */
3985
0
    }
3986
0
    if (c == 0xFF)
3987
0
      return false;
3988
0
    marker = c;
3989
0
    if (pos>=ImageInfo->Thumbnail.size)
3990
0
      return false;
3991
0
    length = php_jpg_get16(data+pos);
3992
0
    if (length > ImageInfo->Thumbnail.size || pos >= ImageInfo->Thumbnail.size - length) {
3993
0
      return false;
3994
0
    }
3995
#ifdef EXIF_DEBUG
3996
    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);
3997
#endif
3998
0
    switch (marker) {
3999
0
      case M_SOF0:
4000
0
      case M_SOF1:
4001
0
      case M_SOF2:
4002
0
      case M_SOF3:
4003
0
      case M_SOF5:
4004
0
      case M_SOF6:
4005
0
      case M_SOF7:
4006
0
      case M_SOF9:
4007
0
      case M_SOF10:
4008
0
      case M_SOF11:
4009
0
      case M_SOF13:
4010
0
      case M_SOF14:
4011
0
      case M_SOF15:
4012
        /* handle SOFn block */
4013
0
        if (length < 8 || ImageInfo->Thumbnail.size - 8 < pos) {
4014
          /* exif_process_SOFn needs 8 bytes */
4015
0
          return false;
4016
0
        }
4017
0
        exif_process_SOFn(data+pos, marker, &sof_info);
4018
0
        ImageInfo->Thumbnail.height   = sof_info.height;
4019
0
        ImageInfo->Thumbnail.width    = sof_info.width;
4020
#ifdef EXIF_DEBUG
4021
        exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Thumbnail: size: %d * %d", sof_info.width, sof_info.height);
4022
#endif
4023
0
        return true;
4024
4025
0
      case M_SOS:
4026
0
      case M_EOI:
4027
0
        exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Could not compute size of thumbnail");
4028
0
        return false;
4029
0
        break;
4030
4031
0
      default:
4032
        /* just skip */
4033
0
        break;
4034
0
    }
4035
0
  }
4036
4037
0
  exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Could not compute size of thumbnail");
4038
0
  return false;
4039
0
}
4040
/* }}} */
4041
4042
/* {{{ exif_process_IFD_in_TIFF
4043
 * Parse the TIFF header; */
4044
static bool exif_process_IFD_in_TIFF_impl(image_info_type *ImageInfo, size_t dir_offset, int section_index)
4045
169k
{
4046
169k
  int i, sn, num_entries, sub_section_index = 0;
4047
169k
  unsigned char *dir_entry;
4048
169k
  size_t ifd_size, dir_size, next_offset, entry_length, entry_value=0;
4049
169k
  int entry_tag , entry_type;
4050
169k
  tag_table_type tag_table = exif_get_tag_table(section_index);
4051
4052
169k
  if (ImageInfo->FileSize >= 2 && ImageInfo->FileSize - 2 >= dir_offset) {
4053
157k
    sn = exif_file_sections_add(ImageInfo, M_PSEUDO, 2, NULL);
4054
#ifdef EXIF_DEBUG
4055
    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);
4056
#endif
4057
157k
    php_stream_seek(ImageInfo->infile, dir_offset, SEEK_SET); /* we do not know the order of sections */
4058
157k
    if (UNEXPECTED(exif_read_from_stream_file_looped(ImageInfo->infile, (char*)ImageInfo->file.list[sn].data, 2) != 2)) {
4059
0
      return false;
4060
0
    }
4061
157k
    num_entries = php_ifd_get16u(ImageInfo->file.list[sn].data, ImageInfo->motorola_intel);
4062
157k
    dir_size = 2/*num dir entries*/ +12/*length of entry*/*(size_t)num_entries +4/* offset to next ifd (points to thumbnail or NULL)*/;
4063
157k
    if (ImageInfo->FileSize >= dir_size && ImageInfo->FileSize - dir_size >= dir_offset) {
4064
#ifdef EXIF_DEBUG
4065
      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);
4066
#endif
4067
155k
      if (exif_file_sections_realloc(ImageInfo, sn, dir_size)) {
4068
0
        return false;
4069
0
      }
4070
155k
      if (UNEXPECTED(exif_read_from_stream_file_looped(ImageInfo->infile, (char*)(ImageInfo->file.list[sn].data+2), dir_size-2) != dir_size - 2)) {
4071
0
        return false;
4072
0
      }
4073
155k
      next_offset = php_ifd_get32u(ImageInfo->file.list[sn].data + dir_size - 4, ImageInfo->motorola_intel);
4074
#ifdef EXIF_DEBUG
4075
      exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Read from TIFF done, next offset x%04X", next_offset);
4076
#endif
4077
      /* now we have the directory we can look how long it should be */
4078
155k
      ifd_size = dir_size;
4079
11.7M
      for(i=0;i<num_entries;i++) {
4080
11.6M
        dir_entry    = ImageInfo->file.list[sn].data+2+i*12;
4081
11.6M
        entry_tag    = php_ifd_get16u(dir_entry+0, ImageInfo->motorola_intel);
4082
11.6M
        entry_type   = php_ifd_get16u(dir_entry+2, ImageInfo->motorola_intel);
4083
11.6M
        if (entry_type > NUM_FORMATS) {
4084
9.39M
          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);
4085
          /* Since this is repeated in exif_process_IFD_TAG make it a notice here */
4086
          /* and make it a warning in the exif_process_IFD_TAG which is called    */
4087
          /* elsewhere. */
4088
9.39M
          entry_type = TAG_FMT_BYTE;
4089
          /*The next line would break the image on writeback: */
4090
          /* php_ifd_set16u(dir_entry+2, entry_type, ImageInfo->motorola_intel);*/
4091
9.39M
        }
4092
11.6M
        entry_length = php_ifd_get32u(dir_entry+4, ImageInfo->motorola_intel) * php_tiff_bytes_per_format[entry_type];
4093
11.6M
        if (entry_length <= 4) {
4094
2.09M
          switch(entry_type) {
4095
6.89k
            case TAG_FMT_USHORT:
4096
6.89k
              entry_value  = php_ifd_get16u(dir_entry+8, ImageInfo->motorola_intel);
4097
6.89k
              break;
4098
14.8k
            case TAG_FMT_SSHORT:
4099
14.8k
              entry_value  = php_ifd_get16s(dir_entry+8, ImageInfo->motorola_intel);
4100
14.8k
              break;
4101
6.91k
            case TAG_FMT_ULONG:
4102
6.91k
              entry_value  = php_ifd_get32u(dir_entry+8, ImageInfo->motorola_intel);
4103
6.91k
              break;
4104
9.71k
            case TAG_FMT_SLONG:
4105
9.71k
              entry_value  = php_ifd_get32s(dir_entry+8, ImageInfo->motorola_intel);
4106
9.71k
              break;
4107
2.09M
          }
4108
2.09M
          switch(entry_tag) {
4109
28.3k
            case TAG_IMAGEWIDTH:
4110
32.5k
            case TAG_COMP_IMAGE_WIDTH:
4111
32.5k
              ImageInfo->Width  = entry_value;
4112
32.5k
              break;
4113
6.45k
            case TAG_IMAGEHEIGHT:
4114
7.98k
            case TAG_COMP_IMAGE_HEIGHT:
4115
7.98k
              ImageInfo->Height = entry_value;
4116
7.98k
              break;
4117
4.97k
            case TAG_PHOTOMETRIC_INTERPRETATION:
4118
4.97k
              switch (entry_value) {
4119
1.16k
                case PMI_BLACK_IS_ZERO:
4120
2.14k
                case PMI_WHITE_IS_ZERO:
4121
2.71k
                case PMI_TRANSPARENCY_MASK:
4122
2.71k
                  ImageInfo->IsColor = 0;
4123
2.71k
                  break;
4124
565
                case PMI_RGB:
4125
818
                case PMI_PALETTE_COLOR:
4126
1.14k
                case PMI_SEPARATED:
4127
1.70k
                case PMI_YCBCR:
4128
2.01k
                case PMI_CIELAB:
4129
2.01k
                  ImageInfo->IsColor = 1;
4130
2.01k
                  break;
4131
4.97k
              }
4132
4.97k
              break;
4133
2.09M
          }
4134
9.51M
        } else {
4135
9.51M
          size_t entry_offset = php_ifd_get32u(dir_entry+8, ImageInfo->motorola_intel);
4136
          /* if entry needs expanding ifd cache and entry is at end of current ifd cache. */
4137
          /* otherwise there may be huge holes between two entries */
4138
9.51M
          if (entry_offset + entry_length > dir_offset + ifd_size
4139
9.30M
            && entry_offset == dir_offset + ifd_size) {
4140
6.16k
            ifd_size = entry_offset + entry_length - dir_offset;
4141
#ifdef EXIF_DEBUG
4142
            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);
4143
#endif
4144
6.16k
          }
4145
9.51M
        }
4146
11.6M
      }
4147
155k
      if (ImageInfo->FileSize >= ImageInfo->file.list[sn].size && ImageInfo->FileSize - ImageInfo->file.list[sn].size >= dir_offset) {
4148
155k
        if (ifd_size > dir_size) {
4149
6.16k
          if (ImageInfo->FileSize < ifd_size || dir_offset > ImageInfo->FileSize - ifd_size) {
4150
727
            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);
4151
727
            return false;
4152
727
          }
4153
5.43k
          if (exif_file_sections_realloc(ImageInfo, sn, ifd_size)) {
4154
0
            return false;
4155
0
          }
4156
          /* read values not stored in directory itself */
4157
#ifdef EXIF_DEBUG
4158
          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);
4159
#endif
4160
5.43k
          exif_read_from_stream_file_looped(ImageInfo->infile, (char*)(ImageInfo->file.list[sn].data+dir_size), ifd_size-dir_size);
4161
#ifdef EXIF_DEBUG
4162
          exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Read from TIFF, done");
4163
#endif
4164
5.43k
        }
4165
        /* now process the tags */
4166
437k
        for(i=0;i<num_entries;i++) {
4167
344k
          dir_entry    = ImageInfo->file.list[sn].data+2+i*12;
4168
344k
          entry_tag    = php_ifd_get16u(dir_entry+0, ImageInfo->motorola_intel);
4169
344k
          entry_type   = php_ifd_get16u(dir_entry+2, ImageInfo->motorola_intel);
4170
          /*entry_length = php_ifd_get32u(dir_entry+4, ImageInfo->motorola_intel);*/
4171
344k
          if (entry_tag == TAG_EXIF_IFD_POINTER ||
4172
234k
            entry_tag == TAG_INTEROP_IFD_POINTER ||
4173
209k
            entry_tag == TAG_GPS_IFD_POINTER ||
4174
203k
            entry_tag == TAG_SUB_IFD
4175
344k
          ) {
4176
165k
            switch(entry_tag) {
4177
110k
              case TAG_EXIF_IFD_POINTER:
4178
110k
                ImageInfo->sections_found |= FOUND_EXIF;
4179
110k
                sub_section_index = SECTION_EXIF;
4180
110k
                break;
4181
5.75k
              case TAG_GPS_IFD_POINTER:
4182
5.75k
                ImageInfo->sections_found |= FOUND_GPS;
4183
5.75k
                sub_section_index = SECTION_GPS;
4184
5.75k
                break;
4185
25.5k
              case TAG_INTEROP_IFD_POINTER:
4186
25.5k
                ImageInfo->sections_found |= FOUND_INTEROP;
4187
25.5k
                sub_section_index = SECTION_INTEROP;
4188
25.5k
                break;
4189
24.2k
              case TAG_SUB_IFD:
4190
24.2k
                ImageInfo->sections_found |= FOUND_THUMBNAIL;
4191
24.2k
                sub_section_index = SECTION_THUMBNAIL;
4192
24.2k
                break;
4193
165k
            }
4194
165k
            size_t entry_offset = php_ifd_get32u(dir_entry+8, ImageInfo->motorola_intel);
4195
#ifdef EXIF_DEBUG
4196
            exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Next IFD: %s @0x%04X", exif_get_sectionname(sub_section_index), entry_offset);
4197
#endif
4198
165k
            exif_process_IFD_in_TIFF(ImageInfo, entry_offset, sub_section_index);
4199
165k
            if (section_index!=SECTION_THUMBNAIL && entry_tag==TAG_SUB_IFD) {
4200
3.58k
              if (ImageInfo->Thumbnail.filetype != IMAGE_FILETYPE_UNKNOWN
4201
1.25k
              &&  ImageInfo->Thumbnail.size
4202
882
              &&  ImageInfo->Thumbnail.offset
4203
433
              &&  ImageInfo->read_thumbnail
4204
3.58k
              ) {
4205
#ifdef EXIF_DEBUG
4206
                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);
4207
#endif
4208
0
                if (!ImageInfo->Thumbnail.data) {
4209
0
                  ImageInfo->Thumbnail.data = safe_emalloc(ImageInfo->Thumbnail.size, 1, 0);
4210
0
                  php_stream_seek(ImageInfo->infile, ImageInfo->Thumbnail.offset, SEEK_SET);
4211
0
                  size_t fgot = exif_read_from_stream_file_looped(ImageInfo->infile, ImageInfo->Thumbnail.data, ImageInfo->Thumbnail.size);
4212
0
                  if (fgot != ImageInfo->Thumbnail.size) {
4213
0
                    EXIF_ERRLOG_THUMBEOF(ImageInfo)
4214
0
                    efree(ImageInfo->Thumbnail.data);
4215
4216
0
                    ImageInfo->Thumbnail.data = NULL;
4217
0
                  } else {
4218
0
                    exif_thumbnail_build(ImageInfo);
4219
0
                  }
4220
0
                }
4221
0
              }
4222
3.58k
            }
4223
#ifdef EXIF_DEBUG
4224
            exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Next IFD: %s done", exif_get_sectionname(sub_section_index));
4225
#endif
4226
179k
          } else {
4227
179k
            exif_offset_info info;
4228
179k
            exif_offset_info_init(&info,
4229
179k
              (char *) (ImageInfo->file.list[sn].data - dir_offset),
4230
179k
              (char *) ImageInfo->file.list[sn].data, ifd_size);
4231
179k
            if (!exif_process_IFD_TAG(ImageInfo, (char*)dir_entry, &info,
4232
179k
                          0, section_index, 0, tag_table)) {
4233
61.3k
              return false;
4234
61.3k
            }
4235
179k
          }
4236
344k
        }
4237
        /* If we had a thumbnail in a SUB_IFD we have ANOTHER image in NEXT IFD */
4238
93.0k
        if (next_offset && section_index != SECTION_THUMBNAIL) {
4239
          /* this should be a thumbnail IFD */
4240
          /* the thumbnail itself is stored at Tag=StripOffsets */
4241
#ifdef EXIF_DEBUG
4242
          exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Read next IFD (THUMBNAIL) at x%04X", next_offset);
4243
#endif
4244
55.5k
          exif_process_IFD_in_TIFF(ImageInfo, next_offset, SECTION_THUMBNAIL);
4245
#ifdef EXIF_DEBUG
4246
          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);
4247
#endif
4248
55.5k
          if (!ImageInfo->Thumbnail.data && ImageInfo->Thumbnail.offset && ImageInfo->Thumbnail.size && ImageInfo->read_thumbnail) {
4249
0
            ImageInfo->Thumbnail.data = safe_emalloc(ImageInfo->Thumbnail.size, 1, 0);
4250
0
            php_stream_seek(ImageInfo->infile, ImageInfo->Thumbnail.offset, SEEK_SET);
4251
0
            size_t fgot = exif_read_from_stream_file_looped(ImageInfo->infile, ImageInfo->Thumbnail.data, ImageInfo->Thumbnail.size);
4252
0
            if (fgot != ImageInfo->Thumbnail.size) {
4253
0
              EXIF_ERRLOG_THUMBEOF(ImageInfo)
4254
0
              efree(ImageInfo->Thumbnail.data);
4255
0
              ImageInfo->Thumbnail.data = NULL;
4256
0
            } else {
4257
0
              exif_thumbnail_build(ImageInfo);
4258
0
            }
4259
0
          }
4260
#ifdef EXIF_DEBUG
4261
          exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Read next IFD (THUMBNAIL) done");
4262
#endif
4263
55.5k
        }
4264
93.0k
        return true;
4265
154k
      } else {
4266
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);
4267
0
        return false;
4268
0
      }
4269
155k
    } else {
4270
2.32k
      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);
4271
2.32k
      return false;
4272
2.32k
    }
4273
157k
  } else {
4274
11.8k
    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);
4275
11.8k
    return false;
4276
11.8k
  }
4277
169k
}
4278
/* }}} */
4279
4280
static bool exif_process_IFD_in_TIFF(image_info_type *ImageInfo, size_t dir_offset, int section_index)
4281
225k
{
4282
225k
  bool result;
4283
225k
  if (ImageInfo->ifd_count++ > MAX_IFD_TAGS) {
4284
823
    return false;
4285
823
  }
4286
224k
  if (ImageInfo->ifd_nesting_level > MAX_IFD_NESTING_LEVEL) {
4287
55.2k
    return false;
4288
55.2k
  }
4289
169k
  ImageInfo->ifd_nesting_level++;
4290
169k
  result = exif_process_IFD_in_TIFF_impl(ImageInfo, dir_offset, section_index);
4291
169k
  ImageInfo->ifd_nesting_level--;
4292
169k
  return result;
4293
224k
}
4294
4295
/* Returns the size of the header, which must be smaller than the size of the box. */
4296
static int exif_isobmff_parse_box(unsigned char *buf, isobmff_box_type *box)
4297
3.00k
{
4298
3.00k
  box->size = php_ifd_get32u(buf, 1);
4299
3.00k
  buf += 4;
4300
3.00k
  box->type = php_ifd_get32u(buf, 1);
4301
3.00k
  if (box->size != 1) {
4302
2.30k
    return 8;
4303
2.30k
  }
4304
696
  buf += 4;
4305
696
  box->size = php_ifd_get64u(buf, 1);
4306
696
  return 16;
4307
3.00k
}
4308
4309
static void exif_isobmff_parse_meta(unsigned char *data, unsigned char *end, isobmff_item_pos_type *pos)
4310
557
{
4311
557
  isobmff_box_type box, item;
4312
557
  unsigned char *p;
4313
557
  int header_size, exif_id = -1, version, item_count, i;
4314
4315
557
  size_t remain;
4316
1.80k
#define CHECK(n) do { \
4317
1.80k
  if (remain < (n)) { \
4318
143
    return; \
4319
143
  } \
4320
1.80k
} while (0)
4321
1.32k
#define ADVANCE(n) do { \
4322
1.32k
  CHECK(n); \
4323
1.32k
  remain -= (n); \
4324
1.22k
  p += (n); \
4325
1.22k
} while (0)
4326
4327
557
  unsigned char *box_offset = data + 4;
4328
1.56k
  while (box_offset < end - 16) {
4329
1.56k
    header_size = exif_isobmff_parse_box(box_offset, &box);
4330
1.56k
    if (box.size < header_size) {
4331
7
      return;
4332
7
    }
4333
1.56k
    p = box_offset;
4334
1.56k
    remain = end - p;
4335
4336
1.56k
    if (box.type == FOURCC("iinf")) {
4337
376
      ADVANCE(header_size + 4);
4338
375
      version = p[-4];
4339
375
      if (version < 2) {
4340
306
        ADVANCE(2);
4341
305
        item_count = php_ifd_get16u(p - 2, 1);
4342
305
      } else {
4343
69
        ADVANCE(4);
4344
66
        item_count = php_ifd_get32u(p - 4, 1);
4345
66
      }
4346
538
      for (i = 0; i < item_count && p < end - 20; i++) {
4347
482
        header_size = exif_isobmff_parse_box(p, &item);
4348
482
        if (item.size < header_size) {
4349
2
          return;
4350
2
        }
4351
480
        CHECK(header_size + 12);
4352
437
        if (!memcmp(p + header_size + 8, "Exif", 4)) {
4353
179
          exif_id = php_ifd_get16u(p + header_size + 4, 1);
4354
179
          break;
4355
179
        }
4356
258
        ADVANCE(item.size);
4357
258
      }
4358
235
      if (exif_id < 0) {
4359
30
        break;
4360
30
      }
4361
235
    }
4362
1.18k
    else if (box.type == FOURCC("iloc")) {
4363
160
      ADVANCE(header_size + 6);
4364
159
      version = p[-6];
4365
159
      if (version < 2) {
4366
7
        ADVANCE(2);
4367
6
        item_count = php_ifd_get16u(p - 2, 1);
4368
152
      } else {
4369
152
        ADVANCE(4);
4370
150
        item_count = php_ifd_get32u(p - 4, 1);
4371
150
      }
4372
2.06k
      for (i = 0; i < item_count && p < end - 16; i++, p += 16) {
4373
2.02k
        if (php_ifd_get16u(p, 1) == exif_id) {
4374
122
          pos->offset = php_ifd_get32u(p + 8, 1);
4375
122
          pos->size = php_ifd_get32u(p + 12, 1);
4376
122
          break;
4377
122
        }
4378
2.02k
      }
4379
156
      break;
4380
159
    }
4381
4382
1.23k
    if (end - 16 - box_offset <= box.size) {
4383
219
      break;
4384
219
    }
4385
1.01k
    box_offset += box.size;
4386
1.01k
  }
4387
4388
557
#undef ADVANCE
4389
557
#undef CHECK
4390
557
}
4391
4392
static bool exif_scan_HEIF_header(image_info_type *ImageInfo, unsigned char *buf)
4393
841
{
4394
841
  isobmff_box_type box;
4395
841
  isobmff_item_pos_type pos;
4396
841
  unsigned char *data;
4397
841
  uint64_t limit;
4398
841
  int box_header_size, remain;
4399
841
  bool ret = false;
4400
4401
1.14k
  for (size_t offset = php_ifd_get32u(buf, 1); ImageInfo->FileSize - 16 > offset; offset += box.size) {
4402
953
    if ((php_stream_seek(ImageInfo->infile, offset, SEEK_SET) < 0) ||
4403
953
      (exif_read_from_stream_file_looped(ImageInfo->infile, (char*)buf, 16) != 16)) {
4404
0
      break;
4405
0
    }
4406
953
    box_header_size = exif_isobmff_parse_box(buf, &box);
4407
953
    if (box.size < box_header_size) {
4408
7
      break;
4409
7
    }
4410
946
    if (box.type == FOURCC("meta")) {
4411
642
      limit = box.size - box_header_size;
4412
642
      if (limit < 36) {
4413
7
        break;
4414
7
      }
4415
635
      data = (unsigned char *)emalloc(limit);
4416
635
      remain = 16 - box_header_size;
4417
635
      if (remain) {
4418
634
        memcpy(data, buf + box_header_size, remain);
4419
634
      }
4420
635
      memset(&pos, 0, sizeof(pos));
4421
635
      if (exif_read_from_stream_file_looped(ImageInfo->infile, (char*)(data + remain), limit - remain) == limit - remain) {
4422
557
        exif_isobmff_parse_meta(data, data + limit, &pos);
4423
557
      }
4424
635
      if ((pos.size) &&
4425
121
        (pos.size < ImageInfo->FileSize) &&
4426
65
        (ImageInfo->FileSize - pos.size >= pos.offset) &&
4427
33
        (php_stream_seek(ImageInfo->infile, pos.offset + 2, SEEK_SET) >= 0)) {
4428
33
        if (limit >= pos.size - 2) {
4429
23
          limit = pos.size - 2;
4430
23
        } else {
4431
10
          limit = pos.size - 2;
4432
10
          efree(data);
4433
10
          data = (unsigned char *)emalloc(limit);
4434
10
        }
4435
33
        if (exif_read_from_stream_file_looped(ImageInfo->infile, (char*)data, limit) == limit) {
4436
33
          exif_process_APP1(ImageInfo, (char*)data, limit, pos.offset + 2);
4437
33
          ret = true;
4438
33
        }
4439
33
      }
4440
635
      efree(data);
4441
635
      break;
4442
642
    }
4443
304
    if (offset + box.size < offset) {
4444
2
      break;
4445
2
    }
4446
304
  }
4447
4448
841
  return ret;
4449
841
}
4450
4451
/* {{{ exif_scan_FILE_header
4452
 * Parse the marker stream until SOS or EOI is seen; */
4453
static bool exif_scan_FILE_header(image_info_type *ImageInfo)
4454
6.02k
{
4455
6.02k
  unsigned char file_header[16];
4456
4457
6.02k
  ImageInfo->FileType = IMAGE_FILETYPE_UNKNOWN;
4458
4459
6.02k
  if (UNEXPECTED(ImageInfo->FileSize < 2)) {
4460
2
    exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "File too small (%d)", ImageInfo->FileSize);
4461
2
    return false;
4462
2
  }
4463
4464
6.02k
  php_stream_seek(ImageInfo->infile, 0, SEEK_SET);
4465
6.02k
  if (exif_read_from_stream_file_looped(ImageInfo->infile, (char*)file_header, 2) != 2) {
4466
0
    return false;
4467
0
  }
4468
4469
6.02k
  if ((file_header[0]==0xff) && (file_header[1]==M_SOI)) {
4470
978
    ImageInfo->FileType = IMAGE_FILETYPE_JPEG;
4471
978
    if (exif_scan_JPEG_header(ImageInfo)) {
4472
107
      return true;
4473
871
    } else {
4474
871
      exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Invalid JPEG file");
4475
871
      return false;
4476
871
    }
4477
5.04k
  } else if (ImageInfo->FileSize >= 8) {
4478
5.01k
    if (exif_read_from_stream_file_looped(ImageInfo->infile, (char*)(file_header+2), 6) != 6) {
4479
0
      return false;
4480
0
    }
4481
4482
5.01k
    if (!memcmp(file_header, "II\x2A\x00", 4)) {
4483
2.95k
      ImageInfo->FileType = IMAGE_FILETYPE_TIFF_II;
4484
2.95k
      ImageInfo->motorola_intel = 0;
4485
#ifdef EXIF_DEBUG
4486
      exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "File has TIFF/II format");
4487
#endif
4488
2.95k
      ImageInfo->sections_found |= FOUND_IFD0;
4489
2.95k
      if (exif_process_IFD_in_TIFF(
4490
2.95k
        ImageInfo,
4491
2.95k
        php_ifd_get32u(file_header + 4, ImageInfo->motorola_intel),
4492
2.95k
        SECTION_IFD0
4493
2.95k
      )) {
4494
2.47k
        return true;
4495
2.47k
      } else {
4496
481
        exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Invalid TIFF file");
4497
481
        return false;
4498
481
      }
4499
2.95k
    } else if (!memcmp(file_header, "MM\x00\x2a", 4)) {
4500
1.18k
      ImageInfo->FileType = IMAGE_FILETYPE_TIFF_MM;
4501
1.18k
      ImageInfo->motorola_intel = 1;
4502
#ifdef EXIF_DEBUG
4503
      exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "File has TIFF/MM format");
4504
#endif
4505
1.18k
      ImageInfo->sections_found |= FOUND_IFD0;
4506
1.18k
      if (exif_process_IFD_in_TIFF(
4507
1.18k
        ImageInfo,
4508
1.18k
        php_ifd_get32u(file_header + 4, ImageInfo->motorola_intel),
4509
1.18k
        SECTION_IFD0
4510
1.18k
      )) {
4511
971
        return true;
4512
971
      } else {
4513
213
        exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Invalid TIFF file");
4514
213
        return false;
4515
213
      }
4516
1.18k
    } else if ((ImageInfo->FileSize > 16) &&
4517
852
         (!memcmp(file_header + 4, "ftyp", 4)) &&
4518
843
         (exif_read_from_stream_file_looped(ImageInfo->infile, (char*)(file_header + 8), 4) == 4) &&
4519
843
         ((!memcmp(file_header + 8, "heic", 4)) || (!memcmp(file_header + 8, "heix", 4)) || (!memcmp(file_header + 8, "mif1", 4)))) {
4520
841
      if (exif_scan_HEIF_header(ImageInfo, file_header)) {
4521
33
        ImageInfo->FileType = IMAGE_FILETYPE_HEIF;
4522
33
        return true;
4523
808
      } else {
4524
808
        exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Invalid HEIF file");
4525
808
        return false;
4526
808
      }
4527
841
    } else {
4528
43
      exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "File not supported");
4529
43
      return false;
4530
43
    }
4531
5.01k
  }
4532
24
  return false;
4533
6.02k
}
4534
/* }}} */
4535
4536
/* {{{ exif_discard_imageinfo
4537
   Discard data scanned by exif_read_file.
4538
*/
4539
static void exif_discard_imageinfo(image_info_type *ImageInfo)
4540
6.02k
{
4541
6.02k
  int i;
4542
4543
6.02k
  EFREE_IF(ImageInfo->FileName);
4544
6.02k
  EFREE_IF(ImageInfo->UserComment);
4545
6.02k
  EFREE_IF(ImageInfo->UserCommentEncoding);
4546
6.02k
  EFREE_IF(ImageInfo->Copyright);
4547
6.02k
  EFREE_IF(ImageInfo->CopyrightPhotographer);
4548
6.02k
  EFREE_IF(ImageInfo->CopyrightEditor);
4549
6.02k
  EFREE_IF(ImageInfo->Thumbnail.data);
4550
6.02k
  EFREE_IF(ImageInfo->encode_unicode);
4551
6.02k
  EFREE_IF(ImageInfo->decode_unicode_be);
4552
6.02k
  EFREE_IF(ImageInfo->decode_unicode_le);
4553
6.02k
  EFREE_IF(ImageInfo->encode_jis);
4554
6.02k
  EFREE_IF(ImageInfo->decode_jis_be);
4555
6.02k
  EFREE_IF(ImageInfo->decode_jis_le);
4556
6.02k
  EFREE_IF(ImageInfo->make);
4557
6.02k
  EFREE_IF(ImageInfo->model);
4558
9.94k
  for (i=0; i<ImageInfo->xp_fields.count; i++) {
4559
3.92k
    EFREE_IF(ImageInfo->xp_fields.list[i].value);
4560
3.92k
  }
4561
6.02k
  EFREE_IF(ImageInfo->xp_fields.list);
4562
90.3k
  for (i=0; i<SECTION_COUNT; i++) {
4563
84.3k
    exif_iif_free(ImageInfo, i);
4564
84.3k
  }
4565
6.02k
  exif_file_sections_free(ImageInfo);
4566
6.02k
  memset(ImageInfo, 0, sizeof(*ImageInfo));
4567
6.02k
}
4568
/* }}} */
4569
4570
/* {{{ exif_read_from_impl */
4571
static bool exif_read_from_impl(image_info_type *ImageInfo, php_stream *stream, int read_thumbnail, int read_all)
4572
6.02k
{
4573
6.02k
  bool ret;
4574
6.02k
  zend_stat_t st = {0};
4575
4576
  /* Start with an empty image information structure. */
4577
6.02k
  memset(ImageInfo, 0, sizeof(*ImageInfo));
4578
4579
6.02k
  ImageInfo->motorola_intel = -1; /* flag as unknown */
4580
6.02k
  ImageInfo->infile     = stream;
4581
6.02k
  ImageInfo->FileName     = NULL;
4582
4583
6.02k
  if (php_stream_is(ImageInfo->infile, PHP_STREAM_IS_STDIO)) {
4584
0
    if (stream->orig_path && VCWD_STAT(stream->orig_path, &st) >= 0) {
4585
0
      zend_string *base;
4586
0
      if ((st.st_mode & S_IFMT) != S_IFREG) {
4587
0
        exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Not a file");
4588
0
        ImageInfo->infile = NULL;
4589
0
        return false;
4590
0
      }
4591
4592
      /* Store file name */
4593
0
      base = php_basename(stream->orig_path, strlen(stream->orig_path), NULL, 0);
4594
0
      ImageInfo->FileName = estrndup(ZSTR_VAL(base), ZSTR_LEN(base));
4595
4596
0
      zend_string_release_ex(base, 0);
4597
4598
      /* Store file date/time. */
4599
0
      ImageInfo->FileDateTime = st.st_mtime;
4600
0
      ImageInfo->FileSize = st.st_size;
4601
0
    }
4602
6.02k
  } else {
4603
6.02k
    if (!ImageInfo->FileSize) {
4604
6.02k
      php_stream_seek(ImageInfo->infile, 0, SEEK_END);
4605
6.02k
      ImageInfo->FileSize = php_stream_tell(ImageInfo->infile);
4606
6.02k
      php_stream_seek(ImageInfo->infile, 0, SEEK_SET);
4607
6.02k
    }
4608
6.02k
  }
4609
4610
6.02k
  ImageInfo->read_thumbnail   = read_thumbnail;
4611
6.02k
  ImageInfo->read_all       = read_all;
4612
6.02k
  ImageInfo->Thumbnail.filetype = IMAGE_FILETYPE_UNKNOWN;
4613
4614
6.02k
  ImageInfo->encode_unicode   = estrdup(EXIF_G(encode_unicode));
4615
6.02k
  ImageInfo->decode_unicode_be  = estrdup(EXIF_G(decode_unicode_be));
4616
6.02k
  ImageInfo->decode_unicode_le  = estrdup(EXIF_G(decode_unicode_le));
4617
6.02k
  ImageInfo->encode_jis     = estrdup(EXIF_G(encode_jis));
4618
6.02k
  ImageInfo->decode_jis_be    = estrdup(EXIF_G(decode_jis_be));
4619
6.02k
  ImageInfo->decode_jis_le    = estrdup(EXIF_G(decode_jis_le));
4620
4621
4622
6.02k
  ImageInfo->ifd_nesting_level = 0;
4623
6.02k
  ImageInfo->ifd_count = 0;
4624
6.02k
  ImageInfo->num_errors = 0;
4625
4626
  /* Scan the headers */
4627
6.02k
  ret = exif_scan_FILE_header(ImageInfo);
4628
4629
6.02k
  return ret;
4630
6.02k
}
4631
/* }}} */
4632
4633
/* {{{ exif_read_from_stream */
4634
static bool exif_read_from_stream(image_info_type *ImageInfo, php_stream *stream, int read_thumbnail, int read_all)
4635
6.02k
{
4636
6.02k
  bool ret;
4637
6.02k
  off_t old_pos = php_stream_tell(stream);
4638
4639
6.02k
  if (old_pos) {
4640
6.02k
    php_stream_seek(stream, 0, SEEK_SET);
4641
6.02k
  }
4642
4643
6.02k
  ret = exif_read_from_impl(ImageInfo, stream, read_thumbnail, read_all);
4644
4645
6.02k
  if (old_pos) {
4646
6.02k
    php_stream_seek(stream, old_pos, SEEK_SET);
4647
6.02k
  }
4648
4649
6.02k
  return ret;
4650
6.02k
}
4651
/* }}} */
4652
4653
/* {{{ exif_read_from_file */
4654
static bool exif_read_from_file(image_info_type *ImageInfo, const char *FileName, int read_thumbnail, int read_all)
4655
0
{
4656
0
  bool ret;
4657
0
  php_stream *stream;
4658
4659
0
  stream = php_stream_open_wrapper(FileName, "rb", STREAM_MUST_SEEK | IGNORE_PATH, NULL);
4660
4661
0
  if (!stream) {
4662
0
    memset(&ImageInfo, 0, sizeof(ImageInfo));
4663
4664
0
    exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Unable to open file");
4665
4666
0
    return false;
4667
0
  }
4668
4669
0
  ret = exif_read_from_stream(ImageInfo, stream, read_thumbnail, read_all);
4670
4671
0
  php_stream_close(stream);
4672
4673
0
  return ret;
4674
0
}
4675
/* }}} */
4676
4677
/* {{{ Reads header data from an image and optionally reads the internal thumbnails */
4678
PHP_FUNCTION(exif_read_data)
4679
6.02k
{
4680
6.02k
  zend_string *z_sections_needed = NULL;
4681
6.02k
  bool sub_arrays = 0, read_thumbnail = 0, read_all = 0;
4682
6.02k
  zval *stream;
4683
6.02k
  bool ret;
4684
6.02k
  int i, sections_needed = 0;
4685
6.02k
  image_info_type ImageInfo;
4686
6.02k
  char *sections_str;
4687
4688
  /* Parse arguments */
4689
18.0k
  ZEND_PARSE_PARAMETERS_START(1, 4)
4690
24.0k
    Z_PARAM_ZVAL(stream)
4691
24.0k
    Z_PARAM_OPTIONAL
4692
24.0k
    Z_PARAM_STR_OR_NULL(z_sections_needed)
4693
0
    Z_PARAM_BOOL(sub_arrays)
4694
0
    Z_PARAM_BOOL(read_thumbnail)
4695
6.02k
  ZEND_PARSE_PARAMETERS_END();
4696
4697
6.02k
  memset(&ImageInfo, 0, sizeof(ImageInfo));
4698
4699
6.02k
  if (z_sections_needed) {
4700
0
    spprintf(&sections_str, 0, ",%s,", ZSTR_VAL(z_sections_needed));
4701
    /* sections_str DOES start with , and SPACES are NOT allowed in names */
4702
0
    char *s = sections_str;
4703
0
    while (*++s) {
4704
0
      if (*s == ' ') {
4705
0
        *s = ',';
4706
0
      }
4707
0
    }
4708
4709
0
    for (i = 0; i < SECTION_COUNT; i++) {
4710
0
      char tmp[64];
4711
0
      snprintf(tmp, sizeof(tmp), ",%s,", exif_get_sectionname(i));
4712
0
      if (strstr(sections_str, tmp)) {
4713
0
        sections_needed |= 1<<i;
4714
0
      }
4715
0
    }
4716
0
    EFREE_IF(sections_str);
4717
    /* now see what we need */
4718
#ifdef EXIF_DEBUG
4719
    sections_str = exif_get_sectionlist(sections_needed);
4720
    if (!sections_str) {
4721
      RETURN_FALSE;
4722
    }
4723
    exif_error_docref(NULL EXIFERR_CC, &ImageInfo, E_NOTICE, "Sections needed: %s", sections_str[0] ? sections_str : "None");
4724
    EFREE_IF(sections_str);
4725
#endif
4726
0
  }
4727
4728
6.02k
  if (Z_TYPE_P(stream) == IS_RESOURCE) {
4729
6.02k
    php_stream *p_stream = NULL;
4730
4731
6.02k
    php_stream_from_res(p_stream, Z_RES_P(stream));
4732
4733
6.02k
    ret = exif_read_from_stream(&ImageInfo, p_stream, read_thumbnail, read_all);
4734
6.02k
  } else {
4735
0
    if (!try_convert_to_string(stream)) {
4736
0
      RETURN_THROWS();
4737
0
    }
4738
4739
0
    if (!Z_STRLEN_P(stream)) {
4740
0
      zend_argument_must_not_be_empty_error(1);
4741
0
      RETURN_THROWS();
4742
0
    }
4743
4744
0
    if (UNEXPECTED(zend_str_has_nul_byte(Z_STR_P(stream)))) {
4745
0
      zend_argument_value_error(1, "must not contain any null bytes");
4746
0
      RETURN_THROWS();
4747
0
    }
4748
4749
0
    ret = exif_read_from_file(&ImageInfo, Z_STRVAL_P(stream), read_thumbnail, read_all);
4750
0
  }
4751
4752
6.02k
  sections_str = exif_get_sectionlist(ImageInfo.sections_found);
4753
4754
#ifdef EXIF_DEBUG
4755
  if (sections_str) {
4756
    exif_error_docref(NULL EXIFERR_CC, &ImageInfo, E_NOTICE, "Sections found: %s", sections_str[0] ? sections_str : "None");
4757
  }
4758
#endif
4759
4760
6.02k
  ImageInfo.sections_found |= FOUND_COMPUTED|FOUND_FILE;/* do not inform about in debug*/
4761
4762
6.02k
  if (ret == false || (sections_needed && !(sections_needed&ImageInfo.sections_found))) {
4763
    /* array_init must be checked at last! otherwise the array must be freed if a later test fails. */
4764
2.44k
    exif_discard_imageinfo(&ImageInfo);
4765
2.44k
      EFREE_IF(sections_str);
4766
2.44k
    RETURN_FALSE;
4767
2.44k
  }
4768
4769
3.58k
  array_init(return_value);
4770
4771
#ifdef EXIF_DEBUG
4772
  exif_error_docref(NULL EXIFERR_CC, &ImageInfo, E_NOTICE, "Generate section FILE");
4773
#endif
4774
4775
  /* now we can add our information */
4776
3.58k
  exif_iif_add_str(&ImageInfo, SECTION_FILE, "FileName",      ImageInfo.FileName);
4777
3.58k
  exif_iif_add_int(&ImageInfo, SECTION_FILE, "FileDateTime",  ImageInfo.FileDateTime);
4778
3.58k
  exif_iif_add_int(&ImageInfo, SECTION_FILE, "FileSize",      ImageInfo.FileSize);
4779
3.58k
  exif_iif_add_int(&ImageInfo, SECTION_FILE, "FileType",      ImageInfo.FileType);
4780
3.58k
  exif_iif_add_str(&ImageInfo, SECTION_FILE, "MimeType",      php_image_type_to_mime_type(ImageInfo.FileType));
4781
3.58k
  exif_iif_add_str(&ImageInfo, SECTION_FILE, "SectionsFound", sections_str ? sections_str : "NONE");
4782
4783
#ifdef EXIF_DEBUG
4784
  exif_error_docref(NULL EXIFERR_CC, &ImageInfo, E_NOTICE, "Generate section COMPUTED");
4785
#endif
4786
4787
3.58k
  if (ImageInfo.Width>0 &&  ImageInfo.Height>0) {
4788
57
    exif_iif_add_fmt(&ImageInfo, SECTION_COMPUTED, "html"   , "width=\"%d\" height=\"%d\"", ImageInfo.Width, ImageInfo.Height);
4789
57
    exif_iif_add_int(&ImageInfo, SECTION_COMPUTED, "Height", ImageInfo.Height);
4790
57
    exif_iif_add_int(&ImageInfo, SECTION_COMPUTED, "Width",  ImageInfo.Width);
4791
57
  }
4792
3.58k
  exif_iif_add_int(&ImageInfo, SECTION_COMPUTED, "IsColor", ImageInfo.IsColor);
4793
3.58k
  if (ImageInfo.motorola_intel != -1) {
4794
3.46k
    exif_iif_add_int(&ImageInfo, SECTION_COMPUTED, "ByteOrderMotorola", ImageInfo.motorola_intel);
4795
3.46k
  }
4796
3.58k
  if (ImageInfo.FocalLength) {
4797
0
    exif_iif_add_fmt(&ImageInfo, SECTION_COMPUTED, "FocalLength", "%4.1Fmm", ImageInfo.FocalLength);
4798
0
    if(ImageInfo.CCDWidth) {
4799
0
      exif_iif_add_fmt(&ImageInfo, SECTION_COMPUTED, "35mmFocalLength", "%dmm", (int)(ImageInfo.FocalLength/ImageInfo.CCDWidth*35+0.5));
4800
0
    }
4801
0
  }
4802
3.58k
  if(ImageInfo.CCDWidth) {
4803
1
    exif_iif_add_fmt(&ImageInfo, SECTION_COMPUTED, "CCDWidth", "%dmm", (int)ImageInfo.CCDWidth);
4804
1
  }
4805
3.58k
  if(ImageInfo.ExposureTime>0) {
4806
96
    float recip_exposure_time = 0.5f + 1.0f/ImageInfo.ExposureTime;
4807
96
    if (ImageInfo.ExposureTime <= 0.5 && recip_exposure_time < (float)INT_MAX) {
4808
25
      exif_iif_add_fmt(&ImageInfo, SECTION_COMPUTED, "ExposureTime", "%0.3F s (1/%d)", ImageInfo.ExposureTime, (int) recip_exposure_time);
4809
71
    } else {
4810
71
      exif_iif_add_fmt(&ImageInfo, SECTION_COMPUTED, "ExposureTime", "%0.3F s", ImageInfo.ExposureTime);
4811
71
    }
4812
96
  }
4813
3.58k
  if(ImageInfo.ApertureFNumber) {
4814
225
    exif_iif_add_fmt(&ImageInfo, SECTION_COMPUTED, "ApertureFNumber", "f/%.1F", ImageInfo.ApertureFNumber);
4815
225
  }
4816
3.58k
  if(ImageInfo.Distance) {
4817
151
    if(ImageInfo.Distance<0) {
4818
9
      exif_iif_add_str(&ImageInfo, SECTION_COMPUTED, "FocusDistance", "Infinite");
4819
142
    } else {
4820
142
      exif_iif_add_fmt(&ImageInfo, SECTION_COMPUTED, "FocusDistance", "%0.2Fm", ImageInfo.Distance);
4821
142
    }
4822
151
  }
4823
3.58k
  if (ImageInfo.UserComment) {
4824
258
    exif_iif_add_buffer(&ImageInfo, SECTION_COMPUTED, "UserComment", ImageInfo.UserCommentLength, ImageInfo.UserComment);
4825
258
    if (ImageInfo.UserCommentEncoding && strlen(ImageInfo.UserCommentEncoding)) {
4826
86
      exif_iif_add_str(&ImageInfo, SECTION_COMPUTED, "UserCommentEncoding", ImageInfo.UserCommentEncoding);
4827
86
    }
4828
258
  }
4829
4830
3.58k
  exif_iif_add_str(&ImageInfo, SECTION_COMPUTED, "Copyright",              ImageInfo.Copyright);
4831
3.58k
  exif_iif_add_str(&ImageInfo, SECTION_COMPUTED, "Copyright.Photographer", ImageInfo.CopyrightPhotographer);
4832
3.58k
  exif_iif_add_str(&ImageInfo, SECTION_COMPUTED, "Copyright.Editor",       ImageInfo.CopyrightEditor);
4833
4834
7.41k
  for (i=0; i<ImageInfo.xp_fields.count; i++) {
4835
3.83k
    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);
4836
3.83k
  }
4837
3.58k
  if (ImageInfo.Thumbnail.size) {
4838
373
    if (read_thumbnail) {
4839
      /* not exif_iif_add_str : this is a buffer */
4840
0
      exif_iif_add_tag(&ImageInfo, SECTION_THUMBNAIL, "THUMBNAIL", TAG_NONE, TAG_FMT_UNDEFINED, ImageInfo.Thumbnail.size, ImageInfo.Thumbnail.data, ImageInfo.Thumbnail.size);
4841
0
    }
4842
373
    if (!ImageInfo.Thumbnail.width || !ImageInfo.Thumbnail.height) {
4843
      /* try to evaluate if thumbnail data is present */
4844
311
      exif_scan_thumbnail(&ImageInfo);
4845
311
    }
4846
373
    exif_iif_add_int(&ImageInfo, SECTION_COMPUTED, "Thumbnail.FileType", ImageInfo.Thumbnail.filetype);
4847
373
    exif_iif_add_str(&ImageInfo, SECTION_COMPUTED, "Thumbnail.MimeType", (char*)php_image_type_to_mime_type(ImageInfo.Thumbnail.filetype));
4848
373
  }
4849
3.58k
  if (ImageInfo.Thumbnail.width && ImageInfo.Thumbnail.height) {
4850
121
    exif_iif_add_int(&ImageInfo, SECTION_COMPUTED, "Thumbnail.Height", ImageInfo.Thumbnail.height);
4851
121
    exif_iif_add_int(&ImageInfo, SECTION_COMPUTED, "Thumbnail.Width",  ImageInfo.Thumbnail.width);
4852
121
  }
4853
3.58k
  EFREE_IF(sections_str);
4854
4855
#ifdef EXIF_DEBUG
4856
  exif_error_docref(NULL EXIFERR_CC, &ImageInfo, E_NOTICE, "Adding image infos");
4857
#endif
4858
4859
3.58k
  add_assoc_image_info(return_value, sub_arrays, &ImageInfo, SECTION_FILE      );
4860
3.58k
  add_assoc_image_info(return_value, 1,          &ImageInfo, SECTION_COMPUTED  );
4861
3.58k
  add_assoc_image_info(return_value, sub_arrays, &ImageInfo, SECTION_ANY_TAG   );
4862
3.58k
  add_assoc_image_info(return_value, sub_arrays, &ImageInfo, SECTION_IFD0      );
4863
3.58k
  add_assoc_image_info(return_value, 1,          &ImageInfo, SECTION_THUMBNAIL );
4864
3.58k
  add_assoc_image_info(return_value, 1,          &ImageInfo, SECTION_COMMENT   );
4865
3.58k
  add_assoc_image_info(return_value, sub_arrays, &ImageInfo, SECTION_EXIF      );
4866
3.58k
  add_assoc_image_info(return_value, sub_arrays, &ImageInfo, SECTION_GPS       );
4867
3.58k
  add_assoc_image_info(return_value, sub_arrays, &ImageInfo, SECTION_INTEROP   );
4868
3.58k
  add_assoc_image_info(return_value, sub_arrays, &ImageInfo, SECTION_FPIX      );
4869
3.58k
  add_assoc_image_info(return_value, sub_arrays, &ImageInfo, SECTION_APP12     );
4870
3.58k
  add_assoc_image_info(return_value, sub_arrays, &ImageInfo, SECTION_WINXP     );
4871
3.58k
  add_assoc_image_info(return_value, sub_arrays, &ImageInfo, SECTION_MAKERNOTE );
4872
4873
#ifdef EXIF_DEBUG
4874
  exif_error_docref(NULL EXIFERR_CC, &ImageInfo, E_NOTICE, "Discarding info");
4875
#endif
4876
4877
3.58k
  exif_discard_imageinfo(&ImageInfo);
4878
4879
#ifdef EXIF_DEBUG
4880
  php_error_docref1(NULL, (Z_TYPE_P(stream) == IS_RESOURCE ? "<stream>" : Z_STRVAL_P(stream)), E_NOTICE, "Done");
4881
#endif
4882
3.58k
}
4883
/* }}} */
4884
4885
/* {{{ Reads the embedded thumbnail */
4886
PHP_FUNCTION(exif_thumbnail)
4887
0
{
4888
0
  bool ret;
4889
0
  image_info_type ImageInfo;
4890
0
  zval *stream;
4891
0
  zval *z_width = NULL, *z_height = NULL, *z_imagetype = NULL;
4892
4893
  /* Parse arguments */
4894
0
  ZEND_PARSE_PARAMETERS_START(1, 4)
4895
0
    Z_PARAM_ZVAL(stream)
4896
0
    Z_PARAM_OPTIONAL
4897
0
    Z_PARAM_ZVAL(z_width)
4898
0
    Z_PARAM_ZVAL(z_height)
4899
0
    Z_PARAM_ZVAL(z_imagetype)
4900
0
  ZEND_PARSE_PARAMETERS_END();
4901
4902
0
  memset(&ImageInfo, 0, sizeof(ImageInfo));
4903
4904
0
  if (Z_TYPE_P(stream) == IS_RESOURCE) {
4905
0
    php_stream *p_stream = NULL;
4906
4907
0
    php_stream_from_res(p_stream, Z_RES_P(stream));
4908
4909
0
    ret = exif_read_from_stream(&ImageInfo, p_stream, 1, 0);
4910
0
  } else {
4911
0
    if (!try_convert_to_string(stream)) {
4912
0
      RETURN_THROWS();
4913
0
    }
4914
4915
0
    if (!Z_STRLEN_P(stream)) {
4916
0
      zend_argument_must_not_be_empty_error(1);
4917
0
      RETURN_THROWS();
4918
0
    }
4919
4920
0
    if (zend_str_has_nul_byte(Z_STR_P(stream))) {
4921
0
      zend_argument_value_error(1, "must not contain any null bytes");
4922
0
      RETURN_THROWS();
4923
0
    }
4924
4925
0
    ret = exif_read_from_file(&ImageInfo, Z_STRVAL_P(stream), 1, 0);
4926
0
  }
4927
4928
0
  if (ret == false) {
4929
0
    exif_discard_imageinfo(&ImageInfo);
4930
0
    RETURN_FALSE;
4931
0
  }
4932
4933
#ifdef EXIF_DEBUG
4934
  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);
4935
#endif
4936
0
  if (!ImageInfo.Thumbnail.data || !ImageInfo.Thumbnail.size) {
4937
0
    exif_discard_imageinfo(&ImageInfo);
4938
0
    RETURN_FALSE;
4939
0
  }
4940
4941
#ifdef EXIF_DEBUG
4942
  exif_error_docref(NULL EXIFERR_CC, &ImageInfo, E_NOTICE, "Returning thumbnail(%d)", ImageInfo.Thumbnail.size);
4943
#endif
4944
4945
0
  RETVAL_STRINGL(ImageInfo.Thumbnail.data, ImageInfo.Thumbnail.size);
4946
0
  if ((z_width || z_height) && (!ImageInfo.Thumbnail.width || !ImageInfo.Thumbnail.height)) {
4947
0
    if (!exif_scan_thumbnail(&ImageInfo)) {
4948
0
      ImageInfo.Thumbnail.width = ImageInfo.Thumbnail.height = 0;
4949
0
    }
4950
0
  }
4951
0
  if (z_width) {
4952
0
    ZEND_TRY_ASSIGN_REF_LONG(z_width,  ImageInfo.Thumbnail.width);
4953
0
  }
4954
0
  if (z_height) {
4955
0
    ZEND_TRY_ASSIGN_REF_LONG(z_height, ImageInfo.Thumbnail.height);
4956
0
  }
4957
0
  if (z_imagetype) {
4958
0
    ZEND_TRY_ASSIGN_REF_LONG(z_imagetype, ImageInfo.Thumbnail.filetype);
4959
0
  }
4960
4961
#ifdef EXIF_DEBUG
4962
  exif_error_docref(NULL EXIFERR_CC, &ImageInfo, E_NOTICE, "Discarding info");
4963
#endif
4964
4965
0
  exif_discard_imageinfo(&ImageInfo);
4966
4967
#ifdef EXIF_DEBUG
4968
  php_error_docref1(NULL, (Z_TYPE_P(stream) == IS_RESOURCE ? "<stream>" : Z_STRVAL_P(stream)), E_NOTICE, "Done");
4969
#endif
4970
0
}
4971
/* }}} */
4972
4973
/* {{{ Get the type of an image */
4974
PHP_FUNCTION(exif_imagetype)
4975
0
{
4976
0
  char *imagefile;
4977
0
  size_t imagefile_len;
4978
0
  php_stream * stream;
4979
0
  int itype = 0;
4980
4981
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "p", &imagefile, &imagefile_len) == FAILURE) {
4982
0
    RETURN_THROWS();
4983
0
  }
4984
4985
0
  stream = php_stream_open_wrapper(imagefile, "rb", IGNORE_PATH|REPORT_ERRORS, NULL);
4986
4987
0
  if (stream == NULL) {
4988
0
    RETURN_FALSE;
4989
0
  }
4990
4991
0
  itype = php_getimagetype(stream, imagefile, NULL);
4992
4993
0
  php_stream_close(stream);
4994
4995
0
  if (itype == IMAGE_FILETYPE_UNKNOWN) {
4996
0
    RETURN_FALSE;
4997
0
  } else {
4998
0
    ZVAL_LONG(return_value, itype);
4999
0
  }
5000
0
}
5001
/* }}} */