exifreader 4.52.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.
@@ -3,7 +3,7 @@
3
3
  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
4
 
5
5
  import {getBoxLength, getTiffHeaderOffset, BOX_TYPE_OFFSET, BOX_MIN_LENGTH} from './image-header-iso-bmff.js';
6
- import {pushMetadataBlock} from './utils.js';
6
+ import {pushRange} from './utils.js';
7
7
 
8
8
  export default {
9
9
  isJxlFile,
@@ -66,34 +66,36 @@ function isJxlCodestream(dataView) {
66
66
  }
67
67
 
68
68
  /**
69
- * Walks the JPEG XL box list and reports the offsets ExifReader needs.
69
+ * Walks the JPEG XL box list and reports the blocks ExifReader reads.
70
70
  *
71
- * @param {DataView} dataView The data view to find offsets in.
72
- * @param {Array<Object>=} metadataBlocks Optional out-array. When provided,
73
- * receives one block per recognized box (Exif, xml, brob with Exif or
74
- * xml original type). The codestream box (`jxlc`/`jxlp`) does not emit
75
- * a block because it wraps the encoded image, not metadata.
76
- * @returns {Object} hasAppMarkers and the per-section offsets.
71
+ * An Exif box that holds its TIFF header offset wins over any brob(Exif) box,
72
+ * otherwise the first brob(Exif) is reported; the same goes for the xml box
73
+ * and brob(xml). The last Exif or xml box wins among its own kind. The
74
+ * `jxlFile` block points at the codestream: the first jxlc box, or the first
75
+ * jxlp box with sequence number 0. A bare codestream is only a `jxlFile`
76
+ * block at offset 0.
77
+ *
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 box (Exif, xml, brob with
81
+ * Exif or xml original type), a brob box's span linked to its own block
82
+ * even when another box wins. The codestream box (`jxlc`/`jxlp`) gets no
83
+ * span because it wraps the encoded image, not metadata.
84
+ * @returns {Array<Object>} The blocks: exif, xmp and jxlFile, each when found.
77
85
  */
78
- function findJxlOffsets(dataView, metadataBlocks) {
86
+ function findJxlOffsets(dataView, ranges) {
87
+ const blocks = [];
79
88
  let offset = 0;
80
- let tiffHeaderOffset;
81
- let xmpChunks;
82
- let brobExifChunk;
83
- let brobXmpChunk;
84
- let jxlCodestreamOffset;
89
+ let exifBoxBlock;
90
+ let brobExifBlock;
91
+ let xmlBoxBlock;
92
+ let brobXmpBlock;
93
+ let jxlFileBlock;
85
94
 
86
95
  if (isJxlCodestream(dataView)) {
87
96
  // A bare codestream has no box structure, so the box walk below
88
97
  // would only ever misinterpret codestream bytes as box headers.
89
- return {
90
- hasAppMarkers: true,
91
- tiffHeaderOffset: undefined,
92
- xmpChunks: undefined,
93
- brobExifChunk: undefined,
94
- brobXmpChunk: undefined,
95
- jxlCodestreamOffset: 0,
96
- };
98
+ return [{kind: 'jxlFile', codestreamOffset: 0}];
97
99
  }
98
100
 
99
101
  while (offset + BOX_MIN_LENGTH <= dataView.byteLength) {
@@ -104,34 +106,40 @@ function findJxlOffsets(dataView, metadataBlocks) {
104
106
  const contentEnd = Math.min(offset + length, dataView.byteLength);
105
107
 
106
108
  const boxType = dataView.getUint32(offset + BOX_TYPE_OFFSET);
107
- let blockType;
109
+ let block;
108
110
 
109
111
  if (boxType === TYPE_EXIF) {
110
112
  if (contentOffset + EXIF_TIFF_HEADER_OFFSET_SIZE <= contentEnd) {
111
- tiffHeaderOffset = getTiffHeaderOffset(dataView, contentOffset);
113
+ exifBoxBlock = exifBoxBlock || {kind: 'exif'};
114
+ exifBoxBlock.tiffHeaderOffset = getTiffHeaderOffset(dataView, contentOffset);
115
+ block = exifBoxBlock;
116
+ } else {
117
+ // Too short to hold its TIFF header offset: listed, never read,
118
+ // and no bar to a brob(Exif) box.
119
+ block = {kind: 'exif'};
112
120
  }
113
- blockType = 'exif';
114
121
  }
115
122
 
116
123
  if (boxType === TYPE_XML) {
117
- xmpChunks = [{
124
+ xmlBoxBlock = xmlBoxBlock || {kind: 'xmp'};
125
+ xmlBoxBlock.chunks = [{
118
126
  dataOffset: contentOffset,
119
127
  length: length - (contentOffset - offset)
120
128
  }];
121
- blockType = 'xmp';
129
+ block = xmlBoxBlock;
122
130
  }
123
131
 
124
- if (boxType === TYPE_JXLC && jxlCodestreamOffset === undefined) {
125
- jxlCodestreamOffset = contentOffset;
126
- // No block emitted: the codestream box wraps the encoded image,
132
+ if (boxType === TYPE_JXLC && jxlFileBlock === undefined) {
133
+ jxlFileBlock = {kind: 'jxlFile', codestreamOffset: contentOffset};
134
+ // No span listed: the codestream box wraps the encoded image,
127
135
  // marking it would inflate metadataRange.end past pixel data.
128
136
  }
129
137
 
130
- if (boxType === TYPE_JXLP && jxlCodestreamOffset === undefined
138
+ if (boxType === TYPE_JXLP && jxlFileBlock === undefined
131
139
  && contentOffset + JXLP_SEQUENCE_NUMBER_SIZE <= contentEnd) {
132
140
  const sequenceNumber = dataView.getUint32(contentOffset) & 0x7FFFFFFF;
133
141
  if (sequenceNumber === 0) {
134
- jxlCodestreamOffset = contentOffset + JXLP_SEQUENCE_NUMBER_SIZE;
142
+ jxlFileBlock = {kind: 'jxlFile', codestreamOffset: contentOffset + JXLP_SEQUENCE_NUMBER_SIZE};
135
143
  // Same reason as TYPE_JXLC above.
136
144
  }
137
145
  }
@@ -141,47 +149,33 @@ function findJxlOffsets(dataView, metadataBlocks) {
141
149
  const compressedDataOffset = contentOffset + BROB_ORIGINAL_TYPE_SIZE;
142
150
  const compressedDataLength = length - (compressedDataOffset - offset);
143
151
 
144
- // Decouple block emission from parsing-source selection. A
145
- // brob(Exif) box must surface as an `exif` block even if a
146
- // plain Exif box already won the parsing race. The brob*Chunk
147
- // variables stay guarded to avoid double-parsing.
152
+ // Every brob box gets its own block for its span, but only the
153
+ // first of a kind can be the one read, and only when no plain box
154
+ // of that kind holds the data.
148
155
  if (originalType === TYPE_EXIF) {
149
- blockType = 'exif';
150
- if (tiffHeaderOffset === undefined && !brobExifChunk) {
151
- brobExifChunk = {
152
- dataOffset: compressedDataOffset,
153
- length: compressedDataLength
154
- };
155
- }
156
+ block = {kind: 'exif', compression: 'brotli', dataOffset: compressedDataOffset, length: compressedDataLength};
157
+ brobExifBlock = brobExifBlock || block;
156
158
  }
157
159
  if (originalType === TYPE_XML) {
158
- blockType = 'xmp';
159
- if (!xmpChunks && !brobXmpChunk) {
160
- brobXmpChunk = {
161
- dataOffset: compressedDataOffset,
162
- length: compressedDataLength
163
- };
164
- }
160
+ block = {kind: 'xmp', compression: 'brotli', dataOffset: compressedDataOffset, length: compressedDataLength};
161
+ brobXmpBlock = brobXmpBlock || block;
165
162
  }
166
163
  }
167
164
 
168
- pushMetadataBlock(metadataBlocks, blockType, offset, offset + length);
165
+ pushRange(ranges, block, offset, offset + length);
169
166
 
170
167
  offset += length;
171
168
  }
172
169
 
173
- const hasAppMarkers = tiffHeaderOffset !== undefined
174
- || xmpChunks !== undefined
175
- || brobExifChunk !== undefined
176
- || brobXmpChunk !== undefined
177
- || jxlCodestreamOffset !== undefined;
178
-
179
- return {
180
- hasAppMarkers,
181
- tiffHeaderOffset,
182
- xmpChunks,
183
- brobExifChunk,
184
- brobXmpChunk,
185
- jxlCodestreamOffset
186
- };
170
+ pushDefinedBlock(blocks, exifBoxBlock || brobExifBlock);
171
+ pushDefinedBlock(blocks, xmlBoxBlock || brobXmpBlock);
172
+ pushDefinedBlock(blocks, jxlFileBlock);
173
+
174
+ return blocks;
175
+ }
176
+
177
+ function pushDefinedBlock(blocks, block) {
178
+ if (block !== undefined) {
179
+ blocks.push(block);
180
+ }
187
181
  }
@@ -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
  }