Replace FXCIPHER_* defines with enum class
Bug: pdfium:1085
Change-Id: Ib947160ba60885f93a7b22c5e453a060b12a7425
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/80570
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fpdfapi/parser/cpdf_crypto_handler.cpp b/core/fpdfapi/parser/cpdf_crypto_handler.cpp
index 2a0fe42..b51d2fb 100644
--- a/core/fpdfapi/parser/cpdf_crypto_handler.cpp
+++ b/core/fpdfapi/parser/cpdf_crypto_handler.cpp
@@ -50,23 +50,23 @@
pdfium::span<const uint8_t> source,
uint8_t* dest_buf,
uint32_t& dest_size) const {
- if (m_Cipher == FXCIPHER_NONE) {
+ if (m_Cipher == Cipher::kNone) {
memcpy(dest_buf, source.data(), source.size());
return;
}
uint8_t realkey[16];
size_t realkeylen = sizeof(realkey);
- if (m_Cipher != FXCIPHER_AES || m_KeyLen != 32) {
+ if (m_Cipher != Cipher::kAES || m_KeyLen != 32) {
uint8_t key1[32];
PopulateKey(objnum, gennum, key1);
- if (m_Cipher == FXCIPHER_AES)
+ if (m_Cipher == Cipher::kAES)
memcpy(key1 + m_KeyLen + 5, "sAlT", 4);
- size_t len = m_Cipher == FXCIPHER_AES ? m_KeyLen + 9 : m_KeyLen + 5;
+ size_t len = m_Cipher == Cipher::kAES ? m_KeyLen + 9 : m_KeyLen + 5;
CRYPT_MD5Generate({key1, len}, realkey);
realkeylen = std::min(m_KeyLen + 5, sizeof(realkey));
}
- if (m_Cipher == FXCIPHER_AES) {
+ if (m_Cipher == Cipher::kAES) {
CRYPT_AESSetKey(m_pAESContext.get(),
m_KeyLen == 32 ? m_EncryptKey : realkey, m_KeyLen,
bEncrypt);
@@ -112,10 +112,10 @@
void* CPDF_CryptoHandler::CryptStart(uint32_t objnum,
uint32_t gennum,
bool bEncrypt) {
- if (m_Cipher == FXCIPHER_NONE) {
+ if (m_Cipher == Cipher::kNone) {
return this;
}
- if (m_Cipher == FXCIPHER_AES && m_KeyLen == 32) {
+ if (m_Cipher == Cipher::kAES && m_KeyLen == 32) {
AESCryptContext* pContext = FX_Alloc(AESCryptContext, 1);
pContext->m_bIV = true;
pContext->m_BlockOffset = 0;
@@ -131,15 +131,15 @@
uint8_t key1[48];
PopulateKey(objnum, gennum, key1);
- if (m_Cipher == FXCIPHER_AES)
+ if (m_Cipher == Cipher::kAES)
memcpy(key1 + m_KeyLen + 5, "sAlT", 4);
uint8_t realkey[16];
- size_t len = m_Cipher == FXCIPHER_AES ? m_KeyLen + 9 : m_KeyLen + 5;
+ size_t len = m_Cipher == Cipher::kAES ? m_KeyLen + 9 : m_KeyLen + 5;
CRYPT_MD5Generate({key1, len}, realkey);
size_t realkeylen = std::min(m_KeyLen + 5, sizeof(realkey));
- if (m_Cipher == FXCIPHER_AES) {
+ if (m_Cipher == Cipher::kAES) {
AESCryptContext* pContext = FX_Alloc(AESCryptContext, 1);
pContext->m_bIV = true;
pContext->m_BlockOffset = 0;
@@ -164,11 +164,11 @@
if (!context)
return false;
- if (m_Cipher == FXCIPHER_NONE) {
+ if (m_Cipher == Cipher::kNone) {
dest_buf.AppendBlock(source.data(), source.size());
return true;
}
- if (m_Cipher == FXCIPHER_RC4) {
+ if (m_Cipher == Cipher::kRC4) {
int old_size = dest_buf.GetSize();
dest_buf.AppendBlock(source.data(), source.size());
CRYPT_ArcFourCrypt(static_cast<CRYPT_rc4_context*>(context),
@@ -222,10 +222,10 @@
if (!context) {
return false;
}
- if (m_Cipher == FXCIPHER_NONE) {
+ if (m_Cipher == Cipher::kNone) {
return true;
}
- if (m_Cipher == FXCIPHER_RC4) {
+ if (m_Cipher == Cipher::kRC4) {
FX_Free(context);
return true;
}
@@ -267,11 +267,11 @@
return CryptStart(objnum, gennum, false);
}
uint32_t CPDF_CryptoHandler::DecryptGetSize(uint32_t src_size) {
- return m_Cipher == FXCIPHER_AES ? src_size - 16 : src_size;
+ return m_Cipher == Cipher::kAES ? src_size - 16 : src_size;
}
bool CPDF_CryptoHandler::IsCipherAES() const {
- return m_Cipher == FXCIPHER_AES;
+ return m_Cipher == Cipher::kAES;
}
bool CPDF_CryptoHandler::DecryptObjectTree(RetainPtr<CPDF_Object> object) {
@@ -367,7 +367,7 @@
size_t CPDF_CryptoHandler::EncryptGetSize(
pdfium::span<const uint8_t> source) const {
- return m_Cipher == FXCIPHER_AES ? source.size() + 32 : source.size();
+ return m_Cipher == Cipher::kAES ? source.size() + 32 : source.size();
}
bool CPDF_CryptoHandler::EncryptContent(uint32_t objnum,
@@ -379,19 +379,19 @@
return true;
}
-CPDF_CryptoHandler::CPDF_CryptoHandler(int cipher,
+CPDF_CryptoHandler::CPDF_CryptoHandler(Cipher cipher,
const uint8_t* key,
size_t keylen)
: m_KeyLen(std::min<size_t>(keylen, 32)), m_Cipher(cipher) {
- DCHECK(cipher != FXCIPHER_AES || keylen == 16 || keylen == 24 ||
+ DCHECK(cipher != Cipher::kAES || keylen == 16 || keylen == 24 ||
keylen == 32);
- DCHECK(cipher != FXCIPHER_AES2 || keylen == 32);
- DCHECK(cipher != FXCIPHER_RC4 || (keylen >= 5 && keylen <= 16));
+ DCHECK(cipher != Cipher::kAES2 || keylen == 32);
+ DCHECK(cipher != Cipher::kRC4 || (keylen >= 5 && keylen <= 16));
- if (m_Cipher != FXCIPHER_NONE)
+ if (m_Cipher != Cipher::kNone)
memcpy(m_EncryptKey, key, m_KeyLen);
- if (m_Cipher == FXCIPHER_AES)
+ if (m_Cipher == Cipher::kAES)
m_pAESContext.reset(FX_Alloc(CRYPT_aes_context, 1));
}
diff --git a/core/fpdfapi/parser/cpdf_crypto_handler.h b/core/fpdfapi/parser/cpdf_crypto_handler.h
index 47b278a..de94f9e 100644
--- a/core/fpdfapi/parser/cpdf_crypto_handler.h
+++ b/core/fpdfapi/parser/cpdf_crypto_handler.h
@@ -19,15 +19,21 @@
class CPDF_Dictionary;
class CPDF_Object;
-class CPDF_SecurityHandler;
class CPDF_CryptoHandler {
public:
- CPDF_CryptoHandler(int cipher, const uint8_t* key, size_t keylen);
- ~CPDF_CryptoHandler();
+ enum class Cipher {
+ kNone = 0,
+ kRC4 = 1,
+ kAES = 2,
+ kAES2 = 3,
+ };
static bool IsSignatureDictionary(const CPDF_Dictionary* dictionary);
+ CPDF_CryptoHandler(Cipher cipher, const uint8_t* key, size_t keylen);
+ ~CPDF_CryptoHandler();
+
bool DecryptObjectTree(RetainPtr<CPDF_Object> object);
size_t EncryptGetSize(pdfium::span<const uint8_t> source) const;
bool EncryptContent(uint32_t objnum,
@@ -62,7 +68,7 @@
bool CryptFinish(void* context, CFX_BinaryBuf& dest_buf, bool bEncrypt);
const size_t m_KeyLen;
- const int m_Cipher;
+ const Cipher m_Cipher;
std::unique_ptr<CRYPT_aes_context, FxFreeDeleter> m_pAESContext;
uint8_t m_EncryptKey[32];
};
diff --git a/core/fpdfapi/parser/cpdf_security_handler.cpp b/core/fpdfapi/parser/cpdf_security_handler.cpp
index 6719464..6a6f81b8 100644
--- a/core/fpdfapi/parser/cpdf_security_handler.cpp
+++ b/core/fpdfapi/parser/cpdf_security_handler.cpp
@@ -73,15 +73,16 @@
memcpy(key, digest, copy_len);
}
-bool IsValidKeyLengthForCipher(int cipher, size_t keylen) {
+bool IsValidKeyLengthForCipher(CPDF_CryptoHandler::Cipher cipher,
+ size_t keylen) {
switch (cipher) {
- case FXCIPHER_AES:
+ case CPDF_CryptoHandler::Cipher::kAES:
return keylen == 16 || keylen == 24 || keylen == 32;
- case FXCIPHER_AES2:
+ case CPDF_CryptoHandler::Cipher::kAES2:
return keylen == 32;
- case FXCIPHER_RC4:
+ case CPDF_CryptoHandler::Cipher::kRC4:
return keylen >= 5 && keylen <= 16;
- case FXCIPHER_NONE:
+ case CPDF_CryptoHandler::Cipher::kNone:
return true;
default:
NOTREACHED();
@@ -199,7 +200,7 @@
m_FileId.clear();
if (!LoadDict(pEncryptDict))
return false;
- if (m_Cipher == FXCIPHER_NONE)
+ if (m_Cipher == CPDF_CryptoHandler::Cipher::kNone)
return true;
if (!CheckSecurity(password))
return false;
@@ -228,10 +229,10 @@
static bool LoadCryptInfo(const CPDF_Dictionary* pEncryptDict,
const ByteString& name,
- int* cipher,
+ CPDF_CryptoHandler::Cipher* cipher,
size_t* keylen_out) {
int Version = pEncryptDict->GetIntegerFor("V");
- *cipher = FXCIPHER_RC4;
+ *cipher = CPDF_CryptoHandler::Cipher::kRC4;
*keylen_out = 0;
int keylen = 0;
if (Version >= 4) {
@@ -240,7 +241,7 @@
return false;
if (name == "Identity") {
- *cipher = FXCIPHER_NONE;
+ *cipher = CPDF_CryptoHandler::Cipher::kNone;
} else {
const CPDF_Dictionary* pDefFilter = pCryptFilters->GetDictFor(name);
if (!pDefFilter)
@@ -264,7 +265,7 @@
keylen = nKeyBits / 8;
ByteString cipher_name = pDefFilter->GetStringFor("CFM");
if (cipher_name == "AESV2" || cipher_name == "AESV3")
- *cipher = FXCIPHER_AES;
+ *cipher = CPDF_CryptoHandler::Cipher::kAES;
}
} else {
keylen = Version > 1 ? pEncryptDict->GetIntegerFor("Length", 40) / 8 : 5;
@@ -296,7 +297,7 @@
}
bool CPDF_SecurityHandler::LoadDict(const CPDF_Dictionary* pEncryptDict,
- int* cipher,
+ CPDF_CryptoHandler::Cipher* cipher,
size_t* key_len) {
m_pEncryptDict.Reset(pEncryptDict);
m_Version = pEncryptDict->GetIntegerFor("V");
@@ -544,7 +545,7 @@
bool bDefault) {
DCHECK(pEncryptDict);
- int cipher = FXCIPHER_NONE;
+ CPDF_CryptoHandler::Cipher cipher = CPDF_CryptoHandler::Cipher::kNone;
size_t key_len = 0;
if (!LoadDict(pEncryptDict, &cipher, &key_len)) {
return;
diff --git a/core/fpdfapi/parser/cpdf_security_handler.h b/core/fpdfapi/parser/cpdf_security_handler.h
index 3e84099..280d92f 100644
--- a/core/fpdfapi/parser/cpdf_security_handler.h
+++ b/core/fpdfapi/parser/cpdf_security_handler.h
@@ -9,17 +9,12 @@
#include <memory>
+#include "core/fpdfapi/parser/cpdf_crypto_handler.h"
#include "core/fxcrt/fx_string.h"
#include "core/fxcrt/fx_system.h"
#include "core/fxcrt/retain_ptr.h"
-#define FXCIPHER_NONE 0
-#define FXCIPHER_RC4 1
-#define FXCIPHER_AES 2
-#define FXCIPHER_AES2 3
-
class CPDF_Array;
-class CPDF_CryptoHandler;
class CPDF_Dictionary;
class CPDF_Parser;
@@ -62,7 +57,7 @@
bool LoadDict(const CPDF_Dictionary* pEncryptDict);
bool LoadDict(const CPDF_Dictionary* pEncryptDict,
- int* cipher,
+ CPDF_CryptoHandler::Cipher* cipher,
size_t* key_len);
ByteString GetUserPassword(const ByteString& owner_password) const;
@@ -88,8 +83,8 @@
int m_Version = 0;
int m_Revision = 0;
uint32_t m_Permissions = 0;
- int m_Cipher = FXCIPHER_NONE;
size_t m_KeyLen = 0;
+ CPDF_CryptoHandler::Cipher m_Cipher = CPDF_CryptoHandler::Cipher::kNone;
PasswordEncodingConversion m_PasswordEncodingConversion = kUnknown;
ByteString m_FileId;
RetainPtr<const CPDF_Dictionary> m_pEncryptDict;