cid#1607171 Data race condition
[LibreOffice.git] / sdext / source / pdfimport / inc / pdfparse.hxx
blob542a9ed4b1a5ea4f12b8403f90a409565de1ab1e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef INCLUDED_SDEXT_SOURCE_PDFIMPORT_INC_PDFPARSE_HXX
21 #define INCLUDED_SDEXT_SOURCE_PDFIMPORT_INC_PDFPARSE_HXX
23 #include <sal/types.h>
24 #include <rtl/ustring.hxx>
25 #include <rtl/string.hxx>
27 #include <string_view>
28 #include <unordered_map>
29 #include <utility>
30 #include <vector>
31 #include <memory>
33 namespace pdfparse
36 struct EmitImplData;
37 struct PDFContainer;
38 class EmitContext
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;
46 explicit EmitContext( const PDFContainer* pTop = nullptr );
47 virtual ~EmitContext();
49 // set this to deflate contained streams
50 bool m_bDeflate;
51 // set this to decrypt the PDF file
52 bool m_bDecrypt;
54 private:
55 friend struct PDFEntry;
56 std::unique_ptr<EmitImplData> m_pImplData;
59 struct PDFEntry
61 PDFEntry() {}
62 virtual ~PDFEntry();
64 virtual bool emit( EmitContext& rWriteContext ) const = 0;
65 virtual PDFEntry* clone() const = 0;
67 protected:
68 static EmitImplData* getEmitData( EmitContext const & rContext );
69 static void setEmitData( EmitContext& rContext, EmitImplData* pNewEmitData );
72 struct PDFComment final : public PDFEntry
74 OString m_aComment;
76 explicit PDFComment( OString aComment )
77 : PDFEntry(), m_aComment(std::move( aComment )) {}
78 virtual ~PDFComment() override;
79 virtual bool emit( EmitContext& rWriteContext ) const override;
80 virtual PDFEntry* clone() const override;
83 struct PDFValue : public PDFEntry
85 // abstract base class for simple values
86 PDFValue() : PDFEntry() {}
87 virtual ~PDFValue() override;
90 struct PDFName final : public PDFValue
92 OString m_aName;
94 explicit PDFName( OString aName )
95 : PDFValue(), m_aName(std::move( aName )) {}
96 virtual ~PDFName() override;
97 virtual bool emit( EmitContext& rWriteContext ) const override;
98 virtual PDFEntry* clone() const override;
100 OUString getFilteredName() const;
103 struct PDFString final : public PDFValue
105 OString m_aString;
107 explicit PDFString( OString aString )
108 : PDFValue(), m_aString(std::move( aString )) {}
109 virtual ~PDFString() override;
110 virtual bool emit( EmitContext& rWriteContext ) const override;
111 virtual PDFEntry* clone() const override;
113 OString getFilteredString() const;
116 struct PDFNumber final : public PDFValue
118 double m_fValue;
120 explicit PDFNumber( double fVal )
121 : PDFValue(), m_fValue( fVal ) {}
122 virtual ~PDFNumber() override;
123 virtual bool emit( EmitContext& rWriteContext ) const override;
124 virtual PDFEntry* clone() const override;
127 struct PDFBool final : public PDFValue
129 bool m_bValue;
131 explicit PDFBool( bool bVal )
132 : PDFValue(), m_bValue( bVal ) {}
133 virtual ~PDFBool() override;
134 virtual bool emit( EmitContext& rWriteContext ) const override;
135 virtual PDFEntry* clone() const override;
138 struct PDFObjectRef final : public PDFValue
140 unsigned int m_nNumber;
141 unsigned int m_nGeneration;
143 PDFObjectRef( unsigned int nNr, unsigned int nGen )
144 : 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;
150 struct PDFNull final : public PDFValue
152 PDFNull() {}
153 virtual ~PDFNull() override;
154 virtual bool emit( EmitContext& rWriteContext ) const override;
155 virtual PDFEntry* clone() const override;
158 struct PDFObject;
159 struct PDFContainer : public PDFEntry
161 sal_Int32 m_nOffset;
162 std::vector<std::unique_ptr<PDFEntry>> m_aSubElements;
164 // this is an abstract base class for identifying
165 // entries that can contain sub elements besides comments
166 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;
171 PDFObject* findObject( unsigned int nNumber, unsigned int nGeneration ) const;
172 PDFObject* findObject( PDFObjectRef const * pRef ) const
173 { return findObject( pRef->m_nNumber, pRef->m_nGeneration ); }
176 struct PDFArray final : public PDFContainer
178 PDFArray() {}
179 virtual ~PDFArray() override;
180 virtual bool emit( EmitContext& rWriteContext ) const override;
181 virtual PDFEntry* clone() const override;
184 struct PDFDict final : public PDFContainer
186 typedef std::unordered_map<OString,PDFEntry*> Map;
187 Map m_aMap;
189 PDFDict() {}
190 virtual ~PDFDict() override;
191 virtual bool emit( EmitContext& rWriteContext ) const override;
192 virtual PDFEntry* clone() const override;
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();
204 struct PDFStream final : public PDFEntry
206 unsigned int m_nBeginOffset;
207 unsigned int m_nEndOffset; // offset of the byte after the stream
208 PDFDict* m_pDict;
210 PDFStream( unsigned int nBegin, unsigned int nEnd, PDFDict* pStreamDict )
211 : 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;
216 unsigned int getDictLength( const PDFContainer* pObjectContainer ) const; // get contents of the "Length" entry of the dict
219 struct PDFTrailer final : public PDFContainer
221 PDFDict* m_pDict;
223 PDFTrailer() : PDFContainer(), m_pDict( nullptr ) {}
224 virtual ~PDFTrailer() override;
225 virtual bool emit( EmitContext& rWriteContext ) const override;
226 virtual PDFEntry* clone() const override;
229 struct PDFFileImplData;
230 struct PDFFile final : public PDFContainer
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
239 PDFFile();
240 virtual ~PDFFile() override;
242 virtual bool emit( EmitContext& rWriteContext ) const override;
243 virtual PDFEntry* clone() const override;
245 bool isEncrypted() const;
247 bool usesSupportedEncryptionFormat() const;
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;
254 bool decrypt( const sal_uInt8* pInBuffer, sal_uInt32 nLen,
255 sal_uInt8* pOutBuffer,
256 unsigned int nObject, unsigned int nGeneration ) const;
259 struct PDFObject final : public PDFContainer
261 PDFEntry* m_pObject;
262 PDFStream* m_pStream;
263 unsigned int m_nNumber;
264 unsigned int m_nGeneration;
266 PDFObject( unsigned int nNr, unsigned int nGen )
267 : 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;
272 // writes only the contained stream, deflated if necessary
273 void writeStream( EmitContext& rContext, const PDFFile* pPDFFile ) const;
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;
283 struct PDFPart final : public PDFContainer
285 PDFPart() : PDFContainer() {}
286 virtual ~PDFPart() override;
287 virtual bool emit( EmitContext& rWriteContext ) const override;
288 virtual PDFEntry* clone() const override;
291 struct PDFReader
293 PDFReader() = delete;
295 static std::unique_ptr<PDFEntry> read(std::u16string_view aFileName);
298 } // namespace
300 #endif
302 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */