Bug 2461 - Make TIFFSetDirectory compatible with more than 64'000 directories
: Make TIFFSetDirectory compatible with more than 64'000 directories
Status: RESOLVED LATER
: libtiff
default
: 4.0.1
: PC Windows NT
: P2 enhancement
: ---
Assigned To:
:
:
: migrated_to_gitlab
:
:
  Show dependency treegraph
 
Reported: 2013-10-22 13:01 by
Modified: 2019-10-01 14:20 (History)


Attachments


Note

You need to log in before you can comment on or make changes to this bug.


Description From 2013-10-22 13:01:39
In analogy to Bug 4260 I'd like to suggest an improvement for TIFFSetDirectory.

The function has a problem to jump to a directory higher than 2^16.

In addition the performance is pretty poor, when it's used to iterate over
directories (because it searches always from the beginning of the file, and
hence preoduces a square effort for seeking all the positions). Of course
"TiffReadDirectory" can be used for iterating, but why not making
TIFFSetDirectory more efficiennt, if it's that easy - just iterate starting
from "tif_curdir" if possible.

That's the sample code (didn't test it - just to show the idea):

/*
 * Set the n-th directory as the current directory.
 * NB: Directories are numbered starting at 0.
 */
int
TIFFSetDirectory(TIFF* tif, uint64 dirn)
{
  uint64 nextdir;
  uint64 n;

  if (tif->tif_curdir == dirn) {
    // nothing to do - we're already at the desired directory
    return (1);
  }
  if (tif->tif_curdir < dirn) {
    // advance from current directory, if destination is after the current
directory
    nextdir = tif->tif_diroff;
    while (tif->tif_curdir < dirn)
      if (!TIFFAdvanceDirectory(tif, &nextdir, NULL))
        return (0);
  }
  else {
    // read from beginning, if destination directory is before the current
directory
    if (!(tif->tif_flags&TIFF_BIGTIFF))
      nextdir = tif->tif_header.classic.tiff_diroff;
    else
      nextdir = tif->tif_header.big.tiff_diroff;
    for (n = dirn; n > 0 && nextdir != 0; n--)
      if (!TIFFAdvanceDirectory(tif, &nextdir, NULL))
        return (0);
  }

  tif->tif_nextdiroff = nextdir;
  /*
   * Set curdir to the actual directory index.  The
   * -1 is because TIFFReadDirectory will increment
   * tif_curdir after successfully reading the directory.
   */
  tif->tif_curdir = (dirn - n) - 1;
  /*
   * Reset tif_dirnumber counter and start new list of seen directories.
   * We need this to prevent IFD loops.
   */
  tif->tif_dirnumber = 0;
  return (TIFFReadDirectory(tif));
}
------- Comment #1 From 2019-10-01 14:20:01 -------
Bugzilla is no longer used for tracking libtiff issues. Remaining open tickets,
such as this one, have been migrated to the libtiff GitLab instance at
https://gitlab.com/libtiff/libtiff/issues .

The migrated tickets have their summary prefixed with [BZ#XXXX] where XXXX is
the initial Bugzilla issue number.