jdmarker.c 41 KB

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