1 /*
2  * Copyright (C) 2013 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 
18 #include "jerr_hook.h"
19 #include "jni_defines.h"
20 
21 /**
22  * Replaces libjpeg's error_exit function, returns control to
23  * the point
24  */
ErrExit(j_common_ptr cinfo)25 void ErrExit(j_common_ptr cinfo) {
26     ErrManager* mgr = reinterpret_cast<ErrManager*>(cinfo->err);
27     (*cinfo->err->output_message) (cinfo);
28     // Returns control to error handling in jpeg_writer
29     longjmp(mgr->setjmp_buf, 1);
30 }
31 
32 /**
33  * Replaces libjpeg's output_message function, writes message
34  * to logcat's error log.
35  */
ErrOutput(j_common_ptr cinfo)36 void ErrOutput(j_common_ptr cinfo) {
37     ErrManager* mgr = reinterpret_cast<ErrManager*>(cinfo->err);
38     char buf[JMSG_LENGTH_MAX];
39     (*cinfo->err->format_message) (cinfo, buf);
40     buf[JMSG_LENGTH_MAX - 1] = '\0';  // Force null terminator
41     // Output error message in ndk logcat.
42     LOGE("%s\n", buf);
43 }
44 
SetupErrMgr(j_common_ptr cinfo,ErrManager * errMgr)45 void SetupErrMgr(j_common_ptr cinfo, ErrManager* errMgr) {
46     jpeg_std_error(&(errMgr->mgr));
47     errMgr->mgr.error_exit = ErrExit;
48     errMgr->mgr.output_message = ErrOutput;
49     cinfo->err = reinterpret_cast<struct jpeg_error_mgr*>(errMgr);
50 }
51 
52 
53