Coverage Report

Created: 2022-02-19 20:30

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