1
0
Fork 0
mirror of https://github.com/geometer/FBReaderJ.git synced 2025-10-03 01:39:18 +02:00

support for utf8-encoded zip entry names

This commit is contained in:
Nikolay Pultsin 2015-10-23 03:43:55 +01:00
parent b512d3505f
commit 4e4e780f03
2 changed files with 39 additions and 7 deletions

View file

@ -89,12 +89,43 @@ final class MyBufferedInputStream extends InputStream {
return (fourthByte << 24) + (thirdByte << 16) + (secondByte << 8) + firstByte; return (fourthByte << 24) + (thirdByte << 16) + (secondByte << 8) + firstByte;
} }
String readString(int stringLength) throws IOException { private static final boolean isUtf8String(byte[] array) {
char[] array = new char[stringLength]; int nonLeadingCharsCounter = 0;
for (int i = 0; i < stringLength; i++) { for (byte b : array) {
array[i] = (char)read(); if (nonLeadingCharsCounter == 0) {
if ((b & 0x80) != 0) {
if ((b & 0xE0) == 0xC0) {
nonLeadingCharsCounter = 1;
} else if ((b & 0xF0) == 0xE0) {
nonLeadingCharsCounter = 2;
} else if ((b & 0xF8) == 0xF0) {
nonLeadingCharsCounter = 3;
} else {
return false;
}
}
} else {
if ((b & 0xC0) != 0x80) {
return false;
}
--nonLeadingCharsCounter;
}
} }
return new String(array); return nonLeadingCharsCounter == 0;
}
String readString(int stringLength) throws IOException {
final byte[] array = new byte[stringLength];
read(array);
if (isUtf8String(array)) {
return new String(array, "utf-8");
}
final char[] chars = new char[stringLength];
for (int i = 0; i < stringLength; i++) {
chars[i] = (char)(array[i] & 0xFF);
}
return new String(chars);
} }
@Override @Override

View file

@ -20,6 +20,7 @@
#include <ZLFile.h> #include <ZLFile.h>
#include <FileEncryptionInfo.h> #include <FileEncryptionInfo.h>
#include <ZLFileImage.h> #include <ZLFileImage.h>
#include <ZLUnicodeUtil.h>
#include "AndroidUtil.h" #include "AndroidUtil.h"
#include "JniEnvelope.h" #include "JniEnvelope.h"
@ -288,13 +289,13 @@ jstring AndroidUtil::createJavaString(JNIEnv* env, const std::string &str) {
} }
std::string AndroidUtil::convertNonUtfString(const std::string &str) { std::string AndroidUtil::convertNonUtfString(const std::string &str) {
const int len = str.length(); if (ZLUnicodeUtil::isUtf8String(str)) {
if (len == 0) {
return str; return str;
} }
JNIEnv *env = getEnv(); JNIEnv *env = getEnv();
const int len = str.length();
jchar *chars = new jchar[len]; jchar *chars = new jchar[len];
for (int i = 0; i < len; ++i) { for (int i = 0; i < len; ++i) {
chars[i] = (unsigned char)str[i]; chars[i] = (unsigned char)str[i];