Path: blob/master/3rdparty/libjpeg-turbo/src/jdmarker.c
16337 views
/*1* jdmarker.c2*3* This file was part of the Independent JPEG Group's software:4* Copyright (C) 1991-1998, Thomas G. Lane.5* libjpeg-turbo Modifications:6* Copyright (C) 2012, 2015, D. R. Commander.7* For conditions of distribution and use, see the accompanying README.ijg8* file.9*10* This file contains routines to decode JPEG datastream markers.11* Most of the complexity arises from our desire to support input12* suspension: if not all of the data for a marker is available,13* we must exit back to the application. On resumption, we reprocess14* the marker.15*/1617#define JPEG_INTERNALS18#include "jinclude.h"19#include "jpeglib.h"202122typedef enum { /* JPEG marker codes */23M_SOF0 = 0xc0,24M_SOF1 = 0xc1,25M_SOF2 = 0xc2,26M_SOF3 = 0xc3,2728M_SOF5 = 0xc5,29M_SOF6 = 0xc6,30M_SOF7 = 0xc7,3132M_JPG = 0xc8,33M_SOF9 = 0xc9,34M_SOF10 = 0xca,35M_SOF11 = 0xcb,3637M_SOF13 = 0xcd,38M_SOF14 = 0xce,39M_SOF15 = 0xcf,4041M_DHT = 0xc4,4243M_DAC = 0xcc,4445M_RST0 = 0xd0,46M_RST1 = 0xd1,47M_RST2 = 0xd2,48M_RST3 = 0xd3,49M_RST4 = 0xd4,50M_RST5 = 0xd5,51M_RST6 = 0xd6,52M_RST7 = 0xd7,5354M_SOI = 0xd8,55M_EOI = 0xd9,56M_SOS = 0xda,57M_DQT = 0xdb,58M_DNL = 0xdc,59M_DRI = 0xdd,60M_DHP = 0xde,61M_EXP = 0xdf,6263M_APP0 = 0xe0,64M_APP1 = 0xe1,65M_APP2 = 0xe2,66M_APP3 = 0xe3,67M_APP4 = 0xe4,68M_APP5 = 0xe5,69M_APP6 = 0xe6,70M_APP7 = 0xe7,71M_APP8 = 0xe8,72M_APP9 = 0xe9,73M_APP10 = 0xea,74M_APP11 = 0xeb,75M_APP12 = 0xec,76M_APP13 = 0xed,77M_APP14 = 0xee,78M_APP15 = 0xef,7980M_JPG0 = 0xf0,81M_JPG13 = 0xfd,82M_COM = 0xfe,8384M_TEM = 0x01,8586M_ERROR = 0x10087} JPEG_MARKER;888990/* Private state */9192typedef struct {93struct jpeg_marker_reader pub; /* public fields */9495/* Application-overridable marker processing methods */96jpeg_marker_parser_method process_COM;97jpeg_marker_parser_method process_APPn[16];9899/* Limit on marker data length to save for each marker type */100unsigned int length_limit_COM;101unsigned int length_limit_APPn[16];102103/* Status of COM/APPn marker saving */104jpeg_saved_marker_ptr cur_marker; /* NULL if not processing a marker */105unsigned int bytes_read; /* data bytes read so far in marker */106/* Note: cur_marker is not linked into marker_list until it's all read. */107} my_marker_reader;108109typedef my_marker_reader *my_marker_ptr;110111112/*113* Macros for fetching data from the data source module.114*115* At all times, cinfo->src->next_input_byte and ->bytes_in_buffer reflect116* the current restart point; we update them only when we have reached a117* suitable place to restart if a suspension occurs.118*/119120/* Declare and initialize local copies of input pointer/count */121#define INPUT_VARS(cinfo) \122struct jpeg_source_mgr *datasrc = (cinfo)->src; \123const JOCTET *next_input_byte = datasrc->next_input_byte; \124size_t bytes_in_buffer = datasrc->bytes_in_buffer125126/* Unload the local copies --- do this only at a restart boundary */127#define INPUT_SYNC(cinfo) \128( datasrc->next_input_byte = next_input_byte, \129datasrc->bytes_in_buffer = bytes_in_buffer )130131/* Reload the local copies --- used only in MAKE_BYTE_AVAIL */132#define INPUT_RELOAD(cinfo) \133( next_input_byte = datasrc->next_input_byte, \134bytes_in_buffer = datasrc->bytes_in_buffer )135136/* Internal macro for INPUT_BYTE and INPUT_2BYTES: make a byte available.137* Note we do *not* do INPUT_SYNC before calling fill_input_buffer,138* but we must reload the local copies after a successful fill.139*/140#define MAKE_BYTE_AVAIL(cinfo,action) \141if (bytes_in_buffer == 0) { \142if (! (*datasrc->fill_input_buffer) (cinfo)) \143{ action; } \144INPUT_RELOAD(cinfo); \145}146147/* Read a byte into variable V.148* If must suspend, take the specified action (typically "return FALSE").149*/150#define INPUT_BYTE(cinfo,V,action) \151MAKESTMT( MAKE_BYTE_AVAIL(cinfo,action); \152bytes_in_buffer--; \153V = GETJOCTET(*next_input_byte++); )154155/* As above, but read two bytes interpreted as an unsigned 16-bit integer.156* V should be declared unsigned int or perhaps JLONG.157*/158#define INPUT_2BYTES(cinfo,V,action) \159MAKESTMT( MAKE_BYTE_AVAIL(cinfo,action); \160bytes_in_buffer--; \161V = ((unsigned int) GETJOCTET(*next_input_byte++)) << 8; \162MAKE_BYTE_AVAIL(cinfo,action); \163bytes_in_buffer--; \164V += GETJOCTET(*next_input_byte++); )165166167/*168* Routines to process JPEG markers.169*170* Entry condition: JPEG marker itself has been read and its code saved171* in cinfo->unread_marker; input restart point is just after the marker.172*173* Exit: if return TRUE, have read and processed any parameters, and have174* updated the restart point to point after the parameters.175* If return FALSE, was forced to suspend before reaching end of176* marker parameters; restart point has not been moved. Same routine177* will be called again after application supplies more input data.178*179* This approach to suspension assumes that all of a marker's parameters180* can fit into a single input bufferload. This should hold for "normal"181* markers. Some COM/APPn markers might have large parameter segments182* that might not fit. If we are simply dropping such a marker, we use183* skip_input_data to get past it, and thereby put the problem on the184* source manager's shoulders. If we are saving the marker's contents185* into memory, we use a slightly different convention: when forced to186* suspend, the marker processor updates the restart point to the end of187* what it's consumed (ie, the end of the buffer) before returning FALSE.188* On resumption, cinfo->unread_marker still contains the marker code,189* but the data source will point to the next chunk of marker data.190* The marker processor must retain internal state to deal with this.191*192* Note that we don't bother to avoid duplicate trace messages if a193* suspension occurs within marker parameters. Other side effects194* require more care.195*/196197198LOCAL(boolean)199get_soi (j_decompress_ptr cinfo)200/* Process an SOI marker */201{202int i;203204TRACEMS(cinfo, 1, JTRC_SOI);205206if (cinfo->marker->saw_SOI)207ERREXIT(cinfo, JERR_SOI_DUPLICATE);208209/* Reset all parameters that are defined to be reset by SOI */210211for (i = 0; i < NUM_ARITH_TBLS; i++) {212cinfo->arith_dc_L[i] = 0;213cinfo->arith_dc_U[i] = 1;214cinfo->arith_ac_K[i] = 5;215}216cinfo->restart_interval = 0;217218/* Set initial assumptions for colorspace etc */219220cinfo->jpeg_color_space = JCS_UNKNOWN;221cinfo->CCIR601_sampling = FALSE; /* Assume non-CCIR sampling??? */222223cinfo->saw_JFIF_marker = FALSE;224cinfo->JFIF_major_version = 1; /* set default JFIF APP0 values */225cinfo->JFIF_minor_version = 1;226cinfo->density_unit = 0;227cinfo->X_density = 1;228cinfo->Y_density = 1;229cinfo->saw_Adobe_marker = FALSE;230cinfo->Adobe_transform = 0;231232cinfo->marker->saw_SOI = TRUE;233234return TRUE;235}236237238LOCAL(boolean)239get_sof (j_decompress_ptr cinfo, boolean is_prog, boolean is_arith)240/* Process a SOFn marker */241{242JLONG length;243int c, ci;244jpeg_component_info *compptr;245INPUT_VARS(cinfo);246247cinfo->progressive_mode = is_prog;248cinfo->arith_code = is_arith;249250INPUT_2BYTES(cinfo, length, return FALSE);251252INPUT_BYTE(cinfo, cinfo->data_precision, return FALSE);253INPUT_2BYTES(cinfo, cinfo->image_height, return FALSE);254INPUT_2BYTES(cinfo, cinfo->image_width, return FALSE);255INPUT_BYTE(cinfo, cinfo->num_components, return FALSE);256257length -= 8;258259TRACEMS4(cinfo, 1, JTRC_SOF, cinfo->unread_marker,260(int) cinfo->image_width, (int) cinfo->image_height,261cinfo->num_components);262263if (cinfo->marker->saw_SOF)264ERREXIT(cinfo, JERR_SOF_DUPLICATE);265266/* We don't support files in which the image height is initially specified */267/* as 0 and is later redefined by DNL. As long as we have to check that, */268/* might as well have a general sanity check. */269if (cinfo->image_height <= 0 || cinfo->image_width <= 0270|| cinfo->num_components <= 0)271ERREXIT(cinfo, JERR_EMPTY_IMAGE);272273if (length != (cinfo->num_components * 3))274ERREXIT(cinfo, JERR_BAD_LENGTH);275276if (cinfo->comp_info == NULL) /* do only once, even if suspend */277cinfo->comp_info = (jpeg_component_info *) (*cinfo->mem->alloc_small)278((j_common_ptr) cinfo, JPOOL_IMAGE,279cinfo->num_components * sizeof(jpeg_component_info));280281for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;282ci++, compptr++) {283compptr->component_index = ci;284INPUT_BYTE(cinfo, compptr->component_id, return FALSE);285INPUT_BYTE(cinfo, c, return FALSE);286compptr->h_samp_factor = (c >> 4) & 15;287compptr->v_samp_factor = (c ) & 15;288INPUT_BYTE(cinfo, compptr->quant_tbl_no, return FALSE);289290TRACEMS4(cinfo, 1, JTRC_SOF_COMPONENT,291compptr->component_id, compptr->h_samp_factor,292compptr->v_samp_factor, compptr->quant_tbl_no);293}294295cinfo->marker->saw_SOF = TRUE;296297INPUT_SYNC(cinfo);298return TRUE;299}300301302LOCAL(boolean)303get_sos (j_decompress_ptr cinfo)304/* Process a SOS marker */305{306JLONG length;307int i, ci, n, c, cc, pi;308jpeg_component_info *compptr;309INPUT_VARS(cinfo);310311if (! cinfo->marker->saw_SOF)312ERREXIT(cinfo, JERR_SOS_NO_SOF);313314INPUT_2BYTES(cinfo, length, return FALSE);315316INPUT_BYTE(cinfo, n, return FALSE); /* Number of components */317318TRACEMS1(cinfo, 1, JTRC_SOS, n);319320if (length != (n * 2 + 6) || n < 1 || n > MAX_COMPS_IN_SCAN)321ERREXIT(cinfo, JERR_BAD_LENGTH);322323cinfo->comps_in_scan = n;324325/* Collect the component-spec parameters */326327for (i = 0; i < MAX_COMPS_IN_SCAN; i++)328cinfo->cur_comp_info[i] = NULL;329330for (i = 0; i < n; i++) {331INPUT_BYTE(cinfo, cc, return FALSE);332INPUT_BYTE(cinfo, c, return FALSE);333334for (ci = 0, compptr = cinfo->comp_info;335ci < cinfo->num_components && ci < MAX_COMPS_IN_SCAN;336ci++, compptr++) {337if (cc == compptr->component_id && !cinfo->cur_comp_info[ci])338goto id_found;339}340341ERREXIT1(cinfo, JERR_BAD_COMPONENT_ID, cc);342343id_found:344345cinfo->cur_comp_info[i] = compptr;346compptr->dc_tbl_no = (c >> 4) & 15;347compptr->ac_tbl_no = (c ) & 15;348349TRACEMS3(cinfo, 1, JTRC_SOS_COMPONENT, cc,350compptr->dc_tbl_no, compptr->ac_tbl_no);351352/* This CSi (cc) should differ from the previous CSi */353for (pi = 0; pi < i; pi++) {354if (cinfo->cur_comp_info[pi] == compptr) {355ERREXIT1(cinfo, JERR_BAD_COMPONENT_ID, cc);356}357}358}359360/* Collect the additional scan parameters Ss, Se, Ah/Al. */361INPUT_BYTE(cinfo, c, return FALSE);362cinfo->Ss = c;363INPUT_BYTE(cinfo, c, return FALSE);364cinfo->Se = c;365INPUT_BYTE(cinfo, c, return FALSE);366cinfo->Ah = (c >> 4) & 15;367cinfo->Al = (c ) & 15;368369TRACEMS4(cinfo, 1, JTRC_SOS_PARAMS, cinfo->Ss, cinfo->Se,370cinfo->Ah, cinfo->Al);371372/* Prepare to scan data & restart markers */373cinfo->marker->next_restart_num = 0;374375/* Count another SOS marker */376cinfo->input_scan_number++;377378INPUT_SYNC(cinfo);379return TRUE;380}381382383#ifdef D_ARITH_CODING_SUPPORTED384385LOCAL(boolean)386get_dac (j_decompress_ptr cinfo)387/* Process a DAC marker */388{389JLONG length;390int index, val;391INPUT_VARS(cinfo);392393INPUT_2BYTES(cinfo, length, return FALSE);394length -= 2;395396while (length > 0) {397INPUT_BYTE(cinfo, index, return FALSE);398INPUT_BYTE(cinfo, val, return FALSE);399400length -= 2;401402TRACEMS2(cinfo, 1, JTRC_DAC, index, val);403404if (index < 0 || index >= (2*NUM_ARITH_TBLS))405ERREXIT1(cinfo, JERR_DAC_INDEX, index);406407if (index >= NUM_ARITH_TBLS) { /* define AC table */408cinfo->arith_ac_K[index-NUM_ARITH_TBLS] = (UINT8) val;409} else { /* define DC table */410cinfo->arith_dc_L[index] = (UINT8) (val & 0x0F);411cinfo->arith_dc_U[index] = (UINT8) (val >> 4);412if (cinfo->arith_dc_L[index] > cinfo->arith_dc_U[index])413ERREXIT1(cinfo, JERR_DAC_VALUE, val);414}415}416417if (length != 0)418ERREXIT(cinfo, JERR_BAD_LENGTH);419420INPUT_SYNC(cinfo);421return TRUE;422}423424#else /* ! D_ARITH_CODING_SUPPORTED */425426#define get_dac(cinfo) skip_variable(cinfo)427428#endif /* D_ARITH_CODING_SUPPORTED */429430431LOCAL(boolean)432get_dht (j_decompress_ptr cinfo)433/* Process a DHT marker */434{435JLONG length;436UINT8 bits[17];437UINT8 huffval[256];438int i, index, count;439JHUFF_TBL **htblptr;440INPUT_VARS(cinfo);441442INPUT_2BYTES(cinfo, length, return FALSE);443length -= 2;444445while (length > 16) {446INPUT_BYTE(cinfo, index, return FALSE);447448TRACEMS1(cinfo, 1, JTRC_DHT, index);449450bits[0] = 0;451count = 0;452for (i = 1; i <= 16; i++) {453INPUT_BYTE(cinfo, bits[i], return FALSE);454count += bits[i];455}456457length -= 1 + 16;458459TRACEMS8(cinfo, 2, JTRC_HUFFBITS,460bits[1], bits[2], bits[3], bits[4],461bits[5], bits[6], bits[7], bits[8]);462TRACEMS8(cinfo, 2, JTRC_HUFFBITS,463bits[9], bits[10], bits[11], bits[12],464bits[13], bits[14], bits[15], bits[16]);465466/* Here we just do minimal validation of the counts to avoid walking467* off the end of our table space. jdhuff.c will check more carefully.468*/469if (count > 256 || ((JLONG) count) > length)470ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);471472for (i = 0; i < count; i++)473INPUT_BYTE(cinfo, huffval[i], return FALSE);474475MEMZERO(&huffval[count], (256 - count) * sizeof(UINT8));476477length -= count;478479if (index & 0x10) { /* AC table definition */480index -= 0x10;481if (index < 0 || index >= NUM_HUFF_TBLS)482ERREXIT1(cinfo, JERR_DHT_INDEX, index);483htblptr = &cinfo->ac_huff_tbl_ptrs[index];484} else { /* DC table definition */485if (index < 0 || index >= NUM_HUFF_TBLS)486ERREXIT1(cinfo, JERR_DHT_INDEX, index);487htblptr = &cinfo->dc_huff_tbl_ptrs[index];488}489490if (*htblptr == NULL)491*htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);492493MEMCOPY((*htblptr)->bits, bits, sizeof((*htblptr)->bits));494MEMCOPY((*htblptr)->huffval, huffval, sizeof((*htblptr)->huffval));495}496497if (length != 0)498ERREXIT(cinfo, JERR_BAD_LENGTH);499500INPUT_SYNC(cinfo);501return TRUE;502}503504505LOCAL(boolean)506get_dqt (j_decompress_ptr cinfo)507/* Process a DQT marker */508{509JLONG length;510int n, i, prec;511unsigned int tmp;512JQUANT_TBL *quant_ptr;513INPUT_VARS(cinfo);514515INPUT_2BYTES(cinfo, length, return FALSE);516length -= 2;517518while (length > 0) {519INPUT_BYTE(cinfo, n, return FALSE);520prec = n >> 4;521n &= 0x0F;522523TRACEMS2(cinfo, 1, JTRC_DQT, n, prec);524525if (n >= NUM_QUANT_TBLS)526ERREXIT1(cinfo, JERR_DQT_INDEX, n);527528if (cinfo->quant_tbl_ptrs[n] == NULL)529cinfo->quant_tbl_ptrs[n] = jpeg_alloc_quant_table((j_common_ptr) cinfo);530quant_ptr = cinfo->quant_tbl_ptrs[n];531532for (i = 0; i < DCTSIZE2; i++) {533if (prec)534INPUT_2BYTES(cinfo, tmp, return FALSE);535else536INPUT_BYTE(cinfo, tmp, return FALSE);537/* We convert the zigzag-order table to natural array order. */538quant_ptr->quantval[jpeg_natural_order[i]] = (UINT16) tmp;539}540541if (cinfo->err->trace_level >= 2) {542for (i = 0; i < DCTSIZE2; i += 8) {543TRACEMS8(cinfo, 2, JTRC_QUANTVALS,544quant_ptr->quantval[i], quant_ptr->quantval[i+1],545quant_ptr->quantval[i+2], quant_ptr->quantval[i+3],546quant_ptr->quantval[i+4], quant_ptr->quantval[i+5],547quant_ptr->quantval[i+6], quant_ptr->quantval[i+7]);548}549}550551length -= DCTSIZE2+1;552if (prec) length -= DCTSIZE2;553}554555if (length != 0)556ERREXIT(cinfo, JERR_BAD_LENGTH);557558INPUT_SYNC(cinfo);559return TRUE;560}561562563LOCAL(boolean)564get_dri (j_decompress_ptr cinfo)565/* Process a DRI marker */566{567JLONG length;568unsigned int tmp;569INPUT_VARS(cinfo);570571INPUT_2BYTES(cinfo, length, return FALSE);572573if (length != 4)574ERREXIT(cinfo, JERR_BAD_LENGTH);575576INPUT_2BYTES(cinfo, tmp, return FALSE);577578TRACEMS1(cinfo, 1, JTRC_DRI, tmp);579580cinfo->restart_interval = tmp;581582INPUT_SYNC(cinfo);583return TRUE;584}585586587/*588* Routines for processing APPn and COM markers.589* These are either saved in memory or discarded, per application request.590* APP0 and APP14 are specially checked to see if they are591* JFIF and Adobe markers, respectively.592*/593594#define APP0_DATA_LEN 14 /* Length of interesting data in APP0 */595#define APP14_DATA_LEN 12 /* Length of interesting data in APP14 */596#define APPN_DATA_LEN 14 /* Must be the largest of the above!! */597598599LOCAL(void)600examine_app0 (j_decompress_ptr cinfo, JOCTET *data,601unsigned int datalen, JLONG remaining)602/* Examine first few bytes from an APP0.603* Take appropriate action if it is a JFIF marker.604* datalen is # of bytes at data[], remaining is length of rest of marker data.605*/606{607JLONG totallen = (JLONG) datalen + remaining;608609if (datalen >= APP0_DATA_LEN &&610GETJOCTET(data[0]) == 0x4A &&611GETJOCTET(data[1]) == 0x46 &&612GETJOCTET(data[2]) == 0x49 &&613GETJOCTET(data[3]) == 0x46 &&614GETJOCTET(data[4]) == 0) {615/* Found JFIF APP0 marker: save info */616cinfo->saw_JFIF_marker = TRUE;617cinfo->JFIF_major_version = GETJOCTET(data[5]);618cinfo->JFIF_minor_version = GETJOCTET(data[6]);619cinfo->density_unit = GETJOCTET(data[7]);620cinfo->X_density = (GETJOCTET(data[8]) << 8) + GETJOCTET(data[9]);621cinfo->Y_density = (GETJOCTET(data[10]) << 8) + GETJOCTET(data[11]);622/* Check version.623* Major version must be 1, anything else signals an incompatible change.624* (We used to treat this as an error, but now it's a nonfatal warning,625* because some bozo at Hijaak couldn't read the spec.)626* Minor version should be 0..2, but process anyway if newer.627*/628if (cinfo->JFIF_major_version != 1)629WARNMS2(cinfo, JWRN_JFIF_MAJOR,630cinfo->JFIF_major_version, cinfo->JFIF_minor_version);631/* Generate trace messages */632TRACEMS5(cinfo, 1, JTRC_JFIF,633cinfo->JFIF_major_version, cinfo->JFIF_minor_version,634cinfo->X_density, cinfo->Y_density, cinfo->density_unit);635/* Validate thumbnail dimensions and issue appropriate messages */636if (GETJOCTET(data[12]) | GETJOCTET(data[13]))637TRACEMS2(cinfo, 1, JTRC_JFIF_THUMBNAIL,638GETJOCTET(data[12]), GETJOCTET(data[13]));639totallen -= APP0_DATA_LEN;640if (totallen !=641((JLONG)GETJOCTET(data[12]) * (JLONG)GETJOCTET(data[13]) * (JLONG) 3))642TRACEMS1(cinfo, 1, JTRC_JFIF_BADTHUMBNAILSIZE, (int) totallen);643} else if (datalen >= 6 &&644GETJOCTET(data[0]) == 0x4A &&645GETJOCTET(data[1]) == 0x46 &&646GETJOCTET(data[2]) == 0x58 &&647GETJOCTET(data[3]) == 0x58 &&648GETJOCTET(data[4]) == 0) {649/* Found JFIF "JFXX" extension APP0 marker */650/* The library doesn't actually do anything with these,651* but we try to produce a helpful trace message.652*/653switch (GETJOCTET(data[5])) {654case 0x10:655TRACEMS1(cinfo, 1, JTRC_THUMB_JPEG, (int) totallen);656break;657case 0x11:658TRACEMS1(cinfo, 1, JTRC_THUMB_PALETTE, (int) totallen);659break;660case 0x13:661TRACEMS1(cinfo, 1, JTRC_THUMB_RGB, (int) totallen);662break;663default:664TRACEMS2(cinfo, 1, JTRC_JFIF_EXTENSION,665GETJOCTET(data[5]), (int) totallen);666break;667}668} else {669/* Start of APP0 does not match "JFIF" or "JFXX", or too short */670TRACEMS1(cinfo, 1, JTRC_APP0, (int) totallen);671}672}673674675LOCAL(void)676examine_app14 (j_decompress_ptr cinfo, JOCTET *data,677unsigned int datalen, JLONG remaining)678/* Examine first few bytes from an APP14.679* Take appropriate action if it is an Adobe marker.680* datalen is # of bytes at data[], remaining is length of rest of marker data.681*/682{683unsigned int version, flags0, flags1, transform;684685if (datalen >= APP14_DATA_LEN &&686GETJOCTET(data[0]) == 0x41 &&687GETJOCTET(data[1]) == 0x64 &&688GETJOCTET(data[2]) == 0x6F &&689GETJOCTET(data[3]) == 0x62 &&690GETJOCTET(data[4]) == 0x65) {691/* Found Adobe APP14 marker */692version = (GETJOCTET(data[5]) << 8) + GETJOCTET(data[6]);693flags0 = (GETJOCTET(data[7]) << 8) + GETJOCTET(data[8]);694flags1 = (GETJOCTET(data[9]) << 8) + GETJOCTET(data[10]);695transform = GETJOCTET(data[11]);696TRACEMS4(cinfo, 1, JTRC_ADOBE, version, flags0, flags1, transform);697cinfo->saw_Adobe_marker = TRUE;698cinfo->Adobe_transform = (UINT8) transform;699} else {700/* Start of APP14 does not match "Adobe", or too short */701TRACEMS1(cinfo, 1, JTRC_APP14, (int) (datalen + remaining));702}703}704705706METHODDEF(boolean)707get_interesting_appn (j_decompress_ptr cinfo)708/* Process an APP0 or APP14 marker without saving it */709{710JLONG length;711JOCTET b[APPN_DATA_LEN];712unsigned int i, numtoread;713INPUT_VARS(cinfo);714715INPUT_2BYTES(cinfo, length, return FALSE);716length -= 2;717718/* get the interesting part of the marker data */719if (length >= APPN_DATA_LEN)720numtoread = APPN_DATA_LEN;721else if (length > 0)722numtoread = (unsigned int) length;723else724numtoread = 0;725for (i = 0; i < numtoread; i++)726INPUT_BYTE(cinfo, b[i], return FALSE);727length -= numtoread;728729/* process it */730switch (cinfo->unread_marker) {731case M_APP0:732examine_app0(cinfo, (JOCTET *) b, numtoread, length);733break;734case M_APP14:735examine_app14(cinfo, (JOCTET *) b, numtoread, length);736break;737default:738/* can't get here unless jpeg_save_markers chooses wrong processor */739ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, cinfo->unread_marker);740break;741}742743/* skip any remaining data -- could be lots */744INPUT_SYNC(cinfo);745if (length > 0)746(*cinfo->src->skip_input_data) (cinfo, (long) length);747748return TRUE;749}750751752#ifdef SAVE_MARKERS_SUPPORTED753754METHODDEF(boolean)755save_marker (j_decompress_ptr cinfo)756/* Save an APPn or COM marker into the marker list */757{758my_marker_ptr marker = (my_marker_ptr) cinfo->marker;759jpeg_saved_marker_ptr cur_marker = marker->cur_marker;760unsigned int bytes_read, data_length;761JOCTET *data;762JLONG length = 0;763INPUT_VARS(cinfo);764765if (cur_marker == NULL) {766/* begin reading a marker */767INPUT_2BYTES(cinfo, length, return FALSE);768length -= 2;769if (length >= 0) { /* watch out for bogus length word */770/* figure out how much we want to save */771unsigned int limit;772if (cinfo->unread_marker == (int) M_COM)773limit = marker->length_limit_COM;774else775limit = marker->length_limit_APPn[cinfo->unread_marker - (int) M_APP0];776if ((unsigned int) length < limit)777limit = (unsigned int) length;778/* allocate and initialize the marker item */779cur_marker = (jpeg_saved_marker_ptr)780(*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,781sizeof(struct jpeg_marker_struct) + limit);782cur_marker->next = NULL;783cur_marker->marker = (UINT8) cinfo->unread_marker;784cur_marker->original_length = (unsigned int) length;785cur_marker->data_length = limit;786/* data area is just beyond the jpeg_marker_struct */787data = cur_marker->data = (JOCTET *) (cur_marker + 1);788marker->cur_marker = cur_marker;789marker->bytes_read = 0;790bytes_read = 0;791data_length = limit;792} else {793/* deal with bogus length word */794bytes_read = data_length = 0;795data = NULL;796}797} else {798/* resume reading a marker */799bytes_read = marker->bytes_read;800data_length = cur_marker->data_length;801data = cur_marker->data + bytes_read;802}803804while (bytes_read < data_length) {805INPUT_SYNC(cinfo); /* move the restart point to here */806marker->bytes_read = bytes_read;807/* If there's not at least one byte in buffer, suspend */808MAKE_BYTE_AVAIL(cinfo, return FALSE);809/* Copy bytes with reasonable rapidity */810while (bytes_read < data_length && bytes_in_buffer > 0) {811*data++ = *next_input_byte++;812bytes_in_buffer--;813bytes_read++;814}815}816817/* Done reading what we want to read */818if (cur_marker != NULL) { /* will be NULL if bogus length word */819/* Add new marker to end of list */820if (cinfo->marker_list == NULL) {821cinfo->marker_list = cur_marker;822} else {823jpeg_saved_marker_ptr prev = cinfo->marker_list;824while (prev->next != NULL)825prev = prev->next;826prev->next = cur_marker;827}828/* Reset pointer & calc remaining data length */829data = cur_marker->data;830length = cur_marker->original_length - data_length;831}832/* Reset to initial state for next marker */833marker->cur_marker = NULL;834835/* Process the marker if interesting; else just make a generic trace msg */836switch (cinfo->unread_marker) {837case M_APP0:838examine_app0(cinfo, data, data_length, length);839break;840case M_APP14:841examine_app14(cinfo, data, data_length, length);842break;843default:844TRACEMS2(cinfo, 1, JTRC_MISC_MARKER, cinfo->unread_marker,845(int) (data_length + length));846break;847}848849/* skip any remaining data -- could be lots */850INPUT_SYNC(cinfo); /* do before skip_input_data */851if (length > 0)852(*cinfo->src->skip_input_data) (cinfo, (long) length);853854return TRUE;855}856857#endif /* SAVE_MARKERS_SUPPORTED */858859860METHODDEF(boolean)861skip_variable (j_decompress_ptr cinfo)862/* Skip over an unknown or uninteresting variable-length marker */863{864JLONG length;865INPUT_VARS(cinfo);866867INPUT_2BYTES(cinfo, length, return FALSE);868length -= 2;869870TRACEMS2(cinfo, 1, JTRC_MISC_MARKER, cinfo->unread_marker, (int) length);871872INPUT_SYNC(cinfo); /* do before skip_input_data */873if (length > 0)874(*cinfo->src->skip_input_data) (cinfo, (long) length);875876return TRUE;877}878879880/*881* Find the next JPEG marker, save it in cinfo->unread_marker.882* Returns FALSE if had to suspend before reaching a marker;883* in that case cinfo->unread_marker is unchanged.884*885* Note that the result might not be a valid marker code,886* but it will never be 0 or FF.887*/888889LOCAL(boolean)890next_marker (j_decompress_ptr cinfo)891{892int c;893INPUT_VARS(cinfo);894895for (;;) {896INPUT_BYTE(cinfo, c, return FALSE);897/* Skip any non-FF bytes.898* This may look a bit inefficient, but it will not occur in a valid file.899* We sync after each discarded byte so that a suspending data source900* can discard the byte from its buffer.901*/902while (c != 0xFF) {903cinfo->marker->discarded_bytes++;904INPUT_SYNC(cinfo);905INPUT_BYTE(cinfo, c, return FALSE);906}907/* This loop swallows any duplicate FF bytes. Extra FFs are legal as908* pad bytes, so don't count them in discarded_bytes. We assume there909* will not be so many consecutive FF bytes as to overflow a suspending910* data source's input buffer.911*/912do {913INPUT_BYTE(cinfo, c, return FALSE);914} while (c == 0xFF);915if (c != 0)916break; /* found a valid marker, exit loop */917/* Reach here if we found a stuffed-zero data sequence (FF/00).918* Discard it and loop back to try again.919*/920cinfo->marker->discarded_bytes += 2;921INPUT_SYNC(cinfo);922}923924if (cinfo->marker->discarded_bytes != 0) {925WARNMS2(cinfo, JWRN_EXTRANEOUS_DATA, cinfo->marker->discarded_bytes, c);926cinfo->marker->discarded_bytes = 0;927}928929cinfo->unread_marker = c;930931INPUT_SYNC(cinfo);932return TRUE;933}934935936LOCAL(boolean)937first_marker (j_decompress_ptr cinfo)938/* Like next_marker, but used to obtain the initial SOI marker. */939/* For this marker, we do not allow preceding garbage or fill; otherwise,940* we might well scan an entire input file before realizing it ain't JPEG.941* If an application wants to process non-JFIF files, it must seek to the942* SOI before calling the JPEG library.943*/944{945int c, c2;946INPUT_VARS(cinfo);947948INPUT_BYTE(cinfo, c, return FALSE);949INPUT_BYTE(cinfo, c2, return FALSE);950if (c != 0xFF || c2 != (int) M_SOI)951ERREXIT2(cinfo, JERR_NO_SOI, c, c2);952953cinfo->unread_marker = c2;954955INPUT_SYNC(cinfo);956return TRUE;957}958959960/*961* Read markers until SOS or EOI.962*963* Returns same codes as are defined for jpeg_consume_input:964* JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.965*/966967METHODDEF(int)968read_markers (j_decompress_ptr cinfo)969{970/* Outer loop repeats once for each marker. */971for (;;) {972/* Collect the marker proper, unless we already did. */973/* NB: first_marker() enforces the requirement that SOI appear first. */974if (cinfo->unread_marker == 0) {975if (! cinfo->marker->saw_SOI) {976if (! first_marker(cinfo))977return JPEG_SUSPENDED;978} else {979if (! next_marker(cinfo))980return JPEG_SUSPENDED;981}982}983/* At this point cinfo->unread_marker contains the marker code and the984* input point is just past the marker proper, but before any parameters.985* A suspension will cause us to return with this state still true.986*/987switch (cinfo->unread_marker) {988case M_SOI:989if (! get_soi(cinfo))990return JPEG_SUSPENDED;991break;992993case M_SOF0: /* Baseline */994case M_SOF1: /* Extended sequential, Huffman */995if (! get_sof(cinfo, FALSE, FALSE))996return JPEG_SUSPENDED;997break;998999case M_SOF2: /* Progressive, Huffman */1000if (! get_sof(cinfo, TRUE, FALSE))1001return JPEG_SUSPENDED;1002break;10031004case M_SOF9: /* Extended sequential, arithmetic */1005if (! get_sof(cinfo, FALSE, TRUE))1006return JPEG_SUSPENDED;1007break;10081009case M_SOF10: /* Progressive, arithmetic */1010if (! get_sof(cinfo, TRUE, TRUE))1011return JPEG_SUSPENDED;1012break;10131014/* Currently unsupported SOFn types */1015case M_SOF3: /* Lossless, Huffman */1016case M_SOF5: /* Differential sequential, Huffman */1017case M_SOF6: /* Differential progressive, Huffman */1018case M_SOF7: /* Differential lossless, Huffman */1019case M_JPG: /* Reserved for JPEG extensions */1020case M_SOF11: /* Lossless, arithmetic */1021case M_SOF13: /* Differential sequential, arithmetic */1022case M_SOF14: /* Differential progressive, arithmetic */1023case M_SOF15: /* Differential lossless, arithmetic */1024ERREXIT1(cinfo, JERR_SOF_UNSUPPORTED, cinfo->unread_marker);1025break;10261027case M_SOS:1028if (! get_sos(cinfo))1029return JPEG_SUSPENDED;1030cinfo->unread_marker = 0; /* processed the marker */1031return JPEG_REACHED_SOS;10321033case M_EOI:1034TRACEMS(cinfo, 1, JTRC_EOI);1035cinfo->unread_marker = 0; /* processed the marker */1036return JPEG_REACHED_EOI;10371038case M_DAC:1039if (! get_dac(cinfo))1040return JPEG_SUSPENDED;1041break;10421043case M_DHT:1044if (! get_dht(cinfo))1045return JPEG_SUSPENDED;1046break;10471048case M_DQT:1049if (! get_dqt(cinfo))1050return JPEG_SUSPENDED;1051break;10521053case M_DRI:1054if (! get_dri(cinfo))1055return JPEG_SUSPENDED;1056break;10571058case M_APP0:1059case M_APP1:1060case M_APP2:1061case M_APP3:1062case M_APP4:1063case M_APP5:1064case M_APP6:1065case M_APP7:1066case M_APP8:1067case M_APP9:1068case M_APP10:1069case M_APP11:1070case M_APP12:1071case M_APP13:1072case M_APP14:1073case M_APP15:1074if (! (*((my_marker_ptr) cinfo->marker)->process_APPn[1075cinfo->unread_marker - (int) M_APP0]) (cinfo))1076return JPEG_SUSPENDED;1077break;10781079case M_COM:1080if (! (*((my_marker_ptr) cinfo->marker)->process_COM) (cinfo))1081return JPEG_SUSPENDED;1082break;10831084case M_RST0: /* these are all parameterless */1085case M_RST1:1086case M_RST2:1087case M_RST3:1088case M_RST4:1089case M_RST5:1090case M_RST6:1091case M_RST7:1092case M_TEM:1093TRACEMS1(cinfo, 1, JTRC_PARMLESS_MARKER, cinfo->unread_marker);1094break;10951096case M_DNL: /* Ignore DNL ... perhaps the wrong thing */1097if (! skip_variable(cinfo))1098return JPEG_SUSPENDED;1099break;11001101default: /* must be DHP, EXP, JPGn, or RESn */1102/* For now, we treat the reserved markers as fatal errors since they are1103* likely to be used to signal incompatible JPEG Part 3 extensions.1104* Once the JPEG 3 version-number marker is well defined, this code1105* ought to change!1106*/1107ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, cinfo->unread_marker);1108break;1109}1110/* Successfully processed marker, so reset state variable */1111cinfo->unread_marker = 0;1112} /* end loop */1113}111411151116/*1117* Read a restart marker, which is expected to appear next in the datastream;1118* if the marker is not there, take appropriate recovery action.1119* Returns FALSE if suspension is required.1120*1121* This is called by the entropy decoder after it has read an appropriate1122* number of MCUs. cinfo->unread_marker may be nonzero if the entropy decoder1123* has already read a marker from the data source. Under normal conditions1124* cinfo->unread_marker will be reset to 0 before returning; if not reset,1125* it holds a marker which the decoder will be unable to read past.1126*/11271128METHODDEF(boolean)1129read_restart_marker (j_decompress_ptr cinfo)1130{1131/* Obtain a marker unless we already did. */1132/* Note that next_marker will complain if it skips any data. */1133if (cinfo->unread_marker == 0) {1134if (! next_marker(cinfo))1135return FALSE;1136}11371138if (cinfo->unread_marker ==1139((int) M_RST0 + cinfo->marker->next_restart_num)) {1140/* Normal case --- swallow the marker and let entropy decoder continue */1141TRACEMS1(cinfo, 3, JTRC_RST, cinfo->marker->next_restart_num);1142cinfo->unread_marker = 0;1143} else {1144/* Uh-oh, the restart markers have been messed up. */1145/* Let the data source manager determine how to resync. */1146if (! (*cinfo->src->resync_to_restart) (cinfo,1147cinfo->marker->next_restart_num))1148return FALSE;1149}11501151/* Update next-restart state */1152cinfo->marker->next_restart_num = (cinfo->marker->next_restart_num + 1) & 7;11531154return TRUE;1155}115611571158/*1159* This is the default resync_to_restart method for data source managers1160* to use if they don't have any better approach. Some data source managers1161* may be able to back up, or may have additional knowledge about the data1162* which permits a more intelligent recovery strategy; such managers would1163* presumably supply their own resync method.1164*1165* read_restart_marker calls resync_to_restart if it finds a marker other than1166* the restart marker it was expecting. (This code is *not* used unless1167* a nonzero restart interval has been declared.) cinfo->unread_marker is1168* the marker code actually found (might be anything, except 0 or FF).1169* The desired restart marker number (0..7) is passed as a parameter.1170* This routine is supposed to apply whatever error recovery strategy seems1171* appropriate in order to position the input stream to the next data segment.1172* Note that cinfo->unread_marker is treated as a marker appearing before1173* the current data-source input point; usually it should be reset to zero1174* before returning.1175* Returns FALSE if suspension is required.1176*1177* This implementation is substantially constrained by wanting to treat the1178* input as a data stream; this means we can't back up. Therefore, we have1179* only the following actions to work with:1180* 1. Simply discard the marker and let the entropy decoder resume at next1181* byte of file.1182* 2. Read forward until we find another marker, discarding intervening1183* data. (In theory we could look ahead within the current bufferload,1184* without having to discard data if we don't find the desired marker.1185* This idea is not implemented here, in part because it makes behavior1186* dependent on buffer size and chance buffer-boundary positions.)1187* 3. Leave the marker unread (by failing to zero cinfo->unread_marker).1188* This will cause the entropy decoder to process an empty data segment,1189* inserting dummy zeroes, and then we will reprocess the marker.1190*1191* #2 is appropriate if we think the desired marker lies ahead, while #3 is1192* appropriate if the found marker is a future restart marker (indicating1193* that we have missed the desired restart marker, probably because it got1194* corrupted).1195* We apply #2 or #3 if the found marker is a restart marker no more than1196* two counts behind or ahead of the expected one. We also apply #2 if the1197* found marker is not a legal JPEG marker code (it's certainly bogus data).1198* If the found marker is a restart marker more than 2 counts away, we do #11199* (too much risk that the marker is erroneous; with luck we will be able to1200* resync at some future point).1201* For any valid non-restart JPEG marker, we apply #3. This keeps us from1202* overrunning the end of a scan. An implementation limited to single-scan1203* files might find it better to apply #2 for markers other than EOI, since1204* any other marker would have to be bogus data in that case.1205*/12061207GLOBAL(boolean)1208jpeg_resync_to_restart (j_decompress_ptr cinfo, int desired)1209{1210int marker = cinfo->unread_marker;1211int action = 1;12121213/* Always put up a warning. */1214WARNMS2(cinfo, JWRN_MUST_RESYNC, marker, desired);12151216/* Outer loop handles repeated decision after scanning forward. */1217for (;;) {1218if (marker < (int) M_SOF0)1219action = 2; /* invalid marker */1220else if (marker < (int) M_RST0 || marker > (int) M_RST7)1221action = 3; /* valid non-restart marker */1222else {1223if (marker == ((int) M_RST0 + ((desired+1) & 7)) ||1224marker == ((int) M_RST0 + ((desired+2) & 7)))1225action = 3; /* one of the next two expected restarts */1226else if (marker == ((int) M_RST0 + ((desired-1) & 7)) ||1227marker == ((int) M_RST0 + ((desired-2) & 7)))1228action = 2; /* a prior restart, so advance */1229else1230action = 1; /* desired restart or too far away */1231}1232TRACEMS2(cinfo, 4, JTRC_RECOVERY_ACTION, marker, action);1233switch (action) {1234case 1:1235/* Discard marker and let entropy decoder resume processing. */1236cinfo->unread_marker = 0;1237return TRUE;1238case 2:1239/* Scan to the next marker, and repeat the decision loop. */1240if (! next_marker(cinfo))1241return FALSE;1242marker = cinfo->unread_marker;1243break;1244case 3:1245/* Return without advancing past this marker. */1246/* Entropy decoder will be forced to process an empty segment. */1247return TRUE;1248}1249} /* end loop */1250}125112521253/*1254* Reset marker processing state to begin a fresh datastream.1255*/12561257METHODDEF(void)1258reset_marker_reader (j_decompress_ptr cinfo)1259{1260my_marker_ptr marker = (my_marker_ptr) cinfo->marker;12611262cinfo->comp_info = NULL; /* until allocated by get_sof */1263cinfo->input_scan_number = 0; /* no SOS seen yet */1264cinfo->unread_marker = 0; /* no pending marker */1265marker->pub.saw_SOI = FALSE; /* set internal state too */1266marker->pub.saw_SOF = FALSE;1267marker->pub.discarded_bytes = 0;1268marker->cur_marker = NULL;1269}127012711272/*1273* Initialize the marker reader module.1274* This is called only once, when the decompression object is created.1275*/12761277GLOBAL(void)1278jinit_marker_reader (j_decompress_ptr cinfo)1279{1280my_marker_ptr marker;1281int i;12821283/* Create subobject in permanent pool */1284marker = (my_marker_ptr)1285(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,1286sizeof(my_marker_reader));1287cinfo->marker = (struct jpeg_marker_reader *) marker;1288/* Initialize public method pointers */1289marker->pub.reset_marker_reader = reset_marker_reader;1290marker->pub.read_markers = read_markers;1291marker->pub.read_restart_marker = read_restart_marker;1292/* Initialize COM/APPn processing.1293* By default, we examine and then discard APP0 and APP14,1294* but simply discard COM and all other APPn.1295*/1296marker->process_COM = skip_variable;1297marker->length_limit_COM = 0;1298for (i = 0; i < 16; i++) {1299marker->process_APPn[i] = skip_variable;1300marker->length_limit_APPn[i] = 0;1301}1302marker->process_APPn[0] = get_interesting_appn;1303marker->process_APPn[14] = get_interesting_appn;1304/* Reset marker processing state */1305reset_marker_reader(cinfo);1306}130713081309/*1310* Control saving of COM and APPn markers into marker_list.1311*/13121313#ifdef SAVE_MARKERS_SUPPORTED13141315GLOBAL(void)1316jpeg_save_markers (j_decompress_ptr cinfo, int marker_code,1317unsigned int length_limit)1318{1319my_marker_ptr marker = (my_marker_ptr) cinfo->marker;1320long maxlength;1321jpeg_marker_parser_method processor;13221323/* Length limit mustn't be larger than what we can allocate1324* (should only be a concern in a 16-bit environment).1325*/1326maxlength = cinfo->mem->max_alloc_chunk - sizeof(struct jpeg_marker_struct);1327if (((long) length_limit) > maxlength)1328length_limit = (unsigned int) maxlength;13291330/* Choose processor routine to use.1331* APP0/APP14 have special requirements.1332*/1333if (length_limit) {1334processor = save_marker;1335/* If saving APP0/APP14, save at least enough for our internal use. */1336if (marker_code == (int) M_APP0 && length_limit < APP0_DATA_LEN)1337length_limit = APP0_DATA_LEN;1338else if (marker_code == (int) M_APP14 && length_limit < APP14_DATA_LEN)1339length_limit = APP14_DATA_LEN;1340} else {1341processor = skip_variable;1342/* If discarding APP0/APP14, use our regular on-the-fly processor. */1343if (marker_code == (int) M_APP0 || marker_code == (int) M_APP14)1344processor = get_interesting_appn;1345}13461347if (marker_code == (int) M_COM) {1348marker->process_COM = processor;1349marker->length_limit_COM = length_limit;1350} else if (marker_code >= (int) M_APP0 && marker_code <= (int) M_APP15) {1351marker->process_APPn[marker_code - (int) M_APP0] = processor;1352marker->length_limit_APPn[marker_code - (int) M_APP0] = length_limit;1353} else1354ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, marker_code);1355}13561357#endif /* SAVE_MARKERS_SUPPORTED */135813591360/*1361* Install a special processing method for COM or APPn markers.1362*/13631364GLOBAL(void)1365jpeg_set_marker_processor (j_decompress_ptr cinfo, int marker_code,1366jpeg_marker_parser_method routine)1367{1368my_marker_ptr marker = (my_marker_ptr) cinfo->marker;13691370if (marker_code == (int) M_COM)1371marker->process_COM = routine;1372else if (marker_code >= (int) M_APP0 && marker_code <= (int) M_APP15)1373marker->process_APPn[marker_code - (int) M_APP0] = routine;1374else1375ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, marker_code);1376}137713781379