Commit d70f77ee authored by Albert Astals Cid's avatar Albert Astals Cid

Decrypt: take a Ref instead of two int

parent 242c5368
Pipeline #56079 passed with stages
in 8 minutes and 9 seconds
......@@ -305,7 +305,7 @@ bool Decrypt::makeFileKey2(int encVersion, int encRevision, int keyLength,
//------------------------------------------------------------------------
BaseCryptStream::BaseCryptStream(Stream *strA, const unsigned char *fileKey, CryptAlgorithm algoA,
int keyLength, int objNum, int objGen):
int keyLength, Ref refA):
FilterStream(strA)
{
algo = algoA;
......@@ -321,11 +321,11 @@ BaseCryptStream::BaseCryptStream(Stream *strA, const unsigned char *fileKey, Cry
switch (algo) {
case cryptRC4:
if (likely(keyLength < static_cast<int>(sizeof(objKey) - 4))) {
objKey[keyLength] = objNum & 0xff;
objKey[keyLength + 1] = (objNum >> 8) & 0xff;
objKey[keyLength + 2] = (objNum >> 16) & 0xff;
objKey[keyLength + 3] = objGen & 0xff;
objKey[keyLength + 4] = (objGen >> 8) & 0xff;
objKey[keyLength] = refA.num & 0xff;
objKey[keyLength + 1] = (refA.num >> 8) & 0xff;
objKey[keyLength + 2] = (refA.num >> 16) & 0xff;
objKey[keyLength + 3] = refA.gen & 0xff;
objKey[keyLength + 4] = (refA.gen >> 8) & 0xff;
md5(objKey, keyLength + 5, objKey);
}
if ((objKeyLength = keyLength + 5) > 16) {
......@@ -333,11 +333,11 @@ BaseCryptStream::BaseCryptStream(Stream *strA, const unsigned char *fileKey, Cry
}
break;
case cryptAES:
objKey[keyLength] = objNum & 0xff;
objKey[keyLength + 1] = (objNum >> 8) & 0xff;
objKey[keyLength + 2] = (objNum >> 16) & 0xff;
objKey[keyLength + 3] = objGen & 0xff;
objKey[keyLength + 4] = (objGen >> 8) & 0xff;
objKey[keyLength] = refA.num & 0xff;
objKey[keyLength + 1] = (refA.num >> 8) & 0xff;
objKey[keyLength + 2] = (refA.num >> 16) & 0xff;
objKey[keyLength + 3] = refA.gen & 0xff;
objKey[keyLength + 4] = (refA.gen >> 8) & 0xff;
objKey[keyLength + 5] = 0x73; // 's'
objKey[keyLength + 6] = 0x41; // 'A'
objKey[keyLength + 7] = 0x6c; // 'l'
......@@ -397,8 +397,8 @@ void BaseCryptStream::setAutoDelete(bool val) {
//------------------------------------------------------------------------
EncryptStream::EncryptStream(Stream *strA, const unsigned char *fileKey, CryptAlgorithm algoA,
int keyLength, int objNum, int objGen):
BaseCryptStream(strA, fileKey, algoA, keyLength, objNum, objGen)
int keyLength, Ref refA):
BaseCryptStream(strA, fileKey, algoA, keyLength, refA)
{
// Fill the CBC initialization vector for AES and AES-256
switch (algo) {
......@@ -489,8 +489,8 @@ int EncryptStream::lookChar() {
//------------------------------------------------------------------------
DecryptStream::DecryptStream(Stream *strA, const unsigned char *fileKey, CryptAlgorithm algoA,
int keyLength, int objNum, int objGen):
BaseCryptStream(strA, fileKey, algoA, keyLength, objNum, objGen)
int keyLength, Ref refA):
BaseCryptStream(strA, fileKey, algoA, keyLength, refA)
{
}
......
......@@ -17,7 +17,7 @@
// Copyright (C) 2009 David Benjamin <davidben@mit.edu>
// Copyright (C) 2012 Fabio D'Urso <fabiodurso@hotmail.it>
// Copyright (C) 2013 Adrian Johnson <ajohnson@redneon.com>
// Copyright (C) 2013, 2018 Albert Astals Cid <aacid@kde.org>
// Copyright (C) 2013, 2018, 2019 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
......@@ -99,7 +99,7 @@ class BaseCryptStream : public FilterStream {
public:
BaseCryptStream(Stream *strA, const unsigned char *fileKey, CryptAlgorithm algoA,
int keyLength, int objNum, int objGen);
int keyLength, Ref ref);
~BaseCryptStream();
StreamKind getKind() override { return strCrypt; }
void reset() override;
......@@ -133,7 +133,7 @@ class EncryptStream : public BaseCryptStream {
public:
EncryptStream(Stream *strA, const unsigned char *fileKey, CryptAlgorithm algoA,
int keyLength, int objNum, int objGen);
int keyLength, Ref ref);
~EncryptStream();
void reset() override;
int lookChar() override;
......@@ -143,7 +143,7 @@ class DecryptStream : public BaseCryptStream {
public:
DecryptStream(Stream *strA, const unsigned char *fileKey, CryptAlgorithm algoA,
int keyLength, int objNum, int objGen);
int keyLength, Ref ref);
~DecryptStream();
void reset() override;
int lookChar() override;
......
......@@ -1308,7 +1308,7 @@ void PDFDoc::writeString (const GooString* s, OutStream* outStr, const unsigned
GooString *sEnc = nullptr;
if (fileKey) {
EncryptStream *enc = new EncryptStream(new MemStream(s->c_str(), 0, s->getLength(), Object(objNull)),
fileKey, encAlgorithm, keyLength, ref.num, ref.gen);
fileKey, encAlgorithm, keyLength, ref);
sEnc = new GooString();
int c;
enc->reset();
......@@ -1440,12 +1440,12 @@ void PDFDoc::writeObject (Object* obj, OutStream* outStr, XRef *xRef, unsigned i
}
}
if (removeFilter) {
encStream = new EncryptStream(stream, fileKey, encAlgorithm, keyLength, ref.num, ref.gen);
encStream = new EncryptStream(stream, fileKey, encAlgorithm, keyLength, ref);
encStream->setAutoDelete(false);
stream = encStream;
}
} else {
encStream = new EncryptStream(stream, fileKey, encAlgorithm, keyLength, ref.num, ref.gen);
encStream = new EncryptStream(stream, fileKey, encAlgorithm, keyLength, ref);
encStream->setAutoDelete(false);
stream = encStream;
}
......@@ -1453,7 +1453,7 @@ void PDFDoc::writeObject (Object* obj, OutStream* outStr, XRef *xRef, unsigned i
removeFilter = false;
}
} else if (fileKey != nullptr) { // Encrypt stream
encStream = new EncryptStream(stream, fileKey, encAlgorithm, keyLength, ref.num, ref.gen);
encStream = new EncryptStream(stream, fileKey, encAlgorithm, keyLength, ref);
encStream->setAutoDelete(false);
stream = encStream;
}
......@@ -1476,7 +1476,7 @@ void PDFDoc::writeObject (Object* obj, OutStream* outStr, XRef *xRef, unsigned i
writeStream (stream,outStr);
delete encStream;
} else if (fileKey != nullptr && stream->getKind() == strFile && static_cast<FileStream*>(stream)->getNeedsEncryptionOnSave()) {
EncryptStream *encStream = new EncryptStream(stream, fileKey, encAlgorithm, keyLength, ref.num, ref.gen);
EncryptStream *encStream = new EncryptStream(stream, fileKey, encAlgorithm, keyLength, ref);
encStream->setAutoDelete(false);
writeDictionnary (encStream->getDict(), outStr, xRef, numOffset, fileKey, encAlgorithm, keyLength, ref, alreadyWrittenDicts);
writeStream (encStream, outStr);
......
......@@ -172,7 +172,7 @@ Object Parser::getObj(bool simpleOnly,
s2 = new GooString();
decrypt = new DecryptStream(new MemStream(s->c_str(), 0, s->getLength(), Object(objNull)),
fileKey, encAlgorithm, keyLength,
objNum, objGen);
{objNum, objGen});
decrypt->reset();
while ((c = decrypt->getChar()) != EOF) {
s2->append((char)c);
......@@ -294,7 +294,7 @@ Stream *Parser::makeStream(Object &&dict, unsigned char *fileKey,
// handle decryption
if (fileKey) {
str = new DecryptStream(str, fileKey, encAlgorithm, keyLength,
objNum, objGen);
{objNum, objGen});
}
// get filters
......
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