LibTIFF 4.7.2
TIFF library public API and implementation reference
Loading...
Searching...
No Matches
Macros | Functions
Strile loading and subdivision

Load deferred/lazy arrays and construct logical uncompressed strips. More...

Collaboration diagram for Strile loading and subdivision:

Macros

#define IO_CACHE_PAGE_SIZE   4096
 Set the lazy strile metadata read page size to 4096 bytes.
 
#define TIFF_MAX(a, b)   (((a) > (b)) ? (a) : (b))
 Select the larger of two expressions.
 
#define TIFF_MIN(a, b)   (((a) < (b)) ? (a) : (b))
 Select the smaller of two expressions.
 

Functions

int _TIFFFillStriles (TIFF *tif)
 Materialise deferred strile arrays when required.
 
static int TIFFFetchStripThing (TIFF *tif, TIFFDirEntry *dir, uint32_t nstrips, uint64_t **lpp)
 Read a strile array and pad permitted missing elements with zero.
 
static void allocChoppedUpStripArrays (TIFF *tif, uint32_t nstrips, uint64_t stripbytes, uint32_t rowsperstrip)
 Replace uncompressed strip metadata with a logical subdivision.
 
static void ChopUpSingleUncompressedStrip (TIFF *tif)
 Try to subdivide one contiguous uncompressed strip.
 
static void TryChopUpUncompressedBigTiff (TIFF *tif)
 Try to subdivide huge contiguous uncompressed strip data.
 
static TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW uint64_t _TIFFUnsanitizedAddUInt64AndInt (uint64_t a, int b)
 Add a signed displacement with intentional unsigned wrap.
 
static int _TIFFPartialReadStripArray (TIFF *tif, TIFFDirEntry *dirent, int strile, uint64_t *panVals)
 Load a strile value and neighbouring page entries into a cache.
 
static int _TIFFFetchStrileValue (TIFF *tif, uint32_t strile, TIFFDirEntry *dirent, uint64_t **parray)
 Grow paired strile caches and fetch an uncached value.
 
static uint64_t _TIFFGetStrileOffsetOrByteCountValue (TIFF *tif, uint32_t strile, TIFFDirEntry *dirent, uint64_t **parray, int *pbErr)
 Resolve a cached, fully deferred, or lazy strile metadata value.
 
static int _TIFFFillStrilesInternal (TIFF *tif, int loadStripByteCount)
 Materialise deferred strile arrays or recognise an already-loaded state.
 

Detailed Description

Load deferred/lazy arrays and construct logical uncompressed strips.

Macro Definition Documentation

◆ IO_CACHE_PAGE_SIZE

#define IO_CACHE_PAGE_SIZE   4096

Set the lazy strile metadata read page size to 4096 bytes.

The local helper can span two pages when an element crosses a boundary. The macro is defined/undefined inside _TIFFPartialReadStripArray().

◆ TIFF_MAX

#define TIFF_MAX (   a,
  b 
)    (((a) > (b)) ? (a) : (b))

Select the larger of two expressions.

Uses C comparison/conditional usual conversions. Each argument is evaluated in the comparison and the selected argument again; avoid side effects. Does not allocate or report diagnostics. Parenthesised expansion yields a value, not a checked size operation.

Parameters
[in]aFirst comparable expression; may be evaluated twice.
[in]bSecond comparable expression; may be evaluated twice.
Returns
Selected maximum according to the C comparison.

◆ TIFF_MIN

#define TIFF_MIN (   a,
  b 
)    (((a) < (b)) ? (a) : (b))

Select the smaller of two expressions.

Uses C comparison/conditional usual conversions. Each argument is evaluated in the comparison and the selected argument again; avoid side effects. Does not allocate or report diagnostics. Parenthesised expansion yields a value, not a checked size operation.

Parameters
[in]aFirst comparable expression; may be evaluated twice.
[in]bSecond comparable expression; may be evaluated twice.
Returns
Selected minimum according to the C comparison.

