Skip to content

Commit

Permalink
fix: negative dataOffset (#13)
Browse files Browse the repository at this point in the history
* Modify buffer to start at TIFF header

* Fix negative dataOffset
  • Loading branch information
Friendseeker authored Sep 22, 2024
1 parent a933ff0 commit 968e7e5
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 12 deletions.
19 changes: 7 additions & 12 deletions src/markers/exif.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ class IDFEntries {
}

_decodeIDFEntries(buffer, tags, offset, log = false) {
let pos = 2;
let pos = 2 + offset;

const entries = {};

Expand All @@ -205,7 +205,7 @@ class IDFEntries {
const uint32 = (pos) =>
this.bigEndian ? readUInt32BE(buffer, pos) : readUInt32LE(buffer, pos);

const numberOfEntries = uint16(0);
const numberOfEntries = uint16(offset);

for (let i = 0; i < numberOfEntries; i++) {
const tagAddress = buffer.slice(pos, pos + 2);
Expand All @@ -217,12 +217,10 @@ class IDFEntries {
let dataValue = buffer.slice(pos + 8, pos + 12);

if (dataLength > 4) {
const valueOffset = this.bigEndian
const dataOffset = this.bigEndian
? readUInt32BE(dataValue, 0)
: readUInt32LE(dataValue, 0);

const dataOffset = valueOffset - offset;

dataValue = buffer.slice(dataOffset, dataOffset + dataLength);
}

Expand All @@ -243,31 +241,28 @@ class IDFEntries {
}

decode(stream, parent) {
let buffer = stream.buffer.slice(stream.pos);
const buffer = stream.buffer.slice(stream.pos - 8);
const offsetToFirstIFD = parent.offsetToFirstIFD;

if (offsetToFirstIFD > buffer.length) {
stream.pos += parent.parent.length - 16;
return {};
}

const firstIFDBuffer = buffer.slice(offsetToFirstIFD - 8);
const entries = this._decodeIDFEntries(firstIFDBuffer, tags.ifd, offsetToFirstIFD);
const entries = this._decodeIDFEntries(buffer, tags.ifd, offsetToFirstIFD);
const { exifIFDPointer, gpsInfoIFDPointer } = entries;

if (exifIFDPointer) {
const exifIFDBuffer = buffer.slice(exifIFDPointer - 8);
entries.subExif = this._decodeIDFEntries(
exifIFDBuffer,
buffer,
tags.ifd,
exifIFDPointer,
);
}

if (gpsInfoIFDPointer) {
const gps = gpsInfoIFDPointer;
const gpsBuffer = buffer.slice(gps - 8);
entries.gpsInfo = this._decodeIDFEntries(gpsBuffer, tags.gps, gps, true);
entries.gpsInfo = this._decodeIDFEntries(buffer, tags.gps, gps, true);
}

stream.pos += parent.parent.length - 16;
Expand Down
Binary file modified tests/__snapshots__/index.test.js.snap
Binary file not shown.
Binary file added tests/images/image-31.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 968e7e5

Please sign in to comment.