exifreader 4.46.0 → 4.48.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.
@@ -4,9 +4,9 @@
4
4
 
5
5
  // Specification: https://www.adobe.com/devnet-apps/photoshop/fileformatashtml/
6
6
 
7
- import {getDataView, getStringFromDataView, getPascalStringFromDataView} from './utils.js';
7
+ import {getByteString, getDataView, getStringFromDataView, getPascalStringFromDataView, getTagKey, setProperty} from './utils.js';
8
8
  import Types from './types.js';
9
- import TagNames from './photoshop-tag-names.js';
9
+ import TagNames, {MAX_PATH_RECORDS} from './photoshop-tag-names.js';
10
10
  import {NOOP_TAG_FILTER} from './tag-filter.js';
11
11
 
12
12
  export default {
@@ -25,7 +25,9 @@ function read(bytes, includeUnknown, tagFilter = NOOP_TAG_FILTER) {
25
25
  if (!Array.isArray(bytes)) {
26
26
  return {};
27
27
  }
28
- const dataView = getDataView(new Uint8Array(bytes).buffer);
28
+ const byteArray = new Uint8Array(bytes);
29
+ const dataView = getDataView(byteArray.buffer);
30
+ const pathRecordBudget = {remaining: MAX_PATH_RECORDS};
29
31
  const tags = {};
30
32
  let offset = 0;
31
33
 
@@ -45,8 +47,8 @@ function read(bytes, includeUnknown, tagFilter = NOOP_TAG_FILTER) {
45
47
  // it by the bytes that are actually present.
46
48
  const resourceSize = Math.min(declaredResourceSize, dataView.byteLength - offset);
47
49
  if (signature === SIGNATURE) {
48
- const resolvedTagName = getTagNameForFiltering(tagId, tagName, includeUnknown);
49
- if (!tagFilter.shouldParseTag('photoshop', resolvedTagName, tagId)) {
50
+ const tagKey = getResourceTagKey(tagId, tagName);
51
+ if (!tagFilter.shouldParseTag('photoshop', tagKey, tagId)) {
50
52
  offset += resourceSize + (resourceSize % 2);
51
53
  continue;
52
54
  }
@@ -54,17 +56,17 @@ function read(bytes, includeUnknown, tagFilter = NOOP_TAG_FILTER) {
54
56
  const valueDataView = getDataView(dataView.buffer, offset, resourceSize);
55
57
  const tag = {
56
58
  id: tagId,
57
- value: getStringFromDataView(valueDataView, 0, resourceSize),
59
+ value: getByteString(byteArray, offset, offset + resourceSize),
58
60
  };
59
61
  if (TagNames[tagId]) {
60
62
  try {
61
- tag.description = TagNames[tagId].description(valueDataView);
63
+ tag.description = TagNames[tagId].description(valueDataView, pathRecordBudget);
62
64
  } catch (error) {
63
65
  tag.description = '<no description formatter>';
64
66
  }
65
- tags[tagName ? tagName : TagNames[tagId].name] = tag;
67
+ setProperty(tags, tagKey, tag);
66
68
  } else if (includeUnknown) {
67
- tags[`undefined-${tagId}`] = tag;
69
+ tags[tagKey] = tag;
68
70
  }
69
71
  }
70
72
  offset += resourceSize + (resourceSize % 2);
@@ -73,20 +75,11 @@ function read(bytes, includeUnknown, tagFilter = NOOP_TAG_FILTER) {
73
75
  return tags;
74
76
  }
75
77
 
76
- function getTagNameForFiltering(tagId, tagName, includeUnknown) {
77
- if (tagName) {
78
- return tagName;
78
+ function getResourceTagKey(tagId, tagName) {
79
+ if (TagNames[tagId]) {
80
+ return getTagKey(tagName || TagNames[tagId].name);
79
81
  }
80
-
81
- if (TagNames[tagId] && TagNames[tagId].name) {
82
- return TagNames[tagId].name;
83
- }
84
-
85
- if (includeUnknown) {
86
- return `undefined-${tagId}`;
87
- }
88
-
89
- return undefined;
82
+ return `undefined-${tagId}`;
90
83
  }
91
84
 
92
85
  function getTagName(dataView, offset) {
@@ -4,13 +4,14 @@
4
4
 
5
5
  // Specification: http://www.libpng.org/pub/png/spec/1.2/
6
6
 
7
- import {getStringValueFromArray, getStringFromDataView, decompress, COMPRESSION_METHOD_NONE} from './utils.js';
7
+ import {getStringValueFromArray, getStringFromDataView, decompress, setProperty, getTagKey, objectAssign, COMPRESSION_METHOD_NONE, getHexDigitValue} from './utils.js';
8
8
  import TagDecoder from './tag-decoder.js';
9
9
  import {TYPE_TEXT, TYPE_ITXT, TYPE_ZTXT} from './image-header-png.js';
10
10
  import Tags from './tags.js';
11
11
  import IptcTags from './iptc-tags.js';
12
12
  import Constants from './constants.js';
13
13
  import {NOOP_TAG_FILTER} from './tag-filter.js';
14
+ import {addDecompressedValueAllowance, addDecompressedIptcAllowance} from './tags-helpers.js';
14
15
 
15
16
  export default {
16
17
  read
@@ -24,7 +25,27 @@ const STATE_TEXT = 'STATE_TEXT';
24
25
  const COMPRESSION_SECTION_ITXT_EXTRA_BYTE = 1;
25
26
  const COMPRESSION_FLAG_COMPRESSED = 1;
26
27
  const EXIF_OFFSET = 6;
28
+ const MAX_COMPRESSED_TEXT_CHUNKS = 255;
29
+ const MAX_DECOMPRESSIONS_IN_FLIGHT = 4;
30
+ // PNG spec: a keyword is 1-79 bytes.
31
+ const MAX_KEYWORD_LENGTH = 79;
32
+ // Our own bound: BCP 47 sets no maximum, but real tags are far shorter.
33
+ const MAX_LANGUAGE_TAG_LENGTH = 79;
27
34
 
35
+ /**
36
+ * @param {function(DataView, object, number): object} [getThumbnail] Called with a
37
+ * decoded Exif raw profile, its thumbnail IFD tags and the TIFF header offset;
38
+ * returns the thumbnail tags, with `image` set when the thumbnail was read.
39
+ * No thumbnail is returned when it is not passed.
40
+ * @returns {{readTags: object, embeddedExifTags: object|undefined, embeddedIptcTags: object|undefined, embeddedExifThumbnail: object|undefined, readTagsPromise: Promise<object[]>|undefined}}
41
+ * `readTags`, the two embedded groups and `embeddedExifThumbnail` come from
42
+ * uncompressed chunks and are ready at once; the embedded groups are undefined
43
+ * when no uncompressed raw profile was parsed, and `embeddedExifThumbnail` is
44
+ * the first thumbnail with an image. `readTagsPromise` resolves to one result
45
+ * per compressed chunk, in chunk order, each with its own
46
+ * `embeddedExifThumbnail` when it has one and no uncompressed chunk had one,
47
+ * and is undefined when none is read.
48
+ */
28
49
  function read(
29
50
  dataView,
30
51
  pngTextChunks,
@@ -32,87 +53,86 @@ function read(
32
53
  includeUnknown,
33
54
  computed = false,
34
55
  tagFilter = NOOP_TAG_FILTER,
35
- decompressConfig
56
+ decompressConfig,
57
+ valueBudget,
58
+ getThumbnail
36
59
  ) {
37
60
  const tags = {};
38
- const tagsPromises = [];
61
+ let embeddedExifTags;
62
+ let embeddedIptcTags;
63
+ let embeddedExifThumbnail;
64
+ const compressedChunks = [];
39
65
 
40
66
  for (let i = 0; i < pngTextChunks.length; i++) {
41
67
  const {offset, length, type} = pngTextChunks[i];
42
- const nameAndValue = getNameAndValue(dataView, offset, length, type, async, decompressConfig);
43
- if (nameAndValue instanceof Promise) {
44
- tagsPromises.push(nameAndValue.then(({name, value, description}) => {
45
- try {
46
- if (Constants.USE_EXIF && isExifGroupTag(name, value)) {
47
- if (!tagFilter.shouldParseGroup('exif')) {
48
- return {};
49
- }
50
- return {
51
- __exif: Tags.read(
52
- decodeRawData(value),
53
- EXIF_OFFSET,
54
- includeUnknown,
55
- computed,
56
- tagFilter
57
- ).tags
58
- };
59
- } else if (Constants.USE_IPTC && isIptcGroupTag(name, value)) {
60
- if (!tagFilter.shouldParseGroup('iptc')) {
61
- return {};
62
- }
63
- return {
64
- __iptc: IptcTags.read(
65
- decodeRawData(value),
66
- 0,
67
- includeUnknown,
68
- tagFilter
69
- )
70
- };
71
- } else if (name && !isExifGroupTag(name, value) && !isIptcGroupTag(name, value)) {
72
- if (!tagFilter.shouldParseGroup('png')) {
73
- return {};
74
- }
75
- return {
76
- [name]: {
77
- value,
78
- description
79
- }
80
- };
81
- }
82
- } catch (error) {
83
- // Ignore the broken tag.
68
+ const textChunk = parseTextChunk(dataView, offset, length, type);
69
+ if (!textChunk) {
70
+ continue;
71
+ }
72
+ if (textChunk.compressionMethod === COMPRESSION_METHOD_NONE) {
73
+ const tag = getUncompressedTag(textChunk);
74
+ if (isRawProfileTag(tag)) {
75
+ const rawProfileTags = getTagsFromTextTag(
76
+ tag,
77
+ includeUnknown,
78
+ computed,
79
+ tagFilter,
80
+ valueBudget,
81
+ embeddedExifThumbnail ? undefined : getThumbnail
82
+ );
83
+ if (rawProfileTags.embeddedExifTags) {
84
+ embeddedExifTags = objectAssign(embeddedExifTags || {}, rawProfileTags.embeddedExifTags);
84
85
  }
85
- return {};
86
- }));
87
- } else {
88
- const {name, value, description} = nameAndValue;
89
- if (name && tagFilter.shouldParseGroup('png')) {
90
- tags[name] = {
91
- value,
92
- description
93
- };
86
+ if (rawProfileTags.embeddedExifThumbnail) {
87
+ embeddedExifThumbnail = rawProfileTags.embeddedExifThumbnail;
88
+ }
89
+ if (rawProfileTags.embeddedIptcTags) {
90
+ embeddedIptcTags = objectAssign(embeddedIptcTags || {}, rawProfileTags.embeddedIptcTags);
91
+ }
92
+ } else if (tag.name && tagFilter.shouldParseGroup('png')) {
93
+ setProperty(tags, getTagKey(tag.name), {
94
+ value: tag.value,
95
+ description: tag.description
96
+ });
94
97
  }
98
+ } else if (async && compressedChunks.length < MAX_COMPRESSED_TEXT_CHUNKS) {
99
+ compressedChunks.push(textChunk);
95
100
  }
96
101
  }
97
102
 
103
+ const compressedThumbnailReader = embeddedExifThumbnail ? undefined : getThumbnail;
104
+ const decompressionTasks = compressedChunks.map((textChunk) => () => decompressTextChunk(textChunk, decompressConfig)
105
+ .then((tag) => getTagsFromTextTag(
106
+ tag,
107
+ includeUnknown,
108
+ computed,
109
+ tagFilter,
110
+ valueBudget,
111
+ compressedThumbnailReader,
112
+ true
113
+ )));
114
+
98
115
  return {
99
116
  readTags: tags,
100
- readTagsPromise: tagsPromises.length > 0 ? Promise.all(tagsPromises) : undefined
117
+ embeddedExifTags,
118
+ embeddedIptcTags,
119
+ embeddedExifThumbnail,
120
+ readTagsPromise: decompressionTasks.length > 0 ? runTasksInOrder(decompressionTasks, MAX_DECOMPRESSIONS_IN_FLIGHT) : undefined
101
121
  };
102
122
  }
103
123
 
104
- function getNameAndValue(dataView, offset, length, type, async, decompressConfig) {
124
+ function parseTextChunk(dataView, offset, length, type) {
105
125
  const keywordChars = [];
106
126
  const langChars = [];
107
- const translatedKeywordChars = [];
108
127
  let valueChars;
109
128
  let parsingState = STATE_KEYWORD;
110
129
  let compressionMethod = COMPRESSION_METHOD_NONE;
111
130
  const byteOffset = dataView.byteOffset || 0;
131
+ const chunkEnd = Math.min(offset + length, dataView.byteLength);
112
132
 
113
133
  for (let i = 0; i < length && offset + i < dataView.byteLength; i++) {
114
134
  if (parsingState === STATE_COMPRESSION) {
115
- compressionMethod = getCompressionMethod({type, dataView, offset: offset + i});
135
+ compressionMethod = getCompressionMethod({type, dataView, offset: offset + i, chunkEnd});
116
136
  if (type === TYPE_ITXT) {
117
137
  i += COMPRESSION_SECTION_ITXT_EXTRA_BYTE;
118
138
  }
@@ -129,29 +149,142 @@ function getNameAndValue(dataView, offset, length, type, async, decompressConfig
129
149
  if (byte === 0) {
130
150
  parsingState = moveToNextState(type, parsingState);
131
151
  } else if (parsingState === STATE_KEYWORD) {
152
+ if (keywordChars.length >= MAX_KEYWORD_LENGTH) {
153
+ return undefined;
154
+ }
132
155
  keywordChars.push(byte);
133
156
  } else if (parsingState === STATE_LANG) {
157
+ if (langChars.length >= MAX_LANGUAGE_TAG_LENGTH) {
158
+ return undefined;
159
+ }
134
160
  langChars.push(byte);
135
- } else if (parsingState === STATE_TRANSLATED_KEYWORD) {
136
- translatedKeywordChars.push(byte);
137
161
  }
138
162
  }
139
163
 
140
- if (compressionMethod !== COMPRESSION_METHOD_NONE && !async) {
141
- return {};
164
+ return {type, keywordChars, langChars, compressionMethod, valueChars};
165
+ }
166
+
167
+ function getUncompressedTag({type, keywordChars, langChars, valueChars}) {
168
+ const decodedValueChars = decompress(valueChars, COMPRESSION_METHOD_NONE, getEncodingFromType(type), 'string');
169
+ return constructTag(decodedValueChars, type, langChars, keywordChars);
170
+ }
171
+
172
+ function isRawProfileTag({name, value}) {
173
+ return isExifGroupTag(name, value) || isIptcGroupTag(name, value);
174
+ }
175
+
176
+ function getTagsFromTextTag({name, value, description}, includeUnknown, computed, tagFilter, valueBudget, getThumbnail, isDecompressed) {
177
+ try {
178
+ if (Constants.USE_EXIF && isExifGroupTag(name, value)) {
179
+ if (!tagFilter.shouldParseGroup('exif')) {
180
+ return {};
181
+ }
182
+ const exifDataView = decodeRawData(value);
183
+ if (isDecompressed) {
184
+ addDecompressedValueAllowance(valueBudget, exifDataView.byteLength);
185
+ }
186
+ const embeddedExifTags = Tags.read(
187
+ exifDataView,
188
+ EXIF_OFFSET,
189
+ includeUnknown,
190
+ computed,
191
+ tagFilter,
192
+ valueBudget
193
+ ).tags;
194
+ if (embeddedExifTags['MakerNote']) {
195
+ delete embeddedExifTags['MakerNote'].__offset;
196
+ }
197
+ return withEmbeddedExifThumbnail({embeddedExifTags}, exifDataView, getThumbnail);
198
+ } else if (Constants.USE_IPTC && isIptcGroupTag(name, value)) {
199
+ if (!tagFilter.shouldParseGroup('iptc')) {
200
+ return {};
201
+ }
202
+ const iptcDataView = decodeRawData(value);
203
+ if (isDecompressed) {
204
+ addDecompressedIptcAllowance(valueBudget, iptcDataView.byteLength);
205
+ }
206
+ return {
207
+ embeddedIptcTags: IptcTags.read(
208
+ iptcDataView,
209
+ 0,
210
+ includeUnknown,
211
+ tagFilter,
212
+ valueBudget
213
+ )
214
+ };
215
+ } else if (name && !isExifGroupTag(name, value) && !isIptcGroupTag(name, value)) {
216
+ if (!tagFilter.shouldParseGroup('png')) {
217
+ return {};
218
+ }
219
+ const readTags = {};
220
+ setProperty(readTags, getTagKey(name), {
221
+ value,
222
+ description
223
+ });
224
+ return {readTags};
225
+ }
226
+ } catch (error) {
227
+ // Ignore the broken tag.
142
228
  }
143
- const decompressedValueChars = decompress(valueChars, compressionMethod, getEncodingFromType(type), 'string', decompressConfig);
144
- if (decompressedValueChars instanceof Promise) {
145
- return decompressedValueChars
146
- .then((_decompressedValueChars) => constructTag(_decompressedValueChars, type, langChars, keywordChars))
147
- .catch(() => constructTag('<text using unknown compression>'.split(''), type, langChars, keywordChars));
229
+ return {};
230
+ }
231
+
232
+ function withEmbeddedExifThumbnail(result, exifDataView, getThumbnail) {
233
+ const thumbnailIfdTags = result.embeddedExifTags.Thumbnail;
234
+ if (!thumbnailIfdTags) {
235
+ return result;
236
+ }
237
+ delete result.embeddedExifTags.Thumbnail;
238
+
239
+ if (getThumbnail) {
240
+ const thumbnail = getThumbnail(exifDataView, thumbnailIfdTags, EXIF_OFFSET);
241
+ if (thumbnail.image) {
242
+ result.embeddedExifThumbnail = thumbnail;
243
+ }
244
+ }
245
+ return result;
246
+ }
247
+
248
+ function decompressTextChunk({type, keywordChars, langChars, compressionMethod, valueChars}, decompressConfig) {
249
+ if (valueChars === undefined) {
250
+ return Promise.resolve(constructPlaceholderTag(type, langChars, keywordChars));
148
251
  }
149
- return constructTag(decompressedValueChars, type, langChars, keywordChars);
252
+ return decompress(valueChars, compressionMethod, getEncodingFromType(type), 'string', decompressConfig)
253
+ .then((decompressedValueChars) => constructTag(decompressedValueChars, type, langChars, keywordChars))
254
+ .catch(() => constructPlaceholderTag(type, langChars, keywordChars));
150
255
  }
151
256
 
152
- function getCompressionMethod({type, dataView, offset}) {
257
+ function constructPlaceholderTag(type, langChars, keywordChars) {
258
+ return constructTag('<text using unknown compression>', type, langChars, keywordChars);
259
+ }
260
+
261
+ function runTasksInOrder(tasks, limit) {
262
+ const results = [];
263
+ const workers = [];
264
+ let nextIndex = 0;
265
+
266
+ for (let i = 0; i < Math.min(limit, tasks.length); i++) {
267
+ workers.push(runNext());
268
+ }
269
+ return Promise.all(workers).then(() => results);
270
+
271
+ function runNext() {
272
+ if (nextIndex >= tasks.length) {
273
+ return Promise.resolve();
274
+ }
275
+ const index = nextIndex++;
276
+ // The wrapper turns a synchronous throw from a task into a rejection
277
+ // that Promise.all observes, instead of an exception out of read().
278
+ return new Promise((resolve) => resolve(tasks[index]())).then((result) => {
279
+ results[index] = result;
280
+ return runNext();
281
+ });
282
+ }
283
+ }
284
+
285
+ function getCompressionMethod({type, dataView, offset, chunkEnd}) {
153
286
  if (type === TYPE_ITXT) {
154
- if (offset + 1 < dataView.byteLength && dataView.getUint8(offset) === COMPRESSION_FLAG_COMPRESSED) {
287
+ if (offset + 1 < chunkEnd && dataView.getUint8(offset) === COMPRESSION_FLAG_COMPRESSED) {
155
288
  return dataView.getUint8(offset + 1);
156
289
  }
157
290
  } else if (type === TYPE_ZTXT) {
@@ -222,14 +355,41 @@ function isIptcGroupTag(name, value) {
222
355
  }
223
356
 
224
357
  function decodeRawData(value) {
225
- const parts = value.match(/\n(exif|iptc)\n\s*\d+\n([\s\S]*)$/);
226
- return hexToDataView(parts[2].replace(/\n/g, ''));
358
+ const header = /\n(exif|iptc)\n\s*\d+\n/.exec(value);
359
+ if (!header) {
360
+ throw new Error('Invalid raw profile header.');
361
+ }
362
+ const dataStart = header.index + header[0].length;
363
+ const digitCount = countHexDigits(value, dataStart);
364
+ if (digitCount % 2 !== 0) {
365
+ throw new Error('Odd number of hex digits in raw profile.');
366
+ }
367
+
368
+ const bytes = new Uint8Array(digitCount / 2);
369
+ let byteIndex = 0;
370
+ let highNibble = -1;
371
+ for (let i = dataStart; i < value.length; i++) {
372
+ const nibble = getHexDigitValue(value.charCodeAt(i));
373
+ if (nibble < 0) {
374
+ // Newlines and any other non-hex character are skipped, as ImageMagick and exiv2 do.
375
+ continue;
376
+ }
377
+ if (highNibble < 0) {
378
+ highNibble = nibble;
379
+ } else {
380
+ bytes[byteIndex++] = (highNibble << 4) | nibble;
381
+ highNibble = -1;
382
+ }
383
+ }
384
+ return new DataView(bytes.buffer);
227
385
  }
228
386
 
229
- function hexToDataView(hex) {
230
- const dataView = new DataView(new ArrayBuffer(hex.length / 2));
231
- for (let i = 0; i < hex.length; i += 2) {
232
- dataView.setUint8(i / 2, parseInt(hex.substring(i, i + 2), 16));
387
+ function countHexDigits(value, start) {
388
+ let count = 0;
389
+ for (let i = start; i < value.length; i++) {
390
+ if (getHexDigitValue(value.charCodeAt(i)) >= 0) {
391
+ count++;
392
+ }
233
393
  }
234
- return dataView;
394
+ return count;
235
395
  }
@@ -3,7 +3,7 @@
3
3
  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
4
 
5
5
  import TextDecoder from './text-decoder.js';
6
- import {decodeUtf8ByteString} from './utils.js';
6
+ import {decodeUtf8ByteString, tryDecodeUtf8ByteString} from './utils.js';
7
7
 
8
8
  const TAG_HEADER_SIZE = 5;
9
9
 
@@ -43,16 +43,16 @@ function decode(encoding, tagValue) {
43
43
  }
44
44
 
45
45
  const stringValue = tagValue.map((charCode) => String.fromCharCode(charCode)).join('');
46
- try {
47
- return decodeURIComponent(escape(stringValue));
48
- } catch (error) {
49
- // The bytes are not valid UTF-8. Many real-world IPTC blocks have no
50
- // Coded Character Set tag but use Windows-1252 (en/em dashes,
51
- // ellipsis, smart quotes, etc.). Remap the bytes through CP1252 so
52
- // those characters render correctly instead of staying as C1 control
53
- // characters.
54
- return decodeWindows1252(tagValue);
46
+ const decoded = tryDecodeUtf8ByteString(stringValue);
47
+ if (decoded !== undefined) {
48
+ return decoded;
55
49
  }
50
+ // The bytes are not valid UTF-8. Many real-world IPTC blocks have no
51
+ // Coded Character Set tag but use Windows-1252 (en/em dashes,
52
+ // ellipsis, smart quotes, etc.). Remap the bytes through CP1252 so
53
+ // those characters render correctly instead of staying as C1 control
54
+ // characters.
55
+ return decodeWindows1252(tagValue);
56
56
  }
57
57
 
58
58
  function decodeWindows1252(tagValue) {
@@ -62,6 +62,8 @@ export const COMPOSITE_DEPENDENCY_TAGS = {
62
62
  'FocalPlaneYResolution',
63
63
  'FocalPlaneResolutionUnit',
64
64
  'FocalLengthIn35mmFilm',
65
+ 'PixelXDimension',
66
+ 'PixelYDimension',
65
67
  ],
66
68
  };
67
69
 
package/src/tag-filter.js CHANGED
@@ -6,6 +6,7 @@ import {
6
6
  FILTER_GROUPS,
7
7
  ID_CAPABLE_GROUPS,
8
8
  COMPOSITE_DEPENDENCY_TAGS,
9
+ EXIF_POINTER_TAGS,
9
10
  getExifTagDependenciesForInclude,
10
11
  } from './tag-filter-config.js';
11
12
 
@@ -181,6 +182,7 @@ function getExifIncludeDependencies(includeTags) {
181
182
 
182
183
  if (isGroupRequested(includeTags, 'composite')) {
183
184
  addRequiredTags(requiredTags, COMPOSITE_DEPENDENCY_TAGS.exif);
185
+ requiredTags[EXIF_POINTER_TAGS.exifIfdPointer] = true;
184
186
  }
185
187
 
186
188
  return requiredTags;
@@ -372,10 +372,22 @@ export default {
372
372
  };
373
373
 
374
374
  function decodeXPValue(value) {
375
+ if (!Array.isArray(value)) {
376
+ // A one-element value arrives as a bare number, which Uint8Array would take as a length.
377
+ return '';
378
+ }
375
379
  // The XP tags are encoded as UCS-2 which uses two bytes per character but
376
380
  // it's close to UTF-16 so we can use that to decode them.
377
381
  // https://www.loc.gov/preservation/digital/formats/content/tiff_tags.shtml
378
382
  const decodedValue = new TextDecoder('utf-16').decode(new Uint8Array(value));
379
383
  // Some softwares pad the string with null characters so we remove them.
380
- return decodedValue.replace(/\u0000+$/, ''); // eslint-disable-line no-control-regex
384
+ return removeTrailingNulls(decodedValue);
385
+ }
386
+
387
+ function removeTrailingNulls(text) {
388
+ let end = text.length;
389
+ while (end > 0 && text.charCodeAt(end - 1) === 0) {
390
+ end--;
391
+ }
392
+ return text.slice(0, end);
381
393
  }