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