Function Documentation

◆ _TIFFFetchStrileValue()

static int _TIFFFetchStrileValue ( TIFF *  tif,
uint32_t  strile,
TIFFDirEntry *  dirent,
uint64_t **  parray 
)
static

Grow paired strile caches and fetch an uncached value.

Requires consistent deferred entries and a strile index below td_nstrips and representable by int for the page reader. Expands both handle-owned arrays together, using UINT64_MAX as uncached sentinel, then loads a page if needed. On either realloc failure frees both current arrays and resets their capacity, losing old cache contents. Partial read failure stores zero at the requested slot, so later calls may return success with that cached zero. Can allocate, seek/read, diagnose, and invalidate borrowed array pointers. No ownership transfers.

Parameters
[in,out]tifNon-NULL live handle with matching schema/state; serialise access externally.
[in]strileLogical strile index in range.
[in]direntNon-NULL external deferred entry with more than four elements.
[in,out]parrayPointer to the matching handle-owned offset or byte-count array slot.
Returns
One when an array slot is available/cached, zero on detected extent/allocation/read failure; zero can be cached after failure.

Definition at line 10375 of file tif_dirread.c.

References _TIFFfreeExt(), _TIFFPartialReadStripArray(), _TIFFreallocExt(), TIFFDirectory::td_stripoffsetbyteallocsize, TIFFDirEntry::tdir_count, tiff::tif_dir, TIFF_MAX, TIFF_MIN, TIFFErrorExtR(), and TIFFGetFileSize.

Referenced by _TIFFGetStrileOffsetOrByteCountValue().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _TIFFFillStriles()

int _TIFFFillStriles ( TIFF *  tif)
extern

Materialise deferred strile arrays when required.

Delegates to _TIFFFillStrilesInternal() with byte-count loading enabled. Lazy mode discards partial caches on first full load; replaces/allocates handle-owned arrays and may do I/O or report diagnostics. Clears deferred entries after the fetch attempt, including failure, so retry is not equivalent to the first call. Already loaded/chopped/non-deferred cases return one without validating all array contents. Borrowed array pointers may be invalidated. No ownership transfers.

Parameters
[in,out]tifNon-NULL live handle with matching schema/state; serialise access externally.
Returns
One for a completed load or no work, zero on detected load failure; partial arrays/state may remain.

Definition at line 10602 of file tif_dirread.c.

References _TIFFFillStrilesInternal().

Referenced by _TIFFGetStrileOffsetOrByteCountValue(), _TIFFVGetField(), TIFFReadDirectory(), TIFFWriteCheck(), and TIFFWriteDirectorySec().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _TIFFFillStrilesInternal()

static int _TIFFFillStrilesInternal ( TIFF *  tif,
int  loadStripByteCount 
)
static

Materialise deferred strile arrays or recognise an already-loaded state.

Returns one without work for non-deferred/chopped arrays. First full load in lazy mode frees both partial caches and marks lazy loading done. An existing offset array is treated as success without revalidating counts. Otherwise fetches offsets and optionally byte counts; clears both deferred entries after attempts, even on failure or when counts were not requested. Owns resulting arrays in the handle; borrowed views can be invalidated. Allocations/I/O/diagnostics and partial-state effects follow the fetch helpers; no rollback.

Parameters
[in,out]tifNon-NULL live handle with matching schema/state; serialise access externally.
[in]loadStripByteCountNonzero to fetch counts too; zero for offset-only recovery.
Returns
One on completed load/no work, zero on absent deferred offset count or detected load error; partial arrays/state may remain.

Definition at line 10625 of file tif_dirread.c.

References _TIFFfreeExt(), _TIFFmemset(), TIFFDirectory::td_nstrips, TIFFDirectory::td_stripbytecount_entry, TIFFDirectory::td_stripbytecount_p, TIFFDirectory::td_stripoffset_entry, TIFFDirectory::td_stripoffset_p, TIFFDirectory::td_stripoffsetbyteallocsize, TIFFDirEntry::tdir_count, tiff::tif_dir, tiff::tif_flags, TIFF_CHOPPEDUPARRAYS, TIFF_DEFERSTRILELOAD, TIFF_LAZYSTRILELOAD_ASKED, TIFF_LAZYSTRILELOAD_DONE, and TIFFFetchStripThing().

