Back to index

lightning-sunbird  0.9+nobinonly
jdmarker.c
Go to the documentation of this file.
00001 /*
00002  * jdmarker.c
00003  *
00004  * Copyright (C) 1991-1998, Thomas G. Lane.
00005  * This file is part of the Independent JPEG Group's software.
00006  * For conditions of distribution and use, see the accompanying README file.
00007  *
00008  * This file contains routines to decode JPEG datastream markers.
00009  * Most of the complexity arises from our desire to support input
00010  * suspension: if not all of the data for a marker is available,
00011  * we must exit back to the application.  On resumption, we reprocess
00012  * the marker.
00013  */
00014 
00015 #define JPEG_INTERNALS
00016 #include "jinclude.h"
00017 #include "jpeglib.h"
00018 
00019 
00020 typedef enum {                     /* JPEG marker codes */
00021   M_SOF0  = 0xc0,
00022   M_SOF1  = 0xc1,
00023   M_SOF2  = 0xc2,
00024   M_SOF3  = 0xc3,
00025   
00026   M_SOF5  = 0xc5,
00027   M_SOF6  = 0xc6,
00028   M_SOF7  = 0xc7,
00029   
00030   M_JPG   = 0xc8,
00031   M_SOF9  = 0xc9,
00032   M_SOF10 = 0xca,
00033   M_SOF11 = 0xcb,
00034   
00035   M_SOF13 = 0xcd,
00036   M_SOF14 = 0xce,
00037   M_SOF15 = 0xcf,
00038   
00039   M_DHT   = 0xc4,
00040   
00041   M_DAC   = 0xcc,
00042   
00043   M_RST0  = 0xd0,
00044   M_RST1  = 0xd1,
00045   M_RST2  = 0xd2,
00046   M_RST3  = 0xd3,
00047   M_RST4  = 0xd4,
00048   M_RST5  = 0xd5,
00049   M_RST6  = 0xd6,
00050   M_RST7  = 0xd7,
00051   
00052   M_SOI   = 0xd8,
00053   M_EOI   = 0xd9,
00054   M_SOS   = 0xda,
00055   M_DQT   = 0xdb,
00056   M_DNL   = 0xdc,
00057   M_DRI   = 0xdd,
00058   M_DHP   = 0xde,
00059   M_EXP   = 0xdf,
00060   
00061   M_APP0  = 0xe0,
00062   M_APP1  = 0xe1,
00063   M_APP2  = 0xe2,
00064   M_APP3  = 0xe3,
00065   M_APP4  = 0xe4,
00066   M_APP5  = 0xe5,
00067   M_APP6  = 0xe6,
00068   M_APP7  = 0xe7,
00069   M_APP8  = 0xe8,
00070   M_APP9  = 0xe9,
00071   M_APP10 = 0xea,
00072   M_APP11 = 0xeb,
00073   M_APP12 = 0xec,
00074   M_APP13 = 0xed,
00075   M_APP14 = 0xee,
00076   M_APP15 = 0xef,
00077   
00078   M_JPG0  = 0xf0,
00079   M_JPG13 = 0xfd,
00080   M_COM   = 0xfe,
00081   
00082   M_TEM   = 0x01
00083 } JPEG_MARKER;
00084 
00085 
00086 /* Private state */
00087 
00088 typedef struct {
00089   struct jpeg_marker_reader pub; /* public fields */
00090 
00091   /* Application-overridable marker processing methods */
00092   jpeg_marker_parser_method process_COM;
00093   jpeg_marker_parser_method process_APPn[16];
00094 
00095   /* Limit on marker data length to save for each marker type */
00096   unsigned int length_limit_COM;
00097   unsigned int length_limit_APPn[16];
00098 
00099   /* Status of COM/APPn marker saving */
00100   jpeg_saved_marker_ptr cur_marker;       /* NULL if not processing a marker */
00101   unsigned int bytes_read;         /* data bytes read so far in marker */
00102   /* Note: cur_marker is not linked into marker_list until it's all read. */
00103 } my_marker_reader;
00104 
00105 typedef my_marker_reader * my_marker_ptr;
00106 
00107 
00108 /*
00109  * Macros for fetching data from the data source module.
00110  *
00111  * At all times, cinfo->src->next_input_byte and ->bytes_in_buffer reflect
00112  * the current restart point; we update them only when we have reached a
00113  * suitable place to restart if a suspension occurs.
00114  */
00115 
00116 /* Declare and initialize local copies of input pointer/count */
00117 #define INPUT_VARS(cinfo)  \
00118        struct jpeg_source_mgr * datasrc = (cinfo)->src;  \
00119        const JOCTET * next_input_byte = datasrc->next_input_byte;  \
00120        size_t bytes_in_buffer = datasrc->bytes_in_buffer
00121 
00122 /* Unload the local copies --- do this only at a restart boundary */
00123 #define INPUT_SYNC(cinfo)  \
00124        ( datasrc->next_input_byte = next_input_byte,  \
00125          datasrc->bytes_in_buffer = bytes_in_buffer )
00126 
00127 /* Reload the local copies --- used only in MAKE_BYTE_AVAIL */
00128 #define INPUT_RELOAD(cinfo)  \
00129        ( next_input_byte = datasrc->next_input_byte,  \
00130          bytes_in_buffer = datasrc->bytes_in_buffer )
00131 
00132 /* Internal macro for INPUT_BYTE and INPUT_2BYTES: make a byte available.
00133  * Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
00134  * but we must reload the local copies after a successful fill.
00135  */
00136 #define MAKE_BYTE_AVAIL(cinfo,action)  \
00137        if (bytes_in_buffer == 0) {  \
00138          if (! (*datasrc->fill_input_buffer) (cinfo))  \
00139            { action; }  \
00140          INPUT_RELOAD(cinfo);  \
00141        }
00142 
00143 /* Read a byte into variable V.
00144  * If must suspend, take the specified action (typically "return FALSE").
00145  */
00146 #define INPUT_BYTE(cinfo,V,action)  \
00147        MAKESTMT( MAKE_BYTE_AVAIL(cinfo,action); \
00148                 bytes_in_buffer--; \
00149                 V = GETJOCTET(*next_input_byte++); )
00150 
00151 /* As above, but read two bytes interpreted as an unsigned 16-bit integer.
00152  * V should be declared unsigned int or perhaps INT32.
00153  */
00154 #define INPUT_2BYTES(cinfo,V,action)  \
00155        MAKESTMT( MAKE_BYTE_AVAIL(cinfo,action); \
00156                 bytes_in_buffer--; \
00157                 V = ((unsigned int) GETJOCTET(*next_input_byte++)) << 8; \
00158                 MAKE_BYTE_AVAIL(cinfo,action); \
00159                 bytes_in_buffer--; \
00160                 V += GETJOCTET(*next_input_byte++); )
00161 
00162 
00163 /*
00164  * Routines to process JPEG markers.
00165  *
00166  * Entry condition: JPEG marker itself has been read and its code saved
00167  *   in cinfo->unread_marker; input restart point is just after the marker.
00168  *
00169  * Exit: if return TRUE, have read and processed any parameters, and have
00170  *   updated the restart point to point after the parameters.
00171  *   If return FALSE, was forced to suspend before reaching end of
00172  *   marker parameters; restart point has not been moved.  Same routine
00173  *   will be called again after application supplies more input data.
00174  *
00175  * This approach to suspension assumes that all of a marker's parameters
00176  * can fit into a single input bufferload.  This should hold for "normal"
00177  * markers.  Some COM/APPn markers might have large parameter segments
00178  * that might not fit.  If we are simply dropping such a marker, we use
00179  * skip_input_data to get past it, and thereby put the problem on the
00180  * source manager's shoulders.  If we are saving the marker's contents
00181  * into memory, we use a slightly different convention: when forced to
00182  * suspend, the marker processor updates the restart point to the end of
00183  * what it's consumed (ie, the end of the buffer) before returning FALSE.
00184  * On resumption, cinfo->unread_marker still contains the marker code,
00185  * but the data source will point to the next chunk of marker data.
00186  * The marker processor must retain internal state to deal with this.
00187  *
00188  * Note that we don't bother to avoid duplicate trace messages if a
00189  * suspension occurs within marker parameters.  Other side effects
00190  * require more care.
00191  */
00192 
00193 
00194 LOCAL(boolean)
00195 get_soi (j_decompress_ptr cinfo)
00196 /* Process an SOI marker */
00197 {
00198   int i;
00199   
00200   TRACEMS(cinfo, 1, JTRC_SOI);
00201 
00202   if (cinfo->marker->saw_SOI)
00203     ERREXIT(cinfo, JERR_SOI_DUPLICATE);
00204 
00205   /* Reset all parameters that are defined to be reset by SOI */
00206 
00207   for (i = 0; i < NUM_ARITH_TBLS; i++) {
00208     cinfo->arith_dc_L[i] = 0;
00209     cinfo->arith_dc_U[i] = 1;
00210     cinfo->arith_ac_K[i] = 5;
00211   }
00212   cinfo->restart_interval = 0;
00213 
00214   /* Set initial assumptions for colorspace etc */
00215 
00216   cinfo->jpeg_color_space = JCS_UNKNOWN;
00217   cinfo->CCIR601_sampling = FALSE; /* Assume non-CCIR sampling??? */
00218 
00219   cinfo->saw_JFIF_marker = FALSE;
00220   cinfo->JFIF_major_version = 1; /* set default JFIF APP0 values */
00221   cinfo->JFIF_minor_version = 1;
00222   cinfo->density_unit = 0;
00223   cinfo->X_density = 1;
00224   cinfo->Y_density = 1;
00225   cinfo->saw_Adobe_marker = FALSE;
00226   cinfo->Adobe_transform = 0;
00227 
00228   cinfo->marker->saw_SOI = TRUE;
00229 
00230   return TRUE;
00231 }
00232 
00233 
00234 LOCAL(boolean)
00235 get_sof (j_decompress_ptr cinfo, boolean is_prog, boolean is_arith)
00236 /* Process a SOFn marker */
00237 {
00238   INT32 length;
00239   int c, ci;
00240   jpeg_component_info * compptr;
00241   INPUT_VARS(cinfo);
00242 
00243   cinfo->progressive_mode = is_prog;
00244   cinfo->arith_code = is_arith;
00245 
00246   INPUT_2BYTES(cinfo, length, return FALSE);
00247 
00248   INPUT_BYTE(cinfo, cinfo->data_precision, return FALSE);
00249   INPUT_2BYTES(cinfo, cinfo->image_height, return FALSE);
00250   INPUT_2BYTES(cinfo, cinfo->image_width, return FALSE);
00251   INPUT_BYTE(cinfo, cinfo->num_components, return FALSE);
00252 
00253   length -= 8;
00254 
00255   TRACEMS4(cinfo, 1, JTRC_SOF, cinfo->unread_marker,
00256           (int) cinfo->image_width, (int) cinfo->image_height,
00257           cinfo->num_components);
00258 
00259   if (cinfo->marker->saw_SOF)
00260     ERREXIT(cinfo, JERR_SOF_DUPLICATE);
00261 
00262   /* We don't support files in which the image height is initially specified */
00263   /* as 0 and is later redefined by DNL.  As long as we have to check that,  */
00264   /* might as well have a general sanity check. */
00265   if (cinfo->image_height <= 0 || cinfo->image_width <= 0
00266       || cinfo->num_components <= 0)
00267     ERREXIT(cinfo, JERR_EMPTY_IMAGE);
00268 
00269   if (length != (cinfo->num_components * 3))
00270     ERREXIT(cinfo, JERR_BAD_LENGTH);
00271 
00272   if (cinfo->comp_info == NULL)    /* do only once, even if suspend */
00273     cinfo->comp_info = (jpeg_component_info *) (*cinfo->mem->alloc_small)
00274                      ((j_common_ptr) cinfo, JPOOL_IMAGE,
00275                       cinfo->num_components * SIZEOF(jpeg_component_info));
00276   
00277   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
00278        ci++, compptr++) {
00279     compptr->component_index = ci;
00280     INPUT_BYTE(cinfo, compptr->component_id, return FALSE);
00281     INPUT_BYTE(cinfo, c, return FALSE);
00282     compptr->h_samp_factor = (c >> 4) & 15;
00283     compptr->v_samp_factor = (c     ) & 15;
00284     INPUT_BYTE(cinfo, compptr->quant_tbl_no, return FALSE);
00285 
00286     TRACEMS4(cinfo, 1, JTRC_SOF_COMPONENT,
00287             compptr->component_id, compptr->h_samp_factor,
00288             compptr->v_samp_factor, compptr->quant_tbl_no);
00289   }
00290 
00291   cinfo->marker->saw_SOF = TRUE;
00292 
00293   INPUT_SYNC(cinfo);
00294   return TRUE;
00295 }
00296 
00297 
00298 LOCAL(boolean)
00299 get_sos (j_decompress_ptr cinfo)
00300 /* Process a SOS marker */
00301 {
00302   INT32 length;
00303   int i, ci, n, c, cc;
00304   jpeg_component_info * compptr;
00305   INPUT_VARS(cinfo);
00306 
00307   if (! cinfo->marker->saw_SOF)
00308     ERREXIT(cinfo, JERR_SOS_NO_SOF);
00309 
00310   INPUT_2BYTES(cinfo, length, return FALSE);
00311 
00312   INPUT_BYTE(cinfo, n, return FALSE); /* Number of components */
00313 
00314   TRACEMS1(cinfo, 1, JTRC_SOS, n);
00315 
00316   if (length != (n * 2 + 6) || n < 1 || n > MAX_COMPS_IN_SCAN)
00317     ERREXIT(cinfo, JERR_BAD_LENGTH);
00318 
00319   cinfo->comps_in_scan = n;
00320 
00321   /* Collect the component-spec parameters */
00322 
00323   for (i = 0; i < n; i++) {
00324     INPUT_BYTE(cinfo, cc, return FALSE);
00325     INPUT_BYTE(cinfo, c, return FALSE);
00326     
00327     for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
00328         ci++, compptr++) {
00329       if (cc == compptr->component_id)
00330        goto id_found;
00331     }
00332 
00333     ERREXIT1(cinfo, JERR_BAD_COMPONENT_ID, cc);
00334 
00335   id_found:
00336 
00337     cinfo->cur_comp_info[i] = compptr;
00338     compptr->dc_tbl_no = (c >> 4) & 15;
00339     compptr->ac_tbl_no = (c     ) & 15;
00340     
00341     TRACEMS3(cinfo, 1, JTRC_SOS_COMPONENT, cc,
00342             compptr->dc_tbl_no, compptr->ac_tbl_no);
00343   }
00344 
00345   /* Collect the additional scan parameters Ss, Se, Ah/Al. */
00346   INPUT_BYTE(cinfo, c, return FALSE);
00347   cinfo->Ss = c;
00348   INPUT_BYTE(cinfo, c, return FALSE);
00349   cinfo->Se = c;
00350   INPUT_BYTE(cinfo, c, return FALSE);
00351   cinfo->Ah = (c >> 4) & 15;
00352   cinfo->Al = (c     ) & 15;
00353 
00354   TRACEMS4(cinfo, 1, JTRC_SOS_PARAMS, cinfo->Ss, cinfo->Se,
00355           cinfo->Ah, cinfo->Al);
00356 
00357   /* Prepare to scan data & restart markers */
00358   cinfo->marker->next_restart_num = 0;
00359 
00360   /* Count another SOS marker */
00361   cinfo->input_scan_number++;
00362 
00363   INPUT_SYNC(cinfo);
00364   return TRUE;
00365 }
00366 
00367 
00368 #ifdef D_ARITH_CODING_SUPPORTED
00369 
00370 LOCAL(boolean)
00371 get_dac (j_decompress_ptr cinfo)
00372 /* Process a DAC marker */
00373 {
00374   INT32 length;
00375   int index, val;
00376   INPUT_VARS(cinfo);
00377 
00378   INPUT_2BYTES(cinfo, length, return FALSE);
00379   length -= 2;
00380   
00381   while (length > 0) {
00382     INPUT_BYTE(cinfo, index, return FALSE);
00383     INPUT_BYTE(cinfo, val, return FALSE);
00384 
00385     length -= 2;
00386 
00387     TRACEMS2(cinfo, 1, JTRC_DAC, index, val);
00388 
00389     if (index < 0 || index >= (2*NUM_ARITH_TBLS))
00390       ERREXIT1(cinfo, JERR_DAC_INDEX, index);
00391 
00392     if (index >= NUM_ARITH_TBLS) { /* define AC table */
00393       cinfo->arith_ac_K[index-NUM_ARITH_TBLS] = (UINT8) val;
00394     } else {                /* define DC table */
00395       cinfo->arith_dc_L[index] = (UINT8) (val & 0x0F);
00396       cinfo->arith_dc_U[index] = (UINT8) (val >> 4);
00397       if (cinfo->arith_dc_L[index] > cinfo->arith_dc_U[index])
00398        ERREXIT1(cinfo, JERR_DAC_VALUE, val);
00399     }
00400   }
00401 
00402   if (length != 0)
00403     ERREXIT(cinfo, JERR_BAD_LENGTH);
00404 
00405   INPUT_SYNC(cinfo);
00406   return TRUE;
00407 }
00408 
00409 #else /* ! D_ARITH_CODING_SUPPORTED */
00410 
00411 #define get_dac(cinfo)  skip_variable(cinfo)
00412 
00413 #endif /* D_ARITH_CODING_SUPPORTED */
00414 
00415 
00416 LOCAL(boolean)
00417 get_dht (j_decompress_ptr cinfo)
00418 /* Process a DHT marker */
00419 {
00420   INT32 length;
00421   UINT8 bits[17];
00422   UINT8 huffval[256];
00423   int i, index, count;
00424   JHUFF_TBL **htblptr;
00425   INPUT_VARS(cinfo);
00426 
00427   INPUT_2BYTES(cinfo, length, return FALSE);
00428   length -= 2;
00429   
00430   while (length > 16) {
00431     INPUT_BYTE(cinfo, index, return FALSE);
00432 
00433     TRACEMS1(cinfo, 1, JTRC_DHT, index);
00434       
00435     bits[0] = 0;
00436     count = 0;
00437     for (i = 1; i <= 16; i++) {
00438       INPUT_BYTE(cinfo, bits[i], return FALSE);
00439       count += bits[i];
00440     }
00441 
00442     length -= 1 + 16;
00443 
00444     TRACEMS8(cinfo, 2, JTRC_HUFFBITS,
00445             bits[1], bits[2], bits[3], bits[4],
00446             bits[5], bits[6], bits[7], bits[8]);
00447     TRACEMS8(cinfo, 2, JTRC_HUFFBITS,
00448             bits[9], bits[10], bits[11], bits[12],
00449             bits[13], bits[14], bits[15], bits[16]);
00450 
00451     /* Here we just do minimal validation of the counts to avoid walking
00452      * off the end of our table space.  jdhuff.c will check more carefully.
00453      */
00454     if (count > 256 || ((INT32) count) > length)
00455       ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
00456 
00457     for (i = 0; i < count; i++)
00458       INPUT_BYTE(cinfo, huffval[i], return FALSE);
00459 
00460     length -= count;
00461 
00462     if (index & 0x10) {            /* AC table definition */
00463       index -= 0x10;
00464       htblptr = &cinfo->ac_huff_tbl_ptrs[index];
00465     } else {                /* DC table definition */
00466       htblptr = &cinfo->dc_huff_tbl_ptrs[index];
00467     }
00468 
00469     if (index < 0 || index >= NUM_HUFF_TBLS)
00470       ERREXIT1(cinfo, JERR_DHT_INDEX, index);
00471 
00472     if (*htblptr == NULL)
00473       *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
00474   
00475     MEMCOPY((*htblptr)->bits, bits, SIZEOF((*htblptr)->bits));
00476     MEMCOPY((*htblptr)->huffval, huffval, SIZEOF((*htblptr)->huffval));
00477   }
00478 
00479   if (length != 0)
00480     ERREXIT(cinfo, JERR_BAD_LENGTH);
00481 
00482   INPUT_SYNC(cinfo);
00483   return TRUE;
00484 }
00485 
00486 
00487 LOCAL(boolean)
00488 get_dqt (j_decompress_ptr cinfo)
00489 /* Process a DQT marker */
00490 {
00491   INT32 length;
00492   int n, i, prec;
00493   unsigned int tmp;
00494   JQUANT_TBL *quant_ptr;
00495   INPUT_VARS(cinfo);
00496 
00497   INPUT_2BYTES(cinfo, length, return FALSE);
00498   length -= 2;
00499 
00500   while (length > 0) {
00501     INPUT_BYTE(cinfo, n, return FALSE);
00502     prec = n >> 4;
00503     n &= 0x0F;
00504 
00505     TRACEMS2(cinfo, 1, JTRC_DQT, n, prec);
00506 
00507     if (n >= NUM_QUANT_TBLS)
00508       ERREXIT1(cinfo, JERR_DQT_INDEX, n);
00509       
00510     if (cinfo->quant_tbl_ptrs[n] == NULL)
00511       cinfo->quant_tbl_ptrs[n] = jpeg_alloc_quant_table((j_common_ptr) cinfo);
00512     quant_ptr = cinfo->quant_tbl_ptrs[n];
00513 
00514     for (i = 0; i < DCTSIZE2; i++) {
00515       if (prec)
00516        INPUT_2BYTES(cinfo, tmp, return FALSE);
00517       else
00518        INPUT_BYTE(cinfo, tmp, return FALSE);
00519       /* We convert the zigzag-order table to natural array order. */
00520       quant_ptr->quantval[jpeg_natural_order[i]] = (UINT16) tmp;
00521     }
00522 
00523     if (cinfo->err->trace_level >= 2) {
00524       for (i = 0; i < DCTSIZE2; i += 8) {
00525        TRACEMS8(cinfo, 2, JTRC_QUANTVALS,
00526                quant_ptr->quantval[i],   quant_ptr->quantval[i+1],
00527                quant_ptr->quantval[i+2], quant_ptr->quantval[i+3],
00528                quant_ptr->quantval[i+4], quant_ptr->quantval[i+5],
00529                quant_ptr->quantval[i+6], quant_ptr->quantval[i+7]);
00530       }
00531     }
00532 
00533     length -= DCTSIZE2+1;
00534     if (prec) length -= DCTSIZE2;
00535   }
00536 
00537   if (length != 0)
00538     ERREXIT(cinfo, JERR_BAD_LENGTH);
00539 
00540   INPUT_SYNC(cinfo);
00541   return TRUE;
00542 }
00543 
00544 
00545 LOCAL(boolean)
00546 get_dri (j_decompress_ptr cinfo)
00547 /* Process a DRI marker */
00548 {
00549   INT32 length;
00550   unsigned int tmp;
00551   INPUT_VARS(cinfo);
00552 
00553   INPUT_2BYTES(cinfo, length, return FALSE);
00554   
00555   if (length != 4)
00556     ERREXIT(cinfo, JERR_BAD_LENGTH);
00557 
00558   INPUT_2BYTES(cinfo, tmp, return FALSE);
00559 
00560   TRACEMS1(cinfo, 1, JTRC_DRI, tmp);
00561 
00562   cinfo->restart_interval = tmp;
00563 
00564   INPUT_SYNC(cinfo);
00565   return TRUE;
00566 }
00567 
00568 
00569 /*
00570  * Routines for processing APPn and COM markers.
00571  * These are either saved in memory or discarded, per application request.
00572  * APP0 and APP14 are specially checked to see if they are
00573  * JFIF and Adobe markers, respectively.
00574  */
00575 
00576 #define APP0_DATA_LEN       14     /* Length of interesting data in APP0 */
00577 #define APP14_DATA_LEN      12     /* Length of interesting data in APP14 */
00578 #define APPN_DATA_LEN       14     /* Must be the largest of the above!! */
00579 
00580 
00581 LOCAL(void)
00582 examine_app0 (j_decompress_ptr cinfo, JOCTET FAR * data,
00583              unsigned int datalen, INT32 remaining)
00584 /* Examine first few bytes from an APP0.
00585  * Take appropriate action if it is a JFIF marker.
00586  * datalen is # of bytes at data[], remaining is length of rest of marker data.
00587  */
00588 {
00589   INT32 totallen = (INT32) datalen + remaining;
00590 
00591   if (datalen >= APP0_DATA_LEN &&
00592       GETJOCTET(data[0]) == 0x4A &&
00593       GETJOCTET(data[1]) == 0x46 &&
00594       GETJOCTET(data[2]) == 0x49 &&
00595       GETJOCTET(data[3]) == 0x46 &&
00596       GETJOCTET(data[4]) == 0) {
00597     /* Found JFIF APP0 marker: save info */
00598     cinfo->saw_JFIF_marker = TRUE;
00599     cinfo->JFIF_major_version = GETJOCTET(data[5]);
00600     cinfo->JFIF_minor_version = GETJOCTET(data[6]);
00601     cinfo->density_unit = GETJOCTET(data[7]);
00602     cinfo->X_density = (GETJOCTET(data[8]) << 8) + GETJOCTET(data[9]);
00603     cinfo->Y_density = (GETJOCTET(data[10]) << 8) + GETJOCTET(data[11]);
00604     /* Check version.
00605      * Major version must be 1, anything else signals an incompatible change.
00606      * (We used to treat this as an error, but now it's a nonfatal warning,
00607      * because some bozo at Hijaak couldn't read the spec.)
00608      * Minor version should be 0..2, but process anyway if newer.
00609      */
00610     if (cinfo->JFIF_major_version != 1)
00611       WARNMS2(cinfo, JWRN_JFIF_MAJOR,
00612              cinfo->JFIF_major_version, cinfo->JFIF_minor_version);
00613     /* Generate trace messages */
00614     TRACEMS5(cinfo, 1, JTRC_JFIF,
00615             cinfo->JFIF_major_version, cinfo->JFIF_minor_version,
00616             cinfo->X_density, cinfo->Y_density, cinfo->density_unit);
00617     /* Validate thumbnail dimensions and issue appropriate messages */
00618     if (GETJOCTET(data[12]) | GETJOCTET(data[13]))
00619       TRACEMS2(cinfo, 1, JTRC_JFIF_THUMBNAIL,
00620               GETJOCTET(data[12]), GETJOCTET(data[13]));
00621     totallen -= APP0_DATA_LEN;
00622     if (totallen !=
00623        ((INT32)GETJOCTET(data[12]) * (INT32)GETJOCTET(data[13]) * (INT32) 3))
00624       TRACEMS1(cinfo, 1, JTRC_JFIF_BADTHUMBNAILSIZE, (int) totallen);
00625   } else if (datalen >= 6 &&
00626       GETJOCTET(data[0]) == 0x4A &&
00627       GETJOCTET(data[1]) == 0x46 &&
00628       GETJOCTET(data[2]) == 0x58 &&
00629       GETJOCTET(data[3]) == 0x58 &&
00630       GETJOCTET(data[4]) == 0) {
00631     /* Found JFIF "JFXX" extension APP0 marker */
00632     /* The library doesn't actually do anything with these,
00633      * but we try to produce a helpful trace message.
00634      */
00635     switch (GETJOCTET(data[5])) {
00636     case 0x10:
00637       TRACEMS1(cinfo, 1, JTRC_THUMB_JPEG, (int) totallen);
00638       break;
00639     case 0x11:
00640       TRACEMS1(cinfo, 1, JTRC_THUMB_PALETTE, (int) totallen);
00641       break;
00642     case 0x13:
00643       TRACEMS1(cinfo, 1, JTRC_THUMB_RGB, (int) totallen);
00644       break;
00645     default:
00646       TRACEMS2(cinfo, 1, JTRC_JFIF_EXTENSION,
00647               GETJOCTET(data[5]), (int) totallen);
00648       break;
00649     }
00650   } else {
00651     /* Start of APP0 does not match "JFIF" or "JFXX", or too short */
00652     TRACEMS1(cinfo, 1, JTRC_APP0, (int) totallen);
00653   }
00654 }
00655 
00656 
00657 LOCAL(void)
00658 examine_app14 (j_decompress_ptr cinfo, JOCTET FAR * data,
00659               unsigned int datalen, INT32 remaining)
00660 /* Examine first few bytes from an APP14.
00661  * Take appropriate action if it is an Adobe marker.
00662  * datalen is # of bytes at data[], remaining is length of rest of marker data.
00663  */
00664 {
00665   unsigned int version, flags0, flags1, transform;
00666 
00667   if (datalen >= APP14_DATA_LEN &&
00668       GETJOCTET(data[0]) == 0x41 &&
00669       GETJOCTET(data[1]) == 0x64 &&
00670       GETJOCTET(data[2]) == 0x6F &&
00671       GETJOCTET(data[3]) == 0x62 &&
00672       GETJOCTET(data[4]) == 0x65) {
00673     /* Found Adobe APP14 marker */
00674     version = (GETJOCTET(data[5]) << 8) + GETJOCTET(data[6]);
00675     flags0 = (GETJOCTET(data[7]) << 8) + GETJOCTET(data[8]);
00676     flags1 = (GETJOCTET(data[9]) << 8) + GETJOCTET(data[10]);
00677     transform = GETJOCTET(data[11]);
00678     TRACEMS4(cinfo, 1, JTRC_ADOBE, version, flags0, flags1, transform);
00679     cinfo->saw_Adobe_marker = TRUE;
00680     cinfo->Adobe_transform = (UINT8) transform;
00681   } else {
00682     /* Start of APP14 does not match "Adobe", or too short */
00683     TRACEMS1(cinfo, 1, JTRC_APP14, (int) (datalen + remaining));
00684   }
00685 }
00686 
00687 
00688 METHODDEF(boolean)
00689 get_interesting_appn (j_decompress_ptr cinfo)
00690 /* Process an APP0 or APP14 marker without saving it */
00691 {
00692   INT32 length;
00693   JOCTET b[APPN_DATA_LEN];
00694   unsigned int i, numtoread;
00695   INPUT_VARS(cinfo);
00696 
00697   INPUT_2BYTES(cinfo, length, return FALSE);
00698   length -= 2;
00699 
00700   /* get the interesting part of the marker data */
00701   if (length >= APPN_DATA_LEN)
00702     numtoread = APPN_DATA_LEN;
00703   else if (length > 0)
00704     numtoread = (unsigned int) length;
00705   else
00706     numtoread = 0;
00707   for (i = 0; i < numtoread; i++)
00708     INPUT_BYTE(cinfo, b[i], return FALSE);
00709   length -= numtoread;
00710 
00711   /* process it */
00712   switch (cinfo->unread_marker) {
00713   case M_APP0:
00714     examine_app0(cinfo, (JOCTET FAR *) b, numtoread, length);
00715     break;
00716   case M_APP14:
00717     examine_app14(cinfo, (JOCTET FAR *) b, numtoread, length);
00718     break;
00719   default:
00720     /* can't get here unless jpeg_save_markers chooses wrong processor */
00721     ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, cinfo->unread_marker);
00722     break;
00723   }
00724 
00725   /* skip any remaining data -- could be lots */
00726   INPUT_SYNC(cinfo);
00727   if (length > 0)
00728     (*cinfo->src->skip_input_data) (cinfo, (long) length);
00729 
00730   return TRUE;
00731 }
00732 
00733 
00734 #ifdef SAVE_MARKERS_SUPPORTED
00735 
00736 METHODDEF(boolean)
00737 save_marker (j_decompress_ptr cinfo)
00738 /* Save an APPn or COM marker into the marker list */
00739 {
00740   my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
00741   jpeg_saved_marker_ptr cur_marker = marker->cur_marker;
00742   unsigned int bytes_read, data_length;
00743   JOCTET FAR * data;
00744   INT32 length = 0;
00745   INPUT_VARS(cinfo);
00746 
00747   if (cur_marker == NULL) {
00748     /* begin reading a marker */
00749     INPUT_2BYTES(cinfo, length, return FALSE);
00750     length -= 2;
00751     if (length >= 0) {             /* watch out for bogus length word */
00752       /* figure out how much we want to save */
00753       unsigned int limit;
00754       if (cinfo->unread_marker == (int) M_COM)
00755        limit = marker->length_limit_COM;
00756       else
00757        limit = marker->length_limit_APPn[cinfo->unread_marker - (int) M_APP0];
00758       if ((unsigned int) length < limit)
00759        limit = (unsigned int) length;
00760       /* allocate and initialize the marker item */
00761       cur_marker = (jpeg_saved_marker_ptr)
00762        (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
00763                                 SIZEOF(struct jpeg_marker_struct) + limit);
00764       cur_marker->next = NULL;
00765       cur_marker->marker = (UINT8) cinfo->unread_marker;
00766       cur_marker->original_length = (unsigned int) length;
00767       cur_marker->data_length = limit;
00768       /* data area is just beyond the jpeg_marker_struct */
00769       data = cur_marker->data = (JOCTET FAR *) (cur_marker + 1);
00770       marker->cur_marker = cur_marker;
00771       marker->bytes_read = 0;
00772       bytes_read = 0;
00773       data_length = limit;
00774     } else {
00775       /* deal with bogus length word */
00776       bytes_read = data_length = 0;
00777       data = NULL;
00778     }
00779   } else {
00780     /* resume reading a marker */
00781     bytes_read = marker->bytes_read;
00782     data_length = cur_marker->data_length;
00783     data = cur_marker->data + bytes_read;
00784   }
00785 
00786   while (bytes_read < data_length) {
00787     INPUT_SYNC(cinfo);             /* move the restart point to here */
00788     marker->bytes_read = bytes_read;
00789     /* If there's not at least one byte in buffer, suspend */
00790     MAKE_BYTE_AVAIL(cinfo, return FALSE);
00791     /* Copy bytes with reasonable rapidity */
00792     while (bytes_read < data_length && bytes_in_buffer > 0) {
00793       *data++ = *next_input_byte++;
00794       bytes_in_buffer--;
00795       bytes_read++;
00796     }
00797   }
00798 
00799   /* Done reading what we want to read */
00800   if (cur_marker != NULL) { /* will be NULL if bogus length word */
00801     /* Add new marker to end of list */
00802     if (cinfo->marker_list == NULL) {
00803       cinfo->marker_list = cur_marker;
00804     } else {
00805       jpeg_saved_marker_ptr prev = cinfo->marker_list;
00806       while (prev->next != NULL)
00807        prev = prev->next;
00808       prev->next = cur_marker;
00809     }
00810     /* Reset pointer & calc remaining data length */
00811     data = cur_marker->data;
00812     length = cur_marker->original_length - data_length;
00813   }
00814   /* Reset to initial state for next marker */
00815   marker->cur_marker = NULL;
00816 
00817   /* Process the marker if interesting; else just make a generic trace msg */
00818   switch (cinfo->unread_marker) {
00819   case M_APP0:
00820     examine_app0(cinfo, data, data_length, length);
00821     break;
00822   case M_APP14:
00823     examine_app14(cinfo, data, data_length, length);
00824     break;
00825   default:
00826     TRACEMS2(cinfo, 1, JTRC_MISC_MARKER, cinfo->unread_marker,
00827             (int) (data_length + length));
00828     break;
00829   }
00830 
00831   /* skip any remaining data -- could be lots */
00832   INPUT_SYNC(cinfo);        /* do before skip_input_data */
00833   if (length > 0)
00834     (*cinfo->src->skip_input_data) (cinfo, (long) length);
00835 
00836   return TRUE;
00837 }
00838 
00839 #endif /* SAVE_MARKERS_SUPPORTED */
00840 
00841 
00842 METHODDEF(boolean)
00843 skip_variable (j_decompress_ptr cinfo)
00844 /* Skip over an unknown or uninteresting variable-length marker */
00845 {
00846   INT32 length;
00847   INPUT_VARS(cinfo);
00848 
00849   INPUT_2BYTES(cinfo, length, return FALSE);
00850   length -= 2;
00851   
00852   TRACEMS2(cinfo, 1, JTRC_MISC_MARKER, cinfo->unread_marker, (int) length);
00853 
00854   INPUT_SYNC(cinfo);        /* do before skip_input_data */
00855   if (length > 0)
00856     (*cinfo->src->skip_input_data) (cinfo, (long) length);
00857 
00858   return TRUE;
00859 }
00860 
00861 
00862 /*
00863  * Find the next JPEG marker, save it in cinfo->unread_marker.
00864  * Returns FALSE if had to suspend before reaching a marker;
00865  * in that case cinfo->unread_marker is unchanged.
00866  *
00867  * Note that the result might not be a valid marker code,
00868  * but it will never be 0 or FF.
00869  */
00870 
00871 LOCAL(boolean)
00872 next_marker (j_decompress_ptr cinfo)
00873 {
00874   int c;
00875   INPUT_VARS(cinfo);
00876 
00877   for (;;) {
00878     INPUT_BYTE(cinfo, c, return FALSE);
00879     /* Skip any non-FF bytes.
00880      * This may look a bit inefficient, but it will not occur in a valid file.
00881      * We sync after each discarded byte so that a suspending data source
00882      * can discard the byte from its buffer.
00883      */
00884     while (c != 0xFF) {
00885       cinfo->marker->discarded_bytes++;
00886       INPUT_SYNC(cinfo);
00887       INPUT_BYTE(cinfo, c, return FALSE);
00888     }
00889     /* This loop swallows any duplicate FF bytes.  Extra FFs are legal as
00890      * pad bytes, so don't count them in discarded_bytes.  We assume there
00891      * will not be so many consecutive FF bytes as to overflow a suspending
00892      * data source's input buffer.
00893      */
00894     do {
00895       INPUT_BYTE(cinfo, c, return FALSE);
00896     } while (c == 0xFF);
00897     if (c != 0)
00898       break;                /* found a valid marker, exit loop */
00899     /* Reach here if we found a stuffed-zero data sequence (FF/00).
00900      * Discard it and loop back to try again.
00901      */
00902     cinfo->marker->discarded_bytes += 2;
00903     INPUT_SYNC(cinfo);
00904   }
00905 
00906   if (cinfo->marker->discarded_bytes != 0) {
00907     WARNMS2(cinfo, JWRN_EXTRANEOUS_DATA, cinfo->marker->discarded_bytes, c);
00908     cinfo->marker->discarded_bytes = 0;
00909   }
00910 
00911   cinfo->unread_marker = c;
00912 
00913   INPUT_SYNC(cinfo);
00914   return TRUE;
00915 }
00916 
00917 
00918 LOCAL(boolean)
00919 first_marker (j_decompress_ptr cinfo)
00920 /* Like next_marker, but used to obtain the initial SOI marker. */
00921 /* For this marker, we do not allow preceding garbage or fill; otherwise,
00922  * we might well scan an entire input file before realizing it ain't JPEG.
00923  * If an application wants to process non-JFIF files, it must seek to the
00924  * SOI before calling the JPEG library.
00925  */
00926 {
00927   int c, c2;
00928   INPUT_VARS(cinfo);
00929 
00930   INPUT_BYTE(cinfo, c, return FALSE);
00931   INPUT_BYTE(cinfo, c2, return FALSE);
00932   if (c != 0xFF || c2 != (int) M_SOI)
00933     ERREXIT2(cinfo, JERR_NO_SOI, c, c2);
00934 
00935   cinfo->unread_marker = c2;
00936 
00937   INPUT_SYNC(cinfo);
00938   return TRUE;
00939 }
00940 
00941 
00942 /*
00943  * Read markers until SOS or EOI.
00944  *
00945  * Returns same codes as are defined for jpeg_consume_input:
00946  * JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.
00947  */
00948 
00949 METHODDEF(int)
00950 read_markers (j_decompress_ptr cinfo)
00951 {
00952   /* Outer loop repeats once for each marker. */
00953   for (;;) {
00954     /* Collect the marker proper, unless we already did. */
00955     /* NB: first_marker() enforces the requirement that SOI appear first. */
00956     if (cinfo->unread_marker == 0) {
00957       if (! cinfo->marker->saw_SOI) {
00958        if (! first_marker(cinfo))
00959          return JPEG_SUSPENDED;
00960       } else {
00961        if (! next_marker(cinfo))
00962          return JPEG_SUSPENDED;
00963       }
00964     }
00965     /* At this point cinfo->unread_marker contains the marker code and the
00966      * input point is just past the marker proper, but before any parameters.
00967      * A suspension will cause us to return with this state still true.
00968      */
00969     switch (cinfo->unread_marker) {
00970     case M_SOI:
00971       if (! get_soi(cinfo))
00972        return JPEG_SUSPENDED;
00973       break;
00974 
00975     case M_SOF0:            /* Baseline */
00976     case M_SOF1:            /* Extended sequential, Huffman */
00977       if (! get_sof(cinfo, FALSE, FALSE))
00978        return JPEG_SUSPENDED;
00979       break;
00980 
00981     case M_SOF2:            /* Progressive, Huffman */
00982       if (! get_sof(cinfo, TRUE, FALSE))
00983        return JPEG_SUSPENDED;
00984       break;
00985 
00986     case M_SOF9:            /* Extended sequential, arithmetic */
00987       if (! get_sof(cinfo, FALSE, TRUE))
00988        return JPEG_SUSPENDED;
00989       break;
00990 
00991     case M_SOF10:           /* Progressive, arithmetic */
00992       if (! get_sof(cinfo, TRUE, TRUE))
00993        return JPEG_SUSPENDED;
00994       break;
00995 
00996     /* Currently unsupported SOFn types */
00997     case M_SOF3:            /* Lossless, Huffman */
00998     case M_SOF5:            /* Differential sequential, Huffman */
00999     case M_SOF6:            /* Differential progressive, Huffman */
01000     case M_SOF7:            /* Differential lossless, Huffman */
01001     case M_JPG:                    /* Reserved for JPEG extensions */
01002     case M_SOF11:           /* Lossless, arithmetic */
01003     case M_SOF13:           /* Differential sequential, arithmetic */
01004     case M_SOF14:           /* Differential progressive, arithmetic */
01005     case M_SOF15:           /* Differential lossless, arithmetic */
01006       ERREXIT1(cinfo, JERR_SOF_UNSUPPORTED, cinfo->unread_marker);
01007       break;
01008 
01009     case M_SOS:
01010       if (! get_sos(cinfo))
01011        return JPEG_SUSPENDED;
01012       cinfo->unread_marker = 0;    /* processed the marker */
01013       return JPEG_REACHED_SOS;
01014     
01015     case M_EOI:
01016       TRACEMS(cinfo, 1, JTRC_EOI);
01017       cinfo->unread_marker = 0;    /* processed the marker */
01018       return JPEG_REACHED_EOI;
01019       
01020     case M_DAC:
01021       if (! get_dac(cinfo))
01022        return JPEG_SUSPENDED;
01023       break;
01024       
01025     case M_DHT:
01026       if (! get_dht(cinfo))
01027        return JPEG_SUSPENDED;
01028       break;
01029       
01030     case M_DQT:
01031       if (! get_dqt(cinfo))
01032        return JPEG_SUSPENDED;
01033       break;
01034       
01035     case M_DRI:
01036       if (! get_dri(cinfo))
01037        return JPEG_SUSPENDED;
01038       break;
01039       
01040     case M_APP0:
01041     case M_APP1:
01042     case M_APP2:
01043     case M_APP3:
01044     case M_APP4:
01045     case M_APP5:
01046     case M_APP6:
01047     case M_APP7:
01048     case M_APP8:
01049     case M_APP9:
01050     case M_APP10:
01051     case M_APP11:
01052     case M_APP12:
01053     case M_APP13:
01054     case M_APP14:
01055     case M_APP15:
01056       if (! (*((my_marker_ptr) cinfo->marker)->process_APPn[
01057               cinfo->unread_marker - (int) M_APP0]) (cinfo))
01058        return JPEG_SUSPENDED;
01059       break;
01060       
01061     case M_COM:
01062       if (! (*((my_marker_ptr) cinfo->marker)->process_COM) (cinfo))
01063        return JPEG_SUSPENDED;
01064       break;
01065 
01066     case M_RST0:            /* these are all parameterless */
01067     case M_RST1:
01068     case M_RST2:
01069     case M_RST3:
01070     case M_RST4:
01071     case M_RST5:
01072     case M_RST6:
01073     case M_RST7:
01074     case M_TEM:
01075       TRACEMS1(cinfo, 1, JTRC_PARMLESS_MARKER, cinfo->unread_marker);
01076       break;
01077 
01078     case M_DNL:                    /* Ignore DNL ... perhaps the wrong thing */
01079       if (! skip_variable(cinfo))
01080        return JPEG_SUSPENDED;
01081       break;
01082 
01083     default:                /* must be DHP, EXP, JPGn, or RESn */
01084       /* For now, we treat the reserved markers as fatal errors since they are
01085        * likely to be used to signal incompatible JPEG Part 3 extensions.
01086        * Once the JPEG 3 version-number marker is well defined, this code
01087        * ought to change!
01088        */
01089       ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, cinfo->unread_marker);
01090       break;
01091     }
01092     /* Successfully processed marker, so reset state variable */
01093     cinfo->unread_marker = 0;
01094   } /* end loop */
01095 }
01096 
01097 
01098 /*
01099  * Read a restart marker, which is expected to appear next in the datastream;
01100  * if the marker is not there, take appropriate recovery action.
01101  * Returns FALSE if suspension is required.
01102  *
01103  * This is called by the entropy decoder after it has read an appropriate
01104  * number of MCUs.  cinfo->unread_marker may be nonzero if the entropy decoder
01105  * has already read a marker from the data source.  Under normal conditions
01106  * cinfo->unread_marker will be reset to 0 before returning; if not reset,
01107  * it holds a marker which the decoder will be unable to read past.
01108  */
01109 
01110 METHODDEF(boolean)
01111 read_restart_marker (j_decompress_ptr cinfo)
01112 {
01113   /* Obtain a marker unless we already did. */
01114   /* Note that next_marker will complain if it skips any data. */
01115   if (cinfo->unread_marker == 0) {
01116     if (! next_marker(cinfo))
01117       return FALSE;
01118   }
01119 
01120   if (cinfo->unread_marker ==
01121       ((int) M_RST0 + cinfo->marker->next_restart_num)) {
01122     /* Normal case --- swallow the marker and let entropy decoder continue */
01123     TRACEMS1(cinfo, 3, JTRC_RST, cinfo->marker->next_restart_num);
01124     cinfo->unread_marker = 0;
01125   } else {
01126     /* Uh-oh, the restart markers have been messed up. */
01127     /* Let the data source manager determine how to resync. */
01128     if (! (*cinfo->src->resync_to_restart) (cinfo,
01129                                        cinfo->marker->next_restart_num))
01130       return FALSE;
01131   }
01132 
01133   /* Update next-restart state */
01134   cinfo->marker->next_restart_num = (cinfo->marker->next_restart_num + 1) & 7;
01135 
01136   return TRUE;
01137 }
01138 
01139 
01140 /*
01141  * This is the default resync_to_restart method for data source managers
01142  * to use if they don't have any better approach.  Some data source managers
01143  * may be able to back up, or may have additional knowledge about the data
01144  * which permits a more intelligent recovery strategy; such managers would
01145  * presumably supply their own resync method.
01146  *
01147  * read_restart_marker calls resync_to_restart if it finds a marker other than
01148  * the restart marker it was expecting.  (This code is *not* used unless
01149  * a nonzero restart interval has been declared.)  cinfo->unread_marker is
01150  * the marker code actually found (might be anything, except 0 or FF).
01151  * The desired restart marker number (0..7) is passed as a parameter.
01152  * This routine is supposed to apply whatever error recovery strategy seems
01153  * appropriate in order to position the input stream to the next data segment.
01154  * Note that cinfo->unread_marker is treated as a marker appearing before
01155  * the current data-source input point; usually it should be reset to zero
01156  * before returning.
01157  * Returns FALSE if suspension is required.
01158  *
01159  * This implementation is substantially constrained by wanting to treat the
01160  * input as a data stream; this means we can't back up.  Therefore, we have
01161  * only the following actions to work with:
01162  *   1. Simply discard the marker and let the entropy decoder resume at next
01163  *      byte of file.
01164  *   2. Read forward until we find another marker, discarding intervening
01165  *      data.  (In theory we could look ahead within the current bufferload,
01166  *      without having to discard data if we don't find the desired marker.
01167  *      This idea is not implemented here, in part because it makes behavior
01168  *      dependent on buffer size and chance buffer-boundary positions.)
01169  *   3. Leave the marker unread (by failing to zero cinfo->unread_marker).
01170  *      This will cause the entropy decoder to process an empty data segment,
01171  *      inserting dummy zeroes, and then we will reprocess the marker.
01172  *
01173  * #2 is appropriate if we think the desired marker lies ahead, while #3 is
01174  * appropriate if the found marker is a future restart marker (indicating
01175  * that we have missed the desired restart marker, probably because it got
01176  * corrupted).
01177  * We apply #2 or #3 if the found marker is a restart marker no more than
01178  * two counts behind or ahead of the expected one.  We also apply #2 if the
01179  * found marker is not a legal JPEG marker code (it's certainly bogus data).
01180  * If the found marker is a restart marker more than 2 counts away, we do #1
01181  * (too much risk that the marker is erroneous; with luck we will be able to
01182  * resync at some future point).
01183  * For any valid non-restart JPEG marker, we apply #3.  This keeps us from
01184  * overrunning the end of a scan.  An implementation limited to single-scan
01185  * files might find it better to apply #2 for markers other than EOI, since
01186  * any other marker would have to be bogus data in that case.
01187  */
01188 
01189 GLOBAL(boolean)
01190 jpeg_resync_to_restart (j_decompress_ptr cinfo, int desired)
01191 {
01192   int marker = cinfo->unread_marker;
01193   int action = 1;
01194   
01195   /* Always put up a warning. */
01196   WARNMS2(cinfo, JWRN_MUST_RESYNC, marker, desired);
01197   
01198   /* Outer loop handles repeated decision after scanning forward. */
01199   for (;;) {
01200     if (marker < (int) M_SOF0)
01201       action = 2;           /* invalid marker */
01202     else if (marker < (int) M_RST0 || marker > (int) M_RST7)
01203       action = 3;           /* valid non-restart marker */
01204     else {
01205       if (marker == ((int) M_RST0 + ((desired+1) & 7)) ||
01206          marker == ((int) M_RST0 + ((desired+2) & 7)))
01207        action = 3;          /* one of the next two expected restarts */
01208       else if (marker == ((int) M_RST0 + ((desired-1) & 7)) ||
01209               marker == ((int) M_RST0 + ((desired-2) & 7)))
01210        action = 2;          /* a prior restart, so advance */
01211       else
01212        action = 1;          /* desired restart or too far away */
01213     }
01214     TRACEMS2(cinfo, 4, JTRC_RECOVERY_ACTION, marker, action);
01215     switch (action) {
01216     case 1:
01217       /* Discard marker and let entropy decoder resume processing. */
01218       cinfo->unread_marker = 0;
01219       return TRUE;
01220     case 2:
01221       /* Scan to the next marker, and repeat the decision loop. */
01222       if (! next_marker(cinfo))
01223        return FALSE;
01224       marker = cinfo->unread_marker;
01225       break;
01226     case 3:
01227       /* Return without advancing past this marker. */
01228       /* Entropy decoder will be forced to process an empty segment. */
01229       return TRUE;
01230     }
01231   } /* end loop */
01232 }
01233 
01234 
01235 /*
01236  * Reset marker processing state to begin a fresh datastream.
01237  */
01238 
01239 METHODDEF(void)
01240 reset_marker_reader (j_decompress_ptr cinfo)
01241 {
01242   my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
01243 
01244   cinfo->comp_info = NULL;         /* until allocated by get_sof */
01245   cinfo->input_scan_number = 0;           /* no SOS seen yet */
01246   cinfo->unread_marker = 0;        /* no pending marker */
01247   marker->pub.saw_SOI = FALSE;            /* set internal state too */
01248   marker->pub.saw_SOF = FALSE;
01249   marker->pub.discarded_bytes = 0;
01250   marker->cur_marker = NULL;
01251 }
01252 
01253 
01254 /*
01255  * Initialize the marker reader module.
01256  * This is called only once, when the decompression object is created.
01257  */
01258 
01259 GLOBAL(void)
01260 jinit_marker_reader (j_decompress_ptr cinfo)
01261 {
01262   my_marker_ptr marker;
01263   int i;
01264 
01265   /* Create subobject in permanent pool */
01266   marker = (my_marker_ptr)
01267     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
01268                             SIZEOF(my_marker_reader));
01269   cinfo->marker = (struct jpeg_marker_reader *) marker;
01270   /* Initialize public method pointers */
01271   marker->pub.reset_marker_reader = reset_marker_reader;
01272   marker->pub.read_markers = read_markers;
01273   marker->pub.read_restart_marker = read_restart_marker;
01274   /* Initialize COM/APPn processing.
01275    * By default, we examine and then discard APP0 and APP14,
01276    * but simply discard COM and all other APPn.
01277    */
01278   marker->process_COM = skip_variable;
01279   marker->length_limit_COM = 0;
01280   for (i = 0; i < 16; i++) {
01281     marker->process_APPn[i] = skip_variable;
01282     marker->length_limit_APPn[i] = 0;
01283   }
01284   marker->process_APPn[0] = get_interesting_appn;
01285   marker->process_APPn[14] = get_interesting_appn;
01286   /* Reset marker processing state */
01287   reset_marker_reader(cinfo);
01288 }
01289 
01290 
01291 /*
01292  * Control saving of COM and APPn markers into marker_list.
01293  */
01294 
01295 #ifdef SAVE_MARKERS_SUPPORTED
01296 
01297 GLOBAL(void)
01298 jpeg_save_markers (j_decompress_ptr cinfo, int marker_code,
01299                  unsigned int length_limit)
01300 {
01301   my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
01302   long maxlength;
01303   jpeg_marker_parser_method processor;
01304 
01305   /* Length limit mustn't be larger than what we can allocate
01306    * (should only be a concern in a 16-bit environment).
01307    */
01308   maxlength = cinfo->mem->max_alloc_chunk - SIZEOF(struct jpeg_marker_struct);
01309   if (((long) length_limit) > maxlength)
01310     length_limit = (unsigned int) maxlength;
01311 
01312   /* Choose processor routine to use.
01313    * APP0/APP14 have special requirements.
01314    */
01315   if (length_limit) {
01316     processor = save_marker;
01317     /* If saving APP0/APP14, save at least enough for our internal use. */
01318     if (marker_code == (int) M_APP0 && length_limit < APP0_DATA_LEN)
01319       length_limit = APP0_DATA_LEN;
01320     else if (marker_code == (int) M_APP14 && length_limit < APP14_DATA_LEN)
01321       length_limit = APP14_DATA_LEN;
01322   } else {
01323     processor = skip_variable;
01324     /* If discarding APP0/APP14, use our regular on-the-fly processor. */
01325     if (marker_code == (int) M_APP0 || marker_code == (int) M_APP14)
01326       processor = get_interesting_appn;
01327   }
01328 
01329   if (marker_code == (int) M_COM) {
01330     marker->process_COM = processor;
01331     marker->length_limit_COM = length_limit;
01332   } else if (marker_code >= (int) M_APP0 && marker_code <= (int) M_APP15) {
01333     marker->process_APPn[marker_code - (int) M_APP0] = processor;
01334     marker->length_limit_APPn[marker_code - (int) M_APP0] = length_limit;
01335   } else
01336     ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, marker_code);
01337 }
01338 
01339 #endif /* SAVE_MARKERS_SUPPORTED */
01340 
01341 
01342 /*
01343  * Install a special processing method for COM or APPn markers.
01344  */
01345 
01346 GLOBAL(void)
01347 jpeg_set_marker_processor (j_decompress_ptr cinfo, int marker_code,
01348                         jpeg_marker_parser_method routine)
01349 {
01350   my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
01351 
01352   if (marker_code == (int) M_COM)
01353     marker->process_COM = routine;
01354   else if (marker_code >= (int) M_APP0 && marker_code <= (int) M_APP15)
01355     marker->process_APPn[marker_code - (int) M_APP0] = routine;
01356   else
01357     ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, marker_code);
01358 }