1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
12 #include <pdf/IPDFEncryptor.hxx>
13 #include <vcl/pdfwriter.hxx>
17 /** Writes the "Carousel" object structure (COS) to the string buffer.
19 * "Carousel" object structure (COS) is used by PDF.
21 * Structure elements like: objects, IDs, dictionaries, key/values, ...
25 std::shared_ptr
<IPDFEncryptor
> mpPDFEncryptor
;
26 EncryptionParams maParams
;
28 OStringBuffer
& mrBuffer
;
30 void appendLiteralString(const char* pStr
, sal_Int32 nLength
);
33 COSWriter(EncryptionParams aParams
= EncryptionParams(),
34 std::shared_ptr
<IPDFEncryptor
> const& pPDFEncryptor
= nullptr)
35 : mpPDFEncryptor(pPDFEncryptor
)
36 , maParams(std::move(aParams
))
42 COSWriter(OStringBuffer
& rBuffer
, EncryptionParams aParams
= EncryptionParams(),
43 std::shared_ptr
<IPDFEncryptor
> const& pPDFEncryptor
= nullptr)
44 : mpPDFEncryptor(pPDFEncryptor
)
45 , maParams(std::move(aParams
))
50 void startObject(sal_Int32 nObjectID
)
52 mrBuffer
.append(nObjectID
);
53 mrBuffer
.append(" 0 obj\n");
56 void endObject() { mrBuffer
.append("endobj\n\n"); }
58 OStringBuffer
& getLine() { return mrBuffer
; }
60 void startDict() { mrBuffer
.append("<<"); }
61 void startDictWithKey(std::string_view key
)
64 mrBuffer
.append("<<");
66 void endDict() { mrBuffer
.append(">>\n"); }
68 void startStream() { mrBuffer
.append("stream\n"); }
69 void endStream() { mrBuffer
.append("\nendstream\n"); }
71 void write(std::string_view key
, std::string_view value
)
74 mrBuffer
.append(value
);
77 void write(std::string_view key
, sal_Int32 value
)
81 mrBuffer
.append(value
);
84 void writeReference(sal_Int32 nObjectID
)
86 mrBuffer
.append(nObjectID
);
87 mrBuffer
.append(" 0 R");
90 void writeKeyAndReference(std::string_view key
, sal_Int32 nObjectID
)
94 writeReference(nObjectID
);
97 void writeKeyAndUnicode(std::string_view key
, OUString
const& rString
)
100 writeUnicode(rString
);
103 void writeUnicode(OUString
const& rString
);
105 void writeKeyAndUnicodeEncrypt(std::string_view key
, OUString
const& rString
, sal_Int32 nObject
)
107 mrBuffer
.append(key
);
108 writeUnicodeEncrypt(rString
, nObject
);
111 void writeUnicodeEncrypt(OUString
const& rString
, sal_Int32 nObject
);
113 void writeLiteral(std::string_view value
)
115 mrBuffer
.append("(");
116 appendLiteralString(value
.data(), value
.size());
117 mrBuffer
.append(")");
120 void writeKeyAndLiteral(std::string_view key
, std::string_view value
)
122 mrBuffer
.append(key
);
126 void writeLiteralEncrypt(std::u16string_view value
, sal_Int32 nObject
,
127 rtl_TextEncoding nEncoding
= RTL_TEXTENCODING_ASCII_US
);
129 void writeKeyAndLiteralEncrypt(std::string_view key
, std::u16string_view value
,
131 rtl_TextEncoding nEncoding
= RTL_TEXTENCODING_ASCII_US
)
133 mrBuffer
.append(key
);
134 writeLiteralEncrypt(value
, nObject
, nEncoding
);
137 void writeLiteralEncrypt(std::string_view value
, sal_Int32 nObject
);
139 void writeKeyAndLiteralEncrypt(std::string_view key
, std::string_view value
, sal_Int32 nObject
)
141 mrBuffer
.append(key
);
142 writeLiteralEncrypt(value
, nObject
);
145 void writeHexArray(std::string_view key
, sal_uInt8
* pData
, size_t nSize
)
147 mrBuffer
.append(key
);
148 mrBuffer
.append("<");
149 COSWriter::appendHexArray(pData
, nSize
, mrBuffer
);
150 mrBuffer
.append(">");
153 template <typename T
> static void appendHex(T nValue
, OStringBuffer
& rBuffer
)
155 static constexpr const auto constHexDigits
= std::to_array
<char>(
156 { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' });
157 rBuffer
.append(constHexDigits
[(nValue
>> 4) & 15]);
158 rBuffer
.append(constHexDigits
[nValue
& 15]);
161 static void appendHexArray(sal_uInt8
* pArray
, size_t nSize
, OStringBuffer
& rBuffer
)
163 for (size_t i
= 0; i
< nSize
; i
++)
164 appendHex(pArray
[i
], rBuffer
);
167 static void appendUnicodeTextString(const OUString
& rString
, OStringBuffer
& rBuffer
);
168 static void appendName(std::u16string_view rString
, OStringBuffer
& rBuffer
);
169 static void appendName(const char* pString
, OStringBuffer
& rBuffer
);
173 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */