exifreader 4.45.2 → 4.47.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
@@ -57,6 +57,92 @@ export function getStringValueFromArray(charArray) {
57
57
  return charArray.map((charCode) => String.fromCharCode(charCode)).join('');
58
58
  }
59
59
 
60
+ /**
61
+ * Decodes a byte string (one byte per character) as UTF-8. A string that is
62
+ * not valid UTF-8 is returned as it is, so single-byte encoded text stays
63
+ * readable.
64
+ * @param {string} byteString
65
+ * @returns {string}
66
+ */
67
+ export function decodeUtf8ByteString(byteString) {
68
+ const decoded = tryDecodeUtf8ByteString(byteString);
69
+ if (decoded === undefined) {
70
+ return byteString;
71
+ }
72
+ return decoded;
73
+ }
74
+
75
+ /**
76
+ * Decodes a byte string (one byte per character) as UTF-8.
77
+ * @param {string} byteString
78
+ * @returns {string | undefined} The decoded text, or undefined when the string
79
+ * is not valid UTF-8.
80
+ */
81
+ export function tryDecodeUtf8ByteString(byteString) {
82
+ let isAscii = true;
83
+ for (let index = 0; index < byteString.length;) {
84
+ const sequenceLength = getWellFormedUtf8SequenceLength(byteString, index);
85
+ if (sequenceLength === 0) {
86
+ return undefined;
87
+ }
88
+ if (sequenceLength > 1) {
89
+ isAscii = false;
90
+ }
91
+ index += sequenceLength;
92
+ }
93
+ if (isAscii) {
94
+ return byteString;
95
+ }
96
+ return decodeURIComponent(escape(byteString));
97
+ }
98
+
99
+ // Well-formed byte sequences per Unicode Table 3-7, which ECMAScript's Decode
100
+ // (decodeURIComponent) enforces. Returns 0 for an ill-formed or cut-off one.
101
+ function getWellFormedUtf8SequenceLength(byteString, index) {
102
+ const lead = byteString.charCodeAt(index);
103
+ if (lead < 0x80) {
104
+ return 1;
105
+ }
106
+
107
+ let length;
108
+ let secondMin = 0x80;
109
+ let secondMax = 0xbf;
110
+ if (lead >= 0xc2 && lead <= 0xdf) {
111
+ length = 2;
112
+ } else if (lead >= 0xe0 && lead <= 0xef) {
113
+ length = 3;
114
+ if (lead === 0xe0) {
115
+ secondMin = 0xa0;
116
+ } else if (lead === 0xed) {
117
+ secondMax = 0x9f;
118
+ }
119
+ } else if (lead >= 0xf0 && lead <= 0xf4) {
120
+ length = 4;
121
+ if (lead === 0xf0) {
122
+ secondMin = 0x90;
123
+ } else if (lead === 0xf4) {
124
+ secondMax = 0x8f;
125
+ }
126
+ } else {
127
+ return 0;
128
+ }
129
+
130
+ if (index + length > byteString.length) {
131
+ return 0;
132
+ }
133
+ const second = byteString.charCodeAt(index + 1);
134
+ if (!(second >= secondMin && second <= secondMax)) {
135
+ return 0;
136
+ }
137
+ for (let offset = 2; offset < length; offset++) {
138
+ const continuation = byteString.charCodeAt(index + offset);
139
+ if (!(continuation >= 0x80 && continuation <= 0xbf)) {
140
+ return 0;
141
+ }
142
+ }
143
+ return length;
144
+ }
145
+
60
146
  export function getCharacterArray(string) {
61
147
  return string.split('').map((character) => character.charCodeAt(0));
62
148
  }
@@ -183,17 +269,39 @@ export const COMPRESSION_METHOD_DEFLATE = 0;
183
269
  export const COMPRESSION_METHOD_BROTLI = 'brotli';
184
270
  export const DEFAULT_MAX_DECOMPRESSED_SIZE = 128 * 1024 * 1024;
185
271
 
