jdmarker.c 41 KB

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