LCOV - code coverage report
Current view: top level - frmts/gtiff/libtiff - tif_zstd.c (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 150 194 77.3 %
Date: 2026-12-05 15:13:29 Functions: 12 13 92.3 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2017, Planet Labs
       3             :  * Author: <even.rouault at spatialys.com>
       4             :  *
       5             :  * Permission to use, copy, modify, distribute, and sell this software and
       6             :  * its documentation for any purpose is hereby granted without fee, provided
       7             :  * that (i) the above copyright notices and this permission notice appear in
       8             :  * all copies of the software and related documentation, and (ii) the names of
       9             :  * Sam Leffler and Silicon Graphics may not be used in any advertising or
      10             :  * publicity relating to the software without the specific, prior written
      11             :  * permission of Sam Leffler and Silicon Graphics.
      12             :  *
      13             :  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
      14             :  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
      15             :  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
      16             :  *
      17             :  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
      18             :  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
      19             :  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
      20             :  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
      21             :  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
      22             :  * OF THIS SOFTWARE.
      23             :  */
      24             : 
      25             : /**
      26             :  * @file tif_zstd.c
      27             :  * @brief Implement predictor-aware ZSTD streaming compression.
      28             :  */
      29             : 
      30             : #include "tiffiop.h"
      31             : #ifdef ZSTD_SUPPORT
      32             : /*
      33             :  * TIFF Library.
      34             :  *
      35             :  * ZSTD Compression Support
      36             :  *
      37             :  */
      38             : 
      39             : #include "tif_predict.h"
      40             : #include "zstd.h"
      41             : 
      42             : #include <stdio.h>
      43             : 
      44             : /*
      45             :  * State block for each open TIFF file using ZSTD compression/decompression.
      46             :  */
      47             : /**
      48             :  * @brief Own ZSTD streams predictor prefix and output cursor.
      49             :  * @ingroup libtiff_private_zstd
      50             :  *
      51             :  * Codec-owned state must remain live until cleanup. Internal buffers and
      52             :  * dependency objects have the individual ownership described below;
      53             :  * borrowed parent methods must remain callable.
      54             :  */
      55             : typedef struct
      56             : {
      57             :     /** @brief Predictor prefix, first in the codec state. */
      58             :     TIFFPredictorState predict;
      59             :     /**
      60             :      * @brief Owned dependency decompression stream; released by
      61             :      * ZSTD_freeDStream.
      62             :      */
      63             :     ZSTD_DStream *dstream;
      64             :     /**
      65             :      * @brief Owned dependency compression stream; released by ZSTD_freeCStream.
      66             :      */
      67             :     ZSTD_CStream *cstream;
      68             :     /**
      69             :      * @brief Cached level applied on the next compression-stream
      70             :      * initialisation.
      71             :      */
      72             :     int compression_level;
      73             :     /** @brief Dependency output descriptor borrowing the handle raw buffer. */
      74             :     ZSTD_outBuffer out_buffer;
      75             :     /**
      76             :      * @brief Decode/encode initialisation flags; switching modes releases the
      77             :      * old stream.
      78             :      */
      79             :     int state;
      80             : #define LSTATE_INIT_DECODE 0x01
      81             : #define LSTATE_INIT_ENCODE 0x02
      82             : 
      83             :     /** @brief Borrowed parent tag getter restored during cleanup. */
      84             :     TIFFVGetMethod vgetparent;
      85             :     /** @brief Borrowed parent tag setter restored during cleanup. */
      86             :     TIFFVSetMethod vsetparent;
      87             : } ZSTDState;
      88             : 
      89             : #define GetZSTDState(tif) ((ZSTDState *)(tif)->tif_data)
      90             : #define ZSTDDecoderState(tif) GetZSTDState(tif)
      91             : #define ZSTDEncoderState(tif) GetZSTDState(tif)
      92             : 
      93             : static int ZSTDEncode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s);
      94             : static int ZSTDDecode(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s);
      95             : 
      96             : /**
      97             :  * @brief Accept ZSTD tag fixup without changes.
      98             :  * @ingroup libtiff_private_zstd
      99             :  *
     100             :  * No allocation, I/O, diagnostics, state or ownership effects.
     101             :  *
     102             :  * @param[in] tif Unused handle.
     103             :  * @return One unconditionally.
     104             :  */
     105         872 : static int ZSTDFixupTags(TIFF *tif)
     106             : {
     107             :     (void)tif;
     108         872 :     return 1;
     109             : }
     110             : 
     111             : /**
     112             :  * @brief Switch ZSTD state into decode mode.
     113             :  * @ingroup libtiff_private_zstd
     114             :  *
     115             :  * Frees any previous compression stream and clears its pointer, then sets
     116             :  * decode flags. Invalidates compression-stream views; no raw/pixel I/O,
     117             :  * diagnostics or output allocation. Requires initialised state.
     118             :  *
     119             :  * @param[in,out] tif Non-NULL live ZSTD handle with valid
     120             :  * predictor-prefixed state; serialise operations.
     121             :  * @return One unconditionally.
     122             :  */
     123         192 : static int ZSTDSetupDecode(TIFF *tif)
     124             : {
     125         192 :     ZSTDState *sp = ZSTDDecoderState(tif);
     126             : 
     127         192 :     assert(sp != NULL);
     128             : 
     129             :     /* if we were last encoding, terminate this mode */
     130         192 :     if (sp->state & LSTATE_INIT_ENCODE)
     131             :     {
     132          32 :         ZSTD_freeCStream(sp->cstream);
     133          32 :         sp->cstream = NULL;
     134          32 :         sp->state = 0;
     135             :     }
     136             : 
     137         192 :     sp->state |= LSTATE_INIT_DECODE;
     138         192 :     return 1;
     139             : }
     140             : 
     141             : /*
     142             :  * Setup state for decoding a strip.
     143             :  */
     144             : /**
     145             :  * @brief Create or reset the ZSTD decompression stream for a strile.
     146             :  * @ingroup libtiff_private_zstd
     147             :  *
     148             :  * Ensures decode setup, lazily creates a dependency-owned DStream and
     149             :  * resets it. Stream remains codec-owned for cleanup even after init
     150             :  * failure. Diagnoses dependency allocation/init errors; no pixel reads or
     151             :  * caller storage ownership transfer.
     152             :  *
     153             :  * @param[in,out] tif Non-NULL live ZSTD handle with valid
     154             :  * predictor-prefixed state; serialise operations.
     155             :  * @param[in] s Unused sample-plane argument.
     156             :  * @return One after reset, zero on dependency allocation/init failure.
     157             :  */
     158         956 : static int ZSTDPreDecode(TIFF *tif, uint16_t s)
     159             : {
     160             :     static const char module[] = "ZSTDPreDecode";
     161         956 :     ZSTDState *sp = ZSTDDecoderState(tif);
     162             :     size_t zstd_ret;
     163             : 
     164             :     (void)s;
     165         956 :     assert(sp != NULL);
     166             : 
     167         956 :     if ((sp->state & LSTATE_INIT_DECODE) == 0)
     168          32 :         tif->tif_setupdecode(tif);
     169             : 
     170         956 :     if (sp->dstream == NULL)
     171             :     {
     172         192 :         sp->dstream = ZSTD_createDStream();
     173         192 :         if (sp->dstream == NULL)
     174             :         {
     175           0 :             TIFFErrorExtR(tif, module, "Cannot allocate decompression stream");
     176           0 :             return 0;
     177             :         }
     178             :     }
     179             : 
     180         956 :     zstd_ret = ZSTD_initDStream(sp->dstream);
     181         956 :     if (ZSTD_isError(zstd_ret))
     182             :     {
     183           0 :         TIFFErrorExtR(tif, module, "Error in ZSTD_initDStream(): %s",
     184             :                       ZSTD_getErrorName(zstd_ret));
     185           0 :         return 0;
     186             :     }
     187             : 
     188         956 :     return 1;
     189             : }
     190             : 
     191             : /**
     192             :  * @brief Decompress cached ZSTD input into caller output.
     193             :  * @ingroup libtiff_private_zstd
     194             :  *
     195             :  * Requires decode mode and initialised DStream. Repeatedly decompresses
     196             :  * until output fills, frame ends or input exhausts. On dependency error or
     197             :  * short output zeroes the unfilled tail and diagnoses, without committing
     198             :  * local raw cursor consumption; stream/output may already change. On
     199             :  * success advances rawcp and reduces rawcc. Does not require consuming a
     200             :  * complete frame when the requested output fills. No allocation owned by
     201             :  * caller, client I/O or ownership transfer.
     202             :  *
     203             :  * @param[in,out] tif Non-NULL live ZSTD handle with valid
     204             :  * predictor-prefixed state; serialise operations.
     205             :  * @param[out] op Borrowed writable occ-byte decoded output, disjoint from
     206             :  * raw input/state.
     207             :  * @param[in] occ Nonnegative decoded byte extent representable by size_t.
     208             :  * @param[in] s Unused sample-plane argument.
     209             :  * @return One when the requested output is filled, zero on
     210             :  * dependency/short-output failure.
     211             :  */
     212         956 : static int ZSTDDecode(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s)
     213             : {
     214             :     static const char module[] = "ZSTDDecode";
     215         956 :     ZSTDState *sp = ZSTDDecoderState(tif);
     216             :     ZSTD_inBuffer in_buffer;
     217             :     ZSTD_outBuffer out_buffer;
     218             :     size_t zstd_ret;
     219             : 
     220             :     (void)s;
     221         956 :     assert(sp != NULL);
     222         956 :     assert(sp->state == LSTATE_INIT_DECODE);
     223             : 
     224         956 :     in_buffer.src = tif->tif_rawcp;
     225         956 :     in_buffer.size = (size_t)tif->tif_rawcc;
     226         956 :     in_buffer.pos = 0;
     227             : 
     228         956 :     out_buffer.dst = op;
     229         956 :     out_buffer.size = (size_t)occ;
     230         956 :     out_buffer.pos = 0;
     231             : 
     232             :     do
     233             :     {
     234         956 :         zstd_ret = ZSTD_decompressStream(sp->dstream, &out_buffer, &in_buffer);
     235         956 :         if (ZSTD_isError(zstd_ret))
     236             :         {
     237           2 :             memset(op + out_buffer.pos, 0, out_buffer.size - out_buffer.pos);
     238           2 :             TIFFErrorExtR(tif, module, "Error in ZSTD_decompressStream(): %s",
     239             :                           ZSTD_getErrorName(zstd_ret));
     240           2 :             return 0;
     241             :         }
     242         210 :     } while (zstd_ret != 0 && in_buffer.pos < in_buffer.size &&
     243        1164 :              out_buffer.pos < out_buffer.size);
     244             : 
     245         954 :     if (out_buffer.pos < (size_t)occ)
     246             :     {
     247           0 :         memset(op + out_buffer.pos, 0, out_buffer.size - out_buffer.pos);
     248           0 :         TIFFErrorExtR(tif, module,
     249             :                       "Not enough data at scanline %lu (short %lu bytes)",
     250           0 :                       (unsigned long)tif->tif_dir.td_row,
     251           0 :                       (unsigned long)((size_t)occ - out_buffer.pos));
     252           0 :         return 0;
     253             :     }
     254             : 
     255         954 :     tif->tif_rawcp += in_buffer.pos;
     256         954 :     tif->tif_rawcc -= (tmsize_t)in_buffer.pos;
     257             : 
     258         954 :     return 1;
     259             : }
     260             : 
     261             : /**
     262             :  * @brief Switch ZSTD state into encode mode.
     263             :  * @ingroup libtiff_private_zstd
     264             :  *
     265             :  * Frees any previous decompression stream and clears its pointer, then sets
     266             :  * encode flags. No pixel I/O or caller-owned output allocation; dependency
     267             :  * views are invalidated.
     268             :  *
     269             :  * @param[in,out] tif Non-NULL live ZSTD handle with valid
     270             :  * predictor-prefixed state; serialise operations.
     271             :  * @return One unconditionally.
     272             :  */
     273         190 : static int ZSTDSetupEncode(TIFF *tif)
     274             : {
     275         190 :     ZSTDState *sp = ZSTDEncoderState(tif);
     276             : 
     277         190 :     assert(sp != NULL);
     278         190 :     if (sp->state & LSTATE_INIT_DECODE)
     279             :     {
     280           0 :         ZSTD_freeDStream(sp->dstream);
     281           0 :         sp->dstream = NULL;
     282           0 :         sp->state = 0;
     283             :     }
     284             : 
     285         190 :     sp->state |= LSTATE_INIT_ENCODE;
     286         190 :     return 1;
     287             : }
     288             : 
     289             : /*
     290             :  * Reset encoding state at the start of a strip.
     291             :  */
     292             : /**
     293             :  * @brief Create and initialise a ZSTD compression stream for a strile.
     294             :  * @ingroup libtiff_private_zstd
     295             :  *
     296             :  * Ensures encode mode, lazily creates CStream, initialises it at the cached
     297             :  * compression level and attaches the raw output buffer. Requires positive
     298             :  * usable raw capacity. Dependency stream remains codec-owned on later
     299             :  * failure; diagnoses dependency allocation/init errors. No payload flush or
     300             :  * input ownership transfer.
     301             :  *
     302             :  * @param[in,out] tif Non-NULL live ZSTD handle with valid
     303             :  * predictor-prefixed state; serialise operations.
     304             :  * @param[in] s Unused sample-plane argument.
     305             :  * @return One after initialisation, zero on dependency allocation/init
     306             :  * failure.
     307             :  */
     308         786 : static int ZSTDPreEncode(TIFF *tif, uint16_t s)
     309             : {
     310             :     static const char module[] = "ZSTDPreEncode";
     311         786 :     ZSTDState *sp = ZSTDEncoderState(tif);
     312             :     size_t zstd_ret;
     313             : 
     314             :     (void)s;
     315         786 :     assert(sp != NULL);
     316         786 :     if (sp->state != LSTATE_INIT_ENCODE)
     317           0 :         tif->tif_setupencode(tif);
     318             : 
     319         786 :     if (sp->cstream == NULL)
     320             :     {
     321         190 :         sp->cstream = ZSTD_createCStream();
     322         190 :         if (sp->cstream == NULL)
     323             :         {
     324           0 :             TIFFErrorExtR(tif, module, "Cannot allocate compression stream");
     325           0 :             return 0;
     326             :         }
     327             :     }
     328             : 
     329         786 :     zstd_ret = ZSTD_initCStream(sp->cstream, sp->compression_level);
     330         786 :     if (ZSTD_isError(zstd_ret))
     331             :     {
     332           0 :         TIFFErrorExtR(tif, module, "Error in ZSTD_initCStream(): %s",
     333             :                       ZSTD_getErrorName(zstd_ret));
     334           0 :         return 0;
     335             :     }
     336             : 
     337         786 :     sp->out_buffer.dst = tif->tif_rawdata;
     338         786 :     sp->out_buffer.size = (size_t)tif->tif_rawdatasize;
     339         786 :     sp->out_buffer.pos = 0;
     340             : 
     341         786 :     return 1;
     342             : }
     343             : 
     344             : /*
     345             :  * Encode a chunk of pixels.
     346             :  */
     347             : /**
     348             :  * @brief Compress input through the active ZSTD stream.
     349             :  * @ingroup libtiff_private_zstd
     350             :  *
     351             :  * Requires successful preencode and live positive-capacity raw storage;
     352             :  * a state not initialised for encoding is diagnosed and rejected. Consumes
     353             :  * borrowed input synchronously, flushing full output buffers via
     354             :  * TIFFFlushData1. Dependency/flush failures diagnose where applicable and
     355             :  * leave partial stream, cache and file progress. Input bytes are not
     356             :  * changed; no ownership transfer or rollback.
     357             :  *
     358             :  * @param[in,out] tif Non-NULL live ZSTD handle with valid
     359             :  * predictor-prefixed state; serialise operations.
     360             :  * @param[in] bp Borrowed readable cc-byte input, not retained or modified.
     361             :  * @param[in] cc Nonnegative input byte extent representable by size_t.
     362             :  * @param[in] s Unused sample-plane argument.
     363             :  * @return One when input has been consumed, zero on wrong codec state or
     364             :  * dependency/flush failure.
     365             :  */
     366         786 : static int ZSTDEncode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)
     367             : {
     368             :     static const char module[] = "ZSTDEncode";
     369         786 :     ZSTDState *sp = ZSTDEncoderState(tif);
     370             :     ZSTD_inBuffer in_buffer;
     371             :     size_t zstd_ret;
     372             : 
     373         786 :     assert(sp != NULL);
     374         786 :     if (sp->state != LSTATE_INIT_ENCODE)
     375             :     {
     376           0 :         TIFFErrorExtR(tif, module,
     377             :                       "ZSTD codec state is not initialized for encoding");
     378           0 :         return 0;
     379             :     }
     380             : 
     381             :     (void)s;
     382             : 
     383         786 :     in_buffer.src = bp;
     384         786 :     in_buffer.size = (size_t)cc;
     385         786 :     in_buffer.pos = 0;
     386             : 
     387             :     do
     388             :     {
     389             :         zstd_ret =
     390         786 :             ZSTD_compressStream(sp->cstream, &sp->out_buffer, &in_buffer);
     391         786 :         if (ZSTD_isError(zstd_ret))
     392             :         {
     393           0 :             TIFFErrorExtR(tif, module, "Error in ZSTD_compressStream(): %s",
     394             :                           ZSTD_getErrorName(zstd_ret));
     395           0 :             return 0;
     396             :         }
     397         786 :         if (sp->out_buffer.pos == sp->out_buffer.size)
     398             :         {
     399           0 :             tif->tif_rawcc = tif->tif_rawdatasize;
     400           0 :             if (!TIFFFlushData1(tif))
     401           0 :                 return 0;
     402           0 :             sp->out_buffer.dst = tif->tif_rawcp;
     403           0 :             sp->out_buffer.pos = 0;
     404             :         }
     405         786 :     } while (in_buffer.pos < in_buffer.size);
     406             : 
     407         786 :     return 1;
     408             : }
     409             : 
     410             : /*
     411             :  * Finish off an encoded strip by flushing it.
     412             :  */
     413             : /**
     414             :  * @brief Finish the ZSTD frame and flush its remaining output.
     415             :  * @ingroup libtiff_private_zstd
     416             :  *
     417             :  * Repeatedly ends the active stream and flushes produced bytes. Dependency
     418             :  * or I/O failure can leave an incomplete frame and changed state/file
     419             :  * bytes. Requires successful preencode and valid raw capacity; a state not
     420             :  * initialised for encoding is diagnosed and rejected. Does not
     421             :  * write the IFD or promise durability; no ownership transfer.
     422             :  *
     423             :  * @param[in,out] tif Non-NULL live ZSTD handle with valid
     424             :  * predictor-prefixed state; serialise operations.
     425             :  * @return One after frame completion and buffer flushes, zero on wrong
     426             :  * codec state or dependency/I/O failure.
     427             :  */
     428         786 : static int ZSTDPostEncode(TIFF *tif)
     429             : {
     430             :     static const char module[] = "ZSTDPostEncode";
     431         786 :     ZSTDState *sp = ZSTDEncoderState(tif);
     432             :     size_t zstd_ret;
     433             : 
     434         786 :     if (sp->state != LSTATE_INIT_ENCODE)
     435             :     {
     436           0 :         TIFFErrorExtR(tif, module,
     437             :                       "ZSTD codec state is not initialized for encoding");
     438           0 :         return 0;
     439             :     }
     440             : 
     441             :     do
     442             :     {
     443         786 :         zstd_ret = ZSTD_endStream(sp->cstream, &sp->out_buffer);
     444         786 :         if (ZSTD_isError(zstd_ret))
     445             :         {
     446           0 :             TIFFErrorExtR(tif, module, "Error in ZSTD_endStream(): %s",
     447             :                           ZSTD_getErrorName(zstd_ret));
     448           0 :             return 0;
     449             :         }
     450         786 :         if (sp->out_buffer.pos > 0)
     451             :         {
     452         786 :             tif->tif_rawcc = (tmsize_t)sp->out_buffer.pos;
     453         786 :             if (!TIFFFlushData1(tif))
     454           0 :                 return 0;
     455         786 :             sp->out_buffer.dst = tif->tif_rawcp;
     456         786 :             sp->out_buffer.pos = 0;
     457             :         }
     458         786 :     } while (zstd_ret != 0);
     459         786 :     return 1;
     460             : }
     461             : 
     462             : /**
     463             :  * @brief Release ZSTD streams state and chained codec methods.
     464             :  * @ingroup libtiff_private_zstd
     465             :  *
     466             :  * Restores predictor and saved tag hooks, frees dependency streams with
     467             :  * their matching ZSTD free functions, frees outer Ext state, clears
     468             :  * tif_data and installs default callbacks. Invalidates state/stream views.
     469             :  * Does not finish frames, flush payload, close the handle or return a
     470             :  * status.
     471             :  *
     472             :  * @param[in,out] tif Non-NULL live ZSTD handle with valid
     473             :  * predictor-prefixed state; serialise operations.
     474             :  */
     475        1129 : static void ZSTDCleanup(TIFF *tif)
     476             : {
     477        1129 :     ZSTDState *sp = GetZSTDState(tif);
     478             : 
     479        1129 :     assert(sp != 0);
     480             : 
     481        1129 :     (void)TIFFPredictorCleanup(tif);
     482             : 
     483        1129 :     tif->tif_tagmethods.vgetfield = sp->vgetparent;
     484        1129 :     tif->tif_tagmethods.vsetfield = sp->vsetparent;
     485             : 
     486        1129 :     if (sp->dstream)
     487             :     {
     488         192 :         ZSTD_freeDStream(sp->dstream);
     489         192 :         sp->dstream = NULL;
     490             :     }
     491        1129 :     if (sp->cstream)
     492             :     {
     493         158 :         ZSTD_freeCStream(sp->cstream);
     494         158 :         sp->cstream = NULL;
     495             :     }
     496        1129 :     _TIFFfreeExt(tif, sp);
     497        1129 :     tif->tif_data = NULL;
     498             : 
     499        1129 :     _TIFFSetDefaultCompressionState(tif);
     500        1129 : }
     501             : 
     502             : /**
     503             :  * @brief Store ZSTD compression level or delegate another tag assignment.
     504             :  * @ingroup libtiff_private_zstd
     505             :  *
     506             :  * TIFFTAG_ZSTD_LEVEL consumes int, stores it even outside
     507             :  * 1..ZSTD_maxCLevel(), warns for that range and returns success. Stream
     508             :  * reinitialisation applies it later. Other tags follow parent variadic
     509             :  * contracts; no ownership transfer for the level. Callbacks/diagnostics can
     510             :  * mutate state.
     511             :  *
     512             :  * @param[in,out] tif Non-NULL live ZSTD handle with valid
     513             :  * predictor-prefixed state; serialise operations.
     514             :  * @param[in] tag Pseudo-tag or delegated tag identifier.
     515             :  * @param[in,out] ap Live tag-specific va_list consumed synchronously; not
     516             :  * retained.
     517             :  * @return One for level assignment including warned values, otherwise
     518             :  * parent status.
     519             :  */
     520       10595 : static int ZSTDVSetField(TIFF *tif, uint32_t tag, va_list ap)
     521             : {
     522             :     static const char module[] = "ZSTDVSetField";
     523       10595 :     ZSTDState *sp = GetZSTDState(tif);
     524             : 
     525       10595 :     switch (tag)
     526             :     {
     527          26 :         case TIFFTAG_ZSTD_LEVEL:
     528          26 :             sp->compression_level = (int)va_arg(ap, int);
     529          52 :             if (sp->compression_level <= 0 ||
     530          26 :                 sp->compression_level > ZSTD_maxCLevel())
     531             :             {
     532           0 :                 TIFFWarningExtR(tif, module,
     533             :                                 "ZSTD_LEVEL should be between 1 and %d",
     534             :                                 ZSTD_maxCLevel());
     535             :             }
     536          26 :             return 1;
     537       10569 :         default:
     538       10569 :             return (*sp->vsetparent)(tif, tag, ap);
     539             :     }
     540             :     /*NOTREACHED*/
     541             : }
     542             : 
     543             : /**
     544             :  * @brief Retrieve ZSTD compression level or delegate another tag query.
     545             :  * @ingroup libtiff_private_zstd
     546             :  *
     547             :  * TIFFTAG_ZSTD_LEVEL consumes a non-NULL int* and writes the cached level.
     548             :  * Other tags follow parent variadic contracts. No allocation or ownership
     549             :  * transfer for the level.
     550             :  *
     551             :  * @param[in,out] tif Non-NULL live ZSTD handle with valid
     552             :  * predictor-prefixed state; serialise operations.
     553             :  * @param[in] tag Pseudo-tag or delegated tag identifier.
     554             :  * @param[in,out] ap Live tag-specific va_list consumed synchronously; not
     555             :  * retained.
     556             :  * @return One for level retrieval, otherwise parent status.
     557             :  */
     558       11529 : static int ZSTDVGetField(TIFF *tif, uint32_t tag, va_list ap)
     559             : {
     560       11529 :     ZSTDState *sp = GetZSTDState(tif);
     561             : 
     562       11529 :     switch (tag)
     563             :     {
     564           0 :         case TIFFTAG_ZSTD_LEVEL:
     565           0 :             *va_arg(ap, int *) = sp->compression_level;
     566           0 :             break;
     567       11529 :         default:
     568       11529 :             return (*sp->vgetparent)(tif, tag, ap);
     569             :     }
     570           0 :     return 1;
     571             : }
     572             : 
     573             : /**
     574             :  * @brief Define the ZSTD compression-level pseudo-tag.
     575             :  * @ingroup libtiff_private_zstd
     576             :  *
     577             :  * Immutable library-lifetime metadata merged into a handle; no caller
     578             :  * ownership transfer.
     579             :  */
     580             : static const TIFFField ZSTDFields[] = {
     581             :     {TIFFTAG_ZSTD_LEVEL, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, FIELD_PSEUDO, TRUE,
     582             :      FALSE, "ZSTD compression_level", NULL},
     583             : };
     584             : 
     585             : /**
     586             :  * @brief Report the ZSTD expansion allocation heuristic.
     587             :  * @ingroup libtiff_private_zstd
     588             :  *
     589             :  * Returns a fixed estimate without handle inspection, I/O, diagnostics,
     590             :  * allocation or state changes; not a universal bound.
     591             :  *
     592             :  * @param[in] tif Unused handle.
     593             :  * @return Thirty-three thousand.
     594             :  */
     595           0 : static uint64_t ZSTDGetMaxCompressionRatio(TIFF *tif)
     596             : {
     597             :     (void)tif;
     598             :     /* Value from the issue #479 tile allocation guard that previously lived
     599             :      * in _TIFFReadEncodedTileAndAllocBuffer(). */
     600           0 :     return 33000;
     601             : }
     602             : 
     603        1129 : int TIFFInitZSTD(TIFF *tif, int scheme)
     604             : {
     605             :     static const char module[] = "TIFFInitZSTD";
     606             :     ZSTDState *sp;
     607             : 
     608             :     (void)scheme;
     609        1129 :     assert(scheme == COMPRESSION_ZSTD);
     610             : 
     611             :     /*
     612             :      * Merge codec-specific tag information.
     613             :      */
     614        1129 :     if (!_TIFFMergeFields(tif, ZSTDFields, TIFFArrayCount(ZSTDFields)))
     615             :     {
     616           0 :         TIFFErrorExtR(tif, module, "Merging ZSTD codec-specific tags failed");
     617           0 :         return 0;
     618             :     }
     619             : 
     620             :     /*
     621             :      * Allocate state block so tag methods have storage to record values.
     622             :      */
     623        1129 :     tif->tif_data = (uint8_t *)_TIFFmallocExt(tif, sizeof(ZSTDState));
     624        1129 :     if (tif->tif_data == NULL)
     625           0 :         goto bad;
     626        1129 :     sp = GetZSTDState(tif);
     627             : 
     628             :     /*
     629             :      * Override parent get/set field methods.
     630             :      */
     631        1129 :     sp->vgetparent = tif->tif_tagmethods.vgetfield;
     632        1129 :     tif->tif_tagmethods.vgetfield = ZSTDVGetField; /* hook for codec tags */
     633        1129 :     sp->vsetparent = tif->tif_tagmethods.vsetfield;
     634        1129 :     tif->tif_tagmethods.vsetfield = ZSTDVSetField; /* hook for codec tags */
     635             : 
     636             :     /* Default values for codec-specific fields */
     637        1129 :     sp->compression_level = 9; /* default comp. level */
     638        1129 :     sp->state = 0;
     639        1129 :     sp->dstream = 0;
     640        1129 :     sp->cstream = 0;
     641        1129 :     sp->out_buffer.dst = NULL;
     642        1129 :     sp->out_buffer.size = 0;
     643        1129 :     sp->out_buffer.pos = 0;
     644             : 
     645             :     /*
     646             :      * Install codec methods.
     647             :      */
     648        1129 :     tif->tif_fixuptags = ZSTDFixupTags;
     649        1129 :     tif->tif_setupdecode = ZSTDSetupDecode;
     650        1129 :     tif->tif_predecode = ZSTDPreDecode;
     651        1129 :     tif->tif_decoderow = ZSTDDecode;
     652        1129 :     tif->tif_decodestrip = ZSTDDecode;
     653        1129 :     tif->tif_decodetile = ZSTDDecode;
     654        1129 :     tif->tif_setupencode = ZSTDSetupEncode;
     655        1129 :     tif->tif_preencode = ZSTDPreEncode;
     656        1129 :     tif->tif_postencode = ZSTDPostEncode;
     657        1129 :     tif->tif_encoderow = ZSTDEncode;
     658        1129 :     tif->tif_encodestrip = ZSTDEncode;
     659        1129 :     tif->tif_encodetile = ZSTDEncode;
     660        1129 :     tif->tif_cleanup = ZSTDCleanup;
     661        1129 :     tif->tif_getmaxcompressionratio = ZSTDGetMaxCompressionRatio;
     662             :     /*
     663             :      * Setup predictor setup.
     664             :      */
     665        1129 :     (void)TIFFPredictorInit(tif);
     666        1129 :     return 1;
     667           0 : bad:
     668           0 :     TIFFErrorExtR(tif, module, "No space for ZSTD state block");
     669           0 :     return 0;
     670             : }
     671             : #endif /* ZSTD_SUPPORT */
     672             : 
     673             : /**
     674             :  * @def LSTATE_INIT_DECODE
     675             :  * @brief Bit 0x01 of the codec state field: decoding has been set up.
     676             :  * @ingroup libtiff_private_zstd
     677             :  *
     678             :  * Set by ZSTDSetupDecode(). Each setup first tears down the other mode, if
     679             :  * active, and clears the field.
     680             :  */
     681             : 
     682             : /**
     683             :  * @def LSTATE_INIT_ENCODE
     684             :  * @brief Bit 0x02 of the codec state field: encoding has been set up.
     685             :  * @ingroup libtiff_private_zstd
     686             :  *
     687             :  * Set by ZSTDSetupEncode(). Each setup first tears down the other mode, if
     688             :  * active, and clears the field.
     689             :  */
     690             : 
     691             : /**
     692             :  * @def GetZSTDState(tif)
     693             :  * @brief Cast tif->tif_data to the ZSTDState.
     694             :  * @ingroup libtiff_private_zstd
     695             :  *
     696             :  * tif is evaluated once. Requires the codec to be initialised (tif_data
     697             :  * non-NULL).
     698             :  *
     699             :  * @param[in] tif Handle whose codec state is returned.
     700             :  */
     701             : 
     702             : /**
     703             :  * @def ZSTDDecoderState(tif)
     704             :  * @brief Alias of GetZSTDState(), returning the ZSTDState for decoding.
     705             :  * @ingroup libtiff_private_zstd
     706             :  *
     707             :  * tif is evaluated once. Requires the codec to be initialised.
     708             :  *
     709             :  * @param[in] tif Handle whose codec state is returned.
     710             :  */
     711             : 
     712             : /**
     713             :  * @def ZSTDEncoderState(tif)
     714             :  * @brief Alias of GetZSTDState(), returning the ZSTDState for encoding.
     715             :  * @ingroup libtiff_private_zstd
     716             :  *
     717             :  * tif is evaluated once. Requires the codec to be initialised.
     718             :  *
     719             :  * @param[in] tif Handle whose codec state is returned.
     720             :  */

Generated by: LCOV version 1.14