Skip to content

Commit

Permalink
Pass Set for processed IFD offsets
Browse files Browse the repository at this point in the history
This allows combining the add and test operations into a single lookup.
  • Loading branch information
don-vip committed Jun 18, 2022
1 parent c90b829 commit b53e3ee
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions Source/com/drew/imaging/tiff/TiffReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,14 @@ public void processTiff(@NotNull final RandomAccessReader reader,
*
* @param handler the {@link com.drew.imaging.tiff.TiffHandler} that will coordinate processing and accept read values
* @param reader the {@link com.drew.lang.RandomAccessReader} from which the data should be read
* @param processedGlobalIfdOffsets the set of visited IFD offsets, to avoid revisiting the same IFD in an endless loop
* @param processedIfdOffsets the set of visited IFD offsets, to avoid revisiting the same IFD in an endless loop
* @param ifdOffset the offset within <code>reader</code> at which the IFD data starts
* @param isBigTiff >Whether the IFD uses the BigTIFF data format.
* @throws IOException an error occurred while accessing the required data
*/
public static void processIfd(@NotNull final TiffHandler handler,
@NotNull final RandomAccessReader reader,
@NotNull final Set<Integer> processedGlobalIfdOffsets,
@NotNull final Set<Integer> processedIfdOffsets,
final int ifdOffset,
final boolean isBigTiff) throws IOException
{
Expand Down Expand Up @@ -166,12 +166,11 @@ public static void processIfd(@NotNull final TiffHandler handler,
// Check for directories we've already visited to avoid stack overflows when recursive/cyclic directory structures exist.
// Note that we track these offsets in the global frame, not the reader's local frame.
int globalIfdOffset = reader.toUnshiftedOffset(ifdOffset);
if (processedGlobalIfdOffsets.contains(Integer.valueOf(globalIfdOffset))) {
return;
}

// remember that we've visited this directory so that we don't visit it again later
processedGlobalIfdOffsets.add(globalIfdOffset);
if (!processedIfdOffsets.add(globalIfdOffset)) {
return;
}

// Validate IFD offset
if (ifdOffset >= reader.getLength() || ifdOffset < 0) {
Expand Down Expand Up @@ -276,13 +275,13 @@ public static void processIfd(@NotNull final TiffHandler handler,
if (handler.tryEnterSubIfd(tagId)) {
isIfdPointer = true;
long subDirOffset = reader.getUInt32((int) (tagValueOffset + i*4));
processIfd(handler, reader, processedGlobalIfdOffsets, (int) subDirOffset, isBigTiff);
processIfd(handler, reader, processedIfdOffsets, (int) subDirOffset, isBigTiff);
}
}
}

// If it wasn't an IFD pointer, allow custom tag processing to occur
if (!isIfdPointer && !handler.customProcessTag((int) tagValueOffset, processedGlobalIfdOffsets, reader, tagId, (int) byteCount, isBigTiff)) {
if (!isIfdPointer && !handler.customProcessTag((int) tagValueOffset, processedIfdOffsets, reader, tagId, (int) byteCount, isBigTiff)) {
// If no custom processing occurred, process the tag in the standard fashion
processTag(handler, tagId, (int) tagValueOffset, (int) componentCount, formatCode, reader);
}
Expand All @@ -308,7 +307,7 @@ public static void processIfd(@NotNull final TiffHandler handler,
}

if (handler.hasFollowerIfd()) {
processIfd(handler, reader, processedGlobalIfdOffsets, nextIfdOffset, isBigTiff);
processIfd(handler, reader, processedIfdOffsets, nextIfdOffset, isBigTiff);
}
}
} finally {
Expand Down

0 comments on commit b53e3ee

Please sign in to comment.