LCOV - code coverage report
Current view: top level - frmts/gtiff/libtiff - tif_ojpeg.c (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 721 1446 49.9 %
Date: 2025-01-18 12:42:00 Functions: 35 63 55.6 %

          Line data    Source code
       1             : /* WARNING: The type of JPEG encapsulation defined by the TIFF Version 6.0
       2             :    specification is now totally obsolete and deprecated for new applications and
       3             :    images. This file was was created solely in order to read unconverted images
       4             :    still present on some users' computer systems. It will never be extended
       5             :    to write such files. Writing new-style JPEG compressed TIFFs is implemented
       6             :    in tif_jpeg.c.
       7             : 
       8             :    The code is carefully crafted to robustly read all gathered JPEG-in-TIFF
       9             :    testfiles, and anticipate as much as possible all other... But still, it may
      10             :    fail on some. If you encounter problems, please report them on the TIFF
      11             :    mailing list and/or to Joris Van Damme <info@awaresystems.be>.
      12             : 
      13             :    Please read the file called "TIFF Technical Note #2" if you need to be
      14             :    convinced this compression scheme is bad and breaks TIFF. That document
      15             :    is linked to from the LibTiff site <http://www.remotesensing.org/libtiff/>
      16             :    and from AWare Systems' TIFF section
      17             :    <http://www.awaresystems.be/imaging/tiff.html>. It is also absorbed
      18             :    in Adobe's specification supplements, marked "draft" up to this day, but
      19             :    supported by the TIFF community.
      20             : 
      21             :    This file interfaces with Release 6B of the JPEG Library written by the
      22             :    Independent JPEG Group. Previous versions of this file required a hack inside
      23             :    the LibJpeg library. This version no longer requires that. Remember to
      24             :    remove the hack if you update from the old version.
      25             : 
      26             :    Copyright (c) Joris Van Damme <info@awaresystems.be>
      27             :    Copyright (c) AWare Systems <http://www.awaresystems.be/>
      28             : 
      29             :    The licence agreement for this file is the same as the rest of the LibTiff
      30             :    library.
      31             : 
      32             :    IN NO EVENT SHALL JORIS VAN DAMME OR AWARE SYSTEMS BE LIABLE FOR
      33             :    ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
      34             :    OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
      35             :    WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
      36             :    LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
      37             :    OF THIS SOFTWARE.
      38             : 
      39             :    Joris Van Damme and/or AWare Systems may be available for custom
      40             :    development. If you like what you see, and need anything similar or related,
      41             :    contact <info@awaresystems.be>.
      42             : */
      43             : 
      44             : /* What is what, and what is not?
      45             : 
      46             :    This decoder starts with an input stream, that is essentially the
      47             :    JpegInterchangeFormat stream, if any, followed by the strile data, if any.
      48             :    This stream is read in OJPEGReadByte and related functions.
      49             : 
      50             :    It analyzes the start of this stream, until it encounters non-marker data,
      51             :    i.e. compressed image data. Some of the header markers it sees have no actual
      52             :    content, like the SOI marker, and APP/COM markers that really shouldn't even
      53             :    be there. Some other markers do have content, and the valuable bits and
      54             :    pieces of information in these markers are saved, checking all to verify that
      55             :    the stream is more or less within expected bounds. This happens inside the
      56             :    OJPEGReadHeaderInfoSecStreamXxx functions.
      57             : 
      58             :    Some OJPEG imagery contains no valid JPEG header markers. This situation is
      59             :    picked up on if we've seen no SOF marker when we're at the start of the
      60             :    compressed image data. In this case, the tables are read from JpegXxxTables
      61             :    tags, and the other bits and pieces of information is initialized to its most
      62             :    basic value. This is implemented in the OJPEGReadHeaderInfoSecTablesXxx
      63             :    functions.
      64             : 
      65             :    When this is complete, a good and valid JPEG header can be assembled, and
      66             :    this is passed through to LibJpeg. When that's done, the remainder of the
      67             :    input stream, i.e. the compressed image data, can be passed through
      68             :    unchanged. This is done in OJPEGWriteStream functions.
      69             : 
      70             :    LibTiff rightly expects to know the subsampling values before decompression.
      71             :    Just like in new-style JPEG-in-TIFF, though, or even more so, actually, the
      72             :    YCbCrsubsampling tag is notoriously unreliable. To correct these tag values
      73             :    with the ones inside the JPEG stream, the first part of the input stream is
      74             :    pre-scanned in OJPEGSubsamplingCorrect, making no note of any other data,
      75             :    reporting no warnings or errors, up to the point where either these values
      76             :    are read, or it's clear they aren't there. This means that some of the data
      77             :    is read twice, but we feel speed in correcting these values is important
      78             :    enough to warrant this sacrifice. Although there is currently no define or
      79             :    other configuration mechanism to disable this behavior, the actual header
      80             :    scanning is build to robustly respond with error report if it should
      81             :    encounter an uncorrected mismatch of subsampling values. See
      82             :    OJPEGReadHeaderInfoSecStreamSof.
      83             : 
      84             :    The restart interval and restart markers are the most tricky part... The
      85             :    restart interval can be specified in a tag. It can also be set inside the
      86             :    input JPEG stream. It can be used inside the input JPEG stream. If reading
      87             :    from strile data, we've consistently discovered the need to insert restart
      88             :    markers in between the different striles, as is also probably the most likely
      89             :    interpretation of the original TIFF 6.0 specification. With all this setting
      90             :    of interval, and actual use of markers that is not predictable at the time of
      91             :    valid JPEG header assembly, the restart thing may turn out the Achilles heel
      92             :    of this implementation. Fortunately, most OJPEG writer vendors succeed in
      93             :    reading back what they write, which may be the reason why we've been able to
      94             :    discover ways that seem to work.
      95             : 
      96             :    Some special provision is made for planarconfig separate OJPEG files. These
      97             :    seem to consistently contain header info, a SOS marker, a plane, SOS marker,
      98             :    plane, SOS, and plane. This may or may not be a valid JPEG configuration, we
      99             :    don't know and don't care. We want LibTiff to be able to access the planes
     100             :    individually, without huge buffering inside LibJpeg, anyway. So we compose
     101             :    headers to feed to LibJpeg, in this case, that allow us to pass a single
     102             :    plane such that LibJpeg sees a valid single-channel JPEG stream. Locating
     103             :    subsequent SOS markers, and thus subsequent planes, is done inside
     104             :    OJPEGReadSecondarySos.
     105             : 
     106             :    The benefit of the scheme is... that it works, basically. We know of no other
     107             :    that does. It works without checking software tag, or otherwise going about
     108             :    things in an OJPEG flavor specific manner. Instead, it is a single scheme,
     109             :    that covers the cases with and without JpegInterchangeFormat, with and
     110             :    without striles, with part of the header in JpegInterchangeFormat and
     111             :    remainder in first strile, etc. It is forgiving and robust, may likely work
     112             :    with OJPEG flavors we've not seen yet, and makes most out of the data.
     113             : 
     114             :    Another nice side-effect is that a complete JPEG single valid stream is build
     115             :    if planarconfig is not separate (vast majority). We may one day use that to
     116             :    build converters to JPEG, and/or to new-style JPEG compression inside TIFF.
     117             : 
     118             :    A disadvantage is the lack of random access to the individual striles. This
     119             :    is the reason for much of the complicated restart-and-position stuff inside
     120             :    OJPEGPreDecode. Applications would do well accessing all striles in order, as
     121             :    this will result in a single sequential scan of the input stream, and no
     122             :    restarting of LibJpeg decoding session.
     123             : */
     124             : 
     125             : #define WIN32_LEAN_AND_MEAN
     126             : #define VC_EXTRALEAN
     127             : 
     128             : #include "tiffiop.h"
     129             : #ifdef OJPEG_SUPPORT
     130             : 
     131             : /* Configuration defines here are:
     132             :  * JPEG_ENCAP_EXTERNAL: The normal way to call libjpeg, uses longjump. In some
     133             :  * environments, like eg LibTiffDelphi, this is not possible. For this reason,
     134             :  * the actual calls to libjpeg, with longjump stuff, are encapsulated in
     135             :  * dedicated functions. When JPEG_ENCAP_EXTERNAL is defined, these encapsulating
     136             :  * functions are declared external to this unit, and can be defined elsewhere to
     137             :  * use stuff other then longjump. The default mode, without JPEG_ENCAP_EXTERNAL,
     138             :  * implements the call encapsulators here, internally, with normal longjump.
     139             :  * SETJMP, LONGJMP, JMP_BUF: On some machines/environments a longjump equivalent
     140             :  * is conveniently available, but still it may be worthwhile to use _setjmp or
     141             :  * sigsetjmp in place of plain setjmp. These macros will make it easier. It is
     142             :  * useless to fiddle with these if you define JPEG_ENCAP_EXTERNAL. OJPEG_BUFFER:
     143             :  * Define the size of the desired buffer here. Should be small enough so as to
     144             :  * guarantee instant processing, optimal streaming and optimal use of processor
     145             :  * cache, but also big enough so as to not result in significant call overhead.
     146             :  * It should be at least a few bytes to accommodate some structures (this is
     147             :  * verified in asserts), but it would not be sensible to make it this small
     148             :  * anyway, and it should be at most 64K since it is indexed with uint16_t. We
     149             :  * recommend 2K. EGYPTIANWALK: You could also define EGYPTIANWALK here, but it
     150             :  * is not used anywhere and has absolutely no effect. That is why most people
     151             :  * insist the EGYPTIANWALK is a bit silly.
     152             :  */
     153             : 
     154             : /* define LIBJPEG_ENCAP_EXTERNAL */
     155             : #define SETJMP(jbuf) setjmp(jbuf)
     156             : #define LONGJMP(jbuf, code) longjmp(jbuf, code)
     157             : #define JMP_BUF jmp_buf
     158             : #define OJPEG_BUFFER 2048
     159             : /* define EGYPTIANWALK */
     160             : 
     161             : #define JPEG_MARKER_SOF0 0xC0
     162             : #define JPEG_MARKER_SOF1 0xC1
     163             : #define JPEG_MARKER_SOF3 0xC3
     164             : #define JPEG_MARKER_DHT 0xC4
     165             : #define JPEG_MARKER_RST0 0XD0
     166             : #define JPEG_MARKER_SOI 0xD8
     167             : #define JPEG_MARKER_EOI 0xD9
     168             : #define JPEG_MARKER_SOS 0xDA
     169             : #define JPEG_MARKER_DQT 0xDB
     170             : #define JPEG_MARKER_DRI 0xDD
     171             : #define JPEG_MARKER_APP0 0xE0
     172             : #define JPEG_MARKER_COM 0xFE
     173             : 
     174             : #define FIELD_OJPEG_JPEGINTERCHANGEFORMAT (FIELD_CODEC + 0)
     175             : #define FIELD_OJPEG_JPEGINTERCHANGEFORMATLENGTH (FIELD_CODEC + 1)
     176             : #define FIELD_OJPEG_JPEGQTABLES (FIELD_CODEC + 2)
     177             : #define FIELD_OJPEG_JPEGDCTABLES (FIELD_CODEC + 3)
     178             : #define FIELD_OJPEG_JPEGACTABLES (FIELD_CODEC + 4)
     179             : #define FIELD_OJPEG_JPEGPROC (FIELD_CODEC + 5)
     180             : #define FIELD_OJPEG_JPEGRESTARTINTERVAL (FIELD_CODEC + 6)
     181             : 
     182             : static const TIFFField ojpegFields[] = {
     183             :     {TIFFTAG_JPEGIFOFFSET, 1, 1, TIFF_LONG8, 0, TIFF_SETGET_UINT64,
     184             :      FIELD_OJPEG_JPEGINTERCHANGEFORMAT, TRUE, FALSE, "JpegInterchangeFormat",
     185             :      NULL},
     186             :     {TIFFTAG_JPEGIFBYTECOUNT, 1, 1, TIFF_LONG8, 0, TIFF_SETGET_UINT64,
     187             :      FIELD_OJPEG_JPEGINTERCHANGEFORMATLENGTH, TRUE, FALSE,
     188             :      "JpegInterchangeFormatLength", NULL},
     189             :     {TIFFTAG_JPEGQTABLES, TIFF_VARIABLE2, TIFF_VARIABLE2, TIFF_LONG8, 0,
     190             :      TIFF_SETGET_C32_UINT64, FIELD_OJPEG_JPEGQTABLES, FALSE, TRUE,
     191             :      "JpegQTables", NULL},
     192             :     {TIFFTAG_JPEGDCTABLES, TIFF_VARIABLE2, TIFF_VARIABLE2, TIFF_LONG8, 0,
     193             :      TIFF_SETGET_C32_UINT64, FIELD_OJPEG_JPEGDCTABLES, FALSE, TRUE,
     194             :      "JpegDcTables", NULL},
     195             :     {TIFFTAG_JPEGACTABLES, TIFF_VARIABLE2, TIFF_VARIABLE2, TIFF_LONG8, 0,
     196             :      TIFF_SETGET_C32_UINT64, FIELD_OJPEG_JPEGACTABLES, FALSE, TRUE,
     197             :      "JpegAcTables", NULL},
     198             :     {TIFFTAG_JPEGPROC, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16,
     199             :      FIELD_OJPEG_JPEGPROC, FALSE, FALSE, "JpegProc", NULL},
     200             :     {TIFFTAG_JPEGRESTARTINTERVAL, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16,
     201             :      FIELD_OJPEG_JPEGRESTARTINTERVAL, FALSE, FALSE, "JpegRestartInterval",
     202             :      NULL},
     203             : };
     204             : 
     205             : #ifndef LIBJPEG_ENCAP_EXTERNAL
     206             : #include <setjmp.h>
     207             : #endif
     208             : 
     209             : #include "jerror.h"
     210             : #include "jpeglib.h"
     211             : 
     212             : #ifndef TIFF_jpeg_source_mgr_defined
     213             : #define TIFF_jpeg_source_mgr_defined
     214             : typedef struct jpeg_source_mgr jpeg_source_mgr;
     215             : #endif
     216             : 
     217             : #ifndef TIFF_jpeg_error_mgr_defined
     218             : #define TIFF_jpeg_error_mgr_defined
     219             : typedef struct jpeg_error_mgr jpeg_error_mgr;
     220             : #endif
     221             : 
     222             : typedef struct jpeg_common_struct jpeg_common_struct;
     223             : typedef struct jpeg_decompress_struct jpeg_decompress_struct;
     224             : 
     225             : typedef enum
     226             : {
     227             :     osibsNotSetYet,
     228             :     osibsJpegInterchangeFormat,
     229             :     osibsStrile,
     230             :     osibsEof
     231             : } OJPEGStateInBufferSource;
     232             : 
     233             : typedef enum
     234             : {
     235             :     ososSoi,
     236             :     ososQTable0,
     237             :     ososQTable1,
     238             :     ososQTable2,
     239             :     ososQTable3,
     240             :     ososDcTable0,
     241             :     ososDcTable1,
     242             :     ososDcTable2,
     243             :     ososDcTable3,
     244             :     ososAcTable0,
     245             :     ososAcTable1,
     246             :     ososAcTable2,
     247             :     ososAcTable3,
     248             :     ososDri,
     249             :     ososSof,
     250             :     ososSos,
     251             :     ososCompressed,
     252             :     ososRst,
     253             :     ososEoi
     254             : } OJPEGStateOutState;
     255             : 
     256             : typedef struct
     257             : {
     258             :     TIFF *tif;
     259             :     int decoder_ok;
     260             :     int error_in_raw_data_decoding;
     261             : #ifndef LIBJPEG_ENCAP_EXTERNAL
     262             :     JMP_BUF exit_jmpbuf;
     263             : #endif
     264             :     TIFFVGetMethod vgetparent;
     265             :     TIFFVSetMethod vsetparent;
     266             :     TIFFPrintMethod printdir;
     267             :     uint64_t file_size;
     268             :     uint32_t image_width;
     269             :     uint32_t image_length;
     270             :     uint32_t strile_width;
     271             :     uint32_t strile_length;
     272             :     uint32_t strile_length_total;
     273             :     uint8_t samples_per_pixel;
     274             :     uint8_t plane_sample_offset;
     275             :     uint8_t samples_per_pixel_per_plane;
     276             :     uint64_t jpeg_interchange_format;
     277             :     uint64_t jpeg_interchange_format_length;
     278             :     uint8_t jpeg_proc;
     279             :     uint8_t subsamplingcorrect;
     280             :     uint8_t subsamplingcorrect_done;
     281             :     uint8_t subsampling_tag;
     282             :     uint8_t subsampling_hor;
     283             :     uint8_t subsampling_ver;
     284             :     uint8_t subsampling_force_desubsampling_inside_decompression;
     285             :     uint8_t qtable_offset_count;
     286             :     uint8_t dctable_offset_count;
     287             :     uint8_t actable_offset_count;
     288             :     uint64_t qtable_offset[3];
     289             :     uint64_t dctable_offset[3];
     290             :     uint64_t actable_offset[3];
     291             :     uint8_t *qtable[4];
     292             :     uint8_t *dctable[4];
     293             :     uint8_t *actable[4];
     294             :     uint16_t restart_interval;
     295             :     uint8_t restart_index;
     296             :     uint8_t sof_log;
     297             :     uint8_t sof_marker_id;
     298             :     uint32_t sof_x;
     299             :     uint32_t sof_y;
     300             :     uint8_t sof_c[3];
     301             :     uint8_t sof_hv[3];
     302             :     uint8_t sof_tq[3];
     303             :     uint8_t sos_cs[3];
     304             :     uint8_t sos_tda[3];
     305             :     struct
     306             :     {
     307             :         uint8_t log;
     308             :         OJPEGStateInBufferSource in_buffer_source;
     309             :         uint32_t in_buffer_next_strile;
     310             :         uint64_t in_buffer_file_pos;
     311             :         uint64_t in_buffer_file_togo;
     312             :     } sos_end[3];
     313             :     uint8_t readheader_done;
     314             :     uint8_t writeheader_done;
     315             :     uint16_t write_cursample;
     316             :     uint32_t write_curstrile;
     317             :     uint8_t libjpeg_session_active;
     318             :     uint8_t libjpeg_jpeg_query_style;
     319             :     jpeg_error_mgr libjpeg_jpeg_error_mgr;
     320             :     jpeg_decompress_struct libjpeg_jpeg_decompress_struct;
     321             :     jpeg_source_mgr libjpeg_jpeg_source_mgr;
     322             :     uint8_t subsampling_convert_log;
     323             :     uint32_t subsampling_convert_ylinelen;
     324             :     uint32_t subsampling_convert_ylines;
     325             :     uint32_t subsampling_convert_clinelen;
     326             :     uint32_t subsampling_convert_clines;
     327             :     uint32_t subsampling_convert_ybuflen;
     328             :     uint32_t subsampling_convert_cbuflen;
     329             :     uint32_t subsampling_convert_ycbcrbuflen;
     330             :     uint8_t *subsampling_convert_ycbcrbuf;
     331             :     uint8_t *subsampling_convert_ybuf;
     332             :     uint8_t *subsampling_convert_cbbuf;
     333             :     uint8_t *subsampling_convert_crbuf;
     334             :     uint32_t subsampling_convert_ycbcrimagelen;
     335             :     uint8_t **subsampling_convert_ycbcrimage;
     336             :     uint32_t subsampling_convert_clinelenout;
     337             :     uint32_t subsampling_convert_state;
     338             :     uint32_t bytes_per_line;   /* if the codec outputs subsampled data, a 'line'
     339             :                                   in bytes_per_line */
     340             :     uint32_t lines_per_strile; /* and lines_per_strile means subsampling_ver
     341             :                                   desubsampled rows     */
     342             :     OJPEGStateInBufferSource in_buffer_source;
     343             :     uint32_t in_buffer_next_strile;
     344             :     uint32_t in_buffer_strile_count;
     345             :     uint64_t in_buffer_file_pos;
     346             :     uint8_t in_buffer_file_pos_log;
     347             :     uint64_t in_buffer_file_togo;
     348             :     uint16_t in_buffer_togo;
     349             :     uint8_t *in_buffer_cur;
     350             :     uint8_t in_buffer[OJPEG_BUFFER];
     351             :     OJPEGStateOutState out_state;
     352             :     uint8_t out_buffer[OJPEG_BUFFER];
     353             :     uint8_t *skip_buffer;
     354             : } OJPEGState;
     355             : 
     356             : static int OJPEGVGetField(TIFF *tif, uint32_t tag, va_list ap);
     357             : static int OJPEGVSetField(TIFF *tif, uint32_t tag, va_list ap);
     358             : static void OJPEGPrintDir(TIFF *tif, FILE *fd, long flags);
     359             : 
     360             : static int OJPEGFixupTags(TIFF *tif);
     361             : static int OJPEGSetupDecode(TIFF *tif);
     362             : static int OJPEGPreDecode(TIFF *tif, uint16_t s);
     363             : static int OJPEGPreDecodeSkipRaw(TIFF *tif);
     364             : static int OJPEGPreDecodeSkipScanlines(TIFF *tif);
     365             : static int OJPEGDecode(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s);
     366             : static int OJPEGDecodeRaw(TIFF *tif, uint8_t *buf, tmsize_t cc);
     367             : static int OJPEGDecodeScanlines(TIFF *tif, uint8_t *buf, tmsize_t cc);
     368             : static void OJPEGPostDecode(TIFF *tif, uint8_t *buf, tmsize_t cc);
     369             : static int OJPEGSetupEncode(TIFF *tif);
     370             : static int OJPEGPreEncode(TIFF *tif, uint16_t s);
     371             : static int OJPEGEncode(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s);
     372             : static int OJPEGPostEncode(TIFF *tif);
     373             : static void OJPEGCleanup(TIFF *tif);
     374             : 
     375             : static void OJPEGSubsamplingCorrect(TIFF *tif);
     376             : static int OJPEGReadHeaderInfo(TIFF *tif);
     377             : static int OJPEGReadSecondarySos(TIFF *tif, uint16_t s);
     378             : static int OJPEGWriteHeaderInfo(TIFF *tif);
     379             : static void OJPEGLibjpegSessionAbort(TIFF *tif);
     380             : 
     381             : static int OJPEGReadHeaderInfoSec(TIFF *tif);
     382             : static int OJPEGReadHeaderInfoSecStreamDri(TIFF *tif);
     383             : static int OJPEGReadHeaderInfoSecStreamDqt(TIFF *tif);
     384             : static int OJPEGReadHeaderInfoSecStreamDht(TIFF *tif);
     385             : static int OJPEGReadHeaderInfoSecStreamSof(TIFF *tif, uint8_t marker_id);
     386             : static int OJPEGReadHeaderInfoSecStreamSos(TIFF *tif);
     387             : static int OJPEGReadHeaderInfoSecTablesQTable(TIFF *tif);
     388             : static int OJPEGReadHeaderInfoSecTablesDcTable(TIFF *tif);
     389             : static int OJPEGReadHeaderInfoSecTablesAcTable(TIFF *tif);
     390             : 
     391             : static int OJPEGReadBufferFill(OJPEGState *sp);
     392             : static int OJPEGReadByte(OJPEGState *sp, uint8_t *byte);
     393             : static int OJPEGReadBytePeek(OJPEGState *sp, uint8_t *byte);
     394             : static void OJPEGReadByteAdvance(OJPEGState *sp);
     395             : static int OJPEGReadWord(OJPEGState *sp, uint16_t *word);
     396             : static int OJPEGReadBlock(OJPEGState *sp, uint16_t len, void *mem);
     397             : static void OJPEGReadSkip(OJPEGState *sp, uint16_t len);
     398             : 
     399             : static int OJPEGWriteStream(TIFF *tif, void **mem, uint32_t *len);
     400             : static void OJPEGWriteStreamSoi(TIFF *tif, void **mem, uint32_t *len);
     401             : static void OJPEGWriteStreamQTable(TIFF *tif, uint8_t table_index, void **mem,
     402             :                                    uint32_t *len);
     403             : static void OJPEGWriteStreamDcTable(TIFF *tif, uint8_t table_index, void **mem,
     404             :                                     uint32_t *len);
     405             : static void OJPEGWriteStreamAcTable(TIFF *tif, uint8_t table_index, void **mem,
     406             :                                     uint32_t *len);
     407             : static void OJPEGWriteStreamDri(TIFF *tif, void **mem, uint32_t *len);
     408             : static void OJPEGWriteStreamSof(TIFF *tif, void **mem, uint32_t *len);
     409             : static void OJPEGWriteStreamSos(TIFF *tif, void **mem, uint32_t *len);
     410             : static int OJPEGWriteStreamCompressed(TIFF *tif, void **mem, uint32_t *len);
     411             : static void OJPEGWriteStreamRst(TIFF *tif, void **mem, uint32_t *len);
     412             : static void OJPEGWriteStreamEoi(TIFF *tif, void **mem, uint32_t *len);
     413             : 
     414             : #ifdef LIBJPEG_ENCAP_EXTERNAL
     415             : extern int jpeg_create_decompress_encap(OJPEGState *sp,
     416             :                                         jpeg_decompress_struct *cinfo);
     417             : extern int jpeg_read_header_encap(OJPEGState *sp, jpeg_decompress_struct *cinfo,
     418             :                                   uint8_t require_image);
     419             : extern int jpeg_start_decompress_encap(OJPEGState *sp,
     420             :                                        jpeg_decompress_struct *cinfo);
     421             : extern int jpeg_read_scanlines_encap(OJPEGState *sp,
     422             :                                      jpeg_decompress_struct *cinfo,
     423             :                                      void *scanlines, uint32_t max_lines);
     424             : extern int jpeg_read_raw_data_encap(OJPEGState *sp,
     425             :                                     jpeg_decompress_struct *cinfo, void *data,
     426             :                                     uint32_t max_lines);
     427             : extern void jpeg_encap_unwind(TIFF *tif);
     428             : #else
     429             : static int jpeg_create_decompress_encap(OJPEGState *sp,
     430             :                                         jpeg_decompress_struct *j);
     431             : static int jpeg_read_header_encap(OJPEGState *sp, jpeg_decompress_struct *cinfo,
     432             :                                   uint8_t require_image);
     433             : static int jpeg_start_decompress_encap(OJPEGState *sp,
     434             :                                        jpeg_decompress_struct *cinfo);
     435             : static int jpeg_read_scanlines_encap(OJPEGState *sp,
     436             :                                      jpeg_decompress_struct *cinfo,
     437             :                                      void *scanlines, uint32_t max_lines);
     438             : static int jpeg_read_raw_data_encap(OJPEGState *sp,
     439             :                                     jpeg_decompress_struct *cinfo, void *data,
     440             :                                     uint32_t max_lines);
     441             : static void jpeg_encap_unwind(TIFF *tif);
     442             : #endif
     443             : 
     444             : static void OJPEGLibjpegJpegErrorMgrOutputMessage(jpeg_common_struct *cinfo);
     445             : static void OJPEGLibjpegJpegErrorMgrErrorExit(jpeg_common_struct *cinfo);
     446             : static void OJPEGLibjpegJpegSourceMgrInitSource(jpeg_decompress_struct *cinfo);
     447             : static boolean
     448             : OJPEGLibjpegJpegSourceMgrFillInputBuffer(jpeg_decompress_struct *cinfo);
     449             : static void
     450             : OJPEGLibjpegJpegSourceMgrSkipInputData(jpeg_decompress_struct *cinfo,
     451             :                                        long num_bytes);
     452             : static boolean
     453             : OJPEGLibjpegJpegSourceMgrResyncToRestart(jpeg_decompress_struct *cinfo,
     454             :                                          int desired);
     455             : static void OJPEGLibjpegJpegSourceMgrTermSource(jpeg_decompress_struct *cinfo);
     456             : 
     457           2 : int TIFFInitOJPEG(TIFF *tif, int scheme)
     458             : {
     459             :     static const char module[] = "TIFFInitOJPEG";
     460             :     OJPEGState *sp;
     461             : 
     462             :     (void)scheme;
     463           2 :     assert(scheme == COMPRESSION_OJPEG);
     464             : 
     465             :     /*
     466             :      * Merge codec-specific tag information.
     467             :      */
     468           2 :     if (!_TIFFMergeFields(tif, ojpegFields, TIFFArrayCount(ojpegFields)))
     469             :     {
     470           0 :         TIFFErrorExtR(tif, module,
     471             :                       "Merging Old JPEG codec-specific tags failed");
     472           0 :         return 0;
     473             :     }
     474             : 
     475             :     /* state block */
     476           2 :     sp = _TIFFmallocExt(tif, sizeof(OJPEGState));
     477           2 :     if (sp == NULL)
     478             :     {
     479           0 :         TIFFErrorExtR(tif, module, "No space for OJPEG state block");
     480           0 :         return (0);
     481             :     }
     482           2 :     _TIFFmemset(sp, 0, sizeof(OJPEGState));
     483           2 :     sp->tif = tif;
     484           2 :     sp->jpeg_proc = 1;
     485           2 :     sp->subsampling_hor = 2;
     486           2 :     sp->subsampling_ver = 2;
     487           2 :     TIFFSetField(tif, TIFFTAG_YCBCRSUBSAMPLING, 2, 2);
     488             :     /* tif codec methods */
     489           2 :     tif->tif_fixuptags = OJPEGFixupTags;
     490           2 :     tif->tif_setupdecode = OJPEGSetupDecode;
     491           2 :     tif->tif_predecode = OJPEGPreDecode;
     492           2 :     tif->tif_postdecode = OJPEGPostDecode;
     493           2 :     tif->tif_decoderow = OJPEGDecode;
     494           2 :     tif->tif_decodestrip = OJPEGDecode;
     495           2 :     tif->tif_decodetile = OJPEGDecode;
     496           2 :     tif->tif_setupencode = OJPEGSetupEncode;
     497           2 :     tif->tif_preencode = OJPEGPreEncode;
     498           2 :     tif->tif_postencode = OJPEGPostEncode;
     499           2 :     tif->tif_encoderow = OJPEGEncode;
     500           2 :     tif->tif_encodestrip = OJPEGEncode;
     501           2 :     tif->tif_encodetile = OJPEGEncode;
     502           2 :     tif->tif_cleanup = OJPEGCleanup;
     503           2 :     tif->tif_data = (uint8_t *)sp;
     504             :     /* tif tag methods */
     505           2 :     sp->vgetparent = tif->tif_tagmethods.vgetfield;
     506           2 :     tif->tif_tagmethods.vgetfield = OJPEGVGetField;
     507           2 :     sp->vsetparent = tif->tif_tagmethods.vsetfield;
     508           2 :     tif->tif_tagmethods.vsetfield = OJPEGVSetField;
     509           2 :     sp->printdir = tif->tif_tagmethods.printdir;
     510           2 :     tif->tif_tagmethods.printdir = OJPEGPrintDir;
     511             :     /* Some OJPEG files don't have strip or tile offsets or bytecounts tags.
     512             :        Some others do, but have totally meaningless or corrupt values
     513             :        in these tags. In these cases, the JpegInterchangeFormat stream is
     514             :        reliable. In any case, this decoder reads the compressed data itself,
     515             :        from the most reliable locations, and we need to notify encapsulating
     516             :        LibTiff not to read raw strips or tiles for us. */
     517           2 :     tif->tif_flags |= TIFF_NOREADRAW;
     518           2 :     return (1);
     519             : }
     520             : 
     521          94 : static int OJPEGVGetField(TIFF *tif, uint32_t tag, va_list ap)
     522             : {
     523          94 :     OJPEGState *sp = (OJPEGState *)tif->tif_data;
     524          94 :     switch (tag)
     525             :     {
     526           0 :         case TIFFTAG_JPEGIFOFFSET:
     527           0 :             *va_arg(ap, uint64_t *) = (uint64_t)sp->jpeg_interchange_format;
     528           0 :             break;
     529           0 :         case TIFFTAG_JPEGIFBYTECOUNT:
     530           0 :             *va_arg(ap, uint64_t *) =
     531           0 :                 (uint64_t)sp->jpeg_interchange_format_length;
     532           0 :             break;
     533           8 :         case TIFFTAG_YCBCRSUBSAMPLING:
     534           8 :             if (sp->subsamplingcorrect_done == 0)
     535           2 :                 OJPEGSubsamplingCorrect(tif);
     536           8 :             *va_arg(ap, uint16_t *) = (uint16_t)sp->subsampling_hor;
     537           8 :             *va_arg(ap, uint16_t *) = (uint16_t)sp->subsampling_ver;
     538           8 :             break;
     539           0 :         case TIFFTAG_JPEGQTABLES:
     540           0 :             *va_arg(ap, uint32_t *) = (uint32_t)sp->qtable_offset_count;
     541           0 :             *va_arg(ap, const void **) = (const void *)sp->qtable_offset;
     542           0 :             break;
     543           0 :         case TIFFTAG_JPEGDCTABLES:
     544           0 :             *va_arg(ap, uint32_t *) = (uint32_t)sp->dctable_offset_count;
     545           0 :             *va_arg(ap, const void **) = (const void *)sp->dctable_offset;
     546           0 :             break;
     547           0 :         case TIFFTAG_JPEGACTABLES:
     548           0 :             *va_arg(ap, uint32_t *) = (uint32_t)sp->actable_offset_count;
     549           0 :             *va_arg(ap, const void **) = (const void *)sp->actable_offset;
     550           0 :             break;
     551           0 :         case TIFFTAG_JPEGPROC:
     552           0 :             *va_arg(ap, uint16_t *) = (uint16_t)sp->jpeg_proc;
     553           0 :             break;
     554           0 :         case TIFFTAG_JPEGRESTARTINTERVAL:
     555           0 :             *va_arg(ap, uint16_t *) = sp->restart_interval;
     556           0 :             break;
     557          86 :         default:
     558          86 :             return (*sp->vgetparent)(tif, tag, ap);
     559             :     }
     560           8 :     return (1);
     561             : }
     562             : 
     563          34 : static int OJPEGVSetField(TIFF *tif, uint32_t tag, va_list ap)
     564             : {
     565             :     static const char module[] = "OJPEGVSetField";
     566          34 :     OJPEGState *sp = (OJPEGState *)tif->tif_data;
     567             :     uint32_t ma;
     568             :     uint64_t *mb;
     569             :     uint32_t n;
     570             :     const TIFFField *fip;
     571             : 
     572          34 :     switch (tag)
     573             :     {
     574           0 :         case TIFFTAG_JPEGIFOFFSET:
     575           0 :             sp->jpeg_interchange_format = (uint64_t)va_arg(ap, uint64_t);
     576           0 :             break;
     577           0 :         case TIFFTAG_JPEGIFBYTECOUNT:
     578           0 :             sp->jpeg_interchange_format_length = (uint64_t)va_arg(ap, uint64_t);
     579           0 :             break;
     580           2 :         case TIFFTAG_YCBCRSUBSAMPLING:
     581           2 :             sp->subsampling_tag = 1;
     582           2 :             sp->subsampling_hor = (uint8_t)va_arg(ap, uint16_vap);
     583           2 :             sp->subsampling_ver = (uint8_t)va_arg(ap, uint16_vap);
     584           2 :             tif->tif_dir.td_ycbcrsubsampling[0] = sp->subsampling_hor;
     585           2 :             tif->tif_dir.td_ycbcrsubsampling[1] = sp->subsampling_ver;
     586           2 :             break;
     587           2 :         case TIFFTAG_JPEGQTABLES:
     588           2 :             ma = (uint32_t)va_arg(ap, uint32_t);
     589           2 :             if (ma != 0)
     590             :             {
     591           2 :                 if (ma > 3)
     592             :                 {
     593           0 :                     TIFFErrorExtR(tif, module,
     594             :                                   "JpegQTables tag has incorrect count");
     595           0 :                     return (0);
     596             :                 }
     597           2 :                 sp->qtable_offset_count = (uint8_t)ma;
     598           2 :                 mb = (uint64_t *)va_arg(ap, uint64_t *);
     599           8 :                 for (n = 0; n < ma; n++)
     600           6 :                     sp->qtable_offset[n] = mb[n];
     601             :             }
     602           2 :             break;
     603           2 :         case TIFFTAG_JPEGDCTABLES:
     604           2 :             ma = (uint32_t)va_arg(ap, uint32_t);
     605           2 :             if (ma != 0)
     606             :             {
     607           2 :                 if (ma > 3)
     608             :                 {
     609           0 :                     TIFFErrorExtR(tif, module,
     610             :                                   "JpegDcTables tag has incorrect count");
     611           0 :                     return (0);
     612             :                 }
     613           2 :                 sp->dctable_offset_count = (uint8_t)ma;
     614           2 :                 mb = (uint64_t *)va_arg(ap, uint64_t *);
     615           8 :                 for (n = 0; n < ma; n++)
     616           6 :                     sp->dctable_offset[n] = mb[n];
     617             :             }
     618           2 :             break;
     619           2 :         case TIFFTAG_JPEGACTABLES:
     620           2 :             ma = (uint32_t)va_arg(ap, uint32_t);
     621           2 :             if (ma != 0)
     622             :             {
     623           2 :                 if (ma > 3)
     624             :                 {
     625           0 :                     TIFFErrorExtR(tif, module,
     626             :                                   "JpegAcTables tag has incorrect count");
     627           0 :                     return (0);
     628             :                 }
     629           2 :                 sp->actable_offset_count = (uint8_t)ma;
     630           2 :                 mb = (uint64_t *)va_arg(ap, uint64_t *);
     631           8 :                 for (n = 0; n < ma; n++)
     632           6 :                     sp->actable_offset[n] = mb[n];
     633             :             }
     634           2 :             break;
     635           2 :         case TIFFTAG_JPEGPROC:
     636           2 :             sp->jpeg_proc = (uint8_t)va_arg(ap, uint16_vap);
     637           2 :             break;
     638           0 :         case TIFFTAG_JPEGRESTARTINTERVAL:
     639           0 :             sp->restart_interval = (uint16_t)va_arg(ap, uint16_vap);
     640           0 :             break;
     641          24 :         default:
     642          24 :             return (*sp->vsetparent)(tif, tag, ap);
     643             :     }
     644          10 :     fip = TIFFFieldWithTag(tif, tag);
     645          10 :     if (fip == NULL) /* shouldn't happen */
     646           0 :         return (0);
     647          10 :     TIFFSetFieldBit(tif, fip->field_bit);
     648          10 :     tif->tif_flags |= TIFF_DIRTYDIRECT;
     649          10 :     return (1);
     650             : }
     651             : 
     652           0 : static void OJPEGPrintDir(TIFF *tif, FILE *fd, long flags)
     653             : {
     654           0 :     OJPEGState *sp = (OJPEGState *)tif->tif_data;
     655             :     uint8_t m;
     656             :     (void)flags;
     657           0 :     assert(sp != NULL);
     658           0 :     if (TIFFFieldSet(tif, FIELD_OJPEG_JPEGINTERCHANGEFORMAT))
     659           0 :         fprintf(fd, "  JpegInterchangeFormat: %" PRIu64 "\n",
     660           0 :                 (uint64_t)sp->jpeg_interchange_format);
     661           0 :     if (TIFFFieldSet(tif, FIELD_OJPEG_JPEGINTERCHANGEFORMATLENGTH))
     662           0 :         fprintf(fd, "  JpegInterchangeFormatLength: %" PRIu64 "\n",
     663           0 :                 (uint64_t)sp->jpeg_interchange_format_length);
     664           0 :     if (TIFFFieldSet(tif, FIELD_OJPEG_JPEGQTABLES))
     665             :     {
     666           0 :         fprintf(fd, "  JpegQTables:");
     667           0 :         for (m = 0; m < sp->qtable_offset_count; m++)
     668           0 :             fprintf(fd, " %" PRIu64, (uint64_t)sp->qtable_offset[m]);
     669           0 :         fprintf(fd, "\n");
     670             :     }
     671           0 :     if (TIFFFieldSet(tif, FIELD_OJPEG_JPEGDCTABLES))
     672             :     {
     673           0 :         fprintf(fd, "  JpegDcTables:");
     674           0 :         for (m = 0; m < sp->dctable_offset_count; m++)
     675           0 :             fprintf(fd, " %" PRIu64, (uint64_t)sp->dctable_offset[m]);
     676           0 :         fprintf(fd, "\n");
     677             :     }
     678           0 :     if (TIFFFieldSet(tif, FIELD_OJPEG_JPEGACTABLES))
     679             :     {
     680           0 :         fprintf(fd, "  JpegAcTables:");
     681           0 :         for (m = 0; m < sp->actable_offset_count; m++)
     682           0 :             fprintf(fd, " %" PRIu64, (uint64_t)sp->actable_offset[m]);
     683           0 :         fprintf(fd, "\n");
     684             :     }
     685           0 :     if (TIFFFieldSet(tif, FIELD_OJPEG_JPEGPROC))
     686           0 :         fprintf(fd, "  JpegProc: %" PRIu8 "\n", sp->jpeg_proc);
     687           0 :     if (TIFFFieldSet(tif, FIELD_OJPEG_JPEGRESTARTINTERVAL))
     688           0 :         fprintf(fd, "  JpegRestartInterval: %" PRIu16 "\n",
     689           0 :                 sp->restart_interval);
     690           0 :     if (sp->printdir)
     691           0 :         (*sp->printdir)(tif, fd, flags);
     692           0 : }
     693             : 
     694           2 : static int OJPEGFixupTags(TIFF *tif)
     695             : {
     696             :     (void)tif;
     697           2 :     return (1);
     698             : }
     699             : 
     700           2 : static int OJPEGSetupDecode(TIFF *tif)
     701             : {
     702             :     static const char module[] = "OJPEGSetupDecode";
     703           2 :     TIFFWarningExtR(tif, module,
     704             :                     "Deprecated and troublesome old-style JPEG compression "
     705             :                     "mode, please convert to new-style JPEG compression and "
     706             :                     "notify vendor of writing software");
     707           2 :     return (1);
     708             : }
     709             : 
     710           2 : static int OJPEGPreDecode(TIFF *tif, uint16_t s)
     711             : {
     712           2 :     OJPEGState *sp = (OJPEGState *)tif->tif_data;
     713             :     uint32_t m;
     714           2 :     if (sp->subsamplingcorrect_done == 0)
     715           0 :         OJPEGSubsamplingCorrect(tif);
     716           2 :     if (sp->readheader_done == 0)
     717             :     {
     718           2 :         if (OJPEGReadHeaderInfo(tif) == 0)
     719           1 :             return (0);
     720             :     }
     721           1 :     if (sp->sos_end[s].log == 0)
     722             :     {
     723           0 :         if (OJPEGReadSecondarySos(tif, s) == 0)
     724           0 :             return (0);
     725             :     }
     726           1 :     if (isTiled(tif))
     727           1 :         m = tif->tif_curtile;
     728             :     else
     729           0 :         m = tif->tif_curstrip;
     730           1 :     if ((sp->writeheader_done != 0) &&
     731           0 :         ((sp->write_cursample != s) || (sp->write_curstrile > m)))
     732             :     {
     733           0 :         if (sp->libjpeg_session_active != 0)
     734           0 :             OJPEGLibjpegSessionAbort(tif);
     735           0 :         sp->writeheader_done = 0;
     736             :     }
     737           1 :     if (sp->writeheader_done == 0)
     738             :     {
     739           1 :         sp->plane_sample_offset = (uint8_t)s;
     740           1 :         sp->write_cursample = s;
     741           1 :         sp->write_curstrile = s * tif->tif_dir.td_stripsperimage;
     742           1 :         if ((sp->in_buffer_file_pos_log == 0) ||
     743           0 :             (sp->in_buffer_file_pos - sp->in_buffer_togo !=
     744           0 :              sp->sos_end[s].in_buffer_file_pos))
     745             :         {
     746           1 :             sp->in_buffer_source = sp->sos_end[s].in_buffer_source;
     747           1 :             sp->in_buffer_next_strile = sp->sos_end[s].in_buffer_next_strile;
     748           1 :             sp->in_buffer_file_pos = sp->sos_end[s].in_buffer_file_pos;
     749           1 :             sp->in_buffer_file_pos_log = 0;
     750           1 :             sp->in_buffer_file_togo = sp->sos_end[s].in_buffer_file_togo;
     751           1 :             sp->in_buffer_togo = 0;
     752           1 :             sp->in_buffer_cur = 0;
     753             :         }
     754           1 :         if (OJPEGWriteHeaderInfo(tif) == 0)
     755           0 :             return (0);
     756             :     }
     757             : 
     758           1 :     sp->subsampling_convert_state = 0;
     759             : 
     760           1 :     while (sp->write_curstrile < m)
     761             :     {
     762           0 :         if (sp->libjpeg_jpeg_query_style == 0)
     763             :         {
     764           0 :             if (OJPEGPreDecodeSkipRaw(tif) == 0)
     765           0 :                 return (0);
     766             :         }
     767             :         else
     768             :         {
     769           0 :             if (OJPEGPreDecodeSkipScanlines(tif) == 0)
     770           0 :                 return (0);
     771             :         }
     772           0 :         sp->write_curstrile++;
     773             :     }
     774           1 :     sp->decoder_ok = 1;
     775           1 :     return (1);
     776             : }
     777             : 
     778           0 : static int OJPEGPreDecodeSkipRaw(TIFF *tif)
     779             : {
     780           0 :     OJPEGState *sp = (OJPEGState *)tif->tif_data;
     781             :     uint32_t m;
     782           0 :     m = sp->lines_per_strile;
     783           0 :     if (sp->subsampling_convert_state != 0)
     784             :     {
     785           0 :         if (sp->subsampling_convert_clines - sp->subsampling_convert_state >= m)
     786             :         {
     787           0 :             sp->subsampling_convert_state += m;
     788           0 :             if (sp->subsampling_convert_state == sp->subsampling_convert_clines)
     789           0 :                 sp->subsampling_convert_state = 0;
     790           0 :             return (1);
     791             :         }
     792           0 :         m -= sp->subsampling_convert_clines - sp->subsampling_convert_state;
     793           0 :         sp->subsampling_convert_state = 0;
     794           0 :         sp->error_in_raw_data_decoding = 0;
     795             :     }
     796           0 :     while (m >= sp->subsampling_convert_clines)
     797             :     {
     798           0 :         if (jpeg_read_raw_data_encap(sp, &(sp->libjpeg_jpeg_decompress_struct),
     799           0 :                                      sp->subsampling_convert_ycbcrimage,
     800           0 :                                      sp->subsampling_ver * 8) == 0)
     801           0 :             return (0);
     802           0 :         m -= sp->subsampling_convert_clines;
     803             :     }
     804           0 :     if (m > 0)
     805             :     {
     806           0 :         if (jpeg_read_raw_data_encap(sp, &(sp->libjpeg_jpeg_decompress_struct),
     807           0 :                                      sp->subsampling_convert_ycbcrimage,
     808           0 :                                      sp->subsampling_ver * 8) == 0)
     809           0 :             return (0);
     810           0 :         sp->subsampling_convert_state = m;
     811             :     }
     812           0 :     return (1);
     813             : }
     814             : 
     815           0 : static int OJPEGPreDecodeSkipScanlines(TIFF *tif)
     816             : {
     817             :     static const char module[] = "OJPEGPreDecodeSkipScanlines";
     818           0 :     OJPEGState *sp = (OJPEGState *)tif->tif_data;
     819             :     uint32_t m;
     820           0 :     if (sp->skip_buffer == NULL)
     821             :     {
     822           0 :         sp->skip_buffer = _TIFFmallocExt(tif, sp->bytes_per_line);
     823           0 :         if (sp->skip_buffer == NULL)
     824             :         {
     825           0 :             TIFFErrorExtR(tif, module, "Out of memory");
     826           0 :             return (0);
     827             :         }
     828             :     }
     829           0 :     for (m = 0; m < sp->lines_per_strile; m++)
     830             :     {
     831           0 :         if (jpeg_read_scanlines_encap(sp, &(sp->libjpeg_jpeg_decompress_struct),
     832           0 :                                       &sp->skip_buffer, 1) == 0)
     833           0 :             return (0);
     834             :     }
     835           0 :     return (1);
     836             : }
     837             : 
     838           1 : static int OJPEGDecode(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s)
     839             : {
     840             :     static const char module[] = "OJPEGDecode";
     841           1 :     OJPEGState *sp = (OJPEGState *)tif->tif_data;
     842             :     (void)s;
     843           1 :     if (!sp->decoder_ok)
     844             :     {
     845           0 :         memset(buf, 0, (size_t)cc);
     846           0 :         TIFFErrorExtR(tif, module,
     847             :                       "Cannot decode: decoder not correctly initialized");
     848           0 :         return 0;
     849             :     }
     850           1 :     if (sp->libjpeg_session_active == 0)
     851             :     {
     852           0 :         memset(buf, 0, (size_t)cc);
     853             :         /* This should normally not happen, except that it does when */
     854             :         /* using TIFFReadScanline() which calls OJPEGPostDecode() for */
     855             :         /* each scanline, which assumes that a whole strile was read */
     856             :         /* and may thus incorrectly consider it has read the whole image,
     857             :          * causing */
     858             :         /* OJPEGLibjpegSessionAbort() to be called prematurely. */
     859             :         /* Triggered by https://gitlab.com/libtiff/libtiff/-/issues/337 */
     860           0 :         TIFFErrorExtR(tif, module,
     861             :                       "Cannot decode: libjpeg_session_active == 0");
     862           0 :         return 0;
     863             :     }
     864           1 :     if (sp->error_in_raw_data_decoding)
     865             :     {
     866           0 :         memset(buf, 0, (size_t)cc);
     867           0 :         return 0;
     868             :     }
     869           1 :     if (sp->libjpeg_jpeg_query_style == 0)
     870             :     {
     871           1 :         if (OJPEGDecodeRaw(tif, buf, cc) == 0)
     872             :         {
     873           0 :             memset(buf, 0, (size_t)cc);
     874           0 :             return (0);
     875             :         }
     876             :     }
     877             :     else
     878             :     {
     879           0 :         if (OJPEGDecodeScanlines(tif, buf, cc) == 0)
     880             :         {
     881           0 :             memset(buf, 0, (size_t)cc);
     882           0 :             return (0);
     883             :         }
     884             :     }
     885           1 :     return (1);
     886             : }
     887             : 
     888           1 : static int OJPEGDecodeRaw(TIFF *tif, uint8_t *buf, tmsize_t cc)
     889             : {
     890             :     static const char module[] = "OJPEGDecodeRaw";
     891           1 :     OJPEGState *sp = (OJPEGState *)tif->tif_data;
     892             :     uint8_t *m;
     893             :     tmsize_t n;
     894             :     uint8_t *oy;
     895             :     uint8_t *ocb;
     896             :     uint8_t *ocr;
     897             :     uint8_t *p;
     898             :     uint32_t q;
     899             :     uint8_t *r;
     900             :     uint8_t sx, sy;
     901           1 :     if (cc % sp->bytes_per_line != 0)
     902             :     {
     903           0 :         TIFFErrorExtR(tif, module, "Fractional scanline not read");
     904           0 :         return (0);
     905             :     }
     906           1 :     assert(cc > 0);
     907           1 :     m = buf;
     908           1 :     n = cc;
     909             :     do
     910             :     {
     911         112 :         if (sp->subsampling_convert_state == 0)
     912             :         {
     913          14 :             if (jpeg_read_raw_data_encap(sp,
     914             :                                          &(sp->libjpeg_jpeg_decompress_struct),
     915          14 :                                          sp->subsampling_convert_ycbcrimage,
     916          14 :                                          sp->subsampling_ver * 8) == 0)
     917             :             {
     918           0 :                 sp->error_in_raw_data_decoding = 1;
     919           0 :                 return (0);
     920             :             }
     921             :         }
     922         112 :         oy = sp->subsampling_convert_ybuf +
     923         112 :              sp->subsampling_convert_state * sp->subsampling_ver *
     924         112 :                  sp->subsampling_convert_ylinelen;
     925         112 :         ocb = sp->subsampling_convert_cbbuf +
     926         112 :               sp->subsampling_convert_state * sp->subsampling_convert_clinelen;
     927         112 :         ocr = sp->subsampling_convert_crbuf +
     928         112 :               sp->subsampling_convert_state * sp->subsampling_convert_clinelen;
     929         112 :         p = m;
     930       13552 :         for (q = 0; q < sp->subsampling_convert_clinelenout; q++)
     931             :         {
     932       13440 :             r = oy;
     933       40320 :             for (sy = 0; sy < sp->subsampling_ver; sy++)
     934             :             {
     935       80640 :                 for (sx = 0; sx < sp->subsampling_hor; sx++)
     936       53760 :                     *p++ = *r++;
     937       26880 :                 r += sp->subsampling_convert_ylinelen - sp->subsampling_hor;
     938             :             }
     939       13440 :             oy += sp->subsampling_hor;
     940       13440 :             *p++ = *ocb++;
     941       13440 :             *p++ = *ocr++;
     942             :         }
     943         112 :         sp->subsampling_convert_state++;
     944         112 :         if (sp->subsampling_convert_state == sp->subsampling_convert_clines)
     945          14 :             sp->subsampling_convert_state = 0;
     946         112 :         m += sp->bytes_per_line;
     947         112 :         n -= sp->bytes_per_line;
     948         112 :     } while (n > 0);
     949           1 :     return (1);
     950             : }
     951             : 
     952           0 : static int OJPEGDecodeScanlines(TIFF *tif, uint8_t *buf, tmsize_t cc)
     953             : {
     954             :     static const char module[] = "OJPEGDecodeScanlines";
     955           0 :     OJPEGState *sp = (OJPEGState *)tif->tif_data;
     956             :     uint8_t *m;
     957             :     tmsize_t n;
     958           0 :     if (cc % sp->bytes_per_line != 0)
     959             :     {
     960           0 :         TIFFErrorExtR(tif, module, "Fractional scanline not read");
     961           0 :         return (0);
     962             :     }
     963           0 :     assert(cc > 0);
     964           0 :     m = buf;
     965           0 :     n = cc;
     966             :     do
     967             :     {
     968           0 :         if (jpeg_read_scanlines_encap(sp, &(sp->libjpeg_jpeg_decompress_struct),
     969             :                                       &m, 1) == 0)
     970           0 :             return (0);
     971           0 :         m += sp->bytes_per_line;
     972           0 :         n -= sp->bytes_per_line;
     973           0 :     } while (n > 0);
     974           0 :     return (1);
     975             : }
     976             : 
     977           0 : static void OJPEGPostDecode(TIFF *tif, uint8_t *buf, tmsize_t cc)
     978             : {
     979           0 :     OJPEGState *sp = (OJPEGState *)tif->tif_data;
     980             :     (void)buf;
     981             :     (void)cc;
     982             :     /* This function somehow incorrectly assumes that a whole strile was read,
     983             :      */
     984             :     /* which is not true when TIFFReadScanline() is called, */
     985             :     /* and may thus incorrectly consider it has read the whole image, causing */
     986             :     /* OJPEGLibjpegSessionAbort() to be called prematurely. */
     987             :     /* So this logic should be fixed to take into account cc, or disable */
     988             :     /* the scan line reading interface. */
     989             :     /* Triggered by https://gitlab.com/libtiff/libtiff/-/issues/337 */
     990           0 :     sp->write_curstrile++;
     991           0 :     if (sp->write_curstrile % tif->tif_dir.td_stripsperimage == 0)
     992             :     {
     993           0 :         assert(sp->libjpeg_session_active != 0);
     994           0 :         OJPEGLibjpegSessionAbort(tif);
     995           0 :         sp->writeheader_done = 0;
     996             :     }
     997           0 : }
     998             : 
     999           0 : static int OJPEGSetupEncode(TIFF *tif)
    1000             : {
    1001             :     static const char module[] = "OJPEGSetupEncode";
    1002           0 :     TIFFErrorExtR(
    1003             :         tif, module,
    1004             :         "OJPEG encoding not supported; use new-style JPEG compression instead");
    1005           0 :     return (0);
    1006             : }
    1007             : 
    1008           0 : static int OJPEGPreEncode(TIFF *tif, uint16_t s)
    1009             : {
    1010             :     static const char module[] = "OJPEGPreEncode";
    1011             :     (void)s;
    1012           0 :     TIFFErrorExtR(
    1013             :         tif, module,
    1014             :         "OJPEG encoding not supported; use new-style JPEG compression instead");
    1015           0 :     return (0);
    1016             : }
    1017             : 
    1018           0 : static int OJPEGEncode(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s)
    1019             : {
    1020             :     static const char module[] = "OJPEGEncode";
    1021             :     (void)buf;
    1022             :     (void)cc;
    1023             :     (void)s;
    1024           0 :     TIFFErrorExtR(
    1025             :         tif, module,
    1026             :         "OJPEG encoding not supported; use new-style JPEG compression instead");
    1027           0 :     return (0);
    1028             : }
    1029             : 
    1030           0 : static int OJPEGPostEncode(TIFF *tif)
    1031             : {
    1032             :     static const char module[] = "OJPEGPostEncode";
    1033           0 :     TIFFErrorExtR(
    1034             :         tif, module,
    1035             :         "OJPEG encoding not supported; use new-style JPEG compression instead");
    1036           0 :     return (0);
    1037             : }
    1038             : 
    1039           2 : static void OJPEGCleanup(TIFF *tif)
    1040             : {
    1041           2 :     OJPEGState *sp = (OJPEGState *)tif->tif_data;
    1042           2 :     if (sp != 0)
    1043             :     {
    1044           2 :         tif->tif_tagmethods.vgetfield = sp->vgetparent;
    1045           2 :         tif->tif_tagmethods.vsetfield = sp->vsetparent;
    1046           2 :         tif->tif_tagmethods.printdir = sp->printdir;
    1047           2 :         if (sp->qtable[0] != 0)
    1048           1 :             _TIFFfreeExt(tif, sp->qtable[0]);
    1049           2 :         if (sp->qtable[1] != 0)
    1050           1 :             _TIFFfreeExt(tif, sp->qtable[1]);
    1051           2 :         if (sp->qtable[2] != 0)
    1052           1 :             _TIFFfreeExt(tif, sp->qtable[2]);
    1053           2 :         if (sp->qtable[3] != 0)
    1054           0 :             _TIFFfreeExt(tif, sp->qtable[3]);
    1055           2 :         if (sp->dctable[0] != 0)
    1056           1 :             _TIFFfreeExt(tif, sp->dctable[0]);
    1057           2 :         if (sp->dctable[1] != 0)
    1058           1 :             _TIFFfreeExt(tif, sp->dctable[1]);
    1059           2 :         if (sp->dctable[2] != 0)
    1060           1 :             _TIFFfreeExt(tif, sp->dctable[2]);
    1061           2 :         if (sp->dctable[3] != 0)
    1062           0 :             _TIFFfreeExt(tif, sp->dctable[3]);
    1063           2 :         if (sp->actable[0] != 0)
    1064           1 :             _TIFFfreeExt(tif, sp->actable[0]);
    1065           2 :         if (sp->actable[1] != 0)
    1066           1 :             _TIFFfreeExt(tif, sp->actable[1]);
    1067           2 :         if (sp->actable[2] != 0)
    1068           1 :             _TIFFfreeExt(tif, sp->actable[2]);
    1069           2 :         if (sp->actable[3] != 0)
    1070           0 :             _TIFFfreeExt(tif, sp->actable[3]);
    1071           2 :         if (sp->libjpeg_session_active != 0)
    1072           1 :             OJPEGLibjpegSessionAbort(tif);
    1073           2 :         if (sp->subsampling_convert_ycbcrbuf != 0)
    1074           1 :             _TIFFfreeExt(tif, sp->subsampling_convert_ycbcrbuf);
    1075           2 :         if (sp->subsampling_convert_ycbcrimage != 0)
    1076           1 :             _TIFFfreeExt(tif, sp->subsampling_convert_ycbcrimage);
    1077           2 :         if (sp->skip_buffer != 0)
    1078           0 :             _TIFFfreeExt(tif, sp->skip_buffer);
    1079           2 :         _TIFFfreeExt(tif, sp);
    1080           2 :         tif->tif_data = NULL;
    1081           2 :         _TIFFSetDefaultCompressionState(tif);
    1082             :     }
    1083           2 : }
    1084             : 
    1085           2 : static void OJPEGSubsamplingCorrect(TIFF *tif)
    1086             : {
    1087             :     static const char module[] = "OJPEGSubsamplingCorrect";
    1088           2 :     OJPEGState *sp = (OJPEGState *)tif->tif_data;
    1089             :     uint8_t mh;
    1090             :     uint8_t mv;
    1091             : 
    1092           2 :     assert(sp->subsamplingcorrect_done == 0);
    1093           2 :     if ((tif->tif_dir.td_samplesperpixel != 3) ||
    1094           2 :         ((tif->tif_dir.td_photometric != PHOTOMETRIC_YCBCR) &&
    1095           0 :          (tif->tif_dir.td_photometric != PHOTOMETRIC_ITULAB)))
    1096             :     {
    1097           0 :         if (sp->subsampling_tag != 0)
    1098           0 :             TIFFWarningExtR(tif, module,
    1099             :                             "Subsampling tag not appropriate for this "
    1100             :                             "Photometric and/or SamplesPerPixel");
    1101           0 :         sp->subsampling_hor = 1;
    1102           0 :         sp->subsampling_ver = 1;
    1103           0 :         sp->subsampling_force_desubsampling_inside_decompression = 0;
    1104             :     }
    1105             :     else
    1106             :     {
    1107           2 :         sp->subsamplingcorrect_done = 1;
    1108           2 :         mh = sp->subsampling_hor;
    1109           2 :         mv = sp->subsampling_ver;
    1110           2 :         sp->subsamplingcorrect = 1;
    1111           2 :         OJPEGReadHeaderInfoSec(tif);
    1112           2 :         if (sp->subsampling_force_desubsampling_inside_decompression != 0)
    1113             :         {
    1114           0 :             sp->subsampling_hor = 1;
    1115           0 :             sp->subsampling_ver = 1;
    1116             :         }
    1117           2 :         sp->subsamplingcorrect = 0;
    1118           2 :         if (((sp->subsampling_hor != mh) || (sp->subsampling_ver != mv)) &&
    1119           0 :             (sp->subsampling_force_desubsampling_inside_decompression == 0))
    1120             :         {
    1121           0 :             if (sp->subsampling_tag == 0)
    1122           0 :                 TIFFWarningExtR(
    1123             :                     tif, module,
    1124             :                     "Subsampling tag is not set, yet subsampling inside JPEG "
    1125             :                     "data [%" PRIu8 ",%" PRIu8
    1126             :                     "] does not match default values [2,2]; assuming "
    1127             :                     "subsampling inside JPEG data is correct",
    1128           0 :                     sp->subsampling_hor, sp->subsampling_ver);
    1129             :             else
    1130           0 :                 TIFFWarningExtR(
    1131             :                     tif, module,
    1132             :                     "Subsampling inside JPEG data [%" PRIu8 ",%" PRIu8
    1133             :                     "] does not match subsampling tag values [%" PRIu8
    1134             :                     ",%" PRIu8
    1135             :                     "]; assuming subsampling inside JPEG data is correct",
    1136           0 :                     sp->subsampling_hor, sp->subsampling_ver, mh, mv);
    1137             :         }
    1138           2 :         if (sp->subsampling_force_desubsampling_inside_decompression != 0)
    1139             :         {
    1140           0 :             if (sp->subsampling_tag == 0)
    1141           0 :                 TIFFWarningExtR(
    1142             :                     tif, module,
    1143             :                     "Subsampling tag is not set, yet subsampling inside JPEG "
    1144             :                     "data does not match default values [2,2] (nor any other "
    1145             :                     "values allowed in TIFF); assuming subsampling inside JPEG "
    1146             :                     "data is correct and desubsampling inside JPEG "
    1147             :                     "decompression");
    1148             :             else
    1149           0 :                 TIFFWarningExtR(
    1150             :                     tif, module,
    1151             :                     "Subsampling inside JPEG data does not match subsampling "
    1152             :                     "tag values [%" PRIu8 ",%" PRIu8
    1153             :                     "] (nor any other values allowed in TIFF); assuming "
    1154             :                     "subsampling inside JPEG data is correct and desubsampling "
    1155             :                     "inside JPEG decompression",
    1156             :                     mh, mv);
    1157             :         }
    1158           2 :         if (sp->subsampling_force_desubsampling_inside_decompression == 0)
    1159             :         {
    1160           2 :             if (sp->subsampling_hor < sp->subsampling_ver)
    1161           0 :                 TIFFWarningExtR(tif, module,
    1162             :                                 "Subsampling values [%" PRIu8 ",%" PRIu8
    1163             :                                 "] are not allowed in TIFF",
    1164           0 :                                 sp->subsampling_hor, sp->subsampling_ver);
    1165             :         }
    1166             :     }
    1167           2 :     sp->subsamplingcorrect_done = 1;
    1168           2 : }
    1169             : 
    1170           2 : static int OJPEGReadHeaderInfo(TIFF *tif)
    1171             : {
    1172             :     static const char module[] = "OJPEGReadHeaderInfo";
    1173           2 :     OJPEGState *sp = (OJPEGState *)tif->tif_data;
    1174           2 :     assert(sp->readheader_done == 0);
    1175           2 :     sp->image_width = tif->tif_dir.td_imagewidth;
    1176           2 :     sp->image_length = tif->tif_dir.td_imagelength;
    1177           2 :     if (isTiled(tif))
    1178             :     {
    1179           2 :         sp->strile_width = tif->tif_dir.td_tilewidth;
    1180           2 :         sp->strile_length = tif->tif_dir.td_tilelength;
    1181           2 :         sp->strile_length_total =
    1182           2 :             ((sp->image_length + sp->strile_length - 1) / sp->strile_length) *
    1183           2 :             sp->strile_length;
    1184             :     }
    1185             :     else
    1186             :     {
    1187           0 :         sp->strile_width = sp->image_width;
    1188           0 :         sp->strile_length = tif->tif_dir.td_rowsperstrip;
    1189           0 :         if (sp->strile_length == (uint32_t)-1)
    1190           0 :             sp->strile_length = sp->image_length;
    1191           0 :         sp->strile_length_total = sp->image_length;
    1192             :     }
    1193           2 :     if (tif->tif_dir.td_samplesperpixel == 1)
    1194             :     {
    1195           0 :         sp->samples_per_pixel = 1;
    1196           0 :         sp->plane_sample_offset = 0;
    1197           0 :         sp->samples_per_pixel_per_plane = sp->samples_per_pixel;
    1198           0 :         sp->subsampling_hor = 1;
    1199           0 :         sp->subsampling_ver = 1;
    1200             :     }
    1201             :     else
    1202             :     {
    1203           2 :         if (tif->tif_dir.td_samplesperpixel != 3)
    1204             :         {
    1205           0 :             TIFFErrorExtR(tif, module,
    1206             :                           "SamplesPerPixel %" PRIu8
    1207             :                           " not supported for this compression scheme",
    1208           0 :                           sp->samples_per_pixel);
    1209           0 :             return (0);
    1210             :         }
    1211           2 :         sp->samples_per_pixel = 3;
    1212           2 :         sp->plane_sample_offset = 0;
    1213           2 :         if (tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG)
    1214           2 :             sp->samples_per_pixel_per_plane = 3;
    1215             :         else
    1216           0 :             sp->samples_per_pixel_per_plane = 1;
    1217             :     }
    1218           2 :     if (sp->strile_length < sp->image_length)
    1219             :     {
    1220           0 :         if (((sp->subsampling_hor != 1) && (sp->subsampling_hor != 2) &&
    1221           0 :              (sp->subsampling_hor != 4)) ||
    1222           0 :             ((sp->subsampling_ver != 1) && (sp->subsampling_ver != 2) &&
    1223           0 :              (sp->subsampling_ver != 4)))
    1224             :         {
    1225           0 :             TIFFErrorExtR(tif, module, "Invalid subsampling values");
    1226           0 :             return (0);
    1227             :         }
    1228           0 :         if (sp->strile_length % (sp->subsampling_ver * 8) != 0)
    1229             :         {
    1230           0 :             TIFFErrorExtR(tif, module,
    1231             :                           "Incompatible vertical subsampling and image "
    1232             :                           "strip/tile length");
    1233           0 :             return (0);
    1234             :         }
    1235           0 :         sp->restart_interval =
    1236           0 :             (uint16_t)(((sp->strile_width + sp->subsampling_hor * 8 - 1) /
    1237           0 :                         (sp->subsampling_hor * 8)) *
    1238           0 :                        (sp->strile_length / (sp->subsampling_ver * 8)));
    1239             :     }
    1240           2 :     if (OJPEGReadHeaderInfoSec(tif) == 0)
    1241           1 :         return (0);
    1242           1 :     sp->sos_end[0].log = 1;
    1243           1 :     sp->sos_end[0].in_buffer_source = sp->in_buffer_source;
    1244           1 :     sp->sos_end[0].in_buffer_next_strile = sp->in_buffer_next_strile;
    1245           1 :     sp->sos_end[0].in_buffer_file_pos =
    1246           1 :         sp->in_buffer_file_pos - sp->in_buffer_togo;
    1247           1 :     sp->sos_end[0].in_buffer_file_togo =
    1248           1 :         sp->in_buffer_file_togo + sp->in_buffer_togo;
    1249           1 :     sp->readheader_done = 1;
    1250           1 :     return (1);
    1251             : }
    1252             : 
    1253           0 : static int OJPEGReadSecondarySos(TIFF *tif, uint16_t s)
    1254             : {
    1255           0 :     OJPEGState *sp = (OJPEGState *)tif->tif_data;
    1256             :     uint8_t m;
    1257           0 :     assert(s > 0);
    1258           0 :     assert(s < 3);
    1259           0 :     assert(sp->sos_end[0].log != 0);
    1260           0 :     assert(sp->sos_end[s].log == 0);
    1261           0 :     sp->plane_sample_offset = (uint8_t)(s - 1);
    1262           0 :     while (sp->sos_end[sp->plane_sample_offset].log == 0)
    1263           0 :         sp->plane_sample_offset--;
    1264           0 :     sp->in_buffer_source =
    1265           0 :         sp->sos_end[sp->plane_sample_offset].in_buffer_source;
    1266           0 :     sp->in_buffer_next_strile =
    1267           0 :         sp->sos_end[sp->plane_sample_offset].in_buffer_next_strile;
    1268           0 :     sp->in_buffer_file_pos =
    1269           0 :         sp->sos_end[sp->plane_sample_offset].in_buffer_file_pos;
    1270           0 :     sp->in_buffer_file_pos_log = 0;
    1271           0 :     sp->in_buffer_file_togo =
    1272           0 :         sp->sos_end[sp->plane_sample_offset].in_buffer_file_togo;
    1273           0 :     sp->in_buffer_togo = 0;
    1274           0 :     sp->in_buffer_cur = 0;
    1275           0 :     while (sp->plane_sample_offset < s)
    1276             :     {
    1277             :         do
    1278             :         {
    1279           0 :             if (OJPEGReadByte(sp, &m) == 0)
    1280           0 :                 return (0);
    1281           0 :             if (m == 255)
    1282             :             {
    1283             :                 do
    1284             :                 {
    1285           0 :                     if (OJPEGReadByte(sp, &m) == 0)
    1286           0 :                         return (0);
    1287           0 :                     if (m != 255)
    1288           0 :                         break;
    1289             :                 } while (1);
    1290           0 :                 if (m == JPEG_MARKER_SOS)
    1291           0 :                     break;
    1292             :             }
    1293             :         } while (1);
    1294           0 :         sp->plane_sample_offset++;
    1295           0 :         if (OJPEGReadHeaderInfoSecStreamSos(tif) == 0)
    1296           0 :             return (0);
    1297           0 :         sp->sos_end[sp->plane_sample_offset].log = 1;
    1298           0 :         sp->sos_end[sp->plane_sample_offset].in_buffer_source =
    1299           0 :             sp->in_buffer_source;
    1300           0 :         sp->sos_end[sp->plane_sample_offset].in_buffer_next_strile =
    1301           0 :             sp->in_buffer_next_strile;
    1302           0 :         sp->sos_end[sp->plane_sample_offset].in_buffer_file_pos =
    1303           0 :             sp->in_buffer_file_pos - sp->in_buffer_togo;
    1304           0 :         sp->sos_end[sp->plane_sample_offset].in_buffer_file_togo =
    1305           0 :             sp->in_buffer_file_togo + sp->in_buffer_togo;
    1306             :     }
    1307           0 :     return (1);
    1308             : }
    1309             : 
    1310           1 : static int OJPEGWriteHeaderInfo(TIFF *tif)
    1311             : {
    1312             :     static const char module[] = "OJPEGWriteHeaderInfo";
    1313           1 :     OJPEGState *sp = (OJPEGState *)tif->tif_data;
    1314             :     uint8_t **m;
    1315             :     uint32_t n;
    1316             :     /* if a previous attempt failed, don't try again */
    1317           1 :     if (sp->libjpeg_session_active != 0)
    1318           0 :         return 0;
    1319           1 :     sp->out_state = ososSoi;
    1320           1 :     sp->restart_index = 0;
    1321           1 :     jpeg_std_error(&(sp->libjpeg_jpeg_error_mgr));
    1322           1 :     sp->libjpeg_jpeg_error_mgr.output_message =
    1323             :         OJPEGLibjpegJpegErrorMgrOutputMessage;
    1324           1 :     sp->libjpeg_jpeg_error_mgr.error_exit = OJPEGLibjpegJpegErrorMgrErrorExit;
    1325           1 :     sp->libjpeg_jpeg_decompress_struct.err = &(sp->libjpeg_jpeg_error_mgr);
    1326           1 :     sp->libjpeg_jpeg_decompress_struct.client_data = (void *)tif;
    1327           1 :     if (jpeg_create_decompress_encap(
    1328             :             sp, &(sp->libjpeg_jpeg_decompress_struct)) == 0)
    1329           0 :         return (0);
    1330           1 :     sp->libjpeg_session_active = 1;
    1331           1 :     sp->libjpeg_jpeg_source_mgr.bytes_in_buffer = 0;
    1332           1 :     sp->libjpeg_jpeg_source_mgr.init_source =
    1333             :         OJPEGLibjpegJpegSourceMgrInitSource;
    1334           1 :     sp->libjpeg_jpeg_source_mgr.fill_input_buffer =
    1335             :         OJPEGLibjpegJpegSourceMgrFillInputBuffer;
    1336           1 :     sp->libjpeg_jpeg_source_mgr.skip_input_data =
    1337             :         OJPEGLibjpegJpegSourceMgrSkipInputData;
    1338           1 :     sp->libjpeg_jpeg_source_mgr.resync_to_restart =
    1339             :         OJPEGLibjpegJpegSourceMgrResyncToRestart;
    1340           1 :     sp->libjpeg_jpeg_source_mgr.term_source =
    1341             :         OJPEGLibjpegJpegSourceMgrTermSource;
    1342           1 :     sp->libjpeg_jpeg_decompress_struct.src = &(sp->libjpeg_jpeg_source_mgr);
    1343           1 :     if (jpeg_read_header_encap(sp, &(sp->libjpeg_jpeg_decompress_struct), 1) ==
    1344             :         0)
    1345           0 :         return (0);
    1346           1 :     if ((sp->subsampling_force_desubsampling_inside_decompression == 0) &&
    1347           1 :         (sp->samples_per_pixel_per_plane > 1))
    1348             :     {
    1349           1 :         sp->libjpeg_jpeg_decompress_struct.raw_data_out = 1;
    1350             : #if JPEG_LIB_VERSION >= 70
    1351           1 :         sp->libjpeg_jpeg_decompress_struct.do_fancy_upsampling = FALSE;
    1352             : #endif
    1353           1 :         sp->libjpeg_jpeg_query_style = 0;
    1354           1 :         if (sp->subsampling_convert_log == 0)
    1355             :         {
    1356           1 :             assert(sp->subsampling_convert_ycbcrbuf == 0);
    1357           1 :             assert(sp->subsampling_convert_ycbcrimage == 0);
    1358             :             /* Check for division by zero. */
    1359           1 :             if (sp->subsampling_hor == 0 || sp->subsampling_ver == 0)
    1360           0 :                 return (0);
    1361           1 :             sp->subsampling_convert_ylinelen =
    1362           1 :                 ((sp->strile_width + sp->subsampling_hor * 8 - 1) /
    1363           1 :                  (sp->subsampling_hor * 8) * sp->subsampling_hor * 8);
    1364           1 :             sp->subsampling_convert_ylines = sp->subsampling_ver * 8;
    1365           1 :             sp->subsampling_convert_clinelen =
    1366           1 :                 sp->subsampling_convert_ylinelen / sp->subsampling_hor;
    1367           1 :             sp->subsampling_convert_clines = 8;
    1368           1 :             sp->subsampling_convert_ybuflen = sp->subsampling_convert_ylinelen *
    1369           1 :                                               sp->subsampling_convert_ylines;
    1370           1 :             sp->subsampling_convert_cbuflen = sp->subsampling_convert_clinelen *
    1371           1 :                                               sp->subsampling_convert_clines;
    1372           1 :             sp->subsampling_convert_ycbcrbuflen =
    1373           1 :                 sp->subsampling_convert_ybuflen +
    1374           1 :                 2 * sp->subsampling_convert_cbuflen;
    1375             :             /* The calloc is not normally necessary, except in some edge/broken
    1376             :              * cases */
    1377             :             /* for example for a tiled image of height 1 with a tile height of 1
    1378             :              * and subsampling_hor=subsampling_ver=2 */
    1379             :             /* In that case, libjpeg will only fill the 8 first lines of the 16
    1380             :              * lines */
    1381             :             /* See https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=16844
    1382             :              */
    1383             :             /* Even if this case is allowed (?), its handling is broken because
    1384             :              * OJPEGPreDecode() should also likely */
    1385             :             /* reset subsampling_convert_state to 0 when changing tile. */
    1386           1 :             sp->subsampling_convert_ycbcrbuf =
    1387           1 :                 _TIFFcallocExt(tif, 1, sp->subsampling_convert_ycbcrbuflen);
    1388           1 :             if (sp->subsampling_convert_ycbcrbuf == 0)
    1389             :             {
    1390           0 :                 TIFFErrorExtR(tif, module, "Out of memory");
    1391           0 :                 return (0);
    1392             :             }
    1393           1 :             sp->subsampling_convert_ybuf = sp->subsampling_convert_ycbcrbuf;
    1394           1 :             sp->subsampling_convert_cbbuf =
    1395           1 :                 sp->subsampling_convert_ybuf + sp->subsampling_convert_ybuflen;
    1396           1 :             sp->subsampling_convert_crbuf =
    1397           1 :                 sp->subsampling_convert_cbbuf + sp->subsampling_convert_cbuflen;
    1398           1 :             sp->subsampling_convert_ycbcrimagelen =
    1399           1 :                 3 + sp->subsampling_convert_ylines +
    1400           1 :                 2 * sp->subsampling_convert_clines;
    1401           2 :             sp->subsampling_convert_ycbcrimage = _TIFFmallocExt(
    1402           1 :                 tif, sp->subsampling_convert_ycbcrimagelen * sizeof(uint8_t *));
    1403           1 :             if (sp->subsampling_convert_ycbcrimage == 0)
    1404             :             {
    1405           0 :                 TIFFErrorExtR(tif, module, "Out of memory");
    1406           0 :                 return (0);
    1407             :             }
    1408           1 :             m = sp->subsampling_convert_ycbcrimage;
    1409           1 :             *m++ = (uint8_t *)(sp->subsampling_convert_ycbcrimage + 3);
    1410           1 :             *m++ = (uint8_t *)(sp->subsampling_convert_ycbcrimage + 3 +
    1411           1 :                                sp->subsampling_convert_ylines);
    1412           1 :             *m++ = (uint8_t *)(sp->subsampling_convert_ycbcrimage + 3 +
    1413           1 :                                sp->subsampling_convert_ylines +
    1414           1 :                                sp->subsampling_convert_clines);
    1415          17 :             for (n = 0; n < sp->subsampling_convert_ylines; n++)
    1416          16 :                 *m++ = sp->subsampling_convert_ybuf +
    1417          16 :                        n * sp->subsampling_convert_ylinelen;
    1418           9 :             for (n = 0; n < sp->subsampling_convert_clines; n++)
    1419           8 :                 *m++ = sp->subsampling_convert_cbbuf +
    1420           8 :                        n * sp->subsampling_convert_clinelen;
    1421           9 :             for (n = 0; n < sp->subsampling_convert_clines; n++)
    1422           8 :                 *m++ = sp->subsampling_convert_crbuf +
    1423           8 :                        n * sp->subsampling_convert_clinelen;
    1424           1 :             sp->subsampling_convert_clinelenout =
    1425           2 :                 sp->strile_width / sp->subsampling_hor +
    1426           1 :                 ((sp->strile_width % sp->subsampling_hor) != 0 ? 1 : 0);
    1427           1 :             sp->subsampling_convert_state = 0;
    1428           1 :             sp->error_in_raw_data_decoding = 0;
    1429           1 :             sp->bytes_per_line =
    1430           1 :                 sp->subsampling_convert_clinelenout *
    1431           1 :                 (sp->subsampling_ver * sp->subsampling_hor + 2);
    1432           1 :             sp->lines_per_strile =
    1433           2 :                 sp->strile_length / sp->subsampling_ver +
    1434           1 :                 ((sp->strile_length % sp->subsampling_ver) != 0 ? 1 : 0);
    1435           1 :             sp->subsampling_convert_log = 1;
    1436             :         }
    1437             :     }
    1438             :     else
    1439             :     {
    1440           0 :         sp->libjpeg_jpeg_decompress_struct.jpeg_color_space = JCS_UNKNOWN;
    1441           0 :         sp->libjpeg_jpeg_decompress_struct.out_color_space = JCS_UNKNOWN;
    1442           0 :         sp->libjpeg_jpeg_query_style = 1;
    1443           0 :         sp->bytes_per_line = sp->samples_per_pixel_per_plane * sp->strile_width;
    1444           0 :         sp->lines_per_strile = sp->strile_length;
    1445             :     }
    1446           1 :     if (jpeg_start_decompress_encap(sp,
    1447             :                                     &(sp->libjpeg_jpeg_decompress_struct)) == 0)
    1448           0 :         return (0);
    1449           1 :     if (sp->libjpeg_jpeg_decompress_struct.image_width != sp->strile_width)
    1450             :     {
    1451           0 :         TIFFErrorExtR(tif, module,
    1452             :                       "jpeg_start_decompress() returned image_width = %u, "
    1453             :                       "expected %" PRIu32,
    1454             :                       sp->libjpeg_jpeg_decompress_struct.image_width,
    1455             :                       sp->strile_width);
    1456           0 :         return 0;
    1457             :     }
    1458           1 :     if (sp->libjpeg_jpeg_decompress_struct.max_h_samp_factor !=
    1459           1 :             sp->subsampling_hor ||
    1460           1 :         sp->libjpeg_jpeg_decompress_struct.max_v_samp_factor !=
    1461           1 :             sp->subsampling_ver)
    1462             :     {
    1463           0 :         TIFFErrorExtR(tif, module,
    1464             :                       "jpeg_start_decompress() returned max_h_samp_factor = %d "
    1465             :                       "and max_v_samp_factor = %d, expected %" PRIu8
    1466             :                       " and %" PRIu8,
    1467             :                       sp->libjpeg_jpeg_decompress_struct.max_h_samp_factor,
    1468             :                       sp->libjpeg_jpeg_decompress_struct.max_v_samp_factor,
    1469           0 :                       sp->subsampling_hor, sp->subsampling_ver);
    1470           0 :         return 0;
    1471             :     }
    1472             : 
    1473           1 :     sp->writeheader_done = 1;
    1474           1 :     return (1);
    1475             : }
    1476             : 
    1477           1 : static void OJPEGLibjpegSessionAbort(TIFF *tif)
    1478             : {
    1479           1 :     OJPEGState *sp = (OJPEGState *)tif->tif_data;
    1480           1 :     assert(sp->libjpeg_session_active != 0);
    1481           1 :     jpeg_destroy((jpeg_common_struct *)(&(sp->libjpeg_jpeg_decompress_struct)));
    1482           1 :     sp->libjpeg_session_active = 0;
    1483           1 : }
    1484             : 
    1485           4 : static int OJPEGReadHeaderInfoSec(TIFF *tif)
    1486             : {
    1487             :     static const char module[] = "OJPEGReadHeaderInfoSec";
    1488           4 :     OJPEGState *sp = (OJPEGState *)tif->tif_data;
    1489             :     uint8_t m;
    1490             :     uint16_t n;
    1491             :     uint8_t o;
    1492           4 :     if (sp->file_size == 0)
    1493           2 :         sp->file_size = TIFFGetFileSize(tif);
    1494           4 :     if (sp->jpeg_interchange_format != 0)
    1495             :     {
    1496           0 :         if (sp->jpeg_interchange_format >= sp->file_size)
    1497             :         {
    1498           0 :             sp->jpeg_interchange_format = 0;
    1499           0 :             sp->jpeg_interchange_format_length = 0;
    1500             :         }
    1501             :         else
    1502             :         {
    1503           0 :             if ((sp->jpeg_interchange_format_length == 0) ||
    1504           0 :                 (sp->jpeg_interchange_format >
    1505           0 :                  UINT64_MAX - sp->jpeg_interchange_format_length) ||
    1506           0 :                 (sp->jpeg_interchange_format +
    1507           0 :                      sp->jpeg_interchange_format_length >
    1508           0 :                  sp->file_size))
    1509           0 :                 sp->jpeg_interchange_format_length =
    1510           0 :                     sp->file_size - sp->jpeg_interchange_format;
    1511             :         }
    1512             :     }
    1513           4 :     sp->in_buffer_source = osibsNotSetYet;
    1514           4 :     sp->in_buffer_next_strile = 0;
    1515           4 :     sp->in_buffer_strile_count = tif->tif_dir.td_nstrips;
    1516           4 :     sp->in_buffer_file_togo = 0;
    1517           4 :     sp->in_buffer_togo = 0;
    1518             :     do
    1519             :     {
    1520           4 :         if (OJPEGReadBytePeek(sp, &m) == 0)
    1521           2 :             return (0);
    1522           2 :         if (m != 255)
    1523           2 :             break;
    1524           0 :         OJPEGReadByteAdvance(sp);
    1525             :         do
    1526             :         {
    1527           0 :             if (OJPEGReadByte(sp, &m) == 0)
    1528           0 :                 return (0);
    1529           0 :         } while (m == 255);
    1530           0 :         switch (m)
    1531             :         {
    1532           0 :             case JPEG_MARKER_SOI:
    1533             :                 /* this type of marker has no data, and should be skipped */
    1534           0 :                 break;
    1535           0 :             case JPEG_MARKER_COM:
    1536             :             case JPEG_MARKER_APP0:
    1537             :             case JPEG_MARKER_APP0 + 1:
    1538             :             case JPEG_MARKER_APP0 + 2:
    1539             :             case JPEG_MARKER_APP0 + 3:
    1540             :             case JPEG_MARKER_APP0 + 4:
    1541             :             case JPEG_MARKER_APP0 + 5:
    1542             :             case JPEG_MARKER_APP0 + 6:
    1543             :             case JPEG_MARKER_APP0 + 7:
    1544             :             case JPEG_MARKER_APP0 + 8:
    1545             :             case JPEG_MARKER_APP0 + 9:
    1546             :             case JPEG_MARKER_APP0 + 10:
    1547             :             case JPEG_MARKER_APP0 + 11:
    1548             :             case JPEG_MARKER_APP0 + 12:
    1549             :             case JPEG_MARKER_APP0 + 13:
    1550             :             case JPEG_MARKER_APP0 + 14:
    1551             :             case JPEG_MARKER_APP0 + 15:
    1552             :                 /* this type of marker has data, but it has no use to us (and no
    1553             :                  * place here) and should be skipped */
    1554           0 :                 if (OJPEGReadWord(sp, &n) == 0)
    1555           0 :                     return (0);
    1556           0 :                 if (n < 2)
    1557             :                 {
    1558           0 :                     if (sp->subsamplingcorrect == 0)
    1559           0 :                         TIFFErrorExtR(tif, module, "Corrupt JPEG data");
    1560           0 :                     return (0);
    1561             :                 }
    1562           0 :                 if (n > 2)
    1563           0 :                     OJPEGReadSkip(sp, n - 2);
    1564           0 :                 break;
    1565           0 :             case JPEG_MARKER_DRI:
    1566           0 :                 if (OJPEGReadHeaderInfoSecStreamDri(tif) == 0)
    1567           0 :                     return (0);
    1568           0 :                 break;
    1569           0 :             case JPEG_MARKER_DQT:
    1570           0 :                 if (OJPEGReadHeaderInfoSecStreamDqt(tif) == 0)
    1571           0 :                     return (0);
    1572           0 :                 break;
    1573           0 :             case JPEG_MARKER_DHT:
    1574           0 :                 if (OJPEGReadHeaderInfoSecStreamDht(tif) == 0)
    1575           0 :                     return (0);
    1576           0 :                 break;
    1577           0 :             case JPEG_MARKER_SOF0:
    1578             :             case JPEG_MARKER_SOF1:
    1579             :             case JPEG_MARKER_SOF3:
    1580           0 :                 if (OJPEGReadHeaderInfoSecStreamSof(tif, m) == 0)
    1581           0 :                     return (0);
    1582           0 :                 if (sp->subsamplingcorrect != 0)
    1583           0 :                     return (1);
    1584           0 :                 break;
    1585           0 :             case JPEG_MARKER_SOS:
    1586           0 :                 if (sp->subsamplingcorrect != 0)
    1587           0 :                     return (1);
    1588           0 :                 assert(sp->plane_sample_offset == 0);
    1589           0 :                 if (OJPEGReadHeaderInfoSecStreamSos(tif) == 0)
    1590           0 :                     return (0);
    1591           0 :                 break;
    1592           0 :             default:
    1593           0 :                 TIFFErrorExtR(tif, module,
    1594             :                               "Unknown marker type %" PRIu8 " in JPEG data", m);
    1595           0 :                 return (0);
    1596             :         }
    1597           0 :     } while (m != JPEG_MARKER_SOS);
    1598           2 :     if (sp->subsamplingcorrect)
    1599           1 :         return (1);
    1600           1 :     if (sp->sof_log == 0)
    1601             :     {
    1602           1 :         if (OJPEGReadHeaderInfoSecTablesQTable(tif) == 0)
    1603           0 :             return (0);
    1604           1 :         sp->sof_marker_id = JPEG_MARKER_SOF0;
    1605           4 :         for (o = 0; o < sp->samples_per_pixel; o++)
    1606           3 :             sp->sof_c[o] = o;
    1607           1 :         sp->sof_hv[0] = ((sp->subsampling_hor << 4) | sp->subsampling_ver);
    1608           3 :         for (o = 1; o < sp->samples_per_pixel; o++)
    1609           2 :             sp->sof_hv[o] = 17;
    1610           1 :         sp->sof_x = sp->strile_width;
    1611           1 :         sp->sof_y = sp->strile_length_total;
    1612           1 :         sp->sof_log = 1;
    1613           1 :         if (OJPEGReadHeaderInfoSecTablesDcTable(tif) == 0)
    1614           0 :             return (0);
    1615           1 :         if (OJPEGReadHeaderInfoSecTablesAcTable(tif) == 0)
    1616           0 :             return (0);
    1617           3 :         for (o = 1; o < sp->samples_per_pixel; o++)
    1618           2 :             sp->sos_cs[o] = o;
    1619             :     }
    1620           1 :     return (1);
    1621             : }
    1622             : 
    1623           0 : static int OJPEGReadHeaderInfoSecStreamDri(TIFF *tif)
    1624             : {
    1625             :     /* This could easily cause trouble in some cases... but no such cases have
    1626             :        occurred so far */
    1627             :     static const char module[] = "OJPEGReadHeaderInfoSecStreamDri";
    1628           0 :     OJPEGState *sp = (OJPEGState *)tif->tif_data;
    1629             :     uint16_t m;
    1630           0 :     if (OJPEGReadWord(sp, &m) == 0)
    1631           0 :         return (0);
    1632           0 :     if (m != 4)
    1633             :     {
    1634           0 :         TIFFErrorExtR(tif, module, "Corrupt DRI marker in JPEG data");
    1635           0 :         return (0);
    1636             :     }
    1637           0 :     if (OJPEGReadWord(sp, &m) == 0)
    1638           0 :         return (0);
    1639           0 :     sp->restart_interval = m;
    1640           0 :     return (1);
    1641             : }
    1642             : 
    1643           0 : static int OJPEGReadHeaderInfoSecStreamDqt(TIFF *tif)
    1644             : {
    1645             :     /* this is a table marker, and it is to be saved as a whole for exact
    1646             :      * pushing on the jpeg stream later on */
    1647             :     static const char module[] = "OJPEGReadHeaderInfoSecStreamDqt";
    1648           0 :     OJPEGState *sp = (OJPEGState *)tif->tif_data;
    1649             :     uint16_t m;
    1650             :     uint32_t na;
    1651             :     uint8_t *nb;
    1652             :     uint8_t o;
    1653           0 :     if (OJPEGReadWord(sp, &m) == 0)
    1654           0 :         return (0);
    1655           0 :     if (m <= 2)
    1656             :     {
    1657           0 :         if (sp->subsamplingcorrect == 0)
    1658           0 :             TIFFErrorExtR(tif, module, "Corrupt DQT marker in JPEG data");
    1659           0 :         return (0);
    1660             :     }
    1661           0 :     if (sp->subsamplingcorrect != 0)
    1662           0 :         OJPEGReadSkip(sp, m - 2);
    1663             :     else
    1664             :     {
    1665           0 :         m -= 2;
    1666             :         do
    1667             :         {
    1668           0 :             if (m < 65)
    1669             :             {
    1670           0 :                 TIFFErrorExtR(tif, module, "Corrupt DQT marker in JPEG data");
    1671           0 :                 return (0);
    1672             :             }
    1673           0 :             na = sizeof(uint32_t) + 69;
    1674           0 :             nb = _TIFFmallocExt(tif, na);
    1675           0 :             if (nb == 0)
    1676             :             {
    1677           0 :                 TIFFErrorExtR(tif, module, "Out of memory");
    1678           0 :                 return (0);
    1679             :             }
    1680           0 :             *(uint32_t *)nb = na;
    1681           0 :             nb[sizeof(uint32_t)] = 255;
    1682           0 :             nb[sizeof(uint32_t) + 1] = JPEG_MARKER_DQT;
    1683           0 :             nb[sizeof(uint32_t) + 2] = 0;
    1684           0 :             nb[sizeof(uint32_t) + 3] = 67;
    1685           0 :             if (OJPEGReadBlock(sp, 65, &nb[sizeof(uint32_t) + 4]) == 0)
    1686             :             {
    1687           0 :                 _TIFFfreeExt(tif, nb);
    1688           0 :                 return (0);
    1689             :             }
    1690           0 :             o = nb[sizeof(uint32_t) + 4] & 15;
    1691           0 :             if (3 < o)
    1692             :             {
    1693           0 :                 TIFFErrorExtR(tif, module, "Corrupt DQT marker in JPEG data");
    1694           0 :                 _TIFFfreeExt(tif, nb);
    1695           0 :                 return (0);
    1696             :             }
    1697           0 :             if (sp->qtable[o] != 0)
    1698           0 :                 _TIFFfreeExt(tif, sp->qtable[o]);
    1699           0 :             sp->qtable[o] = nb;
    1700           0 :             m -= 65;
    1701           0 :         } while (m > 0);
    1702             :     }
    1703           0 :     return (1);
    1704             : }
    1705             : 
    1706           0 : static int OJPEGReadHeaderInfoSecStreamDht(TIFF *tif)
    1707             : {
    1708             :     /* this is a table marker, and it is to be saved as a whole for exact
    1709             :      * pushing on the jpeg stream later on */
    1710             :     /* TODO: the following assumes there is only one table in this marker... but
    1711             :      * i'm not quite sure that assumption is guaranteed correct */
    1712             :     static const char module[] = "OJPEGReadHeaderInfoSecStreamDht";
    1713           0 :     OJPEGState *sp = (OJPEGState *)tif->tif_data;
    1714             :     uint16_t m;
    1715             :     uint32_t na;
    1716             :     uint8_t *nb;
    1717             :     uint8_t o;
    1718           0 :     if (OJPEGReadWord(sp, &m) == 0)
    1719           0 :         return (0);
    1720           0 :     if (m <= 2)
    1721             :     {
    1722           0 :         if (sp->subsamplingcorrect == 0)
    1723           0 :             TIFFErrorExtR(tif, module, "Corrupt DHT marker in JPEG data");
    1724           0 :         return (0);
    1725             :     }
    1726           0 :     if (sp->subsamplingcorrect != 0)
    1727             :     {
    1728           0 :         OJPEGReadSkip(sp, m - 2);
    1729             :     }
    1730             :     else
    1731             :     {
    1732           0 :         na = sizeof(uint32_t) + 2 + m;
    1733           0 :         nb = _TIFFmallocExt(tif, na);
    1734           0 :         if (nb == 0)
    1735             :         {
    1736           0 :             TIFFErrorExtR(tif, module, "Out of memory");
    1737           0 :             return (0);
    1738             :         }
    1739           0 :         *(uint32_t *)nb = na;
    1740           0 :         nb[sizeof(uint32_t)] = 255;
    1741           0 :         nb[sizeof(uint32_t) + 1] = JPEG_MARKER_DHT;
    1742           0 :         nb[sizeof(uint32_t) + 2] = (m >> 8);
    1743           0 :         nb[sizeof(uint32_t) + 3] = (m & 255);
    1744           0 :         if (OJPEGReadBlock(sp, m - 2, &nb[sizeof(uint32_t) + 4]) == 0)
    1745             :         {
    1746           0 :             _TIFFfreeExt(tif, nb);
    1747           0 :             return (0);
    1748             :         }
    1749           0 :         o = nb[sizeof(uint32_t) + 4];
    1750           0 :         if ((o & 240) == 0)
    1751             :         {
    1752           0 :             if (3 < o)
    1753             :             {
    1754           0 :                 TIFFErrorExtR(tif, module, "Corrupt DHT marker in JPEG data");
    1755           0 :                 _TIFFfreeExt(tif, nb);
    1756           0 :                 return (0);
    1757             :             }
    1758           0 :             if (sp->dctable[o] != 0)
    1759           0 :                 _TIFFfreeExt(tif, sp->dctable[o]);
    1760           0 :             sp->dctable[o] = nb;
    1761             :         }
    1762             :         else
    1763             :         {
    1764           0 :             if ((o & 240) != 16)
    1765             :             {
    1766           0 :                 TIFFErrorExtR(tif, module, "Corrupt DHT marker in JPEG data");
    1767           0 :                 _TIFFfreeExt(tif, nb);
    1768           0 :                 return (0);
    1769             :             }
    1770           0 :             o &= 15;
    1771           0 :             if (3 < o)
    1772             :             {
    1773           0 :                 TIFFErrorExtR(tif, module, "Corrupt DHT marker in JPEG data");
    1774           0 :                 _TIFFfreeExt(tif, nb);
    1775           0 :                 return (0);
    1776             :             }
    1777           0 :             if (sp->actable[o] != 0)
    1778           0 :                 _TIFFfreeExt(tif, sp->actable[o]);
    1779           0 :             sp->actable[o] = nb;
    1780             :         }
    1781             :     }
    1782           0 :     return (1);
    1783             : }
    1784             : 
    1785           0 : static int OJPEGReadHeaderInfoSecStreamSof(TIFF *tif, uint8_t marker_id)
    1786             : {
    1787             :     /* this marker needs to be checked, and part of its data needs to be saved
    1788             :      * for regeneration later on */
    1789             :     static const char module[] = "OJPEGReadHeaderInfoSecStreamSof";
    1790           0 :     OJPEGState *sp = (OJPEGState *)tif->tif_data;
    1791             :     uint16_t m;
    1792             :     uint16_t n;
    1793             :     uint8_t o;
    1794             :     uint16_t p;
    1795             :     uint16_t q;
    1796           0 :     if (sp->sof_log != 0)
    1797             :     {
    1798           0 :         TIFFErrorExtR(tif, module, "Corrupt JPEG data");
    1799           0 :         return (0);
    1800             :     }
    1801           0 :     if (sp->subsamplingcorrect == 0)
    1802           0 :         sp->sof_marker_id = marker_id;
    1803             :     /* Lf: data length */
    1804           0 :     if (OJPEGReadWord(sp, &m) == 0)
    1805           0 :         return (0);
    1806           0 :     if (m < 11)
    1807             :     {
    1808           0 :         if (sp->subsamplingcorrect == 0)
    1809           0 :             TIFFErrorExtR(tif, module, "Corrupt SOF marker in JPEG data");
    1810           0 :         return (0);
    1811             :     }
    1812           0 :     m -= 8;
    1813           0 :     if (m % 3 != 0)
    1814             :     {
    1815           0 :         if (sp->subsamplingcorrect == 0)
    1816           0 :             TIFFErrorExtR(tif, module, "Corrupt SOF marker in JPEG data");
    1817           0 :         return (0);
    1818             :     }
    1819           0 :     n = m / 3;
    1820           0 :     if (sp->subsamplingcorrect == 0)
    1821             :     {
    1822           0 :         if (n != sp->samples_per_pixel)
    1823             :         {
    1824           0 :             TIFFErrorExtR(
    1825             :                 tif, module,
    1826             :                 "JPEG compressed data indicates unexpected number of samples");
    1827           0 :             return (0);
    1828             :         }
    1829             :     }
    1830             :     /* P: Sample precision */
    1831           0 :     if (OJPEGReadByte(sp, &o) == 0)
    1832           0 :         return (0);
    1833           0 :     if (o != 8)
    1834             :     {
    1835           0 :         if (sp->subsamplingcorrect == 0)
    1836           0 :             TIFFErrorExtR(tif, module,
    1837             :                           "JPEG compressed data indicates unexpected number of "
    1838             :                           "bits per sample");
    1839           0 :         return (0);
    1840             :     }
    1841             :     /* Y: Number of lines, X: Number of samples per line */
    1842           0 :     if (sp->subsamplingcorrect)
    1843           0 :         OJPEGReadSkip(sp, 4);
    1844             :     else
    1845             :     {
    1846             :         /* Y: Number of lines */
    1847           0 :         if (OJPEGReadWord(sp, &p) == 0)
    1848           0 :             return (0);
    1849           0 :         if (((uint32_t)p < sp->image_length) &&
    1850           0 :             ((uint32_t)p < sp->strile_length_total))
    1851             :         {
    1852           0 :             TIFFErrorExtR(tif, module,
    1853             :                           "JPEG compressed data indicates unexpected height");
    1854           0 :             return (0);
    1855             :         }
    1856           0 :         sp->sof_y = p;
    1857             :         /* X: Number of samples per line */
    1858           0 :         if (OJPEGReadWord(sp, &p) == 0)
    1859           0 :             return (0);
    1860           0 :         if (((uint32_t)p < sp->image_width) && ((uint32_t)p < sp->strile_width))
    1861             :         {
    1862           0 :             TIFFErrorExtR(tif, module,
    1863             :                           "JPEG compressed data indicates unexpected width");
    1864           0 :             return (0);
    1865             :         }
    1866           0 :         if ((uint32_t)p > sp->strile_width)
    1867             :         {
    1868           0 :             TIFFErrorExtR(tif, module,
    1869             :                           "JPEG compressed data image width exceeds expected "
    1870             :                           "image width");
    1871           0 :             return (0);
    1872             :         }
    1873           0 :         sp->sof_x = p;
    1874             :     }
    1875             :     /* Nf: Number of image components in frame */
    1876           0 :     if (OJPEGReadByte(sp, &o) == 0)
    1877           0 :         return (0);
    1878           0 :     if (o != n)
    1879             :     {
    1880           0 :         if (sp->subsamplingcorrect == 0)
    1881           0 :             TIFFErrorExtR(tif, module, "Corrupt SOF marker in JPEG data");
    1882           0 :         return (0);
    1883             :     }
    1884             :     /* per component stuff */
    1885             :     /* TODO: double-check that flow implies that n cannot be as big as to make
    1886             :      * us overflow sof_c, sof_hv and sof_tq arrays */
    1887           0 :     for (q = 0; q < n; q++)
    1888             :     {
    1889             :         /* C: Component identifier */
    1890           0 :         if (OJPEGReadByte(sp, &o) == 0)
    1891           0 :             return (0);
    1892           0 :         if (sp->subsamplingcorrect == 0)
    1893           0 :             sp->sof_c[q] = o;
    1894             :         /* H: Horizontal sampling factor, and V: Vertical sampling factor */
    1895           0 :         if (OJPEGReadByte(sp, &o) == 0)
    1896           0 :             return (0);
    1897           0 :         if (sp->subsamplingcorrect != 0)
    1898             :         {
    1899           0 :             if (q == 0)
    1900             :             {
    1901           0 :                 sp->subsampling_hor = (o >> 4);
    1902           0 :                 sp->subsampling_ver = (o & 15);
    1903           0 :                 if (((sp->subsampling_hor != 1) && (sp->subsampling_hor != 2) &&
    1904           0 :                      (sp->subsampling_hor != 4)) ||
    1905           0 :                     ((sp->subsampling_ver != 1) && (sp->subsampling_ver != 2) &&
    1906           0 :                      (sp->subsampling_ver != 4)))
    1907           0 :                     sp->subsampling_force_desubsampling_inside_decompression =
    1908             :                         1;
    1909             :             }
    1910             :             else
    1911             :             {
    1912           0 :                 if (o != 17)
    1913           0 :                     sp->subsampling_force_desubsampling_inside_decompression =
    1914             :                         1;
    1915             :             }
    1916             :         }
    1917             :         else
    1918             :         {
    1919           0 :             sp->sof_hv[q] = o;
    1920           0 :             if (sp->subsampling_force_desubsampling_inside_decompression == 0)
    1921             :             {
    1922           0 :                 if (q == 0)
    1923             :                 {
    1924           0 :                     if (o != ((sp->subsampling_hor << 4) | sp->subsampling_ver))
    1925             :                     {
    1926           0 :                         TIFFErrorExtR(tif, module,
    1927             :                                       "JPEG compressed data indicates "
    1928             :                                       "unexpected subsampling values");
    1929           0 :                         return (0);
    1930             :                     }
    1931             :                 }
    1932             :                 else
    1933             :                 {
    1934           0 :                     if (o != 17)
    1935             :                     {
    1936           0 :                         TIFFErrorExtR(tif, module,
    1937             :                                       "JPEG compressed data indicates "
    1938             :                                       "unexpected subsampling values");
    1939           0 :                         return (0);
    1940             :                     }
    1941             :                 }
    1942             :             }
    1943             :         }
    1944             :         /* Tq: Quantization table destination selector */
    1945           0 :         if (OJPEGReadByte(sp, &o) == 0)
    1946           0 :             return (0);
    1947           0 :         if (sp->subsamplingcorrect == 0)
    1948           0 :             sp->sof_tq[q] = o;
    1949             :     }
    1950           0 :     if (sp->subsamplingcorrect == 0)
    1951           0 :         sp->sof_log = 1;
    1952           0 :     return (1);
    1953             : }
    1954             : 
    1955           0 : static int OJPEGReadHeaderInfoSecStreamSos(TIFF *tif)
    1956             : {
    1957             :     /* this marker needs to be checked, and part of its data needs to be saved
    1958             :      * for regeneration later on */
    1959             :     static const char module[] = "OJPEGReadHeaderInfoSecStreamSos";
    1960           0 :     OJPEGState *sp = (OJPEGState *)tif->tif_data;
    1961             :     uint16_t m;
    1962             :     uint8_t n;
    1963             :     uint8_t o;
    1964           0 :     assert(sp->subsamplingcorrect == 0);
    1965           0 :     if (sp->sof_log == 0)
    1966             :     {
    1967           0 :         TIFFErrorExtR(tif, module, "Corrupt SOS marker in JPEG data");
    1968           0 :         return (0);
    1969             :     }
    1970             :     /* Ls */
    1971           0 :     if (OJPEGReadWord(sp, &m) == 0)
    1972           0 :         return (0);
    1973           0 :     if (m != 6 + sp->samples_per_pixel_per_plane * 2)
    1974             :     {
    1975           0 :         TIFFErrorExtR(tif, module, "Corrupt SOS marker in JPEG data");
    1976           0 :         return (0);
    1977             :     }
    1978             :     /* Ns */
    1979           0 :     if (OJPEGReadByte(sp, &n) == 0)
    1980           0 :         return (0);
    1981           0 :     if (n != sp->samples_per_pixel_per_plane)
    1982             :     {
    1983           0 :         TIFFErrorExtR(tif, module, "Corrupt SOS marker in JPEG data");
    1984           0 :         return (0);
    1985             :     }
    1986             :     /* Cs, Td, and Ta */
    1987           0 :     for (o = 0; o < sp->samples_per_pixel_per_plane; o++)
    1988             :     {
    1989             :         /* Cs */
    1990           0 :         if (OJPEGReadByte(sp, &n) == 0)
    1991           0 :             return (0);
    1992           0 :         sp->sos_cs[sp->plane_sample_offset + o] = n;
    1993             :         /* Td and Ta */
    1994           0 :         if (OJPEGReadByte(sp, &n) == 0)
    1995           0 :             return (0);
    1996           0 :         sp->sos_tda[sp->plane_sample_offset + o] = n;
    1997             :     }
    1998             :     /* skip Ss, Se, Ah, en Al -> no check, as per Tom Lane recommendation, as
    1999             :      * per LibJpeg source */
    2000           0 :     OJPEGReadSkip(sp, 3);
    2001           0 :     return (1);
    2002             : }
    2003             : 
    2004           1 : static int OJPEGReadHeaderInfoSecTablesQTable(TIFF *tif)
    2005             : {
    2006             :     static const char module[] = "OJPEGReadHeaderInfoSecTablesQTable";
    2007           1 :     OJPEGState *sp = (OJPEGState *)tif->tif_data;
    2008             :     uint8_t m;
    2009             :     uint8_t n;
    2010             :     uint32_t oa;
    2011             :     uint8_t *ob;
    2012             :     uint32_t p;
    2013           1 :     if (sp->qtable_offset[0] == 0)
    2014             :     {
    2015           0 :         TIFFErrorExtR(tif, module, "Missing JPEG tables");
    2016           0 :         return (0);
    2017             :     }
    2018           1 :     sp->in_buffer_file_pos_log = 0;
    2019           4 :     for (m = 0; m < sp->samples_per_pixel; m++)
    2020             :     {
    2021           3 :         if ((sp->qtable_offset[m] != 0) &&
    2022           2 :             ((m == 0) || (sp->qtable_offset[m] != sp->qtable_offset[m - 1])))
    2023             :         {
    2024           4 :             for (n = 0; n < m - 1; n++)
    2025             :             {
    2026           1 :                 if (sp->qtable_offset[m] == sp->qtable_offset[n])
    2027             :                 {
    2028           0 :                     TIFFErrorExtR(tif, module, "Corrupt JpegQTables tag value");
    2029           0 :                     return (0);
    2030             :                 }
    2031             :             }
    2032           3 :             oa = sizeof(uint32_t) + 69;
    2033           3 :             ob = _TIFFmallocExt(tif, oa);
    2034           3 :             if (ob == 0)
    2035             :             {
    2036           0 :                 TIFFErrorExtR(tif, module, "Out of memory");
    2037           0 :                 return (0);
    2038             :             }
    2039           3 :             *(uint32_t *)ob = oa;
    2040           3 :             ob[sizeof(uint32_t)] = 255;
    2041           3 :             ob[sizeof(uint32_t) + 1] = JPEG_MARKER_DQT;
    2042           3 :             ob[sizeof(uint32_t) + 2] = 0;
    2043           3 :             ob[sizeof(uint32_t) + 3] = 67;
    2044           3 :             ob[sizeof(uint32_t) + 4] = m;
    2045           3 :             TIFFSeekFile(tif, sp->qtable_offset[m], SEEK_SET);
    2046           3 :             p = (uint32_t)TIFFReadFile(tif, &ob[sizeof(uint32_t) + 5], 64);
    2047           3 :             if (p != 64)
    2048             :             {
    2049           0 :                 _TIFFfreeExt(tif, ob);
    2050           0 :                 return (0);
    2051             :             }
    2052           3 :             if (sp->qtable[m] != 0)
    2053           0 :                 _TIFFfreeExt(tif, sp->qtable[m]);
    2054           3 :             sp->qtable[m] = ob;
    2055           3 :             sp->sof_tq[m] = m;
    2056             :         }
    2057             :         else
    2058           0 :             sp->sof_tq[m] = sp->sof_tq[m - 1];
    2059             :     }
    2060           1 :     return (1);
    2061             : }
    2062             : 
    2063           1 : static int OJPEGReadHeaderInfoSecTablesDcTable(TIFF *tif)
    2064             : {
    2065             :     static const char module[] = "OJPEGReadHeaderInfoSecTablesDcTable";
    2066           1 :     OJPEGState *sp = (OJPEGState *)tif->tif_data;
    2067             :     uint8_t m;
    2068             :     uint8_t n;
    2069             :     uint8_t o[16];
    2070             :     uint32_t p;
    2071             :     uint32_t q;
    2072             :     uint32_t ra;
    2073             :     uint8_t *rb;
    2074           1 :     if (sp->dctable_offset[0] == 0)
    2075             :     {
    2076           0 :         TIFFErrorExtR(tif, module, "Missing JPEG tables");
    2077           0 :         return (0);
    2078             :     }
    2079           1 :     sp->in_buffer_file_pos_log = 0;
    2080           4 :     for (m = 0; m < sp->samples_per_pixel; m++)
    2081             :     {
    2082           3 :         if ((sp->dctable_offset[m] != 0) &&
    2083           2 :             ((m == 0) || (sp->dctable_offset[m] != sp->dctable_offset[m - 1])))
    2084             :         {
    2085           4 :             for (n = 0; n < m - 1; n++)
    2086             :             {
    2087           1 :                 if (sp->dctable_offset[m] == sp->dctable_offset[n])
    2088             :                 {
    2089           0 :                     TIFFErrorExtR(tif, module,
    2090             :                                   "Corrupt JpegDcTables tag value");
    2091           0 :                     return (0);
    2092             :                 }
    2093             :             }
    2094           3 :             TIFFSeekFile(tif, sp->dctable_offset[m], SEEK_SET);
    2095           3 :             p = (uint32_t)TIFFReadFile(tif, o, 16);
    2096           3 :             if (p != 16)
    2097           0 :                 return (0);
    2098           3 :             q = 0;
    2099          51 :             for (n = 0; n < 16; n++)
    2100          48 :                 q += o[n];
    2101           3 :             ra = sizeof(uint32_t) + 21 + q;
    2102           3 :             rb = _TIFFmallocExt(tif, ra);
    2103           3 :             if (rb == 0)
    2104             :             {
    2105           0 :                 TIFFErrorExtR(tif, module, "Out of memory");
    2106           0 :                 return (0);
    2107             :             }
    2108           3 :             *(uint32_t *)rb = ra;
    2109           3 :             rb[sizeof(uint32_t)] = 255;
    2110           3 :             rb[sizeof(uint32_t) + 1] = JPEG_MARKER_DHT;
    2111           3 :             rb[sizeof(uint32_t) + 2] = (uint8_t)((19 + q) >> 8);
    2112           3 :             rb[sizeof(uint32_t) + 3] = ((19 + q) & 255);
    2113           3 :             rb[sizeof(uint32_t) + 4] = m;
    2114          51 :             for (n = 0; n < 16; n++)
    2115          48 :                 rb[sizeof(uint32_t) + 5 + n] = o[n];
    2116           3 :             p = (uint32_t)TIFFReadFile(tif, &(rb[sizeof(uint32_t) + 21]), q);
    2117           3 :             if (p != q)
    2118             :             {
    2119           0 :                 _TIFFfreeExt(tif, rb);
    2120           0 :                 return (0);
    2121             :             }
    2122           3 :             if (sp->dctable[m] != 0)
    2123           0 :                 _TIFFfreeExt(tif, sp->dctable[m]);
    2124           3 :             sp->dctable[m] = rb;
    2125           3 :             sp->sos_tda[m] = (m << 4);
    2126             :         }
    2127             :         else
    2128           0 :             sp->sos_tda[m] = sp->sos_tda[m - 1];
    2129             :     }
    2130           1 :     return (1);
    2131             : }
    2132             : 
    2133           1 : static int OJPEGReadHeaderInfoSecTablesAcTable(TIFF *tif)
    2134             : {
    2135             :     static const char module[] = "OJPEGReadHeaderInfoSecTablesAcTable";
    2136           1 :     OJPEGState *sp = (OJPEGState *)tif->tif_data;
    2137             :     uint8_t m;
    2138             :     uint8_t n;
    2139             :     uint8_t o[16];
    2140             :     uint32_t p;
    2141             :     uint32_t q;
    2142             :     uint32_t ra;
    2143             :     uint8_t *rb;
    2144           1 :     if (sp->actable_offset[0] == 0)
    2145             :     {
    2146           0 :         TIFFErrorExtR(tif, module, "Missing JPEG tables");
    2147           0 :         return (0);
    2148             :     }
    2149           1 :     sp->in_buffer_file_pos_log = 0;
    2150           4 :     for (m = 0; m < sp->samples_per_pixel; m++)
    2151             :     {
    2152           3 :         if ((sp->actable_offset[m] != 0) &&
    2153           2 :             ((m == 0) || (sp->actable_offset[m] != sp->actable_offset[m - 1])))
    2154             :         {
    2155           4 :             for (n = 0; n < m - 1; n++)
    2156             :             {
    2157           1 :                 if (sp->actable_offset[m] == sp->actable_offset[n])
    2158             :                 {
    2159           0 :                     TIFFErrorExtR(tif, module,
    2160             :                                   "Corrupt JpegAcTables tag value");
    2161           0 :                     return (0);
    2162             :                 }
    2163             :             }
    2164           3 :             TIFFSeekFile(tif, sp->actable_offset[m], SEEK_SET);
    2165           3 :             p = (uint32_t)TIFFReadFile(tif, o, 16);
    2166           3 :             if (p != 16)
    2167           0 :                 return (0);
    2168           3 :             q = 0;
    2169          51 :             for (n = 0; n < 16; n++)
    2170          48 :                 q += o[n];
    2171           3 :             ra = sizeof(uint32_t) + 21 + q;
    2172           3 :             rb = _TIFFmallocExt(tif, ra);
    2173           3 :             if (rb == 0)
    2174             :             {
    2175           0 :                 TIFFErrorExtR(tif, module, "Out of memory");
    2176           0 :                 return (0);
    2177             :             }
    2178           3 :             *(uint32_t *)rb = ra;
    2179           3 :             rb[sizeof(uint32_t)] = 255;
    2180           3 :             rb[sizeof(uint32_t) + 1] = JPEG_MARKER_DHT;
    2181           3 :             rb[sizeof(uint32_t) + 2] = (uint8_t)((19 + q) >> 8);
    2182           3 :             rb[sizeof(uint32_t) + 3] = ((19 + q) & 255);
    2183           3 :             rb[sizeof(uint32_t) + 4] = (16 | m);
    2184          51 :             for (n = 0; n < 16; n++)
    2185          48 :                 rb[sizeof(uint32_t) + 5 + n] = o[n];
    2186           3 :             p = (uint32_t)TIFFReadFile(tif, &(rb[sizeof(uint32_t) + 21]), q);
    2187           3 :             if (p != q)
    2188             :             {
    2189           0 :                 _TIFFfreeExt(tif, rb);
    2190           0 :                 return (0);
    2191             :             }
    2192           3 :             if (sp->actable[m] != 0)
    2193           0 :                 _TIFFfreeExt(tif, sp->actable[m]);
    2194           3 :             sp->actable[m] = rb;
    2195           3 :             sp->sos_tda[m] = (sp->sos_tda[m] | m);
    2196             :         }
    2197             :         else
    2198           0 :             sp->sos_tda[m] = (sp->sos_tda[m] | (sp->sos_tda[m - 1] & 15));
    2199             :     }
    2200           1 :     return (1);
    2201             : }
    2202             : 
    2203          22 : static int OJPEGReadBufferFill(OJPEGState *sp)
    2204             : {
    2205             :     uint16_t m;
    2206             :     tmsize_t n;
    2207             :     /* TODO: double-check: when subsamplingcorrect is set, no call to
    2208             :      * TIFFErrorExt or TIFFWarningExt should be made in any other case, seek or
    2209             :      * read errors should be passed through */
    2210             :     do
    2211             :     {
    2212          22 :         if (sp->in_buffer_file_togo != 0)
    2213             :         {
    2214           6 :             if (sp->in_buffer_file_pos_log == 0)
    2215             :             {
    2216           3 :                 TIFFSeekFile(sp->tif, sp->in_buffer_file_pos, SEEK_SET);
    2217           3 :                 sp->in_buffer_file_pos_log = 1;
    2218             :             }
    2219           6 :             m = OJPEG_BUFFER;
    2220           6 :             if ((uint64_t)m > sp->in_buffer_file_togo)
    2221           1 :                 m = (uint16_t)sp->in_buffer_file_togo;
    2222           6 :             n = TIFFReadFile(sp->tif, sp->in_buffer, (tmsize_t)m);
    2223           6 :             if (n == 0)
    2224           0 :                 return (0);
    2225           6 :             assert(n > 0);
    2226           6 :             assert(n <= OJPEG_BUFFER);
    2227           6 :             assert(n < 65536);
    2228           6 :             assert((uint64_t)n <= sp->in_buffer_file_togo);
    2229           6 :             m = (uint16_t)n;
    2230           6 :             sp->in_buffer_togo = m;
    2231           6 :             sp->in_buffer_cur = sp->in_buffer;
    2232           6 :             sp->in_buffer_file_togo -= m;
    2233           6 :             sp->in_buffer_file_pos += m;
    2234           6 :             break;
    2235             :         }
    2236          16 :         sp->in_buffer_file_pos_log = 0;
    2237          16 :         switch (sp->in_buffer_source)
    2238             :         {
    2239           4 :             case osibsNotSetYet:
    2240           4 :                 if (sp->jpeg_interchange_format != 0)
    2241             :                 {
    2242           0 :                     sp->in_buffer_file_pos = sp->jpeg_interchange_format;
    2243           0 :                     sp->in_buffer_file_togo =
    2244           0 :                         sp->jpeg_interchange_format_length;
    2245             :                 }
    2246           4 :                 sp->in_buffer_source = osibsJpegInterchangeFormat;
    2247           4 :                 break;
    2248           4 :             case osibsJpegInterchangeFormat:
    2249           4 :                 sp->in_buffer_source = osibsStrile;
    2250           4 :                 break;
    2251           6 :             case osibsStrile:
    2252           6 :                 if (sp->in_buffer_next_strile == sp->in_buffer_strile_count)
    2253           2 :                     sp->in_buffer_source = osibsEof;
    2254             :                 else
    2255             :                 {
    2256           4 :                     int err = 0;
    2257           4 :                     sp->in_buffer_file_pos = TIFFGetStrileOffsetWithErr(
    2258             :                         sp->tif, sp->in_buffer_next_strile, &err);
    2259           4 :                     if (err)
    2260           0 :                         return 0;
    2261           4 :                     if (sp->in_buffer_file_pos != 0)
    2262             :                     {
    2263           4 :                         uint64_t bytecount = TIFFGetStrileByteCountWithErr(
    2264             :                             sp->tif, sp->in_buffer_next_strile, &err);
    2265           4 :                         if (err)
    2266           0 :                             return 0;
    2267           4 :                         if (sp->in_buffer_file_pos >= sp->file_size)
    2268           2 :                             sp->in_buffer_file_pos = 0;
    2269           2 :                         else if (bytecount == 0)
    2270           0 :                             sp->in_buffer_file_togo =
    2271           0 :                                 sp->file_size - sp->in_buffer_file_pos;
    2272             :                         else
    2273             :                         {
    2274           2 :                             sp->in_buffer_file_togo = bytecount;
    2275           2 :                             if (sp->in_buffer_file_togo == 0)
    2276           0 :                                 sp->in_buffer_file_pos = 0;
    2277           2 :                             else if (sp->in_buffer_file_pos >
    2278           2 :                                          UINT64_MAX - sp->in_buffer_file_togo ||
    2279           2 :                                      sp->in_buffer_file_pos +
    2280           2 :                                              sp->in_buffer_file_togo >
    2281           2 :                                          sp->file_size)
    2282           0 :                                 sp->in_buffer_file_togo =
    2283           0 :                                     sp->file_size - sp->in_buffer_file_pos;
    2284             :                         }
    2285             :                     }
    2286           4 :                     sp->in_buffer_next_strile++;
    2287             :                 }
    2288           6 :                 break;
    2289           2 :             default:
    2290           2 :                 return (0);
    2291             :         }
    2292             :     } while (1);
    2293           6 :     return (1);
    2294             : }
    2295             : 
    2296           0 : static int OJPEGReadByte(OJPEGState *sp, uint8_t *byte)
    2297             : {
    2298           0 :     if (sp->in_buffer_togo == 0)
    2299             :     {
    2300           0 :         if (OJPEGReadBufferFill(sp) == 0)
    2301           0 :             return (0);
    2302           0 :         assert(sp->in_buffer_togo > 0);
    2303             :     }
    2304           0 :     *byte = *(sp->in_buffer_cur);
    2305           0 :     sp->in_buffer_cur++;
    2306           0 :     sp->in_buffer_togo--;
    2307           0 :     return (1);
    2308             : }
    2309             : 
    2310           4 : static int OJPEGReadBytePeek(OJPEGState *sp, uint8_t *byte)
    2311             : {
    2312           4 :     if (sp->in_buffer_togo == 0)
    2313             :     {
    2314           4 :         if (OJPEGReadBufferFill(sp) == 0)
    2315           2 :             return (0);
    2316           2 :         assert(sp->in_buffer_togo > 0);
    2317             :     }
    2318           2 :     *byte = *(sp->in_buffer_cur);
    2319           2 :     return (1);
    2320             : }
    2321             : 
    2322           0 : static void OJPEGReadByteAdvance(OJPEGState *sp)
    2323             : {
    2324           0 :     assert(sp->in_buffer_togo > 0);
    2325           0 :     sp->in_buffer_cur++;
    2326           0 :     sp->in_buffer_togo--;
    2327           0 : }
    2328             : 
    2329           0 : static int OJPEGReadWord(OJPEGState *sp, uint16_t *word)
    2330             : {
    2331             :     uint8_t m;
    2332           0 :     if (OJPEGReadByte(sp, &m) == 0)
    2333           0 :         return (0);
    2334           0 :     *word = (m << 8);
    2335           0 :     if (OJPEGReadByte(sp, &m) == 0)
    2336           0 :         return (0);
    2337           0 :     *word |= m;
    2338           0 :     return (1);
    2339             : }
    2340             : 
    2341           0 : static int OJPEGReadBlock(OJPEGState *sp, uint16_t len, void *mem)
    2342             : {
    2343             :     uint16_t mlen;
    2344             :     uint8_t *mmem;
    2345             :     uint16_t n;
    2346           0 :     assert(len > 0);
    2347           0 :     mlen = len;
    2348           0 :     mmem = mem;
    2349             :     do
    2350             :     {
    2351           0 :         if (sp->in_buffer_togo == 0)
    2352             :         {
    2353           0 :             if (OJPEGReadBufferFill(sp) == 0)
    2354           0 :                 return (0);
    2355           0 :             assert(sp->in_buffer_togo > 0);
    2356             :         }
    2357           0 :         n = mlen;
    2358           0 :         if (n > sp->in_buffer_togo)
    2359           0 :             n = sp->in_buffer_togo;
    2360           0 :         _TIFFmemcpy(mmem, sp->in_buffer_cur, n);
    2361           0 :         sp->in_buffer_cur += n;
    2362           0 :         sp->in_buffer_togo -= n;
    2363           0 :         mlen -= n;
    2364           0 :         mmem += n;
    2365           0 :     } while (mlen > 0);
    2366           0 :     return (1);
    2367             : }
    2368             : 
    2369           0 : static void OJPEGReadSkip(OJPEGState *sp, uint16_t len)
    2370             : {
    2371             :     uint16_t m;
    2372             :     uint16_t n;
    2373           0 :     m = len;
    2374           0 :     n = m;
    2375           0 :     if (n > sp->in_buffer_togo)
    2376           0 :         n = sp->in_buffer_togo;
    2377           0 :     sp->in_buffer_cur += n;
    2378           0 :     sp->in_buffer_togo -= n;
    2379           0 :     m -= n;
    2380           0 :     if (m > 0)
    2381             :     {
    2382           0 :         assert(sp->in_buffer_togo == 0);
    2383           0 :         n = m;
    2384           0 :         if ((uint64_t)n > sp->in_buffer_file_togo)
    2385           0 :             n = (uint16_t)sp->in_buffer_file_togo;
    2386           0 :         sp->in_buffer_file_pos += n;
    2387           0 :         sp->in_buffer_file_togo -= n;
    2388           0 :         sp->in_buffer_file_pos_log = 0;
    2389             :         /* we don't skip past jpeginterchangeformat/strile block...
    2390             :          * if that is asked from us, we're dealing with totally bazurk
    2391             :          * data anyway, and we've not seen this happening on any
    2392             :          * testfile, so we might as well likely cause some other
    2393             :          * meaningless error to be passed at some later time
    2394             :          */
    2395             :     }
    2396           0 : }
    2397             : 
    2398          17 : static int OJPEGWriteStream(TIFF *tif, void **mem, uint32_t *len)
    2399             : {
    2400          17 :     OJPEGState *sp = (OJPEGState *)tif->tif_data;
    2401          17 :     *len = 0;
    2402             :     do
    2403             :     {
    2404          21 :         assert(sp->out_state <= ososEoi);
    2405          21 :         switch (sp->out_state)
    2406             :         {
    2407           1 :             case ososSoi:
    2408           1 :                 OJPEGWriteStreamSoi(tif, mem, len);
    2409           1 :                 break;
    2410           1 :             case ososQTable0:
    2411           1 :                 OJPEGWriteStreamQTable(tif, 0, mem, len);
    2412           1 :                 break;
    2413           1 :             case ososQTable1:
    2414           1 :                 OJPEGWriteStreamQTable(tif, 1, mem, len);
    2415           1 :                 break;
    2416           1 :             case ososQTable2:
    2417           1 :                 OJPEGWriteStreamQTable(tif, 2, mem, len);
    2418           1 :                 break;
    2419           1 :             case ososQTable3:
    2420           1 :                 OJPEGWriteStreamQTable(tif, 3, mem, len);
    2421           1 :                 break;
    2422           1 :             case ososDcTable0:
    2423           1 :                 OJPEGWriteStreamDcTable(tif, 0, mem, len);
    2424           1 :                 break;
    2425           1 :             case ososDcTable1:
    2426           1 :                 OJPEGWriteStreamDcTable(tif, 1, mem, len);
    2427           1 :                 break;
    2428           1 :             case ososDcTable2:
    2429           1 :                 OJPEGWriteStreamDcTable(tif, 2, mem, len);
    2430           1 :                 break;
    2431           1 :             case ososDcTable3:
    2432           1 :                 OJPEGWriteStreamDcTable(tif, 3, mem, len);
    2433           1 :                 break;
    2434           1 :             case ososAcTable0:
    2435           1 :                 OJPEGWriteStreamAcTable(tif, 0, mem, len);
    2436           1 :                 break;
    2437           1 :             case ososAcTable1:
    2438           1 :                 OJPEGWriteStreamAcTable(tif, 1, mem, len);
    2439           1 :                 break;
    2440           1 :             case ososAcTable2:
    2441           1 :                 OJPEGWriteStreamAcTable(tif, 2, mem, len);
    2442           1 :                 break;
    2443           1 :             case ososAcTable3:
    2444           1 :                 OJPEGWriteStreamAcTable(tif, 3, mem, len);
    2445           1 :                 break;
    2446           1 :             case ososDri:
    2447           1 :                 OJPEGWriteStreamDri(tif, mem, len);
    2448           1 :                 break;
    2449           1 :             case ososSof:
    2450           1 :                 OJPEGWriteStreamSof(tif, mem, len);
    2451           1 :                 break;
    2452           1 :             case ososSos:
    2453           1 :                 OJPEGWriteStreamSos(tif, mem, len);
    2454           1 :                 break;
    2455           4 :             case ososCompressed:
    2456           4 :                 if (OJPEGWriteStreamCompressed(tif, mem, len) == 0)
    2457           0 :                     return (0);
    2458           4 :                 break;
    2459           0 :             case ososRst:
    2460           0 :                 OJPEGWriteStreamRst(tif, mem, len);
    2461           0 :                 break;
    2462           1 :             case ososEoi:
    2463           1 :                 OJPEGWriteStreamEoi(tif, mem, len);
    2464           1 :                 break;
    2465             :         }
    2466          21 :     } while (*len == 0);
    2467          17 :     return (1);
    2468             : }
    2469             : 
    2470           1 : static void OJPEGWriteStreamSoi(TIFF *tif, void **mem, uint32_t *len)
    2471             : {
    2472           1 :     OJPEGState *sp = (OJPEGState *)tif->tif_data;
    2473             :     assert(OJPEG_BUFFER >= 2);
    2474           1 :     sp->out_buffer[0] = 255;
    2475           1 :     sp->out_buffer[1] = JPEG_MARKER_SOI;
    2476           1 :     *len = 2;
    2477           1 :     *mem = (void *)sp->out_buffer;
    2478           1 :     sp->out_state++;
    2479           1 : }
    2480             : 
    2481           4 : static void OJPEGWriteStreamQTable(TIFF *tif, uint8_t table_index, void **mem,
    2482             :                                    uint32_t *len)
    2483             : {
    2484           4 :     OJPEGState *sp = (OJPEGState *)tif->tif_data;
    2485           4 :     if (sp->qtable[table_index] != 0)
    2486             :     {
    2487           3 :         *mem = (void *)(sp->qtable[table_index] + sizeof(uint32_t));
    2488           3 :         *len = *((uint32_t *)sp->qtable[table_index]) - sizeof(uint32_t);
    2489             :     }
    2490           4 :     sp->out_state++;
    2491           4 : }
    2492             : 
    2493           4 : static void OJPEGWriteStreamDcTable(TIFF *tif, uint8_t table_index, void **mem,
    2494             :                                     uint32_t *len)
    2495             : {
    2496           4 :     OJPEGState *sp = (OJPEGState *)tif->tif_data;
    2497           4 :     if (sp->dctable[table_index] != 0)
    2498             :     {
    2499           3 :         *mem = (void *)(sp->dctable[table_index] + sizeof(uint32_t));
    2500           3 :         *len = *((uint32_t *)sp->dctable[table_index]) - sizeof(uint32_t);
    2501             :     }
    2502           4 :     sp->out_state++;
    2503           4 : }
    2504             : 
    2505           4 : static void OJPEGWriteStreamAcTable(TIFF *tif, uint8_t table_index, void **mem,
    2506             :                                     uint32_t *len)
    2507             : {
    2508           4 :     OJPEGState *sp = (OJPEGState *)tif->tif_data;
    2509           4 :     if (sp->actable[table_index] != 0)
    2510             :     {
    2511           3 :         *mem = (void *)(sp->actable[table_index] + sizeof(uint32_t));
    2512           3 :         *len = *((uint32_t *)sp->actable[table_index]) - sizeof(uint32_t);
    2513             :     }
    2514           4 :     sp->out_state++;
    2515           4 : }
    2516             : 
    2517           1 : static void OJPEGWriteStreamDri(TIFF *tif, void **mem, uint32_t *len)
    2518             : {
    2519           1 :     OJPEGState *sp = (OJPEGState *)tif->tif_data;
    2520             :     assert(OJPEG_BUFFER >= 6);
    2521           1 :     if (sp->restart_interval != 0)
    2522             :     {
    2523           0 :         sp->out_buffer[0] = 255;
    2524           0 :         sp->out_buffer[1] = JPEG_MARKER_DRI;
    2525           0 :         sp->out_buffer[2] = 0;
    2526           0 :         sp->out_buffer[3] = 4;
    2527           0 :         sp->out_buffer[4] = (sp->restart_interval >> 8);
    2528           0 :         sp->out_buffer[5] = (sp->restart_interval & 255);
    2529           0 :         *len = 6;
    2530           0 :         *mem = (void *)sp->out_buffer;
    2531             :     }
    2532           1 :     sp->out_state++;
    2533           1 : }
    2534             : 
    2535           1 : static void OJPEGWriteStreamSof(TIFF *tif, void **mem, uint32_t *len)
    2536             : {
    2537           1 :     OJPEGState *sp = (OJPEGState *)tif->tif_data;
    2538             :     uint8_t m;
    2539           1 :     assert(OJPEG_BUFFER >= 2 + 8 + sp->samples_per_pixel_per_plane * 3);
    2540           1 :     assert(255 >= 8 + sp->samples_per_pixel_per_plane * 3);
    2541           1 :     sp->out_buffer[0] = 255;
    2542           1 :     sp->out_buffer[1] = sp->sof_marker_id;
    2543             :     /* Lf */
    2544           1 :     sp->out_buffer[2] = 0;
    2545           1 :     sp->out_buffer[3] = 8 + sp->samples_per_pixel_per_plane * 3;
    2546             :     /* P */
    2547           1 :     sp->out_buffer[4] = 8;
    2548             :     /* Y */
    2549           1 :     sp->out_buffer[5] = (uint8_t)(sp->sof_y >> 8);
    2550           1 :     sp->out_buffer[6] = (sp->sof_y & 255);
    2551             :     /* X */
    2552           1 :     sp->out_buffer[7] = (uint8_t)(sp->sof_x >> 8);
    2553           1 :     sp->out_buffer[8] = (sp->sof_x & 255);
    2554             :     /* Nf */
    2555           1 :     sp->out_buffer[9] = sp->samples_per_pixel_per_plane;
    2556           4 :     for (m = 0; m < sp->samples_per_pixel_per_plane; m++)
    2557             :     {
    2558             :         /* C */
    2559           3 :         sp->out_buffer[10 + m * 3] = sp->sof_c[sp->plane_sample_offset + m];
    2560             :         /* H and V */
    2561           3 :         sp->out_buffer[10 + m * 3 + 1] =
    2562           3 :             sp->sof_hv[sp->plane_sample_offset + m];
    2563             :         /* Tq */
    2564           3 :         sp->out_buffer[10 + m * 3 + 2] =
    2565           3 :             sp->sof_tq[sp->plane_sample_offset + m];
    2566             :     }
    2567           1 :     *len = 10 + sp->samples_per_pixel_per_plane * 3;
    2568           1 :     *mem = (void *)sp->out_buffer;
    2569           1 :     sp->out_state++;
    2570           1 : }
    2571             : 
    2572           1 : static void OJPEGWriteStreamSos(TIFF *tif, void **mem, uint32_t *len)
    2573             : {
    2574           1 :     OJPEGState *sp = (OJPEGState *)tif->tif_data;
    2575             :     uint8_t m;
    2576           1 :     assert(OJPEG_BUFFER >= 2 + 6 + sp->samples_per_pixel_per_plane * 2);
    2577           1 :     assert(255 >= 6 + sp->samples_per_pixel_per_plane * 2);
    2578           1 :     sp->out_buffer[0] = 255;
    2579           1 :     sp->out_buffer[1] = JPEG_MARKER_SOS;
    2580             :     /* Ls */
    2581           1 :     sp->out_buffer[2] = 0;
    2582           1 :     sp->out_buffer[3] = 6 + sp->samples_per_pixel_per_plane * 2;
    2583             :     /* Ns */
    2584           1 :     sp->out_buffer[4] = sp->samples_per_pixel_per_plane;
    2585           4 :     for (m = 0; m < sp->samples_per_pixel_per_plane; m++)
    2586             :     {
    2587             :         /* Cs */
    2588           3 :         sp->out_buffer[5 + m * 2] = sp->sos_cs[sp->plane_sample_offset + m];
    2589             :         /* Td and Ta */
    2590           3 :         sp->out_buffer[5 + m * 2 + 1] =
    2591           3 :             sp->sos_tda[sp->plane_sample_offset + m];
    2592             :     }
    2593             :     /* Ss */
    2594           1 :     sp->out_buffer[5 + sp->samples_per_pixel_per_plane * 2] = 0;
    2595             :     /* Se */
    2596           1 :     sp->out_buffer[5 + sp->samples_per_pixel_per_plane * 2 + 1] = 63;
    2597             :     /* Ah and Al */
    2598           1 :     sp->out_buffer[5 + sp->samples_per_pixel_per_plane * 2 + 2] = 0;
    2599           1 :     *len = 8 + sp->samples_per_pixel_per_plane * 2;
    2600           1 :     *mem = (void *)sp->out_buffer;
    2601           1 :     sp->out_state++;
    2602           1 : }
    2603             : 
    2604           4 : static int OJPEGWriteStreamCompressed(TIFF *tif, void **mem, uint32_t *len)
    2605             : {
    2606           4 :     OJPEGState *sp = (OJPEGState *)tif->tif_data;
    2607           4 :     if (sp->in_buffer_togo == 0)
    2608             :     {
    2609           4 :         if (OJPEGReadBufferFill(sp) == 0)
    2610           0 :             return (0);
    2611           4 :         assert(sp->in_buffer_togo > 0);
    2612             :     }
    2613           4 :     *len = sp->in_buffer_togo;
    2614           4 :     *mem = (void *)sp->in_buffer_cur;
    2615           4 :     sp->in_buffer_togo = 0;
    2616           4 :     if (sp->in_buffer_file_togo == 0)
    2617             :     {
    2618           1 :         switch (sp->in_buffer_source)
    2619             :         {
    2620           1 :             case osibsStrile:
    2621           1 :                 if (sp->in_buffer_next_strile < sp->in_buffer_strile_count)
    2622           0 :                     sp->out_state = ososRst;
    2623             :                 else
    2624           1 :                     sp->out_state = ososEoi;
    2625           1 :                 break;
    2626           0 :             case osibsEof:
    2627           0 :                 sp->out_state = ososEoi;
    2628           0 :                 break;
    2629           0 :             default:
    2630           0 :                 break;
    2631             :         }
    2632           3 :     }
    2633           4 :     return (1);
    2634             : }
    2635             : 
    2636           0 : static void OJPEGWriteStreamRst(TIFF *tif, void **mem, uint32_t *len)
    2637             : {
    2638           0 :     OJPEGState *sp = (OJPEGState *)tif->tif_data;
    2639             :     assert(OJPEG_BUFFER >= 2);
    2640           0 :     sp->out_buffer[0] = 255;
    2641           0 :     sp->out_buffer[1] = JPEG_MARKER_RST0 + sp->restart_index;
    2642           0 :     sp->restart_index++;
    2643           0 :     if (sp->restart_index == 8)
    2644           0 :         sp->restart_index = 0;
    2645           0 :     *len = 2;
    2646           0 :     *mem = (void *)sp->out_buffer;
    2647           0 :     sp->out_state = ososCompressed;
    2648           0 : }
    2649             : 
    2650           1 : static void OJPEGWriteStreamEoi(TIFF *tif, void **mem, uint32_t *len)
    2651             : {
    2652           1 :     OJPEGState *sp = (OJPEGState *)tif->tif_data;
    2653             :     assert(OJPEG_BUFFER >= 2);
    2654           1 :     sp->out_buffer[0] = 255;
    2655           1 :     sp->out_buffer[1] = JPEG_MARKER_EOI;
    2656           1 :     *len = 2;
    2657           1 :     *mem = (void *)sp->out_buffer;
    2658           1 : }
    2659             : 
    2660             : #ifndef LIBJPEG_ENCAP_EXTERNAL
    2661           1 : static int jpeg_create_decompress_encap(OJPEGState *sp,
    2662             :                                         jpeg_decompress_struct *cinfo)
    2663             : {
    2664           1 :     if (SETJMP(sp->exit_jmpbuf))
    2665           0 :         return 0;
    2666             :     else
    2667             :     {
    2668           1 :         jpeg_create_decompress(cinfo);
    2669           1 :         return 1;
    2670             :     }
    2671             : }
    2672             : #endif
    2673             : 
    2674             : #ifndef LIBJPEG_ENCAP_EXTERNAL
    2675           1 : static int jpeg_read_header_encap(OJPEGState *sp, jpeg_decompress_struct *cinfo,
    2676             :                                   uint8_t require_image)
    2677             : {
    2678           1 :     if (SETJMP(sp->exit_jmpbuf))
    2679           0 :         return 0;
    2680             :     else
    2681             :     {
    2682           1 :         jpeg_read_header(cinfo, require_image);
    2683           1 :         return 1;
    2684             :     }
    2685             : }
    2686             : #endif
    2687             : 
    2688             : #ifndef LIBJPEG_ENCAP_EXTERNAL
    2689           1 : static int jpeg_start_decompress_encap(OJPEGState *sp,
    2690             :                                        jpeg_decompress_struct *cinfo)
    2691             : {
    2692           1 :     if (SETJMP(sp->exit_jmpbuf))
    2693           0 :         return 0;
    2694             :     else
    2695             :     {
    2696           1 :         jpeg_start_decompress(cinfo);
    2697           1 :         return 1;
    2698             :     }
    2699             : }
    2700             : #endif
    2701             : 
    2702             : #ifndef LIBJPEG_ENCAP_EXTERNAL
    2703           0 : static int jpeg_read_scanlines_encap(OJPEGState *sp,
    2704             :                                      jpeg_decompress_struct *cinfo,
    2705             :                                      void *scanlines, uint32_t max_lines)
    2706             : {
    2707           0 :     if (SETJMP(sp->exit_jmpbuf))
    2708           0 :         return 0;
    2709             :     else
    2710             :     {
    2711           0 :         jpeg_read_scanlines(cinfo, scanlines, max_lines);
    2712           0 :         return 1;
    2713             :     }
    2714             : }
    2715             : #endif
    2716             : 
    2717             : #ifndef LIBJPEG_ENCAP_EXTERNAL
    2718          14 : static int jpeg_read_raw_data_encap(OJPEGState *sp,
    2719             :                                     jpeg_decompress_struct *cinfo, void *data,
    2720             :                                     uint32_t max_lines)
    2721             : {
    2722          14 :     if (SETJMP(sp->exit_jmpbuf))
    2723           0 :         return 0;
    2724             :     else
    2725             :     {
    2726          14 :         jpeg_read_raw_data(cinfo, data, max_lines);
    2727          14 :         return 1;
    2728             :     }
    2729             : }
    2730             : #endif
    2731             : 
    2732             : #ifndef LIBJPEG_ENCAP_EXTERNAL
    2733           0 : static void jpeg_encap_unwind(TIFF *tif)
    2734             : {
    2735           0 :     OJPEGState *sp = (OJPEGState *)tif->tif_data;
    2736           0 :     LONGJMP(sp->exit_jmpbuf, 1);
    2737             : }
    2738             : #endif
    2739             : 
    2740           0 : static void OJPEGLibjpegJpegErrorMgrOutputMessage(jpeg_common_struct *cinfo)
    2741             : {
    2742             :     char buffer[JMSG_LENGTH_MAX];
    2743           0 :     (*cinfo->err->format_message)(cinfo, buffer);
    2744           0 :     TIFFWarningExtR(((TIFF *)(cinfo->client_data)), "LibJpeg", "%s", buffer);
    2745           0 : }
    2746             : 
    2747           0 : static void OJPEGLibjpegJpegErrorMgrErrorExit(jpeg_common_struct *cinfo)
    2748             : {
    2749             :     char buffer[JMSG_LENGTH_MAX];
    2750           0 :     (*cinfo->err->format_message)(cinfo, buffer);
    2751           0 :     TIFFErrorExtR(((TIFF *)(cinfo->client_data)), "LibJpeg", "%s", buffer);
    2752           0 :     jpeg_encap_unwind((TIFF *)(cinfo->client_data));
    2753           0 : }
    2754             : 
    2755           1 : static void OJPEGLibjpegJpegSourceMgrInitSource(jpeg_decompress_struct *cinfo)
    2756             : {
    2757             :     (void)cinfo;
    2758           1 : }
    2759             : 
    2760             : static boolean
    2761          17 : OJPEGLibjpegJpegSourceMgrFillInputBuffer(jpeg_decompress_struct *cinfo)
    2762             : {
    2763          17 :     TIFF *tif = (TIFF *)cinfo->client_data;
    2764          17 :     OJPEGState *sp = (OJPEGState *)tif->tif_data;
    2765          17 :     void *mem = 0;
    2766          17 :     uint32_t len = 0U;
    2767          17 :     if (OJPEGWriteStream(tif, &mem, &len) == 0)
    2768             :     {
    2769           0 :         TIFFErrorExtR(tif, "LibJpeg", "Premature end of JPEG data");
    2770           0 :         jpeg_encap_unwind(tif);
    2771             :     }
    2772          17 :     sp->libjpeg_jpeg_source_mgr.bytes_in_buffer = len;
    2773          17 :     sp->libjpeg_jpeg_source_mgr.next_input_byte = mem;
    2774          17 :     return (1);
    2775             : }
    2776             : 
    2777             : static void
    2778           0 : OJPEGLibjpegJpegSourceMgrSkipInputData(jpeg_decompress_struct *cinfo,
    2779             :                                        long num_bytes)
    2780             : {
    2781           0 :     TIFF *tif = (TIFF *)cinfo->client_data;
    2782             :     (void)num_bytes;
    2783           0 :     TIFFErrorExtR(tif, "LibJpeg", "Unexpected error");
    2784           0 :     jpeg_encap_unwind(tif);
    2785           0 : }
    2786             : 
    2787             : #ifdef _MSC_VER
    2788             : #pragma warning(push)
    2789             : #pragma warning(disable : 4702) /* unreachable code */
    2790             : #endif
    2791             : static boolean
    2792           0 : OJPEGLibjpegJpegSourceMgrResyncToRestart(jpeg_decompress_struct *cinfo,
    2793             :                                          int desired)
    2794             : {
    2795           0 :     TIFF *tif = (TIFF *)cinfo->client_data;
    2796             :     (void)desired;
    2797           0 :     TIFFErrorExtR(tif, "LibJpeg", "Unexpected error");
    2798           0 :     jpeg_encap_unwind(tif);
    2799           0 :     return (0);
    2800             : }
    2801             : #ifdef _MSC_VER
    2802             : #pragma warning(pop)
    2803             : #endif
    2804             : 
    2805           0 : static void OJPEGLibjpegJpegSourceMgrTermSource(jpeg_decompress_struct *cinfo)
    2806             : {
    2807             :     (void)cinfo;
    2808           0 : }
    2809             : 
    2810             : #endif

Generated by: LCOV version 1.14