Coverage Report

Created: 2026-10-14 09:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/sdext/source/pdfimport/inc/pdfparse.hxx
Line
Count
Source
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
/*
3
 * This file is part of the LibreOffice project.
4
 *
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/.
8
 *
9
 * This file incorporates work covered by the following license notice:
10
 *
11
 *   Licensed to the Apache Software Foundation (ASF) under one or more
12
 *   contributor license agreements. See the NOTICE file distributed
13
 *   with this work for additional information regarding copyright
14
 *   ownership. The ASF licenses this file to you under the Apache
15
 *   License, Version 2.0 (the "License"); you may not use this file
16
 *   except in compliance with the License. You may obtain a copy of
17
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18
 */
19
20
#ifndef INCLUDED_SDEXT_SOURCE_PDFIMPORT_INC_PDFPARSE_HXX
21
#define INCLUDED_SDEXT_SOURCE_PDFIMPORT_INC_PDFPARSE_HXX
22
23
#include <sal/types.h>
24
#include <rtl/ustring.hxx>
25
#include <rtl/string.hxx>
26
27
#include <string_view>
28
#include <unordered_map>
29
#include <utility>
30
#include <vector>
31
#include <memory>
32
33
namespace pdfparse
34
{
35
36
struct EmitImplData;
37
struct PDFContainer;
38
class EmitContext
39
{
40
public:
41
    virtual bool write( const void* pBuf, unsigned int nLen ) = 0;
42
    virtual unsigned int getCurPos() = 0;
43
    virtual bool copyOrigBytes( unsigned int nOrigOffset, unsigned int nLen ) = 0;
44
    virtual unsigned int readOrigBytes( unsigned int nOrigOffset, unsigned int nLen, void* pBuf ) = 0;
45
46
    explicit EmitContext( const PDFContainer* pTop = nullptr );
47
    virtual ~EmitContext();
48
49
    // set this to deflate contained streams
50
    bool m_bDeflate;
51
    // set this to decrypt the PDF file
52
    bool m_bDecrypt;
53
54
private:
55
    friend struct PDFEntry;
56
    std::unique_ptr<EmitImplData> m_pImplData;
57
};
58
59
struct PDFEntry
60
{
61
0
    PDFEntry() {}
62
    virtual ~PDFEntry();
63
64
    virtual bool emit( EmitContext& rWriteContext ) const = 0;
65
    virtual PDFEntry* clone() const = 0;
66
67
protected:
68
    static EmitImplData* getEmitData( EmitContext const & rContext );
69
    static void setEmitData( EmitContext& rContext, EmitImplData* pNewEmitData );
70
};
71
72
struct PDFComment final : public PDFEntry
73
{
74
    OString  m_aComment;
75
76
    explicit PDFComment( OString aComment )
77
0
    : PDFEntry(), m_aComment(std::move( aComment )) {}
78
    virtual ~PDFComment() override;
79
    virtual bool emit( EmitContext& rWriteContext ) const override;
80
    virtual PDFEntry* clone() const override;
81
};
82
83
struct PDFValue : public PDFEntry
84
{
85
    // abstract base class for simple values
86
0
    PDFValue() : PDFEntry() {}
87
    virtual ~PDFValue() override;
88
};
89
90
struct PDFName final : public PDFValue
91
{
92
    OString  m_aName;
93
94
    explicit PDFName( OString aName )
95
0
    : PDFValue(), m_aName(std::move( aName )) {}
96
    virtual ~PDFName() override;
97
    virtual bool emit( EmitContext& rWriteContext ) const override;
98
    virtual PDFEntry* clone() const override;
99
100
    OUString getFilteredName() const;
101
};
102
103
struct PDFString final : public PDFValue
104
{
105
    OString  m_aString;
106
107
    explicit PDFString( OString aString )
108
0
    : PDFValue(), m_aString(std::move( aString )) {}
109
    virtual ~PDFString() override;
110
    virtual bool emit( EmitContext& rWriteContext ) const override;
111
    virtual PDFEntry* clone() const override;
112
113
    OString getFilteredString() const;
114
};
115
116
struct PDFNumber final : public PDFValue
117
{
118
    double m_fValue;
119
120
    explicit PDFNumber( double fVal )
121
0
    : PDFValue(), m_fValue( fVal ) {}
122
    virtual ~PDFNumber() override;
123
    virtual bool emit( EmitContext& rWriteContext ) const override;
124
    virtual PDFEntry* clone() const override;
125
};
126
127
struct PDFBool final : public PDFValue
128
{
129
    bool m_bValue;
130
131
    explicit PDFBool( bool bVal )
132
0
    : PDFValue(), m_bValue( bVal ) {}
133
    virtual ~PDFBool() override;
134
    virtual bool emit( EmitContext& rWriteContext ) const override;
135
    virtual PDFEntry* clone() const override;
136
};
137
138
struct PDFObjectRef final : public PDFValue
139
{
140
    unsigned int    m_nNumber;
141
    unsigned int    m_nGeneration;
142
143
    PDFObjectRef( unsigned int nNr, unsigned int nGen )
144
0
    : PDFValue(), m_nNumber( nNr ), m_nGeneration( nGen ) {}
145
    virtual ~PDFObjectRef() override;
146
    virtual bool emit( EmitContext& rWriteContext ) const override;
147
    virtual PDFEntry* clone() const override;
148
};
149
150
struct PDFNull final : public PDFValue
151
{
152
0
    PDFNull() {}
153
    virtual ~PDFNull() override;
154
    virtual bool emit( EmitContext& rWriteContext ) const override;
155
    virtual PDFEntry* clone() const override;
156
};
157
158
struct PDFObject;
159
struct PDFContainer : public PDFEntry
160
{
161
    sal_Int32              m_nOffset;
162
    std::vector<std::unique_ptr<PDFEntry>> m_aSubElements;
163
164
    // this is an abstract base class for identifying
165
    // entries that can contain sub elements besides comments
166
0
    PDFContainer() : PDFEntry(), m_nOffset( 0 ) {}
167
    virtual ~PDFContainer() override;
168
    bool emitSubElements( EmitContext& rWriteContext ) const;
169
    void cloneSubElements( std::vector<std::unique_ptr<PDFEntry>>& rNewSubElements ) const;
170
171
    PDFObject* findObject( unsigned int nNumber, unsigned int nGeneration ) const;
172
    PDFObject* findObject( PDFObjectRef const * pRef ) const
173
0
    { return findObject( pRef->m_nNumber, pRef->m_nGeneration ); }
174
};
175
176
struct PDFArray final : public PDFContainer
177
{
178
0
    PDFArray() {}
179
    virtual ~PDFArray() override;
180
    virtual bool emit( EmitContext& rWriteContext ) const override;
181
    virtual PDFEntry* clone() const override;
182
};
183
184
struct PDFDict final : public PDFContainer
185
{
186
    typedef std::unordered_map<OString,PDFEntry*> Map;
187
    Map m_aMap;
188
189
0
    PDFDict() {}
190
    virtual ~PDFDict() override;
191
    virtual bool emit( EmitContext& rWriteContext ) const override;
192
    virtual PDFEntry* clone() const override;
193
194
    // inserting a value of NULL will remove rName and the previous value
195
    // from the dictionary
196
    void insertValue( const OString& rName, std::unique_ptr<PDFEntry> pValue );
197
    // removes a name/value pair from the dict
198
    void eraseValue( std::string_view rName );
199
    // builds new map as of sub elements
200
    // returns NULL if successful, else the first offending element
201
    PDFEntry* buildMap();
202
};
203
204
struct PDFStream final : public PDFEntry
205
{
206
    unsigned int    m_nBeginOffset;
207
    unsigned int    m_nEndOffset; // offset of the byte after the stream
208
    PDFDict*        m_pDict;
209
210
    PDFStream( unsigned int nBegin, unsigned int nEnd, PDFDict* pStreamDict )
211
0
    : PDFEntry(), m_nBeginOffset( nBegin ), m_nEndOffset( nEnd ), m_pDict( pStreamDict ) {}
212
    virtual ~PDFStream() override;
213
    virtual bool emit( EmitContext& rWriteContext ) const override;
214
    virtual PDFEntry* clone() const override;
215
216
    unsigned int getDictLength( const PDFContainer* pObjectContainer ) const; // get contents of the "Length" entry of the dict
217
};
218
219
struct PDFTrailer final : public PDFContainer
220
{
221
    PDFDict*        m_pDict;
222
223
0
    PDFTrailer() : PDFContainer(), m_pDict( nullptr ) {}
224
    virtual ~PDFTrailer() override;
225
    virtual bool emit( EmitContext& rWriteContext ) const override;
226
    virtual PDFEntry* clone() const override;
227
};
228
229
struct PDFFileImplData;
230
struct PDFFile final : public PDFContainer
231
{
232
private:
233
    mutable std::unique_ptr<PDFFileImplData> m_pData;
234
    PDFFileImplData*            impl_getData() const;
235
public:
236
    unsigned int        m_nMajor;           // PDF major
237
    unsigned int        m_nMinor;           // PDF minor
238
239
    PDFFile();
240
    virtual ~PDFFile() override;
241
242
    virtual bool emit( EmitContext& rWriteContext ) const override;
243
    virtual PDFEntry* clone() const override;
244
245
    bool isEncrypted() const;
246
247
    bool usesSupportedEncryptionFormat() const;
248
249
    // this method checks whether rPwd is compatible with
250
    // either user or owner password and sets up decrypt data in that case
251
    // returns true if decryption can be done
252
    bool setupDecryptionData( const OString& rPwd ) const;
253
254
    bool decrypt( const sal_uInt8* pInBuffer, sal_uInt32 nLen,
255
                  sal_uInt8* pOutBuffer,
256
                  unsigned int nObject, unsigned int nGeneration ) const;
257
};
258
259
struct PDFObject final : public PDFContainer
260
{
261
    PDFEntry*       m_pObject;
262
    PDFStream*      m_pStream;
263
    unsigned int    m_nNumber;
264
    unsigned int    m_nGeneration;
265
266
    PDFObject( unsigned int nNr, unsigned int nGen )
267
0
    : m_pObject( nullptr ), m_pStream( nullptr ), m_nNumber( nNr ), m_nGeneration( nGen ) {}
268
    virtual ~PDFObject() override;
269
    virtual bool emit( EmitContext& rWriteContext ) const override;
270
    virtual PDFEntry* clone() const override;
271
272
    // writes only the contained stream, deflated if necessary
273
    void writeStream( EmitContext& rContext, const PDFFile* pPDFFile ) const;
274
275
private:
276
    // returns true if stream is deflated
277
    // fills *ppStream and *pBytes with start of stream and count of bytes
278
    // memory returned in *ppStream must be freed with std::free afterwards
279
    // fills in NULL and 0 in case of error
280
    bool getDeflatedStream( std::unique_ptr<char[]>& rpStream, unsigned int* pBytes, const PDFContainer* pObjectContainer, EmitContext& rContext ) const;
281
};
282
283
struct PDFPart final : public PDFContainer
284
{
285
0
    PDFPart() : PDFContainer() {}
286
    virtual ~PDFPart() override;
287
    virtual bool emit( EmitContext& rWriteContext ) const override;
288
    virtual PDFEntry* clone() const override;
289
};
290
291
struct PDFReader
292
{
293
    PDFReader() = delete;
294
295
    static std::unique_ptr<PDFEntry> read(std::u16string_view aFileName);
296
};
297
298
} // namespace
299
300
#endif
301
302
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */