diff options
-rw-r--r-- | include/vcl/pdfwriter.hxx | 3 | ||||
-rw-r--r-- | vcl/inc/pdf/COSWriter.hxx | 78 | ||||
-rw-r--r-- | vcl/inc/pdf/pdfwriter_impl.hxx | 2 | ||||
-rw-r--r-- | vcl/source/filter/ipdf/pdfdocument.cxx | 8 | ||||
-rw-r--r-- | vcl/source/gdi/pdfwriter_impl.cxx | 49 |
5 files changed, 68 insertions, 72 deletions
diff --git a/include/vcl/pdfwriter.hxx b/include/vcl/pdfwriter.hxx index 73ba0fcb9742..06f9577401cc 100644 --- a/include/vcl/pdfwriter.hxx +++ b/include/vcl/pdfwriter.hxx @@ -1278,9 +1278,6 @@ public: */ VCL_DLLPUBLIC void AddAttachedFile(OUString const& rFileName, OUString const& rMimeType, OUString const& rDescription, std::unique_ptr<PDFOutputStream> pStream); - /// Write rString as a PDF hex string into rBuffer. - static void AppendUnicodeTextString(const OUString& rString, OStringBuffer& rBuffer); - /// Get current date/time in PDF D:YYYYMMDDHHMMSS form. static OString GetDateTime(svl::crypto::SigningContext* pSigningContext = nullptr); }; diff --git a/vcl/inc/pdf/COSWriter.hxx b/vcl/inc/pdf/COSWriter.hxx index 767654689b73..4c90f6f4968e 100644 --- a/vcl/inc/pdf/COSWriter.hxx +++ b/vcl/inc/pdf/COSWriter.hxx @@ -21,7 +21,7 @@ namespace vcl::pdf */ class COSWriter { - std::unique_ptr<IPDFEncryptor>& mpPDFEncryptor; + std::shared_ptr<IPDFEncryptor> mpPDFEncryptor; OStringBuffer maLine; void appendLiteralString(const char* pStr, sal_Int32 nLength) @@ -59,22 +59,23 @@ class COSWriter nLength--; } } - template <typename T> void appendHex(T nValue) + + template <typename T> static void appendHex(T nValue, OStringBuffer& rBuffer) { static constexpr const auto constHexDigits = std::to_array<char>( { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }); - maLine.append(constHexDigits[(nValue >> 4) & 15]); - maLine.append(constHexDigits[nValue & 15]); + rBuffer.append(constHexDigits[(nValue >> 4) & 15]); + rBuffer.append(constHexDigits[nValue & 15]); } void appendHexArray(sal_uInt8* pArray, size_t nSize) { for (size_t i = 0; i < nSize; i++) - appendHex(pArray[i]); + appendHex(pArray[i], maLine); } public: - COSWriter(std::unique_ptr<IPDFEncryptor>& pPDFEncryptor) + COSWriter(std::shared_ptr<IPDFEncryptor> const& pPDFEncryptor) : mpPDFEncryptor(pPDFEncryptor) , maLine(1024) { @@ -107,22 +108,21 @@ public: maLine.append(value); } - void writeString(std::string_view key, char* pString, sal_Int32 nSize) + void writeKeyAndUnicode(std::string_view key, OUString const& rString) { maLine.append(key); - maLine.append(" ("); - appendLiteralString(pString, nSize); - maLine.append(")"); + maLine.append("<"); + appendUnicodeTextString(rString, maLine); + maLine.append(">"); } - void writeUnicodeEncrypt(std::string_view key, OUString const& rString, sal_Int32 nObject, - bool bEncrypt, std::vector<sal_uInt8>& rKey) + void writeKeyAndUnicodeEncrypt(std::string_view key, OUString const& rString, sal_Int32 nObject, + bool bEncrypt, std::vector<sal_uInt8>& rKey) { - maLine.append(key); - maLine.append("<"); - - if (bEncrypt) + if (bEncrypt && mpPDFEncryptor) { + maLine.append(key); + maLine.append("<"); const sal_Unicode* pString = rString.getStr(); size_t nLength = rString.getLength(); //prepare a unicode string, encrypt it @@ -143,44 +143,41 @@ public: mpPDFEncryptor->encrypt(aEncryptionBuffer.data(), nChars, aNewBuffer, nChars); //now append, hexadecimal (appendHex), the encrypted result appendHexArray(aNewBuffer.data(), aNewBuffer.size()); + maLine.append(">"); } else { - //PDFWriter::AppendUnicodeTextString(rInString, maLine); - maLine.append("FEFF"); - const sal_Unicode* pString = rString.getStr(); - size_t nLength = rString.getLength(); - for (size_t i = 0; i < nLength; i++) - { - sal_Unicode aChar = pString[i]; - appendHex(sal_Int8(aChar >> 8)); - appendHex(sal_Int8(aChar & 255)); - } + writeKeyAndUnicode(key, rString); } - maLine.append(">"); } - void writeLiteralEncrypt(std::string_view key, std::string_view value, sal_Int32 nObject, - bool bEncrypt, std::vector<sal_uInt8>& rKey) + void writeKeyAndLiteral(std::string_view key, std::string_view value) { maLine.append(key); - maLine.append("("); - size_t nChars = value.size(); + appendLiteralString(value.data(), value.size()); + maLine.append(")"); + } - if (bEncrypt) + void writeKeyAndLiteralEncrypt(std::string_view key, std::string_view value, sal_Int32 nObject, + bool bEncrypt, std::vector<sal_uInt8>& rKey) + { + if (bEncrypt && mpPDFEncryptor) { + maLine.append(key); + maLine.append("("); + size_t nChars = value.size(); std::vector<sal_uInt8> aEncryptionBuffer(nChars); mpPDFEncryptor->setupEncryption(rKey, nObject); mpPDFEncryptor->encrypt(value.data(), nChars, aEncryptionBuffer, nChars); appendLiteralString(reinterpret_cast<char*>(aEncryptionBuffer.data()), aEncryptionBuffer.size()); + maLine.append(")"); } else { - appendLiteralString(value.data(), nChars); + writeKeyAndLiteral(key, value); } - maLine.append(")"); } void writeHexArray(std::string_view key, sal_uInt8* pData, size_t nSize) @@ -190,6 +187,19 @@ public: appendHexArray(pData, nSize); maLine.append(">"); } + + static void appendUnicodeTextString(const OUString& rString, OStringBuffer& rBuffer) + { + rBuffer.append("FEFF"); + const sal_Unicode* pString = rString.getStr(); + size_t nLength = rString.getLength(); + for (size_t i = 0; i < nLength; i++) + { + sal_Unicode aChar = pString[i]; + COSWriter::appendHex(sal_Int8(aChar >> 8), rBuffer); + COSWriter::appendHex(sal_Int8(aChar & 255), rBuffer); + } + } }; } diff --git a/vcl/inc/pdf/pdfwriter_impl.hxx b/vcl/inc/pdf/pdfwriter_impl.hxx index 4b7b72dce4fc..0a30fb363be6 100644 --- a/vcl/inc/pdf/pdfwriter_impl.hxx +++ b/vcl/inc/pdf/pdfwriter_impl.hxx @@ -818,7 +818,7 @@ private: ExternalPDFStreams m_aExternalPDFStreams; - std::unique_ptr<IPDFEncryptor> m_pPDFEncryptor; + std::shared_ptr<IPDFEncryptor> m_pPDFEncryptor; /* output redirection; e.g. to accumulate content streams for XObjects diff --git a/vcl/source/filter/ipdf/pdfdocument.cxx b/vcl/source/filter/ipdf/pdfdocument.cxx index 65faa176c8a8..5b2b9ca412e0 100644 --- a/vcl/source/filter/ipdf/pdfdocument.cxx +++ b/vcl/source/filter/ipdf/pdfdocument.cxx @@ -31,6 +31,7 @@ #include <o3tl/safeint.hxx> #include <pdf/objectcopier.hxx> +#include <pdf/COSWriter.hxx> using namespace com::sun::star; @@ -133,6 +134,7 @@ sal_Int32 PDFDocument::WriteSignatureObject(svl::crypto::SigningContext& rSignin aSignatureEntry.SetOffset(m_aEditBuffer.Tell()); aSignatureEntry.SetDirty(true); m_aXRef[nSignatureId] = aSignatureEntry; + OStringBuffer aSigBuffer(OString::number(nSignatureId) + " 0 obj\n" "<</Contents <"); @@ -167,9 +169,9 @@ sal_Int32 PDFDocument::WriteSignatureObject(svl::crypto::SigningContext& rSignin if (!rDescription.isEmpty()) { - aSigBuffer.append("/Reason<"); - vcl::PDFWriter::AppendUnicodeTextString(rDescription, aSigBuffer); - aSigBuffer.append(">"); + pdf::COSWriter aWriter(nullptr); + aWriter.writeKeyAndUnicode("/Reason", rDescription); + aSigBuffer.append(aWriter.getLine()); } aSigBuffer.append(" >>\nendobj\n\n"); diff --git a/vcl/source/gdi/pdfwriter_impl.cxx b/vcl/source/gdi/pdfwriter_impl.cxx index a4c2a501443b..ae63f2844d37 100644 --- a/vcl/source/gdi/pdfwriter_impl.cxx +++ b/vcl/source/gdi/pdfwriter_impl.cxx @@ -311,19 +311,6 @@ void removePlaceholderSE(std::vector<PDFStructureElement> & rStructure, PDFStruc } // end anonymous namespace -void PDFWriter::AppendUnicodeTextString(const OUString& rString, OStringBuffer& rBuffer) -{ - rBuffer.append( "FEFF" ); - const sal_Unicode* pStr = rString.getStr(); - sal_Int32 nLen = rString.getLength(); - for( int i = 0; i < nLen; i++ ) - { - sal_Unicode aChar = pStr[i]; - appendHex( static_cast<sal_Int8>(aChar >> 8), rBuffer ); - appendHex( static_cast<sal_Int8>(aChar & 255 ), rBuffer ); - } -} - void PDFWriterImpl::createWidgetFieldName( sal_Int32 i_nWidgetIndex, const PDFWriter::AnyWidget& i_rControl ) { /* #i80258# previously we use appendName here @@ -663,17 +650,17 @@ void computeDocumentIdentifier(std::vector<sal_uInt8>& o_rIdentifier, OString aInfoValuesOut; OStringBuffer aID(1024); if (!i_rDocInfo.Title.isEmpty()) - PDFWriter::AppendUnicodeTextString(i_rDocInfo.Title, aID); + COSWriter::appendUnicodeTextString(i_rDocInfo.Title, aID); if (!i_rDocInfo.Author.isEmpty()) - PDFWriter::AppendUnicodeTextString(i_rDocInfo.Author, aID); + COSWriter::appendUnicodeTextString(i_rDocInfo.Author, aID); if (!i_rDocInfo.Subject.isEmpty()) - PDFWriter::AppendUnicodeTextString(i_rDocInfo.Subject, aID); + COSWriter::appendUnicodeTextString(i_rDocInfo.Subject, aID); if (!i_rDocInfo.Keywords.isEmpty()) - PDFWriter::AppendUnicodeTextString(i_rDocInfo.Keywords, aID); + COSWriter::appendUnicodeTextString(i_rDocInfo.Keywords, aID); if (!i_rDocInfo.Creator.isEmpty()) - PDFWriter::AppendUnicodeTextString(i_rDocInfo.Creator, aID); + COSWriter::appendUnicodeTextString(i_rDocInfo.Creator, aID); if (!i_rDocInfo.Producer.isEmpty()) - PDFWriter::AppendUnicodeTextString(i_rDocInfo.Producer, aID); + COSWriter::appendUnicodeTextString(i_rDocInfo.Producer, aID); TimeValue aTVal, aGMT; oslDateTime aDT; @@ -1557,7 +1544,7 @@ inline void PDFWriterImpl::appendUnicodeTextStringEncrypt( const OUString& rInSt appendHexArray(aNewBuffer.data(), aNewBuffer.size(), rOutBuffer); } else - PDFWriter::AppendUnicodeTextString(rInString, rOutBuffer); + COSWriter::appendUnicodeTextString(rInString, rOutBuffer); rOutBuffer.append( ">" ); } @@ -5373,18 +5360,18 @@ bool PDFWriterImpl::emitCatalog() appendObjectID(rAttachedFile.mnObjectId, aLine); aLine.append("<</Type /Filespec"); aLine.append("/F<"); - PDFWriter::AppendUnicodeTextString(rAttachedFile.maFilename, aLine); + COSWriter::appendUnicodeTextString(rAttachedFile.maFilename, aLine); aLine.append("> "); if (PDFWriter::PDFVersion::PDF_1_7 <= m_aContext.Version) { aLine.append("/UF<"); - PDFWriter::AppendUnicodeTextString(rAttachedFile.maFilename, aLine); + COSWriter::appendUnicodeTextString(rAttachedFile.maFilename, aLine); aLine.append("> "); } if (!rAttachedFile.maDescription.isEmpty()) { aLine.append("/Desc <"); - PDFWriter::AppendUnicodeTextString(rAttachedFile.maDescription, aLine); + COSWriter::appendUnicodeTextString(rAttachedFile.maDescription, aLine); aLine.append("> "); } aLine.append("/EF <</F "); @@ -5420,7 +5407,7 @@ bool PDFWriterImpl::emitCatalog() for (auto & rAttachedFile : m_aDocumentAttachedFiles) { aLine.append('<'); - PDFWriter::AppendUnicodeTextString(rAttachedFile.maFilename, aLine); + COSWriter::appendUnicodeTextString(rAttachedFile.maFilename, aLine); aLine.append('>'); aLine.append(' '); appendObjectReference(rAttachedFile.mnObjectId, aLine); @@ -5816,31 +5803,31 @@ sal_Int32 PDFWriterImpl::emitInfoDict( ) { if (!m_aContext.DocumentInfo.Title.isEmpty()) { - aWriter.writeUnicodeEncrypt("/Title", m_aContext.DocumentInfo.Title, nObject, bEncrypt, rKey); + aWriter.writeKeyAndUnicodeEncrypt("/Title", m_aContext.DocumentInfo.Title, nObject, bEncrypt, rKey); } if (!m_aContext.DocumentInfo.Author.isEmpty()) { - aWriter.writeUnicodeEncrypt("/Author", m_aContext.DocumentInfo.Author, nObject, bEncrypt, rKey); + aWriter.writeKeyAndUnicodeEncrypt("/Author", m_aContext.DocumentInfo.Author, nObject, bEncrypt, rKey); } if (!m_aContext.DocumentInfo.Subject.isEmpty()) { - aWriter.writeUnicodeEncrypt("/Subject", m_aContext.DocumentInfo.Subject, nObject, bEncrypt, rKey); + aWriter.writeKeyAndUnicodeEncrypt("/Subject", m_aContext.DocumentInfo.Subject, nObject, bEncrypt, rKey); } if (!m_aContext.DocumentInfo.Keywords.isEmpty()) { - aWriter.writeUnicodeEncrypt("/Keywords", m_aContext.DocumentInfo.Keywords, nObject, bEncrypt, rKey); + aWriter.writeKeyAndUnicodeEncrypt("/Keywords", m_aContext.DocumentInfo.Keywords, nObject, bEncrypt, rKey); } if (!m_aContext.DocumentInfo.Creator.isEmpty()) { - aWriter.writeUnicodeEncrypt("/Creator", m_aContext.DocumentInfo.Creator, nObject, bEncrypt, rKey); + aWriter.writeKeyAndUnicodeEncrypt("/Creator", m_aContext.DocumentInfo.Creator, nObject, bEncrypt, rKey); } if (!m_aContext.DocumentInfo.Producer.isEmpty()) { - aWriter.writeUnicodeEncrypt("/Producer", m_aContext.DocumentInfo.Producer, nObject, bEncrypt, rKey); + aWriter.writeKeyAndUnicodeEncrypt("/Producer", m_aContext.DocumentInfo.Producer, nObject, bEncrypt, rKey); } } // Allowed in PDF 2.0 - aWriter.writeLiteralEncrypt("/CreationDate", m_aCreationDateString, nObject, bEncrypt, rKey); + aWriter.writeKeyAndLiteralEncrypt("/CreationDate", m_aCreationDateString, nObject, bEncrypt, rKey); aWriter.endDict(); aWriter.endObject(); |