exifreader 4.45.1 → 4.46.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/dist/exif-reader.js +1 -1
- package/dist/exif-reader.js.map +1 -1
- package/exif-reader.d.ts +7 -3
- package/package.json +5 -5
- package/src/dataview.js +4 -0
- package/src/exif-reader.js +6 -2
- package/src/icc-tag-names.js +10 -1
- package/src/icc-tags.js +30 -4
- package/src/image-header-iso-bmff-iloc.js +4 -0
- package/src/image-header-iso-bmff-utils.js +4 -0
- package/src/image-header-iso-bmff.js +4 -0
- package/src/load-auto.js +2 -1
- package/src/loadview-pipeline.js +3 -6
- package/src/png-text-tags.js +4 -1
- package/src/tag-decoder.js +2 -9
- package/src/tags-helpers.js +2 -5
- package/src/utils.js +29 -0
- package/src/xmp-namespaces.js +4 -0
- package/src/xmp-tags.js +58 -19
package/exif-reader.d.ts
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
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
|
+
|
|
1
5
|
/// <reference types="node" />
|
|
2
6
|
/// <reference lib="es2017.sharedmemory" />
|
|
3
7
|
|
|
@@ -230,9 +234,9 @@ export interface ExpandedTags {
|
|
|
230
234
|
iptc?: ExifTags,
|
|
231
235
|
xmp?: {
|
|
232
236
|
/**
|
|
233
|
-
* The raw, unsanitized XMP packet
|
|
234
|
-
* Treat it as untrusted: escape or sanitize it before
|
|
235
|
-
* a page as HTML to avoid cross-site scripting.
|
|
237
|
+
* The raw, unsanitized XMP packet as it appears in the image, decoded
|
|
238
|
+
* as text. Treat it as untrusted: escape or sanitize it before
|
|
239
|
+
* inserting it into a page as HTML to avoid cross-site scripting.
|
|
236
240
|
*/
|
|
237
241
|
_raw: string
|
|
238
242
|
} & XmpTags,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "exifreader",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.46.0",
|
|
4
4
|
"description": "Library that parses Exif metadata in images.",
|
|
5
5
|
"author": "Mattias Wallander <mattias@wallander.eu>",
|
|
6
6
|
"repository": {
|
|
@@ -32,24 +32,24 @@
|
|
|
32
32
|
"@babel/preset-env": "^7.29.7",
|
|
33
33
|
"@cypress/webpack-preprocessor": "^8.0.0",
|
|
34
34
|
"@eslint/js": "^9.39.5",
|
|
35
|
-
"@types/node": "^26.
|
|
35
|
+
"@types/node": "^26.6.2",
|
|
36
36
|
"babel-loader": "^10.0.0",
|
|
37
37
|
"c8": "^12.0.0",
|
|
38
38
|
"chai": "^6.2.2",
|
|
39
39
|
"cross-env": "^10.0.0",
|
|
40
|
-
"cypress": "^16.
|
|
40
|
+
"cypress": "^16.1.0",
|
|
41
41
|
"eslint": "^9.39.4",
|
|
42
42
|
"eslint-plugin-cypress": "^6.4.4",
|
|
43
43
|
"globals": "^17.12.0",
|
|
44
44
|
"husky": "^9.0.11",
|
|
45
45
|
"linkedom": "^0.18.13",
|
|
46
|
-
"mocha": "^12.0.
|
|
46
|
+
"mocha": "^12.0.2",
|
|
47
47
|
"npm-run-all2": "^9.0.3",
|
|
48
48
|
"rimraf": "^5.0.5",
|
|
49
49
|
"string-replace-loader": "^3.1.0",
|
|
50
50
|
"terser-webpack-plugin": "^5.6.1",
|
|
51
51
|
"typescript": "^7.0.2",
|
|
52
|
-
"webpack": "5.111.
|
|
52
|
+
"webpack": "5.111.1",
|
|
53
53
|
"webpack-cli": "^7.2.3",
|
|
54
54
|
"webpack-dev-server": "^5.2.6"
|
|
55
55
|
},
|
package/src/dataview.js
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
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
|
+
|
|
1
5
|
export default class DataView {
|
|
2
6
|
constructor(buffer) {
|
|
3
7
|
if (bufferTypeIsUnsupported(buffer)) {
|
package/src/exif-reader.js
CHANGED
|
@@ -648,7 +648,9 @@ export function loadView(
|
|
|
648
648
|
}
|
|
649
649
|
}
|
|
650
650
|
|
|
651
|
-
|
|
651
|
+
if (Constants.USE_EXIF) {
|
|
652
|
+
mergeSteps.push({type: 'gps'});
|
|
653
|
+
}
|
|
652
654
|
if (Constants.USE_EXIF || Constants.USE_XMP) {
|
|
653
655
|
mergeSteps.push({type: 'composite'});
|
|
654
656
|
}
|
|
@@ -672,7 +674,9 @@ export function loadView(
|
|
|
672
674
|
hasPngTextData,
|
|
673
675
|
filterTagsForParse,
|
|
674
676
|
filterTagsForReturn,
|
|
675
|
-
|
|
677
|
+
// A plain property here would keep the function alive for the bundler,
|
|
678
|
+
// so a build without Exif would still carry getGpsGroupFromExifTags.
|
|
679
|
+
getGpsGroupFromExifTags: Constants.USE_EXIF ? getGpsGroupFromExifTags : undefined,
|
|
676
680
|
// A plain property here would keep the import alive for the bundler, so
|
|
677
681
|
// a build with neither Exif nor XMP would still carry composite.js.
|
|
678
682
|
Composite: (Constants.USE_EXIF || Constants.USE_XMP) ? Composite : undefined,
|
package/src/icc-tag-names.js
CHANGED
|
@@ -119,8 +119,17 @@ function parseDate(dataView, offset) {
|
|
|
119
119
|
return new Date(Date.UTC(year, month, day, hours, minutes, seconds));
|
|
120
120
|
}
|
|
121
121
|
|
|
122
|
+
// Engines cap the number of arguments Function.prototype.apply can pass
|
|
123
|
+
// through, so a large slice is converted in fixed-size chunks.
|
|
124
|
+
const MAX_CHARS_PER_CALL = 8192;
|
|
125
|
+
|
|
122
126
|
export function sliceToString(slice) {
|
|
123
|
-
|
|
127
|
+
const bytes = new Uint8Array(slice);
|
|
128
|
+
const chunks = [];
|
|
129
|
+
for (let offset = 0; offset < bytes.length; offset += MAX_CHARS_PER_CALL) {
|
|
130
|
+
chunks.push(String.fromCharCode.apply(null, bytes.subarray(offset, offset + MAX_CHARS_PER_CALL)));
|
|
131
|
+
}
|
|
132
|
+
return chunks.join('');
|
|
124
133
|
}
|
|
125
134
|
|
|
126
135
|
function toCompany(value) {
|
package/src/icc-tags.js
CHANGED
|
@@ -16,6 +16,7 @@ const TAG_TYPE_MULTI_LOCALIZED_UNICODE_TYPE = 'mluc';
|
|
|
16
16
|
const TAG_TYPE_TEXT = 'text';
|
|
17
17
|
const TAG_TYPE_SIGNATURE = 'sig ';
|
|
18
18
|
const TAG_TABLE_SINGLE_TAG_DATA = 12;
|
|
19
|
+
const DESC_VALUE_SIZE_OFFSET_END = 12;
|
|
19
20
|
const MIN_MULTI_LOCALIZED_UNICODE_RECORD_SIZE = 12;
|
|
20
21
|
const MULTI_LOCALIZED_UNICODE_RECORDS_OFFSET = 16;
|
|
21
22
|
const MAX_MLUC_RECORDS = 1000;
|
|
@@ -40,7 +41,10 @@ function readCompressedIcc(dataView, iccData, decompressConfig) {
|
|
|
40
41
|
return {};
|
|
41
42
|
}
|
|
42
43
|
const byteOffset = dataView.byteOffset || 0;
|
|
43
|
-
const
|
|
44
|
+
const slice = dataView.buffer.slice(byteOffset + iccData[0].offset, byteOffset + iccData[0].offset + iccData[0].length);
|
|
45
|
+
// On the Node Buffer wrapper, slice() returns a Buffer, which DataView
|
|
46
|
+
// rejects. Uint8Array copies a Buffer and leaves an ArrayBuffer as is.
|
|
47
|
+
const compressedDataView = new DataView(new Uint8Array(slice).buffer);
|
|
44
48
|
return decompress(compressedDataView, iccData[0].compressionMethod, 'utf-8', 'dataview', decompressConfig)
|
|
45
49
|
.then(parseTags)
|
|
46
50
|
.catch(() => ({}));
|
|
@@ -61,9 +65,10 @@ function readIcc(dataView, iccData) {
|
|
|
61
65
|
|
|
62
66
|
const iccBinaryData = new Uint8Array(totalIccProfileLength);
|
|
63
67
|
let offset = 0;
|
|
68
|
+
const chunksByNumber = getChunksByNumber(iccData);
|
|
64
69
|
|
|
65
70
|
for (let chunkNumber = 1; chunkNumber <= iccData.length; chunkNumber++) {
|
|
66
|
-
const iccDataChunk =
|
|
71
|
+
const iccDataChunk = chunksByNumber[chunkNumber];
|
|
67
72
|
if (!iccDataChunk) {
|
|
68
73
|
throw new Error(`ICC chunk ${chunkNumber} not found`);
|
|
69
74
|
}
|
|
@@ -88,6 +93,18 @@ function getBuffer(dataView) {
|
|
|
88
93
|
return dataView.buffer;
|
|
89
94
|
}
|
|
90
95
|
|
|
96
|
+
// A crafted file can repeat a chunk number. The first descriptor with that
|
|
97
|
+
// number is the one used.
|
|
98
|
+
function getChunksByNumber(iccData) {
|
|
99
|
+
const chunksByNumber = Object.create(null);
|
|
100
|
+
for (let i = 0; i < iccData.length; i++) {
|
|
101
|
+
if (chunksByNumber[iccData[i].chunkNumber] === undefined) {
|
|
102
|
+
chunksByNumber[iccData[i].chunkNumber] = iccData[i];
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return chunksByNumber;
|
|
106
|
+
}
|
|
107
|
+
|
|
91
108
|
function iccDoesNotHaveTagCount(dataView) {
|
|
92
109
|
return dataView.byteLength < (ICC_TAG_COUNT_OFFSET + 4);
|
|
93
110
|
}
|
|
@@ -158,6 +175,10 @@ export function parseTags(dataView) {
|
|
|
158
175
|
const tagType = getStringFromDataView(dataView, tagOffset, 4);
|
|
159
176
|
|
|
160
177
|
if (tagType === TAG_TYPE_DESC) {
|
|
178
|
+
if (tagOffset + DESC_VALUE_SIZE_OFFSET_END > dataView.byteLength) {
|
|
179
|
+
// Tag data is invalid, lets return what we managed to parse
|
|
180
|
+
return tags;
|
|
181
|
+
}
|
|
161
182
|
const tagValueSize = dataView.getUint32(tagOffset + 8);
|
|
162
183
|
if (tagValueSize > tagSize) {
|
|
163
184
|
// Tag data is invalid, lets return what we managed to parse
|
|
@@ -167,6 +188,10 @@ export function parseTags(dataView) {
|
|
|
167
188
|
const val = readBoundedString(dataView, tagOffset + 12, tagValueSize - 1, decodeBudget);
|
|
168
189
|
addTag(tags, tagSignature, val);
|
|
169
190
|
} else if (tagType === TAG_TYPE_MULTI_LOCALIZED_UNICODE_TYPE) {
|
|
191
|
+
if (tagOffset + MULTI_LOCALIZED_UNICODE_RECORDS_OFFSET > dataView.byteLength) {
|
|
192
|
+
// Tag data is invalid, lets return what we managed to parse
|
|
193
|
+
return tags;
|
|
194
|
+
}
|
|
170
195
|
const numRecords = dataView.getUint32(tagOffset + 8);
|
|
171
196
|
const recordSize = dataView.getUint32(tagOffset + 12);
|
|
172
197
|
if (recordSize < MIN_MULTI_LOCALIZED_UNICODE_RECORD_SIZE) {
|
|
@@ -216,8 +241,9 @@ export function parseTags(dataView) {
|
|
|
216
241
|
addTag(tags, tagSignature, valObj);
|
|
217
242
|
}
|
|
218
243
|
} else if (tagType === TAG_TYPE_TEXT) {
|
|
219
|
-
const val = readBoundedString(dataView, tagOffset + 8, tagSize -
|
|
220
|
-
|
|
244
|
+
const val = readBoundedString(dataView, tagOffset + 8, tagSize - 8, decodeBudget);
|
|
245
|
+
const nulIndex = val.indexOf('\0');
|
|
246
|
+
addTag(tags, tagSignature, nulIndex === -1 ? val : val.slice(0, nulIndex));
|
|
221
247
|
} else if (tagType === TAG_TYPE_SIGNATURE) {
|
|
222
248
|
const val = sliceToString(buffer.slice(tagOffset + 8, tagOffset + 12));
|
|
223
249
|
addTag(tags, tagSignature, val);
|
|
@@ -1,3 +1,7 @@
|
|
|
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
|
+
|
|
1
5
|
import {get64BitValue} from './image-header-iso-bmff-utils.js';
|
|
2
6
|
|
|
3
7
|
// Absolute backstop on the number of extent objects a single iloc box may
|
|
@@ -1,3 +1,7 @@
|
|
|
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
|
+
|
|
1
5
|
export function get64BitValue(dataView, offset) {
|
|
2
6
|
// It's a bit tricky to handle 64 bit numbers in JavaScript. Let's
|
|
3
7
|
// wait until there are real-world examples where it is necessary.
|
|
@@ -1,3 +1,7 @@
|
|
|
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
|
+
|
|
1
5
|
import Constants from './constants.js';
|
|
2
6
|
import {getNullTerminatedStringFromDataView, getStringFromDataView, pushMetadataBlock} from './utils.js';
|
|
3
7
|
// import {get64BitValue} from './image-header-iso-bmff-utils.js';
|
package/src/load-auto.js
CHANGED
|
@@ -284,7 +284,8 @@ function sliceInputBuffer(data, end) {
|
|
|
284
284
|
return data.slice(0, end);
|
|
285
285
|
}
|
|
286
286
|
if (ArrayBuffer.isView(data)) {
|
|
287
|
-
|
|
287
|
+
const viewEnd = Math.max(0, Math.min(end, data.byteLength));
|
|
288
|
+
return data.buffer.slice(data.byteOffset, data.byteOffset + viewEnd);
|
|
288
289
|
}
|
|
289
290
|
return data;
|
|
290
291
|
}
|
package/src/loadview-pipeline.js
CHANGED
|
@@ -222,7 +222,7 @@ export function applyMergeStep({
|
|
|
222
222
|
return tags;
|
|
223
223
|
}
|
|
224
224
|
|
|
225
|
-
if (step.type === 'gps') {
|
|
225
|
+
if (Constants.USE_EXIF && step.type === 'gps') {
|
|
226
226
|
if (
|
|
227
227
|
expanded
|
|
228
228
|
&& tagFilter.shouldReturnGroup('gps')
|
|
@@ -282,14 +282,11 @@ export function applyMergeStep({
|
|
|
282
282
|
return tags;
|
|
283
283
|
}
|
|
284
284
|
|
|
285
|
-
const parsedThumbnailIfdTags =
|
|
285
|
+
const parsedThumbnailIfdTags = deps.filterTagsForParse(
|
|
286
286
|
'thumbnail',
|
|
287
287
|
thumbnailIfdTags,
|
|
288
288
|
tagFilter
|
|
289
|
-
)
|
|
290
|
-
if (parsedThumbnailIfdTags) {
|
|
291
|
-
parsedGroups.thumbnail = parsedThumbnailIfdTags;
|
|
292
|
-
}
|
|
289
|
+
);
|
|
293
290
|
|
|
294
291
|
const thumbnail = Constants.USE_EXIF
|
|
295
292
|
&& Constants.USE_THUMBNAIL
|
package/src/png-text-tags.js
CHANGED
|
@@ -119,7 +119,10 @@ function getNameAndValue(dataView, offset, length, type, async, decompressConfig
|
|
|
119
119
|
parsingState = moveToNextState(type, parsingState);
|
|
120
120
|
continue;
|
|
121
121
|
} else if (parsingState === STATE_TEXT) {
|
|
122
|
-
|
|
122
|
+
const slice = dataView.buffer.slice(byteOffset + offset + i, byteOffset + offset + length);
|
|
123
|
+
// On the Node Buffer wrapper, slice() returns a Buffer, which DataView
|
|
124
|
+
// rejects. Uint8Array copies a Buffer and leaves an ArrayBuffer as is.
|
|
125
|
+
valueChars = new DataView(new Uint8Array(slice).buffer);
|
|
123
126
|
break;
|
|
124
127
|
}
|
|
125
128
|
const byte = dataView.getUint8(offset + i);
|
package/src/tag-decoder.js
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
|
4
4
|
|
|
5
5
|
import TextDecoder from './text-decoder.js';
|
|
6
|
+
import {decodeUtf8ByteString} from './utils.js';
|
|
6
7
|
|
|
7
8
|
const TAG_HEADER_SIZE = 5;
|
|
8
9
|
|
|
@@ -30,7 +31,7 @@ export default {
|
|
|
30
31
|
|
|
31
32
|
function decode(encoding, tagValue) {
|
|
32
33
|
if (typeof tagValue === 'string') {
|
|
33
|
-
return
|
|
34
|
+
return decodeUtf8ByteString(tagValue);
|
|
34
35
|
}
|
|
35
36
|
const Decoder = TextDecoder.get();
|
|
36
37
|
if ((typeof Decoder !== 'undefined') && (encoding !== undefined)) {
|
|
@@ -66,11 +67,3 @@ function decodeWindows1252(tagValue) {
|
|
|
66
67
|
}
|
|
67
68
|
return String.fromCharCode.apply(null, codePoints);
|
|
68
69
|
}
|
|
69
|
-
|
|
70
|
-
function decodeAsciiValue(asciiValue) {
|
|
71
|
-
try {
|
|
72
|
-
return decodeURIComponent(escape(asciiValue));
|
|
73
|
-
} catch (error) {
|
|
74
|
-
return asciiValue;
|
|
75
|
-
}
|
|
76
|
-
}
|
package/src/tags-helpers.js
CHANGED
|
@@ -7,6 +7,7 @@ import Types from './types.js';
|
|
|
7
7
|
import TagNames, {IFD_TYPE_0TH, IFD_TYPE_1ST, IFD_TYPE_PENTAX} from './tag-names.js';
|
|
8
8
|
import {IFD_ENTRY_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';
|
|
10
11
|
|
|
11
12
|
// Measured across the test corpus, the most demanding parse decodes tag
|
|
12
13
|
// values totalling 1.2241 times the size of the buffer they are read from
|
|
@@ -297,11 +298,7 @@ function splitNullSeparatedAsciiString(string) {
|
|
|
297
298
|
}
|
|
298
299
|
|
|
299
300
|
function decodeAsciiValue(asciiValue) {
|
|
300
|
-
|
|
301
|
-
return asciiValue.map((value) => decodeURIComponent(escape(value)));
|
|
302
|
-
} catch (error) {
|
|
303
|
-
return asciiValue;
|
|
304
|
-
}
|
|
301
|
+
return asciiValue.map((value) => decodeUtf8ByteString(value));
|
|
305
302
|
}
|
|
306
303
|
|
|
307
304
|
function getDescriptionFromTagValue(tagValue) {
|
package/src/utils.js
CHANGED
|
@@ -57,6 +57,35 @@ export function getStringValueFromArray(charArray) {
|
|
|
57
57
|
return charArray.map((charCode) => String.fromCharCode(charCode)).join('');
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
+
/**
|
|
61
|
+
* Decodes a byte string (one byte per character) as UTF-8. A string that is
|
|
62
|
+
* not valid UTF-8 is returned as it is, so single-byte encoded text stays
|
|
63
|
+
* readable.
|
|
64
|
+
* @param {string} byteString
|
|
65
|
+
* @returns {string}
|
|
66
|
+
*/
|
|
67
|
+
export function decodeUtf8ByteString(byteString) {
|
|
68
|
+
const decoded = tryDecodeUtf8ByteString(byteString);
|
|
69
|
+
if (decoded === undefined) {
|
|
70
|
+
return byteString;
|
|
71
|
+
}
|
|
72
|
+
return decoded;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Decodes a byte string (one byte per character) as UTF-8.
|
|
77
|
+
* @param {string} byteString
|
|
78
|
+
* @returns {string | undefined} The decoded text, or undefined when the string
|
|
79
|
+
* is not valid UTF-8.
|
|
80
|
+
*/
|
|
81
|
+
export function tryDecodeUtf8ByteString(byteString) {
|
|
82
|
+
try {
|
|
83
|
+
return decodeURIComponent(escape(byteString));
|
|
84
|
+
} catch (error) {
|
|
85
|
+
return undefined;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
60
89
|
export function getCharacterArray(string) {
|
|
61
90
|
return string.split('').map((character) => character.charCodeAt(0));
|
|
62
91
|
}
|
package/src/xmp-namespaces.js
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
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
|
+
|
|
1
5
|
export function isMissingNamespaceError(error) {
|
|
2
6
|
const missingNamespaceStrings = [
|
|
3
7
|
// @xmldom/xmldom
|
package/src/xmp-tags.js
CHANGED
|
@@ -2,9 +2,10 @@
|
|
|
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 {getStringFromDataView, objectAssign, setProperty} from './utils.js';
|
|
5
|
+
import {decodeUtf8ByteString, getStringFromDataView, objectAssign, setProperty, tryDecodeUtf8ByteString} from './utils.js';
|
|
6
6
|
import XmpTagNames from './xmp-tag-names.js';
|
|
7
7
|
import DOMParser from './dom-parser.js';
|
|
8
|
+
import TextDecoder from './text-decoder.js';
|
|
8
9
|
import {isMissingNamespaceError, addMissingNamespaces} from './xmp-namespaces.js';
|
|
9
10
|
|
|
10
11
|
export default {
|
|
@@ -90,11 +91,11 @@ function getBoundedChunkBytes(dataView, chunk) {
|
|
|
90
91
|
|
|
91
92
|
function readTags(tags, chunkDataView, domParser) {
|
|
92
93
|
try {
|
|
93
|
-
const {doc, raw} = getDocument(chunkDataView, domParser);
|
|
94
|
+
const {doc, raw, decodeValue} = getDocument(chunkDataView, domParser);
|
|
94
95
|
tags._raw = (tags._raw || '') + raw;
|
|
95
96
|
const rdf = getRDF(doc);
|
|
96
97
|
|
|
97
|
-
objectAssign(tags, parseXMPObject(convertToObject(rdf, true)));
|
|
98
|
+
objectAssign(tags, parseXMPObject(convertToObject(rdf, true, decodeValue)));
|
|
98
99
|
return true;
|
|
99
100
|
} catch (error) {
|
|
100
101
|
return false;
|
|
@@ -108,15 +109,53 @@ function getDocument(chunkDataView, _domParser) {
|
|
|
108
109
|
throw new Error();
|
|
109
110
|
}
|
|
110
111
|
|
|
111
|
-
const xmlString =
|
|
112
|
+
const {xmlString, decodeValue} = decodeXmlSource(chunkDataView);
|
|
112
113
|
const doc = parseFromString(domParser, trimXmlSource(xmlString));
|
|
113
114
|
|
|
114
115
|
return {
|
|
115
116
|
doc,
|
|
116
117
|
raw: xmlString,
|
|
118
|
+
decodeValue
|
|
117
119
|
};
|
|
118
120
|
}
|
|
119
121
|
|
|
122
|
+
// The packet has to be decoded before it is parsed. When it is parsed one
|
|
123
|
+
// character per byte, the UTF-8 encoding of e.g. Å (0xC3 0x85) contains a lone
|
|
124
|
+
// U+0085, which xmldom turns into a line feed before parsing (an XML 1.1 line
|
|
125
|
+
// ending). That corrupts element text, and attribute value normalization then
|
|
126
|
+
// makes it a space. XMP is UTF-8 by specification, but some writers store a
|
|
127
|
+
// single-byte encoding. Such a packet is parsed one character per byte and
|
|
128
|
+
// each value is decoded on its own afterwards where it is valid UTF-8.
|
|
129
|
+
function decodeXmlSource(source) {
|
|
130
|
+
if (typeof source === 'string') {
|
|
131
|
+
return decodeByteString(source);
|
|
132
|
+
}
|
|
133
|
+
const Decoder = TextDecoder.get();
|
|
134
|
+
if (Decoder !== undefined) {
|
|
135
|
+
try {
|
|
136
|
+
return {
|
|
137
|
+
xmlString: new Decoder('utf-8', {fatal: true}).decode(source),
|
|
138
|
+
decodeValue: keepValue
|
|
139
|
+
};
|
|
140
|
+
} catch (error) {
|
|
141
|
+
// Not valid UTF-8.
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return decodeByteString(getStringFromDataView(source, 0, source.byteLength));
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function decodeByteString(byteString) {
|
|
148
|
+
const decoded = tryDecodeUtf8ByteString(byteString);
|
|
149
|
+
if (decoded !== undefined) {
|
|
150
|
+
return {xmlString: decoded, decodeValue: keepValue};
|
|
151
|
+
}
|
|
152
|
+
return {xmlString: byteString, decodeValue: decodeUtf8ByteString};
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function keepValue(value) {
|
|
156
|
+
return value;
|
|
157
|
+
}
|
|
158
|
+
|
|
120
159
|
function trimXmlSource(xmlSource) {
|
|
121
160
|
return xmlSource.replace(/^.+(<\?xpacket begin)/, '$1').replace(/(<\?xpacket end=".*"\?>).+$/, '$1');
|
|
122
161
|
}
|
|
@@ -151,17 +190,17 @@ function getRDF(node) {
|
|
|
151
190
|
throw new Error();
|
|
152
191
|
}
|
|
153
192
|
|
|
154
|
-
function convertToObject(node, isTopNode
|
|
193
|
+
function convertToObject(node, isTopNode, decodeValue) {
|
|
155
194
|
const childNodes = getChildNodes(node);
|
|
156
195
|
|
|
157
196
|
if (hasTextOnlyContent(childNodes)) {
|
|
158
197
|
if (isTopNode) {
|
|
159
198
|
return {};
|
|
160
199
|
}
|
|
161
|
-
return getTextValue(childNodes[0]);
|
|
200
|
+
return decodeValue(getTextValue(childNodes[0]));
|
|
162
201
|
}
|
|
163
202
|
|
|
164
|
-
return getElementsFromNodes(childNodes);
|
|
203
|
+
return getElementsFromNodes(childNodes, decodeValue);
|
|
165
204
|
}
|
|
166
205
|
|
|
167
206
|
function getChildNodes(node) {
|
|
@@ -185,12 +224,12 @@ function getTextValue(node) {
|
|
|
185
224
|
// The elements object must not have a prototype. The element names come from
|
|
186
225
|
// the image, so an element named e.g. constructor would be found among the
|
|
187
226
|
// inherited properties, and one named __proto__ would replace the prototype.
|
|
188
|
-
function getElementsFromNodes(nodes) {
|
|
227
|
+
function getElementsFromNodes(nodes, decodeValue) {
|
|
189
228
|
const elements = Object.create(null);
|
|
190
229
|
|
|
191
230
|
nodes.forEach((node) => {
|
|
192
231
|
if (isElement(node)) {
|
|
193
|
-
const nodeElement = getElementFromNode(node);
|
|
232
|
+
const nodeElement = getElementFromNode(node, decodeValue);
|
|
194
233
|
|
|
195
234
|
if (elements[node.nodeName] !== undefined) {
|
|
196
235
|
if (!Array.isArray(elements[node.nodeName])) {
|
|
@@ -210,18 +249,18 @@ function isElement(node) {
|
|
|
210
249
|
return (node.nodeName) && (node.nodeName !== '#text');
|
|
211
250
|
}
|
|
212
251
|
|
|
213
|
-
function getElementFromNode(node) {
|
|
252
|
+
function getElementFromNode(node, decodeValue) {
|
|
214
253
|
return {
|
|
215
|
-
attributes: getAttributes(node),
|
|
216
|
-
value: convertToObject(node)
|
|
254
|
+
attributes: getAttributes(node, decodeValue),
|
|
255
|
+
value: convertToObject(node, false, decodeValue)
|
|
217
256
|
};
|
|
218
257
|
}
|
|
219
258
|
|
|
220
|
-
function getAttributes(element) {
|
|
259
|
+
function getAttributes(element, decodeValue) {
|
|
221
260
|
const attributes = {};
|
|
222
261
|
|
|
223
262
|
for (let i = 0; i < element.attributes.length; i++) {
|
|
224
|
-
setProperty(attributes, element.attributes[i].nodeName,
|
|
263
|
+
setProperty(attributes, element.attributes[i].nodeName, decodeValue(element.attributes[i].value));
|
|
225
264
|
}
|
|
226
265
|
|
|
227
266
|
return attributes;
|
|
@@ -309,14 +348,14 @@ function getDescription(value, name = undefined) {
|
|
|
309
348
|
return getDescriptionOfObject(value);
|
|
310
349
|
}
|
|
311
350
|
|
|
312
|
-
|
|
313
|
-
|
|
351
|
+
if (hasTagNameFunction(name)) {
|
|
352
|
+
try {
|
|
314
353
|
return XmpTagNames[name](value);
|
|
354
|
+
} catch (error) {
|
|
355
|
+
return value;
|
|
315
356
|
}
|
|
316
|
-
return decodeURIComponent(escape(value));
|
|
317
|
-
} catch (error) {
|
|
318
|
-
return value;
|
|
319
357
|
}
|
|
358
|
+
return value;
|
|
320
359
|
}
|
|
321
360
|
|
|
322
361
|
function getDescriptionOfArray(value) {
|