exifreader 4.39.1 → 4.40.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.
@@ -2,6 +2,7 @@ import Constants from './constants.js';
2
2
  import {getNullTerminatedStringFromDataView, getStringFromDataView} from './utils.js';
3
3
  // import {get64BitValue} from './image-header-iso-bmff-utils.js';
4
4
  import {parseItemLocationBox} from './image-header-iso-bmff-iloc.js';
5
+ import {hasBytes} from './image-header-iso-bmff-utils.js';
5
6
 
6
7
  // HEIC and AVIF files are based on the ISO-BMFF format. This file format is
7
8
  // built up by boxes. There are boxes and full boxes. All box types have a
@@ -22,6 +23,7 @@ const TYPE_FTYP = 0x66747970;
22
23
  const TYPE_IPRP = 0x69707270;
23
24
  const TYPE_META = 0x6d657461;
24
25
  const TYPE_ILOC = 0x696c6f63;
26
+ const TYPE_IDAT = 0x69646174;
25
27
  const TYPE_IINF = 0x69696e66;
26
28
  const TYPE_INFE = 0x696e6665;
27
29
  const TYPE_IPCO = 0x6970636f;
@@ -45,47 +47,64 @@ export function parseBox(dataView, offset) {
45
47
  const BOX_MIN_LENGTH = 8;
46
48
  const VERSION_SIZE = 1;
47
49
 
48
- const {length, contentOffset} = getBoxLength(dataView, offset);
49
- if (length < BOX_MIN_LENGTH) {
50
+ if (!hasBytes(dataView, offset, BOX_MIN_LENGTH)) {
50
51
  return undefined;
51
52
  }
52
53
 
53
- const type = dataView.getUint32(offset + BOX_TYPE_OFFSET);
54
+ // A malformed box can make a deeper read overrun the buffer; treat it as
55
+ // unparseable (return undefined) rather than throw a RangeError to the caller.
56
+ try {
57
+ const {length, contentOffset} = getBoxLength(dataView, offset);
58
+ if (length < BOX_MIN_LENGTH) {
59
+ return undefined;
60
+ }
54
61
 
55
- if (type === TYPE_FTYP) {
56
- return parseFileTypeBox(dataView, contentOffset, length);
57
- }
58
- if (type === TYPE_IPRP) {
59
- return parseItemPropertiesBox(dataView, offset, contentOffset, length);
60
- }
61
- if (type === TYPE_IPCO) {
62
- return parseItemPropertyContainerBox(dataView, offset, contentOffset, length);
63
- }
64
- if (type === TYPE_COLR) {
65
- return parseColorInformationBox(dataView, contentOffset, length);
66
- }
62
+ const type = dataView.getUint32(offset + BOX_TYPE_OFFSET);
67
63
 
68
- // The following are full boxes, also containing version and flags.
69
- const version = dataView.getUint8(contentOffset);
64
+ if (type === TYPE_FTYP) {
65
+ return parseFileTypeBox(dataView, contentOffset, length);
66
+ }
67
+ if (type === TYPE_IPRP) {
68
+ return parseItemPropertiesBox(dataView, offset, contentOffset, length);
69
+ }
70
+ if (type === TYPE_IPCO) {
71
+ return parseItemPropertyContainerBox(dataView, offset, contentOffset, length);
72
+ }
73
+ if (type === TYPE_COLR) {
74
+ return parseColorInformationBox(dataView, contentOffset, length);
75
+ }
76
+ if (type === TYPE_IDAT) {
77
+ // idat is a plain box (no version/flags); its data starts directly
78
+ // after the box header.
79
+ return parseItemDataBox(contentOffset, length);
80
+ }
70
81
 
71
- if (type === TYPE_META) {
72
- return parseMetadataBox(dataView, offset, contentOffset + VERSION_SIZE, length);
73
- }
74
- if (type === TYPE_ILOC) {
75
- return parseItemLocationBox(dataView, version, contentOffset + VERSION_SIZE, length);
76
- }
77
- if (type === TYPE_IINF) {
78
- return parseItemInformationBox(dataView, offset, version, contentOffset + VERSION_SIZE, length);
79
- }
80
- if (type === TYPE_INFE) {
81
- return parseItemInformationEntryBox(dataView, offset, version, contentOffset + VERSION_SIZE, length);
82
- }
82
+ // The following are full boxes, also containing version and flags.
83
+ if (!hasBytes(dataView, contentOffset, VERSION_SIZE)) {
84
+ return undefined;
85
+ }
86
+ const version = dataView.getUint8(contentOffset);
83
87
 
84
- return {
85
- // type: getStringFromDataView(dataView, offset + BOX_TYPE_OFFSET, 4),
86
- type: undefined,
87
- length
88
- };
88
+ if (type === TYPE_META) {
89
+ return parseMetadataBox(dataView, offset, contentOffset + VERSION_SIZE, length);
90
+ }
91
+ if (type === TYPE_ILOC) {
92
+ return parseItemLocationBox(dataView, version, contentOffset + VERSION_SIZE, length);
93
+ }
94
+ if (type === TYPE_IINF) {
95
+ return parseItemInformationBox(dataView, offset, version, contentOffset + VERSION_SIZE, length);
96
+ }
97
+ if (type === TYPE_INFE) {
98
+ return parseItemInformationEntryBox(dataView, offset, version, contentOffset + VERSION_SIZE, length);
99
+ }
100
+
101
+ return {
102
+ type: undefined,
103
+ length
104
+ };
105
+ } catch (error) {
106
+ return undefined;
107
+ }
89
108
  }
90
109
 
91
110
  /**
@@ -104,6 +123,7 @@ export function getBoxLength(dataView, offset) {
104
123
  const BOX_TYPE_SIZE = 4;
105
124
  const BOX_EXTENDED_SIZE = 8;
106
125
  const BOX_EXTENDED_SIZE_LOW_OFFSET = 12;
126
+ const BOX_EXTENDED_FULL_SIZE = BOX_LENGTH_SIZE + BOX_TYPE_SIZE + BOX_EXTENDED_SIZE;
107
127
 
108
128
  const boxLength = dataView.getUint32(offset);
109
129
  if (extendsToEndOfFile(boxLength)) {
@@ -113,6 +133,10 @@ export function getBoxLength(dataView, offset) {
113
133
  };
114
134
  }
115
135
  if (hasExtendedSize(boxLength)) {
136
+ if (!hasBytes(dataView, offset, BOX_EXTENDED_FULL_SIZE)) {
137
+ // Extended size field absent; length 0 makes parseBox treat it as unparseable.
138
+ return {length: 0, contentOffset: offset + BOX_LENGTH_SIZE + BOX_TYPE_SIZE};
139
+ }
116
140
  if (hasEmptyHighBits(dataView, offset)) {
117
141
  // It's a bit tricky to handle 64 bit numbers in JavaScript. Let's
118
142
  // wait until there are real-world examples where it is necessary.
@@ -148,15 +172,25 @@ function hasEmptyHighBits(dataView, offset) {
148
172
  * @property {Array<Object>} xmpChunks
149
173
  * @property {Array<Object>} iccChunks
150
174
  * @property {boolean} hasAppMarkers
175
+ * @property {DataView=} exifDataView Synthetic buffer when an Exif item
176
+ * spans multiple extents. tiffHeaderOffset then indexes into this view
177
+ * rather than the source dataView.
178
+ * @property {DataView=} xmpDataView Synthetic buffer when an XMP item
179
+ * spans multiple extents. xmpChunks then references this view.
151
180
  */
152
181
 
153
182
  /**
154
- * Finds the offsets of ISO-BMFF-structued data in the provided data view.
183
+ * Finds the offsets of ISO-BMFF-structured data in the provided data view.
155
184
  *
156
- * @param {DataView} dataView - The data view to find offsets in.
157
- * @returns {Offsets} An object containing the offsets of the TIFF header, XMP chunks, ICC chunks, and a boolean indicating if any of these exist.
185
+ * @param {DataView} dataView The data view to find offsets in.
186
+ * @param {Array<Object>=} metadataBlocks Optional out-array. When provided,
187
+ * receives one block per iloc extent (Exif/XMP) at the resolved file
188
+ * offset (cm 0 direct, cm 1 via the idat box) plus the ICC profile
189
+ * bytes. cm 2 (item_offset) items are skipped.
190
+ * @returns {Offsets} The TIFF header offset, XMP chunks, ICC chunks, and
191
+ * whether any were found.
158
192
  */
159
- export function findOffsets(dataView) {
193
+ export function findOffsets(dataView, metadataBlocks) {
160
194
  if (Constants.USE_EXIF || Constants.USE_XMP || Constants.USE_ICC) {
161
195
  const offsets = {};
162
196
  const metaBox = findMetaBox(dataView);
@@ -166,13 +200,25 @@ export function findOffsets(dataView) {
166
200
  }
167
201
 
168
202
  if (Constants.USE_EXIF) {
169
- offsets.tiffHeaderOffset = findExifOffset(dataView, metaBox);
203
+ const exif = findExifOffset(dataView, metaBox, metadataBlocks);
204
+ if (exif !== undefined) {
205
+ offsets.tiffHeaderOffset = exif.tiffHeaderOffset;
206
+ if (exif.dataView !== undefined) {
207
+ offsets.exifDataView = exif.dataView;
208
+ }
209
+ }
170
210
  }
171
211
  if (Constants.USE_XMP) {
172
- offsets.xmpChunks = findXmpChunks(metaBox);
212
+ const xmp = findXmpChunks(dataView, metaBox, metadataBlocks);
213
+ if (xmp !== undefined) {
214
+ offsets.xmpChunks = xmp.chunks;
215
+ if (xmp.dataView !== undefined) {
216
+ offsets.xmpDataView = xmp.dataView;
217
+ }
218
+ }
173
219
  }
174
220
  if (Constants.USE_ICC) {
175
- offsets.iccChunks = findIccChunks(metaBox);
221
+ offsets.iccChunks = findIccChunks(metaBox, metadataBlocks);
176
222
  }
177
223
  offsets.hasAppMarkers = (offsets.tiffHeaderOffset !== undefined) || (offsets.xmpChunks !== undefined) || (offsets.iccChunks !== undefined);
178
224
  return offsets;
@@ -204,17 +250,111 @@ function findMetaBox(dataView) {
204
250
  return undefined;
205
251
  }
206
252
 
207
- function findExifOffset(dataView, metaBox) {
253
+ function findExifOffset(dataView, metaBox, metadataBlocks) {
208
254
  try {
209
255
  const exifItemId = findIinfExifItemId(metaBox).itemId;
210
256
  const ilocItem = findIlocItem(metaBox, exifItemId);
211
- const exifOffset = ilocItem.baseOffset + ilocItem.extents[0].extentOffset;
212
- return getTiffHeaderOffset(dataView, exifOffset);
257
+ warnIfUnsupportedConstructionMethod(ilocItem);
258
+ const idatContentOffset = findIdatContentOffset(metaBox);
259
+ pushIlocExtentBlocks(metadataBlocks, ilocItem, 'exif', idatContentOffset);
260
+ if (ilocItem.extents.length > 1) {
261
+ const assembled = assembleExtents(dataView, ilocItem, idatContentOffset);
262
+ if (assembled === undefined) {
263
+ return undefined;
264
+ }
265
+ return {
266
+ tiffHeaderOffset: getTiffHeaderOffset(assembled, 0),
267
+ dataView: assembled,
268
+ };
269
+ }
270
+ const exifOffset = resolveExtentToFileOffset(ilocItem, ilocItem.extents[0], idatContentOffset);
271
+ if (exifOffset === undefined) {
272
+ return undefined;
273
+ }
274
+ return {tiffHeaderOffset: getTiffHeaderOffset(dataView, exifOffset)};
213
275
  } catch (error) {
214
276
  return undefined;
215
277
  }
216
278
  }
217
279
 
280
+ function pushIlocExtentBlocks(metadataBlocks, ilocItem, blockType, idatContentOffset) {
281
+ if (!metadataBlocks) {
282
+ return;
283
+ }
284
+ for (let i = 0; i < ilocItem.extents.length; i++) {
285
+ const extent = ilocItem.extents[i];
286
+ const start = resolveExtentToFileOffset(ilocItem, extent, idatContentOffset);
287
+ if (start === undefined) {
288
+ continue;
289
+ }
290
+ metadataBlocks.push({
291
+ type: blockType,
292
+ start,
293
+ end: start + extent.extentLength,
294
+ });
295
+ }
296
+ }
297
+
298
+ // Translates an iloc extent to a real file offset.
299
+ // cm 0 / undefined: baseOffset + extentOffset is already a file offset.
300
+ // cm 1 (idat): bytes live inside the meta box's idat sub-box.
301
+ // cm 2 (item): not yet supported; returns undefined so the item is skipped.
302
+ function resolveExtentToFileOffset(ilocItem, extent, idatContentOffset) {
303
+ const cm = ilocItem.constructionMethod;
304
+ if (cm === undefined || cm === 0) {
305
+ return ilocItem.baseOffset + extent.extentOffset;
306
+ }
307
+ if (cm === 1 && idatContentOffset !== undefined) {
308
+ return idatContentOffset + ilocItem.baseOffset + extent.extentOffset;
309
+ }
310
+ return undefined;
311
+ }
312
+
313
+ function findIdatContentOffset(metaBox) {
314
+ const idatBox = metaBox.subBoxes.find((box) => box.type === 'idat');
315
+ return idatBox ? idatBox.contentOffset : undefined;
316
+ }
317
+
318
+ function warnIfUnsupportedConstructionMethod(ilocItem) {
319
+ if (ilocItem.constructionMethod === 2) {
320
+ // eslint-disable-next-line no-console
321
+ console.warn('This file uses iloc constructionMethod 2 (item_offset) which is currently not supported by ExifReader. Contact the maintainer to get it fixed.');
322
+ }
323
+ }
324
+
325
+ // Copies the bytes of every iloc extent into a single contiguous buffer.
326
+ // Used when an item spans more than one extent, the parsers downstream
327
+ // require contiguous bytes. Returns undefined if any extent fails to
328
+ // resolve or falls outside the source dataView.
329
+ function assembleExtents(dataView, ilocItem, idatContentOffset) {
330
+ const resolved = [];
331
+ let totalLength = 0;
332
+ for (let i = 0; i < ilocItem.extents.length; i++) {
333
+ const extent = ilocItem.extents[i];
334
+ const start = resolveExtentToFileOffset(ilocItem, extent, idatContentOffset);
335
+ if (start === undefined || start + extent.extentLength > dataView.byteLength) {
336
+ return undefined;
337
+ }
338
+ resolved.push({start, length: extent.extentLength});
339
+ totalLength += extent.extentLength;
340
+ // A reassembled item can never legitimately be larger than the source
341
+ // file. Overlapping extents could otherwise amplify the allocation.
342
+ if (totalLength > dataView.byteLength) {
343
+ return undefined;
344
+ }
345
+ }
346
+ const buffer = new Uint8Array(totalLength);
347
+ let writeOffset = 0;
348
+ for (let i = 0; i < resolved.length; i++) {
349
+ const {start, length} = resolved[i];
350
+ for (let j = 0; j < length; j++) {
351
+ buffer[writeOffset + j] = dataView.getUint8(start + j);
352
+ }
353
+ writeOffset += length;
354
+ }
355
+ return new DataView(buffer.buffer);
356
+ }
357
+
218
358
  function findIinfExifItemId(metaBox) {
219
359
  return metaBox.subBoxes.find((box) => box.type === 'iinf').itemInfos.find((itemInfo) => itemInfo.itemType === ITEM_INFO_TYPE_EXIF);
220
360
  }
@@ -230,17 +370,36 @@ export function getTiffHeaderOffset(dataView, exifOffset) {
230
370
  return exifOffset + TIFF_HEADER_OFFSET_SIZE + dataView.getUint32(exifOffset);
231
371
  }
232
372
 
233
- function findXmpChunks(metaBox) {
373
+ function findXmpChunks(dataView, metaBox, metadataBlocks) {
234
374
  try {
235
375
  const xmpItemId = findIinfXmpItemId(metaBox).itemId;
236
376
  const ilocItem = findIlocItem(metaBox, xmpItemId);
237
- const ilocItemExtent = findIlocItem(metaBox, xmpItemId).extents[0];
238
- return [
239
- {
240
- dataOffset: ilocItem.baseOffset + ilocItemExtent.extentOffset,
241
- length: ilocItemExtent.extentLength,
377
+ warnIfUnsupportedConstructionMethod(ilocItem);
378
+ const idatContentOffset = findIdatContentOffset(metaBox);
379
+ pushIlocExtentBlocks(metadataBlocks, ilocItem, 'xmp', idatContentOffset);
380
+ if (ilocItem.extents.length > 1) {
381
+ const assembled = assembleExtents(dataView, ilocItem, idatContentOffset);
382
+ if (assembled === undefined) {
383
+ return undefined;
242
384
  }
243
- ];
385
+ return {
386
+ chunks: [{dataOffset: 0, length: assembled.byteLength}],
387
+ dataView: assembled,
388
+ };
389
+ }
390
+ const ilocItemExtent = ilocItem.extents[0];
391
+ const dataOffset = resolveExtentToFileOffset(ilocItem, ilocItemExtent, idatContentOffset);
392
+ if (dataOffset === undefined) {
393
+ return undefined;
394
+ }
395
+ return {
396
+ chunks: [
397
+ {
398
+ dataOffset,
399
+ length: ilocItemExtent.extentLength,
400
+ }
401
+ ],
402
+ };
244
403
  } catch (error) {
245
404
  return undefined;
246
405
  }
@@ -251,7 +410,7 @@ function findIinfXmpItemId(metaBox) {
251
410
  .itemInfos.find((itemInfo) => itemInfo.itemType === ITEM_INFO_TYPE_MIME && itemInfo.contentType === 'application/rdf+xml');
252
411
  }
253
412
 
254
- function findIccChunks(metaBox) {
413
+ function findIccChunks(metaBox, metadataBlocks) {
255
414
  // This finds the first ICC chunk, but there could be one for each image
256
415
  // that is embedded in the file. If it turns out we need to match the ICC
257
416
  // chunk to a specific image, we need to check the "ipma" in addition to the
@@ -263,6 +422,13 @@ function findIccChunks(metaBox) {
263
422
  .properties.find((box) => box.type === 'colr')
264
423
  .icc;
265
424
  if (icc) {
425
+ if (metadataBlocks) {
426
+ metadataBlocks.push({
427
+ type: 'icc',
428
+ start: icc.offset,
429
+ end: icc.offset + icc.length,
430
+ });
431
+ }
266
432
  return [icc];
267
433
  }
268
434
  } catch (error) {
@@ -335,6 +501,14 @@ function parseMetadataBox(dataView, startOffset, contentOffset, length) {
335
501
  };
336
502
  }
337
503
 
504
+ function parseItemDataBox(contentOffset, length) {
505
+ return {
506
+ type: 'idat',
507
+ contentOffset,
508
+ length,
509
+ };
510
+ }
511
+
338
512
  /**
339
513
  * @param {DataView} dataView
340
514
  * @param {number} offset The offset to start parsing from.
@@ -441,7 +615,6 @@ function parseItemInformationEntryBox(dataView, startOffset, version, contentOff
441
615
  }
442
616
  entry.itemProtectionIndex = dataView.getUint16(contentOffset);
443
617
  contentOffset += 2;
444
- // entry.itemTypeAscii = getStringFromDataView(dataView, offset, 4); // For testing.
445
618
  entry.itemType = dataView.getUint32(contentOffset);
446
619
  contentOffset += 4;
447
620
  entry.itemName = getNullTerminatedStringFromDataView(dataView, contentOffset);
@@ -65,11 +65,27 @@ const APP1_XMP_EXTENDED_IDENTIFIER = 'http://ns.adobe.com/xmp/extension/\x00';
65
65
  const APP13_IPTC_IDENTIFIER = 'Photoshop 3.0';
66
66
  const APP13_RESOURCE_BLOCK_IDENTIFIER = '8BIM';
67
67
 
68
+ /**
69
+ * Checks if the provided data view represents a JPEG file.
70
+ *
71
+ * @param {DataView} dataView The data view to check.
72
+ * @returns {boolean} True if the data view starts with the JPEG SOI marker.
73
+ */
68
74
  function isJpegFile(dataView) {
69
75
  return !!dataView && (dataView.byteLength >= MIN_JPEG_DATA_BUFFER_LENGTH) && (dataView.getUint16(0) === JPEG_ID);
70
76
  }
71
77
 
72
- function findJpegOffsets(dataView) {
78
+ /**
79
+ * Walks the JPEG APP/SOF markers and reports the offsets ExifReader needs.
80
+ *
81
+ * @param {DataView} dataView The data view to find offsets in.
82
+ * @param {Array<Object>=} metadataBlocks Optional out-array. When provided,
83
+ * receives one block per recognized segment (Exif/XMP/IPTC/ICC/MPF/JFIF
84
+ * and SOF for file tags) and gets `truncated` set to true if the loop
85
+ * never saw the SOS marker.
86
+ * @returns {Object} Per-segment offsets plus `hasAppMarkers`.
87
+ */
88
+ function findJpegOffsets(dataView, metadataBlocks) {
73
89
  let appMarkerPosition = JPEG_ID_SIZE;
74
90
  let fieldLength;
75
91
  let sof0DataOffset;
@@ -85,17 +101,26 @@ function findJpegOffsets(dataView) {
85
101
  let bestExifSegmentTiffHeaderOffset;
86
102
  let bestExifSegmentAppMarkerPosition;
87
103
  let bestExifSegmentFieldLength;
104
+ let sawSos = false;
88
105
 
89
106
  while (appMarkerPosition + APP_ID_OFFSET + 5 <= dataView.byteLength) {
107
+ let blockType;
108
+ if (dataView.getUint16(appMarkerPosition) === SOS_MARKER) {
109
+ // Reaching SOS means the metadata region is closed.
110
+ sawSos = true;
111
+ }
90
112
  if (Constants.USE_FILE && isSOF0Marker(dataView, appMarkerPosition)) {
91
113
  fieldLength = dataView.getUint16(appMarkerPosition + APP_MARKER_SIZE);
92
114
  sof0DataOffset = appMarkerPosition + APP_MARKER_SIZE;
115
+ blockType = 'file';
93
116
  } else if (Constants.USE_FILE && isSOF2Marker(dataView, appMarkerPosition)) {
94
117
  fieldLength = dataView.getUint16(appMarkerPosition + APP_MARKER_SIZE);
95
118
  sof2DataOffset = appMarkerPosition + APP_MARKER_SIZE;
119
+ blockType = 'file';
96
120
  } else if (Constants.USE_JFIF && isApp0JfifMarker(dataView, appMarkerPosition)) {
97
121
  fieldLength = dataView.getUint16(appMarkerPosition + APP_MARKER_SIZE);
98
122
  jfifDataOffset = appMarkerPosition + JFIF_DATA_OFFSET;
123
+ blockType = 'jfif';
99
124
  } else if (Constants.USE_EXIF && isApp1ExifMarker(dataView, appMarkerPosition)) {
100
125
  fieldLength = dataView.getUint16(appMarkerPosition + APP_MARKER_SIZE);
101
126
  exifSegmentCount++;
@@ -142,21 +167,25 @@ function findJpegOffsets(dataView) {
142
167
  }
143
168
 
144
169
  tiffHeaderOffset = bestExifSegmentTiffHeaderOffset;
170
+ blockType = 'exif';
145
171
  } else if (Constants.USE_XMP && isApp1XmpMarker(dataView, appMarkerPosition)) {
146
172
  if (!xmpChunks) {
147
173
  xmpChunks = [];
148
174
  }
149
175
  fieldLength = dataView.getUint16(appMarkerPosition + APP_MARKER_SIZE);
150
176
  xmpChunks.push(getXmpChunkDetails(appMarkerPosition, fieldLength));
177
+ blockType = 'xmp';
151
178
  } else if (Constants.USE_XMP && isApp1ExtendedXmpMarker(dataView, appMarkerPosition)) {
152
179
  if (!xmpChunks) {
153
180
  xmpChunks = [];
154
181
  }
155
182
  fieldLength = dataView.getUint16(appMarkerPosition + APP_MARKER_SIZE);
156
183
  xmpChunks.push(getExtendedXmpChunkDetails(appMarkerPosition, fieldLength));
184
+ blockType = 'xmp';
157
185
  } else if (Constants.USE_IPTC && isApp13PhotoshopMarker(dataView, appMarkerPosition)) {
158
186
  fieldLength = dataView.getUint16(appMarkerPosition + APP_MARKER_SIZE);
159
187
  iptcDataOffset = appMarkerPosition + IPTC_DATA_OFFSET;
188
+ blockType = 'iptc';
160
189
  } else if (Constants.USE_ICC && isApp2ICCMarker(dataView, appMarkerPosition)) {
161
190
  fieldLength = dataView.getUint16(appMarkerPosition + APP_MARKER_SIZE);
162
191
  const iccDataOffset = appMarkerPosition + APP2_ICC_DATA_OFFSET;
@@ -168,9 +197,11 @@ function findJpegOffsets(dataView) {
168
197
  iccChunks = [];
169
198
  }
170
199
  iccChunks.push({offset: iccDataOffset, length: iccDataLength, chunkNumber: iccChunkNumber, chunksTotal: iccChunksTotal});
200
+ blockType = 'icc';
171
201
  } else if (Constants.USE_MPF && isApp2MPFMarker(dataView, appMarkerPosition)) {
172
202
  fieldLength = dataView.getUint16(appMarkerPosition + APP_MARKER_SIZE);
173
203
  mpfDataOffset = appMarkerPosition + MPF_DATA_OFFSET;
204
+ blockType = 'mpf';
174
205
  } else if (isAppMarker(dataView, appMarkerPosition)) {
175
206
  fieldLength = dataView.getUint16(appMarkerPosition + APP_MARKER_SIZE);
176
207
  } else if (isFillByte(dataView, appMarkerPosition)) {
@@ -179,6 +210,13 @@ function findJpegOffsets(dataView) {
179
210
  } else {
180
211
  break;
181
212
  }
213
+ if (metadataBlocks && blockType) {
214
+ metadataBlocks.push({
215
+ type: blockType,
216
+ start: appMarkerPosition,
217
+ end: appMarkerPosition + APP_MARKER_SIZE + fieldLength,
218
+ });
219
+ }
182
220
  appMarkerPosition += APP_MARKER_SIZE + fieldLength;
183
221
  }
184
222
 
@@ -186,6 +224,20 @@ function findJpegOffsets(dataView) {
186
224
  warnAboutMultipleExifSegments(exifSegmentCount);
187
225
  }
188
226
 
227
+ if (metadataBlocks) {
228
+ // The while-condition requires +5 bytes to enter, so the SOS marker
229
+ // can sit right at the boundary where the loop exits without
230
+ // entering. Catch that here.
231
+ if (!sawSos
232
+ && appMarkerPosition + APP_MARKER_SIZE <= dataView.byteLength
233
+ && dataView.getUint16(appMarkerPosition) === SOS_MARKER) {
234
+ sawSos = true;
235
+ }
236
+ // The pipeline combines this with the per-block end <= byteLength
237
+ // check to compute the overall `complete` flag.
238
+ metadataBlocks.truncated = !sawSos;
239
+ }
240
+
189
241
  return {
190
242
  hasAppMarkers: appMarkerPosition > JPEG_ID_SIZE,
191
243
  fileDataOffset: sof0DataOffset || sof2DataOffset,
@@ -27,6 +27,14 @@ const TYPE_JXLP = 0x6a786c70;
27
27
  const JXLP_SEQUENCE_NUMBER_SIZE = 4;
28
28
  const BROB_ORIGINAL_TYPE_SIZE = 4;
29
29
 
30
+ /**
31
+ * Checks if the provided data view represents a JPEG XL file (container or
32
+ * bare codestream).
33
+ *
34
+ * @param {DataView} dataView The data view to check.
35
+ * @returns {boolean} True for either a JXL container signature or a bare
36
+ * codestream signature.
37
+ */
30
38
  function isJxlFile(dataView) {
31
39
  try {
32
40
  return isJxlContainer(dataView) || isJxlCodestream(dataView);
@@ -58,7 +66,17 @@ function isJxlCodestream(dataView) {
58
66
  && dataView.getUint8(1) === JXL_CODESTREAM_SIGNATURE[1];
59
67
  }
60
68
 
61
- function findJxlOffsets(dataView) {
69
+ /**
70
+ * Walks the JPEG XL box list and reports the offsets ExifReader needs.
71
+ *
72
+ * @param {DataView} dataView The data view to find offsets in.
73
+ * @param {Array<Object>=} metadataBlocks Optional out-array. When provided,
74
+ * receives one block per recognized box (Exif, xml, brob with Exif or
75
+ * xml original type). The codestream box (`jxlc`/`jxlp`) does not emit
76
+ * a block because it wraps the encoded image, not metadata.
77
+ * @returns {Object} hasAppMarkers and the per-section offsets.
78
+ */
79
+ function findJxlOffsets(dataView, metadataBlocks) {
62
80
  let offset = 0;
63
81
  let tiffHeaderOffset;
64
82
  let xmpChunks;
@@ -67,7 +85,16 @@ function findJxlOffsets(dataView) {
67
85
  let jxlCodestreamOffset;
68
86
 
69
87
  if (isJxlCodestream(dataView)) {
70
- jxlCodestreamOffset = 0;
88
+ // A bare codestream has no box structure, so the box walk below
89
+ // would only ever misinterpret codestream bytes as box headers.
90
+ return {
91
+ hasAppMarkers: true,
92
+ tiffHeaderOffset: undefined,
93
+ xmpChunks: undefined,
94
+ brobExifChunk: undefined,
95
+ brobXmpChunk: undefined,
96
+ jxlCodestreamOffset: 0,
97
+ };
71
98
  }
72
99
 
73
100
  while (offset + BOX_HEADER_MIN_SIZE <= dataView.byteLength) {
@@ -77,6 +104,7 @@ function findJxlOffsets(dataView) {
77
104
  }
78
105
 
79
106
  const boxType = dataView.getUint32(offset + BOX_TYPE_OFFSET);
107
+ let blockType;
80
108
 
81
109
  if (Constants.USE_EXIF && boxType === TYPE_EXIF) {
82
110
  try {
@@ -84,6 +112,7 @@ function findJxlOffsets(dataView) {
84
112
  } catch (_error) {
85
113
  // Truncated Exif data.
86
114
  }
115
+ blockType = 'exif';
87
116
  }
88
117
 
89
118
  if (Constants.USE_XMP && boxType === TYPE_XML) {
@@ -91,10 +120,13 @@ function findJxlOffsets(dataView) {
91
120
  dataOffset: contentOffset,
92
121
  length: length - (contentOffset - offset)
93
122
  }];
123
+ blockType = 'xmp';
94
124
  }
95
125
 
96
126
  if (boxType === TYPE_JXLC && jxlCodestreamOffset === undefined) {
97
127
  jxlCodestreamOffset = contentOffset;
128
+ // No block emitted: the codestream box wraps the encoded image,
129
+ // marking it would inflate metadataRange.end past pixel data.
98
130
  }
99
131
 
100
132
  if (boxType === TYPE_JXLP && jxlCodestreamOffset === undefined
@@ -102,6 +134,7 @@ function findJxlOffsets(dataView) {
102
134
  const sequenceNumber = dataView.getUint32(contentOffset) & 0x7FFFFFFF;
103
135
  if (sequenceNumber === 0) {
104
136
  jxlCodestreamOffset = contentOffset + JXLP_SEQUENCE_NUMBER_SIZE;
137
+ // Same reason as TYPE_JXLC above.
105
138
  }
106
139
  }
107
140
 
@@ -110,20 +143,38 @@ function findJxlOffsets(dataView) {
110
143
  const compressedDataOffset = contentOffset + BROB_ORIGINAL_TYPE_SIZE;
111
144
  const compressedDataLength = length - (compressedDataOffset - offset);
112
145
 
113
- if (Constants.USE_EXIF && originalType === TYPE_EXIF && tiffHeaderOffset === undefined && !brobExifChunk) {
114
- brobExifChunk = {
115
- dataOffset: compressedDataOffset,
116
- length: compressedDataLength
117
- };
146
+ // Decouple block emission from parsing-source selection. A
147
+ // brob(Exif) box must surface as an `exif` block even if a
148
+ // plain Exif box already won the parsing race. The brob*Chunk
149
+ // variables stay guarded to avoid double-parsing.
150
+ if (Constants.USE_EXIF && originalType === TYPE_EXIF) {
151
+ blockType = 'exif';
152
+ if (tiffHeaderOffset === undefined && !brobExifChunk) {
153
+ brobExifChunk = {
154
+ dataOffset: compressedDataOffset,
155
+ length: compressedDataLength
156
+ };
157
+ }
118
158
  }
119
- if (Constants.USE_XMP && originalType === TYPE_XML && !xmpChunks && !brobXmpChunk) {
120
- brobXmpChunk = {
121
- dataOffset: compressedDataOffset,
122
- length: compressedDataLength
123
- };
159
+ if (Constants.USE_XMP && originalType === TYPE_XML) {
160
+ blockType = 'xmp';
161
+ if (!xmpChunks && !brobXmpChunk) {
162
+ brobXmpChunk = {
163
+ dataOffset: compressedDataOffset,
164
+ length: compressedDataLength
165
+ };
166
+ }
124
167
  }
125
168
  }
126
169
 
170
+ if (metadataBlocks && blockType) {
171
+ metadataBlocks.push({
172
+ type: blockType,
173
+ start: offset,
174
+ end: offset + length,
175
+ });
176
+ }
177
+
127
178
  offset += length;
128
179
  }
129
180