Commit 2264e7a9 authored by Thomas Freitag's avatar Thomas Freitag Committed by Albert Astals Cid

implement Crypt filter

Bug #62800
parent a65a4e6f
......@@ -18,6 +18,7 @@
// Copyright (C) 2012 Fabio D'Urso <fabiodurso@hotmail.it>
// Copyright (C) 2013 Adrian Johnson <ajohnson@redneon.com>
// Copyright (C) 2013 Albert Astals Cid <aacid@kde.org>
// Copyright (C) 2013 Thomas Freitag <Thomas.Freitag@alfa.de>
//
// To see a description of the changes please see the Changelog file that
// came with your tarball or type make ChangeLog if you are building from git
......@@ -105,7 +106,7 @@ public:
BaseCryptStream(Stream *strA, Guchar *fileKey, CryptAlgorithm algoA,
int keyLength, int objNum, int objGen);
virtual ~BaseCryptStream();
virtual StreamKind getKind() { return strWeird; }
virtual StreamKind getKind() { return strCrypt; }
virtual void reset();
virtual int getChar();
virtual int lookChar() = 0;
......
......@@ -1144,12 +1144,41 @@ void PDFDoc::writeObject (Object* obj, OutStream* outStr, XRef *xRef, Guint numO
//We can't modify stream with the current implementation (no write functions in Stream API)
// => the only type of streams which that have been modified are internal streams (=strWeird)
Stream *stream = obj->getStream();
if (stream->getKind() == strWeird) {
if (stream->getKind() == strWeird || stream->getKind() == strCrypt) {
//we write the stream unencoded => TODO: write stream encoder
// Encrypt stream
EncryptStream *encStream = NULL;
if (fileKey) {
GBool removeFilter = gTrue;
if (stream->getKind() == strWeird && fileKey) {
Object filter;
stream->getDict()->lookup("Filter", &filter);
if (!filter.isName("Crypt")) {
if (filter.isArray()) {
for (int i = 0; i < filter.arrayGetLength(); i++) {
Object filterEle;
filter.arrayGet(i, &filterEle);
if (filterEle.isName("Crypt")) {
filterEle.free();
removeFilter = gFalse;
break;
}
}
if (removeFilter) {
encStream = new EncryptStream(stream, fileKey, encAlgorithm, keyLength, objNum, objGen);
encStream->setAutoDelete(gFalse);
stream = encStream;
}
} else {
encStream = new EncryptStream(stream, fileKey, encAlgorithm, keyLength, objNum, objGen);
encStream->setAutoDelete(gFalse);
stream = encStream;
}
} else {
removeFilter = gFalse;
}
filter.free();
} else if (fileKey != NULL) { // Encrypt stream
encStream = new EncryptStream(stream, fileKey, encAlgorithm, keyLength, objNum, objGen);
encStream->setAutoDelete(gFalse);
stream = encStream;
......@@ -1165,7 +1194,9 @@ void PDFDoc::writeObject (Object* obj, OutStream* outStr, XRef *xRef, Guint numO
stream->getDict()->set("Length", &obj1);
//Remove Stream encoding
stream->getDict()->remove("Filter");
if (removeFilter) {
stream->getDict()->remove("Filter");
}
stream->getDict()->remove("DecodeParms");
writeDictionnary (stream->getDict(),outStr, xRef, numOffset, fileKey, encAlgorithm, keyLength, objNum, objGen);
......
......@@ -342,6 +342,12 @@ Stream *Stream::makeFilter(char *name, Stream *str, Object *params, int recursio
globals.free();
} else if (!strcmp(name, "JPXDecode")) {
str = new JPXStream(str);
} else if (!strcmp(name, "Crypt")) {
if (str->getKind() == strCrypt) {
str = str->getBaseStream();
} else {
error(errSyntaxError, getPos(), "Can't revert non decrypt streams");
}
} else {
error(errSyntaxError, getPos(), "Unknown filter '{0:s}'", name);
str = new EOFStream(str);
......
......@@ -62,7 +62,8 @@ enum StreamKind {
strFlate,
strJBIG2,
strJPX,
strWeird // internal-use stream types
strWeird, // internal-use stream types
strCrypt // internal-use to detect decode streams
};
enum StreamColorSpaceMode {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment