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
|
@@ -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
|
+
}
|
package/src/xmp-namespaces.js
CHANGED
|
@@ -108,11 +108,11 @@ function scanStartTag(xmlString, fromIndex) {
|
|
|
108
108
|
return {insertionIndex: -1, attributeValueSpans};
|
|
109
109
|
}
|
|
110
110
|
|
|
111
|
-
function hasSubstringAt(xmlString, substring, index) {
|
|
111
|
+
export function hasSubstringAt(xmlString, substring, index) {
|
|
112
112
|
return xmlString.slice(index, index + substring.length) === substring;
|
|
113
113
|
}
|
|
114
114
|
|
|
115
|
-
function skipPast(xmlString, fromIndex, endMarker) {
|
|
115
|
+
export function skipPast(xmlString, fromIndex, endMarker) {
|
|
116
116
|
const endIndex = xmlString.indexOf(endMarker, fromIndex);
|
|
117
117
|
if (endIndex === -1) {
|
|
118
118
|
return xmlString.length;
|
package/src/xmp-tags.js
CHANGED
|
@@ -2,11 +2,12 @@
|
|
|
2
2
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
3
3
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
|
4
4
|
|
|
5
|
-
import {decodeUtf8ByteString,
|
|
5
|
+
import {decodeUtf8ByteString, getByteString, getTagKey, objectAssign, setProperty, tryDecodeUtf8ByteString} from './utils.js';
|
|
6
6
|
import XmpTagNames from './xmp-tag-names.js';
|
|
7
7
|
import DOMParser from './dom-parser.js';
|
|
8
8
|
import TextDecoder from './text-decoder.js';
|
|
9
9
|
import {isMissingNamespaceError, addMissingNamespaces} from './xmp-namespaces.js';
|
|
10
|
+
import {exceedsMarkupBounds} from './xmp-element-depth.js';
|
|
10
11
|
|
|
11
12
|
export default {
|
|
12
13
|
read
|
|
@@ -23,9 +24,18 @@ const COMMENT_NODE = 8;
|
|
|
23
24
|
const PACKET_TRAILER_START = '<?xpacket end="';
|
|
24
25
|
const PACKET_TRAILER_END = '"?>';
|
|
25
26
|
|
|
26
|
-
// Each
|
|
27
|
-
//
|
|
28
|
-
const MAX_NESTING_DEPTH =
|
|
27
|
+
// Each level repeats the descriptions of everything below it, so any part of
|
|
28
|
+
// the packet's text is copied into at most twice this many descriptions.
|
|
29
|
+
const MAX_NESTING_DEPTH = 16;
|
|
30
|
+
|
|
31
|
+
// xmldom 0.9.12 walks one link per enclosing declaring element on every prefix
|
|
32
|
+
// lookup, and the DOM conversion here recurses once per level.
|
|
33
|
+
const MAX_ELEMENT_DEPTH = 256;
|
|
34
|
+
|
|
35
|
+
// Each markup node takes about 2 KB of peak heap in xmldom 0.9.12. With short
|
|
36
|
+
// names and values, reading a packet within this bound into tags peaks below
|
|
37
|
+
// about 800 MB.
|
|
38
|
+
const MAX_MARKUP_NODES = 250000;
|
|
29
39
|
|
|
30
40
|
// Parsing is synchronous and oneLevelDeeper always restores the counter, so
|
|
31
41
|
// one counter serves every read.
|
|
@@ -101,6 +111,8 @@ function combineChunks(dataView, chunks) {
|
|
|
101
111
|
function getBoundedChunkBytes(dataView, chunk) {
|
|
102
112
|
// The bound is the view's own extent, not the buffer's, since the slice
|
|
103
113
|
// starts at the view's byteOffset.
|
|
114
|
+
// dataView can be a DataViewWrapper whose buffer is a Buffer, which a
|
|
115
|
+
// Uint8Array view constructor would copy whole, so the bytes come from slice.
|
|
104
116
|
const bufferLength = dataView.byteLength;
|
|
105
117
|
const byteOffset = dataView.byteOffset || 0;
|
|
106
118
|
const start = Math.min(Math.max(chunk.dataOffset, 0), bufferLength);
|
|
@@ -116,7 +128,12 @@ function readTags(tags, chunkDataView, domParser) {
|
|
|
116
128
|
|
|
117
129
|
const xmpTags = {};
|
|
118
130
|
parseXMPObject(convertToObject(rdf, true, decodeValue), xmpTags, Object.create(null));
|
|
119
|
-
|
|
131
|
+
// A parsed tag named _raw must not replace the packet string.
|
|
132
|
+
delete xmpTags._raw;
|
|
133
|
+
const names = Object.keys(xmpTags);
|
|
134
|
+
for (let i = 0; i < names.length; i++) {
|
|
135
|
+
setProperty(tags, getTagKey(names[i]), xmpTags[names[i]]);
|
|
136
|
+
}
|
|
120
137
|
return true;
|
|
121
138
|
} catch (error) {
|
|
122
139
|
return false;
|
|
@@ -162,7 +179,8 @@ function decodeXmlSource(source) {
|
|
|
162
179
|
// Not valid UTF-8.
|
|
163
180
|
}
|
|
164
181
|
}
|
|
165
|
-
|
|
182
|
+
const bytes = new Uint8Array(source.buffer, source.byteOffset, source.byteLength);
|
|
183
|
+
return decodeByteString(getByteString(bytes));
|
|
166
184
|
}
|
|
167
185
|
|
|
168
186
|
function decodeByteString(byteString) {
|
|
@@ -202,6 +220,9 @@ function trimAfterPacketTrailer(xmlSource) {
|
|
|
202
220
|
}
|
|
203
221
|
|
|
204
222
|
function parseFromString(domParser, xmlString, isRetry = false) {
|
|
223
|
+
if (exceedsMarkupBounds(xmlString, MAX_ELEMENT_DEPTH, MAX_MARKUP_NODES)) {
|
|
224
|
+
throw new ParseError(`XMP elements nested deeper than ${MAX_ELEMENT_DEPTH} levels or more than ${MAX_MARKUP_NODES} markup nodes.`);
|
|
225
|
+
}
|
|
205
226
|
try {
|
|
206
227
|
const doc = domParser.parseFromString(xmlString, 'application/xml');
|
|
207
228
|
const errors = doc.getElementsByTagName('parsererror');
|
|
@@ -369,7 +390,9 @@ function getLocalName(name) {
|
|
|
369
390
|
if (/^MicrosoftPhoto(_\d+_)?:Rating$/i.test(name)) {
|
|
370
391
|
return 'RatingPercent';
|
|
371
392
|
}
|
|
372
|
-
|
|
393
|
+
const localName = name.split(':')[1];
|
|
394
|
+
// No prefix (the name can be in a default namespace) or an empty local part: keep the whole name.
|
|
395
|
+
return localName || name;
|
|
373
396
|
}
|
|
374
397
|
|
|
375
398
|
// A parent describes its members without their description functions, so
|