Coverage Report

Created: 2025-06-13 06:43

/src/php-src/ext/exif/exif.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
   +----------------------------------------------------------------------+
3
   | Copyright (c) The PHP Group                                          |
4
   +----------------------------------------------------------------------+
5
   | This source file is subject to version 3.01 of the PHP license,      |
6
   | that is bundled with this package in the file LICENSE, and is        |
7
   | available through the world-wide-web at the following url:           |
8
   | 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
5
#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
1.11M
#define EFREE_IF(ptr) if (ptr) efree(ptr)
64
65
434k
#define MAX_IFD_NESTING_LEVEL 10
66
436k
#define MAX_IFD_TAGS 1000
67
68
/* {{{ PHP_MINFO_FUNCTION */
69
PHP_MINFO_FUNCTION(exif)
70
5
{
71
5
  php_info_print_table_start();
72
5
  php_info_print_table_row(2, "EXIF Support", "enabled");
73
5
  php_info_print_table_row(2, "Supported EXIF Version", "0220");
74
5
  php_info_print_table_row(2, "Supported filetypes", "JPEG, TIFF");
75
76
5
  if (USE_MBSTRING) {
77
0
    php_info_print_table_row(2, "Multibyte decoding support using mbstring", "enabled");
78
5
  } else {
79
5
    php_info_print_table_row(2, "Multibyte decoding support using mbstring", "disabled");
80
5
  }
81
82
5
  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
5
  php_info_print_table_end();
84
85
5
  DISPLAY_INI_ENTRIES();
86
5
}
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
31.7M
#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
989k
{
223
989k
  size_t total_read = 0;
224
1.66M
  while (total_read < count) {
225
679k
    ssize_t ret = php_stream_read(stream, buf + total_read, count - total_read);
226
679k
    if (ret == -1) {
227
0
      return -1;
228
0
    }
229
679k
    if (ret == 0) {
230
158
      break;
231
158
    }
232
678k
    total_read += ret;
233
678k
  }
234
989k
  return total_read;
235
989k
}
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
32
#define EXIF_ERRLOG_FILEEOF(ImageInfo)    exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "%s", EXIF_ERROR_FILEEOF);
246
806
#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
18.9M
#define NUM_FORMATS 13
256
257
16.4M
#define TAG_FMT_BYTE       1
258
2.04M
#define TAG_FMT_STRING     2
259
5.65M
#define TAG_FMT_USHORT     3
260
3.00M
#define TAG_FMT_ULONG      4
261
59.2k
#define TAG_FMT_URATIONAL  5
262
648k
#define TAG_FMT_SBYTE      6
263
98.6k
#define TAG_FMT_UNDEFINED  7
264
895k
#define TAG_FMT_SSHORT     8
265
697k
#define TAG_FMT_SLONG      9
266
276k
#define TAG_FMT_SRATIONAL 10
267
765k
#define TAG_FMT_SINGLE    11
268
397k
#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
52.8k
#define TAG_IMAGEWIDTH                  0x0100
329
13.1k
#define TAG_IMAGEHEIGHT                 0x0101
330
#define TAG_BITS_PER_SAMPLE             0x0102
331
#define TAG_COMPRESSION                 0x0103
332
6.10k
#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.91k
#define TAG_MAKE                        0x010F
340
347
#define TAG_MODEL                       0x0110
341
2.03k
#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.89k
#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
349k
#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
4.08k
#define TAG_JPEG_INTERCHANGE_FORMAT     0x0201
388
1.62k
#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.97k
#define TAG_COPYRIGHT                   0x8298
410
506
#define TAG_EXPOSURETIME                0x829A
411
1.01k
#define TAG_FNUMBER                     0x829D
412
842k
#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
598k
#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.59k
#define TAG_SHUTTERSPEED                0x9201
429
823
#define TAG_APERTURE                    0x9202
430
#define TAG_BRIGHTNESS_VALUE            0x9203
431
#define TAG_EXPOSURE_BIAS_VALUE         0x9204
432
1.25k
#define TAG_MAX_APERTURE                0x9205
433
1.62k
#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
56.7k
#define TAG_MAKER_NOTE                  0x927C
442
4.28k
#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.37k
#define TAG_XP_TITLE                    0x9C9B
449
1.97k
#define TAG_XP_COMMENTS                 0x9C9C
450
2.74k
#define TAG_XP_AUTHOR                   0x9C9D
451
3.56k
#define TAG_XP_KEYWORDS                 0x9C9E
452
4.18k
#define TAG_XP_SUBJECT                  0x9C9F
453
#define TAG_FLASH_PIX_VERSION           0xA000
454
#define TAG_COLOR_SPACE                 0xA001
455
59.8k
#define TAG_COMP_IMAGE_WIDTH            0xA002 /* compressed images only */
456
15.2k
#define TAG_COMP_IMAGE_HEIGHT           0xA003
457
#define TAG_RELATED_SOUND_FILE          0xA004
458
630k
#define TAG_INTEROP_IFD_POINTER         0xA005 /* IFD pointer */
459
#define TAG_FLASH_ENERGY                0xA20B
460
#define TAG_SPATIAL_FREQUENCY_RESPONSE  0xA20C
461
2.99k
#define TAG_FOCALPLANE_X_RES            0xA20E
462
#define TAG_FOCALPLANE_Y_RES            0xA20F
463
5.34k
#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
37.8k
#define TAG_NONE                    -1 /* note that -1 <> 0xFFFF */
496
582k
#define TAG_COMPUTED_VALUE          -2
497
718
#define TAG_END_OF_LIST                 0xFFFD
498
499
/* Values for TAG_PHOTOMETRIC_INTERPRETATION */
500
1.32k
#define PMI_BLACK_IS_ZERO       0
501
2.00k
#define PMI_WHITE_IS_ZERO       1
502
608
#define PMI_RGB               2
503
1.27k
#define PMI_PALETTE_COLOR       3
504
2.56k
#define PMI_TRANSPARENCY_MASK   4
505
1.84k
#define PMI_SEPARATED           5
506
2.41k
#define PMI_YCBCR               6
507
2.64k
#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
/* Some maker notes (e.g. DJI info tag) require custom parsing */
1294
#define REQUIRES_CUSTOM_PARSING NULL
1295
1296
/* Remember to update PHP_MINFO if updated */
1297
static const maker_note_type maker_note_array[] = {
1298
  { tag_table_VND_CANON,     "Canon",                   NULL,               0,  0,  MN_ORDER_INTEL,    MN_OFFSET_NORMAL},
1299
  { tag_table_VND_CASIO,     "CASIO",                   NULL,               0,  0,  MN_ORDER_MOTOROLA, MN_OFFSET_NORMAL},
1300
  { tag_table_VND_FUJI,      "FUJIFILM",                "FUJIFILM\x0C\x00\x00\x00",     12, 12, MN_ORDER_INTEL,    MN_OFFSET_MAKER},
1301
  { tag_table_VND_NIKON,     "NIKON",                   "Nikon\x00\x01\x00",        8,  8,  MN_ORDER_NORMAL,   MN_OFFSET_NORMAL},
1302
  { tag_table_VND_NIKON_990, "NIKON",                   NULL,               0,  0,  MN_ORDER_NORMAL,   MN_OFFSET_NORMAL},
1303
  { tag_table_VND_OLYMPUS,   "OLYMPUS OPTICAL CO.,LTD", "OLYMP\x00\x01\x00",        8,  8,  MN_ORDER_NORMAL,   MN_OFFSET_NORMAL},
1304
  { tag_table_VND_SAMSUNG,   "SAMSUNG",                 NULL,               0,  0,  MN_ORDER_NORMAL,   MN_OFFSET_NORMAL},
1305
  { tag_table_VND_PANASONIC, "Panasonic",               "Panasonic\x00\x00\x00",      12, 12, MN_ORDER_NORMAL,   MN_OFFSET_NORMAL},
1306
  { REQUIRES_CUSTOM_PARSING, "DJI",                     "[ae_dbg_info:",          13, 13, MN_ORDER_MOTOROLA, MN_OFFSET_NORMAL},
1307
  { tag_table_VND_DJI,       "DJI",                     NULL,               0,  0,  MN_ORDER_NORMAL,   MN_OFFSET_NORMAL},
1308
  { tag_table_VND_SONY,      "SONY",                    "SONY DSC \x00\x00\x00",      12, 12, MN_ORDER_NORMAL,   MN_OFFSET_NORMAL},
1309
  { tag_table_VND_SONY,      "SONY",                    NULL,               0,  0,  MN_ORDER_NORMAL,   MN_OFFSET_NORMAL},
1310
  { tag_table_VND_PENTAX,    "PENTAX",                  "AOC\x00",              6,  6,  MN_ORDER_NORMAL,   MN_OFFSET_NORMAL},
1311
  { tag_table_VND_MINOLTA,   "Minolta, KONICA MINOLTA", NULL,               0,  0,  MN_ORDER_NORMAL,   MN_OFFSET_NORMAL},
1312
  { tag_table_VND_SIGMA,     "SIGMA, FOVEON",           "SIGMA\x00\x00\x00",        10, 10, MN_ORDER_NORMAL,   MN_OFFSET_NORMAL},
1313
  { tag_table_VND_SIGMA,     "SIGMA, FOVEON",           "FOVEON\x00\x00\x00",       10, 10, MN_ORDER_NORMAL,   MN_OFFSET_NORMAL},
1314
  { tag_table_VND_KYOCERA,   "KYOCERA, CONTAX",     "KYOCERA            \x00\x00\x00",  22, 22, MN_ORDER_NORMAL,   MN_OFFSET_MAKER},
1315
  { tag_table_VND_RICOH,   "RICOH",         "Ricoh",              5,  5,  MN_ORDER_MOTOROLA, MN_OFFSET_NORMAL},
1316
  { tag_table_VND_RICOH,     "RICOH",         "RICOH",              5,  5,  MN_ORDER_MOTOROLA, MN_OFFSET_NORMAL},
1317
1318
  /* These re-uses existing formats */
1319
  { tag_table_VND_OLYMPUS,   "AGFA",          "AGFA \x00\x01",          8,  8,  MN_ORDER_NORMAL,   MN_OFFSET_NORMAL},
1320
  { tag_table_VND_OLYMPUS,   "EPSON",         "EPSON\x00\x01\x00",        8,  8,  MN_ORDER_NORMAL,   MN_OFFSET_NORMAL}
1321
};
1322
/* }}} */
1323
1324
static HashTable *exif_make_tag_ht(tag_info_type *tag_table)
1325
17
{
1326
17
  HashTable *ht = malloc(sizeof(HashTable));
1327
17
  zend_hash_init(ht, 0, NULL, NULL, 1);
1328
718
  while (tag_table->Tag != TAG_END_OF_LIST) {
1329
701
    if (!zend_hash_index_add_ptr(ht, tag_table->Tag, tag_table->Desc)) {
1330
0
      zend_error(E_CORE_ERROR, "Duplicate tag %x", tag_table->Tag);
1331
0
    }
1332
701
    tag_table++;
1333
701
  }
1334
17
  return ht;
1335
17
}
1336
1337
static void exif_tag_ht_dtor(zval *zv)
1338
0
{
1339
0
  HashTable *ht = Z_PTR_P(zv);
1340
0
  zend_hash_destroy(ht);
1341
0
  free(ht);
1342
0
}
1343
1344
static HashTable *exif_get_tag_ht(tag_info_type *tag_table)
1345
15.8M
{
1346
15.8M
  HashTable *ht;
1347
1348
15.8M
  if (!EXIF_G(tag_table_cache)) {
1349
1
    EXIF_G(tag_table_cache) = malloc(sizeof(HashTable));
1350
1
    zend_hash_init(EXIF_G(tag_table_cache), 0, NULL, exif_tag_ht_dtor, 1);
1351
1
  }
1352
1353
15.8M
  ht = zend_hash_index_find_ptr(EXIF_G(tag_table_cache), (uintptr_t) tag_table);
1354
15.8M
  if (ht) {
1355
15.8M
    return ht;
1356
15.8M
  }
1357
1358
17
  ht = exif_make_tag_ht(tag_table);
1359
17
  zend_hash_index_add_new_ptr(EXIF_G(tag_table_cache), (uintptr_t) tag_table, ht);
1360
17
  return ht;
1361
15.8M
}
1362
1363
/* {{{ exif_get_tagname
1364
  Get headername for tag_num or NULL if not defined */
1365
static char *exif_get_tagname(int tag_num, tag_table_type tag_table)
1366
0
{
1367
0
  return zend_hash_index_find_ptr(exif_get_tag_ht(tag_table), tag_num);
1368
0
}
1369
/* }}} */
1370
1371
static char *exif_get_tagname_debug(int tag_num, tag_table_type tag_table)
1372
15.7M
{
1373
15.7M
  char *desc = zend_hash_index_find_ptr(exif_get_tag_ht(tag_table), tag_num);
1374
15.7M
  if (desc) {
1375
2.09M
    return desc;
1376
2.09M
  }
1377
13.6M
  return "UndefinedTag";
1378
15.7M
}
1379
1380
static char *exif_get_tagname_key(int tag_num, char *buf, size_t buf_size, tag_table_type tag_table)
1381
130k
{
1382
130k
  char *desc = zend_hash_index_find_ptr(exif_get_tag_ht(tag_table), tag_num);
1383
130k
  if (desc) {
1384
81.0k
    return desc;
1385
81.0k
  }
1386
49.3k
  snprintf(buf, buf_size, "UndefinedTag:0x%04X", tag_num);
1387
49.3k
  return buf;
1388
130k
}
1389
1390
/* {{{ exif_char_dump
1391
 * Do not use! This is a debug function... */
1392
#ifdef EXIF_DEBUG
1393
static char* exif_char_dump(char * addr, int len, int offset)
1394
{
1395
  static char buf[4096+1];
1396
  static char tmp[20];
1397
  int c, i, p=0, n = 5+31;
1398
1399
  p += slprintf(buf+p, sizeof(buf)-p, "\nDump Len: %08X (%d)", len, len);
1400
  if (len) {
1401
    for(i=0; i<len+15 && p+n<=sizeof(buf); i++) {
1402
      if (i%16==0) {
1403
        p += slprintf(buf+p, sizeof(buf)-p, "\n%08X: ", i+offset);
1404
      }
1405
      if (i<len) {
1406
        c = *((unsigned char *)addr++);
1407
        p += slprintf(buf+p, sizeof(buf)-p, "%02X ", c);
1408
        tmp[i%16] = c>=32 ? c : '.';
1409
        tmp[(i%16)+1] = '\0';
1410
      } else {
1411
        p += slprintf(buf+p, sizeof(buf)-p, "   ");
1412
      }
1413
      if (i%16==15) {
1414
        p += slprintf(buf+p, sizeof(buf)-p, "    %s", tmp);
1415
        if (i>=len) {
1416
          break;
1417
        }
1418
      }
1419
    }
1420
  }
1421
  buf[sizeof(buf)-1] = '\0';
1422
  return buf;
1423
}
1424
#endif
1425
/* }}} */
1426
1427
/* {{{ php_jpg_get16
1428
   Get 16 bits motorola order (always) for jpeg header stuff.
1429
*/
1430
static int php_jpg_get16(void *value)
1431
5.94k
{
1432
5.94k
  return (((uchar *)value)[0] << 8) | ((uchar *)value)[1];
1433
5.94k
}
1434
/* }}} */
1435
1436
/* {{{ php_ifd_get16u
1437
 * Convert a 16 bit unsigned value from file's native byte order */
1438
static int php_ifd_get16u(void *value, int motorola_intel)
1439
42.0M
{
1440
42.0M
  if (motorola_intel) {
1441
14.4M
    return (((uchar *)value)[0] << 8) | ((uchar *)value)[1];
1442
27.5M
  } else {
1443
27.5M
    return (((uchar *)value)[1] << 8) | ((uchar *)value)[0];
1444
27.5M
  }
1445
42.0M
}
1446
/* }}} */
1447
1448
/* {{{ php_ifd_get16s
1449
 * Convert a 16 bit signed value from file's native byte order */
1450
static signed short php_ifd_get16s(void *value, int motorola_intel)
1451
428k
{
1452
428k
  return (signed short)php_ifd_get16u(value, motorola_intel);
1453
428k
}
1454
/* }}} */
1455
1456
/* {{{ php_ifd_get32u
1457
 * Convert a 32 bit unsigned value from file's native byte order */
1458
static unsigned php_ifd_get32u(void *void_value, int motorola_intel)
1459
35.8M
{
1460
35.8M
  uchar *value = (uchar *) void_value;
1461
35.8M
  if (motorola_intel) {
1462
12.6M
    return  ((unsigned)value[0] << 24)
1463
12.6M
        | ((unsigned)value[1] << 16)
1464
12.6M
        | ((unsigned)value[2] << 8 )
1465
12.6M
        | ((unsigned)value[3]      );
1466
23.1M
  } else {
1467
23.1M
    return  ((unsigned)value[3] << 24)
1468
23.1M
        | ((unsigned)value[2] << 16)
1469
23.1M
        | ((unsigned)value[1] << 8 )
1470
23.1M
        | ((unsigned)value[0]      );
1471
23.1M
  }
1472
35.8M
}
1473
/* }}} */
1474
1475
/* {{{ php_ifd_get64u
1476
 * Convert a 64 bit unsigned value from file's native byte order */
1477
static uint64_t php_ifd_get64u(void *void_value, int motorola_intel)
1478
100k
{
1479
100k
  uchar *value = (uchar *) void_value;
1480
100k
  if (motorola_intel) {
1481
0
    return ((uint64_t)value[0] << 56)
1482
0
      | ((uint64_t)value[1] << 48)
1483
0
      | ((uint64_t)value[2] << 40)
1484
0
      | ((uint64_t)value[3] << 32)
1485
0
      | ((uint64_t)value[4] << 24)
1486
0
      | ((uint64_t)value[5] << 16)
1487
0
      | ((uint64_t)value[6] << 8 )
1488
0
      | ((uint64_t)value[7]      );
1489
100k
  } else {
1490
100k
    return ((uint64_t)value[7] << 56)
1491
100k
      | ((uint64_t)value[6] << 48)
1492
100k
      | ((uint64_t)value[5] << 40)
1493
100k
      | ((uint64_t)value[4] << 32)
1494
100k
      | ((uint64_t)value[3] << 24)
1495
100k
      | ((uint64_t)value[2] << 16)
1496
100k
      | ((uint64_t)value[1] << 8 )
1497
100k
      | ((uint64_t)value[0]      );
1498
100k
  }
1499
100k
}
1500
/* }}} */
1501
1502
/* {{{ php_ifd_get32u
1503
 * Convert a 32 bit signed value from file's native byte order */
1504
static unsigned php_ifd_get32s(void *value, int motorola_intel)
1505
57.4k
{
1506
57.4k
  return (int) php_ifd_get32u(value, motorola_intel);
1507
57.4k
}
1508
/* }}} */
1509
1510
/* {{{ php_ifd_set16u
1511
 * Write 16 bit unsigned value to data */
1512
static void php_ifd_set16u(char *data, unsigned int value, int motorola_intel)
1513
0
{
1514
0
  if (motorola_intel) {
1515
0
    data[0] = (value & 0xFF00) >> 8;
1516
0
    data[1] = (value & 0x00FF);
1517
0
  } else {
1518
0
    data[1] = (value & 0xFF00) >> 8;
1519
0
    data[0] = (value & 0x00FF);
1520
0
  }
1521
0
}
1522
/* }}} */
1523
1524
/* {{{ php_ifd_set32u
1525
 * Convert a 32 bit unsigned value from file's native byte order */
1526
static void php_ifd_set32u(char *data, size_t value, int motorola_intel)
1527
0
{
1528
0
  if (motorola_intel) {
1529
0
    data[0] = (value & 0xFF000000) >> 24;
1530
0
    data[1] = (char) ((value & 0x00FF0000) >> 16);
1531
0
    data[2] = (value & 0x0000FF00) >>  8;
1532
0
    data[3] = (value & 0x000000FF);
1533
0
  } else {
1534
0
    data[3] = (value & 0xFF000000) >> 24;
1535
0
    data[2] = (char) ((value & 0x00FF0000) >> 16);
1536
0
    data[1] = (value & 0x0000FF00) >>  8;
1537
0
    data[0] = (value & 0x000000FF);
1538
0
  }
1539
0
}
1540
/* }}} */
1541
1542
297k
static float php_ifd_get_float(char *data) {
1543
297k
  union { uint32_t i; float f; } u;
1544
297k
  u.i = php_ifd_get32u(data, 0);
1545
297k
  return u.f;
1546
297k
}
1547
1548
100k
static double php_ifd_get_double(char *data) {
1549
100k
  union { uint64_t i; double f; } u;
1550
100k
  u.i = php_ifd_get64u(data, 0);
1551
100k
  return u.f;
1552
100k
}
1553
1554
#ifdef EXIF_DEBUG
1555
char * exif_dump_data(int *dump_free, int format, int components, int motorola_intel, char *value_ptr) /* {{{ */
1556
{
1557
  char *dump;
1558
  int len;
1559
1560
  *dump_free = 0;
1561
  if (format == TAG_FMT_STRING) {
1562
    return value_ptr ? value_ptr : "<no data>";
1563
  }
1564
  if (format == TAG_FMT_UNDEFINED) {
1565
    return "<undefined>";
1566
  }
1567
  if (format == TAG_FMT_IFD) {
1568
    return "";
1569
  }
1570
  if (format == TAG_FMT_SINGLE || format == TAG_FMT_DOUBLE) {
1571
    return "<not implemented>";
1572
  }
1573
  *dump_free = 1;
1574
  if (components > 1) {
1575
    len = spprintf(&dump, 0, "(%d) {", components);
1576
  } else {
1577
    len = spprintf(&dump, 0, "{");
1578
  }
1579
  while(components > 0) {
1580
    switch(format) {
1581
      case TAG_FMT_BYTE:
1582
      case TAG_FMT_UNDEFINED:
1583
      case TAG_FMT_STRING:
1584
      case TAG_FMT_SBYTE:
1585
        dump = erealloc(dump, len + 4 + 1);
1586
        snprintf(dump + len, 4 + 1, "0x%02X", *value_ptr);
1587
        len += 4;
1588
        value_ptr++;
1589
        break;
1590
      case TAG_FMT_USHORT:
1591
      case TAG_FMT_SSHORT:
1592
        dump = erealloc(dump, len + 6 + 1);
1593
        snprintf(dump + len, 6 + 1, "0x%04X", php_ifd_get16s(value_ptr, motorola_intel));
1594
        len += 6;
1595
        value_ptr += 2;
1596
        break;
1597
      case TAG_FMT_ULONG:
1598
      case TAG_FMT_SLONG:
1599
        dump = erealloc(dump, len + 6 + 1);
1600
        snprintf(dump + len, 6 + 1, "0x%04X", php_ifd_get32s(value_ptr, motorola_intel));
1601
        len += 6;
1602
        value_ptr += 4;
1603
        break;
1604
      case TAG_FMT_URATIONAL:
1605
      case TAG_FMT_SRATIONAL:
1606
        dump = erealloc(dump, len + 13 + 1);
1607
        snprintf(dump + len, 13 + 1, "0x%04X/0x%04X", php_ifd_get32s(value_ptr, motorola_intel), php_ifd_get32s(value_ptr+4, motorola_intel));
1608
        len += 13;
1609
        value_ptr += 8;
1610
        break;
1611
    }
1612
    if (components > 0) {
1613
      dump = erealloc(dump, len + 2 + 1);
1614
      snprintf(dump + len, 2 + 1, ", ");
1615
      len += 2;
1616
      components--;
1617
    } else{
1618
      break;
1619
    }
1620
  }
1621
  dump = erealloc(dump, len + 1 + 1);
1622
  snprintf(dump + len, 1 + 1, "}");
1623
  return dump;
1624
}
1625
/* }}} */
1626
#endif
1627
1628
/* {{{ exif_convert_any_format
1629
 * Evaluate number, be it int, rational, or float from directory. */
