Skip to content

Commit 6c8f347

Browse files
authored
fix decryption (#395)
1 parent 7ac4fab commit 6c8f347

4 files changed

Lines changed: 131 additions & 33 deletions

File tree

app/conanfile.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[requires]
2-
odrcore/5.0.0
2+
odrcore/5.0.1
33

44
[generators]
55
CMakeToolchain

app/src/main/cpp/CoreWrapper.cpp

Lines changed: 60 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <odr/document.hpp>
44
#include <odr/file.hpp>
55
#include <odr/html.hpp>
6+
#include <odr/logger.hpp>
67
#include <odr/odr.hpp>
78
#include <odr/exceptions.hpp>
89
#include <odr/http_server.hpp>
@@ -35,6 +36,43 @@ std::string getStringField(JNIEnv *env, jobject object, const char *name) {
3536
return getStringField(env, clazz, object, name);
3637
}
3738

39+
class AndroidLogger final : public odr::Logger {
40+
public:
41+
static int to_android_log_level(odr::LogLevel level) {
42+
switch (level) {
43+
case odr::LogLevel::verbose:
44+
return ANDROID_LOG_VERBOSE;
45+
case odr::LogLevel::debug:
46+
return ANDROID_LOG_DEBUG;
47+
case odr::LogLevel::info:
48+
return ANDROID_LOG_INFO;
49+
case odr::LogLevel::warning:
50+
return ANDROID_LOG_WARN;
51+
case odr::LogLevel::error:
52+
return ANDROID_LOG_ERROR;
53+
case odr::LogLevel::fatal:
54+
return ANDROID_LOG_FATAL;
55+
default:
56+
return ANDROID_LOG_UNKNOWN;
57+
}
58+
}
59+
60+
void flush() override {}
61+
62+
[[nodiscard]] bool will_log(odr::LogLevel level) const override {
63+
return true;
64+
}
65+
66+
protected:
67+
void log_impl(Time time, odr::LogLevel level, const std::string &message,
68+
const std::source_location &location) override {
69+
__android_log_print(to_android_log_level(level), "smn", "%s", message.c_str());
70+
}
71+
72+
private:
73+
74+
};
75+
3876
}
3977

