jdmarker.c 45 KB

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