exifreader 4.34.0 → 4.35.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "exifreader",
3
- "version": "4.34.0",
3
+ "version": "4.35.0",
4
4
  "description": "Library that parses Exif metadata in images.",
5
5
  "author": "Mattias Wallander <mattias@wallander.eu>",
6
6
  "repository": {
@@ -4,6 +4,17 @@
4
4
 
5
5
  import {getStringFromDataView} from './utils.js';
6
6
  import Constants from './constants.js';
7
+ import ByteOrder from './byte-order.js';
8
+ import {
9
+ IFD_ENTRY_COUNT_LENGTH,
10
+ IFD_ENTRY_LENGTH,
11
+ NEXT_IFD_POINTER_LENGTH,
12
+ TIFF_BYTE_ORDER_OFFSET,
13
+ TIFF_HEADER_LENGTH,
14
+ TIFF_ID,
15
+ TIFF_ID_OFFSET,
16
+ TIFF_IFD_OFFSET_OFFSET,
17
+ } from './tiff-constants.js';
7
18
 
8
19
  export default {
9
20
  isJpegFile,
@@ -68,6 +79,11 @@ function findJpegOffsets(dataView) {
68
79
  let xmpChunks;
69
80
  let iccChunks;
70
81
  let mpfDataOffset;
82
+ let exifSegmentCount = 0;
83
+ let bestExifSegmentScore;
84
+ let bestExifSegmentTiffHeaderOffset;
85
+ let bestExifSegmentAppMarkerPosition;
86
+ let bestExifSegmentFieldLength;
71
87
 
72
88
  while (appMarkerPosition + APP_ID_OFFSET + 5 <= dataView.byteLength) {
73
89
  if (Constants.USE_FILE && isSOF0Marker(dataView, appMarkerPosition)) {
@@ -81,7 +97,50 @@ function findJpegOffsets(dataView) {
81
97
  jfifDataOffset = appMarkerPosition + JFIF_DATA_OFFSET;
82
98
  } else if (Constants.USE_EXIF && isApp1ExifMarker(dataView, appMarkerPosition)) {
83
99
  fieldLength = dataView.getUint16(appMarkerPosition + APP_MARKER_SIZE);
84
- tiffHeaderOffset = appMarkerPosition + TIFF_HEADER_OFFSET;
100
+ exifSegmentCount++;
101
+
102
+ const currentTiffHeaderOffset = appMarkerPosition + TIFF_HEADER_OFFSET;
103
+
104
+ if (exifSegmentCount === 1) {
105
+ bestExifSegmentTiffHeaderOffset = currentTiffHeaderOffset;
106
+ bestExifSegmentAppMarkerPosition = appMarkerPosition;
107
+ bestExifSegmentFieldLength = fieldLength;
108
+ } else if (exifSegmentCount === 2) {
109
+ bestExifSegmentScore = getExifMarkerScore(
110
+ dataView,
111
+ bestExifSegmentAppMarkerPosition,
112
+ bestExifSegmentFieldLength,
113
+ bestExifSegmentTiffHeaderOffset
114
+ );
115
+ const currentExifSegmentScore = getExifMarkerScore(
116
+ dataView,
117
+ appMarkerPosition,
118
+ fieldLength,
119
+ currentTiffHeaderOffset
120
+ );
121
+
122
+ if (currentExifSegmentScore > bestExifSegmentScore) {
123
+ bestExifSegmentScore = currentExifSegmentScore;
124
+ bestExifSegmentTiffHeaderOffset = currentTiffHeaderOffset;
125
+ bestExifSegmentAppMarkerPosition = appMarkerPosition;
126
+ bestExifSegmentFieldLength = fieldLength;
127
+ }
128
+ } else {
129
+ const currentExifSegmentScore = getExifMarkerScore(
130
+ dataView,
131
+ appMarkerPosition,
132
+ fieldLength,
133
+ currentTiffHeaderOffset
134
+ );
135
+ if (currentExifSegmentScore > bestExifSegmentScore) {
136
+ bestExifSegmentScore = currentExifSegmentScore;
137
+ bestExifSegmentTiffHeaderOffset = currentTiffHeaderOffset;
138
+ bestExifSegmentAppMarkerPosition = appMarkerPosition;
139
+ bestExifSegmentFieldLength = fieldLength;
140
+ }
141
+ }
142
+
143
+ tiffHeaderOffset = bestExifSegmentTiffHeaderOffset;
85
144
  } else if (Constants.USE_XMP && isApp1XmpMarker(dataView, appMarkerPosition)) {
86
145
  if (!xmpChunks) {
87
146
  xmpChunks = [];
@@ -122,6 +181,10 @@ function findJpegOffsets(dataView) {
122
181
  appMarkerPosition += APP_MARKER_SIZE + fieldLength;
123
182
  }
124
183
 
184
+ if (exifSegmentCount > 1) {
185
+ warnAboutMultipleExifSegments(exifSegmentCount);
186
+ }
187
+
125
188
  return {
126
189
  hasAppMarkers: appMarkerPosition > JPEG_ID_SIZE,
127
190
  fileDataOffset: sof0DataOffset || sof2DataOffset,
@@ -229,3 +292,74 @@ function isAppMarker(dataView, appMarkerPosition) {
229
292
  function isFillByte(dataView, appMarkerPosition) {
230
293
  return dataView.getUint16(appMarkerPosition) === FILL_BYTE;
231
294
  }
295
+
296
+ function getExifMarkerScore(dataView, appMarkerPosition, fieldLength, tiffHeaderOffset) {
297
+ const segmentEnd = appMarkerPosition + APP_MARKER_SIZE + fieldLength;
298
+ const details = getExifTiffHeaderDetails(dataView, tiffHeaderOffset, segmentEnd);
299
+ if (!details) {
300
+ return 0;
301
+ }
302
+
303
+ const ifdEntriesScore = Number.isInteger(details.ifdEntries) ? details.ifdEntries : 0;
304
+
305
+ return (details.isValid ? 1000000000 : 0)
306
+ + (ifdEntriesScore * 1000000)
307
+ + fieldLength;
308
+ }
309
+
310
+ function getExifTiffHeaderDetails(dataView, tiffHeaderOffset, segmentEnd) {
311
+ try {
312
+ if (tiffHeaderOffset + TIFF_HEADER_LENGTH > segmentEnd) {
313
+ return undefined;
314
+ }
315
+
316
+ const byteOrderRaw = dataView.getUint16(
317
+ tiffHeaderOffset + TIFF_BYTE_ORDER_OFFSET
318
+ );
319
+ const isLittleEndian = byteOrderRaw === ByteOrder.LITTLE_ENDIAN;
320
+ if (!isLittleEndian && (byteOrderRaw !== ByteOrder.BIG_ENDIAN)) {
321
+ return undefined;
322
+ }
323
+
324
+ const tiffId = dataView.getUint16(
325
+ tiffHeaderOffset + TIFF_ID_OFFSET,
326
+ isLittleEndian
327
+ );
328
+ if (tiffId !== TIFF_ID) {
329
+ return undefined;
330
+ }
331
+
332
+ const ifdOffset = dataView.getUint32(
333
+ tiffHeaderOffset + TIFF_IFD_OFFSET_OFFSET,
334
+ isLittleEndian
335
+ );
336
+ const ifdEntryCountOffset = tiffHeaderOffset + ifdOffset;
337
+ if (ifdEntryCountOffset + IFD_ENTRY_COUNT_LENGTH > segmentEnd) {
338
+ return undefined;
339
+ }
340
+
341
+ const ifdEntries = dataView.getUint16(ifdEntryCountOffset, isLittleEndian);
342
+ const ifdTotalLength = IFD_ENTRY_COUNT_LENGTH
343
+ + (ifdEntries * IFD_ENTRY_LENGTH)
344
+ + NEXT_IFD_POINTER_LENGTH;
345
+ const hasFullIfd = (ifdEntryCountOffset + ifdTotalLength) <= segmentEnd;
346
+ const isValid = hasFullIfd && (ifdEntries > 0);
347
+
348
+ return {ifdEntries, isValid};
349
+ } catch (error) {
350
+ return undefined;
351
+ }
352
+ }
353
+
354
+ function warnAboutMultipleExifSegments(exifSegmentCount) {
355
+ /* eslint-disable no-console */
356
+ if (typeof console === 'undefined' || typeof console.warn !== 'function') {
357
+ return;
358
+ }
359
+
360
+ console.warn(
361
+ `ExifReader: Found ${exifSegmentCount} Exif segments (APP1). `
362
+ + 'Will pick the best candidate segment.'
363
+ );
364
+ /* eslint-enable no-console */
365
+ }
@@ -4,6 +4,7 @@
4
4
 
5
5
  import ByteOrder from './byte-order.js';
6
6
  import Constants from './constants.js';
7
+ import {TIFF_ID, TIFF_ID_OFFSET} from './tiff-constants.js';
7
8
 
8
9
  export default {
9
10
  isTiffFile,
@@ -17,9 +18,6 @@ function isTiffFile(dataView) {
17
18
  }
18
19
 
19
20
  function hasTiffMarker(dataView) {
20
- const TIFF_ID = 0x2a;
21
- const TIFF_ID_OFFSET = 2;
22
-
23
21
  const littleEndian = dataView.getUint16(0) === ByteOrder.LITTLE_ENDIAN;
24
22
  return dataView.getUint16(TIFF_ID_OFFSET, littleEndian) === TIFF_ID;
25
23
  }
@@ -5,6 +5,7 @@
5
5
  import Constants from './constants.js';
6
6
  import Types from './types.js';
7
7
  import TagNames, {IFD_TYPE_0TH, IFD_TYPE_1ST, IFD_TYPE_PENTAX} from './tag-names.js';
8
+ import {IFD_ENTRY_LENGTH, TIFF_IFD_OFFSET_OFFSET} from './tiff-constants.js';
8
9
 
9
10
  const getTagValueAt = {
10
11
  1: Types.getByteAt,
@@ -19,12 +20,17 @@ const getTagValueAt = {
19
20
  };
20
21
 
21
22
  export function get0thIfdOffset(dataView, tiffHeaderOffset, byteOrder) {
22
- return tiffHeaderOffset + Types.getLongAt(dataView, tiffHeaderOffset + 4, byteOrder);
23
+ return tiffHeaderOffset
24
+ + Types.getLongAt(
25
+ dataView,
26
+ tiffHeaderOffset + TIFF_IFD_OFFSET_OFFSET,
27
+ byteOrder
28
+ );
23
29
  }
24
30
 
25
31
  export function readIfd(dataView, ifdType, offsetOrigin, offset, byteOrder, includeUnknown, computed = false) {
26
32
  const FIELD_COUNT_SIZE = Types.getTypeSize('SHORT');
27
- const FIELD_SIZE = 12;
33
+ const FIELD_SIZE = IFD_ENTRY_LENGTH;
28
34
 
29
35
  const tags = {};
30
36
  const numberOfFields = getNumberOfFields(dataView, offset, byteOrder);
@@ -0,0 +1,25 @@
1
+ /* This Source Code Form is subject to the terms of the Mozilla Public
2
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
3
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
+
5
+ export const TIFF_BYTE_ORDER_OFFSET = 0;
6
+ export const TIFF_ID_OFFSET = 2;
7
+ export const TIFF_IFD_OFFSET_OFFSET = 4;
8
+ export const TIFF_HEADER_LENGTH = 8;
9
+
10
+ export const TIFF_ID = 0x2a;
11
+
12
+ export const IFD_ENTRY_COUNT_LENGTH = 2;
13
+ export const IFD_ENTRY_LENGTH = 12;
14
+ export const NEXT_IFD_POINTER_LENGTH = 4;
15
+
16
+ export default {
17
+ TIFF_BYTE_ORDER_OFFSET,
18
+ TIFF_ID_OFFSET,
19
+ TIFF_IFD_OFFSET_OFFSET,
20
+ TIFF_HEADER_LENGTH,
21
+ TIFF_ID,
22
+ IFD_ENTRY_COUNT_LENGTH,
23
+ IFD_ENTRY_LENGTH,
24
+ NEXT_IFD_POINTER_LENGTH,
25
+ };