jdmarker.cpp 31 KB

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