Leaked source code of windows server 2003
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.

1055 lines
30 KiB

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