LibTIFF 4.7.2
TIFF library public API and implementation reference
Loading...
Searching...
No Matches
Functions
Checked arithmetic and conversions

Compute checked sizes/offsets and bounded scalar conversions. More...

Collaboration diagram for Checked arithmetic and conversions:

Functions

uint32_t _TIFFMultiply32 (TIFF *tif, uint32_t first, uint32_t second, const char *where)
 Multiply two unsigned values with an overflow check.
 
uint32_t _TIFFParseUInt32 (const char *str, uint32_t default_value, uint32_t max_value)
 Parse a decimal unsigned integer with a fallback value.
 
uint64_t _TIFFMultiply64 (TIFF *tif, uint64_t first, uint64_t second, const char *where)
 Multiply two unsigned values with an overflow check.
 
uint64_t _TIFFAdd64 (TIFF *tif, uint64_t first, uint64_t second, const char *where)
 Check and add two unsigned operands.
 
tmsize_t _TIFFMultiplySSize (TIFF *tif, tmsize_t first, tmsize_t second, const char *where)
 Check and multiply two signed-size operands.
 
tmsize_t _TIFFAddSSize (TIFF *tif, tmsize_t first, tmsize_t second, const char *where)
 Check and add two signed-size operands.
 
tmsize_t _TIFFCastUInt64ToSSize (TIFF *tif, uint64_t val, const char *module)
 Convert an unsigned 64-bit value after checking the destination range.
 
uint32_t _TIFFCastUInt64ToUInt32 (TIFF *tif, uint64_t val, const char *module)
 Convert an unsigned 64-bit value after checking the destination range.
 
uint32_t _TIFFMultiply32Ext (TIFF *tif, uint32_t first, uint32_t second, const char *where, bool *pOverflowed)
 Multiply two unsigned values with an overflow check, and record an overflow in a caller-provided flag.
 
uint64_t _TIFFAdd64Ext (TIFF *tif, uint64_t first, uint64_t second, const char *where, bool *pOverflowed)
 Check and add two unsigned operands, and record an overflow in a caller-provided flag.
 
uint32_t _TIFFCastUInt64ToUInt32Ext (TIFF *tif, uint64_t val, const char *module, bool *pOverflowed)
 Convert an unsigned 64-bit value after checking the destination range, and record a rejection in a caller-provided flag.
 
uint64_t _TIFFComputeRowSize64 (TIFF *tif, uint32_t width, uint16_t spp, uint16_t bps, const char *where)
 Compute a byte-rounded row size from width and sample geometry.
 
tmsize_t _TIFFComputeRowOffset (TIFF *tif, tmsize_t rowstride, uint32_t row, const char *where)
 Compute a checked byte offset for a row in a memory raster.
 
uint64_t _TIFFComputeBitOffset (TIFF *tif, uint32_t col, uint16_t spp, uint16_t bps, const char *where)
 Compute a checked packed-sample column offset in bits.
 
float _TIFFClampDoubleToFloat (double val)
 Clamp a double to the finite float range before conversion.
 
uint32_t _TIFFClampDoubleToUInt32 (double val)
 Clamp and truncate a double to an unsigned 32-bit value.
 
int _TIFFSeekOK (TIFF *tif, toff_t off)
 Test whether an allowed absolute client seek reaches its target.
 

Detailed Description

Compute checked sizes/offsets and bounded scalar conversions.

Function Documentation

◆ _TIFFAdd64()

uint64_t _TIFFAdd64 ( TIFF *  tif,
uint64_t  first,
uint64_t  second,
const char *  where 
)
extern

Check and add two unsigned operands.

Unsigned operands. A rejection emits an error only when both tif and where are non-NULL; otherwise it returns zero silently. Uses bounds checks before the arithmetic operation. Does not allocate or change handle fields; diagnostics can have callback-defined effects.

Parameters
[in]tifOptional diagnostic handle.
[in]firstFirst operand.
[in]secondSecond operand.
[in]whereOptional borrowed diagnostic context name.
Returns
The result, or zero for invalid operands or a result beyond UINT64_MAX; a valid zero result is indistinguishable from rejection where zero is allowed.

Definition at line 85 of file tif_aux.c.

References _TIFFAdd64Ext().

