Commit 80093a05ecc5539d25a58711ce7a3281dc992355

Authored by m-holger
1 parent 232c037a

Refactor `QPDFWriter` to remove `encrypt_metadata` member, centralize its handli…

…ng in method parameters, and simplify encryption logic for improved maintainability and reduced redundancy.
include/qpdf/QPDFWriter.hh
... ... @@ -530,6 +530,7 @@ class QPDFWriter
530 530 int V,
531 531 int R,
532 532 int key_len,
  533 + bool encrypt_metadata,
533 534 std::set<int>& bits_to_clear);
534 535 void setEncryptionParametersInternal(
535 536 std::string const& user_password, std::string const& encryption_key);
... ...
libqpdf/QPDFWriter.cc
... ... @@ -89,7 +89,6 @@ class QPDFWriter::Members
89 89  
90 90 std::unique_ptr<QPDF::EncryptionData> encryption;
91 91 std::string encryption_key;
92   - bool encrypt_metadata{true};
93 92 bool encrypt_use_aes{false};
94 93 std::map<std::string, std::string> encryption_dictionary;
95 94  
... ... @@ -445,7 +444,7 @@ QPDFWriter::setR2EncryptionParametersInsecure(
445 444 clear.insert(6);
446 445 }
447 446  
448   - setEncryptionParameters(user_password, owner_password, 1, 2, 5, clear);
  447 + setEncryptionParameters(user_password, owner_password, 1, 2, 5, true, clear);
449 448 }
450 449  
451 450 void
... ... @@ -473,7 +472,7 @@ QPDFWriter::setR3EncryptionParametersInsecure(
473 472 allow_modify_other,
474 473 print,
475 474 qpdf_r3m_all);
476   - setEncryptionParameters(user_password, owner_password, 2, 3, 16, clear);
  475 + setEncryptionParameters(user_password, owner_password, 2, 3, 16, true, clear);
477 476 }
478 477  
479 478 void
... ... @@ -504,8 +503,7 @@ QPDFWriter::setR4EncryptionParametersInsecure(
504 503 print,
505 504 qpdf_r3m_all);
506 505 m->encrypt_use_aes = use_aes;
507   - m->encrypt_metadata = encrypt_metadata;
508   - setEncryptionParameters(user_password, owner_password, 4, 4, 16, clear);
  506 + setEncryptionParameters(user_password, owner_password, 4, 4, 16, encrypt_metadata, clear);
509 507 }
510 508  
511 509 void
... ... @@ -535,8 +533,7 @@ QPDFWriter::setR5EncryptionParameters(
535 533 print,
536 534 qpdf_r3m_all);
537 535 m->encrypt_use_aes = true;
538   - m->encrypt_metadata = encrypt_metadata;
539   - setEncryptionParameters(user_password, owner_password, 5, 5, 32, clear);
  536 + setEncryptionParameters(user_password, owner_password, 5, 5, 32, encrypt_metadata, clear);
540 537 }
541 538  
542 539 void
... ... @@ -566,8 +563,7 @@ QPDFWriter::setR6EncryptionParameters(
566 563 print,
567 564 qpdf_r3m_all);
568 565 m->encrypt_use_aes = true;
569   - m->encrypt_metadata = encrypt_metadata;
570   - setEncryptionParameters(user_password, owner_password, 5, 6, 32, clear);
  566 + setEncryptionParameters(user_password, owner_password, 5, 6, 32, encrypt_metadata, clear);
571 567 }
572 568  
573 569 void
... ... @@ -683,6 +679,7 @@ QPDFWriter::setEncryptionParameters(
683 679 int V,
684 680 int R,
685 681 int key_len,
  682 + bool encrypt_metadata,
686 683 std::set<int>& bits_to_clear)
687 684 {
688 685 // PDF specification refers to bits with the low bit numbered 1.
... ... @@ -707,7 +704,7 @@ QPDFWriter::setEncryptionParameters(
707 704  
708 705 generateID();
709 706 m->encryption = std::make_unique<QPDF::EncryptionData>(
710   - V, R, key_len, P, "", "", "", "", "", m->id1, m->encrypt_metadata);
  707 + V, R, key_len, P, "", "", "", "", "", m->id1, encrypt_metadata);
711 708 auto encryption_key = m->encryption->compute_parameters(user_password, owner_password);
712 709 setEncryptionParametersInternal(user_password, encryption_key);
713 710 }
... ... @@ -726,9 +723,10 @@ QPDFWriter::copyEncryptionParameters(QPDF&amp; qpdf)
726 723 if (V > 1) {
727 724 key_len = encrypt.getKey("/Length").getIntValueAsInt() / 8;
728 725 }
729   - if (encrypt.hasKey("/EncryptMetadata") && encrypt.getKey("/EncryptMetadata").isBool()) {
730   - m->encrypt_metadata = encrypt.getKey("/EncryptMetadata").getBoolValue();
731   - }
  726 + const bool encrypt_metadata =
  727 + encrypt.hasKey("/EncryptMetadata") && encrypt.getKey("/EncryptMetadata").isBool()
  728 + ? encrypt.getKey("/EncryptMetadata").getBoolValue()
  729 + : true;