Referenced by _TIFFFillStriles(), and EstimateStripByteCounts().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _TIFFGetStrileOffsetOrByteCountValue()

static uint64_t _TIFFGetStrileOffsetOrByteCountValue ( TIFF *  tif,
uint32_t  strile,
TIFFDirEntry *  dirent,
uint64_t **  parray,
int *  pbErr 
)
static

Resolve a cached, fully deferred, or lazy strile metadata value.

Validates index and consistency of the two small/large-entry paths. Loads full arrays or a lazy page as required, potentially freeing/reallocating paired caches. Initialises optional error status to zero; sets it for errors detected now, not historical cached failures. A failed full fill does not force an immediate zero if an array value remains available. No pixel decoding, ownership transfer, or range validation of the resulting file payload.

Parameters
[in,out]tifNon-NULL live handle with matching schema/state; serialise access externally.
[in]strileZero-based logical strile index.
[in,out]direntBorrowed matching deferred entry; may be cleared indirectly by full fill.
[in,out]parrayMatching handle-owned offset/count array slot; may be replaced/cleared.
[out]pbErrOptional current-call error slot.
Returns
Cached value, or zero on unavailable/out-of-range data; pbErr can flag a fill failure even when a nonzero value is returned.

Definition at line 10501 of file tif_dirread.c.

References _TIFFFetchStrileValue(), _TIFFFillStriles(), TIFFDirectory::td_nstrips, TIFFDirectory::td_stripbytecount_entry, TIFFDirectory::td_stripoffset_entry, TIFFDirEntry::tdir_count, tiff::tif_dir, tiff::tif_flags, TIFF_CHOPPEDUPARRAYS, TIFF_DEFERSTRILELOAD, TIFF_LAZYSTRILELOAD_ASKED, and TIFFErrorExtR().

Referenced by TIFFGetStrileByteCountWithErr(), and TIFFGetStrileOffsetWithErr().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _TIFFPartialReadStripArray()

static int _TIFFPartialReadStripArray ( TIFF *  tif,
TIFFDirEntry *  dirent,
int  strile,
uint64_t *  panVals 
)
static

Load a strile value and neighbouring page entries into a cache.

Requires an external entry with count greater than four, a valid nonnegative cache index, and a sufficiently allocated panVals array. Accepts SHORT/LONG/LONG8 and nonconforming SLONG8; negative signed values are cast modulo 2^64 without a nonnegative range check. Uses a stack buffer for one/two 4096-byte pages, normalises endianness, and updates multiple cache elements. Errors can zero the target or leave partial/unchanged cache data; diagnostics vary and I/O position changes. Does not allocate or transfer ownership.

Parameters
[in,out]tifNon-NULL live handle with matching schema/state; serialise access externally.
[in]direntNon-NULL deferred parsed entry with raw offset bytes.
[in]strileIndex within the allocated logical cache; representable by int.
[in,out]panValsWritable borrowed uint64_t cache with td_stripoffsetbyteallocsize elements.
Returns
One after page conversion, zero on type/range/seek/read failure; cache outputs are not rollback-safe.

Definition at line 10164 of file tif_dirread.c.

References _TIFFUnsanitizedAddUInt64AndInt(), IO_CACHE_PAGE_SIZE, SeekOK, TIFFDirectory::td_stripoffsetbyteallocsize, TIFFDirEntry::tdir_count, TIFFDirEntry::tdir_offset, TIFFDirEntry::tdir_type, tiff::tif_dir, tiff::tif_flags, TIFF_BIGTIFF, TIFF_LONG, TIFF_LONG8, TIFF_SHORT, TIFF_SLONG8, TIFF_SWAB, TIFFErrorExtR(), TIFFReadFile, TIFFSwabLong(), TIFFSwabLong8(), TIFFSwabShort(), TIFFDirEntry::toff_long, and TIFFDirEntry::toff_long8.

