LCOV - code coverage report
Current view: top level - sdext/source/pdfimport/inc - pdfparse.hxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 28 31 90.3 %
Date: 2015-06-13 12:38:46 Functions: 18 20 90.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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 <unordered_map>
      28             : #include <vector>
      29             : 
      30             : namespace pdfparse
      31             : {
      32             : 
      33             : struct EmitImplData;
      34             : struct PDFContainer;
      35             : class EmitContext
      36             : {
      37             : public:
      38             :     virtual bool write( const void* pBuf, unsigned int nLen ) = 0;
      39             :     virtual unsigned int getCurPos() = 0;
      40             :     virtual bool copyOrigBytes( unsigned int nOrigOffset, unsigned int nLen ) = 0;
      41             :     virtual unsigned int readOrigBytes( unsigned int nOrigOffset, unsigned int nLen, void* pBuf ) = 0;
      42             : 
      43             :     EmitContext( const PDFContainer* pTop = NULL );
      44             :     virtual ~EmitContext();
      45             : 
      46             :     // set this to deflate contained streams
      47             :     bool m_bDeflate;
      48             :     // set this to decrypt the PDF file
      49             :     bool m_bDecrypt;
      50             : 
      51             : private:
      52             :     friend struct PDFEntry;
      53             :     EmitImplData* m_pImplData;
      54             : };
      55             : 
      56             : struct PDFEntry
      57             : {
      58        1590 :     PDFEntry() {}
      59             :     virtual ~PDFEntry();
      60             : 
      61             :     virtual bool emit( EmitContext& rWriteContext ) const = 0;
      62             :     virtual PDFEntry* clone() const = 0;
      63             : 
      64             : protected:
      65             :     static EmitImplData* getEmitData( EmitContext& rContext );
      66             :     static void setEmitData( EmitContext& rContext, EmitImplData* pNewEmitData );
      67             : };
      68             : 
      69             : struct PDFComment : public PDFEntry
      70             : {
      71             :     OString  m_aComment;
      72             : 
      73           5 :     PDFComment( const OString& rComment )
      74           5 :     : PDFEntry(), m_aComment( rComment ) {}
      75             :     virtual ~PDFComment();
      76             :     virtual bool emit( EmitContext& rWriteContext ) const SAL_OVERRIDE;
      77             :     virtual PDFEntry* clone() const SAL_OVERRIDE;
      78             : };
      79             : 
      80             : struct PDFValue : public PDFEntry
      81             : {
      82             :     // abstract base class for simple values
      83        1264 :     PDFValue() : PDFEntry() {}
      84             :     virtual ~PDFValue();
      85             : };
      86             : 
      87             : struct PDFName : public PDFValue
      88             : {
      89             :     OString  m_aName;
      90             : 
      91         690 :     PDFName( const OString& rName )
      92         690 :     : PDFValue(), m_aName( rName ) {}
      93             :     virtual ~PDFName();
      94             :     virtual bool emit( EmitContext& rWriteContext ) const SAL_OVERRIDE;
      95             :     virtual PDFEntry* clone() const SAL_OVERRIDE;
      96             : 
      97             :     OUString getFilteredName() const;
      98             : };
      99             : 
     100             : struct PDFString : public PDFValue
     101             : {
     102             :     OString  m_aString;
     103             : 
     104          30 :     PDFString( const OString& rString )
     105          30 :     : PDFValue(), m_aString( rString ) {}
     106             :     virtual ~PDFString();
     107             :     virtual bool emit( EmitContext& rWriteContext ) const SAL_OVERRIDE;
     108             :     virtual PDFEntry* clone() const SAL_OVERRIDE;
     109             : 
     110             :     OString getFilteredString() const;
     111             : };
     112             : 
     113             : struct PDFNumber : public PDFValue
     114             : {
     115             :     double m_fValue;
     116             : 
     117         407 :     PDFNumber( double fVal )
     118         407 :     : PDFValue(), m_fValue( fVal ) {}
     119             :     virtual ~PDFNumber();
     120             :     virtual bool emit( EmitContext& rWriteContext ) const SAL_OVERRIDE;
     121             :     virtual PDFEntry* clone() const SAL_OVERRIDE;
     122             : };
     123             : 
     124             : struct PDFBool : public PDFValue
     125             : {
     126             :     bool m_bValue;
     127             : 
     128           5 :     PDFBool( bool bVal )
     129           5 :     : PDFValue(), m_bValue( bVal ) {}
     130             :     virtual ~PDFBool();
     131             :     virtual bool emit( EmitContext& rWriteContext ) const SAL_OVERRIDE;
     132             :     virtual PDFEntry* clone() const SAL_OVERRIDE;
     133             : };
     134             : 
     135             : struct PDFObjectRef : public PDFValue
     136             : {
     137             :     unsigned int    m_nNumber;
     138             :     unsigned int    m_nGeneration;
     139             : 
     140         122 :     PDFObjectRef( unsigned int nNr, unsigned int nGen )
     141         122 :     : PDFValue(), m_nNumber( nNr ), m_nGeneration( nGen ) {}
     142             :     virtual ~PDFObjectRef();
     143             :     virtual bool emit( EmitContext& rWriteContext ) const SAL_OVERRIDE;
     144             :     virtual PDFEntry* clone() const SAL_OVERRIDE;
     145             : };
     146             : 
     147             : struct PDFNull : public PDFValue
     148             : {
     149          10 :     PDFNull() {}
     150             :     virtual ~PDFNull();
     151             :     virtual bool emit( EmitContext& rWriteContext ) const SAL_OVERRIDE;
     152             :     virtual PDFEntry* clone() const SAL_OVERRIDE;
     153             : };
     154             : 
     155             : struct PDFObject;
     156             : struct PDFContainer : public PDFEntry
     157             : {
     158             :     sal_Int32              m_nOffset;
     159             :     std::vector<PDFEntry*> m_aSubElements;
     160             : 
     161             :     // this is an abstract base class for identifying
     162             :     // entries that can contain sub elements besides comments
     163         287 :     PDFContainer() : PDFEntry(), m_nOffset( 0 ) {}
     164             :     virtual ~PDFContainer();
     165             :     bool emitSubElements( EmitContext& rWriteContext ) const;
     166             :     void cloneSubElements( std::vector<PDFEntry*>& rNewSubElements ) const;
     167             : 
     168             :     PDFObject* findObject( unsigned int nNumber, unsigned int nGeneration ) const;
     169           0 :     PDFObject* findObject( PDFObjectRef* pRef ) const
     170           0 :     { return findObject( pRef->m_nNumber, pRef->m_nGeneration ); }
     171             : };
     172             : 
     173             : struct PDFArray : public PDFContainer
     174             : {
     175          63 :     PDFArray() {}
     176             :     virtual ~PDFArray();
     177             :     virtual bool emit( EmitContext& rWriteContext ) const SAL_OVERRIDE;
     178             :     virtual PDFEntry* clone() const SAL_OVERRIDE;
     179             : };
     180             : 
     181             : struct PDFDict : public PDFContainer
     182             : {
     183             :     typedef std::unordered_map<OString,PDFEntry*,OStringHash> Map;
     184             :     Map m_aMap;
     185             : 
     186         107 :     PDFDict() {}
     187             :     virtual ~PDFDict();
     188             :     virtual bool emit( EmitContext& rWriteContext ) const SAL_OVERRIDE;
     189             :     virtual PDFEntry* clone() const SAL_OVERRIDE;
     190             : 
     191             :     // inserting a value of NULL will remove rName and the previous value
     192             :     // from the dictionary
     193             :     void insertValue( const OString& rName, PDFEntry* pValue );
     194             :     // removes a name/value pair from the dict
     195             :     void eraseValue( const OString& rName );
     196             :     // builds new map as of sub elements
     197             :     // returns NULL if successful, else the first offending element
     198             :     PDFEntry* buildMap();
     199             : };
     200             : 
     201             : struct PDFStream : public PDFEntry
     202             : {
     203             :     unsigned int    m_nBeginOffset;
     204             :     unsigned int    m_nEndOffset; // offset of the byte after the stream
     205             :     PDFDict*        m_pDict;
     206             : 
     207          34 :     PDFStream( unsigned int nBegin, unsigned int nEnd, PDFDict* pStreamDict )
     208          34 :     : PDFEntry(), m_nBeginOffset( nBegin ), m_nEndOffset( nEnd ), m_pDict( pStreamDict ) {}
     209             :     virtual ~PDFStream();
     210             :     virtual bool emit( EmitContext& rWriteContext ) const SAL_OVERRIDE;
     211             :     virtual PDFEntry* clone() const SAL_OVERRIDE;
     212             : 
     213             :     unsigned int getDictLength( const PDFContainer* pObjectContainer = NULL ) const; // get contents of the "Length" entry of the dict
     214             : };
     215             : 
     216             : struct PDFTrailer : public PDFContainer
     217             : {
     218             :     PDFDict*        m_pDict;
     219             : 
     220           5 :     PDFTrailer() : PDFContainer(), m_pDict( NULL ) {}
     221             :     virtual ~PDFTrailer();
     222             :     virtual bool emit( EmitContext& rWriteContext ) const SAL_OVERRIDE;
     223             :     virtual PDFEntry* clone() const SAL_OVERRIDE;
     224             : };
     225             : 
     226             : struct PDFFileImplData;
     227             : struct PDFFile : public PDFContainer
     228             : {
     229             : private:
     230             :     mutable PDFFileImplData*    m_pData;
     231             :     PDFFileImplData*            impl_getData() const;
     232             : public:
     233             :     unsigned int        m_nMajor;           // PDF major
     234             :     unsigned int        m_nMinor;           // PDF minor
     235             : 
     236           5 :     PDFFile()
     237             :     : PDFContainer(),
     238             :       m_pData( NULL ),
     239           5 :       m_nMajor( 0 ), m_nMinor( 0 )
     240           5 :     {}
     241             :     virtual ~PDFFile();
     242             : 
     243             :     virtual bool emit( EmitContext& rWriteContext ) const SAL_OVERRIDE;
     244             :     virtual PDFEntry* clone() const SAL_OVERRIDE;
     245             : 
     246             :     bool isEncrypted() const;
     247             : 
     248             :     bool usesSupportedEncryptionFormat() const;
     249             : 
     250             :     // this method checks whether rPwd is compatible with
     251             :     // either user or owner password and sets up decrypt data in that case
     252             :     // returns true if decryption can be done
     253             :     bool setupDecryptionData( const OString& rPwd ) const;
     254             : 
     255             :     bool decrypt( const sal_uInt8* pInBuffer, sal_uInt32 nLen,
     256             :                   sal_uInt8* pOutBuffer,
     257             :                   unsigned int nObject, unsigned int nGeneration ) const;
     258             : 
     259             :     OUString getDecryptionKey() const;
     260             : };
     261             : 
     262             : struct PDFObject : public PDFContainer
     263             : {
     264             :     PDFEntry*       m_pObject;
     265             :     PDFStream*      m_pStream;
     266             :     unsigned int    m_nNumber;
     267             :     unsigned int    m_nGeneration;
     268             : 
     269         107 :     PDFObject( unsigned int nNr, unsigned int nGen )
     270         107 :     : m_pObject( NULL ), m_pStream( NULL ), m_nNumber( nNr ), m_nGeneration( nGen ) {}
     271             :     virtual ~PDFObject();
     272             :     virtual bool emit( EmitContext& rWriteContext ) const SAL_OVERRIDE;
     273             :     virtual PDFEntry* clone() const SAL_OVERRIDE;
     274             : 
     275             :     // writes only the contained stream, deflated if necessary
     276             :     bool writeStream( EmitContext& rContext, const PDFFile* pPDFFile ) const;
     277             : 
     278             : private:
     279             :     // returns true if stream is deflated
     280             :     // fills *ppStream and *pBytes with start of stream and count of bytes
     281             :     // memory returned in *ppStream must be freed with rtl_freeMemory afterwards
     282             :     // fills in NULL and 0 in case of error
     283             :     bool getDeflatedStream( char** ppStream, unsigned int* pBytes, const PDFContainer* pObjectContainer, EmitContext& rContext ) const;
     284             : };
     285             : 
     286             : struct PDFPart : public PDFContainer
     287             : {
     288           0 :     PDFPart() : PDFContainer() {}
     289             :     virtual ~PDFPart();
     290             :     virtual bool emit( EmitContext& rWriteContext ) const SAL_OVERRIDE;
     291             :     virtual PDFEntry* clone() const SAL_OVERRIDE;
     292             : };
     293             : 
     294             : class PDFReader
     295             : {
     296             : public:
     297          89 :     PDFReader() {}
     298          89 :     ~PDFReader() {}
     299             : 
     300             :     static PDFEntry* read( const char* pFileName );
     301             : #ifdef WIN32
     302             :     static PDFEntry* read( const char* pBuffer, unsigned int nLen );
     303             : #endif
     304             : };
     305             : 
     306             : } // namespace
     307             : 
     308             : #endif
     309             : 
     310             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11