1 /*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7 #include "src/codec/SkJpegDecoderMgr.h"
8
9 #include "include/core/SkTypes.h"
10 #include "src/codec/SkCodecPriv.h"
11 #include "src/codec/SkJpegSourceMgr.h"
12 #include "src/codec/SkJpegUtility.h"
13
14 #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
15 #include "include/android/SkAndroidFrameworkUtils.h"
16 #endif
17
18 #include <jpeglib.h>
19 #include <cstddef>
20 #include <utility>
21
22 class SkStream;
23
24 /*
25 * Print information, warning, and error messages
26 */
print_message(const j_common_ptr info,const char caller[])27 static void print_message(const j_common_ptr info, const char caller[]) {
28 [[maybe_unused]] char buffer[JMSG_LENGTH_MAX];
29 SkCodecPrintf("libjpeg error %d <%s> from %s\n",
30 info->err->msg_code,
31 (info->err->format_message(info, buffer), buffer),
32 caller);
33 }
34
35 /*
36 * Reporting function for error and warning messages.
37 */
output_message(j_common_ptr info)38 static void output_message(j_common_ptr info) {
39 print_message(info, "output_message");
40 }
41
progress_monitor(j_common_ptr info)42 static void progress_monitor(j_common_ptr info) {
43 int scan = ((j_decompress_ptr)info)->input_scan_number;
44 // Progressive images with a very large number of scans can cause the
45 // decoder to hang. Here we use the progress monitor to abort on
46 // a very large number of scans. 100 is arbitrary, but much larger
47 // than the number of scans we might expect in a normal image.
48 if (scan >= 100) {
49 skjpeg_err_exit(info);
50 }
51 }
52
53 ////////////////////////////////////////////////////////////////////////////////////////////////////
54 // JpegDecoderMgr
55
returnFalse(const char caller[])56 bool JpegDecoderMgr::returnFalse(const char caller[]) {
57 print_message((j_common_ptr) &fDInfo, caller);
58 return false;
59 }
60
returnFailure(const char caller[],SkCodec::Result result)61 SkCodec::Result JpegDecoderMgr::returnFailure(const char caller[], SkCodec::Result result) {
62 print_message((j_common_ptr) &fDInfo, caller);
63 return result;
64 }
65
getEncodedColor(SkEncodedInfo::Color * outColor)66 bool JpegDecoderMgr::getEncodedColor(SkEncodedInfo::Color* outColor) {
67 switch (fDInfo.jpeg_color_space) {
68 case JCS_GRAYSCALE:
69 *outColor = SkEncodedInfo::kGray_Color;
70 return true;
71 case JCS_YCbCr:
72 *outColor = SkEncodedInfo::kYUV_Color;
73 return true;
74 case JCS_RGB:
75 #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
76 SkAndroidFrameworkUtils::SafetyNetLog("118372692");
77 #endif
78 *outColor = SkEncodedInfo::kRGB_Color;
79 return true;
80 case JCS_YCCK:
81 *outColor = SkEncodedInfo::kYCCK_Color;
82 return true;
83 case JCS_CMYK:
84 *outColor = SkEncodedInfo::kInvertedCMYK_Color;
85 return true;
86 default:
87 return false;
88 }
89 }
90
getSourceMgr()91 SkJpegSourceMgr* JpegDecoderMgr::getSourceMgr() {
92 return fSrcMgr.fSourceMgr.get();
93 }
94
JpegDecoderMgr(SkStream * stream)95 JpegDecoderMgr::JpegDecoderMgr(SkStream* stream)
96 : fSrcMgr(SkJpegSourceMgr::Make(stream)), fInit(false) {
97 // An error manager must be set before any calls to libjpeg, in order to handle failures.
98 fDInfo.err = jpeg_std_error(&fErrorMgr);
99 fErrorMgr.error_exit = skjpeg_err_exit;
100 }
101
init()102 void JpegDecoderMgr::init() {
103 jpeg_create_decompress(&fDInfo);
104 fInit = true;
105 fDInfo.src = &fSrcMgr;
106 fDInfo.err->output_message = &output_message;
107 fDInfo.progress = &fProgressMgr;
108 fProgressMgr.progress_monitor = &progress_monitor;
109 }
110
~JpegDecoderMgr()111 JpegDecoderMgr::~JpegDecoderMgr() {
112 if (fInit) {
113 jpeg_destroy_decompress(&fDInfo);
114 }
115 }
116
117 ////////////////////////////////////////////////////////////////////////////////////////////////////
118 // JpegDecoderMgr::SourceMgr
119
120 // static
InitSource(j_decompress_ptr dinfo)121 void JpegDecoderMgr::SourceMgr::InitSource(j_decompress_ptr dinfo) {
122 JpegDecoderMgr::SourceMgr* src = (JpegDecoderMgr::SourceMgr*)dinfo->src;
123 src->fSourceMgr->initSource(src->next_input_byte, src->bytes_in_buffer);
124 }
125
126 // static
SkipInputData(j_decompress_ptr dinfo,long num_bytes_long)127 void JpegDecoderMgr::SourceMgr::SkipInputData(j_decompress_ptr dinfo, long num_bytes_long) {
128 JpegDecoderMgr::SourceMgr* src = (JpegDecoderMgr::SourceMgr*)dinfo->src;
129 size_t num_bytes = static_cast<size_t>(num_bytes_long);
130 if (!src->fSourceMgr->skipInputBytes(num_bytes, src->next_input_byte, src->bytes_in_buffer)) {
131 SkCodecPrintf("Failure to skip.\n");
132 src->next_input_byte = nullptr;
133 src->bytes_in_buffer = 0;
134 dinfo->err->error_exit((j_common_ptr)dinfo);
135 }
136 }
137
138 // static
FillInputBuffer(j_decompress_ptr dinfo)139 boolean JpegDecoderMgr::SourceMgr::FillInputBuffer(j_decompress_ptr dinfo) {
140 JpegDecoderMgr::SourceMgr* src = (JpegDecoderMgr::SourceMgr*)dinfo->src;
141 if (!src->fSourceMgr->fillInputBuffer(src->next_input_byte, src->bytes_in_buffer)) {
142 SkCodecPrintf("Failure to fill input buffer.\n");
143 src->next_input_byte = nullptr;
144 src->bytes_in_buffer = 0;
145 return false;
146 }
147 return true;
148 }
149
150 // static
TermSource(j_decompress_ptr dinfo)151 void JpegDecoderMgr::SourceMgr::TermSource(j_decompress_ptr dinfo) {}
152
SourceMgr(std::unique_ptr<SkJpegSourceMgr> sourceMgr)153 JpegDecoderMgr::SourceMgr::SourceMgr(std::unique_ptr<SkJpegSourceMgr> sourceMgr)
154 : fSourceMgr(std::move(sourceMgr)) {
155 init_source = JpegDecoderMgr::SourceMgr::InitSource;
156 fill_input_buffer = JpegDecoderMgr::SourceMgr::FillInputBuffer;
157 skip_input_data = JpegDecoderMgr::SourceMgr::SkipInputData;
158 resync_to_restart = jpeg_resync_to_restart;
159 term_source = JpegDecoderMgr::SourceMgr::TermSource;
160 }
161