jdmarker.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378
  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, 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 = GETJOCTET(*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) GETJOCTET(*next_input_byte++)) << 8; \
  134. MAKE_BYTE_AVAIL(cinfo,action); \
  135. bytes_in_buffer--; \
  136. V += GETJOCTET(*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. MEMZERO(&huffval[count], (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. MEMCOPY((*htblptr)->bits, bits, sizeof((*htblptr)->bits));
  387. MEMCOPY((*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,
  467. unsigned int datalen, 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. GETJOCTET(data[0]) == 0x4A &&
  476. GETJOCTET(data[1]) == 0x46 &&
  477. GETJOCTET(data[2]) == 0x49 &&
  478. GETJOCTET(data[3]) == 0x46 &&
  479. GETJOCTET(data[4]) == 0) {
  480. /* Found JFIF APP0 marker: save info */
  481. cinfo->saw_JFIF_marker = TRUE;
  482. cinfo->JFIF_major_version = GETJOCTET(data[5]);
  483. cinfo->JFIF_minor_version = GETJOCTET(data[6]);
  484. cinfo->density_unit = GETJOCTET(data[7]);
  485. cinfo->X_density = (GETJOCTET(data[8]) << 8) + GETJOCTET(data[9]);
  486. cinfo->Y_density = (GETJOCTET(data[10]) << 8) + GETJOCTET(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 (GETJOCTET(data[12]) | GETJOCTET(data[13]))
  502. TRACEMS2(cinfo, 1, JTRC_JFIF_THUMBNAIL,
  503. GETJOCTET(data[12]), GETJOCTET(data[13]));
  504. totallen -= APP0_DATA_LEN;
  505. if (totallen !=
  506. ((JLONG)GETJOCTET(data[12]) * (JLONG)GETJOCTET(data[13]) * (JLONG) 3))
  507. TRACEMS1(cinfo, 1, JTRC_JFIF_BADTHUMBNAILSIZE, (int) totallen);
  508. } else if (datalen >= 6 &&
  509. GETJOCTET(data[0]) == 0x4A &&
  510. GETJOCTET(data[1]) == 0x46 &&
  511. GETJOCTET(data[2]) == 0x58 &&
  512. GETJOCTET(data[3]) == 0x58 &&
  513. GETJOCTET(data[4]) == 0) {
  514. /* Found JFIF "JFXX" extension APP0 marker */
  515. /* The library doesn't actually do anything with these,
  516. * but we try to produce a helpful trace message.
  517. */
  518. switch (GETJOCTET(data[5])) {
  519. case 0x10:
  520. TRACEMS1(cinfo, 1, JTRC_THUMB_JPEG, (int) totallen);
  521. break;
  522. case 0x11:
  523. TRACEMS1(cinfo, 1, JTRC_THUMB_PALETTE, (int) totallen);
  524. break;
  525. case 0x13:
  526. TRACEMS1(cinfo, 1, JTRC_THUMB_RGB, (int) totallen);
  527. break;
  528. default:
  529. TRACEMS2(cinfo, 1, JTRC_JFIF_EXTENSION,
  530. GETJOCTET(data[5]), (int) totallen);
  531. break;
  532. }
  533. } else {
  534. /* Start of APP0 does not match "JFIF" or "JFXX", or too short */
  535. TRACEMS1(cinfo, 1, JTRC_APP0, (int) totallen);
  536. }
  537. }
  538. LOCAL(void)
  539. examine_app14 (j_decompress_ptr cinfo, JOCTET *data,
  540. unsigned int datalen, JLONG remaining)
  541. /* Examine first few bytes from an APP14.
  542. * Take appropriate action if it is an Adobe marker.
  543. * datalen is # of bytes at data[], remaining is length of rest of marker data.
  544. */
  545. {
  546. unsigned int version, flags0, flags1, transform;
  547. if (datalen >= APP14_DATA_LEN &&
  548. GETJOCTET(data[0]) == 0x41 &&
  549. GETJOCTET(data[1]) == 0x64 &&
  550. GETJOCTET(data[2]) == 0x6F &&
  551. GETJOCTET(data[3]) == 0x62 &&
  552. GETJOCTET(data[4]) == 0x65) {
  553. /* Found Adobe APP14 marker */
  554. version = (GETJOCTET(data[5]) << 8) + GETJOCTET(data[6]);
  555. flags0 = (GETJOCTET(data[7]) << 8) + GETJOCTET(data[8]);
  556. flags1 = (GETJOCTET(data[9]) << 8) + GETJOCTET(data[10]);
  557. transform = GETJOCTET(data[11]);
  558. TRACEMS4(cinfo, 1, JTRC_ADOBE, version, flags0, flags1, transform);
  559. cinfo->saw_Adobe_marker = TRUE;
  560. cinfo->Adobe_transform = (UINT8) transform;
  561. } else {
  562. /* Start of APP14 does not match "Adobe", or too short */
  563. TRACEMS1(cinfo, 1, JTRC_APP14, (int) (datalen + remaining));
  564. }
  565. }
  566. METHODDEF(boolean)
  567. get_interesting_appn (j_decompress_ptr cinfo)
  568. /* Process an APP0 or APP14 marker without saving it */
  569. {
  570. JLONG length;
  571. JOCTET b[APPN_DATA_LEN];
  572. unsigned int i, numtoread;
  573. INPUT_VARS(cinfo);
  574. INPUT_2BYTES(cinfo, length, return FALSE);
  575. length -= 2;
  576. /* get the interesting part of the marker data */
  577. if (length >= APPN_DATA_LEN)
  578. numtoread = APPN_DATA_LEN;
  579. else if (length > 0)
  580. numtoread = (unsigned int) length;
  581. else
  582. numtoread = 0;
  583. for (i = 0; i < numtoread; i++)
  584. INPUT_BYTE(cinfo, b[i], return FALSE);
  585. length -= numtoread;
  586. /* process it */
  587. switch (cinfo->unread_marker) {
  588. case M_APP0:
  589. examine_app0(cinfo, (JOCTET *) b, numtoread, length);
  590. break;
  591. case M_APP14:
  592. examine_app14(cinfo, (JOCTET *) b, numtoread, length);
  593. break;
  594. default:
  595. /* can't get here unless jpeg_save_markers chooses wrong processor */
  596. ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, cinfo->unread_marker);
  597. break;
  598. }
  599. /* skip any remaining data -- could be lots */
  600. INPUT_SYNC(cinfo);
  601. if (length > 0)
  602. (*cinfo->src->skip_input_data) (cinfo, (long) length);
  603. return TRUE;
  604. }
  605. #ifdef SAVE_MARKERS_SUPPORTED
  606. METHODDEF(boolean)
  607. save_marker (j_decompress_ptr cinfo)
  608. /* Save an APPn or COM marker into the marker list */
  609. {
  610. my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
  611. jpeg_saved_marker_ptr cur_marker = marker->cur_marker;
  612. unsigned int bytes_read, data_length;
  613. JOCTET *data;
  614. JLONG length = 0;
  615. INPUT_VARS(cinfo);
  616. if (cur_marker == NULL) {
  617. /* begin reading a marker */
  618. INPUT_2BYTES(cinfo, length, return FALSE);
  619. length -= 2;
  620. if (length >= 0) { /* watch out for bogus length word */
  621. /* figure out how much we want to save */
  622. unsigned int limit;
  623. if (cinfo->unread_marker == (int) M_COM)
  624. limit = marker->length_limit_COM;
  625. else
  626. limit = marker->length_limit_APPn[cinfo->unread_marker - (int) M_APP0];
  627. if ((unsigned int) length < limit)
  628. limit = (unsigned int) length;
  629. /* allocate and initialize the marker item */
  630. cur_marker = (jpeg_saved_marker_ptr)
  631. (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  632. sizeof(struct jpeg_marker_struct) + limit);
  633. cur_marker->next = NULL;
  634. cur_marker->marker = (UINT8) cinfo->unread_marker;
  635. cur_marker->original_length = (unsigned int) length;
  636. cur_marker->data_length = limit;
  637. /* data area is just beyond the jpeg_marker_struct */
  638. data = cur_marker->data = (JOCTET *) (cur_marker + 1);
  639. marker->cur_marker = cur_marker;
  640. marker->bytes_read = 0;
  641. bytes_read = 0;
  642. data_length = limit;
  643. } else {
  644. /* deal with bogus length word */
  645. bytes_read = data_length = 0;
  646. data = NULL;
  647. }
  648. } else {
  649. /* resume reading a marker */
  650. bytes_read = marker->bytes_read;
  651. data_length = cur_marker->data_length;
  652. data = cur_marker->data + bytes_read;
  653. }
  654. while (bytes_read < data_length) {
  655. INPUT_SYNC(cinfo); /* move the restart point to here */
  656. marker->bytes_read = bytes_read;
  657. /* If there's not at least one byte in buffer, suspend */
  658. MAKE_BYTE_AVAIL(cinfo, return FALSE);
  659. /* Copy bytes with reasonable rapidity */
  660. while (bytes_read < data_length && bytes_in_buffer > 0) {
  661. *data++ = *next_input_byte++;
  662. bytes_in_buffer--;
  663. bytes_read++;
  664. }
  665. }
  666. /* Done reading what we want to read */
  667. if (cur_marker != NULL) { /* will be NULL if bogus length word */
  668. /* Add new marker to end of list */
  669. if (cinfo->marker_list == NULL) {
  670. cinfo->marker_list = cur_marker;
  671. } else {
  672. jpeg_saved_marker_ptr prev = cinfo->marker_list;
  673. while (prev->next != NULL)
  674. prev = prev->next;
  675. prev->next = cur_marker;
  676. }
  677. /* Reset pointer & calc remaining data length */
  678. data = cur_marker->data;
  679. length = cur_marker->original_length - data_length;
  680. }
  681. /* Reset to initial state for next marker */
  682. marker->cur_marker = NULL;
  683. /* Process the marker if interesting; else just make a generic trace msg */
  684. switch (cinfo->unread_marker) {
  685. case M_APP0:
  686. examine_app0(cinfo, data, data_length, length);
  687. break;
  688. case M_APP14:
  689. examine_app14(cinfo, data, data_length, length);
  690. break;
  691. default:
  692. TRACEMS2(cinfo, 1, JTRC_MISC_MARKER, cinfo->unread_marker,
  693. (int) (data_length + length));
  694. break;
  695. }
  696. /* skip any remaining data -- could be lots */
  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. #endif /* SAVE_MARKERS_SUPPORTED */
  703. METHODDEF(boolean)
  704. skip_variable (j_decompress_ptr cinfo)
  705. /* Skip over an unknown or uninteresting variable-length marker */
  706. {
  707. JLONG length;
  708. INPUT_VARS(cinfo);
  709. INPUT_2BYTES(cinfo, length, return FALSE);
  710. length -= 2;
  711. TRACEMS2(cinfo, 1, JTRC_MISC_MARKER, cinfo->unread_marker, (int) length);
  712. INPUT_SYNC(cinfo); /* do before skip_input_data */
  713. if (length > 0)
  714. (*cinfo->src->skip_input_data) (cinfo, (long) length);
  715. return TRUE;
  716. }
  717. /*
  718. * Find the next JPEG marker, save it in cinfo->unread_marker.
  719. * Returns FALSE if had to suspend before reaching a marker;
  720. * in that case cinfo->unread_marker is unchanged.
  721. *
  722. * Note that the result might not be a valid marker code,
  723. * but it will never be 0 or FF.
  724. */
  725. LOCAL(boolean)
  726. next_marker (j_decompress_ptr cinfo)
  727. {
  728. int c;
  729. INPUT_VARS(cinfo);
  730. for (;;) {
  731. INPUT_BYTE(cinfo, c, return FALSE);
  732. /* Skip any non-FF bytes.
  733. * This may look a bit inefficient, but it will not occur in a valid file.
  734. * We sync after each discarded byte so that a suspending data source
  735. * can discard the byte from its buffer.
  736. */
  737. while (c != 0xFF) {
  738. cinfo->marker->discarded_bytes++;
  739. INPUT_SYNC(cinfo);
  740. INPUT_BYTE(cinfo, c, return FALSE);
  741. }
  742. /* This loop swallows any duplicate FF bytes. Extra FFs are legal as
  743. * pad bytes, so don't count them in discarded_bytes. We assume there
  744. * will not be so many consecutive FF bytes as to overflow a suspending
  745. * data source's input buffer.
  746. */
  747. do {
  748. INPUT_BYTE(cinfo, c, return FALSE);
  749. } while (c == 0xFF);
  750. if (c != 0)
  751. break; /* found a valid marker, exit loop */
  752. /* Reach here if we found a stuffed-zero data sequence (FF/00).
  753. * Discard it and loop back to try again.
  754. */
  755. cinfo->marker->discarded_bytes += 2;
  756. INPUT_SYNC(cinfo);
  757. }
  758. if (cinfo->marker->discarded_bytes != 0) {
  759. WARNMS2(cinfo, JWRN_EXTRANEOUS_DATA, cinfo->marker->discarded_bytes, c);
  760. cinfo->marker->discarded_bytes = 0;
  761. }
  762. cinfo->unread_marker = c;
  763. INPUT_SYNC(cinfo);
  764. return TRUE;
  765. }
  766. LOCAL(boolean)
  767. first_marker (j_decompress_ptr cinfo)
  768. /* Like next_marker, but used to obtain the initial SOI marker. */
  769. /* For this marker, we do not allow preceding garbage or fill; otherwise,
  770. * we might well scan an entire input file before realizing it ain't JPEG.
  771. * If an application wants to process non-JFIF files, it must seek to the
  772. * SOI before calling the JPEG library.
  773. */
  774. {
  775. int c, c2;
  776. INPUT_VARS(cinfo);
  777. INPUT_BYTE(cinfo, c, return FALSE);
  778. INPUT_BYTE(cinfo, c2, return FALSE);
  779. if (c != 0xFF || c2 != (int) M_SOI)
  780. ERREXIT2(cinfo, JERR_NO_SOI, c, c2);
  781. cinfo->unread_marker = c2;
  782. INPUT_SYNC(cinfo);
  783. return TRUE;
  784. }
  785. /*
  786. * Read markers until SOS or EOI.
  787. *
  788. * Returns same codes as are defined for jpeg_consume_input:
  789. * JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.
  790. */
  791. METHODDEF(int)
  792. read_markers (j_decompress_ptr cinfo)
  793. {
  794. /* Outer loop repeats once for each marker. */
  795. for (;;) {
  796. /* Collect the marker proper, unless we already did. */
  797. /* NB: first_marker() enforces the requirement that SOI appear first. */
  798. if (cinfo->unread_marker == 0) {
  799. if (! cinfo->marker->saw_SOI) {
  800. if (! first_marker(cinfo))
  801. return JPEG_SUSPENDED;
  802. } else {
  803. if (! next_marker(cinfo))
  804. return JPEG_SUSPENDED;
  805. }
  806. }
  807. /* At this point cinfo->unread_marker contains the marker code and the
  808. * input point is just past the marker proper, but before any parameters.
  809. * A suspension will cause us to return with this state still true.
  810. */
  811. switch (cinfo->unread_marker) {
  812. case M_SOI:
  813. if (! get_soi(cinfo))
  814. return JPEG_SUSPENDED;
  815. break;
  816. case M_SOF0: /* Baseline */
  817. case M_SOF1: /* Extended sequential, Huffman */
  818. if (! get_sof(cinfo, FALSE, FALSE))
  819. return JPEG_SUSPENDED;
  820. break;
  821. case M_SOF2: /* Progressive, Huffman */
  822. if (! get_sof(cinfo, TRUE, FALSE))
  823. return JPEG_SUSPENDED;
  824. break;
  825. case M_SOF9: /* Extended sequential, arithmetic */
  826. if (! get_sof(cinfo, FALSE, TRUE))
  827. return JPEG_SUSPENDED;
  828. break;
  829. case M_SOF10: /* Progressive, arithmetic */
  830. if (! get_sof(cinfo, TRUE, TRUE))
  831. return JPEG_SUSPENDED;
  832. break;
  833. /* Currently unsupported SOFn types */
  834. case M_SOF3: /* Lossless, Huffman */
  835. case M_SOF5: /* Differential sequential, Huffman */
  836. case M_SOF6: /* Differential progressive, Huffman */
  837. case M_SOF7: /* Differential lossless, Huffman */
  838. case M_JPG: /* Reserved for JPEG extensions */
  839. case M_SOF11: /* Lossless, arithmetic */
  840. case M_SOF13: /* Differential sequential, arithmetic */
  841. case M_SOF14: /* Differential progressive, arithmetic */
  842. case M_SOF15: /* Differential lossless, arithmetic */
  843. ERREXIT1(cinfo, JERR_SOF_UNSUPPORTED, cinfo->unread_marker);
  844. break;
  845. case M_SOS:
  846. if (! get_sos(cinfo))
  847. return JPEG_SUSPENDED;
  848. cinfo->unread_marker = 0; /* processed the marker */
  849. return JPEG_REACHED_SOS;
  850. case M_EOI:
  851. TRACEMS(cinfo, 1, JTRC_EOI);
  852. cinfo->unread_marker = 0; /* processed the marker */
  853. return JPEG_REACHED_EOI;
  854. case M_DAC:
  855. if (! get_dac(cinfo))
  856. return JPEG_SUSPENDED;
  857. break;
  858. case M_DHT:
  859. if (! get_dht(cinfo))
  860. return JPEG_SUSPENDED;
  861. break;
  862. case M_DQT:
  863. if (! get_dqt(cinfo))
  864. return JPEG_SUSPENDED;
  865. break;
  866. case M_DRI:
  867. if (! get_dri(cinfo))
  868. return JPEG_SUSPENDED;
  869. break;
  870. case M_APP0:
  871. case M_APP1:
  872. case M_APP2:
  873. case M_APP3:
  874. case M_APP4:
  875. case M_APP5:
  876. case M_APP6:
  877. case M_APP7:
  878. case M_APP8:
  879. case M_APP9:
  880. case M_APP10:
  881. case M_APP11:
  882. case M_APP12:
  883. case M_APP13:
  884. case M_APP14:
  885. case M_APP15:
  886. if (! (*((my_marker_ptr) cinfo->marker)->process_APPn[
  887. cinfo->unread_marker - (int) M_APP0]) (cinfo))
  888. return JPEG_SUSPENDED;
  889. break;
  890. case M_COM:
  891. if (! (*((my_marker_ptr) cinfo->marker)->process_COM) (cinfo))
  892. return JPEG_SUSPENDED;
  893. break;
  894. case M_RST0: /* these are all parameterless */
  895. case M_RST1:
  896. case M_RST2:
  897. case M_RST3:
  898. case M_RST4:
  899. case M_RST5:
  900. case M_RST6:
  901. case M_RST7:
  902. case M_TEM:
  903. TRACEMS1(cinfo, 1, JTRC_PARMLESS_MARKER, cinfo->unread_marker);
  904. break;
  905. case M_DNL: /* Ignore DNL ... perhaps the wrong thing */
  906. if (! skip_variable(cinfo))
  907. return JPEG_SUSPENDED;
  908. break;
  909. default: /* must be DHP, EXP, JPGn, or RESn */
  910. /* For now, we treat the reserved markers as fatal errors since they are
  911. * likely to be used to signal incompatible JPEG Part 3 extensions.
  912. * Once the JPEG 3 version-number marker is well defined, this code
  913. * ought to change!
  914. */
  915. ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, cinfo->unread_marker);
  916. break;
  917. }
  918. /* Successfully processed marker, so reset state variable */
  919. cinfo->unread_marker = 0;
  920. } /* end loop */
  921. }
  922. /*
  923. * Read a restart marker, which is expected to appear next in the datastream;
  924. * if the marker is not there, take appropriate recovery action.
  925. * Returns FALSE if suspension is required.
  926. *
  927. * This is called by the entropy decoder after it has read an appropriate
  928. * number of MCUs. cinfo->unread_marker may be nonzero if the entropy decoder
  929. * has already read a marker from the data source. Under normal conditions
  930. * cinfo->unread_marker will be reset to 0 before returning; if not reset,
  931. * it holds a marker which the decoder will be unable to read past.
  932. */
  933. METHODDEF(boolean)
  934. read_restart_marker (j_decompress_ptr cinfo)
  935. {
  936. /* Obtain a marker unless we already did. */
  937. /* Note that next_marker will complain if it skips any data. */
  938. if (cinfo->unread_marker == 0) {
  939. if (! next_marker(cinfo))
  940. return FALSE;
  941. }
  942. if (cinfo->unread_marker ==
  943. ((int) M_RST0 + cinfo->marker->next_restart_num)) {
  944. /* Normal case --- swallow the marker and let entropy decoder continue */
  945. TRACEMS1(cinfo, 3, JTRC_RST, cinfo->marker->next_restart_num);
  946. cinfo->unread_marker = 0;
  947. } else {
  948. /* Uh-oh, the restart markers have been messed up. */
  949. /* Let the data source manager determine how to resync. */
  950. if (! (*cinfo->src->resync_to_restart) (cinfo,
  951. cinfo->marker->next_restart_num))
  952. return FALSE;
  953. }
  954. /* Update next-restart state */
  955. cinfo->marker->next_restart_num = (cinfo->marker->next_restart_num + 1) & 7;
  956. return TRUE;
  957. }
  958. /*
  959. * This is the default resync_to_restart method for data source managers
  960. * to use if they don't have any better approach. Some data source managers
  961. * may be able to back up, or may have additional knowledge about the data
  962. * which permits a more intelligent recovery strategy; such managers would
  963. * presumably supply their own resync method.
  964. *
  965. * read_restart_marker calls resync_to_restart if it finds a marker other than
  966. * the restart marker it was expecting. (This code is *not* used unless
  967. * a nonzero restart interval has been declared.) cinfo->unread_marker is
  968. * the marker code actually found (might be anything, except 0 or FF).
  969. * The desired restart marker number (0..7) is passed as a parameter.
  970. * This routine is supposed to apply whatever error recovery strategy seems
  971. * appropriate in order to position the input stream to the next data segment.
  972. * Note that cinfo->unread_marker is treated as a marker appearing before
  973. * the current data-source input point; usually it should be reset to zero
  974. * before returning.
  975. * Returns FALSE if suspension is required.
  976. *
  977. * This implementation is substantially constrained by wanting to treat the
  978. * input as a data stream; this means we can't back up. Therefore, we have
  979. * only the following actions to work with:
  980. * 1. Simply discard the marker and let the entropy decoder resume at next
  981. * byte of file.
  982. * 2. Read forward until we find another marker, discarding intervening
  983. * data. (In theory we could look ahead within the current bufferload,
  984. * without having to discard data if we don't find the desired marker.
  985. * This idea is not implemented here, in part because it makes behavior
  986. * dependent on buffer size and chance buffer-boundary positions.)
  987. * 3. Leave the marker unread (by failing to zero cinfo->unread_marker).
  988. * This will cause the entropy decoder to process an empty data segment,
  989. * inserting dummy zeroes, and then we will reprocess the marker.
  990. *
  991. * #2 is appropriate if we think the desired marker lies ahead, while #3 is
  992. * appropriate if the found marker is a future restart marker (indicating
  993. * that we have missed the desired restart marker, probably because it got
  994. * corrupted).
  995. * We apply #2 or #3 if the found marker is a restart marker no more than
  996. * two counts behind or ahead of the expected one. We also apply #2 if the
  997. * found marker is not a legal JPEG marker code (it's certainly bogus data).
  998. * If the found marker is a restart marker more than 2 counts away, we do #1
  999. * (too much risk that the marker is erroneous; with luck we will be able to
  1000. * resync at some future point).
  1001. * For any valid non-restart JPEG marker, we apply #3. This keeps us from
  1002. * overrunning the end of a scan. An implementation limited to single-scan
  1003. * files might find it better to apply #2 for markers other than EOI, since
  1004. * any other marker would have to be bogus data in that case.
  1005. */
  1006. GLOBAL(boolean)
  1007. jpeg_resync_to_restart (j_decompress_ptr cinfo, int desired)
  1008. {
  1009. int marker = cinfo->unread_marker;
  1010. int action = 1;
  1011. /* Always put up a warning. */
  1012. WARNMS2(cinfo, JWRN_MUST_RESYNC, marker, desired);
  1013. /* Outer loop handles repeated decision after scanning forward. */
  1014. for (;;) {
  1015. if (marker < (int) M_SOF0)
  1016. action = 2; /* invalid marker */
  1017. else if (marker < (int) M_RST0 || marker > (int) M_RST7)
  1018. action = 3; /* valid non-restart marker */
  1019. else {
  1020. if (marker == ((int) M_RST0 + ((desired+1) & 7)) ||
  1021. marker == ((int) M_RST0 + ((desired+2) & 7)))
  1022. action = 3; /* one of the next two expected restarts */
  1023. else if (marker == ((int) M_RST0 + ((desired-1) & 7)) ||
  1024. marker == ((int) M_RST0 + ((desired-2) & 7)))
  1025. action = 2; /* a prior restart, so advance */
  1026. else
  1027. action = 1; /* desired restart or too far away */
  1028. }
  1029. TRACEMS2(cinfo, 4, JTRC_RECOVERY_ACTION, marker, action);
  1030. switch (action) {
  1031. case 1:
  1032. /* Discard marker and let entropy decoder resume processing. */
  1033. cinfo->unread_marker = 0;
  1034. return TRUE;
  1035. case 2:
  1036. /* Scan to the next marker, and repeat the decision loop. */
  1037. if (! next_marker(cinfo))
  1038. return FALSE;
  1039. marker = cinfo->unread_marker;
  1040. break;
  1041. case 3:
  1042. /* Return without advancing past this marker. */
  1043. /* Entropy decoder will be forced to process an empty segment. */
  1044. return TRUE;
  1045. }
  1046. } /* end loop */
  1047. }
  1048. /*
  1049. * Reset marker processing state to begin a fresh datastream.
  1050. */
  1051. METHODDEF(void)
  1052. reset_marker_reader (j_decompress_ptr cinfo)
  1053. {
  1054. my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
  1055. cinfo->comp_info = NULL; /* until allocated by get_sof */
  1056. cinfo->input_scan_number = 0; /* no SOS seen yet */
  1057. cinfo->unread_marker = 0; /* no pending marker */
  1058. marker->pub.saw_SOI = FALSE; /* set internal state too */
  1059. marker->pub.saw_SOF = FALSE;
  1060. marker->pub.discarded_bytes = 0;
  1061. marker->cur_marker = NULL;
  1062. }
  1063. /*
  1064. * Initialize the marker reader module.
  1065. * This is called only once, when the decompression object is created.
  1066. */
  1067. GLOBAL(void)
  1068. jinit_marker_reader (j_decompress_ptr cinfo)
  1069. {
  1070. my_marker_ptr marker;
  1071. int i;
  1072. /* Create subobject in permanent pool */
  1073. marker = (my_marker_ptr)
  1074. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
  1075. sizeof(my_marker_reader));
  1076. cinfo->marker = (struct jpeg_marker_reader *) marker;
  1077. /* Initialize public method pointers */
  1078. marker->pub.reset_marker_reader = reset_marker_reader;
  1079. marker->pub.read_markers = read_markers;
  1080. marker->pub.read_restart_marker = read_restart_marker;
  1081. /* Initialize COM/APPn processing.
  1082. * By default, we examine and then discard APP0 and APP14,
  1083. * but simply discard COM and all other APPn.
  1084. */
  1085. marker->process_COM = skip_variable;
  1086. marker->length_limit_COM = 0;
  1087. for (i = 0; i < 16; i++) {
  1088. marker->process_APPn[i] = skip_variable;
  1089. marker->length_limit_APPn[i] = 0;
  1090. }
  1091. marker->process_APPn[0] = get_interesting_appn;
  1092. marker->process_APPn[14] = get_interesting_appn;
  1093. /* Reset marker processing state */
  1094. reset_marker_reader(cinfo);
  1095. }
  1096. /*
  1097. * Control saving of COM and APPn markers into marker_list.
  1098. */
  1099. #ifdef SAVE_MARKERS_SUPPORTED
  1100. GLOBAL(void)
  1101. jpeg_save_markers (j_decompress_ptr cinfo, int marker_code,
  1102. unsigned int length_limit)
  1103. {
  1104. my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
  1105. long maxlength;
  1106. jpeg_marker_parser_method processor;
  1107. /* Length limit mustn't be larger than what we can allocate
  1108. * (should only be a concern in a 16-bit environment).
  1109. */
  1110. maxlength = cinfo->mem->max_alloc_chunk - sizeof(struct jpeg_marker_struct);
  1111. if (((long) length_limit) > maxlength)
  1112. length_limit = (unsigned int) maxlength;
  1113. /* Choose processor routine to use.
  1114. * APP0/APP14 have special requirements.
  1115. */
  1116. if (length_limit) {
  1117. processor = save_marker;
  1118. /* If saving APP0/APP14, save at least enough for our internal use. */
  1119. if (marker_code == (int) M_APP0 && length_limit < APP0_DATA_LEN)
  1120. length_limit = APP0_DATA_LEN;
  1121. else if (marker_code == (int) M_APP14 && length_limit < APP14_DATA_LEN)
  1122. length_limit = APP14_DATA_LEN;
  1123. } else {
  1124. processor = skip_variable;
  1125. /* If discarding APP0/APP14, use our regular on-the-fly processor. */
  1126. if (marker_code == (int) M_APP0 || marker_code == (int) M_APP14)
  1127. processor = get_interesting_appn;
  1128. }
  1129. if (marker_code == (int) M_COM) {
  1130. marker->process_COM = processor;
  1131. marker->length_limit_COM = length_limit;
  1132. } else if (marker_code >= (int) M_APP0 && marker_code <= (int) M_APP15) {
  1133. marker->process_APPn[marker_code - (int) M_APP0] = processor;
  1134. marker->length_limit_APPn[marker_code - (int) M_APP0] = length_limit;
  1135. } else
  1136. ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, marker_code);
  1137. }
  1138. #endif /* SAVE_MARKERS_SUPPORTED */
  1139. /*
  1140. * Install a special processing method for COM or APPn markers.
  1141. */
  1142. GLOBAL(void)
  1143. jpeg_set_marker_processor (j_decompress_ptr cinfo, int marker_code,
  1144. jpeg_marker_parser_method routine)
  1145. {
  1146. my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
  1147. if (marker_code == (int) M_COM)
  1148. marker->process_COM = routine;
  1149. else if (marker_code >= (int) M_APP0 && marker_code <= (int) M_APP15)
  1150. marker->process_APPn[marker_code - (int) M_APP0] = routine;
  1151. else
  1152. ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, marker_code);
  1153. }