exifreader 4.48.0 → 4.49.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.
package/src/utils.js CHANGED
@@ -63,14 +63,41 @@ export function getPascalStringFromDataView(dataView, offset) {
63
63
  return [size, string];
64
64
  }
65
65
 
66
- export function getStringValueFromArray(charArray) {
67
- return charArray.map((charCode) => String.fromCharCode(charCode)).join('');
68
- }
69
-
70
66
  // Engines cap the number of arguments Function.prototype.apply can pass, and
71
67
  // some older ones reject a typed array there, so bytes go in as plain chunks.
72
68
  const MAX_CHARS_PER_CALL = 8192;
73
69
 
70
+ /**
71
+ * Converts character codes to a string, one character per element. Like
72
+ * `map`, it skips the holes of a sparse array.
73
+ * @param {Array} charArray
74
+ * @returns {string}
75
+ * @throws {TypeError} When `charArray` is not an array.
76
+ */
77
+ export function getStringValueFromArray(charArray) {
78
+ if (!Array.isArray(charArray)) {
79
+ throw new TypeError('Expected an array.');
80
+ }
81
+
82
+ const chunks = [];
83
+ // Overwritten by index, not emptied, so that its storage is reused across
84
+ // chunks. Emptying it measured about 20 MB more peak memory for 8 MiB.
85
+ const charCodes = [];
86
+ let count = 0;
87
+ for (let i = 0; i < charArray.length; i++) {
88
+ if (i in charArray) {
89
+ charCodes[count++] = charArray[i];
90
+ if (count === MAX_CHARS_PER_CALL) {
91
+ chunks.push(String.fromCharCode.apply(null, charCodes));
92
+ count = 0;
93
+ }
94
+ }
95
+ }
96
+ charCodes.length = count;
97
+ chunks.push(String.fromCharCode.apply(null, charCodes));
98
+ return chunks.join('');
99
+ }
100
+
74
101
  /**
75
102
  * Converts bytes to a string with one character per byte.
76
103
  * @param {Uint8Array|number[]} bytes
@@ -101,6 +128,41 @@ function getChunkString(bytes, start, end, charCodes) {
101
128
  return String.fromCharCode.apply(null, charCodes);
102
129
  }
103
130
 
131
+ // Smaller chunks measured a higher peak memory for 8 MiB values.
132
+ export const JOIN_CHUNK_SIZE = 65536;
133
+
134
+ /**
135
+ * Returns what `values.map(getText).join(separator)` returns, without building
136
+ * an array as long as `values`. Unlike `map`, a hole in a sparse array reaches
137
+ * `getText` as `undefined`.
138
+ * @param {Array} values
139
+ * @param {function(*): (string|undefined|null)} getText Called with the
140
+ * element only, in index order.
141
+ * @param {string} separator
142
+ * @returns {string}
143
+ * @throws {TypeError} When `values` is not an array.
144
+ */
145
+ export function joinMapped(values, getText, separator) {
146
+ if (!Array.isArray(values)) {
147
+ throw new TypeError('Expected an array.');
148
+ }
149
+
150
+ const texts = [];
151
+ const chunks = [];
152
+ for (let chunkStart = 0; chunkStart < values.length; chunkStart += JOIN_CHUNK_SIZE) {
153
+ chunks.push(getJoinedChunk(values, chunkStart, Math.min(chunkStart + JOIN_CHUNK_SIZE, values.length), getText, separator, texts));
154
+ }
155
+ return chunks.join(separator);
156
+ }
157
+
158
+ function getJoinedChunk(values, start, end, getText, separator, texts) {
159
+ for (let i = start; i < end; i++) {
160
+ texts[i - start] = getText(values[i]);
161
+ }
162
+ texts.length = end - start;
163
+ return texts.join(separator);
164
+ }
165
+
104
166
  /**
105
167
  * Decodes a byte string (one byte per character) as UTF-8. A string that is
106
168
  * not valid UTF-8 is returned as it is, so single-byte encoded text stays
@@ -191,9 +253,29 @@ export function getCharacterArray(string) {
191
253
  return string.split('').map((character) => character.charCodeAt(0));
192
254
  }
193
255
 
256
+ // Guards against memory exhaustion from a crafted file. A real JPEG has at most 255 ICC
257
+ // segments, ExifReader reads at most 1024 XMP ones, and other containers are few.
258
+ export const MAX_METADATA_BLOCKS = 4096;
259
+
260
+ /**
261
+ * Records one metadata block. The list holds at most MAX_METADATA_BLOCKS
262
+ * entries; past that, the last entry is widened to cover each further block
263
+ * and keeps its own type. Does nothing without a list or a type.
264
+ * @param {Array<{type: string, start: number, end: number}>|undefined} metadataBlocks
265
+ * @param {string|undefined} type
266
+ * @param {number} start
267
+ * @param {number} end Exclusive.
268
+ */
194
269
  export function pushMetadataBlock(metadataBlocks, type, start, end) {
195
- if (metadataBlocks && type) {
270
+ if (!metadataBlocks || !type) {
271
+ return;
272
+ }
273
+ if (metadataBlocks.length < MAX_METADATA_BLOCKS) {
196
274
  metadataBlocks.push({type, start, end});
275
+ } else {
276
+ const lastBlock = metadataBlocks[metadataBlocks.length - 1];
277
+ lastBlock.start = Math.min(lastBlock.start, start);
278
+ lastBlock.end = Math.max(lastBlock.end, end);
197
279
  }
198
280
  }
199
281
 
@@ -267,23 +349,27 @@ export function deferInit(object, key, initializer) {
267
349
  }
268
350
 
269
351
  export function getBase64Image(image) {
352
+ if (typeof image === 'string' && typeof btoa !== 'undefined') {
353
+ // Only tests pass a string. btoa reads it as Latin-1, Buffer.from would use UTF-8.
354
+ return btoa(image);
355
+ }
356
+ if (hasBufferFrom()) {
357
+ return Buffer.from(image).toString('base64'); // eslint-disable-line no-undef
358
+ }
270
359
  if (typeof btoa !== 'undefined') {
271
- if (typeof image === 'string') {
272
- // This only happens during the build tests using Node 16+ (npm run test:build).
273
- return btoa(image);
274
- }
275
- // IE11- does not implement reduce on the Uint8Array prototype.
276
- return btoa(Array.prototype.reduce.call(new Uint8Array(image), (data, byte) => data + String.fromCharCode(byte), ''));
360
+ return btoa(getByteString(new Uint8Array(image)));
277
361
  }
278
362
  if (typeof Buffer === 'undefined') {
279
363
  return undefined;
280
364
  }
281
- if (typeof Buffer.from !== 'undefined') { // eslint-disable-line no-undef
282
- return Buffer.from(image).toString('base64'); // eslint-disable-line no-undef
283
- }
284
365
  return (new Buffer(image)).toString('base64'); // eslint-disable-line no-undef
285
366
  }
286
367
 
368
+ function hasBufferFrom() {
369
+ // Buffer polyfills before buffer@4.6.0 inherit Uint8Array.from, which returns an empty array for an ArrayBuffer.
370
+ return typeof Buffer !== 'undefined' && typeof Buffer.from !== 'undefined' && Buffer.from !== Uint8Array.from; // eslint-disable-line no-undef
371
+ }
372
+
287
373
  export function dataUriToBuffer(dataUri) {
288
374
  const commaIndex = dataUri.indexOf(',');
289
375
  const header = dataUri.substring(0, commaIndex);
@@ -472,8 +558,8 @@ export function decompress(dataView, compressionMethod, encoding, returnType = '
472
558
  try {
473
559
  return new TextDecoder(encoding).decode(dataView);
474
560
  } catch (error) {
475
- const bytes = new Uint8Array(dataView.buffer, dataView.byteOffset, dataView.byteLength);
476
- return Array.from(bytes, (byte) => String.fromCharCode(byte)).join('');
561
+ // Like TextDecoder's decode(), read a missing view as no bytes.
562
+ return dataView ? getByteString(getUint8View(dataView)) : '';
477
563
  }
478
564
  }
479
565
  return dataView;
@@ -44,7 +44,7 @@ export default {
44
44
  'exif:ColorSpace': (value) => TagNamesCommon.ColorSpace(parseNumber(value)),
45
45
  'exif:ComponentsConfiguration'(value, description) {
46
46
  if (/^\d, \d, \d, \d$/.test(description)) {
47
- const numbers = description.split(', ').map((number) => number.charCodeAt(0));
47
+ const numbers = description.split(', ').map((number) => parseInt(number, 10));
48
48
  return TagNamesCommon.ComponentsConfiguration(numbers);
49
49
  }
50
50
  return description;
package/src/xmp-tags.js CHANGED
@@ -132,7 +132,7 @@ function readTags(tags, chunkDataView, domParser) {
132
132
  delete xmpTags._raw;
133
133
  const names = Object.keys(xmpTags);
134
134
  for (let i = 0; i < names.length; i++) {
135
- setProperty(tags, getTagKey(names[i]), xmpTags[names[i]]);
135
+ setProperty(tags, names[i], xmpTags[names[i]]);
136
136
  }
137
137
  return true;
138
138
  } catch (error) {
@@ -335,7 +335,9 @@ function getElementFromNode(node, decodeValue) {
335
335
 
336
336
  function getAttributes(element, decodeValue) {
337
337
  const elementAttributes = element.attributes;
338
- const attributes = {};
338
+ // A qualifier with element content returns these unchanged in its element
339
+ // tree, so like the elements object they must not have a prototype.
340
+ const attributes = Object.create(null);
339
341
 
340
342
  for (let i = 0; i < elementAttributes.length; i++) {
341
343
  setProperty(attributes, elementAttributes[i].nodeName, decodeValue(elementAttributes[i].value));
@@ -382,8 +384,9 @@ function isNamespaceDefinition(name) {
382
384
  }
383
385
 
384
386
  function setParsedTag(tags, plainDescriptions, name, parsedTag) {
385
- setProperty(tags, name, parsedTag.tag);
386
- setProperty(plainDescriptions, name, parsedTag.plainDescription);
387
+ const key = getTagKey(name);
388
+ setProperty(tags, key, parsedTag.tag);
389
+ setProperty(plainDescriptions, key, parsedTag.plainDescription);
387
390
  }
388
391
 
389
392
  function getLocalName(name) {
@@ -531,7 +534,7 @@ function parseNodeAttributes(node) {
531
534
 
532
535
  for (const name in node.attributes) {
533
536
  if ((name !== 'rdf:parseType') && (name !== 'rdf:resource') && (!isNamespaceDefinition(name))) {
534
- setProperty(attributes, getLocalName(name), node.attributes[name]);
537
+ setProperty(attributes, getTagKey(getLocalName(name)), node.attributes[name]);
535
538
  }
536
539
  }
537
540
 
@@ -547,7 +550,7 @@ function parseNodeChildrenAsAttributes(node) {
547
550
 
548
551
  for (const name in node.value) {
549
552
  if ((name !== 'rdf:value') && (!isNamespaceDefinition(name))) {
550
- setProperty(attributes, getLocalName(name), node.value[name].value);
553
+ setProperty(attributes, getTagKey(getLocalName(name)), node.value[name].value);
551
554
  }
552
555
  }
553
556