3dphotoshoot

diff libs/libjpeg/jdmarker.c @ 14:06dc8b9b4f89

added libimago, libjpeg and libpng
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 07 Jun 2015 17:25:49 +0300
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libs/libjpeg/jdmarker.c	Sun Jun 07 17:25:49 2015 +0300
     1.3 @@ -0,0 +1,1360 @@
     1.4 +/*
     1.5 + * jdmarker.c
     1.6 + *
     1.7 + * Copyright (C) 1991-1998, Thomas G. Lane.
     1.8 + * This file is part of the Independent JPEG Group's software.
     1.9 + * For conditions of distribution and use, see the accompanying README file.
    1.10 + *
    1.11 + * This file contains routines to decode JPEG datastream markers.
    1.12 + * Most of the complexity arises from our desire to support input
    1.13 + * suspension: if not all of the data for a marker is available,
    1.14 + * we must exit back to the application.  On resumption, we reprocess
    1.15 + * the marker.
    1.16 + */
    1.17 +
    1.18 +#define JPEG_INTERNALS
    1.19 +#include "jinclude.h"
    1.20 +#include "jpeglib.h"
    1.21 +
    1.22 +
    1.23 +typedef enum {			/* JPEG marker codes */
    1.24 +  M_SOF0  = 0xc0,
    1.25 +  M_SOF1  = 0xc1,
    1.26 +  M_SOF2  = 0xc2,
    1.27 +  M_SOF3  = 0xc3,
    1.28 +  
    1.29 +  M_SOF5  = 0xc5,
    1.30 +  M_SOF6  = 0xc6,
    1.31 +  M_SOF7  = 0xc7,
    1.32 +  
    1.33 +  M_JPG   = 0xc8,
    1.34 +  M_SOF9  = 0xc9,
    1.35 +  M_SOF10 = 0xca,
    1.36 +  M_SOF11 = 0xcb,
    1.37 +  
    1.38 +  M_SOF13 = 0xcd,
    1.39 +  M_SOF14 = 0xce,
    1.40 +  M_SOF15 = 0xcf,
    1.41 +  
    1.42 +  M_DHT   = 0xc4,
    1.43 +  
    1.44 +  M_DAC   = 0xcc,
    1.45 +  
    1.46 +  M_RST0  = 0xd0,
    1.47 +  M_RST1  = 0xd1,
    1.48 +  M_RST2  = 0xd2,
    1.49 +  M_RST3  = 0xd3,
    1.50 +  M_RST4  = 0xd4,
    1.51 +  M_RST5  = 0xd5,
    1.52 +  M_RST6  = 0xd6,
    1.53 +  M_RST7  = 0xd7,
    1.54 +  
    1.55 +  M_SOI   = 0xd8,
    1.56 +  M_EOI   = 0xd9,
    1.57 +  M_SOS   = 0xda,
    1.58 +  M_DQT   = 0xdb,
    1.59 +  M_DNL   = 0xdc,
    1.60 +  M_DRI   = 0xdd,
    1.61 +  M_DHP   = 0xde,
    1.62 +  M_EXP   = 0xdf,
    1.63 +  
    1.64 +  M_APP0  = 0xe0,
    1.65 +  M_APP1  = 0xe1,
    1.66 +  M_APP2  = 0xe2,
    1.67 +  M_APP3  = 0xe3,
    1.68 +  M_APP4  = 0xe4,
    1.69 +  M_APP5  = 0xe5,
    1.70 +  M_APP6  = 0xe6,
    1.71 +  M_APP7  = 0xe7,
    1.72 +  M_APP8  = 0xe8,
    1.73 +  M_APP9  = 0xe9,
    1.74 +  M_APP10 = 0xea,
    1.75 +  M_APP11 = 0xeb,
    1.76 +  M_APP12 = 0xec,
    1.77 +  M_APP13 = 0xed,
    1.78 +  M_APP14 = 0xee,
    1.79 +  M_APP15 = 0xef,
    1.80 +  
    1.81 +  M_JPG0  = 0xf0,
    1.82 +  M_JPG13 = 0xfd,
    1.83 +  M_COM   = 0xfe,
    1.84 +  
    1.85 +  M_TEM   = 0x01,
    1.86 +  
    1.87 +  M_ERROR = 0x100
    1.88 +} JPEG_MARKER;
    1.89 +
    1.90 +
    1.91 +/* Private state */
    1.92 +
    1.93 +typedef struct {
    1.94 +  struct jpeg_marker_reader pub; /* public fields */
    1.95 +
    1.96 +  /* Application-overridable marker processing methods */
    1.97 +  jpeg_marker_parser_method process_COM;
    1.98 +  jpeg_marker_parser_method process_APPn[16];
    1.99 +
   1.100 +  /* Limit on marker data length to save for each marker type */
   1.101 +  unsigned int length_limit_COM;
   1.102 +  unsigned int length_limit_APPn[16];
   1.103 +
   1.104 +  /* Status of COM/APPn marker saving */
   1.105 +  jpeg_saved_marker_ptr cur_marker;	/* NULL if not processing a marker */
   1.106 +  unsigned int bytes_read;		/* data bytes read so far in marker */
   1.107 +  /* Note: cur_marker is not linked into marker_list until it's all read. */
   1.108 +} my_marker_reader;
   1.109 +
   1.110 +typedef my_marker_reader * my_marker_ptr;
   1.111 +
   1.112 +
   1.113 +/*
   1.114 + * Macros for fetching data from the data source module.
   1.115 + *
   1.116 + * At all times, cinfo->src->next_input_byte and ->bytes_in_buffer reflect
   1.117 + * the current restart point; we update them only when we have reached a
   1.118 + * suitable place to restart if a suspension occurs.
   1.119 + */
   1.120 +
   1.121 +/* Declare and initialize local copies of input pointer/count */
   1.122 +#define INPUT_VARS(cinfo)  \
   1.123 +	struct jpeg_source_mgr * datasrc = (cinfo)->src;  \
   1.124 +	const JOCTET * next_input_byte = datasrc->next_input_byte;  \
   1.125 +	size_t bytes_in_buffer = datasrc->bytes_in_buffer
   1.126 +
   1.127 +/* Unload the local copies --- do this only at a restart boundary */
   1.128 +#define INPUT_SYNC(cinfo)  \
   1.129 +	( datasrc->next_input_byte = next_input_byte,  \
   1.130 +	  datasrc->bytes_in_buffer = bytes_in_buffer )
   1.131 +
   1.132 +/* Reload the local copies --- used only in MAKE_BYTE_AVAIL */
   1.133 +#define INPUT_RELOAD(cinfo)  \
   1.134 +	( next_input_byte = datasrc->next_input_byte,  \
   1.135 +	  bytes_in_buffer = datasrc->bytes_in_buffer )
   1.136 +
   1.137 +/* Internal macro for INPUT_BYTE and INPUT_2BYTES: make a byte available.
   1.138 + * Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
   1.139 + * but we must reload the local copies after a successful fill.
   1.140 + */
   1.141 +#define MAKE_BYTE_AVAIL(cinfo,action)  \
   1.142 +	if (bytes_in_buffer == 0) {  \
   1.143 +	  if (! (*datasrc->fill_input_buffer) (cinfo))  \
   1.144 +	    { action; }  \
   1.145 +	  INPUT_RELOAD(cinfo);  \
   1.146 +	}
   1.147 +
   1.148 +/* Read a byte into variable V.
   1.149 + * If must suspend, take the specified action (typically "return FALSE").
   1.150 + */
   1.151 +#define INPUT_BYTE(cinfo,V,action)  \
   1.152 +	MAKESTMT( MAKE_BYTE_AVAIL(cinfo,action); \
   1.153 +		  bytes_in_buffer--; \
   1.154 +		  V = GETJOCTET(*next_input_byte++); )
   1.155 +
   1.156 +/* As above, but read two bytes interpreted as an unsigned 16-bit integer.
   1.157 + * V should be declared unsigned int or perhaps INT32.
   1.158 + */
   1.159 +#define INPUT_2BYTES(cinfo,V,action)  \
   1.160 +	MAKESTMT( MAKE_BYTE_AVAIL(cinfo,action); \
   1.161 +		  bytes_in_buffer--; \
   1.162 +		  V = ((unsigned int) GETJOCTET(*next_input_byte++)) << 8; \
   1.163 +		  MAKE_BYTE_AVAIL(cinfo,action); \
   1.164 +		  bytes_in_buffer--; \
   1.165 +		  V += GETJOCTET(*next_input_byte++); )
   1.166 +
   1.167 +
   1.168 +/*
   1.169 + * Routines to process JPEG markers.
   1.170 + *
   1.171 + * Entry condition: JPEG marker itself has been read and its code saved
   1.172 + *   in cinfo->unread_marker; input restart point is just after the marker.
   1.173 + *
   1.174 + * Exit: if return TRUE, have read and processed any parameters, and have
   1.175 + *   updated the restart point to point after the parameters.
   1.176 + *   If return FALSE, was forced to suspend before reaching end of
   1.177 + *   marker parameters; restart point has not been moved.  Same routine
   1.178 + *   will be called again after application supplies more input data.
   1.179 + *
   1.180 + * This approach to suspension assumes that all of a marker's parameters
   1.181 + * can fit into a single input bufferload.  This should hold for "normal"
   1.182 + * markers.  Some COM/APPn markers might have large parameter segments
   1.183 + * that might not fit.  If we are simply dropping such a marker, we use
   1.184 + * skip_input_data to get past it, and thereby put the problem on the
   1.185 + * source manager's shoulders.  If we are saving the marker's contents
   1.186 + * into memory, we use a slightly different convention: when forced to
   1.187 + * suspend, the marker processor updates the restart point to the end of
   1.188 + * what it's consumed (ie, the end of the buffer) before returning FALSE.
   1.189 + * On resumption, cinfo->unread_marker still contains the marker code,
   1.190 + * but the data source will point to the next chunk of marker data.
   1.191 + * The marker processor must retain internal state to deal with this.
   1.192 + *
   1.193 + * Note that we don't bother to avoid duplicate trace messages if a
   1.194 + * suspension occurs within marker parameters.  Other side effects
   1.195 + * require more care.
   1.196 + */
   1.197 +
   1.198 +
   1.199 +LOCAL(boolean)
   1.200 +get_soi (j_decompress_ptr cinfo)
   1.201 +/* Process an SOI marker */
   1.202 +{
   1.203 +  int i;
   1.204 +  
   1.205 +  TRACEMS(cinfo, 1, JTRC_SOI);
   1.206 +
   1.207 +  if (cinfo->marker->saw_SOI)
   1.208 +    ERREXIT(cinfo, JERR_SOI_DUPLICATE);
   1.209 +
   1.210 +  /* Reset all parameters that are defined to be reset by SOI */
   1.211 +
   1.212 +  for (i = 0; i < NUM_ARITH_TBLS; i++) {
   1.213 +    cinfo->arith_dc_L[i] = 0;
   1.214 +    cinfo->arith_dc_U[i] = 1;
   1.215 +    cinfo->arith_ac_K[i] = 5;
   1.216 +  }
   1.217 +  cinfo->restart_interval = 0;
   1.218 +
   1.219 +  /* Set initial assumptions for colorspace etc */
   1.220 +
   1.221 +  cinfo->jpeg_color_space = JCS_UNKNOWN;
   1.222 +  cinfo->CCIR601_sampling = FALSE; /* Assume non-CCIR sampling??? */
   1.223 +
   1.224 +  cinfo->saw_JFIF_marker = FALSE;
   1.225 +  cinfo->JFIF_major_version = 1; /* set default JFIF APP0 values */
   1.226 +  cinfo->JFIF_minor_version = 1;
   1.227 +  cinfo->density_unit = 0;
   1.228 +  cinfo->X_density = 1;
   1.229 +  cinfo->Y_density = 1;
   1.230 +  cinfo->saw_Adobe_marker = FALSE;
   1.231 +  cinfo->Adobe_transform = 0;
   1.232 +
   1.233 +  cinfo->marker->saw_SOI = TRUE;
   1.234 +
   1.235 +  return TRUE;
   1.236 +}
   1.237 +
   1.238 +
   1.239 +LOCAL(boolean)
   1.240 +get_sof (j_decompress_ptr cinfo, boolean is_prog, boolean is_arith)
   1.241 +/* Process a SOFn marker */
   1.242 +{
   1.243 +  INT32 length;
   1.244 +  int c, ci;
   1.245 +  jpeg_component_info * compptr;
   1.246 +  INPUT_VARS(cinfo);
   1.247 +
   1.248 +  cinfo->progressive_mode = is_prog;
   1.249 +  cinfo->arith_code = is_arith;
   1.250 +
   1.251 +  INPUT_2BYTES(cinfo, length, return FALSE);
   1.252 +
   1.253 +  INPUT_BYTE(cinfo, cinfo->data_precision, return FALSE);
   1.254 +  INPUT_2BYTES(cinfo, cinfo->image_height, return FALSE);
   1.255 +  INPUT_2BYTES(cinfo, cinfo->image_width, return FALSE);
   1.256 +  INPUT_BYTE(cinfo, cinfo->num_components, return FALSE);
   1.257 +
   1.258 +  length -= 8;
   1.259 +
   1.260 +  TRACEMS4(cinfo, 1, JTRC_SOF, cinfo->unread_marker,
   1.261 +	   (int) cinfo->image_width, (int) cinfo->image_height,
   1.262 +	   cinfo->num_components);
   1.263 +
   1.264 +  if (cinfo->marker->saw_SOF)
   1.265 +    ERREXIT(cinfo, JERR_SOF_DUPLICATE);
   1.266 +
   1.267 +  /* We don't support files in which the image height is initially specified */
   1.268 +  /* as 0 and is later redefined by DNL.  As long as we have to check that,  */
   1.269 +  /* might as well have a general sanity check. */
   1.270 +  if (cinfo->image_height <= 0 || cinfo->image_width <= 0
   1.271 +      || cinfo->num_components <= 0)
   1.272 +    ERREXIT(cinfo, JERR_EMPTY_IMAGE);
   1.273 +
   1.274 +  if (length != (cinfo->num_components * 3))
   1.275 +    ERREXIT(cinfo, JERR_BAD_LENGTH);
   1.276 +
   1.277 +  if (cinfo->comp_info == NULL)	/* do only once, even if suspend */
   1.278 +    cinfo->comp_info = (jpeg_component_info *) (*cinfo->mem->alloc_small)
   1.279 +			((j_common_ptr) cinfo, JPOOL_IMAGE,
   1.280 +			 cinfo->num_components * SIZEOF(jpeg_component_info));
   1.281 +  
   1.282 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
   1.283 +       ci++, compptr++) {
   1.284 +    compptr->component_index = ci;
   1.285 +    INPUT_BYTE(cinfo, compptr->component_id, return FALSE);
   1.286 +    INPUT_BYTE(cinfo, c, return FALSE);
   1.287 +    compptr->h_samp_factor = (c >> 4) & 15;
   1.288 +    compptr->v_samp_factor = (c     ) & 15;
   1.289 +    INPUT_BYTE(cinfo, compptr->quant_tbl_no, return FALSE);
   1.290 +
   1.291 +    TRACEMS4(cinfo, 1, JTRC_SOF_COMPONENT,
   1.292 +	     compptr->component_id, compptr->h_samp_factor,
   1.293 +	     compptr->v_samp_factor, compptr->quant_tbl_no);
   1.294 +  }
   1.295 +
   1.296 +  cinfo->marker->saw_SOF = TRUE;
   1.297 +
   1.298 +  INPUT_SYNC(cinfo);
   1.299 +  return TRUE;
   1.300 +}
   1.301 +
   1.302 +
   1.303 +LOCAL(boolean)
   1.304 +get_sos (j_decompress_ptr cinfo)
   1.305 +/* Process a SOS marker */
   1.306 +{
   1.307 +  INT32 length;
   1.308 +  int i, ci, n, c, cc;
   1.309 +  jpeg_component_info * compptr;
   1.310 +  INPUT_VARS(cinfo);
   1.311 +
   1.312 +  if (! cinfo->marker->saw_SOF)
   1.313 +    ERREXIT(cinfo, JERR_SOS_NO_SOF);
   1.314 +
   1.315 +  INPUT_2BYTES(cinfo, length, return FALSE);
   1.316 +
   1.317 +  INPUT_BYTE(cinfo, n, return FALSE); /* Number of components */
   1.318 +
   1.319 +  TRACEMS1(cinfo, 1, JTRC_SOS, n);
   1.320 +
   1.321 +  if (length != (n * 2 + 6) || n < 1 || n > MAX_COMPS_IN_SCAN)
   1.322 +    ERREXIT(cinfo, JERR_BAD_LENGTH);
   1.323 +
   1.324 +  cinfo->comps_in_scan = n;
   1.325 +
   1.326 +  /* Collect the component-spec parameters */
   1.327 +
   1.328 +  for (i = 0; i < n; i++) {
   1.329 +    INPUT_BYTE(cinfo, cc, return FALSE);
   1.330 +    INPUT_BYTE(cinfo, c, return FALSE);
   1.331 +    
   1.332 +    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
   1.333 +	 ci++, compptr++) {
   1.334 +      if (cc == compptr->component_id)
   1.335 +	goto id_found;
   1.336 +    }
   1.337 +
   1.338 +    ERREXIT1(cinfo, JERR_BAD_COMPONENT_ID, cc);
   1.339 +
   1.340 +  id_found:
   1.341 +
   1.342 +    cinfo->cur_comp_info[i] = compptr;
   1.343 +    compptr->dc_tbl_no = (c >> 4) & 15;
   1.344 +    compptr->ac_tbl_no = (c     ) & 15;
   1.345 +    
   1.346 +    TRACEMS3(cinfo, 1, JTRC_SOS_COMPONENT, cc,
   1.347 +	     compptr->dc_tbl_no, compptr->ac_tbl_no);
   1.348 +  }
   1.349 +
   1.350 +  /* Collect the additional scan parameters Ss, Se, Ah/Al. */
   1.351 +  INPUT_BYTE(cinfo, c, return FALSE);
   1.352 +  cinfo->Ss = c;
   1.353 +  INPUT_BYTE(cinfo, c, return FALSE);
   1.354 +  cinfo->Se = c;
   1.355 +  INPUT_BYTE(cinfo, c, return FALSE);
   1.356 +  cinfo->Ah = (c >> 4) & 15;
   1.357 +  cinfo->Al = (c     ) & 15;
   1.358 +
   1.359 +  TRACEMS4(cinfo, 1, JTRC_SOS_PARAMS, cinfo->Ss, cinfo->Se,
   1.360 +	   cinfo->Ah, cinfo->Al);
   1.361 +
   1.362 +  /* Prepare to scan data & restart markers */
   1.363 +  cinfo->marker->next_restart_num = 0;
   1.364 +
   1.365 +  /* Count another SOS marker */
   1.366 +  cinfo->input_scan_number++;
   1.367 +
   1.368 +  INPUT_SYNC(cinfo);
   1.369 +  return TRUE;
   1.370 +}
   1.371 +
   1.372 +
   1.373 +#ifdef D_ARITH_CODING_SUPPORTED
   1.374 +
   1.375 +LOCAL(boolean)
   1.376 +get_dac (j_decompress_ptr cinfo)
   1.377 +/* Process a DAC marker */
   1.378 +{
   1.379 +  INT32 length;
   1.380 +  int index, val;
   1.381 +  INPUT_VARS(cinfo);
   1.382 +
   1.383 +  INPUT_2BYTES(cinfo, length, return FALSE);
   1.384 +  length -= 2;
   1.385 +  
   1.386 +  while (length > 0) {
   1.387 +    INPUT_BYTE(cinfo, index, return FALSE);
   1.388 +    INPUT_BYTE(cinfo, val, return FALSE);
   1.389 +
   1.390 +    length -= 2;
   1.391 +
   1.392 +    TRACEMS2(cinfo, 1, JTRC_DAC, index, val);
   1.393 +
   1.394 +    if (index < 0 || index >= (2*NUM_ARITH_TBLS))
   1.395 +      ERREXIT1(cinfo, JERR_DAC_INDEX, index);
   1.396 +
   1.397 +    if (index >= NUM_ARITH_TBLS) { /* define AC table */
   1.398 +      cinfo->arith_ac_K[index-NUM_ARITH_TBLS] = (UINT8) val;
   1.399 +    } else {			/* define DC table */
   1.400 +      cinfo->arith_dc_L[index] = (UINT8) (val & 0x0F);
   1.401 +      cinfo->arith_dc_U[index] = (UINT8) (val >> 4);
   1.402 +      if (cinfo->arith_dc_L[index] > cinfo->arith_dc_U[index])
   1.403 +	ERREXIT1(cinfo, JERR_DAC_VALUE, val);
   1.404 +    }
   1.405 +  }
   1.406 +
   1.407 +  if (length != 0)
   1.408 +    ERREXIT(cinfo, JERR_BAD_LENGTH);
   1.409 +
   1.410 +  INPUT_SYNC(cinfo);
   1.411 +  return TRUE;
   1.412 +}
   1.413 +
   1.414 +#else /* ! D_ARITH_CODING_SUPPORTED */
   1.415 +
   1.416 +#define get_dac(cinfo)  skip_variable(cinfo)
   1.417 +
   1.418 +#endif /* D_ARITH_CODING_SUPPORTED */
   1.419 +
   1.420 +
   1.421 +LOCAL(boolean)
   1.422 +get_dht (j_decompress_ptr cinfo)
   1.423 +/* Process a DHT marker */
   1.424 +{
   1.425 +  INT32 length;
   1.426 +  UINT8 bits[17];
   1.427 +  UINT8 huffval[256];
   1.428 +  int i, index, count;
   1.429 +  JHUFF_TBL **htblptr;
   1.430 +  INPUT_VARS(cinfo);
   1.431 +
   1.432 +  INPUT_2BYTES(cinfo, length, return FALSE);
   1.433 +  length -= 2;
   1.434 +  
   1.435 +  while (length > 16) {
   1.436 +    INPUT_BYTE(cinfo, index, return FALSE);
   1.437 +
   1.438 +    TRACEMS1(cinfo, 1, JTRC_DHT, index);
   1.439 +      
   1.440 +    bits[0] = 0;
   1.441 +    count = 0;
   1.442 +    for (i = 1; i <= 16; i++) {
   1.443 +      INPUT_BYTE(cinfo, bits[i], return FALSE);
   1.444 +      count += bits[i];
   1.445 +    }
   1.446 +
   1.447 +    length -= 1 + 16;
   1.448 +
   1.449 +    TRACEMS8(cinfo, 2, JTRC_HUFFBITS,
   1.450 +	     bits[1], bits[2], bits[3], bits[4],
   1.451 +	     bits[5], bits[6], bits[7], bits[8]);
   1.452 +    TRACEMS8(cinfo, 2, JTRC_HUFFBITS,
   1.453 +	     bits[9], bits[10], bits[11], bits[12],
   1.454 +	     bits[13], bits[14], bits[15], bits[16]);
   1.455 +
   1.456 +    /* Here we just do minimal validation of the counts to avoid walking
   1.457 +     * off the end of our table space.  jdhuff.c will check more carefully.
   1.458 +     */
   1.459 +    if (count > 256 || ((INT32) count) > length)
   1.460 +      ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
   1.461 +
   1.462 +    for (i = 0; i < count; i++)
   1.463 +      INPUT_BYTE(cinfo, huffval[i], return FALSE);
   1.464 +
   1.465 +    length -= count;
   1.466 +
   1.467 +    if (index & 0x10) {		/* AC table definition */
   1.468 +      index -= 0x10;
   1.469 +      htblptr = &cinfo->ac_huff_tbl_ptrs[index];
   1.470 +    } else {			/* DC table definition */
   1.471 +      htblptr = &cinfo->dc_huff_tbl_ptrs[index];
   1.472 +    }
   1.473 +
   1.474 +    if (index < 0 || index >= NUM_HUFF_TBLS)
   1.475 +      ERREXIT1(cinfo, JERR_DHT_INDEX, index);
   1.476 +
   1.477 +    if (*htblptr == NULL)
   1.478 +      *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
   1.479 +  
   1.480 +    MEMCOPY((*htblptr)->bits, bits, SIZEOF((*htblptr)->bits));
   1.481 +    MEMCOPY((*htblptr)->huffval, huffval, SIZEOF((*htblptr)->huffval));
   1.482 +  }
   1.483 +
   1.484 +  if (length != 0)
   1.485 +    ERREXIT(cinfo, JERR_BAD_LENGTH);
   1.486 +
   1.487 +  INPUT_SYNC(cinfo);
   1.488 +  return TRUE;
   1.489 +}
   1.490 +
   1.491 +
   1.492 +LOCAL(boolean)
   1.493 +get_dqt (j_decompress_ptr cinfo)
   1.494 +/* Process a DQT marker */
   1.495 +{
   1.496 +  INT32 length;
   1.497 +  int n, i, prec;
   1.498 +  unsigned int tmp;
   1.499 +  JQUANT_TBL *quant_ptr;
   1.500 +  INPUT_VARS(cinfo);
   1.501 +
   1.502 +  INPUT_2BYTES(cinfo, length, return FALSE);
   1.503 +  length -= 2;
   1.504 +
   1.505 +  while (length > 0) {
   1.506 +    INPUT_BYTE(cinfo, n, return FALSE);
   1.507 +    prec = n >> 4;
   1.508 +    n &= 0x0F;
   1.509 +
   1.510 +    TRACEMS2(cinfo, 1, JTRC_DQT, n, prec);
   1.511 +
   1.512 +    if (n >= NUM_QUANT_TBLS)
   1.513 +      ERREXIT1(cinfo, JERR_DQT_INDEX, n);
   1.514 +      
   1.515 +    if (cinfo->quant_tbl_ptrs[n] == NULL)
   1.516 +      cinfo->quant_tbl_ptrs[n] = jpeg_alloc_quant_table((j_common_ptr) cinfo);
   1.517 +    quant_ptr = cinfo->quant_tbl_ptrs[n];
   1.518 +
   1.519 +    for (i = 0; i < DCTSIZE2; i++) {
   1.520 +      if (prec)
   1.521 +	INPUT_2BYTES(cinfo, tmp, return FALSE);
   1.522 +      else
   1.523 +	INPUT_BYTE(cinfo, tmp, return FALSE);
   1.524 +      /* We convert the zigzag-order table to natural array order. */
   1.525 +      quant_ptr->quantval[jpeg_natural_order[i]] = (UINT16) tmp;
   1.526 +    }
   1.527 +
   1.528 +    if (cinfo->err->trace_level >= 2) {
   1.529 +      for (i = 0; i < DCTSIZE2; i += 8) {
   1.530 +	TRACEMS8(cinfo, 2, JTRC_QUANTVALS,
   1.531 +		 quant_ptr->quantval[i],   quant_ptr->quantval[i+1],
   1.532 +		 quant_ptr->quantval[i+2], quant_ptr->quantval[i+3],
   1.533 +		 quant_ptr->quantval[i+4], quant_ptr->quantval[i+5],
   1.534 +		 quant_ptr->quantval[i+6], quant_ptr->quantval[i+7]);
   1.535 +      }
   1.536 +    }
   1.537 +
   1.538 +    length -= DCTSIZE2+1;
   1.539 +    if (prec) length -= DCTSIZE2;
   1.540 +  }
   1.541 +
   1.542 +  if (length != 0)
   1.543 +    ERREXIT(cinfo, JERR_BAD_LENGTH);
   1.544 +
   1.545 +  INPUT_SYNC(cinfo);
   1.546 +  return TRUE;
   1.547 +}
   1.548 +
   1.549 +
   1.550 +LOCAL(boolean)
   1.551 +get_dri (j_decompress_ptr cinfo)
   1.552 +/* Process a DRI marker */
   1.553 +{
   1.554 +  INT32 length;
   1.555 +  unsigned int tmp;
   1.556 +  INPUT_VARS(cinfo);
   1.557 +
   1.558 +  INPUT_2BYTES(cinfo, length, return FALSE);
   1.559 +  
   1.560 +  if (length != 4)
   1.561 +    ERREXIT(cinfo, JERR_BAD_LENGTH);
   1.562 +
   1.563 +  INPUT_2BYTES(cinfo, tmp, return FALSE);
   1.564 +
   1.565 +  TRACEMS1(cinfo, 1, JTRC_DRI, tmp);
   1.566 +
   1.567 +  cinfo->restart_interval = tmp;
   1.568 +
   1.569 +  INPUT_SYNC(cinfo);
   1.570 +  return TRUE;
   1.571 +}
   1.572 +
   1.573 +
   1.574 +/*
   1.575 + * Routines for processing APPn and COM markers.
   1.576 + * These are either saved in memory or discarded, per application request.
   1.577 + * APP0 and APP14 are specially checked to see if they are
   1.578 + * JFIF and Adobe markers, respectively.
   1.579 + */
   1.580 +
   1.581 +#define APP0_DATA_LEN	14	/* Length of interesting data in APP0 */
   1.582 +#define APP14_DATA_LEN	12	/* Length of interesting data in APP14 */
   1.583 +#define APPN_DATA_LEN	14	/* Must be the largest of the above!! */
   1.584 +
   1.585 +
   1.586 +LOCAL(void)
   1.587 +examine_app0 (j_decompress_ptr cinfo, JOCTET FAR * data,
   1.588 +	      unsigned int datalen, INT32 remaining)
   1.589 +/* Examine first few bytes from an APP0.
   1.590 + * Take appropriate action if it is a JFIF marker.
   1.591 + * datalen is # of bytes at data[], remaining is length of rest of marker data.
   1.592 + */
   1.593 +{
   1.594 +  INT32 totallen = (INT32) datalen + remaining;
   1.595 +
   1.596 +  if (datalen >= APP0_DATA_LEN &&
   1.597 +      GETJOCTET(data[0]) == 0x4A &&
   1.598 +      GETJOCTET(data[1]) == 0x46 &&
   1.599 +      GETJOCTET(data[2]) == 0x49 &&
   1.600 +      GETJOCTET(data[3]) == 0x46 &&
   1.601 +      GETJOCTET(data[4]) == 0) {
   1.602 +    /* Found JFIF APP0 marker: save info */
   1.603 +    cinfo->saw_JFIF_marker = TRUE;
   1.604 +    cinfo->JFIF_major_version = GETJOCTET(data[5]);
   1.605 +    cinfo->JFIF_minor_version = GETJOCTET(data[6]);
   1.606 +    cinfo->density_unit = GETJOCTET(data[7]);
   1.607 +    cinfo->X_density = (GETJOCTET(data[8]) << 8) + GETJOCTET(data[9]);
   1.608 +    cinfo->Y_density = (GETJOCTET(data[10]) << 8) + GETJOCTET(data[11]);
   1.609 +    /* Check version.
   1.610 +     * Major version must be 1, anything else signals an incompatible change.
   1.611 +     * (We used to treat this as an error, but now it's a nonfatal warning,
   1.612 +     * because some bozo at Hijaak couldn't read the spec.)
   1.613 +     * Minor version should be 0..2, but process anyway if newer.
   1.614 +     */
   1.615 +    if (cinfo->JFIF_major_version != 1)
   1.616 +      WARNMS2(cinfo, JWRN_JFIF_MAJOR,
   1.617 +	      cinfo->JFIF_major_version, cinfo->JFIF_minor_version);
   1.618 +    /* Generate trace messages */
   1.619 +    TRACEMS5(cinfo, 1, JTRC_JFIF,
   1.620 +	     cinfo->JFIF_major_version, cinfo->JFIF_minor_version,
   1.621 +	     cinfo->X_density, cinfo->Y_density, cinfo->density_unit);
   1.622 +    /* Validate thumbnail dimensions and issue appropriate messages */
   1.623 +    if (GETJOCTET(data[12]) | GETJOCTET(data[13]))
   1.624 +      TRACEMS2(cinfo, 1, JTRC_JFIF_THUMBNAIL,
   1.625 +	       GETJOCTET(data[12]), GETJOCTET(data[13]));
   1.626 +    totallen -= APP0_DATA_LEN;
   1.627 +    if (totallen !=
   1.628 +	((INT32)GETJOCTET(data[12]) * (INT32)GETJOCTET(data[13]) * (INT32) 3))
   1.629 +      TRACEMS1(cinfo, 1, JTRC_JFIF_BADTHUMBNAILSIZE, (int) totallen);
   1.630 +  } else if (datalen >= 6 &&
   1.631 +      GETJOCTET(data[0]) == 0x4A &&
   1.632 +      GETJOCTET(data[1]) == 0x46 &&
   1.633 +      GETJOCTET(data[2]) == 0x58 &&
   1.634 +      GETJOCTET(data[3]) == 0x58 &&
   1.635 +      GETJOCTET(data[4]) == 0) {
   1.636 +    /* Found JFIF "JFXX" extension APP0 marker */
   1.637 +    /* The library doesn't actually do anything with these,
   1.638 +     * but we try to produce a helpful trace message.
   1.639 +     */
   1.640 +    switch (GETJOCTET(data[5])) {
   1.641 +    case 0x10:
   1.642 +      TRACEMS1(cinfo, 1, JTRC_THUMB_JPEG, (int) totallen);
   1.643 +      break;
   1.644 +    case 0x11:
   1.645 +      TRACEMS1(cinfo, 1, JTRC_THUMB_PALETTE, (int) totallen);
   1.646 +      break;
   1.647 +    case 0x13:
   1.648 +      TRACEMS1(cinfo, 1, JTRC_THUMB_RGB, (int) totallen);
   1.649 +      break;
   1.650 +    default:
   1.651 +      TRACEMS2(cinfo, 1, JTRC_JFIF_EXTENSION,
   1.652 +	       GETJOCTET(data[5]), (int) totallen);
   1.653 +      break;
   1.654 +    }
   1.655 +  } else {
   1.656 +    /* Start of APP0 does not match "JFIF" or "JFXX", or too short */
   1.657 +    TRACEMS1(cinfo, 1, JTRC_APP0, (int) totallen);
   1.658 +  }
   1.659 +}
   1.660 +
   1.661 +
   1.662 +LOCAL(void)
   1.663 +examine_app14 (j_decompress_ptr cinfo, JOCTET FAR * data,
   1.664 +	       unsigned int datalen, INT32 remaining)
   1.665 +/* Examine first few bytes from an APP14.
   1.666 + * Take appropriate action if it is an Adobe marker.
   1.667 + * datalen is # of bytes at data[], remaining is length of rest of marker data.
   1.668 + */
   1.669 +{
   1.670 +  unsigned int version, flags0, flags1, transform;
   1.671 +
   1.672 +  if (datalen >= APP14_DATA_LEN &&
   1.673 +      GETJOCTET(data[0]) == 0x41 &&
   1.674 +      GETJOCTET(data[1]) == 0x64 &&
   1.675 +      GETJOCTET(data[2]) == 0x6F &&
   1.676 +      GETJOCTET(data[3]) == 0x62 &&
   1.677 +      GETJOCTET(data[4]) == 0x65) {
   1.678 +    /* Found Adobe APP14 marker */
   1.679 +    version = (GETJOCTET(data[5]) << 8) + GETJOCTET(data[6]);
   1.680 +    flags0 = (GETJOCTET(data[7]) << 8) + GETJOCTET(data[8]);
   1.681 +    flags1 = (GETJOCTET(data[9]) << 8) + GETJOCTET(data[10]);
   1.682 +    transform = GETJOCTET(data[11]);
   1.683 +    TRACEMS4(cinfo, 1, JTRC_ADOBE, version, flags0, flags1, transform);
   1.684 +    cinfo->saw_Adobe_marker = TRUE;
   1.685 +    cinfo->Adobe_transform = (UINT8) transform;
   1.686 +  } else {
   1.687 +    /* Start of APP14 does not match "Adobe", or too short */
   1.688 +    TRACEMS1(cinfo, 1, JTRC_APP14, (int) (datalen + remaining));
   1.689 +  }
   1.690 +}
   1.691 +
   1.692 +
   1.693 +METHODDEF(boolean)
   1.694 +get_interesting_appn (j_decompress_ptr cinfo)
   1.695 +/* Process an APP0 or APP14 marker without saving it */
   1.696 +{
   1.697 +  INT32 length;
   1.698 +  JOCTET b[APPN_DATA_LEN];
   1.699 +  unsigned int i, numtoread;
   1.700 +  INPUT_VARS(cinfo);
   1.701 +
   1.702 +  INPUT_2BYTES(cinfo, length, return FALSE);
   1.703 +  length -= 2;
   1.704 +
   1.705 +  /* get the interesting part of the marker data */
   1.706 +  if (length >= APPN_DATA_LEN)
   1.707 +    numtoread = APPN_DATA_LEN;
   1.708 +  else if (length > 0)
   1.709 +    numtoread = (unsigned int) length;
   1.710 +  else
   1.711 +    numtoread = 0;
   1.712 +  for (i = 0; i < numtoread; i++)
   1.713 +    INPUT_BYTE(cinfo, b[i], return FALSE);
   1.714 +  length -= numtoread;
   1.715 +
   1.716 +  /* process it */
   1.717 +  switch (cinfo->unread_marker) {
   1.718 +  case M_APP0:
   1.719 +    examine_app0(cinfo, (JOCTET FAR *) b, numtoread, length);
   1.720 +    break;
   1.721 +  case M_APP14:
   1.722 +    examine_app14(cinfo, (JOCTET FAR *) b, numtoread, length);
   1.723 +    break;
   1.724 +  default:
   1.725 +    /* can't get here unless jpeg_save_markers chooses wrong processor */
   1.726 +    ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, cinfo->unread_marker);
   1.727 +    break;
   1.728 +  }
   1.729 +
   1.730 +  /* skip any remaining data -- could be lots */
   1.731 +  INPUT_SYNC(cinfo);
   1.732 +  if (length > 0)
   1.733 +    (*cinfo->src->skip_input_data) (cinfo, (long) length);
   1.734 +
   1.735 +  return TRUE;
   1.736 +}
   1.737 +
   1.738 +
   1.739 +#ifdef SAVE_MARKERS_SUPPORTED
   1.740 +
   1.741 +METHODDEF(boolean)
   1.742 +save_marker (j_decompress_ptr cinfo)
   1.743 +/* Save an APPn or COM marker into the marker list */
   1.744 +{
   1.745 +  my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
   1.746 +  jpeg_saved_marker_ptr cur_marker = marker->cur_marker;
   1.747 +  unsigned int bytes_read, data_length;
   1.748 +  JOCTET FAR * data;
   1.749 +  INT32 length = 0;
   1.750 +  INPUT_VARS(cinfo);
   1.751 +
   1.752 +  if (cur_marker == NULL) {
   1.753 +    /* begin reading a marker */
   1.754 +    INPUT_2BYTES(cinfo, length, return FALSE);
   1.755 +    length -= 2;
   1.756 +    if (length >= 0) {		/* watch out for bogus length word */
   1.757 +      /* figure out how much we want to save */
   1.758 +      unsigned int limit;
   1.759 +      if (cinfo->unread_marker == (int) M_COM)
   1.760 +	limit = marker->length_limit_COM;
   1.761 +      else
   1.762 +	limit = marker->length_limit_APPn[cinfo->unread_marker - (int) M_APP0];
   1.763 +      if ((unsigned int) length < limit)
   1.764 +	limit = (unsigned int) length;
   1.765 +      /* allocate and initialize the marker item */
   1.766 +      cur_marker = (jpeg_saved_marker_ptr)
   1.767 +	(*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
   1.768 +				    SIZEOF(struct jpeg_marker_struct) + limit);
   1.769 +      cur_marker->next = NULL;
   1.770 +      cur_marker->marker = (UINT8) cinfo->unread_marker;
   1.771 +      cur_marker->original_length = (unsigned int) length;
   1.772 +      cur_marker->data_length = limit;
   1.773 +      /* data area is just beyond the jpeg_marker_struct */
   1.774 +      data = cur_marker->data = (JOCTET FAR *) (cur_marker + 1);
   1.775 +      marker->cur_marker = cur_marker;
   1.776 +      marker->bytes_read = 0;
   1.777 +      bytes_read = 0;
   1.778 +      data_length = limit;
   1.779 +    } else {
   1.780 +      /* deal with bogus length word */
   1.781 +      bytes_read = data_length = 0;
   1.782 +      data = NULL;
   1.783 +    }
   1.784 +  } else {
   1.785 +    /* resume reading a marker */
   1.786 +    bytes_read = marker->bytes_read;
   1.787 +    data_length = cur_marker->data_length;
   1.788 +    data = cur_marker->data + bytes_read;
   1.789 +  }
   1.790 +
   1.791 +  while (bytes_read < data_length) {
   1.792 +    INPUT_SYNC(cinfo);		/* move the restart point to here */
   1.793 +    marker->bytes_read = bytes_read;
   1.794 +    /* If there's not at least one byte in buffer, suspend */
   1.795 +    MAKE_BYTE_AVAIL(cinfo, return FALSE);
   1.796 +    /* Copy bytes with reasonable rapidity */
   1.797 +    while (bytes_read < data_length && bytes_in_buffer > 0) {
   1.798 +      *data++ = *next_input_byte++;
   1.799 +      bytes_in_buffer--;
   1.800 +      bytes_read++;
   1.801 +    }
   1.802 +  }
   1.803 +
   1.804 +  /* Done reading what we want to read */
   1.805 +  if (cur_marker != NULL) {	/* will be NULL if bogus length word */
   1.806 +    /* Add new marker to end of list */
   1.807 +    if (cinfo->marker_list == NULL) {
   1.808 +      cinfo->marker_list = cur_marker;
   1.809 +    } else {
   1.810 +      jpeg_saved_marker_ptr prev = cinfo->marker_list;
   1.811 +      while (prev->next != NULL)
   1.812 +	prev = prev->next;
   1.813 +      prev->next = cur_marker;
   1.814 +    }
   1.815 +    /* Reset pointer & calc remaining data length */
   1.816 +    data = cur_marker->data;
   1.817 +    length = cur_marker->original_length - data_length;
   1.818 +  }
   1.819 +  /* Reset to initial state for next marker */
   1.820 +  marker->cur_marker = NULL;
   1.821 +
   1.822 +  /* Process the marker if interesting; else just make a generic trace msg */
   1.823 +  switch (cinfo->unread_marker) {
   1.824 +  case M_APP0:
   1.825 +    examine_app0(cinfo, data, data_length, length);
   1.826 +    break;
   1.827 +  case M_APP14:
   1.828 +    examine_app14(cinfo, data, data_length, length);
   1.829 +    break;
   1.830 +  default:
   1.831 +    TRACEMS2(cinfo, 1, JTRC_MISC_MARKER, cinfo->unread_marker,
   1.832 +	     (int) (data_length + length));
   1.833 +    break;
   1.834 +  }
   1.835 +
   1.836 +  /* skip any remaining data -- could be lots */
   1.837 +  INPUT_SYNC(cinfo);		/* do before skip_input_data */
   1.838 +  if (length > 0)
   1.839 +    (*cinfo->src->skip_input_data) (cinfo, (long) length);
   1.840 +
   1.841 +  return TRUE;
   1.842 +}
   1.843 +
   1.844 +#endif /* SAVE_MARKERS_SUPPORTED */
   1.845 +
   1.846 +
   1.847 +METHODDEF(boolean)
   1.848 +skip_variable (j_decompress_ptr cinfo)
   1.849 +/* Skip over an unknown or uninteresting variable-length marker */
   1.850 +{
   1.851 +  INT32 length;
   1.852 +  INPUT_VARS(cinfo);
   1.853 +
   1.854 +  INPUT_2BYTES(cinfo, length, return FALSE);
   1.855 +  length -= 2;
   1.856 +  
   1.857 +  TRACEMS2(cinfo, 1, JTRC_MISC_MARKER, cinfo->unread_marker, (int) length);
   1.858 +
   1.859 +  INPUT_SYNC(cinfo);		/* do before skip_input_data */
   1.860 +  if (length > 0)
   1.861 +    (*cinfo->src->skip_input_data) (cinfo, (long) length);
   1.862 +
   1.863 +  return TRUE;
   1.864 +}
   1.865 +
   1.866 +
   1.867 +/*
   1.868 + * Find the next JPEG marker, save it in cinfo->unread_marker.
   1.869 + * Returns FALSE if had to suspend before reaching a marker;
   1.870 + * in that case cinfo->unread_marker is unchanged.
   1.871 + *
   1.872 + * Note that the result might not be a valid marker code,
   1.873 + * but it will never be 0 or FF.
   1.874 + */
   1.875 +
   1.876 +LOCAL(boolean)
   1.877 +next_marker (j_decompress_ptr cinfo)
   1.878 +{
   1.879 +  int c;
   1.880 +  INPUT_VARS(cinfo);
   1.881 +
   1.882 +  for (;;) {
   1.883 +    INPUT_BYTE(cinfo, c, return FALSE);
   1.884 +    /* Skip any non-FF bytes.
   1.885 +     * This may look a bit inefficient, but it will not occur in a valid file.
   1.886 +     * We sync after each discarded byte so that a suspending data source
   1.887 +     * can discard the byte from its buffer.
   1.888 +     */
   1.889 +    while (c != 0xFF) {
   1.890 +      cinfo->marker->discarded_bytes++;
   1.891 +      INPUT_SYNC(cinfo);
   1.892 +      INPUT_BYTE(cinfo, c, return FALSE);
   1.893 +    }
   1.894 +    /* This loop swallows any duplicate FF bytes.  Extra FFs are legal as
   1.895 +     * pad bytes, so don't count them in discarded_bytes.  We assume there
   1.896 +     * will not be so many consecutive FF bytes as to overflow a suspending
   1.897 +     * data source's input buffer.
   1.898 +     */
   1.899 +    do {
   1.900 +      INPUT_BYTE(cinfo, c, return FALSE);
   1.901 +    } while (c == 0xFF);
   1.902 +    if (c != 0)
   1.903 +      break;			/* found a valid marker, exit loop */
   1.904 +    /* Reach here if we found a stuffed-zero data sequence (FF/00).
   1.905 +     * Discard it and loop back to try again.
   1.906 +     */
   1.907 +    cinfo->marker->discarded_bytes += 2;
   1.908 +    INPUT_SYNC(cinfo);
   1.909 +  }
   1.910 +
   1.911 +  if (cinfo->marker->discarded_bytes != 0) {
   1.912 +    WARNMS2(cinfo, JWRN_EXTRANEOUS_DATA, cinfo->marker->discarded_bytes, c);
   1.913 +    cinfo->marker->discarded_bytes = 0;
   1.914 +  }
   1.915 +
   1.916 +  cinfo->unread_marker = c;
   1.917 +
   1.918 +  INPUT_SYNC(cinfo);
   1.919 +  return TRUE;
   1.920 +}
   1.921 +
   1.922 +
   1.923 +LOCAL(boolean)
   1.924 +first_marker (j_decompress_ptr cinfo)
   1.925 +/* Like next_marker, but used to obtain the initial SOI marker. */
   1.926 +/* For this marker, we do not allow preceding garbage or fill; otherwise,
   1.927 + * we might well scan an entire input file before realizing it ain't JPEG.
   1.928 + * If an application wants to process non-JFIF files, it must seek to the
   1.929 + * SOI before calling the JPEG library.
   1.930 + */
   1.931 +{
   1.932 +  int c, c2;
   1.933 +  INPUT_VARS(cinfo);
   1.934 +
   1.935 +  INPUT_BYTE(cinfo, c, return FALSE);
   1.936 +  INPUT_BYTE(cinfo, c2, return FALSE);
   1.937 +  if (c != 0xFF || c2 != (int) M_SOI)
   1.938 +    ERREXIT2(cinfo, JERR_NO_SOI, c, c2);
   1.939 +
   1.940 +  cinfo->unread_marker = c2;
   1.941 +
   1.942 +  INPUT_SYNC(cinfo);
   1.943 +  return TRUE;
   1.944 +}
   1.945 +
   1.946 +
   1.947 +/*
   1.948 + * Read markers until SOS or EOI.
   1.949 + *
   1.950 + * Returns same codes as are defined for jpeg_consume_input:
   1.951 + * JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.
   1.952 + */
   1.953 +
   1.954 +METHODDEF(int)
   1.955 +read_markers (j_decompress_ptr cinfo)
   1.956 +{
   1.957 +  /* Outer loop repeats once for each marker. */
   1.958 +  for (;;) {
   1.959 +    /* Collect the marker proper, unless we already did. */
   1.960 +    /* NB: first_marker() enforces the requirement that SOI appear first. */
   1.961 +    if (cinfo->unread_marker == 0) {
   1.962 +      if (! cinfo->marker->saw_SOI) {
   1.963 +	if (! first_marker(cinfo))
   1.964 +	  return JPEG_SUSPENDED;
   1.965 +      } else {
   1.966 +	if (! next_marker(cinfo))
   1.967 +	  return JPEG_SUSPENDED;
   1.968 +      }
   1.969 +    }
   1.970 +    /* At this point cinfo->unread_marker contains the marker code and the
   1.971 +     * input point is just past the marker proper, but before any parameters.
   1.972 +     * A suspension will cause us to return with this state still true.
   1.973 +     */
   1.974 +    switch (cinfo->unread_marker) {
   1.975 +    case M_SOI:
   1.976 +      if (! get_soi(cinfo))
   1.977 +	return JPEG_SUSPENDED;
   1.978 +      break;
   1.979 +
   1.980 +    case M_SOF0:		/* Baseline */
   1.981 +    case M_SOF1:		/* Extended sequential, Huffman */
   1.982 +      if (! get_sof(cinfo, FALSE, FALSE))
   1.983 +	return JPEG_SUSPENDED;
   1.984 +      break;
   1.985 +
   1.986 +    case M_SOF2:		/* Progressive, Huffman */
   1.987 +      if (! get_sof(cinfo, TRUE, FALSE))
   1.988 +	return JPEG_SUSPENDED;
   1.989 +      break;
   1.990 +
   1.991 +    case M_SOF9:		/* Extended sequential, arithmetic */
   1.992 +      if (! get_sof(cinfo, FALSE, TRUE))
   1.993 +	return JPEG_SUSPENDED;
   1.994 +      break;
   1.995 +
   1.996 +    case M_SOF10:		/* Progressive, arithmetic */
   1.997 +      if (! get_sof(cinfo, TRUE, TRUE))
   1.998 +	return JPEG_SUSPENDED;
   1.999 +      break;
  1.1000 +
  1.1001 +    /* Currently unsupported SOFn types */
  1.1002 +    case M_SOF3:		/* Lossless, Huffman */
  1.1003 +    case M_SOF5:		/* Differential sequential, Huffman */
  1.1004 +    case M_SOF6:		/* Differential progressive, Huffman */
  1.1005 +    case M_SOF7:		/* Differential lossless, Huffman */
  1.1006 +    case M_JPG:			/* Reserved for JPEG extensions */
  1.1007 +    case M_SOF11:		/* Lossless, arithmetic */
  1.1008 +    case M_SOF13:		/* Differential sequential, arithmetic */
  1.1009 +    case M_SOF14:		/* Differential progressive, arithmetic */
  1.1010 +    case M_SOF15:		/* Differential lossless, arithmetic */
  1.1011 +      ERREXIT1(cinfo, JERR_SOF_UNSUPPORTED, cinfo->unread_marker);
  1.1012 +      break;
  1.1013 +
  1.1014 +    case M_SOS:
  1.1015 +      if (! get_sos(cinfo))
  1.1016 +	return JPEG_SUSPENDED;
  1.1017 +      cinfo->unread_marker = 0;	/* processed the marker */
  1.1018 +      return JPEG_REACHED_SOS;
  1.1019 +    
  1.1020 +    case M_EOI:
  1.1021 +      TRACEMS(cinfo, 1, JTRC_EOI);
  1.1022 +      cinfo->unread_marker = 0;	/* processed the marker */
  1.1023 +      return JPEG_REACHED_EOI;
  1.1024 +      
  1.1025 +    case M_DAC:
  1.1026 +      if (! get_dac(cinfo))
  1.1027 +	return JPEG_SUSPENDED;
  1.1028 +      break;
  1.1029 +      
  1.1030 +    case M_DHT:
  1.1031 +      if (! get_dht(cinfo))
  1.1032 +	return JPEG_SUSPENDED;
  1.1033 +      break;
  1.1034 +      
  1.1035 +    case M_DQT:
  1.1036 +      if (! get_dqt(cinfo))
  1.1037 +	return JPEG_SUSPENDED;
  1.1038 +      break;
  1.1039 +      
  1.1040 +    case M_DRI:
  1.1041 +      if (! get_dri(cinfo))
  1.1042 +	return JPEG_SUSPENDED;
  1.1043 +      break;
  1.1044 +      
  1.1045 +    case M_APP0:
  1.1046 +    case M_APP1:
  1.1047 +    case M_APP2:
  1.1048 +    case M_APP3:
  1.1049 +    case M_APP4:
  1.1050 +    case M_APP5:
  1.1051 +    case M_APP6:
  1.1052 +    case M_APP7:
  1.1053 +    case M_APP8:
  1.1054 +    case M_APP9:
  1.1055 +    case M_APP10:
  1.1056 +    case M_APP11:
  1.1057 +    case M_APP12:
  1.1058 +    case M_APP13:
  1.1059 +    case M_APP14:
  1.1060 +    case M_APP15:
  1.1061 +      if (! (*((my_marker_ptr) cinfo->marker)->process_APPn[
  1.1062 +		cinfo->unread_marker - (int) M_APP0]) (cinfo))
  1.1063 +	return JPEG_SUSPENDED;
  1.1064 +      break;
  1.1065 +      
  1.1066 +    case M_COM:
  1.1067 +      if (! (*((my_marker_ptr) cinfo->marker)->process_COM) (cinfo))
  1.1068 +	return JPEG_SUSPENDED;
  1.1069 +      break;
  1.1070 +
  1.1071 +    case M_RST0:		/* these are all parameterless */
  1.1072 +    case M_RST1:
  1.1073 +    case M_RST2:
  1.1074 +    case M_RST3:
  1.1075 +    case M_RST4:
  1.1076 +    case M_RST5:
  1.1077 +    case M_RST6:
  1.1078 +    case M_RST7:
  1.1079 +    case M_TEM:
  1.1080 +      TRACEMS1(cinfo, 1, JTRC_PARMLESS_MARKER, cinfo->unread_marker);
  1.1081 +      break;
  1.1082 +
  1.1083 +    case M_DNL:			/* Ignore DNL ... perhaps the wrong thing */
  1.1084 +      if (! skip_variable(cinfo))
  1.1085 +	return JPEG_SUSPENDED;
  1.1086 +      break;
  1.1087 +
  1.1088 +    default:			/* must be DHP, EXP, JPGn, or RESn */
  1.1089 +      /* For now, we treat the reserved markers as fatal errors since they are
  1.1090 +       * likely to be used to signal incompatible JPEG Part 3 extensions.
  1.1091 +       * Once the JPEG 3 version-number marker is well defined, this code
  1.1092 +       * ought to change!
  1.1093 +       */
  1.1094 +      ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, cinfo->unread_marker);
  1.1095 +      break;
  1.1096 +    }
  1.1097 +    /* Successfully processed marker, so reset state variable */
  1.1098 +    cinfo->unread_marker = 0;
  1.1099 +  } /* end loop */
  1.1100 +}
  1.1101 +
  1.1102 +
  1.1103 +/*
  1.1104 + * Read a restart marker, which is expected to appear next in the datastream;
  1.1105 + * if the marker is not there, take appropriate recovery action.
  1.1106 + * Returns FALSE if suspension is required.
  1.1107 + *
  1.1108 + * This is called by the entropy decoder after it has read an appropriate
  1.1109 + * number of MCUs.  cinfo->unread_marker may be nonzero if the entropy decoder
  1.1110 + * has already read a marker from the data source.  Under normal conditions
  1.1111 + * cinfo->unread_marker will be reset to 0 before returning; if not reset,
  1.1112 + * it holds a marker which the decoder will be unable to read past.
  1.1113 + */
  1.1114 +
  1.1115 +METHODDEF(boolean)
  1.1116 +read_restart_marker (j_decompress_ptr cinfo)
  1.1117 +{
  1.1118 +  /* Obtain a marker unless we already did. */
  1.1119 +  /* Note that next_marker will complain if it skips any data. */
  1.1120 +  if (cinfo->unread_marker == 0) {
  1.1121 +    if (! next_marker(cinfo))
  1.1122 +      return FALSE;
  1.1123 +  }
  1.1124 +
  1.1125 +  if (cinfo->unread_marker ==
  1.1126 +      ((int) M_RST0 + cinfo->marker->next_restart_num)) {
  1.1127 +    /* Normal case --- swallow the marker and let entropy decoder continue */
  1.1128 +    TRACEMS1(cinfo, 3, JTRC_RST, cinfo->marker->next_restart_num);
  1.1129 +    cinfo->unread_marker = 0;
  1.1130 +  } else {
  1.1131 +    /* Uh-oh, the restart markers have been messed up. */
  1.1132 +    /* Let the data source manager determine how to resync. */
  1.1133 +    if (! (*cinfo->src->resync_to_restart) (cinfo,
  1.1134 +					    cinfo->marker->next_restart_num))
  1.1135 +      return FALSE;
  1.1136 +  }
  1.1137 +
  1.1138 +  /* Update next-restart state */
  1.1139 +  cinfo->marker->next_restart_num = (cinfo->marker->next_restart_num + 1) & 7;
  1.1140 +
  1.1141 +  return TRUE;
  1.1142 +}
  1.1143 +
  1.1144 +
  1.1145 +/*
  1.1146 + * This is the default resync_to_restart method for data source managers
  1.1147 + * to use if they don't have any better approach.  Some data source managers
  1.1148 + * may be able to back up, or may have additional knowledge about the data
  1.1149 + * which permits a more intelligent recovery strategy; such managers would
  1.1150 + * presumably supply their own resync method.
  1.1151 + *
  1.1152 + * read_restart_marker calls resync_to_restart if it finds a marker other than
  1.1153 + * the restart marker it was expecting.  (This code is *not* used unless
  1.1154 + * a nonzero restart interval has been declared.)  cinfo->unread_marker is
  1.1155 + * the marker code actually found (might be anything, except 0 or FF).
  1.1156 + * The desired restart marker number (0..7) is passed as a parameter.
  1.1157 + * This routine is supposed to apply whatever error recovery strategy seems
  1.1158 + * appropriate in order to position the input stream to the next data segment.
  1.1159 + * Note that cinfo->unread_marker is treated as a marker appearing before
  1.1160 + * the current data-source input point; usually it should be reset to zero
  1.1161 + * before returning.
  1.1162 + * Returns FALSE if suspension is required.
  1.1163 + *
  1.1164 + * This implementation is substantially constrained by wanting to treat the
  1.1165 + * input as a data stream; this means we can't back up.  Therefore, we have
  1.1166 + * only the following actions to work with:
  1.1167 + *   1. Simply discard the marker and let the entropy decoder resume at next
  1.1168 + *      byte of file.
  1.1169 + *   2. Read forward until we find another marker, discarding intervening
  1.1170 + *      data.  (In theory we could look ahead within the current bufferload,
  1.1171 + *      without having to discard data if we don't find the desired marker.
  1.1172 + *      This idea is not implemented here, in part because it makes behavior
  1.1173 + *      dependent on buffer size and chance buffer-boundary positions.)
  1.1174 + *   3. Leave the marker unread (by failing to zero cinfo->unread_marker).
  1.1175 + *      This will cause the entropy decoder to process an empty data segment,
  1.1176 + *      inserting dummy zeroes, and then we will reprocess the marker.
  1.1177 + *
  1.1178 + * #2 is appropriate if we think the desired marker lies ahead, while #3 is
  1.1179 + * appropriate if the found marker is a future restart marker (indicating
  1.1180 + * that we have missed the desired restart marker, probably because it got
  1.1181 + * corrupted).
  1.1182 + * We apply #2 or #3 if the found marker is a restart marker no more than
  1.1183 + * two counts behind or ahead of the expected one.  We also apply #2 if the
  1.1184 + * found marker is not a legal JPEG marker code (it's certainly bogus data).
  1.1185 + * If the found marker is a restart marker more than 2 counts away, we do #1
  1.1186 + * (too much risk that the marker is erroneous; with luck we will be able to
  1.1187 + * resync at some future point).
  1.1188 + * For any valid non-restart JPEG marker, we apply #3.  This keeps us from
  1.1189 + * overrunning the end of a scan.  An implementation limited to single-scan
  1.1190 + * files might find it better to apply #2 for markers other than EOI, since
  1.1191 + * any other marker would have to be bogus data in that case.
  1.1192 + */
  1.1193 +
  1.1194 +GLOBAL(boolean)
  1.1195 +jpeg_resync_to_restart (j_decompress_ptr cinfo, int desired)
  1.1196 +{
  1.1197 +  int marker = cinfo->unread_marker;
  1.1198 +  int action = 1;
  1.1199 +  
  1.1200 +  /* Always put up a warning. */
  1.1201 +  WARNMS2(cinfo, JWRN_MUST_RESYNC, marker, desired);
  1.1202 +  
  1.1203 +  /* Outer loop handles repeated decision after scanning forward. */
  1.1204 +  for (;;) {
  1.1205 +    if (marker < (int) M_SOF0)
  1.1206 +      action = 2;		/* invalid marker */
  1.1207 +    else if (marker < (int) M_RST0 || marker > (int) M_RST7)
  1.1208 +      action = 3;		/* valid non-restart marker */
  1.1209 +    else {
  1.1210 +      if (marker == ((int) M_RST0 + ((desired+1) & 7)) ||
  1.1211 +	  marker == ((int) M_RST0 + ((desired+2) & 7)))
  1.1212 +	action = 3;		/* one of the next two expected restarts */
  1.1213 +      else if (marker == ((int) M_RST0 + ((desired-1) & 7)) ||
  1.1214 +	       marker == ((int) M_RST0 + ((desired-2) & 7)))
  1.1215 +	action = 2;		/* a prior restart, so advance */
  1.1216 +      else
  1.1217 +	action = 1;		/* desired restart or too far away */
  1.1218 +    }
  1.1219 +    TRACEMS2(cinfo, 4, JTRC_RECOVERY_ACTION, marker, action);
  1.1220 +    switch (action) {
  1.1221 +    case 1:
  1.1222 +      /* Discard marker and let entropy decoder resume processing. */
  1.1223 +      cinfo->unread_marker = 0;
  1.1224 +      return TRUE;
  1.1225 +    case 2:
  1.1226 +      /* Scan to the next marker, and repeat the decision loop. */
  1.1227 +      if (! next_marker(cinfo))
  1.1228 +	return FALSE;
  1.1229 +      marker = cinfo->unread_marker;
  1.1230 +      break;
  1.1231 +    case 3:
  1.1232 +      /* Return without advancing past this marker. */
  1.1233 +      /* Entropy decoder will be forced to process an empty segment. */
  1.1234 +      return TRUE;
  1.1235 +    }
  1.1236 +  } /* end loop */
  1.1237 +}
  1.1238 +
  1.1239 +
  1.1240 +/*
  1.1241 + * Reset marker processing state to begin a fresh datastream.
  1.1242 + */
  1.1243 +
  1.1244 +METHODDEF(void)
  1.1245 +reset_marker_reader (j_decompress_ptr cinfo)
  1.1246 +{
  1.1247 +  my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
  1.1248 +
  1.1249 +  cinfo->comp_info = NULL;		/* until allocated by get_sof */
  1.1250 +  cinfo->input_scan_number = 0;		/* no SOS seen yet */
  1.1251 +  cinfo->unread_marker = 0;		/* no pending marker */
  1.1252 +  marker->pub.saw_SOI = FALSE;		/* set internal state too */
  1.1253 +  marker->pub.saw_SOF = FALSE;
  1.1254 +  marker->pub.discarded_bytes = 0;
  1.1255 +  marker->cur_marker = NULL;
  1.1256 +}
  1.1257 +
  1.1258 +
  1.1259 +/*
  1.1260 + * Initialize the marker reader module.
  1.1261 + * This is called only once, when the decompression object is created.
  1.1262 + */
  1.1263 +
  1.1264 +GLOBAL(void)
  1.1265 +jinit_marker_reader (j_decompress_ptr cinfo)
  1.1266 +{
  1.1267 +  my_marker_ptr marker;
  1.1268 +  int i;
  1.1269 +
  1.1270 +  /* Create subobject in permanent pool */
  1.1271 +  marker = (my_marker_ptr)
  1.1272 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
  1.1273 +				SIZEOF(my_marker_reader));
  1.1274 +  cinfo->marker = (struct jpeg_marker_reader *) marker;
  1.1275 +  /* Initialize public method pointers */
  1.1276 +  marker->pub.reset_marker_reader = reset_marker_reader;
  1.1277 +  marker->pub.read_markers = read_markers;
  1.1278 +  marker->pub.read_restart_marker = read_restart_marker;
  1.1279 +  /* Initialize COM/APPn processing.
  1.1280 +   * By default, we examine and then discard APP0 and APP14,
  1.1281 +   * but simply discard COM and all other APPn.
  1.1282 +   */
  1.1283 +  marker->process_COM = skip_variable;
  1.1284 +  marker->length_limit_COM = 0;
  1.1285 +  for (i = 0; i < 16; i++) {
  1.1286 +    marker->process_APPn[i] = skip_variable;
  1.1287 +    marker->length_limit_APPn[i] = 0;
  1.1288 +  }
  1.1289 +  marker->process_APPn[0] = get_interesting_appn;
  1.1290 +  marker->process_APPn[14] = get_interesting_appn;
  1.1291 +  /* Reset marker processing state */
  1.1292 +  reset_marker_reader(cinfo);
  1.1293 +}
  1.1294 +
  1.1295 +
  1.1296 +/*
  1.1297 + * Control saving of COM and APPn markers into marker_list.
  1.1298 + */
  1.1299 +
  1.1300 +#ifdef SAVE_MARKERS_SUPPORTED
  1.1301 +
  1.1302 +GLOBAL(void)
  1.1303 +jpeg_save_markers (j_decompress_ptr cinfo, int marker_code,
  1.1304 +		   unsigned int length_limit)
  1.1305 +{
  1.1306 +  my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
  1.1307 +  long maxlength;
  1.1308 +  jpeg_marker_parser_method processor;
  1.1309 +
  1.1310 +  /* Length limit mustn't be larger than what we can allocate
  1.1311 +   * (should only be a concern in a 16-bit environment).
  1.1312 +   */
  1.1313 +  maxlength = cinfo->mem->max_alloc_chunk - SIZEOF(struct jpeg_marker_struct);
  1.1314 +  if (((long) length_limit) > maxlength)
  1.1315 +    length_limit = (unsigned int) maxlength;
  1.1316 +
  1.1317 +  /* Choose processor routine to use.
  1.1318 +   * APP0/APP14 have special requirements.
  1.1319 +   */
  1.1320 +  if (length_limit) {
  1.1321 +    processor = save_marker;
  1.1322 +    /* If saving APP0/APP14, save at least enough for our internal use. */
  1.1323 +    if (marker_code == (int) M_APP0 && length_limit < APP0_DATA_LEN)
  1.1324 +      length_limit = APP0_DATA_LEN;
  1.1325 +    else if (marker_code == (int) M_APP14 && length_limit < APP14_DATA_LEN)
  1.1326 +      length_limit = APP14_DATA_LEN;
  1.1327 +  } else {
  1.1328 +    processor = skip_variable;
  1.1329 +    /* If discarding APP0/APP14, use our regular on-the-fly processor. */
  1.1330 +    if (marker_code == (int) M_APP0 || marker_code == (int) M_APP14)
  1.1331 +      processor = get_interesting_appn;
  1.1332 +  }
  1.1333 +
  1.1334 +  if (marker_code == (int) M_COM) {
  1.1335 +    marker->process_COM = processor;
  1.1336 +    marker->length_limit_COM = length_limit;
  1.1337 +  } else if (marker_code >= (int) M_APP0 && marker_code <= (int) M_APP15) {
  1.1338 +    marker->process_APPn[marker_code - (int) M_APP0] = processor;
  1.1339 +    marker->length_limit_APPn[marker_code - (int) M_APP0] = length_limit;
  1.1340 +  } else
  1.1341 +    ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, marker_code);
  1.1342 +}
  1.1343 +
  1.1344 +#endif /* SAVE_MARKERS_SUPPORTED */
  1.1345 +
  1.1346 +
  1.1347 +/*
  1.1348 + * Install a special processing method for COM or APPn markers.
  1.1349 + */
  1.1350 +
  1.1351 +GLOBAL(void)
  1.1352 +jpeg_set_marker_processor (j_decompress_ptr cinfo, int marker_code,
  1.1353 +			   jpeg_marker_parser_method routine)
  1.1354 +{
  1.1355 +  my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
  1.1356 +
  1.1357 +  if (marker_code == (int) M_COM)
  1.1358 +    marker->process_COM = routine;
  1.1359 +  else if (marker_code >= (int) M_APP0 && marker_code <= (int) M_APP15)
  1.1360 +    marker->process_APPn[marker_code - (int) M_APP0] = routine;
  1.1361 +  else
  1.1362 +    ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, marker_code);
  1.1363 +}