732 730 if (V >= 4) {
733 731 // When copying encryption parameters, use AES even if the original file did not.
734 732 // Acrobat doesn't create files with V >= 4 that don't use AES, and the logic of
... ... @@ -736,17 +734,11 @@ QPDFWriter::copyEncryptionParameters(QPDF&amp; qpdf)
736 734 // all potentially having different values.
737 735 m->encrypt_use_aes = true;
738 736 }
739   - QTC::TC("qpdf", "QPDFWriter copy encrypt metadata", m->encrypt_metadata ? 0 : 1);
  737 + QTC::TC("qpdf", "QPDFWriter copy encrypt metadata", encrypt_metadata ? 0 : 1);
740 738 QTC::TC("qpdf", "QPDFWriter copy use_aes", m->encrypt_use_aes ? 0 : 1);
741   - std::string OE;
742   - std::string UE;
743   - std::string Perms;
744 739 std::string encryption_key;
745 740 if (V >= 5) {
746 741 QTC::TC("qpdf", "QPDFWriter copy V5");
747   - OE = encrypt.getKey("/OE").getStringValue();
748   - UE = encrypt.getKey("/UE").getStringValue();
749   - Perms = encrypt.getKey("/Perms").getStringValue();
750 742 encryption_key = qpdf.getEncryptionKey();
751 743 }
752 744  
... ... @@ -757,11 +749,11 @@ QPDFWriter::copyEncryptionParameters(QPDF&amp; qpdf)
757 749 static_cast<int>(encrypt.getKey("/P").getIntValue()),
758 750 encrypt.getKey("/O").getStringValue(),
759 751 encrypt.getKey("/U").getStringValue(),
760   - OE,
761   - UE,
762   - Perms,
  752 + V < 5 ? "" : encrypt.getKey("/OE").getStringValue(),
  753 + V < 5 ? "" : encrypt.getKey("/UE").getStringValue(),
  754 + V < 5 ? "" : encrypt.getKey("/Perms").getStringValue(),
763 755 m->id1, // m->id1 == the other file's id1
764   - m->encrypt_metadata);
  756 + encrypt_metadata);
765 757  
766 758 setEncryptionParametersInternal(qpdf.getPaddedUserPassword(), encryption_key);
767 759 }
... ... @@ -870,7 +862,7 @@ QPDFWriter::setEncryptionParametersInternal(
870 862 setMinimumPDFVersion("1.3");
871 863 }
872 864  
873   - if (R >= 4 && !m->encrypt_metadata) {
  865 + if (R >= 4 && !m->encryption->getEncryptMetadata()) {
874 866 m->encryption_dictionary["/EncryptMetadata"] = "false";
875 867 }
876 868 if ((V == 4) || (V == 5)) {
... ... @@ -1339,7 +1331,8 @@ QPDFWriter::willFilterStream(
1339 1331 }
1340 1332 bool normalize = false;
1341 1333 bool uncompress = false;
1342   - if (filter_on_write && is_root_metadata && (!m->encrypted || !m->encrypt_metadata)) {
  1334 + if (filter_on_write && is_root_metadata &&
  1335 + (!m->encrypted || !m->encryption->getEncryptMetadata())) {
1343 1336 QTC::TC("qpdf", "QPDFWriter not compressing metadata");
1344 1337 filter = true;
1345 1338 compress_stream = false;
... ... @@ -1627,7 +1620,7 @@ QPDFWriter::unparseObject(
1627 1620 QPDFObjectHandle stream_dict = object.getDict();
1628 1621  
1629 1622 m->cur_stream_length = stream_data.size();
1630   - if (is_metadata && m->encrypted && (!m->encrypt_metadata)) {
  1623 + if (is_metadata && m->encrypted && !m->encryption->getEncryptMetadata()) {
1631 1624 // Don't encrypt stream data for the metadata stream
1632 1625 m->cur_data_key.clear();
1633 1626 }
... ... @@ -1641,13 +1634,12 @@ QPDFWriter::unparseObject(
1641 1634 writeString(stream_data);
1642 1635 }
1643 1636  
1644   - if (m->newline_before_endstream || (m->qdf_mode && last_char != '\n')) {
1645   - writeString("\n");
1646   - m->added_newline = true;
  1637 + if ((m->added_newline =
  1638 + m->newline_before_endstream || (m->qdf_mode && last_char != '\n'))) {
  1639 + writeString("\nendstream");
1647 1640 } else {
1648   - m->added_newline = false;
  1641 + writeString("endstream");
1649 1642 }
1650   - writeString("endstream");
1651 1643 } else if (tc == ::ot_string) {
1652 1644 std::string val;
1653 1645 if (m->encrypted && (!(flags & f_in_ostream)) && (!(flags & f_no_encryption)) &&
... ...