Referenced by _TIFFFetchStrileValue().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _TIFFUnsanitizedAddUInt64AndInt()

static TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW uint64_t _TIFFUnsanitizedAddUInt64AndInt ( uint64_t  a,
int  b 
)
static

Add a signed displacement with intentional unsigned wrap.

Uses modulo-2^64 arithmetic after converting b to uint64_t. No diagnostics, allocation, state, or ownership effects. The source disables unsigned-overflow sanitisation for this helper.

Parameters
[in]aUnsigned base value.
[in]bSigned displacement, including negative page-relative indices.
Returns
Wrapped unsigned sum, not a checked size/offset result.

Definition at line 10123 of file tif_dirread.c.

Referenced by _TIFFPartialReadStripArray().

Here is the caller graph for this function:

◆ allocChoppedUpStripArrays()

static void allocChoppedUpStripArrays ( TIFF *  tif,
uint32_t  nstrips,
uint64_t  stripbytes,
uint32_t  rowsperstrip 
)
static

Replace uncompressed strip metadata with a logical subdivision.

Requires consistent existing contiguous uncompressed geometry and nonzero old/new strip counts. Computes the old total span, allocates both new arrays, and only then frees/replaces old arrays, adjusts counts and RowsPerStrip, and marks chopped arrays. Allocation/large-size rejection can diagnose; span rejection is silent and leaves old arrays. May load deferred old values first, so caches/I/O can change even on refusal. Ignores RowsPerStrip setter status. Arrays remain handle-owned and borrowed views can be invalidated; does not rewrite the on-disk IFD.

Parameters
[in,out]tifNon-NULL live handle with matching schema/state; serialise access externally.
[in]nstripsPositive new number of logical strips.
[in]stripbytesRequested full-strip bytes, clipped to remaining total span.
[in]rowsperstripPositive logical row count per new strip.

Definition at line 9816 of file tif_dirread.c.

References _TIFFCheckMalloc(), _TIFFfreeExt(), TIFFDirectory::td_nstrips, TIFFDirectory::td_stripbytecount_p, TIFFDirectory::td_stripoffset_p, TIFFDirectory::td_stripsperimage, tiff::tif_dir, tiff::tif_flags, TIFF_CHOPPEDUPARRAYS, TIFFGetFileSize, TIFFGetStrileByteCount(), TIFFGetStrileOffset(), TIFFSetField(), TIFFTAG_ROWSPERSTRIP, and TIFFWarningExtR().

Referenced by ChopUpSingleUncompressedStrip(), and TryChopUpUncompressedBigTiff().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ChopUpSingleUncompressedStrip()

static void ChopUpSingleUncompressedStrip ( TIFF *  tif)
static

Try to subdivide one contiguous uncompressed strip.

Requires strip-chopping enabled, contiguous samples, one uncompressed non-tiled strip, and established geometry. Targets STRIP_SIZE_DEFAULT with rowblock/YCbCr alignment. Oversized geometry, suspicious file extents, or allocation failure can leave the original layout; diagnostics follow size/allocation helpers. May load metadata and change I/O/caches before declining. Successful replacement changes directory-owned strile arrays/RowsPerStrip without changing on-disk entries; old borrowed views may be invalidated.

Parameters
[in,out]tifNon-NULL live handle with matching schema/state; serialise access externally.

Definition at line 9929 of file tif_dirread.c.

References allocChoppedUpStripArrays(), isUpSampled, PHOTOMETRIC_YCBCR, PLANARCONFIG_CONTIG, STRIP_SIZE_DEFAULT, TIFFDirectory::td_imagelength, TIFFDirectory::td_photometric, TIFFDirectory::td_planarconfig, TIFFDirectory::td_rowsperstrip, TIFFDirectory::td_ycbcrsubsampling, tiff::tif_dir, tiff::tif_mode, TIFFGetFileSize, TIFFGetStrileByteCount(), TIFFGetStrileOffset(), TIFFhowmany_32, and TIFFVTileSize64().

