1
0
Fork 0
mirror of https://github.com/geometer/FBReaderJ.git synced 2025-10-05 10:49:24 +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/AndroidUtil.cpp \
NativeFormats/util/JniEnvelope.cpp \ NativeFormats/util/JniEnvelope.cpp \
NativeFormats/zlibrary/core/src/constants/ZLXMLNamespace.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/DummyEncodingConverter.cpp \
NativeFormats/zlibrary/core/src/encoding/Utf16EncodingConverters.cpp \ NativeFormats/zlibrary/core/src/encoding/Utf16EncodingConverters.cpp \
NativeFormats/zlibrary/core/src/encoding/Utf8EncodingConverter.cpp \ NativeFormats/zlibrary/core/src/encoding/Utf8EncodingConverter.cpp \
NativeFormats/zlibrary/core/src/encoding/JavaEncodingConverter.cpp \ NativeFormats/zlibrary/core/src/encoding/JavaEncodingConverter.cpp \
NativeFormats/zlibrary/core/src/encoding/ZLEncodingCollection.cpp \ NativeFormats/zlibrary/core/src/encoding/ZLEncodingCollection.cpp \
NativeFormats/zlibrary/core/src/encoding/ZLEncodingConverter.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/ZLDir.cpp \
NativeFormats/zlibrary/core/src/filesystem/ZLFSManager.cpp \ NativeFormats/zlibrary/core/src/filesystem/ZLFSManager.cpp \
NativeFormats/zlibrary/core/src/filesystem/ZLFile.cpp \ NativeFormats/zlibrary/core/src/filesystem/ZLFile.cpp \
@ -158,8 +158,8 @@ LOCAL_SRC_FILES := \
LOCAL_C_INCLUDES := \ LOCAL_C_INCLUDES := \
$(LOCAL_PATH)/NativeFormats/util \ $(LOCAL_PATH)/NativeFormats/util \
$(LOCAL_PATH)/NativeFormats/zlibrary/core/src/constants \ $(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/encoding \
$(LOCAL_PATH)/NativeFormats/zlibrary/core/src/encryption \
$(LOCAL_PATH)/NativeFormats/zlibrary/core/src/filesystem \ $(LOCAL_PATH)/NativeFormats/zlibrary/core/src/filesystem \
$(LOCAL_PATH)/NativeFormats/zlibrary/core/src/image \ $(LOCAL_PATH)/NativeFormats/zlibrary/core/src/image \
$(LOCAL_PATH)/NativeFormats/zlibrary/core/src/language \ $(LOCAL_PATH)/NativeFormats/zlibrary/core/src/language \

View file

@ -132,14 +132,27 @@ JNIEXPORT jint JNICALL Java_org_geometerplus_fbreader_formats_NativeFormatPlugin
} }
extern "C" 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); shared_ptr<FormatPlugin> plugin = findCppPlugin(thiz);
if (plugin.isNull()) { if (plugin.isNull()) {
return AndroidUtil::createJavaString(env, EncryptionMethod::UNSUPPORTED); return 0;
} }
shared_ptr<Book> book = Book::loadFromJavaBook(env, javaBook); 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" extern "C"

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -33,7 +33,7 @@ public:
bool providesMetaInfo() const; bool providesMetaInfo() const;
const std::string supportedFileType() const; const std::string supportedFileType() const;
bool readMetaInfo(Book &book) 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 readUids(Book &book) const;
bool readLanguageAndEncoding(Book &book) const; bool readLanguageAndEncoding(Book &book) const;
bool readModel(BookModel &model) const; bool readModel(BookModel &model) const;

View file

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

View file

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

View file

@ -19,6 +19,9 @@
package org.geometerplus.fbreader.formats; 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.EncodingCollection;
import org.geometerplus.zlibrary.core.encodings.JavaEncodingCollection; import org.geometerplus.zlibrary.core.encodings.JavaEncodingCollection;
import org.geometerplus.zlibrary.core.filesystem.ZLFile; import org.geometerplus.zlibrary.core.filesystem.ZLFile;
@ -61,7 +64,14 @@ public class NativeFormatPlugin extends FormatPlugin {
private native int readMetaInfoNative(Book book); private native int readMetaInfoNative(Book book);
@Override @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 @Override
synchronized public void readUids(Book book) throws BookReadingException { synchronized public void readUids(Book book) throws BookReadingException {

View file

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

View file

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