1630
static double exif_convert_any_format(void *value, int format, int motorola_intel)
1631
6.16k
{
1632
6.16k
  int     s_den;
1633
6.16k
  unsigned  u_den;
1634
1635
6.16k
  switch(format) {
1636
249
    case TAG_FMT_SBYTE:     return *(signed char *)value;
1637
667
    case TAG_FMT_BYTE:      return *(uchar *)value;
1638
1639
486
    case TAG_FMT_USHORT:    return php_ifd_get16u(value, motorola_intel);
1640
629
    case TAG_FMT_ULONG:     return php_ifd_get32u(value, motorola_intel);
1641
1642
879
    case TAG_FMT_URATIONAL:
1643
879
      u_den = php_ifd_get32u(4+(char *)value, motorola_intel);
1644
879
      if (u_den == 0) {
1645
240
        return 0;
1646
639
      } else {
1647
639
        return (double)php_ifd_get32u(value, motorola_intel) / u_den;
1648
639
      }
1649
1650
836
    case TAG_FMT_SRATIONAL:
1651
836
      s_den = php_ifd_get32s(4+(char *)value, motorola_intel);
1652
836
      if (s_den == 0) {
1653
250
        return 0;
1654
586
      } else {
1655
586
        return (double)php_ifd_get32s(value, motorola_intel) / s_den;
1656
586
      }
1657
1658
626
    case TAG_FMT_SSHORT:    return (signed short)php_ifd_get16u(value, motorola_intel);
1659
534
    case TAG_FMT_SLONG:     return php_ifd_get32s(value, motorola_intel);
1660
1661
    /* Not sure if this is correct (never seen float used in Exif format) */
1662
418
    case TAG_FMT_SINGLE:
1663
#ifdef EXIF_DEBUG
1664
      php_error_docref(NULL, E_NOTICE, "Found value of type single");
1665
#endif
1666
418
      return (double) php_ifd_get_float(value);
1667
447
    case TAG_FMT_DOUBLE:
1668
#ifdef EXIF_DEBUG
1669
      php_error_docref(NULL, E_NOTICE, "Found value of type double");
1670
#endif
1671
447
      return php_ifd_get_double(value);
1672
6.16k
  }
1673
389
  return 0;
1674
6.16k
}
1675
/* }}} */
1676
1677
/* {{{ exif_rewrite_tag_format_to_unsigned
1678
 * Rewrite format tag so that it specifies an unsigned type for a tag */
1679
static int exif_rewrite_tag_format_to_unsigned(int format)
1680
17.7k
{
1681
17.7k
  switch(format) {
1682
369
    case TAG_FMT_SBYTE: return TAG_FMT_BYTE;
1683
531
    case TAG_FMT_SRATIONAL: return TAG_FMT_URATIONAL;
1684
2.14k
    case TAG_FMT_SSHORT: return TAG_FMT_USHORT;
1685
2.22k
    case TAG_FMT_SLONG: return TAG_FMT_ULONG;
1686
17.7k
  }
1687
12.5k
  return format;
1688
17.7k
}
1689
/* }}} */
1690
1691
/* Use saturation for out of bounds values to avoid UB */
1692
3.12k
static size_t float_to_size_t(float x) {
1693
3.12k
  if (x < 0.0f || zend_isnan(x)) {
1694
1.00k
    return 0;
1695
2.12k
  } else if (x > (float) SIZE_MAX) {
1696
622
    return SIZE_MAX;
1697
1.50k
  } else {
1698
1.50k
    return (size_t) x;
1699
1.50k
  }
1700
3.12k
}
1701
1702
3.87k
static size_t double_to_size_t(double x) {
1703
3.87k
  if (x < 0.0 || zend_isnan(x)) {
1704
564
    return 0;
1705
3.31k
  } else if (x > (double) SIZE_MAX) {
1706
225
    return SIZE_MAX;
1707
3.09k
  } else {
1708
3.09k
    return (size_t) x;
1709
3.09k
  }
1710
3.87k
}
1711
1712
/* {{{ exif_convert_any_to_int
1713
 * Evaluate number, be it int, rational, or float from directory. */
1714
static size_t exif_convert_any_to_int(void *value, int format, int motorola_intel)
1715
22.8k
{
1716
22.8k
  switch (format) {
1717
234
    case TAG_FMT_SBYTE:     return *(signed char *)value;
1718
4.19k
    case TAG_FMT_BYTE:      return *(uchar *)value;
1719
1720
2.86k
    case TAG_FMT_USHORT:    return php_ifd_get16u(value, motorola_intel);
1721
2.84k
    case TAG_FMT_ULONG:     return php_ifd_get32u(value, motorola_intel);
1722
1723
1.33k
    case TAG_FMT_URATIONAL: {
1724
1.33k
      unsigned u_den = php_ifd_get32u(4+(char *)value, motorola_intel);
1725
1.33k
      if (u_den == 0) {
1726
450
        return 0;
1727
880
      } else {
1728
880
        return php_ifd_get32u(value, motorola_intel) / u_den;
1729
880
      }
1730
1.33k
    }
1731
1732
1.43k
    case TAG_FMT_SRATIONAL: {
1733
1.43k
      int s_num = php_ifd_get32s(value, motorola_intel);
1734
1.43k
      int s_den = php_ifd_get32s(4+(char *)value, motorola_intel);
1735
1.43k
      if (s_den == 0) {
1736
387
        return 0;
1737
1.05k
      } else if (s_num == INT_MIN && s_den == -1) {
1738
217
        return INT_MAX;
1739
833
      } else {
1740
833
        return s_num / s_den;
1741
833
      }
1742
1.43k
    }
1743
1744
478
    case TAG_FMT_SSHORT:    return php_ifd_get16u(value, motorola_intel);
1745
481
    case TAG_FMT_SLONG:     return php_ifd_get32s(value, motorola_intel);
1746
1747
    /* Not sure if this is correct (never seen float used in Exif format) */
1748
3.12k
    case TAG_FMT_SINGLE:
1749
#ifdef EXIF_DEBUG
1750
      php_error_docref(NULL, E_NOTICE, "Found value of type single");
1751
#endif
1752
3.12k
      return float_to_size_t(php_ifd_get_float(value));
1753
3.87k
    case TAG_FMT_DOUBLE:
1754
#ifdef EXIF_DEBUG
1755
      php_error_docref(NULL, E_NOTICE, "Found value of type double");
1756
#endif
1757
3.87k
      return double_to_size_t(php_ifd_get_double(value));
1758
22.8k
  }
1759
2.00k
  return 0;
1760
22.8k
}
1761
/* }}} */
1762
1763
/* {{{ struct image_info_value, image_info_list */
1764
#ifndef WORD
1765
#define WORD unsigned short
1766
#endif
1767
#ifndef DWORD
1768
#define DWORD unsigned int
1769
#endif
1770
1771
typedef struct {
1772
  int             num;
1773
  int             den;
1774
} signed_rational;
1775
1776
typedef struct {
1777
  unsigned int    num;
1778
  unsigned int    den;
1779
} unsigned_rational;
1780
1781
typedef union _image_info_value {
1782
  char        *s;
1783
  unsigned            u;
1784
  int         i;
1785
  float               f;
1786
  double              d;
1787
  signed_rational   sr;
1788
  unsigned_rational   ur;
1789
  union _image_info_value   *list;
1790
} image_info_value;
1791
1792
typedef struct {
1793
  WORD                tag;
1794
  WORD                format;
1795
  DWORD               length;
1796
  DWORD               dummy;  /* value ptr of tiff directory entry */
1797
  char        *name;
1798
  image_info_value    value;
1799
} image_info_data;
1800
1801
typedef struct {
1802
  int                 count;
1803
  int                 alloc_count;
1804
  image_info_data   *list;
1805
} image_info_list;
1806
/* }}} */
1807
1808
/* {{{ exif_get_sectionname
1809
 Returns the name of a section
1810
*/
1811
42.8k
#define SECTION_FILE        0
1812
45.3k
#define SECTION_COMPUTED    1
1813
151k
#define SECTION_ANY_TAG     2
1814
225k
#define SECTION_IFD0        3
1815
784k
#define SECTION_THUMBNAIL   4
1816
852k
#define SECTION_COMMENT     5
1817
6.11k
#define SECTION_APP0        6
1818
418k
#define SECTION_EXIF        7
1819
10.4k
#define SECTION_FPIX        8
1820
40.7k
#define SECTION_GPS         9
1821
69.2k
#define SECTION_INTEROP     10
1822
11.4k
#define SECTION_APP12       11
1823
27.2k
#define SECTION_WINXP       12
1824
18.8k
#define SECTION_MAKERNOTE   13
1825
275k
#define SECTION_COUNT       14
1826
1827
6.11k
#define FOUND_FILE          (1<<SECTION_FILE)
1828
6.20k
#define FOUND_COMPUTED      (1<<SECTION_COMPUTED)
1829
136k
#define FOUND_ANY_TAG       (1<<SECTION_ANY_TAG)
1830
10.4k
#define FOUND_IFD0          (1<<SECTION_IFD0)
1831
26.1k
#define FOUND_THUMBNAIL     (1<<SECTION_THUMBNAIL)
1832
#define FOUND_COMMENT       (1<<SECTION_COMMENT)
1833
#define FOUND_APP0          (1<<SECTION_APP0)
1834
116k
#define FOUND_EXIF          (1<<SECTION_EXIF)
1835
#define FOUND_FPIX          (1<<SECTION_FPIX)
1836
11.3k
#define FOUND_GPS           (1<<SECTION_GPS)
1837
21.8k
#define FOUND_INTEROP       (1<<SECTION_INTEROP)
1838
#define FOUND_APP12         (1<<SECTION_APP12)
1839
4.18k
#define FOUND_WINXP         (1<<SECTION_WINXP)
1840
3.06k
#define FOUND_MAKERNOTE     (1<<SECTION_MAKERNOTE)
1841
1842
static const char *exif_get_sectionname(int section)
1843
106k
{
1844
106k
  switch(section) {
1845
6.11k
    case SECTION_FILE:      return "FILE";
1846
10.4k
    case SECTION_COMPUTED:  return "COMPUTED";
1847
10.7k
    case SECTION_ANY_TAG:   return "ANY_TAG";
1848
11.4k
    case SECTION_IFD0:      return "IFD0";
1849
9.89k
    case SECTION_THUMBNAIL: return "THUMBNAIL";
1850
6.43k
    case SECTION_COMMENT:   return "COMMENT";
1851
6.11k
    case SECTION_APP0:      return "APP0";
1852
7.49k
    case SECTION_EXIF:      return "EXIF";
1853
6.11k
    case SECTION_FPIX:      return "FPIX";
1854
6.20k
    case SECTION_GPS:       return "GPS";
1855
6.37k
    case SECTION_INTEROP:   return "INTEROP";
1856
6.21k
    case SECTION_APP12:     return "APP12";
1857
6.18k
    case SECTION_WINXP:     return "WINXP";
1858
6.38k
    case SECTION_MAKERNOTE: return "MAKERNOTE";
1859
106k
  }
1860
0
  return "";
1861
106k
}
1862
1863
static tag_table_type exif_get_tag_table(int section)
1864
190k
{
1865
190k
  switch(section) {
1866
0
    case SECTION_FILE:      return &tag_table_IFD[0];
1867
0
    case SECTION_COMPUTED:  return &tag_table_IFD[0];
1868
0
    case SECTION_ANY_TAG:   return &tag_table_IFD[0];
1869
6.26k
    case SECTION_IFD0:      return &tag_table_IFD[0];
1870
72.1k
    case SECTION_THUMBNAIL: return &tag_table_IFD[0];
1871
0
    case SECTION_COMMENT:   return &tag_table_IFD[0];
1872
0
    case SECTION_APP0:      return &tag_table_IFD[0];
1873
85.5k
    case SECTION_EXIF:      return &tag_table_IFD[0];
1874
0
    case SECTION_FPIX:      return &tag_table_IFD[0];
1875
7.43k
    case SECTION_GPS:       return &tag_table_GPS[0];
1876
14.8k
    case SECTION_INTEROP:   return &tag_table_IOP[0];
1877
0
    case SECTION_APP12:     return &tag_table_IFD[0];
1878
4.18k
    case SECTION_WINXP:     return &tag_table_IFD[0];
1879
190k
  }
1880
0
  return &tag_table_IFD[0];
1881
190k
}
1882
/* }}} */
1883
1884
/* {{{ exif_get_sectionlist
1885
   Return list of sectionnames specified by sectionlist. Return value must be freed
1886
*/
1887
static char *exif_get_sectionlist(int sectionlist)
1888
6.11k
{
1889
6.11k
  int i, len, ml = 0;
1890
6.11k
  char *sections;
1891
1892
91.7k
  for(i=0; i<SECTION_COUNT; i++) {
1893
85.5k
    ml += strlen(exif_get_sectionname(i))+2;
1894
85.5k
  }
1895
6.11k
  sections = safe_emalloc(ml, 1, 1);
1896
6.11k
  sections[0] = '\0';
1897
6.11k
  len = 0;
1898
91.7k
  for(i=0; i<SECTION_COUNT; i++) {
1899
85.5k
    if (sectionlist&(1<<i)) {
1900
14.3k
      snprintf(sections+len, ml-len, "%s, ", exif_get_sectionname(i));
1901
14.3k
      len = strlen(sections);
1902
14.3k
    }
1903
85.5k
  }
1904
6.11k
  if (len>2)
1905
5.56k
    sections[len-2] = '\0';
1906
6.11k
  return sections;
1907
6.11k
}
1908
/* }}} */
1909
1910
/* {{{ struct image_info_type
1911
   This structure stores Exif header image elements in a simple manner
1912
   Used to store camera data as extracted from the various ways that it can be
1913
   stored in a nexif header
1914
*/
1915
1916
typedef struct {
1917
  int     type;
1918
  size_t  size;
1919
  uchar   *data;
1920
} file_section;
1921
1922
typedef struct {
1923
  int             count;
1924
  int             alloc_count;
1925
  file_section    *list;
1926
} file_section_list;
1927
1928
typedef struct {
1929
  image_filetype  filetype;
1930
  size_t          width, height;
1931
  size_t          size;
1932
  size_t          offset;
1933
  char          *data;
1934
} thumbnail_data;
1935
1936
typedef struct {
1937
  char      *value;
1938
  size_t      size;
1939
  int       tag;
1940
} xp_field_type;
1941
1942
typedef struct {
1943
  int             count;
1944
  xp_field_type   *list;
1945
} xp_field_list;
1946
1947
/* This structure is used to store a section of a Jpeg file. */
1948
typedef struct {
1949
  php_stream      *infile;
1950
  char            *FileName;
1951
  time_t          FileDateTime;
1952
  size_t          FileSize;
1953
  image_filetype  FileType;
1954
  int             Height, Width;
1955
  int             IsColor;
1956
1957
  char            *make;
1958
  char            *model;
1959
1960
  float           ApertureFNumber;
1961
  float           ExposureTime;
1962
  double          FocalplaneUnits;
1963
  float           CCDWidth;
1964
  double          FocalplaneXRes;
1965
  size_t          ExifImageWidth;
1966
  float           FocalLength;
1967
  float           Distance;
1968
1969
  int             motorola_intel; /* 1 Motorola; 0 Intel */
1970
1971
  char            *UserComment;
1972
  int             UserCommentLength;
1973
  char            *UserCommentEncoding;
1974
  char            *encode_unicode;
1975
  char            *decode_unicode_be;
1976
  char            *decode_unicode_le;
1977
  char            *encode_jis;
1978
  char            *decode_jis_be;
1979
  char            *decode_jis_le;
1980
  char            *Copyright;/* EXIF standard defines Copyright as "<Photographer> [ '\0' <Editor> ] ['\0']" */
1981
  char            *CopyrightPhotographer;
1982
  char            *CopyrightEditor;
1983
1984
  xp_field_list   xp_fields;
1985
1986
  thumbnail_data  Thumbnail;
1987
  /* other */
1988
  int             sections_found; /* FOUND_<marker> */
1989
  image_info_list info_list[SECTION_COUNT];
1990
  /* for parsing */
1991
  int             read_thumbnail;
1992
  int             read_all;
1993
  int             ifd_nesting_level;
1994
  int             ifd_count;
1995
  int             num_errors;
1996
  /* internal */
1997
  file_section_list   file;
1998
} image_info_type;
1999
/* }}} */
2000
2001
// EXIF_DEBUG can produce lots of messages
2002
#ifndef EXIF_DEBUG
2003
32.7M
#define EXIF_MAX_ERRORS 10
2004
#else
2005
#define EXIF_MAX_ERRORS 100000
2006
#endif
2007
2008
/* {{{ exif_error_docref */
2009
static void exif_error_docref(const char *docref EXIFERR_DC, image_info_type *ImageInfo, int type, const char *format, ...)
2010
16.3M
{
2011
16.3M
  va_list args;
2012
2013
16.3M
  if (ImageInfo) {
2014
16.3M
    if (++ImageInfo->num_errors > EXIF_MAX_ERRORS) {
2015
16.3M
      if (ImageInfo->num_errors == EXIF_MAX_ERRORS+1) {
2016
1.62k
        php_error_docref(docref, type,
2017
1.62k
          "Further exif parsing errors have been suppressed");
2018
1.62k
      }
2019
16.3M
      return;
2020
16.3M
    }
2021
16.3M
  }
2022
2023
26.9k
  va_start(args, format);
2024
#ifdef EXIF_DEBUG
2025
  {
2026
    char *buf;
2027
2028
    spprintf(&buf, 0, "%s(%ld): %s", _file, _line, format);
2029
    php_verror(docref, ImageInfo && ImageInfo->FileName ? ImageInfo->FileName:"", type, buf, args);
2030
    efree(buf);
2031
  }
2032
#else
2033
26.9k
  php_verror(docref, ImageInfo && ImageInfo->FileName ? ImageInfo->FileName:"", type, format, args);
2034
26.9k
#endif
2035
26.9k
  va_end(args);
2036
26.9k
}
2037
/* }}} */
2038
2039
/* {{{ jpeg_sof_info */
2040
typedef struct {
2041
  int     bits_per_sample;
2042
  size_t  width;
2043
  size_t  height;
2044
  int     num_components;
2045
} jpeg_sof_info;
2046
/* }}} */
2047
2048
/* Base address for offset references, together with valid memory range.
2049
 * The valid range does not necessarily include the offset base. */
