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.
- package/README.md +100 -34
- package/bin/TagFilterPlugin.js +23 -14
- package/bin/analyze.js +345 -0
- package/bin/build.js +13 -2
- package/bin/cli.js +449 -23
- package/bin/custom-build-marker.js +78 -0
- package/bin/unshare-file.js +29 -0
- package/bin/verify-build.js +147 -0
- package/dist/exif-reader.js +1 -1
- package/dist/exif-reader.js.map +1 -1
- package/package.json +3 -3
- package/src/canon-tags.js +4 -2
- package/src/composite.js +94 -47
- package/src/exif-reader.js +81 -16
- package/src/file-loaders.js +204 -30
- package/src/image-header-iso-bmff-iloc.js +5 -2
- package/src/image-header-iso-bmff.js +41 -10
- package/src/image-header-jpeg.js +9 -2
- package/src/image-header-png.js +33 -21
- package/src/iptc-tags.js +46 -11
- package/src/load-auto.js +6 -3
- package/src/loadview-pipeline.js +111 -61
- package/src/mpf-tags.js +10 -3
- package/src/pentax-tags.js +49 -23
- package/src/photoshop-tag-names.js +6 -2
- package/src/photoshop-tags.js +15 -22
- package/src/png-text-tags.js +237 -77
- package/src/tag-decoder.js +10 -10
- package/src/tag-filter-config.js +2 -0
- package/src/tag-filter.js +2 -0
- package/src/tag-names-0th-ifd.js +13 -1
- package/src/tags-helpers.js +220 -44
- package/src/tags.js +22 -4
- package/src/types.js +0 -5
- package/src/utils.js +260 -32
- package/src/xml.js +19 -2
- package/src/xmp-element-depth.js +226 -0
- package/src/xmp-namespaces.js +22 -7
- package/src/xmp-tags.js +285 -165
package/src/utils.js
CHANGED
|
@@ -20,10 +20,20 @@ export function getStringFromDataView(dataView, offset, length) {
|
|
|
20
20
|
return getStringValueFromArray(chars);
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
-
|
|
23
|
+
/**
|
|
24
|
+
* Reads a one-byte-per-character string up to its NUL terminator.
|
|
25
|
+
* @param {DataView} dataView
|
|
26
|
+
* @param {number} offset - Where the string starts.
|
|
27
|
+
* @param {number} [end] - Exclusive end of the read, clamped to the buffer
|
|
28
|
+
* end. Defaults to the buffer end.
|
|
29
|
+
* @returns {string} The bytes before the first NUL or before the end,
|
|
30
|
+
* whichever comes first.
|
|
31
|
+
*/
|
|
32
|
+
export function getNullTerminatedStringFromDataView(dataView, offset, end = dataView.byteLength) {
|
|
33
|
+
const readEnd = Math.min(end, dataView.byteLength);
|
|
24
34
|
const chars = [];
|
|
25
35
|
let i = 0;
|
|
26
|
-
while (offset + i <
|
|
36
|
+
while (offset + i < readEnd) {
|
|
27
37
|
const char = dataView.getUint8(offset + i);
|
|
28
38
|
if (char === 0) {
|
|
29
39
|
break;
|
|
@@ -57,6 +67,40 @@ export function getStringValueFromArray(charArray) {
|
|
|
57
67
|
return charArray.map((charCode) => String.fromCharCode(charCode)).join('');
|
|
58
68
|
}
|
|
59
69
|
|
|
70
|
+
// Engines cap the number of arguments Function.prototype.apply can pass, and
|
|
71
|
+
// some older ones reject a typed array there, so bytes go in as plain chunks.
|
|
72
|
+
const MAX_CHARS_PER_CALL = 8192;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Converts bytes to a string with one character per byte.
|
|
76
|
+
* @param {Uint8Array|number[]} bytes
|
|
77
|
+
* @param {number} [start=0]
|
|
78
|
+
* @param {number} [end=bytes.length] Exclusive.
|
|
79
|
+
* @param {number[]} [charCodes=[]] Scratch array. A caller that converts many
|
|
80
|
+
* short strings passes the same one so that each string does not allocate
|
|
81
|
+
* its own.
|
|
82
|
+
* @returns {string}
|
|
83
|
+
*/
|
|
84
|
+
export function getByteString(bytes, start = 0, end = bytes.length, charCodes = []) {
|
|
85
|
+
if (end - start <= MAX_CHARS_PER_CALL) {
|
|
86
|
+
return getChunkString(bytes, start, end, charCodes);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const chunks = [];
|
|
90
|
+
for (let chunkStart = start; chunkStart < end; chunkStart += MAX_CHARS_PER_CALL) {
|
|
91
|
+
chunks.push(getChunkString(bytes, chunkStart, Math.min(chunkStart + MAX_CHARS_PER_CALL, end), charCodes));
|
|
92
|
+
}
|
|
93
|
+
return chunks.join('');
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function getChunkString(bytes, start, end, charCodes) {
|
|
97
|
+
for (let i = start; i < end; i++) {
|
|
98
|
+
charCodes[i - start] = bytes[i];
|
|
99
|
+
}
|
|
100
|
+
charCodes.length = end - start;
|
|
101
|
+
return String.fromCharCode.apply(null, charCodes);
|
|
102
|
+
}
|
|
103
|
+
|
|
60
104
|
/**
|
|
61
105
|
* Decodes a byte string (one byte per character) as UTF-8. A string that is
|
|
62
106
|
* not valid UTF-8 is returned as it is, so single-byte encoded text stays
|
|
@@ -79,11 +123,68 @@ export function decodeUtf8ByteString(byteString) {
|
|
|
79
123
|
* is not valid UTF-8.
|
|
80
124
|
*/
|
|
81
125
|
export function tryDecodeUtf8ByteString(byteString) {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
126
|
+
let isAscii = true;
|
|
127
|
+
for (let index = 0; index < byteString.length;) {
|
|
128
|
+
const sequenceLength = getWellFormedUtf8SequenceLength(byteString, index);
|
|
129
|
+
if (sequenceLength === 0) {
|
|
130
|
+
return undefined;
|
|
131
|
+
}
|
|
132
|
+
if (sequenceLength > 1) {
|
|
133
|
+
isAscii = false;
|
|
134
|
+
}
|
|
135
|
+
index += sequenceLength;
|
|
86
136
|
}
|
|
137
|
+
if (isAscii) {
|
|
138
|
+
return byteString;
|
|
139
|
+
}
|
|
140
|
+
return decodeURIComponent(escape(byteString));
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// Well-formed byte sequences per Unicode Table 3-7, which ECMAScript's Decode
|
|
144
|
+
// (decodeURIComponent) enforces. Returns 0 for an ill-formed or cut-off one.
|
|
145
|
+
function getWellFormedUtf8SequenceLength(byteString, index) {
|
|
146
|
+
const lead = byteString.charCodeAt(index);
|
|
147
|
+
if (lead < 0x80) {
|
|
148
|
+
return 1;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
let length;
|
|
152
|
+
let secondMin = 0x80;
|
|
153
|
+
let secondMax = 0xbf;
|
|
154
|
+
if (lead >= 0xc2 && lead <= 0xdf) {
|
|
155
|
+
length = 2;
|
|
156
|
+
} else if (lead >= 0xe0 && lead <= 0xef) {
|
|
157
|
+
length = 3;
|
|
158
|
+
if (lead === 0xe0) {
|
|
159
|
+
secondMin = 0xa0;
|
|
160
|
+
} else if (lead === 0xed) {
|
|
161
|
+
secondMax = 0x9f;
|
|
162
|
+
}
|
|
163
|
+
} else if (lead >= 0xf0 && lead <= 0xf4) {
|
|
164
|
+
length = 4;
|
|
165
|
+
if (lead === 0xf0) {
|
|
166
|
+
secondMin = 0x90;
|
|
167
|
+
} else if (lead === 0xf4) {
|
|
168
|
+
secondMax = 0x8f;
|
|
169
|
+
}
|
|
170
|
+
} else {
|
|
171
|
+
return 0;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
if (index + length > byteString.length) {
|
|
175
|
+
return 0;
|
|
176
|
+
}
|
|
177
|
+
const second = byteString.charCodeAt(index + 1);
|
|
178
|
+
if (!(second >= secondMin && second <= secondMax)) {
|
|
179
|
+
return 0;
|
|
180
|
+
}
|
|
181
|
+
for (let offset = 2; offset < length; offset++) {
|
|
182
|
+
const continuation = byteString.charCodeAt(index + offset);
|
|
183
|
+
if (!(continuation >= 0x80 && continuation <= 0xbf)) {
|
|
184
|
+
return 0;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
return length;
|
|
87
188
|
}
|
|
88
189
|
|
|
89
190
|
export function getCharacterArray(string) {
|
|
@@ -129,6 +230,22 @@ export function setProperty(object, key, value) {
|
|
|
129
230
|
object[key] = value;
|
|
130
231
|
}
|
|
131
232
|
|
|
233
|
+
/**
|
|
234
|
+
* Returns the key to store a tag under whose name comes from an image. A name
|
|
235
|
+
* that is an `Object.prototype` property, such as `hasOwnProperty`, would hide
|
|
236
|
+
* that method on the object holding it, so it gets `_` appended. `__proto__`
|
|
237
|
+
* is kept, since `setProperty` stores it as an own property.
|
|
238
|
+
*
|
|
239
|
+
* @param {string} name The tag name from the image.
|
|
240
|
+
* @returns {string} The name, with `_` appended if it would hide an inherited property.
|
|
241
|
+
*/
|
|
242
|
+
export function getTagKey(name) {
|
|
243
|
+
if (name !== '__proto__' && Object.prototype.hasOwnProperty.call(Object.prototype, name)) {
|
|
244
|
+
return name + '_';
|
|
245
|
+
}
|
|
246
|
+
return name;
|
|
247
|
+
}
|
|
248
|
+
|
|
132
249
|
export function deferInit(object, key, initializer) {
|
|
133
250
|
let initialized = false;
|
|
134
251
|
Object.defineProperty(object, key, {
|
|
@@ -168,29 +285,93 @@ export function getBase64Image(image) {
|
|
|
168
285
|
}
|
|
169
286
|
|
|
170
287
|
export function dataUriToBuffer(dataUri) {
|
|
171
|
-
const
|
|
288
|
+
const commaIndex = dataUri.indexOf(',');
|
|
289
|
+
const header = dataUri.substring(0, commaIndex);
|
|
290
|
+
const data = dataUri.substring(commaIndex + 1);
|
|
172
291
|
|
|
173
|
-
if (
|
|
292
|
+
if (header.indexOf(';base64') !== -1) {
|
|
293
|
+
const base64 = percentDecodeBase64Payload(data);
|
|
174
294
|
if (typeof atob !== 'undefined') {
|
|
175
|
-
return
|
|
295
|
+
return binaryStringToArrayBuffer(atob(base64));
|
|
176
296
|
}
|
|
177
297
|
if (typeof Buffer === 'undefined') {
|
|
178
298
|
return undefined;
|
|
179
299
|
}
|
|
180
300
|
if (typeof Buffer.from !== 'undefined') { // eslint-disable-line no-undef
|
|
181
|
-
return Buffer.from(
|
|
301
|
+
return Buffer.from(base64, 'base64'); // eslint-disable-line no-undef
|
|
182
302
|
}
|
|
183
|
-
return new Buffer(
|
|
303
|
+
return new Buffer(base64, 'base64'); // eslint-disable-line no-undef
|
|
184
304
|
}
|
|
185
305
|
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
306
|
+
return percentDecodeToBytes(data).buffer;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
// WHATWG data: URL processing percent-decodes the body before the forgiving-base64 decode.
|
|
310
|
+
// A non-ASCII payload can never be valid base64, so it goes to the decoder unchanged.
|
|
311
|
+
function percentDecodeBase64Payload(data) {
|
|
312
|
+
if (data.indexOf('%') === -1 || /[\u0080-\uffff]/.test(data)) {
|
|
313
|
+
return data;
|
|
314
|
+
}
|
|
315
|
+
return getByteString(percentDecodeToBytes(data));
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
function binaryStringToArrayBuffer(string) {
|
|
319
|
+
const bytes = new Uint8Array(string.length);
|
|
320
|
+
for (let i = 0; i < string.length; i++) {
|
|
321
|
+
bytes[i] = string.charCodeAt(i);
|
|
322
|
+
}
|
|
323
|
+
return bytes.buffer;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
// WHATWG URL "string percent-decode" (used for data: URLs, RFC 2397): UTF-8 encode, then each %XX is one byte.
|
|
327
|
+
function percentDecodeToBytes(string) {
|
|
328
|
+
const byteString = /[\u0080-\uffff]/.test(string) ? unescape(encodeURIComponent(string)) : string;
|
|
329
|
+
const bytes = new Uint8Array(byteString.length - 2 * countPercentEscapes(byteString));
|
|
330
|
+
let byteIndex = 0;
|
|
331
|
+
for (let i = 0; i < byteString.length; i++) {
|
|
332
|
+
if (isPercentEscape(byteString, i)) {
|
|
333
|
+
bytes[byteIndex++] = getHexDigitValue(byteString.charCodeAt(i + 1)) * 16 + getHexDigitValue(byteString.charCodeAt(i + 2));
|
|
334
|
+
i += 2;
|
|
335
|
+
} else {
|
|
336
|
+
bytes[byteIndex++] = byteString.charCodeAt(i);
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
return bytes;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
function countPercentEscapes(byteString) {
|
|
343
|
+
let count = 0;
|
|
344
|
+
for (let i = 0; i < byteString.length; i++) {
|
|
345
|
+
if (isPercentEscape(byteString, i)) {
|
|
346
|
+
count++;
|
|
347
|
+
i += 2;
|
|
190
348
|
}
|
|
191
|
-
return new Buffer(decodedData); // eslint-disable-line no-undef
|
|
192
349
|
}
|
|
193
|
-
return
|
|
350
|
+
return count;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
function isPercentEscape(byteString, index) {
|
|
354
|
+
return byteString.charCodeAt(index) === 0x25
|
|
355
|
+
&& index + 2 < byteString.length
|
|
356
|
+
&& getHexDigitValue(byteString.charCodeAt(index + 1)) !== -1
|
|
357
|
+
&& getHexDigitValue(byteString.charCodeAt(index + 2)) !== -1;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/**
|
|
361
|
+
* @param {number} charCode
|
|
362
|
+
* @returns {number} The value of the hex digit, or -1 for any other character.
|
|
363
|
+
*/
|
|
364
|
+
export function getHexDigitValue(charCode) {
|
|
365
|
+
if (charCode >= 0x30 && charCode <= 0x39) {
|
|
366
|
+
return charCode - 0x30;
|
|
367
|
+
}
|
|
368
|
+
if (charCode >= 0x41 && charCode <= 0x46) {
|
|
369
|
+
return charCode - 0x41 + 10;
|
|
370
|
+
}
|
|
371
|
+
if (charCode >= 0x61 && charCode <= 0x66) {
|
|
372
|
+
return charCode - 0x61 + 10;
|
|
373
|
+
}
|
|
374
|
+
return -1;
|
|
194
375
|
}
|
|
195
376
|
|
|
196
377
|
export function padStart(string, length, character) {
|
|
@@ -212,17 +393,39 @@ export const COMPRESSION_METHOD_DEFLATE = 0;
|
|
|
212
393
|
export const COMPRESSION_METHOD_BROTLI = 'brotli';
|
|
213
394
|
export const DEFAULT_MAX_DECOMPRESSED_SIZE = 128 * 1024 * 1024;
|
|
214
395
|
|
|
396
|
+
/**
|
|
397
|
+
* Returns a copy of the decompress config whose compressed blocks share one
|
|
398
|
+
* total budget of maxDecompressedSize bytes. The input is never mutated.
|
|
399
|
+
*
|
|
400
|
+
* @param {Object|undefined} decompressConfig - The caller's decompress option.
|
|
401
|
+
* @returns {Object} {brotli, deflate, maxDecompressedSize, budget: {remaining, warned}}
|
|
402
|
+
*/
|
|
403
|
+
export function withDecompressBudget(decompressConfig) {
|
|
404
|
+
return {
|
|
405
|
+
brotli: decompressConfig ? decompressConfig.brotli : undefined,
|
|
406
|
+
deflate: decompressConfig ? decompressConfig.deflate : undefined,
|
|
407
|
+
maxDecompressedSize: decompressConfig ? decompressConfig.maxDecompressedSize : undefined,
|
|
408
|
+
budget: {remaining: getMaxDecompressedSize(decompressConfig), warned: false}
|
|
409
|
+
};
|
|
410
|
+
}
|
|
411
|
+
|
|
215
412
|
export function decompress(dataView, compressionMethod, encoding, returnType = 'string', decompressConfig) {
|
|
216
413
|
const maxDecompressedSize = getMaxDecompressedSize(decompressConfig);
|
|
414
|
+
const budget = getDecompressBudget(decompressConfig, maxDecompressedSize);
|
|
217
415
|
|
|
218
|
-
if (
|
|
219
|
-
|
|
416
|
+
if (compressionMethod !== COMPRESSION_METHOD_NONE && budget.remaining < 0) {
|
|
417
|
+
return rejectExceedsMax(budget, maxDecompressedSize);
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
const decompressType = getDecompressType(compressionMethod);
|
|
421
|
+
if (decompressConfig && decompressType) {
|
|
220
422
|
const customFn = decompressConfig[decompressType];
|
|
221
423
|
if (typeof customFn === 'function') {
|
|
222
|
-
|
|
223
|
-
return Promise
|
|
224
|
-
|
|
225
|
-
|
|
424
|
+
// Called now, not deferred: the input can view the caller's buffer, which may be reused once load() returns.
|
|
425
|
+
return new Promise((resolve) => resolve(customFn(getUint8View(dataView)))).then((result) => {
|
|
426
|
+
budget.remaining -= getResultByteLength(result);
|
|
427
|
+
if (budget.remaining < 0) {
|
|
428
|
+
return rejectExceedsMax(budget, maxDecompressedSize);
|
|
226
429
|
}
|
|
227
430
|
if (returnType === 'dataview') {
|
|
228
431
|
if (result instanceof ArrayBuffer) {
|
|
@@ -243,7 +446,7 @@ export function decompress(dataView, compressionMethod, encoding, returnType = '
|
|
|
243
446
|
|
|
244
447
|
if (compressionMethod === COMPRESSION_METHOD_DEFLATE) {
|
|
245
448
|
if (typeof DecompressionStream === 'function') {
|
|
246
|
-
return readBoundedDecompressedStream(dataView, 'deflate', maxDecompressedSize)
|
|
449
|
+
return readBoundedDecompressedStream(dataView, 'deflate', budget, maxDecompressedSize)
|
|
247
450
|
.then((arrayBuffer) => decodeBuffer(arrayBuffer, returnType, encoding));
|
|
248
451
|
}
|
|
249
452
|
}
|
|
@@ -251,7 +454,7 @@ export function decompress(dataView, compressionMethod, encoding, returnType = '
|
|
|
251
454
|
if (compressionMethod === COMPRESSION_METHOD_BROTLI) {
|
|
252
455
|
if (typeof DecompressionStream === 'function') {
|
|
253
456
|
try {
|
|
254
|
-
return readBoundedDecompressedStream(dataView, 'brotli', maxDecompressedSize)
|
|
457
|
+
return readBoundedDecompressedStream(dataView, 'brotli', budget, maxDecompressedSize)
|
|
255
458
|
.then((arrayBuffer) => decodeBuffer(arrayBuffer, returnType, encoding));
|
|
256
459
|
} catch (_error) {
|
|
257
460
|
// brotli not supported by this DecompressionStream implementation
|
|
@@ -276,6 +479,16 @@ export function decompress(dataView, compressionMethod, encoding, returnType = '
|
|
|
276
479
|
return dataView;
|
|
277
480
|
}
|
|
278
481
|
|
|
482
|
+
function getDecompressType(compressionMethod) {
|
|
483
|
+
if (compressionMethod === COMPRESSION_METHOD_DEFLATE) {
|
|
484
|
+
return 'deflate';
|
|
485
|
+
}
|
|
486
|
+
if (compressionMethod === COMPRESSION_METHOD_BROTLI) {
|
|
487
|
+
return 'brotli';
|
|
488
|
+
}
|
|
489
|
+
return undefined;
|
|
490
|
+
}
|
|
491
|
+
|
|
279
492
|
function getMaxDecompressedSize(decompressConfig) {
|
|
280
493
|
if (decompressConfig && typeof decompressConfig.maxDecompressedSize === 'number') {
|
|
281
494
|
return decompressConfig.maxDecompressedSize;
|
|
@@ -283,7 +496,18 @@ function getMaxDecompressedSize(decompressConfig) {
|
|
|
283
496
|
return DEFAULT_MAX_DECOMPRESSED_SIZE;
|
|
284
497
|
}
|
|
285
498
|
|
|
286
|
-
function
|
|
499
|
+
function getDecompressBudget(decompressConfig, maxDecompressedSize) {
|
|
500
|
+
if (decompressConfig && decompressConfig.budget) {
|
|
501
|
+
return decompressConfig.budget;
|
|
502
|
+
}
|
|
503
|
+
return {remaining: maxDecompressedSize, warned: false};
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
function getUint8View(dataView) {
|
|
507
|
+
return new Uint8Array(dataView.buffer, dataView.byteOffset, dataView.byteLength);
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
function readBoundedDecompressedStream(dataView, format, budget, maxDecompressedSize) {
|
|
287
511
|
const decompressionStream = new DecompressionStream(format);
|
|
288
512
|
const decompressedStream = new Blob([dataView]).stream().pipeThrough(decompressionStream);
|
|
289
513
|
const reader = decompressedStream.getReader();
|
|
@@ -298,8 +522,11 @@ function readBoundedDecompressedStream(dataView, format, maxDecompressedSize) {
|
|
|
298
522
|
return concatChunks(chunks, total);
|
|
299
523
|
}
|
|
300
524
|
total += value.byteLength;
|
|
301
|
-
|
|
302
|
-
|
|
525
|
+
// Charge before checking and never give bytes back, so an
|
|
526
|
+
// exceeded budget stays exhausted for every later block.
|
|
527
|
+
budget.remaining -= value.byteLength;
|
|
528
|
+
if (budget.remaining < 0) {
|
|
529
|
+
return reader.cancel().then(() => rejectExceedsMax(budget, maxDecompressedSize));
|
|
303
530
|
}
|
|
304
531
|
chunks.push(value);
|
|
305
532
|
return pump();
|
|
@@ -331,10 +558,11 @@ function getResultByteLength(result) {
|
|
|
331
558
|
return 0;
|
|
332
559
|
}
|
|
333
560
|
|
|
334
|
-
function rejectExceedsMax(maxDecompressedSize) {
|
|
335
|
-
if (typeof console !== 'undefined' && typeof console.warn === 'function') { // eslint-disable-line no-console
|
|
561
|
+
function rejectExceedsMax(budget, maxDecompressedSize) {
|
|
562
|
+
if (!budget.warned && typeof console !== 'undefined' && typeof console.warn === 'function') { // eslint-disable-line no-console
|
|
563
|
+
budget.warned = true;
|
|
336
564
|
// eslint-disable-next-line no-console
|
|
337
|
-
console.warn(`ExifReader:
|
|
565
|
+
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.`);
|
|
338
566
|
}
|
|
339
|
-
return Promise.reject(`Decompressed metadata exceeded the maximum allowed size of ${maxDecompressedSize} bytes.`);
|
|
567
|
+
return Promise.reject(`Decompressed metadata exceeded the maximum allowed total size of ${maxDecompressedSize} bytes.`);
|
|
340
568
|
}
|
package/src/xml.js
CHANGED
|
@@ -53,11 +53,28 @@ function findOffsets(dataView, metadataBlocks) {
|
|
|
53
53
|
function hasXmpClosingMarker(dataView) {
|
|
54
54
|
// XMP packets can have arbitrary trailing whitespace padding, so scan
|
|
55
55
|
// the entire buffer rather than a fixed tail window.
|
|
56
|
-
const text = getStringFromDataView(dataView, 0, dataView.byteLength);
|
|
57
56
|
for (let i = 0; i < XMP_END_PATTERNS.length; i++) {
|
|
58
|
-
if (
|
|
57
|
+
if (containsAsciiString(dataView, XMP_END_PATTERNS[i])) {
|
|
59
58
|
return true;
|
|
60
59
|
}
|
|
61
60
|
}
|
|
62
61
|
return false;
|
|
63
62
|
}
|
|
63
|
+
|
|
64
|
+
function containsAsciiString(dataView, string) {
|
|
65
|
+
for (let offset = 0; offset + string.length <= dataView.byteLength; offset++) {
|
|
66
|
+
if (hasAsciiStringAt(dataView, offset, string)) {
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function hasAsciiStringAt(dataView, offset, string) {
|
|
74
|
+
for (let i = 0; i < string.length; i++) {
|
|
75
|
+
if (dataView.getUint8(offset + i) !== string.charCodeAt(i)) {
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return true;
|
|
80
|
+
}
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
2
|
+
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
3
|
+
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
|
4
|
+
|
|
5
|
+
import {hasSubstringAt, skipPast} from './xmp-namespaces.js';
|
|
6
|
+
|
|
7
|
+
const WHITESPACE_CHARACTERS = ' \t\r\n';
|
|
8
|
+
const NAME_END_CHARACTERS = ' \t\r\n/><="\'';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Tells whether an XML string has elements nested deeper than a bound or more
|
|
12
|
+
* markup nodes than a bound, in linear time. The nodes are elements, attributes
|
|
13
|
+
* (namespace declarations included), comments, processing instructions, CDATA
|
|
14
|
+
* sections and <! declarations together; text is not counted. Malformed markup
|
|
15
|
+
* may be counted deeper and with more nodes than @xmldom/xmldom 0.9.12 builds,
|
|
16
|
+
* never shallower or with fewer.
|
|
17
|
+
*
|
|
18
|
+
* @param {string} xmlString
|
|
19
|
+
* @param {number} maxDepth The deepest allowed element, the root being 1.
|
|
20
|
+
* @param {number} [maxNodes] The most markup nodes allowed.
|
|
21
|
+
* @returns {boolean} True as soon as an element is nested deeper than maxDepth
|
|
22
|
+
* or the markup nodes are more than maxNodes.
|
|
23
|
+
*/
|
|
24
|
+
export function exceedsMarkupBounds(xmlString, maxDepth, maxNodes = Infinity) {
|
|
25
|
+
let depth = 0;
|
|
26
|
+
let nodeCount = 0;
|
|
27
|
+
let index = xmlString.indexOf('<');
|
|
28
|
+
while (index !== -1) {
|
|
29
|
+
if (hasSubstringAt(xmlString, '<!--', index)) {
|
|
30
|
+
nodeCount++;
|
|
31
|
+
index = skipPast(xmlString, index + 4, '-->');
|
|
32
|
+
} else if (hasSubstringAt(xmlString, '<![CDATA[', index)) {
|
|
33
|
+
nodeCount++;
|
|
34
|
+
index = skipPast(xmlString, index + 9, ']]>');
|
|
35
|
+
} else if (hasSubstringAt(xmlString, '<?', index)) {
|
|
36
|
+
nodeCount++;
|
|
37
|
+
index = skipPast(xmlString, index + 2, '?>');
|
|
38
|
+
} else if (hasSubstringAt(xmlString, '<!', index)) {
|
|
39
|
+
nodeCount++;
|
|
40
|
+
index = skipDeclaration(xmlString, index + 2);
|
|
41
|
+
} else if (hasSubstringAt(xmlString, '</', index)) {
|
|
42
|
+
depth = Math.max(depth - 1, 0);
|
|
43
|
+
// xmldom ends an end tag at its first >, whatever comes between.
|
|
44
|
+
index = skipPast(xmlString, index + 2, '>');
|
|
45
|
+
} else {
|
|
46
|
+
if (depth + 1 > maxDepth) {
|
|
47
|
+
return true;
|
|
48
|
+
}
|
|
49
|
+
const nextTagIndex = xmlString.indexOf('<', index + 1);
|
|
50
|
+
const limit = nextTagIndex === -1 ? xmlString.length : nextTagIndex;
|
|
51
|
+
if (!isSelfClosingTag(xmlString, index + 1, limit)) {
|
|
52
|
+
depth++;
|
|
53
|
+
}
|
|
54
|
+
nodeCount += 1 + countAttributes(xmlString, index + 1, limit);
|
|
55
|
+
index++;
|
|
56
|
+
}
|
|
57
|
+
if (nodeCount > maxNodes) {
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
60
|
+
index = xmlString.indexOf('<', index);
|
|
61
|
+
}
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Stopping at [ lets the main loop read the declarations of a DOCTYPE internal
|
|
66
|
+
// subset one by one.
|
|
67
|
+
function skipDeclaration(xmlString, fromIndex) {
|
|
68
|
+
for (let index = fromIndex; index < xmlString.length; index++) {
|
|
69
|
+
const character = xmlString.charAt(index);
|
|
70
|
+
if (character === '"' || character === '\'') {
|
|
71
|
+
index = xmlString.indexOf(character, index + 1);
|
|
72
|
+
if (index === -1) {
|
|
73
|
+
return xmlString.length;
|
|
74
|
+
}
|
|
75
|
+
} else if (character === '>' || character === '[') {
|
|
76
|
+
return index + 1;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return xmlString.length;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// A start tag cannot hold a <, so every search in it stops at the next <, which
|
|
83
|
+
// keeps the scan linear. Only a well-formed tag is read as self-closing: some
|
|
84
|
+
// parsers keep a malformed one open, so reading it as open can only count deeper.
|
|
85
|
+
function isSelfClosingTag(xmlString, nameIndex, limit) {
|
|
86
|
+
let index = skipName(xmlString, nameIndex, limit);
|
|
87
|
+
if (index === nameIndex) {
|
|
88
|
+
return false;
|
|
89
|
+
}
|
|
90
|
+
for (;;) {
|
|
91
|
+
const attributeIndex = skipWhitespace(xmlString, index, limit);
|
|
92
|
+
if (hasSubstringAt(xmlString, '/>', attributeIndex)) {
|
|
93
|
+
return true;
|
|
94
|
+
}
|
|
95
|
+
if (attributeIndex === index) {
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
98
|
+
index = skipAttribute(xmlString, attributeIndex, limit);
|
|
99
|
+
if (index === -1) {
|
|
100
|
+
return false;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function skipName(xmlString, fromIndex, limit) {
|
|
106
|
+
let index = fromIndex;
|
|
107
|
+
while (index < limit && NAME_END_CHARACTERS.indexOf(xmlString.charAt(index)) === -1) {
|
|
108
|
+
index++;
|
|
109
|
+
}
|
|
110
|
+
return index;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function skipWhitespace(xmlString, fromIndex, limit) {
|
|
114
|
+
let index = fromIndex;
|
|
115
|
+
while (index < limit && WHITESPACE_CHARACTERS.indexOf(xmlString.charAt(index)) !== -1) {
|
|
116
|
+
index++;
|
|
117
|
+
}
|
|
118
|
+
return index;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// Only a quoted value is well-formed.
|
|
122
|
+
function skipAttribute(xmlString, nameIndex, limit) {
|
|
123
|
+
const nameEndIndex = skipName(xmlString, nameIndex, limit);
|
|
124
|
+
if (nameEndIndex === nameIndex) {
|
|
125
|
+
return -1;
|
|
126
|
+
}
|
|
127
|
+
const equalsIndex = skipWhitespace(xmlString, nameEndIndex, limit);
|
|
128
|
+
if (xmlString.charAt(equalsIndex) !== '=') {
|
|
129
|
+
return -1;
|
|
130
|
+
}
|
|
131
|
+
const quoteIndex = skipWhitespace(xmlString, equalsIndex + 1, limit);
|
|
132
|
+
const quote = xmlString.charAt(quoteIndex);
|
|
133
|
+
if (quote !== '"' && quote !== '\'') {
|
|
134
|
+
return -1;
|
|
135
|
+
}
|
|
136
|
+
for (let index = quoteIndex + 1; index < limit; index++) {
|
|
137
|
+
if (xmlString.charAt(index) === quote) {
|
|
138
|
+
return index + 1;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return -1;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const TAG = 0;
|
|
145
|
+
const ATTRIBUTE_NAME = 1;
|
|
146
|
+
const SPACE_AFTER_ATTRIBUTE_NAME = 2;
|
|
147
|
+
const EQUALS = 3;
|
|
148
|
+
const UNQUOTED_VALUE = 4;
|
|
149
|
+
const ATTRIBUTE_END = 5;
|
|
150
|
+
const TAG_SPACE = 6;
|
|
151
|
+
const TAG_CLOSE = 7;
|
|
152
|
+
|
|
153
|
+
// Follows the start tag state machine of xmldom 0.9.12 (parseElementStartPart)
|
|
154
|
+
// so that no attribute it builds goes uncounted. Every way out before > is
|
|
155
|
+
// either the end of the tag or an error that makes xmldom drop the element.
|
|
156
|
+
function countAttributes(xmlString, nameIndex, limit) {
|
|
157
|
+
let state = TAG;
|
|
158
|
+
let count = 0;
|
|
159
|
+
for (let index = nameIndex; index < limit; index++) {
|
|
160
|
+
const character = xmlString.charAt(index);
|
|
161
|
+
if (character === '=') {
|
|
162
|
+
if (state !== ATTRIBUTE_NAME && state !== SPACE_AFTER_ATTRIBUTE_NAME) {
|
|
163
|
+
return count;
|
|
164
|
+
}
|
|
165
|
+
state = EQUALS;
|
|
166
|
+
} else if (character === '"' || character === '\'') {
|
|
167
|
+
if (state === EQUALS || state === ATTRIBUTE_NAME) {
|
|
168
|
+
const quoteIndex = xmlString.indexOf(character, index + 1);
|
|
169
|
+
if (quoteIndex === -1 || quoteIndex >= limit) {
|
|
170
|
+
return count;
|
|
171
|
+
}
|
|
172
|
+
index = quoteIndex;
|
|
173
|
+
} else if (state !== UNQUOTED_VALUE) {
|
|
174
|
+
return count;
|
|
175
|
+
}
|
|
176
|
+
count++;
|
|
177
|
+
state = ATTRIBUTE_END;
|
|
178
|
+
} else if (character === '/') {
|
|
179
|
+
if (state === EQUALS) {
|
|
180
|
+
return count;
|
|
181
|
+
}
|
|
182
|
+
if (state === TAG || state === ATTRIBUTE_END || state === TAG_SPACE) {
|
|
183
|
+
state = TAG_CLOSE;
|
|
184
|
+
}
|
|
185
|
+
} else if (character === '>') {
|
|
186
|
+
return isAttributePending(state) ? count + 1 : count;
|
|
187
|
+
} else if (isXmldomWhitespace(character)) {
|
|
188
|
+
if (state === UNQUOTED_VALUE) {
|
|
189
|
+
count++;
|
|
190
|
+
}
|
|
191
|
+
if (state === TAG || state === UNQUOTED_VALUE || state === ATTRIBUTE_END) {
|
|
192
|
+
state = TAG_SPACE;
|
|
193
|
+
} else if (state === ATTRIBUTE_NAME) {
|
|
194
|
+
state = SPACE_AFTER_ATTRIBUTE_NAME;
|
|
195
|
+
}
|
|
196
|
+
} else {
|
|
197
|
+
if (state === TAG_CLOSE) {
|
|
198
|
+
return count;
|
|
199
|
+
}
|
|
200
|
+
if (state === SPACE_AFTER_ATTRIBUTE_NAME) {
|
|
201
|
+
count++;
|
|
202
|
+
}
|
|
203
|
+
if (state === SPACE_AFTER_ATTRIBUTE_NAME || state === ATTRIBUTE_END || state === TAG_SPACE) {
|
|
204
|
+
state = ATTRIBUTE_NAME;
|
|
205
|
+
} else if (state === EQUALS) {
|
|
206
|
+
state = UNQUOTED_VALUE;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
return isAttributePending(state) ? count + 1 : count;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function isAttributePending(state) {
|
|
214
|
+
return state === ATTRIBUTE_NAME || state === SPACE_AFTER_ATTRIBUTE_NAME || state === UNQUOTED_VALUE;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// Exactly xmldom's whitespace: every character up to a space, \u0080, and the
|
|
218
|
+
// line ends its normalizeLineEndings turns into \n. Adding or removing any
|
|
219
|
+
// character lets attributes go uncounted.
|
|
220
|
+
function isXmldomWhitespace(character) {
|
|
221
|
+
return character <= ' '
|
|
222
|
+
|| character === '\u0080'
|
|
223
|
+
|| character === '\u0085'
|
|
224
|
+
|| character === '\u2028'
|
|
225
|
+
|| character === '\u2029';
|
|
226
|
+
}
|