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/tags-helpers.js
CHANGED
|
@@ -5,22 +5,59 @@
|
|
|
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
|
|
|
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;
|
|
58
|
+
|
|
21
59
|
const getTagValueAt = {
|
|
22
60
|
1: Types.getByteAt,
|
|
23
|
-
2: Types.getAsciiAt,
|
|
24
61
|
3: Types.getShortAt,
|
|
25
62
|
4: Types.getLongAt,
|
|
26
63
|
5: Types.getRationalAt,
|
|
@@ -30,22 +67,34 @@ const getTagValueAt = {
|
|
|
30
67
|
13: Types.getIfdPointerAt
|
|
31
68
|
};
|
|
32
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
|
+
*/
|
|
33
74
|
export function get0thIfdOffset(dataView, tiffHeaderOffset, byteOrder) {
|
|
34
75
|
const offset = tiffHeaderOffset + TIFF_IFD_OFFSET_OFFSET;
|
|
35
76
|
if (offset + Types.getTypeSize('LONG') > dataView.byteLength) {
|
|
36
77
|
return undefined;
|
|
37
78
|
}
|
|
38
|
-
|
|
39
|
-
|
|
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;
|
|
40
85
|
}
|
|
41
86
|
|
|
42
87
|
/**
|
|
43
88
|
* Reads the tags of an IFD, and for a 0th IFD also those of the thumbnail IFD
|
|
44
89
|
* it points to.
|
|
45
90
|
*
|
|
46
|
-
* @param {{remaining: number}} [valueBudget] -
|
|
47
|
-
* decoded tag values
|
|
48
|
-
*
|
|
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.
|
|
49
98
|
* @returns {Object} The read tags, keyed by tag name.
|
|
50
99
|
*/
|
|
51
100
|
export function readIfd(
|
|
@@ -65,10 +114,16 @@ export function readIfd(
|
|
|
65
114
|
|
|
66
115
|
const tags = {};
|
|
67
116
|
const numberOfFields = getNumberOfFields(dataView, offset, byteOrder);
|
|
117
|
+
let ranOutOfIfdEntries = false;
|
|
68
118
|
|
|
69
119
|
offset += FIELD_COUNT_SIZE;
|
|
70
120
|
for (let fieldIndex = 0; fieldIndex < numberOfFields; fieldIndex++) {
|
|
71
121
|
if (offset + FIELD_SIZE > dataView.byteLength) {
|
|
122
|
+
ranOutOfIfdEntries = true;
|
|
123
|
+
break;
|
|
124
|
+
}
|
|
125
|
+
if (!takeIfdEntry(valueBudget)) {
|
|
126
|
+
ranOutOfIfdEntries = true;
|
|
72
127
|
break;
|
|
73
128
|
}
|
|
74
129
|
|
|
@@ -100,9 +155,9 @@ export function readIfd(
|
|
|
100
155
|
offset += FIELD_SIZE;
|
|
101
156
|
}
|
|
102
157
|
|
|
103
|
-
if (Constants.USE_THUMBNAIL && (offset
|
|
158
|
+
if (Constants.USE_THUMBNAIL && !ranOutOfIfdEntries && (offset + Types.getTypeSize('LONG') <= dataView.byteLength)) {
|
|
104
159
|
const nextIfdOffset = Types.getLongAt(dataView, offset, byteOrder);
|
|
105
|
-
if (nextIfdOffset
|
|
160
|
+
if (nextIfdOffset >= TIFF_HEADER_LENGTH && ifdType === IFD_TYPE_0TH) {
|
|
106
161
|
if (tagFilter.shouldParseGroup('thumbnail')) {
|
|
107
162
|
tags['Thumbnail'] = readIfd(
|
|
108
163
|
dataView,
|
|
@@ -123,6 +178,14 @@ export function readIfd(
|
|
|
123
178
|
return tags;
|
|
124
179
|
}
|
|
125
180
|
|
|
181
|
+
function takeIfdEntry(valueBudget) {
|
|
182
|
+
if (valueBudget.ifdEntriesRemaining > 0) {
|
|
183
|
+
valueBudget.ifdEntriesRemaining--;
|
|
184
|
+
return true;
|
|
185
|
+
}
|
|
186
|
+
return false;
|
|
187
|
+
}
|
|
188
|
+
|
|
126
189
|
/**
|
|
127
190
|
* Creates a budget for the total size of the tag values decoded from a buffer.
|
|
128
191
|
*
|
|
@@ -132,11 +195,86 @@ export function readIfd(
|
|
|
132
195
|
* to the input, which is what stops a crafted file from having thousands of
|
|
133
196
|
* tags decode the same bytes over and over.
|
|
134
197
|
*
|
|
198
|
+
* loadView shares one budget across the Exif IFDs, then the maker note, then
|
|
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.
|
|
201
|
+
* Once it is used up, later out-of-slot values decode empty, Make included,
|
|
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.
|
|
211
|
+
*
|
|
135
212
|
* @param {DataView} dataView - The buffer the values are decoded from.
|
|
136
|
-
* @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.
|
|
137
225
|
*/
|
|
138
226
|
export function getValueBudget(dataView) {
|
|
139
|
-
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;
|
|
140
278
|
}
|
|
141
279
|
|
|
142
280
|
function getNumberOfFields(dataView, offset, byteOrder) {
|
|
@@ -167,6 +305,7 @@ function readTag(
|
|
|
167
305
|
const tagCount = Types.getLongAt(dataView, offset + TAG_COUNT_OFFSET, byteOrder);
|
|
168
306
|
let tagValue;
|
|
169
307
|
let tagValueOffset;
|
|
308
|
+
let stringBudget;
|
|
170
309
|
|
|
171
310
|
if (Types.typeSizes[tagType] === undefined || (!includeUnknown && TagNames[ifdType][tagCode] === undefined)) {
|
|
172
311
|
return undefined;
|
|
@@ -178,14 +317,15 @@ function readTag(
|
|
|
178
317
|
}
|
|
179
318
|
|
|
180
319
|
if (tagValueFitsInOffsetSlot(tagType, tagCount)) {
|
|
181
|
-
tagValueOffset = offset + TAG_VALUE_OFFSET;
|
|
182
|
-
tagValue = getTagValue(dataView, tagValueOffset, tagType, tagCount, byteOrder);
|
|
320
|
+
tagValueOffset = offset + TAG_VALUE_OFFSET - offsetOrigin;
|
|
321
|
+
tagValue = getTagValue(dataView, offsetOrigin + tagValueOffset, tagType, tagCount, byteOrder);
|
|
183
322
|
} else {
|
|
184
323
|
tagValueOffset = Types.getLongAt(dataView, offset + TAG_VALUE_OFFSET, byteOrder);
|
|
185
324
|
if (tagValueFitsInDataView(dataView, offsetOrigin, tagValueOffset, tagType, tagCount)) {
|
|
186
325
|
const forceByteType = tagCode === TAG_CODE_IPTC_NAA;
|
|
187
326
|
const boundedTagCount = getBoundedTagCount(valueBudget.remaining, tagType, tagCount);
|
|
188
327
|
valueBudget.remaining -= boundedTagCount * Types.typeSizes[tagType];
|
|
328
|
+
stringBudget = valueBudget;
|
|
189
329
|
tagValue = getTagValue(dataView, offsetOrigin + tagValueOffset, tagType, boundedTagCount, byteOrder, forceByteType);
|
|
190
330
|
} else {
|
|
191
331
|
tagValue = '<faulty value>';
|
|
@@ -193,8 +333,7 @@ function readTag(
|
|
|
193
333
|
}
|
|
194
334
|
|
|
195
335
|
if (tagType === Types.tagTypes['ASCII']) {
|
|
196
|
-
tagValue =
|
|
197
|
-
tagValue = decodeAsciiValue(tagValue);
|
|
336
|
+
tagValue = getAsciiTagValue(tagValue, stringBudget);
|
|
198
337
|
}
|
|
199
338
|
|
|
200
339
|
let tagDescription = tagValue;
|
|
@@ -241,33 +380,42 @@ function tagValueFitsInOffsetSlot(tagType, tagCount) {
|
|
|
241
380
|
}
|
|
242
381
|
|
|
243
382
|
function getTagValue(dataView, offset, type, count, byteOrder, forceByteType = false) {
|
|
244
|
-
|
|
383
|
+
const value = [];
|
|
245
384
|
|
|
246
385
|
if (forceByteType) {
|
|
247
386
|
count = count * Types.typeSizes[type];
|
|
248
387
|
type = Types.tagTypes['BYTE'];
|
|
249
388
|
}
|
|
389
|
+
if (type === Types.tagTypes['ASCII']) {
|
|
390
|
+
return getAsciiBytes(dataView, offset, count);
|
|
391
|
+
}
|
|
250
392
|
for (let valueIndex = 0; valueIndex < count; valueIndex++) {
|
|
251
393
|
value.push(getTagValueAt[type](dataView, offset, byteOrder));
|
|
252
394
|
offset += Types.typeSizes[type];
|
|
253
395
|
}
|
|
254
396
|
|
|
255
|
-
if (
|
|
256
|
-
|
|
257
|
-
} else if (value.length === 1) {
|
|
258
|
-
value = value[0];
|
|
397
|
+
if (value.length === 1) {
|
|
398
|
+
return value[0];
|
|
259
399
|
}
|
|
260
400
|
|
|
261
401
|
return value;
|
|
262
402
|
}
|
|
263
403
|
|
|
404
|
+
function getAsciiBytes(dataView, offset, count) {
|
|
405
|
+
const bytes = new Uint8Array(count);
|
|
406
|
+
for (let i = 0; i < count; i++) {
|
|
407
|
+
bytes[i] = Types.getAsciiAt(dataView, offset + i);
|
|
408
|
+
}
|
|
409
|
+
return bytes;
|
|
410
|
+
}
|
|
411
|
+
|
|
264
412
|
function tagValueFitsInDataView(dataView, offsetOrigin, tagValueOffset, tagType, tagCount) {
|
|
265
413
|
return offsetOrigin + tagValueOffset + Types.typeSizes[tagType] * tagCount <= dataView.byteLength;
|
|
266
414
|
}
|
|
267
415
|
|
|
268
416
|
// Draws each out-of-slot value from the shared budget, so a crafted file
|
|
269
417
|
// cannot have thousands of tags decode the same bytes over and over. Real
|
|
270
|
-
// files
|
|
418
|
+
// files stay below it.
|
|
271
419
|
function getBoundedTagCount(remainingBudget, tagType, tagCount) {
|
|
272
420
|
const boundedCount = Math.min(tagCount, Math.floor(remainingBudget / Types.typeSizes[tagType]));
|
|
273
421
|
if (boundedCount === 1 && tagCount > 1) {
|
|
@@ -279,26 +427,54 @@ function getBoundedTagCount(remainingBudget, tagType, tagCount) {
|
|
|
279
427
|
return boundedCount;
|
|
280
428
|
}
|
|
281
429
|
|
|
282
|
-
function
|
|
283
|
-
|
|
284
|
-
|
|
430
|
+
function getAsciiTagValue(tagValue, stringBudget) {
|
|
431
|
+
if (tagValue instanceof Uint8Array) {
|
|
432
|
+
return getNullSeparatedStrings(tagValue, stringBudget);
|
|
433
|
+
}
|
|
434
|
+
if (typeof tagValue === 'string') {
|
|
435
|
+
return [tagValue];
|
|
436
|
+
}
|
|
437
|
+
// An IPTC-NAA value read as bytes: its numbers have always been joined
|
|
438
|
+
// into one string of decimal digits.
|
|
439
|
+
return tagValue.length > 0 ? [decodeUtf8ByteString(tagValue.join(''))] : [];
|
|
440
|
+
}
|
|
285
441
|
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
442
|
+
// Empty strings are left as holes in the array, so each string keeps the
|
|
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) {
|
|
446
|
+
const strings = [];
|
|
447
|
+
const charCodes = [];
|
|
448
|
+
let hasString = false;
|
|
449
|
+
let stringIndex = 0;
|
|
450
|
+
let stringStart = 0;
|
|
451
|
+
|
|
452
|
+
for (let i = 0; i <= bytes.length; i++) {
|
|
453
|
+
if (i === bytes.length || bytes[i] === 0) {
|
|
454
|
+
if (i > stringStart) {
|
|
455
|
+
if (hasString && !drawExtraString(stringBudget)) {
|
|
456
|
+
break;
|
|
457
|
+
}
|
|
458
|
+
strings[stringIndex] = decodeUtf8ByteString(getByteString(bytes, stringStart, i, charCodes));
|
|
459
|
+
hasString = true;
|
|
460
|
+
}
|
|
461
|
+
stringIndex++;
|
|
462
|
+
stringStart = i + 1;
|
|
293
463
|
}
|
|
294
|
-
tagValue[i] += string[j];
|
|
295
464
|
}
|
|
296
465
|
|
|
297
|
-
return
|
|
466
|
+
return strings;
|
|
298
467
|
}
|
|
299
468
|
|
|
300
|
-
function
|
|
301
|
-
|
|
469
|
+
function drawExtraString(stringBudget) {
|
|
470
|
+
if (stringBudget === undefined) {
|
|
471
|
+
return true;
|
|
472
|
+
}
|
|
473
|
+
if (stringBudget.remaining < BUDGET_BYTES_PER_EXTRA_ASCII_STRING) {
|
|
474
|
+
return false;
|
|
475
|
+
}
|
|
476
|
+
stringBudget.remaining -= BUDGET_BYTES_PER_EXTRA_ASCII_STRING;
|
|
477
|
+
return true;
|
|
302
478
|
}
|
|
303
479
|
|
|
304
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},
|
|
@@ -17,11 +18,19 @@ export default {
|
|
|
17
18
|
read,
|
|
18
19
|
};
|
|
19
20
|
|
|
20
|
-
|
|
21
|
+
// One decoded-value budget is shared by the 0th, sub-, and thumbnail IFD reads
|
|
22
|
+
// so their combined decoded values stay proportional to the input. A caller
|
|
23
|
+
// passes its own to share it with later reads (loadView: maker note, MPF
|
|
24
|
+
// and decompressed Exif).
|
|
25
|
+
function read(
|
|
26
|
+
dataView,
|
|
27
|
+
tiffHeaderOffset,
|
|
28
|
+
includeUnknown,
|
|
29
|
+
computed = false,
|
|
30
|
+
tagFilter = undefined,
|
|
31
|
+
valueBudget = getValueBudget(dataView)
|
|
32
|
+
) {
|
|
21
33
|
const byteOrder = ByteOrder.getByteOrder(dataView, tiffHeaderOffset);
|
|
22
|
-
// One decoded-value budget shared by the 0th, sub-, and thumbnail IFD
|
|
23
|
-
// reads so their combined decoded values stay proportional to the input.
|
|
24
|
-
const valueBudget = getValueBudget(dataView);
|
|
25
34
|
let tags = read0thIfd(dataView, tiffHeaderOffset, byteOrder, includeUnknown, computed, tagFilter, valueBudget);
|
|
26
35
|
for (let i = 0; i < SUB_IFDS.length; i++) {
|
|
27
36
|
tags = readSubIfd(SUB_IFDS[i], tags, dataView, tiffHeaderOffset, byteOrder, includeUnknown, computed, tagFilter, valueBudget);
|
|
@@ -54,6 +63,11 @@ function readSubIfd(subIfd, tags, dataView, tiffHeaderOffset, byteOrder, include
|
|
|
54
63
|
if (pointerTag === undefined) {
|
|
55
64
|
return tags;
|
|
56
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
|
+
}
|
|
57
71
|
|
|
58
72
|
return objectAssign(
|
|
59
73
|
tags,
|
|
@@ -71,3 +85,7 @@ function readSubIfd(subIfd, tags, dataView, tiffHeaderOffset, byteOrder, include
|
|
|
71
85
|
)
|
|
72
86
|
);
|
|
73
87
|
}
|
|
88
|
+
|
|
89
|
+
function isOffsetPastTiffHeader(value) {
|
|
90
|
+
return (typeof value === 'number') && (value >= TIFF_HEADER_LENGTH) && (value % 1 === 0);
|
|
91
|
+
}
|
package/src/types.js
CHANGED
|
@@ -29,7 +29,6 @@ const tagTypes = {
|
|
|
29
29
|
};
|
|
30
30
|
|
|
31
31
|
export default {
|
|
32
|
-
getAsciiValue,
|
|
33
32
|
getByteAt,
|
|
34
33
|
getAsciiAt,
|
|
35
34
|
getShortAt,
|
|
@@ -44,10 +43,6 @@ export default {
|
|
|
44
43
|
getTypeSize
|
|
45
44
|
};
|
|
46
45
|
|
|
47
|
-
function getAsciiValue(charArray) {
|
|
48
|
-
return charArray.map((charCode) => String.fromCharCode(charCode));
|
|
49
|
-
}
|
|
50
|
-
|
|
51
46
|
function getByteAt(dataView, offset) {
|
|
52
47
|
return dataView.getUint8(offset);
|
|
53
48
|
}
|