2050
typedef struct {
2051
  char *offset_base;
2052
  char *valid_start; /* inclusive */
2053
  char *valid_end;   /* exclusive */
2054
} exif_offset_info;
2055
2056
352k
static zend_always_inline bool ptr_offset_overflows(const char *ptr, size_t offset) {
2057
352k
  return UINTPTR_MAX - (uintptr_t) ptr < offset;
2058
352k
}
2059
2060
static inline void exif_offset_info_init(
2061
187k
    exif_offset_info *info, char *offset_base, char *valid_start, size_t valid_length) {
2062
187k
  ZEND_ASSERT(!ptr_offset_overflows(valid_start, valid_length));
2063
#ifdef __SANITIZE_ADDRESS__
2064
  ZEND_ASSERT(!__asan_region_is_poisoned(valid_start, valid_length));
2065
#endif
2066
187k
  info->offset_base = offset_base;
2067
187k
  info->valid_start = valid_start;
2068
187k
  info->valid_end = valid_start + valid_length;
2069
187k
}
2070
2071
/* Try to get a pointer at offset_base+offset with length dereferenceable bytes. */
2072
static inline char *exif_offset_info_try_get(
2073
80.0k
    const exif_offset_info *info, size_t offset, size_t length) {
2074
80.0k
  char *start, *end;
2075
80.0k
  if (ptr_offset_overflows(info->offset_base, offset)) {
2076
40
    return NULL;
2077
40
  }
2078
2079
80.0k
  start = info->offset_base + offset;
2080
80.0k
  if (ptr_offset_overflows(start, length)) {
2081
0
    return NULL;
2082
0
  }
2083
2084
80.0k
  end = start + length;
2085
80.0k
  if (start < info->valid_start || end > info->valid_end) {
2086
48.7k
    return NULL;
2087
48.7k
  }
2088
2089
31.2k
  return start;
2090
80.0k
}
2091
2092
static inline bool exif_offset_info_contains(
2093
4.70k
    const exif_offset_info *info, const char *start, size_t length) {
2094
4.70k
  if (ptr_offset_overflows(start, length)) {
2095
0
    return 0;
2096
0
  }
2097
2098
  /* start and valid_start are both inclusive, end and valid_end are both exclusive,
2099
   * so we use >= and <= to do the checks, respectively. */
2100
4.70k
  const char *end = start + length;
2101
4.70k
  return start >= info->valid_start && end <= info->valid_end;
2102
4.70k
}
2103
2104
#ifdef EXIF_DEBUG
2105
static inline int exif_offset_info_length(const exif_offset_info *info)
2106
{
2107
  return info->valid_end - info->valid_start;
2108
}
2109
#endif
2110
2111
/* {{{ exif_file_sections_add
2112
 Add a file_section to image_info
2113
 returns the used block or -1. if size>0 and data == NULL buffer of size is allocated
2114
*/
2115
static int exif_file_sections_add(image_info_type *ImageInfo, int type, size_t size, uchar *data)
2116
761k
{
2117
761k
  int count = ImageInfo->file.count;
2118
761k
  if (count == ImageInfo->file.alloc_count) {
2119
18.4k
    int new_alloc_count = ImageInfo->file.alloc_count ? ImageInfo->file.alloc_count * 2 : 1;
2120
18.4k
    ImageInfo->file.list = safe_erealloc(
2121
18.4k
      ImageInfo->file.list, new_alloc_count, sizeof(file_section), 0);
2122
18.4k
    ImageInfo->file.alloc_count = new_alloc_count;
2123
18.4k
  }
2124
2125
761k
  ImageInfo->file.list[count].type = 0xFFFF;
2126
761k
  ImageInfo->file.list[count].data = NULL;
2127
761k
  ImageInfo->file.list[count].size = 0;
2128
761k
  ImageInfo->file.count = count+1;
2129
761k
  if (!size) {
2130
0
    data = NULL;
2131
761k
  } else if (data == NULL) {
2132
761k
    data = safe_emalloc(size, 1, 0);
2133
761k
  }
2134
761k
  ImageInfo->file.list[count].type = type;
2135
761k
  ImageInfo->file.list[count].data = data;
2136
761k
  ImageInfo->file.list[count].size = size;
2137
761k
  return count;
2138
761k
}
2139
/* }}} */
2140
2141
/* {{{ exif_file_sections_realloc
2142
 Reallocate a file section returns 0 on success and -1 on failure
2143
*/
2144
static int exif_file_sections_realloc(image_info_type *ImageInfo, int section_index, size_t size)
2145
173k
{
2146
173k
  void *tmp;
2147
2148
  /* This is not a malloc/realloc check. It is a plausibility check for the
2149
   * function parameters (requirements engineering).
2150
   */
2151
173k
  if (section_index >= ImageInfo->file.count) {
2152
0
    EXIF_ERRLOG_FSREALLOC(ImageInfo)
2153
0
    return -1;
2154
0
  }
2155
173k
  tmp = safe_erealloc(ImageInfo->file.list[section_index].data, 1, size, 0);
2156
173k
  ImageInfo->file.list[section_index].data = tmp;
2157
173k
  ImageInfo->file.list[section_index].size = size;
2158
173k
  return 0;
2159
173k
}
2160
/* }}} */
2161
2162
/* {{{ exif_file_section_free
2163
   Discard all file_sections in ImageInfo
2164
*/
2165
static void exif_file_sections_free(image_info_type *ImageInfo)
2166
6.11k
{
2167
6.11k
  if (ImageInfo->file.count) {
2168
767k
    for (int i = 0; i<ImageInfo->file.count; i++) {
2169
761k
      EFREE_IF(ImageInfo->file.list[i].data);
2170
761k
    }
2171
6.00k
  }
2172
6.11k
  EFREE_IF(ImageInfo->file.list);
2173
6.11k
  ImageInfo->file.count = 0;
2174
6.11k
}
2175
/* }}} */
2176
2177
750k
static image_info_data *exif_alloc_image_info_data(image_info_list *info_list) {
2178
750k
  if (info_list->count == info_list->alloc_count) {
2179
48.9k
    int new_alloc_count = info_list->alloc_count ? info_list->alloc_count * 2 : 1;
2180
48.9k
    info_list->list = safe_erealloc(
2181
48.9k
      info_list->list, new_alloc_count, sizeof(image_info_data), 0);
2182
48.9k
    info_list->alloc_count = new_alloc_count;
2183
48.9k
  }
2184
750k
  return &info_list->list[info_list->count++];
2185
750k
}
2186
2187
/* {{{ exif_iif_add_value
2188
 Add a value to image_info
2189
*/
2190
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)
2191
713k
{
2192
713k
  size_t idex;
2193
713k
  void *vptr, *vptr_end;
2194
713k
  image_info_value *info_value;
2195
713k
  image_info_data  *info_data;
2196
2197
713k
  info_data = exif_alloc_image_info_data(&image_info->info_list[section_index]);
2198
713k
  memset(info_data, 0, sizeof(image_info_data));
2199
713k
  info_data->tag    = tag;
2200
713k
  info_data->format = format;
2201
713k
  info_data->length = length;
2202
713k
  info_data->name   = estrdup(name);
2203
713k
  info_value        = &info_data->value;
2204
2205
713k
  switch (format) {
2206
586k
    case TAG_FMT_STRING:
2207
586k
      if (length > value_len) {
2208
0
        exif_error_docref("exif_iif_add_value" EXIFERR_CC, image_info, E_WARNING, "length > value_len: %d > %zu", length, value_len);
2209
0
        value = NULL;
2210
0
      }
2211
586k
      if (value) {
2212
586k
        length = zend_strnlen(value, length);
2213
586k
        info_value->s = estrndup(value, length);
2214
586k
        info_data->length = length;
2215
586k
      } else {
2216
0
        info_data->length = 0;
2217
0
        info_value->s = estrdup("");
2218
0
      }
2219
586k
      break;
2220
2221
1.00k
    default:
2222
      /* Standard says more types possible but skip them...
2223
       * but allow users to handle data if they know how to
2224
       * So not return but use type UNDEFINED
2225
       * return;
2226
       */
2227
1.00k
      info_data->tag = TAG_FMT_UNDEFINED;/* otherwise not freed from memory */
2228
1.00k
      ZEND_FALLTHROUGH;
2229
2.97k
    case TAG_FMT_SBYTE:
2230
57.9k
    case TAG_FMT_BYTE:
2231
    /* in contrast to strings bytes do not need to allocate buffer for NULL if length==0 */
2232
57.9k
      if (!length) {
2233
10.7k
        break;
2234
10.7k
      }
2235
47.1k
      ZEND_FALLTHROUGH;
2236
51.7k
    case TAG_FMT_UNDEFINED:
2237
51.7k
      if (length > value_len) {
2238
0
        exif_error_docref("exif_iif_add_value" EXIFERR_CC, image_info, E_WARNING, "length > value_len: %d > %zu", length, value_len);
2239
0
        value = NULL;
2240
0
      }
2241
51.7k
      if (value) {
2242
51.7k
        if (tag == TAG_MAKER_NOTE) {
2243
4.32k
          length = zend_strnlen(value, length);
2244
4.32k
        }
2245
2246
        /* do not recompute length here */
2247
51.7k
        info_value->s = estrndup(value, length);
2248
51.7k
        info_data->length = length;
2249
51.7k
      } else {
2250
0
        info_data->length = 0;
2251
0
        info_value->s = estrdup("");
2252
0
      }
2253
51.7k
      break;
2254
2255
7.78k
    case TAG_FMT_USHORT:
2256
11.8k
    case TAG_FMT_ULONG:
2257
15.4k
    case TAG_FMT_URATIONAL:
2258
27.2k
    case TAG_FMT_SSHORT:
2259
34.9k
    case TAG_FMT_SLONG:
2260
40.3k
    case TAG_FMT_SRATIONAL:
2261
56.5k
    case TAG_FMT_SINGLE:
2262
64.7k
    case TAG_FMT_DOUBLE:
2263
64.7k
      if (length==0) {
2264
2.35k
        break;
2265
2.35k
      }
2266
62.3k
      if (length>1) {
2267
35.8k
        info_value->list = safe_emalloc(length, sizeof(image_info_value), 0);
2268
35.8k
      } else {
2269
26.4k
        info_value = &info_data->value;
2270
26.4k
      }
2271
62.3k
      vptr_end = (char *) value + value_len;
2272
3.85M
      for (idex=0,vptr=value; idex<length; idex++,vptr=(char *) vptr + php_tiff_bytes_per_format[format]) {
2273
3.79M
        if ((char *) vptr_end - (char *) vptr < php_tiff_bytes_per_format[format]) {
2274
0
          exif_error_docref("exif_iif_add_value" EXIFERR_CC, image_info, E_WARNING, "Value too short");
2275
0
          break;
2276
0
        }
2277
3.79M
        if (length>1) {
2278
3.76M
          info_value = &info_data->value.list[idex];
2279
3.76M
        }
2280
3.79M
        switch (format) {
2281
2.80M
          case TAG_FMT_USHORT:
2282
2.80M
            info_value->u = php_ifd_get16u(vptr, motorola_intel);
2283
2.80M
            break;
2284
2285
62.4k
          case TAG_FMT_ULONG:
2286
62.4k
            info_value->u = php_ifd_get32u(vptr, motorola_intel);
2287
62.4k
            break;
2288
2289
12.3k
          case TAG_FMT_URATIONAL:
2290
12.3k
            info_value->ur.num = php_ifd_get32u(vptr, motorola_intel);
2291
12.3k
            info_value->ur.den = php_ifd_get32u(4+(char *)vptr, motorola_intel);
2292
12.3k
            break;
2293
2294
410k
          case TAG_FMT_SSHORT:
2295
410k
            info_value->i = php_ifd_get16s(vptr, motorola_intel);
2296
410k
            break;
2297
2298
37.9k
          case TAG_FMT_SLONG:
2299
37.9k
            info_value->i = php_ifd_get32s(vptr, motorola_intel);
2300
37.9k
            break;
2301
2302
81.0k
          case TAG_FMT_SRATIONAL:
2303
81.0k
            info_value->sr.num = php_ifd_get32u(vptr, motorola_intel);
2304
81.0k
            info_value->sr.den = php_ifd_get32u(4+(char *)vptr, motorola_intel);
2305
81.0k
            break;
2306
2307
293k
          case TAG_FMT_SINGLE:
2308
#ifdef EXIF_DEBUG
2309
            php_error_docref(NULL, E_WARNING, "Found value of type single");
2310
#endif
2311
293k
            info_value->f = php_ifd_get_float(value);
2312
293k
            break;
2313
96.1k
          case TAG_FMT_DOUBLE:
2314
#ifdef EXIF_DEBUG
2315
            php_error_docref(NULL, E_WARNING, "Found value of type double");
2316
#endif
2317
96.1k
            info_value->d = php_ifd_get_double(value);
2318
96.1k
            break;
2319
3.79M
        }
2320
3.79M
      }
2321
713k
  }
2322
713k
  image_info->sections_found |= 1<<section_index;
2323
713k
}
2324
/* }}} */
2325
2326
/* {{{ exif_iif_add_tag
2327
 Add a tag from IFD to image_info
2328
*/
2329
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)
2330
713k
{
2331
713k
  exif_iif_add_value(image_info, section_index, name, tag, format, length, value, value_len, image_info->motorola_intel);
2332
713k
}
2333
/* }}} */
2334
2335
/* {{{ exif_iif_add_int
2336
 Add an int value to image_info
2337
*/
2338
static void exif_iif_add_int(image_info_type *image_info, int section_index, const char *name, int value)
2339
22.7k
{
2340
22.7k
  image_info_data *info_data = exif_alloc_image_info_data(&image_info->info_list[section_index]);
2341
22.7k
  info_data->tag    = TAG_NONE;
2342
22.7k
  info_data->format = TAG_FMT_SLONG;
2343
22.7k
  info_data->length = 1;
2344
22.7k
  info_data->name   = estrdup(name);
2345
22.7k
  info_data->value.i = value;
2346
22.7k
  image_info->sections_found |= 1<<section_index;
2347
22.7k
}
2348
/* }}} */
2349
2350
/* {{{ exif_iif_add_str
2351
 Add a string value to image_info MUST BE NUL TERMINATED
2352
*/
2353
static void exif_iif_add_str(image_info_type *image_info, int section_index, const char *name, const char *value)
2354
31.6k
{
2355
31.6k
  if (value) {
2356
13.8k
    image_info_data *info_data =
2357
13.8k
      exif_alloc_image_info_data(&image_info->info_list[section_index]);
2358
13.8k
    info_data->tag    = TAG_NONE;
2359
13.8k
    info_data->format = TAG_FMT_STRING;
2360
13.8k
    info_data->length = 1;
2361
13.8k
    info_data->name   = estrdup(name);
2362
13.8k
    info_data->value.s = estrdup(value);
2363
13.8k
    image_info->sections_found |= 1<<section_index;
2364
13.8k
  }
2365
31.6k
}
2366
/* }}} */
2367
2368
/* {{{ exif_iif_add_fmt
2369
 Add a format string value to image_info MUST BE NUL TERMINATED
2370
*/
2371
static void exif_iif_add_fmt(image_info_type *image_info, int section_index, const char *name, char *value, ...)
2372
550
{
2373
550
  char             *tmp;
2374
550
  va_list      arglist;
2375
2376
550
  va_start(arglist, value);
2377
550
  if (value) {
2378
550
    vspprintf(&tmp, 0, value, arglist);
2379
550
    exif_iif_add_str(image_info, section_index, name, tmp);
2380
550
    efree(tmp);
2381
550
  }
2382
550
  va_end(arglist);
2383
550
}
2384
/* }}} */
2385
2386
/* {{{ exif_iif_add_str
2387
 Add a string value to image_info MUST BE NUL TERMINATED
2388
*/
2389
static void exif_iif_add_buffer(image_info_type *image_info, int section_index, const char *name, int length, const char *value)
2390
293
{
2391
293
  if (value) {
2392
293
    image_info_data *info_data =
2393
293
      exif_alloc_image_info_data(&image_info->info_list[section_index]);
2394
293
    info_data->tag    = TAG_NONE;
2395
293
    info_data->format = TAG_FMT_UNDEFINED;
2396
293
    info_data->length = length;
2397
293
    info_data->name   = estrdup(name);
2398
293
    info_data->value.s = safe_emalloc(length, 1, 1);
2399
293
    memcpy(info_data->value.s, value, length);
2400
293
    info_data->value.s[length] = 0;
2401
293
    image_info->sections_found |= 1<<section_index;
2402
293
  }
2403
293
}
2404
/* }}} */
2405
2406
/* {{{ exif_iif_free
2407
 Free memory allocated for image_info
2408
*/
2409
85.5k
static void exif_iif_free(image_info_type *image_info, int section_index) {
2410
85.5k
  void *f; /* faster */
2411
2412
85.5k
  if (image_info->info_list[section_index].count) {
2413
766k
    for (int i = 0; i < image_info->info_list[section_index].count; i++) {
2414
750k
      if ((f=image_info->info_list[section_index].list[i].name) != NULL) {
2415
750k
        efree(f);
2416
750k
      }
2417
750k
      switch(image_info->info_list[section_index].list[i].format) {
2418
4.90k
        case TAG_FMT_UNDEFINED:
2419
605k
        case TAG_FMT_STRING:
2420
607k
        case TAG_FMT_SBYTE:
2421
662k
        case TAG_FMT_BYTE:
2422
663k
        default:
2423
663k
          if ((f=image_info->info_list[section_index].list[i].value.s) != NULL) {
2424
652k
            efree(f);
2425
652k
          }
2426
663k
          break;
2427
2428
7.78k
        case TAG_FMT_USHORT:
2429
11.8k
        case TAG_FMT_ULONG:
2430
15.4k
        case TAG_FMT_URATIONAL:
2431
27.2k
        case TAG_FMT_SSHORT:
2432
57.7k
        case TAG_FMT_SLONG:
2433
63.1k
        case TAG_FMT_SRATIONAL:
2434
79.3k
        case TAG_FMT_SINGLE:
2435
87.5k
        case TAG_FMT_DOUBLE:
2436
          /* nothing to do here */
2437
87.5k
          if (image_info->info_list[section_index].list[i].length > 1) {
2438
35.8k
            if ((f=image_info->info_list[section_index].list[i].value.list) != NULL) {
2439
35.8k
              efree(f);
2440
35.8k
            }
2441
35.8k
          }
2442
87.5k
          break;
2443
750k
      }
2444
750k
    }
2445
15.9k
  }
2446
85.5k
  EFREE_IF(image_info->info_list[section_index].list);
2447
85.5k
}
2448
/* }}} */
2449
2450
/* {{{ add_assoc_image_info
2451
 * Add image_info to associative array value. */
2452
static void add_assoc_image_info(zval *value, int sub_array, image_info_type *image_info, int section_index)
2453
56.8k
{
2454
56.8k
  int idx = 0, unknown = 0;
2455
2456
56.8k
  if (!image_info->info_list[section_index].count) {
2457
41.9k
    return;
2458
41.9k
  }
2459
2460
14.9k
  zval tmpi;
2461
14.9k
  if (sub_array) {
2462
6.23k
    array_init(&tmpi);
2463
8.69k
  } else {
2464
8.69k
    ZVAL_COPY_VALUE(&tmpi, value);
2465
8.69k
  }
2466
2467
409k
  for (int i = 0; i<image_info->info_list[section_index].count; i++) {
2468
394k
    image_info_data *info_data = &image_info->info_list[section_index].list[i];
2469
394k
    image_info_value *info_value = &info_data->value;
2470
394k
    const char *name = info_data->name;
2471
394k
    char uname[64];
2472
2473
394k
    if (!name) {
2474
0
      snprintf(uname, sizeof(uname), "%d", unknown++);
2475
0
      name = uname;
2476
0
    }
2477
2478
394k
    if (info_data->length == 0) {
2479
12.1k
      add_assoc_null(&tmpi, name);
2480
382k
    } else {
2481
382k
      switch (info_data->format) {
2482
914
        default:
2483
          /* Standard says more types possible but skip them...
2484
           * but allow users to handle data if they know how to
2485
           * So not return but use type UNDEFINED
2486
           * return;
2487
           */
2488
35.3k
        case TAG_FMT_BYTE:
2489
37.1k
        case TAG_FMT_SBYTE:
2490
40.7k
        case TAG_FMT_UNDEFINED:
2491
40.7k
          if (!info_value->s) {
2492
0
            add_assoc_stringl(&tmpi, name, "", 0);
2493
40.7k
          } else {
2494
40.7k
            add_assoc_stringl(&tmpi, name, info_value->s, info_data->length);
2495
40.7k
          }
2496
40.7k
          break;
2497
2498
259k
        case TAG_FMT_STRING: {
2499
259k
          const char *val = info_value->s ? info_value->s : "";
2500
259k
          if (section_index==SECTION_COMMENT) {
2501
243k
            add_index_string(&tmpi, idx++, val);
2502
243k
          } else {
2503
16.1k
            add_assoc_string(&tmpi, name, val);
2504
16.1k
          }
2505
259k
          break;
2506
37.1k
        }
2507
2508
3.37k
        case TAG_FMT_URATIONAL:
2509
8.41k
        case TAG_FMT_SRATIONAL:
2510
14.7k
        case TAG_FMT_USHORT:
2511
25.7k
        case TAG_FMT_SSHORT:
2512
41.5k
        case TAG_FMT_SINGLE:
2513
49.0k
        case TAG_FMT_DOUBLE:
2514
52.6k
        case TAG_FMT_ULONG:
2515
82.1k
        case TAG_FMT_SLONG: {
2516
          /* now the rest, first see if it becomes an array */
2517
82.1k
          zval array;
2518
82.1k
          int l = info_data->length;
2519
82.1k
          if (l > 1) {
2520
35.1k
            array_init(&array);
2521
35.1k
          }
2522
3.85M
          for (int ap = 0; ap < l; ap++) {
2523
3.77M
            char buffer[64];
2524
3.77M
            if (l>1) {
2525
3.72M
              info_value = &info_data->value.list[ap];
2526
3.72M
            }
2527
3.77M
            switch (info_data->format) {
2528
0
              case TAG_FMT_BYTE:
2529
0
                if (l>1) {
2530
0
                  info_value = &info_data->value;
2531
0
                  for (int b = 0; b < l; b++) {
2532
0
                    add_index_long(&array, b, (int)(info_value->s[b]));
2533
0
                  }
2534
0
                  break;
2535
0
                }
2536
0
                ZEND_FALLTHROUGH;
2537
2.79M
              case TAG_FMT_USHORT:
2538
2.84M
              case TAG_FMT_ULONG:
2539
2.84M
                if (l==1) {
2540
4.09k
                  add_assoc_long(&tmpi, name, (int)info_value->u);
2541
2.84M
                } else {
2542
2.84M
                  add_index_long(&array, ap, (int)info_value->u);
2543
2.84M
                }
2544
2.84M
                break;
2545
2546
9.91k
              case TAG_FMT_URATIONAL:
2547
9.91k
                snprintf(buffer, sizeof(buffer), "%u/%u", info_value->ur.num, info_value->ur.den);
2548
9.91k
                if (l==1) {
2549
1.34k
                  add_assoc_string(&tmpi, name, buffer);
2550
8.56k
                } else {
2551
8.56k
                  add_index_string(&array, ap, buffer);
2552
8.56k
                }
2553
9.91k
                break;
2554
2555
0
              case TAG_FMT_SBYTE:
2556
0
                if (l>1) {
2557
0
                  info_value = &info_data->value;
2558
0
                  for (int b = 0; b < l; b++) {
2559
0
                    add_index_long(&array, ap, (int)info_value->s[b]);
2560
0
                  }
2561
0
                  break;
2562
0
                }
2563
0
                ZEND_FALLTHROUGH;
2564
384k
              case TAG_FMT_SSHORT:
2565
444k
              case TAG_FMT_SLONG:
2566
444k
                if (l==1) {
2567
30.9k
                  add_assoc_long(&tmpi, name, info_value->i);
2568
413k
                } else {
2569
413k
                  add_index_long(&array, ap, info_value->i);
2570
413k
                }
2571
444k
                break;
2572
2573
80.7k
              case TAG_FMT_SRATIONAL:
2574
80.7k
                snprintf(buffer, sizeof(buffer), "%i/%i", info_value->sr.num, info_value->sr.den);
2575
80.7k
                if (l==1) {
2576
2.76k
                  add_assoc_string(&tmpi, name, buffer);
2577
77.9k
                } else {
2578
77.9k
                  add_index_string(&array, ap, buffer);
2579
77.9k
                }
2580
80.7k
                break;
2581
2582
290k
              case TAG_FMT_SINGLE:
2583
290k
                if (l==1) {
2584
3.67k
                  add_assoc_double(&tmpi, name, info_value->f);
2585
287k
                } else {
2586
287k
                  add_index_double(&array, ap, info_value->f);
2587
287k
                }
2588
290k
                break;
2589
2590
95.6k
              case TAG_FMT_DOUBLE:
2591
95.6k
                if (l==1) {
2592
4.19k
                  add_assoc_double(&tmpi, name, info_value->d);
2593
91.5k
                } else {
2594
91.5k
                  add_index_double(&array, ap, info_value->d);
2595
91.5k
                }
2596
95.6k
                break;
2597
3.77M
            }
2598
3.77M
          }
2599
82.1k
          if (l > 1) {
2600
35.1k
            add_assoc_zval(&tmpi, name, &array);
2601
35.1k
          }
2602
82.1k
          break;
2603
82.1k
        }
2604
382k
      }
2605
382k
    }
2606
394k
  }
2607
14.9k
  if (sub_array) {
2608
6.23k
    add_assoc_zval(value, exif_get_sectionname(section_index), &tmpi);
2609
6.23k
  }
2610
14.9k
}
2611
/* }}} */
2612
2613
/* {{{ Markers
2614
   JPEG markers consist of one or more 0xFF bytes, followed by a marker
2615
   code byte (which is not an FF).  Here are the marker codes of interest
2616
   in this program.  (See jdmarker.c for a more complete list.)
2617
*/
2618
2619
#define M_TEM   0x01    /* temp for arithmetic coding              */
2620
#define M_RES   0x02    /* reserved                                */
2621
202
#define M_SOF0  0xC0    /* Start Of Frame N                        */
2622
429
#define M_SOF1  0xC1    /* N indicates which compression process   */
2623
632
#define M_SOF2  0xC2    /* Only SOF0-SOF2 are now in common use    */
2624
945
#define M_SOF3  0xC3
2625
#define M_DHT   0xC4
2626
1.14k
#define M_SOF5  0xC5    /* NB: codes C4 and CC are NOT SOF markers */
2627
1.34k
#define M_SOF6  0xC6
2628
1.53k
#define M_SOF7  0xC7
2629
#define M_JPEG  0x08    /* reserved for extensions                 */
2630
1.73k
#define M_SOF9  0xC9
2631
2.10k
#define M_SOF10 0xCA
2632
2.31k
#define M_SOF11 0xCB
2633
#define M_DAC   0xCC    /* arithmetic table                         */
2634
2.65k
#define M_SOF13 0xCD
2635
2.86k
#define M_SOF14 0xCE
2636
3.06k
#define M_SOF15 0xCF
2637
#define M_RST0  0xD0    /* restart segment                          */
2638
#define M_RST1  0xD1
2639
#define M_RST2  0xD2
2640
#define M_RST3  0xD3
2641
#define M_RST4  0xD4
2642
#define M_RST5  0xD5
2643
#define M_RST6  0xD6
2644
#define M_RST7  0xD7
2645
1.17k
#define M_SOI   0xD8    /* Start Of Image (beginning of datastream) */
2646
91
#define M_EOI   0xD9    /* End Of Image (end of datastream)         */
2647
53
#define M_SOS   0xDA    /* Start Of Scan (begins compressed data)   */
2648
#define M_DQT   0xDB
2649
#define M_DNL   0xDC
2650
#define M_DRI   0xDD
2651
#define M_DHP   0xDE
2652
#define M_EXP   0xDF
2653
#define M_APP0  0xE0    /* JPEG: 'JFIFF' AND (additional 'JFXX')    */
2654
3.17k
#define M_EXIF  0xE1    /* Exif Attribute Information               */
2655
#define M_APP2  0xE2    /* Flash Pix Extension Data?                */
2656
#define M_APP3  0xE3
2657
#define M_APP4  0xE4
2658
#define M_APP5  0xE5
2659
#define M_APP6  0xE6
2660
#define M_APP7  0xE7
2661
#define M_APP8  0xE8
2662
#define M_APP9  0xE9
2663
#define M_APP10 0xEA
2664
#define M_APP11 0xEB
2665
928
#define M_APP12 0xEC
2666
#define M_APP13 0xED    /* IPTC International Press Telecommunications Council */
2667
#define M_APP14 0xEE    /* Software, Copyright?                     */
2668
#define M_APP15 0xEF
2669
#define M_JPG0  0xF0
2670
#define M_JPG1  0xF1
2671
#define M_JPG2  0xF2
2672
#define M_JPG3  0xF3
2673
#define M_JPG4  0xF4
2674
#define M_JPG5  0xF5
2675
#define M_JPG6  0xF6
2676
#define M_JPG7  0xF7
2677
#define M_JPG8  0xF8
2678
#define M_JPG9  0xF9
2679
#define M_JPG10 0xFA
2680
#define M_JPG11 0xFB
2681
#define M_JPG12 0xFC
2682
#define M_JPG13 0xFD
2683
5.32M
#define M_COM   0xFE    /* COMment                                  */
2684
2685
171k
#define M_PSEUDO 0x123  /* Extra value.                             */
2686
/* }}} */
2687
2688
/* {{{ exif_process_COM
2689
   Process a COM marker.
2690
   We want to print out the marker contents as legible text;
2691
   we must guard against random junk and varying newline representations.
2692
*/
2693
static void exif_process_COM (image_info_type *image_info, char *value, size_t length)
2694
582k
{
2695
582k
  exif_iif_add_tag(image_info, SECTION_COMMENT, "Comment", TAG_COMPUTED_VALUE, TAG_FMT_STRING, length-2, value+2, length-2);
2696
582k
}
2697
/* }}} */
2698
2699
/* {{{ exif_process_SOFn
2700
 * Process a SOFn marker.  This is useful for the image dimensions */
