exifreader 4.53.0 → 4.55.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.
Files changed (100) hide show
  1. package/README.md +404 -33
  2. package/bin/analyze.js +139 -9
  3. package/bin/build-pages.js +21 -35
  4. package/bin/cli.js +81 -13
  5. package/bin/generate-tag-modules.js +287 -0
  6. package/dist/exif-reader.js +1 -1
  7. package/dist/exif-reader.js.map +1 -1
  8. package/exif-reader.d.ts +213 -38
  9. package/package.json +71 -11
  10. package/src/base64-image.js +27 -0
  11. package/src/composable/brand.js +10 -0
  12. package/src/composable/composition-tag-filter.js +97 -0
  13. package/src/composable/composition.js +195 -0
  14. package/src/composable/core.d.ts +148 -0
  15. package/src/composable/core.js +526 -0
  16. package/src/composable/exif-block-reader.js +197 -0
  17. package/src/composable/formats/avif.js +19 -0
  18. package/src/composable/formats/gif.js +28 -0
  19. package/src/composable/formats/heic.js +19 -0
  20. package/src/composable/formats/iso-bmff-file-reader.js +14 -0
  21. package/src/composable/formats/jpeg.js +55 -0
  22. package/src/composable/formats/jxl.js +30 -0
  23. package/src/composable/formats/png.js +187 -0
  24. package/src/composable/formats/tiff.js +18 -0
  25. package/src/composable/formats/webp.js +28 -0
  26. package/src/composable/formats/xml.js +18 -0
  27. package/src/composable/formats.d.ts +17 -0
  28. package/src/composable/formats.js +17 -0
  29. package/src/composable/group-helpers.js +129 -0
  30. package/src/composable/groups/canon.js +26 -0
  31. package/src/composable/groups/composite.js +43 -0
  32. package/src/composable/groups/exif.js +48 -0
  33. package/src/composable/groups/gps.js +75 -0
  34. package/src/composable/groups/icc.js +52 -0
  35. package/src/composable/groups/iptc.js +86 -0
  36. package/src/composable/groups/maker-notes.js +21 -0
  37. package/src/composable/groups/mpf.js +25 -0
  38. package/src/composable/groups/pentax.js +34 -0
  39. package/src/composable/groups/photoshop.js +18 -0
  40. package/src/composable/groups/thumbnail.js +131 -0
  41. package/src/composable/groups/xmp.js +118 -0
  42. package/src/composable/groups.d.ts +20 -0
  43. package/src/composable/groups.js +105 -0
  44. package/src/composable/iptc-block-reader.js +44 -0
  45. package/src/composable/read-view.js +165 -0
  46. package/src/composable/tag-binding-name.js +17 -0
  47. package/src/composable/tags/exif.d.ts +295 -0
  48. package/src/composable/tags/exif.js +396 -0
  49. package/src/composable/tags/iptc.d.ts +84 -0
  50. package/src/composable/tags/iptc.js +109 -0
  51. package/src/composable/value-budget.js +24 -0
  52. package/src/composite.js +2 -2
  53. package/src/decompression.js +60 -0
  54. package/src/dom-parser.js +5 -6
  55. package/src/engine.js +332 -0
  56. package/src/errors.js +8 -2
  57. package/src/exif-reader.d.ts +8 -0
  58. package/src/exif-reader.js +118 -1121
  59. package/src/file-loaders.js +74 -1
  60. package/src/file-tags.js +9 -9
  61. package/src/image-header-avif.js +7 -8
  62. package/src/image-header-gif.js +9 -10
  63. package/src/image-header-heic.js +7 -8
  64. package/src/image-header-iso-bmff.js +71 -92
  65. package/src/image-header-jpeg.js +60 -59
  66. package/src/image-header-jxl.js +59 -65
  67. package/src/image-header-png.js +199 -76
  68. package/src/image-header-tiff.js +7 -4
  69. package/src/image-header-webp.js +29 -35
  70. package/src/image-header.js +25 -59
  71. package/src/iptc-tag-names-descriptions.js +334 -0
  72. package/src/iptc-tag-names.js +50 -306
  73. package/src/iptc-tags.js +41 -27
  74. package/src/jfif-tags.js +9 -9
  75. package/src/load-auto.js +2 -2
  76. package/src/loadview-pipeline.js +197 -498
  77. package/src/mpf-tags.js +10 -7
  78. package/src/node-require.js +15 -2
  79. package/src/png-text-tags.js +90 -327
  80. package/src/tag-filter-config.js +24 -15
  81. package/src/tag-filter.js +67 -3
  82. package/src/tag-names-0th-ifd-descriptions.js +232 -0
  83. package/src/tag-names-0th-ifd.js +49 -173
  84. package/src/tag-names-common.js +263 -211
  85. package/src/tag-names-exif-ifd-descriptions.js +249 -0
  86. package/src/tag-names-exif-ifd.js +55 -187
  87. package/src/tag-names-gps-ifd-descriptions.js +156 -0
  88. package/src/tag-names-gps-ifd.js +39 -146
  89. package/src/tag-names-utils.js +33 -10
  90. package/src/tags-helpers.js +14 -24
  91. package/src/tags.js +23 -11
  92. package/src/thumbnail.js +7 -5
  93. package/src/types.js +2 -0
  94. package/src/unsupported-format-error.js +21 -0
  95. package/src/utils.js +64 -119
  96. package/src/writer/exif-tag-table.js +321 -0
  97. package/src/xml.js +12 -15
  98. package/src/xmp-tag-names.js +42 -21
  99. package/src/xmp-tags.js +9 -10
  100. package/webpack.config.js +1 -1
