Source code of Windows XP (NT5)
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.

1058 lines
30 KiB

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