1
0
Fork 0
mirror of https://github.com/geometer/FBReaderJ.git synced 2025-10-05 19:42:17 +02:00

no "encryption method" for entire epub, just encryption info map for separate files

This commit is contained in:
Nikolay Pultsin 2014-04-06 10:27:55 +01:00
parent 4fb3888b07
commit cb08c3bc3c
16 changed files with 86 additions and 60 deletions

View file

@ -41,13 +41,13 @@ LOCAL_SRC_FILES := \
NativeFormats/util/AndroidUtil.cpp \
NativeFormats/util/JniEnvelope.cpp \
NativeFormats/zlibrary/core/src/constants/ZLXMLNamespace.cpp \
NativeFormats/zlibrary/core/src/drm/FileEncryptionInfo.cpp \
NativeFormats/zlibrary/core/src/encoding/DummyEncodingConverter.cpp \
NativeFormats/zlibrary/core/src/encoding/Utf16EncodingConverters.cpp \
NativeFormats/zlibrary/core/src/encoding/Utf8EncodingConverter.cpp \
NativeFormats/zlibrary/core/src/encoding/JavaEncodingConverter.cpp \
NativeFormats/zlibrary/core/src/encoding/ZLEncodingCollection.cpp \
NativeFormats/zlibrary/core/src/encoding/ZLEncodingConverter.cpp \
NativeFormats/zlibrary/core/src/encryption/FileEncryptionInfo.cpp \
NativeFormats/zlibrary/core/src/filesystem/ZLDir.cpp \
NativeFormats/zlibrary/core/src/filesystem/ZLFSManager.cpp \
NativeFormats/zlibrary/core/src/filesystem/ZLFile.cpp \
@ -158,8 +158,8 @@ LOCAL_SRC_FILES := \
LOCAL_C_INCLUDES := \
$(LOCAL_PATH)/NativeFormats/util \
$(LOCAL_PATH)/NativeFormats/zlibrary/core/src/constants \
$(LOCAL_PATH)/NativeFormats/zlibrary/core/src/drm \
$(LOCAL_PATH)/NativeFormats/zlibrary/core/src/encoding \
$(LOCAL_PATH)/NativeFormats/zlibrary/core/src/encryption \
$(LOCAL_PATH)/NativeFormats/zlibrary/core/src/filesystem \
$(LOCAL_PATH)/NativeFormats/zlibrary/core/src/image \
$(LOCAL_PATH)/NativeFormats/zlibrary/core/src/language \

View file