2701
static void exif_process_SOFn (uchar *Data, int marker, jpeg_sof_info *result)
2702
2.97k
{
2703
  /* 0xFF SOSn SectLen(2) Bits(1) Height(2) Width(2) Channels(1)  3*Channels (1)  */
2704
2.97k
  result->bits_per_sample = Data[2];
2705
2.97k
  result->height          = php_jpg_get16(Data+3);
2706
2.97k
  result->width           = php_jpg_get16(Data+5);
2707
2.97k
  result->num_components  = Data[7];
2708
2.97k
}
2709
/* }}} */
2710
2711
/* forward declarations */
2712
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);
2713
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);
2714
static bool exif_process_IFD_in_TIFF(image_info_type *ImageInfo, size_t dir_offset, int section_index);
2715
2716
/* {{{ exif_get_markername
2717
  Get name of marker */
2718
#ifdef EXIF_DEBUG
2719
static char * exif_get_markername(int marker)
2720
{
2721
  switch(marker) {
2722
    case 0xC0: return "SOF0";
2723
    case 0xC1: return "SOF1";
2724
    case 0xC2: return "SOF2";
2725
    case 0xC3: return "SOF3";
2726
    case 0xC4: return "DHT";
2727
    case 0xC5: return "SOF5";
2728
    case 0xC6: return "SOF6";
2729
    case 0xC7: return "SOF7";
2730
    case 0xC9: return "SOF9";
2731
    case 0xCA: return "SOF10";
2732
    case 0xCB: return "SOF11";
2733
    case 0xCD: return "SOF13";
2734
    case 0xCE: return "SOF14";
2735
    case 0xCF: return "SOF15";
2736
    case 0xD8: return "SOI";
2737
    case 0xD9: return "EOI";
2738
    case 0xDA: return "SOS";
2739
    case 0xDB: return "DQT";
2740
    case 0xDC: return "DNL";
2741
    case 0xDD: return "DRI";
2742
    case 0xDE: return "DHP";
2743
    case 0xDF: return "EXP";
2744
    case 0xE0: return "APP0";
2745
    case 0xE1: return "EXIF";
2746
    case 0xE2: return "FPIX";
2747
    case 0xE3: return "APP3";
2748
    case 0xE4: return "APP4";
2749
    case 0xE5: return "APP5";
2750
    case 0xE6: return "APP6";
2751
    case 0xE7: return "APP7";
2752
    case 0xE8: return "APP8";
2753
    case 0xE9: return "APP9";
2754
    case 0xEA: return "APP10";
2755
    case 0xEB: return "APP11";
2756
    case 0xEC: return "APP12";
2757
    case 0xED: return "APP13";
2758
    case 0xEE: return "APP14";
2759
    case 0xEF: return "APP15";
2760
    case 0xF0: return "JPG0";
2761
    case 0xFD: return "JPG13";
2762
    case 0xFE: return "COM";
2763
    case 0x01: return "TEM";
2764
  }
2765
  return "Unknown";
2766
}
2767
#endif
2768
/* }}} */
2769
2770
/* {{{ Get headername for index or false if not defined */
2771
PHP_FUNCTION(exif_tagname)
2772
0
{
2773
0
  zend_long tag;
2774
0
  char *szTemp;
2775
2776
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &tag) == FAILURE) {
2777
0
    RETURN_THROWS();
2778
0
  }
2779
2780
0
  szTemp = exif_get_tagname(tag, tag_table_IFD);
2781
0
  if (tag < 0 || !szTemp) {
2782
0
    RETURN_FALSE;
2783
0
  }
2784
2785
0
  RETURN_STRING(szTemp);
2786
0
}
2787
/* }}} */
2788
2789
/* {{{ exif_ifd_make_value
2790
 * Create a value for an ifd from an info_data pointer */
2791
0
static void* exif_ifd_make_value(image_info_data *info_data, int motorola_intel) {
2792
0
  size_t  byte_count;
2793
0
  char    *value_ptr;
2794
2795
0
  image_info_value  *info_value;
2796
2797
0
  byte_count = php_tiff_bytes_per_format[info_data->format] * info_data->length;
2798
0
  value_ptr = safe_emalloc(max(byte_count, 4), 1, 0);
2799
0
  memset(value_ptr, 0, 4);
2800
0
  if (!info_data->length) {
2801
0
    return value_ptr;
2802
0
  }
2803
0
  if (info_data->format == TAG_FMT_UNDEFINED || info_data->format == TAG_FMT_STRING
2804
0
    || (byte_count>1 && (info_data->format == TAG_FMT_BYTE || info_data->format == TAG_FMT_SBYTE))
2805
0
  ) {
2806
0
    memcpy(value_ptr, info_data->value.s, byte_count);
2807
0
    return value_ptr;
2808
0
  } else if (info_data->format == TAG_FMT_BYTE) {
2809
0
    *value_ptr = info_data->value.u;
2810
0
    return value_ptr;
2811
0
  } else if (info_data->format == TAG_FMT_SBYTE) {
2812
0
    *value_ptr = info_data->value.i;
2813
0
    return value_ptr;
2814
0
  } else {
2815
0
    char *data_ptr = value_ptr;
2816
0
    for(size_t i = 0; i < info_data->length; i++) {
2817
0
      if (info_data->length==1) {
2818
0
        info_value = &info_data->value;
2819
0
      } else {
2820
0
        info_value = &info_data->value.list[i];
2821
0
      }
2822
0
      switch(info_data->format) {
2823
0
        case TAG_FMT_USHORT:
2824
0
          php_ifd_set16u(data_ptr, info_value->u, motorola_intel);
2825
0
          data_ptr += 2;
2826
0
          break;
2827
0
        case TAG_FMT_ULONG:
2828
0
          php_ifd_set32u(data_ptr, info_value->u, motorola_intel);
2829
0
          data_ptr += 4;
2830
0
          break;
2831
0
        case TAG_FMT_SSHORT:
2832
0
          php_ifd_set16u(data_ptr, info_value->i, motorola_intel);
2833
0
          data_ptr += 2;
2834
0
          break;
2835
0
        case TAG_FMT_SLONG:
2836
0
          php_ifd_set32u(data_ptr, info_value->i, motorola_intel);
2837
0
          data_ptr += 4;
2838
0
          break;
2839
0
        case TAG_FMT_URATIONAL:
2840
0
          php_ifd_set32u(data_ptr,   info_value->sr.num, motorola_intel);
2841
0
          php_ifd_set32u(data_ptr+4, info_value->sr.den, motorola_intel);
2842
0
          data_ptr += 8;
2843
0
          break;
2844
0
        case TAG_FMT_SRATIONAL:
2845
0
          php_ifd_set32u(data_ptr,   info_value->ur.num, motorola_intel);
2846
0
          php_ifd_set32u(data_ptr+4, info_value->ur.den, motorola_intel);
2847
0
          data_ptr += 8;
2848
0
          break;
2849
0
        case TAG_FMT_SINGLE:
2850
0
          memcpy(data_ptr, &info_value->f, 4);
2851
0
          data_ptr += 4;
2852
0
          break;
2853
0
        case TAG_FMT_DOUBLE:
2854
0
          memcpy(data_ptr, &info_value->d, 8);
2855
0
          data_ptr += 8;
2856
0
          break;
2857
0
      }
2858
0
    }
2859
0
  }
2860
0
  return value_ptr;
2861
0
}
2862
/* }}} */
2863
2864
/* {{{ exif_thumbnail_build
2865
 * Check and build thumbnail */
2866
0
static void exif_thumbnail_build(image_info_type *ImageInfo) {
2867
0
  size_t            new_size, new_move, new_value;
2868
0
  char              *new_data;
2869
0
  void              *value_ptr;
2870
0
  int               i, byte_count;
2871
0
  image_info_list   *info_list;
2872
0
  image_info_data   *info_data;
2873
2874
0
  if (!ImageInfo->read_thumbnail || !ImageInfo->Thumbnail.offset || !ImageInfo->Thumbnail.size) {
2875
0
    return; /* ignore this call */
2876
0
  }
2877
#ifdef EXIF_DEBUG
2878
  exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Thumbnail: filetype = %d", ImageInfo->Thumbnail.filetype);
2879
#endif
2880
0
  switch(ImageInfo->Thumbnail.filetype) {
2881
0
    default:
2882
0
    case IMAGE_FILETYPE_JPEG:
2883
      /* done */
2884
0
      break;
2885
0
    case IMAGE_FILETYPE_TIFF_II:
2886
0
    case IMAGE_FILETYPE_TIFF_MM:
2887
0
      info_list = &ImageInfo->info_list[SECTION_THUMBNAIL];
2888
0
      new_size  = 8 + 2 + info_list->count * 12 + 4;
2889
#ifdef EXIF_DEBUG
2890
      exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Thumbnail: size of signature + directory(%d): 0x%02X", info_list->count, new_size);
2891
#endif
2892
0
      new_value= new_size; /* offset for ifd values outside ifd directory */
2893
0
      for (i=0; i<info_list->count; i++) {
2894
0
        info_data  = &info_list->list[i];
2895
0
        byte_count = php_tiff_bytes_per_format[info_data->format] * info_data->length;
2896
0
        if (byte_count > 4) {
2897
0
          new_size += byte_count;
2898
0
        }
2899
0
      }
2900
0
      new_move = new_size;
2901
0
      new_data = safe_erealloc(ImageInfo->Thumbnail.data, 1, ImageInfo->Thumbnail.size, new_size);
2902
0
      ImageInfo->Thumbnail.data = new_data;
2903
0
      memmove(ImageInfo->Thumbnail.data + new_move, ImageInfo->Thumbnail.data, ImageInfo->Thumbnail.size);
2904
0
      ImageInfo->Thumbnail.size += new_size;
2905
      /* fill in data */
2906
0
      if (ImageInfo->motorola_intel) {
2907
0
        memcpy(new_data, "MM\x00\x2a\x00\x00\x00\x08", 8);
2908
0
      } else {
2909
0
        memcpy(new_data, "II\x2a\x00\x08\x00\x00\x00", 8);
2910
0
      }
2911
0
      new_data += 8;
2912
0
      php_ifd_set16u(new_data, info_list->count, ImageInfo->motorola_intel);
2913
0
      new_data += 2;
2914
0
      for (i=0; i<info_list->count; i++) {
2915
0
        info_data  = &info_list->list[i];
2916
0
        byte_count = php_tiff_bytes_per_format[info_data->format] * info_data->length;
2917
#ifdef EXIF_DEBUG
2918
        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);
2919
#endif
2920
0
        if (info_data->tag==TAG_STRIP_OFFSETS || info_data->tag==TAG_JPEG_INTERCHANGE_FORMAT) {
2921
0
          php_ifd_set16u(new_data + 0, info_data->tag,    ImageInfo->motorola_intel);
2922
0
          php_ifd_set16u(new_data + 2, TAG_FMT_ULONG,     ImageInfo->motorola_intel);
2923
0
          php_ifd_set32u(new_data + 4, 1,                 ImageInfo->motorola_intel);
2924
0
          php_ifd_set32u(new_data + 8, new_move,          ImageInfo->motorola_intel);
2925
0
        } else {
2926
0
          php_ifd_set16u(new_data + 0, info_data->tag,    ImageInfo->motorola_intel);
2927
0
          php_ifd_set16u(new_data + 2, info_data->format, ImageInfo->motorola_intel);
2928
0
          php_ifd_set32u(new_data + 4, info_data->length, ImageInfo->motorola_intel);
2929
0
          value_ptr  = exif_ifd_make_value(info_data, ImageInfo->motorola_intel);
2930
0
          if (byte_count <= 4) {
2931
0
            memmove(new_data+8, value_ptr, 4);
2932
0
          } else {
2933
0
            php_ifd_set32u(new_data+8, new_value, ImageInfo->motorola_intel);
2934
#ifdef EXIF_DEBUG
2935
            exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Thumbnail: writing with value offset: 0x%04X + 0x%02X", new_value, byte_count);
2936
#endif
2937
0
            memmove(ImageInfo->Thumbnail.data+new_value, value_ptr, byte_count);
2938
0
            new_value += byte_count;
2939
0
          }
2940
0
          efree(value_ptr);
2941
0
        }
2942
0
        new_data += 12;
2943
0
      }
2944
0
      memset(new_data, 0, 4); /* next ifd pointer */
2945
#ifdef EXIF_DEBUG
2946
      exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Thumbnail: created");
2947
#endif
2948
0
      break;
2949
0
  }
2950
0
}
2951
/* }}} */
2952
2953
/* {{{ exif_thumbnail_extract
2954
 * Grab the thumbnail, corrected */
2955
0
static void exif_thumbnail_extract(image_info_type *ImageInfo, const exif_offset_info *info) {
2956
0
  if (ImageInfo->Thumbnail.data) {
2957
0
    exif_error_docref("exif_read_data#error_mult_thumb" EXIFERR_CC, ImageInfo, E_WARNING, "Multiple possible thumbnails");
2958
0
    return; /* Should not happen */
2959
0
  }
2960
0
  if (!ImageInfo->read_thumbnail) {
2961
0
    return; /* ignore this call */
2962
0
  }
2963
  /* according to exif2.1, the thumbnail is not supposed to be greater than 64K */
2964
0
  if (ImageInfo->Thumbnail.size >= 65536
2965
0
   || ImageInfo->Thumbnail.size <= 0
2966
0
   || ImageInfo->Thumbnail.offset <= 0
2967
0
  ) {
2968
0
    exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Illegal thumbnail size/offset");
2969
0
    return;
2970
0
  }
2971
  /* Check to make sure we are not going to go past the ExifLength */
2972
0
  char *thumbnail = exif_offset_info_try_get(
2973
0
    info, ImageInfo->Thumbnail.offset, ImageInfo->Thumbnail.size);
2974
0
  if (!thumbnail) {
2975
0
    EXIF_ERRLOG_THUMBEOF(ImageInfo)
2976
0
    return;
2977
0
  }
2978
0
  ImageInfo->Thumbnail.data = estrndup(thumbnail, ImageInfo->Thumbnail.size);
2979
0
  exif_thumbnail_build(ImageInfo);
2980
0
}
2981
/* }}} */
2982
2983
/* {{{ exif_process_undefined
2984
 * Copy a string/buffer in Exif header to a character string and return length of allocated buffer if any. */
2985
1.51k
static int exif_process_undefined(char **result, const char *value, size_t byte_count) {
2986
  /* we cannot use strlcpy - here the problem is that we have to copy NUL
2987
   * chars up to byte_count, we also have to add a single NUL character to
2988
   * force end of string.
2989
   * estrndup does not return length
2990
   */
2991
1.51k
  if (byte_count) {
2992
1.51k
    (*result) = estrndup(value, byte_count); /* NULL @ byte_count!!! */
2993
1.51k
    return byte_count+1;
2994
1.51k
  }
2995
0
  return 0;
2996
1.51k
}
2997
/* }}} */
2998
2999
/* {{{ exif_process_string_raw
3000
 * Copy a string in Exif header to a character string returns length of allocated buffer if any. */
3001
6.25k
static int exif_process_string_raw(char **result, const char *value, size_t byte_count) {
3002
  /* we cannot use strlcpy - here the problem is that we have to copy NUL
3003
   * chars up to byte_count, we also have to add a single NUL character to
3004
   * force end of string.
3005
   */
3006
6.25k
  if (byte_count) {
3007
4.45k
    (*result) = safe_emalloc(byte_count, 1, 1);
3008
4.45k
    memcpy(*result, value, byte_count);
3009
4.45k
    (*result)[byte_count] = '\0';
3010
4.45k
    return byte_count+1;
3011
4.45k
  }
3012
1.79k
  return 0;
3013
6.25k
}
3014
/* }}} */
3015
3016
/* {{{ exif_process_string
3017
 * Copy a string in Exif header to a character string and return length of allocated buffer if any.
3018
 * In contrast to exif_process_string this function does always return a string buffer */
3019
2.21k
static int exif_process_string(char **result, const char *value, size_t byte_count) {
3020
  /* we cannot use strlcpy - here the problem is that we cannot use strlen to
3021
   * determine length of string and we cannot use strlcpy with len=byte_count+1
3022
   * because then we might get into an EXCEPTION if we exceed an allocated
3023
   * memory page...so we use zend_strnlen in conjunction with memcpy and add the NUL
3024
   * char.
3025
   * estrdup would sometimes allocate more memory and does not return length
3026
   */
3027
2.21k
  if ((byte_count=zend_strnlen(value, byte_count)) > 0) {
3028
1.51k
    return exif_process_undefined(result, value, byte_count);
3029
1.51k
  }
3030
701
  (*result) = estrndup("", 1); /* force empty string */
3031
701
  return byte_count+1;
3032
2.21k
}
3033
/* }}} */
3034
3035
/* {{{ exif_process_user_comment
3036
 * Process UserComment in IFD. */
3037
static int exif_process_user_comment(const image_info_type *ImageInfo, char **pszInfoPtr, char **pszEncoding, char *szValuePtr, int ByteCount)
3038
4.28k
{
3039
4.28k
  size_t len;
3040
3041
4.28k
  *pszEncoding = NULL;
3042
  /* Copy the comment */
3043
4.28k
  if (ByteCount>=8) {
3044
3.69k
    const zend_encoding *from, *to;
3045
3.69k
    if (!memcmp(szValuePtr, "UNICODE\0", 8)) {
3046
1.37k
      char  *decode;
3047
1.37k
      *pszEncoding = estrdup(szValuePtr);
3048
1.37k
      szValuePtr = szValuePtr+8;
3049
1.37k
      ByteCount -= 8;
3050
      /* First try to detect BOM: ZERO WIDTH NOBREAK SPACE (FEFF 16)
3051
       * since we have no encoding support for the BOM yet we skip that.
3052
       */
3053
1.37k
      if (ByteCount >=2 && !memcmp(szValuePtr, "\xFE\xFF", 2)) {
3054
216
        decode = "UCS-2BE";
3055
216
        szValuePtr = szValuePtr+2;
3056
216
        ByteCount -= 2;
3057
1.15k
      } else if (ByteCount >=2 && !memcmp(szValuePtr, "\xFF\xFE", 2)) {
3058
216
        decode = "UCS-2LE";
3059
216
        szValuePtr = szValuePtr+2;
3060
216
        ByteCount -= 2;
3061
938
      } else if (ImageInfo->motorola_intel) {
3062
217
        decode = ImageInfo->decode_unicode_be;
3063
721
      } else {
3064
721
        decode = ImageInfo->decode_unicode_le;
3065
721
      }
3066
1.37k
      to = zend_multibyte_fetch_encoding(ImageInfo->encode_unicode);
3067
1.37k
      from = zend_multibyte_fetch_encoding(decode);
3068
      /* XXX this will fail again if encoding_converter returns on error something different than SIZE_MAX   */
3069
1.37k
      if (!to || !from || zend_multibyte_encoding_converter(
3070
0
          (unsigned char**)pszInfoPtr,
3071
0
          &len,
3072
0
          (unsigned char*)szValuePtr,
3073
0
          ByteCount,
3074
0
          to,
3075
1.37k
          from) == (size_t)-1) {
3076
1.37k
        len = exif_process_string_raw(pszInfoPtr, szValuePtr, ByteCount);
3077
1.37k
      }
3078
1.37k
      return len;
3079
2.32k
    } else if (!memcmp(szValuePtr, "ASCII\0\0\0", 8)) {
3080
233
      *pszEncoding = estrdup(szValuePtr);
3081
233
      szValuePtr = szValuePtr+8;
3082
233
      ByteCount -= 8;
3083
2.09k
    } else if (!memcmp(szValuePtr, "JIS\0\0\0\0\0", 8)) {
3084
      /* JIS should be translated to MB or we leave it to the user - leave it to the user */
3085
697
      *pszEncoding = estrdup(szValuePtr);
3086
697
      szValuePtr = szValuePtr+8;
3087
697
      ByteCount -= 8;
3088
      /* XXX this will fail again if encoding_converter returns on error something different than SIZE_MAX   */
3089
697
      to = zend_multibyte_fetch_encoding(ImageInfo->encode_jis);
3090
697
      from = zend_multibyte_fetch_encoding(ImageInfo->motorola_intel ? ImageInfo->decode_jis_be : ImageInfo->decode_jis_le);
3091
697
      if (!to || !from || zend_multibyte_encoding_converter(
3092
0
          (unsigned char**)pszInfoPtr,
3093
0
          &len,
3094
0
          (unsigned char*)szValuePtr,
3095
0
          ByteCount,
3096
0
          to,
3097
697
          from) == (size_t)-1) {
3098
697
        len = exif_process_string_raw(pszInfoPtr, szValuePtr, ByteCount);
3099
697
      }
3100
697
      return len;
3101
1.39k
    } else if (!memcmp(szValuePtr, "\0\0\0\0\0\0\0\0", 8)) {
3102
      /* 8 NULL means undefined and should be ASCII... */
3103
245
      *pszEncoding = estrdup("UNDEFINED");
3104
245
      szValuePtr = szValuePtr+8;
3105
245
      ByteCount -= 8;
3106
245
    }
3107
3.69k
  }
3108
3109
  /* Olympus has this padded with trailing spaces.  Remove these first. */
3110
2.21k
  if (ByteCount>0) {
3111
2.08k
    for (int a = ByteCount-1; a && szValuePtr[a]==' '; a--) {
3112
374
      (szValuePtr)[a] = '\0';
3113
374
    }
3114
1.71k
  }
3115
3116
  /* normal text without encoding */
3117
2.21k
  exif_process_string(pszInfoPtr, szValuePtr, ByteCount);
3118
2.21k
  return strlen(*pszInfoPtr);
3119
4.28k
}
3120
/* }}} */
3121
3122
/* {{{ exif_process_unicode
3123
 * Process unicode field in IFD. */
