1 /* WARNING: The type of JPEG encapsulation defined by the TIFF Version 6.0
2    specification is now totally obsolete and deprecated for new applications and
3    images. This file was was created solely in order to read unconverted images
4    still present on some users' computer systems. It will never be extended
5    to write such files. Writing new-style JPEG compressed TIFFs is implemented
6    in tif_jpeg.c.
7 
8    The code is carefully crafted to robustly read all gathered JPEG-in-TIFF
9    testfiles, and anticipate as much as possible all other... But still, it may
10    fail on some. If you encounter problems, please report them on the TIFF
11    mailing list and/or to Joris Van Damme <info@awaresystems.be>.
12 
13    Please read the file called "TIFF Technical Note #2" if you need to be
14    convinced this compression scheme is bad and breaks TIFF. That document
15    is linked to from the LibTiff site <http://www.remotesensing.org/libtiff/>
16    and from AWare Systems' TIFF section
17    <http://www.awaresystems.be/imaging/tiff.html>. It is also absorbed
18    in Adobe's specification supplements, marked "draft" up to this day, but
19    supported by the TIFF community.
20 
21    This file interfaces with Release 6B of the JPEG Library written by the
22    Independent JPEG Group. Previous versions of this file required a hack inside
23    the LibJpeg library. This version no longer requires that. Remember to
24    remove the hack if you update from the old version.
25 
26    Copyright (c) Joris Van Damme <info@awaresystems.be>
27    Copyright (c) AWare Systems <http://www.awaresystems.be/>
28 
29    The licence agreement for this file is the same as the rest of the LibTiff
30    library.
31 
32    IN NO EVENT SHALL JORIS VAN DAMME OR AWARE SYSTEMS BE LIABLE FOR
33    ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
34    OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
35    WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
36    LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
37    OF THIS SOFTWARE.
38 
39    Joris Van Damme and/or AWare Systems may be available for custom
40    development. If you like what you see, and need anything similar or related,
41    contact <info@awaresystems.be>.
42 */
43 
44 /* What is what, and what is not?
45 
46    This decoder starts with an input stream, that is essentially the JpegInterchangeFormat
47    stream, if any, followed by the strile data, if any. This stream is read in
48    OJPEGReadByte and related functions.
49 
50    It analyzes the start of this stream, until it encounters non-marker data, i.e.
51    compressed image data. Some of the header markers it sees have no actual content,
52    like the SOI marker, and APP/COM markers that really shouldn't even be there. Some
53    other markers do have content, and the valuable bits and pieces of information
54    in these markers are saved, checking all to verify that the stream is more or
55    less within expected bounds. This happens inside the OJPEGReadHeaderInfoSecStreamXxx
56    functions.
57 
58    Some OJPEG imagery contains no valid JPEG header markers. This situation is picked
59    up on if we've seen no SOF marker when we're at the start of the compressed image
60    data. In this case, the tables are read from JpegXxxTables tags, and the other
61    bits and pieces of information is initialized to its most basic value. This is
62    implemented in the OJPEGReadHeaderInfoSecTablesXxx functions.
63 
64    When this is complete, a good and valid JPEG header can be assembled, and this is
65    passed through to LibJpeg. When that's done, the remainder of the input stream, i.e.
66    the compressed image data, can be passed through unchanged. This is done in
67    OJPEGWriteStream functions.
68 
69    LibTiff rightly expects to know the subsampling values before decompression. Just like
70    in new-style JPEG-in-TIFF, though, or even more so, actually, the YCbCrsubsampling
71    tag is notoriously unreliable. To correct these tag values with the ones inside
72    the JPEG stream, the first part of the input stream is pre-scanned in
73    OJPEGSubsamplingCorrect, making no note of any other data, reporting no warnings
74    or errors, up to the point where either these values are read, or it's clear they
75    aren't there. This means that some of the data is read twice, but we feel speed
76    in correcting these values is important enough to warrant this sacrifice. Although
77    there is currently no define or other configuration mechanism to disable this behaviour,
78    the actual header scanning is build to robustly respond with error report if it
79    should encounter an uncorrected mismatch of subsampling values. See
80    OJPEGReadHeaderInfoSecStreamSof.
81 
82    The restart interval and restart markers are the most tricky part... The restart
83    interval can be specified in a tag. It can also be set inside the input JPEG stream.
84    It can be used inside the input JPEG stream. If reading from strile data, we've
85    consistently discovered the need to insert restart markers in between the different
86    striles, as is also probably the most likely interpretation of the original TIFF 6.0
87    specification. With all this setting of interval, and actual use of markers that is not
88    predictable at the time of valid JPEG header assembly, the restart thing may turn
89    out the Achilles heel of this implementation. Fortunately, most OJPEG writer vendors
90    succeed in reading back what they write, which may be the reason why we've been able
91    to discover ways that seem to work.
92 
93    Some special provision is made for planarconfig separate OJPEG files. These seem
94    to consistently contain header info, a SOS marker, a plane, SOS marker, plane, SOS,
95    and plane. This may or may not be a valid JPEG configuration, we don't know and don't
96    care. We want LibTiff to be able to access the planes individually, without huge
97    buffering inside LibJpeg, anyway. So we compose headers to feed to LibJpeg, in this
98    case, that allow us to pass a single plane such that LibJpeg sees a valid
99    single-channel JPEG stream. Locating subsequent SOS markers, and thus subsequent
100    planes, is done inside OJPEGReadSecondarySos.
101 
102    The benefit of the scheme is... that it works, basically. We know of no other that
103    does. It works without checking software tag, or otherwise going about things in an
104    OJPEG flavor specific manner. Instead, it is a single scheme, that covers the cases
105    with and without JpegInterchangeFormat, with and without striles, with part of
106    the header in JpegInterchangeFormat and remainder in first strile, etc. It is forgiving
107    and robust, may likely work with OJPEG flavors we've not seen yet, and makes most out
108    of the data.
109 
110    Another nice side-effect is that a complete JPEG single valid stream is build if
111    planarconfig is not separate (vast majority). We may one day use that to build
112    converters to JPEG, and/or to new-style JPEG compression inside TIFF.
113 
114    A disadvantage is the lack of random access to the individual striles. This is the
115    reason for much of the complicated restart-and-position stuff inside OJPEGPreDecode.
116    Applications would do well accessing all striles in order, as this will result in
117    a single sequential scan of the input stream, and no restarting of LibJpeg decoding
118    session.
119 */
120 
121 #define WIN32_LEAN_AND_MEAN
122 #define VC_EXTRALEAN
123 
124 #include "tiffiop.h"
125 #ifdef OJPEG_SUPPORT
126 
127 /* Configuration defines here are:
128  * JPEG_ENCAP_EXTERNAL: The normal way to call libjpeg, uses longjump. In some environments,
129  * 	like eg LibTiffDelphi, this is not possible. For this reason, the actual calls to
130  * 	libjpeg, with longjump stuff, are encapsulated in dedicated functions. When
131  * 	JPEG_ENCAP_EXTERNAL is defined, these encapsulating functions are declared external
132  * 	to this unit, and can be defined elsewhere to use stuff other then longjump.
133  * 	The default mode, without JPEG_ENCAP_EXTERNAL, implements the call encapsulators
134  * 	here, internally, with normal longjump.
135  * SETJMP, LONGJMP, JMP_BUF: On some machines/environments a longjump equivalent is
136  * 	conveniently available, but still it may be worthwhile to use _setjmp or sigsetjmp
137  * 	in place of plain setjmp. These macros will make it easier. It is useless
138  * 	to fiddle with these if you define JPEG_ENCAP_EXTERNAL.
139  * OJPEG_BUFFER: Define the size of the desired buffer here. Should be small enough so as to guarantee
140  * 	instant processing, optimal streaming and optimal use of processor cache, but also big
141  * 	enough so as to not result in significant call overhead. It should be at least a few
142  * 	bytes to accommodate some structures (this is verified in asserts), but it would not be
143  * 	sensible to make it this small anyway, and it should be at most 64K since it is indexed
144  * 	with uint16. We recommend 2K.
145  * EGYPTIANWALK: You could also define EGYPTIANWALK here, but it is not used anywhere and has
146  * 	absolutely no effect. That is why most people insist the EGYPTIANWALK is a bit silly.
147  */
148 
149 /* define LIBJPEG_ENCAP_EXTERNAL */
150 #define SETJMP(jbuf) setjmp(jbuf)
151 #define LONGJMP(jbuf,code) longjmp(jbuf,code)
152 #define JMP_BUF jmp_buf
153 #define OJPEG_BUFFER 2048
154 /* define EGYPTIANWALK */
155 
156 #define JPEG_MARKER_SOF0 0xC0
157 #define JPEG_MARKER_SOF1 0xC1
158 #define JPEG_MARKER_SOF3 0xC3
159 #define JPEG_MARKER_DHT 0xC4
160 #define JPEG_MARKER_RST0 0XD0
161 #define JPEG_MARKER_SOI 0xD8
162 #define JPEG_MARKER_EOI 0xD9
163 #define JPEG_MARKER_SOS 0xDA
164 #define JPEG_MARKER_DQT 0xDB
165 #define JPEG_MARKER_DRI 0xDD
166 #define JPEG_MARKER_APP0 0xE0
167 #define JPEG_MARKER_COM 0xFE
168 
169 #define FIELD_OJPEG_JPEGINTERCHANGEFORMAT (FIELD_CODEC+0)
170 #define FIELD_OJPEG_JPEGINTERCHANGEFORMATLENGTH (FIELD_CODEC+1)
171 #define FIELD_OJPEG_JPEGQTABLES (FIELD_CODEC+2)
172 #define FIELD_OJPEG_JPEGDCTABLES (FIELD_CODEC+3)
173 #define FIELD_OJPEG_JPEGACTABLES (FIELD_CODEC+4)
174 #define FIELD_OJPEG_JPEGPROC (FIELD_CODEC+5)
175 #define FIELD_OJPEG_JPEGRESTARTINTERVAL (FIELD_CODEC+6)
176 
177 static const TIFFField ojpegFields[] = {
178 	{TIFFTAG_JPEGIFOFFSET,1,1,TIFF_LONG8,0,TIFF_SETGET_UINT64,TIFF_SETGET_UNDEFINED,FIELD_OJPEG_JPEGINTERCHANGEFORMAT,TRUE,FALSE,"JpegInterchangeFormat",NULL},
179 	{TIFFTAG_JPEGIFBYTECOUNT,1,1,TIFF_LONG8,0,TIFF_SETGET_UINT64,TIFF_SETGET_UNDEFINED,FIELD_OJPEG_JPEGINTERCHANGEFORMATLENGTH,TRUE,FALSE,"JpegInterchangeFormatLength",NULL},
180 	{TIFFTAG_JPEGQTABLES,TIFF_VARIABLE2,TIFF_VARIABLE2,TIFF_LONG8,0,TIFF_SETGET_C32_UINT64,TIFF_SETGET_UNDEFINED,FIELD_OJPEG_JPEGQTABLES,FALSE,TRUE,"JpegQTables",NULL},
181 	{TIFFTAG_JPEGDCTABLES,TIFF_VARIABLE2,TIFF_VARIABLE2,TIFF_LONG8,0,TIFF_SETGET_C32_UINT64,TIFF_SETGET_UNDEFINED,FIELD_OJPEG_JPEGDCTABLES,FALSE,TRUE,"JpegDcTables",NULL},
182 	{TIFFTAG_JPEGACTABLES,TIFF_VARIABLE2,TIFF_VARIABLE2,TIFF_LONG8,0,TIFF_SETGET_C32_UINT64,TIFF_SETGET_UNDEFINED,FIELD_OJPEG_JPEGACTABLES,FALSE,TRUE,"JpegAcTables",NULL},
183 	{TIFFTAG_JPEGPROC,1,1,TIFF_SHORT,0,TIFF_SETGET_UINT16,TIFF_SETGET_UNDEFINED,FIELD_OJPEG_JPEGPROC,FALSE,FALSE,"JpegProc",NULL},
184 	{TIFFTAG_JPEGRESTARTINTERVAL,1,1,TIFF_SHORT,0,TIFF_SETGET_UINT16,TIFF_SETGET_UNDEFINED,FIELD_OJPEG_JPEGRESTARTINTERVAL,FALSE,FALSE,"JpegRestartInterval",NULL},
185 };
186 
187 #ifndef LIBJPEG_ENCAP_EXTERNAL
188 #include <setjmp.h>
189 #endif
190 
191 /* We undefine FAR to avoid conflict with JPEG definition */
192 
193 #ifdef FAR
194 #undef FAR
195 #endif
196 
197 /*
198   Libjpeg's jmorecfg.h defines INT16 and INT32, but only if XMD_H is
199   not defined.  Unfortunately, the MinGW and Borland compilers include
200   a typedef for INT32, which causes a conflict.  MSVC does not include
201   a conflicting typedef given the headers which are included.
202 */
203 #if defined(__BORLANDC__) || defined(__MINGW32__)
204 # define XMD_H 1
205 #endif
206 
207 /* Define "boolean" as unsigned char, not int, per Windows custom. */
208 #if defined(__WIN32__) && !defined(__MINGW32__)
209 # ifndef __RPCNDR_H__            /* don't conflict if rpcndr.h already read */
210    typedef unsigned char boolean;
211 # endif
212 # define HAVE_BOOLEAN            /* prevent jmorecfg.h from redefining it */
213 #endif
214 
215 #include "jpeglib.h"
216 #include "jerror.h"
217 
218 typedef struct jpeg_error_mgr jpeg_error_mgr;
219 typedef struct jpeg_common_struct jpeg_common_struct;
220 typedef struct jpeg_decompress_struct jpeg_decompress_struct;
221 typedef struct jpeg_source_mgr jpeg_source_mgr;
222 
223 typedef enum {
224 	osibsNotSetYet,
225 	osibsJpegInterchangeFormat,
226 	osibsStrile,
227 	osibsEof
228 } OJPEGStateInBufferSource;
229 
230 typedef enum {
231 	ososSoi,
232 	ososQTable0,ososQTable1,ososQTable2,ososQTable3,
233 	ososDcTable0,ososDcTable1,ososDcTable2,ososDcTable3,
234 	ososAcTable0,ososAcTable1,ososAcTable2,ososAcTable3,
235 	ososDri,
236 	ososSof,
237 	ososSos,
238 	ososCompressed,
239 	ososRst,
240 	ososEoi
241 } OJPEGStateOutState;
242 
243 typedef struct {
244 	TIFF* tif;
245         int decoder_ok;
246 	#ifndef LIBJPEG_ENCAP_EXTERNAL
247 	JMP_BUF exit_jmpbuf;
248 	#endif
249 	TIFFVGetMethod vgetparent;
250 	TIFFVSetMethod vsetparent;
251 	TIFFPrintMethod printdir;
252 	uint64 file_size;
253 	uint32 image_width;
254 	uint32 image_length;
255 	uint32 strile_width;
256 	uint32 strile_length;
257 	uint32 strile_length_total;
258 	uint8 samples_per_pixel;
259 	uint8 plane_sample_offset;
260 	uint8 samples_per_pixel_per_plane;
261 	uint64 jpeg_interchange_format;
262 	uint64 jpeg_interchange_format_length;
263 	uint8 jpeg_proc;
264 	uint8 subsamplingcorrect;
265 	uint8 subsamplingcorrect_done;
266 	uint8 subsampling_tag;
267 	uint8 subsampling_hor;
268 	uint8 subsampling_ver;
269 	uint8 subsampling_force_desubsampling_inside_decompression;
270 	uint8 qtable_offset_count;
271 	uint8 dctable_offset_count;
272 	uint8 actable_offset_count;
273 	uint64 qtable_offset[3];
274 	uint64 dctable_offset[3];
275 	uint64 actable_offset[3];
276 	uint8* qtable[4];
277 	uint8* dctable[4];
278 	uint8* actable[4];
279 	uint16 restart_interval;
280 	uint8 restart_index;
281 	uint8 sof_log;
282 	uint8 sof_marker_id;
283 	uint32 sof_x;
284 	uint32 sof_y;
285 	uint8 sof_c[3];
286 	uint8 sof_hv[3];
287 	uint8 sof_tq[3];
288 	uint8 sos_cs[3];
289 	uint8 sos_tda[3];
290 	struct {
291 		uint8 log;
292 		OJPEGStateInBufferSource in_buffer_source;
293 		uint32 in_buffer_next_strile;
294 		uint64 in_buffer_file_pos;
295 		uint64 in_buffer_file_togo;
296 	} sos_end[3];
297 	uint8 readheader_done;
298 	uint8 writeheader_done;
299 	uint16 write_cursample;
300 	uint32 write_curstrile;
301 	uint8 libjpeg_session_active;
302 	uint8 libjpeg_jpeg_query_style;
303 	jpeg_error_mgr libjpeg_jpeg_error_mgr;
304 	jpeg_decompress_struct libjpeg_jpeg_decompress_struct;
305 	jpeg_source_mgr libjpeg_jpeg_source_mgr;
306 	uint8 subsampling_convert_log;
307 	uint32 subsampling_convert_ylinelen;
308 	uint32 subsampling_convert_ylines;
309 	uint32 subsampling_convert_clinelen;
310 	uint32 subsampling_convert_clines;
311 	uint32 subsampling_convert_ybuflen;
312 	uint32 subsampling_convert_cbuflen;
313 	uint32 subsampling_convert_ycbcrbuflen;
314 	uint8* subsampling_convert_ycbcrbuf;
315 	uint8* subsampling_convert_ybuf;
316 	uint8* subsampling_convert_cbbuf;
317 	uint8* subsampling_convert_crbuf;
318 	uint32 subsampling_convert_ycbcrimagelen;
319 	uint8** subsampling_convert_ycbcrimage;
320 	uint32 subsampling_convert_clinelenout;
321 	uint32 subsampling_convert_state;
322 	uint32 bytes_per_line;   /* if the codec outputs subsampled data, a 'line' in bytes_per_line */
323 	uint32 lines_per_strile; /* and lines_per_strile means subsampling_ver desubsampled rows     */
324 	OJPEGStateInBufferSource in_buffer_source;
325 	uint32 in_buffer_next_strile;
326 	uint32 in_buffer_strile_count;
327 	uint64 in_buffer_file_pos;
328 	uint8 in_buffer_file_pos_log;
329 	uint64 in_buffer_file_togo;
330 	uint16 in_buffer_togo;
331 	uint8* in_buffer_cur;
332 	uint8 in_buffer[OJPEG_BUFFER];
333 	OJPEGStateOutState out_state;
334 	uint8 out_buffer[OJPEG_BUFFER];
335 	uint8* skip_buffer;
336 } OJPEGState;
337 
338 static int OJPEGVGetField(TIFF* tif, uint32 tag, va_list ap);
339 static int OJPEGVSetField(TIFF* tif, uint32 tag, va_list ap);
340 static void OJPEGPrintDir(TIFF* tif, FILE* fd, long flags);
341 
342 static int OJPEGFixupTags(TIFF* tif);
343 static int OJPEGSetupDecode(TIFF* tif);
344 static int OJPEGPreDecode(TIFF* tif, uint16 s);
345 static int OJPEGPreDecodeSkipRaw(TIFF* tif);
346 static int OJPEGPreDecodeSkipScanlines(TIFF* tif);
347 static int OJPEGDecode(TIFF* tif, uint8* buf, tmsize_t cc, uint16 s);
348 static int OJPEGDecodeRaw(TIFF* tif, uint8* buf, tmsize_t cc);
349 static int OJPEGDecodeScanlines(TIFF* tif, uint8* buf, tmsize_t cc);
350 static void OJPEGPostDecode(TIFF* tif, uint8* buf, tmsize_t cc);
351 static int OJPEGSetupEncode(TIFF* tif);
352 static int OJPEGPreEncode(TIFF* tif, uint16 s);
353 static int OJPEGEncode(TIFF* tif, uint8* buf, tmsize_t cc, uint16 s);
354 static int OJPEGPostEncode(TIFF* tif);
355 static void OJPEGCleanup(TIFF* tif);
356 
357 static void OJPEGSubsamplingCorrect(TIFF* tif);
358 static int OJPEGReadHeaderInfo(TIFF* tif);
359 static int OJPEGReadSecondarySos(TIFF* tif, uint16 s);
360 static int OJPEGWriteHeaderInfo(TIFF* tif);
361 static void OJPEGLibjpegSessionAbort(TIFF* tif);
362 
363 static int OJPEGReadHeaderInfoSec(TIFF* tif);
364 static int OJPEGReadHeaderInfoSecStreamDri(TIFF* tif);
365 static int OJPEGReadHeaderInfoSecStreamDqt(TIFF* tif);
366 static int OJPEGReadHeaderInfoSecStreamDht(TIFF* tif);
367 static int OJPEGReadHeaderInfoSecStreamSof(TIFF* tif, uint8 marker_id);
368 static int OJPEGReadHeaderInfoSecStreamSos(TIFF* tif);
369 static int OJPEGReadHeaderInfoSecTablesQTable(TIFF* tif);
370 static int OJPEGReadHeaderInfoSecTablesDcTable(TIFF* tif);
371 static int OJPEGReadHeaderInfoSecTablesAcTable(TIFF* tif);
372 
373 static int OJPEGReadBufferFill(OJPEGState* sp);
374 static int OJPEGReadByte(OJPEGState* sp, uint8* byte);
375 static int OJPEGReadBytePeek(OJPEGState* sp, uint8* byte);
376 static void OJPEGReadByteAdvance(OJPEGState* sp);
377 static int OJPEGReadWord(OJPEGState* sp, uint16* word);
378 static int OJPEGReadBlock(OJPEGState* sp, uint16 len, void* mem);
379 static void OJPEGReadSkip(OJPEGState* sp, uint16 len);
380 
381 static int OJPEGWriteStream(TIFF* tif, void** mem, uint32* len);
382 static void OJPEGWriteStreamSoi(TIFF* tif, void** mem, uint32* len);
383 static void OJPEGWriteStreamQTable(TIFF* tif, uint8 table_index, void** mem, uint32* len);
384 static void OJPEGWriteStreamDcTable(TIFF* tif, uint8 table_index, void** mem, uint32* len);
385 static void OJPEGWriteStreamAcTable(TIFF* tif, uint8 table_index, void** mem, uint32* len);
386 static void OJPEGWriteStreamDri(TIFF* tif, void** mem, uint32* len);
387 static void OJPEGWriteStreamSof(TIFF* tif, void** mem, uint32* len);
388 static void OJPEGWriteStreamSos(TIFF* tif, void** mem, uint32* len);
389 static int OJPEGWriteStreamCompressed(TIFF* tif, void** mem, uint32* len);
390 static void OJPEGWriteStreamRst(TIFF* tif, void** mem, uint32* len);
391 static void OJPEGWriteStreamEoi(TIFF* tif, void** mem, uint32* len);
392 
393 #ifdef LIBJPEG_ENCAP_EXTERNAL
394 extern int jpeg_create_decompress_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo);
395 extern int jpeg_read_header_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo, uint8 require_image);
396 extern int jpeg_start_decompress_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo);
397 extern int jpeg_read_scanlines_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo, void* scanlines, uint32 max_lines);
398 extern int jpeg_read_raw_data_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo, void* data, uint32 max_lines);
399 extern void jpeg_encap_unwind(TIFF* tif);
400 #else
401 static int jpeg_create_decompress_encap(OJPEGState* sp, jpeg_decompress_struct* j);
402 static int jpeg_read_header_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo, uint8 require_image);
403 static int jpeg_start_decompress_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo);
404 static int jpeg_read_scanlines_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo, void* scanlines, uint32 max_lines);
405 static int jpeg_read_raw_data_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo, void* data, uint32 max_lines);
406 static void jpeg_encap_unwind(TIFF* tif);
407 #endif
408 
409 static void OJPEGLibjpegJpegErrorMgrOutputMessage(jpeg_common_struct* cinfo);
410 static void OJPEGLibjpegJpegErrorMgrErrorExit(jpeg_common_struct* cinfo);
411 static void OJPEGLibjpegJpegSourceMgrInitSource(jpeg_decompress_struct* cinfo);
412 static boolean OJPEGLibjpegJpegSourceMgrFillInputBuffer(jpeg_decompress_struct* cinfo);
413 static void OJPEGLibjpegJpegSourceMgrSkipInputData(jpeg_decompress_struct* cinfo, long num_bytes);
414 static boolean OJPEGLibjpegJpegSourceMgrResyncToRestart(jpeg_decompress_struct* cinfo, int desired);
415 static void OJPEGLibjpegJpegSourceMgrTermSource(jpeg_decompress_struct* cinfo);
416 
417 int
TIFFInitOJPEG(TIFF * tif,int scheme)418 TIFFInitOJPEG(TIFF* tif, int scheme)
419 {
420 	static const char module[]="TIFFInitOJPEG";
421 	OJPEGState* sp;
422 
423 	assert(scheme==COMPRESSION_OJPEG);
424 
425         /*
426 	 * Merge codec-specific tag information.
427 	 */
428 	if (!_TIFFMergeFields(tif, ojpegFields, TIFFArrayCount(ojpegFields))) {
429 		TIFFErrorExt(tif->tif_clientdata, module,
430 		    "Merging Old JPEG codec-specific tags failed");
431 		return 0;
432 	}
433 
434 	/* state block */
435 	sp=_TIFFmalloc(sizeof(OJPEGState));
436 	if (sp==NULL)
437 	{
438 		TIFFErrorExt(tif->tif_clientdata,module,"No space for OJPEG state block");
439 		return(0);
440 	}
441 	_TIFFmemset(sp,0,sizeof(OJPEGState));
442 	sp->tif=tif;
443 	sp->jpeg_proc=1;
444 	sp->subsampling_hor=2;
445 	sp->subsampling_ver=2;
446 	TIFFSetField(tif,TIFFTAG_YCBCRSUBSAMPLING,2,2);
447 	/* tif codec methods */
448 	tif->tif_fixuptags=OJPEGFixupTags;
449 	tif->tif_setupdecode=OJPEGSetupDecode;
450 	tif->tif_predecode=OJPEGPreDecode;
451 	tif->tif_postdecode=OJPEGPostDecode;
452 	tif->tif_decoderow=OJPEGDecode;
453 	tif->tif_decodestrip=OJPEGDecode;
454 	tif->tif_decodetile=OJPEGDecode;
455 	tif->tif_setupencode=OJPEGSetupEncode;
456 	tif->tif_preencode=OJPEGPreEncode;
457 	tif->tif_postencode=OJPEGPostEncode;
458 	tif->tif_encoderow=OJPEGEncode;
459 	tif->tif_encodestrip=OJPEGEncode;
460 	tif->tif_encodetile=OJPEGEncode;
461 	tif->tif_cleanup=OJPEGCleanup;
462 	tif->tif_data=(uint8*)sp;
463 	/* tif tag methods */
464 	sp->vgetparent=tif->tif_tagmethods.vgetfield;
465 	tif->tif_tagmethods.vgetfield=OJPEGVGetField;
466 	sp->vsetparent=tif->tif_tagmethods.vsetfield;
467 	tif->tif_tagmethods.vsetfield=OJPEGVSetField;
468 	sp->printdir=tif->tif_tagmethods.printdir;
469 	tif->tif_tagmethods.printdir=OJPEGPrintDir;
470 	/* Some OJPEG files don't have strip or tile offsets or bytecounts tags.
471 	   Some others do, but have totally meaningless or corrupt values
472 	   in these tags. In these cases, the JpegInterchangeFormat stream is
473 	   reliable. In any case, this decoder reads the compressed data itself,
474 	   from the most reliable locations, and we need to notify encapsulating
475 	   LibTiff not to read raw strips or tiles for us. */
476 	tif->tif_flags|=TIFF_NOREADRAW;
477 	return(1);
478 }
479 
480 static int
OJPEGVGetField(TIFF * tif,uint32 tag,va_list ap)481 OJPEGVGetField(TIFF* tif, uint32 tag, va_list ap)
482 {
483 	OJPEGState* sp=(OJPEGState*)tif->tif_data;
484 	switch(tag)
485 	{
486 		case TIFFTAG_JPEGIFOFFSET:
487 			*va_arg(ap,uint64*)=(uint64)sp->jpeg_interchange_format;
488 			break;
489 		case TIFFTAG_JPEGIFBYTECOUNT:
490 			*va_arg(ap,uint64*)=(uint64)sp->jpeg_interchange_format_length;
491 			break;
492 		case TIFFTAG_YCBCRSUBSAMPLING:
493 			if (sp->subsamplingcorrect_done==0)
494 				OJPEGSubsamplingCorrect(tif);
495 			*va_arg(ap,uint16*)=(uint16)sp->subsampling_hor;
496 			*va_arg(ap,uint16*)=(uint16)sp->subsampling_ver;
497 			break;
498 		case TIFFTAG_JPEGQTABLES:
499 			*va_arg(ap,uint32*)=(uint32)sp->qtable_offset_count;
500 			*va_arg(ap,void**)=(void*)sp->qtable_offset;
501 			break;
502 		case TIFFTAG_JPEGDCTABLES:
503 			*va_arg(ap,uint32*)=(uint32)sp->dctable_offset_count;
504 			*va_arg(ap,void**)=(void*)sp->dctable_offset;
505 			break;
506 		case TIFFTAG_JPEGACTABLES:
507 			*va_arg(ap,uint32*)=(uint32)sp->actable_offset_count;
508 			*va_arg(ap,void**)=(void*)sp->actable_offset;
509 			break;
510 		case TIFFTAG_JPEGPROC:
511 			*va_arg(ap,uint16*)=(uint16)sp->jpeg_proc;
512 			break;
513 		case TIFFTAG_JPEGRESTARTINTERVAL:
514 			*va_arg(ap,uint16*)=sp->restart_interval;
515 			break;
516 		default:
517 			return (*sp->vgetparent)(tif,tag,ap);
518 	}
519 	return (1);
520 }
521 
522 static int
OJPEGVSetField(TIFF * tif,uint32 tag,va_list ap)523 OJPEGVSetField(TIFF* tif, uint32 tag, va_list ap)
524 {
525 	static const char module[]="OJPEGVSetField";
526 	OJPEGState* sp=(OJPEGState*)tif->tif_data;
527 	uint32 ma;
528 	uint64* mb;
529 	uint32 n;
530 	const TIFFField* fip;
531 
532 	switch(tag)
533 	{
534 		case TIFFTAG_JPEGIFOFFSET:
535 			sp->jpeg_interchange_format=(uint64)va_arg(ap,uint64);
536 			break;
537 		case TIFFTAG_JPEGIFBYTECOUNT:
538 			sp->jpeg_interchange_format_length=(uint64)va_arg(ap,uint64);
539 			break;
540 		case TIFFTAG_YCBCRSUBSAMPLING:
541 			sp->subsampling_tag=1;
542 			sp->subsampling_hor=(uint8)va_arg(ap,uint16_vap);
543 			sp->subsampling_ver=(uint8)va_arg(ap,uint16_vap);
544 			tif->tif_dir.td_ycbcrsubsampling[0]=sp->subsampling_hor;
545 			tif->tif_dir.td_ycbcrsubsampling[1]=sp->subsampling_ver;
546 			break;
547 		case TIFFTAG_JPEGQTABLES:
548 			ma=(uint32)va_arg(ap,uint32);
549 			if (ma!=0)
550 			{
551 				if (ma>3)
552 				{
553 					TIFFErrorExt(tif->tif_clientdata,module,"JpegQTables tag has incorrect count");
554 					return(0);
555 				}
556 				sp->qtable_offset_count=(uint8)ma;
557 				mb=(uint64*)va_arg(ap,uint64*);
558 				for (n=0; n<ma; n++)
559 					sp->qtable_offset[n]=mb[n];
560 			}
561 			break;
562 		case TIFFTAG_JPEGDCTABLES:
563 			ma=(uint32)va_arg(ap,uint32);
564 			if (ma!=0)
565 			{
566 				if (ma>3)
567 				{
568 					TIFFErrorExt(tif->tif_clientdata,module,"JpegDcTables tag has incorrect count");
569 					return(0);
570 				}
571 				sp->dctable_offset_count=(uint8)ma;
572 				mb=(uint64*)va_arg(ap,uint64*);
573 				for (n=0; n<ma; n++)
574 					sp->dctable_offset[n]=mb[n];
575 			}
576 			break;
577 		case TIFFTAG_JPEGACTABLES:
578 			ma=(uint32)va_arg(ap,uint32);
579 			if (ma!=0)
580 			{
581 				if (ma>3)
582 				{
583 					TIFFErrorExt(tif->tif_clientdata,module,"JpegAcTables tag has incorrect count");
584 					return(0);
585 				}
586 				sp->actable_offset_count=(uint8)ma;
587 				mb=(uint64*)va_arg(ap,uint64*);
588 				for (n=0; n<ma; n++)
589 					sp->actable_offset[n]=mb[n];
590 			}
591 			break;
592 		case TIFFTAG_JPEGPROC:
593 			sp->jpeg_proc=(uint8)va_arg(ap,uint16_vap);
594 			break;
595 		case TIFFTAG_JPEGRESTARTINTERVAL:
596 			sp->restart_interval=(uint16)va_arg(ap,uint16_vap);
597 			break;
598 		default:
599 			return (*sp->vsetparent)(tif,tag,ap);
600 	}
601 	fip = TIFFFieldWithTag(tif,tag);
602 	if( fip == NULL ) /* shouldn't happen */
603 	    return(0);
604 	TIFFSetFieldBit(tif,fip->field_bit);
605 	tif->tif_flags|=TIFF_DIRTYDIRECT;
606 	return(1);
607 }
608 
609 static void
OJPEGPrintDir(TIFF * tif,FILE * fd,long flags)610 OJPEGPrintDir(TIFF* tif, FILE* fd, long flags)
611 {
612 	OJPEGState* sp=(OJPEGState*)tif->tif_data;
613 	uint8 m;
614 	(void)flags;
615 	assert(sp!=NULL);
616 	if (TIFFFieldSet(tif,FIELD_OJPEG_JPEGINTERCHANGEFORMAT))
617 		fprintf(fd,"  JpegInterchangeFormat: " TIFF_UINT64_FORMAT "\n",(TIFF_UINT64_T)sp->jpeg_interchange_format);
618 	if (TIFFFieldSet(tif,FIELD_OJPEG_JPEGINTERCHANGEFORMATLENGTH))
619 		fprintf(fd,"  JpegInterchangeFormatLength: " TIFF_UINT64_FORMAT "\n",(TIFF_UINT64_T)sp->jpeg_interchange_format_length);
620 	if (TIFFFieldSet(tif,FIELD_OJPEG_JPEGQTABLES))
621 	{
622 		fprintf(fd,"  JpegQTables:");
623 		for (m=0; m<sp->qtable_offset_count; m++)
624 			fprintf(fd," " TIFF_UINT64_FORMAT,(TIFF_UINT64_T)sp->qtable_offset[m]);
625 		fprintf(fd,"\n");
626 	}
627 	if (TIFFFieldSet(tif,FIELD_OJPEG_JPEGDCTABLES))
628 	{
629 		fprintf(fd,"  JpegDcTables:");
630 		for (m=0; m<sp->dctable_offset_count; m++)
631 			fprintf(fd," " TIFF_UINT64_FORMAT,(TIFF_UINT64_T)sp->dctable_offset[m]);
632 		fprintf(fd,"\n");
633 	}
634 	if (TIFFFieldSet(tif,FIELD_OJPEG_JPEGACTABLES))
635 	{
636 		fprintf(fd,"  JpegAcTables:");
637 		for (m=0; m<sp->actable_offset_count; m++)
638 			fprintf(fd," " TIFF_UINT64_FORMAT,(TIFF_UINT64_T)sp->actable_offset[m]);
639 		fprintf(fd,"\n");
640 	}
641 	if (TIFFFieldSet(tif,FIELD_OJPEG_JPEGPROC))
642 		fprintf(fd,"  JpegProc: %u\n",(unsigned int)sp->jpeg_proc);
643 	if (TIFFFieldSet(tif,FIELD_OJPEG_JPEGRESTARTINTERVAL))
644 		fprintf(fd,"  JpegRestartInterval: %u\n",(unsigned int)sp->restart_interval);
645 	if (sp->printdir)
646 		(*sp->printdir)(tif, fd, flags);
647 }
648 
649 static int
OJPEGFixupTags(TIFF * tif)650 OJPEGFixupTags(TIFF* tif)
651 {
652 	(void) tif;
653 	return(1);
654 }
655 
656 static int
OJPEGSetupDecode(TIFF * tif)657 OJPEGSetupDecode(TIFF* tif)
658 {
659 	static const char module[]="OJPEGSetupDecode";
660 	TIFFWarningExt(tif->tif_clientdata,module,"Depreciated and troublesome old-style JPEG compression mode, please convert to new-style JPEG compression and notify vendor of writing software");
661 	return(1);
662 }
663 
664 static int
OJPEGPreDecode(TIFF * tif,uint16 s)665 OJPEGPreDecode(TIFF* tif, uint16 s)
666 {
667 	OJPEGState* sp=(OJPEGState*)tif->tif_data;
668 	uint32 m;
669 	if (sp->subsamplingcorrect_done==0)
670 		OJPEGSubsamplingCorrect(tif);
671 	if (sp->readheader_done==0)
672 	{
673 		if (OJPEGReadHeaderInfo(tif)==0)
674 			return(0);
675 	}
676 	if (sp->sos_end[s].log==0)
677 	{
678 		if (OJPEGReadSecondarySos(tif,s)==0)
679 			return(0);
680 	}
681 	if isTiled(tif)
682 		m=tif->tif_curtile;
683 	else
684 		m=tif->tif_curstrip;
685 	if ((sp->writeheader_done!=0) && ((sp->write_cursample!=s) || (sp->write_curstrile>m)))
686 	{
687 		if (sp->libjpeg_session_active!=0)
688 			OJPEGLibjpegSessionAbort(tif);
689 		sp->writeheader_done=0;
690 	}
691 	if (sp->writeheader_done==0)
692 	{
693 		sp->plane_sample_offset=(uint8)s;
694 		sp->write_cursample=s;
695 		sp->write_curstrile=s*tif->tif_dir.td_stripsperimage;
696 		if ((sp->in_buffer_file_pos_log==0) ||
697 		    (sp->in_buffer_file_pos-sp->in_buffer_togo!=sp->sos_end[s].in_buffer_file_pos))
698 		{
699 			sp->in_buffer_source=sp->sos_end[s].in_buffer_source;
700 			sp->in_buffer_next_strile=sp->sos_end[s].in_buffer_next_strile;
701 			sp->in_buffer_file_pos=sp->sos_end[s].in_buffer_file_pos;
702 			sp->in_buffer_file_pos_log=0;
703 			sp->in_buffer_file_togo=sp->sos_end[s].in_buffer_file_togo;
704 			sp->in_buffer_togo=0;
705 			sp->in_buffer_cur=0;
706 		}
707 		if (OJPEGWriteHeaderInfo(tif)==0)
708 			return(0);
709 	}
710 	while (sp->write_curstrile<m)
711 	{
712 		if (sp->libjpeg_jpeg_query_style==0)
713 		{
714 			if (OJPEGPreDecodeSkipRaw(tif)==0)
715 				return(0);
716 		}
717 		else
718 		{
719 			if (OJPEGPreDecodeSkipScanlines(tif)==0)
720 				return(0);
721 		}
722 		sp->write_curstrile++;
723 	}
724 	sp->decoder_ok = 1;
725 	return(1);
726 }
727 
728 static int
OJPEGPreDecodeSkipRaw(TIFF * tif)729 OJPEGPreDecodeSkipRaw(TIFF* tif)
730 {
731 	OJPEGState* sp=(OJPEGState*)tif->tif_data;
732 	uint32 m;
733 	m=sp->lines_per_strile;
734 	if (sp->subsampling_convert_state!=0)
735 	{
736 		if (sp->subsampling_convert_clines-sp->subsampling_convert_state>=m)
737 		{
738 			sp->subsampling_convert_state+=m;
739 			if (sp->subsampling_convert_state==sp->subsampling_convert_clines)
740 				sp->subsampling_convert_state=0;
741 			return(1);
742 		}
743 		m-=sp->subsampling_convert_clines-sp->subsampling_convert_state;
744 		sp->subsampling_convert_state=0;
745 	}
746 	while (m>=sp->subsampling_convert_clines)
747 	{
748 		if (jpeg_read_raw_data_encap(sp,&(sp->libjpeg_jpeg_decompress_struct),sp->subsampling_convert_ycbcrimage,sp->subsampling_ver*8)==0)
749 			return(0);
750 		m-=sp->subsampling_convert_clines;
751 	}
752 	if (m>0)
753 	{
754 		if (jpeg_read_raw_data_encap(sp,&(sp->libjpeg_jpeg_decompress_struct),sp->subsampling_convert_ycbcrimage,sp->subsampling_ver*8)==0)
755 			return(0);
756 		sp->subsampling_convert_state=m;
757 	}
758 	return(1);
759 }
760 
761 static int
OJPEGPreDecodeSkipScanlines(TIFF * tif)762 OJPEGPreDecodeSkipScanlines(TIFF* tif)
763 {
764 	static const char module[]="OJPEGPreDecodeSkipScanlines";
765 	OJPEGState* sp=(OJPEGState*)tif->tif_data;
766 	uint32 m;
767 	if (sp->skip_buffer==NULL)
768 	{
769 		sp->skip_buffer=_TIFFmalloc(sp->bytes_per_line);
770 		if (sp->skip_buffer==NULL)
771 		{
772 			TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
773 			return(0);
774 		}
775 	}
776 	for (m=0; m<sp->lines_per_strile; m++)
777 	{
778 		if (jpeg_read_scanlines_encap(sp,&(sp->libjpeg_jpeg_decompress_struct),&sp->skip_buffer,1)==0)
779 			return(0);
780 	}
781 	return(1);
782 }
783 
784 static int
OJPEGDecode(TIFF * tif,uint8 * buf,tmsize_t cc,uint16 s)785 OJPEGDecode(TIFF* tif, uint8* buf, tmsize_t cc, uint16 s)
786 {
787         static const char module[]="OJPEGDecode";
788 	OJPEGState* sp=(OJPEGState*)tif->tif_data;
789 	(void)s;
790         if( !sp->decoder_ok )
791         {
792             TIFFErrorExt(tif->tif_clientdata,module,"Cannot decode: decoder not correctly initialized");
793             return 0;
794         }
795 	if (sp->libjpeg_jpeg_query_style==0)
796 	{
797 		if (OJPEGDecodeRaw(tif,buf,cc)==0)
798 			return(0);
799 	}
800 	else
801 	{
802 		if (OJPEGDecodeScanlines(tif,buf,cc)==0)
803 			return(0);
804 	}
805 	return(1);
806 }
807 
808 static int
OJPEGDecodeRaw(TIFF * tif,uint8 * buf,tmsize_t cc)809 OJPEGDecodeRaw(TIFF* tif, uint8* buf, tmsize_t cc)
810 {
811 	static const char module[]="OJPEGDecodeRaw";
812 	OJPEGState* sp=(OJPEGState*)tif->tif_data;
813 	uint8* m;
814 	tmsize_t n;
815 	uint8* oy;
816 	uint8* ocb;
817 	uint8* ocr;
818 	uint8* p;
819 	uint32 q;
820 	uint8* r;
821 	uint8 sx,sy;
822 	if (cc%sp->bytes_per_line!=0)
823 	{
824 		TIFFErrorExt(tif->tif_clientdata,module,"Fractional scanline not read");
825 		return(0);
826 	}
827 	assert(cc>0);
828 	m=buf;
829 	n=cc;
830 	do
831 	{
832 		if (sp->subsampling_convert_state==0)
833 		{
834 			if (jpeg_read_raw_data_encap(sp,&(sp->libjpeg_jpeg_decompress_struct),sp->subsampling_convert_ycbcrimage,sp->subsampling_ver*8)==0)
835 				return(0);
836 		}
837 		oy=sp->subsampling_convert_ybuf+sp->subsampling_convert_state*sp->subsampling_ver*sp->subsampling_convert_ylinelen;
838 		ocb=sp->subsampling_convert_cbbuf+sp->subsampling_convert_state*sp->subsampling_convert_clinelen;
839 		ocr=sp->subsampling_convert_crbuf+sp->subsampling_convert_state*sp->subsampling_convert_clinelen;
840 		p=m;
841 		for (q=0; q<sp->subsampling_convert_clinelenout; q++)
842 		{
843 			r=oy;
844 			for (sy=0; sy<sp->subsampling_ver; sy++)
845 			{
846 				for (sx=0; sx<sp->subsampling_hor; sx++)
847 					*p++=*r++;
848 				r+=sp->subsampling_convert_ylinelen-sp->subsampling_hor;
849 			}
850 			oy+=sp->subsampling_hor;
851 			*p++=*ocb++;
852 			*p++=*ocr++;
853 		}
854 		sp->subsampling_convert_state++;
855 		if (sp->subsampling_convert_state==sp->subsampling_convert_clines)
856 			sp->subsampling_convert_state=0;
857 		m+=sp->bytes_per_line;
858 		n-=sp->bytes_per_line;
859 	} while(n>0);
860 	return(1);
861 }
862 
863 static int
OJPEGDecodeScanlines(TIFF * tif,uint8 * buf,tmsize_t cc)864 OJPEGDecodeScanlines(TIFF* tif, uint8* buf, tmsize_t cc)
865 {
866 	static const char module[]="OJPEGDecodeScanlines";
867 	OJPEGState* sp=(OJPEGState*)tif->tif_data;
868 	uint8* m;
869 	tmsize_t n;
870 	if (cc%sp->bytes_per_line!=0)
871 	{
872 		TIFFErrorExt(tif->tif_clientdata,module,"Fractional scanline not read");
873 		return(0);
874 	}
875 	assert(cc>0);
876 	m=buf;
877 	n=cc;
878 	do
879 	{
880 		if (jpeg_read_scanlines_encap(sp,&(sp->libjpeg_jpeg_decompress_struct),&m,1)==0)
881 			return(0);
882 		m+=sp->bytes_per_line;
883 		n-=sp->bytes_per_line;
884 	} while(n>0);
885 	return(1);
886 }
887 
888 static void
OJPEGPostDecode(TIFF * tif,uint8 * buf,tmsize_t cc)889 OJPEGPostDecode(TIFF* tif, uint8* buf, tmsize_t cc)
890 {
891 	OJPEGState* sp=(OJPEGState*)tif->tif_data;
892 	(void)buf;
893 	(void)cc;
894 	sp->write_curstrile++;
895 	if (sp->write_curstrile%tif->tif_dir.td_stripsperimage==0)
896 	{
897 		assert(sp->libjpeg_session_active!=0);
898 		OJPEGLibjpegSessionAbort(tif);
899 		sp->writeheader_done=0;
900 	}
901 }
902 
903 static int
OJPEGSetupEncode(TIFF * tif)904 OJPEGSetupEncode(TIFF* tif)
905 {
906 	static const char module[]="OJPEGSetupEncode";
907 	TIFFErrorExt(tif->tif_clientdata,module,"OJPEG encoding not supported; use new-style JPEG compression instead");
908 	return(0);
909 }
910 
911 static int
OJPEGPreEncode(TIFF * tif,uint16 s)912 OJPEGPreEncode(TIFF* tif, uint16 s)
913 {
914 	static const char module[]="OJPEGPreEncode";
915 	(void)s;
916 	TIFFErrorExt(tif->tif_clientdata,module,"OJPEG encoding not supported; use new-style JPEG compression instead");
917 	return(0);
918 }
919 
920 static int
OJPEGEncode(TIFF * tif,uint8 * buf,tmsize_t cc,uint16 s)921 OJPEGEncode(TIFF* tif, uint8* buf, tmsize_t cc, uint16 s)
922 {
923 	static const char module[]="OJPEGEncode";
924 	(void)buf;
925 	(void)cc;
926 	(void)s;
927 	TIFFErrorExt(tif->tif_clientdata,module,"OJPEG encoding not supported; use new-style JPEG compression instead");
928 	return(0);
929 }
930 
931 static int
OJPEGPostEncode(TIFF * tif)932 OJPEGPostEncode(TIFF* tif)
933 {
934 	static const char module[]="OJPEGPostEncode";
935 	TIFFErrorExt(tif->tif_clientdata,module,"OJPEG encoding not supported; use new-style JPEG compression instead");
936 	return(0);
937 }
938 
939 static void
OJPEGCleanup(TIFF * tif)940 OJPEGCleanup(TIFF* tif)
941 {
942 	OJPEGState* sp=(OJPEGState*)tif->tif_data;
943 	if (sp!=0)
944 	{
945 		tif->tif_tagmethods.vgetfield=sp->vgetparent;
946 		tif->tif_tagmethods.vsetfield=sp->vsetparent;
947 		tif->tif_tagmethods.printdir=sp->printdir;
948 		if (sp->qtable[0]!=0)
949 			_TIFFfree(sp->qtable[0]);
950 		if (sp->qtable[1]!=0)
951 			_TIFFfree(sp->qtable[1]);
952 		if (sp->qtable[2]!=0)
953 			_TIFFfree(sp->qtable[2]);
954 		if (sp->qtable[3]!=0)
955 			_TIFFfree(sp->qtable[3]);
956 		if (sp->dctable[0]!=0)
957 			_TIFFfree(sp->dctable[0]);
958 		if (sp->dctable[1]!=0)
959 			_TIFFfree(sp->dctable[1]);
960 		if (sp->dctable[2]!=0)
961 			_TIFFfree(sp->dctable[2]);
962 		if (sp->dctable[3]!=0)
963 			_TIFFfree(sp->dctable[3]);
964 		if (sp->actable[0]!=0)
965 			_TIFFfree(sp->actable[0]);
966 		if (sp->actable[1]!=0)
967 			_TIFFfree(sp->actable[1]);
968 		if (sp->actable[2]!=0)
969 			_TIFFfree(sp->actable[2]);
970 		if (sp->actable[3]!=0)
971 			_TIFFfree(sp->actable[3]);
972 		if (sp->libjpeg_session_active!=0)
973 			OJPEGLibjpegSessionAbort(tif);
974 		if (sp->subsampling_convert_ycbcrbuf!=0)
975 			_TIFFfree(sp->subsampling_convert_ycbcrbuf);
976 		if (sp->subsampling_convert_ycbcrimage!=0)
977 			_TIFFfree(sp->subsampling_convert_ycbcrimage);
978 		if (sp->skip_buffer!=0)
979 			_TIFFfree(sp->skip_buffer);
980 		_TIFFfree(sp);
981 		tif->tif_data=NULL;
982 		_TIFFSetDefaultCompressionState(tif);
983 	}
984 }
985 
986 static void
OJPEGSubsamplingCorrect(TIFF * tif)987 OJPEGSubsamplingCorrect(TIFF* tif)
988 {
989 	static const char module[]="OJPEGSubsamplingCorrect";
990 	OJPEGState* sp=(OJPEGState*)tif->tif_data;
991 	uint8 mh;
992 	uint8 mv;
993         _TIFFFillStriles( tif );
994 
995 	assert(sp->subsamplingcorrect_done==0);
996 	if ((tif->tif_dir.td_samplesperpixel!=3) || ((tif->tif_dir.td_photometric!=PHOTOMETRIC_YCBCR) &&
997 	    (tif->tif_dir.td_photometric!=PHOTOMETRIC_ITULAB)))
998 	{
999 		if (sp->subsampling_tag!=0)
1000 			TIFFWarningExt(tif->tif_clientdata,module,"Subsampling tag not appropriate for this Photometric and/or SamplesPerPixel");
1001 		sp->subsampling_hor=1;
1002 		sp->subsampling_ver=1;
1003 		sp->subsampling_force_desubsampling_inside_decompression=0;
1004 	}
1005 	else
1006 	{
1007 		sp->subsamplingcorrect_done=1;
1008 		mh=sp->subsampling_hor;
1009 		mv=sp->subsampling_ver;
1010 		sp->subsamplingcorrect=1;
1011 		OJPEGReadHeaderInfoSec(tif);
1012 		if (sp->subsampling_force_desubsampling_inside_decompression!=0)
1013 		{
1014 			sp->subsampling_hor=1;
1015 			sp->subsampling_ver=1;
1016 		}
1017 		sp->subsamplingcorrect=0;
1018 		if (((sp->subsampling_hor!=mh) || (sp->subsampling_ver!=mv)) && (sp->subsampling_force_desubsampling_inside_decompression==0))
1019 		{
1020 			if (sp->subsampling_tag==0)
1021 				TIFFWarningExt(tif->tif_clientdata,module,"Subsampling tag is not set, yet subsampling inside JPEG data [%d,%d] does not match default values [2,2]; assuming subsampling inside JPEG data is correct",sp->subsampling_hor,sp->subsampling_ver);
1022 			else
1023 				TIFFWarningExt(tif->tif_clientdata,module,"Subsampling inside JPEG data [%d,%d] does not match subsampling tag values [%d,%d]; assuming subsampling inside JPEG data is correct",sp->subsampling_hor,sp->subsampling_ver,mh,mv);
1024 		}
1025 		if (sp->subsampling_force_desubsampling_inside_decompression!=0)
1026 		{
1027 			if (sp->subsampling_tag==0)
1028 				TIFFWarningExt(tif->tif_clientdata,module,"Subsampling tag is not set, yet subsampling inside JPEG data does not match default values [2,2] (nor any other values allowed in TIFF); assuming subsampling inside JPEG data is correct and desubsampling inside JPEG decompression");
1029 			else
1030 				TIFFWarningExt(tif->tif_clientdata,module,"Subsampling inside JPEG data does not match subsampling tag values [%d,%d] (nor any other values allowed in TIFF); assuming subsampling inside JPEG data is correct and desubsampling inside JPEG decompression",mh,mv);
1031 		}
1032 		if (sp->subsampling_force_desubsampling_inside_decompression==0)
1033 		{
1034 			if (sp->subsampling_hor<sp->subsampling_ver)
1035 				TIFFWarningExt(tif->tif_clientdata,module,"Subsampling values [%d,%d] are not allowed in TIFF",sp->subsampling_hor,sp->subsampling_ver);
1036 		}
1037 	}
1038 	sp->subsamplingcorrect_done=1;
1039 }
1040 
1041 static int
OJPEGReadHeaderInfo(TIFF * tif)1042 OJPEGReadHeaderInfo(TIFF* tif)
1043 {
1044 	static const char module[]="OJPEGReadHeaderInfo";
1045 	OJPEGState* sp=(OJPEGState*)tif->tif_data;
1046 	assert(sp->readheader_done==0);
1047 	sp->image_width=tif->tif_dir.td_imagewidth;
1048 	sp->image_length=tif->tif_dir.td_imagelength;
1049 	if isTiled(tif)
1050 	{
1051 		sp->strile_width=tif->tif_dir.td_tilewidth;
1052 		sp->strile_length=tif->tif_dir.td_tilelength;
1053 		sp->strile_length_total=((sp->image_length+sp->strile_length-1)/sp->strile_length)*sp->strile_length;
1054 	}
1055 	else
1056 	{
1057 		sp->strile_width=sp->image_width;
1058 		sp->strile_length=tif->tif_dir.td_rowsperstrip;
1059 		sp->strile_length_total=sp->image_length;
1060 	}
1061 	if (tif->tif_dir.td_samplesperpixel==1)
1062 	{
1063 		sp->samples_per_pixel=1;
1064 		sp->plane_sample_offset=0;
1065 		sp->samples_per_pixel_per_plane=sp->samples_per_pixel;
1066 		sp->subsampling_hor=1;
1067 		sp->subsampling_ver=1;
1068 	}
1069 	else
1070 	{
1071 		if (tif->tif_dir.td_samplesperpixel!=3)
1072 		{
1073 			TIFFErrorExt(tif->tif_clientdata,module,"SamplesPerPixel %d not supported for this compression scheme",sp->samples_per_pixel);
1074 			return(0);
1075 		}
1076 		sp->samples_per_pixel=3;
1077 		sp->plane_sample_offset=0;
1078 		if (tif->tif_dir.td_planarconfig==PLANARCONFIG_CONTIG)
1079 			sp->samples_per_pixel_per_plane=3;
1080 		else
1081 			sp->samples_per_pixel_per_plane=1;
1082 	}
1083 	if (sp->strile_length<sp->image_length)
1084 	{
1085 		if (sp->strile_length%(sp->subsampling_ver*8)!=0)
1086 		{
1087 			TIFFErrorExt(tif->tif_clientdata,module,"Incompatible vertical subsampling and image strip/tile length");
1088 			return(0);
1089 		}
1090 		sp->restart_interval=(uint16)(((sp->strile_width+sp->subsampling_hor*8-1)/(sp->subsampling_hor*8))*(sp->strile_length/(sp->subsampling_ver*8)));
1091 	}
1092 	if (OJPEGReadHeaderInfoSec(tif)==0)
1093 		return(0);
1094 	sp->sos_end[0].log=1;
1095 	sp->sos_end[0].in_buffer_source=sp->in_buffer_source;
1096 	sp->sos_end[0].in_buffer_next_strile=sp->in_buffer_next_strile;
1097 	sp->sos_end[0].in_buffer_file_pos=sp->in_buffer_file_pos-sp->in_buffer_togo;
1098 	sp->sos_end[0].in_buffer_file_togo=sp->in_buffer_file_togo+sp->in_buffer_togo;
1099 	sp->readheader_done=1;
1100 	return(1);
1101 }
1102 
1103 static int
OJPEGReadSecondarySos(TIFF * tif,uint16 s)1104 OJPEGReadSecondarySos(TIFF* tif, uint16 s)
1105 {
1106 	OJPEGState* sp=(OJPEGState*)tif->tif_data;
1107 	uint8 m;
1108 	assert(s>0);
1109 	assert(s<3);
1110 	assert(sp->sos_end[0].log!=0);
1111 	assert(sp->sos_end[s].log==0);
1112 	sp->plane_sample_offset=(uint8)(s-1);
1113 	while(sp->sos_end[sp->plane_sample_offset].log==0)
1114 		sp->plane_sample_offset--;
1115 	sp->in_buffer_source=sp->sos_end[sp->plane_sample_offset].in_buffer_source;
1116 	sp->in_buffer_next_strile=sp->sos_end[sp->plane_sample_offset].in_buffer_next_strile;
1117 	sp->in_buffer_file_pos=sp->sos_end[sp->plane_sample_offset].in_buffer_file_pos;
1118 	sp->in_buffer_file_pos_log=0;
1119 	sp->in_buffer_file_togo=sp->sos_end[sp->plane_sample_offset].in_buffer_file_togo;
1120 	sp->in_buffer_togo=0;
1121 	sp->in_buffer_cur=0;
1122 	while(sp->plane_sample_offset<s)
1123 	{
1124 		do
1125 		{
1126 			if (OJPEGReadByte(sp,&m)==0)
1127 				return(0);
1128 			if (m==255)
1129 			{
1130 				do
1131 				{
1132 					if (OJPEGReadByte(sp,&m)==0)
1133 						return(0);
1134 					if (m!=255)
1135 						break;
1136 				} while(1);
1137 				if (m==JPEG_MARKER_SOS)
1138 					break;
1139 			}
1140 		} while(1);
1141 		sp->plane_sample_offset++;
1142 		if (OJPEGReadHeaderInfoSecStreamSos(tif)==0)
1143 			return(0);
1144 		sp->sos_end[sp->plane_sample_offset].log=1;
1145 		sp->sos_end[sp->plane_sample_offset].in_buffer_source=sp->in_buffer_source;
1146 		sp->sos_end[sp->plane_sample_offset].in_buffer_next_strile=sp->in_buffer_next_strile;
1147 		sp->sos_end[sp->plane_sample_offset].in_buffer_file_pos=sp->in_buffer_file_pos-sp->in_buffer_togo;
1148 		sp->sos_end[sp->plane_sample_offset].in_buffer_file_togo=sp->in_buffer_file_togo+sp->in_buffer_togo;
1149 	}
1150 	return(1);
1151 }
1152 
1153 static int
OJPEGWriteHeaderInfo(TIFF * tif)1154 OJPEGWriteHeaderInfo(TIFF* tif)
1155 {
1156 	static const char module[]="OJPEGWriteHeaderInfo";
1157 	OJPEGState* sp=(OJPEGState*)tif->tif_data;
1158 	uint8** m;
1159 	uint32 n;
1160 	/* if a previous attempt failed, don't try again */
1161 	if (sp->libjpeg_session_active != 0)
1162 		return 0;
1163 	sp->out_state=ososSoi;
1164 	sp->restart_index=0;
1165 	jpeg_std_error(&(sp->libjpeg_jpeg_error_mgr));
1166 	sp->libjpeg_jpeg_error_mgr.output_message=OJPEGLibjpegJpegErrorMgrOutputMessage;
1167 	sp->libjpeg_jpeg_error_mgr.error_exit=OJPEGLibjpegJpegErrorMgrErrorExit;
1168 	sp->libjpeg_jpeg_decompress_struct.err=&(sp->libjpeg_jpeg_error_mgr);
1169 	sp->libjpeg_jpeg_decompress_struct.client_data=(void*)tif;
1170 	if (jpeg_create_decompress_encap(sp,&(sp->libjpeg_jpeg_decompress_struct))==0)
1171 		return(0);
1172 	sp->libjpeg_session_active=1;
1173 	sp->libjpeg_jpeg_source_mgr.bytes_in_buffer=0;
1174 	sp->libjpeg_jpeg_source_mgr.init_source=OJPEGLibjpegJpegSourceMgrInitSource;
1175 	sp->libjpeg_jpeg_source_mgr.fill_input_buffer=OJPEGLibjpegJpegSourceMgrFillInputBuffer;
1176 	sp->libjpeg_jpeg_source_mgr.skip_input_data=OJPEGLibjpegJpegSourceMgrSkipInputData;
1177 	sp->libjpeg_jpeg_source_mgr.resync_to_restart=OJPEGLibjpegJpegSourceMgrResyncToRestart;
1178 	sp->libjpeg_jpeg_source_mgr.term_source=OJPEGLibjpegJpegSourceMgrTermSource;
1179 	sp->libjpeg_jpeg_decompress_struct.src=&(sp->libjpeg_jpeg_source_mgr);
1180 	if (jpeg_read_header_encap(sp,&(sp->libjpeg_jpeg_decompress_struct),1)==0)
1181 		return(0);
1182 	if ((sp->subsampling_force_desubsampling_inside_decompression==0) && (sp->samples_per_pixel_per_plane>1))
1183 	{
1184 		sp->libjpeg_jpeg_decompress_struct.raw_data_out=1;
1185 #if JPEG_LIB_VERSION >= 70
1186 		sp->libjpeg_jpeg_decompress_struct.do_fancy_upsampling=FALSE;
1187 #endif
1188 		sp->libjpeg_jpeg_query_style=0;
1189 		if (sp->subsampling_convert_log==0)
1190 		{
1191 			assert(sp->subsampling_convert_ycbcrbuf==0);
1192 			assert(sp->subsampling_convert_ycbcrimage==0);
1193 			sp->subsampling_convert_ylinelen=((sp->strile_width+sp->subsampling_hor*8-1)/(sp->subsampling_hor*8)*sp->subsampling_hor*8);
1194 			sp->subsampling_convert_ylines=sp->subsampling_ver*8;
1195 			sp->subsampling_convert_clinelen=sp->subsampling_convert_ylinelen/sp->subsampling_hor;
1196 			sp->subsampling_convert_clines=8;
1197 			sp->subsampling_convert_ybuflen=sp->subsampling_convert_ylinelen*sp->subsampling_convert_ylines;
1198 			sp->subsampling_convert_cbuflen=sp->subsampling_convert_clinelen*sp->subsampling_convert_clines;
1199 			sp->subsampling_convert_ycbcrbuflen=sp->subsampling_convert_ybuflen+2*sp->subsampling_convert_cbuflen;
1200 			sp->subsampling_convert_ycbcrbuf=_TIFFmalloc(sp->subsampling_convert_ycbcrbuflen);
1201 			if (sp->subsampling_convert_ycbcrbuf==0)
1202 			{
1203 				TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
1204 				return(0);
1205 			}
1206 			sp->subsampling_convert_ybuf=sp->subsampling_convert_ycbcrbuf;
1207 			sp->subsampling_convert_cbbuf=sp->subsampling_convert_ybuf+sp->subsampling_convert_ybuflen;
1208 			sp->subsampling_convert_crbuf=sp->subsampling_convert_cbbuf+sp->subsampling_convert_cbuflen;
1209 			sp->subsampling_convert_ycbcrimagelen=3+sp->subsampling_convert_ylines+2*sp->subsampling_convert_clines;
1210 			sp->subsampling_convert_ycbcrimage=_TIFFmalloc(sp->subsampling_convert_ycbcrimagelen*sizeof(uint8*));
1211 			if (sp->subsampling_convert_ycbcrimage==0)
1212 			{
1213 				TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
1214 				return(0);
1215 			}
1216 			m=sp->subsampling_convert_ycbcrimage;
1217 			*m++=(uint8*)(sp->subsampling_convert_ycbcrimage+3);
1218 			*m++=(uint8*)(sp->subsampling_convert_ycbcrimage+3+sp->subsampling_convert_ylines);
1219 			*m++=(uint8*)(sp->subsampling_convert_ycbcrimage+3+sp->subsampling_convert_ylines+sp->subsampling_convert_clines);
1220 			for (n=0; n<sp->subsampling_convert_ylines; n++)
1221 				*m++=sp->subsampling_convert_ybuf+n*sp->subsampling_convert_ylinelen;
1222 			for (n=0; n<sp->subsampling_convert_clines; n++)
1223 				*m++=sp->subsampling_convert_cbbuf+n*sp->subsampling_convert_clinelen;
1224 			for (n=0; n<sp->subsampling_convert_clines; n++)
1225 				*m++=sp->subsampling_convert_crbuf+n*sp->subsampling_convert_clinelen;
1226 			sp->subsampling_convert_clinelenout=((sp->strile_width+sp->subsampling_hor-1)/sp->subsampling_hor);
1227 			sp->subsampling_convert_state=0;
1228 			sp->bytes_per_line=sp->subsampling_convert_clinelenout*(sp->subsampling_ver*sp->subsampling_hor+2);
1229 			sp->lines_per_strile=((sp->strile_length+sp->subsampling_ver-1)/sp->subsampling_ver);
1230 			sp->subsampling_convert_log=1;
1231 		}
1232 	}
1233 	else
1234 	{
1235 		sp->libjpeg_jpeg_decompress_struct.jpeg_color_space=JCS_UNKNOWN;
1236 		sp->libjpeg_jpeg_decompress_struct.out_color_space=JCS_UNKNOWN;
1237 		sp->libjpeg_jpeg_query_style=1;
1238 		sp->bytes_per_line=sp->samples_per_pixel_per_plane*sp->strile_width;
1239 		sp->lines_per_strile=sp->strile_length;
1240 	}
1241 	if (jpeg_start_decompress_encap(sp,&(sp->libjpeg_jpeg_decompress_struct))==0)
1242 		return(0);
1243 	sp->writeheader_done=1;
1244 	return(1);
1245 }
1246 
1247 static void
OJPEGLibjpegSessionAbort(TIFF * tif)1248 OJPEGLibjpegSessionAbort(TIFF* tif)
1249 {
1250 	OJPEGState* sp=(OJPEGState*)tif->tif_data;
1251 	assert(sp->libjpeg_session_active!=0);
1252 	jpeg_destroy((jpeg_common_struct*)(&(sp->libjpeg_jpeg_decompress_struct)));
1253 	sp->libjpeg_session_active=0;
1254 }
1255 
1256 static int
OJPEGReadHeaderInfoSec(TIFF * tif)1257 OJPEGReadHeaderInfoSec(TIFF* tif)
1258 {
1259 	static const char module[]="OJPEGReadHeaderInfoSec";
1260 	OJPEGState* sp=(OJPEGState*)tif->tif_data;
1261 	uint8 m;
1262 	uint16 n;
1263 	uint8 o;
1264 	if (sp->file_size==0)
1265 		sp->file_size=TIFFGetFileSize(tif);
1266 	if (sp->jpeg_interchange_format!=0)
1267 	{
1268 		if (sp->jpeg_interchange_format>=sp->file_size)
1269 		{
1270 			sp->jpeg_interchange_format=0;
1271 			sp->jpeg_interchange_format_length=0;
1272 		}
1273 		else
1274 		{
1275 			if ((sp->jpeg_interchange_format_length==0) || (sp->jpeg_interchange_format+sp->jpeg_interchange_format_length>sp->file_size))
1276 				sp->jpeg_interchange_format_length=sp->file_size-sp->jpeg_interchange_format;
1277 		}
1278 	}
1279 	sp->in_buffer_source=osibsNotSetYet;
1280 	sp->in_buffer_next_strile=0;
1281 	sp->in_buffer_strile_count=tif->tif_dir.td_nstrips;
1282 	sp->in_buffer_file_togo=0;
1283 	sp->in_buffer_togo=0;
1284 	do
1285 	{
1286 		if (OJPEGReadBytePeek(sp,&m)==0)
1287 			return(0);
1288 		if (m!=255)
1289 			break;
1290 		OJPEGReadByteAdvance(sp);
1291 		do
1292 		{
1293 			if (OJPEGReadByte(sp,&m)==0)
1294 				return(0);
1295 		} while(m==255);
1296 		switch(m)
1297 		{
1298 			case JPEG_MARKER_SOI:
1299 				/* this type of marker has no data, and should be skipped */
1300 				break;
1301 			case JPEG_MARKER_COM:
1302 			case JPEG_MARKER_APP0:
1303 			case JPEG_MARKER_APP0+1:
1304 			case JPEG_MARKER_APP0+2:
1305 			case JPEG_MARKER_APP0+3:
1306 			case JPEG_MARKER_APP0+4:
1307 			case JPEG_MARKER_APP0+5:
1308 			case JPEG_MARKER_APP0+6:
1309 			case JPEG_MARKER_APP0+7:
1310 			case JPEG_MARKER_APP0+8:
1311 			case JPEG_MARKER_APP0+9:
1312 			case JPEG_MARKER_APP0+10:
1313 			case JPEG_MARKER_APP0+11:
1314 			case JPEG_MARKER_APP0+12:
1315 			case JPEG_MARKER_APP0+13:
1316 			case JPEG_MARKER_APP0+14:
1317 			case JPEG_MARKER_APP0+15:
1318 				/* this type of marker has data, but it has no use to us (and no place here) and should be skipped */
1319 				if (OJPEGReadWord(sp,&n)==0)
1320 					return(0);
1321 				if (n<2)
1322 				{
1323 					if (sp->subsamplingcorrect==0)
1324 						TIFFErrorExt(tif->tif_clientdata,module,"Corrupt JPEG data");
1325 					return(0);
1326 				}
1327 				if (n>2)
1328 					OJPEGReadSkip(sp,n-2);
1329 				break;
1330 			case JPEG_MARKER_DRI:
1331 				if (OJPEGReadHeaderInfoSecStreamDri(tif)==0)
1332 					return(0);
1333 				break;
1334 			case JPEG_MARKER_DQT:
1335 				if (OJPEGReadHeaderInfoSecStreamDqt(tif)==0)
1336 					return(0);
1337 				break;
1338 			case JPEG_MARKER_DHT:
1339 				if (OJPEGReadHeaderInfoSecStreamDht(tif)==0)
1340 					return(0);
1341 				break;
1342 			case JPEG_MARKER_SOF0:
1343 			case JPEG_MARKER_SOF1:
1344 			case JPEG_MARKER_SOF3:
1345 				if (OJPEGReadHeaderInfoSecStreamSof(tif,m)==0)
1346 					return(0);
1347 				if (sp->subsamplingcorrect!=0)
1348 					return(1);
1349 				break;
1350 			case JPEG_MARKER_SOS:
1351 				if (sp->subsamplingcorrect!=0)
1352 					return(1);
1353 				assert(sp->plane_sample_offset==0);
1354 				if (OJPEGReadHeaderInfoSecStreamSos(tif)==0)
1355 					return(0);
1356 				break;
1357 			default:
1358 				TIFFErrorExt(tif->tif_clientdata,module,"Unknown marker type %d in JPEG data",m);
1359 				return(0);
1360 		}
1361 	} while(m!=JPEG_MARKER_SOS);
1362 	if (sp->subsamplingcorrect)
1363 		return(1);
1364 	if (sp->sof_log==0)
1365 	{
1366 		if (OJPEGReadHeaderInfoSecTablesQTable(tif)==0)
1367 			return(0);
1368 		sp->sof_marker_id=JPEG_MARKER_SOF0;
1369 		for (o=0; o<sp->samples_per_pixel; o++)
1370 			sp->sof_c[o]=o;
1371 		sp->sof_hv[0]=((sp->subsampling_hor<<4)|sp->subsampling_ver);
1372 		for (o=1; o<sp->samples_per_pixel; o++)
1373 			sp->sof_hv[o]=17;
1374 		sp->sof_x=sp->strile_width;
1375 		sp->sof_y=sp->strile_length_total;
1376 		sp->sof_log=1;
1377 		if (OJPEGReadHeaderInfoSecTablesDcTable(tif)==0)
1378 			return(0);
1379 		if (OJPEGReadHeaderInfoSecTablesAcTable(tif)==0)
1380 			return(0);
1381 		for (o=1; o<sp->samples_per_pixel; o++)
1382 			sp->sos_cs[o]=o;
1383 	}
1384 	return(1);
1385 }
1386 
1387 static int
OJPEGReadHeaderInfoSecStreamDri(TIFF * tif)1388 OJPEGReadHeaderInfoSecStreamDri(TIFF* tif)
1389 {
1390 	/* This could easily cause trouble in some cases... but no such cases have
1391            occurred so far */
1392 	static const char module[]="OJPEGReadHeaderInfoSecStreamDri";
1393 	OJPEGState* sp=(OJPEGState*)tif->tif_data;
1394 	uint16 m;
1395 	if (OJPEGReadWord(sp,&m)==0)
1396 		return(0);
1397 	if (m!=4)
1398 	{
1399 		TIFFErrorExt(tif->tif_clientdata,module,"Corrupt DRI marker in JPEG data");
1400 		return(0);
1401 	}
1402 	if (OJPEGReadWord(sp,&m)==0)
1403 		return(0);
1404 	sp->restart_interval=m;
1405 	return(1);
1406 }
1407 
1408 static int
OJPEGReadHeaderInfoSecStreamDqt(TIFF * tif)1409 OJPEGReadHeaderInfoSecStreamDqt(TIFF* tif)
1410 {
1411 	/* this is a table marker, and it is to be saved as a whole for exact pushing on the jpeg stream later on */
1412 	static const char module[]="OJPEGReadHeaderInfoSecStreamDqt";
1413 	OJPEGState* sp=(OJPEGState*)tif->tif_data;
1414 	uint16 m;
1415 	uint32 na;
1416 	uint8* nb;
1417 	uint8 o;
1418 	if (OJPEGReadWord(sp,&m)==0)
1419 		return(0);
1420 	if (m<=2)
1421 	{
1422 		if (sp->subsamplingcorrect==0)
1423 			TIFFErrorExt(tif->tif_clientdata,module,"Corrupt DQT marker in JPEG data");
1424 		return(0);
1425 	}
1426 	if (sp->subsamplingcorrect!=0)
1427 		OJPEGReadSkip(sp,m-2);
1428 	else
1429 	{
1430 		m-=2;
1431 		do
1432 		{
1433 			if (m<65)
1434 			{
1435 				TIFFErrorExt(tif->tif_clientdata,module,"Corrupt DQT marker in JPEG data");
1436 				return(0);
1437 			}
1438 			na=sizeof(uint32)+69;
1439 			nb=_TIFFmalloc(na);
1440 			if (nb==0)
1441 			{
1442 				TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
1443 				return(0);
1444 			}
1445 			*(uint32*)nb=na;
1446 			nb[sizeof(uint32)]=255;
1447 			nb[sizeof(uint32)+1]=JPEG_MARKER_DQT;
1448 			nb[sizeof(uint32)+2]=0;
1449 			nb[sizeof(uint32)+3]=67;
1450 			if (OJPEGReadBlock(sp,65,&nb[sizeof(uint32)+4])==0) {
1451 				_TIFFfree(nb);
1452 				return(0);
1453 			}
1454 			o=nb[sizeof(uint32)+4]&15;
1455 			if (3<o)
1456 			{
1457 				TIFFErrorExt(tif->tif_clientdata,module,"Corrupt DQT marker in JPEG data");
1458 				_TIFFfree(nb);
1459 				return(0);
1460 			}
1461 			if (sp->qtable[o]!=0)
1462 				_TIFFfree(sp->qtable[o]);
1463 			sp->qtable[o]=nb;
1464 			m-=65;
1465 		} while(m>0);
1466 	}
1467 	return(1);
1468 }
1469 
1470 static int
OJPEGReadHeaderInfoSecStreamDht(TIFF * tif)1471 OJPEGReadHeaderInfoSecStreamDht(TIFF* tif)
1472 {
1473 	/* this is a table marker, and it is to be saved as a whole for exact pushing on the jpeg stream later on */
1474 	/* TODO: the following assumes there is only one table in this marker... but i'm not quite sure that assumption is guaranteed correct */
1475 	static const char module[]="OJPEGReadHeaderInfoSecStreamDht";
1476 	OJPEGState* sp=(OJPEGState*)tif->tif_data;
1477 	uint16 m;
1478 	uint32 na;
1479 	uint8* nb;
1480 	uint8 o;
1481 	if (OJPEGReadWord(sp,&m)==0)
1482 		return(0);
1483 	if (m<=2)
1484 	{
1485 		if (sp->subsamplingcorrect==0)
1486 			TIFFErrorExt(tif->tif_clientdata,module,"Corrupt DHT marker in JPEG data");
1487 		return(0);
1488 	}
1489 	if (sp->subsamplingcorrect!=0)
1490 	{
1491 		OJPEGReadSkip(sp,m-2);
1492 	}
1493 	else
1494 	{
1495 		na=sizeof(uint32)+2+m;
1496 		nb=_TIFFmalloc(na);
1497 		if (nb==0)
1498 		{
1499 			TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
1500 			return(0);
1501 		}
1502 		*(uint32*)nb=na;
1503 		nb[sizeof(uint32)]=255;
1504 		nb[sizeof(uint32)+1]=JPEG_MARKER_DHT;
1505 		nb[sizeof(uint32)+2]=(m>>8);
1506 		nb[sizeof(uint32)+3]=(m&255);
1507 		if (OJPEGReadBlock(sp,m-2,&nb[sizeof(uint32)+4])==0) {
1508                         _TIFFfree(nb);
1509 			return(0);
1510                 }
1511 		o=nb[sizeof(uint32)+4];
1512 		if ((o&240)==0)
1513 		{
1514 			if (3<o)
1515 			{
1516 				TIFFErrorExt(tif->tif_clientdata,module,"Corrupt DHT marker in JPEG data");
1517                                 _TIFFfree(nb);
1518 				return(0);
1519 			}
1520 			if (sp->dctable[o]!=0)
1521 				_TIFFfree(sp->dctable[o]);
1522 			sp->dctable[o]=nb;
1523 		}
1524 		else
1525 		{
1526 			if ((o&240)!=16)
1527 			{
1528 				TIFFErrorExt(tif->tif_clientdata,module,"Corrupt DHT marker in JPEG data");
1529                                 _TIFFfree(nb);
1530 				return(0);
1531 			}
1532 			o&=15;
1533 			if (3<o)
1534 			{
1535 				TIFFErrorExt(tif->tif_clientdata,module,"Corrupt DHT marker in JPEG data");
1536                                 _TIFFfree(nb);
1537 				return(0);
1538 			}
1539 			if (sp->actable[o]!=0)
1540 				_TIFFfree(sp->actable[o]);
1541 			sp->actable[o]=nb;
1542 		}
1543 	}
1544 	return(1);
1545 }
1546 
1547 static int
OJPEGReadHeaderInfoSecStreamSof(TIFF * tif,uint8 marker_id)1548 OJPEGReadHeaderInfoSecStreamSof(TIFF* tif, uint8 marker_id)
1549 {
1550 	/* this marker needs to be checked, and part of its data needs to be saved for regeneration later on */
1551 	static const char module[]="OJPEGReadHeaderInfoSecStreamSof";
1552 	OJPEGState* sp=(OJPEGState*)tif->tif_data;
1553 	uint16 m;
1554 	uint16 n;
1555 	uint8 o;
1556 	uint16 p;
1557 	uint16 q;
1558 	if (sp->sof_log!=0)
1559 	{
1560 		TIFFErrorExt(tif->tif_clientdata,module,"Corrupt JPEG data");
1561 		return(0);
1562 	}
1563 	if (sp->subsamplingcorrect==0)
1564 		sp->sof_marker_id=marker_id;
1565 	/* Lf: data length */
1566 	if (OJPEGReadWord(sp,&m)==0)
1567 		return(0);
1568 	if (m<11)
1569 	{
1570 		if (sp->subsamplingcorrect==0)
1571 			TIFFErrorExt(tif->tif_clientdata,module,"Corrupt SOF marker in JPEG data");
1572 		return(0);
1573 	}
1574 	m-=8;
1575 	if (m%3!=0)
1576 	{
1577 		if (sp->subsamplingcorrect==0)
1578 			TIFFErrorExt(tif->tif_clientdata,module,"Corrupt SOF marker in JPEG data");
1579 		return(0);
1580 	}
1581 	n=m/3;
1582 	if (sp->subsamplingcorrect==0)
1583 	{
1584 		if (n!=sp->samples_per_pixel)
1585 		{
1586 			TIFFErrorExt(tif->tif_clientdata,module,"JPEG compressed data indicates unexpected number of samples");
1587 			return(0);
1588 		}
1589 	}
1590 	/* P: Sample precision */
1591 	if (OJPEGReadByte(sp,&o)==0)
1592 		return(0);
1593 	if (o!=8)
1594 	{
1595 		if (sp->subsamplingcorrect==0)
1596 			TIFFErrorExt(tif->tif_clientdata,module,"JPEG compressed data indicates unexpected number of bits per sample");
1597 		return(0);
1598 	}
1599 	/* Y: Number of lines, X: Number of samples per line */
1600 	if (sp->subsamplingcorrect)
1601 		OJPEGReadSkip(sp,4);
1602 	else
1603 	{
1604 		/* Y: Number of lines */
1605 		if (OJPEGReadWord(sp,&p)==0)
1606 			return(0);
1607 		if (((uint32)p<sp->image_length) && ((uint32)p<sp->strile_length_total))
1608 		{
1609 			TIFFErrorExt(tif->tif_clientdata,module,"JPEG compressed data indicates unexpected height");
1610 			return(0);
1611 		}
1612 		sp->sof_y=p;
1613 		/* X: Number of samples per line */
1614 		if (OJPEGReadWord(sp,&p)==0)
1615 			return(0);
1616 		if (((uint32)p<sp->image_width) && ((uint32)p<sp->strile_width))
1617 		{
1618 			TIFFErrorExt(tif->tif_clientdata,module,"JPEG compressed data indicates unexpected width");
1619 			return(0);
1620 		}
1621 		if ((uint32)p>sp->strile_width)
1622 		{
1623 			TIFFErrorExt(tif->tif_clientdata,module,"JPEG compressed data image width exceeds expected image width");
1624 			return(0);
1625 		}
1626 		sp->sof_x=p;
1627 	}
1628 	/* Nf: Number of image components in frame */
1629 	if (OJPEGReadByte(sp,&o)==0)
1630 		return(0);
1631 	if (o!=n)
1632 	{
1633 		if (sp->subsamplingcorrect==0)
1634 			TIFFErrorExt(tif->tif_clientdata,module,"Corrupt SOF marker in JPEG data");
1635 		return(0);
1636 	}
1637 	/* per component stuff */
1638 	/* TODO: double-check that flow implies that n cannot be as big as to make us overflow sof_c, sof_hv and sof_tq arrays */
1639 	for (q=0; q<n; q++)
1640 	{
1641 		/* C: Component identifier */
1642 		if (OJPEGReadByte(sp,&o)==0)
1643 			return(0);
1644 		if (sp->subsamplingcorrect==0)
1645 			sp->sof_c[q]=o;
1646 		/* H: Horizontal sampling factor, and V: Vertical sampling factor */
1647 		if (OJPEGReadByte(sp,&o)==0)
1648 			return(0);
1649 		if (sp->subsamplingcorrect!=0)
1650 		{
1651 			if (q==0)
1652 			{
1653 				sp->subsampling_hor=(o>>4);
1654 				sp->subsampling_ver=(o&15);
1655 				if (((sp->subsampling_hor!=1) && (sp->subsampling_hor!=2) && (sp->subsampling_hor!=4)) ||
1656 					((sp->subsampling_ver!=1) && (sp->subsampling_ver!=2) && (sp->subsampling_ver!=4)))
1657 					sp->subsampling_force_desubsampling_inside_decompression=1;
1658 			}
1659 			else
1660 			{
1661 				if (o!=17)
1662 					sp->subsampling_force_desubsampling_inside_decompression=1;
1663 			}
1664 		}
1665 		else
1666 		{
1667 			sp->sof_hv[q]=o;
1668 			if (sp->subsampling_force_desubsampling_inside_decompression==0)
1669 			{
1670 				if (q==0)
1671 				{
1672 					if (o!=((sp->subsampling_hor<<4)|sp->subsampling_ver))
1673 					{
1674 						TIFFErrorExt(tif->tif_clientdata,module,"JPEG compressed data indicates unexpected subsampling values");
1675 						return(0);
1676 					}
1677 				}
1678 				else
1679 				{
1680 					if (o!=17)
1681 					{
1682 						TIFFErrorExt(tif->tif_clientdata,module,"JPEG compressed data indicates unexpected subsampling values");
1683 						return(0);
1684 					}
1685 				}
1686 			}
1687 		}
1688 		/* Tq: Quantization table destination selector */
1689 		if (OJPEGReadByte(sp,&o)==0)
1690 			return(0);
1691 		if (sp->subsamplingcorrect==0)
1692 			sp->sof_tq[q]=o;
1693 	}
1694 	if (sp->subsamplingcorrect==0)
1695 		sp->sof_log=1;
1696 	return(1);
1697 }
1698 
1699 static int
OJPEGReadHeaderInfoSecStreamSos(TIFF * tif)1700 OJPEGReadHeaderInfoSecStreamSos(TIFF* tif)
1701 {
1702 	/* this marker needs to be checked, and part of its data needs to be saved for regeneration later on */
1703 	static const char module[]="OJPEGReadHeaderInfoSecStreamSos";
1704 	OJPEGState* sp=(OJPEGState*)tif->tif_data;
1705 	uint16 m;
1706 	uint8 n;
1707 	uint8 o;
1708 	assert(sp->subsamplingcorrect==0);
1709 	if (sp->sof_log==0)
1710 	{
1711 		TIFFErrorExt(tif->tif_clientdata,module,"Corrupt SOS marker in JPEG data");
1712 		return(0);
1713 	}
1714 	/* Ls */
1715 	if (OJPEGReadWord(sp,&m)==0)
1716 		return(0);
1717 	if (m!=6+sp->samples_per_pixel_per_plane*2)
1718 	{
1719 		TIFFErrorExt(tif->tif_clientdata,module,"Corrupt SOS marker in JPEG data");
1720 		return(0);
1721 	}
1722 	/* Ns */
1723 	if (OJPEGReadByte(sp,&n)==0)
1724 		return(0);
1725 	if (n!=sp->samples_per_pixel_per_plane)
1726 	{
1727 		TIFFErrorExt(tif->tif_clientdata,module,"Corrupt SOS marker in JPEG data");
1728 		return(0);
1729 	}
1730 	/* Cs, Td, and Ta */
1731 	for (o=0; o<sp->samples_per_pixel_per_plane; o++)
1732 	{
1733 		/* Cs */
1734 		if (OJPEGReadByte(sp,&n)==0)
1735 			return(0);
1736 		sp->sos_cs[sp->plane_sample_offset+o]=n;
1737 		/* Td and Ta */
1738 		if (OJPEGReadByte(sp,&n)==0)
1739 			return(0);
1740 		sp->sos_tda[sp->plane_sample_offset+o]=n;
1741 	}
1742 	/* skip Ss, Se, Ah, en Al -> no check, as per Tom Lane recommendation, as per LibJpeg source */
1743 	OJPEGReadSkip(sp,3);
1744 	return(1);
1745 }
1746 
1747 static int
OJPEGReadHeaderInfoSecTablesQTable(TIFF * tif)1748 OJPEGReadHeaderInfoSecTablesQTable(TIFF* tif)
1749 {
1750 	static const char module[]="OJPEGReadHeaderInfoSecTablesQTable";
1751 	OJPEGState* sp=(OJPEGState*)tif->tif_data;
1752 	uint8 m;
1753 	uint8 n;
1754 	uint32 oa;
1755 	uint8* ob;
1756 	uint32 p;
1757 	if (sp->qtable_offset[0]==0)
1758 	{
1759 		TIFFErrorExt(tif->tif_clientdata,module,"Missing JPEG tables");
1760 		return(0);
1761 	}
1762 	sp->in_buffer_file_pos_log=0;
1763 	for (m=0; m<sp->samples_per_pixel; m++)
1764 	{
1765 		if ((sp->qtable_offset[m]!=0) && ((m==0) || (sp->qtable_offset[m]!=sp->qtable_offset[m-1])))
1766 		{
1767 			for (n=0; n<m-1; n++)
1768 			{
1769 				if (sp->qtable_offset[m]==sp->qtable_offset[n])
1770 				{
1771 					TIFFErrorExt(tif->tif_clientdata,module,"Corrupt JpegQTables tag value");
1772 					return(0);
1773 				}
1774 			}
1775 			oa=sizeof(uint32)+69;
1776 			ob=_TIFFmalloc(oa);
1777 			if (ob==0)
1778 			{
1779 				TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
1780 				return(0);
1781 			}
1782 			*(uint32*)ob=oa;
1783 			ob[sizeof(uint32)]=255;
1784 			ob[sizeof(uint32)+1]=JPEG_MARKER_DQT;
1785 			ob[sizeof(uint32)+2]=0;
1786 			ob[sizeof(uint32)+3]=67;
1787 			ob[sizeof(uint32)+4]=m;
1788 			TIFFSeekFile(tif,sp->qtable_offset[m],SEEK_SET);
1789 			p=(uint32)TIFFReadFile(tif,&ob[sizeof(uint32)+5],64);
1790 			if (p!=64)
1791                         {
1792                                 _TIFFfree(ob);
1793 				return(0);
1794                         }
1795 			if (sp->qtable[m]!=0)
1796 				_TIFFfree(sp->qtable[m]);
1797 			sp->qtable[m]=ob;
1798 			sp->sof_tq[m]=m;
1799 		}
1800 		else
1801 			sp->sof_tq[m]=sp->sof_tq[m-1];
1802 	}
1803 	return(1);
1804 }
1805 
1806 static int
OJPEGReadHeaderInfoSecTablesDcTable(TIFF * tif)1807 OJPEGReadHeaderInfoSecTablesDcTable(TIFF* tif)
1808 {
1809 	static const char module[]="OJPEGReadHeaderInfoSecTablesDcTable";
1810 	OJPEGState* sp=(OJPEGState*)tif->tif_data;
1811 	uint8 m;
1812 	uint8 n;
1813 	uint8 o[16];
1814 	uint32 p;
1815 	uint32 q;
1816 	uint32 ra;
1817 	uint8* rb;
1818 	if (sp->dctable_offset[0]==0)
1819 	{
1820 		TIFFErrorExt(tif->tif_clientdata,module,"Missing JPEG tables");
1821 		return(0);
1822 	}
1823 	sp->in_buffer_file_pos_log=0;
1824 	for (m=0; m<sp->samples_per_pixel; m++)
1825 	{
1826 		if ((sp->dctable_offset[m]!=0) && ((m==0) || (sp->dctable_offset[m]!=sp->dctable_offset[m-1])))
1827 		{
1828 			for (n=0; n<m-1; n++)
1829 			{
1830 				if (sp->dctable_offset[m]==sp->dctable_offset[n])
1831 				{
1832 					TIFFErrorExt(tif->tif_clientdata,module,"Corrupt JpegDcTables tag value");
1833 					return(0);
1834 				}
1835 			}
1836 			TIFFSeekFile(tif,sp->dctable_offset[m],SEEK_SET);
1837 			p=(uint32)TIFFReadFile(tif,o,16);
1838 			if (p!=16)
1839 				return(0);
1840 			q=0;
1841 			for (n=0; n<16; n++)
1842 				q+=o[n];
1843 			ra=sizeof(uint32)+21+q;
1844 			rb=_TIFFmalloc(ra);
1845 			if (rb==0)
1846 			{
1847 				TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
1848 				return(0);
1849 			}
1850 			*(uint32*)rb=ra;
1851 			rb[sizeof(uint32)]=255;
1852 			rb[sizeof(uint32)+1]=JPEG_MARKER_DHT;
1853 			rb[sizeof(uint32)+2]=(uint8)((19+q)>>8);
1854 			rb[sizeof(uint32)+3]=((19+q)&255);
1855 			rb[sizeof(uint32)+4]=m;
1856 			for (n=0; n<16; n++)
1857 				rb[sizeof(uint32)+5+n]=o[n];
1858 			p=(uint32)TIFFReadFile(tif,&(rb[sizeof(uint32)+21]),q);
1859 			if (p!=q)
1860                         {
1861                                 _TIFFfree(rb);
1862 				return(0);
1863                         }
1864 			if (sp->dctable[m]!=0)
1865 				_TIFFfree(sp->dctable[m]);
1866 			sp->dctable[m]=rb;
1867 			sp->sos_tda[m]=(m<<4);
1868 		}
1869 		else
1870 			sp->sos_tda[m]=sp->sos_tda[m-1];
1871 	}
1872 	return(1);
1873 }
1874 
1875 static int
OJPEGReadHeaderInfoSecTablesAcTable(TIFF * tif)1876 OJPEGReadHeaderInfoSecTablesAcTable(TIFF* tif)
1877 {
1878 	static const char module[]="OJPEGReadHeaderInfoSecTablesAcTable";
1879 	OJPEGState* sp=(OJPEGState*)tif->tif_data;
1880 	uint8 m;
1881 	uint8 n;
1882 	uint8 o[16];
1883 	uint32 p;
1884 	uint32 q;
1885 	uint32 ra;
1886 	uint8* rb;
1887 	if (sp->actable_offset[0]==0)
1888 	{
1889 		TIFFErrorExt(tif->tif_clientdata,module,"Missing JPEG tables");
1890 		return(0);
1891 	}
1892 	sp->in_buffer_file_pos_log=0;
1893 	for (m=0; m<sp->samples_per_pixel; m++)
1894 	{
1895 		if ((sp->actable_offset[m]!=0) && ((m==0) || (sp->actable_offset[m]!=sp->actable_offset[m-1])))
1896 		{
1897 			for (n=0; n<m-1; n++)
1898 			{
1899 				if (sp->actable_offset[m]==sp->actable_offset[n])
1900 				{
1901 					TIFFErrorExt(tif->tif_clientdata,module,"Corrupt JpegAcTables tag value");
1902 					return(0);
1903 				}
1904 			}
1905 			TIFFSeekFile(tif,sp->actable_offset[m],SEEK_SET);
1906 			p=(uint32)TIFFReadFile(tif,o,16);
1907 			if (p!=16)
1908 				return(0);
1909 			q=0;
1910 			for (n=0; n<16; n++)
1911 				q+=o[n];
1912 			ra=sizeof(uint32)+21+q;
1913 			rb=_TIFFmalloc(ra);
1914 			if (rb==0)
1915 			{
1916 				TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
1917 				return(0);
1918 			}
1919 			*(uint32*)rb=ra;
1920 			rb[sizeof(uint32)]=255;
1921 			rb[sizeof(uint32)+1]=JPEG_MARKER_DHT;
1922 			rb[sizeof(uint32)+2]=(uint8)((19+q)>>8);
1923 			rb[sizeof(uint32)+3]=((19+q)&255);
1924 			rb[sizeof(uint32)+4]=(16|m);
1925 			for (n=0; n<16; n++)
1926 				rb[sizeof(uint32)+5+n]=o[n];
1927 			p=(uint32)TIFFReadFile(tif,&(rb[sizeof(uint32)+21]),q);
1928 			if (p!=q)
1929                         {
1930                                 _TIFFfree(rb);
1931 				return(0);
1932                         }
1933 			if (sp->actable[m]!=0)
1934 				_TIFFfree(sp->actable[m]);
1935 			sp->actable[m]=rb;
1936 			sp->sos_tda[m]=(sp->sos_tda[m]|m);
1937 		}
1938 		else
1939 			sp->sos_tda[m]=(sp->sos_tda[m]|(sp->sos_tda[m-1]&15));
1940 	}
1941 	return(1);
1942 }
1943 
1944 static int
OJPEGReadBufferFill(OJPEGState * sp)1945 OJPEGReadBufferFill(OJPEGState* sp)
1946 {
1947 	uint16 m;
1948 	tmsize_t n;
1949 	/* TODO: double-check: when subsamplingcorrect is set, no call to TIFFErrorExt or TIFFWarningExt should be made
1950 	 * in any other case, seek or read errors should be passed through */
1951 	do
1952 	{
1953 		if (sp->in_buffer_file_togo!=0)
1954 		{
1955 			if (sp->in_buffer_file_pos_log==0)
1956 			{
1957 				TIFFSeekFile(sp->tif,sp->in_buffer_file_pos,SEEK_SET);
1958 				sp->in_buffer_file_pos_log=1;
1959 			}
1960 			m=OJPEG_BUFFER;
1961 			if ((uint64)m>sp->in_buffer_file_togo)
1962 				m=(uint16)sp->in_buffer_file_togo;
1963 			n=TIFFReadFile(sp->tif,sp->in_buffer,(tmsize_t)m);
1964 			if (n==0)
1965 				return(0);
1966 			assert(n>0);
1967 			assert(n<=OJPEG_BUFFER);
1968 			assert(n<65536);
1969 			assert((uint64)n<=sp->in_buffer_file_togo);
1970 			m=(uint16)n;
1971 			sp->in_buffer_togo=m;
1972 			sp->in_buffer_cur=sp->in_buffer;
1973 			sp->in_buffer_file_togo-=m;
1974 			sp->in_buffer_file_pos+=m;
1975 			break;
1976 		}
1977 		sp->in_buffer_file_pos_log=0;
1978 		switch(sp->in_buffer_source)
1979 		{
1980 			case osibsNotSetYet:
1981 				if (sp->jpeg_interchange_format!=0)
1982 				{
1983 					sp->in_buffer_file_pos=sp->jpeg_interchange_format;
1984 					sp->in_buffer_file_togo=sp->jpeg_interchange_format_length;
1985 				}
1986 				sp->in_buffer_source=osibsJpegInterchangeFormat;
1987 				break;
1988 			case osibsJpegInterchangeFormat:
1989 				sp->in_buffer_source=osibsStrile;
1990                                 break;
1991 			case osibsStrile:
1992 				if (!_TIFFFillStriles( sp->tif )
1993 				    || sp->tif->tif_dir.td_stripoffset == NULL
1994 				    || sp->tif->tif_dir.td_stripbytecount == NULL)
1995 					return 0;
1996 
1997 				if (sp->in_buffer_next_strile==sp->in_buffer_strile_count)
1998 					sp->in_buffer_source=osibsEof;
1999 				else
2000 				{
2001 					sp->in_buffer_file_pos=sp->tif->tif_dir.td_stripoffset[sp->in_buffer_next_strile];
2002 					if (sp->in_buffer_file_pos!=0)
2003 					{
2004 						if (sp->in_buffer_file_pos>=sp->file_size)
2005 							sp->in_buffer_file_pos=0;
2006 						else if (sp->tif->tif_dir.td_stripbytecount==NULL)
2007 							sp->in_buffer_file_togo=sp->file_size-sp->in_buffer_file_pos;
2008 						else
2009 						{
2010 							if (sp->tif->tif_dir.td_stripbytecount == 0) {
2011 								TIFFErrorExt(sp->tif->tif_clientdata,sp->tif->tif_name,"Strip byte counts are missing");
2012 								return(0);
2013 							}
2014 							sp->in_buffer_file_togo=sp->tif->tif_dir.td_stripbytecount[sp->in_buffer_next_strile];
2015 							if (sp->in_buffer_file_togo==0)
2016 								sp->in_buffer_file_pos=0;
2017 							else if (sp->in_buffer_file_pos+sp->in_buffer_file_togo>sp->file_size)
2018 								sp->in_buffer_file_togo=sp->file_size-sp->in_buffer_file_pos;
2019 						}
2020 					}
2021 					sp->in_buffer_next_strile++;
2022 				}
2023 				break;
2024 			default:
2025 				return(0);
2026 		}
2027 	} while (1);
2028 	return(1);
2029 }
2030 
2031 static int
OJPEGReadByte(OJPEGState * sp,uint8 * byte)2032 OJPEGReadByte(OJPEGState* sp, uint8* byte)
2033 {
2034 	if (sp->in_buffer_togo==0)
2035 	{
2036 		if (OJPEGReadBufferFill(sp)==0)
2037 			return(0);
2038 		assert(sp->in_buffer_togo>0);
2039 	}
2040 	*byte=*(sp->in_buffer_cur);
2041 	sp->in_buffer_cur++;
2042 	sp->in_buffer_togo--;
2043 	return(1);
2044 }
2045 
2046 static int
OJPEGReadBytePeek(OJPEGState * sp,uint8 * byte)2047 OJPEGReadBytePeek(OJPEGState* sp, uint8* byte)
2048 {
2049 	if (sp->in_buffer_togo==0)
2050 	{
2051 		if (OJPEGReadBufferFill(sp)==0)
2052 			return(0);
2053 		assert(sp->in_buffer_togo>0);
2054 	}
2055 	*byte=*(sp->in_buffer_cur);
2056 	return(1);
2057 }
2058 
2059 static void
OJPEGReadByteAdvance(OJPEGState * sp)2060 OJPEGReadByteAdvance(OJPEGState* sp)
2061 {
2062 	assert(sp->in_buffer_togo>0);
2063 	sp->in_buffer_cur++;
2064 	sp->in_buffer_togo--;
2065 }
2066 
2067 static int
OJPEGReadWord(OJPEGState * sp,uint16 * word)2068 OJPEGReadWord(OJPEGState* sp, uint16* word)
2069 {
2070 	uint8 m;
2071 	if (OJPEGReadByte(sp,&m)==0)
2072 		return(0);
2073 	*word=(m<<8);
2074 	if (OJPEGReadByte(sp,&m)==0)
2075 		return(0);
2076 	*word|=m;
2077 	return(1);
2078 }
2079 
2080 static int
OJPEGReadBlock(OJPEGState * sp,uint16 len,void * mem)2081 OJPEGReadBlock(OJPEGState* sp, uint16 len, void* mem)
2082 {
2083 	uint16 mlen;
2084 	uint8* mmem;
2085 	uint16 n;
2086 	assert(len>0);
2087 	mlen=len;
2088 	mmem=mem;
2089 	do
2090 	{
2091 		if (sp->in_buffer_togo==0)
2092 		{
2093 			if (OJPEGReadBufferFill(sp)==0)
2094 				return(0);
2095 			assert(sp->in_buffer_togo>0);
2096 		}
2097 		n=mlen;
2098 		if (n>sp->in_buffer_togo)
2099 			n=sp->in_buffer_togo;
2100 		_TIFFmemcpy(mmem,sp->in_buffer_cur,n);
2101 		sp->in_buffer_cur+=n;
2102 		sp->in_buffer_togo-=n;
2103 		mlen-=n;
2104 		mmem+=n;
2105 	} while(mlen>0);
2106 	return(1);
2107 }
2108 
2109 static void
OJPEGReadSkip(OJPEGState * sp,uint16 len)2110 OJPEGReadSkip(OJPEGState* sp, uint16 len)
2111 {
2112 	uint16 m;
2113 	uint16 n;
2114 	m=len;
2115 	n=m;
2116 	if (n>sp->in_buffer_togo)
2117 		n=sp->in_buffer_togo;
2118 	sp->in_buffer_cur+=n;
2119 	sp->in_buffer_togo-=n;
2120 	m-=n;
2121 	if (m>0)
2122 	{
2123 		assert(sp->in_buffer_togo==0);
2124 		n=m;
2125 		if ((uint64)n>sp->in_buffer_file_togo)
2126 			n=(uint16)sp->in_buffer_file_togo;
2127 		sp->in_buffer_file_pos+=n;
2128 		sp->in_buffer_file_togo-=n;
2129 		sp->in_buffer_file_pos_log=0;
2130 		/* we don't skip past jpeginterchangeformat/strile block...
2131 		 * if that is asked from us, we're dealing with totally bazurk
2132 		 * data anyway, and we've not seen this happening on any
2133 		 * testfile, so we might as well likely cause some other
2134 		 * meaningless error to be passed at some later time
2135 		 */
2136 	}
2137 }
2138 
2139 static int
OJPEGWriteStream(TIFF * tif,void ** mem,uint32 * len)2140 OJPEGWriteStream(TIFF* tif, void** mem, uint32* len)
2141 {
2142 	OJPEGState* sp=(OJPEGState*)tif->tif_data;
2143 	*len=0;
2144 	do
2145 	{
2146 		assert(sp->out_state<=ososEoi);
2147 		switch(sp->out_state)
2148 		{
2149 			case ososSoi:
2150 				OJPEGWriteStreamSoi(tif,mem,len);
2151 				break;
2152 			case ososQTable0:
2153 				OJPEGWriteStreamQTable(tif,0,mem,len);
2154 				break;
2155 			case ososQTable1:
2156 				OJPEGWriteStreamQTable(tif,1,mem,len);
2157 				break;
2158 			case ososQTable2:
2159 				OJPEGWriteStreamQTable(tif,2,mem,len);
2160 				break;
2161 			case ososQTable3:
2162 				OJPEGWriteStreamQTable(tif,3,mem,len);
2163 				break;
2164 			case ososDcTable0:
2165 				OJPEGWriteStreamDcTable(tif,0,mem,len);
2166 				break;
2167 			case ososDcTable1:
2168 				OJPEGWriteStreamDcTable(tif,1,mem,len);
2169 				break;
2170 			case ososDcTable2:
2171 				OJPEGWriteStreamDcTable(tif,2,mem,len);
2172 				break;
2173 			case ososDcTable3:
2174 				OJPEGWriteStreamDcTable(tif,3,mem,len);
2175 				break;
2176 			case ososAcTable0:
2177 				OJPEGWriteStreamAcTable(tif,0,mem,len);
2178 				break;
2179 			case ososAcTable1:
2180 				OJPEGWriteStreamAcTable(tif,1,mem,len);
2181 				break;
2182 			case ososAcTable2:
2183 				OJPEGWriteStreamAcTable(tif,2,mem,len);
2184 				break;
2185 			case ososAcTable3:
2186 				OJPEGWriteStreamAcTable(tif,3,mem,len);
2187 				break;
2188 			case ososDri:
2189 				OJPEGWriteStreamDri(tif,mem,len);
2190 				break;
2191 			case ososSof:
2192 				OJPEGWriteStreamSof(tif,mem,len);
2193 				break;
2194 			case ososSos:
2195 				OJPEGWriteStreamSos(tif,mem,len);
2196 				break;
2197 			case ososCompressed:
2198 				if (OJPEGWriteStreamCompressed(tif,mem,len)==0)
2199 					return(0);
2200 				break;
2201 			case ososRst:
2202 				OJPEGWriteStreamRst(tif,mem,len);
2203 				break;
2204 			case ososEoi:
2205 				OJPEGWriteStreamEoi(tif,mem,len);
2206 				break;
2207 		}
2208 	} while (*len==0);
2209 	return(1);
2210 }
2211 
2212 static void
OJPEGWriteStreamSoi(TIFF * tif,void ** mem,uint32 * len)2213 OJPEGWriteStreamSoi(TIFF* tif, void** mem, uint32* len)
2214 {
2215 	OJPEGState* sp=(OJPEGState*)tif->tif_data;
2216 	assert(OJPEG_BUFFER>=2);
2217 	sp->out_buffer[0]=255;
2218 	sp->out_buffer[1]=JPEG_MARKER_SOI;
2219 	*len=2;
2220 	*mem=(void*)sp->out_buffer;
2221 	sp->out_state++;
2222 }
2223 
2224 static void
OJPEGWriteStreamQTable(TIFF * tif,uint8 table_index,void ** mem,uint32 * len)2225 OJPEGWriteStreamQTable(TIFF* tif, uint8 table_index, void** mem, uint32* len)
2226 {
2227 	OJPEGState* sp=(OJPEGState*)tif->tif_data;
2228 	if (sp->qtable[table_index]!=0)
2229 	{
2230 		*mem=(void*)(sp->qtable[table_index]+sizeof(uint32));
2231 		*len=*((uint32*)sp->qtable[table_index])-sizeof(uint32);
2232 	}
2233 	sp->out_state++;
2234 }
2235 
2236 static void
OJPEGWriteStreamDcTable(TIFF * tif,uint8 table_index,void ** mem,uint32 * len)2237 OJPEGWriteStreamDcTable(TIFF* tif, uint8 table_index, void** mem, uint32* len)
2238 {
2239 	OJPEGState* sp=(OJPEGState*)tif->tif_data;
2240 	if (sp->dctable[table_index]!=0)
2241 	{
2242 		*mem=(void*)(sp->dctable[table_index]+sizeof(uint32));
2243 		*len=*((uint32*)sp->dctable[table_index])-sizeof(uint32);
2244 	}
2245 	sp->out_state++;
2246 }
2247 
2248 static void
OJPEGWriteStreamAcTable(TIFF * tif,uint8 table_index,void ** mem,uint32 * len)2249 OJPEGWriteStreamAcTable(TIFF* tif, uint8 table_index, void** mem, uint32* len)
2250 {
2251 	OJPEGState* sp=(OJPEGState*)tif->tif_data;
2252 	if (sp->actable[table_index]!=0)
2253 	{
2254 		*mem=(void*)(sp->actable[table_index]+sizeof(uint32));
2255 		*len=*((uint32*)sp->actable[table_index])-sizeof(uint32);
2256 	}
2257 	sp->out_state++;
2258 }
2259 
2260 static void
OJPEGWriteStreamDri(TIFF * tif,void ** mem,uint32 * len)2261 OJPEGWriteStreamDri(TIFF* tif, void** mem, uint32* len)
2262 {
2263 	OJPEGState* sp=(OJPEGState*)tif->tif_data;
2264 	assert(OJPEG_BUFFER>=6);
2265 	if (sp->restart_interval!=0)
2266 	{
2267 		sp->out_buffer[0]=255;
2268 		sp->out_buffer[1]=JPEG_MARKER_DRI;
2269 		sp->out_buffer[2]=0;
2270 		sp->out_buffer[3]=4;
2271 		sp->out_buffer[4]=(sp->restart_interval>>8);
2272 		sp->out_buffer[5]=(sp->restart_interval&255);
2273 		*len=6;
2274 		*mem=(void*)sp->out_buffer;
2275 	}
2276 	sp->out_state++;
2277 }
2278 
2279 static void
OJPEGWriteStreamSof(TIFF * tif,void ** mem,uint32 * len)2280 OJPEGWriteStreamSof(TIFF* tif, void** mem, uint32* len)
2281 {
2282 	OJPEGState* sp=(OJPEGState*)tif->tif_data;
2283 	uint8 m;
2284 	assert(OJPEG_BUFFER>=2+8+sp->samples_per_pixel_per_plane*3);
2285 	assert(255>=8+sp->samples_per_pixel_per_plane*3);
2286 	sp->out_buffer[0]=255;
2287 	sp->out_buffer[1]=sp->sof_marker_id;
2288 	/* Lf */
2289 	sp->out_buffer[2]=0;
2290 	sp->out_buffer[3]=8+sp->samples_per_pixel_per_plane*3;
2291 	/* P */
2292 	sp->out_buffer[4]=8;
2293 	/* Y */
2294 	sp->out_buffer[5]=(uint8)(sp->sof_y>>8);
2295 	sp->out_buffer[6]=(sp->sof_y&255);
2296 	/* X */
2297 	sp->out_buffer[7]=(uint8)(sp->sof_x>>8);
2298 	sp->out_buffer[8]=(sp->sof_x&255);
2299 	/* Nf */
2300 	sp->out_buffer[9]=sp->samples_per_pixel_per_plane;
2301 	for (m=0; m<sp->samples_per_pixel_per_plane; m++)
2302 	{
2303 		/* C */
2304 		sp->out_buffer[10+m*3]=sp->sof_c[sp->plane_sample_offset+m];
2305 		/* H and V */
2306 		sp->out_buffer[10+m*3+1]=sp->sof_hv[sp->plane_sample_offset+m];
2307 		/* Tq */
2308 		sp->out_buffer[10+m*3+2]=sp->sof_tq[sp->plane_sample_offset+m];
2309 	}
2310 	*len=10+sp->samples_per_pixel_per_plane*3;
2311 	*mem=(void*)sp->out_buffer;
2312 	sp->out_state++;
2313 }
2314 
2315 static void
OJPEGWriteStreamSos(TIFF * tif,void ** mem,uint32 * len)2316 OJPEGWriteStreamSos(TIFF* tif, void** mem, uint32* len)
2317 {
2318 	OJPEGState* sp=(OJPEGState*)tif->tif_data;
2319 	uint8 m;
2320 	assert(OJPEG_BUFFER>=2+6+sp->samples_per_pixel_per_plane*2);
2321 	assert(255>=6+sp->samples_per_pixel_per_plane*2);
2322 	sp->out_buffer[0]=255;
2323 	sp->out_buffer[1]=JPEG_MARKER_SOS;
2324 	/* Ls */
2325 	sp->out_buffer[2]=0;
2326 	sp->out_buffer[3]=6+sp->samples_per_pixel_per_plane*2;
2327 	/* Ns */
2328 	sp->out_buffer[4]=sp->samples_per_pixel_per_plane;
2329 	for (m=0; m<sp->samples_per_pixel_per_plane; m++)
2330 	{
2331 		/* Cs */
2332 		sp->out_buffer[5+m*2]=sp->sos_cs[sp->plane_sample_offset+m];
2333 		/* Td and Ta */
2334 		sp->out_buffer[5+m*2+1]=sp->sos_tda[sp->plane_sample_offset+m];
2335 	}
2336 	/* Ss */
2337 	sp->out_buffer[5+sp->samples_per_pixel_per_plane*2]=0;
2338 	/* Se */
2339 	sp->out_buffer[5+sp->samples_per_pixel_per_plane*2+1]=63;
2340 	/* Ah and Al */
2341 	sp->out_buffer[5+sp->samples_per_pixel_per_plane*2+2]=0;
2342 	*len=8+sp->samples_per_pixel_per_plane*2;
2343 	*mem=(void*)sp->out_buffer;
2344 	sp->out_state++;
2345 }
2346 
2347 static int
OJPEGWriteStreamCompressed(TIFF * tif,void ** mem,uint32 * len)2348 OJPEGWriteStreamCompressed(TIFF* tif, void** mem, uint32* len)
2349 {
2350 	OJPEGState* sp=(OJPEGState*)tif->tif_data;
2351 	if (sp->in_buffer_togo==0)
2352 	{
2353 		if (OJPEGReadBufferFill(sp)==0)
2354 			return(0);
2355 		assert(sp->in_buffer_togo>0);
2356 	}
2357 	*len=sp->in_buffer_togo;
2358 	*mem=(void*)sp->in_buffer_cur;
2359 	sp->in_buffer_togo=0;
2360 	if (sp->in_buffer_file_togo==0)
2361 	{
2362 		switch(sp->in_buffer_source)
2363 		{
2364 			case osibsStrile:
2365 				if (sp->in_buffer_next_strile<sp->in_buffer_strile_count)
2366 					sp->out_state=ososRst;
2367 				else
2368 					sp->out_state=ososEoi;
2369 				break;
2370 			case osibsEof:
2371 				sp->out_state=ososEoi;
2372 				break;
2373 			default:
2374 				break;
2375 		}
2376 	}
2377 	return(1);
2378 }
2379 
2380 static void
OJPEGWriteStreamRst(TIFF * tif,void ** mem,uint32 * len)2381 OJPEGWriteStreamRst(TIFF* tif, void** mem, uint32* len)
2382 {
2383 	OJPEGState* sp=(OJPEGState*)tif->tif_data;
2384 	assert(OJPEG_BUFFER>=2);
2385 	sp->out_buffer[0]=255;
2386 	sp->out_buffer[1]=JPEG_MARKER_RST0+sp->restart_index;
2387 	sp->restart_index++;
2388 	if (sp->restart_index==8)
2389 		sp->restart_index=0;
2390 	*len=2;
2391 	*mem=(void*)sp->out_buffer;
2392 	sp->out_state=ososCompressed;
2393 }
2394 
2395 static void
OJPEGWriteStreamEoi(TIFF * tif,void ** mem,uint32 * len)2396 OJPEGWriteStreamEoi(TIFF* tif, void** mem, uint32* len)
2397 {
2398 	OJPEGState* sp=(OJPEGState*)tif->tif_data;
2399 	assert(OJPEG_BUFFER>=2);
2400 	sp->out_buffer[0]=255;
2401 	sp->out_buffer[1]=JPEG_MARKER_EOI;
2402 	*len=2;
2403 	*mem=(void*)sp->out_buffer;
2404 }
2405 
2406 #ifndef LIBJPEG_ENCAP_EXTERNAL
2407 static int
jpeg_create_decompress_encap(OJPEGState * sp,jpeg_decompress_struct * cinfo)2408 jpeg_create_decompress_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo)
2409 {
2410 	if( SETJMP(sp->exit_jmpbuf) )
2411 		return 0;
2412 	else {
2413 		jpeg_create_decompress(cinfo);
2414 		return 1;
2415 	}
2416 }
2417 #endif
2418 
2419 #ifndef LIBJPEG_ENCAP_EXTERNAL
2420 static int
jpeg_read_header_encap(OJPEGState * sp,jpeg_decompress_struct * cinfo,uint8 require_image)2421 jpeg_read_header_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo, uint8 require_image)
2422 {
2423 	if( SETJMP(sp->exit_jmpbuf) )
2424 		return 0;
2425 	else {
2426 		jpeg_read_header(cinfo,require_image);
2427 		return 1;
2428 	}
2429 }
2430 #endif
2431 
2432 #ifndef LIBJPEG_ENCAP_EXTERNAL
2433 static int
jpeg_start_decompress_encap(OJPEGState * sp,jpeg_decompress_struct * cinfo)2434 jpeg_start_decompress_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo)
2435 {
2436 	if( SETJMP(sp->exit_jmpbuf) )
2437 		return 0;
2438 	else {
2439 		jpeg_start_decompress(cinfo);
2440 		return 1;
2441 	}
2442 }
2443 #endif
2444 
2445 #ifndef LIBJPEG_ENCAP_EXTERNAL
2446 static int
jpeg_read_scanlines_encap(OJPEGState * sp,jpeg_decompress_struct * cinfo,void * scanlines,uint32 max_lines)2447 jpeg_read_scanlines_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo, void* scanlines, uint32 max_lines)
2448 {
2449 	if( SETJMP(sp->exit_jmpbuf) )
2450 		return 0;
2451 	else {
2452 		jpeg_read_scanlines(cinfo,scanlines,max_lines);
2453 		return 1;
2454 	}
2455 }
2456 #endif
2457 
2458 #ifndef LIBJPEG_ENCAP_EXTERNAL
2459 static int
jpeg_read_raw_data_encap(OJPEGState * sp,jpeg_decompress_struct * cinfo,void * data,uint32 max_lines)2460 jpeg_read_raw_data_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo, void* data, uint32 max_lines)
2461 {
2462 	if( SETJMP(sp->exit_jmpbuf) )
2463 		return 0;
2464 	else {
2465 		jpeg_read_raw_data(cinfo,data,max_lines);
2466 		return 1;
2467 	}
2468 }
2469 #endif
2470 
2471 #ifndef LIBJPEG_ENCAP_EXTERNAL
2472 static void
jpeg_encap_unwind(TIFF * tif)2473 jpeg_encap_unwind(TIFF* tif)
2474 {
2475 	OJPEGState* sp=(OJPEGState*)tif->tif_data;
2476 	LONGJMP(sp->exit_jmpbuf,1);
2477 }
2478 #endif
2479 
2480 static void
OJPEGLibjpegJpegErrorMgrOutputMessage(jpeg_common_struct * cinfo)2481 OJPEGLibjpegJpegErrorMgrOutputMessage(jpeg_common_struct* cinfo)
2482 {
2483 	char buffer[JMSG_LENGTH_MAX];
2484 	(*cinfo->err->format_message)(cinfo,buffer);
2485 	TIFFWarningExt(((TIFF*)(cinfo->client_data))->tif_clientdata,"LibJpeg","%s",buffer);
2486 }
2487 
2488 static void
OJPEGLibjpegJpegErrorMgrErrorExit(jpeg_common_struct * cinfo)2489 OJPEGLibjpegJpegErrorMgrErrorExit(jpeg_common_struct* cinfo)
2490 {
2491 	char buffer[JMSG_LENGTH_MAX];
2492 	(*cinfo->err->format_message)(cinfo,buffer);
2493 	TIFFErrorExt(((TIFF*)(cinfo->client_data))->tif_clientdata,"LibJpeg","%s",buffer);
2494 	jpeg_encap_unwind((TIFF*)(cinfo->client_data));
2495 }
2496 
2497 static void
OJPEGLibjpegJpegSourceMgrInitSource(jpeg_decompress_struct * cinfo)2498 OJPEGLibjpegJpegSourceMgrInitSource(jpeg_decompress_struct* cinfo)
2499 {
2500 	(void)cinfo;
2501 }
2502 
2503 static boolean
OJPEGLibjpegJpegSourceMgrFillInputBuffer(jpeg_decompress_struct * cinfo)2504 OJPEGLibjpegJpegSourceMgrFillInputBuffer(jpeg_decompress_struct* cinfo)
2505 {
2506 	TIFF* tif=(TIFF*)cinfo->client_data;
2507 	OJPEGState* sp=(OJPEGState*)tif->tif_data;
2508 	void* mem=0;
2509 	uint32 len=0U;
2510 	if (OJPEGWriteStream(tif,&mem,&len)==0)
2511 	{
2512 		TIFFErrorExt(tif->tif_clientdata,"LibJpeg","Premature end of JPEG data");
2513 		jpeg_encap_unwind(tif);
2514 	}
2515 	sp->libjpeg_jpeg_source_mgr.bytes_in_buffer=len;
2516 	sp->libjpeg_jpeg_source_mgr.next_input_byte=mem;
2517 	return(1);
2518 }
2519 
2520 static void
OJPEGLibjpegJpegSourceMgrSkipInputData(jpeg_decompress_struct * cinfo,long num_bytes)2521 OJPEGLibjpegJpegSourceMgrSkipInputData(jpeg_decompress_struct* cinfo, long num_bytes)
2522 {
2523 	TIFF* tif=(TIFF*)cinfo->client_data;
2524 	(void)num_bytes;
2525 	TIFFErrorExt(tif->tif_clientdata,"LibJpeg","Unexpected error");
2526 	jpeg_encap_unwind(tif);
2527 }
2528 
2529 #ifdef _MSC_VER
2530 #pragma warning( push )
2531 #pragma warning( disable : 4702 ) /* unreachable code */
2532 #endif
2533 static boolean
OJPEGLibjpegJpegSourceMgrResyncToRestart(jpeg_decompress_struct * cinfo,int desired)2534 OJPEGLibjpegJpegSourceMgrResyncToRestart(jpeg_decompress_struct* cinfo, int desired)
2535 {
2536 	TIFF* tif=(TIFF*)cinfo->client_data;
2537 	(void)desired;
2538 	TIFFErrorExt(tif->tif_clientdata,"LibJpeg","Unexpected error");
2539 	jpeg_encap_unwind(tif);
2540 	return(0);
2541 }
2542 #ifdef _MSC_VER
2543 #pragma warning( pop )
2544 #endif
2545 
2546 static void
OJPEGLibjpegJpegSourceMgrTermSource(jpeg_decompress_struct * cinfo)2547 OJPEGLibjpegJpegSourceMgrTermSource(jpeg_decompress_struct* cinfo)
2548 {
2549 	(void)cinfo;
2550 }
2551 
2552 #endif
2553 
2554 
2555 /*
2556  * Local Variables:
2557  * mode: c
2558  * c-basic-offset: 8
2559  * fill-column: 78
2560  * End:
2561  */
2562