exifreader 4.45.1 → 4.45.2

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/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
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "exifreader",
3
- "version": "4.45.1",
3
+ "version": "4.45.2",
4
4
  "description": "Library that parses Exif metadata in images.",
5
5
  "author": "Mattias Wallander <mattias@wallander.eu>",
6
6
  "repository": {
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)) {
@@ -648,7 +648,9 @@ export function loadView(
648
648
  }
649
649
  }
650
650
 
651
- mergeSteps.push({type: 'gps'});
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
- getGpsGroupFromExifTags,
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,
@@ -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
- return String.fromCharCode.apply(null, new Uint8Array(slice));
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 compressedDataView = new DataView(dataView.buffer.slice(byteOffset + iccData[0].offset, byteOffset + iccData[0].offset + iccData[0].length));
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 = iccData.find((x) => x.chunkNumber === chunkNumber);
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 - 15, decodeBudget);
220
- addTag(tags, tagSignature, val);
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
- return data.buffer.slice(data.byteOffset, data.byteOffset + end);
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
  }
@@ -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 = thumbnailIfdTags ? deps.filterTagsForParse(
285
+ const parsedThumbnailIfdTags = deps.filterTagsForParse(
286
286
  'thumbnail',
287
287
  thumbnailIfdTags,
288
288
  tagFilter
289
- ) : undefined;
290
- if (parsedThumbnailIfdTags) {
291
- parsedGroups.thumbnail = parsedThumbnailIfdTags;
292
- }
289
+ );
293
290
 
294
291
  const thumbnail = Constants.USE_EXIF
295
292
  && Constants.USE_THUMBNAIL
@@ -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
- valueChars = new DataView(dataView.buffer.slice(byteOffset + offset + i, byteOffset + offset + length));
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);
@@ -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