exifreader 4.47.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 +22 -22
- 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/package.json +2 -2
- package/src/composite.js +94 -47
- package/src/exif-reader.js +54 -8
- package/src/file-loaders.js +64 -22
- package/src/image-header-iso-bmff.js +31 -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 +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 +15 -22
- package/src/png-text-tags.js +169 -42
- package/src/tag-filter-config.js +2 -0
- package/src/tags-helpers.js +177 -50
- package/src/tags.js +10 -0
- package/src/utils.js +149 -15
- 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 +30 -7
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
|
|
@@ -186,6 +230,22 @@ export function setProperty(object, key, value) {
|
|
|
186
230
|
object[key] = value;
|
|
187
231
|
}
|
|
188
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
|
+
|
|
189
249
|
export function deferInit(object, key, initializer) {
|
|
190
250
|
let initialized = false;
|
|
191
251
|
Object.defineProperty(object, key, {
|
|
@@ -225,29 +285,93 @@ export function getBase64Image(image) {
|
|
|
225
285
|
}
|
|
226
286
|
|
|
227
287
|
export function dataUriToBuffer(dataUri) {
|
|
228
|
-
const
|
|
288
|
+
const commaIndex = dataUri.indexOf(',');
|
|
289
|
+
const header = dataUri.substring(0, commaIndex);
|
|
290
|
+
const data = dataUri.substring(commaIndex + 1);
|
|
229
291
|
|
|
230
|
-
if (
|
|
292
|
+
if (header.indexOf(';base64') !== -1) {
|
|
293
|
+
const base64 = percentDecodeBase64Payload(data);
|
|
231
294
|
if (typeof atob !== 'undefined') {
|
|
232
|
-
return
|
|
295
|
+
return binaryStringToArrayBuffer(atob(base64));
|
|
233
296
|
}
|
|
234
297
|
if (typeof Buffer === 'undefined') {
|
|
235
298
|
return undefined;
|
|
236
299
|
}
|
|
237
300
|
if (typeof Buffer.from !== 'undefined') { // eslint-disable-line no-undef
|
|
238
|
-
return Buffer.from(
|
|
301
|
+
return Buffer.from(base64, 'base64'); // eslint-disable-line no-undef
|
|
239
302
|
}
|
|
240
|
-
return new Buffer(
|
|
303
|
+
return new Buffer(base64, 'base64'); // eslint-disable-line no-undef
|
|
241
304
|
}
|
|
242
305
|
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
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;
|
|
247
348
|
}
|
|
248
|
-
return new Buffer(decodedData); // eslint-disable-line no-undef
|
|
249
349
|
}
|
|
250
|
-
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;
|
|
251
375
|
}
|
|
252
376
|
|
|
253
377
|
export function padStart(string, length, character) {
|
|
@@ -293,8 +417,8 @@ export function decompress(dataView, compressionMethod, encoding, returnType = '
|
|
|
293
417
|
return rejectExceedsMax(budget, maxDecompressedSize);
|
|
294
418
|
}
|
|
295
419
|
|
|
296
|
-
|
|
297
|
-
|
|
420
|
+
const decompressType = getDecompressType(compressionMethod);
|
|
421
|
+
if (decompressConfig && decompressType) {
|
|
298
422
|
const customFn = decompressConfig[decompressType];
|
|
299
423
|
if (typeof customFn === 'function') {
|
|
300
424
|
// Called now, not deferred: the input can view the caller's buffer, which may be reused once load() returns.
|
|
@@ -355,6 +479,16 @@ export function decompress(dataView, compressionMethod, encoding, returnType = '
|
|
|
355
479
|
return dataView;
|
|
356
480
|
}
|
|
357
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
|
+
|
|
358
492
|
function getMaxDecompressedSize(decompressConfig) {
|
|
359
493
|
if (decompressConfig && typeof decompressConfig.maxDecompressedSize === 'number') {
|
|
360
494
|
return decompressConfig.maxDecompressedSize;
|
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
|
+
}
|