jdmarker.c 40 KB

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