3124
static int exif_process_unicode(const image_info_type *ImageInfo, xp_field_type *xp_field, int tag, const char *szValuePtr, int ByteCount)
3125
4.18k
{
3126
4.18k
  xp_field->tag = tag;
3127
4.18k
  xp_field->value = NULL;
3128
  /* XXX this will fail again if encoding_converter returns on error something different than SIZE_MAX   */
3129
4.18k
  if (zend_multibyte_encoding_converter(
3130
4.18k
      (unsigned char**)&xp_field->value,
3131
4.18k
      &xp_field->size,
3132
4.18k
      (const unsigned char*)szValuePtr,
3133
4.18k
      ByteCount,
3134
4.18k
      zend_multibyte_fetch_encoding(ImageInfo->encode_unicode),
3135
4.18k
      zend_multibyte_fetch_encoding(ImageInfo->motorola_intel ? ImageInfo->decode_unicode_be : ImageInfo->decode_unicode_le)
3136
4.18k
      ) == (size_t)-1) {
3137
4.18k
    xp_field->size = exif_process_string_raw(&xp_field->value, szValuePtr, ByteCount);
3138
4.18k
  }
3139
4.18k
  return xp_field->size;
3140
4.18k
}
3141
/* }}} */
3142
3143
/* {{{ exif_process_IFD_in_MAKERNOTE
3144
 * Process nested IFDs directories in Maker Note. */
3145
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)
3146
5.04k
{
3147
5.04k
  int section_index = SECTION_MAKERNOTE;
3148
5.04k
  int NumDirEntries, old_motorola_intel;
3149
5.04k
  const maker_note_type *maker_note;
3150
5.04k
  char *dir_start;
3151
5.04k
  exif_offset_info new_info;
3152
3153
58.1k
  for (size_t i = 0; i <= sizeof(maker_note_array)/sizeof(maker_note_type); i++) {
3154
58.1k
    if (i==sizeof(maker_note_array)/sizeof(maker_note_type)) {
3155
#ifdef EXIF_DEBUG
3156
      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);
3157
#endif
3158
      /* unknown manufacturer, not an error, use it as a string */
3159
1.05k
      return true;
3160
1.05k
    }
3161
3162
57.0k
    maker_note = maker_note_array+i;
3163
3164
57.0k
    if (maker_note->make && (!ImageInfo->make || strcmp(maker_note->make, ImageInfo->make)))
3165
51.3k
      continue;
3166
5.67k
    if (maker_note->id_string && value_len >= maker_note->id_string_len
3167
5.67k
        && strncmp(maker_note->id_string, value_ptr, maker_note->id_string_len))
3168
1.68k
      continue;
3169
3.99k
    break;
3170
5.67k
  }
3171
3172
3.99k
  if (value_len < 2 || maker_note->offset >= value_len - 1) {
3173
    /* Do not go past the value end */
3174
845
    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);
3175
845
    return true;
3176
845
  }
3177
3178
3.14k
  if (UNEXPECTED(maker_note->tag_table == REQUIRES_CUSTOM_PARSING)) {
3179
    /* Custom parsing required, which is not implemented at this point
3180
     * Return true so that other metadata can still be parsed. */
3181
81
    return true;
3182
81
  }
3183
3184
3.06k
  dir_start = value_ptr + maker_note->offset;
3185
3186
#ifdef EXIF_DEBUG
3187
  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));
3188
#endif
3189
3190
3.06k
  ImageInfo->sections_found |= FOUND_MAKERNOTE;
3191
3192
3.06k
  old_motorola_intel = ImageInfo->motorola_intel;
3193
3.06k
  switch (maker_note->byte_order) {
3194
996
    case MN_ORDER_INTEL:
3195
996
      ImageInfo->motorola_intel = 0;
3196
996
      break;
3197
434
    case MN_ORDER_MOTOROLA:
3198
434
      ImageInfo->motorola_intel = 1;
3199
434
      break;
3200
0
    default:
3201
1.63k
    case MN_ORDER_NORMAL:
3202
1.63k
      break;
3203
3.06k
  }
3204
3205
3.06k
  NumDirEntries = php_ifd_get16u(dir_start, ImageInfo->motorola_intel);
3206
3207
  /* It can be that motorola_intel is wrongly mapped, let's try inverting it */
3208
3.06k
  if ((2+NumDirEntries*12) > value_len) {
3209
683
    exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Potentially invalid endianness, trying again with different endianness before imminent failure.");
3210
3211
683
    ImageInfo->motorola_intel = ImageInfo->motorola_intel == 0 ? 1 : 0;
3212
683
    NumDirEntries = php_ifd_get16u(dir_start, ImageInfo->motorola_intel);
3213
683
  }
3214
3215
3.06k
  if ((2+NumDirEntries*12) > value_len) {
3216
389
    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);
3217
389
    return false;
3218
389
  }
3219
2.67k
  if ((dir_start - value_ptr) > value_len - (2+NumDirEntries*12)) {
3220
210
    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);
3221
210
    return false;
3222
210
  }
3223
3224
2.46k
  switch (maker_note->offset_mode) {
3225
711
    case MN_OFFSET_MAKER:
3226
711
      exif_offset_info_init(&new_info, value_ptr, value_ptr, value_len);
3227
711
      info = &new_info;
3228
711
      break;
3229
0
    default:
3230
1.75k
    case MN_OFFSET_NORMAL:
3231
1.75k
      break;
3232
2.46k
  }
3233
3234
4.97k
  for (int de = 0; de < NumDirEntries; de++) {
3235
3.35k
    size_t offset = 2 + 12 * de;
3236
3.35k
    if (!exif_process_IFD_TAG(ImageInfo, dir_start + offset,
3237
3.35k
                  info, displacement, section_index, 0, maker_note->tag_table)) {
3238
847
      return false;
3239
847
    }
3240
3.35k
  }
3241
1.61k
  ImageInfo->motorola_intel = old_motorola_intel;
3242
/*  NextDirOffset (must be NULL) = php_ifd_get32u(dir_start+2+12*de, ImageInfo->motorola_intel);*/
3243
#ifdef EXIF_DEBUG
3244
  exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Subsection %s done", exif_get_sectionname(SECTION_MAKERNOTE));
3245
#endif
3246
1.61k
  return true;
3247
2.46k
}
3248
/* }}} */
3249
3250
58.5k
#define REQUIRE_NON_EMPTY() do { \
3251
58.5k
  if (byte_count == 0) { \
3252
4.53k
    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)); \
3253
4.53k
    return false; \
3254
4.53k
  } \
3255
58.5k
} while (0)
3256
3257
3258
/* {{{ exif_process_IFD_TAG
3259
 * Process one of the nested IFDs directories. */
3260
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)
3261
144k
{
3262
144k
  unsigned int tag, format, components;
3263
144k
  char *value_ptr, tagname[64], cbuf[32], *outside=NULL;
3264
144k
  size_t byte_count, offset_val;
3265
144k
  int64_t byte_count_signed;
3266
3267
144k
  tag = php_ifd_get16u(dir_entry, ImageInfo->motorola_intel);
3268
144k
  format = php_ifd_get16u(dir_entry+2, ImageInfo->motorola_intel);
3269
144k
  components = php_ifd_get32u(dir_entry+4, ImageInfo->motorola_intel);
3270
3271
144k
  if (!format || format > NUM_FORMATS) {
3272
    /* (-1) catches illegal zero case as unsigned underflows to positive large. */
3273
65.3k
    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);
3274
65.3k
    format = TAG_FMT_BYTE;
3275
65.3k
  }
3276
3277
144k
  byte_count_signed = (int64_t)components * php_tiff_bytes_per_format[format];
3278
3279
144k
  if (byte_count_signed < 0 || (byte_count_signed > INT32_MAX)) {
3280
1.95k
    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));
3281
1.95k
    return false;
3282
1.95k
  }
3283
3284
142k
  byte_count = (size_t)byte_count_signed;
3285
3286
142k
  if (byte_count > 4) {
3287
    /* If its bigger than 4 bytes, the dir entry contains an offset. */
3288
78.4k
    offset_val = php_ifd_get32u(dir_entry+8, ImageInfo->motorola_intel);
3289
78.4k
    value_ptr = exif_offset_info_try_get(info, offset_val, byte_count);
3290
78.4k
    if (!value_ptr) {
3291
      /* It is important to check for IMAGE_FILETYPE_TIFF
3292
       * JPEG does not use absolute pointers instead its pointers are
3293
       * relative to the start of the TIFF header in APP1 section. */
3294
      // TODO: Shouldn't we also be taking "displacement" into account here?
3295
48.6k
      if (byte_count > ImageInfo->FileSize || offset_val>ImageInfo->FileSize-byte_count || (ImageInfo->FileType!=IMAGE_FILETYPE_TIFF_II && ImageInfo->FileType!=IMAGE_FILETYPE_TIFF_MM && ImageInfo->FileType!=IMAGE_FILETYPE_JPEG)) {
3296
5.46k
        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);
3297
5.46k
        return false;
3298
5.46k
      }
3299
43.2k
      if (byte_count>sizeof(cbuf)) {
3300
        /* mark as outside range and get buffer */
3301
23.8k
        value_ptr = safe_emalloc(byte_count, 1, 0);
3302
23.8k
        outside = value_ptr;
3303
23.8k
      } else {
3304
        /* In most cases we only access a small range so
3305
         * it is faster to use a static buffer there
3306
         * BUT it offers also the possibility to have
3307
         * pointers read without the need to free them
3308
         * explicitly before returning. */
3309
19.4k
        memset(&cbuf, 0, sizeof(cbuf));
3310
19.4k
        value_ptr = cbuf;
3311
19.4k
      }
3312
3313
43.2k
      size_t fpos = php_stream_tell(ImageInfo->infile);
3314
43.2k
      php_stream_seek(ImageInfo->infile, displacement+offset_val, SEEK_SET);
3315
43.2k
      size_t fgot = php_stream_tell(ImageInfo->infile);
3316
43.2k
      if (fgot!=displacement+offset_val) {
3317
0
        EFREE_IF(outside);
3318
0
        exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Wrong file pointer: 0x%08X != 0x%08X", fgot, displacement+offset_val);
3319
0
        return false;
3320
0
      }
3321
43.2k
      fgot = exif_read_from_stream_file_looped(ImageInfo->infile, value_ptr, byte_count);
3322
43.2k
      php_stream_seek(ImageInfo->infile, fpos, SEEK_SET);
3323
43.2k
      if (fgot != byte_count) {
3324
32
        EFREE_IF(outside);
3325
32
        EXIF_ERRLOG_FILEEOF(ImageInfo)
3326
32
        return false;
3327
32
      }
3328
43.2k
    }
3329
78.4k
  } else {
3330
    /* 4 bytes or less and value is in the dir entry itself */
3331
63.9k
    value_ptr = dir_entry+8;
3332
    // TODO: This is dubious, but the value is only used for debugging.
3333
63.9k
    offset_val = value_ptr-info->offset_base;
3334
63.9k
  }
3335
3336
136k
  ImageInfo->sections_found |= FOUND_ANY_TAG;
3337
#ifdef EXIF_DEBUG
3338
  int dump_free;
3339
  char *dump_data = exif_dump_data(&dump_free, format, components, ImageInfo->motorola_intel, value_ptr);
3340
  exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE,
3341
    "Process tag(x%04X=%s,@0x%04X + x%04X(=%d)): %s%s %s",
3342
    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);
3343
  if (dump_free) {
3344
    efree(dump_data);
3345
  }
3346
#endif
3347
3348
  /* NB: The following code may not assume that there is at least one component!
3349
   * byte_count may be zero! */
3350
3351
136k
  if (section_index==SECTION_THUMBNAIL) {
3352
43.1k
    if (!ImageInfo->Thumbnail.data) {
3353
43.1k
      REQUIRE_NON_EMPTY();
3354
40.3k
      switch(tag) {
3355
3.05k
        case TAG_IMAGEWIDTH:
3356
4.06k
        case TAG_COMP_IMAGE_WIDTH:
3357
4.06k
          ImageInfo->Thumbnail.width = exif_convert_any_to_int(value_ptr, exif_rewrite_tag_format_to_unsigned(format), ImageInfo->motorola_intel);
3358
4.06k
          break;
3359
3360
2.52k
        case TAG_IMAGEHEIGHT:
3361
3.01k
        case TAG_COMP_IMAGE_HEIGHT:
3362
3.01k
          ImageInfo->Thumbnail.height = exif_convert_any_to_int(value_ptr, exif_rewrite_tag_format_to_unsigned(format), ImageInfo->motorola_intel);
3363
3.01k
          break;
3364
3365
2.03k
        case TAG_STRIP_OFFSETS:
3366
4.08k
        case TAG_JPEG_INTERCHANGE_FORMAT:
3367
          /* accept both formats */
3368
4.08k
          ImageInfo->Thumbnail.offset = exif_convert_any_to_int(value_ptr, exif_rewrite_tag_format_to_unsigned(format), ImageInfo->motorola_intel);
3369
4.08k
          break;
3370
3371
4.89k
        case TAG_STRIP_BYTE_COUNTS:
3372
4.89k
          if (ImageInfo->FileType == IMAGE_FILETYPE_TIFF_II || ImageInfo->FileType == IMAGE_FILETYPE_TIFF_MM) {
3373
4.61k
            ImageInfo->Thumbnail.filetype = ImageInfo->FileType;
3374
4.61k
          } else {
3375
            /* motorola is easier to read */
3376
277
            ImageInfo->Thumbnail.filetype = IMAGE_FILETYPE_TIFF_MM;
3377
277
          }
3378
4.89k
          ImageInfo->Thumbnail.size = exif_convert_any_to_int(value_ptr, exif_rewrite_tag_format_to_unsigned(format), ImageInfo->motorola_intel);
3379
4.89k
          break;
3380
3381
1.62k
        case TAG_JPEG_INTERCHANGE_FORMAT_LEN:
3382
1.62k
          if (ImageInfo->Thumbnail.filetype == IMAGE_FILETYPE_UNKNOWN) {
3383
321
            ImageInfo->Thumbnail.filetype = IMAGE_FILETYPE_JPEG;
3384
321
            ImageInfo->Thumbnail.size = exif_convert_any_to_int(value_ptr, exif_rewrite_tag_format_to_unsigned(format), ImageInfo->motorola_intel);
3385
321
          }
3386
1.62k
          break;
3387
40.3k
      }
3388
40.3k
    }
3389
93.7k
  } else {
3390
93.7k
    if (section_index==SECTION_IFD0 || section_index==SECTION_EXIF)
3391
69.6k
    switch(tag) {
3392
2.97k
      case TAG_COPYRIGHT: {
3393
2.97k
        size_t length;
3394
        /* check for "<photographer> NUL <editor> NUL" */
3395
2.97k
        if (byte_count>1 && (length=zend_strnlen(value_ptr, byte_count)) > 0) {
3396
2.20k
          if (length<byte_count-1) {
3397
            /* When there are any characters after the first NUL */
3398
1.92k
            EFREE_IF(ImageInfo->CopyrightPhotographer);
3399
1.92k
            EFREE_IF(ImageInfo->CopyrightEditor);
3400
1.92k
            EFREE_IF(ImageInfo->Copyright);
3401
1.92k
            ImageInfo->CopyrightPhotographer  = estrdup(value_ptr);
3402
1.92k
            ImageInfo->CopyrightEditor        = estrndup(value_ptr+length+1, byte_count-length-1);
3403
1.92k
            spprintf(&ImageInfo->Copyright, 0, "%s, %s", ImageInfo->CopyrightPhotographer, ImageInfo->CopyrightEditor);
3404
            /* format = TAG_FMT_UNDEFINED; this mustn't be ASCII         */
3405
            /* but we are not supposed to change this                   */
3406
            /* keep in mind that image_info does not store editor value */
3407
1.92k
          } else {
3408
286
            EFREE_IF(ImageInfo->Copyright);
3409
286
            ImageInfo->Copyright = estrndup(value_ptr, byte_count);
3410
286
          }
3411
2.20k
        }
3412
2.97k
        break;
3413
0
      }
3414
3415
4.28k
      case TAG_USERCOMMENT:
3416
4.28k
        EFREE_IF(ImageInfo->UserComment);
3417
4.28k
        ImageInfo->UserComment = NULL;
3418
4.28k
        EFREE_IF(ImageInfo->UserCommentEncoding);
3419
4.28k
        ImageInfo->UserCommentEncoding = NULL;
3420
4.28k
        ImageInfo->UserCommentLength = exif_process_user_comment(ImageInfo, &(ImageInfo->UserComment), &(ImageInfo->UserCommentEncoding), value_ptr, byte_count);
3421
4.28k
        break;
3422
3423
1.37k
      case TAG_XP_TITLE:
3424
1.97k
      case TAG_XP_COMMENTS:
3425
2.74k
      case TAG_XP_AUTHOR:
3426
3.56k
      case TAG_XP_KEYWORDS:
3427
4.18k
      case TAG_XP_SUBJECT: {
3428
4.18k
        xp_field_type *tmp_xp = (xp_field_type*)safe_erealloc(ImageInfo->xp_fields.list, (ImageInfo->xp_fields.count+1), sizeof(xp_field_type), 0);
3429
4.18k
        ImageInfo->sections_found |= FOUND_WINXP;
3430
4.18k
        ImageInfo->xp_fields.list = tmp_xp;
3431
4.18k
        ImageInfo->xp_fields.count++;
3432
4.18k
        exif_process_unicode(ImageInfo, &(ImageInfo->xp_fields.list[ImageInfo->xp_fields.count-1]), tag, value_ptr, byte_count);
3433
4.18k
        break;
3434
3.56k
      }
3435
3436
1.01k
      case TAG_FNUMBER:
3437
        /* Simplest way of expressing aperture, so I trust it the most.
3438
           (overwrite previously computed value if there is one) */
3439
1.01k
        REQUIRE_NON_EMPTY();
3440
774
        ImageInfo->ApertureFNumber = (float)exif_convert_any_format(value_ptr, format, ImageInfo->motorola_intel);
3441
774
        break;
3442
3443
823
      case TAG_APERTURE:
3444
1.25k
      case TAG_MAX_APERTURE:
3445
        /* More relevant info always comes earlier, so only use this field if we don't
3446
           have appropriate aperture information yet. */
3447
1.25k
        if (ImageInfo->ApertureFNumber == 0) {
3448
666
          REQUIRE_NON_EMPTY();
3449
421
          ImageInfo->ApertureFNumber
3450
421
            = expf(exif_convert_any_format(value_ptr, format, ImageInfo->motorola_intel)*logf(2.0)*0.5);
3451
421
        }
3452
1.01k
        break;
3453
3454
1.59k
      case TAG_SHUTTERSPEED:
3455
        /* More complicated way of expressing exposure time, so only use
3456
           this value if we don't already have it from somewhere else.
3457
           SHUTTERSPEED comes after EXPOSURE TIME
3458
          */
3459
1.59k
        if (ImageInfo->ExposureTime == 0) {
3460
1.05k
          REQUIRE_NON_EMPTY();
3461
810
          ImageInfo->ExposureTime
3462
810
            = expf(-exif_convert_any_format(value_ptr, format, ImageInfo->motorola_intel)*logf(2.0));
3463
810
        }
3464
1.34k
        break;
3465
1.34k
      case TAG_EXPOSURETIME:
3466
506
        ImageInfo->ExposureTime = -1;
3467
506
        break;
3468
3469
1.64k
      case TAG_COMP_IMAGE_WIDTH:
3470
1.64k
        REQUIRE_NON_EMPTY();
3471
1.39k
        ImageInfo->ExifImageWidth = exif_convert_any_to_int(value_ptr, exif_rewrite_tag_format_to_unsigned(format), ImageInfo->motorola_intel);
3472
1.39k
        break;
3473
3474
2.99k
      case TAG_FOCALPLANE_X_RES:
3475
2.99k
        REQUIRE_NON_EMPTY();
3476
2.76k
        ImageInfo->FocalplaneXRes = exif_convert_any_format(value_ptr, format, ImageInfo->motorola_intel);
3477
2.76k
        break;
3478
3479
1.62k
      case TAG_SUBJECT_DISTANCE:
3480
        /* Indicates the distance the autofocus camera is focused to.
3481
           Tends to be less accurate as distance increases. */
3482
1.62k
        REQUIRE_NON_EMPTY();
3483
1.39k
        ImageInfo->Distance = (float)exif_convert_any_format(value_ptr, format, ImageInfo->motorola_intel);
3484
1.39k
        break;
3485
3486
5.34k
      case TAG_FOCALPLANE_RESOLUTION_UNIT:
3487
5.34k
        REQUIRE_NON_EMPTY();
3488
5.10k
        switch (exif_convert_any_to_int(value_ptr, format, ImageInfo->motorola_intel)) {
3489
287
          case 1: ImageInfo->FocalplaneUnits = 25.4; break; /* inch */
3490
291
          case 2:
3491
            /* According to the information I was using, 2 means meters.
3492
               But looking at the Cannon powershot's files, inches is the only
3493
               sensible value. */
3494
291
            ImageInfo->FocalplaneUnits = 25.4;
3495
291
            break;
3496
3497
245
          case 3: ImageInfo->FocalplaneUnits = 10;   break;  /* centimeter */
3498
586
          case 4: ImageInfo->FocalplaneUnits = 1;    break;  /* millimeter */
3499
377
          case 5: ImageInfo->FocalplaneUnits = .001; break;  /* micrometer */
3500
5.10k
        }
3501
5.10k
        break;
3502
3503
5.10k
      case TAG_SUB_IFD:
3504
152
        if (format==TAG_FMT_IFD) {
3505
          /* If this is called we are either in a TIFFs thumbnail or a JPEG where we cannot handle it */
3506
          /* TIFF thumbnail: our data structure cannot store a thumbnail of a thumbnail */
3507
          /* JPEG do we have the data area and what to do with it */
3508
76
          exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Skip SUB IFD");
3509
76
        }
3510
152
        break;
3511
3512
3.91k
      case TAG_MAKE:
3513
3.91k
        EFREE_IF(ImageInfo->make);
3514
3.91k
        ImageInfo->make = estrndup(value_ptr, byte_count);
3515
3.91k
        break;
3516
347
      case TAG_MODEL:
3517
347
        EFREE_IF(ImageInfo->model);
3518
347
        ImageInfo->model = estrndup(value_ptr, byte_count);
3519
347
        break;
3520
3521
5.04k
      case TAG_MAKER_NOTE:
3522
5.04k
        if (!exif_process_IFD_in_MAKERNOTE(ImageInfo, value_ptr, byte_count, info, displacement)) {
3523
1.44k
          EFREE_IF(outside);
3524
1.44k
          return false;
3525
1.44k
        }
3526
3.60k
        break;
3527
3528
3.60k
      case TAG_EXIF_IFD_POINTER:
3529
620
      case TAG_GPS_IFD_POINTER:
3530
1.01k
      case TAG_INTEROP_IFD_POINTER:
3531
1.01k
        if (ReadNextIFD) {
3532
1.01k
          REQUIRE_NON_EMPTY();
3533
1.01k
          char *Subdir_start;
3534
1.01k
          int sub_section_index = 0;
3535
1.01k
          switch(tag) {
3536
521
            case TAG_EXIF_IFD_POINTER:
3537
#ifdef EXIF_DEBUG
3538
              exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Found EXIF");
3539
#endif
3540
521
              ImageInfo->sections_found |= FOUND_EXIF;
3541
521
              sub_section_index = SECTION_EXIF;
3542
521
              break;
3543
98
            case TAG_GPS_IFD_POINTER:
3544
#ifdef EXIF_DEBUG
3545
              exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Found GPS");
3546
#endif
3547
98
              ImageInfo->sections_found |= FOUND_GPS;
3548
98
              sub_section_index = SECTION_GPS;
3549
98
              break;
3550
397
            case TAG_INTEROP_IFD_POINTER:
3551
#ifdef EXIF_DEBUG
3552
              exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Found INTEROPERABILITY");
3553
#endif
3554
397
              ImageInfo->sections_found |= FOUND_INTEROP;
3555
397
              sub_section_index = SECTION_INTEROP;
3556
397
              break;
3557
1.01k
          }
3558
1.01k
          offset_val = php_ifd_get32u(value_ptr, ImageInfo->motorola_intel);
3559
1.01k
          Subdir_start = exif_offset_info_try_get(info, offset_val, 0);
3560
1.01k
          if (!Subdir_start) {
3561
30
            exif_error_docref("exif_read_data#error_ifd" EXIFERR_CC, ImageInfo, E_WARNING, "Illegal IFD Pointer");
3562
30
            EFREE_IF(outside);
3563
30
            return false;
3564
30
          }
3565
986
          if (!exif_process_IFD_in_JPEG(ImageInfo, Subdir_start, info, displacement, sub_section_index, tag)) {
3566
443
            EFREE_IF(outside);
3567
443
            return false;
3568
443
          }
3569
#ifdef EXIF_DEBUG
3570
          exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Subsection %s done", exif_get_sectionname(sub_section_index));
3571
#endif
3572
986
        }
3573
69.6k
    }
3574
93.7k
  }
