exifreader 4.47.0 → 4.48.1
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 +42 -28
- package/bin/build.js +3 -0
- package/bin/unshare-file.js +29 -0
- package/dist/exif-reader.js +1 -1
- package/dist/exif-reader.js.map +1 -1
- package/exif-reader.d.ts +6 -6
- package/package.json +6 -6
- package/src/canon-tags.js +4 -3
- package/src/composite.js +94 -47
- package/src/exif-reader.js +61 -9
- package/src/file-loaders.js +64 -22
- package/src/icc-tag-names.js +2 -11
- package/src/image-header-iso-bmff.js +31 -10
- package/src/image-header-jpeg.js +9 -2
- package/src/image-header-png.js +34 -27
- package/src/iptc-tags.js +46 -11
- package/src/load-auto.js +6 -3
- package/src/loadview-pipeline.js +58 -23
- package/src/mpf-tags.js +6 -1
- package/src/pentax-tags.js +45 -21
- package/src/photoshop-tag-names.js +6 -2
- package/src/photoshop-tags.js +24 -23
- package/src/png-text-tags.js +175 -45
- package/src/tag-filter-config.js +25 -0
- package/src/tag-names-exif-ifd.js +32 -11
- package/src/tag-names-interoperability-ifd.js +2 -2
- package/src/tag-names-mpf-ifd.js +2 -2
- package/src/tag-names-utils.js +17 -3
- package/src/tags-helpers.js +177 -50
- package/src/tags.js +10 -0
- package/src/utils.js +185 -27
- package/src/xml.js +19 -2
- package/src/xmp-element-depth.js +226 -0
- package/src/xmp-namespaces.js +2 -2
- package/src/xmp-tags.js +38 -12
package/src/tags-helpers.js
CHANGED
|
@@ -5,22 +5,56 @@
|
|
|
5
5
|
import Constants from './constants.js';
|
|
6
6
|
import Types from './types.js';
|
|
7
7
|
import TagNames, {IFD_TYPE_0TH, IFD_TYPE_1ST, IFD_TYPE_PENTAX} from './tag-names.js';
|
|
8
|
-
import {IFD_ENTRY_LENGTH, TIFF_IFD_OFFSET_OFFSET} from './tiff-constants.js';
|
|
8
|
+
import {IFD_ENTRY_LENGTH, TIFF_HEADER_LENGTH, TIFF_IFD_OFFSET_OFFSET} from './tiff-constants.js';
|
|
9
9
|
import {NOOP_TAG_FILTER} from './tag-filter.js';
|
|
10
|
-
import {decodeUtf8ByteString} from './utils.js';
|
|
11
|
-
|
|
12
|
-
//
|
|
13
|
-
// values
|
|
14
|
-
// (
|
|
15
|
-
//
|
|
16
|
-
//
|
|
17
|
-
//
|
|
18
|
-
//
|
|
10
|
+
import {decodeUtf8ByteString, getByteString} from './utils.js';
|
|
11
|
+
|
|
12
|
+
// Across the test corpus, for whole files and for the 128 KiB and
|
|
13
|
+
// length: 'auto' reads, out-of-slot values decode at most 1.2241 times the
|
|
14
|
+
// buffer (faulty files parsed with includeUnknown). Exif decompressed from a
|
|
15
|
+
// PNG or JPEG XL gets its own allowance on top of the file-sized budget, see
|
|
16
|
+
// MAX_DECOMPRESSED_VALUE_ALLOWANCE. Description text is not charged, as real
|
|
17
|
+
// TIFFs whose large BYTE tags get joined descriptions would need 4.48 times.
|
|
18
|
+
// A decoded byte costs about 15 bytes of heap, mostly as an array element and
|
|
19
|
+
// its share of the joined description.
|
|
19
20
|
const MAX_VALUE_SIZE_PER_BUFFER_SIZE = 4;
|
|
20
21
|
|
|
21
|
-
//
|
|
22
|
-
//
|
|
23
|
-
|
|
22
|
+
// Real Exif decodes at most about 2 times its own size (the MakerNote bytes
|
|
23
|
+
// decode twice), so MAX_VALUE_SIZE_PER_BUFFER_SIZE times its decompressed size
|
|
24
|
+
// covers it. The cap keeps a small crafted file from decoding values in
|
|
25
|
+
// proportion to what it decompresses to: at most this much more per load.
|
|
26
|
+
const MAX_DECOMPRESSED_VALUE_ALLOWANCE = 1024 * 1024;
|
|
27
|
+
|
|
28
|
+
// A buffer holds at most byteLength / 12 IFD entries and a real file reads each
|
|
29
|
+
// once. The multiple leaves room for an IFD reached through more than one
|
|
30
|
+
// pointer. In the test corpus, whole files and 128 KiB and length: 'auto' reads
|
|
31
|
+
// visit at most about as many entries as the buffer holds.
|
|
32
|
+
const MAX_IFD_ENTRY_READS_PER_BUFFER_ENTRY = 4;
|
|
33
|
+
|
|
34
|
+
// Each string after the first of an out-of-slot ASCII value costs this much
|
|
35
|
+
// budget. 4 MiB files whose tags use the budget up on short strings peaked at
|
|
36
|
+
// up to 107 times their size in RSS without it, 88 with 1 and 76 with 2,
|
|
37
|
+
// against 87 for BYTE values.
|
|
38
|
+
export const BUDGET_BYTES_PER_EXTRA_ASCII_STRING = 2;
|
|
39
|
+
|
|
40
|
+
// A buffer gets one IPTC dataset unit per this many bytes, and a dataset draws
|
|
41
|
+
// one unit per started this many bytes, header included. Short datasets cost
|
|
42
|
+
// up to about 1.2 KB of peak memory each, so this keeps an 8 MiB file of them
|
|
43
|
+
// at most about 33 times its size. Longer datasets draw about one unit per this
|
|
44
|
+
// many of their own bytes, so the count does not cut them below what the file
|
|
45
|
+
// holds; they cost about 25 to 36 times their size.
|
|
46
|
+
export const BYTES_PER_IPTC_DATASET = 48;
|
|
47
|
+
|
|
48
|
+
// Every load can read this many units whatever its size, so a small file with
|
|
49
|
+
// a real IPTC block is read in full. They cost at most about 1.2 MB.
|
|
50
|
+
const MIN_IPTC_DATASET_UNITS = 1024;
|
|
51
|
+
|
|
52
|
+
// The dataset header, the smallest dataset there is.
|
|
53
|
+
export const IPTC_DATASET_HEADER_SIZE = 5;
|
|
54
|
+
|
|
55
|
+
// Real compressed IPTC is far smaller. The cap bounds how many datasets a small
|
|
56
|
+
// file can add by decompressing to a large IPTC block.
|
|
57
|
+
const MAX_DECOMPRESSED_IPTC_ALLOWANCE = 8192;
|
|
24
58
|
|
|
25
59
|
const getTagValueAt = {
|
|
26
60
|
1: Types.getByteAt,
|
|
@@ -33,22 +67,34 @@ const getTagValueAt = {
|
|
|
33
67
|
13: Types.getIfdPointerAt
|
|
34
68
|
};
|
|
35
69
|
|
|
70
|
+
/**
|
|
71
|
+
* @returns {number|undefined} The absolute offset of the 0th IFD, or undefined
|
|
72
|
+
* when the header is truncated or the offset points into it.
|
|
73
|
+
*/
|
|
36
74
|
export function get0thIfdOffset(dataView, tiffHeaderOffset, byteOrder) {
|
|
37
75
|
const offset = tiffHeaderOffset + TIFF_IFD_OFFSET_OFFSET;
|
|
38
76
|
if (offset + Types.getTypeSize('LONG') > dataView.byteLength) {
|
|
39
77
|
return undefined;
|
|
40
78
|
}
|
|
41
|
-
|
|
42
|
-
|
|
79
|
+
const ifdOffset = Types.getLongAt(dataView, offset, byteOrder);
|
|
80
|
+
// An IFD starts past the TIFF header.
|
|
81
|
+
if (ifdOffset < TIFF_HEADER_LENGTH) {
|
|
82
|
+
return undefined;
|
|
83
|
+
}
|
|
84
|
+
return tiffHeaderOffset + ifdOffset;
|
|
43
85
|
}
|
|
44
86
|
|
|
45
87
|
/**
|
|
46
88
|
* Reads the tags of an IFD, and for a 0th IFD also those of the thumbnail IFD
|
|
47
89
|
* it points to.
|
|
48
90
|
*
|
|
49
|
-
* @param {{remaining: number}} [valueBudget] -
|
|
50
|
-
* decoded tag values
|
|
51
|
-
*
|
|
91
|
+
* @param {{remaining: number, ifdEntriesRemaining: number, decompressedAllowanceRemaining: number}} [valueBudget] -
|
|
92
|
+
* Caps the total size of the decoded tag values and the number of IFD entries
|
|
93
|
+
* read, see getValueBudget. Pass the same object to several calls to bound them
|
|
94
|
+
* together; omit it to give this call its own budget. Once the entry count runs
|
|
95
|
+
* out, later entries and IFDs are not read, so their tags are absent rather than
|
|
96
|
+
* empty, and an IFD cut short does not follow its next-IFD offset. Nor does one
|
|
97
|
+
* whose next-IFD offset points into the TIFF header.
|
|
52
98
|
* @returns {Object} The read tags, keyed by tag name.
|
|
53
99
|
*/
|
|
54
100
|
export function readIfd(
|
|
@@ -68,10 +114,16 @@ export function readIfd(
|
|
|
68
114
|
|
|
69
115
|
const tags = {};
|
|
70
116
|
const numberOfFields = getNumberOfFields(dataView, offset, byteOrder);
|
|
117
|
+
let ranOutOfIfdEntries = false;
|
|
71
118
|
|
|
72
119
|
offset += FIELD_COUNT_SIZE;
|
|
73
120
|
for (let fieldIndex = 0; fieldIndex < numberOfFields; fieldIndex++) {
|
|
74
121
|
if (offset + FIELD_SIZE > dataView.byteLength) {
|
|
122
|
+
ranOutOfIfdEntries = true;
|
|
123
|
+
break;
|
|
124
|
+
}
|
|
125
|
+
if (!takeIfdEntry(valueBudget)) {
|
|
126
|
+
ranOutOfIfdEntries = true;
|
|
75
127
|
break;
|
|
76
128
|
}
|
|
77
129
|
|
|
@@ -103,9 +155,9 @@ export function readIfd(
|
|
|
103
155
|
offset += FIELD_SIZE;
|
|
104
156
|
}
|
|
105
157
|
|
|
106
|
-
if (Constants.USE_THUMBNAIL && (offset
|
|
158
|
+
if (Constants.USE_THUMBNAIL && !ranOutOfIfdEntries && (offset + Types.getTypeSize('LONG') <= dataView.byteLength)) {
|
|
107
159
|
const nextIfdOffset = Types.getLongAt(dataView, offset, byteOrder);
|
|
108
|
-
if (nextIfdOffset
|
|
160
|
+
if (nextIfdOffset >= TIFF_HEADER_LENGTH && ifdType === IFD_TYPE_0TH) {
|
|
109
161
|
if (tagFilter.shouldParseGroup('thumbnail')) {
|
|
110
162
|
tags['Thumbnail'] = readIfd(
|
|
111
163
|
dataView,
|
|
@@ -126,6 +178,14 @@ export function readIfd(
|
|
|
126
178
|
return tags;
|
|
127
179
|
}
|
|
128
180
|
|
|
181
|
+
function takeIfdEntry(valueBudget) {
|
|
182
|
+
if (valueBudget.ifdEntriesRemaining > 0) {
|
|
183
|
+
valueBudget.ifdEntriesRemaining--;
|
|
184
|
+
return true;
|
|
185
|
+
}
|
|
186
|
+
return false;
|
|
187
|
+
}
|
|
188
|
+
|
|
129
189
|
/**
|
|
130
190
|
* Creates a budget for the total size of the tag values decoded from a buffer.
|
|
131
191
|
*
|
|
@@ -136,15 +196,85 @@ export function readIfd(
|
|
|
136
196
|
* tags decode the same bytes over and over.
|
|
137
197
|
*
|
|
138
198
|
* loadView shares one budget across the Exif IFDs, then the maker note, then
|
|
139
|
-
* MPF, then the Exif decompressed from PNG text chunks and JPEG XL brob boxes
|
|
199
|
+
* MPF, then the Exif decompressed from PNG text chunks and JPEG XL brob boxes,
|
|
200
|
+
* each of which first adds its allowance, see addDecompressedValueAllowance.
|
|
140
201
|
* Once it is used up, later out-of-slot values decode empty, Make included,
|
|
141
|
-
* and an empty Make or MakerNote turns maker note detection off.
|
|
202
|
+
* and an empty Make or MakerNote turns maker note detection off. An ASCII
|
|
203
|
+
* value also draws for each string after its first, and keeps only the
|
|
204
|
+
* strings the budget covers.
|
|
205
|
+
*
|
|
206
|
+
* The budget also counts the IFD entries read, shared the same way, so that
|
|
207
|
+
* Exif decompressed from a small file cannot have its entries read in
|
|
208
|
+
* proportion to the decompressed size. It counts the IPTC datasets read too,
|
|
209
|
+
* across every IPTC read of the load; IPTC decompressed from a PNG text chunk
|
|
210
|
+
* first adds its allowance, see addDecompressedIptcAllowance.
|
|
142
211
|
*
|
|
143
212
|
* @param {DataView} dataView - The buffer the values are decoded from.
|
|
144
|
-
* @returns {{remaining: number
|
|
213
|
+
* @returns {{remaining: number, ifdEntriesRemaining: number, decompressedAllowanceRemaining: number, iptcDatasetsRemaining: number, decompressedIptcAllowanceRemaining: number}}
|
|
214
|
+
* The budget: remaining is the bytes left to decode, with each extra ASCII
|
|
215
|
+
* string counted as BUDGET_BYTES_PER_EXTRA_ASCII_STRING bytes;
|
|
216
|
+
* ifdEntriesRemaining is the IFD entries left to read,
|
|
217
|
+
* MAX_IFD_ENTRY_READS_PER_BUFFER_ENTRY times the byteLength / 12 entries the
|
|
218
|
+
* buffer can hold; decompressedAllowanceRemaining is what decompressed Exif
|
|
219
|
+
* can still add to remaining, MAX_DECOMPRESSED_VALUE_ALLOWANCE per budget;
|
|
220
|
+
* iptcDatasetsRemaining is the IPTC dataset units left, MIN_IPTC_DATASET_UNITS
|
|
221
|
+
* plus one per BYTES_PER_IPTC_DATASET bytes of the buffer, of which a dataset
|
|
222
|
+
* draws one per started BYTES_PER_IPTC_DATASET bytes; decompressedIptcAllowanceRemaining
|
|
223
|
+
* is what decompressed IPTC can still add to iptcDatasetsRemaining,
|
|
224
|
+
* MAX_DECOMPRESSED_IPTC_ALLOWANCE per budget.
|
|
145
225
|
*/
|
|
146
226
|
export function getValueBudget(dataView) {
|
|
147
|
-
return {
|
|
227
|
+
return {
|
|
228
|
+
remaining: dataView.byteLength * MAX_VALUE_SIZE_PER_BUFFER_SIZE,
|
|
229
|
+
ifdEntriesRemaining: Math.floor(dataView.byteLength / IFD_ENTRY_LENGTH) * MAX_IFD_ENTRY_READS_PER_BUFFER_ENTRY,
|
|
230
|
+
decompressedAllowanceRemaining: MAX_DECOMPRESSED_VALUE_ALLOWANCE,
|
|
231
|
+
iptcDatasetsRemaining: MIN_IPTC_DATASET_UNITS + Math.floor(dataView.byteLength / BYTES_PER_IPTC_DATASET),
|
|
232
|
+
decompressedIptcAllowanceRemaining: MAX_DECOMPRESSED_IPTC_ALLOWANCE
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Adds the allowance of an Exif block decompressed from the file to the
|
|
238
|
+
* budget: MAX_VALUE_SIZE_PER_BUFFER_SIZE times its decompressed size, up to
|
|
239
|
+
* what is left of the budget's decompressedAllowanceRemaining, which it draws
|
|
240
|
+
* from. Leaves the IFD entry count and the IPTC dataset count as they are.
|
|
241
|
+
* Does nothing without a budget.
|
|
242
|
+
*
|
|
243
|
+
* @param {{remaining: number, decompressedAllowanceRemaining: number}} [valueBudget]
|
|
244
|
+
* @param {number} decompressedByteLength - The size of the decompressed Exif.
|
|
245
|
+
*/
|
|
246
|
+
export function addDecompressedValueAllowance(valueBudget, decompressedByteLength) {
|
|
247
|
+
if (valueBudget === undefined) {
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
const allowance = Math.min(
|
|
251
|
+
decompressedByteLength * MAX_VALUE_SIZE_PER_BUFFER_SIZE,
|
|
252
|
+
valueBudget.decompressedAllowanceRemaining
|
|
253
|
+
);
|
|
254
|
+
valueBudget.remaining += allowance;
|
|
255
|
+
valueBudget.decompressedAllowanceRemaining -= allowance;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Adds the allowance of an IPTC block decompressed from the file to the
|
|
260
|
+
* budget's iptcDatasetsRemaining: one unit per IPTC_DATASET_HEADER_SIZE
|
|
261
|
+
* decompressed bytes, enough for every dataset the block holds, up to what is
|
|
262
|
+
* left of the budget's decompressedIptcAllowanceRemaining, which it draws from.
|
|
263
|
+
* Leaves the other fields as they are. Does nothing without a budget.
|
|
264
|
+
*
|
|
265
|
+
* @param {{iptcDatasetsRemaining: number, decompressedIptcAllowanceRemaining: number}} [valueBudget]
|
|
266
|
+
* @param {number} decompressedByteLength - The size of the decompressed IPTC.
|
|
267
|
+
*/
|
|
268
|
+
export function addDecompressedIptcAllowance(valueBudget, decompressedByteLength) {
|
|
269
|
+
if (valueBudget === undefined) {
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
const allowance = Math.min(
|
|
273
|
+
Math.floor(decompressedByteLength / IPTC_DATASET_HEADER_SIZE),
|
|
274
|
+
valueBudget.decompressedIptcAllowanceRemaining
|
|
275
|
+
);
|
|
276
|
+
valueBudget.iptcDatasetsRemaining += allowance;
|
|
277
|
+
valueBudget.decompressedIptcAllowanceRemaining -= allowance;
|
|
148
278
|
}
|
|
149
279
|
|
|
150
280
|
function getNumberOfFields(dataView, offset, byteOrder) {
|
|
@@ -175,6 +305,7 @@ function readTag(
|
|
|
175
305
|
const tagCount = Types.getLongAt(dataView, offset + TAG_COUNT_OFFSET, byteOrder);
|
|
176
306
|
let tagValue;
|
|
177
307
|
let tagValueOffset;
|
|
308
|
+
let stringBudget;
|
|
178
309
|
|
|
179
310
|
if (Types.typeSizes[tagType] === undefined || (!includeUnknown && TagNames[ifdType][tagCode] === undefined)) {
|
|
180
311
|
return undefined;
|
|
@@ -186,14 +317,15 @@ function readTag(
|
|
|
186
317
|
}
|
|
187
318
|
|
|
188
319
|
if (tagValueFitsInOffsetSlot(tagType, tagCount)) {
|
|
189
|
-
tagValueOffset = offset + TAG_VALUE_OFFSET;
|
|
190
|
-
tagValue = getTagValue(dataView, tagValueOffset, tagType, tagCount, byteOrder);
|
|
320
|
+
tagValueOffset = offset + TAG_VALUE_OFFSET - offsetOrigin;
|
|
321
|
+
tagValue = getTagValue(dataView, offsetOrigin + tagValueOffset, tagType, tagCount, byteOrder);
|
|
191
322
|
} else {
|
|
192
323
|
tagValueOffset = Types.getLongAt(dataView, offset + TAG_VALUE_OFFSET, byteOrder);
|
|
193
324
|
if (tagValueFitsInDataView(dataView, offsetOrigin, tagValueOffset, tagType, tagCount)) {
|
|
194
325
|
const forceByteType = tagCode === TAG_CODE_IPTC_NAA;
|
|
195
326
|
const boundedTagCount = getBoundedTagCount(valueBudget.remaining, tagType, tagCount);
|
|
196
327
|
valueBudget.remaining -= boundedTagCount * Types.typeSizes[tagType];
|
|
328
|
+
stringBudget = valueBudget;
|
|
197
329
|
tagValue = getTagValue(dataView, offsetOrigin + tagValueOffset, tagType, boundedTagCount, byteOrder, forceByteType);
|
|
198
330
|
} else {
|
|
199
331
|
tagValue = '<faulty value>';
|
|
@@ -201,7 +333,7 @@ function readTag(
|
|
|
201
333
|
}
|
|
202
334
|
|
|
203
335
|
if (tagType === Types.tagTypes['ASCII']) {
|
|
204
|
-
tagValue = getAsciiTagValue(tagValue);
|
|
336
|
+
tagValue = getAsciiTagValue(tagValue, stringBudget);
|
|
205
337
|
}
|
|
206
338
|
|
|
207
339
|
let tagDescription = tagValue;
|
|
@@ -283,7 +415,7 @@ function tagValueFitsInDataView(dataView, offsetOrigin, tagValueOffset, tagType,
|
|
|
283
415
|
|
|
284
416
|
// Draws each out-of-slot value from the shared budget, so a crafted file
|
|
285
417
|
// cannot have thousands of tags decode the same bytes over and over. Real
|
|
286
|
-
// files
|
|
418
|
+
// files stay below it.
|
|
287
419
|
function getBoundedTagCount(remainingBudget, tagType, tagCount) {
|
|
288
420
|
const boundedCount = Math.min(tagCount, Math.floor(remainingBudget / Types.typeSizes[tagType]));
|
|
289
421
|
if (boundedCount === 1 && tagCount > 1) {
|
|
@@ -295,9 +427,9 @@ function getBoundedTagCount(remainingBudget, tagType, tagCount) {
|
|
|
295
427
|
return boundedCount;
|
|
296
428
|
}
|
|
297
429
|
|
|
298
|
-
function getAsciiTagValue(tagValue) {
|
|
430
|
+
function getAsciiTagValue(tagValue, stringBudget) {
|
|
299
431
|
if (tagValue instanceof Uint8Array) {
|
|
300
|
-
return getNullSeparatedStrings(tagValue);
|
|
432
|
+
return getNullSeparatedStrings(tagValue, stringBudget);
|
|
301
433
|
}
|
|
302
434
|
if (typeof tagValue === 'string') {
|
|
303
435
|
return [tagValue];
|
|
@@ -308,17 +440,23 @@ function getAsciiTagValue(tagValue) {
|
|
|
308
440
|
}
|
|
309
441
|
|
|
310
442
|
// Empty strings are left as holes in the array, so each string keeps the
|
|
311
|
-
// index given by the number of NULs before it.
|
|
312
|
-
|
|
443
|
+
// index given by the number of NULs before it. With a string budget, each
|
|
444
|
+
// string after the first draws from it, and the value stops when it runs out.
|
|
445
|
+
function getNullSeparatedStrings(bytes, stringBudget) {
|
|
313
446
|
const strings = [];
|
|
314
447
|
const charCodes = [];
|
|
448
|
+
let hasString = false;
|
|
315
449
|
let stringIndex = 0;
|
|
316
450
|
let stringStart = 0;
|
|
317
451
|
|
|
318
452
|
for (let i = 0; i <= bytes.length; i++) {
|
|
319
453
|
if (i === bytes.length || bytes[i] === 0) {
|
|
320
454
|
if (i > stringStart) {
|
|
455
|
+
if (hasString && !drawExtraString(stringBudget)) {
|
|
456
|
+
break;
|
|
457
|
+
}
|
|
321
458
|
strings[stringIndex] = decodeUtf8ByteString(getByteString(bytes, stringStart, i, charCodes));
|
|
459
|
+
hasString = true;
|
|
322
460
|
}
|
|
323
461
|
stringIndex++;
|
|
324
462
|
stringStart = i + 1;
|
|
@@ -328,26 +466,15 @@ function getNullSeparatedStrings(bytes) {
|
|
|
328
466
|
return strings;
|
|
329
467
|
}
|
|
330
468
|
|
|
331
|
-
function
|
|
332
|
-
if (
|
|
333
|
-
return
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
const chunks = [];
|
|
337
|
-
for (let chunkStart = start; chunkStart < end; chunkStart += MAX_CHARS_PER_CALL) {
|
|
338
|
-
chunks.push(getChunkString(bytes, chunkStart, Math.min(chunkStart + MAX_CHARS_PER_CALL, end), charCodes));
|
|
469
|
+
function drawExtraString(stringBudget) {
|
|
470
|
+
if (stringBudget === undefined) {
|
|
471
|
+
return true;
|
|
339
472
|
}
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
// Reuses the caller's array, so a value made of many short strings does not
|
|
344
|
-
// allocate an array per string.
|
|
345
|
-
function getChunkString(bytes, start, end, charCodes) {
|
|
346
|
-
for (let i = start; i < end; i++) {
|
|
347
|
-
charCodes[i - start] = bytes[i];
|
|
473
|
+
if (stringBudget.remaining < BUDGET_BYTES_PER_EXTRA_ASCII_STRING) {
|
|
474
|
+
return false;
|
|
348
475
|
}
|
|
349
|
-
|
|
350
|
-
return
|
|
476
|
+
stringBudget.remaining -= BUDGET_BYTES_PER_EXTRA_ASCII_STRING;
|
|
477
|
+
return true;
|
|
351
478
|
}
|
|
352
479
|
|
|
353
480
|
function getDescriptionFromTagValue(tagValue) {
|
package/src/tags.js
CHANGED
|
@@ -6,6 +6,7 @@ import {objectAssign} from './utils.js';
|
|
|
6
6
|
import ByteOrder from './byte-order.js';
|
|
7
7
|
import {IFD_TYPE_0TH, IFD_TYPE_EXIF, IFD_TYPE_GPS, IFD_TYPE_INTEROPERABILITY} from './tag-names.js';
|
|
8
8
|
import {readIfd, get0thIfdOffset, getValueBudget} from './tags-helpers.js';
|
|
9
|
+
import {TIFF_HEADER_LENGTH} from './tiff-constants.js';
|
|
9
10
|
|
|
10
11
|
const SUB_IFDS = [
|
|
11
12
|
{pointerKey: 'Exif IFD Pointer', ifdType: IFD_TYPE_EXIF},
|
|
@@ -62,6 +63,11 @@ function readSubIfd(subIfd, tags, dataView, tiffHeaderOffset, byteOrder, include
|
|
|
62
63
|
if (pointerTag === undefined) {
|
|
63
64
|
return tags;
|
|
64
65
|
}
|
|
66
|
+
// An IFD starts past the TIFF header. An SLONG pointer can be negative, and
|
|
67
|
+
// an array or a faulty value is not an offset.
|
|
68
|
+
if (!isOffsetPastTiffHeader(pointerTag.value)) {
|
|
69
|
+
return tags;
|
|
70
|
+
}
|
|
65
71
|
|
|
66
72
|
return objectAssign(
|
|
67
73
|
tags,
|
|
@@ -79,3 +85,7 @@ function readSubIfd(subIfd, tags, dataView, tiffHeaderOffset, byteOrder, include
|
|
|
79
85
|
)
|
|
80
86
|
);
|
|
81
87
|
}
|
|
88
|
+
|
|
89
|
+
function isOffsetPastTiffHeader(value) {
|
|
90
|
+
return (typeof value === 'number') && (value >= TIFF_HEADER_LENGTH) && (value % 1 === 0);
|
|
91
|
+
}
|
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
|
|
@@ -147,9 +191,29 @@ export function getCharacterArray(string) {
|
|
|
147
191
|
return string.split('').map((character) => character.charCodeAt(0));
|
|
148
192
|
}
|
|
149
193
|
|
|
194
|
+
// Guards against memory exhaustion from a crafted file. A real JPEG has at most 255 ICC
|
|
195
|
+
// segments, ExifReader reads at most 1024 XMP ones, and other containers are few.
|
|
196
|
+
export const MAX_METADATA_BLOCKS = 4096;
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Records one metadata block. The list holds at most MAX_METADATA_BLOCKS
|
|
200
|
+
* entries; past that, the last entry is widened to cover each further block
|
|
201
|
+
* and keeps its own type. Does nothing without a list or a type.
|
|
202
|
+
* @param {Array<{type: string, start: number, end: number}>|undefined} metadataBlocks
|
|
203
|
+
* @param {string|undefined} type
|
|
204
|
+
* @param {number} start
|
|
205
|
+
* @param {number} end Exclusive.
|
|
206
|
+
*/
|
|
150
207
|
export function pushMetadataBlock(metadataBlocks, type, start, end) {
|
|
151
|
-
if (metadataBlocks
|
|
208
|
+
if (!metadataBlocks || !type) {
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
if (metadataBlocks.length < MAX_METADATA_BLOCKS) {
|
|
152
212
|
metadataBlocks.push({type, start, end});
|
|
213
|
+
} else {
|
|
214
|
+
const lastBlock = metadataBlocks[metadataBlocks.length - 1];
|
|
215
|
+
lastBlock.start = Math.min(lastBlock.start, start);
|
|
216
|
+
lastBlock.end = Math.max(lastBlock.end, end);
|
|
153
217
|
}
|
|
154
218
|
}
|
|
155
219
|
|
|
@@ -186,6 +250,22 @@ export function setProperty(object, key, value) {
|
|
|
186
250
|
object[key] = value;
|
|
187
251
|
}
|
|
188
252
|
|
|
253
|
+
/**
|
|
254
|
+
* Returns the key to store a tag under whose name comes from an image. A name
|
|
255
|
+
* that is an `Object.prototype` property, such as `hasOwnProperty`, would hide
|
|
256
|
+
* that method on the object holding it, so it gets `_` appended. `__proto__`
|
|
257
|
+
* is kept, since `setProperty` stores it as an own property.
|
|
258
|
+
*
|
|
259
|
+
* @param {string} name The tag name from the image.
|
|
260
|
+
* @returns {string} The name, with `_` appended if it would hide an inherited property.
|
|
261
|
+
*/
|
|
262
|
+
export function getTagKey(name) {
|
|
263
|
+
if (name !== '__proto__' && Object.prototype.hasOwnProperty.call(Object.prototype, name)) {
|
|
264
|
+
return name + '_';
|
|
265
|
+
}
|
|
266
|
+
return name;
|
|
267
|
+
}
|
|
268
|
+
|
|
189
269
|
export function deferInit(object, key, initializer) {
|
|
190
270
|
let initialized = false;
|
|
191
271
|
Object.defineProperty(object, key, {
|
|
@@ -207,47 +287,115 @@ export function deferInit(object, key, initializer) {
|
|
|
207
287
|
}
|
|
208
288
|
|
|
209
289
|
export function getBase64Image(image) {
|
|
290
|
+
if (typeof image === 'string' && typeof btoa !== 'undefined') {
|
|
291
|
+
// Only tests pass a string. btoa reads it as Latin-1, Buffer.from would use UTF-8.
|
|
292
|
+
return btoa(image);
|
|
293
|
+
}
|
|
294
|
+
if (hasBufferFrom()) {
|
|
295
|
+
return Buffer.from(image).toString('base64'); // eslint-disable-line no-undef
|
|
296
|
+
}
|
|
210
297
|
if (typeof btoa !== 'undefined') {
|
|
211
|
-
|
|
212
|
-
// This only happens during the build tests using Node 16+ (npm run test:build).
|
|
213
|
-
return btoa(image);
|
|
214
|
-
}
|
|
215
|
-
// IE11- does not implement reduce on the Uint8Array prototype.
|
|
216
|
-
return btoa(Array.prototype.reduce.call(new Uint8Array(image), (data, byte) => data + String.fromCharCode(byte), ''));
|
|
298
|
+
return btoa(getByteString(new Uint8Array(image)));
|
|
217
299
|
}
|
|
218
300
|
if (typeof Buffer === 'undefined') {
|
|
219
301
|
return undefined;
|
|
220
302
|
}
|
|
221
|
-
if (typeof Buffer.from !== 'undefined') { // eslint-disable-line no-undef
|
|
222
|
-
return Buffer.from(image).toString('base64'); // eslint-disable-line no-undef
|
|
223
|
-
}
|
|
224
303
|
return (new Buffer(image)).toString('base64'); // eslint-disable-line no-undef
|
|
225
304
|
}
|
|
226
305
|
|
|
306
|
+
function hasBufferFrom() {
|
|
307
|
+
// Buffer polyfills before buffer@4.6.0 inherit Uint8Array.from, which returns an empty array for an ArrayBuffer.
|
|
308
|
+
return typeof Buffer !== 'undefined' && typeof Buffer.from !== 'undefined' && Buffer.from !== Uint8Array.from; // eslint-disable-line no-undef
|
|
309
|
+
}
|
|
310
|
+
|
|
227
311
|
export function dataUriToBuffer(dataUri) {
|
|
228
|
-
const
|
|
312
|
+
const commaIndex = dataUri.indexOf(',');
|
|
313
|
+
const header = dataUri.substring(0, commaIndex);
|
|
314
|
+
const data = dataUri.substring(commaIndex + 1);
|
|
229
315
|
|
|
230
|
-
if (
|
|
316
|
+
if (header.indexOf(';base64') !== -1) {
|
|
317
|
+
const base64 = percentDecodeBase64Payload(data);
|
|
231
318
|
if (typeof atob !== 'undefined') {
|
|
232
|
-
return
|
|
319
|
+
return binaryStringToArrayBuffer(atob(base64));
|
|
233
320
|
}
|
|
234
321
|
if (typeof Buffer === 'undefined') {
|
|
235
322
|
return undefined;
|
|
236
323
|
}
|
|
237
324
|
if (typeof Buffer.from !== 'undefined') { // eslint-disable-line no-undef
|
|
238
|
-
return Buffer.from(
|
|
325
|
+
return Buffer.from(base64, 'base64'); // eslint-disable-line no-undef
|
|
239
326
|
}
|
|
240
|
-
return new Buffer(
|
|
327
|
+
return new Buffer(base64, 'base64'); // eslint-disable-line no-undef
|
|
241
328
|
}
|
|
242
329
|
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
330
|
+
return percentDecodeToBytes(data).buffer;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
// WHATWG data: URL processing percent-decodes the body before the forgiving-base64 decode.
|
|
334
|
+
// A non-ASCII payload can never be valid base64, so it goes to the decoder unchanged.
|
|
335
|
+
function percentDecodeBase64Payload(data) {
|
|
336
|
+
if (data.indexOf('%') === -1 || /[\u0080-\uffff]/.test(data)) {
|
|
337
|
+
return data;
|
|
338
|
+
}
|
|
339
|
+
return getByteString(percentDecodeToBytes(data));
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
function binaryStringToArrayBuffer(string) {
|
|
343
|
+
const bytes = new Uint8Array(string.length);
|
|
344
|
+
for (let i = 0; i < string.length; i++) {
|
|
345
|
+
bytes[i] = string.charCodeAt(i);
|
|
346
|
+
}
|
|
347
|
+
return bytes.buffer;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
// WHATWG URL "string percent-decode" (used for data: URLs, RFC 2397): UTF-8 encode, then each %XX is one byte.
|
|
351
|
+
function percentDecodeToBytes(string) {
|
|
352
|
+
const byteString = /[\u0080-\uffff]/.test(string) ? unescape(encodeURIComponent(string)) : string;
|
|
353
|
+
const bytes = new Uint8Array(byteString.length - 2 * countPercentEscapes(byteString));
|
|
354
|
+
let byteIndex = 0;
|
|
355
|
+
for (let i = 0; i < byteString.length; i++) {
|
|
356
|
+
if (isPercentEscape(byteString, i)) {
|
|
357
|
+
bytes[byteIndex++] = getHexDigitValue(byteString.charCodeAt(i + 1)) * 16 + getHexDigitValue(byteString.charCodeAt(i + 2));
|
|
358
|
+
i += 2;
|
|
359
|
+
} else {
|
|
360
|
+
bytes[byteIndex++] = byteString.charCodeAt(i);
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
return bytes;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
function countPercentEscapes(byteString) {
|
|
367
|
+
let count = 0;
|
|
368
|
+
for (let i = 0; i < byteString.length; i++) {
|
|
369
|
+
if (isPercentEscape(byteString, i)) {
|
|
370
|
+
count++;
|
|
371
|
+
i += 2;
|
|
247
372
|
}
|
|
248
|
-
return new Buffer(decodedData); // eslint-disable-line no-undef
|
|
249
373
|
}
|
|
250
|
-
return
|
|
374
|
+
return count;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
function isPercentEscape(byteString, index) {
|
|
378
|
+
return byteString.charCodeAt(index) === 0x25
|
|
379
|
+
&& index + 2 < byteString.length
|
|
380
|
+
&& getHexDigitValue(byteString.charCodeAt(index + 1)) !== -1
|
|
381
|
+
&& getHexDigitValue(byteString.charCodeAt(index + 2)) !== -1;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/**
|
|
385
|
+
* @param {number} charCode
|
|
386
|
+
* @returns {number} The value of the hex digit, or -1 for any other character.
|
|
387
|
+
*/
|
|
388
|
+
export function getHexDigitValue(charCode) {
|
|
389
|
+
if (charCode >= 0x30 && charCode <= 0x39) {
|
|
390
|
+
return charCode - 0x30;
|
|
391
|
+
}
|
|
392
|
+
if (charCode >= 0x41 && charCode <= 0x46) {
|
|
393
|
+
return charCode - 0x41 + 10;
|
|
394
|
+
}
|
|
395
|
+
if (charCode >= 0x61 && charCode <= 0x66) {
|
|
396
|
+
return charCode - 0x61 + 10;
|
|
397
|
+
}
|
|
398
|
+
return -1;
|
|
251
399
|
}
|
|
252
400
|
|
|
253
401
|
export function padStart(string, length, character) {
|
|
@@ -293,8 +441,8 @@ export function decompress(dataView, compressionMethod, encoding, returnType = '
|
|
|
293
441
|
return rejectExceedsMax(budget, maxDecompressedSize);
|
|
294
442
|
}
|
|
295
443
|
|
|
296
|
-
|
|
297
|
-
|
|
444
|
+
const decompressType = getDecompressType(compressionMethod);
|
|
445
|
+
if (decompressConfig && decompressType) {
|
|
298
446
|
const customFn = decompressConfig[decompressType];
|
|
299
447
|
if (typeof customFn === 'function') {
|
|
300
448
|
// Called now, not deferred: the input can view the caller's buffer, which may be reused once load() returns.
|
|
@@ -348,13 +496,23 @@ export function decompress(dataView, compressionMethod, encoding, returnType = '
|
|
|
348
496
|
try {
|
|
349
497
|
return new TextDecoder(encoding).decode(dataView);
|
|
350
498
|
} catch (error) {
|
|
351
|
-
|
|
352
|
-
return
|
|
499
|
+
// Like TextDecoder's decode(), read a missing view as no bytes.
|
|
500
|
+
return dataView ? getByteString(getUint8View(dataView)) : '';
|
|
353
501
|
}
|
|
354
502
|
}
|
|
355
503
|
return dataView;
|
|
356
504
|
}
|
|
357
505
|
|
|
506
|
+
function getDecompressType(compressionMethod) {
|
|
507
|
+
if (compressionMethod === COMPRESSION_METHOD_DEFLATE) {
|
|
508
|
+
return 'deflate';
|
|
509
|
+
}
|
|
510
|
+
if (compressionMethod === COMPRESSION_METHOD_BROTLI) {
|
|
511
|
+
return 'brotli';
|
|
512
|
+
}
|
|
513
|
+
return undefined;
|
|
514
|
+
}
|
|
515
|
+
|
|
358
516
|
function getMaxDecompressedSize(decompressConfig) {
|
|
359
517
|
if (decompressConfig && typeof decompressConfig.maxDecompressedSize === 'number') {
|
|
360
518
|
return decompressConfig.maxDecompressedSize;
|