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