Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1406 lines
42 KiB

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