exifreader 4.52.0 → 4.54.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.
@@ -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 (isArrayWithoutCharacterCodes(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,34 @@ 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 (isArrayWithoutCharacterCodes(value)) {
45
+ return getStringsText(value);
43
46
  }
44
47
  return getByteString(value);
45
48
  }
46
49
  return value;
47
50
  }
48
51
 
52
+ /**
53
+ * An ASCII-typed value holds strings, with a hole where an empty piece was,
54
+ * index 0 included, and a rational value holds number pairs.
55
+ * @param {*} value
56
+ * @returns {boolean} True for an array whose element 0 is not a number.
57
+ */
58
+ export function isArrayWithoutCharacterCodes(value) {
59
+ return Array.isArray(value) && typeof value[0] !== 'number';
60
+ }
61
+
62
+ /**
63
+ * Describes an ASCII-typed value as its strings.
64
+ * @param {Array} value
65
+ * @returns {string} The strings joined with ', ', holes and non-strings
66
+ * skipped, or '' when there are none.
67
+ */
68
+ export function getStringsText(value) {
69
+ return value.filter(isString).join(', ');
70
+ }
71
+
49
72
  function isString(item) {
50
73
  return typeof item === 'string';
51
74
  }
@@ -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
 
@@ -101,6 +105,8 @@ export function get0thIfdOffset(dataView, tiffHeaderOffset, byteOrder) {
101
105
  * offset into the thumbnail IFD, whose entries are otherwise not charged to the
102
106
  * value budget. Defaults to whether tagNames has an IFD_TYPE_1ST dictionary and
103
107
  * tagFilter parses the thumbnail group.
108
+ * @param {Object} [valueOffsets] - Given, receives the offset of the MakerNote
109
+ * value as `MakerNote`, relative to the offset origin.
104
110
  * @returns {Object} The read tags, keyed by tag name.
105
111
  */
106
112
  export function readIfd(
@@ -115,7 +121,8 @@ export function readIfd(
115
121
  groupKey = 'exif',
116
122
  valueBudget = getValueBudget(dataView),
117
123
  tagNames,
118
- readThumbnailIfd = tagNames[IFD_TYPE_1ST] !== undefined && tagFilter.shouldParseGroup('thumbnail')
124
+ readThumbnailIfd = tagNames[IFD_TYPE_1ST] !== undefined && tagFilter.shouldParseGroup('thumbnail'),
125
+ valueOffsets = undefined
119
126
  ) {
120
127
  const FIELD_COUNT_SIZE = Types.getTypeSize('SHORT');
121
128
  const FIELD_SIZE = IFD_ENTRY_LENGTH;
@@ -156,9 +163,12 @@ export function readIfd(
156
163
  if (computed) {
157
164
  tags[tag.name].computed = getComputedTagValue(tag.tagType, tag.value);
158
165
  }
159
- if (tag.name === 'MakerNote' || (ifdType === IFD_TYPE_PENTAX && tag.name === 'LevelInfo')) {
166
+ if (ifdType === IFD_TYPE_PENTAX && tag.name === 'LevelInfo') {
160
167
  tags[tag.name].__offset = tag.__offset;
161
168
  }
169
+ if (valueOffsets !== undefined && tag.name === 'MakerNote') {
170
+ valueOffsets.MakerNote = tag.__offset;
171
+ }
162
172
  }
163
173
 
164
174
  offset += FIELD_SIZE;
@@ -341,7 +351,7 @@ function readTag(
341
351
  }
342
352
  }
343
353
 
344
- if (tagType === Types.tagTypes['ASCII']) {
354
+ if (isStringType(tagType)) {
345
355
  tagValue = getAsciiTagValue(tagValue, stringBudget);
346
356
  }
347
357
 
@@ -395,7 +405,7 @@ function getTagValue(dataView, offset, type, count, byteOrder, forceByteType = f
395
405
  count = count * Types.typeSizes[type];
396
406
  type = Types.tagTypes['BYTE'];
397
407
  }
398
- if (type === Types.tagTypes['ASCII']) {
408
+ if (isStringType(type)) {
399
409
  return getAsciiBytes(dataView, offset, count);
400
410
  }
401
411
  for (let valueIndex = 0; valueIndex < count; valueIndex++) {
@@ -436,6 +446,10 @@ function getBoundedTagCount(remainingBudget, tagType, tagCount) {
436
446
  return boundedCount;
437
447
  }
438
448
 
449
+ function isStringType(type) {
450
+ return type === Types.tagTypes['ASCII'] || type === Types.tagTypes['UTF8'];
451
+ }
452
+
439
453
  function getAsciiTagValue(tagValue, stringBudget) {
440
454
  if (tagValue instanceof Uint8Array) {
441
455
  return getNullSeparatedStrings(tagValue, stringBudget);
@@ -494,7 +508,7 @@ function getDescriptionFromTagValue(tagValue) {
494
508
  }
495
509
 
496
510
  function getComputedTagValue(tagType, value) {
497
- if (tagType === Types.tagTypes['ASCII']) {
511
+ if (isStringType(tagType)) {
498
512
  if (Array.isArray(value) && value.length === 1) {
499
513
  return value[0];
500
514
  }
@@ -542,23 +556,3 @@ function getComputedRationalValue(rational) {
542
556
 
543
557
  return numerator / denominator;
544
558
  }
545
-
546
- /**
547
- * Removes the thumbnail IFD from the tags of an embedded Exif block and reads
548
- * its image with `readThumbnail`, when given.
549
- *
550
- * @returns {object|undefined} The thumbnail tags when an image was read.
551
- */
552
- export function takeEmbeddedExifThumbnail(readTags, dataView, tiffHeaderOffset, readThumbnail) {
553
- const thumbnailIfdTags = readTags.Thumbnail;
554
- if (!thumbnailIfdTags) {
555
- return undefined;
556
- }
557
- delete readTags.Thumbnail;
558
-
559
- if (!readThumbnail) {
560
- return undefined;
561
- }
562
- const thumbnail = readThumbnail(dataView, thumbnailIfdTags, tiffHeaderOffset);
563
- return thumbnail.image ? thumbnail : undefined;
564
- }
package/src/tags.js CHANGED
@@ -18,10 +18,18 @@ export default {
18
18
  read,
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).
21
+ /**
22
+ * Reads the 0th, Exif, GPS and Interoperability IFDs, and the thumbnail IFD
23
+ * when asked to.
24
+ *
25
+ * One decoded-value budget is shared by the IFD reads so their combined
26
+ * decoded values stay proportional to the input. A caller passes its own to
27
+ * share it with later reads (the maker note, MPF and decompressed Exif).
28
+ *
29
+ * @returns {{tags: Object, byteOrder: number, makerNoteOffset: (number|undefined)}}
30
+ * `makerNoteOffset` is the MakerNote value's offset relative to the TIFF
31
+ * header, for the maker note readers.
32
+ */
25
33
  function read(
26
34
  dataView,
27
35
  tiffHeaderOffset,
@@ -33,15 +41,16 @@ function read(
33
41
  readThumbnailIfd = undefined
34
42
  ) {
35
43
  const byteOrder = ByteOrder.getByteOrder(dataView, tiffHeaderOffset);
36
- let tags = read0thIfd(dataView, tiffHeaderOffset, byteOrder, includeUnknown, computed, tagFilter, valueBudget, tagNames, readThumbnailIfd);
44
+ const valueOffsets = {};
45
+ let tags = read0thIfd(dataView, tiffHeaderOffset, byteOrder, includeUnknown, computed, tagFilter, valueBudget, tagNames, readThumbnailIfd, valueOffsets);
37
46
  for (let i = 0; i < SUB_IFDS.length; i++) {
38
- tags = readSubIfd(SUB_IFDS[i], tags, dataView, tiffHeaderOffset, byteOrder, includeUnknown, computed, tagFilter, valueBudget, tagNames);
47
+ tags = readSubIfd(SUB_IFDS[i], tags, dataView, tiffHeaderOffset, byteOrder, includeUnknown, computed, tagFilter, valueBudget, tagNames, valueOffsets);
39
48
  }
40
49
 
41
- return {tags, byteOrder};
50
+ return {tags, byteOrder, makerNoteOffset: valueOffsets.MakerNote};
42
51
  }
43
52
 
44
- function read0thIfd(dataView, tiffHeaderOffset, byteOrder, includeUnknown, computed, tagFilter, valueBudget, tagNames, readThumbnailIfd) {
53
+ function read0thIfd(dataView, tiffHeaderOffset, byteOrder, includeUnknown, computed, tagFilter, valueBudget, tagNames, readThumbnailIfd, valueOffsets) {
45
54
  const ifdOffset = get0thIfdOffset(dataView, tiffHeaderOffset, byteOrder);
46
55
  if (ifdOffset === undefined) {
47
56
  return {};
@@ -58,11 +67,12 @@ function read0thIfd(dataView, tiffHeaderOffset, byteOrder, includeUnknown, compu
58
67
  'exif',
59
68
  valueBudget,
60
69
  tagNames,
61
- readThumbnailIfd
70
+ readThumbnailIfd,
71
+ valueOffsets
62
72
  );
63
73
  }
64
74
 
65
- function readSubIfd(subIfd, tags, dataView, tiffHeaderOffset, byteOrder, includeUnknown, computed, tagFilter, valueBudget, tagNames) {
75
+ function readSubIfd(subIfd, tags, dataView, tiffHeaderOffset, byteOrder, includeUnknown, computed, tagFilter, valueBudget, tagNames, valueOffsets) {
66
76
  const pointerTag = tags[subIfd.pointerKey];
67
77
  if (pointerTag === undefined) {
68
78
  return tags;
@@ -86,7 +96,9 @@ function readSubIfd(subIfd, tags, dataView, tiffHeaderOffset, byteOrder, include
86
96
  tagFilter,
87
97
  'exif',
88
98
  valueBudget,
89
- tagNames
99
+ tagNames,
100
+ undefined,
101
+ valueOffsets
90
102
  )
91
103
  );
92
104
  }
package/src/thumbnail.js CHANGED
@@ -2,7 +2,7 @@
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 {deferInit, getBase64Image} from './utils.js';
5
+ import {deferInit} from './utils.js';
6
6
 
7
7
  // https://exiftool.org/TagNames/EXIF.html#Compression
8
8
  const COMPRESSION_JPEG = [6, 7, 99];
@@ -11,7 +11,7 @@ export default {
11
11
  get,
12
12
  };
13
13
 
14
- function get(dataView, thumbnailTags, tiffHeaderOffset) {
14
+ function get(dataView, thumbnailTags, tiffHeaderOffset, getBase64Image) {
15
15
  if (hasJpegThumbnail(thumbnailTags)) {
16
16
  const offset = tiffHeaderOffset + thumbnailTags.JPEGInterchangeFormat.value;
17
17
  const length = thumbnailTags.JPEGInterchangeFormatLength.value;
@@ -20,9 +20,11 @@ function get(dataView, thumbnailTags, tiffHeaderOffset) {
20
20
  thumbnailTags.type = 'image/jpeg';
21
21
  const byteOffset = dataView.byteOffset;
22
22
  thumbnailTags.image = dataView.buffer.slice(byteOffset + offset, byteOffset + offset + length);
23
- deferInit(thumbnailTags, 'base64', function () {
24
- return getBase64Image(this.image);
25
- });
23
+ if (getBase64Image) {
24
+ deferInit(thumbnailTags, 'base64', function () {
25
+ return getBase64Image(this.image);
26
+ });
27
+ }
26
28
  }
27
29
  }
28
30
 
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
  }
package/src/utils.js CHANGED
@@ -280,24 +280,37 @@ export function getCharacterArray(string) {
280
280
  export const MAX_METADATA_BLOCKS = 4096;
281
281
 
282
282
  /**
283
- * Records one metadata block. The list holds at most MAX_METADATA_BLOCKS
284
- * entries; past that, the last entry is widened to cover each further block
285
- * and keeps its own type. Does nothing without a list or a type.
286
- * @param {Array<{type: string, start: number, end: number}>|undefined} metadataBlocks
287
- * @param {string|undefined} type
283
+ * Appends a block to a finder's list and returns it, so that a finder can
284
+ * create a block the first time it meets its kind and update it after that.
285
+ * @param {Array<Object>} blocks
286
+ * @param {Object} block
287
+ * @returns {Object} The block.
288
+ */
289
+ export function addBlock(blocks, block) {
290
+ blocks.push(block);
291
+ return block;
292
+ }
293
+
294
+ /**
295
+ * Records the span of one block for metadataRange. The list holds at most
296
+ * MAX_METADATA_BLOCKS entries; past that, the last entry is widened to cover
297
+ * each further span and keeps its own block. Does nothing without a list or
298
+ * a block.
299
+ * @param {Array<{block: Object, start: number, end: number}>|undefined} ranges
300
+ * @param {Object|undefined} block The block the span belongs to.
288
301
  * @param {number} start
289
302
  * @param {number} end Exclusive.
290
303
  */
291
- export function pushMetadataBlock(metadataBlocks, type, start, end) {
292
- if (!metadataBlocks || !type) {
304
+ export function pushRange(ranges, block, start, end) {
305
+ if (!ranges || !block) {
293
306
  return;
294
307
  }
295
- if (metadataBlocks.length < MAX_METADATA_BLOCKS) {
296
- metadataBlocks.push({type, start, end});
308
+ if (ranges.length < MAX_METADATA_BLOCKS) {
309
+ ranges.push({block, start, end});
297
310
  } else {
298
- const lastBlock = metadataBlocks[metadataBlocks.length - 1];
299
- lastBlock.start = Math.min(lastBlock.start, start);
300
- lastBlock.end = Math.max(lastBlock.end, end);
311
+ const lastRange = ranges[ranges.length - 1];
312
+ lastRange.start = Math.min(lastRange.start, start);
313
+ lastRange.end = Math.max(lastRange.end, end);
301
314
  }
302
315
  }
303
316
 
@@ -370,28 +383,6 @@ export function deferInit(object, key, initializer) {
370
383
  });
371
384
  }
372
385
 
373
- export function getBase64Image(image) {
374
- if (typeof image === 'string' && typeof btoa !== 'undefined') {
375
- // Only tests pass a string. btoa reads it as Latin-1, Buffer.from would use UTF-8.
376
- return btoa(image);
377
- }
378
- if (hasBufferFrom()) {
379
- return Buffer.from(image).toString('base64'); // eslint-disable-line no-undef
380
- }
381
- if (typeof btoa !== 'undefined') {
382
- return btoa(getByteString(new Uint8Array(image)));
383
- }
384
- if (typeof Buffer === 'undefined') {
385
- return undefined;
386
- }
387
- return (new Buffer(image)).toString('base64'); // eslint-disable-line no-undef
388
- }
389
-
390
- function hasBufferFrom() {
391
- // Buffer polyfills before buffer@4.6.0 inherit Uint8Array.from, which returns an empty array for an ArrayBuffer.
392
- return typeof Buffer !== 'undefined' && typeof Buffer.from !== 'undefined' && Buffer.from !== Uint8Array.from; // eslint-disable-line no-undef
393
- }
394
-
395
386
  export function dataUriToBuffer(dataUri) {
396
387
  const commaIndex = dataUri.indexOf(',');
397
388
  const header = dataUri.substring(0, commaIndex);
@@ -519,6 +510,23 @@ export function withDecompressBudget(decompressConfig) {
519
510
  };
520
511
  }
521
512
 
513
+ /**
514
+ * Maps a block's `compression` to the method `decompress` takes. Anything but
515
+ * 'deflate' and 'brotli' is passed through unchanged, so `decompress` rejects
516
+ * it as an unknown method and never selects a custom function for it.
517
+ * @param {string} compression
518
+ * @returns {number|string}
519
+ */
520
+ export function getCompressionMethod(compression) {
521
+ if (compression === 'deflate') {
522
+ return COMPRESSION_METHOD_DEFLATE;
523
+ }
524
+ if (compression === 'brotli') {
525
+ return COMPRESSION_METHOD_BROTLI;
526
+ }
527
+ return compression;
528
+ }
529
+
522
530
  export function decompress(dataView, compressionMethod, encoding, returnType = 'string', decompressConfig) {
523
531
  const maxDecompressedSize = getMaxDecompressedSize(decompressConfig);
524
532
  const budget = getDecompressBudget(decompressConfig, maxDecompressedSize);