exifreader 4.52.0 → 4.53.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "exifreader",
3
- "version": "4.52.0",
3
+ "version": "4.53.0",
4
4
  "description": "Library that parses Exif metadata in images.",
5
5
  "author": "Mattias Wallander <mattias@wallander.eu>",
6
6
  "repository": {
@@ -59,14 +59,14 @@
59
59
  "eslint-plugin-cypress": "^6.4.4",
60
60
  "globals": "^17.13.0",
61
61
  "husky": "^9.0.11",
62
- "linkedom": "^0.18.13",
62
+ "linkedom": "^0.18.14",
63
63
  "mocha": "^12.0.3",
64
- "npm-run-all2": "^9.0.3",
64
+ "npm-run-all2": "^9.0.4",
65
65
  "rimraf": "^5.0.5",
66
66
  "string-replace-loader": "^3.1.0",
67
67
  "terser-webpack-plugin": "^5.6.1",
68
68
  "typescript": "^7.0.2",
69
- "webpack": "5.112.0",
69
+ "webpack": "5.112.2",
70
70
  "webpack-cli": "^7.2.3",
71
71
  "webpack-dev-server": "^6.1.0"
72
72
  },
package/src/dom-parser.js CHANGED
@@ -4,22 +4,72 @@
4
4
 
5
5
  import NodeRequire from './node-require.js';
6
6
 
7
+ // The XMP depth and markup node limits are checked the way @xmldom/xmldom 0.9.12 reads markup.
8
+ const MIN_XMLDOM_VERSION = [0, 9, 12];
9
+ const NO_PARSER_WARNING = 'Warning: DOMParser is not available. It is needed to be able to parse XMP tags.';
10
+ const XMLDOM_REMEDY = 'ExifReader needs 0.9.12 or later, or a parser passed in the domParser option.';
11
+ const UNREADABLE_VERSION_WARNING = 'Warning: XMP tags are skipped because the version of the @xmldom/xmldom that was found could not be read. ' + XMLDOM_REMEDY;
12
+
7
13
  export default {
8
14
  get
9
15
  };
10
16
 
17
+ /**
18
+ * Finds the DOM parser to read XMP with: the one passed in, a global DOMParser,
19
+ * or an @xmldom/xmldom of version 0.9.12 or later.
20
+ *
21
+ * @param {Object} [domParser] A parser passed in by the caller.
22
+ * @returns {{parser: (Object|undefined), warning: (string|undefined)}} The
23
+ * parser, or no parser and the warning that says why.
24
+ */
11
25
  function get(domParser) {
12
26
  if (domParser) {
13
- return domParser;
27
+ return {parser: domParser};
14
28
  }
15
29
 
16
30
  if (typeof DOMParser !== 'undefined') {
17
- return new DOMParser();
31
+ return {parser: new DOMParser()};
18
32
  }
33
+
34
+ return getXmldomParser();
35
+ }
36
+
37
+ function getXmldomParser() {
19
38
  try {
20
39
  const {DOMParser, onErrorStopParsing} = NodeRequire.optionalPackage('@xmldom/xmldom');
21
- return new DOMParser({onError: onErrorStopParsing});
40
+ const warning = getXmldomVersionWarning(readXmldomVersion());
41
+ if (warning) {
42
+ return {parser: undefined, warning};
43
+ }
44
+ return {parser: new DOMParser({onError: onErrorStopParsing})};
22
45
  } catch (error) {
23
- return undefined;
46
+ return {parser: undefined, warning: NO_PARSER_WARNING};
47
+ }
48
+ }
49
+
50
+ function readXmldomVersion() {
51
+ const packageJson = NodeRequire.optionalPackage('@xmldom/xmldom/package.json');
52
+ return packageJson ? packageJson.version : undefined;
53
+ }
54
+
55
+ function getXmldomVersionWarning(version) {
56
+ const match = typeof version === 'string' ? /^(\d+)\.(\d+)\.(\d+)(-?)/.exec(version) : null;
57
+ if (!match) {
58
+ return UNREADABLE_VERSION_WARNING;
59
+ }
60
+ if (isOlderThanMinimum(match)) {
61
+ return `Warning: XMP tags are skipped because the @xmldom/xmldom that was found is version ${version}. ${XMLDOM_REMEDY}`;
62
+ }
63
+ return undefined;
64
+ }
65
+
66
+ // A prerelease of the minimum version (0.9.12-beta.1) comes before it.
67
+ function isOlderThanMinimum(match) {
68
+ for (let i = 0; i < MIN_XMLDOM_VERSION.length; i++) {
69
+ const part = Number(match[i + 1]);
70
+ if (part !== MIN_XMLDOM_VERSION[i]) {
71
+ return part < MIN_XMLDOM_VERSION[i];
72
+ }
24
73
  }
74
+ return match[4] === '-';
25
75
  }