Referenced by JPEGRawBytesPerClumpLine(), TIFFComputeStrip(), TIFFSeek(), and TIFFWriteScanline().

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

◆ _TIFFAdd64Ext()

uint64_t _TIFFAdd64Ext ( TIFF *  tif,
uint64_t  first,
uint64_t  second,
const char *  where,
bool *  pOverflowed 
)
extern

Check and add two unsigned operands, and record an overflow in a caller-provided flag.

Behaves as _TIFFAdd64(): a result beyond UINT64_MAX emits an error only when both tif and where are non-NULL; otherwise it returns zero silently. On overflow, also sets *pOverflowed to true when pOverflowed is not NULL. The flag is never reset to false, so several operations can share one flag that is tested once. Does not allocate or change handle fields; diagnostics can have callback-defined effects.

Parameters
[in]tifOptional diagnostic handle.
[in]firstFirst operand.
[in]secondSecond operand.
[in]whereOptional borrowed diagnostic context name.
[out]pOverflowedOverflow flag, or NULL; set to true on overflow and left unchanged otherwise.
Returns
The sum, or zero if it exceeds UINT64_MAX; zero is also a valid sum, which the flag tells apart.

Definition at line 180 of file tif_aux.c.

References TIFFErrorExtR().

Referenced by _TIFFAdd64(), and _TIFFComputeTileExt().

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

◆ _TIFFAddSSize()

tmsize_t _TIFFAddSSize ( TIFF *  tif,
tmsize_t  first,
tmsize_t  second,
const char *  where 
)
extern

Check and add two signed-size operands.

Both operands must be nonnegative; zero is accepted. A rejection emits an error only when both tif and where are non-NULL; otherwise it returns zero silently. Uses bounds checks before the arithmetic operation. Does not allocate or change handle fields; diagnostics can have callback-defined effects.

Parameters
[in]tifOptional diagnostic handle.
[in]firstFirst operand.
[in]secondSecond operand.
[in]whereOptional borrowed diagnostic context name.
Returns
The result, or zero for invalid operands or a result beyond TIFF_TMSIZE_T_MAX; a valid zero result is indistinguishable from rejection where zero is allowed.

Definition at line 116 of file tif_aux.c.

References TIFF_TMSIZE_T_MAX, and TIFFErrorExtR().

Here is the call graph for this function:

◆ _TIFFCastUInt64ToSSize()

tmsize_t _TIFFCastUInt64ToSSize ( TIFF *  tif,
uint64_t  val,
const char *  module 
)
extern

Convert an unsigned 64-bit value after checking the destination range.

Range rejection reports an error only when both tif and module are non-NULL. Otherwise it fails silently with zero. Does not allocate, retain pointers, or change handle state except for diagnostic callback effects.

Parameters
[in]tifOptional diagnostic handle.
[in]valUnsigned value to convert.
[in]moduleOptional borrowed diagnostic module name.
Returns
The converted value, or zero if val exceeds TIFF_TMSIZE_T_MAX; zero is also a valid converted value.

Definition at line 140 of file tif_aux.c.

References TIFF_TMSIZE_T_MAX, and TIFFErrorExtR().

Referenced by JBIGEncode(), JPEGDecodeRaw(), JPEGRawBytesPerClumpLine(), PredictorSetup(), TIFFRasterScanlineSize(), TIFFReadRawStrip(), TIFFReadRawTile(), TIFFScanlineSize(), TIFFStripSize(), TIFFTileRowSize(), TIFFTileSize(), TIFFVStripSize(), TIFFVTileSize(), TIFFWriteDirectoryTagColormap(), and TIFFWriteDirectoryTagTransferfunction().

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

◆ _TIFFCastUInt64ToUInt32()

uint32_t _TIFFCastUInt64ToUInt32 ( TIFF *  tif,
uint64_t  val,
const char *  module 
)
extern

Convert an unsigned 64-bit value after checking the destination range.

Range rejection reports an error only when both tif and module are non-NULL. Otherwise it fails silently with zero. Does not allocate, retain pointers, or change handle state except for diagnostic callback effects.

Parameters
[in]tifOptional diagnostic handle.
[in]valUnsigned value to convert.
[in]moduleOptional borrowed diagnostic module name.
Returns
The converted value, or zero if val exceeds UINT32_MAX; zero is also a valid converted value.

Definition at line 153 of file tif_aux.c.

References _TIFFCastUInt64ToUInt32Ext().

Referenced by TIFFComputeStrip(), TIFFSeek(), TIFFWriteDirectoryTagColormap(), TIFFWriteDirectoryTagTransferfunction(), and TIFFWriteScanline().

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

◆ _TIFFCastUInt64ToUInt32Ext()

uint32_t _TIFFCastUInt64ToUInt32Ext ( TIFF *  tif,
uint64_t  val,
const char *  module,
bool *  pOverflowed 
)
extern

Convert an unsigned 64-bit value after checking the destination range, and record a rejection in a caller-provided flag.

Behaves as _TIFFCastUInt64ToUInt32(): range rejection reports an error only when both tif and module are non-NULL; otherwise it fails silently with zero. On rejection, also sets *pOverflowed to true when pOverflowed is not NULL. The flag is never reset to false, so several operations can share one flag that is tested once. Does not allocate, retain pointers, or change handle state except for diagnostic callback effects.

Parameters
[in]tifOptional diagnostic handle.
[in]valUnsigned value to convert.
[in]moduleOptional borrowed diagnostic module name.
[out]pOverflowedOverflow flag, or NULL; set to true if val exceeds UINT32_MAX and left unchanged otherwise.
Returns
The converted value, or zero if val exceeds UINT32_MAX; zero is also a valid converted value, which the flag tells apart.

Definition at line 197 of file tif_aux.c.

References TIFFErrorExtR().

Referenced by _TIFFCastUInt64ToUInt32(), and _TIFFComputeTileExt().

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

◆ _TIFFClampDoubleToFloat()

float _TIFFClampDoubleToFloat ( double  val)
extern

Clamp a double to the finite float range before conversion.

Performs no allocation, handle mutation, ownership transfer, or library diagnostics. Normal floating-point rounding/exception behaviour applies; NaN payload preservation is not promised.

Parameters
[in]valInput double, including infinities or NaN.
Returns
FLT_MAX or -FLT_MAX outside that finite range, otherwise the float conversion of val. Infinities clamp; NaN follows the floating conversion rather than selecting a bound.

Definition at line 602 of file tif_aux.c.

Referenced by _TIFFVSetField(), and TIFFWriteDirectoryTagSampleformatArray().

Here is the caller graph for this function:

◆ _TIFFClampDoubleToUInt32()

uint32_t _TIFFClampDoubleToUInt32 ( double  val)
extern

Clamp and truncate a double to an unsigned 32-bit value.

Performs no allocation, ownership transfer, handle mutation, or library diagnostics. Normal floating-point exception behaviour applies; the NaN-to-maximum rule is intentional current behaviour.

Parameters
[in]valInput double, including infinities or NaN.
Returns
Zero for negative values; UINT32_MAX for NaN or values above UINT32_MAX; otherwise the uint32_t conversion truncating toward zero.

Definition at line 611 of file tif_aux.c.

◆ _TIFFComputeBitOffset()

uint64_t _TIFFComputeBitOffset ( TIFF *  tif,
uint32_t  col,
uint16_t  spp,
uint16_t  bps,
const char *  where 
)
extern

Compute a checked packed-sample column offset in bits.

Uses _TIFFMultiply64() for the two products and its diagnostic rules. Does not round to a byte boundary or validate a buffer extent. Does not allocate or change handle fields.

Parameters
[in]tifOptional diagnostic handle.
[in]colZero-based pixel column.
[in]sppSamples per pixel.
[in]bpsBits per sample.
[in]whereNon-NULL diagnostic context if multiplication overflow is possible.
Returns
col * spp * bps, or zero for a zero product or checked rejection; these cases are not distinguished.

Definition at line 221 of file tif_aux.c.

References _TIFFMultiply64().

Here is the call graph for this function:

◆ _TIFFComputeRowOffset()

tmsize_t _TIFFComputeRowOffset ( TIFF *  tif,
tmsize_t  rowstride,
uint32_t  row,
const char *  where 
)
extern

Compute a checked byte offset for a row in a memory raster.

Delegates nonzero rows to _TIFFMultiplySSize(), including its conditional diagnostics. On narrower hosts, the row conversion must itself be representable. Does not allocate, validate an actual buffer extent, or change handle fields.

Parameters
[in]tifOptional diagnostic handle.
[in]rowstridePositive bytes per row when row is nonzero.
[in]rowZero-based row; must be representable by tmsize_t when nonzero.
[in]whereOptional borrowed diagnostic context.
Returns
Zero for row zero without validating rowstride; otherwise the checked positive product, or zero on rejection.

Definition at line 213 of file tif_aux.c.

References _TIFFMultiplySSize().

Here is the call graph for this function:

◆ _TIFFComputeRowSize64()

uint64_t _TIFFComputeRowSize64 ( TIFF *  tif,
uint32_t  width,
uint16_t  spp,
uint16_t  bps,
const char *  where 
)
extern

Compute a byte-rounded row size from width and sample geometry.

Rounds the complete row once, rather than each pixel/sample. Zero geometry returns zero without a direct diagnostic. Multiplication rejection uses _TIFFMultiply64() reporting rules. This is a generic packed-row computation, not a complete codec/YCbCr layout rule. No allocation, ownership transfer, or handle-field mutation occurs.

Parameters
[in]tifOptional diagnostic handle used by checked multiplication.
[in]widthRow width in pixels.
[in]sppSamples per pixel.
[in]bpsBits per sample.
[in]whereNon-NULL diagnostic context if multiplication overflow is possible.
Returns
ceil(width * spp * bps / 8), or zero if an intermediate product is zero or a multiplication check rejects it.

Definition at line 234 of file tif_aux.c.

References _TIFFMultiply64(), and TIFFhowmany8_64.

Here is the call graph for this function:

◆ _TIFFMultiply32()

uint32_t _TIFFMultiply32 ( TIFF *  tif,
uint32_t  first,
uint32_t  second,
const char *  where 
)
extern

Multiply two unsigned values with an overflow check.

Checks division bounds before multiplying. Overflow always invokes TIFFErrorExtR(), including when tif is NULL; where is also formatted as a string, so it must be valid on that path. Does not allocate, mutate handle fields, retain arguments, or perform I/O except through diagnostic callbacks.

Parameters
[in]tifDiagnostic handle, or NULL for global diagnostic dispatch.
[in]firstFirst factor.
[in]secondSecond factor.
[in]whereNon-NULL diagnostic context name when an overflow can occur.
Returns
The product, or zero if it exceeds UINT32_MAX; zero is also a valid product.

Definition at line 40 of file tif_aux.c.

References _TIFFMultiply32Ext().

Referenced by LERCPostEncode(), LERCPreDecode(), OJPEGReadHeaderInfo(), OJPEGWriteHeaderInfo(), SetupBuffers(), TIFFNumberOfStrips(), and TIFFNumberOfTiles().

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

◆ _TIFFMultiply32Ext()

uint32_t _TIFFMultiply32Ext ( TIFF *  tif,
uint32_t  first,
uint32_t  second,
const char *  where,
bool *  pOverflowed 
)
extern

Multiply two unsigned values with an overflow check, and record an overflow in a caller-provided flag.

Behaves as _TIFFMultiply32(): checks division bounds before multiplying, and overflow always invokes TIFFErrorExtR(), including when tif is NULL; where is also formatted as a string, so it must be valid on that path. On overflow, also sets *pOverflowed to true when pOverflowed is not NULL. The flag is never reset to false, so several operations can share one flag that is tested once. Does not allocate, mutate handle fields, retain arguments, or perform I/O except through diagnostic callbacks.

Parameters
[in]tifDiagnostic handle, or NULL for global diagnostic dispatch.
[in]firstFirst factor.
[in]secondSecond factor.
[in]whereNon-NULL diagnostic context name when an overflow can occur.
[out]pOverflowedOverflow flag, or NULL; set to true on overflow and left unchanged otherwise.
Returns
The product, or zero if it exceeds UINT32_MAX; zero is also a valid product, which the flag tells apart.

Definition at line 166 of file tif_aux.c.

References TIFFErrorExtR().

Referenced by _TIFFComputeTileExt(), and _TIFFMultiply32().

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

◆ _TIFFMultiply64()