3575
130k
  exif_iif_add_tag(ImageInfo, section_index, exif_get_tagname_key(tag, tagname, sizeof(tagname), tag_table), tag, format, components, value_ptr, byte_count);
3576
130k
  EFREE_IF(outside);
3577
130k
  return true;
3578
136k
}
3579
/* }}} */
3580
3581
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)
3582
196k
{
3583
196k
  bool result;
3584
  /* Protect against corrupt headers */
3585
196k
  if (ImageInfo->ifd_count++ > MAX_IFD_TAGS) {
3586
1.03k
    exif_error_docref("exif_read_data#error_ifd" EXIFERR_CC, ImageInfo, E_WARNING, "corrupt EXIF header: maximum IFD tag count reached");
3587
1.03k
    return false;
3588
1.03k
  }
3589
195k
  if (ImageInfo->ifd_nesting_level > MAX_IFD_NESTING_LEVEL) {
3590
51.0k
    exif_error_docref("exif_read_data#error_ifd" EXIFERR_CC, ImageInfo, E_WARNING, "corrupt EXIF header: maximum directory nesting level reached");
3591
51.0k
    return false;
3592
51.0k
  }
3593
144k
  ImageInfo->ifd_nesting_level++;
3594
144k
  result = exif_process_IFD_TAG_impl(ImageInfo, dir_entry, info, displacement, section_index, ReadNextIFD, tag_table);
3595
144k
  ImageInfo->ifd_nesting_level--;
3596
144k
  return result;
3597
195k
}
3598
3599
/* {{{ exif_process_IFD_in_JPEG
3600
 * Process one of the nested IFDs directories. */
3601
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)
3602
1.94k
{
3603
1.94k
  int de;
3604
1.94k
  int NumDirEntries;
3605
1.94k
  int NextDirOffset = 0;
3606
3607
#ifdef EXIF_DEBUG
3608
  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));
3609
#endif
3610
3611
1.94k
  ImageInfo->sections_found |= FOUND_IFD0;
3612
3613
1.94k
  if (!exif_offset_info_contains(info, dir_start, 2)) {
3614
9
    exif_error_docref("exif_read_data#error_ifd" EXIFERR_CC, ImageInfo, E_WARNING, "Illegal IFD size");
3615
9
    return false;
3616
9
  }
3617
3618
1.93k
  NumDirEntries = php_ifd_get16u(dir_start, ImageInfo->motorola_intel);
3619
3620
1.93k
  if (!exif_offset_info_contains(info, dir_start+2, NumDirEntries*12)) {
3621
16
    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);
3622
16
    return false;
3623
16
  }
3624
3625
7.74k
  for (de=0;de<NumDirEntries;de++) {
3626
6.39k
    if (!exif_process_IFD_TAG(ImageInfo, dir_start + 2 + 12 * de,
3627
6.39k
                  info, displacement, section_index, 1, exif_get_tag_table(section_index))) {
3628
572
      return false;
3629
572
    }
3630
6.39k
  }
3631
  /*
3632
   * Ignore IFD2 if it purportedly exists
3633
   */
3634
1.34k
  if (section_index == SECTION_THUMBNAIL) {
3635
517
    return true;
3636
517
  }
3637
  /*
3638
   * Hack to make it process IDF1 I hope
3639
   * There are 2 IDFs, the second one holds the keys (0x0201 and 0x0202) to the thumbnail
3640
   */
3641
829
  if (!exif_offset_info_contains(info, dir_start+2+NumDirEntries*12, 4)) {
3642
26
    exif_error_docref("exif_read_data#error_ifd" EXIFERR_CC, ImageInfo, E_WARNING, "Illegal IFD size");
3643
26
    return false;
3644
26
  }
3645
3646
803
  if (tag != TAG_EXIF_IFD_POINTER && tag != TAG_GPS_IFD_POINTER) {
3647
617
    NextDirOffset = php_ifd_get32u(dir_start+2+12*de, ImageInfo->motorola_intel);
3648
617
  }
3649
3650
803
  if (NextDirOffset) {
3651
603
    char *next_dir_start = exif_offset_info_try_get(info, NextDirOffset, 0);
3652
603
    if (!next_dir_start) {
3653
65
      exif_error_docref("exif_read_data#error_ifd" EXIFERR_CC, ImageInfo, E_WARNING, "Illegal IFD offset");
3654
65
      return false;
3655
65
    }
3656
    /* That is the IFD for the first thumbnail */
3657
#ifdef EXIF_DEBUG
3658
    exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Expect next IFD to be thumbnail");
3659
#endif
3660
538
    if (exif_process_IFD_in_JPEG(ImageInfo, next_dir_start, info, displacement, SECTION_THUMBNAIL, 0)) {
3661
#ifdef EXIF_DEBUG
3662
      exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Thumbnail size: 0x%04X", ImageInfo->Thumbnail.size);
3663
#endif
3664
517
      if (ImageInfo->Thumbnail.filetype != IMAGE_FILETYPE_UNKNOWN
3665
517
      &&  ImageInfo->Thumbnail.size
3666
517
      &&  ImageInfo->Thumbnail.offset
3667
517
      &&  ImageInfo->read_thumbnail
3668
517
      ) {
3669
0
        exif_thumbnail_extract(ImageInfo, info);
3670
0
      }
3671
517
      return true;
3672
517
    } else {
3673
21
      return false;
3674
21
    }
3675
538
  }
3676
200
  return true;
3677
803
}
3678
/* }}} */
3679
3680
/* {{{ exif_process_TIFF_in_JPEG
3681
   Process a TIFF header in a JPEG file
3682
*/
3683
static void exif_process_TIFF_in_JPEG(image_info_type *ImageInfo, char *CharBuf, size_t length, size_t displacement)
3684
2.11k
{
3685
2.11k
  unsigned exif_value_2a, offset_of_ifd;
3686
2.11k
  exif_offset_info info;
3687
3688
2.11k
  if (length < 2) {
3689
194
    exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Missing TIFF alignment marker");
3690
194
    return;
3691
194
  }
3692
3693
  /* set the thumbnail stuff to nothing so we can test to see if they get set up */
3694
1.92k
  if (memcmp(CharBuf, "II", 2) == 0) {
3695
905
    ImageInfo->motorola_intel = 0;
3696
1.01k
  } else if (memcmp(CharBuf, "MM", 2) == 0) {
3697
687
    ImageInfo->motorola_intel = 1;
3698
687
  } else {
3699
332
    exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Invalid TIFF alignment marker");
3700
332
    return;
3701
332
  }
3702
3703
  /* Check the next two values for correctness. */
3704
1.59k
  if (length < 8) {
3705
393
    exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Invalid TIFF start (1)");
3706
393
    return;
3707
393
  }
3708
1.19k
  exif_value_2a = php_ifd_get16u(CharBuf+2, ImageInfo->motorola_intel);
3709
1.19k
  offset_of_ifd = php_ifd_get32u(CharBuf+4, ImageInfo->motorola_intel);
3710
1.19k
  if (exif_value_2a != 0x2a || offset_of_ifd < 0x08) {
3711
530
    exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Invalid TIFF start (1)");
3712
530
    return;
3713
530
  }
3714
669
  if (offset_of_ifd > length) {
3715
250
    exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Invalid IFD start");
3716
250
    return;
3717
250
  }
3718
3719
419
  ImageInfo->sections_found |= FOUND_IFD0;
3720
  /* First directory starts at offset 8. Offsets starts at 0. */
3721
419
  exif_offset_info_init(&info, CharBuf, CharBuf, length/*-14*/);
3722
419
  exif_process_IFD_in_JPEG(ImageInfo, CharBuf+offset_of_ifd, &info, displacement, SECTION_IFD0, 0);
3723
3724
#ifdef EXIF_DEBUG
3725
  exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Process TIFF in JPEG done");
3726
#endif
3727
3728
  /* Compute the CCD width, in millimeters. */
3729
419
  if (ImageInfo->FocalplaneXRes != 0) {
3730
2
    ImageInfo->CCDWidth = (float)(ImageInfo->ExifImageWidth * ImageInfo->FocalplaneUnits / ImageInfo->FocalplaneXRes);
3731
2
  }
3732
419
}
3733
/* }}} */
3734
3735
/* {{{ exif_process_APP1
3736
   Process an JPEG APP1 block marker
3737
   Describes all the drivel that most digital cameras include...
3738
*/
3739
static void exif_process_APP1(image_info_type *ImageInfo, char *CharBuf, size_t length, size_t displacement)
3740
2.97k
{
3741
  /* Check the APP1 for Exif Identifier Code */
3742
2.97k
  static const uchar ExifHeader[] = {0x45, 0x78, 0x69, 0x66, 0x00, 0x00};
3743
2.97k
  if (length <= 8 || memcmp(CharBuf+2, ExifHeader, 6)) {
3744
859
    exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Incorrect APP1 Exif Identifier Code");
3745
859
    return;
3746
859
  }
3747
2.11k
  exif_process_TIFF_in_JPEG(ImageInfo, CharBuf + 8, length - 8, displacement+8);
3748
#ifdef EXIF_DEBUG
3749
  exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Process APP1/EXIF done");
3750
#endif
3751
2.11k
}
3752
/* }}} */
3753
3754
/* {{{ exif_process_APP12
3755
   Process an JPEG APP12 block marker used by OLYMPUS
3756
*/
3757
static void exif_process_APP12(image_info_type *ImageInfo, char *buffer, size_t length)
3758
928
{
3759
928
  size_t l1, l2=0;
3760
3761
928
  if ((l1 = zend_strnlen(buffer+2, length-2)) > 0) {
3762
615
    exif_iif_add_tag(ImageInfo, SECTION_APP12, "Company", TAG_NONE, TAG_FMT_STRING, l1, buffer+2, l1);
3763
615
    if (length > 2+l1+1) {
3764
270
      l2 = zend_strnlen(buffer+2+l1+1, length-2-l1-1);
3765
270
      exif_iif_add_tag(ImageInfo, SECTION_APP12, "Info", TAG_NONE, TAG_FMT_STRING, l2, buffer+2+l1+1, l2);
3766
270
    }
3767
615
  }
3768
#ifdef EXIF_DEBUG
3769
  exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Process section APP12 with l1=%d, l2=%d done", l1, l2);
3770
#endif
3771
928
}
3772
/* }}} */
3773
3774
/* {{{ exif_scan_JPEG_header
3775
 * Parse the marker stream until SOS or EOI is seen; */
3776
static bool exif_scan_JPEG_header(image_info_type *ImageInfo)
3777
1.16k
{
3778
1.16k
  int sn;
3779
1.16k
  int marker = 0, last_marker = M_PSEUDO, comment_correction=1;
3780
1.16k
  unsigned int ll, lh;
3781
1.16k
  uchar *Data;
3782
1.16k
  size_t fpos, size, got, itemlen;
3783
1.16k
  jpeg_sof_info sof_info;
3784
3785
592k
  while (true) {
3786
#ifdef EXIF_DEBUG
3787
    fpos = php_stream_tell(ImageInfo->infile);
3788
    exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Needing section %d @ 0x%08X", ImageInfo->file.count, fpos);
3789
#endif
3790
3791
    /* get marker byte, swallowing possible padding                           */
3792
    /* some software does not count the length bytes of COM section           */
3793
    /* one company doing so is very much involved in JPEG... so we accept too */
3794
592k
    if (last_marker==M_COM && comment_correction) {
3795
766
      comment_correction = 2;
3796
766
    }
3797
597k
    do {
3798
597k
      if ((marker = php_stream_getc(ImageInfo->infile)) == EOF) {
3799
642
        EXIF_ERRLOG_CORRUPT(ImageInfo)
3800
642
        return false;
3801
642
      }
3802
596k
      if (last_marker==M_COM && comment_correction>0) {
3803
1.10k
        if (marker!=0xFF) {
3804
538
          marker = 0xff;
3805
538
          comment_correction--;
3806
570
        } else  {
3807
570
          last_marker = M_PSEUDO; /* stop skipping 0 for M_COM */
3808
570
        }
3809
1.10k
      }
3810
596k
    } while (marker == 0xff);
3811
591k
    if (last_marker==M_COM && !comment_correction) {
3812
581k
      exif_error_docref("exif_read_data#error_mcom" EXIFERR_CC, ImageInfo, E_NOTICE, "Image has corrupt COM section: some software set wrong length information");
3813
581k
    }
3814
591k
    if (last_marker==M_COM && comment_correction)
3815
0
      return M_EOI; /* ah illegal: char after COM section not 0xFF */
3816
3817
591k
    fpos = php_stream_tell(ImageInfo->infile);
3818
3819
    /* safety net in case the above algorithm change dramatically, should not trigger */
3820
591k
    ZEND_ASSERT(marker != 0xff);
3821
3822
    /* Read the length of the section. */
3823
591k
    if ((lh = php_stream_getc(ImageInfo->infile)) == (unsigned int)EOF) {
3824
73
      EXIF_ERRLOG_CORRUPT(ImageInfo)
3825
73
      return false;
3826
73
    }
3827
591k
    if ((ll = php_stream_getc(ImageInfo->infile)) == (unsigned int)EOF) {
3828
81
      EXIF_ERRLOG_CORRUPT(ImageInfo)
3829
81
      return false;
3830
81
    }
3831
3832
591k
    itemlen = (lh << 8) | ll;
3833
3834
591k
    if (itemlen < 2) {
3835
#ifdef EXIF_DEBUG
3836
      exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "%s, Section length: 0x%02X%02X", EXIF_ERROR_CORRUPT, lh, ll);
3837
#else
3838
10
      EXIF_ERRLOG_CORRUPT(ImageInfo)
3839
10
#endif
3840
10
      return false;
3841
10
    }
3842
3843
591k
    sn = exif_file_sections_add(ImageInfo, marker, itemlen, NULL);
3844
591k
    Data = ImageInfo->file.list[sn].data;
3845
3846
    /* Store first two pre-read bytes. */
3847
591k
    Data[0] = (uchar)lh;
3848
591k
    Data[1] = (uchar)ll;
3849
3850
591k
    got = exif_read_from_stream_file_looped(ImageInfo->infile, (char*)(Data+2), itemlen-2); /* Read the whole section. */
3851
591k
    if (got != itemlen-2) {
3852
126
      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);
3853
126
      return false;
3854
126
    }
3855
3856
#ifdef EXIF_DEBUG
3857
    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);
3858
#endif
3859
591k
    switch(marker) {
3860
53
      case M_SOS:   /* stop before hitting compressed data  */
3861
        /* If reading entire image is requested, read the rest of the data. */
3862
53
        if (ImageInfo->read_all) {
3863
          /* Determine how much file is left. */
3864
0
          fpos = php_stream_tell(ImageInfo->infile);
3865
0
          size = ImageInfo->FileSize - fpos;
3866
0
          sn = exif_file_sections_add(ImageInfo, M_PSEUDO, size, NULL);
3867
0
          Data = ImageInfo->file.list[sn].data;
3868
0
          got = exif_read_from_stream_file_looped(ImageInfo->infile, (char*)Data, size);
3869
0
          if (got != size) {
3870
0
            EXIF_ERRLOG_FILEEOF(ImageInfo)
3871
0
            return false;
3872
0
          }
3873
0
        }
3874
53
        return true;
3875
3876
91
      case M_EOI:   /* in case it's a tables-only JPEG stream */
3877
91
        exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "No image in jpeg!");
3878
91
        return (ImageInfo->sections_found&(~FOUND_COMPUTED)) ? true : false;
3879
3880
582k
      case M_COM: /* Comment section */
3881
582k
        exif_process_COM(ImageInfo, (char *)Data, itemlen);
3882
582k
        break;
3883
3884
3.17k
      case M_EXIF:
3885
3.17k
        if (!(ImageInfo->sections_found&FOUND_IFD0)) {
3886
          /*ImageInfo->sections_found |= FOUND_EXIF;*/
3887
          /* Seen files from some 'U-lead' software with Vivitar scanner
3888
             that uses marker 31 later in the file (no clue what for!) */
3889
2.97k
          exif_process_APP1(ImageInfo, (char *)Data, itemlen, fpos);
3890
2.97k
        }
3891
3.17k
        break;
3892
3893
928
      case M_APP12:
3894
928
        exif_process_APP12(ImageInfo, (char *)Data, itemlen);
3895
928
        break;
3896
3897
3898
202
      case M_SOF0:
3899
429
      case M_SOF1:
3900
632
      case M_SOF2:
3901
945
      case M_SOF3:
3902
1.14k
      case M_SOF5:
3903
1.34k
      case M_SOF6:
3904
1.53k
      case M_SOF7:
3905
1.73k
      case M_SOF9:
3906
2.10k
      case M_SOF10:
3907
2.31k
      case M_SOF11:
3908
2.65k
      case M_SOF13:
3909
2.86k
      case M_SOF14:
3910
3.06k
      case M_SOF15:
3911
3.06k
        if ((itemlen - 2) < 6) {
3912
91
          return false;
3913
91
        }
3914
3915
2.97k
        exif_process_SOFn(Data, marker, &sof_info);
3916
2.97k
        ImageInfo->Width  = sof_info.width;
3917
2.97k
        ImageInfo->Height = sof_info.height;
3918
2.97k
        if (sof_info.num_components == 3) {
3919
613
          ImageInfo->IsColor = 1;
3920
2.36k
        } else {
3921
2.36k
          ImageInfo->IsColor = 0;
3922
2.36k
        }
3923
2.97k
        break;
3924
1.63k
      default:
3925
        /* skip any other marker silently. */
3926
1.63k
        break;
3927
591k
    }
3928
3929
    /* keep track of last marker */
3930
591k
    last_marker = marker;
3931
591k
  }
3932
#ifdef EXIF_DEBUG
3933
  exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Done");
3934
#endif
3935
0
  return true;
3936
1.16k
}
3937
/* }}} */
3938
3939
/* {{{ exif_scan_thumbnail
3940
 * scan JPEG in thumbnail (memory) */
3941
static bool exif_scan_thumbnail(image_info_type *ImageInfo)
3942
449
{
3943
449
  uchar           c, *data = (uchar*)ImageInfo->Thumbnail.data;
3944
449
  int             n, marker;
3945
449
  size_t          length=2, pos=0;
3946
449
  jpeg_sof_info   sof_info;
3947
3948
449
  if (!data || ImageInfo->Thumbnail.size < 4) {
3949
449
    return false; /* nothing to do here */
3950
449
  }
3951
0
  if (memcmp(data, "\xFF\xD8\xFF", 3)) {
3952
0
    if (!ImageInfo->Thumbnail.width && !ImageInfo->Thumbnail.height) {
3953
0
      exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Thumbnail is not a JPEG image");
3954
0
    }
3955
0
    return false;
3956
0
  }
3957
0
  for (;;) {
3958
0
    pos += length;
3959
0
    if (pos>=ImageInfo->Thumbnail.size)
3960
0
      return false;
3961
0
    c = data[pos++];
3962
0
    if (pos>=ImageInfo->Thumbnail.size)
3963
0
      return false;
3964
0
    if (c != 0xFF) {
3965
0
      return false;
3966
0
    }
3967
0
    n = 8;
3968
0
    while ((c = data[pos++]) == 0xFF && n--) {
3969
0
      if (pos+3>=ImageInfo->Thumbnail.size)
3970
0
        return false;
3971
      /* +3 = pos++ of next check when reaching marker + 2 bytes for length */
3972
0
    }
3973
0
    if (c == 0xFF)
3974
0
      return false;
3975
0
    marker = c;
3976
0
    if (pos>=ImageInfo->Thumbnail.size)
3977
0
      return false;
3978
0
    length = php_jpg_get16(data+pos);
3979
0
    if (length > ImageInfo->Thumbnail.size || pos >= ImageInfo->Thumbnail.size - length) {
3980
0
      return false;
3981
0
    }
3982
#ifdef EXIF_DEBUG
3983
    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);
3984
#endif
3985
0
    switch (marker) {
3986
0
      case M_SOF0:
3987
0
      case M_SOF1:
3988
0
      case M_SOF2:
3989
0
      case M_SOF3:
3990
0
      case M_SOF5:
3991
0
      case M_SOF6:
3992
0
      case M_SOF7:
3993
0
      case M_SOF9:
3994
0
      case M_SOF10:
3995
0
      case M_SOF11:
3996
0
      case M_SOF13:
3997
0
      case M_SOF14:
3998
0
      case M_SOF15:
3999
        /* handle SOFn block */
4000
0
        if (length < 8 || ImageInfo->Thumbnail.size - 8 < pos) {
4001
          /* exif_process_SOFn needs 8 bytes */
4002
0
          return false;
4003
0
        }
4004
0
        exif_process_SOFn(data+pos, marker, &sof_info);
4005
0
        ImageInfo->Thumbnail.height   = sof_info.height;
4006
0
        ImageInfo->Thumbnail.width    = sof_info.width;
4007
#ifdef EXIF_DEBUG
4008
        exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Thumbnail: size: %d * %d", sof_info.width, sof_info.height);
4009
#endif
4010
0
        return true;
4011
4012
0
      case M_SOS:
4013
0
      case M_EOI:
4014
0
        exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Could not compute size of thumbnail");
4015
0
        return false;
4016
0
        break;
4017
4018
0
      default:
4019
        /* just skip */
4020
0
        break;
4021
0
    }
4022
0
  }
