vcl/Library_vcl.mk | 1 vcl/inc/pdf/COSWriter.hxx | 142 ++--------------------------------------- vcl/source/pdf/COSWriter.cxx | 147 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 155 insertions(+), 135 deletions(-)
New commits: commit 5d2b03b564e5f89d972d49a86154163abeeef0e7 Author: Tomaž Vajngerl <tomaz.vajng...@collabora.co.uk> AuthorDate: Fri Dec 13 15:06:48 2024 +0900 Commit: Miklos Vajna <vmik...@collabora.com> CommitDate: Mon Dec 16 13:39:35 2024 +0100 pdf: move COSWriter complex methods into the cxx source file So those are not inlined needlesly all over the PDF writer code. Change-Id: Ic3e3b442a3dc95d3bd944e7d7add285fe1beb772 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/178412 Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoff...@gmail.com> Reviewed-by: Miklos Vajna <vmik...@collabora.com> diff --git a/vcl/Library_vcl.mk b/vcl/Library_vcl.mk index 685ddfd46316..b3f6f6b314a1 100644 --- a/vcl/Library_vcl.mk +++ b/vcl/Library_vcl.mk @@ -494,6 +494,7 @@ $(eval $(call gb_Library_add_exception_objects,vcl,\ vcl/source/fontsubset/sft \ vcl/source/fontsubset/ttcr \ vcl/source/fontsubset/xlat \ + vcl/source/pdf/COSWriter \ vcl/source/pdf/EncryptionHashTransporter \ vcl/source/pdf/ExternalPDFStreams \ vcl/source/pdf/PDFiumTools \ diff --git a/vcl/inc/pdf/COSWriter.hxx b/vcl/inc/pdf/COSWriter.hxx index 8cd7045f2011..2142de1f846e 100644 --- a/vcl/inc/pdf/COSWriter.hxx +++ b/vcl/inc/pdf/COSWriter.hxx @@ -27,41 +27,7 @@ class COSWriter OStringBuffer maLine; OStringBuffer& mrBuffer; - void appendLiteralString(const char* pStr, sal_Int32 nLength) - { - while (nLength) - { - switch (*pStr) - { - case ' ': - mrBuffer.append("\n"); - break; - case ' ': - mrBuffer.append("\r"); - break; - case ' ': - mrBuffer.append("\t"); - break; - case '': - mrBuffer.append("\b"); - break; - case '': - mrBuffer.append("\f"); - break; - case '(': - case ')': - case '\': - mrBuffer.append("\"); - mrBuffer.append(static_cast<char>(*pStr)); - break; - default: - mrBuffer.append(static_cast<char>(*pStr)); - break; - } - pStr++; - nLength--; - } - } + void appendLiteralString(const char* pStr, sal_Int32 nLength); template <typename T> static void appendHex(T nValue, OStringBuffer& rBuffer) { @@ -143,22 +109,7 @@ public: writeUnicode(rString); } - void writeUnicode(OUString const& rString) - { - mrBuffer.append("<"); - - mrBuffer.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), mrBuffer); - appendHex(sal_Int8(aChar & 255), mrBuffer); - } - - mrBuffer.append(">"); - } + void writeUnicode(OUString const& rString); void writeKeyAndUnicodeEncrypt(std::string_view key, OUString const& rString, sal_Int32 nObject) { @@ -166,38 +117,7 @@ public: writeUnicodeEncrypt(rString, nObject); } - void writeUnicodeEncrypt(OUString const& rString, sal_Int32 nObject) - { - if (maParams.mbCanEncrypt && mpPDFEncryptor) - { - mrBuffer.append("<"); - const sal_Unicode* pString = rString.getStr(); - size_t nLength = rString.getLength(); - //prepare a unicode string, encrypt it - mpPDFEncryptor->setupEncryption(maParams.maKey, nObject); - sal_Int32 nChars = 2 + (nLength * 2); - std::vector<sal_uInt8> aEncryptionBuffer(nChars); - sal_uInt8* pCopy = aEncryptionBuffer.data(); - *pCopy++ = 0xFE; - *pCopy++ = 0xFF; - // we need to prepare a byte stream from the unicode string buffer - for (size_t i = 0; i < nLength; i++) - { - sal_Unicode aUnicodeChar = pString[i]; - *pCopy++ = sal_uInt8(aUnicodeChar >> 8); - *pCopy++ = sal_uInt8(aUnicodeChar & 255); - } - std::vector<sal_uInt8> aNewBuffer(nChars); - mpPDFEncryptor->encrypt(aEncryptionBuffer.data(), nChars, aNewBuffer, nChars); - //now append, hexadecimal (appendHex), the encrypted result - appendHexArray(aNewBuffer.data(), aNewBuffer.size()); - mrBuffer.append(">"); - } - else - { - writeUnicode(rString); - } - } + void writeUnicodeEncrypt(OUString const& rString, sal_Int32 nObject); void writeLiteral(std::string_view value) { @@ -207,73 +127,25 @@ public: } void writeLiteralEncrypt(std::u16string_view value, sal_Int32 nObject, - rtl_TextEncoding nEncoding = RTL_TEXTENCODING_ASCII_US) - { - OString aBufferString(OUStringToOString(value, nEncoding)); - sal_Int32 nLength = aBufferString.getLength(); - OStringBuffer aBuffer(nLength); - const char* pT = aBufferString.getStr(); - - for (sal_Int32 i = 0; i < nLength; i++, pT++) - { - if ((*pT & 0x80) == 0) - aBuffer.append(*pT); - else - { - aBuffer.append('<'); - appendHex(*pT, aBuffer); - aBuffer.append('>'); - } - } - writeLiteralEncrypt(aBuffer.makeStringAndClear(), nObject); - } + rtl_TextEncoding nEncoding = RTL_TEXTENCODING_ASCII_US); - void writeLiteralEncrypt(std::string_view value, sal_Int32 nObject) - { - if (maParams.mbCanEncrypt && mpPDFEncryptor) - { - mrBuffer.append("("); - size_t nChars = value.size(); - std::vector<sal_uInt8> aEncryptionBuffer(nChars); - mpPDFEncryptor->setupEncryption(maParams.maKey, nObject); - mpPDFEncryptor->encrypt(value.data(), nChars, aEncryptionBuffer, nChars); - appendLiteralString(reinterpret_cast<char*>(aEncryptionBuffer.data()), - aEncryptionBuffer.size()); - mrBuffer.append(")"); - } - else - { - writeLiteral(value); - } - } + void writeLiteralEncrypt(std::string_view value, sal_Int32 nObject); void writeKeyAndLiteralEncrypt(std::string_view key, std::string_view value, sal_Int32 nObject) { mrBuffer.append(key); - mrBuffer.append(" "); writeLiteralEncrypt(value, nObject); } void writeHexArray(std::string_view key, sal_uInt8* pData, size_t nSize) { mrBuffer.append(key); - mrBuffer.append(" <"); + mrBuffer.append("<"); appendHexArray(pData, nSize); mrBuffer.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); - } - } + static void appendUnicodeTextString(const OUString& rString, OStringBuffer& rBuffer); }; } diff --git a/vcl/source/pdf/COSWriter.cxx b/vcl/source/pdf/COSWriter.cxx new file mode 100644 index 000000000000..100da2e243aa --- /dev/null +++ b/vcl/source/pdf/COSWriter.cxx @@ -0,0 +1,147 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include <pdf/COSWriter.hxx> +#include <rtl/ustring.hxx> + +namespace vcl::pdf +{ +void COSWriter::appendLiteralString(const char* pStr, sal_Int32 nLength) +{ + while (nLength) + { + switch (*pStr) + { + case ' ': + mrBuffer.append("\n"); + break; + case ' ': + mrBuffer.append("\r"); + break; + case ' ': + mrBuffer.append("\t"); + break; + case '': + mrBuffer.append("\b"); + break; + case '': + mrBuffer.append("\f"); + break; + case '(': + case ')': + case '\': + mrBuffer.append("\"); + mrBuffer.append(static_cast<char>(*pStr)); + break; + default: + mrBuffer.append(static_cast<char>(*pStr)); + break; + } + pStr++; + nLength--; + } +} + +void COSWriter::writeUnicode(OUString const& rString) +{ + mrBuffer.append("<"); + COSWriter::appendUnicodeTextString(rString, mrBuffer); + mrBuffer.append(">"); +} + +void COSWriter::writeUnicodeEncrypt(OUString const& rString, sal_Int32 nObject) +{ + if (maParams.mbCanEncrypt && mpPDFEncryptor) + { + mrBuffer.append("<"); + const sal_Unicode* pString = rString.getStr(); + size_t nLength = rString.getLength(); + //prepare a unicode string, encrypt it + mpPDFEncryptor->setupEncryption(maParams.maKey, nObject); + sal_Int32 nChars = 2 + (nLength * 2); + std::vector<sal_uInt8> aEncryptionBuffer(nChars); + sal_uInt8* pCopy = aEncryptionBuffer.data(); + *pCopy++ = 0xFE; + *pCopy++ = 0xFF; + // we need to prepare a byte stream from the unicode string buffer + for (size_t i = 0; i < nLength; i++) + { + sal_Unicode aUnicodeChar = pString[i]; + *pCopy++ = sal_uInt8(aUnicodeChar >> 8); + *pCopy++ = sal_uInt8(aUnicodeChar & 255); + } + std::vector<sal_uInt8> aNewBuffer(nChars); + mpPDFEncryptor->encrypt(aEncryptionBuffer.data(), nChars, aNewBuffer, nChars); + //now append, hexadecimal (appendHex), the encrypted result + appendHexArray(aNewBuffer.data(), aNewBuffer.size()); + mrBuffer.append(">"); + } + else + { + writeUnicode(rString); + } +} + +void COSWriter::writeLiteralEncrypt(std::u16string_view value, sal_Int32 nObject, + rtl_TextEncoding nEncoding) +{ + OString aBufferString(OUStringToOString(value, nEncoding)); + sal_Int32 nLength = aBufferString.getLength(); + OStringBuffer aBuffer(nLength); + const char* pT = aBufferString.getStr(); + + for (sal_Int32 i = 0; i < nLength; i++, pT++) + { + if ((*pT & 0x80) == 0) + aBuffer.append(*pT); + else + { + aBuffer.append('<'); + appendHex(*pT, aBuffer); + aBuffer.append('>'); + } + } + writeLiteralEncrypt(aBuffer.makeStringAndClear(), nObject); +} + +void COSWriter::writeLiteralEncrypt(std::string_view value, sal_Int32 nObject) +{ + if (maParams.mbCanEncrypt && mpPDFEncryptor) + { + mrBuffer.append("("); + size_t nChars = value.size(); + std::vector<sal_uInt8> aEncryptionBuffer(nChars); + mpPDFEncryptor->setupEncryption(maParams.maKey, nObject); + mpPDFEncryptor->encrypt(value.data(), nChars, aEncryptionBuffer, nChars); + appendLiteralString(reinterpret_cast<char*>(aEncryptionBuffer.data()), + aEncryptionBuffer.size()); + mrBuffer.append(")"); + } + else + { + writeLiteral(value); + } +} + +void COSWriter::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); + } +} + +} //end vcl::pdf + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */