1 /* Copyright (C) 2001-2019 Artifex Software, Inc.
2    All Rights Reserved.
3 
4    This software is provided AS-IS with no warranty, either express or
5    implied.
6 
7    This software is distributed under license and may not be copied,
8    modified or distributed except as expressly authorized under the terms
9    of the license contained in the file LICENSE in this distribution.
10 
11    Refer to licensing information at http://www.artifex.com or contact
12    Artifex Software, Inc.,  1305 Grant Avenue - Suite 200, Novato,
13    CA 94945, U.S.A., +1(415)492-9861, for further information.
14 */
15 
16 
17 /* Interface routines for IJG decoding code. */
18 #include "stdio_.h"
19 #include "string_.h"
20 #include "jpeglib_.h"
21 #include "jerror_.h"
22 #include "gserrors.h"
23 #include "gx.h"
24 #include "strimpl.h"
25 #include "sdct.h"
26 #include "sjpeg.h"
27 
28 /*
29  * Interface routines.  This layer of routines exists solely to limit
30  * side-effects from using setjmp.
31  */
32 
33 int
gs_jpeg_create_decompress(stream_DCT_state * st)34 gs_jpeg_create_decompress(stream_DCT_state * st)
35 {				/* Initialize error handling */
36     gs_jpeg_error_setup(st);
37     /* Establish the setjmp return context for gs_jpeg_error_exit to use. */
38     if (setjmp(find_jmp_buf(st->data.common->exit_jmpbuf)))
39         return_error(gs_jpeg_log_error(st));
40 
41     jpeg_stream_data_common_init(st->data.decompress);
42 
43     if (gs_jpeg_mem_init (st->memory, (j_common_ptr)&st->data.decompress->dinfo) < 0)
44         return_error(gs_error_VMerror);
45 
46     jpeg_create_decompress(&st->data.decompress->dinfo);
47     return 0;
48 }
49 
50 int
gs_jpeg_read_header(stream_DCT_state * st,boolean require_image)51 gs_jpeg_read_header(stream_DCT_state * st,
52                     boolean require_image)
53 {
54     if (setjmp(find_jmp_buf(st->data.common->exit_jmpbuf)))
55         return_error(gs_jpeg_log_error(st));
56     return jpeg_read_header(&st->data.decompress->dinfo, require_image);
57 }
58 
59 int
gs_jpeg_start_decompress(stream_DCT_state * st)60 gs_jpeg_start_decompress(stream_DCT_state * st)
61 {
62     if (setjmp(find_jmp_buf(st->data.common->exit_jmpbuf)))
63         return_error(gs_jpeg_log_error(st));
64 #if JPEG_LIB_VERSION > 55
65     return (int)jpeg_start_decompress(&st->data.decompress->dinfo);
66 #else
67     /* in IJG version 5, jpeg_start_decompress had no return value */
68     jpeg_start_decompress(&st->data.decompress->dinfo);
69     return 1;
70 #endif
71 }
72 
73 int
gs_jpeg_read_scanlines(stream_DCT_state * st,JSAMPARRAY scanlines,int max_lines)74 gs_jpeg_read_scanlines(stream_DCT_state * st,
75                        JSAMPARRAY scanlines,
76                        int max_lines)
77 {
78     if (setjmp(find_jmp_buf(st->data.common->exit_jmpbuf)))
79         return_error(gs_jpeg_log_error(st));
80     return (int)jpeg_read_scanlines(&st->data.decompress->dinfo,
81                                     scanlines, (JDIMENSION) max_lines);
82 }
83 
84 int
gs_jpeg_finish_decompress(stream_DCT_state * st)85 gs_jpeg_finish_decompress(stream_DCT_state * st)
86 {
87     int code = 0;
88     if (setjmp(find_jmp_buf(st->data.common->exit_jmpbuf)))
89         code = gs_note_error(gs_jpeg_log_error(st));
90     if (code >= 0)
91         code = (int)jpeg_finish_decompress(&st->data.decompress->dinfo);
92     stream_dct_end_passthrough(st->data.decompress);
93     return code;
94 }
95