4078
std::optional<odr::Document> s_document;
@@ -60,6 +98,8 @@ Java_at_tomtasche_reader_background_CoreWrapper_setGlobalParams(JNIEnv *env, jcl
6098
JNIEXPORT jobject JNICALL
6199
Java_at_tomtasche_reader_background_CoreWrapper_parseNative(JNIEnv *env, jclass clazz,
62100
jobject options) {
101+
auto logger = std::make_shared<AndroidLogger>();
102+
63103
jclass resultClass = env->FindClass("at/tomtasche/reader/background/CoreWrapper$CoreResult");
64104
jmethodID resultConstructor = env->GetMethodID(resultClass, "<init>", "()V");
65105
jobject result = env->NewObject(resultClass, resultConstructor);
@@ -106,7 +146,7 @@ Java_at_tomtasche_reader_background_CoreWrapper_parseNative(JNIEnv *env, jclass
106146
try {
107147
odr::FileType fileType;
108148
try {
109-
const auto types = odr::list_file_types(inputPathCpp);
149+
const auto types = odr::list_file_types(inputPathCpp, *logger);
110150
if (types.empty()) {
111151
env->SetIntField(result, errorField, -5);
112152
return result;
@@ -125,7 +165,7 @@ Java_at_tomtasche_reader_background_CoreWrapper_parseNative(JNIEnv *env, jclass
125165

126166
__android_log_print(ANDROID_LOG_VERBOSE, "smn", "Open %s", inputPathCpp.c_str());
127167

128-
auto file = odr::open(inputPathCpp);
168+
auto file = odr::open(inputPathCpp, *logger);
129169

130170
if (file.password_encrypted()) {
131171
if (!passwordCpp.has_value()) {
@@ -149,15 +189,15 @@ Java_at_tomtasche_reader_background_CoreWrapper_parseNative(JNIEnv *env, jclass
149189
extension = env->NewStringUTF(extensionCpp.c_str());
150190
env->SetObjectField(result, extensionField, extension);
151191

152-
odr::HtmlConfig config;
153-
config.editable = editable;
154-
config.text_document_margin = paging;
192+
odr::HtmlConfig htmlConfig;
193+
htmlConfig.editable = editable;
194+
htmlConfig.text_document_margin = paging;
155195

156196
__android_log_print(ANDROID_LOG_VERBOSE, "smn", "Translate to HTML");
157197

158198
std::string output_tmp = outputPathCpp + "/tmp";
159199
std::filesystem::create_directories(output_tmp);
160-
odr::HtmlService service = odr::html::translate(file, output_tmp, config);
200+
odr::HtmlService service = odr::html::translate(file, output_tmp, htmlConfig, logger);
161201
odr::Html html = service.bring_offline(outputPathCpp);
162202
std::filesystem::remove_all(output_tmp);
163203

@@ -259,7 +299,7 @@ Java_at_tomtasche_reader_background_CoreWrapper_closeNative(JNIEnv *env, jclass
259299
std::optional<odr::HttpServer> s_server;
260300

261301
JNIEXPORT void JNICALL
262-
Java_at_tomtasche_reader_background_CoreWrapper_createServer(JNIEnv *env, jclass clazz,
302+
Java_at_tomtasche_reader_background_CoreWrapper_createServerNative(JNIEnv *env, jclass clazz,
263303
jstring cachePath) {
264304
__android_log_print(ANDROID_LOG_INFO, "smn", "create server");
265305

@@ -273,10 +313,12 @@ Java_at_tomtasche_reader_background_CoreWrapper_createServer(JNIEnv *env, jclass
273313
}
274314

275315
JNIEXPORT jobject JNICALL
276-
Java_at_tomtasche_reader_background_CoreWrapper_hostFile(JNIEnv *env, jclass clazz, jstring prefix,
316+
Java_at_tomtasche_reader_background_CoreWrapper_hostFileNative(JNIEnv *env, jclass clazz, jstring prefix,
277317
jobject options) {
278318
__android_log_print(ANDROID_LOG_INFO, "smn", "host file");
279319

320+
auto logger = std::make_shared<AndroidLogger>();
321+
280322
jclass resultClass = env->FindClass("at/tomtasche/reader/background/CoreWrapper$CoreResult");
281323
jmethodID resultConstructor = env->GetMethodID(resultClass, "<init>", "()V");
282324
jobject result = env->NewObject(resultClass, resultConstructor);
@@ -305,6 +347,8 @@ Java_at_tomtasche_reader_background_CoreWrapper_hostFile(JNIEnv *env, jclass cla
305347
jfieldID editableField = env->GetFieldID(optionsClass, "editable", "Z");
306348
jboolean editable = env->GetBooleanField(options, editableField);
307349

350+
std::string outputPathCpp = getStringField(env, optionsClass, options, "outputPath");
351+
308352
jclass listClass = env->FindClass("java/util/List");
309353
jmethodID addMethod = env->GetMethodID(listClass, "add", "(Ljava/lang/Object;)Z");
310354

@@ -320,7 +364,7 @@ Java_at_tomtasche_reader_background_CoreWrapper_hostFile(JNIEnv *env, jclass cla
320364
odr::DecodePreference decodePreference;
321365
decodePreference.engine_priority = {odr::DecoderEngine::poppler, odr::DecoderEngine::wvware,
322366
odr::DecoderEngine::odr};
323-
odr::DecodedFile file = odr::open(inputPathCpp, decodePreference);
367+
odr::DecodedFile file = odr::open(inputPathCpp, decodePreference, *logger);
324368

325369
if (file.password_encrypted()) {
326370
if (!passwordCpp.has_value()) {
@@ -348,7 +392,11 @@ Java_at_tomtasche_reader_background_CoreWrapper_hostFile(JNIEnv *env, jclass cla
348392
htmlConfig.editable = editable;
349393

350394
try {
351-
odr::HtmlViews htmlViews = s_server->serve_file(file, prefixCpp, htmlConfig);
395+
std::string output_tmp = outputPathCpp + "/tmp";
396+
std::filesystem::create_directories(output_tmp);
397+
odr::HtmlService service = odr::html::translate(file, output_tmp, htmlConfig, logger);
398+
s_server->connect_service(service, prefixCpp);
399+
odr::HtmlViews htmlViews = service.list_views();
352400

353401
for (const auto &view: htmlViews) {
354402
__android_log_print(ANDROID_LOG_INFO, "smn", "view name=%s path=%s", view.name().c_str(), view.path().c_str());
@@ -392,7 +440,7 @@ Java_at_tomtasche_reader_background_CoreWrapper_hostFile(JNIEnv *env, jclass cla
392440
}
393441

394442
JNIEXPORT void JNICALL
395-
Java_at_tomtasche_reader_background_CoreWrapper_listenServer(JNIEnv *env, jclass clazz, jint port) {
443+
Java_at_tomtasche_reader_background_CoreWrapper_listenServerNative(JNIEnv *env, jclass clazz, jint port) {
396444
__android_log_print(ANDROID_LOG_INFO, "smn", "listen ...");
397445

398446
s_server->listen("127.0.0.1", port);
@@ -401,7 +449,7 @@ Java_at_tomtasche_reader_background_CoreWrapper_listenServer(JNIEnv *env, jclass
401449
}
402450

403451
JNIEXPORT void JNICALL
404-
Java_at_tomtasche_reader_background_CoreWrapper_stopServer(JNIEnv *env, jclass clazz) {
452+
Java_at_tomtasche_reader_background_CoreWrapper_stopServerNative(JNIEnv *env, jclass clazz) {
405453
__android_log_print(ANDROID_LOG_INFO, "smn", "stop server");
406454

407455
s_server->stop();

app/src/main/cpp/CoreWrapper.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@ extern "C" {
1919
Java_at_tomtasche_reader_background_CoreWrapper_closeNative(JNIEnv *env, jclass clazz, jobject options);
2020

2121
JNIEXPORT void JNICALL
22-
Java_at_tomtasche_reader_background_CoreWrapper_createServer(JNIEnv *env, jclass clazz, jstring outputPath);
22+
Java_at_tomtasche_reader_background_CoreWrapper_createServerNative(JNIEnv *env, jclass clazz, jstring outputPath);
2323

2424
JNIEXPORT jobject JNICALL
25-
Java_at_tomtasche_reader_background_CoreWrapper_hostFile(JNIEnv *env, jclass clazz, jstring prefix, jobject options);
25+
Java_at_tomtasche_reader_background_CoreWrapper_hostFileNative(JNIEnv *env, jclass clazz, jstring prefix, jobject options);
2626

2727
JNIEXPORT void JNICALL
28-
Java_at_tomtasche_reader_background_CoreWrapper_listenServer(JNIEnv *env, jclass clazz, jint port);
28+
Java_at_tomtasche_reader_background_CoreWrapper_listenServerNative(JNIEnv *env, jclass clazz, jint port);
2929

3030
JNIEXPORT void JNICALL
31-
Java_at_tomtasche_reader_background_CoreWrapper_stopServer(JNIEnv *env, jclass clazz);
31+
Java_at_tomtasche_reader_background_CoreWrapper_stopServerNative(JNIEnv *env, jclass clazz);
3232
}
3333

3434
#endif //ANDROID_CORE_WRAPPER_H

app/src/main/java/at/tomtasche/reader/background/CoreWrapper.java

Lines changed: 66 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ public static CoreResult parse(CoreOptions options) {
8282
break;
8383
default:
8484
result.exception = new CoreUnexpectedErrorCodeException();
85+
break;
8586
}
8687

8788
return result;
@@ -95,18 +96,18 @@ public static CoreResult backtranslate(CoreOptions options, String htmlDiff) {
9596
switch (result.errorCode) {
9697
case 0:
9798
break;
98-
9999
case -3:
100100
result.exception = new CoreUnknownErrorException();
101-
101+
break;
102102
case -6:
103103
result.exception = new CoreCouldNotEditException();
104-
104+
break;
105105
case -7:
106106
result.exception = new CoreCouldNotSaveException();
107-
107+
break;
108108
default:
109109
result.exception = new CoreUnexpectedErrorCodeException();
110+
break;
110111
}
111112

112113
return result;
@@ -122,13 +123,54 @@ public static void close() {
122123

123124
private static native void closeNative(CoreOptions options);
124125

125-
public static native void createServer(String cachePath);
126+
public static void createServer(String cachePath) {
127+
createServerNative(cachePath);
128+
}
129+
130+
private static native void createServerNative(String cachePath);
131+
132+
public static CoreResult hostFile(String prefix, CoreOptions options) {
133+
CoreResult result = hostFileNative(prefix, options);
134+
135+
switch (result.errorCode) {
136+
case 0:
137+
break;
138+
case -1:
139+
result.exception = new CoreCouldNotOpenException();
140+
break;
141+
case -2:
142+
result.exception = new CoreEncryptedException();
143+
break;
144+
case -3:
145+
result.exception = new CoreUnknownErrorException();
146+
break;
147+
case -4:
148+
result.exception = new CoreCouldNotTranslateException();
149+
break;
150+
case -5:
151+
result.exception = new CoreUnexpectedFormatException();
152+
break;
153+
default:
154+
result.exception = new CoreUnexpectedErrorCodeException();
155+
break;
156+
}
157+
158+
return result;
159+
}
126160

127-
public static native CoreResult hostFile(String prefix, CoreOptions options);
161+
private static native CoreResult hostFileNative(String prefix, CoreOptions options);
128162

129-
public static native void listenServer(int port);
163+
public static void listenServer(int port) {
164+
listenServerNative(port);
165+
}
166+
167+
private static native void listenServerNative(int port);
168+
169+
public static void stopServer() {
170+
stopServerNative();
171+
}
130172

131-
public static native void stopServer();
173+
private static native void stopServerNative();
132174

133175
public static class CoreResult {
134176
public int errorCode;
@@ -143,19 +185,27 @@ public static class CoreResult {
143185
public String extension;
144186
}
145187

146-
public static class CoreCouldNotOpenException extends RuntimeException {}
188+
public static class CoreCouldNotOpenException extends RuntimeException {
189+
}
147190

148-
public static class CoreEncryptedException extends RuntimeException {}
191+
public static class CoreEncryptedException extends RuntimeException {
192+
}
149193

150-
public static class CoreCouldNotTranslateException extends RuntimeException {}
194+
public static class CoreCouldNotTranslateException extends RuntimeException {
195+
}
151196

152-
public static class CoreUnexpectedFormatException extends RuntimeException {}
197+
public static class CoreUnexpectedFormatException extends RuntimeException {
198+
}
153199

154-
public static class CoreUnexpectedErrorCodeException extends RuntimeException {}
200+
public static class CoreUnexpectedErrorCodeException extends RuntimeException {
201+
}
155202

156-
public static class CoreUnknownErrorException extends RuntimeException {}
203+
public static class CoreUnknownErrorException extends RuntimeException {
204+
}
157205

158-
public static class CoreCouldNotEditException extends RuntimeException {}
206+
public static class CoreCouldNotEditException extends RuntimeException {
207+
}
159208

160-
public static class CoreCouldNotSaveException extends RuntimeException {}
209+
public static class CoreCouldNotSaveException extends RuntimeException {
210+
}
161211
}

0 commit comments

Comments
 (0)