media/libjpeg/jdmarker.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /*
michael@0 2 * jdmarker.c
michael@0 3 *
michael@0 4 * This file was part of the Independent JPEG Group's software:
michael@0 5 * Copyright (C) 1991-1998, Thomas G. Lane.
michael@0 6 * libjpeg-turbo Modifications:
michael@0 7 * Copyright (C) 2012, D. R. Commander.
michael@0 8 * For conditions of distribution and use, see the accompanying README file.
michael@0 9 *
michael@0 10 * This file contains routines to decode JPEG datastream markers.
michael@0 11 * Most of the complexity arises from our desire to support input
michael@0 12 * suspension: if not all of the data for a marker is available,
michael@0 13 * we must exit back to the application. On resumption, we reprocess
michael@0 14 * the marker.
michael@0 15 */
michael@0 16
michael@0 17 #define JPEG_INTERNALS
michael@0 18 #include "jinclude.h"
michael@0 19 #include "jpeglib.h"
michael@0 20
michael@0 21
michael@0 22 typedef enum { /* JPEG marker codes */
michael@0 23 M_SOF0 = 0xc0,
michael@0 24 M_SOF1 = 0xc1,
michael@0 25 M_SOF2 = 0xc2,
michael@0 26 M_SOF3 = 0xc3,
michael@0 27
michael@0 28 M_SOF5 = 0xc5,
michael@0 29 M_SOF6 = 0xc6,
michael@0 30 M_SOF7 = 0xc7,
michael@0 31
michael@0 32 M_JPG = 0xc8,
michael@0 33 M_SOF9 = 0xc9,
michael@0 34 M_SOF10 = 0xca,
michael@0 35 M_SOF11 = 0xcb,
michael@0 36
michael@0 37 M_SOF13 = 0xcd,
michael@0 38 M_SOF14 = 0xce,
michael@0 39 M_SOF15 = 0xcf,
michael@0 40
michael@0 41 M_DHT = 0xc4,
michael@0 42
michael@0 43 M_DAC = 0xcc,
michael@0 44
michael@0 45 M_RST0 = 0xd0,
michael@0 46 M_RST1 = 0xd1,
michael@0 47 M_RST2 = 0xd2,
michael@0 48 M_RST3 = 0xd3,
michael@0 49 M_RST4 = 0xd4,
michael@0 50 M_RST5 = 0xd5,
michael@0 51 M_RST6 = 0xd6,
michael@0 52 M_RST7 = 0xd7,
michael@0 53
michael@0 54 M_SOI = 0xd8,
michael@0 55 M_EOI = 0xd9,
michael@0 56 M_SOS = 0xda,
michael@0 57 M_DQT = 0xdb,
michael@0 58 M_DNL = 0xdc,
michael@0 59 M_DRI = 0xdd,
michael@0 60 M_DHP = 0xde,
michael@0 61 M_EXP = 0xdf,
michael@0 62
michael@0 63 M_APP0 = 0xe0,
michael@0 64 M_APP1 = 0xe1,
michael@0 65 M_APP2 = 0xe2,
michael@0 66 M_APP3 = 0xe3,
michael@0 67 M_APP4 = 0xe4,
michael@0 68 M_APP5 = 0xe5,
michael@0 69 M_APP6 = 0xe6,
michael@0 70 M_APP7 = 0xe7,
michael@0 71 M_APP8 = 0xe8,
michael@0 72 M_APP9 = 0xe9,
michael@0 73 M_APP10 = 0xea,
michael@0 74 M_APP11 = 0xeb,
michael@0 75 M_APP12 = 0xec,
michael@0 76 M_APP13 = 0xed,
michael@0 77 M_APP14 = 0xee,
michael@0 78 M_APP15 = 0xef,
michael@0 79
michael@0 80 M_JPG0 = 0xf0,
michael@0 81 M_JPG13 = 0xfd,
michael@0 82 M_COM = 0xfe,
michael@0 83
michael@0 84 M_TEM = 0x01,
michael@0 85
michael@0 86 M_ERROR = 0x100
michael@0 87 } JPEG_MARKER;
michael@0 88
michael@0 89
michael@0 90 /* Private state */
michael@0 91
michael@0 92 typedef struct {
michael@0 93 struct jpeg_marker_reader pub; /* public fields */
michael@0 94
michael@0 95 /* Application-overridable marker processing methods */
michael@0 96 jpeg_marker_parser_method process_COM;
michael@0 97 jpeg_marker_parser_method process_APPn[16];
michael@0 98
michael@0 99 /* Limit on marker data length to save for each marker type */
michael@0 100 unsigned int length_limit_COM;
michael@0 101 unsigned int length_limit_APPn[16];
michael@0 102
michael@0 103 /* Status of COM/APPn marker saving */
michael@0 104 jpeg_saved_marker_ptr cur_marker; /* NULL if not processing a marker */
michael@0 105 unsigned int bytes_read; /* data bytes read so far in marker */
michael@0 106 /* Note: cur_marker is not linked into marker_list until it's all read. */
michael@0 107 } my_marker_reader;
michael@0 108
michael@0 109 typedef my_marker_reader * my_marker_ptr;
michael@0 110
michael@0 111
michael@0 112 /*
michael@0 113 * Macros for fetching data from the data source module.
michael@0 114 *
michael@0 115 * At all times, cinfo->src->next_input_byte and ->bytes_in_buffer reflect
michael@0 116 * the current restart point; we update them only when we have reached a
michael@0 117 * suitable place to restart if a suspension occurs.
michael@0 118 */
michael@0 119
michael@0 120 /* Declare and initialize local copies of input pointer/count */
michael@0 121 #define INPUT_VARS(cinfo) \
michael@0 122 struct jpeg_source_mgr * datasrc = (cinfo)->src; \
michael@0 123 const JOCTET * next_input_byte = datasrc->next_input_byte; \
michael@0 124 size_t bytes_in_buffer = datasrc->bytes_in_buffer
michael@0 125
michael@0 126 /* Unload the local copies --- do this only at a restart boundary */
michael@0 127 #define INPUT_SYNC(cinfo) \
michael@0 128 ( datasrc->next_input_byte = next_input_byte, \
michael@0 129 datasrc->bytes_in_buffer = bytes_in_buffer )
michael@0 130
michael@0 131 /* Reload the local copies --- used only in MAKE_BYTE_AVAIL */
michael@0 132 #define INPUT_RELOAD(cinfo) \
michael@0 133 ( next_input_byte = datasrc->next_input_byte, \
michael@0 134 bytes_in_buffer = datasrc->bytes_in_buffer )
michael@0 135
michael@0 136 /* Internal macro for INPUT_BYTE and INPUT_2BYTES: make a byte available.
michael@0 137 * Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
michael@0 138 * but we must reload the local copies after a successful fill.
michael@0 139 */
michael@0 140 #define MAKE_BYTE_AVAIL(cinfo,action) \
michael@0 141 if (bytes_in_buffer == 0) { \
michael@0 142 if (! (*datasrc->fill_input_buffer) (cinfo)) \
michael@0 143 { action; } \
michael@0 144 INPUT_RELOAD(cinfo); \
michael@0 145 }
michael@0 146
michael@0 147 /* Read a byte into variable V.
michael@0 148 * If must suspend, take the specified action (typically "return FALSE").
michael@0 149 */
michael@0 150 #define INPUT_BYTE(cinfo,V,action) \
michael@0 151 MAKESTMT( MAKE_BYTE_AVAIL(cinfo,action); \
michael@0 152 bytes_in_buffer--; \
michael@0 153 V = GETJOCTET(*next_input_byte++); )
michael@0 154
michael@0 155 /* As above, but read two bytes interpreted as an unsigned 16-bit integer.
michael@0 156 * V should be declared unsigned int or perhaps INT32.
michael@0 157 */
michael@0 158 #define INPUT_2BYTES(cinfo,V,action) \
michael@0 159 MAKESTMT( MAKE_BYTE_AVAIL(cinfo,action); \
michael@0 160 bytes_in_buffer--; \
michael@0 161 V = ((unsigned int) GETJOCTET(*next_input_byte++)) << 8; \
michael@0 162 MAKE_BYTE_AVAIL(cinfo,action); \
michael@0 163 bytes_in_buffer--; \
michael@0 164 V += GETJOCTET(*next_input_byte++); )
michael@0 165
michael@0 166
michael@0 167 /*
michael@0 168 * Routines to process JPEG markers.
michael@0 169 *
michael@0 170 * Entry condition: JPEG marker itself has been read and its code saved
michael@0 171 * in cinfo->unread_marker; input restart point is just after the marker.
michael@0 172 *
michael@0 173 * Exit: if return TRUE, have read and processed any parameters, and have
michael@0 174 * updated the restart point to point after the parameters.
michael@0 175 * If return FALSE, was forced to suspend before reaching end of
michael@0 176 * marker parameters; restart point has not been moved. Same routine
michael@0 177 * will be called again after application supplies more input data.
michael@0 178 *
michael@0 179 * This approach to suspension assumes that all of a marker's parameters
michael@0 180 * can fit into a single input bufferload. This should hold for "normal"
michael@0 181 * markers. Some COM/APPn markers might have large parameter segments
michael@0 182 * that might not fit. If we are simply dropping such a marker, we use
michael@0 183 * skip_input_data to get past it, and thereby put the problem on the
michael@0 184 * source manager's shoulders. If we are saving the marker's contents
michael@0 185 * into memory, we use a slightly different convention: when forced to
michael@0 186 * suspend, the marker processor updates the restart point to the end of
michael@0 187 * what it's consumed (ie, the end of the buffer) before returning FALSE.
michael@0 188 * On resumption, cinfo->unread_marker still contains the marker code,
michael@0 189 * but the data source will point to the next chunk of marker data.
michael@0 190 * The marker processor must retain internal state to deal with this.
michael@0 191 *
michael@0 192 * Note that we don't bother to avoid duplicate trace messages if a
michael@0 193 * suspension occurs within marker parameters. Other side effects
michael@0 194 * require more care.
michael@0 195 */
michael@0 196
michael@0 197
michael@0 198 LOCAL(boolean)
michael@0 199 get_soi (j_decompress_ptr cinfo)
michael@0 200 /* Process an SOI marker */
michael@0 201 {
michael@0 202 int i;
michael@0 203
michael@0 204 TRACEMS(cinfo, 1, JTRC_SOI);
michael@0 205
michael@0 206 if (cinfo->marker->saw_SOI)
michael@0 207 ERREXIT(cinfo, JERR_SOI_DUPLICATE);
michael@0 208
michael@0 209 /* Reset all parameters that are defined to be reset by SOI */
michael@0 210
michael@0 211 for (i = 0; i < NUM_ARITH_TBLS; i++) {
michael@0 212 cinfo->arith_dc_L[i] = 0;
michael@0 213 cinfo->arith_dc_U[i] = 1;
michael@0 214 cinfo->arith_ac_K[i] = 5;
michael@0 215 }
michael@0 216 cinfo->restart_interval = 0;
michael@0 217
michael@0 218 /* Set initial assumptions for colorspace etc */
michael@0 219
michael@0 220 cinfo->jpeg_color_space = JCS_UNKNOWN;
michael@0 221 cinfo->CCIR601_sampling = FALSE; /* Assume non-CCIR sampling??? */
michael@0 222
michael@0 223 cinfo->saw_JFIF_marker = FALSE;
michael@0 224 cinfo->JFIF_major_version = 1; /* set default JFIF APP0 values */
michael@0 225 cinfo->JFIF_minor_version = 1;
michael@0 226 cinfo->density_unit = 0;
michael@0 227 cinfo->X_density = 1;
michael@0 228 cinfo->Y_density = 1;
michael@0 229 cinfo->saw_Adobe_marker = FALSE;
michael@0 230 cinfo->Adobe_transform = 0;
michael@0 231
michael@0 232 cinfo->marker->saw_SOI = TRUE;
michael@0 233
michael@0 234 return TRUE;
michael@0 235 }
michael@0 236
michael@0 237
michael@0 238 LOCAL(boolean)
michael@0 239 get_sof (j_decompress_ptr cinfo, boolean is_prog, boolean is_arith)
michael@0 240 /* Process a SOFn marker */
michael@0 241 {
michael@0 242 INT32 length;
michael@0 243 int c, ci;
michael@0 244 jpeg_component_info * compptr;
michael@0 245 INPUT_VARS(cinfo);
michael@0 246
michael@0 247 cinfo->progressive_mode = is_prog;
michael@0 248 cinfo->arith_code = is_arith;
michael@0 249
michael@0 250 INPUT_2BYTES(cinfo, length, return FALSE);
michael@0 251
michael@0 252 INPUT_BYTE(cinfo, cinfo->data_precision, return FALSE);
michael@0 253 INPUT_2BYTES(cinfo, cinfo->image_height, return FALSE);
michael@0 254 INPUT_2BYTES(cinfo, cinfo->image_width, return FALSE);
michael@0 255 INPUT_BYTE(cinfo, cinfo->num_components, return FALSE);
michael@0 256
michael@0 257 length -= 8;
michael@0 258
michael@0 259 TRACEMS4(cinfo, 1, JTRC_SOF, cinfo->unread_marker,
michael@0 260 (int) cinfo->image_width, (int) cinfo->image_height,
michael@0 261 cinfo->num_components);
michael@0 262
michael@0 263 if (cinfo->marker->saw_SOF)
michael@0 264 ERREXIT(cinfo, JERR_SOF_DUPLICATE);
michael@0 265
michael@0 266 /* We don't support files in which the image height is initially specified */
michael@0 267 /* as 0 and is later redefined by DNL. As long as we have to check that, */
michael@0 268 /* might as well have a general sanity check. */
michael@0 269 if (cinfo->image_height <= 0 || cinfo->image_width <= 0
michael@0 270 || cinfo->num_components <= 0)
michael@0 271 ERREXIT(cinfo, JERR_EMPTY_IMAGE);
michael@0 272
michael@0 273 if (length != (cinfo->num_components * 3))
michael@0 274 ERREXIT(cinfo, JERR_BAD_LENGTH);
michael@0 275
michael@0 276 if (cinfo->comp_info == NULL) /* do only once, even if suspend */
michael@0 277 cinfo->comp_info = (jpeg_component_info *) (*cinfo->mem->alloc_small)
michael@0 278 ((j_common_ptr) cinfo, JPOOL_IMAGE,
michael@0 279 cinfo->num_components * SIZEOF(jpeg_component_info));
michael@0 280
michael@0 281 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
michael@0 282 ci++, compptr++) {
michael@0 283 compptr->component_index = ci;
michael@0 284 INPUT_BYTE(cinfo, compptr->component_id, return FALSE);
michael@0 285 INPUT_BYTE(cinfo, c, return FALSE);
michael@0 286 compptr->h_samp_factor = (c >> 4) & 15;
michael@0 287 compptr->v_samp_factor = (c ) & 15;
michael@0 288 INPUT_BYTE(cinfo, compptr->quant_tbl_no, return FALSE);
michael@0 289
michael@0 290 TRACEMS4(cinfo, 1, JTRC_SOF_COMPONENT,
michael@0 291 compptr->component_id, compptr->h_samp_factor,
michael@0 292 compptr->v_samp_factor, compptr->quant_tbl_no);
michael@0 293 }
michael@0 294
michael@0 295 cinfo->marker->saw_SOF = TRUE;
michael@0 296
michael@0 297 INPUT_SYNC(cinfo);
michael@0 298 return TRUE;
michael@0 299 }
michael@0 300
michael@0 301
michael@0 302 LOCAL(boolean)
michael@0 303 get_sos (j_decompress_ptr cinfo)
michael@0 304 /* Process a SOS marker */
michael@0 305 {
michael@0 306 INT32 length;
michael@0 307 int i, ci, n, c, cc, pi;
michael@0 308 jpeg_component_info * compptr;
michael@0 309 INPUT_VARS(cinfo);
michael@0 310
michael@0 311 if (! cinfo->marker->saw_SOF)
michael@0 312 ERREXIT(cinfo, JERR_SOS_NO_SOF);
michael@0 313
michael@0 314 INPUT_2BYTES(cinfo, length, return FALSE);
michael@0 315
michael@0 316 INPUT_BYTE(cinfo, n, return FALSE); /* Number of components */
michael@0 317
michael@0 318 TRACEMS1(cinfo, 1, JTRC_SOS, n);
michael@0 319
michael@0 320 if (length != (n * 2 + 6) || n < 1 || n > MAX_COMPS_IN_SCAN)
michael@0 321 ERREXIT(cinfo, JERR_BAD_LENGTH);
michael@0 322
michael@0 323 cinfo->comps_in_scan = n;
michael@0 324
michael@0 325 /* Collect the component-spec parameters */
michael@0 326
michael@0 327 for (i = 0; i < MAX_COMPS_IN_SCAN; i++)
michael@0 328 cinfo->cur_comp_info[i] = NULL;
michael@0 329
michael@0 330 for (i = 0; i < n; i++) {
michael@0 331 INPUT_BYTE(cinfo, cc, return FALSE);
michael@0 332 INPUT_BYTE(cinfo, c, return FALSE);
michael@0 333
michael@0 334 for (ci = 0, compptr = cinfo->comp_info;
michael@0 335 ci < cinfo->num_components && ci < MAX_COMPS_IN_SCAN;
michael@0 336 ci++, compptr++) {
michael@0 337 if (cc == compptr->component_id && !cinfo->cur_comp_info[ci])
michael@0 338 goto id_found;
michael@0 339 }
michael@0 340
michael@0 341 ERREXIT1(cinfo, JERR_BAD_COMPONENT_ID, cc);
michael@0 342
michael@0 343 id_found:
michael@0 344
michael@0 345 cinfo->cur_comp_info[i] = compptr;
michael@0 346 compptr->dc_tbl_no = (c >> 4) & 15;
michael@0 347 compptr->ac_tbl_no = (c ) & 15;
michael@0 348
michael@0 349 TRACEMS3(cinfo, 1, JTRC_SOS_COMPONENT, cc,
michael@0 350 compptr->dc_tbl_no, compptr->ac_tbl_no);
michael@0 351
michael@0 352 /* This CSi (cc) should differ from the previous CSi */
michael@0 353 for (pi = 0; pi < i; pi++) {
michael@0 354 if (cinfo->cur_comp_info[pi] == compptr) {
michael@0 355 ERREXIT1(cinfo, JERR_BAD_COMPONENT_ID, cc);
michael@0 356 }
michael@0 357 }
michael@0 358 }
michael@0 359
michael@0 360 /* Collect the additional scan parameters Ss, Se, Ah/Al. */
michael@0 361 INPUT_BYTE(cinfo, c, return FALSE);
michael@0 362 cinfo->Ss = c;
michael@0 363 INPUT_BYTE(cinfo, c, return FALSE);
michael@0 364 cinfo->Se = c;
michael@0 365 INPUT_BYTE(cinfo, c, return FALSE);
michael@0 366 cinfo->Ah = (c >> 4) & 15;
michael@0 367 cinfo->Al = (c ) & 15;
michael@0 368
michael@0 369 TRACEMS4(cinfo, 1, JTRC_SOS_PARAMS, cinfo->Ss, cinfo->Se,
michael@0 370 cinfo->Ah, cinfo->Al);
michael@0 371
michael@0 372 /* Prepare to scan data & restart markers */
michael@0 373 cinfo->marker->next_restart_num = 0;
michael@0 374
michael@0 375 /* Count another SOS marker */
michael@0 376 cinfo->input_scan_number++;
michael@0 377
michael@0 378 INPUT_SYNC(cinfo);
michael@0 379 return TRUE;
michael@0 380 }
michael@0 381
michael@0 382
michael@0 383 #ifdef D_ARITH_CODING_SUPPORTED
michael@0 384
michael@0 385 LOCAL(boolean)
michael@0 386 get_dac (j_decompress_ptr cinfo)
michael@0 387 /* Process a DAC marker */
michael@0 388 {
michael@0 389 INT32 length;
michael@0 390 int index, val;
michael@0 391 INPUT_VARS(cinfo);
michael@0 392
michael@0 393 INPUT_2BYTES(cinfo, length, return FALSE);
michael@0 394 length -= 2;
michael@0 395
michael@0 396 while (length > 0) {
michael@0 397 INPUT_BYTE(cinfo, index, return FALSE);
michael@0 398 INPUT_BYTE(cinfo, val, return FALSE);
michael@0 399
michael@0 400 length -= 2;
michael@0 401
michael@0 402 TRACEMS2(cinfo, 1, JTRC_DAC, index, val);
michael@0 403
michael@0 404 if (index < 0 || index >= (2*NUM_ARITH_TBLS))
michael@0 405 ERREXIT1(cinfo, JERR_DAC_INDEX, index);
michael@0 406
michael@0 407 if (index >= NUM_ARITH_TBLS) { /* define AC table */
michael@0 408 cinfo->arith_ac_K[index-NUM_ARITH_TBLS] = (UINT8) val;
michael@0 409 } else { /* define DC table */
michael@0 410 cinfo->arith_dc_L[index] = (UINT8) (val & 0x0F);
michael@0 411 cinfo->arith_dc_U[index] = (UINT8) (val >> 4);
michael@0 412 if (cinfo->arith_dc_L[index] > cinfo->arith_dc_U[index])
michael@0 413 ERREXIT1(cinfo, JERR_DAC_VALUE, val);
michael@0 414 }
michael@0 415 }
michael@0 416
michael@0 417 if (length != 0)
michael@0 418 ERREXIT(cinfo, JERR_BAD_LENGTH);
michael@0 419
michael@0 420 INPUT_SYNC(cinfo);
michael@0 421 return TRUE;
michael@0 422 }
michael@0 423
michael@0 424 #else /* ! D_ARITH_CODING_SUPPORTED */
michael@0 425
michael@0 426 #define get_dac(cinfo) skip_variable(cinfo)
michael@0 427
michael@0 428 #endif /* D_ARITH_CODING_SUPPORTED */
michael@0 429
michael@0 430
michael@0 431 LOCAL(boolean)
michael@0 432 get_dht (j_decompress_ptr cinfo)
michael@0 433 /* Process a DHT marker */
michael@0 434 {
michael@0 435 INT32 length;
michael@0 436 UINT8 bits[17];
michael@0 437 UINT8 huffval[256];
michael@0 438 int i, index, count;
michael@0 439 JHUFF_TBL **htblptr;
michael@0 440 INPUT_VARS(cinfo);
michael@0 441
michael@0 442 INPUT_2BYTES(cinfo, length, return FALSE);
michael@0 443 length -= 2;
michael@0 444
michael@0 445 while (length > 16) {
michael@0 446 INPUT_BYTE(cinfo, index, return FALSE);
michael@0 447
michael@0 448 TRACEMS1(cinfo, 1, JTRC_DHT, index);
michael@0 449
michael@0 450 bits[0] = 0;
michael@0 451 count = 0;
michael@0 452 for (i = 1; i <= 16; i++) {
michael@0 453 INPUT_BYTE(cinfo, bits[i], return FALSE);
michael@0 454 count += bits[i];
michael@0 455 }
michael@0 456
michael@0 457 length -= 1 + 16;
michael@0 458
michael@0 459 TRACEMS8(cinfo, 2, JTRC_HUFFBITS,
michael@0 460 bits[1], bits[2], bits[3], bits[4],
michael@0 461 bits[5], bits[6], bits[7], bits[8]);
michael@0 462 TRACEMS8(cinfo, 2, JTRC_HUFFBITS,
michael@0 463 bits[9], bits[10], bits[11], bits[12],
michael@0 464 bits[13], bits[14], bits[15], bits[16]);
michael@0 465
michael@0 466 /* Here we just do minimal validation of the counts to avoid walking
michael@0 467 * off the end of our table space. jdhuff.c will check more carefully.
michael@0 468 */
michael@0 469 if (count > 256 || ((INT32) count) > length)
michael@0 470 ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
michael@0 471
michael@0 472 for (i = 0; i < count; i++)
michael@0 473 INPUT_BYTE(cinfo, huffval[i], return FALSE);
michael@0 474
michael@0 475 MEMZERO(&huffval[count], (256 - count) * SIZEOF(UINT8));
michael@0 476
michael@0 477 length -= count;
michael@0 478
michael@0 479 if (index & 0x10) { /* AC table definition */
michael@0 480 index -= 0x10;
michael@0 481 if (index < 0 || index >= NUM_HUFF_TBLS)
michael@0 482 ERREXIT1(cinfo, JERR_DHT_INDEX, index);
michael@0 483 htblptr = &cinfo->ac_huff_tbl_ptrs[index];
michael@0 484 } else { /* DC table definition */
michael@0 485 if (index < 0 || index >= NUM_HUFF_TBLS)
michael@0 486 ERREXIT1(cinfo, JERR_DHT_INDEX, index);
michael@0 487 htblptr = &cinfo->dc_huff_tbl_ptrs[index];
michael@0 488 }
michael@0 489
michael@0 490 if (*htblptr == NULL)
michael@0 491 *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
michael@0 492
michael@0 493 MEMCOPY((*htblptr)->bits, bits, SIZEOF((*htblptr)->bits));
michael@0 494 MEMCOPY((*htblptr)->huffval, huffval, SIZEOF((*htblptr)->huffval));
michael@0 495 }
michael@0 496
michael@0 497 if (length != 0)
michael@0 498 ERREXIT(cinfo, JERR_BAD_LENGTH);
michael@0 499
michael@0 500 INPUT_SYNC(cinfo);
michael@0 501 return TRUE;
michael@0 502 }
michael@0 503
michael@0 504
michael@0 505 LOCAL(boolean)
michael@0 506 get_dqt (j_decompress_ptr cinfo)
michael@0 507 /* Process a DQT marker */
michael@0 508 {
michael@0 509 INT32 length;
michael@0 510 int n, i, prec;
michael@0 511 unsigned int tmp;
michael@0 512 JQUANT_TBL *quant_ptr;
michael@0 513 INPUT_VARS(cinfo);
michael@0 514
michael@0 515 INPUT_2BYTES(cinfo, length, return FALSE);
michael@0 516 length -= 2;
michael@0 517
michael@0 518 while (length > 0) {
michael@0 519 INPUT_BYTE(cinfo, n, return FALSE);
michael@0 520 prec = n >> 4;
michael@0 521 n &= 0x0F;
michael@0 522
michael@0 523 TRACEMS2(cinfo, 1, JTRC_DQT, n, prec);
michael@0 524
michael@0 525 if (n >= NUM_QUANT_TBLS)
michael@0 526 ERREXIT1(cinfo, JERR_DQT_INDEX, n);
michael@0 527
michael@0 528 if (cinfo->quant_tbl_ptrs[n] == NULL)
michael@0 529 cinfo->quant_tbl_ptrs[n] = jpeg_alloc_quant_table((j_common_ptr) cinfo);
michael@0 530 quant_ptr = cinfo->quant_tbl_ptrs[n];
michael@0 531
michael@0 532 for (i = 0; i < DCTSIZE2; i++) {
michael@0 533 if (prec)
michael@0 534 INPUT_2BYTES(cinfo, tmp, return FALSE);
michael@0 535 else
michael@0 536 INPUT_BYTE(cinfo, tmp, return FALSE);
michael@0 537 /* We convert the zigzag-order table to natural array order. */
michael@0 538 quant_ptr->quantval[jpeg_natural_order[i]] = (UINT16) tmp;
michael@0 539 }
michael@0 540
michael@0 541 if (cinfo->err->trace_level >= 2) {
michael@0 542 for (i = 0; i < DCTSIZE2; i += 8) {
michael@0 543 TRACEMS8(cinfo, 2, JTRC_QUANTVALS,
michael@0 544 quant_ptr->quantval[i], quant_ptr->quantval[i+1],
michael@0 545 quant_ptr->quantval[i+2], quant_ptr->quantval[i+3],
michael@0 546 quant_ptr->quantval[i+4], quant_ptr->quantval[i+5],
michael@0 547 quant_ptr->quantval[i+6], quant_ptr->quantval[i+7]);
michael@0 548 }
michael@0 549 }
michael@0 550
michael@0 551 length -= DCTSIZE2+1;
michael@0 552 if (prec) length -= DCTSIZE2;
michael@0 553 }
michael@0 554
michael@0 555 if (length != 0)
michael@0 556 ERREXIT(cinfo, JERR_BAD_LENGTH);
michael@0 557
michael@0 558 INPUT_SYNC(cinfo);
michael@0 559 return TRUE;
michael@0 560 }
michael@0 561
michael@0 562
michael@0 563 LOCAL(boolean)
michael@0 564 get_dri (j_decompress_ptr cinfo)
michael@0 565 /* Process a DRI marker */
michael@0 566 {
michael@0 567 INT32 length;
michael@0 568 unsigned int tmp;
michael@0 569 INPUT_VARS(cinfo);
michael@0 570
michael@0 571 INPUT_2BYTES(cinfo, length, return FALSE);
michael@0 572
michael@0 573 if (length != 4)
michael@0 574 ERREXIT(cinfo, JERR_BAD_LENGTH);
michael@0 575
michael@0 576 INPUT_2BYTES(cinfo, tmp, return FALSE);
michael@0 577
michael@0 578 TRACEMS1(cinfo, 1, JTRC_DRI, tmp);
michael@0 579
michael@0 580 cinfo->restart_interval = tmp;
michael@0 581
michael@0 582 INPUT_SYNC(cinfo);
michael@0 583 return TRUE;
michael@0 584 }
michael@0 585
michael@0 586
michael@0 587 /*
michael@0 588 * Routines for processing APPn and COM markers.
michael@0 589 * These are either saved in memory or discarded, per application request.
michael@0 590 * APP0 and APP14 are specially checked to see if they are
michael@0 591 * JFIF and Adobe markers, respectively.
michael@0 592 */
michael@0 593
michael@0 594 #define APP0_DATA_LEN 14 /* Length of interesting data in APP0 */
michael@0 595 #define APP14_DATA_LEN 12 /* Length of interesting data in APP14 */
michael@0 596 #define APPN_DATA_LEN 14 /* Must be the largest of the above!! */
michael@0 597
michael@0 598
michael@0 599 LOCAL(void)
michael@0 600 examine_app0 (j_decompress_ptr cinfo, JOCTET FAR * data,
michael@0 601 unsigned int datalen, INT32 remaining)
michael@0 602 /* Examine first few bytes from an APP0.
michael@0 603 * Take appropriate action if it is a JFIF marker.
michael@0 604 * datalen is # of bytes at data[], remaining is length of rest of marker data.
michael@0 605 */
michael@0 606 {
michael@0 607 INT32 totallen = (INT32) datalen + remaining;
michael@0 608
michael@0 609 if (datalen >= APP0_DATA_LEN &&
michael@0 610 GETJOCTET(data[0]) == 0x4A &&
michael@0 611 GETJOCTET(data[1]) == 0x46 &&
michael@0 612 GETJOCTET(data[2]) == 0x49 &&
michael@0 613 GETJOCTET(data[3]) == 0x46 &&
michael@0 614 GETJOCTET(data[4]) == 0) {
michael@0 615 /* Found JFIF APP0 marker: save info */
michael@0 616 cinfo->saw_JFIF_marker = TRUE;
michael@0 617 cinfo->JFIF_major_version = GETJOCTET(data[5]);
michael@0 618 cinfo->JFIF_minor_version = GETJOCTET(data[6]);
michael@0 619 cinfo->density_unit = GETJOCTET(data[7]);
michael@0 620 cinfo->X_density = (GETJOCTET(data[8]) << 8) + GETJOCTET(data[9]);
michael@0 621 cinfo->Y_density = (GETJOCTET(data[10]) << 8) + GETJOCTET(data[11]);
michael@0 622 /* Check version.
michael@0 623 * Major version must be 1, anything else signals an incompatible change.
michael@0 624 * (We used to treat this as an error, but now it's a nonfatal warning,
michael@0 625 * because some bozo at Hijaak couldn't read the spec.)
michael@0 626 * Minor version should be 0..2, but process anyway if newer.
michael@0 627 */
michael@0 628 if (cinfo->JFIF_major_version != 1)
michael@0 629 WARNMS2(cinfo, JWRN_JFIF_MAJOR,
michael@0 630 cinfo->JFIF_major_version, cinfo->JFIF_minor_version);
michael@0 631 /* Generate trace messages */
michael@0 632 TRACEMS5(cinfo, 1, JTRC_JFIF,
michael@0 633 cinfo->JFIF_major_version, cinfo->JFIF_minor_version,
michael@0 634 cinfo->X_density, cinfo->Y_density, cinfo->density_unit);
michael@0 635 /* Validate thumbnail dimensions and issue appropriate messages */
michael@0 636 if (GETJOCTET(data[12]) | GETJOCTET(data[13]))
michael@0 637 TRACEMS2(cinfo, 1, JTRC_JFIF_THUMBNAIL,
michael@0 638 GETJOCTET(data[12]), GETJOCTET(data[13]));
michael@0 639 totallen -= APP0_DATA_LEN;
michael@0 640 if (totallen !=
michael@0 641 ((INT32)GETJOCTET(data[12]) * (INT32)GETJOCTET(data[13]) * (INT32) 3))
michael@0 642 TRACEMS1(cinfo, 1, JTRC_JFIF_BADTHUMBNAILSIZE, (int) totallen);
michael@0 643 } else if (datalen >= 6 &&
michael@0 644 GETJOCTET(data[0]) == 0x4A &&
michael@0 645 GETJOCTET(data[1]) == 0x46 &&
michael@0 646 GETJOCTET(data[2]) == 0x58 &&
michael@0 647 GETJOCTET(data[3]) == 0x58 &&
michael@0 648 GETJOCTET(data[4]) == 0) {
michael@0 649 /* Found JFIF "JFXX" extension APP0 marker */
michael@0 650 /* The library doesn't actually do anything with these,
michael@0 651 * but we try to produce a helpful trace message.
michael@0 652 */
michael@0 653 switch (GETJOCTET(data[5])) {
michael@0 654 case 0x10:
michael@0 655 TRACEMS1(cinfo, 1, JTRC_THUMB_JPEG, (int) totallen);
michael@0 656 break;
michael@0 657 case 0x11:
michael@0 658 TRACEMS1(cinfo, 1, JTRC_THUMB_PALETTE, (int) totallen);
michael@0 659 break;
michael@0 660 case 0x13:
michael@0 661 TRACEMS1(cinfo, 1, JTRC_THUMB_RGB, (int) totallen);
michael@0 662 break;
michael@0 663 default:
michael@0 664 TRACEMS2(cinfo, 1, JTRC_JFIF_EXTENSION,
michael@0 665 GETJOCTET(data[5]), (int) totallen);
michael@0 666 break;
michael@0 667 }
michael@0 668 } else {
michael@0 669 /* Start of APP0 does not match "JFIF" or "JFXX", or too short */
michael@0 670 TRACEMS1(cinfo, 1, JTRC_APP0, (int) totallen);
michael@0 671 }
michael@0 672 }
michael@0 673
michael@0 674
michael@0 675 LOCAL(void)
michael@0 676 examine_app14 (j_decompress_ptr cinfo, JOCTET FAR * data,
michael@0 677 unsigned int datalen, INT32 remaining)
michael@0 678 /* Examine first few bytes from an APP14.
michael@0 679 * Take appropriate action if it is an Adobe marker.
michael@0 680 * datalen is # of bytes at data[], remaining is length of rest of marker data.
michael@0 681 */
michael@0 682 {
michael@0 683 unsigned int version, flags0, flags1, transform;
michael@0 684
michael@0 685 if (datalen >= APP14_DATA_LEN &&
michael@0 686 GETJOCTET(data[0]) == 0x41 &&
michael@0 687 GETJOCTET(data[1]) == 0x64 &&
michael@0 688 GETJOCTET(data[2]) == 0x6F &&
michael@0 689 GETJOCTET(data[3]) == 0x62 &&
michael@0 690 GETJOCTET(data[4]) == 0x65) {
michael@0 691 /* Found Adobe APP14 marker */
michael@0 692 version = (GETJOCTET(data[5]) << 8) + GETJOCTET(data[6]);
michael@0 693 flags0 = (GETJOCTET(data[7]) << 8) + GETJOCTET(data[8]);
michael@0 694 flags1 = (GETJOCTET(data[9]) << 8) + GETJOCTET(data[10]);
michael@0 695 transform = GETJOCTET(data[11]);
michael@0 696 TRACEMS4(cinfo, 1, JTRC_ADOBE, version, flags0, flags1, transform);
michael@0 697 cinfo->saw_Adobe_marker = TRUE;
michael@0 698 cinfo->Adobe_transform = (UINT8) transform;
michael@0 699 } else {
michael@0 700 /* Start of APP14 does not match "Adobe", or too short */
michael@0 701 TRACEMS1(cinfo, 1, JTRC_APP14, (int) (datalen + remaining));
michael@0 702 }
michael@0 703 }
michael@0 704
michael@0 705
michael@0 706 METHODDEF(boolean)
michael@0 707 get_interesting_appn (j_decompress_ptr cinfo)
michael@0 708 /* Process an APP0 or APP14 marker without saving it */
michael@0 709 {
michael@0 710 INT32 length;
michael@0 711 JOCTET b[APPN_DATA_LEN];
michael@0 712 unsigned int i, numtoread;
michael@0 713 INPUT_VARS(cinfo);
michael@0 714
michael@0 715 INPUT_2BYTES(cinfo, length, return FALSE);
michael@0 716 length -= 2;
michael@0 717
michael@0 718 /* get the interesting part of the marker data */
michael@0 719 if (length >= APPN_DATA_LEN)
michael@0 720 numtoread = APPN_DATA_LEN;
michael@0 721 else if (length > 0)
michael@0 722 numtoread = (unsigned int) length;
michael@0 723 else
michael@0 724 numtoread = 0;
michael@0 725 for (i = 0; i < numtoread; i++)
michael@0 726 INPUT_BYTE(cinfo, b[i], return FALSE);
michael@0 727 length -= numtoread;
michael@0 728
michael@0 729 /* process it */
michael@0 730 switch (cinfo->unread_marker) {
michael@0 731 case M_APP0:
michael@0 732 examine_app0(cinfo, (JOCTET FAR *) b, numtoread, length);
michael@0 733 break;
michael@0 734 case M_APP14:
michael@0 735 examine_app14(cinfo, (JOCTET FAR *) b, numtoread, length);
michael@0 736 break;
michael@0 737 default:
michael@0 738 /* can't get here unless jpeg_save_markers chooses wrong processor */
michael@0 739 ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, cinfo->unread_marker);
michael@0 740 break;
michael@0 741 }
michael@0 742
michael@0 743 /* skip any remaining data -- could be lots */
michael@0 744 INPUT_SYNC(cinfo);
michael@0 745 if (length > 0)
michael@0 746 (*cinfo->src->skip_input_data) (cinfo, (long) length);
michael@0 747
michael@0 748 return TRUE;
michael@0 749 }
michael@0 750
michael@0 751
michael@0 752 #ifdef SAVE_MARKERS_SUPPORTED
michael@0 753
michael@0 754 METHODDEF(boolean)
michael@0 755 save_marker (j_decompress_ptr cinfo)
michael@0 756 /* Save an APPn or COM marker into the marker list */
michael@0 757 {
michael@0 758 my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
michael@0 759 jpeg_saved_marker_ptr cur_marker = marker->cur_marker;
michael@0 760 unsigned int bytes_read, data_length;
michael@0 761 JOCTET FAR * data;
michael@0 762 INT32 length = 0;
michael@0 763 INPUT_VARS(cinfo);
michael@0 764
michael@0 765 if (cur_marker == NULL) {
michael@0 766 /* begin reading a marker */
michael@0 767 INPUT_2BYTES(cinfo, length, return FALSE);
michael@0 768 length -= 2;
michael@0 769 if (length >= 0) { /* watch out for bogus length word */
michael@0 770 /* figure out how much we want to save */
michael@0 771 unsigned int limit;
michael@0 772 if (cinfo->unread_marker == (int) M_COM)
michael@0 773 limit = marker->length_limit_COM;
michael@0 774 else
michael@0 775 limit = marker->length_limit_APPn[cinfo->unread_marker - (int) M_APP0];
michael@0 776 if ((unsigned int) length < limit)
michael@0 777 limit = (unsigned int) length;
michael@0 778 /* allocate and initialize the marker item */
michael@0 779 cur_marker = (jpeg_saved_marker_ptr)
michael@0 780 (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
michael@0 781 SIZEOF(struct jpeg_marker_struct) + limit);
michael@0 782 cur_marker->next = NULL;
michael@0 783 cur_marker->marker = (UINT8) cinfo->unread_marker;
michael@0 784 cur_marker->original_length = (unsigned int) length;
michael@0 785 cur_marker->data_length = limit;
michael@0 786 /* data area is just beyond the jpeg_marker_struct */
michael@0 787 data = cur_marker->data = (JOCTET FAR *) (cur_marker + 1);
michael@0 788 marker->cur_marker = cur_marker;
michael@0 789 marker->bytes_read = 0;
michael@0 790 bytes_read = 0;
michael@0 791 data_length = limit;
michael@0 792 } else {
michael@0 793 /* deal with bogus length word */
michael@0 794 bytes_read = data_length = 0;
michael@0 795 data = NULL;
michael@0 796 }
michael@0 797 } else {
michael@0 798 /* resume reading a marker */
michael@0 799 bytes_read = marker->bytes_read;
michael@0 800 data_length = cur_marker->data_length;
michael@0 801 data = cur_marker->data + bytes_read;
michael@0 802 }
michael@0 803
michael@0 804 while (bytes_read < data_length) {
michael@0 805 INPUT_SYNC(cinfo); /* move the restart point to here */
michael@0 806 marker->bytes_read = bytes_read;
michael@0 807 /* If there's not at least one byte in buffer, suspend */
michael@0 808 MAKE_BYTE_AVAIL(cinfo, return FALSE);
michael@0 809 /* Copy bytes with reasonable rapidity */
michael@0 810 while (bytes_read < data_length && bytes_in_buffer > 0) {
michael@0 811 *data++ = *next_input_byte++;
michael@0 812 bytes_in_buffer--;
michael@0 813 bytes_read++;
michael@0 814 }
michael@0 815 }
michael@0 816
michael@0 817 /* Done reading what we want to read */
michael@0 818 if (cur_marker != NULL) { /* will be NULL if bogus length word */
michael@0 819 /* Add new marker to end of list */
michael@0 820 if (cinfo->marker_list == NULL) {
michael@0 821 cinfo->marker_list = cur_marker;
michael@0 822 } else {
michael@0 823 jpeg_saved_marker_ptr prev = cinfo->marker_list;
michael@0 824 while (prev->next != NULL)
michael@0 825 prev = prev->next;
michael@0 826 prev->next = cur_marker;
michael@0 827 }
michael@0 828 /* Reset pointer & calc remaining data length */
michael@0 829 data = cur_marker->data;
michael@0 830 length = cur_marker->original_length - data_length;
michael@0 831 }
michael@0 832 /* Reset to initial state for next marker */
michael@0 833 marker->cur_marker = NULL;
michael@0 834
michael@0 835 /* Process the marker if interesting; else just make a generic trace msg */
michael@0 836 switch (cinfo->unread_marker) {
michael@0 837 case M_APP0:
michael@0 838 examine_app0(cinfo, data, data_length, length);
michael@0 839 break;
michael@0 840 case M_APP14:
michael@0 841 examine_app14(cinfo, data, data_length, length);
michael@0 842 break;
michael@0 843 default:
michael@0 844 TRACEMS2(cinfo, 1, JTRC_MISC_MARKER, cinfo->unread_marker,
michael@0 845 (int) (data_length + length));
michael@0 846 break;
michael@0 847 }
michael@0 848
michael@0 849 /* skip any remaining data -- could be lots */
michael@0 850 INPUT_SYNC(cinfo); /* do before skip_input_data */
michael@0 851 if (length > 0)
michael@0 852 (*cinfo->src->skip_input_data) (cinfo, (long) length);
michael@0 853
michael@0 854 return TRUE;
michael@0 855 }
michael@0 856
michael@0 857 #endif /* SAVE_MARKERS_SUPPORTED */
michael@0 858
michael@0 859
michael@0 860 METHODDEF(boolean)
michael@0 861 skip_variable (j_decompress_ptr cinfo)
michael@0 862 /* Skip over an unknown or uninteresting variable-length marker */
michael@0 863 {
michael@0 864 INT32 length;
michael@0 865 INPUT_VARS(cinfo);
michael@0 866
michael@0 867 INPUT_2BYTES(cinfo, length, return FALSE);
michael@0 868 length -= 2;
michael@0 869
michael@0 870 TRACEMS2(cinfo, 1, JTRC_MISC_MARKER, cinfo->unread_marker, (int) length);
michael@0 871
michael@0 872 INPUT_SYNC(cinfo); /* do before skip_input_data */
michael@0 873 if (length > 0)
michael@0 874 (*cinfo->src->skip_input_data) (cinfo, (long) length);
michael@0 875
michael@0 876 return TRUE;
michael@0 877 }
michael@0 878
michael@0 879
michael@0 880 /*
michael@0 881 * Find the next JPEG marker, save it in cinfo->unread_marker.
michael@0 882 * Returns FALSE if had to suspend before reaching a marker;
michael@0 883 * in that case cinfo->unread_marker is unchanged.
michael@0 884 *
michael@0 885 * Note that the result might not be a valid marker code,
michael@0 886 * but it will never be 0 or FF.
michael@0 887 */
michael@0 888
michael@0 889 LOCAL(boolean)
michael@0 890 next_marker (j_decompress_ptr cinfo)
michael@0 891 {
michael@0 892 int c;
michael@0 893 INPUT_VARS(cinfo);
michael@0 894
michael@0 895 for (;;) {
michael@0 896 INPUT_BYTE(cinfo, c, return FALSE);
michael@0 897 /* Skip any non-FF bytes.
michael@0 898 * This may look a bit inefficient, but it will not occur in a valid file.
michael@0 899 * We sync after each discarded byte so that a suspending data source
michael@0 900 * can discard the byte from its buffer.
michael@0 901 */
michael@0 902 while (c != 0xFF) {
michael@0 903 cinfo->marker->discarded_bytes++;
michael@0 904 INPUT_SYNC(cinfo);
michael@0 905 INPUT_BYTE(cinfo, c, return FALSE);
michael@0 906 }
michael@0 907 /* This loop swallows any duplicate FF bytes. Extra FFs are legal as
michael@0 908 * pad bytes, so don't count them in discarded_bytes. We assume there
michael@0 909 * will not be so many consecutive FF bytes as to overflow a suspending
michael@0 910 * data source's input buffer.
michael@0 911 */
michael@0 912 do {
michael@0 913 INPUT_BYTE(cinfo, c, return FALSE);
michael@0 914 } while (c == 0xFF);
michael@0 915 if (c != 0)
michael@0 916 break; /* found a valid marker, exit loop */
michael@0 917 /* Reach here if we found a stuffed-zero data sequence (FF/00).
michael@0 918 * Discard it and loop back to try again.
michael@0 919 */
michael@0 920 cinfo->marker->discarded_bytes += 2;
michael@0 921 INPUT_SYNC(cinfo);
michael@0 922 }
michael@0 923
michael@0 924 if (cinfo->marker->discarded_bytes != 0) {
michael@0 925 WARNMS2(cinfo, JWRN_EXTRANEOUS_DATA, cinfo->marker->discarded_bytes, c);
michael@0 926 cinfo->marker->discarded_bytes = 0;
michael@0 927 }
michael@0 928
michael@0 929 cinfo->unread_marker = c;
michael@0 930
michael@0 931 INPUT_SYNC(cinfo);
michael@0 932 return TRUE;
michael@0 933 }
michael@0 934
michael@0 935
michael@0 936 LOCAL(boolean)
michael@0 937 first_marker (j_decompress_ptr cinfo)
michael@0 938 /* Like next_marker, but used to obtain the initial SOI marker. */
michael@0 939 /* For this marker, we do not allow preceding garbage or fill; otherwise,
michael@0 940 * we might well scan an entire input file before realizing it ain't JPEG.
michael@0 941 * If an application wants to process non-JFIF files, it must seek to the
michael@0 942 * SOI before calling the JPEG library.
michael@0 943 */
michael@0 944 {
michael@0 945 int c, c2;
michael@0 946 INPUT_VARS(cinfo);
michael@0 947
michael@0 948 INPUT_BYTE(cinfo, c, return FALSE);
michael@0 949 INPUT_BYTE(cinfo, c2, return FALSE);
michael@0 950 if (c != 0xFF || c2 != (int) M_SOI)
michael@0 951 ERREXIT2(cinfo, JERR_NO_SOI, c, c2);
michael@0 952
michael@0 953 cinfo->unread_marker = c2;
michael@0 954
michael@0 955 INPUT_SYNC(cinfo);
michael@0 956 return TRUE;
michael@0 957 }
michael@0 958
michael@0 959
michael@0 960 /*
michael@0 961 * Read markers until SOS or EOI.
michael@0 962 *
michael@0 963 * Returns same codes as are defined for jpeg_consume_input:
michael@0 964 * JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.
michael@0 965 */
michael@0 966
michael@0 967 METHODDEF(int)
michael@0 968 read_markers (j_decompress_ptr cinfo)
michael@0 969 {
michael@0 970 /* Outer loop repeats once for each marker. */
michael@0 971 for (;;) {
michael@0 972 /* Collect the marker proper, unless we already did. */
michael@0 973 /* NB: first_marker() enforces the requirement that SOI appear first. */
michael@0 974 if (cinfo->unread_marker == 0) {
michael@0 975 if (! cinfo->marker->saw_SOI) {
michael@0 976 if (! first_marker(cinfo))
michael@0 977 return JPEG_SUSPENDED;
michael@0 978 } else {
michael@0 979 if (! next_marker(cinfo))
michael@0 980 return JPEG_SUSPENDED;
michael@0 981 }
michael@0 982 }
michael@0 983 /* At this point cinfo->unread_marker contains the marker code and the
michael@0 984 * input point is just past the marker proper, but before any parameters.
michael@0 985 * A suspension will cause us to return with this state still true.
michael@0 986 */
michael@0 987 switch (cinfo->unread_marker) {
michael@0 988 case M_SOI:
michael@0 989 if (! get_soi(cinfo))
michael@0 990 return JPEG_SUSPENDED;
michael@0 991 break;
michael@0 992
michael@0 993 case M_SOF0: /* Baseline */
michael@0 994 case M_SOF1: /* Extended sequential, Huffman */
michael@0 995 if (! get_sof(cinfo, FALSE, FALSE))
michael@0 996 return JPEG_SUSPENDED;
michael@0 997 break;
michael@0 998
michael@0 999 case M_SOF2: /* Progressive, Huffman */
michael@0 1000 if (! get_sof(cinfo, TRUE, FALSE))
michael@0 1001 return JPEG_SUSPENDED;
michael@0 1002 break;
michael@0 1003
michael@0 1004 case M_SOF9: /* Extended sequential, arithmetic */
michael@0 1005 if (! get_sof(cinfo, FALSE, TRUE))
michael@0 1006 return JPEG_SUSPENDED;
michael@0 1007 break;
michael@0 1008
michael@0 1009 case M_SOF10: /* Progressive, arithmetic */
michael@0 1010 if (! get_sof(cinfo, TRUE, TRUE))
michael@0 1011 return JPEG_SUSPENDED;
michael@0 1012 break;
michael@0 1013
michael@0 1014 /* Currently unsupported SOFn types */
michael@0 1015 case M_SOF3: /* Lossless, Huffman */
michael@0 1016 case M_SOF5: /* Differential sequential, Huffman */
michael@0 1017 case M_SOF6: /* Differential progressive, Huffman */
michael@0 1018 case M_SOF7: /* Differential lossless, Huffman */
michael@0 1019 case M_JPG: /* Reserved for JPEG extensions */
michael@0 1020 case M_SOF11: /* Lossless, arithmetic */
michael@0 1021 case M_SOF13: /* Differential sequential, arithmetic */
michael@0 1022 case M_SOF14: /* Differential progressive, arithmetic */
michael@0 1023 case M_SOF15: /* Differential lossless, arithmetic */
michael@0 1024 ERREXIT1(cinfo, JERR_SOF_UNSUPPORTED, cinfo->unread_marker);
michael@0 1025 break;
michael@0 1026
michael@0 1027 case M_SOS:
michael@0 1028 if (! get_sos(cinfo))
michael@0 1029 return JPEG_SUSPENDED;
michael@0 1030 cinfo->unread_marker = 0; /* processed the marker */
michael@0 1031 return JPEG_REACHED_SOS;
michael@0 1032
michael@0 1033 case M_EOI:
michael@0 1034 TRACEMS(cinfo, 1, JTRC_EOI);
michael@0 1035 cinfo->unread_marker = 0; /* processed the marker */
michael@0 1036 return JPEG_REACHED_EOI;
michael@0 1037
michael@0 1038 case M_DAC:
michael@0 1039 if (! get_dac(cinfo))
michael@0 1040 return JPEG_SUSPENDED;
michael@0 1041 break;
michael@0 1042
michael@0 1043 case M_DHT:
michael@0 1044 if (! get_dht(cinfo))
michael@0 1045 return JPEG_SUSPENDED;
michael@0 1046 break;
michael@0 1047
michael@0 1048 case M_DQT:
michael@0 1049 if (! get_dqt(cinfo))
michael@0 1050 return JPEG_SUSPENDED;
michael@0 1051 break;
michael@0 1052
michael@0 1053 case M_DRI:
michael@0 1054 if (! get_dri(cinfo))
michael@0 1055 return JPEG_SUSPENDED;
michael@0 1056 break;
michael@0 1057
michael@0 1058 case M_APP0:
michael@0 1059 case M_APP1:
michael@0 1060 case M_APP2:
michael@0 1061 case M_APP3:
michael@0 1062 case M_APP4:
michael@0 1063 case M_APP5:
michael@0 1064 case M_APP6:
michael@0 1065 case M_APP7:
michael@0 1066 case M_APP8:
michael@0 1067 case M_APP9:
michael@0 1068 case M_APP10:
michael@0 1069 case M_APP11:
michael@0 1070 case M_APP12:
michael@0 1071 case M_APP13:
michael@0 1072 case M_APP14:
michael@0 1073 case M_APP15:
michael@0 1074 if (! (*((my_marker_ptr) cinfo->marker)->process_APPn[
michael@0 1075 cinfo->unread_marker - (int) M_APP0]) (cinfo))
michael@0 1076 return JPEG_SUSPENDED;
michael@0 1077 break;
michael@0 1078
michael@0 1079 case M_COM:
michael@0 1080 if (! (*((my_marker_ptr) cinfo->marker)->process_COM) (cinfo))
michael@0 1081 return JPEG_SUSPENDED;
michael@0 1082 break;
michael@0 1083
michael@0 1084 case M_RST0: /* these are all parameterless */
michael@0 1085 case M_RST1:
michael@0 1086 case M_RST2:
michael@0 1087 case M_RST3:
michael@0 1088 case M_RST4:
michael@0 1089 case M_RST5:
michael@0 1090 case M_RST6:
michael@0 1091 case M_RST7:
michael@0 1092 case M_TEM:
michael@0 1093 TRACEMS1(cinfo, 1, JTRC_PARMLESS_MARKER, cinfo->unread_marker);
michael@0 1094 break;
michael@0 1095
michael@0 1096 case M_DNL: /* Ignore DNL ... perhaps the wrong thing */
michael@0 1097 if (! skip_variable(cinfo))
michael@0 1098 return JPEG_SUSPENDED;
michael@0 1099 break;
michael@0 1100
michael@0 1101 default: /* must be DHP, EXP, JPGn, or RESn */
michael@0 1102 /* For now, we treat the reserved markers as fatal errors since they are
michael@0 1103 * likely to be used to signal incompatible JPEG Part 3 extensions.
michael@0 1104 * Once the JPEG 3 version-number marker is well defined, this code
michael@0 1105 * ought to change!
michael@0 1106 */
michael@0 1107 ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, cinfo->unread_marker);
michael@0 1108 break;
michael@0 1109 }
michael@0 1110 /* Successfully processed marker, so reset state variable */
michael@0 1111 cinfo->unread_marker = 0;
michael@0 1112 } /* end loop */
michael@0 1113 }
michael@0 1114
michael@0 1115
michael@0 1116 /*
michael@0 1117 * Read a restart marker, which is expected to appear next in the datastream;
michael@0 1118 * if the marker is not there, take appropriate recovery action.
michael@0 1119 * Returns FALSE if suspension is required.
michael@0 1120 *
michael@0 1121 * This is called by the entropy decoder after it has read an appropriate
michael@0 1122 * number of MCUs. cinfo->unread_marker may be nonzero if the entropy decoder
michael@0 1123 * has already read a marker from the data source. Under normal conditions
michael@0 1124 * cinfo->unread_marker will be reset to 0 before returning; if not reset,
michael@0 1125 * it holds a marker which the decoder will be unable to read past.
michael@0 1126 */
michael@0 1127
michael@0 1128 METHODDEF(boolean)
michael@0 1129 read_restart_marker (j_decompress_ptr cinfo)
michael@0 1130 {
michael@0 1131 /* Obtain a marker unless we already did. */
michael@0 1132 /* Note that next_marker will complain if it skips any data. */
michael@0 1133 if (cinfo->unread_marker == 0) {
michael@0 1134 if (! next_marker(cinfo))
michael@0 1135 return FALSE;
michael@0 1136 }
michael@0 1137
michael@0 1138 if (cinfo->unread_marker ==
michael@0 1139 ((int) M_RST0 + cinfo->marker->next_restart_num)) {
michael@0 1140 /* Normal case --- swallow the marker and let entropy decoder continue */
michael@0 1141 TRACEMS1(cinfo, 3, JTRC_RST, cinfo->marker->next_restart_num);
michael@0 1142 cinfo->unread_marker = 0;
michael@0 1143 } else {
michael@0 1144 /* Uh-oh, the restart markers have been messed up. */
michael@0 1145 /* Let the data source manager determine how to resync. */
michael@0 1146 if (! (*cinfo->src->resync_to_restart) (cinfo,
michael@0 1147 cinfo->marker->next_restart_num))
michael@0 1148 return FALSE;
michael@0 1149 }
michael@0 1150
michael@0 1151 /* Update next-restart state */
michael@0 1152 cinfo->marker->next_restart_num = (cinfo->marker->next_restart_num + 1) & 7;
michael@0 1153
michael@0 1154 return TRUE;
michael@0 1155 }
michael@0 1156
michael@0 1157
michael@0 1158 /*
michael@0 1159 * This is the default resync_to_restart method for data source managers
michael@0 1160 * to use if they don't have any better approach. Some data source managers
michael@0 1161 * may be able to back up, or may have additional knowledge about the data
michael@0 1162 * which permits a more intelligent recovery strategy; such managers would
michael@0 1163 * presumably supply their own resync method.
michael@0 1164 *
michael@0 1165 * read_restart_marker calls resync_to_restart if it finds a marker other than
michael@0 1166 * the restart marker it was expecting. (This code is *not* used unless
michael@0 1167 * a nonzero restart interval has been declared.) cinfo->unread_marker is
michael@0 1168 * the marker code actually found (might be anything, except 0 or FF).
michael@0 1169 * The desired restart marker number (0..7) is passed as a parameter.
michael@0 1170 * This routine is supposed to apply whatever error recovery strategy seems
michael@0 1171 * appropriate in order to position the input stream to the next data segment.
michael@0 1172 * Note that cinfo->unread_marker is treated as a marker appearing before
michael@0 1173 * the current data-source input point; usually it should be reset to zero
michael@0 1174 * before returning.
michael@0 1175 * Returns FALSE if suspension is required.
michael@0 1176 *
michael@0 1177 * This implementation is substantially constrained by wanting to treat the
michael@0 1178 * input as a data stream; this means we can't back up. Therefore, we have
michael@0 1179 * only the following actions to work with:
michael@0 1180 * 1. Simply discard the marker and let the entropy decoder resume at next
michael@0 1181 * byte of file.
michael@0 1182 * 2. Read forward until we find another marker, discarding intervening
michael@0 1183 * data. (In theory we could look ahead within the current bufferload,
michael@0 1184 * without having to discard data if we don't find the desired marker.
michael@0 1185 * This idea is not implemented here, in part because it makes behavior
michael@0 1186 * dependent on buffer size and chance buffer-boundary positions.)
michael@0 1187 * 3. Leave the marker unread (by failing to zero cinfo->unread_marker).
michael@0 1188 * This will cause the entropy decoder to process an empty data segment,
michael@0 1189 * inserting dummy zeroes, and then we will reprocess the marker.
michael@0 1190 *
michael@0 1191 * #2 is appropriate if we think the desired marker lies ahead, while #3 is
michael@0 1192 * appropriate if the found marker is a future restart marker (indicating
michael@0 1193 * that we have missed the desired restart marker, probably because it got
michael@0 1194 * corrupted).
michael@0 1195 * We apply #2 or #3 if the found marker is a restart marker no more than
michael@0 1196 * two counts behind or ahead of the expected one. We also apply #2 if the
michael@0 1197 * found marker is not a legal JPEG marker code (it's certainly bogus data).
michael@0 1198 * If the found marker is a restart marker more than 2 counts away, we do #1
michael@0 1199 * (too much risk that the marker is erroneous; with luck we will be able to
michael@0 1200 * resync at some future point).
michael@0 1201 * For any valid non-restart JPEG marker, we apply #3. This keeps us from
michael@0 1202 * overrunning the end of a scan. An implementation limited to single-scan
michael@0 1203 * files might find it better to apply #2 for markers other than EOI, since
michael@0 1204 * any other marker would have to be bogus data in that case.
michael@0 1205 */
michael@0 1206
michael@0 1207 GLOBAL(boolean)
michael@0 1208 jpeg_resync_to_restart (j_decompress_ptr cinfo, int desired)
michael@0 1209 {
michael@0 1210 int marker = cinfo->unread_marker;
michael@0 1211 int action = 1;
michael@0 1212
michael@0 1213 /* Always put up a warning. */
michael@0 1214 WARNMS2(cinfo, JWRN_MUST_RESYNC, marker, desired);
michael@0 1215
michael@0 1216 /* Outer loop handles repeated decision after scanning forward. */
michael@0 1217 for (;;) {
michael@0 1218 if (marker < (int) M_SOF0)
michael@0 1219 action = 2; /* invalid marker */
michael@0 1220 else if (marker < (int) M_RST0 || marker > (int) M_RST7)
michael@0 1221 action = 3; /* valid non-restart marker */
michael@0 1222 else {
michael@0 1223 if (marker == ((int) M_RST0 + ((desired+1) & 7)) ||
michael@0 1224 marker == ((int) M_RST0 + ((desired+2) & 7)))
michael@0 1225 action = 3; /* one of the next two expected restarts */
michael@0 1226 else if (marker == ((int) M_RST0 + ((desired-1) & 7)) ||
michael@0 1227 marker == ((int) M_RST0 + ((desired-2) & 7)))
michael@0 1228 action = 2; /* a prior restart, so advance */
michael@0 1229 else
michael@0 1230 action = 1; /* desired restart or too far away */
michael@0 1231 }
michael@0 1232 TRACEMS2(cinfo, 4, JTRC_RECOVERY_ACTION, marker, action);
michael@0 1233 switch (action) {
michael@0 1234 case 1:
michael@0 1235 /* Discard marker and let entropy decoder resume processing. */
michael@0 1236 cinfo->unread_marker = 0;
michael@0 1237 return TRUE;
michael@0 1238 case 2:
michael@0 1239 /* Scan to the next marker, and repeat the decision loop. */
michael@0 1240 if (! next_marker(cinfo))
michael@0 1241 return FALSE;
michael@0 1242 marker = cinfo->unread_marker;
michael@0 1243 break;
michael@0 1244 case 3:
michael@0 1245 /* Return without advancing past this marker. */
michael@0 1246 /* Entropy decoder will be forced to process an empty segment. */
michael@0 1247 return TRUE;
michael@0 1248 }
michael@0 1249 } /* end loop */
michael@0 1250 }
michael@0 1251
michael@0 1252
michael@0 1253 /*
michael@0 1254 * Reset marker processing state to begin a fresh datastream.
michael@0 1255 */
michael@0 1256
michael@0 1257 METHODDEF(void)
michael@0 1258 reset_marker_reader (j_decompress_ptr cinfo)
michael@0 1259 {
michael@0 1260 my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
michael@0 1261
michael@0 1262 cinfo->comp_info = NULL; /* until allocated by get_sof */
michael@0 1263 cinfo->input_scan_number = 0; /* no SOS seen yet */
michael@0 1264 cinfo->unread_marker = 0; /* no pending marker */
michael@0 1265 marker->pub.saw_SOI = FALSE; /* set internal state too */
michael@0 1266 marker->pub.saw_SOF = FALSE;
michael@0 1267 marker->pub.discarded_bytes = 0;
michael@0 1268 marker->cur_marker = NULL;
michael@0 1269 }
michael@0 1270
michael@0 1271
michael@0 1272 /*
michael@0 1273 * Initialize the marker reader module.
michael@0 1274 * This is called only once, when the decompression object is created.
michael@0 1275 */
michael@0 1276
michael@0 1277 GLOBAL(void)
michael@0 1278 jinit_marker_reader (j_decompress_ptr cinfo)
michael@0 1279 {
michael@0 1280 my_marker_ptr marker;
michael@0 1281 int i;
michael@0 1282
michael@0 1283 /* Create subobject in permanent pool */
michael@0 1284 marker = (my_marker_ptr)
michael@0 1285 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
michael@0 1286 SIZEOF(my_marker_reader));
michael@0 1287 cinfo->marker = (struct jpeg_marker_reader *) marker;
michael@0 1288 /* Initialize public method pointers */
michael@0 1289 marker->pub.reset_marker_reader = reset_marker_reader;
michael@0 1290 marker->pub.read_markers = read_markers;
michael@0 1291 marker->pub.read_restart_marker = read_restart_marker;
michael@0 1292 /* Initialize COM/APPn processing.
michael@0 1293 * By default, we examine and then discard APP0 and APP14,
michael@0 1294 * but simply discard COM and all other APPn.
michael@0 1295 */
michael@0 1296 marker->process_COM = skip_variable;
michael@0 1297 marker->length_limit_COM = 0;
michael@0 1298 for (i = 0; i < 16; i++) {
michael@0 1299 marker->process_APPn[i] = skip_variable;
michael@0 1300 marker->length_limit_APPn[i] = 0;
michael@0 1301 }
michael@0 1302 marker->process_APPn[0] = get_interesting_appn;
michael@0 1303 marker->process_APPn[14] = get_interesting_appn;
michael@0 1304 /* Reset marker processing state */
michael@0 1305 reset_marker_reader(cinfo);
michael@0 1306 }
michael@0 1307
michael@0 1308
michael@0 1309 /*
michael@0 1310 * Control saving of COM and APPn markers into marker_list.
michael@0 1311 */
michael@0 1312
michael@0 1313 #ifdef SAVE_MARKERS_SUPPORTED
michael@0 1314
michael@0 1315 GLOBAL(void)
michael@0 1316 jpeg_save_markers (j_decompress_ptr cinfo, int marker_code,
michael@0 1317 unsigned int length_limit)
michael@0 1318 {
michael@0 1319 my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
michael@0 1320 long maxlength;
michael@0 1321 jpeg_marker_parser_method processor;
michael@0 1322
michael@0 1323 /* Length limit mustn't be larger than what we can allocate
michael@0 1324 * (should only be a concern in a 16-bit environment).
michael@0 1325 */
michael@0 1326 maxlength = cinfo->mem->max_alloc_chunk - SIZEOF(struct jpeg_marker_struct);
michael@0 1327 if (((long) length_limit) > maxlength)
michael@0 1328 length_limit = (unsigned int) maxlength;
michael@0 1329
michael@0 1330 /* Choose processor routine to use.
michael@0 1331 * APP0/APP14 have special requirements.
michael@0 1332 */
michael@0 1333 if (length_limit) {
michael@0 1334 processor = save_marker;
michael@0 1335 /* If saving APP0/APP14, save at least enough for our internal use. */
michael@0 1336 if (marker_code == (int) M_APP0 && length_limit < APP0_DATA_LEN)
michael@0 1337 length_limit = APP0_DATA_LEN;
michael@0 1338 else if (marker_code == (int) M_APP14 && length_limit < APP14_DATA_LEN)
michael@0 1339 length_limit = APP14_DATA_LEN;
michael@0 1340 } else {
michael@0 1341 processor = skip_variable;
michael@0 1342 /* If discarding APP0/APP14, use our regular on-the-fly processor. */
michael@0 1343 if (marker_code == (int) M_APP0 || marker_code == (int) M_APP14)
michael@0 1344 processor = get_interesting_appn;
michael@0 1345 }
michael@0 1346
michael@0 1347 if (marker_code == (int) M_COM) {
michael@0 1348 marker->process_COM = processor;
michael@0 1349 marker->length_limit_COM = length_limit;
michael@0 1350 } else if (marker_code >= (int) M_APP0 && marker_code <= (int) M_APP15) {
michael@0 1351 marker->process_APPn[marker_code - (int) M_APP0] = processor;
michael@0 1352 marker->length_limit_APPn[marker_code - (int) M_APP0] = length_limit;
michael@0 1353 } else
michael@0 1354 ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, marker_code);
michael@0 1355 }
michael@0 1356
michael@0 1357 #endif /* SAVE_MARKERS_SUPPORTED */
michael@0 1358
michael@0 1359
michael@0 1360 /*
michael@0 1361 * Install a special processing method for COM or APPn markers.
michael@0 1362 */
michael@0 1363
michael@0 1364 GLOBAL(void)
michael@0 1365 jpeg_set_marker_processor (j_decompress_ptr cinfo, int marker_code,
michael@0 1366 jpeg_marker_parser_method routine)
michael@0 1367 {
michael@0 1368 my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
michael@0 1369
michael@0 1370 if (marker_code == (int) M_COM)
michael@0 1371 marker->process_COM = routine;
michael@0 1372 else if (marker_code >= (int) M_APP0 && marker_code <= (int) M_APP15)
michael@0 1373 marker->process_APPn[marker_code - (int) M_APP0] = routine;
michael@0 1374 else
michael@0 1375 ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, marker_code);
michael@0 1376 }

mercurial