@@ -4,7 +4,7 @@
4
4
 
5
5
  // Specification: http://www.libpng.org/pub/png/spec/1.2/
6
6
 
7
- import {getStringFromDataView, pushMetadataBlock} from './utils.js';
7
+ import {getStringFromDataView, getStringValueFromArray, addBlock, pushRange, COMPRESSION_METHOD_NONE, MAX_METADATA_BLOCKS} from './utils.js';
8
8
 
9
9
  export default {
10
10
  isPngFile,
@@ -27,6 +27,33 @@ export const TYPE_EXIF = 'eXIf';
27
27
  export const TYPE_ICCP = 'iCCP';
28
28
  // PNG spec: an iCCP profile name is at most 79 bytes, like a text keyword.
29
29
  const MAX_ICC_PROFILE_NAME_LENGTH = 79;
30
+ export const EXIF_PROFILE_NAME = 'raw profile type exif';
31
+ export const IPTC_PROFILE_NAME = 'raw profile type iptc';
32
+ const STATE_KEYWORD = 'STATE_KEYWORD';
33
+ const STATE_COMPRESSION = 'STATE_COMPRESSION';
34
+ const STATE_LANG = 'STATE_LANG';
35
+ const STATE_TRANSLATED_KEYWORD = 'STATE_TRANSLATED_KEYWORD';
36
+ const STATE_TEXT = 'STATE_TEXT';
37
+ const COMPRESSION_SECTION_ITXT_EXTRA_BYTE = 1;
38
+ const COMPRESSION_FLAG_COMPRESSED = 1;
39
+ const COMPRESSION_METHOD_DEFLATE = 0;
40
+ // PNG spec: a keyword is 1-79 bytes.
41
+ const MAX_KEYWORD_LENGTH = 79;
42
+ // Our own bound: BCP 47 sets no maximum, but real tags are far shorter.
43
+ const MAX_LANGUAGE_TAG_LENGTH = 79;
44
+ // Guards against memory exhaustion from a crafted file: every text chunk is its
45
+ // own block, and a file of 8 MiB holds 600,000 empty ones. Real files have tens.
46
+ const MAX_TEXT_CHUNK_BLOCKS = MAX_METADATA_BLOCKS;
47
+ // A text chunk past the cap is listed against the shared block of its type and
48
+ // never read, so that metadataRange still covers it.
49
+ const DROPPED_TEXT_CHUNK_BLOCKS = {};
50
+ DROPPED_TEXT_CHUNK_BLOCKS[TYPE_TEXT] = getDroppedTextChunkBlock(TYPE_TEXT);
51
+ DROPPED_TEXT_CHUNK_BLOCKS[TYPE_ITXT] = getDroppedTextChunkBlock(TYPE_ITXT);
52
+ DROPPED_TEXT_CHUNK_BLOCKS[TYPE_ZTXT] = getDroppedTextChunkBlock(TYPE_ZTXT);
53
+
54
+ function getDroppedTextChunkBlock(chunkType) {
55
+ return {kind: 'pngText', text: {chunkType, name: undefined, valueOffset: undefined, valueLength: undefined}};
56
+ }
30
57
 
31
58
  /**
32
59
  * Checks if the provided data view represents a PNG file.
@@ -39,86 +66,91 @@ function isPngFile(dataView) {
39
66
  }
40
67
 
41
68
  /**
42
- * Walks the PNG chunk list and reports the offsets ExifReader needs.
69
+ * Walks the PNG chunk list and reports the blocks ExifReader reads.
70
+ *
71
+ * Every tEXt, iTXt and zTXt chunk but the XMP iTXt is its own block, classified
72
+ * by keyword: `exif` for a raw Exif profile, `iptc` for a raw IPTC profile and
73
+ * `pngText` for anything else, with `compression` set for a compressed chunk.
74
+ * zTXt chunks are reported in every mode; the orchestrator decides whether a
75
+ * compressed block is read. Text chunks past the first MAX_TEXT_CHUNK_BLOCKS
76
+ * are only listed in `ranges`.
43
77
  *
44
- * @param {DataView} dataView The data view to find offsets in.
45
- * @param {boolean} async Whether async parsing is enabled (gates zTXt/iCCP).
46
- * @param {Array<Object>=} metadataBlocks Optional out-array. When provided,
47
- * receives one block per recognized chunk and gets `truncated` set to
48
- * true if the loop never saw an IEND chunk.
49
- * @returns {Object} Per-section offsets plus `hasAppMarkers`.
78
+ * @param {DataView} dataView The data view to find blocks in.
79
+ * @param {Array<Object>=} ranges Optional out-array. When provided, receives
80
+ * one `{block, start, end}` span per recognized chunk and gets
81
+ * `truncated` set to true if the loop never saw an IEND chunk.
82
+ * @returns {Array<Object>} The blocks, in chunk order.
50
83
  */
51
- function findPngOffsets(dataView, async, metadataBlocks) {
84
+ function findPngOffsets(dataView, ranges) {
52
85
  const PNG_CRC_SIZE = 4;
53
86
  const TYPE_IEND = 'IEND';
87
+ const TYPE_IHDR = 'IHDR';
54
88
 
55
- const offsets = {
56
- hasAppMarkers: false
57
- };
89
+ const blocks = [];
90
+ let fileBlock;
91
+ let xmpBlock;
92
+ let exifBlock;
93
+ let iccBlock;
94
+ let textChunkCount = 0;
58
95
 
59
96
  let offset = PNG_ID.length;
60
97
  let sawIEnd = false;
61
98
 
62
99
  while (offset + PNG_CHUNK_LENGTH_SIZE + PNG_CHUNK_TYPE_SIZE <= dataView.byteLength) {
63
- let blockType;
100
+ let block;
64
101
  const chunkDataLength = dataView.getUint32(offset + PNG_CHUNK_LENGTH_OFFSET);
102
+ const chunkType = getStringFromDataView(dataView, offset + PNG_CHUNK_TYPE_OFFSET, PNG_CHUNK_TYPE_SIZE);
65
103
 
66
- // The IEND check is only needed for the truncation signal, so skip
67
- // the per-chunk string read for default callers.
68
- if (metadataBlocks
69
- && getStringFromDataView(dataView, offset + PNG_CHUNK_TYPE_OFFSET, PNG_CHUNK_TYPE_SIZE) === TYPE_IEND) {
104
+ if (chunkType === TYPE_IEND) {
70
105
  sawIEnd = true;
71
- }
72
- if (isPngImageHeaderChunk(dataView, offset)) {
73
- offsets.hasAppMarkers = true;
74
- offsets.pngHeaderOffset = offset + PNG_CHUNK_DATA_OFFSET;
75
- blockType = 'file';
76
- } else if (isPngXmpChunk(dataView, offset)) {
106
+ } else if (chunkType === TYPE_IHDR) {
107
+ fileBlock = fileBlock || addBlock(blocks, {kind: 'pngFile'});
108
+ fileBlock.dataOffset = offset + PNG_CHUNK_DATA_OFFSET;
109
+ block = fileBlock;
110
+ } else if (isPngXmpChunk(dataView, chunkType, offset)) {
77
111
  const dataOffset = getPngXmpDataOffset(dataView, offset);
78
112
  if (dataOffset !== undefined) {
79
- offsets.hasAppMarkers = true;
80
- offsets.xmpChunks = [{
113
+ xmpBlock = xmpBlock || addBlock(blocks, {kind: 'xmp'});
114
+ xmpBlock.chunks = [{
81
115
  dataOffset,
82
116
  length: chunkDataLength - (dataOffset - (offset + PNG_CHUNK_DATA_OFFSET))
83
117
  }];
84
- blockType = 'xmp';
118
+ block = xmpBlock;
85
119
  }
86
- } else if (isPngTextChunk(dataView, offset, async)) {
87
- offsets.hasAppMarkers = true;
88
- if (!offsets.pngTextChunks) {
89
- offsets.pngTextChunks = [];
120
+ } else if (isPngTextChunk(chunkType)) {
121
+ if (textChunkCount < MAX_TEXT_CHUNK_BLOCKS) {
122
+ block = addBlock(blocks, getTextChunkBlock(dataView, offset + PNG_CHUNK_DATA_OFFSET, chunkDataLength, chunkType));
123
+ } else {
124
+ block = DROPPED_TEXT_CHUNK_BLOCKS[chunkType];
90
125
  }
91
- offsets.pngTextChunks.push(offset + PNG_CHUNK_LENGTH_OFFSET);
92
- blockType = 'png';
93
- } else if (isPngExifChunk(dataView, offset)) {
94
- offsets.hasAppMarkers = true;
95
- offsets.tiffHeaderOffset = offset + PNG_CHUNK_DATA_OFFSET;
96
- blockType = 'exif';
97
- } else if (async && isPngIccpChunk(dataView, offset)) {
126
+ textChunkCount++;
127
+ } else if (chunkType === TYPE_EXIF) {
128
+ exifBlock = exifBlock || addBlock(blocks, {kind: 'exif'});
129
+ exifBlock.tiffHeaderOffset = offset + PNG_CHUNK_DATA_OFFSET;
130
+ block = exifBlock;
131
+ } else if (chunkType === TYPE_ICCP) {
98
132
  const iccHeaderOffset = offset + PNG_CHUNK_DATA_OFFSET;
99
133
  const iccHeader = parseIccHeader(dataView, iccHeaderOffset, chunkDataLength);
100
134
  if (iccHeader !== undefined) {
101
- offsets.hasAppMarkers = true;
102
135
  // PNG spec: a file has at most one iCCP chunk, so only the first valid one is kept.
103
- if (!offsets.iccChunks) {
104
- offsets.iccChunks = [{
105
- offset: iccHeader.compressedProfileOffset,
106
- length: chunkDataLength - (iccHeader.compressedProfileOffset - iccHeaderOffset),
107
- chunkNumber: 1,
108
- chunksTotal: 1,
109
- profileName: iccHeader.profileName,
110
- compressionMethod: iccHeader.compressionMethod
111
- }];
136
+ if (!iccBlock) {
137
+ iccBlock = addBlock(blocks, {
138
+ kind: 'icc',
139
+ compression: iccHeader.compressionMethod === COMPRESSION_METHOD_DEFLATE ? 'deflate' : 'unknown',
140
+ chunks: [{
141
+ offset: iccHeader.compressedProfileOffset,
142
+ length: chunkDataLength - (iccHeader.compressedProfileOffset - iccHeaderOffset),
143
+ chunkNumber: 1,
144
+ chunksTotal: 1,
145
+ profileName: iccHeader.profileName,
146
+ compressionMethod: iccHeader.compressionMethod
147
+ }]
148
+ });
112
149
  }
113
- blockType = 'icc';
150
+ block = iccBlock;
114
151
  }
115
- } else if (isPngChunk(dataView, offset)) {
116
- offsets.hasAppMarkers = true;
117
- if (!offsets.pngChunkOffsets) {
118
- offsets.pngChunkOffsets = [];
119
- }
120
- offsets.pngChunkOffsets.push(offset + PNG_CHUNK_LENGTH_OFFSET);
121
- blockType = 'png';
152
+ } else if (isPngChunk(chunkType)) {
153
+ block = addBlock(blocks, {kind: 'pngChunk', dataOffset: offset + PNG_CHUNK_LENGTH_OFFSET});
122
154
  }
123
155
 
124
156
  const chunkTotalSize = chunkDataLength
@@ -126,45 +158,136 @@ function findPngOffsets(dataView, async, metadataBlocks) {
126
158
  + PNG_CHUNK_TYPE_SIZE
127
159
  + PNG_CRC_SIZE;
128
160
 
129
- pushMetadataBlock(metadataBlocks, blockType, offset, offset + chunkTotalSize);
161
+ pushRange(ranges, block, offset, offset + chunkTotalSize);
130
162
 
131
163
  offset += chunkTotalSize;
132
164
  }
133
165
 
134
- if (metadataBlocks) {
135
- metadataBlocks.truncated = !sawIEnd;
166
+ if (ranges) {
167
+ ranges.truncated = !sawIEnd;
136
168
  }
137
169
 
138
- return offsets;
170
+ return blocks;
139
171
  }
140
172
 
141
- function isPngImageHeaderChunk(dataView, offset) {
142
- const PNG_CHUNK_TYPE_IMAGE_HEADER = 'IHDR';
143
- return getStringFromDataView(dataView, offset + PNG_CHUNK_TYPE_OFFSET, PNG_CHUNK_TYPE_SIZE) === PNG_CHUNK_TYPE_IMAGE_HEADER;
173
+ function isPngXmpChunk(dataView, chunkType, offset) {
174
+ return (chunkType === TYPE_ITXT)
175
+ && (getStringFromDataView(dataView, offset + PNG_CHUNK_DATA_OFFSET, PNG_XMP_PREFIX.length) === PNG_XMP_PREFIX);
144
176
  }
145
177
 
146
- function isPngXmpChunk(dataView, offset) {
147
- return (getStringFromDataView(dataView, offset + PNG_CHUNK_TYPE_OFFSET, PNG_CHUNK_TYPE_SIZE) === TYPE_ITXT)
148
- && (getStringFromDataView(dataView, offset + PNG_CHUNK_DATA_OFFSET, PNG_XMP_PREFIX.length) === PNG_XMP_PREFIX);
178
+ function isPngTextChunk(chunkType) {
179
+ return chunkType === TYPE_TEXT || chunkType === TYPE_ITXT || chunkType === TYPE_ZTXT;
149
180
  }
150
181
 
151
- function isPngTextChunk(dataView, offset, async) {
152
- const chunkType = getStringFromDataView(dataView, offset + PNG_CHUNK_TYPE_OFFSET, PNG_CHUNK_TYPE_SIZE);
153
- return chunkType === TYPE_TEXT || chunkType === TYPE_ITXT || (chunkType === TYPE_ZTXT && async);
182
+ function isPngChunk(chunkType) {
183
+ const SUPPORTED_PNG_CHUNK_TYPES = [TYPE_PHYS, TYPE_TIME];
184
+ return SUPPORTED_PNG_CHUNK_TYPES.includes(chunkType);
154
185
  }
155
186
 
156
- function isPngExifChunk(dataView, offset) {
157
- return getStringFromDataView(dataView, offset + PNG_CHUNK_TYPE_OFFSET, PNG_CHUNK_TYPE_SIZE) === TYPE_EXIF;
187
+ // The block carries where the text bytes are, not a copy of them: a block the
188
+ // orchestrator hides or a group the filter leaves out is never read.
189
+ function getTextChunkBlock(dataView, offset, length, chunkType) {
190
+ const textChunk = parseTextChunk(dataView, offset, length, chunkType);
191
+ if (textChunk === undefined) {
192
+ return getDroppedTextChunkBlock(chunkType);
193
+ }
194
+ const name = getName(chunkType, textChunk.langChars, textChunk.keywordChars);
195
+ const block = {kind: getTextChunkKind(name)};
196
+ if (textChunk.compressionMethod !== COMPRESSION_METHOD_NONE) {
197
+ block.compression = textChunk.compressionMethod === COMPRESSION_METHOD_DEFLATE ? 'deflate' : 'unknown';
198
+ }
199
+ block.text = {chunkType, name, valueOffset: textChunk.valueOffset, valueLength: textChunk.valueLength};
200
+ return block;
158
201
  }
159
202
 
160
- function isPngIccpChunk(dataView, offset) {
161
- return getStringFromDataView(dataView, offset + PNG_CHUNK_TYPE_OFFSET, PNG_CHUNK_TYPE_SIZE) === TYPE_ICCP;
203
+ function parseTextChunk(dataView, offset, length, type) {
204
+ const keywordChars = [];
205
+ const langChars = [];
206
+ let valueOffset;
207
+ let valueLength;
208
+ let parsingState = STATE_KEYWORD;
209
+ let compressionMethod = COMPRESSION_METHOD_NONE;
210
+ const chunkEnd = Math.min(offset + length, dataView.byteLength);
211
+
212
+ for (let i = 0; i < length && offset + i < dataView.byteLength; i++) {
213
+ if (parsingState === STATE_COMPRESSION) {
214
+ compressionMethod = getTextChunkCompressionMethod({type, dataView, offset: offset + i, chunkEnd});
215
+ if (type === TYPE_ITXT) {
216
+ i += COMPRESSION_SECTION_ITXT_EXTRA_BYTE;
217
+ }
218
+ parsingState = moveToNextState(type, parsingState);
219
+ continue;
220
+ } else if (parsingState === STATE_TEXT) {
221
+ valueOffset = offset + i;
222
+ valueLength = length - i;
223
+ break;
224
+ }
225
+ const byte = dataView.getUint8(offset + i);
226
+ if (byte === 0) {
227
+ parsingState = moveToNextState(type, parsingState);
228
+ } else if (parsingState === STATE_KEYWORD) {
229
+ if (keywordChars.length >= MAX_KEYWORD_LENGTH) {
230
+ return undefined;
231
+ }
232
+ keywordChars.push(byte);
233
+ } else if (parsingState === STATE_LANG) {
234
+ if (langChars.length >= MAX_LANGUAGE_TAG_LENGTH) {
235
+ return undefined;
236
+ }
237
+ langChars.push(byte);
238
+ }
239
+ }
240
+
241
+ return {keywordChars, langChars, compressionMethod, valueOffset, valueLength};
162
242
  }
163
243
 
164
- function isPngChunk(dataView, offset) {
165
- const SUPPORTED_PNG_CHUNK_TYPES = [TYPE_PHYS, TYPE_TIME];
166
- const chunkType = getStringFromDataView(dataView, offset + PNG_CHUNK_TYPE_OFFSET, PNG_CHUNK_TYPE_SIZE);
167
- return SUPPORTED_PNG_CHUNK_TYPES.includes(chunkType);
244
+ function getTextChunkCompressionMethod({type, dataView, offset, chunkEnd}) {
245
+ if (type === TYPE_ITXT) {
246
+ if (offset + 1 < chunkEnd && dataView.getUint8(offset) === COMPRESSION_FLAG_COMPRESSED) {
247
+ return dataView.getUint8(offset + 1);
248
+ }
249
+ } else if (type === TYPE_ZTXT) {
250
+ return dataView.getUint8(offset);
251
+ }
252
+ return COMPRESSION_METHOD_NONE;
253
+ }
254
+
255
+ function moveToNextState(type, parsingState) {
256
+ if (parsingState === STATE_KEYWORD && [TYPE_ITXT, TYPE_ZTXT].includes(type)) {
257
+ return STATE_COMPRESSION;
258
+ }
259
+ if (parsingState === STATE_COMPRESSION) {
260
+ if (type === TYPE_ITXT) {
261
+ return STATE_LANG;
262
+ }
263
+ return STATE_TEXT;
264
+ }
265
+ if (parsingState === STATE_LANG) {
266
+ return STATE_TRANSLATED_KEYWORD;
267
+ }
268
+ return STATE_TEXT;
269
+ }
270
+
271
+ function getName(type, langChars, keywordChars) {
272
+ const name = getStringValueFromArray(keywordChars);
273
+ if (type === TYPE_TEXT || langChars.length === 0) {
274
+ return name;
275
+ }
276
+ const lang = getStringValueFromArray(langChars);
277
+ return `${name} (${lang})`;
278
+ }
279
+
280
+ // A raw profile is told by its keyword here; the reader checks the payload
281
+ // and reads a block whose value is not a profile as plain text.
282
+ function getTextChunkKind(name) {
283
+ const lowerCaseName = name.toLowerCase();
284
+ if (lowerCaseName === EXIF_PROFILE_NAME) {
285
+ return 'exif';
286
+ }
287
+ if (lowerCaseName === IPTC_PROFILE_NAME) {
288
+ return 'iptc';
289
+ }
290
+ return 'pngText';
168
291
  }
169
292
 
170
293
  function getPngXmpDataOffset(dataView, offset) {
@@ -21,11 +21,14 @@ function hasTiffMarker(dataView) {
21
21
  return dataView.getUint16(TIFF_ID_OFFSET, littleEndian) === TIFF_ID;
22
22
  }
23
23
 
24
+ /**
25
+ * A TIFF file is its own Exif block: the TIFF header sits at offset 0. There
26
+ * is no container around it, so no range is recorded.
27
+ *
28
+ * @returns {Array<Object>} One exif block.
29
+ */
24
30
  function findTiffOffsets() {
25
31
  const TIFF_FILE_HEADER_OFFSET = 0;
26
32
 
27
- return {
28
- hasAppMarkers: true,
29
- tiffHeaderOffset: TIFF_FILE_HEADER_OFFSET
30
- };
33
+ return [{kind: 'exif', tiffHeaderOffset: TIFF_FILE_HEADER_OFFSET}];
31
34
  }
@@ -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
  }
@@ -2,17 +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 Constants from './constants.js';
6
- import Tiff from './image-header-tiff.js';
7
- import Jpeg from './image-header-jpeg.js';
8
- import Png from './image-header-png.js';
9
- import Heic from './image-header-heic.js';
10
- import Avif from './image-header-avif.js';
11
- import Jxl from './image-header-jxl.js';
12
- import Webp from './image-header-webp.js';
13
- import Gif from './image-header-gif.js';
14
- import Xml from './xml.js';
15
- import {objectAssign} from './utils.js';
5
+ import {UnsupportedFormatError} from './unsupported-format-error.js';
16
6
 
17
7
  export default {
18
8
  parseAppMarkers
@@ -22,62 +12,38 @@ export default {
22
12
  * Detects the image format and delegates to the matching finder.
23
13
  *
24
14
  * @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`.
30
- * @throws {Error} When the data view is not a recognized image format.
15
+ * @param {boolean=} includeRanges When true, the finder also records where
16
+ * each block's bytes sit in the file. The result then gains `ranges`, a
17
+ * list of `{block, start, end}` in file order, and `truncated`, true when
18
+ * the finder never reached the end of the file's metadata.
19
+ * @param {Array<{header: {fileType: {value: string, description: string}, detect: function, find: function}}>} formats
20
+ * The composed formats in detection order; the first whose `detect`
21
+ * matches runs its `find`.
22
+ * @returns {{fileType: {value: string, description: string}, blocks: Array<Object>, ranges: (Array<Object>|undefined), truncated: (boolean|undefined)}}
23
+ * The blocks the finder found, each `{kind, ...payload}`.
24
+ * @throws {UnsupportedFormatError} When the data view is not a recognized
25
+ * image format.
31
26
  */
32
- function parseAppMarkers(dataView, async, includeMetadataBlocks) {
33
- const metadataBlocks = includeMetadataBlocks ? [] : undefined;
27
+ function parseAppMarkers(dataView, includeRanges, formats) {
28
+ const ranges = includeRanges ? [] : undefined;
34
29
 
35
- if (Constants.USE_TIFF && Tiff.isTiffFile(dataView)) {
36
- return finalize(Tiff.findTiffOffsets(), 'tiff', 'TIFF', metadataBlocks);
30
+ for (let i = 0; i < formats.length; i++) {
31
+ const header = formats[i].header;
32
+ if (header.detect(dataView)) {
33
+ return finalize(header.find(dataView, ranges), header.fileType.value, header.fileType.description, ranges);
34
+ }
37
35
  }
38
36
 
39
- if (Constants.USE_JPEG && Jpeg.isJpegFile(dataView)) {
40
- return finalize(Jpeg.findJpegOffsets(dataView, metadataBlocks), 'jpeg', 'JPEG', metadataBlocks);
41
- }
42
-
43
- if (Constants.USE_PNG && Png.isPngFile(dataView)) {
44
- return finalize(Png.findPngOffsets(dataView, async, metadataBlocks), 'png', 'PNG', metadataBlocks);
45
- }
46
-
47
- if (Constants.USE_HEIC && Heic.isHeicFile(dataView)) {
48
- return finalize(Heic.findHeicOffsets(dataView, metadataBlocks), 'heic', 'HEIC', metadataBlocks);
49
- }
50
-
51
- if (Constants.USE_AVIF && Avif.isAvifFile(dataView)) {
52
- return finalize(Avif.findAvifOffsets(dataView, metadataBlocks), 'avif', 'AVIF', metadataBlocks);
53
- }
54
-
55
- if (Constants.USE_JXL && Jxl.isJxlFile(dataView)) {
56
- return finalize(Jxl.findJxlOffsets(dataView, metadataBlocks), 'jxl', 'JPEG XL', metadataBlocks);
57
- }
58
-
59
- if (Constants.USE_WEBP && Webp.isWebpFile(dataView)) {
60
- return finalize(Webp.findOffsets(dataView, metadataBlocks), 'webp', 'WebP', metadataBlocks);
61
- }
62
-
63
- if (Constants.USE_GIF && Gif.isGifFile(dataView)) {
64
- return finalize(Gif.findOffsets(dataView, metadataBlocks), 'gif', 'GIF', metadataBlocks);
65
- }
66
-
67
- if (Constants.USE_XMP && Xml.isXMLFile(dataView)) {
68
- return finalize(Xml.findOffsets(dataView, metadataBlocks), 'xml', 'XML', metadataBlocks);
69
- }
70
-
71
- throw new Error('Invalid image format');
37
+ throw new UnsupportedFormatError('Invalid image format');
72
38
  }
73
39
 
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;
40
+ function finalize(blocks, fileType, fileTypeDescription, ranges) {
41
+ const result = {fileType: {value: fileType, description: fileTypeDescription}, blocks};
42
+ if (ranges !== undefined) {
43
+ result.ranges = ranges;
78
44
  // Surface the finder's .truncated array convention as a sibling
79
45
  // field so consumers don't depend on properties on the array.
80
- result.metadataTruncated = !!metadataBlocks.truncated;
46
+ result.truncated = !!ranges.truncated;
81
47
  }
82
48
  return result;
83
49
  }