uint64_t _TIFFMultiply64 ( TIFF *  tif,
uint64_t  first,
uint64_t  second,
const char *  where 
)
extern

Multiply two unsigned values with an overflow check.

Checks division bounds before multiplying. Overflow always invokes TIFFErrorExtR(), including when tif is NULL; where is also formatted as a string, so it must be valid on that path. Does not allocate, mutate handle fields, retain arguments, or perform I/O except through diagnostic callbacks.

Parameters
[in]tifDiagnostic handle, or NULL for global diagnostic dispatch.
[in]firstFirst factor.
[in]secondSecond factor.
[in]whereNon-NULL diagnostic context name when an overflow can occur.
Returns
The product, or zero if it exceeds UINT64_MAX; zero is also a valid product.

Definition at line 46 of file tif_aux.c.

References TIFFErrorExtR().

Referenced by _TIFFComputeBitOffset(), _TIFFComputeRowSize64(), _TIFFStrileSize64(), JBIGEncode(), JPEGDecodeRaw(), JPEGRawBytesPerClumpLine(), PredictorSetup(), TIFFComputeStrip(), TIFFRasterScanlineSize64(), TIFFScanlineSize64(), TIFFSeek(), TIFFTileRowSize64(), TIFFWriteDirectoryTagColormap(), TIFFWriteDirectoryTagTransferfunction(), and TIFFWriteScanline().

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

◆ _TIFFMultiplySSize()

tmsize_t _TIFFMultiplySSize ( TIFF *  tif,
tmsize_t  first,
tmsize_t  second,
const char *  where 
)
extern

Check and multiply two signed-size operands.

Both operands must be positive; zero and negatives are rejected. A rejection emits an error only when both tif and where are non-NULL; otherwise it returns zero silently. Uses bounds checks before the arithmetic operation. Does not allocate or change handle fields; diagnostics can have callback-defined effects.

Parameters
[in]tifOptional diagnostic handle.
[in]firstFirst operand.
[in]secondSecond operand.
[in]whereOptional borrowed diagnostic context name.
Returns
The result, or zero for invalid operands or a result beyond TIFF_TMSIZE_T_MAX; a valid zero result is indistinguishable from rejection where zero is allowed.

Definition at line 91 of file tif_aux.c.

References TIFF_TMSIZE_T_MAX, and TIFFErrorExtR().

Referenced by _TIFFCheckRealloc(), _TIFFComputeRowOffset(), _TIFFVSetField(), gtStripSeparate(), gtTileSeparate(), multiply_ms(), multiply_ms(), setByteArray(), setDoubleArrayOneValue(), and TIFFGrowStrips().

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

◆ _TIFFParseUInt32()

uint32_t _TIFFParseUInt32 ( const char *  str,
uint32_t  default_value,
uint32_t  max_value 
)
extern

Parse a decimal unsigned integer with a fallback value.

Accepts optional leading C whitespace and an optional '+', then one or more decimal digits and nothing else; trailing characters, including whitespace, are rejected. The value is accumulated with an overflow check against max_value. Used for environment-variable tuning limits. Does not allocate, retain str, use errno, or emit diagnostics.

Parameters
[in]strNUL-terminated text to parse, or NULL.
[in]default_valueValue returned when str is NULL, malformed, or out of range; it need not be at most max_value.
[in]max_valueLargest accepted value.
Returns
The parsed value in [0, max_value], or default_value.

Definition at line 58 of file tif_aux.c.

Referenced by TIFFFetchStripThing(), and TIFFjpeg_start_decompress().

Here is the caller graph for this function:

◆ _TIFFSeekOK()

int _TIFFSeekOK ( TIFF *  tif,
toff_t  off 
)
extern

Test whether an allowed absolute client seek reaches its target.

Offsets above the bound are rejected without seeking. Otherwise invokes one SEEK_SET callback. May change client position/state and inherits its failure conventions; a zero-returning native Windows seek failure can be confused with a successful seek to zero. Adds no diagnostics, allocation, or ownership transfer.

Parameters
[in,out]tifNon-NULL live TIFF handle with seek callback.
[in]offRequested absolute byte position.
Returns
Nonzero if off is at most UINT64_MAX / 2 and the client reports that exact position; zero otherwise.

Definition at line 620 of file tif_aux.c.

References TIFFSeekFile.