Referenced by TIFFReadDirectory().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ TIFFFetchStripThing()

static int TIFFFetchStripThing ( TIFF *  tif,
TIFFDirEntry *  dir,
uint32_t  nstrips,
uint64_t **  lpp 
)
static

Read a strile array and pad permitted missing elements with zero.

Reads at most nstrips values into newly owned host-order uint64_t storage; excess entries are ignored. A short array warns and is zero-padded to nstrips when nstrips is within LIBTIFF_STRILE_ARRAY_MAX_RESIZE_COUNT (default one million; a malformed or out-of-range value keeps the default); otherwise an error is reported and zero is returned. A padded array larger than 100 MiB that also exceeds the file size is refused with a warning and zero is returned. Does not free prior *lpp; provide an empty slot. On failure frees new temporary data; conversion and allocation errors are reported. Success transfers an Ext allocation to be released with _TIFFfreeExt(tif,...); I/O position may change.

Parameters
[in,out]tifNon-NULL live handle with matching schema/state; serialise access externally.
[in]dirNon-NULL parsed offset/count entry.
[in]nstripsPositive expected number of logical striles.
[out]lppNon-NULL empty owner slot; assigned on success.
Returns
One with the new array (NULL when nstrips is zero), zero on rejected resize or conversion/allocation/I/O error.

Definition at line 9651 of file tif_dirread.c.

References _TIFFCheckMalloc(), _TIFFfreeExt(), _TIFFmemcpy(), _TIFFmemset(), _TIFFParseUInt32(), _TIFFField::field_name, TIFFDirEntry::tdir_count, TIFFDirEntry::tdir_tag, TIFFFieldWithTag(), TIFFGetFileSize, TIFFReadDirEntryErrCount, TIFFReadDirEntryErrOk, TIFFReadDirEntryLong8ArrayWithLimit(), TIFFReadDirEntryOutputErr(), and TIFFWarningExtR().

Referenced by _TIFFFillStrilesInternal(), and TIFFReadDirectory().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ TryChopUpUncompressedBigTiff()

static void TryChopUpUncompressedBigTiff ( TIFF *  tif)
static

Try to subdivide huge contiguous uncompressed strip data.

Requires contiguous, non-tiled, uncompressed, chop-enabled geometry with strip size above INT32_MAX. Checks existing strip sizes/contiguity and rowblock bounds, then aims for 512 MiB logical strips, respecting YCbCr rowblocks. Refuses unsupported/suspicious cases without a status; allocation helpers can diagnose. May allocate/load metadata and change I/O/caches. On replacement directory arrays/RowsPerStrip change and borrowed views are invalidated; file bytes are unchanged.

Parameters
[in,out]tifNon-NULL live handle with matching schema/state; serialise access externally.

Definition at line 10014 of file tif_dirread.c.

References allocChoppedUpStripArrays(), COMPRESSION_NONE, isUpSampled, PHOTOMETRIC_YCBCR, PLANARCONFIG_CONTIG, TIFFDirectory::td_compression, TIFFDirectory::td_imagelength, TIFFDirectory::td_nstrips, TIFFDirectory::td_photometric, TIFFDirectory::td_planarconfig, TIFFDirectory::td_rowsperstrip, TIFFDirectory::td_ycbcrsubsampling, tiff::tif_dir, tiff::tif_flags, tiff::tif_mode, TIFF_ISTILED, TIFF_STRIPCHOP, TIFFGetFileSize, TIFFGetStrileByteCount(), TIFFGetStrileOffset(), TIFFhowmany_32, TIFFStripSize64(), and TIFFVStripSize64().

Referenced by TIFFReadDirectory().

Here is the call graph for this function:
Here is the caller graph for this function: