exifreader 4.44.1 → 4.45.1

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.44.1",
3
+ "version": "4.45.1",
4
4
  "description": "Library that parses Exif metadata in images.",
5
5
  "author": "Mattias Wallander <mattias@wallander.eu>",
6
6
  "repository": {
@@ -32,24 +32,24 @@
32
32
  "@babel/preset-env": "^7.29.7",
33
33
  "@cypress/webpack-preprocessor": "^8.0.0",
34
34
  "@eslint/js": "^9.39.5",
35
- "@types/node": "^26.4.0",
35
+ "@types/node": "^26.5.1",
36
36
  "babel-loader": "^10.0.0",
37
37
  "c8": "^12.0.0",
38
38
  "chai": "^6.2.2",
39
39
  "cross-env": "^10.0.0",
40
- "cypress": "^15.21.1",
40
+ "cypress": "^16.0.0",
41
41
  "eslint": "^9.39.4",
42
42
  "eslint-plugin-cypress": "^6.4.4",
43
43
  "globals": "^17.12.0",
44
44
  "husky": "^9.0.11",
45
45
  "linkedom": "^0.18.13",
46
- "mocha": "^12.0.0",
46
+ "mocha": "^12.0.1",
47
47
  "npm-run-all2": "^9.0.3",
48
48
  "rimraf": "^5.0.5",
49
49
  "string-replace-loader": "^3.1.0",
50
50
  "terser-webpack-plugin": "^5.6.1",
51
51
  "typescript": "^7.0.2",
52
- "webpack": "5.110.2",
52
+ "webpack": "5.111.0",
53
53
  "webpack-cli": "^7.2.3",
54
54
  "webpack-dev-server": "^5.2.6"
55
55
  },
@@ -92,6 +92,8 @@ export function loadView(
92
92
  decompress: decompressConfig = undefined
93
93
  } = {}
94
94
  ) {
95
+ dataView = getSelfContainedDataView(dataView);
96
+
95
97
  const tagFilter = createTagFilter({includeTags, excludeTags});
96
98
  const parsedGroups = Object.create(null);
97
99
  const mergeSteps = [];
@@ -201,8 +203,7 @@ export function loadView(
201
203
  parsedGroups.exif = parsedExifTags;
202
204
 
203
205
  if (
204
- Constants.USE_TIFF
205
- && Constants.USE_IPTC
206
+ Constants.USE_IPTC
206
207
  && parsedExifTags['IPTC-NAA']
207
208
  && iptcDataOffset === undefined
208
209
  && tagFilter.shouldParseGroup('iptc')
@@ -227,8 +228,7 @@ export function loadView(
227
228
  }
228
229
 
229
230
  if (
230
- Constants.USE_TIFF
231
- && Constants.USE_XMP
231
+ Constants.USE_XMP
232
232
  && parsedExifTags['ApplicationNotes']
233
233
  && Array.isArray(parsedExifTags['ApplicationNotes'].value)
234
234
  && !hasXmpData(xmpChunks)
@@ -282,8 +282,7 @@ export function loadView(
282
282
  }
283
283
 
284
284
  if (
285
- Constants.USE_TIFF
286
- && Constants.USE_ICC
285
+ Constants.USE_ICC
287
286
  && parsedExifTags['ICC_Profile']
288
287
  && !hasIccData(iccChunks)
289
288
  && tagFilter.shouldParseGroup('icc')
@@ -650,7 +649,9 @@ export function loadView(
650
649
  }
651
650
 
652
651
  mergeSteps.push({type: 'gps'});
653
- mergeSteps.push({type: 'composite'});
652
+ if (Constants.USE_EXIF || Constants.USE_XMP) {
653
+ mergeSteps.push({type: 'composite'});
654
+ }
654
655
  mergeSteps.push({type: 'thumbnail'});
655
656
  mergeSteps.push({type: 'fileType'});
656
657
 
@@ -672,7 +673,9 @@ export function loadView(
672
673
  filterTagsForParse,
673
674
  filterTagsForReturn,
674
675
  getGpsGroupFromExifTags,
675
- Composite,
676
+ // A plain property here would keep the import alive for the bundler, so
677
+ // a build with neither Exif nor XMP would still carry composite.js.
678
+ Composite: (Constants.USE_EXIF || Constants.USE_XMP) ? Composite : undefined,
676
679
  Thumbnail,
677
680
  };
678
681
 
@@ -728,6 +731,32 @@ export function loadView(
728
731
  }
729
732
  }
730
733
 
734
+ // A caller-supplied DataView can be a window into a larger buffer. Extractors
735
+ // slice dataView.buffer, so copy the window out to keep them inside the image.
736
+ function getSelfContainedDataView(dataView) {
737
+ if (isWindowIntoLargerBuffer(dataView)) {
738
+ return getDataView(dataView.buffer.slice(dataView.byteOffset, dataView.byteOffset + dataView.byteLength));
739
+ }
740
+ return dataView;
741
+ }
742
+
743
+ function isWindowIntoLargerBuffer(dataView) {
744
+ return isDataViewLike(dataView)
745
+ && ((dataView.byteOffset > 0) || (dataView.byteLength < dataView.buffer.byteLength));
746
+ }
747
+
748
+ // A DataView from another realm, an iframe for example, fails instanceof, so
749
+ // match on the shape instead. A typed array has no getUint8 and the Node Buffer
750
+ // wrapper has no byteOffset, so neither is treated as a window here.
751
+ function isDataViewLike(dataView) {
752
+ return !!dataView
753
+ && (typeof dataView.getUint8 === 'function')
754
+ && (typeof dataView.byteOffset === 'number')
755
+ && (typeof dataView.byteLength === 'number')
756
+ && !!dataView.buffer
757
+ && (typeof dataView.buffer.slice === 'function');
758
+ }
759
+
731
760
  function getBrobDataView(dataView, brobChunk) {
732
761
  // The box length is untrusted and comes through negative for a box shorter
733
762
  // than its header, so bound the view by the bytes present. The JXL header
package/src/icc-tags.js CHANGED
@@ -18,6 +18,9 @@ const TAG_TYPE_SIGNATURE = 'sig ';
18
18
  const TAG_TABLE_SINGLE_TAG_DATA = 12;
19
19
  const MIN_MULTI_LOCALIZED_UNICODE_RECORD_SIZE = 12;
20
20
  const MULTI_LOCALIZED_UNICODE_RECORDS_OFFSET = 16;
21
+ const MAX_MLUC_RECORDS = 1000;
22
+ const MAX_TAG_COUNT = 1000;
23
+ const MAX_DECODE_BYTES = 1024 * 1024;
21
24
 
22
25
  // ICC profile data can be longer than application segment max length of ~64k.
23
26
  // so it can be split into multiple APP2 segments. Each segment includes
@@ -36,7 +39,8 @@ function readCompressedIcc(dataView, iccData, decompressConfig) {
36
39
  if (!compressionMethodIsSupported(iccData[0].compressionMethod)) {
37
40
  return {};
38
41
  }
39
- const compressedDataView = new DataView(dataView.buffer.slice(iccData[0].offset, iccData[0].offset + iccData[0].length));
42
+ const byteOffset = dataView.byteOffset || 0;
43
+ const compressedDataView = new DataView(dataView.buffer.slice(byteOffset + iccData[0].offset, byteOffset + iccData[0].offset + iccData[0].length));
40
44
  return decompress(compressedDataView, iccData[0].compressionMethod, 'utf-8', 'dataview', decompressConfig)
41
45
  .then(parseTags)
42
46
  .catch(() => ({}));
@@ -49,6 +53,7 @@ function compressionMethodIsSupported(compressionMethod) {
49
53
  function readIcc(dataView, iccData) {
50
54
  try {
51
55
  const buffer = getBuffer(dataView);
56
+ const byteOffset = dataView.byteOffset || 0;
52
57
  const declaredLength = iccData.reduce((sum, icc) => sum + icc.length, 0);
53
58
  // The declared chunk lengths come from the file and are not trusted, so
54
59
  // bound the profile by the size of the buffer it is copied out of.
@@ -63,7 +68,7 @@ function readIcc(dataView, iccData) {
63
68
  throw new Error(`ICC chunk ${chunkNumber} not found`);
64
69
  }
65
70
 
66
- const data = buffer.slice(iccDataChunk.offset, iccDataChunk.offset + iccDataChunk.length);
71
+ const data = buffer.slice(byteOffset + iccDataChunk.offset, byteOffset + iccDataChunk.offset + iccDataChunk.length);
67
72
  const chunkData = new Uint8Array(data);
68
73
 
69
74
  iccBinaryData.set(chunkData, offset);
@@ -92,7 +97,6 @@ function doesNotHaveTagData(dataView, tagHeaderOffset) {
92
97
  }
93
98
 
94
99
  export function parseTags(dataView) {
95
- const MAX_MLUC_RECORDS = 1000;
96
100
  const buffer = dataView.buffer;
97
101
 
98
102
  const length = dataView.getUint32();
@@ -132,11 +136,11 @@ export function parseTags(dataView) {
132
136
  return tags;
133
137
  }
134
138
 
135
- const tagCount = dataView.getUint32(128);
139
+ const tagCount = Math.min(dataView.getUint32(ICC_TAG_COUNT_OFFSET), MAX_TAG_COUNT);
136
140
  let tagHeaderOffset = 132;
137
- // Budget for the total mluc text decoded across the whole profile. Caps
138
- // the decoded text at O(profile size); real profiles use a small fraction.
139
- let remainingMlucTextBytes = dataView.byteLength;
141
+ // Budget for everything decoded from tag data (desc, text and mluc) across
142
+ // the whole profile, bounded by its size and a constant far above real text.
143
+ const decodeBudget = {remaining: Math.min(dataView.byteLength, MAX_DECODE_BYTES)};
140
144
 
141
145
  for (let i = 0; i < tagCount; i++) {
142
146
  if (doesNotHaveTagData(dataView, tagHeaderOffset)) {
@@ -160,7 +164,7 @@ export function parseTags(dataView) {
160
164
  return tags;
161
165
  }
162
166
 
163
- const val = sliceToString(buffer.slice(tagOffset + 12, tagOffset + tagValueSize + 11));
167
+ const val = readBoundedString(dataView, tagOffset + 12, tagValueSize - 1, decodeBudget);
164
168
  addTag(tags, tagSignature, val);
165
169
  } else if (tagType === TAG_TYPE_MULTI_LOCALIZED_UNICODE_TYPE) {
166
170
  const numRecords = dataView.getUint32(tagOffset + 8);
@@ -177,11 +181,15 @@ export function parseTags(dataView) {
177
181
  if (recordsSize > availableRecordsSize) {
178
182
  return tags;
179
183
  }
184
+ if (recordsSize > decodeBudget.remaining) {
185
+ return tags;
186
+ }
187
+ decodeBudget.remaining -= recordsSize;
180
188
  // Records may legitimately share or overlap their text within a
181
189
  // tag, so clamp each read to the tag bounds and draw from a
182
190
  // profile-wide text budget rather than assuming non-overlapping
183
- // storage. This caps the total decoded text at O(profile size)
184
- // without truncating real profiles.
191
+ // storage. This caps the total decoded text without truncating
192
+ // real profiles.
185
193
  const tagTextEnd = Math.min(tagSize, dataView.byteLength - tagOffset);
186
194
  let offset = tagOffset + MULTI_LOCALIZED_UNICODE_RECORDS_OFFSET;
187
195
  const val = [];
@@ -192,8 +200,8 @@ export function parseTags(dataView) {
192
200
  const textOffset = dataView.getUint32(offset + 8);
193
201
 
194
202
  const availableInTag = Math.max(0, tagTextEnd - textOffset);
195
- const boundedTextLength = Math.min(textLength, availableInTag, remainingMlucTextBytes);
196
- remainingMlucTextBytes -= boundedTextLength;
203
+ const boundedTextLength = Math.min(textLength, availableInTag, decodeBudget.remaining);
204
+ decodeBudget.remaining -= boundedTextLength;
197
205
  const text = getUnicodeStringFromDataView(dataView, tagOffset + textOffset, boundedTextLength);
198
206
  val.push({languageCode, countryCode, text});
199
207
  offset += recordSize;
@@ -208,7 +216,7 @@ export function parseTags(dataView) {
208
216
  addTag(tags, tagSignature, valObj);
209
217
  }
210
218
  } else if (tagType === TAG_TYPE_TEXT) {
211
- const val = sliceToString(buffer.slice(tagOffset + 8, tagOffset + tagSize - 7));
219
+ const val = readBoundedString(dataView, tagOffset + 8, tagSize - 15, decodeBudget);
212
220
  addTag(tags, tagSignature, val);
213
221
  } else if (tagType === TAG_TYPE_SIGNATURE) {
214
222
  const val = sliceToString(buffer.slice(tagOffset + 8, tagOffset + 12));
@@ -220,6 +228,12 @@ export function parseTags(dataView) {
220
228
  return tags;
221
229
  }
222
230
 
231
+ function readBoundedString(dataView, start, requestedLength, budget) {
232
+ const length = Math.max(0, Math.min(requestedLength, dataView.byteLength - start, budget.remaining));
233
+ budget.remaining -= length;
234
+ return sliceToString(dataView.buffer.slice(start, start + length));
235
+ }
236
+
223
237
  function addTag(tags, tagSignature, value) {
224
238
  if (iccTags[tagSignature]) {
225
239
  tags[iccTags[tagSignature].name] = {value, description: value};
@@ -37,6 +37,9 @@ const MPF_DATA_OFFSET = 8;
37
37
  const APP2_ICC_IDENTIFIER = 'ICC_PROFILE\0';
38
38
  const ICC_CHUNK_NUMBER_OFFSET = APP_ID_OFFSET + APP2_ICC_IDENTIFIER.length;
39
39
  const ICC_TOTAL_CHUNKS_OFFSET = ICC_CHUNK_NUMBER_OFFSET + 1;
40
+ // Guards against memory exhaustion from a crafted file. Both chunk fields are a
41
+ // single byte, so a longer list could never be assembled into a profile anyway.
42
+ const MAX_ICC_CHUNKS = 255;
40
43
 
41
44
  const APP2_MPF_IDENTIFIER = 'MPF\0';
42
45
 
@@ -180,15 +183,17 @@ function findJpegOffsets(dataView, metadataBlocks) {
180
183
  blockType = 'iptc';
181
184
  } else if (Constants.USE_ICC && isApp2ICCMarker(dataView, appMarkerPosition)) {
182
185
  fieldLength = dataView.getUint16(appMarkerPosition + APP_MARKER_SIZE);
183
- const iccDataOffset = appMarkerPosition + APP2_ICC_DATA_OFFSET;
184
- const iccDataLength = fieldLength - (APP2_ICC_DATA_OFFSET - APP_MARKER_SIZE);
185
-
186
- const iccChunkNumber = dataView.getUint8(appMarkerPosition + ICC_CHUNK_NUMBER_OFFSET);
187
- const iccChunksTotal = dataView.getUint8(appMarkerPosition + ICC_TOTAL_CHUNKS_OFFSET);
188
186
  if (!iccChunks) {
189
187
  iccChunks = [];
190
188
  }
191
- iccChunks.push({offset: iccDataOffset, length: iccDataLength, chunkNumber: iccChunkNumber, chunksTotal: iccChunksTotal});
189
+ if (iccChunks.length < MAX_ICC_CHUNKS) {
190
+ const iccDataOffset = appMarkerPosition + APP2_ICC_DATA_OFFSET;
191
+ const iccDataLength = fieldLength - (APP2_ICC_DATA_OFFSET - APP_MARKER_SIZE);
192
+
193
+ const iccChunkNumber = dataView.getUint8(appMarkerPosition + ICC_CHUNK_NUMBER_OFFSET);
194
+ const iccChunksTotal = dataView.getUint8(appMarkerPosition + ICC_TOTAL_CHUNKS_OFFSET);
195
+ iccChunks.push({offset: iccDataOffset, length: iccDataLength, chunkNumber: iccChunkNumber, chunksTotal: iccChunksTotal});
196
+ }
192
197
  blockType = 'icc';
193
198
  } else if (Constants.USE_MPF && isApp2MPFMarker(dataView, appMarkerPosition)) {
194
199
  fieldLength = dataView.getUint16(appMarkerPosition + APP_MARKER_SIZE);
package/src/jfif-tags.js CHANGED
@@ -153,7 +153,8 @@ function getThumbnail(dataView, jfifDataOffset, thumbnailLength, length) {
153
153
  return undefined;
154
154
  }
155
155
 
156
- const value = dataView.buffer.slice(jfifDataOffset + OFFSET, jfifDataOffset + OFFSET + thumbnailLength);
156
+ const byteOffset = dataView.byteOffset || 0;
157
+ const value = dataView.buffer.slice(byteOffset + jfifDataOffset + OFFSET, byteOffset + jfifDataOffset + OFFSET + thumbnailLength);
157
158
  return {
158
159
  value,
159
160
  description: '<24-bit RGB pixel data>'
@@ -239,7 +239,7 @@ export function applyMergeStep({
239
239
  return tags;
240
240
  }
241
241
 
242
- if (step.type === 'composite') {
242
+ if ((Constants.USE_EXIF || Constants.USE_XMP) && step.type === 'composite') {
243
243
  if (!tagFilter.shouldReturnGroup('composite')) {
244
244
  return tags;
245
245
  }
@@ -291,8 +291,7 @@ export function applyMergeStep({
291
291
  parsedGroups.thumbnail = parsedThumbnailIfdTags;
292
292
  }
293
293
 
294
- const thumbnail = (Constants.USE_JPEG || Constants.USE_WEBP)
295
- && Constants.USE_EXIF
294
+ const thumbnail = Constants.USE_EXIF
296
295
  && Constants.USE_THUMBNAIL
297
296
  && deps.Thumbnail.get(exifDataView || dataView, parsedThumbnailIfdTags, tiffHeaderOffset);
298
297
  if (thumbnail) {
@@ -414,7 +413,13 @@ export function addPngTextReadTagsToTagsAndGroups({
414
413
  returnedEmbeddedExifTags
415
414
  );
416
415
  } else {
417
- tags = deps.objectAssign({}, tags, returnedEmbeddedExifTags);
416
+ // The thumbnail image is never read from a text chunk, so the
417
+ // thumbnail IFD would land at the top level with no image.
418
+ const returnedEmbeddedExifTagsForFlat =
419
+ deps.objectAssign({}, returnedEmbeddedExifTags);
420
+ delete returnedEmbeddedExifTagsForFlat.Thumbnail;
421
+
422
+ tags = deps.objectAssign({}, tags, returnedEmbeddedExifTagsForFlat);
418
423
  }
419
424
  }
420
425
  }
package/src/mpf-tags.js CHANGED
@@ -55,7 +55,10 @@ function addMpfImages(dataView, dataOffset, tags, byteOrder) {
55
55
  return tags;
56
56
  }
57
57
 
58
- const bufferLength = dataView.buffer.byteLength;
58
+ // The bound is the view's own extent, not the buffer's, since the slice
59
+ // starts at the view's byteOffset.
60
+ const bufferLength = dataView.byteLength;
61
+ const byteOffset = dataView.byteOffset || 0;
59
62
  let remainingImageBytes = bufferLength * MAX_IMAGE_SIZE_PER_BUFFER_SIZE;
60
63
  const images = [];
61
64
  for (let i = 0; i < Math.ceil(tags['MPEntry'].value.length / ENTRY_SIZE); i++) {
@@ -94,7 +97,7 @@ function addMpfImages(dataView, dataOffset, tags, byteOrder) {
94
97
 
95
98
  const start = Math.min(Math.max(imageOffset, 0), bufferLength);
96
99
  const end = Math.min(start + Math.max(imageSize, 0), bufferLength, start + remainingImageBytes);
97
- images[i].image = dataView.buffer.slice(start, end);
100
+ images[i].image = dataView.buffer.slice(byteOffset + start, byteOffset + end);
98
101
  remainingImageBytes -= end - start;
99
102
  deferInit(images[i], 'base64', function () {
100
103
  return getBase64Image(this.image);
@@ -108,6 +108,7 @@ function getNameAndValue(dataView, offset, length, type, async, decompressConfig
108
108
  let valueChars;
109
109
  let parsingState = STATE_KEYWORD;
110
110
  let compressionMethod = COMPRESSION_METHOD_NONE;
111
+ const byteOffset = dataView.byteOffset || 0;
111
112
 
112
113
  for (let i = 0; i < length && offset + i < dataView.byteLength; i++) {
113
114
  if (parsingState === STATE_COMPRESSION) {
@@ -118,7 +119,7 @@ function getNameAndValue(dataView, offset, length, type, async, decompressConfig
118
119
  parsingState = moveToNextState(type, parsingState);
119
120
  continue;
120
121
  } else if (parsingState === STATE_TEXT) {
121
- valueChars = new DataView(dataView.buffer.slice(offset + i, offset + length));
122
+ valueChars = new DataView(dataView.buffer.slice(byteOffset + offset + i, byteOffset + offset + length));
122
123
  break;
123
124
  }
124
125
  const byte = dataView.getUint8(offset + i);
package/src/tag-names.js CHANGED
@@ -12,7 +12,7 @@ import TagNamesMpfIfd from './tag-names-mpf-ifd.js';
12
12
  import TagNamesCanonIfd from './tag-names-canon-ifd.js';
13
13
  import TagNamesPentaxIfd from './tag-names-pentax-ifd.js';
14
14
 
15
- const tagNames0thExifIfds = objectAssign({}, TagNames0thIfd, TagNamesExifIfd);
15
+ const tagNames0thExifIfds = Constants.USE_EXIF ? objectAssign({}, TagNames0thIfd, TagNamesExifIfd) : {};
16
16
 
17
17
  export const IFD_TYPE_0TH = '0th';
18
18
  export const IFD_TYPE_1ST = '1st';
@@ -25,10 +25,10 @@ export const IFD_TYPE_PENTAX = 'pentax';
25
25
 
26
26
  export default {
27
27
  [IFD_TYPE_0TH]: tagNames0thExifIfds,
28
- [IFD_TYPE_1ST]: TagNames0thIfd,
28
+ [IFD_TYPE_1ST]: Constants.USE_EXIF ? TagNames0thIfd : {},
29
29
  [IFD_TYPE_EXIF]: tagNames0thExifIfds,
30
- [IFD_TYPE_GPS]: TagNamesGpsIfd,
31
- [IFD_TYPE_INTEROPERABILITY]: TagNamesInteroperabilityIfd,
30
+ [IFD_TYPE_GPS]: Constants.USE_EXIF ? TagNamesGpsIfd : {},
31
+ [IFD_TYPE_INTEROPERABILITY]: Constants.USE_EXIF ? TagNamesInteroperabilityIfd : {},
32
32
  [IFD_TYPE_MPF]: Constants.USE_MPF ? TagNamesMpfIfd : {},
33
33
  [IFD_TYPE_CANON]: Constants.USE_MAKER_NOTES ? TagNamesCanonIfd : {},
34
34
  [IFD_TYPE_PENTAX]: Constants.USE_MAKER_NOTES ? TagNamesPentaxIfd : {},
package/src/thumbnail.js CHANGED
@@ -13,12 +13,17 @@ export default {
13
13
 
14
14
  function get(dataView, thumbnailTags, tiffHeaderOffset) {
15
15
  if (hasJpegThumbnail(thumbnailTags)) {
16
- thumbnailTags.type = 'image/jpeg';
17
16
  const offset = tiffHeaderOffset + thumbnailTags.JPEGInterchangeFormat.value;
18
- thumbnailTags.image = dataView.buffer.slice(offset, offset + thumbnailTags.JPEGInterchangeFormatLength.value);
19
- deferInit(thumbnailTags, 'base64', function () {
20
- return getBase64Image(this.image);
21
- });
17
+ const length = thumbnailTags.JPEGInterchangeFormatLength.value;
18
+
19
+ if (fitsInDataView(dataView, offset, length)) {
20
+ thumbnailTags.type = 'image/jpeg';
21
+ const byteOffset = dataView.byteOffset || 0;
22
+ thumbnailTags.image = dataView.buffer.slice(byteOffset + offset, byteOffset + offset + length);
23
+ deferInit(thumbnailTags, 'base64', function () {
24
+ return getBase64Image(this.image);
25
+ });
26
+ }
22
27
  }
23
28
 
24
29
  // There is a small possibility of thumbnails in TIFF format but they are
@@ -34,3 +39,17 @@ function hasJpegThumbnail(tags) {
34
39
  && tags.JPEGInterchangeFormat && tags.JPEGInterchangeFormat.value
35
40
  && tags.JPEGInterchangeFormatLength && tags.JPEGInterchangeFormatLength.value;
36
41
  }
42
+
43
+ // The file declares the offset and the length, and slicing clamps a bad range
44
+ // instead of throwing, reading a negative start from the end of the buffer. The
45
+ // bound is the view's own extent, not the buffer's, since the slice starts at
46
+ // the view's byteOffset.
47
+ function fitsInDataView(dataView, offset, length) {
48
+ return isNonNegativeInteger(offset)
49
+ && isNonNegativeInteger(length)
50
+ && (offset + length <= dataView.byteLength);
51
+ }
52
+
53
+ function isNonNegativeInteger(value) {
54
+ return (typeof value === 'number') && (value >= 0) && (value % 1 === 0);
55
+ }
package/src/utils.js CHANGED
@@ -196,13 +196,16 @@ export function decompress(dataView, compressionMethod, encoding, returnType = '
196
196
  return rejectExceedsMax(maxDecompressedSize);
197
197
  }
198
198
  if (returnType === 'dataview') {
199
- if (result instanceof DataView) {
200
- return result;
201
- }
202
199
  if (result instanceof ArrayBuffer) {
203
200
  return new DataView(result);
204
201
  }
205
- return new DataView(result.buffer, result.byteOffset, result.byteLength);
202
+ // A DataView or a pooled Buffer can be a window into a
203
+ // larger buffer, and parsers slice dataView.buffer, so
204
+ // copy a window out but keep a whole buffer as it is.
205
+ if ((result.byteOffset === 0) && (result.byteLength === result.buffer.byteLength)) {
206
+ return new DataView(result.buffer);
207
+ }
208
+ return new DataView(result.buffer.slice(result.byteOffset, result.byteOffset + result.byteLength));
206
209
  }
207
210
  return new TextDecoder(encoding).decode(result);
208
211
  });
package/src/xmp-tags.js CHANGED
@@ -79,10 +79,13 @@ function combineChunks(dataView, chunks) {
79
79
  // A chunk length comes verbatim from the image and is not trusted. Bound it to
80
80
  // the bytes actually present so a crafted length cannot force a huge allocation.
81
81
  function getBoundedChunkBytes(dataView, chunk) {
82
- const bufferLength = dataView.buffer.byteLength;
82
+ // The bound is the view's own extent, not the buffer's, since the slice
83
+ // starts at the view's byteOffset.
84
+ const bufferLength = dataView.byteLength;
85
+ const byteOffset = dataView.byteOffset || 0;
83
86
  const start = Math.min(Math.max(chunk.dataOffset, 0), bufferLength);
84
87
  const end = Math.min(start + Math.max(chunk.length, 0), bufferLength);
85
- return new Uint8Array(dataView.buffer.slice(start, end));
88
+ return new Uint8Array(dataView.buffer.slice(byteOffset + start, byteOffset + end));
86
89
  }
87
90
 
88
91
  function readTags(tags, chunkDataView, domParser) {
package/webpack.config.js CHANGED
@@ -6,6 +6,7 @@ const path = require('path');
6
6
  const TerserPlugin = require('terser-webpack-plugin');
7
7
  const findConfigFromClosestPackageJson = require('./bin/findDependentConfig');
8
8
  const TagFilterPlugin = require('./bin/TagFilterPlugin');
9
+ const parseConfig = require('./bin/parse-config');
9
10
 
10
11
  const config = getConfig();
11
12
  const includedModules = parseConfig(config);
@@ -92,59 +93,6 @@ module.exports = {
92
93
  }
93
94
  };
94
95
 
95
- function parseConfig({include: includesConfig, exclude: excludesConfig}) {
96
- const modules = [
97
- 'file',
98
- 'jfif',
99
- 'png_file',
100
- 'exif',
101
- 'iptc',
102
- 'xmp',
103
- 'icc',
104
- 'photoshop',
105
- 'maker_notes',
106
- 'mpf',
107
- 'thumbnail',
108
- 'tiff',
109
- 'jpeg',
110
- 'png',
111
- 'heic',
112
- 'avif',
113
- 'jxl',
114
- 'webp',
115
- 'gif'
116
- ];
117
-
118
- if (includesConfig) {
119
- const includes = {};
120
- for (const module of modules) {
121
- includes[module] =
122
- (
123
- Object.keys(includesConfig).includes(module)
124
- || ((module === 'exif') && Object.keys(includesConfig).includes('thumbnail'))
125
- || ((module === 'exif') && Object.keys(includesConfig).includes('mpf'))
126
- )
127
- && includesConfig[module];
128
- }
129
- return includes;
130
- }
131
-
132
- if (excludesConfig) {
133
- const includes = {};
134
- for (const module of modules) {
135
- includes[module] =
136
- !(
137
- excludesConfig.includes(module)
138
- || ((module === 'thumbnail') && excludesConfig.includes('exif'))
139
- || ((module === 'mpf') && excludesConfig.includes('exif'))
140
- );
141
- }
142
- return includes;
143
- }
144
-
145
- return false;
146
- }
147
-
148
96
  function getConfig() {
149
97
  const packageJson = getPackageJson();
150
98