exifreader 4.49.0 → 4.51.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.
@@ -2,9 +2,8 @@
2
2
  * License, v. 2.0. If a copy of the MPL was not distributed with this
3
3
  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
4
 
5
- import Constants from './constants.js';
6
5
  import Types from './types.js';
7
- import TagNames, {IFD_TYPE_0TH, IFD_TYPE_1ST, IFD_TYPE_PENTAX} from './tag-names.js';
6
+ import {IFD_TYPE_0TH, IFD_TYPE_1ST, IFD_TYPE_PENTAX} from './ifd-types.js';
8
7
  import {IFD_ENTRY_LENGTH, TIFF_HEADER_LENGTH, TIFF_IFD_OFFSET_OFFSET} from './tiff-constants.js';
9
8
  import {NOOP_TAG_FILTER} from './tag-filter.js';
10
9
  import {decodeUtf8ByteString, getByteString} from './utils.js';
@@ -86,7 +85,7 @@ export function get0thIfdOffset(dataView, tiffHeaderOffset, byteOrder) {
86
85
 
87
86
  /**
88
87
  * Reads the tags of an IFD, and for a 0th IFD also those of the thumbnail IFD
89
- * it points to.
88
+ * it points to when readThumbnailIfd is set.
90
89
  *
91
90
  * @param {{remaining: number, ifdEntriesRemaining: number, decompressedAllowanceRemaining: number}} [valueBudget] -
92
91
  * Caps the total size of the decoded tag values and the number of IFD entries
@@ -95,6 +94,13 @@ export function get0thIfdOffset(dataView, tiffHeaderOffset, byteOrder) {
95
94
  * out, later entries and IFDs are not read, so their tags are absent rather than
96
95
  * empty, and an IFD cut short does not follow its next-IFD offset. Nor does one
97
96
  * whose next-IFD offset points into the TIFF header.
97
+ * @param {Object} tagNames - Tag dictionaries keyed by IFD type, then by tag code,
98
+ * passed in so that a caller composing its own does not bundle every dictionary.
99
+ * Every IFD type read needs a dictionary, `{}` if empty; a missing one throws.
100
+ * @param {boolean} [readThumbnailIfd] - Whether a 0th IFD follows its next-IFD
101
+ * offset into the thumbnail IFD, whose entries are otherwise not charged to the
102
+ * value budget. Defaults to whether tagNames has an IFD_TYPE_1ST dictionary and
103
+ * tagFilter parses the thumbnail group.
98
104
  * @returns {Object} The read tags, keyed by tag name.
99
105
  */
100
106
  export function readIfd(
@@ -107,7 +113,9 @@ export function readIfd(
107
113
  computed = false,
108
114
  tagFilter = NOOP_TAG_FILTER,
109
115
  groupKey = 'exif',
110
- valueBudget = getValueBudget(dataView)
116
+ valueBudget = getValueBudget(dataView),
117
+ tagNames,
118
+ readThumbnailIfd = tagNames[IFD_TYPE_1ST] !== undefined && tagFilter.shouldParseGroup('thumbnail')
111
119
  ) {
112
120
  const FIELD_COUNT_SIZE = Types.getTypeSize('SHORT');
113
121
  const FIELD_SIZE = IFD_ENTRY_LENGTH;
@@ -136,7 +144,8 @@ export function readIfd(
136
144
  includeUnknown,
137
145
  tagFilter,
138
146
  groupKey,
139
- valueBudget
147
+ valueBudget,
148
+ tagNames
140
149
  );
141
150
  if (tag !== undefined) {
142
151
  tags[tag.name] = {
@@ -155,23 +164,22 @@ export function readIfd(
155
164
  offset += FIELD_SIZE;
156
165
  }
157
166
 
158
- if (Constants.USE_THUMBNAIL && !ranOutOfIfdEntries && (offset + Types.getTypeSize('LONG') <= dataView.byteLength)) {
167
+ if (readThumbnailIfd && !ranOutOfIfdEntries && (offset + Types.getTypeSize('LONG') <= dataView.byteLength)) {
159
168
  const nextIfdOffset = Types.getLongAt(dataView, offset, byteOrder);
160
169
  if (nextIfdOffset >= TIFF_HEADER_LENGTH && ifdType === IFD_TYPE_0TH) {
161
- if (tagFilter.shouldParseGroup('thumbnail')) {
162
- tags['Thumbnail'] = readIfd(
163
- dataView,
164
- IFD_TYPE_1ST,
165
- offsetOrigin,
166
- offsetOrigin + nextIfdOffset,
167
- byteOrder,
168
- includeUnknown,
169
- computed,
170
- tagFilter,
171
- 'thumbnail',
172
- valueBudget
173
- );
174
- }
170
+ tags['Thumbnail'] = readIfd(
171
+ dataView,
172
+ IFD_TYPE_1ST,
173
+ offsetOrigin,
174
+ offsetOrigin + nextIfdOffset,
175
+ byteOrder,
176
+ includeUnknown,
177
+ computed,
178
+ tagFilter,
179
+ 'thumbnail',
180
+ valueBudget,
181
+ tagNames
182
+ );
175
183
  }
176
184
  }
177
185
 
@@ -293,7 +301,8 @@ function readTag(
293
301
  includeUnknown = false,
294
302
  tagFilter = NOOP_TAG_FILTER,
295
303
  groupKey = 'exif',
296
- valueBudget
304
+ valueBudget,
305
+ tagNames
297
306
  ) {
298
307
  const TAG_CODE_IPTC_NAA = 0x83bb;
299
308
  const TAG_TYPE_OFFSET = Types.getTypeSize('SHORT');
@@ -307,11 +316,11 @@ function readTag(
307
316
  let tagValueOffset;
308
317
  let stringBudget;
309
318
 
310
- if (Types.typeSizes[tagType] === undefined || (!includeUnknown && TagNames[ifdType][tagCode] === undefined)) {
319
+ if (Types.typeSizes[tagType] === undefined || (!includeUnknown && tagNames[ifdType][tagCode] === undefined)) {
311
320
  return undefined;
312
321
  }
313
322
 
314
- const tagName = getTagName(ifdType, tagCode);
323
+ const tagName = getTagName(ifdType, tagCode, tagNames);
315
324
  if (!tagFilter.shouldParseTag(groupKey, tagName, tagCode)) {
316
325
  return undefined;
317
326
  }
@@ -338,10 +347,10 @@ function readTag(
338
347
 
339
348
  let tagDescription = tagValue;
340
349
 
341
- if (TagNames[ifdType][tagCode] !== undefined) {
342
- if ((TagNames[ifdType][tagCode]['name'] !== undefined) && (TagNames[ifdType][tagCode]['description'] !== undefined)) {
350
+ if (tagNames[ifdType][tagCode] !== undefined) {
351
+ if ((tagNames[ifdType][tagCode]['name'] !== undefined) && (tagNames[ifdType][tagCode]['description'] !== undefined)) {
343
352
  try {
344
- tagDescription = TagNames[ifdType][tagCode]['description'](tagValue);
353
+ tagDescription = tagNames[ifdType][tagCode]['description'](tagValue);
345
354
  } catch (error) {
346
355
  tagDescription = getDescriptionFromTagValue(tagValue);
347
356
  }
@@ -362,13 +371,13 @@ function readTag(
362
371
  };
363
372
  }
364
373
 
365
- function getTagName(ifdType, tagCode) {
366
- if (TagNames[ifdType][tagCode] !== undefined) {
367
- if (typeof TagNames[ifdType][tagCode] === 'string') {
368
- return TagNames[ifdType][tagCode];
374
+ function getTagName(ifdType, tagCode, tagNames) {
375
+ if (tagNames[ifdType][tagCode] !== undefined) {
376
+ if (typeof tagNames[ifdType][tagCode] === 'string') {
377
+ return tagNames[ifdType][tagCode];
369
378
  }
370
- if (TagNames[ifdType][tagCode].name) {
371
- return TagNames[ifdType][tagCode].name;
379
+ if (tagNames[ifdType][tagCode].name) {
380
+ return tagNames[ifdType][tagCode].name;
372
381
  }
373
382
  }
374
383
 
package/src/tags.js CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  import {objectAssign} from './utils.js';
6
6
  import ByteOrder from './byte-order.js';
7
- import {IFD_TYPE_0TH, IFD_TYPE_EXIF, IFD_TYPE_GPS, IFD_TYPE_INTEROPERABILITY} from './tag-names.js';
7
+ import {IFD_TYPE_0TH, IFD_TYPE_EXIF, IFD_TYPE_GPS, IFD_TYPE_INTEROPERABILITY} from './ifd-types.js';
8
8
  import {readIfd, get0thIfdOffset, getValueBudget} from './tags-helpers.js';
9
9
  import {TIFF_HEADER_LENGTH} from './tiff-constants.js';
10
10
 
@@ -28,18 +28,20 @@ function read(
28
28
  includeUnknown,
29
29
  computed = false,
30
30
  tagFilter = undefined,
31
- valueBudget = getValueBudget(dataView)
31
+ valueBudget = getValueBudget(dataView),
32
+ tagNames = undefined,
33
+ readThumbnailIfd = undefined
32
34
  ) {
33
35
  const byteOrder = ByteOrder.getByteOrder(dataView, tiffHeaderOffset);
34
- let tags = read0thIfd(dataView, tiffHeaderOffset, byteOrder, includeUnknown, computed, tagFilter, valueBudget);
36
+ let tags = read0thIfd(dataView, tiffHeaderOffset, byteOrder, includeUnknown, computed, tagFilter, valueBudget, tagNames, readThumbnailIfd);
35
37
  for (let i = 0; i < SUB_IFDS.length; i++) {
36
- tags = readSubIfd(SUB_IFDS[i], tags, dataView, tiffHeaderOffset, byteOrder, includeUnknown, computed, tagFilter, valueBudget);
38
+ tags = readSubIfd(SUB_IFDS[i], tags, dataView, tiffHeaderOffset, byteOrder, includeUnknown, computed, tagFilter, valueBudget, tagNames);
37
39
  }
38
40
 
39
41
  return {tags, byteOrder};
40
42
  }
41
43
 
42
- function read0thIfd(dataView, tiffHeaderOffset, byteOrder, includeUnknown, computed, tagFilter, valueBudget) {
44
+ function read0thIfd(dataView, tiffHeaderOffset, byteOrder, includeUnknown, computed, tagFilter, valueBudget, tagNames, readThumbnailIfd) {
43
45
  const ifdOffset = get0thIfdOffset(dataView, tiffHeaderOffset, byteOrder);
44
46
  if (ifdOffset === undefined) {
45
47
  return {};
@@ -54,11 +56,13 @@ function read0thIfd(dataView, tiffHeaderOffset, byteOrder, includeUnknown, compu
54
56
  computed,
55
57
  tagFilter,
56
58
  'exif',
57
- valueBudget
59
+ valueBudget,
60
+ tagNames,
61
+ readThumbnailIfd
58
62
  );
59
63
  }
60
64
 
61
- function readSubIfd(subIfd, tags, dataView, tiffHeaderOffset, byteOrder, includeUnknown, computed, tagFilter, valueBudget) {
65
+ function readSubIfd(subIfd, tags, dataView, tiffHeaderOffset, byteOrder, includeUnknown, computed, tagFilter, valueBudget, tagNames) {
62
66
  const pointerTag = tags[subIfd.pointerKey];
63
67
  if (pointerTag === undefined) {
64
68
  return tags;
@@ -81,7 +85,8 @@ function readSubIfd(subIfd, tags, dataView, tiffHeaderOffset, byteOrder, include
81
85
  computed,
82
86
  tagFilter,
83
87
  'exif',
84
- valueBudget
88
+ valueBudget,
89
+ tagNames
85
90
  )
86
91
  );
87
92
  }
package/src/thumbnail.js CHANGED
@@ -18,7 +18,7 @@ function get(dataView, thumbnailTags, tiffHeaderOffset) {
18
18
 
19
19
  if (fitsInDataView(dataView, offset, length)) {
20
20
  thumbnailTags.type = 'image/jpeg';
21
- const byteOffset = dataView.byteOffset || 0;
21
+ const byteOffset = dataView.byteOffset;
22
22
  thumbnailTags.image = dataView.buffer.slice(byteOffset + offset, byteOffset + offset + length);
23
23
  deferInit(thumbnailTags, 'base64', function () {
24
24
  return getBase64Image(this.image);
package/src/utils.js CHANGED
@@ -2,16 +2,38 @@
2
2
  * License, v. 2.0. If a copy of the MPL was not distributed with this
3
3
  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
4
 
5
- import DataViewWrapper from './dataview.js';
6
-
7
5
  export function getDataView(data, byteOffset, byteLength) {
6
+ return new DataView(data, byteOffset, byteLength);
7
+ }
8
+
9
+ /**
10
+ * Views the bytes of an ArrayBuffer, a SharedArrayBuffer, any typed array (a
11
+ * Node Buffer included) or a DataView, without copying them.
12
+ * @param {ArrayBuffer|SharedArrayBuffer|ArrayBufferView} data
13
+ * @returns {DataView} A view over the same bytes as `data`.
14
+ * @throws {TypeError} If `data` is none of the accepted types.
15
+ */
16
+ export function getDataViewFromBytes(data) {
17
+ if (isArrayBufferViewLike(data)) {
18
+ return new DataView(data.buffer, data.byteOffset, data.byteLength);
19
+ }
8
20
  try {
9
- return new DataView(data, byteOffset, byteLength);
21
+ return new DataView(data);
10
22
  } catch (error) {
11
- return new DataViewWrapper(data, byteOffset, byteLength);
23
+ throw new TypeError('Passed data type is unsupported. Pass an ArrayBuffer, SharedArrayBuffer, DataView, a typed array such as a Uint8Array or Node Buffer, or a File.');
12
24
  }
13
25
  }
14
26
 
27
+ /**
28
+ * Tells a typed array or DataView by its shape: older runtimes lack the native check.
29
+ */
30
+ export function isArrayBufferViewLike(data) {
31
+ return !!data
32
+ && !!data.buffer
33
+ && (typeof data.byteOffset === 'number')
34
+ && (typeof data.byteLength === 'number');
35
+ }
36
+
15
37
  export function getStringFromDataView(dataView, offset, length) {
16
38
  const chars = [];
17
39
  for (let i = 0; i < length && offset + i < dataView.byteLength; i++) {
package/src/xmp-tags.js CHANGED
@@ -109,15 +109,13 @@ function combineChunks(dataView, chunks) {
109
109
  // A chunk length comes verbatim from the image and is not trusted. Bound it to
110
110
  // the bytes actually present so a crafted length cannot force a huge allocation.
111
111
  function getBoundedChunkBytes(dataView, chunk) {
112
- // The bound is the view's own extent, not the buffer's, since the slice
113
- // starts at the view's byteOffset.
114
- // dataView can be a DataViewWrapper whose buffer is a Buffer, which a
115
- // Uint8Array view constructor would copy whole, so the bytes come from slice.
112
+ // The bound is the view's own extent, not the buffer's, since the bytes
113
+ // start at the view's byteOffset.
116
114
  const bufferLength = dataView.byteLength;
117
- const byteOffset = dataView.byteOffset || 0;
115
+ const byteOffset = dataView.byteOffset;
118
116
  const start = Math.min(Math.max(chunk.dataOffset, 0), bufferLength);
119
117
  const end = Math.min(start + Math.max(chunk.length, 0), bufferLength);
120
- return new Uint8Array(dataView.buffer.slice(byteOffset + start, byteOffset + end));
118
+ return new Uint8Array(dataView.buffer, byteOffset + start, end - start);
121
119
  }
122
120
 
123
121
  function readTags(tags, chunkDataView, domParser) {
package/webpack.config.js CHANGED
@@ -83,7 +83,7 @@ module.exports = {
83
83
  }
84
84
  },
85
85
  {
86
- test: /[/\\](exif-reader|image-header-?(tiff|jpeg|png|heic|avif|jxl|iso-bmff|webp|gif)?|tags|tag-names|tags-helpers|png-text-tags|loadview-pipeline)\.js$/,
86
+ test: /[/\\](exif-reader|image-header|loadview-pipeline)\.js$/,
87
87
  loader: 'string-replace-loader',
88
88
  options: {
89
89
  multiple: getConstantReplacements(includedModules)
package/src/dataview.js DELETED
@@ -1,51 +0,0 @@
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 default class DataView {
6
- constructor(buffer) {
7
- if (bufferTypeIsUnsupported(buffer)) {
8
- throw new Error('DataView: Passed buffer type is unsupported.');
9
- }
10
-
11
- this.buffer = buffer;
12
- this.byteLength = this.buffer.length;
13
- }
14
-
15
- getUint8(offset) {
16
- return this.buffer.readUInt8(offset);
17
- }
18
-
19
- getUint16(offset, littleEndian) {
20
- if (littleEndian) {
21
- return this.buffer.readUInt16LE(offset);
22
- }
23
- return this.buffer.readUInt16BE(offset);
24
- }
25
-
26
- getUint32(offset, littleEndian) {
27
- if (littleEndian) {
28
- return this.buffer.readUInt32LE(offset);
29
- }
30
- return this.buffer.readUInt32BE(offset);
31
- }
32
-
33
- getInt32(offset, littleEndian) {
34
- if (littleEndian) {
35
- return this.buffer.readInt32LE(offset);
36
- }
37
- return this.buffer.readInt32BE(offset);
38
- }
39
- }
40
-
41
- function bufferTypeIsUnsupported(buffer) {
42
- return typeof buffer !== 'object'
43
- || buffer.length === undefined
44
- || buffer.readUInt8 === undefined
45
- || buffer.readUInt16LE === undefined
46
- || buffer.readUInt16BE === undefined
47
- || buffer.readUInt32LE === undefined
48
- || buffer.readUInt32BE === undefined
49
- || buffer.readInt32LE === undefined
50
- || buffer.readInt32BE === undefined;
51
- }