From 09df396931c285586eb0335f69de69f0672339bc Mon Sep 17 00:00:00 2001 From: m-holger Date: Fri, 4 Jul 2025 14:46:22 +0100 Subject: [PATCH] Refactor `EncryptionParameters` to encapsulate crypt filter interpretation logic, transition `interpretCF` to a class method, and eliminate redundant parameter passing for improved clarity and maintainability. --- include/qpdf/QPDF.hh | 1 - libqpdf/QPDF_encryption.cc | 36 ++++++++++++++++++------------------ libqpdf/qpdf/QPDF_private.hh | 3 +++ 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/include/qpdf/QPDF.hh b/include/qpdf/QPDF.hh index fb79314..6ec3215 100644 --- a/include/qpdf/QPDF.hh +++ b/include/qpdf/QPDF.hh @@ -964,7 +964,6 @@ class QPDF void insertPageobjToPage(QPDFObjectHandle const& obj, int pos, bool check_duplicate); // methods to support encryption -- implemented in QPDF_encryption.cc - static encryption_method_e interpretCF(EncryptionParameters& encp, QPDFObjectHandle const& cf); void initializeEncryption(); static std::string getKeyForObject(std::shared_ptr encp, QPDFObjGen og, bool use_aes); diff --git a/libqpdf/QPDF_encryption.cc b/libqpdf/QPDF_encryption.cc index 678d079..15690ca 100644 --- a/libqpdf/QPDF_encryption.cc +++ b/libqpdf/QPDF_encryption.cc @@ -687,21 +687,21 @@ QPDF::EncryptionData::recover_encryption_key_with_password( } QPDF::encryption_method_e -QPDF::interpretCF(EncryptionParameters& encp, QPDFObjectHandle const& cf) +QPDF::EncryptionParameters::interpretCF(QPDFObjectHandle const& cf) const { - if (cf.isName()) { - std::string filter = cf.getName(); - auto it = encp.crypt_filters.find(filter); - if (it != encp.crypt_filters.end()) { - return it->second; - } - if (filter == "/Identity") { - return e_none; - } - return e_unknown; + if (!cf.isName()) { + // Default: /Identity + return e_none; + } + std::string filter = cf.getName(); + auto it = crypt_filters.find(filter); + if (it != crypt_filters.end()) { + return it->second; + } + if (filter == "/Identity") { + return e_none; } - // Default: /Identity - return e_none; + return e_unknown; } void @@ -873,8 +873,8 @@ QPDF::EncryptionParameters::initialize(QPDF& qpdf) } } - cf_stream = interpretCF(*this, encryption_dict.getKey("/StmF")); - cf_string = interpretCF(*this, encryption_dict.getKey("/StrF")); + cf_stream = interpretCF(encryption_dict.getKey("/StmF")); + cf_string = interpretCF(encryption_dict.getKey("/StrF")); if (auto EFF = encryption_dict.getKey("/EFF"); EFF.isName()) { // qpdf does not use this for anything other than informational purposes. This is // intended to instruct conforming writers on which crypt filter should be used when new @@ -885,7 +885,7 @@ QPDF::EncryptionParameters::initialize(QPDF& qpdf) // the way I was imagining. Still, providing this information could be useful when // looking at a file generated by something else, such as Acrobat when specifying that // only attachments should be encrypted. - cf_file = interpretCF(*this, EFF); + cf_file = interpretCF(EFF); } else { cf_file = cf_stream; } @@ -1052,7 +1052,7 @@ QPDF::decryptStream( QPDFObjectHandle decode_parms = stream_dict.getKey("/DecodeParms"); if (decode_parms.isDictionaryOfType("/CryptFilterDecodeParms")) { QTC::TC("qpdf", "QPDF_encryption stream crypt filter"); - method = interpretCF(*encp, decode_parms.getKey("/Name")); + method = encp->interpretCF(decode_parms.getKey("/Name")); method_source = "stream's Crypt decode parameters"; } } else if ( @@ -1067,7 +1067,7 @@ QPDF::decryptStream( if (crypt_params.isDictionary() && crypt_params.getKey("/Name").isName()) { QTC::TC("qpdf", "QPDF_encrypt crypt array"); - method = interpretCF(*encp, crypt_params.getKey("/Name")); + method = encp->interpretCF(crypt_params.getKey("/Name")); method_source = "stream's Crypt decode parameters (array)"; } } diff --git a/libqpdf/qpdf/QPDF_private.hh b/libqpdf/qpdf/QPDF_private.hh index 40e3311..f6be303 100644 --- a/libqpdf/qpdf/QPDF_private.hh +++ b/libqpdf/qpdf/QPDF_private.hh @@ -187,8 +187,11 @@ class QPDF::EncryptionParameters public: EncryptionParameters() = default; + encryption_method_e interpretCF(QPDFObjectHandle const& cf) const; + private: void initialize(QPDF& qpdf); + bool encrypted{false}; bool encryption_initialized{false}; int encryption_P{0}; -- libgit2 0.21.4