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,6 +530,7 @@ class QPDFWriter
530 int V, 530 int V,
531 int R, 531 int R,
532 int key_len, 532 int key_len,
  533 + bool encrypt_metadata,
533 std::set<int>& bits_to_clear); 534 std::set<int>& bits_to_clear);
534 void setEncryptionParametersInternal( 535 void setEncryptionParametersInternal(
535 std::string const& user_password, std::string const& encryption_key); 536 std::string const& user_password, std::string const& encryption_key);
libqpdf/QPDFWriter.cc
@@ -89,7 +89,6 @@ class QPDFWriter::Members @@ -89,7 +89,6 @@ class QPDFWriter::Members
89 89
90 std::unique_ptr<QPDF::EncryptionData> encryption; 90 std::unique_ptr<QPDF::EncryptionData> encryption;
91 std::string encryption_key; 91 std::string encryption_key;
92 - bool encrypt_metadata{true};  
93 bool encrypt_use_aes{false}; 92 bool encrypt_use_aes{false};
94 std::map<std::string, std::string> encryption_dictionary; 93 std::map<std::string, std::string> encryption_dictionary;
95 94
@@ -445,7 +444,7 @@ QPDFWriter::setR2EncryptionParametersInsecure( @@ -445,7 +444,7 @@ QPDFWriter::setR2EncryptionParametersInsecure(
445 clear.insert(6); 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 void 450 void
@@ -473,7 +472,7 @@ QPDFWriter::setR3EncryptionParametersInsecure( @@ -473,7 +472,7 @@ QPDFWriter::setR3EncryptionParametersInsecure(
473 allow_modify_other, 472 allow_modify_other,
474 print, 473 print,
475 qpdf_r3m_all); 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 void 478 void
@@ -504,8 +503,7 @@ QPDFWriter::setR4EncryptionParametersInsecure( @@ -504,8 +503,7 @@ QPDFWriter::setR4EncryptionParametersInsecure(
504 print, 503 print,
505 qpdf_r3m_all); 504 qpdf_r3m_all);
506 m->encrypt_use_aes = use_aes; 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 void 509 void
@@ -535,8 +533,7 @@ QPDFWriter::setR5EncryptionParameters( @@ -535,8 +533,7 @@ QPDFWriter::setR5EncryptionParameters(
535 print, 533 print,
536 qpdf_r3m_all); 534 qpdf_r3m_all);
537 m->encrypt_use_aes = true; 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 void 539 void
@@ -566,8 +563,7 @@ QPDFWriter::setR6EncryptionParameters( @@ -566,8 +563,7 @@ QPDFWriter::setR6EncryptionParameters(
566 print, 563 print,
567 qpdf_r3m_all); 564 qpdf_r3m_all);
568 m->encrypt_use_aes = true; 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 void 569 void
@@ -683,6 +679,7 @@ QPDFWriter::setEncryptionParameters( @@ -683,6 +679,7 @@ QPDFWriter::setEncryptionParameters(
683 int V, 679 int V,
684 int R, 680 int R,
685 int key_len, 681 int key_len,
  682 + bool encrypt_metadata,
686 std::set<int>& bits_to_clear) 683 std::set<int>& bits_to_clear)
687 { 684 {
688 // PDF specification refers to bits with the low bit numbered 1. 685 // PDF specification refers to bits with the low bit numbered 1.
@@ -707,7 +704,7 @@ QPDFWriter::setEncryptionParameters( @@ -707,7 +704,7 @@ QPDFWriter::setEncryptionParameters(
707 704
708 generateID(); 705 generateID();
709 m->encryption = std::make_unique<QPDF::EncryptionData>( 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 auto encryption_key = m->encryption->compute_parameters(user_password, owner_password); 708 auto encryption_key = m->encryption->compute_parameters(user_password, owner_password);
712 setEncryptionParametersInternal(user_password, encryption_key); 709 setEncryptionParametersInternal(user_password, encryption_key);
713 } 710 }
@@ -726,9 +723,10 @@ QPDFWriter::copyEncryptionParameters(QPDF&amp; qpdf) @@ -726,9 +723,10 @@ QPDFWriter::copyEncryptionParameters(QPDF&amp; qpdf)
726 if (V > 1) { 723 if (V > 1) {
727 key_len = encrypt.getKey("/Length").getIntValueAsInt() / 8; 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 if (V >= 4) { 730 if (V >= 4) {
733 // When copying encryption parameters, use AES even if the original file did not. 731 // When copying encryption parameters, use AES even if the original file did not.
734 // Acrobat doesn't create files with V >= 4 that don't use AES, and the logic of 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,17 +734,11 @@ QPDFWriter::copyEncryptionParameters(QPDF&amp; qpdf)
736 // all potentially having different values. 734 // all potentially having different values.
737 m->encrypt_use_aes = true; 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 QTC::TC("qpdf", "QPDFWriter copy use_aes", m->encrypt_use_aes ? 0 : 1); 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 std::string encryption_key; 739 std::string encryption_key;
745 if (V >= 5) { 740 if (V >= 5) {
746 QTC::TC("qpdf", "QPDFWriter copy V5"); 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 encryption_key = qpdf.getEncryptionKey(); 742 encryption_key = qpdf.getEncryptionKey();
751 } 743 }
752 744
@@ -757,11 +749,11 @@ QPDFWriter::copyEncryptionParameters(QPDF&amp; qpdf) @@ -757,11 +749,11 @@ QPDFWriter::copyEncryptionParameters(QPDF&amp; qpdf)
757 static_cast<int>(encrypt.getKey("/P").getIntValue()), 749 static_cast<int>(encrypt.getKey("/P").getIntValue()),
758 encrypt.getKey("/O").getStringValue(), 750 encrypt.getKey("/O").getStringValue(),
759 encrypt.getKey("/U").getStringValue(), 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 m->id1, // m->id1 == the other file's id1 755 m->id1, // m->id1 == the other file's id1
764 - m->encrypt_metadata); 756 + encrypt_metadata);
765 757
766 setEncryptionParametersInternal(qpdf.getPaddedUserPassword(), encryption_key); 758 setEncryptionParametersInternal(qpdf.getPaddedUserPassword(), encryption_key);
767 } 759 }
@@ -870,7 +862,7 @@ QPDFWriter::setEncryptionParametersInternal( @@ -870,7 +862,7 @@ QPDFWriter::setEncryptionParametersInternal(
870 setMinimumPDFVersion("1.3"); 862 setMinimumPDFVersion("1.3");
871 } 863 }
872 864
873 - if (R >= 4 && !m->encrypt_metadata) { 865 + if (R >= 4 && !m->encryption->getEncryptMetadata()) {
874 m->encryption_dictionary["/EncryptMetadata"] = "false"; 866 m->encryption_dictionary["/EncryptMetadata"] = "false";
875 } 867 }
876 if ((V == 4) || (V == 5)) { 868 if ((V == 4) || (V == 5)) {
@@ -1339,7 +1331,8 @@ QPDFWriter::willFilterStream( @@ -1339,7 +1331,8 @@ QPDFWriter::willFilterStream(
1339 } 1331 }
1340 bool normalize = false; 1332 bool normalize = false;
1341 bool uncompress = false; 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 QTC::TC("qpdf", "QPDFWriter not compressing metadata"); 1336 QTC::TC("qpdf", "QPDFWriter not compressing metadata");
1344 filter = true; 1337 filter = true;
1345 compress_stream = false; 1338 compress_stream = false;
@@ -1627,7 +1620,7 @@ QPDFWriter::unparseObject( @@ -1627,7 +1620,7 @@ QPDFWriter::unparseObject(
1627 QPDFObjectHandle stream_dict = object.getDict(); 1620 QPDFObjectHandle stream_dict = object.getDict();
1628 1621
1629 m->cur_stream_length = stream_data.size(); 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 // Don't encrypt stream data for the metadata stream 1624 // Don't encrypt stream data for the metadata stream
1632 m->cur_data_key.clear(); 1625 m->cur_data_key.clear();
1633 } 1626 }
@@ -1641,13 +1634,12 @@ QPDFWriter::unparseObject( @@ -1641,13 +1634,12 @@ QPDFWriter::unparseObject(
1641 writeString(stream_data); 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 } else { 1640 } else {
1648 - m->added_newline = false; 1641 + writeString("endstream");
1649 } 1642 }
1650 - writeString("endstream");  
1651 } else if (tc == ::ot_string) { 1643 } else if (tc == ::ot_string) {
1652 std::string val; 1644 std::string val;
1653 if (m->encrypted && (!(flags & f_in_ostream)) && (!(flags & f_no_encryption)) && 1645 if (m->encrypted && (!(flags & f_in_ostream)) && (!(flags & f_no_encryption)) &&