0N/A/*
0N/A * reserved comment block
0N/A * DO NOT REMOVE OR ALTER!
0N/A */
0N/A/*
0N/A * jdmarker.c
0N/A *
0N/A * Copyright (C) 1991-1998, Thomas G. Lane.
0N/A * This file is part of the Independent JPEG Group's software.
0N/A * For conditions of distribution and use, see the accompanying README file.
0N/A *
0N/A * This file contains routines to decode JPEG datastream markers.
0N/A * Most of the complexity arises from our desire to support input
0N/A * suspension: if not all of the data for a marker is available,
0N/A * we must exit back to the application. On resumption, we reprocess
0N/A * the marker.
0N/A */
0N/A
0N/A#define JPEG_INTERNALS
0N/A#include "jinclude.h"
0N/A#include "jpeglib.h"
0N/A
0N/A
0N/Atypedef enum { /* JPEG marker codes */
0N/A M_SOF0 = 0xc0,
0N/A M_SOF1 = 0xc1,
0N/A M_SOF2 = 0xc2,
0N/A M_SOF3 = 0xc3,
0N/A
0N/A M_SOF5 = 0xc5,
0N/A M_SOF6 = 0xc6,
0N/A M_SOF7 = 0xc7,
0N/A
0N/A M_JPG = 0xc8,
0N/A M_SOF9 = 0xc9,
0N/A M_SOF10 = 0xca,
0N/A M_SOF11 = 0xcb,
0N/A
0N/A M_SOF13 = 0xcd,
0N/A M_SOF14 = 0xce,
0N/A M_SOF15 = 0xcf,
0N/A
0N/A M_DHT = 0xc4,
0N/A
0N/A M_DAC = 0xcc,
0N/A
0N/A M_RST0 = 0xd0,
0N/A M_RST1 = 0xd1,
0N/A M_RST2 = 0xd2,
0N/A M_RST3 = 0xd3,
0N/A M_RST4 = 0xd4,
0N/A M_RST5 = 0xd5,
0N/A M_RST6 = 0xd6,
0N/A M_RST7 = 0xd7,
0N/A
0N/A M_SOI = 0xd8,
0N/A M_EOI = 0xd9,
0N/A M_SOS = 0xda,
0N/A M_DQT = 0xdb,
0N/A M_DNL = 0xdc,
0N/A M_DRI = 0xdd,
0N/A M_DHP = 0xde,
0N/A M_EXP = 0xdf,
0N/A
0N/A M_APP0 = 0xe0,
0N/A M_APP1 = 0xe1,
0N/A M_APP2 = 0xe2,
0N/A M_APP3 = 0xe3,
0N/A M_APP4 = 0xe4,
0N/A M_APP5 = 0xe5,
0N/A M_APP6 = 0xe6,
0N/A M_APP7 = 0xe7,
0N/A M_APP8 = 0xe8,
0N/A M_APP9 = 0xe9,
0N/A M_APP10 = 0xea,
0N/A M_APP11 = 0xeb,
0N/A M_APP12 = 0xec,
0N/A M_APP13 = 0xed,
0N/A M_APP14 = 0xee,
0N/A M_APP15 = 0xef,
0N/A
0N/A M_JPG0 = 0xf0,
0N/A M_JPG13 = 0xfd,
0N/A M_COM = 0xfe,
0N/A
0N/A M_TEM = 0x01,
0N/A
0N/A M_ERROR = 0x100
0N/A} JPEG_MARKER;
0N/A
0N/A
0N/A/* Private state */
0N/A
0N/Atypedef struct {
0N/A struct jpeg_marker_reader pub; /* public fields */
0N/A
0N/A /* Application-overridable marker processing methods */
0N/A jpeg_marker_parser_method process_COM;
0N/A jpeg_marker_parser_method process_APPn[16];
0N/A
0N/A /* Limit on marker data length to save for each marker type */
0N/A unsigned int length_limit_COM;
0N/A unsigned int length_limit_APPn[16];
0N/A
0N/A /* Status of COM/APPn marker saving */
0N/A jpeg_saved_marker_ptr cur_marker; /* NULL if not processing a marker */
0N/A unsigned int bytes_read; /* data bytes read so far in marker */
0N/A /* Note: cur_marker is not linked into marker_list until it's all read. */
0N/A} my_marker_reader;
0N/A
0N/Atypedef my_marker_reader * my_marker_ptr;
0N/A
0N/A
0N/A/*
0N/A * Macros for fetching data from the data source module.
0N/A *
0N/A * At all times, cinfo->src->next_input_byte and ->bytes_in_buffer reflect
0N/A * the current restart point; we update them only when we have reached a
0N/A * suitable place to restart if a suspension occurs.
0N/A */
0N/A
0N/A/* Declare and initialize local copies of input pointer/count */
0N/A#define INPUT_VARS(cinfo) \
0N/A struct jpeg_source_mgr * datasrc = (cinfo)->src; \
0N/A const JOCTET * next_input_byte = datasrc->next_input_byte; \
0N/A size_t bytes_in_buffer = datasrc->bytes_in_buffer
0N/A
0N/A/* Unload the local copies --- do this only at a restart boundary */
0N/A#define INPUT_SYNC(cinfo) \
0N/A ( datasrc->next_input_byte = next_input_byte, \
0N/A datasrc->bytes_in_buffer = bytes_in_buffer )
0N/A
0N/A/* Reload the local copies --- used only in MAKE_BYTE_AVAIL */
0N/A#define INPUT_RELOAD(cinfo) \
0N/A ( next_input_byte = datasrc->next_input_byte, \
0N/A bytes_in_buffer = datasrc->bytes_in_buffer )
0N/A
0N/A/* Internal macro for INPUT_BYTE and INPUT_2BYTES: make a byte available.
0N/A * Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
0N/A * but we must reload the local copies after a successful fill.
0N/A */
0N/A#define MAKE_BYTE_AVAIL(cinfo,action) \
0N/A if (bytes_in_buffer == 0) { \
0N/A if (! (*datasrc->fill_input_buffer) (cinfo)) \
0N/A { action; } \
0N/A INPUT_RELOAD(cinfo); \
0N/A }
0N/A
0N/A/* Read a byte into variable V.
0N/A * If must suspend, take the specified action (typically "return FALSE").
0N/A */
0N/A#define INPUT_BYTE(cinfo,V,action) \
0N/A MAKESTMT( MAKE_BYTE_AVAIL(cinfo,action); \
0N/A bytes_in_buffer--; \
0N/A V = GETJOCTET(*next_input_byte++); )
0N/A
0N/A/* As above, but read two bytes interpreted as an unsigned 16-bit integer.
0N/A * V should be declared unsigned int or perhaps INT32.
0N/A */
0N/A#define INPUT_2BYTES(cinfo,V,action) \
0N/A MAKESTMT( MAKE_BYTE_AVAIL(cinfo,action); \
0N/A bytes_in_buffer--; \
0N/A V = ((unsigned int) GETJOCTET(*next_input_byte++)) << 8; \
0N/A MAKE_BYTE_AVAIL(cinfo,action); \
0N/A bytes_in_buffer--; \
0N/A V += GETJOCTET(*next_input_byte++); )
0N/A
0N/A
0N/A/*
0N/A * Routines to process JPEG markers.
0N/A *
0N/A * Entry condition: JPEG marker itself has been read and its code saved
0N/A * in cinfo->unread_marker; input restart point is just after the marker.
0N/A *
0N/A * Exit: if return TRUE, have read and processed any parameters, and have
0N/A * updated the restart point to point after the parameters.
0N/A * If return FALSE, was forced to suspend before reaching end of
0N/A * marker parameters; restart point has not been moved. Same routine
0N/A * will be called again after application supplies more input data.
0N/A *
0N/A * This approach to suspension assumes that all of a marker's parameters
0N/A * can fit into a single input bufferload. This should hold for "normal"
0N/A * markers. Some COM/APPn markers might have large parameter segments
0N/A * that might not fit. If we are simply dropping such a marker, we use
0N/A * skip_input_data to get past it, and thereby put the problem on the
0N/A * source manager's shoulders. If we are saving the marker's contents
0N/A * into memory, we use a slightly different convention: when forced to
0N/A * suspend, the marker processor updates the restart point to the end of
0N/A * what it's consumed (ie, the end of the buffer) before returning FALSE.
0N/A * On resumption, cinfo->unread_marker still contains the marker code,
0N/A * but the data source will point to the next chunk of marker data.
0N/A * The marker processor must retain internal state to deal with this.
0N/A *
0N/A * Note that we don't bother to avoid duplicate trace messages if a
0N/A * suspension occurs within marker parameters. Other side effects
0N/A * require more care.
0N/A */
0N/A
0N/A
0N/ALOCAL(boolean)
0N/Aget_soi (j_decompress_ptr cinfo)
0N/A/* Process an SOI marker */
0N/A{
0N/A int i;
0N/A
0N/A TRACEMS(cinfo, 1, JTRC_SOI);
0N/A
0N/A if (cinfo->marker->saw_SOI)
0N/A ERREXIT(cinfo, JERR_SOI_DUPLICATE);
0N/A
0N/A /* Reset all parameters that are defined to be reset by SOI */
0N/A
0N/A for (i = 0; i < NUM_ARITH_TBLS; i++) {
0N/A cinfo->arith_dc_L[i] = 0;
0N/A cinfo->arith_dc_U[i] = 1;
0N/A cinfo->arith_ac_K[i] = 5;
0N/A }
0N/A cinfo->restart_interval = 0;
0N/A
0N/A /* Set initial assumptions for colorspace etc */
0N/A
0N/A cinfo->jpeg_color_space = JCS_UNKNOWN;
0N/A cinfo->CCIR601_sampling = FALSE; /* Assume non-CCIR sampling??? */
0N/A
0N/A cinfo->saw_JFIF_marker = FALSE;
0N/A cinfo->JFIF_major_version = 1; /* set default JFIF APP0 values */
0N/A cinfo->JFIF_minor_version = 1;
0N/A cinfo->density_unit = 0;
0N/A cinfo->X_density = 1;
0N/A cinfo->Y_density = 1;
0N/A cinfo->saw_Adobe_marker = FALSE;
0N/A cinfo->Adobe_transform = 0;
0N/A
0N/A cinfo->marker->saw_SOI = TRUE;
0N/A
0N/A return TRUE;
0N/A}
0N/A
0N/A
0N/ALOCAL(boolean)
0N/Aget_sof (j_decompress_ptr cinfo, boolean is_prog, boolean is_arith)
0N/A/* Process a SOFn marker */
0N/A{
0N/A INT32 length;
0N/A int c, ci;
0N/A jpeg_component_info * compptr;
0N/A INPUT_VARS(cinfo);
0N/A
0N/A cinfo->progressive_mode = is_prog;
0N/A cinfo->arith_code = is_arith;
0N/A
0N/A INPUT_2BYTES(cinfo, length, return FALSE);
0N/A
0N/A INPUT_BYTE(cinfo, cinfo->data_precision, return FALSE);
0N/A INPUT_2BYTES(cinfo, cinfo->image_height, return FALSE);
0N/A INPUT_2BYTES(cinfo, cinfo->image_width, return FALSE);
0N/A INPUT_BYTE(cinfo, cinfo->num_components, return FALSE);
0N/A
0N/A length -= 8;
0N/A
0N/A TRACEMS4(cinfo, 1, JTRC_SOF, cinfo->unread_marker,
0N/A (int) cinfo->image_width, (int) cinfo->image_height,
0N/A cinfo->num_components);
0N/A
0N/A if (cinfo->marker->saw_SOF)
0N/A ERREXIT(cinfo, JERR_SOF_DUPLICATE);
0N/A
0N/A /* We don't support files in which the image height is initially specified */
0N/A /* as 0 and is later redefined by DNL. As long as we have to check that, */
0N/A /* might as well have a general sanity check. */
0N/A if (cinfo->image_height <= 0 || cinfo->image_width <= 0
0N/A || cinfo->num_components <= 0)
0N/A ERREXIT(cinfo, JERR_EMPTY_IMAGE);
0N/A
0N/A if (length != (cinfo->num_components * 3))
0N/A ERREXIT(cinfo, JERR_BAD_LENGTH);
0N/A
0N/A if (cinfo->comp_info == NULL) { /* do only once, even if suspend */
0N/A cinfo->comp_info = (jpeg_component_info *) (*cinfo->mem->alloc_small)
0N/A ((j_common_ptr) cinfo, JPOOL_IMAGE,
0N/A cinfo->num_components * SIZEOF(jpeg_component_info));
0N/A MEMZERO(cinfo->comp_info,
0N/A cinfo->num_components * SIZEOF(jpeg_component_info));
0N/A }
0N/A
0N/A for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
0N/A ci++, compptr++) {
0N/A compptr->component_index = ci;
0N/A INPUT_BYTE(cinfo, compptr->component_id, return FALSE);
0N/A INPUT_BYTE(cinfo, c, return FALSE);
0N/A compptr->h_samp_factor = (c >> 4) & 15;
0N/A compptr->v_samp_factor = (c ) & 15;
0N/A INPUT_BYTE(cinfo, compptr->quant_tbl_no, return FALSE);
0N/A
0N/A TRACEMS4(cinfo, 1, JTRC_SOF_COMPONENT,
0N/A compptr->component_id, compptr->h_samp_factor,
0N/A compptr->v_samp_factor, compptr->quant_tbl_no);
0N/A }
0N/A
0N/A cinfo->marker->saw_SOF = TRUE;
0N/A
0N/A INPUT_SYNC(cinfo);
0N/A return TRUE;
0N/A}
0N/A
0N/A
0N/ALOCAL(boolean)
0N/Aget_sos (j_decompress_ptr cinfo)
0N/A/* Process a SOS marker */
0N/A{
0N/A INT32 length;
0N/A int i, ci, n, c, cc;
0N/A jpeg_component_info * compptr;
0N/A INPUT_VARS(cinfo);
0N/A
0N/A if (! cinfo->marker->saw_SOF)
0N/A ERREXIT(cinfo, JERR_SOS_NO_SOF);
0N/A
0N/A INPUT_2BYTES(cinfo, length, return FALSE);
0N/A
0N/A INPUT_BYTE(cinfo, n, return FALSE); /* Number of components */
0N/A
0N/A TRACEMS1(cinfo, 1, JTRC_SOS, n);
0N/A
0N/A if (length != (n * 2 + 6) || n < 1 || n > MAX_COMPS_IN_SCAN)
0N/A ERREXIT(cinfo, JERR_BAD_LENGTH);
0N/A
0N/A cinfo->comps_in_scan = n;
0N/A
0N/A /* Collect the component-spec parameters */
0N/A
0N/A for (i = 0; i < n; i++) {
0N/A INPUT_BYTE(cinfo, cc, return FALSE);
0N/A INPUT_BYTE(cinfo, c, return FALSE);
0N/A
0N/A for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
0N/A ci++, compptr++) {
0N/A if (cc == compptr->component_id)
0N/A goto id_found;
0N/A }
0N/A
0N/A ERREXIT1(cinfo, JERR_BAD_COMPONENT_ID, cc);
0N/A
0N/A id_found:
0N/A
0N/A cinfo->cur_comp_info[i] = compptr;
0N/A compptr->dc_tbl_no = (c >> 4) & 15;
0N/A compptr->ac_tbl_no = (c ) & 15;
0N/A
0N/A TRACEMS3(cinfo, 1, JTRC_SOS_COMPONENT, cc,
0N/A compptr->dc_tbl_no, compptr->ac_tbl_no);
0N/A }
0N/A
0N/A /* Collect the additional scan parameters Ss, Se, Ah/Al. */
0N/A INPUT_BYTE(cinfo, c, return FALSE);
0N/A cinfo->Ss = c;
0N/A INPUT_BYTE(cinfo, c, return FALSE);
0N/A cinfo->Se = c;
0N/A INPUT_BYTE(cinfo, c, return FALSE);
0N/A cinfo->Ah = (c >> 4) & 15;
0N/A cinfo->Al = (c ) & 15;
0N/A
0N/A TRACEMS4(cinfo, 1, JTRC_SOS_PARAMS, cinfo->Ss, cinfo->Se,
0N/A cinfo->Ah, cinfo->Al);
0N/A
0N/A /* Prepare to scan data & restart markers */
0N/A cinfo->marker->next_restart_num = 0;
0N/A
0N/A /* Count another SOS marker */
0N/A cinfo->input_scan_number++;
0N/A
0N/A INPUT_SYNC(cinfo);
0N/A return TRUE;
0N/A}
0N/A
0N/A
0N/A#ifdef D_ARITH_CODING_SUPPORTED
0N/A
0N/ALOCAL(boolean)
0N/Aget_dac (j_decompress_ptr cinfo)
0N/A/* Process a DAC marker */
0N/A{
0N/A INT32 length;
0N/A int index, val;
0N/A INPUT_VARS(cinfo);
0N/A
0N/A INPUT_2BYTES(cinfo, length, return FALSE);
0N/A length -= 2;
0N/A
0N/A while (length > 0) {
0N/A INPUT_BYTE(cinfo, index, return FALSE);
0N/A INPUT_BYTE(cinfo, val, return FALSE);
0N/A
0N/A length -= 2;
0N/A
0N/A TRACEMS2(cinfo, 1, JTRC_DAC, index, val);
0N/A
0N/A if (index < 0 || index >= (2*NUM_ARITH_TBLS))
0N/A ERREXIT1(cinfo, JERR_DAC_INDEX, index);
0N/A
0N/A if (index >= NUM_ARITH_TBLS) { /* define AC table */
0N/A cinfo->arith_ac_K[index-NUM_ARITH_TBLS] = (UINT8) val;
0N/A } else { /* define DC table */
0N/A cinfo->arith_dc_L[index] = (UINT8) (val & 0x0F);
0N/A cinfo->arith_dc_U[index] = (UINT8) (val >> 4);
0N/A if (cinfo->arith_dc_L[index] > cinfo->arith_dc_U[index])
0N/A ERREXIT1(cinfo, JERR_DAC_VALUE, val);
0N/A }
0N/A }
0N/A
0N/A if (length != 0)
0N/A ERREXIT(cinfo, JERR_BAD_LENGTH);
0N/A
0N/A INPUT_SYNC(cinfo);
0N/A return TRUE;
0N/A}
0N/A
0N/A#else /* ! D_ARITH_CODING_SUPPORTED */
0N/A
0N/A#define get_dac(cinfo) skip_variable(cinfo)
0N/A
0N/A#endif /* D_ARITH_CODING_SUPPORTED */
0N/A
0N/A
0N/ALOCAL(boolean)
0N/Aget_dht (j_decompress_ptr cinfo)
0N/A/* Process a DHT marker */
0N/A{
0N/A INT32 length;
0N/A UINT8 bits[17];
0N/A UINT8 huffval[256];
0N/A int i, index, count;
0N/A JHUFF_TBL **htblptr;
0N/A INPUT_VARS(cinfo);
0N/A
0N/A INPUT_2BYTES(cinfo, length, return FALSE);
0N/A length -= 2;
0N/A
0N/A while (length > 16) {
0N/A INPUT_BYTE(cinfo, index, return FALSE);
0N/A
0N/A TRACEMS1(cinfo, 1, JTRC_DHT, index);
0N/A
0N/A bits[0] = 0;
0N/A count = 0;
0N/A for (i = 1; i <= 16; i++) {
0N/A INPUT_BYTE(cinfo, bits[i], return FALSE);
0N/A count += bits[i];
0N/A }
0N/A
0N/A length -= 1 + 16;
0N/A
0N/A TRACEMS8(cinfo, 2, JTRC_HUFFBITS,
0N/A bits[1], bits[2], bits[3], bits[4],
0N/A bits[5], bits[6], bits[7], bits[8]);
0N/A TRACEMS8(cinfo, 2, JTRC_HUFFBITS,
0N/A bits[9], bits[10], bits[11], bits[12],
0N/A bits[13], bits[14], bits[15], bits[16]);
0N/A
0N/A /* Here we just do minimal validation of the counts to avoid walking
0N/A * off the end of our table space. jdhuff.c will check more carefully.
0N/A */
0N/A if (count > 256 || ((INT32) count) > length)
0N/A ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
0N/A
0N/A for (i = 0; i < count; i++)
0N/A INPUT_BYTE(cinfo, huffval[i], return FALSE);
0N/A
0N/A length -= count;
0N/A
0N/A if (index & 0x10) { /* AC table definition */
0N/A index -= 0x10;
0N/A htblptr = &cinfo->ac_huff_tbl_ptrs[index];
0N/A } else { /* DC table definition */
0N/A htblptr = &cinfo->dc_huff_tbl_ptrs[index];
0N/A }
0N/A
0N/A if (index < 0 || index >= NUM_HUFF_TBLS)
0N/A ERREXIT1(cinfo, JERR_DHT_INDEX, index);
0N/A
0N/A if (*htblptr == NULL)
0N/A *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
0N/A
0N/A MEMCOPY((*htblptr)->bits, bits, SIZEOF((*htblptr)->bits));
0N/A MEMCOPY((*htblptr)->huffval, huffval, SIZEOF((*htblptr)->huffval));
0N/A }
0N/A
0N/A if (length != 0)
0N/A ERREXIT(cinfo, JERR_BAD_LENGTH);
0N/A
0N/A INPUT_SYNC(cinfo);
0N/A return TRUE;
0N/A}
0N/A
0N/A
0N/ALOCAL(boolean)
0N/Aget_dqt (j_decompress_ptr cinfo)
0N/A/* Process a DQT marker */
0N/A{
0N/A INT32 length;
0N/A int n, i, prec;
0N/A unsigned int tmp;
0N/A JQUANT_TBL *quant_ptr;
0N/A INPUT_VARS(cinfo);
0N/A
0N/A INPUT_2BYTES(cinfo, length, return FALSE);
0N/A length -= 2;
0N/A
0N/A while (length > 0) {
0N/A INPUT_BYTE(cinfo, n, return FALSE);
0N/A prec = n >> 4;
0N/A n &= 0x0F;
0N/A
0N/A TRACEMS2(cinfo, 1, JTRC_DQT, n, prec);
0N/A
0N/A if (n >= NUM_QUANT_TBLS)
0N/A ERREXIT1(cinfo, JERR_DQT_INDEX, n);
0N/A
0N/A if (cinfo->quant_tbl_ptrs[n] == NULL)
0N/A cinfo->quant_tbl_ptrs[n] = jpeg_alloc_quant_table((j_common_ptr) cinfo);
0N/A quant_ptr = cinfo->quant_tbl_ptrs[n];
0N/A
0N/A for (i = 0; i < DCTSIZE2; i++) {
0N/A if (prec)
0N/A INPUT_2BYTES(cinfo, tmp, return FALSE);
0N/A else
0N/A INPUT_BYTE(cinfo, tmp, return FALSE);
0N/A /* We convert the zigzag-order table to natural array order. */
0N/A quant_ptr->quantval[jpeg_natural_order[i]] = (UINT16) tmp;
0N/A }
0N/A
0N/A if (cinfo->err->trace_level >= 2) {
0N/A for (i = 0; i < DCTSIZE2; i += 8) {
0N/A TRACEMS8(cinfo, 2, JTRC_QUANTVALS,
0N/A quant_ptr->quantval[i], quant_ptr->quantval[i+1],
0N/A quant_ptr->quantval[i+2], quant_ptr->quantval[i+3],
0N/A quant_ptr->quantval[i+4], quant_ptr->quantval[i+5],
0N/A quant_ptr->quantval[i+6], quant_ptr->quantval[i+7]);
0N/A }
0N/A }
0N/A
0N/A length -= DCTSIZE2+1;
0N/A if (prec) length -= DCTSIZE2;
0N/A }
0N/A
0N/A if (length != 0)
0N/A ERREXIT(cinfo, JERR_BAD_LENGTH);
0N/A
0N/A INPUT_SYNC(cinfo);
0N/A return TRUE;
0N/A}
0N/A
0N/A
0N/ALOCAL(boolean)
0N/Aget_dri (j_decompress_ptr cinfo)
0N/A/* Process a DRI marker */
0N/A{
0N/A INT32 length;
0N/A unsigned int tmp;
0N/A INPUT_VARS(cinfo);
0N/A
0N/A INPUT_2BYTES(cinfo, length, return FALSE);
0N/A
0N/A if (length != 4)
0N/A ERREXIT(cinfo, JERR_BAD_LENGTH);
0N/A
0N/A INPUT_2BYTES(cinfo, tmp, return FALSE);
0N/A
0N/A TRACEMS1(cinfo, 1, JTRC_DRI, tmp);
0N/A
0N/A cinfo->restart_interval = tmp;
0N/A
0N/A INPUT_SYNC(cinfo);
0N/A return TRUE;
0N/A}
0N/A
0N/A
0N/A/*
0N/A * Routines for processing APPn and COM markers.
0N/A * These are either saved in memory or discarded, per application request.
0N/A * APP0 and APP14 are specially checked to see if they are
0N/A * JFIF and Adobe markers, respectively.
0N/A */
0N/A
0N/A#define APP0_DATA_LEN 14 /* Length of interesting data in APP0 */
0N/A#define APP14_DATA_LEN 12 /* Length of interesting data in APP14 */
0N/A#define APPN_DATA_LEN 14 /* Must be the largest of the above!! */
0N/A
0N/A
0N/ALOCAL(void)
0N/Aexamine_app0 (j_decompress_ptr cinfo, JOCTET FAR * data,
0N/A unsigned int datalen, INT32 remaining)
0N/A/* Examine first few bytes from an APP0.
0N/A * Take appropriate action if it is a JFIF marker.
0N/A * datalen is # of bytes at data[], remaining is length of rest of marker data.
0N/A */
0N/A{
0N/A INT32 totallen = (INT32) datalen + remaining;
0N/A
0N/A if (datalen >= APP0_DATA_LEN &&
0N/A GETJOCTET(data[0]) == 0x4A &&
0N/A GETJOCTET(data[1]) == 0x46 &&
0N/A GETJOCTET(data[2]) == 0x49 &&
0N/A GETJOCTET(data[3]) == 0x46 &&
0N/A GETJOCTET(data[4]) == 0) {
0N/A /* Found JFIF APP0 marker: save info */
0N/A cinfo->saw_JFIF_marker = TRUE;
0N/A cinfo->JFIF_major_version = GETJOCTET(data[5]);
0N/A cinfo->JFIF_minor_version = GETJOCTET(data[6]);
0N/A cinfo->density_unit = GETJOCTET(data[7]);
0N/A cinfo->X_density = (GETJOCTET(data[8]) << 8) + GETJOCTET(data[9]);
0N/A cinfo->Y_density = (GETJOCTET(data[10]) << 8) + GETJOCTET(data[11]);
0N/A /* Check version.
0N/A * Major version must be 1, anything else signals an incompatible change.
0N/A * (We used to treat this as an error, but now it's a nonfatal warning,
0N/A * because some bozo at Hijaak couldn't read the spec.)
0N/A * Minor version should be 0..2, but process anyway if newer.
0N/A */
0N/A if (cinfo->JFIF_major_version != 1)
0N/A WARNMS2(cinfo, JWRN_JFIF_MAJOR,
0N/A cinfo->JFIF_major_version, cinfo->JFIF_minor_version);
0N/A /* Generate trace messages */
0N/A TRACEMS5(cinfo, 1, JTRC_JFIF,
0N/A cinfo->JFIF_major_version, cinfo->JFIF_minor_version,
0N/A cinfo->X_density, cinfo->Y_density, cinfo->density_unit);
0N/A /* Validate thumbnail dimensions and issue appropriate messages */
0N/A if (GETJOCTET(data[12]) | GETJOCTET(data[13]))
0N/A TRACEMS2(cinfo, 1, JTRC_JFIF_THUMBNAIL,
0N/A GETJOCTET(data[12]), GETJOCTET(data[13]));
0N/A totallen -= APP0_DATA_LEN;
0N/A if (totallen !=
0N/A ((INT32)GETJOCTET(data[12]) * (INT32)GETJOCTET(data[13]) * (INT32) 3))
0N/A TRACEMS1(cinfo, 1, JTRC_JFIF_BADTHUMBNAILSIZE, (int) totallen);
0N/A } else if (datalen >= 6 &&
0N/A GETJOCTET(data[0]) == 0x4A &&
0N/A GETJOCTET(data[1]) == 0x46 &&
0N/A GETJOCTET(data[2]) == 0x58 &&
0N/A GETJOCTET(data[3]) == 0x58 &&
0N/A GETJOCTET(data[4]) == 0) {
0N/A /* Found JFIF "JFXX" extension APP0 marker */
0N/A /* The library doesn't actually do anything with these,
0N/A * but we try to produce a helpful trace message.
0N/A */
0N/A switch (GETJOCTET(data[5])) {
0N/A case 0x10:
0N/A TRACEMS1(cinfo, 1, JTRC_THUMB_JPEG, (int) totallen);
0N/A break;
0N/A case 0x11:
0N/A TRACEMS1(cinfo, 1, JTRC_THUMB_PALETTE, (int) totallen);
0N/A break;
0N/A case 0x13:
0N/A TRACEMS1(cinfo, 1, JTRC_THUMB_RGB, (int) totallen);
0N/A break;
0N/A default:
0N/A TRACEMS2(cinfo, 1, JTRC_JFIF_EXTENSION,
0N/A GETJOCTET(data[5]), (int) totallen);
0N/A break;
0N/A }
0N/A } else {
0N/A /* Start of APP0 does not match "JFIF" or "JFXX", or too short */
0N/A TRACEMS1(cinfo, 1, JTRC_APP0, (int) totallen);
0N/A
0N/A /*
0N/A * In this case we have seen the APP0 marker but the remaining
0N/A * APP0 section may be corrupt. Regardless, we will set the
0N/A * saw_JFIF_marker flag as it is important for making the
0N/A * correct choice of JPEG color space later (we will assume
0N/A * YCbCr in this case). The version and density fields will
0N/A * contain default values, which should be sufficient for our needs.
0N/A */
0N/A cinfo->saw_JFIF_marker = TRUE;
0N/A }
0N/A}
0N/A
0N/A
0N/ALOCAL(void)
0N/Aexamine_app14 (j_decompress_ptr cinfo, JOCTET FAR * data,
0N/A unsigned int datalen, INT32 remaining)
0N/A/* Examine first few bytes from an APP14.
0N/A * Take appropriate action if it is an Adobe marker.
0N/A * datalen is # of bytes at data[], remaining is length of rest of marker data.
0N/A */
0N/A{
0N/A unsigned int version, flags0, flags1, transform;
0N/A
0N/A if (datalen >= APP14_DATA_LEN &&
0N/A GETJOCTET(data[0]) == 0x41 &&
0N/A GETJOCTET(data[1]) == 0x64 &&
0N/A GETJOCTET(data[2]) == 0x6F &&
0N/A GETJOCTET(data[3]) == 0x62 &&
0N/A GETJOCTET(data[4]) == 0x65) {
0N/A /* Found Adobe APP14 marker */
0N/A version = (GETJOCTET(data[5]) << 8) + GETJOCTET(data[6]);
0N/A flags0 = (GETJOCTET(data[7]) << 8) + GETJOCTET(data[8]);
0N/A flags1 = (GETJOCTET(data[9]) << 8) + GETJOCTET(data[10]);
0N/A transform = GETJOCTET(data[11]);
0N/A TRACEMS4(cinfo, 1, JTRC_ADOBE, version, flags0, flags1, transform);
0N/A cinfo->saw_Adobe_marker = TRUE;
0N/A cinfo->Adobe_transform = (UINT8) transform;
0N/A } else {
0N/A /* Start of APP14 does not match "Adobe", or too short */
0N/A TRACEMS1(cinfo, 1, JTRC_APP14, (int) (datalen + remaining));
0N/A }
0N/A}
0N/A
0N/A
0N/AMETHODDEF(boolean)
0N/Aget_interesting_appn (j_decompress_ptr cinfo)
0N/A/* Process an APP0 or APP14 marker without saving it */
0N/A{
0N/A INT32 length;
0N/A JOCTET b[APPN_DATA_LEN];
0N/A unsigned int i, numtoread;
0N/A INPUT_VARS(cinfo);
0N/A
0N/A INPUT_2BYTES(cinfo, length, return FALSE);
0N/A length -= 2;
0N/A
0N/A /* get the interesting part of the marker data */
0N/A if (length >= APPN_DATA_LEN)
0N/A numtoread = APPN_DATA_LEN;
0N/A else if (length > 0)
0N/A numtoread = (unsigned int) length;
0N/A else
0N/A numtoread = 0;
0N/A for (i = 0; i < numtoread; i++)
0N/A INPUT_BYTE(cinfo, b[i], return FALSE);
0N/A length -= numtoread;
0N/A
0N/A /* process it */
0N/A switch (cinfo->unread_marker) {
0N/A case M_APP0:
0N/A examine_app0(cinfo, (JOCTET FAR *) b, numtoread, length);
0N/A break;
0N/A case M_APP14:
0N/A examine_app14(cinfo, (JOCTET FAR *) b, numtoread, length);
0N/A break;
0N/A default:
0N/A /* can't get here unless jpeg_save_markers chooses wrong processor */
0N/A ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, cinfo->unread_marker);
0N/A break;
0N/A }
0N/A
0N/A /* skip any remaining data -- could be lots */
0N/A INPUT_SYNC(cinfo);
0N/A if (length > 0)
0N/A (*cinfo->src->skip_input_data) (cinfo, (long) length);
0N/A
0N/A return TRUE;
0N/A}
0N/A
0N/A
0N/A#ifdef SAVE_MARKERS_SUPPORTED
0N/A
0N/AMETHODDEF(boolean)
0N/Asave_marker (j_decompress_ptr cinfo)
0N/A/* Save an APPn or COM marker into the marker list */
0N/A{
0N/A my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
0N/A jpeg_saved_marker_ptr cur_marker = marker->cur_marker;
0N/A unsigned int bytes_read, data_length;
0N/A JOCTET FAR * data;
0N/A INT32 length = 0;
0N/A INPUT_VARS(cinfo);
0N/A
0N/A if (cur_marker == NULL) {
0N/A /* begin reading a marker */
0N/A INPUT_2BYTES(cinfo, length, return FALSE);
0N/A length -= 2;
0N/A if (length >= 0) { /* watch out for bogus length word */
0N/A /* figure out how much we want to save */
0N/A unsigned int limit;
0N/A if (cinfo->unread_marker == (int) M_COM)
0N/A limit = marker->length_limit_COM;
0N/A else
0N/A limit = marker->length_limit_APPn[cinfo->unread_marker - (int) M_APP0];
0N/A if ((unsigned int) length < limit)
0N/A limit = (unsigned int) length;
0N/A /* allocate and initialize the marker item */
0N/A cur_marker = (jpeg_saved_marker_ptr)
0N/A (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
0N/A SIZEOF(struct jpeg_marker_struct) + limit);
0N/A cur_marker->next = NULL;
0N/A cur_marker->marker = (UINT8) cinfo->unread_marker;
0N/A cur_marker->original_length = (unsigned int) length;
0N/A cur_marker->data_length = limit;
0N/A /* data area is just beyond the jpeg_marker_struct */
0N/A data = cur_marker->data = (JOCTET FAR *) (cur_marker + 1);
0N/A marker->cur_marker = cur_marker;
0N/A marker->bytes_read = 0;
0N/A bytes_read = 0;
0N/A data_length = limit;
0N/A } else {
0N/A /* deal with bogus length word */
0N/A bytes_read = data_length = 0;
0N/A data = NULL;
0N/A }
0N/A } else {
0N/A /* resume reading a marker */
0N/A bytes_read = marker->bytes_read;
0N/A data_length = cur_marker->data_length;
0N/A data = cur_marker->data + bytes_read;
0N/A }
0N/A
0N/A while (bytes_read < data_length) {
0N/A INPUT_SYNC(cinfo); /* move the restart point to here */
0N/A marker->bytes_read = bytes_read;
0N/A /* If there's not at least one byte in buffer, suspend */
0N/A MAKE_BYTE_AVAIL(cinfo, return FALSE);
0N/A /* Copy bytes with reasonable rapidity */
0N/A while (bytes_read < data_length && bytes_in_buffer > 0) {
0N/A *data++ = *next_input_byte++;
0N/A bytes_in_buffer--;
0N/A bytes_read++;
0N/A }
0N/A }
0N/A
0N/A /* Done reading what we want to read */
0N/A if (cur_marker != NULL) { /* will be NULL if bogus length word */
0N/A /* Add new marker to end of list */
0N/A if (cinfo->marker_list == NULL) {
0N/A cinfo->marker_list = cur_marker;
0N/A } else {
0N/A jpeg_saved_marker_ptr prev = cinfo->marker_list;
0N/A while (prev->next != NULL)
0N/A prev = prev->next;
0N/A prev->next = cur_marker;
0N/A }
0N/A /* Reset pointer & calc remaining data length */
0N/A data = cur_marker->data;
0N/A length = cur_marker->original_length - data_length;
0N/A }
0N/A /* Reset to initial state for next marker */
0N/A marker->cur_marker = NULL;
0N/A
0N/A /* Process the marker if interesting; else just make a generic trace msg */
0N/A switch (cinfo->unread_marker) {
0N/A case M_APP0:
0N/A examine_app0(cinfo, data, data_length, length);
0N/A break;
0N/A case M_APP14:
0N/A examine_app14(cinfo, data, data_length, length);
0N/A break;
0N/A default:
0N/A TRACEMS2(cinfo, 1, JTRC_MISC_MARKER, cinfo->unread_marker,
0N/A (int) (data_length + length));
0N/A break;
0N/A }
0N/A
0N/A /* skip any remaining data -- could be lots */
0N/A INPUT_SYNC(cinfo); /* do before skip_input_data */
0N/A if (length > 0)
0N/A (*cinfo->src->skip_input_data) (cinfo, (long) length);
0N/A
0N/A return TRUE;
0N/A}
0N/A
0N/A#endif /* SAVE_MARKERS_SUPPORTED */
0N/A
0N/A
0N/AMETHODDEF(boolean)
0N/Askip_variable (j_decompress_ptr cinfo)
0N/A/* Skip over an unknown or uninteresting variable-length marker */
0N/A{
0N/A INT32 length;
0N/A INPUT_VARS(cinfo);
0N/A
0N/A INPUT_2BYTES(cinfo, length, return FALSE);
0N/A length -= 2;
0N/A
0N/A TRACEMS2(cinfo, 1, JTRC_MISC_MARKER, cinfo->unread_marker, (int) length);
0N/A
0N/A INPUT_SYNC(cinfo); /* do before skip_input_data */
0N/A if (length > 0)
0N/A (*cinfo->src->skip_input_data) (cinfo, (long) length);
0N/A
0N/A return TRUE;
0N/A}
0N/A
0N/A
0N/A/*
0N/A * Find the next JPEG marker, save it in cinfo->unread_marker.
0N/A * Returns FALSE if had to suspend before reaching a marker;
0N/A * in that case cinfo->unread_marker is unchanged.
0N/A *
0N/A * Note that the result might not be a valid marker code,
0N/A * but it will never be 0 or FF.
0N/A */
0N/A
0N/ALOCAL(boolean)
0N/Anext_marker (j_decompress_ptr cinfo)
0N/A{
0N/A int c;
0N/A INPUT_VARS(cinfo);
0N/A
0N/A for (;;) {
0N/A INPUT_BYTE(cinfo, c, return FALSE);
0N/A /* Skip any non-FF bytes.
0N/A * This may look a bit inefficient, but it will not occur in a valid file.
0N/A * We sync after each discarded byte so that a suspending data source
0N/A * can discard the byte from its buffer.
0N/A */
0N/A while (c != 0xFF) {
0N/A cinfo->marker->discarded_bytes++;
0N/A INPUT_SYNC(cinfo);
0N/A INPUT_BYTE(cinfo, c, return FALSE);
0N/A }
0N/A /* This loop swallows any duplicate FF bytes. Extra FFs are legal as
0N/A * pad bytes, so don't count them in discarded_bytes. We assume there
0N/A * will not be so many consecutive FF bytes as to overflow a suspending
0N/A * data source's input buffer.
0N/A */
0N/A do {
0N/A INPUT_BYTE(cinfo, c, return FALSE);
0N/A } while (c == 0xFF);
0N/A if (c != 0)
0N/A break; /* found a valid marker, exit loop */
0N/A /* Reach here if we found a stuffed-zero data sequence (FF/00).
0N/A * Discard it and loop back to try again.
0N/A */
0N/A cinfo->marker->discarded_bytes += 2;
0N/A INPUT_SYNC(cinfo);
0N/A }
0N/A
0N/A if (cinfo->marker->discarded_bytes != 0) {
0N/A WARNMS2(cinfo, JWRN_EXTRANEOUS_DATA, cinfo->marker->discarded_bytes, c);
0N/A cinfo->marker->discarded_bytes = 0;
0N/A }
0N/A
0N/A cinfo->unread_marker = c;
0N/A
0N/A INPUT_SYNC(cinfo);
0N/A return TRUE;
0N/A}
0N/A
0N/A
0N/ALOCAL(boolean)
0N/Afirst_marker (j_decompress_ptr cinfo)
0N/A/* Like next_marker, but used to obtain the initial SOI marker. */
0N/A/* For this marker, we do not allow preceding garbage or fill; otherwise,
0N/A * we might well scan an entire input file before realizing it ain't JPEG.
0N/A * If an application wants to process non-JFIF files, it must seek to the
0N/A * SOI before calling the JPEG library.
0N/A */
0N/A{
0N/A int c, c2;
0N/A INPUT_VARS(cinfo);
0N/A
0N/A INPUT_BYTE(cinfo, c, return FALSE);
0N/A INPUT_BYTE(cinfo, c2, return FALSE);
0N/A if (c != 0xFF || c2 != (int) M_SOI)
0N/A ERREXIT2(cinfo, JERR_NO_SOI, c, c2);
0N/A
0N/A cinfo->unread_marker = c2;
0N/A
0N/A INPUT_SYNC(cinfo);
0N/A return TRUE;
0N/A}
0N/A
0N/A
0N/A/*
0N/A * Read markers until SOS or EOI.
0N/A *
0N/A * Returns same codes as are defined for jpeg_consume_input:
0N/A * JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.
0N/A */
0N/A
0N/AMETHODDEF(int)
0N/Aread_markers (j_decompress_ptr cinfo)
0N/A{
0N/A /* Outer loop repeats once for each marker. */
0N/A for (;;) {
0N/A /* Collect the marker proper, unless we already did. */
0N/A /* NB: first_marker() enforces the requirement that SOI appear first. */
0N/A if (cinfo->unread_marker == 0) {
0N/A if (! cinfo->marker->saw_SOI) {
0N/A if (! first_marker(cinfo))
0N/A return JPEG_SUSPENDED;
0N/A } else {
0N/A if (! next_marker(cinfo))
0N/A return JPEG_SUSPENDED;
0N/A }
0N/A }
0N/A /* At this point cinfo->unread_marker contains the marker code and the
0N/A * input point is just past the marker proper, but before any parameters.
0N/A * A suspension will cause us to return with this state still true.
0N/A */
0N/A switch (cinfo->unread_marker) {
0N/A case M_SOI:
0N/A if (! get_soi(cinfo))
0N/A return JPEG_SUSPENDED;
0N/A break;
0N/A
0N/A case M_SOF0: /* Baseline */
0N/A case M_SOF1: /* Extended sequential, Huffman */
0N/A if (! get_sof(cinfo, FALSE, FALSE))
0N/A return JPEG_SUSPENDED;
0N/A break;
0N/A
0N/A case M_SOF2: /* Progressive, Huffman */
0N/A if (! get_sof(cinfo, TRUE, FALSE))
0N/A return JPEG_SUSPENDED;
0N/A break;
0N/A
0N/A case M_SOF9: /* Extended sequential, arithmetic */
0N/A if (! get_sof(cinfo, FALSE, TRUE))
0N/A return JPEG_SUSPENDED;
0N/A break;
0N/A
0N/A case M_SOF10: /* Progressive, arithmetic */
0N/A if (! get_sof(cinfo, TRUE, TRUE))
0N/A return JPEG_SUSPENDED;
0N/A break;
0N/A
0N/A /* Currently unsupported SOFn types */
0N/A case M_SOF3: /* Lossless, Huffman */
0N/A case M_SOF5: /* Differential sequential, Huffman */
0N/A case M_SOF6: /* Differential progressive, Huffman */
0N/A case M_SOF7: /* Differential lossless, Huffman */
0N/A case M_JPG: /* Reserved for JPEG extensions */
0N/A case M_SOF11: /* Lossless, arithmetic */
0N/A case M_SOF13: /* Differential sequential, arithmetic */
0N/A case M_SOF14: /* Differential progressive, arithmetic */
0N/A case M_SOF15: /* Differential lossless, arithmetic */
0N/A ERREXIT1(cinfo, JERR_SOF_UNSUPPORTED, cinfo->unread_marker);
0N/A break;
0N/A
0N/A case M_SOS:
0N/A if (! get_sos(cinfo))
0N/A return JPEG_SUSPENDED;
0N/A cinfo->unread_marker = 0; /* processed the marker */
0N/A return JPEG_REACHED_SOS;
0N/A
0N/A case M_EOI:
0N/A TRACEMS(cinfo, 1, JTRC_EOI);
0N/A cinfo->unread_marker = 0; /* processed the marker */
0N/A return JPEG_REACHED_EOI;
0N/A
0N/A case M_DAC:
0N/A if (! get_dac(cinfo))
0N/A return JPEG_SUSPENDED;
0N/A break;
0N/A
0N/A case M_DHT:
0N/A if (! get_dht(cinfo))
0N/A return JPEG_SUSPENDED;
0N/A break;
0N/A
0N/A case M_DQT:
0N/A if (! get_dqt(cinfo))
0N/A return JPEG_SUSPENDED;
0N/A break;
0N/A
0N/A case M_DRI:
0N/A if (! get_dri(cinfo))
0N/A return JPEG_SUSPENDED;
0N/A break;
0N/A
0N/A case M_APP0:
0N/A case M_APP1:
0N/A case M_APP2:
0N/A case M_APP3:
0N/A case M_APP4:
0N/A case M_APP5:
0N/A case M_APP6:
0N/A case M_APP7:
0N/A case M_APP8:
0N/A case M_APP9:
0N/A case M_APP10:
0N/A case M_APP11:
0N/A case M_APP12:
0N/A case M_APP13:
0N/A case M_APP14:
0N/A case M_APP15:
0N/A if (! (*((my_marker_ptr) cinfo->marker)->process_APPn[
0N/A cinfo->unread_marker - (int) M_APP0]) (cinfo))
0N/A return JPEG_SUSPENDED;
0N/A break;
0N/A
0N/A case M_COM:
0N/A if (! (*((my_marker_ptr) cinfo->marker)->process_COM) (cinfo))
0N/A return JPEG_SUSPENDED;
0N/A break;
0N/A
0N/A case M_RST0: /* these are all parameterless */
0N/A case M_RST1:
0N/A case M_RST2:
0N/A case M_RST3:
0N/A case M_RST4:
0N/A case M_RST5:
0N/A case M_RST6:
0N/A case M_RST7:
0N/A case M_TEM:
0N/A TRACEMS1(cinfo, 1, JTRC_PARMLESS_MARKER, cinfo->unread_marker);
0N/A break;
0N/A
0N/A case M_DNL: /* Ignore DNL ... perhaps the wrong thing */
0N/A if (! skip_variable(cinfo))
0N/A return JPEG_SUSPENDED;
0N/A break;
0N/A
0N/A default: /* must be DHP, EXP, JPGn, or RESn */
0N/A /* For now, we treat the reserved markers as fatal errors since they are
0N/A * likely to be used to signal incompatible JPEG Part 3 extensions.
0N/A * Once the JPEG 3 version-number marker is well defined, this code
0N/A * ought to change!
0N/A * [To be behaviorally compatible with other popular image display
0N/A * applications, we are now treating these unknown markers as warnings,
0N/A * rather than errors. This allows processing to continue, although
0N/A * any portions of the image after the bad marker may be corrupted
0N/A * and/or rendered gray. See 4511441.]
0N/A */
0N/A WARNMS1(cinfo, JERR_UNKNOWN_MARKER, cinfo->unread_marker);
0N/A break;
0N/A }
0N/A /* Successfully processed marker, so reset state variable */
0N/A cinfo->unread_marker = 0;
0N/A } /* end loop */
0N/A}
0N/A
0N/A
0N/A/*
0N/A * Read a restart marker, which is expected to appear next in the datastream;
0N/A * if the marker is not there, take appropriate recovery action.
0N/A * Returns FALSE if suspension is required.
0N/A *
0N/A * This is called by the entropy decoder after it has read an appropriate
0N/A * number of MCUs. cinfo->unread_marker may be nonzero if the entropy decoder
0N/A * has already read a marker from the data source. Under normal conditions
0N/A * cinfo->unread_marker will be reset to 0 before returning; if not reset,
0N/A * it holds a marker which the decoder will be unable to read past.
0N/A */
0N/A
0N/AMETHODDEF(boolean)
0N/Aread_restart_marker (j_decompress_ptr cinfo)
0N/A{
0N/A /* Obtain a marker unless we already did. */
0N/A /* Note that next_marker will complain if it skips any data. */
0N/A if (cinfo->unread_marker == 0) {
0N/A if (! next_marker(cinfo))
0N/A return FALSE;
0N/A }
0N/A
0N/A if (cinfo->unread_marker ==
0N/A ((int) M_RST0 + cinfo->marker->next_restart_num)) {
0N/A /* Normal case --- swallow the marker and let entropy decoder continue */
0N/A TRACEMS1(cinfo, 3, JTRC_RST, cinfo->marker->next_restart_num);
0N/A cinfo->unread_marker = 0;
0N/A } else {
0N/A /* Uh-oh, the restart markers have been messed up. */
0N/A /* Let the data source manager determine how to resync. */
0N/A if (! (*cinfo->src->resync_to_restart) (cinfo,
0N/A cinfo->marker->next_restart_num))
0N/A return FALSE;
0N/A }
0N/A
0N/A /* Update next-restart state */
0N/A cinfo->marker->next_restart_num = (cinfo->marker->next_restart_num + 1) & 7;
0N/A
0N/A return TRUE;
0N/A}
0N/A
0N/A
0N/A/*
0N/A * This is the default resync_to_restart method for data source managers
0N/A * to use if they don't have any better approach. Some data source managers
0N/A * may be able to back up, or may have additional knowledge about the data
0N/A * which permits a more intelligent recovery strategy; such managers would
0N/A * presumably supply their own resync method.
0N/A *
0N/A * read_restart_marker calls resync_to_restart if it finds a marker other than
0N/A * the restart marker it was expecting. (This code is *not* used unless
0N/A * a nonzero restart interval has been declared.) cinfo->unread_marker is
0N/A * the marker code actually found (might be anything, except 0 or FF).
0N/A * The desired restart marker number (0..7) is passed as a parameter.
0N/A * This routine is supposed to apply whatever error recovery strategy seems
0N/A * appropriate in order to position the input stream to the next data segment.
0N/A * Note that cinfo->unread_marker is treated as a marker appearing before
0N/A * the current data-source input point; usually it should be reset to zero
0N/A * before returning.
0N/A * Returns FALSE if suspension is required.
0N/A *
0N/A * This implementation is substantially constrained by wanting to treat the
0N/A * input as a data stream; this means we can't back up. Therefore, we have
0N/A * only the following actions to work with:
0N/A * 1. Simply discard the marker and let the entropy decoder resume at next
0N/A * byte of file.
0N/A * 2. Read forward until we find another marker, discarding intervening
0N/A * data. (In theory we could look ahead within the current bufferload,
0N/A * without having to discard data if we don't find the desired marker.
0N/A * This idea is not implemented here, in part because it makes behavior
0N/A * dependent on buffer size and chance buffer-boundary positions.)
0N/A * 3. Leave the marker unread (by failing to zero cinfo->unread_marker).
0N/A * This will cause the entropy decoder to process an empty data segment,
0N/A * inserting dummy zeroes, and then we will reprocess the marker.
0N/A *
0N/A * #2 is appropriate if we think the desired marker lies ahead, while #3 is
0N/A * appropriate if the found marker is a future restart marker (indicating
0N/A * that we have missed the desired restart marker, probably because it got
0N/A * corrupted).
0N/A * We apply #2 or #3 if the found marker is a restart marker no more than
0N/A * two counts behind or ahead of the expected one. We also apply #2 if the
0N/A * found marker is not a legal JPEG marker code (it's certainly bogus data).
0N/A * If the found marker is a restart marker more than 2 counts away, we do #1
0N/A * (too much risk that the marker is erroneous; with luck we will be able to
0N/A * resync at some future point).
0N/A * For any valid non-restart JPEG marker, we apply #3. This keeps us from
0N/A * overrunning the end of a scan. An implementation limited to single-scan
0N/A * files might find it better to apply #2 for markers other than EOI, since
0N/A * any other marker would have to be bogus data in that case.
0N/A */
0N/A
0N/AGLOBAL(boolean)
0N/Ajpeg_resync_to_restart (j_decompress_ptr cinfo, int desired)
0N/A{
0N/A int marker = cinfo->unread_marker;
0N/A int action = 1;
0N/A
0N/A /* Always put up a warning. */
0N/A WARNMS2(cinfo, JWRN_MUST_RESYNC, marker, desired);
0N/A
0N/A /* Outer loop handles repeated decision after scanning forward. */
0N/A for (;;) {
0N/A if (marker < (int) M_SOF0)
0N/A action = 2; /* invalid marker */
0N/A else if (marker < (int) M_RST0 || marker > (int) M_RST7)
0N/A action = 3; /* valid non-restart marker */
0N/A else {
0N/A if (marker == ((int) M_RST0 + ((desired+1) & 7)) ||
0N/A marker == ((int) M_RST0 + ((desired+2) & 7)))
0N/A action = 3; /* one of the next two expected restarts */
0N/A else if (marker == ((int) M_RST0 + ((desired-1) & 7)) ||
0N/A marker == ((int) M_RST0 + ((desired-2) & 7)))
0N/A action = 2; /* a prior restart, so advance */
0N/A else
0N/A action = 1; /* desired restart or too far away */
0N/A }
0N/A TRACEMS2(cinfo, 4, JTRC_RECOVERY_ACTION, marker, action);
0N/A switch (action) {
0N/A case 1:
0N/A /* Discard marker and let entropy decoder resume processing. */
0N/A cinfo->unread_marker = 0;
0N/A return TRUE;
0N/A case 2:
0N/A /* Scan to the next marker, and repeat the decision loop. */
0N/A if (! next_marker(cinfo))
0N/A return FALSE;
0N/A marker = cinfo->unread_marker;
0N/A break;
0N/A case 3:
0N/A /* Return without advancing past this marker. */
0N/A /* Entropy decoder will be forced to process an empty segment. */
0N/A return TRUE;
0N/A }
0N/A } /* end loop */
0N/A}
0N/A
0N/A
0N/A/*
0N/A * Reset marker processing state to begin a fresh datastream.
0N/A */
0N/A
0N/AMETHODDEF(void)
0N/Areset_marker_reader (j_decompress_ptr cinfo)
0N/A{
0N/A my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
0N/A
0N/A cinfo->comp_info = NULL; /* until allocated by get_sof */
0N/A cinfo->input_scan_number = 0; /* no SOS seen yet */
0N/A cinfo->unread_marker = 0; /* no pending marker */
0N/A marker->pub.saw_SOI = FALSE; /* set internal state too */
0N/A marker->pub.saw_SOF = FALSE;
0N/A marker->pub.discarded_bytes = 0;
0N/A marker->cur_marker = NULL;
0N/A}
0N/A
0N/A
0N/A/*
0N/A * Initialize the marker reader module.
0N/A * This is called only once, when the decompression object is created.
0N/A */
0N/A
0N/AGLOBAL(void)
0N/Ajinit_marker_reader (j_decompress_ptr cinfo)
0N/A{
0N/A my_marker_ptr marker;
0N/A int i;
0N/A
0N/A /* Create subobject in permanent pool */
0N/A marker = (my_marker_ptr)
0N/A (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
0N/A SIZEOF(my_marker_reader));
0N/A cinfo->marker = (struct jpeg_marker_reader *) marker;
0N/A /* Initialize public method pointers */
0N/A marker->pub.reset_marker_reader = reset_marker_reader;
0N/A marker->pub.read_markers = read_markers;
0N/A marker->pub.read_restart_marker = read_restart_marker;
0N/A /* Initialize COM/APPn processing.
0N/A * By default, we examine and then discard APP0 and APP14.
0N/A * We also may need to save APP1 to detect the case of EXIF images (see 4881314).
0N/A * COM and all other APPn are simply discarded.
0N/A */
0N/A marker->process_COM = skip_variable;
0N/A marker->length_limit_COM = 0;
0N/A for (i = 0; i < 16; i++) {
0N/A marker->process_APPn[i] = skip_variable;
0N/A marker->length_limit_APPn[i] = 0;
0N/A }
0N/A marker->process_APPn[0] = get_interesting_appn;
0N/A marker->process_APPn[1] = save_marker;
0N/A marker->process_APPn[14] = get_interesting_appn;
0N/A /* Reset marker processing state */
0N/A reset_marker_reader(cinfo);
0N/A}
0N/A
0N/A
0N/A/*
0N/A * Control saving of COM and APPn markers into marker_list.
0N/A */
0N/A
0N/A#ifdef SAVE_MARKERS_SUPPORTED
0N/A
0N/AGLOBAL(void)
0N/Ajpeg_save_markers (j_decompress_ptr cinfo, int marker_code,
0N/A unsigned int length_limit)
0N/A{
0N/A my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
3538N/A size_t maxlength;
0N/A jpeg_marker_parser_method processor;
0N/A
0N/A /* Length limit mustn't be larger than what we can allocate
0N/A * (should only be a concern in a 16-bit environment).
0N/A */
0N/A maxlength = cinfo->mem->max_alloc_chunk - SIZEOF(struct jpeg_marker_struct);
3538N/A if (length_limit > maxlength)
0N/A length_limit = (unsigned int) maxlength;
0N/A
0N/A /* Choose processor routine to use.
0N/A * APP0/APP14 have special requirements.
0N/A */
0N/A if (length_limit) {
0N/A processor = save_marker;
0N/A /* If saving APP0/APP14, save at least enough for our internal use. */
0N/A if (marker_code == (int) M_APP0 && length_limit < APP0_DATA_LEN)
0N/A length_limit = APP0_DATA_LEN;
0N/A else if (marker_code == (int) M_APP14 && length_limit < APP14_DATA_LEN)
0N/A length_limit = APP14_DATA_LEN;
0N/A } else {
0N/A processor = skip_variable;
0N/A /* If discarding APP0/APP14, use our regular on-the-fly processor. */
0N/A if (marker_code == (int) M_APP0 || marker_code == (int) M_APP14)
0N/A processor = get_interesting_appn;
0N/A }
0N/A
0N/A if (marker_code == (int) M_COM) {
0N/A marker->process_COM = processor;
0N/A marker->length_limit_COM = length_limit;
0N/A } else if (marker_code >= (int) M_APP0 && marker_code <= (int) M_APP15) {
0N/A marker->process_APPn[marker_code - (int) M_APP0] = processor;
0N/A marker->length_limit_APPn[marker_code - (int) M_APP0] = length_limit;
0N/A } else
0N/A ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, marker_code);
0N/A}
0N/A
0N/A#endif /* SAVE_MARKERS_SUPPORTED */
0N/A
0N/A
0N/A/*
0N/A * Install a special processing method for COM or APPn markers.
0N/A */
0N/A
0N/AGLOBAL(void)
0N/Ajpeg_set_marker_processor (j_decompress_ptr cinfo, int marker_code,
0N/A jpeg_marker_parser_method routine)
0N/A{
0N/A my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
0N/A
0N/A if (marker_code == (int) M_COM)
0N/A marker->process_COM = routine;
0N/A else if (marker_code >= (int) M_APP0 && marker_code <= (int) M_APP15)
0N/A marker->process_APPn[marker_code - (int) M_APP0] = routine;
0N/A else
0N/A ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, marker_code);
0N/A}