4023
4024
0
  exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Could not compute size of thumbnail");
4025
0
  return false;
4026
0
}
4027
/* }}} */
4028
4029
/* {{{ exif_process_IFD_in_TIFF
4030
 * Parse the TIFF header; */
4031
static bool exif_process_IFD_in_TIFF_impl(image_info_type *ImageInfo, size_t dir_offset, int section_index)
4032
179k
{
4033
179k
  int i, sn, num_entries, sub_section_index = 0;
4034
179k
  unsigned char *dir_entry;
4035
179k
  size_t ifd_size, dir_size, next_offset, entry_length, entry_value=0;
4036
179k
  int entry_tag , entry_type;
4037
179k
  tag_table_type tag_table = exif_get_tag_table(section_index);
4038
4039
179k
  if (ImageInfo->FileSize >= 2 && ImageInfo->FileSize - 2 >= dir_offset) {
4040
169k
    sn = exif_file_sections_add(ImageInfo, M_PSEUDO, 2, NULL);
4041
#ifdef EXIF_DEBUG
4042
    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);
4043
#endif
4044
169k
    php_stream_seek(ImageInfo->infile, dir_offset, SEEK_SET); /* we do not know the order of sections */
4045
169k
    if (UNEXPECTED(exif_read_from_stream_file_looped(ImageInfo->infile, (char*)ImageInfo->file.list[sn].data, 2) != 2)) {
4046
0
      return false;
4047
0
    }
4048
169k
    num_entries = php_ifd_get16u(ImageInfo->file.list[sn].data, ImageInfo->motorola_intel);
4049
169k
    dir_size = 2/*num dir entries*/ +12/*length of entry*/*(size_t)num_entries +4/* offset to next ifd (points to thumbnail or NULL)*/;
4050
169k
    if (ImageInfo->FileSize >= dir_size && ImageInfo->FileSize - dir_size >= dir_offset) {
4051
#ifdef EXIF_DEBUG
4052
      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);
4053
#endif
4054
167k
      if (exif_file_sections_realloc(ImageInfo, sn, dir_size)) {
4055
0
        return false;
4056
0
      }
4057
167k
      if (UNEXPECTED(exif_read_from_stream_file_looped(ImageInfo->infile, (char*)(ImageInfo->file.list[sn].data+2), dir_size-2) != dir_size - 2)) {
4058
0
        return false;
4059
0
      }
4060
167k
      next_offset = php_ifd_get32u(ImageInfo->file.list[sn].data + dir_size - 4, ImageInfo->motorola_intel);
4061
#ifdef EXIF_DEBUG
4062
      exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Read from TIFF done, next offset x%04X", next_offset);
4063
#endif
4064
      /* now we have the directory we can look how long it should be */
4065
167k
      ifd_size = dir_size;
4066
18.9M
      for(i=0;i<num_entries;i++) {
4067
18.7M
        dir_entry    = ImageInfo->file.list[sn].data+2+i*12;
4068
18.7M
        entry_tag    = php_ifd_get16u(dir_entry+0, ImageInfo->motorola_intel);
4069
18.7M
        entry_type   = php_ifd_get16u(dir_entry+2, ImageInfo->motorola_intel);
4070
18.7M
        if (entry_type > NUM_FORMATS) {
4071
15.6M
          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);
4072
          /* Since this is repeated in exif_process_IFD_TAG make it a notice here */
4073
          /* and make it a warning in the exif_process_IFD_TAG which is called    */
4074
          /* elsewhere. */
4075
15.6M
          entry_type = TAG_FMT_BYTE;
4076
          /*The next line would break the image on writeback: */
4077
          /* php_ifd_set16u(dir_entry+2, entry_type, ImageInfo->motorola_intel);*/
4078
15.6M
        }
4079
18.7M
        entry_length = php_ifd_get32u(dir_entry+4, ImageInfo->motorola_intel) * php_tiff_bytes_per_format[entry_type];
4080
18.7M
        if (entry_length <= 4) {
4081
2.93M
          switch(entry_type) {
4082
24.5k
            case TAG_FMT_USHORT:
4083
24.5k
              entry_value  = php_ifd_get16u(dir_entry+8, ImageInfo->motorola_intel);
4084
24.5k
              break;
4085
18.1k
            case TAG_FMT_SSHORT:
4086
18.1k
              entry_value  = php_ifd_get16s(dir_entry+8, ImageInfo->motorola_intel);
4087
18.1k
              break;
4088
11.1k
            case TAG_FMT_ULONG:
4089
11.1k
              entry_value  = php_ifd_get32u(dir_entry+8, ImageInfo->motorola_intel);
4090
11.1k
              break;
4091
14.1k
            case TAG_FMT_SLONG:
4092
14.1k
              entry_value  = php_ifd_get32s(dir_entry+8, ImageInfo->motorola_intel);
4093
14.1k
              break;
4094
2.93M
          }
4095
2.93M
          switch(entry_tag) {
4096
49.7k
            case TAG_IMAGEWIDTH:
4097
54.1k
            case TAG_COMP_IMAGE_WIDTH:
4098
54.1k
              ImageInfo->Width  = entry_value;
4099
54.1k
              break;
4100
10.6k
            case TAG_IMAGEHEIGHT:
4101
12.2k
            case TAG_COMP_IMAGE_HEIGHT:
4102
12.2k
              ImageInfo->Height = entry_value;
4103
12.2k
              break;
4104
6.10k
            case TAG_PHOTOMETRIC_INTERPRETATION:
4105
6.10k
              switch (entry_value) {
4106
1.32k
                case PMI_BLACK_IS_ZERO:
4107
2.00k
                case PMI_WHITE_IS_ZERO:
4108
2.56k
                case PMI_TRANSPARENCY_MASK:
4109
2.56k
                  ImageInfo->IsColor = 0;
4110
2.56k
                  break;
4111
608
                case PMI_RGB:
4112
1.27k
                case PMI_PALETTE_COLOR:
4113
1.84k
                case PMI_SEPARATED:
4114
2.41k
                case PMI_YCBCR:
4115
2.64k
                case PMI_CIELAB:
4116
2.64k
                  ImageInfo->IsColor = 1;
4117
2.64k
                  break;
4118
6.10k
              }
4119
6.10k
              break;
4120
2.93M
          }
4121
15.8M
        } else {
4122
15.8M
          size_t entry_offset = php_ifd_get32u(dir_entry+8, ImageInfo->motorola_intel);
4123
          /* if entry needs expanding ifd cache and entry is at end of current ifd cache. */
4124
          /* otherwise there may be huge holes between two entries */
4125
15.8M
          if (entry_offset + entry_length > dir_offset + ifd_size
4126
15.8M
            && entry_offset == dir_offset + ifd_size) {
4127
6.94k
            ifd_size = entry_offset + entry_length - dir_offset;
4128
#ifdef EXIF_DEBUG
4129
            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);
4130
#endif
4131
6.94k
          }
4132
15.8M
        }
4133
18.7M
      }
4134
167k
      if (ImageInfo->FileSize >= ImageInfo->file.list[sn].size && ImageInfo->FileSize - ImageInfo->file.list[sn].size >= dir_offset) {
4135
167k
        if (ifd_size > dir_size) {
4136
6.94k
          if (ImageInfo->FileSize < ifd_size || dir_offset > ImageInfo->FileSize - ifd_size) {
4137
736
            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);
4138
736
            return false;
4139
736
          }
4140
6.20k
          if (exif_file_sections_realloc(ImageInfo, sn, ifd_size)) {
4141
0
            return false;
4142
0
          }
4143
          /* read values not stored in directory itself */
4144
#ifdef EXIF_DEBUG
4145
          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);
4146
#endif
4147
6.20k
          exif_read_from_stream_file_looped(ImageInfo->infile, (char*)(ImageInfo->file.list[sn].data+dir_size), ifd_size-dir_size);
4148
#ifdef EXIF_DEBUG
4149
          exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Read from TIFF, done");
4150
#endif
4151
6.20k
        }
4152
        /* now process the tags */
4153
463k
        for(i=0;i<num_entries;i++) {
4154
361k
          dir_entry    = ImageInfo->file.list[sn].data+2+i*12;
4155
361k
          entry_tag    = php_ifd_get16u(dir_entry+0, ImageInfo->motorola_intel);
4156
361k
          entry_type   = php_ifd_get16u(dir_entry+2, ImageInfo->motorola_intel);
4157
          /*entry_length = php_ifd_get32u(dir_entry+4, ImageInfo->motorola_intel);*/
4158
361k
          if (entry_tag == TAG_EXIF_IFD_POINTER ||
4159
361k
            entry_tag == TAG_INTEROP_IFD_POINTER ||
4160
361k
            entry_tag == TAG_GPS_IFD_POINTER ||
4161
361k
            entry_tag == TAG_SUB_IFD
4162
361k
          ) {
4163
175k
            switch(entry_tag) {
4164
116k
              case TAG_EXIF_IFD_POINTER:
4165
116k
                ImageInfo->sections_found |= FOUND_EXIF;
4166
116k
                sub_section_index = SECTION_EXIF;
4167
116k
                break;
4168
11.2k
              case TAG_GPS_IFD_POINTER:
4169
11.2k
                ImageInfo->sections_found |= FOUND_GPS;
4170
11.2k
                sub_section_index = SECTION_GPS;
4171
11.2k
                break;
4172
21.4k
              case TAG_INTEROP_IFD_POINTER:
4173
21.4k
                ImageInfo->sections_found |= FOUND_INTEROP;
4174
21.4k
                sub_section_index = SECTION_INTEROP;
4175
21.4k
                break;
4176
26.1k
              case TAG_SUB_IFD:
4177
26.1k
                ImageInfo->sections_found |= FOUND_THUMBNAIL;
4178
26.1k
                sub_section_index = SECTION_THUMBNAIL;
4179
26.1k
                break;
4180
175k
            }
4181
175k
            size_t entry_offset = php_ifd_get32u(dir_entry+8, ImageInfo->motorola_intel);
4182
#ifdef EXIF_DEBUG
4183
            exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Next IFD: %s @0x%04X", exif_get_sectionname(sub_section_index), entry_offset);
4184
#endif
4185
175k
            exif_process_IFD_in_TIFF(ImageInfo, entry_offset, sub_section_index);
4186
175k
            if (section_index!=SECTION_THUMBNAIL && entry_tag==TAG_SUB_IFD) {
4187
4.29k
              if (ImageInfo->Thumbnail.filetype != IMAGE_FILETYPE_UNKNOWN
4188
4.29k
              &&  ImageInfo->Thumbnail.size
4189
4.29k
              &&  ImageInfo->Thumbnail.offset
4190
4.29k
              &&  ImageInfo->read_thumbnail
4191
4.29k
              ) {
4192
#ifdef EXIF_DEBUG
4193
                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);
4194
#endif
4195
0
                if (!ImageInfo->Thumbnail.data) {
4196
0
                  ImageInfo->Thumbnail.data = safe_emalloc(ImageInfo->Thumbnail.size, 1, 0);
4197
0
                  php_stream_seek(ImageInfo->infile, ImageInfo->Thumbnail.offset, SEEK_SET);
4198
0
                  size_t fgot = exif_read_from_stream_file_looped(ImageInfo->infile, ImageInfo->Thumbnail.data, ImageInfo->Thumbnail.size);
4199
0
                  if (fgot != ImageInfo->Thumbnail.size) {
4200
0
                    EXIF_ERRLOG_THUMBEOF(ImageInfo)
4201
0
                    efree(ImageInfo->Thumbnail.data);
4202
4203
0
                    ImageInfo->Thumbnail.data = NULL;
4204
0
                  } else {
4205
0
                    exif_thumbnail_build(ImageInfo);
4206
0
                  }
4207
0
                }
4208
0
              }
4209
4.29k
            }
4210
#ifdef EXIF_DEBUG
4211
            exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Next IFD: %s done", exif_get_sectionname(sub_section_index));
4212
#endif
4213
186k
          } else {
4214
186k
            exif_offset_info info;
4215
186k
            exif_offset_info_init(&info,
4216
186k
              (char *) (ImageInfo->file.list[sn].data - dir_offset),
4217
186k
              (char *) ImageInfo->file.list[sn].data, ifd_size);
4218
186k
            if (!exif_process_IFD_TAG(ImageInfo, (char*)dir_entry, &info,
4219
186k
                          0, section_index, 0, tag_table)) {
4220
64.5k
              return false;
4221
64.5k
            }
4222
186k
          }
4223
361k
        }
4224
        /* If we had a thumbnail in a SUB_IFD we have ANOTHER image in NEXT IFD */
4225
102k
        if (next_offset && section_index != SECTION_THUMBNAIL) {
4226
          /* this should be a thumbnail IFD */
4227
          /* the thumbnail itself is stored at Tag=StripOffsets */
4228
#ifdef EXIF_DEBUG
4229
          exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Read next IFD (THUMBNAIL) at x%04X", next_offset);
4230
#endif
4231
59.7k
          exif_process_IFD_in_TIFF(ImageInfo, next_offset, SECTION_THUMBNAIL);
4232
#ifdef EXIF_DEBUG
4233
          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);
4234
#endif
4235
59.7k
          if (!ImageInfo->Thumbnail.data && ImageInfo->Thumbnail.offset && ImageInfo->Thumbnail.size && ImageInfo->read_thumbnail) {
4236
0
            ImageInfo->Thumbnail.data = safe_emalloc(ImageInfo->Thumbnail.size, 1, 0);
4237
0
            php_stream_seek(ImageInfo->infile, ImageInfo->Thumbnail.offset, SEEK_SET);
4238
0
            size_t fgot = exif_read_from_stream_file_looped(ImageInfo->infile, ImageInfo->Thumbnail.data, ImageInfo->Thumbnail.size);
4239
0
            if (fgot != ImageInfo->Thumbnail.size) {
4240
0
              EXIF_ERRLOG_THUMBEOF(ImageInfo)
4241
0
              efree(ImageInfo->Thumbnail.data);
4242
0
              ImageInfo->Thumbnail.data = NULL;
4243
0
            } else {
4244
0
              exif_thumbnail_build(ImageInfo);
4245
0
            }
4246
0
          }
4247
#ifdef EXIF_DEBUG
4248
          exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Read next IFD (THUMBNAIL) done");
4249
#endif
4250
59.7k
        }
4251
102k
        return true;
4252
166k
      } else {
4253
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);
4254
0
        return false;
4255
0
      }
4256
167k
    } else {
4257
2.51k
      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);
4258
2.51k
      return false;
4259
2.51k
    }
4260
169k
  } else {
4261
9.91k
    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);
4262
9.91k
    return false;
4263
9.91k
  }
4264
179k
}
4265
/* }}} */
4266
4267
static bool exif_process_IFD_in_TIFF(image_info_type *ImageInfo, size_t dir_offset, int section_index)
4268
239k
{
4269
239k
  bool result;
4270
239k
  if (ImageInfo->ifd_count++ > MAX_IFD_TAGS) {
4271
855
    return false;
4272
855
  }
4273
239k
  if (ImageInfo->ifd_nesting_level > MAX_IFD_NESTING_LEVEL) {
4274
59.2k
    return false;
4275
59.2k
  }
4276
179k
  ImageInfo->ifd_nesting_level++;
4277
179k
  result = exif_process_IFD_in_TIFF_impl(ImageInfo, dir_offset, section_index);
4278
179k
  ImageInfo->ifd_nesting_level--;
4279
179k
  return result;
4280
239k
}
4281
4282
/* {{{ exif_scan_FILE_header
4283
 * Parse the marker stream until SOS or EOI is seen; */
4284
static bool exif_scan_FILE_header(image_info_type *ImageInfo)
4285
6.11k
{
4286
6.11k
  unsigned char file_header[8];
4287
4288
6.11k
  ImageInfo->FileType = IMAGE_FILETYPE_UNKNOWN;
4289
4290
6.11k
  if (UNEXPECTED(ImageInfo->FileSize < 2)) {
4291
2
    exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "File too small (%d)", ImageInfo->FileSize);
4292
2
    return false;
4293
2
  }
4294
4295
6.11k
  php_stream_seek(ImageInfo->infile, 0, SEEK_SET);
4296
6.11k
  if (exif_read_from_stream_file_looped(ImageInfo->infile, (char*)file_header, 2) != 2) {
4297
0
    return false;
4298
0
  }
4299
4300
6.11k
  if ((file_header[0]==0xff) && (file_header[1]==M_SOI)) {
4301
1.16k
    ImageInfo->FileType = IMAGE_FILETYPE_JPEG;
4302
1.16k
    if (exif_scan_JPEG_header(ImageInfo)) {
4303
143
      return true;
4304
1.02k
    } else {
4305
1.02k
      exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Invalid JPEG file");
4306
1.02k
      return false;
4307
1.02k
    }
4308
4.94k
  } else if (ImageInfo->FileSize >= 8) {
4309
4.92k
    if (exif_read_from_stream_file_looped(ImageInfo->infile, (char*)(file_header+2), 6) != 6) {
4310
0
      return false;
4311
0
    }
4312
4313
4.92k
    if (!memcmp(file_header, "II\x2A\x00", 4)) {
4314
3.52k
      ImageInfo->FileType = IMAGE_FILETYPE_TIFF_II;
4315
3.52k
      ImageInfo->motorola_intel = 0;
4316
#ifdef EXIF_DEBUG
4317
      exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "File has TIFF/II format");
4318
#endif
4319
3.52k
      ImageInfo->sections_found |= FOUND_IFD0;
4320
3.52k
      if (exif_process_IFD_in_TIFF(
4321
3.52k
        ImageInfo,
4322
3.52k
        php_ifd_get32u(file_header + 4, ImageInfo->motorola_intel),
4323
3.52k
        SECTION_IFD0
4324
3.52k
      )) {
4325
3.05k
        return true;
4326
3.05k
      } else {
4327
472
        exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Invalid TIFF file");
4328
472
        return false;
4329
472
      }
4330
3.52k
    } else if (!memcmp(file_header, "MM\x00\x2a", 4)) {
4331
1.36k
      ImageInfo->FileType = IMAGE_FILETYPE_TIFF_MM;
4332
1.36k
      ImageInfo->motorola_intel = 1;
4333
#ifdef EXIF_DEBUG
4334
      exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "File has TIFF/MM format");
4335
#endif
4336
1.36k
      ImageInfo->sections_found |= FOUND_IFD0;
4337
1.36k
      if (exif_process_IFD_in_TIFF(
4338
1.36k
        ImageInfo,
4339
1.36k
        php_ifd_get32u(file_header + 4, ImageInfo->motorola_intel),
4340
1.36k
        SECTION_IFD0
4341
1.36k
      )) {
4342
1.17k
        return true;
4343
1.17k
      } else {
4344
193
        exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Invalid TIFF file");
4345
193
        return false;
4346
193
      }
4347
1.36k
    } else {
4348
28
      exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "File not supported");
4349
28
      return false;
4350
28
    }
4351
4.92k
  }
4352
23
  return false;