@@ -926,8 +926,7 @@ function isParsedMetadataBlock(format, blockType) {
926
926
  case 'file':
927
927
  return format === 'png' ? Constants.USE_PNG_FILE : Constants.USE_FILE;
928
928
  case 'exif':
929
- // The PNG finder never gated eXIf, so it is listed in every build.
930
- return format === 'png' || Constants.USE_EXIF;
929
+ return Constants.USE_EXIF;
931
930
  case 'jfif':
932
931
  return Constants.USE_JFIF;
933
932
  case 'xmp':
package/src/load-auto.js CHANGED
@@ -290,9 +290,20 @@ function sliceInputBuffer(data, end) {
290
290
  const viewEnd = Math.max(0, Math.min(end, data.byteLength));
291
291
  return data.buffer.slice(data.byteOffset, data.byteOffset + viewEnd);
292
292
  }
293
+ if (isArrayBufferLike(data)) {
294
+ return data.slice(0, end);
295
+ }
293
296
  return data;
294
297
  }
295
298
 
299
+ // Tells an ArrayBuffer or SharedArrayBuffer from another realm, where instanceof fails.
300
+ function isArrayBufferLike(data) {
301
+ return !!data
302
+ && !data.buffer
303
+ && (typeof data.slice === 'function')
304
+ && (typeof data.byteLength === 'number');
305
+ }
306
+
296
307
  function bufferByteLength(buffer) {
297
308
  if (!buffer) {
298
309
  return 0;
@@ -49,7 +49,7 @@ function firstLoaded(name, loaders) {
49
49
 
50
50
  // Literal names make webpack emit require('fs') and so on, which bundlers and
51
51
  // file tracers follow. No typeof guard: webpack may not rewrite the identifier there.
52
- // A try in this function makes bundlers treat fs and @xmldom/xmldom as optional.
52
+ // A try in this function makes bundlers treat fs, @xmldom/xmldom and its package.json as optional.
53
53
  function webpackRequire(name) {
54
54
  switch (name) {
55
55
  case 'fs':
@@ -70,6 +70,13 @@ function webpackRequire(name) {
70
70
  } catch (error) {
71
71
  throw error;
72
72
  }
73
+ case '@xmldom/xmldom/package.json':
74
+ // eslint-disable-next-line no-useless-catch
75
+ try {
76
+ return __non_webpack_require__('@xmldom/xmldom/package.json');
77
+ } catch (error) {
78
+ throw error;
79
+ }
73
80
  default:
74
81
  throw new Error(`No webpack require for ${name}.`);
75
82
  }
@@ -4,7 +4,7 @@
4
4
 
5
5
  import TagNamesCommon from './tag-names-common.js';
6
6
  import {joinMapped} from './utils.js';
7
- import {getTableDescription} from './tag-names-utils.js';
7
+ import {getTableDescription, isStringValue, getStringsText} from './tag-names-utils.js';
8
8
 
9
9
  export default {
10
10
  0x000b: 'ProcessingSoftware',
@@ -385,6 +385,10 @@ function decodeXPValue(value) {
385
385
  // A one-element value arrives as a bare number, which Uint8Array would take as a length.
386
386
  return '';
387
387
  }
388
+ if (isStringValue(value)) {
389
+ // An ASCII-typed value holds strings, not UCS-2 bytes.
390
+ return getStringsText(value);
391
+ }
388
392
  // The XP tags are encoded as UCS-2 which uses two bytes per character but
389
393
  // it's close to UTF-16 so we can use that to decode them.
390
394
  // https://www.loc.gov/preservation/digital/formats/content/tiff_tags.shtml
@@ -7,6 +7,9 @@ import {getStringValueFromArray as getStringValue, getByteString} from './utils.
7
7
  export {getStringValue};
8
8
 
9
9
  export function getEncodedString(value) {
10
+ if (isStringValue(value)) {
11
+ return getStringsText(value) || 'Undefined';
12
+ }
10
13
  if (value.length >= 8) {
11
14
  const encoding = getStringValue(value.slice(0, 8));
12
15
 
@@ -38,14 +41,36 @@ export function getEncodedString(value) {
38
41
  */
39
42
  export function getVersionString(value) {
40
43
  if (Array.isArray(value)) {
41
- if (value.some(isString)) {
42
- return value.filter(isString).join(', ');
44
+ if (isStringValue(value)) {
45
+ return getStringsText(value);
43
46
  }
44
47
  return getByteString(value);
45
48
  }
46
49
  return value;
47
50
  }
48
51
 
52
+ /**
53
+ * Tells whether a value is an array without character codes. Character codes
54
+ * are numbers. An ASCII-typed value holds strings, with a hole where an empty
55
+ * piece was, index 0 included; a rational value holds number pairs. Neither
56
+ * has character codes to convert, and an empty array has none either.
57
+ * @param {*} value
58
+ * @returns {boolean} True for an array whose element 0 is not a number.
59
+ */
60
+ export function isStringValue(value) {
61
+ return Array.isArray(value) && typeof value[0] !== 'number';
62
+ }
63
+
64
+ /**
65
+ * Describes an ASCII-typed value as its strings.
66
+ * @param {Array} value
67
+ * @returns {string} The strings joined with ', ', holes and non-strings
68
+ * skipped, or '' when there are none.
69
+ */
70
+ export function getStringsText(value) {
71
+ return value.filter(isString).join(', ');
72
+ }
73
+
49
74
  function isString(item) {
50
75
  return typeof item === 'string';
51
76
  }
@@ -30,10 +30,10 @@ const MAX_DECOMPRESSED_VALUE_ALLOWANCE = 1024 * 1024;
30
30
  // visit at most about as many entries as the buffer holds.
31
31
  const MAX_IFD_ENTRY_READS_PER_BUFFER_ENTRY = 4;
32
32
 
33
- // Each string after the first of an out-of-slot ASCII value costs this much
34
- // budget. 4 MiB files whose tags use the budget up on short strings peaked at
35
- // up to 107 times their size in RSS without it, 88 with 1 and 76 with 2,
36
- // against 87 for BYTE values.
33
+ // Each string after the first of an out-of-slot ASCII or UTF-8 value costs
34
+ // this much budget. 4 MiB files whose tags use the budget up on short strings
35
+ // peaked at up to 107 times their size in RSS without it, 88 with 1 and 76
36
+ // with 2, against 87 for BYTE values.
37
37
  export const BUDGET_BYTES_PER_EXTRA_ASCII_STRING = 2;
38
38
 
39
39
  // A buffer gets one IPTC dataset unit per this many bytes, and a dataset draws
@@ -60,9 +60,13 @@ const getTagValueAt = {
60
60
  3: Types.getShortAt,
61
61
  4: Types.getLongAt,
62
62
  5: Types.getRationalAt,
63
+ 6: Types.getSbyteAt,
63
64
  7: Types.getUndefinedAt,
65
+ 8: Types.getSshortAt,
64
66
  9: Types.getSlongAt,
65
67
  10: Types.getSrationalAt,
68
+ 11: Types.getFloatAt,
69
+ 12: Types.getDoubleAt,
66
70
  13: Types.getIfdPointerAt
67
71
  };
68
72
 
@@ -341,7 +345,7 @@ function readTag(
341
345
  }
342
346
  }
343
347
 
344
- if (tagType === Types.tagTypes['ASCII']) {
348
+ if (isStringType(tagType)) {
345
349
  tagValue = getAsciiTagValue(tagValue, stringBudget);
346
350
  }
347
351
 
@@ -395,7 +399,7 @@ function getTagValue(dataView, offset, type, count, byteOrder, forceByteType = f
395
399
  count = count * Types.typeSizes[type];
396
400
  type = Types.tagTypes['BYTE'];
397
401
  }
398
- if (type === Types.tagTypes['ASCII']) {
402
+ if (isStringType(type)) {
399
403
  return getAsciiBytes(dataView, offset, count);
400
404
  }
401
405
  for (let valueIndex = 0; valueIndex < count; valueIndex++) {
@@ -436,6 +440,10 @@ function getBoundedTagCount(remainingBudget, tagType, tagCount) {
436
440
  return boundedCount;
437
441
  }
438
442
 
443
+ function isStringType(type) {
444
+ return type === Types.tagTypes['ASCII'] || type === Types.tagTypes['UTF8'];
445
+ }
446
+
439
447
  function getAsciiTagValue(tagValue, stringBudget) {
440
448
  if (tagValue instanceof Uint8Array) {
441
449
  return getNullSeparatedStrings(tagValue, stringBudget);
@@ -494,7 +502,7 @@ function getDescriptionFromTagValue(tagValue) {
494
502
  }
495
503
 
496
504
  function getComputedTagValue(tagType, value) {
497
- if (tagType === Types.tagTypes['ASCII']) {
505
+ if (isStringType(tagType)) {
498
506
  if (Array.isArray(value) && value.length === 1) {
499
507
  return value[0];
500
508
  }
package/src/types.js CHANGED
@@ -10,10 +10,15 @@ const typeSizes = {
10
10
  3: 2, // SHORT
11
11
  4: 4, // LONG
12
12
  5: 8, // RATIONAL
13
+ 6: 1, // SBYTE
13
14
  7: 1, // UNDEFINED
15
+ 8: 2, // SSHORT
14
16
  9: 4, // SLONG
15
17
  10: 8, // SRATIONAL
16
- 13: 4 // IFD
18
+ 11: 4, // FLOAT
19
+ 12: 8, // DOUBLE
20
+ 13: 4, // IFD
21
+ 129: 1 // UTF8
17
22
  };
18
23
 
19
24
  const tagTypes = {
@@ -22,10 +27,15 @@ const tagTypes = {
22
27
  'SHORT': 3,
23
28
  'LONG': 4,
24
29
  'RATIONAL': 5,
30
+ 'SBYTE': 6,
25
31
  'UNDEFINED': 7,
32
+ 'SSHORT': 8,
26
33
  'SLONG': 9,
27
34
  'SRATIONAL': 10,
28
- 'IFD': 13
35
+ 'FLOAT': 11,
36
+ 'DOUBLE': 12,
37
+ 'IFD': 13,
38
+ 'UTF8': 129
29
39
  };
30
40
 
31
41
  export default {
@@ -34,9 +44,13 @@ export default {
34
44
  getShortAt,
35
45
  getLongAt,
36
46
  getRationalAt,
47
+ getSbyteAt,
37
48
  getUndefinedAt,
49
+ getSshortAt,
38
50
  getSlongAt,
39
51
  getSrationalAt,
52
+ getFloatAt,
53
+ getDoubleAt,
40
54
  getIfdPointerAt,
41
55
  typeSizes,
42
56
  tagTypes,
@@ -63,10 +77,18 @@ function getRationalAt(dataView, offset, byteOrder) {
63
77
  return [getLongAt(dataView, offset, byteOrder), getLongAt(dataView, offset + 4, byteOrder)];
64
78
  }
65
79
 
80
+ function getSbyteAt(dataView, offset) {
81
+ return dataView.getInt8(offset);
82
+ }
83
+
66
84
  function getUndefinedAt(dataView, offset) {
67
85
  return getByteAt(dataView, offset);
68
86
  }
69
87
 
88
+ function getSshortAt(dataView, offset, byteOrder) {
89
+ return dataView.getInt16(offset, byteOrder === ByteOrder.LITTLE_ENDIAN);
90
+ }
91
+
70
92
  function getSlongAt(dataView, offset, byteOrder) {
71
93
  return dataView.getInt32(offset, byteOrder === ByteOrder.LITTLE_ENDIAN);
72
94
  }
@@ -75,6 +97,14 @@ function getSrationalAt(dataView, offset, byteOrder) {
75
97
  return [getSlongAt(dataView, offset, byteOrder), getSlongAt(dataView, offset + 4, byteOrder)];
76
98
  }
77
99
 
100
+ function getFloatAt(dataView, offset, byteOrder) {
101
+ return dataView.getFloat32(offset, byteOrder === ByteOrder.LITTLE_ENDIAN);
102
+ }
103
+
104
+ function getDoubleAt(dataView, offset, byteOrder) {
105
+ return dataView.getFloat64(offset, byteOrder === ByteOrder.LITTLE_ENDIAN);
106
+ }
107
+
78
108
  function getIfdPointerAt(dataView, offset, byteOrder) {
79
109
  return getLongAt(dataView, offset, byteOrder);
80
110
  }
@@ -10,16 +10,19 @@ const NAME_END_CHARACTERS = ' \t\r\n/><="\'';
10
10
  /**
11
11
  * Tells whether an XML string has elements nested deeper than a bound or more
12
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.
13
+ * (namespace declarations included), comments, processing instructions and
14
+ * CDATA sections together; text is not counted. Malformed markup may be counted
15
+ * deeper and with more nodes than @xmldom/xmldom 0.9.12 builds, never shallower
16
+ * or with fewer. A markup declaration such as a DOCTYPE also makes it true,
17
+ * unless it is in a comment, a CDATA section or an end tag, and so does one in a
18
+ * processing instruction: a parser may expand an entity declared there into
19
+ * markup the scan cannot count.
17
20
  *
18
21
  * @param {string} xmlString
19
22
  * @param {number} maxDepth The deepest allowed element, the root being 1.
20
23
  * @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.
24
+ * @returns {boolean} True as soon as an element is nested deeper than maxDepth,
25
+ * the markup nodes are more than maxNodes, or a markup declaration is found.
23
26
  */
24
27
  export function exceedsMarkupBounds(xmlString, maxDepth, maxNodes = Infinity) {
25
28
  let depth = 0;
@@ -33,11 +36,15 @@ export function exceedsMarkupBounds(xmlString, maxDepth, maxNodes = Infinity) {
33
36
  nodeCount++;
34
37
  index = skipPast(xmlString, index + 9, ']]>');
35
38
  } else if (hasSubstringAt(xmlString, '<?', index)) {
39
+ const endIndex = skipPast(xmlString, index + 2, '?>');
40
+ // A parser that recovers from errors may end the instruction early.
41
+ if (xmlString.slice(index + 2, endIndex).indexOf('<!') !== -1) {
42
+ return true;
43
+ }
36
44
  nodeCount++;
37
- index = skipPast(xmlString, index + 2, '?>');
45
+ index = endIndex;
38
46
  } else if (hasSubstringAt(xmlString, '<!', index)) {
39
- nodeCount++;
40
- index = skipDeclaration(xmlString, index + 2);
47
+ return true;
41
48
  } else if (hasSubstringAt(xmlString, '</', index)) {
42
49
  depth = Math.max(depth - 1, 0);
43
50
  // xmldom ends an end tag at its first >, whatever comes between.
@@ -62,23 +69,6 @@ export function exceedsMarkupBounds(xmlString, maxDepth, maxNodes = Infinity) {
62
69
  return false;
63
70
  }
64
71
 
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
72
  // A start tag cannot hold a <, so every search in it stops at the next <, which
83
73
  // keeps the scan linear. Only a well-formed tag is read as self-closing: some
84
74
  // parsers keep a malformed one open, so reading it as open can only count deeper.
package/src/xmp-tags.js CHANGED
@@ -139,9 +139,9 @@ function readTags(tags, chunkDataView, domParser) {
139
139
  }
140
140
 
141
141
  function getDocument(chunkDataView, _domParser) {
142
- const domParser = DOMParser.get(_domParser);
142
+ const {parser: domParser, warning} = DOMParser.get(_domParser);
143
143
  if (!domParser) {
144
- console.warn('Warning: DOMParser is not available. It is needed to be able to parse XMP tags.'); // eslint-disable-line no-console
144
+ console.warn(warning); // eslint-disable-line no-console
145
145
  throw new Error();
146
146
  }
147
147
 
@@ -219,7 +219,7 @@ function trimAfterPacketTrailer(xmlSource) {
219
219
 
220
220
  function parseFromString(domParser, xmlString, isRetry = false) {
221
221
  if (exceedsMarkupBounds(xmlString, MAX_ELEMENT_DEPTH, MAX_MARKUP_NODES)) {
222
- throw new ParseError(`XMP elements nested deeper than ${MAX_ELEMENT_DEPTH} levels or more than ${MAX_MARKUP_NODES} markup nodes.`);
222
+ throw new ParseError(`XMP has elements nested deeper than ${MAX_ELEMENT_DEPTH} levels, more than ${MAX_MARKUP_NODES} markup nodes, or a markup declaration.`);
223
223
  }
224
224
  try {
225
225
  const doc = domParser.parseFromString(xmlString, 'application/xml');