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