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

Generated by: LCOV version 1.14