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.
@@ -4,13 +4,14 @@
4
4
 
5
5
  // Specification: http://www.libpng.org/pub/png/spec/1.2/
6
6
 
7
- import {getStringValueFromArray, getStringFromDataView, decompress, setProperty, COMPRESSION_METHOD_NONE} from './utils.js';
7
+ import {getStringValueFromArray, getStringFromDataView, decompress, setProperty, getTagKey, objectAssign, COMPRESSION_METHOD_NONE, getHexDigitValue} from './utils.js';
8
8
  import TagDecoder from './tag-decoder.js';
9
9
  import {TYPE_TEXT, TYPE_ITXT, TYPE_ZTXT} from './image-header-png.js';
10
10
  import Tags from './tags.js';
11
11
  import IptcTags from './iptc-tags.js';
12
12
  import Constants from './constants.js';
13
13
  import {NOOP_TAG_FILTER} from './tag-filter.js';
14
+ import {addDecompressedValueAllowance, addDecompressedIptcAllowance} from './tags-helpers.js';
14
15
 
15
16
  export default {
16
17
  read
@@ -26,7 +27,25 @@ const COMPRESSION_FLAG_COMPRESSED = 1;
26
27
  const EXIF_OFFSET = 6;
27
28
  const MAX_COMPRESSED_TEXT_CHUNKS = 255;
28
29
  const MAX_DECOMPRESSIONS_IN_FLIGHT = 4;
30
+ // PNG spec: a keyword is 1-79 bytes.
31
+ const MAX_KEYWORD_LENGTH = 79;
32
+ // Our own bound: BCP 47 sets no maximum, but real tags are far shorter.
33
+ const MAX_LANGUAGE_TAG_LENGTH = 79;
29
34
 
35
+ /**
36
+ * @param {function(DataView, object, number): object} [getThumbnail] Called with a
37
+ * decoded Exif raw profile, its thumbnail IFD tags and the TIFF header offset;
38
+ * returns the thumbnail tags, with `image` set when the thumbnail was read.
39
+ * No thumbnail is returned when it is not passed.
40
+ * @returns {{readTags: object, embeddedExifTags: object|undefined, embeddedIptcTags: object|undefined, embeddedExifThumbnail: object|undefined, readTagsPromise: Promise<object[]>|undefined}}
41
+ * `readTags`, the two embedded groups and `embeddedExifThumbnail` come from
42
+ * uncompressed chunks and are ready at once; the embedded groups are undefined
43
+ * when no uncompressed raw profile was parsed, and `embeddedExifThumbnail` is
44
+ * the first thumbnail with an image. `readTagsPromise` resolves to one result
45
+ * per compressed chunk, in chunk order, each with its own
46
+ * `embeddedExifThumbnail` when it has one and no uncompressed chunk had one,
47
+ * and is undefined when none is read.
48
+ */
30
49
  function read(
31
50
  dataView,
32
51
  pngTextChunks,
@@ -35,30 +54,69 @@ function read(
35
54
  computed = false,
36
55
  tagFilter = NOOP_TAG_FILTER,
37
56
  decompressConfig,
38
- valueBudget
57
+ valueBudget,
58
+ getThumbnail
39
59
  ) {
40
60
  const tags = {};
41
- const decompressionTasks = [];
61
+ let embeddedExifTags;
62
+ let embeddedIptcTags;
63
+ let embeddedExifThumbnail;
64
+ const compressedChunks = [];
42
65
 
43
66
  for (let i = 0; i < pngTextChunks.length; i++) {
44
67
  const {offset, length, type} = pngTextChunks[i];
45
68
  const textChunk = parseTextChunk(dataView, offset, length, type);
69
+ if (!textChunk) {
70
+ continue;
71
+ }
46
72
  if (textChunk.compressionMethod === COMPRESSION_METHOD_NONE) {
47
- const {name, value, description} = getUncompressedTag(textChunk);
48
- if (name && tagFilter.shouldParseGroup('png')) {
49
- setProperty(tags, name, {
50
- value,
51
- description
73
+ const tag = getUncompressedTag(textChunk);
74
+ if (isRawProfileTag(tag)) {
75
+ const rawProfileTags = getTagsFromTextTag(
76
+ tag,
77
+ includeUnknown,
78
+ computed,
79
+ tagFilter,
80
+ valueBudget,
81
+ embeddedExifThumbnail ? undefined : getThumbnail
82
+ );
83
+ if (rawProfileTags.embeddedExifTags) {
84
+ embeddedExifTags = objectAssign(embeddedExifTags || {}, rawProfileTags.embeddedExifTags);
85
+ }
86
+ if (rawProfileTags.embeddedExifThumbnail) {
87
+ embeddedExifThumbnail = rawProfileTags.embeddedExifThumbnail;
88
+ }
89
+ if (rawProfileTags.embeddedIptcTags) {
90
+ embeddedIptcTags = objectAssign(embeddedIptcTags || {}, rawProfileTags.embeddedIptcTags);
91
+ }
92
+ } else if (tag.name && tagFilter.shouldParseGroup('png')) {
93
+ setProperty(tags, getTagKey(tag.name), {
94
+ value: tag.value,
95
+ description: tag.description
52
96
  });
53
97
  }
54
- } else if (async && decompressionTasks.length < MAX_COMPRESSED_TEXT_CHUNKS) {
55
- decompressionTasks.push(() => decompressTextChunk(textChunk, decompressConfig)
56
- .then((tag) => getTagsFromDecompressedTag(tag, includeUnknown, computed, tagFilter, valueBudget)));
98
+ } else if (async && compressedChunks.length < MAX_COMPRESSED_TEXT_CHUNKS) {
99
+ compressedChunks.push(textChunk);
57
100
  }
58
101
  }
59
102
 
103
+ const compressedThumbnailReader = embeddedExifThumbnail ? undefined : getThumbnail;
104
+ const decompressionTasks = compressedChunks.map((textChunk) => () => decompressTextChunk(textChunk, decompressConfig)
105
+ .then((tag) => getTagsFromTextTag(
106
+ tag,
107
+ includeUnknown,
108
+ computed,
109
+ tagFilter,
110
+ valueBudget,
111
+ compressedThumbnailReader,
112
+ true
113
+ )));
114
+
60
115
  return {
61
116
  readTags: tags,
117
+ embeddedExifTags,
118
+ embeddedIptcTags,
119
+ embeddedExifThumbnail,
62
120
  readTagsPromise: decompressionTasks.length > 0 ? runTasksInOrder(decompressionTasks, MAX_DECOMPRESSIONS_IN_FLIGHT) : undefined
63
121
  };
64
122
  }
@@ -66,15 +124,15 @@ function read(
66
124
  function parseTextChunk(dataView, offset, length, type) {
67
125
  const keywordChars = [];
68
126
  const langChars = [];
69
- const translatedKeywordChars = [];
70
127
  let valueChars;
71
128
  let parsingState = STATE_KEYWORD;
72
129
  let compressionMethod = COMPRESSION_METHOD_NONE;
73
130
  const byteOffset = dataView.byteOffset || 0;
131
+ const chunkEnd = Math.min(offset + length, dataView.byteLength);
74
132
 
75
133
  for (let i = 0; i < length && offset + i < dataView.byteLength; i++) {
76
134
  if (parsingState === STATE_COMPRESSION) {
77
- compressionMethod = getCompressionMethod({type, dataView, offset: offset + i});
135
+ compressionMethod = getCompressionMethod({type, dataView, offset: offset + i, chunkEnd});
78
136
  if (type === TYPE_ITXT) {
79
137
  i += COMPRESSION_SECTION_ITXT_EXTRA_BYTE;
80
138
  }
@@ -91,11 +149,15 @@ function parseTextChunk(dataView, offset, length, type) {
91
149
  if (byte === 0) {
92
150
  parsingState = moveToNextState(type, parsingState);
93
151
  } else if (parsingState === STATE_KEYWORD) {
152
+ if (keywordChars.length >= MAX_KEYWORD_LENGTH) {
153
+ return undefined;
154
+ }
94
155
  keywordChars.push(byte);
95
156
  } else if (parsingState === STATE_LANG) {
157
+ if (langChars.length >= MAX_LANGUAGE_TAG_LENGTH) {
158
+ return undefined;
159
+ }
96
160
  langChars.push(byte);
97
- } else if (parsingState === STATE_TRANSLATED_KEYWORD) {
98
- translatedKeywordChars.push(byte);
99
161
  }
100
162
  }
101
163
 
@@ -107,38 +169,47 @@ function getUncompressedTag({type, keywordChars, langChars, valueChars}) {
107
169
  return constructTag(decodedValueChars, type, langChars, keywordChars);
108
170
  }
109
171
 
110
- function decompressTextChunk({type, keywordChars, langChars, compressionMethod, valueChars}, decompressConfig) {
111
- return decompress(valueChars, compressionMethod, getEncodingFromType(type), 'string', decompressConfig)
112
- .then((decompressedValueChars) => constructTag(decompressedValueChars, type, langChars, keywordChars))
113
- .catch(() => constructTag('<text using unknown compression>'.split(''), type, langChars, keywordChars));
172
+ function isRawProfileTag({name, value}) {
173
+ return isExifGroupTag(name, value) || isIptcGroupTag(name, value);
114
174
  }
115
175
 
116
- function getTagsFromDecompressedTag({name, value, description}, includeUnknown, computed, tagFilter, valueBudget) {
176
+ function getTagsFromTextTag({name, value, description}, includeUnknown, computed, tagFilter, valueBudget, getThumbnail, isDecompressed) {
117
177
  try {
118
178
  if (Constants.USE_EXIF && isExifGroupTag(name, value)) {
119
179
  if (!tagFilter.shouldParseGroup('exif')) {
120
180
  return {};
121
181
  }
122
- return {
123
- embeddedExifTags: Tags.read(
124
- decodeRawData(value),
125
- EXIF_OFFSET,
126
- includeUnknown,
127
- computed,
128
- tagFilter,
129
- valueBudget
130
- ).tags
131
- };
182
+ const exifDataView = decodeRawData(value);
183
+ if (isDecompressed) {
184
+ addDecompressedValueAllowance(valueBudget, exifDataView.byteLength);
185
+ }
186
+ const embeddedExifTags = Tags.read(
187
+ exifDataView,
188
+ EXIF_OFFSET,
189
+ includeUnknown,
190
+ computed,
191
+ tagFilter,
192
+ valueBudget
193
+ ).tags;
194
+ if (embeddedExifTags['MakerNote']) {
195
+ delete embeddedExifTags['MakerNote'].__offset;
196
+ }
197
+ return withEmbeddedExifThumbnail({embeddedExifTags}, exifDataView, getThumbnail);
132
198
  } else if (Constants.USE_IPTC && isIptcGroupTag(name, value)) {
133
199
  if (!tagFilter.shouldParseGroup('iptc')) {
134
200
  return {};
135
201
  }
202
+ const iptcDataView = decodeRawData(value);
203
+ if (isDecompressed) {
204
+ addDecompressedIptcAllowance(valueBudget, iptcDataView.byteLength);
205
+ }
136
206
  return {
137
207
  embeddedIptcTags: IptcTags.read(
138
- decodeRawData(value),
208
+ iptcDataView,
139
209
  0,
140
210
  includeUnknown,
141
- tagFilter
211
+ tagFilter,
212
+ valueBudget
142
213
  )
143
214
  };
144
215
  } else if (name && !isExifGroupTag(name, value) && !isIptcGroupTag(name, value)) {
@@ -146,7 +217,7 @@ function getTagsFromDecompressedTag({name, value, description}, includeUnknown,
146
217
  return {};
147
218
  }
148
219
  const readTags = {};
149
- setProperty(readTags, name, {
220
+ setProperty(readTags, getTagKey(name), {
150
221
  value,
151
222
  description
152
223
  });
@@ -158,6 +229,35 @@ function getTagsFromDecompressedTag({name, value, description}, includeUnknown,
158
229
  return {};
159
230
  }
160
231
 
232
+ function withEmbeddedExifThumbnail(result, exifDataView, getThumbnail) {
233
+ const thumbnailIfdTags = result.embeddedExifTags.Thumbnail;
234
+ if (!thumbnailIfdTags) {
235
+ return result;
236
+ }
237
+ delete result.embeddedExifTags.Thumbnail;
238
+
239
+ if (getThumbnail) {
240
+ const thumbnail = getThumbnail(exifDataView, thumbnailIfdTags, EXIF_OFFSET);
241
+ if (thumbnail.image) {
242
+ result.embeddedExifThumbnail = thumbnail;
243
+ }
244
+ }
245
+ return result;
246
+ }
247
+
248
+ function decompressTextChunk({type, keywordChars, langChars, compressionMethod, valueChars}, decompressConfig) {
249
+ if (valueChars === undefined) {
250
+ return Promise.resolve(constructPlaceholderTag(type, langChars, keywordChars));
251
+ }
252
+ return decompress(valueChars, compressionMethod, getEncodingFromType(type), 'string', decompressConfig)
253
+ .then((decompressedValueChars) => constructTag(decompressedValueChars, type, langChars, keywordChars))
254
+ .catch(() => constructPlaceholderTag(type, langChars, keywordChars));
255
+ }
256
+
257
+ function constructPlaceholderTag(type, langChars, keywordChars) {
258
+ return constructTag('<text using unknown compression>', type, langChars, keywordChars);
259
+ }
260
+
161
261
  function runTasksInOrder(tasks, limit) {
162
262
  const results = [];
163
263
  const workers = [];
@@ -182,9 +282,9 @@ function runTasksInOrder(tasks, limit) {
182
282
  }
183
283
  }
184
284
 
185
- function getCompressionMethod({type, dataView, offset}) {
285
+ function getCompressionMethod({type, dataView, offset, chunkEnd}) {
186
286
  if (type === TYPE_ITXT) {
187
- if (offset + 1 < dataView.byteLength && dataView.getUint8(offset) === COMPRESSION_FLAG_COMPRESSED) {
287
+ if (offset + 1 < chunkEnd && dataView.getUint8(offset) === COMPRESSION_FLAG_COMPRESSED) {
188
288
  return dataView.getUint8(offset + 1);
189
289
  }
190
290
  } else if (type === TYPE_ZTXT) {
@@ -255,14 +355,41 @@ function isIptcGroupTag(name, value) {
255
355
  }
256
356
 
257
357
  function decodeRawData(value) {
258
- const parts = value.match(/\n(exif|iptc)\n\s*\d+\n([\s\S]*)$/);
259
- return hexToDataView(parts[2].replace(/\n/g, ''));
358
+ const header = /\n(exif|iptc)\n\s*\d+\n/.exec(value);
359
+ if (!header) {
360
+ throw new Error('Invalid raw profile header.');
361
+ }
362
+ const dataStart = header.index + header[0].length;
363
+ const digitCount = countHexDigits(value, dataStart);
364
+ if (digitCount % 2 !== 0) {
365
+ throw new Error('Odd number of hex digits in raw profile.');
366
+ }
367
+
368
+ const bytes = new Uint8Array(digitCount / 2);
369
+ let byteIndex = 0;
370
+ let highNibble = -1;
371
+ for (let i = dataStart; i < value.length; i++) {
372
+ const nibble = getHexDigitValue(value.charCodeAt(i));
373
+ if (nibble < 0) {
374
+ // Newlines and any other non-hex character are skipped, as ImageMagick and exiv2 do.
375
+ continue;
376
+ }
377
+ if (highNibble < 0) {
378
+ highNibble = nibble;
379
+ } else {
380
+ bytes[byteIndex++] = (highNibble << 4) | nibble;
381
+ highNibble = -1;
382
+ }
383
+ }
384
+ return new DataView(bytes.buffer);
260
385
  }
261
386
 
262
- function hexToDataView(hex) {
263
- const dataView = new DataView(new ArrayBuffer(hex.length / 2));
264
- for (let i = 0; i < hex.length; i += 2) {
265
- dataView.setUint8(i / 2, parseInt(hex.substring(i, i + 2), 16));
387
+ function countHexDigits(value, start) {
388
+ let count = 0;
389
+ for (let i = start; i < value.length; i++) {
390
+ if (getHexDigitValue(value.charCodeAt(i)) >= 0) {
391
+ count++;
392
+ }
266
393
  }
267
- return dataView;
394
+ return count;
268
395
  }
@@ -62,6 +62,8 @@ export const COMPOSITE_DEPENDENCY_TAGS = {
62
62
  'FocalPlaneYResolution',
63
63
  'FocalPlaneResolutionUnit',
64
64
  'FocalLengthIn35mmFilm',
65
+ 'PixelXDimension',
66
+ 'PixelYDimension',
65
67
  ],
66
68
  };
67
69