< prev index next >

modules/javafx.graphics/src/main/native-iio/libjpeg7/jdmarker.c

Print this page


   1 /*
   2  * jdmarker.c
   3  *
   4  * Copyright (C) 1991-1998, Thomas G. Lane.

   5  * This file is part of the Independent JPEG Group's software.
   6  * For conditions of distribution and use, see the accompanying README file.
   7  *
   8  * This file contains routines to decode JPEG datastream markers.
   9  * Most of the complexity arises from our desire to support input
  10  * suspension: if not all of the data for a marker is available,
  11  * we must exit back to the application.  On resumption, we reprocess
  12  * the marker.
  13  */
  14 
  15 #define JPEG_INTERNALS
  16 #include "jinclude.h"
  17 #include "jpeglib.h"
  18 
  19 
  20 typedef enum {                  /* JPEG marker codes */
  21   M_SOF0  = 0xc0,
  22   M_SOF1  = 0xc1,
  23   M_SOF2  = 0xc2,
  24   M_SOF3  = 0xc3,


  59   M_EXP   = 0xdf,
  60 
  61   M_APP0  = 0xe0,
  62   M_APP1  = 0xe1,
  63   M_APP2  = 0xe2,
  64   M_APP3  = 0xe3,
  65   M_APP4  = 0xe4,
  66   M_APP5  = 0xe5,
  67   M_APP6  = 0xe6,
  68   M_APP7  = 0xe7,
  69   M_APP8  = 0xe8,
  70   M_APP9  = 0xe9,
  71   M_APP10 = 0xea,
  72   M_APP11 = 0xeb,
  73   M_APP12 = 0xec,
  74   M_APP13 = 0xed,
  75   M_APP14 = 0xee,
  76   M_APP15 = 0xef,
  77 
  78   M_JPG0  = 0xf0,

  79   M_JPG13 = 0xfd,
  80   M_COM   = 0xfe,
  81 
  82   M_TEM   = 0x01,
  83 
  84   M_ERROR = 0x100
  85 } JPEG_MARKER;
  86 
  87 
  88 /* Private state */
  89 
  90 typedef struct {
  91   struct jpeg_marker_reader pub; /* public fields */
  92 
  93   /* Application-overridable marker processing methods */
  94   jpeg_marker_parser_method process_COM;
  95   jpeg_marker_parser_method process_APPn[16];
  96 
  97   /* Limit on marker data length to save for each marker type */
  98   unsigned int length_limit_COM;


 199 {
 200   int i;
 201 
 202   TRACEMS(cinfo, 1, JTRC_SOI);
 203 
 204   if (cinfo->marker->saw_SOI)
 205     ERREXIT(cinfo, JERR_SOI_DUPLICATE);
 206 
 207   /* Reset all parameters that are defined to be reset by SOI */
 208 
 209   for (i = 0; i < NUM_ARITH_TBLS; i++) {
 210     cinfo->arith_dc_L[i] = 0;
 211     cinfo->arith_dc_U[i] = 1;
 212     cinfo->arith_ac_K[i] = 5;
 213   }
 214   cinfo->restart_interval = 0;
 215 
 216   /* Set initial assumptions for colorspace etc */
 217 
 218   cinfo->jpeg_color_space = JCS_UNKNOWN;

 219   cinfo->CCIR601_sampling = FALSE; /* Assume non-CCIR sampling??? */
 220 
 221   cinfo->saw_JFIF_marker = FALSE;
 222   cinfo->JFIF_major_version = 1; /* set default JFIF APP0 values */
 223   cinfo->JFIF_minor_version = 1;
 224   cinfo->density_unit = 0;
 225   cinfo->X_density = 1;
 226   cinfo->Y_density = 1;
 227   cinfo->saw_Adobe_marker = FALSE;
 228   cinfo->Adobe_transform = 0;
 229 
 230   cinfo->marker->saw_SOI = TRUE;
 231 
 232   return TRUE;
 233 }
 234 
 235 
 236 LOCAL(boolean)
 237 get_sof (j_decompress_ptr cinfo, boolean is_prog, boolean is_arith)

 238 /* Process a SOFn marker */
 239 {
 240   INT32 length;
 241   int c, ci;
 242   jpeg_component_info * compptr;
 243   INPUT_VARS(cinfo);
 244 

 245   cinfo->progressive_mode = is_prog;
 246   cinfo->arith_code = is_arith;
 247 
 248   INPUT_2BYTES(cinfo, length, return FALSE);
 249 
 250   INPUT_BYTE(cinfo, cinfo->data_precision, return FALSE);
 251   INPUT_2BYTES(cinfo, cinfo->image_height, return FALSE);
 252   INPUT_2BYTES(cinfo, cinfo->image_width, return FALSE);
 253   INPUT_BYTE(cinfo, cinfo->num_components, return FALSE);
 254 
 255   length -= 8;
 256 
 257   TRACEMS4(cinfo, 1, JTRC_SOF, cinfo->unread_marker,
 258            (int) cinfo->image_width, (int) cinfo->image_height,
 259            cinfo->num_components);
 260 
 261   if (cinfo->marker->saw_SOF)
 262     ERREXIT(cinfo, JERR_SOF_DUPLICATE);
 263 
 264   /* We don't support files in which the image height is initially specified */
 265   /* as 0 and is later redefined by DNL.  As long as we have to check that,  */
 266   /* might as well have a general sanity check. */
 267   if (cinfo->image_height <= 0 || cinfo->image_width <= 0
 268       || cinfo->num_components <= 0)
 269     ERREXIT(cinfo, JERR_EMPTY_IMAGE);
 270 
 271   if (length != (cinfo->num_components * 3))
 272     ERREXIT(cinfo, JERR_BAD_LENGTH);
 273 
 274   if (cinfo->comp_info == NULL) /* do only once, even if suspend */
 275     cinfo->comp_info = (jpeg_component_info *) (*cinfo->mem->alloc_small)
 276                         ((j_common_ptr) cinfo, JPOOL_IMAGE,
 277                          cinfo->num_components * SIZEOF(jpeg_component_info));
 278 
 279   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
 280        ci++, compptr++) {

















 281     compptr->component_index = ci;
 282     INPUT_BYTE(cinfo, compptr->component_id, return FALSE);
 283     INPUT_BYTE(cinfo, c, return FALSE);
 284     compptr->h_samp_factor = (c >> 4) & 15;
 285     compptr->v_samp_factor = (c     ) & 15;
 286     INPUT_BYTE(cinfo, compptr->quant_tbl_no, return FALSE);
 287 
 288     TRACEMS4(cinfo, 1, JTRC_SOF_COMPONENT,
 289              compptr->component_id, compptr->h_samp_factor,
 290              compptr->v_samp_factor, compptr->quant_tbl_no);
 291   }
 292 
 293   cinfo->marker->saw_SOF = TRUE;
 294 
 295   INPUT_SYNC(cinfo);
 296   return TRUE;
 297 }
 298 
 299 
 300 LOCAL(boolean)
 301 get_sos (j_decompress_ptr cinfo)
 302 /* Process a SOS marker */
 303 {
 304   INT32 length;
 305   int i, ci, n, c, cc;
 306   jpeg_component_info * compptr;
 307   INPUT_VARS(cinfo);
 308 
 309   if (! cinfo->marker->saw_SOF)
 310     ERREXIT(cinfo, JERR_SOS_NO_SOF);
 311 
 312   INPUT_2BYTES(cinfo, length, return FALSE);
 313 
 314   INPUT_BYTE(cinfo, n, return FALSE); /* Number of components */
 315 
 316   TRACEMS1(cinfo, 1, JTRC_SOS, n);
 317 
 318   if (length != (n * 2 + 6) || n < 1 || n > MAX_COMPS_IN_SCAN)


 319     ERREXIT(cinfo, JERR_BAD_LENGTH);
 320 
 321   cinfo->comps_in_scan = n;
 322 
 323   /* Collect the component-spec parameters */
 324 
 325   for (i = 0; i < n; i++) {
 326     INPUT_BYTE(cinfo, cc, return FALSE);
 327     INPUT_BYTE(cinfo, c, return FALSE);
 328 

















 329     for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
 330          ci++, compptr++) {
 331       if (cc == compptr->component_id)
 332         goto id_found;
 333     }
 334 
 335     ERREXIT1(cinfo, JERR_BAD_COMPONENT_ID, cc);
 336 
 337   id_found:
 338 
 339     cinfo->cur_comp_info[i] = compptr;

 340     compptr->dc_tbl_no = (c >> 4) & 15;
 341     compptr->ac_tbl_no = (c     ) & 15;
 342 
 343     TRACEMS3(cinfo, 1, JTRC_SOS_COMPONENT, cc,
 344              compptr->dc_tbl_no, compptr->ac_tbl_no);
 345   }
 346 
 347   /* Collect the additional scan parameters Ss, Se, Ah/Al. */
 348   INPUT_BYTE(cinfo, c, return FALSE);
 349   cinfo->Ss = c;
 350   INPUT_BYTE(cinfo, c, return FALSE);
 351   cinfo->Se = c;
 352   INPUT_BYTE(cinfo, c, return FALSE);
 353   cinfo->Ah = (c >> 4) & 15;
 354   cinfo->Al = (c     ) & 15;
 355 
 356   TRACEMS4(cinfo, 1, JTRC_SOS_PARAMS, cinfo->Ss, cinfo->Se,
 357            cinfo->Ah, cinfo->Al);
 358 
 359   /* Prepare to scan data & restart markers */
 360   cinfo->marker->next_restart_num = 0;
 361 
 362   /* Count another SOS marker */
 363   cinfo->input_scan_number++;
 364 
 365   INPUT_SYNC(cinfo);
 366   return TRUE;
 367 }
 368 
 369 
 370 #ifdef D_ARITH_CODING_SUPPORTED
 371 
 372 LOCAL(boolean)
 373 get_dac (j_decompress_ptr cinfo)
 374 /* Process a DAC marker */
 375 {
 376   INT32 length;
 377   int index, val;
 378   INPUT_VARS(cinfo);
 379 
 380   INPUT_2BYTES(cinfo, length, return FALSE);
 381   length -= 2;
 382 
 383   while (length > 0) {


 439     for (i = 1; i <= 16; i++) {
 440       INPUT_BYTE(cinfo, bits[i], return FALSE);
 441       count += bits[i];
 442     }
 443 
 444     length -= 1 + 16;
 445 
 446     TRACEMS8(cinfo, 2, JTRC_HUFFBITS,
 447              bits[1], bits[2], bits[3], bits[4],
 448              bits[5], bits[6], bits[7], bits[8]);
 449     TRACEMS8(cinfo, 2, JTRC_HUFFBITS,
 450              bits[9], bits[10], bits[11], bits[12],
 451              bits[13], bits[14], bits[15], bits[16]);
 452 
 453     /* Here we just do minimal validation of the counts to avoid walking
 454      * off the end of our table space.  jdhuff.c will check more carefully.
 455      */
 456     if (count > 256 || ((INT32) count) > length)
 457       ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
 458 


 459     for (i = 0; i < count; i++)
 460       INPUT_BYTE(cinfo, huffval[i], return FALSE);
 461 
 462     length -= count;
 463 
 464     if (index & 0x10) {         /* AC table definition */
 465       index -= 0x10;
 466       htblptr = &cinfo->ac_huff_tbl_ptrs[index];
 467     } else {                    /* DC table definition */
 468       htblptr = &cinfo->dc_huff_tbl_ptrs[index];
 469     }
 470 
 471     if (index < 0 || index >= NUM_HUFF_TBLS)
 472       ERREXIT1(cinfo, JERR_DHT_INDEX, index);
 473 
 474     if (*htblptr == NULL)
 475       *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
 476 
 477     MEMCOPY((*htblptr)->bits, bits, SIZEOF((*htblptr)->bits));
 478     MEMCOPY((*htblptr)->huffval, huffval, SIZEOF((*htblptr)->huffval));
 479   }
 480 
 481   if (length != 0)
 482     ERREXIT(cinfo, JERR_BAD_LENGTH);
 483 
 484   INPUT_SYNC(cinfo);
 485   return TRUE;
 486 }
 487 
 488 
 489 LOCAL(boolean)
 490 get_dqt (j_decompress_ptr cinfo)
 491 /* Process a DQT marker */
 492 {
 493   INT32 length;
 494   int n, i, prec;
 495   unsigned int tmp;
 496   JQUANT_TBL *quant_ptr;

 497   INPUT_VARS(cinfo);
 498 
 499   INPUT_2BYTES(cinfo, length, return FALSE);
 500   length -= 2;
 501 
 502   while (length > 0) {

 503     INPUT_BYTE(cinfo, n, return FALSE);
 504     prec = n >> 4;
 505     n &= 0x0F;
 506 
 507     TRACEMS2(cinfo, 1, JTRC_DQT, n, prec);
 508 
 509     if (n >= NUM_QUANT_TBLS)
 510       ERREXIT1(cinfo, JERR_DQT_INDEX, n);
 511 
 512     if (cinfo->quant_tbl_ptrs[n] == NULL)
 513       cinfo->quant_tbl_ptrs[n] = jpeg_alloc_quant_table((j_common_ptr) cinfo);
 514     quant_ptr = cinfo->quant_tbl_ptrs[n];
 515 












 516     for (i = 0; i < DCTSIZE2; i++) {


















 517       if (prec)
 518         INPUT_2BYTES(cinfo, tmp, return FALSE);
 519       else
 520         INPUT_BYTE(cinfo, tmp, return FALSE);
 521       /* We convert the zigzag-order table to natural array order. */
 522       quant_ptr->quantval[jpeg_natural_order[i]] = (UINT16) tmp;
 523     }
 524 
 525     if (cinfo->err->trace_level >= 2) {
 526       for (i = 0; i < DCTSIZE2; i += 8) {
 527         TRACEMS8(cinfo, 2, JTRC_QUANTVALS,
 528                  quant_ptr->quantval[i],   quant_ptr->quantval[i+1],
 529                  quant_ptr->quantval[i+2], quant_ptr->quantval[i+3],
 530                  quant_ptr->quantval[i+4], quant_ptr->quantval[i+5],
 531                  quant_ptr->quantval[i+6], quant_ptr->quantval[i+7]);
 532       }
 533     }
 534 
 535     length -= DCTSIZE2+1;
 536     if (prec) length -= DCTSIZE2;
 537   }
 538 
 539   if (length != 0)
 540     ERREXIT(cinfo, JERR_BAD_LENGTH);
 541 
 542   INPUT_SYNC(cinfo);
 543   return TRUE;
 544 }
 545 
 546 
 547 LOCAL(boolean)
 548 get_dri (j_decompress_ptr cinfo)
 549 /* Process a DRI marker */
 550 {
 551   INT32 length;
 552   unsigned int tmp;
 553   INPUT_VARS(cinfo);
 554 
 555   INPUT_2BYTES(cinfo, length, return FALSE);
 556 
 557   if (length != 4)
 558     ERREXIT(cinfo, JERR_BAD_LENGTH);
 559 
 560   INPUT_2BYTES(cinfo, tmp, return FALSE);
 561 
 562   TRACEMS1(cinfo, 1, JTRC_DRI, tmp);
 563 
 564   cinfo->restart_interval = tmp;
 565 
 566   INPUT_SYNC(cinfo);
 567   return TRUE;
 568 }
 569 
 570 






























































 571 /*
 572  * Routines for processing APPn and COM markers.
 573  * These are either saved in memory or discarded, per application request.
 574  * APP0 and APP14 are specially checked to see if they are
 575  * JFIF and Adobe markers, respectively.
 576  */
 577 
 578 #define APP0_DATA_LEN   14      /* Length of interesting data in APP0 */
 579 #define APP14_DATA_LEN  12      /* Length of interesting data in APP14 */
 580 #define APPN_DATA_LEN   14      /* Must be the largest of the above!! */
 581 
 582 
 583 LOCAL(void)
 584 examine_app0 (j_decompress_ptr cinfo, JOCTET FAR * data,
 585               unsigned int datalen, INT32 remaining)
 586 /* Examine first few bytes from an APP0.
 587  * Take appropriate action if it is a JFIF marker.
 588  * datalen is # of bytes at data[], remaining is length of rest of marker data.
 589  */
 590 {
 591   INT32 totallen = (INT32) datalen + remaining;
 592 
 593   if (datalen >= APP0_DATA_LEN &&
 594       GETJOCTET(data[0]) == 0x4A &&
 595       GETJOCTET(data[1]) == 0x46 &&
 596       GETJOCTET(data[2]) == 0x49 &&
 597       GETJOCTET(data[3]) == 0x46 &&
 598       GETJOCTET(data[4]) == 0) {
 599     /* Found JFIF APP0 marker: save info */
 600     cinfo->saw_JFIF_marker = TRUE;
 601     cinfo->JFIF_major_version = GETJOCTET(data[5]);
 602     cinfo->JFIF_minor_version = GETJOCTET(data[6]);
 603     cinfo->density_unit = GETJOCTET(data[7]);
 604     cinfo->X_density = (GETJOCTET(data[8]) << 8) + GETJOCTET(data[9]);
 605     cinfo->Y_density = (GETJOCTET(data[10]) << 8) + GETJOCTET(data[11]);
 606     /* Check version.
 607      * Major version must be 1, anything else signals an incompatible change.

 608      * (We used to treat this as an error, but now it's a nonfatal warning,
 609      * because some bozo at Hijaak couldn't read the spec.)
 610      * Minor version should be 0..2, but process anyway if newer.
 611      */
 612     if (cinfo->JFIF_major_version != 1)
 613       WARNMS2(cinfo, JWRN_JFIF_MAJOR,
 614               cinfo->JFIF_major_version, cinfo->JFIF_minor_version);
 615     /* Generate trace messages */
 616     TRACEMS5(cinfo, 1, JTRC_JFIF,
 617              cinfo->JFIF_major_version, cinfo->JFIF_minor_version,
 618              cinfo->X_density, cinfo->Y_density, cinfo->density_unit);
 619     /* Validate thumbnail dimensions and issue appropriate messages */
 620     if (GETJOCTET(data[12]) | GETJOCTET(data[13]))
 621       TRACEMS2(cinfo, 1, JTRC_JFIF_THUMBNAIL,
 622                GETJOCTET(data[12]), GETJOCTET(data[13]));
 623     totallen -= APP0_DATA_LEN;
 624     if (totallen !=
 625         ((INT32)GETJOCTET(data[12]) * (INT32)GETJOCTET(data[13]) * (INT32) 3))
 626       TRACEMS1(cinfo, 1, JTRC_JFIF_BADTHUMBNAILSIZE, (int) totallen);
 627   } else if (datalen >= 6 &&
 628       GETJOCTET(data[0]) == 0x4A &&
 629       GETJOCTET(data[1]) == 0x46 &&
 630       GETJOCTET(data[2]) == 0x58 &&
 631       GETJOCTET(data[3]) == 0x58 &&
 632       GETJOCTET(data[4]) == 0) {


 929   int c, c2;
 930   INPUT_VARS(cinfo);
 931 
 932   INPUT_BYTE(cinfo, c, return FALSE);
 933   INPUT_BYTE(cinfo, c2, return FALSE);
 934   if (c != 0xFF || c2 != (int) M_SOI)
 935     ERREXIT2(cinfo, JERR_NO_SOI, c, c2);
 936 
 937   cinfo->unread_marker = c2;
 938 
 939   INPUT_SYNC(cinfo);
 940   return TRUE;
 941 }
 942 
 943 
 944 /*
 945  * Read markers until SOS or EOI.
 946  *
 947  * Returns same codes as are defined for jpeg_consume_input:
 948  * JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.





 949  */
 950 
 951 METHODDEF(int)
 952 read_markers (j_decompress_ptr cinfo)
 953 {
 954   /* Outer loop repeats once for each marker. */
 955   for (;;) {
 956     /* Collect the marker proper, unless we already did. */
 957     /* NB: first_marker() enforces the requirement that SOI appear first. */
 958     if (cinfo->unread_marker == 0) {
 959       if (! cinfo->marker->saw_SOI) {
 960         if (! first_marker(cinfo))
 961           return JPEG_SUSPENDED;
 962       } else {
 963         if (! next_marker(cinfo))
 964           return JPEG_SUSPENDED;
 965       }
 966     }
 967     /* At this point cinfo->unread_marker contains the marker code and the
 968      * input point is just past the marker proper, but before any parameters.
 969      * A suspension will cause us to return with this state still true.
 970      */
 971     switch (cinfo->unread_marker) {
 972     case M_SOI:
 973       if (! get_soi(cinfo))
 974         return JPEG_SUSPENDED;
 975       break;
 976 
 977     case M_SOF0:                /* Baseline */




 978     case M_SOF1:                /* Extended sequential, Huffman */
 979       if (! get_sof(cinfo, FALSE, FALSE))
 980         return JPEG_SUSPENDED;
 981       break;
 982 
 983     case M_SOF2:                /* Progressive, Huffman */
 984       if (! get_sof(cinfo, TRUE, FALSE))
 985         return JPEG_SUSPENDED;
 986       break;
 987 
 988     case M_SOF9:                /* Extended sequential, arithmetic */
 989       if (! get_sof(cinfo, FALSE, TRUE))
 990         return JPEG_SUSPENDED;
 991       break;
 992 
 993     case M_SOF10:               /* Progressive, arithmetic */
 994       if (! get_sof(cinfo, TRUE, TRUE))
 995         return JPEG_SUSPENDED;
 996       break;
 997 
 998     /* Currently unsupported SOFn types */
 999     case M_SOF3:                /* Lossless, Huffman */
1000     case M_SOF5:                /* Differential sequential, Huffman */
1001     case M_SOF6:                /* Differential progressive, Huffman */
1002     case M_SOF7:                /* Differential lossless, Huffman */
1003     case M_JPG:                 /* Reserved for JPEG extensions */
1004     case M_SOF11:               /* Lossless, arithmetic */
1005     case M_SOF13:               /* Differential sequential, arithmetic */
1006     case M_SOF14:               /* Differential progressive, arithmetic */
1007     case M_SOF15:               /* Differential lossless, arithmetic */
1008       ERREXIT1(cinfo, JERR_SOF_UNSUPPORTED, cinfo->unread_marker);
1009       break;
1010 
1011     case M_SOS:
1012       if (! get_sos(cinfo))
1013         return JPEG_SUSPENDED;
1014       cinfo->unread_marker = 0; /* processed the marker */


1022     case M_DAC:
1023       if (! get_dac(cinfo))
1024         return JPEG_SUSPENDED;
1025       break;
1026 
1027     case M_DHT:
1028       if (! get_dht(cinfo))
1029         return JPEG_SUSPENDED;
1030       break;
1031 
1032     case M_DQT:
1033       if (! get_dqt(cinfo))
1034         return JPEG_SUSPENDED;
1035       break;
1036 
1037     case M_DRI:
1038       if (! get_dri(cinfo))
1039         return JPEG_SUSPENDED;
1040       break;
1041 





1042     case M_APP0:
1043     case M_APP1:
1044     case M_APP2:
1045     case M_APP3:
1046     case M_APP4:
1047     case M_APP5:
1048     case M_APP6:
1049     case M_APP7:
1050     case M_APP8:
1051     case M_APP9:
1052     case M_APP10:
1053     case M_APP11:
1054     case M_APP12:
1055     case M_APP13:
1056     case M_APP14:
1057     case M_APP15:
1058       if (! (*((my_marker_ptr) cinfo->marker)->process_APPn[
1059                 cinfo->unread_marker - (int) M_APP0]) (cinfo))
1060         return JPEG_SUSPENDED;
1061       break;


1251   marker->pub.discarded_bytes = 0;
1252   marker->cur_marker = NULL;
1253 }
1254 
1255 
1256 /*
1257  * Initialize the marker reader module.
1258  * This is called only once, when the decompression object is created.
1259  */
1260 
1261 GLOBAL(void)
1262 jinit_marker_reader (j_decompress_ptr cinfo)
1263 {
1264   my_marker_ptr marker;
1265   int i;
1266 
1267   /* Create subobject in permanent pool */
1268   marker = (my_marker_ptr)
1269     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
1270                                 SIZEOF(my_marker_reader));
1271   cinfo->marker = (struct jpeg_marker_reader *) marker;
1272   /* Initialize public method pointers */
1273   marker->pub.reset_marker_reader = reset_marker_reader;
1274   marker->pub.read_markers = read_markers;
1275   marker->pub.read_restart_marker = read_restart_marker;
1276   /* Initialize COM/APPn processing.
1277    * By default, we examine and then discard APP0 and APP14,
1278    * but simply discard COM and all other APPn.
1279    */
1280   marker->process_COM = skip_variable;
1281   marker->length_limit_COM = 0;
1282   for (i = 0; i < 16; i++) {
1283     marker->process_APPn[i] = skip_variable;
1284     marker->length_limit_APPn[i] = 0;
1285   }
1286   marker->process_APPn[0] = get_interesting_appn;
1287   marker->process_APPn[14] = get_interesting_appn;
1288   /* Reset marker processing state */
1289   reset_marker_reader(cinfo);
1290 }
1291 


   1 /*
   2  * jdmarker.c
   3  *
   4  * Copyright (C) 1991-1998, Thomas G. Lane.
   5  * Modified 2009-2013 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 
  21 typedef enum {                  /* JPEG marker codes */
  22   M_SOF0  = 0xc0,
  23   M_SOF1  = 0xc1,
  24   M_SOF2  = 0xc2,
  25   M_SOF3  = 0xc3,


  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 
  92 typedef 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;


 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 
 239 LOCAL(boolean)
 240 get_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 
 321 LOCAL(boolean)
 322 get_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     /* Note:  This also ensures that all of the SOF components are      */
 354     /* referenced in the single scan case, which prevents access to     */
 355     /* uninitialized memory in later decoding stages. */
 356     for (ci = 0; ci < i; ci++) {
 357       if (c == cinfo->cur_comp_info[ci]->component_id) {
 358         c = cinfo->cur_comp_info[0]->component_id;
 359         for (ci = 1; ci < i; ci++) {
 360           compptr = cinfo->cur_comp_info[ci];
 361           if (compptr->component_id > c) c = compptr->component_id;
 362         }
 363         c++;
 364         break;
 365       }
 366     }
 367 
 368     for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
 369          ci++, compptr++) {
 370       if (c == compptr->component_id)
 371         goto id_found;
 372     }
 373 
 374     ERREXIT1(cinfo, JERR_BAD_COMPONENT_ID, c);
 375 
 376   id_found:
 377 
 378     cinfo->cur_comp_info[i] = compptr;
 379     INPUT_BYTE(cinfo, c, return FALSE);
 380     compptr->dc_tbl_no = (c >> 4) & 15;
 381     compptr->ac_tbl_no = (c     ) & 15;
 382 
 383     TRACEMS3(cinfo, 1, JTRC_SOS_COMPONENT, compptr->component_id,
 384              compptr->dc_tbl_no, compptr->ac_tbl_no);
 385   }
 386 
 387   /* Collect the additional scan parameters Ss, Se, Ah/Al. */
 388   INPUT_BYTE(cinfo, c, return FALSE);
 389   cinfo->Ss = c;
 390   INPUT_BYTE(cinfo, c, return FALSE);
 391   cinfo->Se = c;
 392   INPUT_BYTE(cinfo, c, return FALSE);
 393   cinfo->Ah = (c >> 4) & 15;
 394   cinfo->Al = (c     ) & 15;
 395 
 396   TRACEMS4(cinfo, 1, JTRC_SOS_PARAMS, cinfo->Ss, cinfo->Se,
 397            cinfo->Ah, cinfo->Al);
 398 
 399   /* Prepare to scan data & restart markers */
 400   cinfo->marker->next_restart_num = 0;
 401 
 402   /* Count another (non-pseudo) SOS marker */
 403   if (n) cinfo->input_scan_number++;
 404 
 405   INPUT_SYNC(cinfo);
 406   return TRUE;
 407 }
 408 
 409 
 410 #ifdef D_ARITH_CODING_SUPPORTED
 411 
 412 LOCAL(boolean)
 413 get_dac (j_decompress_ptr cinfo)
 414 /* Process a DAC marker */
 415 {
 416   INT32 length;
 417   int index, val;
 418   INPUT_VARS(cinfo);
 419 
 420   INPUT_2BYTES(cinfo, length, return FALSE);
 421   length -= 2;
 422   
 423   while (length > 0) {


 479     for (i = 1; i <= 16; i++) {
 480       INPUT_BYTE(cinfo, bits[i], return FALSE);
 481       count += bits[i];
 482     }
 483 
 484     length -= 1 + 16;
 485 
 486     TRACEMS8(cinfo, 2, JTRC_HUFFBITS,
 487              bits[1], bits[2], bits[3], bits[4],
 488              bits[5], bits[6], bits[7], bits[8]);
 489     TRACEMS8(cinfo, 2, JTRC_HUFFBITS,
 490              bits[9], bits[10], bits[11], bits[12],
 491              bits[13], bits[14], bits[15], bits[16]);
 492 
 493     /* Here we just do minimal validation of the counts to avoid walking
 494      * off the end of our table space.  jdhuff.c will check more carefully.
 495      */
 496     if (count > 256 || ((INT32) count) > length)
 497       ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
 498 
 499     MEMZERO(huffval, SIZEOF(huffval)); /* pre-zero array for later copy */
 500 
 501     for (i = 0; i < count; i++)
 502       INPUT_BYTE(cinfo, huffval[i], return FALSE);
 503 
 504     length -= count;
 505 
 506     if (index & 0x10) {             /* AC table definition */
 507       index -= 0x10;
 508       htblptr = &cinfo->ac_huff_tbl_ptrs[index];
 509     } else {                    /* DC table definition */
 510       htblptr = &cinfo->dc_huff_tbl_ptrs[index];
 511     }
 512 
 513     if (index < 0 || index >= NUM_HUFF_TBLS)
 514       ERREXIT1(cinfo, JERR_DHT_INDEX, index);
 515 
 516     if (*htblptr == NULL)
 517       *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
 518   
 519     MEMCOPY((*htblptr)->bits, bits, SIZEOF((*htblptr)->bits));
 520     MEMCOPY((*htblptr)->huffval, huffval, SIZEOF((*htblptr)->huffval));
 521   }
 522 
 523   if (length != 0)
 524     ERREXIT(cinfo, JERR_BAD_LENGTH);
 525 
 526   INPUT_SYNC(cinfo);
 527   return TRUE;
 528 }
 529 
 530 
 531 LOCAL(boolean)
 532 get_dqt (j_decompress_ptr cinfo)
 533 /* Process a DQT marker */
 534 {
 535   INT32 length, count, i;
 536   int n, prec;
 537   unsigned int tmp;
 538   JQUANT_TBL *quant_ptr;
 539   const int *natural_order;
 540   INPUT_VARS(cinfo);
 541 
 542   INPUT_2BYTES(cinfo, length, return FALSE);
 543   length -= 2;
 544 
 545   while (length > 0) {
 546     length--;
 547     INPUT_BYTE(cinfo, n, return FALSE);
 548     prec = n >> 4;
 549     n &= 0x0F;
 550 
 551     TRACEMS2(cinfo, 1, JTRC_DQT, n, prec);
 552 
 553     if (n >= NUM_QUANT_TBLS)
 554       ERREXIT1(cinfo, JERR_DQT_INDEX, n);
 555       
 556     if (cinfo->quant_tbl_ptrs[n] == NULL)
 557       cinfo->quant_tbl_ptrs[n] = jpeg_alloc_quant_table((j_common_ptr) cinfo);
 558     quant_ptr = cinfo->quant_tbl_ptrs[n];
 559 
 560     if (prec) {
 561       if (length < DCTSIZE2 * 2) {
 562         /* Initialize full table for safety. */
 563         for (i = 0; i < DCTSIZE2; i++) {
 564           quant_ptr->quantval[i] = 1;
 565         }
 566         count = length >> 1;
 567       } else
 568         count = DCTSIZE2;
 569     } else {
 570       if (length < DCTSIZE2) {
 571         /* Initialize full table for safety. */
 572         for (i = 0; i < DCTSIZE2; i++) {
 573           quant_ptr->quantval[i] = 1;
 574         }
 575         count = length;
 576       } else
 577         count = DCTSIZE2;
 578     }
 579 
 580     switch (count) {
 581     case (2*2): natural_order = jpeg_natural_order2; break;
 582     case (3*3): natural_order = jpeg_natural_order3; break;
 583     case (4*4): natural_order = jpeg_natural_order4; break;
 584     case (5*5): natural_order = jpeg_natural_order5; break;
 585     case (6*6): natural_order = jpeg_natural_order6; break;
 586     case (7*7): natural_order = jpeg_natural_order7; break;
 587     default:    natural_order = jpeg_natural_order;  break;
 588     }
 589 
 590     for (i = 0; i < count; i++) {
 591       if (prec)
 592         INPUT_2BYTES(cinfo, tmp, return FALSE);
 593       else
 594         INPUT_BYTE(cinfo, tmp, return FALSE);
 595       /* We convert the zigzag-order table to natural array order. */
 596       quant_ptr->quantval[natural_order[i]] = (UINT16) tmp;
 597     }
 598 
 599     if (cinfo->err->trace_level >= 2) {
 600       for (i = 0; i < DCTSIZE2; i += 8) {
 601         TRACEMS8(cinfo, 2, JTRC_QUANTVALS,
 602                  quant_ptr->quantval[i],   quant_ptr->quantval[i+1],
 603                  quant_ptr->quantval[i+2], quant_ptr->quantval[i+3],
 604                  quant_ptr->quantval[i+4], quant_ptr->quantval[i+5],
 605                  quant_ptr->quantval[i+6], quant_ptr->quantval[i+7]);
 606       }
 607     }
 608 
 609     length -= count;
 610     if (prec) length -= count;
 611   }
 612 
 613   if (length != 0)
 614     ERREXIT(cinfo, JERR_BAD_LENGTH);
 615 
 616   INPUT_SYNC(cinfo);
 617   return TRUE;
 618 }
 619 
 620 
 621 LOCAL(boolean)
 622 get_dri (j_decompress_ptr cinfo)
 623 /* Process a DRI marker */
 624 {
 625   INT32 length;
 626   unsigned int tmp;
 627   INPUT_VARS(cinfo);
 628 
 629   INPUT_2BYTES(cinfo, length, return FALSE);
 630   
 631   if (length != 4)
 632     ERREXIT(cinfo, JERR_BAD_LENGTH);
 633 
 634   INPUT_2BYTES(cinfo, tmp, return FALSE);
 635 
 636   TRACEMS1(cinfo, 1, JTRC_DRI, tmp);
 637 
 638   cinfo->restart_interval = tmp;
 639 
 640   INPUT_SYNC(cinfo);
 641   return TRUE;
 642 }
 643 
 644 
 645 LOCAL(boolean)
 646 get_lse (j_decompress_ptr cinfo)
 647 /* Process an LSE marker */
 648 {
 649   INT32 length;
 650   unsigned int tmp;
 651   int cid;
 652   INPUT_VARS(cinfo);
 653 
 654   if (! cinfo->marker->saw_SOF)
 655     ERREXITS(cinfo, JERR_SOF_BEFORE, "LSE");
 656 
 657   if (cinfo->num_components < 3) goto bad;
 658 
 659   INPUT_2BYTES(cinfo, length, return FALSE);
 660 
 661   if (length != 24)
 662     ERREXIT(cinfo, JERR_BAD_LENGTH);
 663 
 664   INPUT_BYTE(cinfo, tmp, return FALSE);
 665   if (tmp != 0x0D)      /* ID inverse transform specification */
 666     ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, cinfo->unread_marker);
 667   INPUT_2BYTES(cinfo, tmp, return FALSE);
 668   if (tmp != MAXJSAMPLE) goto bad;              /* MAXTRANS */
 669   INPUT_BYTE(cinfo, tmp, return FALSE);
 670   if (tmp != 3) goto bad;                       /* Nt=3 */
 671   INPUT_BYTE(cinfo, cid, return FALSE);
 672   if (cid != cinfo->comp_info[1].component_id) goto bad;
 673   INPUT_BYTE(cinfo, cid, return FALSE);
 674   if (cid != cinfo->comp_info[0].component_id) goto bad;
 675   INPUT_BYTE(cinfo, cid, return FALSE);
 676   if (cid != cinfo->comp_info[2].component_id) goto bad;
 677   INPUT_BYTE(cinfo, tmp, return FALSE);
 678   if (tmp != 0x80) goto bad;            /* F1: CENTER1=1, NORM1=0 */
 679   INPUT_2BYTES(cinfo, tmp, return FALSE);
 680   if (tmp != 0) goto bad;                       /* A(1,1)=0 */
 681   INPUT_2BYTES(cinfo, tmp, return FALSE);
 682   if (tmp != 0) goto bad;                       /* A(1,2)=0 */
 683   INPUT_BYTE(cinfo, tmp, return FALSE);
 684   if (tmp != 0) goto bad;               /* F2: CENTER2=0, NORM2=0 */
 685   INPUT_2BYTES(cinfo, tmp, return FALSE);
 686   if (tmp != 1) goto bad;                       /* A(2,1)=1 */
 687   INPUT_2BYTES(cinfo, tmp, return FALSE);
 688   if (tmp != 0) goto bad;                       /* A(2,2)=0 */
 689   INPUT_BYTE(cinfo, tmp, return FALSE);
 690   if (tmp != 0) goto bad;               /* F3: CENTER3=0, NORM3=0 */
 691   INPUT_2BYTES(cinfo, tmp, return FALSE);
 692   if (tmp != 1) goto bad;                       /* A(3,1)=1 */
 693   INPUT_2BYTES(cinfo, tmp, return FALSE);
 694   if (tmp != 0) {                               /* A(3,2)=0 */
 695     bad:
 696     ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
 697   }
 698 
 699   /* OK, valid transform that we can handle. */
 700   cinfo->color_transform = JCT_SUBTRACT_GREEN;
 701 
 702   INPUT_SYNC(cinfo);
 703   return TRUE;
 704 }
 705 
 706 
 707 /*
 708  * Routines for processing APPn and COM markers.
 709  * These are either saved in memory or discarded, per application request.
 710  * APP0 and APP14 are specially checked to see if they are
 711  * JFIF and Adobe markers, respectively.
 712  */
 713 
 714 #define APP0_DATA_LEN   14      /* Length of interesting data in APP0 */
 715 #define APP14_DATA_LEN  12      /* Length of interesting data in APP14 */
 716 #define APPN_DATA_LEN   14      /* Must be the largest of the above!! */
 717 
 718 
 719 LOCAL(void)
 720 examine_app0 (j_decompress_ptr cinfo, JOCTET FAR * data,
 721               unsigned int datalen, INT32 remaining)
 722 /* Examine first few bytes from an APP0.
 723  * Take appropriate action if it is a JFIF marker.
 724  * datalen is # of bytes at data[], remaining is length of rest of marker data.
 725  */
 726 {
 727   INT32 totallen = (INT32) datalen + remaining;
 728 
 729   if (datalen >= APP0_DATA_LEN &&
 730       GETJOCTET(data[0]) == 0x4A &&
 731       GETJOCTET(data[1]) == 0x46 &&
 732       GETJOCTET(data[2]) == 0x49 &&
 733       GETJOCTET(data[3]) == 0x46 &&
 734       GETJOCTET(data[4]) == 0) {
 735     /* Found JFIF APP0 marker: save info */
 736     cinfo->saw_JFIF_marker = TRUE;
 737     cinfo->JFIF_major_version = GETJOCTET(data[5]);
 738     cinfo->JFIF_minor_version = GETJOCTET(data[6]);
 739     cinfo->density_unit = GETJOCTET(data[7]);
 740     cinfo->X_density = (GETJOCTET(data[8]) << 8) + GETJOCTET(data[9]);
 741     cinfo->Y_density = (GETJOCTET(data[10]) << 8) + GETJOCTET(data[11]);
 742     /* Check version.
 743      * Major version must be 1 or 2, anything else signals an incompatible
 744      * change.
 745      * (We used to treat this as an error, but now it's a nonfatal warning,
 746      * because some bozo at Hijaak couldn't read the spec.)
 747      * Minor version should be 0..2, but process anyway if newer.
 748      */
 749     if (cinfo->JFIF_major_version != 1 && cinfo->JFIF_major_version != 2)
 750       WARNMS2(cinfo, JWRN_JFIF_MAJOR,
 751               cinfo->JFIF_major_version, cinfo->JFIF_minor_version);
 752     /* Generate trace messages */
 753     TRACEMS5(cinfo, 1, JTRC_JFIF,
 754              cinfo->JFIF_major_version, cinfo->JFIF_minor_version,
 755              cinfo->X_density, cinfo->Y_density, cinfo->density_unit);
 756     /* Validate thumbnail dimensions and issue appropriate messages */
 757     if (GETJOCTET(data[12]) | GETJOCTET(data[13]))
 758       TRACEMS2(cinfo, 1, JTRC_JFIF_THUMBNAIL,
 759                GETJOCTET(data[12]), GETJOCTET(data[13]));
 760     totallen -= APP0_DATA_LEN;
 761     if (totallen !=
 762         ((INT32)GETJOCTET(data[12]) * (INT32)GETJOCTET(data[13]) * (INT32) 3))
 763       TRACEMS1(cinfo, 1, JTRC_JFIF_BADTHUMBNAILSIZE, (int) totallen);
 764   } else if (datalen >= 6 &&
 765       GETJOCTET(data[0]) == 0x4A &&
 766       GETJOCTET(data[1]) == 0x46 &&
 767       GETJOCTET(data[2]) == 0x58 &&
 768       GETJOCTET(data[3]) == 0x58 &&
 769       GETJOCTET(data[4]) == 0) {


1066   int c, c2;
1067   INPUT_VARS(cinfo);
1068 
1069   INPUT_BYTE(cinfo, c, return FALSE);
1070   INPUT_BYTE(cinfo, c2, return FALSE);
1071   if (c != 0xFF || c2 != (int) M_SOI)
1072     ERREXIT2(cinfo, JERR_NO_SOI, c, c2);
1073 
1074   cinfo->unread_marker = c2;
1075 
1076   INPUT_SYNC(cinfo);
1077   return TRUE;
1078 }
1079 
1080 
1081 /*
1082  * Read markers until SOS or EOI.
1083  *
1084  * Returns same codes as are defined for jpeg_consume_input:
1085  * JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.
1086  *
1087  * Note: This function may return a pseudo SOS marker (with zero
1088  * component number) for treat by input controller's consume_input.
1089  * consume_input itself should filter out (skip) the pseudo marker
1090  * after processing for the caller.
1091  */
1092 
1093 METHODDEF(int)
1094 read_markers (j_decompress_ptr cinfo)
1095 {
1096   /* Outer loop repeats once for each marker. */
1097   for (;;) {
1098     /* Collect the marker proper, unless we already did. */
1099     /* NB: first_marker() enforces the requirement that SOI appear first. */
1100     if (cinfo->unread_marker == 0) {
1101       if (! cinfo->marker->saw_SOI) {
1102         if (! first_marker(cinfo))
1103           return JPEG_SUSPENDED;
1104       } else {
1105         if (! next_marker(cinfo))
1106           return JPEG_SUSPENDED;
1107       }
1108     }
1109     /* At this point cinfo->unread_marker contains the marker code and the
1110      * input point is just past the marker proper, but before any parameters.
1111      * A suspension will cause us to return with this state still true.
1112      */
1113     switch (cinfo->unread_marker) {
1114     case M_SOI:
1115       if (! get_soi(cinfo))
1116         return JPEG_SUSPENDED;
1117       break;
1118 
1119     case M_SOF0:                /* Baseline */
1120       if (! get_sof(cinfo, TRUE, FALSE, FALSE))
1121         return JPEG_SUSPENDED;
1122       break;
1123 
1124     case M_SOF1:                /* Extended sequential, Huffman */
1125       if (! get_sof(cinfo, FALSE, FALSE, FALSE))
1126         return JPEG_SUSPENDED;
1127       break;
1128 
1129     case M_SOF2:                /* Progressive, Huffman */
1130       if (! get_sof(cinfo, FALSE, TRUE, FALSE))
1131         return JPEG_SUSPENDED;
1132       break;
1133 
1134     case M_SOF9:                /* Extended sequential, arithmetic */
1135       if (! get_sof(cinfo, FALSE, FALSE, TRUE))
1136         return JPEG_SUSPENDED;
1137       break;
1138 
1139     case M_SOF10:               /* Progressive, arithmetic */
1140       if (! get_sof(cinfo, FALSE, TRUE, TRUE))
1141         return JPEG_SUSPENDED;
1142       break;
1143 
1144     /* Currently unsupported SOFn types */
1145     case M_SOF3:                /* Lossless, Huffman */
1146     case M_SOF5:                /* Differential sequential, Huffman */
1147     case M_SOF6:                /* Differential progressive, Huffman */
1148     case M_SOF7:                /* Differential lossless, Huffman */
1149     case M_JPG:                 /* Reserved for JPEG extensions */
1150     case M_SOF11:               /* Lossless, arithmetic */
1151     case M_SOF13:               /* Differential sequential, arithmetic */
1152     case M_SOF14:               /* Differential progressive, arithmetic */
1153     case M_SOF15:               /* Differential lossless, arithmetic */
1154       ERREXIT1(cinfo, JERR_SOF_UNSUPPORTED, cinfo->unread_marker);
1155       break;
1156 
1157     case M_SOS:
1158       if (! get_sos(cinfo))
1159         return JPEG_SUSPENDED;
1160       cinfo->unread_marker = 0;      /* processed the marker */


1168     case M_DAC:
1169       if (! get_dac(cinfo))
1170         return JPEG_SUSPENDED;
1171       break;
1172 
1173     case M_DHT:
1174       if (! get_dht(cinfo))
1175         return JPEG_SUSPENDED;
1176       break;
1177 
1178     case M_DQT:
1179       if (! get_dqt(cinfo))
1180         return JPEG_SUSPENDED;
1181       break;
1182 
1183     case M_DRI:
1184       if (! get_dri(cinfo))
1185         return JPEG_SUSPENDED;
1186       break;
1187 
1188     case M_JPG8:
1189       if (! get_lse(cinfo))
1190         return JPEG_SUSPENDED;
1191       break;
1192 
1193     case M_APP0:
1194     case M_APP1:
1195     case M_APP2:
1196     case M_APP3:
1197     case M_APP4:
1198     case M_APP5:
1199     case M_APP6:
1200     case M_APP7:
1201     case M_APP8:
1202     case M_APP9:
1203     case M_APP10:
1204     case M_APP11:
1205     case M_APP12:
1206     case M_APP13:
1207     case M_APP14:
1208     case M_APP15:
1209       if (! (*((my_marker_ptr) cinfo->marker)->process_APPn[
1210                 cinfo->unread_marker - (int) M_APP0]) (cinfo))
1211         return JPEG_SUSPENDED;
1212       break;


1402   marker->pub.discarded_bytes = 0;
1403   marker->cur_marker = NULL;
1404 }
1405 
1406 
1407 /*
1408  * Initialize the marker reader module.
1409  * This is called only once, when the decompression object is created.
1410  */
1411 
1412 GLOBAL(void)
1413 jinit_marker_reader (j_decompress_ptr cinfo)
1414 {
1415   my_marker_ptr marker;
1416   int i;
1417 
1418   /* Create subobject in permanent pool */
1419   marker = (my_marker_ptr)
1420     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
1421                                 SIZEOF(my_marker_reader));
1422   cinfo->marker = &marker->pub;
1423   /* Initialize public method pointers */
1424   marker->pub.reset_marker_reader = reset_marker_reader;
1425   marker->pub.read_markers = read_markers;
1426   marker->pub.read_restart_marker = read_restart_marker;
1427   /* Initialize COM/APPn processing.
1428    * By default, we examine and then discard APP0 and APP14,
1429    * but simply discard COM and all other APPn.
1430    */
1431   marker->process_COM = skip_variable;
1432   marker->length_limit_COM = 0;
1433   for (i = 0; i < 16; i++) {
1434     marker->process_APPn[i] = skip_variable;
1435     marker->length_limit_APPn[i] = 0;
1436   }
1437   marker->process_APPn[0] = get_interesting_appn;
1438   marker->process_APPn[14] = get_interesting_appn;
1439   /* Reset marker processing state */
1440   reset_marker_reader(cinfo);
1441 }
1442 


< prev index next >