Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : #ifndef INCLUDED_PDFI_PDFPARSE_HXX
30 : : #define INCLUDED_PDFI_PDFPARSE_HXX
31 : :
32 : : #include <sal/types.h>
33 : : #include <rtl/ustring.hxx>
34 : : #include <rtl/string.hxx>
35 : :
36 : : #include <vector>
37 : : #include <boost/unordered_map.hpp>
38 : :
39 : : namespace pdfparse
40 : : {
41 : :
42 : : struct EmitImplData;
43 : : struct PDFContainer;
44 : : class EmitContext
45 : : {
46 : : public:
47 : : virtual bool write( const void* pBuf, unsigned int nLen ) = 0;
48 : : virtual unsigned int getCurPos() = 0;
49 : : virtual bool copyOrigBytes( unsigned int nOrigOffset, unsigned int nLen ) = 0;
50 : : virtual unsigned int readOrigBytes( unsigned int nOrigOffset, unsigned int nLen, void* pBuf ) = 0;
51 : :
52 : : EmitContext( const PDFContainer* pTop = NULL );
53 : : virtual ~EmitContext();
54 : :
55 : : // set this to deflate contained streams
56 : : bool m_bDeflate;
57 : : // set this to decrypt the PDF file
58 : : bool m_bDecrypt;
59 : :
60 : : private:
61 : : friend struct PDFEntry;
62 : : EmitImplData* m_pImplData;
63 : : };
64 : :
65 : : struct PDFEntry
66 : : {
67 : 5580 : PDFEntry() {}
68 : : virtual ~PDFEntry();
69 : :
70 : : virtual bool emit( EmitContext& rWriteContext ) const = 0;
71 : : virtual PDFEntry* clone() const = 0;
72 : :
73 : : protected:
74 : : EmitImplData* getEmitData( EmitContext& rContext ) const;
75 : : void setEmitData( EmitContext& rContext, EmitImplData* pNewEmitData ) const;
76 : : };
77 : :
78 : : struct PDFComment : public PDFEntry
79 : : {
80 : : rtl::OString m_aComment;
81 : :
82 : 15 : PDFComment( const rtl::OString& rComment )
83 : 15 : : PDFEntry(), m_aComment( rComment ) {}
84 : : virtual ~PDFComment();
85 : : virtual bool emit( EmitContext& rWriteContext ) const;
86 : : virtual PDFEntry* clone() const;
87 : : };
88 : :
89 : : struct PDFValue : public PDFEntry
90 : : {
91 : : // abstract base class for simple values
92 : 4440 : PDFValue() : PDFEntry() {}
93 : : virtual ~PDFValue();
94 : : };
95 : :
96 : : struct PDFName : public PDFValue
97 : : {
98 : : rtl::OString m_aName;
99 : :
100 : 2430 : PDFName( const rtl::OString& rName )
101 : 2430 : : PDFValue(), m_aName( rName ) {}
102 : : virtual ~PDFName();
103 : : virtual bool emit( EmitContext& rWriteContext ) const;
104 : : virtual PDFEntry* clone() const;
105 : :
106 : : rtl::OUString getFilteredName() const;
107 : : };
108 : :
109 : : struct PDFString : public PDFValue
110 : : {
111 : : rtl::OString m_aString;
112 : :
113 : 90 : PDFString( const rtl::OString& rString )
114 : 90 : : PDFValue(), m_aString( rString ) {}
115 : : virtual ~PDFString();
116 : : virtual bool emit( EmitContext& rWriteContext ) const;
117 : : virtual PDFEntry* clone() const;
118 : :
119 : : rtl::OString getFilteredString() const;
120 : : };
121 : :
122 : : struct PDFNumber : public PDFValue
123 : : {
124 : : double m_fValue;
125 : :
126 : 1455 : PDFNumber( double fVal )
127 : 1455 : : PDFValue(), m_fValue( fVal ) {}
128 : : virtual ~PDFNumber();
129 : : virtual bool emit( EmitContext& rWriteContext ) const;
130 : : virtual PDFEntry* clone() const;
131 : : };
132 : :
133 : : struct PDFBool : public PDFValue
134 : : {
135 : : bool m_bValue;
136 : :
137 : 15 : PDFBool( bool bVal )
138 : 15 : : PDFValue(), m_bValue( bVal ) {}
139 : : virtual ~PDFBool();
140 : : virtual bool emit( EmitContext& rWriteContext ) const;
141 : : virtual PDFEntry* clone() const;
142 : : };
143 : :
144 : : struct PDFObjectRef : public PDFValue
145 : : {
146 : : unsigned int m_nNumber;
147 : : unsigned int m_nGeneration;
148 : :
149 : 420 : PDFObjectRef( unsigned int nNr, unsigned int nGen )
150 : 420 : : PDFValue(), m_nNumber( nNr ), m_nGeneration( nGen ) {}
151 : : virtual ~PDFObjectRef();
152 : : virtual bool emit( EmitContext& rWriteContext ) const;
153 : : virtual PDFEntry* clone() const;
154 : : };
155 : :
156 : : struct PDFNull : public PDFValue
157 : : {
158 : 30 : PDFNull() {}
159 : : virtual ~PDFNull();
160 : : virtual bool emit( EmitContext& rWriteContext ) const;
161 : : virtual PDFEntry* clone() const;
162 : : };
163 : :
164 : : struct PDFObject;
165 : : struct PDFContainer : public PDFEntry
166 : : {
167 : : sal_Int32 m_nOffset;
168 : : std::vector<PDFEntry*> m_aSubElements;
169 : :
170 : : // this is an abstract base class for identifying
171 : : // entries that can contain sub elements besides comments
172 [ + - ]: 1005 : PDFContainer() : PDFEntry(), m_nOffset( 0 ) {}
173 : : virtual ~PDFContainer();
174 : : virtual bool emitSubElements( EmitContext& rWriteContext ) const;
175 : : virtual void cloneSubElements( std::vector<PDFEntry*>& rNewSubElements ) const;
176 : :
177 : : PDFObject* findObject( unsigned int nNumber, unsigned int nGeneration ) const;
178 : 0 : PDFObject* findObject( PDFObjectRef* pRef ) const
179 : 0 : { return findObject( pRef->m_nNumber, pRef->m_nGeneration ); }
180 : : };
181 : :
182 : : struct PDFArray : public PDFContainer
183 : : {
184 : 225 : PDFArray() {}
185 : : virtual ~PDFArray();
186 : : virtual bool emit( EmitContext& rWriteContext ) const;
187 : : virtual PDFEntry* clone() const;
188 : : };
189 : :
190 : : struct PDFDict : public PDFContainer
191 : : {
192 : : typedef boost::unordered_map<rtl::OString,PDFEntry*,rtl::OStringHash> Map;
193 : : Map m_aMap;
194 : :
195 [ + - ]: 375 : PDFDict() {}
196 : : virtual ~PDFDict();
197 : : virtual bool emit( EmitContext& rWriteContext ) const;
198 : : virtual PDFEntry* clone() const;
199 : :
200 : : // inserting a value of NULL will remove rName and the previous value
201 : : // from the dictionary
202 : : void insertValue( const rtl::OString& rName, PDFEntry* pValue );
203 : : // removes a name/value pair from the dict
204 : : void eraseValue( const rtl::OString& rName );
205 : : // builds new map as of sub elements
206 : : // returns NULL if successfull, else the first offending element
207 : : PDFEntry* buildMap();
208 : : };
209 : :
210 : : struct PDFStream : public PDFEntry
211 : : {
212 : : unsigned int m_nBeginOffset;
213 : : unsigned int m_nEndOffset; // offset of the byte after the stream
214 : : PDFDict* m_pDict;
215 : :
216 : 120 : PDFStream( unsigned int nBegin, unsigned int nEnd, PDFDict* pStreamDict )
217 : 120 : : PDFEntry(), m_nBeginOffset( nBegin ), m_nEndOffset( nEnd ), m_pDict( pStreamDict ) {}
218 : : virtual ~PDFStream();
219 : : virtual bool emit( EmitContext& rWriteContext ) const;
220 : : virtual PDFEntry* clone() const;
221 : :
222 : : unsigned int getDictLength( const PDFContainer* pObjectContainer = NULL ) const; // get contents of the "Length" entry of the dict
223 : : };
224 : :
225 : : struct PDFTrailer : public PDFContainer
226 : : {
227 : : PDFDict* m_pDict;
228 : :
229 : 15 : PDFTrailer() : PDFContainer(), m_pDict( NULL ) {}
230 : : virtual ~PDFTrailer();
231 : : virtual bool emit( EmitContext& rWriteContext ) const;
232 : : virtual PDFEntry* clone() const;
233 : : };
234 : :
235 : : struct PDFFileImplData;
236 : : struct PDFFile : public PDFContainer
237 : : {
238 : : private:
239 : : mutable PDFFileImplData* m_pData;
240 : : PDFFileImplData* impl_getData() const;
241 : : public:
242 : : unsigned int m_nMajor; // PDF major
243 : : unsigned int m_nMinor; // PDF minor
244 : :
245 : 15 : PDFFile()
246 : : : PDFContainer(),
247 : : m_pData( NULL ),
248 : 15 : m_nMajor( 0 ), m_nMinor( 0 )
249 : 15 : {}
250 : : virtual ~PDFFile();
251 : :
252 : : virtual bool emit( EmitContext& rWriteContext ) const;
253 : : virtual PDFEntry* clone() const;
254 : :
255 : : bool isEncrypted() const;
256 : : // this method checks whether rPwd is compatible with
257 : : // either user or owner password and sets up decrypt data in that case
258 : : // returns true if decryption can be done
259 : : bool setupDecryptionData( const rtl::OString& rPwd ) const;
260 : :
261 : : bool decrypt( const sal_uInt8* pInBuffer, sal_uInt32 nLen,
262 : : sal_uInt8* pOutBuffer,
263 : : unsigned int nObject, unsigned int nGeneration ) const;
264 : :
265 : : rtl::OUString getDecryptionKey() const;
266 : : };
267 : :
268 : : struct PDFObject : public PDFContainer
269 : : {
270 : : PDFEntry* m_pObject;
271 : : PDFStream* m_pStream;
272 : : unsigned int m_nNumber;
273 : : unsigned int m_nGeneration;
274 : :
275 : 375 : PDFObject( unsigned int nNr, unsigned int nGen )
276 : 375 : : m_pObject( NULL ), m_pStream( NULL ), m_nNumber( nNr ), m_nGeneration( nGen ) {}
277 : : virtual ~PDFObject();
278 : : virtual bool emit( EmitContext& rWriteContext ) const;
279 : : virtual PDFEntry* clone() const;
280 : :
281 : : // writes only the contained stream, deflated if necessary
282 : : bool writeStream( EmitContext& rContext, const PDFFile* pPDFFile ) const;
283 : :
284 : : private:
285 : : // returns true if stream is deflated
286 : : // fills *ppStream and *pBytes with start of stream and count of bytes
287 : : // memory returned in *ppStream must be freed with rtl_freeMemory afterwards
288 : : // fills in NULL and 0 in case of error
289 : : bool getDeflatedStream( char** ppStream, unsigned int* pBytes, const PDFContainer* pObjectContainer, EmitContext& rContext ) const;
290 : : };
291 : :
292 : : struct PDFPart : public PDFContainer
293 : : {
294 : 0 : PDFPart() : PDFContainer() {}
295 : : virtual ~PDFPart();
296 : : virtual bool emit( EmitContext& rWriteContext ) const;
297 : : virtual PDFEntry* clone() const;
298 : : };
299 : :
300 : : class PDFReader
301 : : {
302 : : public:
303 : 19 : PDFReader() {}
304 : 19 : ~PDFReader() {}
305 : :
306 : : PDFEntry* read( const char* pFileName );
307 : : #ifdef WIN32
308 : : PDFEntry* read( const char* pBuffer, unsigned int nLen );
309 : : #endif
310 : : };
311 : :
312 : : } // namespace
313 : :
314 : : #endif
315 : :
316 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|