4353
6.11k
}
4354
/* }}} */
4355
4356
/* {{{ exif_discard_imageinfo
4357
   Discard data scanned by exif_read_file.
4358
*/
4359
static void exif_discard_imageinfo(image_info_type *ImageInfo)
4360
6.11k
{
4361
6.11k
  int i;
4362
4363
6.11k
  EFREE_IF(ImageInfo->FileName);
4364
6.11k
  EFREE_IF(ImageInfo->UserComment);
4365
6.11k
  EFREE_IF(ImageInfo->UserCommentEncoding);
4366
6.11k
  EFREE_IF(ImageInfo->Copyright);
4367
6.11k
  EFREE_IF(ImageInfo->CopyrightPhotographer);
4368
6.11k
  EFREE_IF(ImageInfo->CopyrightEditor);
4369
6.11k
  EFREE_IF(ImageInfo->Thumbnail.data);
4370
6.11k
  EFREE_IF(ImageInfo->encode_unicode);
4371
6.11k
  EFREE_IF(ImageInfo->decode_unicode_be);
4372
6.11k
  EFREE_IF(ImageInfo->decode_unicode_le);
4373
6.11k
  EFREE_IF(ImageInfo->encode_jis);
4374
6.11k
  EFREE_IF(ImageInfo->decode_jis_be);
4375
6.11k
  EFREE_IF(ImageInfo->decode_jis_le);
4376
6.11k
  EFREE_IF(ImageInfo->make);
4377
6.11k
  EFREE_IF(ImageInfo->model);
4378
10.2k
  for (i=0; i<ImageInfo->xp_fields.count; i++) {
4379
4.18k
    EFREE_IF(ImageInfo->xp_fields.list[i].value);
4380
4.18k
  }
4381
6.11k
  EFREE_IF(ImageInfo->xp_fields.list);
4382
91.7k
  for (i=0; i<SECTION_COUNT; i++) {
4383
85.5k
    exif_iif_free(ImageInfo, i);
4384
85.5k
  }
4385
6.11k
  exif_file_sections_free(ImageInfo);
4386
6.11k
  memset(ImageInfo, 0, sizeof(*ImageInfo));
4387
6.11k
}
4388
/* }}} */
4389
4390
/* {{{ exif_read_from_impl */
4391
static bool exif_read_from_impl(image_info_type *ImageInfo, php_stream *stream, int read_thumbnail, int read_all)
4392
6.11k
{
4393
6.11k
  bool ret;
4394
6.11k
  zend_stat_t st = {0};
4395
4396
  /* Start with an empty image information structure. */
4397
6.11k
  memset(ImageInfo, 0, sizeof(*ImageInfo));
4398
4399
6.11k
  ImageInfo->motorola_intel = -1; /* flag as unknown */
4400
6.11k
  ImageInfo->infile     = stream;
4401
6.11k
  ImageInfo->FileName     = NULL;
4402
4403
6.11k
  if (php_stream_is(ImageInfo->infile, PHP_STREAM_IS_STDIO)) {
4404
0
    if (stream->orig_path && VCWD_STAT(stream->orig_path, &st) >= 0) {
4405
0
      zend_string *base;
4406
0
      if ((st.st_mode & S_IFMT) != S_IFREG) {
4407
0
        exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Not a file");
4408
0
        ImageInfo->infile = NULL;
4409
0
        return false;
4410
0
      }
4411
4412
      /* Store file name */
4413
0
      base = php_basename(stream->orig_path, strlen(stream->orig_path), NULL, 0);
4414
0
      ImageInfo->FileName = estrndup(ZSTR_VAL(base), ZSTR_LEN(base));
4415
4416
0
      zend_string_release_ex(base, 0);
4417
4418
      /* Store file date/time. */
4419
0
      ImageInfo->FileDateTime = st.st_mtime;
4420
0
      ImageInfo->FileSize = st.st_size;
4421
0
    }
4422
6.11k
  } else {
4423
6.11k
    if (!ImageInfo->FileSize) {
4424
6.11k
      php_stream_seek(ImageInfo->infile, 0, SEEK_END);
4425
6.11k
      ImageInfo->FileSize = php_stream_tell(ImageInfo->infile);
4426
6.11k
      php_stream_seek(ImageInfo->infile, 0, SEEK_SET);
4427
6.11k
    }
4428
6.11k
  }
4429
4430
6.11k
  ImageInfo->read_thumbnail   = read_thumbnail;
4431
6.11k
  ImageInfo->read_all       = read_all;
4432
6.11k
  ImageInfo->Thumbnail.filetype = IMAGE_FILETYPE_UNKNOWN;
4433
4434
6.11k
  ImageInfo->encode_unicode   = estrdup(EXIF_G(encode_unicode));
4435
6.11k
  ImageInfo->decode_unicode_be  = estrdup(EXIF_G(decode_unicode_be));
4436
6.11k
  ImageInfo->decode_unicode_le  = estrdup(EXIF_G(decode_unicode_le));
4437
6.11k
  ImageInfo->encode_jis     = estrdup(EXIF_G(encode_jis));
4438
6.11k
  ImageInfo->decode_jis_be    = estrdup(EXIF_G(decode_jis_be));
4439
6.11k
  ImageInfo->decode_jis_le    = estrdup(EXIF_G(decode_jis_le));
4440
4441
4442
6.11k
  ImageInfo->ifd_nesting_level = 0;
4443
6.11k
  ImageInfo->ifd_count = 0;
4444
6.11k
  ImageInfo->num_errors = 0;
4445
4446
  /* Scan the headers */
4447
6.11k
  ret = exif_scan_FILE_header(ImageInfo);
4448
4449
6.11k
  return ret;
4450
6.11k
}
4451
/* }}} */
4452
4453
/* {{{ exif_read_from_stream */
4454
static bool exif_read_from_stream(image_info_type *ImageInfo, php_stream *stream, int read_thumbnail, int read_all)
4455
6.11k
{
4456
6.11k
  bool ret;
4457
6.11k
  off_t old_pos = php_stream_tell(stream);
4458
4459
6.11k
  if (old_pos) {
4460
6.11k
    php_stream_seek(stream, 0, SEEK_SET);
4461
6.11k
  }
4462
4463
6.11k
  ret = exif_read_from_impl(ImageInfo, stream, read_thumbnail, read_all);
4464
4465
6.11k
  if (old_pos) {
4466
6.11k
    php_stream_seek(stream, old_pos, SEEK_SET);
4467
6.11k
  }
4468
4469
6.11k
  return ret;
4470
6.11k
}
4471
/* }}} */
4472
4473
/* {{{ exif_read_from_file */
4474
static bool exif_read_from_file(image_info_type *ImageInfo, const char *FileName, int read_thumbnail, int read_all)
4475
0
{
4476
0
  bool ret;
4477
0
  php_stream *stream;
4478
4479
0
  stream = php_stream_open_wrapper(FileName, "rb", STREAM_MUST_SEEK | IGNORE_PATH, NULL);
4480
4481
0
  if (!stream) {
4482
0
    memset(&ImageInfo, 0, sizeof(ImageInfo));
4483
4484
0
    exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Unable to open file");
4485
4486
0
    return false;
4487
0
  }
4488
4489
0
  ret = exif_read_from_stream(ImageInfo, stream, read_thumbnail, read_all);
4490
4491
0
  php_stream_close(stream);
4492
4493
0
  return ret;
4494
0
}
4495
/* }}} */
4496
4497
/* {{{ Reads header data from an image and optionally reads the internal thumbnails */
4498
PHP_FUNCTION(exif_read_data)
4499
6.11k
{
4500
6.11k
  zend_string *z_sections_needed = NULL;
4501
6.11k
  bool sub_arrays = 0, read_thumbnail = 0, read_all = 0;
4502
6.11k
  zval *stream;
4503
6.11k
  bool ret;
4504
6.11k
  int i, sections_needed = 0;
4505
6.11k
  image_info_type ImageInfo;
4506
6.11k
  char *sections_str;
4507
4508
  /* Parse arguments */
4509
18.3k
  ZEND_PARSE_PARAMETERS_START(1, 4)
4510
24.4k
    Z_PARAM_ZVAL(stream)
4511
24.4k
    Z_PARAM_OPTIONAL
4512
24.4k
    Z_PARAM_STR_OR_NULL(z_sections_needed)
4513
0
    Z_PARAM_BOOL(sub_arrays)
4514
0
    Z_PARAM_BOOL(read_thumbnail)
4515
6.11k
  ZEND_PARSE_PARAMETERS_END();
4516
4517
6.11k
  memset(&ImageInfo, 0, sizeof(ImageInfo));
4518
4519
6.11k
  if (z_sections_needed) {
4520
0
    spprintf(&sections_str, 0, ",%s,", ZSTR_VAL(z_sections_needed));
4521
    /* sections_str DOES start with , and SPACES are NOT allowed in names */
4522
0
    char *s = sections_str;
4523
0
    while (*++s) {
4524
0
      if (*s == ' ') {
4525
0
        *s = ',';
4526
0
      }
4527
0
    }
4528
4529
0
    for (i = 0; i < SECTION_COUNT; i++) {
4530
0
      char tmp[64];
4531
0
      snprintf(tmp, sizeof(tmp), ",%s,", exif_get_sectionname(i));
4532
0
      if (strstr(sections_str, tmp)) {
4533
0
        sections_needed |= 1<<i;
4534
0
      }
4535
0
    }
4536
0
    EFREE_IF(sections_str);
4537
    /* now see what we need */
4538
#ifdef EXIF_DEBUG
4539
    sections_str = exif_get_sectionlist(sections_needed);
4540
    if (!sections_str) {
4541
      RETURN_FALSE;
4542
    }
4543
    exif_error_docref(NULL EXIFERR_CC, &ImageInfo, E_NOTICE, "Sections needed: %s", sections_str[0] ? sections_str : "None");
4544
    EFREE_IF(sections_str);
4545
#endif
4546
0
  }
4547
4548
6.11k
  if (Z_TYPE_P(stream) == IS_RESOURCE) {
4549
6.11k
    php_stream *p_stream = NULL;
4550
4551
6.11k
    php_stream_from_res(p_stream, Z_RES_P(stream));
4552
4553
6.11k
    ret = exif_read_from_stream(&ImageInfo, p_stream, read_thumbnail, read_all);
4554
6.11k
  } else {
4555
0
    if (!try_convert_to_string(stream)) {
4556
0
      RETURN_THROWS();
4557
0
    }
4558
4559
0
    if (!Z_STRLEN_P(stream)) {
4560
0
      zend_argument_must_not_be_empty_error(1);
4561
0
      RETURN_THROWS();
4562
0
    }
4563
4564
0
    if (UNEXPECTED(zend_str_has_nul_byte(Z_STR_P(stream)))) {
4565
0
      zend_argument_value_error(1, "must not contain any null bytes");
4566
0
      RETURN_THROWS();
4567
0
    }
4568
4569
0
    ret = exif_read_from_file(&ImageInfo, Z_STRVAL_P(stream), read_thumbnail, read_all);
4570
0
  }
4571
4572
6.11k
  sections_str = exif_get_sectionlist(ImageInfo.sections_found);
4573
4574
#ifdef EXIF_DEBUG
4575
  if (sections_str) {
4576
    exif_error_docref(NULL EXIFERR_CC, &ImageInfo, E_NOTICE, "Sections found: %s", sections_str[0] ? sections_str : "None");
4577
  }
4578
#endif
4579
4580
6.11k
  ImageInfo.sections_found |= FOUND_COMPUTED|FOUND_FILE;/* do not inform about in debug*/
4581
4582
6.11k
  if (ret == false || (sections_needed && !(sections_needed&ImageInfo.sections_found))) {
4583
    /* array_init must be checked at last! otherwise the array must be freed if a later test fails. */
4584
1.74k
    exif_discard_imageinfo(&ImageInfo);
4585
1.74k
      EFREE_IF(sections_str);
4586
1.74k
    RETURN_FALSE;
4587
1.74k
  }
4588
4589
4.37k
  array_init(return_value);
4590
4591
#ifdef EXIF_DEBUG
4592
  exif_error_docref(NULL EXIFERR_CC, &ImageInfo, E_NOTICE, "Generate section FILE");
4593
#endif
4594
4595
  /* now we can add our information */
4596
4.37k
  exif_iif_add_str(&ImageInfo, SECTION_FILE, "FileName",      ImageInfo.FileName);
4597
4.37k
  exif_iif_add_int(&ImageInfo, SECTION_FILE, "FileDateTime",  ImageInfo.FileDateTime);
4598
4.37k
  exif_iif_add_int(&ImageInfo, SECTION_FILE, "FileSize",      ImageInfo.FileSize);
4599
4.37k
  exif_iif_add_int(&ImageInfo, SECTION_FILE, "FileType",      ImageInfo.FileType);
4600
4.37k
  exif_iif_add_str(&ImageInfo, SECTION_FILE, "MimeType",      php_image_type_to_mime_type(ImageInfo.FileType));
4601
4.37k
  exif_iif_add_str(&ImageInfo, SECTION_FILE, "SectionsFound", sections_str ? sections_str : "NONE");
4602
4603
#ifdef EXIF_DEBUG
4604
  exif_error_docref(NULL EXIFERR_CC, &ImageInfo, E_NOTICE, "Generate section COMPUTED");
4605
#endif
4606
4607
4.37k
  if (ImageInfo.Width>0 &&  ImageInfo.Height>0) {
4608
76
    exif_iif_add_fmt(&ImageInfo, SECTION_COMPUTED, "html"   , "width=\"%d\" height=\"%d\"", ImageInfo.Width, ImageInfo.Height);
4609
76
    exif_iif_add_int(&ImageInfo, SECTION_COMPUTED, "Height", ImageInfo.Height);
4610
76
    exif_iif_add_int(&ImageInfo, SECTION_COMPUTED, "Width",  ImageInfo.Width);
4611
76
  }
4612
4.37k
  exif_iif_add_int(&ImageInfo, SECTION_COMPUTED, "IsColor", ImageInfo.IsColor);
4613
4.37k
  if (ImageInfo.motorola_intel != -1) {
4614
4.23k
    exif_iif_add_int(&ImageInfo, SECTION_COMPUTED, "ByteOrderMotorola", ImageInfo.motorola_intel);
4615
4.23k
  }
4616
4.37k
  if (ImageInfo.FocalLength) {
4617
0
    exif_iif_add_fmt(&ImageInfo, SECTION_COMPUTED, "FocalLength", "%4.1Fmm", ImageInfo.FocalLength);
4618
0
    if(ImageInfo.CCDWidth) {
4619
0
      exif_iif_add_fmt(&ImageInfo, SECTION_COMPUTED, "35mmFocalLength", "%dmm", (int)(ImageInfo.FocalLength/ImageInfo.CCDWidth*35+0.5));
4620
0
    }
4621
0
  }
4622
4.37k
  if(ImageInfo.CCDWidth) {
4623
1
    exif_iif_add_fmt(&ImageInfo, SECTION_COMPUTED, "CCDWidth", "%dmm", (int)ImageInfo.CCDWidth);
4624
1
  }
4625
4.37k
  if(ImageInfo.ExposureTime>0) {
4626
84
    float recip_exposure_time = 0.5f + 1.0f/ImageInfo.ExposureTime;
4627
84
    if (ImageInfo.ExposureTime <= 0.5 && recip_exposure_time < (float)INT_MAX) {
4628
22
      exif_iif_add_fmt(&ImageInfo, SECTION_COMPUTED, "ExposureTime", "%0.3F s (1/%d)", ImageInfo.ExposureTime, (int) recip_exposure_time);
4629
62
    } else {
4630
62
      exif_iif_add_fmt(&ImageInfo, SECTION_COMPUTED, "ExposureTime", "%0.3F s", ImageInfo.ExposureTime);
4631
62
    }
4632
84
  }
4633
4.37k
  if(ImageInfo.ApertureFNumber) {
4634
219
    exif_iif_add_fmt(&ImageInfo, SECTION_COMPUTED, "ApertureFNumber", "f/%.1F", ImageInfo.ApertureFNumber);
4635
219
  }
4636
4.37k
  if(ImageInfo.Distance) {
4637
185
    if(ImageInfo.Distance<0) {
4638
15
      exif_iif_add_str(&ImageInfo, SECTION_COMPUTED, "FocusDistance", "Infinite");
4639
170
    } else {
4640
170
      exif_iif_add_fmt(&ImageInfo, SECTION_COMPUTED, "FocusDistance", "%0.2Fm", ImageInfo.Distance);
4641
170
    }
4642
185
  }
4643
4.37k
  if (ImageInfo.UserComment) {
4644
293
    exif_iif_add_buffer(&ImageInfo, SECTION_COMPUTED, "UserComment", ImageInfo.UserCommentLength, ImageInfo.UserComment);
4645
293
    if (ImageInfo.UserCommentEncoding && strlen(ImageInfo.UserCommentEncoding)) {
4646
102
      exif_iif_add_str(&ImageInfo, SECTION_COMPUTED, "UserCommentEncoding", ImageInfo.UserCommentEncoding);
4647
102
    }
4648
293
  }
4649
4650
4.37k
  exif_iif_add_str(&ImageInfo, SECTION_COMPUTED, "Copyright",              ImageInfo.Copyright);
4651
4.37k
  exif_iif_add_str(&ImageInfo, SECTION_COMPUTED, "Copyright.Photographer", ImageInfo.CopyrightPhotographer);
4652
4.37k
  exif_iif_add_str(&ImageInfo, SECTION_COMPUTED, "Copyright.Editor",       ImageInfo.CopyrightEditor);
4653
4654
8.55k
  for (i=0; i<ImageInfo.xp_fields.count; i++) {
4655
4.18k
    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);
4656
4.18k
  }
4657
4.37k
  if (ImageInfo.Thumbnail.size) {
4658
539
    if (read_thumbnail) {
4659
      /* not exif_iif_add_str : this is a buffer */
4660
0
      exif_iif_add_tag(&ImageInfo, SECTION_THUMBNAIL, "THUMBNAIL", TAG_NONE, TAG_FMT_UNDEFINED, ImageInfo.Thumbnail.size, ImageInfo.Thumbnail.data, ImageInfo.Thumbnail.size);
4661
0
    }
4662
539
    if (!ImageInfo.Thumbnail.width || !ImageInfo.Thumbnail.height) {
4663
      /* try to evaluate if thumbnail data is present */
4664
449
      exif_scan_thumbnail(&ImageInfo);
4665
449
    }
4666
539
    exif_iif_add_int(&ImageInfo, SECTION_COMPUTED, "Thumbnail.FileType", ImageInfo.Thumbnail.filetype);
4667
539
    exif_iif_add_str(&ImageInfo, SECTION_COMPUTED, "Thumbnail.MimeType", (char*)php_image_type_to_mime_type(ImageInfo.Thumbnail.filetype));
4668
539
  }
4669
4.37k
  if (ImageInfo.Thumbnail.width && ImageInfo.Thumbnail.height) {
4670
178
    exif_iif_add_int(&ImageInfo, SECTION_COMPUTED, "Thumbnail.Height", ImageInfo.Thumbnail.height);
4671
178
    exif_iif_add_int(&ImageInfo, SECTION_COMPUTED, "Thumbnail.Width",  ImageInfo.Thumbnail.width);
4672
178
  }
4673
4.37k
  EFREE_IF(sections_str);
4674
4675
#ifdef EXIF_DEBUG
4676
  exif_error_docref(NULL EXIFERR_CC, &ImageInfo, E_NOTICE, "Adding image infos");
4677
#endif
4678
4679
4.37k
  add_assoc_image_info(return_value, sub_arrays, &ImageInfo, SECTION_FILE      );
4680
4.37k
  add_assoc_image_info(return_value, 1,          &ImageInfo, SECTION_COMPUTED  );
4681
4.37k
  add_assoc_image_info(return_value, sub_arrays, &ImageInfo, SECTION_ANY_TAG   );
4682
4.37k
  add_assoc_image_info(return_value, sub_arrays, &ImageInfo, SECTION_IFD0      );
4683
4.37k
  add_assoc_image_info(return_value, 1,          &ImageInfo, SECTION_THUMBNAIL );
4684
4.37k
  add_assoc_image_info(return_value, 1,          &ImageInfo, SECTION_COMMENT   );
4685
4.37k
  add_assoc_image_info(return_value, sub_arrays, &ImageInfo, SECTION_EXIF      );
4686
4.37k
  add_assoc_image_info(return_value, sub_arrays, &ImageInfo, SECTION_GPS       );
4687
4.37k
  add_assoc_image_info(return_value, sub_arrays, &ImageInfo, SECTION_INTEROP   );
4688
4.37k
  add_assoc_image_info(return_value, sub_arrays, &ImageInfo, SECTION_FPIX      );
4689
4.37k
  add_assoc_image_info(return_value, sub_arrays, &ImageInfo, SECTION_APP12     );
4690
4.37k
  add_assoc_image_info(return_value, sub_arrays, &ImageInfo, SECTION_WINXP     );
4691
4.37k
  add_assoc_image_info(return_value, sub_arrays, &ImageInfo, SECTION_MAKERNOTE );
4692
4693
#ifdef EXIF_DEBUG
4694
  exif_error_docref(NULL EXIFERR_CC, &ImageInfo, E_NOTICE, "Discarding info");
4695
#endif
4696
4697
4.37k
  exif_discard_imageinfo(&ImageInfo);
4698
4699
#ifdef EXIF_DEBUG
4700
  php_error_docref1(NULL, (Z_TYPE_P(stream) == IS_RESOURCE ? "<stream>" : Z_STRVAL_P(stream)), E_NOTICE, "Done");
4701
#endif
4702
4.37k
}
4703
/* }}} */
4704
4705
/* {{{ Reads the embedded thumbnail */
4706
PHP_FUNCTION(exif_thumbnail)
4707
0
{
4708
0
  bool ret;
4709
0
  image_info_type ImageInfo;
4710
0
  zval *stream;
4711
0
  zval *z_width = NULL, *z_height = NULL, *z_imagetype = NULL;
4712
4713
  /* Parse arguments */
4714
0
  ZEND_PARSE_PARAMETERS_START(1, 4)
4715
0
    Z_PARAM_ZVAL(stream)
4716
0
    Z_PARAM_OPTIONAL
4717
0
    Z_PARAM_ZVAL(z_width)
4718
0
    Z_PARAM_ZVAL(z_height)
4719
0
    Z_PARAM_ZVAL(z_imagetype)
4720
0
  ZEND_PARSE_PARAMETERS_END();
4721
4722
0
  memset(&ImageInfo, 0, sizeof(ImageInfo));
4723
4724
0
  if (Z_TYPE_P(stream) == IS_RESOURCE) {
4725
0
    php_stream *p_stream = NULL;
4726
4727
0
    php_stream_from_res(p_stream, Z_RES_P(stream));
4728
4729
0
    ret = exif_read_from_stream(&ImageInfo, p_stream, 1, 0);
4730
0
  } else {
4731
0
    if (!try_convert_to_string(stream)) {
4732
0
      RETURN_THROWS();
4733
0
    }
4734
4735
0
    if (!Z_STRLEN_P(stream)) {
4736
0
      zend_argument_must_not_be_empty_error(1);
4737
0
      RETURN_THROWS();
4738
0
    }
4739
4740
0
    if (zend_str_has_nul_byte(Z_STR_P(stream))) {
4741
0
      zend_argument_value_error(1, "must not contain any null bytes");
4742
0
      RETURN_THROWS();
4743
0
    }
4744
4745
0
    ret = exif_read_from_file(&ImageInfo, Z_STRVAL_P(stream), 1, 0);
4746
0
  }
4747
4748
0
  if (ret == false) {
4749
0
    exif_discard_imageinfo(&ImageInfo);
4750
0
    RETURN_FALSE;
4751
0
  }
4752
4753
#ifdef EXIF_DEBUG
4754
  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);
4755
#endif
4756
0
  if (!ImageInfo.Thumbnail.data || !ImageInfo.Thumbnail.size) {
4757
0
    exif_discard_imageinfo(&ImageInfo);
4758
0
    RETURN_FALSE;
4759
0
  }
4760
4761
#ifdef EXIF_DEBUG
4762
  exif_error_docref(NULL EXIFERR_CC, &ImageInfo, E_NOTICE, "Returning thumbnail(%d)", ImageInfo.Thumbnail.size);
4763
#endif
4764
4765
0
  RETVAL_STRINGL(ImageInfo.Thumbnail.data, ImageInfo.Thumbnail.size);
4766
0
  if ((z_width || z_height) && (!ImageInfo.Thumbnail.width || !ImageInfo.Thumbnail.height)) {
4767
0
    if (!exif_scan_thumbnail(&ImageInfo)) {
4768
0
      ImageInfo.Thumbnail.width = ImageInfo.Thumbnail.height = 0;
4769
0
    }
4770
0
  }
4771
0
  if (z_width) {
4772
0
    ZEND_TRY_ASSIGN_REF_LONG(z_width,  ImageInfo.Thumbnail.width);
4773
0
  }
4774
0
  if (z_height) {
4775
0
    ZEND_TRY_ASSIGN_REF_LONG(z_height, ImageInfo.Thumbnail.height);
4776
0
  }
4777
0
  if (z_imagetype) {
4778
0
    ZEND_TRY_ASSIGN_REF_LONG(z_imagetype, ImageInfo.Thumbnail.filetype);
4779
0
  }
4780
4781
#ifdef EXIF_DEBUG
4782
  exif_error_docref(NULL EXIFERR_CC, &ImageInfo, E_NOTICE, "Discarding info");
4783
#endif
4784
4785
0
  exif_discard_imageinfo(&ImageInfo);
4786
4787
#ifdef EXIF_DEBUG
4788
  php_error_docref1(NULL, (Z_TYPE_P(stream) == IS_RESOURCE ? "<stream>" : Z_STRVAL_P(stream)), E_NOTICE, "Done");
4789
#endif
4790
0
}
4791
/* }}} */
4792
4793
/* {{{ Get the type of an image */
4794
PHP_FUNCTION(exif_imagetype)
4795
0
{
4796
0
  char *imagefile;
4797
0
  size_t imagefile_len;
4798
0
  php_stream * stream;
4799
0
  int itype = 0;
4800
4801
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "p", &imagefile, &imagefile_len) == FAILURE) {
4802
0
    RETURN_THROWS();
4803
0
  }
4804
4805
0
  stream = php_stream_open_wrapper(imagefile, "rb", IGNORE_PATH|REPORT_ERRORS, NULL);
4806
4807
0
  if (stream == NULL) {
4808
0
    RETURN_FALSE;
4809
0
  }
4810
4811
0
  itype = php_getimagetype(stream, imagefile, NULL);
4812
4813
0
  php_stream_close(stream);
4814
4815
0
  if (itype == IMAGE_FILETYPE_UNKNOWN) {
4816
0
    RETURN_FALSE;
4817
0
  } else {
4818
0
    ZVAL_LONG(return_value, itype);
4819
0
  }
4820
0
}
4821
/* }}} */