@ -132,14 +132,27 @@ JNIEXPORT jint JNICALL Java_org_geometerplus_fbreader_formats_NativeFormatPlugin
}
extern "C"
JNIEXPORT jstring JNICALL Java_org_geometerplus_fbreader_formats_NativeFormatPlugin_readEncryptionMethod(JNIEnv* env, jobject thiz, jobject javaBook) {
JNIEXPORT jobject JNICALL Java_org_geometerplus_fbreader_formats_NativeFormatPlugin_readEncryptionInfosNative(JNIEnv* env, jobject thiz, jobject javaBook) {
shared_ptr<FormatPlugin> plugin = findCppPlugin(thiz);
if (plugin.isNull()) {
return AndroidUtil::createJavaString(env, EncryptionMethod::UNSUPPORTED);
return 0;
}
shared_ptr<Book> book = Book::loadFromJavaBook(env, javaBook);
return AndroidUtil::createJavaString(env, plugin->readEncryptionMethod(*book));
std::vector<shared_ptr<FileEncryptionInfo> > infos = plugin->readEncryptionInfos(*book);
if (infos.empty()) {
return 0;
}
jobjectArray jList = env->NewObjectArray(
infos.size(), AndroidUtil::Class_FileEncryptionInfo.j(), 0
);
for (std::size_t i = 0; i < infos.size(); ++i) {
jobject jInfo = AndroidUtil::createJavaEncryptionInfo(env, infos[i]);
env->SetObjectArrayElement(jList, i, jInfo);
env->DeleteLocalRef(jInfo);
}
return jList;
}
extern "C"

View file

@ -99,8 +99,8 @@ const std::string &FormatPlugin::tryOpen(const ZLFile&) const {
return EMPTY;
}
std::string FormatPlugin::readEncryptionMethod(Book &book) const {
return EncryptionMethod::NONE;
std::vector<shared_ptr<FileEncryptionInfo> > FormatPlugin::readEncryptionInfos(Book &book) const {
return std::vector<shared_ptr<FileEncryptionInfo> >();
}
shared_ptr<const ZLImage> FormatPlugin::coverImage(const ZLFile &file) const {

View file

@ -29,6 +29,7 @@
class Book;
class BookModel;
class FileEncryptionInfo;
//class ZLOptionsDialog;
//class ZLOptionsDialogTab;
class ZLFile;
@ -58,7 +59,7 @@ public:
virtual const std::string &tryOpen(const ZLFile &file) const;
virtual bool readMetaInfo(Book &book) const = 0;
virtual std::string readEncryptionMethod(Book &book) const;
virtual std::vector<shared_ptr<FileEncryptionInfo> > readEncryptionInfos(Book &book) const;
virtual bool readUids(Book &book) const = 0;
virtual bool readLanguageAndEncoding(Book &book) const = 0;
virtual bool readModel(BookModel &model) const = 0;

View file

@ -17,6 +17,8 @@
* 02110-1301, USA.
*/
#include <algorithm>
#include <ZLDir.h>
#include <ZLXMLNamespace.h>
#include <ZLXMLReader.h>
@ -43,7 +45,7 @@ class EpubEncryptionFileReader : public ZLXMLReader {
public:
EpubEncryptionFileReader();
void addKnownMethod(const std::string &method);
const std::vector<shared_ptr<FileEncryptionInfo> > &infos() const;
private:
@ -64,6 +66,7 @@ private:
};
private:
std::vector<std::string> myKnownMethods;
std::vector<shared_ptr<FileEncryptionInfo> > myInfos;
State myState;
@ -72,38 +75,31 @@ private:
std::string myUri;
};
std::string OEBEncryptionReader::readEncryptionMethod(const ZLFile &epubFile) {
static const std::string EMBEDDING_ALGORITHM = "http://www.idpf.org/2008/embedding";
std::vector<shared_ptr<FileEncryptionInfo> > OEBEncryptionReader::readEncryptionInfos(const ZLFile &epubFile) {
shared_ptr<ZLDir> epubDir = epubFile.directory();
if (epubDir.isNull()) {
return EncryptionMethod::UNSUPPORTED;
return std::vector<shared_ptr<FileEncryptionInfo> >();
}
const ZLFile rightsFile(epubDir->itemPath("META-INF/rights.xml"));
const ZLFile encryptionFile(epubDir->itemPath("META-INF/encryption.xml"));
if (!rightsFile.exists() && !encryptionFile.exists()) {
return EncryptionMethod::NONE;
}
if (!rightsFile.exists() || !encryptionFile.exists()) {
return EncryptionMethod::UNSUPPORTED;
if (!encryptionFile.exists()) {
return std::vector<shared_ptr<FileEncryptionInfo> >();
}
EpubRightsFileReader reader;
reader.readDocument(rightsFile);
return reader.method();
}
EpubEncryptionFileReader reader = EpubEncryptionFileReader();
std::vector<shared_ptr<FileEncryptionInfo> > OEBEncryptionReader::readEncryptionInfos(const ZLFile &epubFile) {
const std::string method = readEncryptionMethod(epubFile);
if (method == EncryptionMethod::MARLIN) {
shared_ptr<ZLDir> epubDir = epubFile.directory();
if (!epubDir.isNull()) {
const ZLFile encryptionFile(epubDir->itemPath("META-INF/encryption.xml"));
EpubEncryptionFileReader reader = EpubEncryptionFileReader();
reader.readDocument(encryptionFile);
return reader.infos();
}
if (rightsFile.exists()) {
EpubRightsFileReader rightsReader;
rightsReader.readDocument(rightsFile);
reader.addKnownMethod(rightsReader.method());
}
return std::vector<shared_ptr<FileEncryptionInfo> >();
reader.readDocument(encryptionFile);
return reader.infos();
}
EpubRightsFileReader::EpubRightsFileReader() : myMethod(EncryptionMethod::UNSUPPORTED) {
@ -127,6 +123,10 @@ bool EpubRightsFileReader::processNamespaces() const {
EpubEncryptionFileReader::EpubEncryptionFileReader() : myState(READ_NONE) {
}
void EpubEncryptionFileReader::addKnownMethod(const std::string &method) {
myKnownMethods.push_back(method);
}
const std::vector<shared_ptr<FileEncryptionInfo> > &EpubEncryptionFileReader::infos() const {
return myInfos;
}
@ -203,7 +203,19 @@ void EpubEncryptionFileReader::endElementHandler(const char *tag) {
break;
case READ_ENCRYPTED_DATA:
if (testTag(ZLXMLNamespace::XMLEncryption, "EncryptedData", tag)) {
myInfos.push_back(new FileEncryptionInfo(myUri, EncryptionMethod::MARLIN, myAlgorithm, myKeyName));
if (EMBEDDING_ALGORITHM == myAlgorithm) {
myInfos.push_back(new FileEncryptionInfo(
myUri, EncryptionMethod::EMBEDDING, myAlgorithm, std::string()
));
} else {
std::vector<std::string>::const_iterator it =
std::find(myKnownMethods.begin(), myKnownMethods.end(), EncryptionMethod::MARLIN);
if (it != myKnownMethods.end()) {
myInfos.push_back(new FileEncryptionInfo(
myUri, EncryptionMethod::MARLIN, myAlgorithm, myKeyName
));
}
}
myState = READ_ENCRYPTION;
}
break;

View file

@ -32,7 +32,6 @@ class OEBEncryptionReader {
public:
std::vector<shared_ptr<FileEncryptionInfo> > readEncryptionInfos(const ZLFile &file);
std::string readEncryptionMethod(const ZLFile &file);
};
#endif /* __OEBENCRYPTIONREADER_H__ */

View file

@ -129,8 +129,8 @@ bool OEBPlugin::readMetaInfo(Book &book) const {
return OEBMetaInfoReader(book).readMetaInfo(opfFile(file));
}
std::string OEBPlugin::readEncryptionMethod(Book &book) const {
return OEBEncryptionReader().readEncryptionMethod(epubFile(book.file()));
std::vector<shared_ptr<FileEncryptionInfo> > OEBPlugin::readEncryptionInfos(Book &book) const {
return OEBEncryptionReader().readEncryptionInfos(epubFile(book.file()));
}
bool OEBPlugin::readUids(Book &book) const {

View file

@ -33,7 +33,7 @@ public:
bool providesMetaInfo() const;
const std::string supportedFileType() const;
bool readMetaInfo(Book &book) const;
std::string readEncryptionMethod(Book &book) const;
virtual std::vector<shared_ptr<FileEncryptionInfo> > readEncryptionInfos(Book &book) const;
bool readUids(Book &book) const;
bool readLanguageAndEncoding(Book &book) const;
bool readModel(BookModel &model) const;

View file

@ -22,6 +22,7 @@ package org.geometerplus.fbreader.fbreader;
import java.util.*;
import org.geometerplus.zlibrary.core.application.*;
import org.geometerplus.zlibrary.core.drm.FileEncryptionInfo;
import org.geometerplus.zlibrary.core.drm.EncryptionMethod;
import org.geometerplus.zlibrary.core.library.ZLibrary;
import org.geometerplus.zlibrary.core.options.*;
@ -282,9 +283,11 @@ public final class FBReaderApp extends ZLApplication {
getViewWidget().repaint();
try {
final String method = book.getPlugin().readEncryptionMethod(book);
if (!EncryptionMethod.NONE.equals(method)) {
showErrorMessage("unsupportedEncryptionMethod", book.File.getPath());
for (FileEncryptionInfo info : book.getPlugin().readEncryptionInfos(book)) {
if (!EncryptionMethod.NONE.equals(info.Method)) {
showErrorMessage("unsupportedEncryptionMethod", book.File.getPath());
break;
}
}
} catch (BookReadingException e) {
// ignore

View file

@ -19,8 +19,12 @@
package org.geometerplus.fbreader.formats;
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
import java.util.Collections;
import java.util.List;
import org.geometerplus.zlibrary.core.drm.FileEncryptionInfo;
import org.geometerplus.zlibrary.core.encodings.EncodingCollection;
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
import org.geometerplus.zlibrary.core.image.ZLImage;
import org.geometerplus.fbreader.book.Book;
@ -41,8 +45,10 @@ public abstract class FormatPlugin {
public ZLFile realBookFile(ZLFile file) throws BookReadingException {
return file;
}
public List<FileEncryptionInfo> readEncryptionInfos(Book book) {
return Collections.emptyList();
}
public abstract void readMetaInfo(Book book) throws BookReadingException;
public abstract String readEncryptionMethod(Book book);
public abstract void readUids(Book book) throws BookReadingException;
public abstract void readModel(BookModel model) throws BookReadingException;
public abstract void detectLanguageAndEncoding(Book book) throws BookReadingException;

View file

@ -19,6 +19,9 @@
package org.geometerplus.fbreader.formats;
import java.util.*;
import org.geometerplus.zlibrary.core.drm.FileEncryptionInfo;
import org.geometerplus.zlibrary.core.encodings.EncodingCollection;
import org.geometerplus.zlibrary.core.encodings.JavaEncodingCollection;
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
@ -61,7 +64,14 @@ public class NativeFormatPlugin extends FormatPlugin {
private native int readMetaInfoNative(Book book);
@Override
public native String readEncryptionMethod(Book book);
public List<FileEncryptionInfo> readEncryptionInfos(Book book) {
final FileEncryptionInfo[] infos = readEncryptionInfosNative(book);
return infos != null
? Arrays.<FileEncryptionInfo>asList(infos)
: Collections.<FileEncryptionInfo>emptyList();
}
private native FileEncryptionInfo[] readEncryptionInfosNative(Book book);
@Override
synchronized public void readUids(Book book) throws BookReadingException {

View file

@ -19,7 +19,6 @@
package org.geometerplus.fbreader.formats.fb2;
import org.geometerplus.zlibrary.core.drm.EncryptionMethod;
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
import org.geometerplus.zlibrary.core.encodings.AutoEncodingCollection;
import org.geometerplus.zlibrary.core.image.ZLImage;
@ -48,11 +47,6 @@ public class FB2Plugin extends JavaFormatPlugin {
new FB2MetaInfoReader(book).readMetaInfo();
}
@Override
public String readEncryptionMethod(Book book) {
return EncryptionMethod.NONE;
}
@Override
public void readUids(Book book) throws BookReadingException {
// this method does nothing, we expect it will be never called

View file

@ -19,7 +19,6 @@
package org.geometerplus.fbreader.formats.oeb;
import org.geometerplus.zlibrary.core.drm.EncryptionMethod;
import org.geometerplus.zlibrary.core.filesystem.*;
import org.geometerplus.zlibrary.core.encodings.AutoEncodingCollection;
import org.geometerplus.zlibrary.core.image.ZLImage;
@ -62,11 +61,6 @@ public class OEBPlugin extends JavaFormatPlugin {
new OEBMetaInfoReader(book).readMetaInfo(getOpfFile(book.File));
}
@Override
public String readEncryptionMethod(Book book) {
return EncryptionMethod.NONE;
}
@Override
public void readUids(Book book) throws BookReadingException {
// this method does nothing, we expect it will be never called

View file

@ -21,7 +21,6 @@ package org.geometerplus.fbreader.formats.pdb;
import java.io.*;
import org.geometerplus.zlibrary.core.drm.EncryptionMethod;
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
import org.geometerplus.zlibrary.core.image.*;
import org.geometerplus.zlibrary.core.encodings.Encoding;
@ -124,11 +123,6 @@ public class MobipocketPlugin extends JavaFormatPlugin {
}
}
@Override
public String readEncryptionMethod(Book book) {
return EncryptionMethod.NONE;
}
@Override
public void readUids(Book book) throws BookReadingException {
if (book.uids().isEmpty()) {