272
+ /**
273
+ * Returns a copy of the decompress config whose compressed blocks share one
274
+ * total budget of maxDecompressedSize bytes. The input is never mutated.
275
+ *
276
+ * @param {Object|undefined} decompressConfig - The caller's decompress option.
277
+ * @returns {Object} {brotli, deflate, maxDecompressedSize, budget: {remaining, warned}}
278
+ */
279
+ export function withDecompressBudget(decompressConfig) {
280
+ return {
281
+ brotli: decompressConfig ? decompressConfig.brotli : undefined,
282
+ deflate: decompressConfig ? decompressConfig.deflate : undefined,
283
+ maxDecompressedSize: decompressConfig ? decompressConfig.maxDecompressedSize : undefined,
284
+ budget: {remaining: getMaxDecompressedSize(decompressConfig), warned: false}
285
+ };
286
+ }
287
+
186
288
  export function decompress(dataView, compressionMethod, encoding, returnType = 'string', decompressConfig) {
187
289
  const maxDecompressedSize = getMaxDecompressedSize(decompressConfig);
290
+ const budget = getDecompressBudget(decompressConfig, maxDecompressedSize);
291
+
292
+ if (compressionMethod !== COMPRESSION_METHOD_NONE && budget.remaining < 0) {
293
+ return rejectExceedsMax(budget, maxDecompressedSize);
294
+ }
188
295
 
189
296
  if (decompressConfig && compressionMethod !== COMPRESSION_METHOD_NONE) {
190
297
  const decompressType = compressionMethod === COMPRESSION_METHOD_DEFLATE ? 'deflate' : 'brotli';
191
298
  const customFn = decompressConfig[decompressType];
192
299
  if (typeof customFn === 'function') {
193
- const uint8 = new Uint8Array(dataView.buffer, dataView.byteOffset, dataView.byteLength);
194
- return Promise.resolve(customFn(uint8)).then((result) => {
195
- if (getResultByteLength(result) > maxDecompressedSize) {
196
- return rejectExceedsMax(maxDecompressedSize);
300
+ // Called now, not deferred: the input can view the caller's buffer, which may be reused once load() returns.
301
+ return new Promise((resolve) => resolve(customFn(getUint8View(dataView)))).then((result) => {
302
+ budget.remaining -= getResultByteLength(result);
303
+ if (budget.remaining < 0) {
304
+ return rejectExceedsMax(budget, maxDecompressedSize);
197
305
  }
198
306
  if (returnType === 'dataview') {
199
307
  if (result instanceof ArrayBuffer) {
@@ -214,7 +322,7 @@ export function decompress(dataView, compressionMethod, encoding, returnType = '
214
322
 
215
323
  if (compressionMethod === COMPRESSION_METHOD_DEFLATE) {
216
324
  if (typeof DecompressionStream === 'function') {
217
- return readBoundedDecompressedStream(dataView, 'deflate', maxDecompressedSize)
325
+ return readBoundedDecompressedStream(dataView, 'deflate', budget, maxDecompressedSize)
218
326
  .then((arrayBuffer) => decodeBuffer(arrayBuffer, returnType, encoding));
219
327
  }
220
328
  }
@@ -222,7 +330,7 @@ export function decompress(dataView, compressionMethod, encoding, returnType = '
222
330
  if (compressionMethod === COMPRESSION_METHOD_BROTLI) {
223
331
  if (typeof DecompressionStream === 'function') {
224
332
  try {
225
- return readBoundedDecompressedStream(dataView, 'brotli', maxDecompressedSize)
333
+ return readBoundedDecompressedStream(dataView, 'brotli', budget, maxDecompressedSize)
226
334
  .then((arrayBuffer) => decodeBuffer(arrayBuffer, returnType, encoding));
227
335
  } catch (_error) {
228
336
  // brotli not supported by this DecompressionStream implementation
@@ -254,7 +362,18 @@ function getMaxDecompressedSize(decompressConfig) {
254
362
  return DEFAULT_MAX_DECOMPRESSED_SIZE;
255
363
  }
256
364
 
257
- function readBoundedDecompressedStream(dataView, format, maxDecompressedSize) {
365
+ function getDecompressBudget(decompressConfig, maxDecompressedSize) {
366
+ if (decompressConfig && decompressConfig.budget) {
367
+ return decompressConfig.budget;
368
+ }
369
+ return {remaining: maxDecompressedSize, warned: false};
370
+ }
371
+
372
+ function getUint8View(dataView) {
373
+ return new Uint8Array(dataView.buffer, dataView.byteOffset, dataView.byteLength);
374
+ }
375
+
376
+ function readBoundedDecompressedStream(dataView, format, budget, maxDecompressedSize) {
258
377
  const decompressionStream = new DecompressionStream(format);
259
378
  const decompressedStream = new Blob([dataView]).stream().pipeThrough(decompressionStream);
260
379
  const reader = decompressedStream.getReader();
@@ -269,8 +388,11 @@ function readBoundedDecompressedStream(dataView, format, maxDecompressedSize) {
269
388
  return concatChunks(chunks, total);
270
389
  }
271
390
  total += value.byteLength;
272
- if (total > maxDecompressedSize) {
273
- return reader.cancel().then(() => rejectExceedsMax(maxDecompressedSize));
391
+ // Charge before checking and never give bytes back, so an
392
+ // exceeded budget stays exhausted for every later block.
393
+ budget.remaining -= value.byteLength;
394
+ if (budget.remaining < 0) {
395
+ return reader.cancel().then(() => rejectExceedsMax(budget, maxDecompressedSize));
274
396
  }
275
397
  chunks.push(value);
276
398
  return pump();
@@ -302,10 +424,11 @@ function getResultByteLength(result) {
302
424
  return 0;
303
425
  }
304
426
 
305
- function rejectExceedsMax(maxDecompressedSize) {
306
- if (typeof console !== 'undefined' && typeof console.warn === 'function') { // eslint-disable-line no-console
427
+ function rejectExceedsMax(budget, maxDecompressedSize) {
428
+ if (!budget.warned && typeof console !== 'undefined' && typeof console.warn === 'function') { // eslint-disable-line no-console
429
+ budget.warned = true;
307
430
  // eslint-disable-next-line no-console
308
- console.warn(`ExifReader: skipped a compressed metadata block that would exceed the maximum decompressed size of ${maxDecompressedSize} bytes.`);
431
+ console.warn(`ExifReader: the total decompressed metadata size for this call would exceed the maximum of ${maxDecompressedSize} bytes, so this and any later compressed metadata blocks are skipped.`);
309
432
  }
310
- return Promise.reject(`Decompressed metadata exceeded the maximum allowed size of ${maxDecompressedSize} bytes.`);
433
+ return Promise.reject(`Decompressed metadata exceeded the maximum allowed total size of ${maxDecompressedSize} bytes.`);
311
434
  }
@@ -2,24 +2,39 @@
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
+ const CHROME_MESSAGE_START = 'Namespace prefix ';
6
+ const CHROME_MESSAGE_END = ' is not defined';
7
+
5
8
  export function isMissingNamespaceError(error) {
9
+ const message = String(error.message);
6
10
  const missingNamespaceStrings = [
7
11
  // @xmldom/xmldom
8
12
  'prefix is non-null and namespace is null',
9
13
  // Firefox
10
14
  'prefix not bound to a namespace', // en
11
- 'prefix inte bundet till en namnrymd', // sv
12
- // Chrome
13
- /Namespace prefix .+ is not defined/
15
+ 'prefix inte bundet till en namnrymd' // sv
14
16
  ];
15
17
 
16
18
  for (let i = 0; i < missingNamespaceStrings.length; i++) {
17
- const regexp = new RegExp(missingNamespaceStrings[i]);
18
- if (regexp.test(error.message)) {
19
+ if (message.indexOf(missingNamespaceStrings[i]) !== -1) {
19
20
  return true;
20
21
  }
21
22
  }
22
23
 
24
+ return isChromeMissingNamespaceMessage(message);
25
+ }
26
+
27
+ // Same result as the former Chrome check /Namespace prefix .+ is not defined/,
28
+ // in linear time.
29
+ function isChromeMissingNamespaceMessage(message) {
30
+ const lines = message.split(/[\n\r\u2028\u2029]/);
31
+ for (let i = 0; i < lines.length; i++) {
32
+ const prefixIndex = lines[i].indexOf(CHROME_MESSAGE_START);
33
+ if (prefixIndex !== -1
34
+ && lines[i].lastIndexOf(CHROME_MESSAGE_END) > prefixIndex + CHROME_MESSAGE_START.length) {
35
+ return true;
36
+ }
37
+ }
23
38
  return false;
24
39
  }
25
40