exifreader 4.53.0 → 4.54.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,7 +2,7 @@
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 {getStringFromDataView, pushMetadataBlock} from './utils.js';
5
+ import {getStringFromDataView, addBlock, pushRange} from './utils.js';
6
6
 
7
7
  export default {
8
8
  isWebpFile,
@@ -26,74 +26,68 @@ function isWebpFile(dataView) {
26
26
  }
27
27
 
28
28
  /**
29
- * Finds the offsets of metadata chunks in a WebP file.
29
+ * Finds the metadata blocks of a WebP file. The last chunk of each id wins.
30
30
  *
31
- * @param {DataView} dataView The data view to find offsets in.
32
- * @param {Array<Object>=} metadataBlocks Optional out-array. When provided,
33
- * receives one block per recognized chunk (EXIF/XMP/ICCP/VP8X) with
34
- * `start` and `end` covering the full RIFF chunk header and content.
35
- * @returns {Object} hasAppMarkers and the per-section offsets.
31
+ * @param {DataView} dataView The data view to find blocks in.
32
+ * @param {Array<Object>=} ranges Optional out-array. When provided, receives
33
+ * one `{block, start, end}` span per recognized chunk (EXIF/XMP/ICCP/VP8X)
34
+ * with `start` and `end` covering the full RIFF chunk header and content.
35
+ * @returns {Array<Object>} The blocks, in the order their first chunk was met.
36
36
  */
37
- function findOffsets(dataView, metadataBlocks) {
37
+ function findOffsets(dataView, ranges) {
38
38
  const SUB_CHUNK_START_OFFSET = 12;
39
39
  const CHUNK_SIZE_OFFSET = 4;
40
40
  const EXIF_IDENTIFIER = 'Exif\x00\x00';
41
41
  const CHUNK_HEADER_SIZE = 8;
42
42
 
43
+ const blocks = [];
43
44
  let offset = SUB_CHUNK_START_OFFSET;
44
- let hasAppMarkers = false;
45
- let tiffHeaderOffset;
46
- let xmpChunks;
47
- let iccChunks;
48
- let vp8xChunkOffset;
45
+ let exifBlock;
46
+ let xmpBlock;
47
+ let iccBlock;
48
+ let riffBlock;
49
49
 
50
50
  while (offset + CHUNK_HEADER_SIZE < dataView.byteLength) {
51
51
  const chunkId = getStringFromDataView(dataView, offset, 4);
52
52
  const chunkSize = dataView.getUint32(offset + CHUNK_SIZE_OFFSET, true);
53
- let blockType;
53
+ let block;
54
54
 
55
55
  if (chunkId === 'EXIF') {
56
- hasAppMarkers = true;
56
+ exifBlock = exifBlock || addBlock(blocks, {kind: 'exif'});
57
57
  if (getStringFromDataView(dataView, offset + CHUNK_HEADER_SIZE, EXIF_IDENTIFIER.length) === EXIF_IDENTIFIER) {
58
- tiffHeaderOffset = offset + CHUNK_HEADER_SIZE + EXIF_IDENTIFIER.length;
58
+ exifBlock.tiffHeaderOffset = offset + CHUNK_HEADER_SIZE + EXIF_IDENTIFIER.length;
59
59
  } else {
60
- tiffHeaderOffset = offset + CHUNK_HEADER_SIZE;
60
+ exifBlock.tiffHeaderOffset = offset + CHUNK_HEADER_SIZE;
61
61
  }
62
- blockType = 'exif';
62
+ block = exifBlock;
63
63
  } else if (chunkId === 'XMP ') {
64
- hasAppMarkers = true;
65
- xmpChunks = [{
64
+ xmpBlock = xmpBlock || addBlock(blocks, {kind: 'xmp'});
65
+ xmpBlock.chunks = [{
66
66
  dataOffset: offset + CHUNK_HEADER_SIZE,
67
67
  length: chunkSize
68
68
  }];
69
- blockType = 'xmp';
69
+ block = xmpBlock;
70
70
  } else if (chunkId === 'ICCP') {
71
- hasAppMarkers = true;
72
- iccChunks = [{
71
+ iccBlock = iccBlock || addBlock(blocks, {kind: 'icc'});
72
+ iccBlock.chunks = [{
73
73
  offset: offset + CHUNK_HEADER_SIZE,
74
74
  length: chunkSize,
75
75
  chunkNumber: 1,
76
76
  chunksTotal: 1
77
77
  }];
78
- blockType = 'icc';
78
+ block = iccBlock;
79
79
  } else if (chunkId === 'VP8X') {
80
- hasAppMarkers = true;
81
- vp8xChunkOffset = offset + CHUNK_HEADER_SIZE;
82
- blockType = 'riff';
80
+ riffBlock = riffBlock || addBlock(blocks, {kind: 'riff'});
81
+ riffBlock.dataOffset = offset + CHUNK_HEADER_SIZE;
82
+ block = riffBlock;
83
83
  }
84
84
 
85
85
  const chunkTotalSize = CHUNK_HEADER_SIZE + (chunkSize % 2 === 0 ? chunkSize : chunkSize + 1);
86
86
 
87
- pushMetadataBlock(metadataBlocks, blockType, offset, offset + chunkTotalSize);
87
+ pushRange(ranges, block, offset, offset + chunkTotalSize);
88
88
 
89
89
  offset += chunkTotalSize;
90
90
  }
91
91
 
92
- return {
93
- hasAppMarkers,
94
- tiffHeaderOffset,
95
- xmpChunks,
96
- iccChunks,
97
- vp8xChunkOffset
98
- };
92
+ return blocks;
99
93
  }
@@ -12,7 +12,6 @@ import Jxl from './image-header-jxl.js';
12
12
  import Webp from './image-header-webp.js';
13
13
  import Gif from './image-header-gif.js';
14
14
  import Xml from './xml.js';
15
- import {objectAssign} from './utils.js';
16
15
 
17
16
  export default {
18
17
  parseAppMarkers
@@ -22,62 +21,63 @@ export default {
22
21
  * Detects the image format and delegates to the matching finder.
23
22
  *
24
23
  * @param {DataView} dataView The data view to inspect.
25
- * @param {boolean=} async Whether async parsing is enabled.
26
- * @param {boolean=} includeMetadataBlocks When true, the finder collects a
27
- * block list describing where metadata sits in the file. The result
28
- * gains `metadataBlocks` and `metadataTruncated` fields.
29
- * @returns {Object} Per-format offsets plus `fileType`.
24
+ * @param {boolean=} includeRanges When true, the finder also records where
25
+ * each block's bytes sit in the file. The result then gains `ranges`, a
26
+ * list of `{block, start, end}` in file order, and `truncated`, true when
27
+ * the finder never reached the end of the file's metadata.
28
+ * @returns {{fileType: {value: string, description: string}, blocks: Array<Object>, ranges: (Array<Object>|undefined), truncated: (boolean|undefined)}}
29
+ * The blocks the finder found, each `{kind, ...payload}`.
30
30
  * @throws {Error} When the data view is not a recognized image format.
31
31
  */
32
- function parseAppMarkers(dataView, async, includeMetadataBlocks) {
33
- const metadataBlocks = includeMetadataBlocks ? [] : undefined;
32
+ function parseAppMarkers(dataView, includeRanges) {
33
+ const ranges = includeRanges ? [] : undefined;
34
34
 
35
35
  if (Constants.USE_TIFF && Tiff.isTiffFile(dataView)) {
36
- return finalize(Tiff.findTiffOffsets(), 'tiff', 'TIFF', metadataBlocks);
36
+ return finalize(Tiff.findTiffOffsets(), 'tiff', 'TIFF', ranges);
37
37
  }
38
38
 
39
39
  if (Constants.USE_JPEG && Jpeg.isJpegFile(dataView)) {
40
- return finalize(Jpeg.findJpegOffsets(dataView, metadataBlocks), 'jpeg', 'JPEG', metadataBlocks);
40
+ return finalize(Jpeg.findJpegOffsets(dataView, ranges), 'jpeg', 'JPEG', ranges);
41
41
  }
42
42
 
43
43
  if (Constants.USE_PNG && Png.isPngFile(dataView)) {
44
- return finalize(Png.findPngOffsets(dataView, async, metadataBlocks), 'png', 'PNG', metadataBlocks);
44
+ return finalize(Png.findPngOffsets(dataView, ranges), 'png', 'PNG', ranges);
45
45
  }
46
46
 
47
47
  if (Constants.USE_HEIC && Heic.isHeicFile(dataView)) {
48
- return finalize(Heic.findHeicOffsets(dataView, metadataBlocks), 'heic', 'HEIC', metadataBlocks);
48
+ return finalize(Heic.findHeicOffsets(dataView, ranges), 'heic', 'HEIC', ranges);
49
49
  }
50
50
 
51
51
  if (Constants.USE_AVIF && Avif.isAvifFile(dataView)) {
52
- return finalize(Avif.findAvifOffsets(dataView, metadataBlocks), 'avif', 'AVIF', metadataBlocks);
52
+ return finalize(Avif.findAvifOffsets(dataView, ranges), 'avif', 'AVIF', ranges);
53
53
  }
54
54
 
55
55
  if (Constants.USE_JXL && Jxl.isJxlFile(dataView)) {
56
- return finalize(Jxl.findJxlOffsets(dataView, metadataBlocks), 'jxl', 'JPEG XL', metadataBlocks);
56
+ return finalize(Jxl.findJxlOffsets(dataView, ranges), 'jxl', 'JPEG XL', ranges);
57
57
  }
58
58
 
59
59
  if (Constants.USE_WEBP && Webp.isWebpFile(dataView)) {
60
- return finalize(Webp.findOffsets(dataView, metadataBlocks), 'webp', 'WebP', metadataBlocks);
60
+ return finalize(Webp.findOffsets(dataView, ranges), 'webp', 'WebP', ranges);
61
61
  }
62
62
 
63
63
  if (Constants.USE_GIF && Gif.isGifFile(dataView)) {
64
- return finalize(Gif.findOffsets(dataView, metadataBlocks), 'gif', 'GIF', metadataBlocks);
64
+ return finalize(Gif.findOffsets(dataView, ranges), 'gif', 'GIF', ranges);
65
65
  }
66
66
 
67
67
  if (Constants.USE_XMP && Xml.isXMLFile(dataView)) {
68
- return finalize(Xml.findOffsets(dataView, metadataBlocks), 'xml', 'XML', metadataBlocks);
68
+ return finalize(Xml.findOffsets(dataView, ranges), 'xml', 'XML', ranges);
69
69
  }
70
70
 
71
71
  throw new Error('Invalid image format');
72
72
  }
73
73
 
74
- function finalize(offsets, fileType, fileTypeDescription, metadataBlocks) {
75
- const result = objectAssign({}, offsets, {fileType: {value: fileType, description: fileTypeDescription}});
76
- if (metadataBlocks !== undefined) {
77
- result.metadataBlocks = metadataBlocks;
74
+ function finalize(blocks, fileType, fileTypeDescription, ranges) {
75
+ const result = {fileType: {value: fileType, description: fileTypeDescription}, blocks};
76
+ if (ranges !== undefined) {
77
+ result.ranges = ranges;
78
78
  // Surface the finder's .truncated array convention as a sibling
79
79
  // field so consumers don't depend on properties on the array.
80
- result.metadataTruncated = !!metadataBlocks.truncated;
80
+ result.truncated = !!ranges.truncated;
81
81
  }
82
82
  return result;
83
83
  }