1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
/* -*- 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/.
*/
#pragma once
#include <pdf/IPDFEncryptor.hxx>
#include <vcl/pdfwriter.hxx>
namespace vcl::pdf
{
/** Writes the "Carousel" object structure (COS) to the string buffer.
*
* "Carousel" object structure (COS) is used by PDF.
*
* Structure elements like: objects, IDs, dictionaries, key/values, ...
*/
class COSWriter
{
std::shared_ptr<IPDFEncryptor> mpPDFEncryptor;
EncryptionParams maParams;
OStringBuffer maLine;
OStringBuffer& mrBuffer;
void appendLiteralString(const char* pStr, sal_Int32 nLength);
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' });
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], mrBuffer);
}
public:
COSWriter(EncryptionParams aParams = EncryptionParams(),
std::shared_ptr<IPDFEncryptor> const& pPDFEncryptor = nullptr)
: mpPDFEncryptor(pPDFEncryptor)
, maParams(aParams)
, maLine(1024)
, mrBuffer(maLine)
{
}
COSWriter(OStringBuffer& rBuffer, EncryptionParams aParams = EncryptionParams(),
std::shared_ptr<IPDFEncryptor> const& pPDFEncryptor = nullptr)
: mpPDFEncryptor(pPDFEncryptor)
, maParams(aParams)
, mrBuffer(rBuffer)
{
}
void startObject(sal_Int32 nObjectID)
{
mrBuffer.append(nObjectID);
mrBuffer.append(" 0 obj\n");
}
void endObject() { mrBuffer.append("endobj\n\n"); }
OStringBuffer& getLine() { return mrBuffer; }
void startDict() { mrBuffer.append("<<"); }
void startDictWithKey(std::string_view key)
{
mrBuffer.append(key);
mrBuffer.append("<<");
}
void endDict() { mrBuffer.append(">>\n"); }
void startStream() { mrBuffer.append("stream\n"); }
void endStream() { mrBuffer.append("\nendstream\n"); }
void write(std::string_view key, std::string_view value)
{
mrBuffer.append(key);
mrBuffer.append(value);
}
void write(std::string_view key, sal_Int32 value)
{
mrBuffer.append(key);
mrBuffer.append(" ");
mrBuffer.append(value);
}
void writeReference(sal_Int32 nObjectID)
{
mrBuffer.append(nObjectID);
mrBuffer.append(" 0 R");
}
void writeKeyAndReference(std::string_view key, sal_Int32 nObjectID)
{
mrBuffer.append(key);
mrBuffer.append(" ");
writeReference(nObjectID);
}
void writeKeyAndUnicode(std::string_view key, OUString const& rString)
{
mrBuffer.append(key);
writeUnicode(rString);
}
void writeUnicode(OUString const& rString);
void writeKeyAndUnicodeEncrypt(std::string_view key, OUString const& rString, sal_Int32 nObject)
{
mrBuffer.append(key);
writeUnicodeEncrypt(rString, nObject);
}
void writeUnicodeEncrypt(OUString const& rString, sal_Int32 nObject);
void writeLiteral(std::string_view value)
{
mrBuffer.append("(");
appendLiteralString(value.data(), value.size());
mrBuffer.append(")");
}
void writeLiteralEncrypt(std::u16string_view value, sal_Int32 nObject,
rtl_TextEncoding nEncoding = RTL_TEXTENCODING_ASCII_US);
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);
writeLiteralEncrypt(value, nObject);
}
void writeHexArray(std::string_view key, sal_uInt8* pData, size_t nSize)
{
mrBuffer.append(key);
mrBuffer.append("<");
appendHexArray(pData, nSize);
mrBuffer.append(">");
}
static void appendUnicodeTextString(const OUString& rString, OStringBuffer& rBuffer);
static void appendName(std::u16string_view rString, OStringBuffer& rBuffer);
static void appendName(const char* pString, OStringBuffer& rBuffer);
};
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|