jdmarker.c 45 KB

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