Commit 09df396931c285586eb0335f69de69f0672339bc

Authored by m-holger
1 parent a051a162

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
@@ -964,7 +964,6 @@ class QPDF @@ -964,7 +964,6 @@ class QPDF
964 void insertPageobjToPage(QPDFObjectHandle const& obj, int pos, bool check_duplicate); 964 void insertPageobjToPage(QPDFObjectHandle const& obj, int pos, bool check_duplicate);
965 965
966 // methods to support encryption -- implemented in QPDF_encryption.cc 966 // methods to support encryption -- implemented in QPDF_encryption.cc
967 - static encryption_method_e interpretCF(EncryptionParameters& encp, QPDFObjectHandle const& cf);  
968 void initializeEncryption(); 967 void initializeEncryption();
969 static std::string 968 static std::string
970 getKeyForObject(std::shared_ptr<EncryptionParameters> encp, QPDFObjGen og, bool use_aes); 969 getKeyForObject(std::shared_ptr<EncryptionParameters> encp, QPDFObjGen og, bool use_aes);
libqpdf/QPDF_encryption.cc
@@ -687,21 +687,21 @@ QPDF::EncryptionData::recover_encryption_key_with_password( @@ -687,21 +687,21 @@ QPDF::EncryptionData::recover_encryption_key_with_password(
687 } 687 }
688 688
689 QPDF::encryption_method_e 689 QPDF::encryption_method_e
690 -QPDF::interpretCF(EncryptionParameters& encp, QPDFObjectHandle const& cf) 690 +QPDF::EncryptionParameters::interpretCF(QPDFObjectHandle const& cf) const
691 { 691 {
692 - if (cf.isName()) {  
693 - std::string filter = cf.getName();  
694 - auto it = encp.crypt_filters.find(filter);  
695 - if (it != encp.crypt_filters.end()) {  
696 - return it->second;  
697 - }  
698 - if (filter == "/Identity") {  
699 - return e_none;  
700 - }  
701 - return e_unknown; 692 + if (!cf.isName()) {
  693 + // Default: /Identity
  694 + return e_none;
  695 + }
  696 + std::string filter = cf.getName();
  697 + auto it = crypt_filters.find(filter);
  698 + if (it != crypt_filters.end()) {
  699 + return it->second;
  700 + }
  701 + if (filter == "/Identity") {
  702 + return e_none;
702 } 703 }
703 - // Default: /Identity  
704 - return e_none; 704 + return e_unknown;
705 } 705 }
706 706
707 void 707 void
@@ -873,8 +873,8 @@ QPDF::EncryptionParameters::initialize(QPDF&amp; qpdf) @@ -873,8 +873,8 @@ QPDF::EncryptionParameters::initialize(QPDF&amp; qpdf)
873 } 873 }
874 } 874 }
875 875
876 - cf_stream = interpretCF(*this, encryption_dict.getKey("/StmF"));  
877 - cf_string = interpretCF(*this, encryption_dict.getKey("/StrF")); 876 + cf_stream = interpretCF(encryption_dict.getKey("/StmF"));
  877 + cf_string = interpretCF(encryption_dict.getKey("/StrF"));
878 if (auto EFF = encryption_dict.getKey("/EFF"); EFF.isName()) { 878 if (auto EFF = encryption_dict.getKey("/EFF"); EFF.isName()) {
879 // qpdf does not use this for anything other than informational purposes. This is 879 // qpdf does not use this for anything other than informational purposes. This is
880 // intended to instruct conforming writers on which crypt filter should be used when new 880 // intended to instruct conforming writers on which crypt filter should be used when new
@@ -885,7 +885,7 @@ QPDF::EncryptionParameters::initialize(QPDF&amp; qpdf) @@ -885,7 +885,7 @@ QPDF::EncryptionParameters::initialize(QPDF&amp; qpdf)
885 // the way I was imagining. Still, providing this information could be useful when 885 // the way I was imagining. Still, providing this information could be useful when
886 // looking at a file generated by something else, such as Acrobat when specifying that 886 // looking at a file generated by something else, such as Acrobat when specifying that
887 // only attachments should be encrypted. 887 // only attachments should be encrypted.
888 - cf_file = interpretCF(*this, EFF); 888 + cf_file = interpretCF(EFF);
889 } else { 889 } else {
890 cf_file = cf_stream; 890 cf_file = cf_stream;
891 } 891 }
@@ -1052,7 +1052,7 @@ QPDF::decryptStream( @@ -1052,7 +1052,7 @@ QPDF::decryptStream(
1052 QPDFObjectHandle decode_parms = stream_dict.getKey("/DecodeParms"); 1052 QPDFObjectHandle decode_parms = stream_dict.getKey("/DecodeParms");
1053 if (decode_parms.isDictionaryOfType("/CryptFilterDecodeParms")) { 1053 if (decode_parms.isDictionaryOfType("/CryptFilterDecodeParms")) {
1054 QTC::TC("qpdf", "QPDF_encryption stream crypt filter"); 1054 QTC::TC("qpdf", "QPDF_encryption stream crypt filter");
1055 - method = interpretCF(*encp, decode_parms.getKey("/Name")); 1055 + method = encp->interpretCF(decode_parms.getKey("/Name"));
1056 method_source = "stream's Crypt decode parameters"; 1056 method_source = "stream's Crypt decode parameters";
1057 } 1057 }
1058 } else if ( 1058 } else if (
@@ -1067,7 +1067,7 @@ QPDF::decryptStream( @@ -1067,7 +1067,7 @@ QPDF::decryptStream(
1067 if (crypt_params.isDictionary() && 1067 if (crypt_params.isDictionary() &&
1068 crypt_params.getKey("/Name").isName()) { 1068 crypt_params.getKey("/Name").isName()) {
1069 QTC::TC("qpdf", "QPDF_encrypt crypt array"); 1069 QTC::TC("qpdf", "QPDF_encrypt crypt array");
1070 - method = interpretCF(*encp, crypt_params.getKey("/Name")); 1070 + method = encp->interpretCF(crypt_params.getKey("/Name"));
1071 method_source = "stream's Crypt decode parameters (array)"; 1071 method_source = "stream's Crypt decode parameters (array)";
1072 } 1072 }
1073 } 1073 }
libqpdf/qpdf/QPDF_private.hh
@@ -187,8 +187,11 @@ class QPDF::EncryptionParameters @@ -187,8 +187,11 @@ class QPDF::EncryptionParameters
187 public: 187 public:
188 EncryptionParameters() = default; 188 EncryptionParameters() = default;
189 189
  190 + encryption_method_e interpretCF(QPDFObjectHandle const& cf) const;
  191 +
190 private: 192 private:
191 void initialize(QPDF& qpdf); 193 void initialize(QPDF& qpdf);
  194 +
192 bool encrypted{false}; 195 bool encrypted{false};
193 bool encryption_initialized{false}; 196 bool encryption_initialized{false};
194 int encryption_P{0}; 197 int encryption_P{0};