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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
/* -*- 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/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#ifndef INCLUDED_SDEXT_SOURCE_PDFIMPORT_INC_PDFPARSE_HXX
#define INCLUDED_SDEXT_SOURCE_PDFIMPORT_INC_PDFPARSE_HXX
#include <sal/types.h>
#include <rtl/ustring.hxx>
#include <rtl/string.hxx>
#include <unordered_map>
#include <vector>
#include <memory>
namespace pdfparse
{
struct EmitImplData;
struct PDFContainer;
class EmitContext
{
public:
virtual bool write( const void* pBuf, unsigned int nLen ) = 0;
virtual unsigned int getCurPos() = 0;
virtual bool copyOrigBytes( unsigned int nOrigOffset, unsigned int nLen ) = 0;
virtual unsigned int readOrigBytes( unsigned int nOrigOffset, unsigned int nLen, void* pBuf ) = 0;
explicit EmitContext( const PDFContainer* pTop = nullptr );
virtual ~EmitContext();
// set this to deflate contained streams
bool m_bDeflate;
// set this to decrypt the PDF file
bool m_bDecrypt;
private:
friend struct PDFEntry;
std::unique_ptr<EmitImplData> m_pImplData;
};
struct PDFEntry
{
PDFEntry() {}
virtual ~PDFEntry();
virtual bool emit( EmitContext& rWriteContext ) const = 0;
virtual PDFEntry* clone() const = 0;
protected:
static EmitImplData* getEmitData( EmitContext& rContext );
static void setEmitData( EmitContext& rContext, EmitImplData* pNewEmitData );
};
struct PDFComment : public PDFEntry
{
OString m_aComment;
explicit PDFComment( const OString& rComment )
: PDFEntry(), m_aComment( rComment ) {}
virtual ~PDFComment() override;
virtual bool emit( EmitContext& rWriteContext ) const override;
virtual PDFEntry* clone() const override;
};
struct PDFValue : public PDFEntry
{
// abstract base class for simple values
PDFValue() : PDFEntry() {}
virtual ~PDFValue() override;
};
struct PDFName : public PDFValue
{
OString m_aName;
explicit PDFName( const OString& rName )
: PDFValue(), m_aName( rName ) {}
virtual ~PDFName() override;
virtual bool emit( EmitContext& rWriteContext ) const override;
virtual PDFEntry* clone() const override;
OUString getFilteredName() const;
};
struct PDFString : public PDFValue
{
OString m_aString;
explicit PDFString( const OString& rString )
: PDFValue(), m_aString( rString ) {}
virtual ~PDFString() override;
virtual bool emit( EmitContext& rWriteContext ) const override;
virtual PDFEntry* clone() const override;
OString getFilteredString() const;
};
struct PDFNumber : public PDFValue
{
double m_fValue;
explicit PDFNumber( double fVal )
: PDFValue(), m_fValue( fVal ) {}
virtual ~PDFNumber() override;
virtual bool emit( EmitContext& rWriteContext ) const override;
virtual PDFEntry* clone() const override;
};
struct PDFBool : public PDFValue
{
bool m_bValue;
explicit PDFBool( bool bVal )
: PDFValue(), m_bValue( bVal ) {}
virtual ~PDFBool() override;
virtual bool emit( EmitContext& rWriteContext ) const override;
virtual PDFEntry* clone() const override;
};
struct PDFObjectRef : public PDFValue
{
unsigned int m_nNumber;
unsigned int m_nGeneration;
PDFObjectRef( unsigned int nNr, unsigned int nGen )
: PDFValue(), m_nNumber( nNr ), m_nGeneration( nGen ) {}
virtual ~PDFObjectRef() override;
virtual bool emit( EmitContext& rWriteContext ) const override;
virtual PDFEntry* clone() const override;
};
struct PDFNull : public PDFValue
{
PDFNull() {}
virtual ~PDFNull() override;
virtual bool emit( EmitContext& rWriteContext ) const override;
virtual PDFEntry* clone() const override;
};
struct PDFObject;
struct PDFContainer : public PDFEntry
{
sal_Int32 m_nOffset;
std::vector<PDFEntry*> m_aSubElements;
// this is an abstract base class for identifying
// entries that can contain sub elements besides comments
PDFContainer() : PDFEntry(), m_nOffset( 0 ) {}
virtual ~PDFContainer() override;
bool emitSubElements( EmitContext& rWriteContext ) const;
void cloneSubElements( std::vector<PDFEntry*>& rNewSubElements ) const;
PDFObject* findObject( unsigned int nNumber, unsigned int nGeneration ) const;
PDFObject* findObject( PDFObjectRef* pRef ) const
{ return findObject( pRef->m_nNumber, pRef->m_nGeneration ); }
};
struct PDFArray : public PDFContainer
{
PDFArray() {}
virtual ~PDFArray() override;
virtual bool emit( EmitContext& rWriteContext ) const override;
virtual PDFEntry* clone() const override;
};
struct PDFDict : public PDFContainer
{
typedef std::unordered_map<OString,PDFEntry*,OStringHash> Map;
Map m_aMap;
PDFDict() {}
virtual ~PDFDict() override;
virtual bool emit( EmitContext& rWriteContext ) const override;
virtual PDFEntry* clone() const override;
// inserting a value of NULL will remove rName and the previous value
// from the dictionary
void insertValue( const OString& rName, PDFEntry* pValue );
// removes a name/value pair from the dict
void eraseValue( const OString& rName );
// builds new map as of sub elements
// returns NULL if successful, else the first offending element
PDFEntry* buildMap();
};
struct PDFStream : public PDFEntry
{
unsigned int m_nBeginOffset;
unsigned int m_nEndOffset; // offset of the byte after the stream
PDFDict* m_pDict;
PDFStream( unsigned int nBegin, unsigned int nEnd, PDFDict* pStreamDict )
: PDFEntry(), m_nBeginOffset( nBegin ), m_nEndOffset( nEnd ), m_pDict( pStreamDict ) {}
virtual ~PDFStream() override;
virtual bool emit( EmitContext& rWriteContext ) const override;
virtual PDFEntry* clone() const override;
unsigned int getDictLength( const PDFContainer* pObjectContainer ) const; // get contents of the "Length" entry of the dict
};
struct PDFTrailer : public PDFContainer
{
PDFDict* m_pDict;
PDFTrailer() : PDFContainer(), m_pDict( nullptr ) {}
virtual ~PDFTrailer() override;
virtual bool emit( EmitContext& rWriteContext ) const override;
virtual PDFEntry* clone() const override;
};
struct PDFFileImplData;
struct PDFFile : public PDFContainer
{
private:
mutable std::unique_ptr<PDFFileImplData> m_pData;
PDFFileImplData* impl_getData() const;
public:
unsigned int m_nMajor; // PDF major
unsigned int m_nMinor; // PDF minor
PDFFile();
virtual ~PDFFile() override;
virtual bool emit( EmitContext& rWriteContext ) const override;
virtual PDFEntry* clone() const override;
bool isEncrypted() const;
bool usesSupportedEncryptionFormat() const;
// this method checks whether rPwd is compatible with
// either user or owner password and sets up decrypt data in that case
// returns true if decryption can be done
bool setupDecryptionData( const OString& rPwd ) const;
bool decrypt( const sal_uInt8* pInBuffer, sal_uInt32 nLen,
sal_uInt8* pOutBuffer,
unsigned int nObject, unsigned int nGeneration ) const;
};
struct PDFObject : public PDFContainer
{
PDFEntry* m_pObject;
PDFStream* m_pStream;
unsigned int m_nNumber;
unsigned int m_nGeneration;
PDFObject( unsigned int nNr, unsigned int nGen )
: m_pObject( nullptr ), m_pStream( nullptr ), m_nNumber( nNr ), m_nGeneration( nGen ) {}
virtual ~PDFObject() override;
virtual bool emit( EmitContext& rWriteContext ) const override;
virtual PDFEntry* clone() const override;
// writes only the contained stream, deflated if necessary
void writeStream( EmitContext& rContext, const PDFFile* pPDFFile ) const;
private:
// returns true if stream is deflated
// fills *ppStream and *pBytes with start of stream and count of bytes
// memory returned in *ppStream must be freed with rtl_freeMemory afterwards
// fills in NULL and 0 in case of error
bool getDeflatedStream( char** ppStream, unsigned int* pBytes, const PDFContainer* pObjectContainer, EmitContext& rContext ) const;
};
struct PDFPart : public PDFContainer
{
PDFPart() : PDFContainer() {}
virtual ~PDFPart() override;
virtual bool emit( EmitContext& rWriteContext ) const override;
virtual PDFEntry* clone() const override;
};
class PDFReader
{
public:
PDFReader() {}
~PDFReader() {}
static PDFEntry* read( const char* pFileName );
#ifdef _WIN32
static PDFEntry* read( const char* pBuffer, unsigned int nLen );
#endif
};
} // namespace
#endif
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|