LCOV - code coverage report
Current view: top level - sdext/source/pdfimport - filterdet.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 0 277 0.0 %
Date: 2014-11-03 Functions: 0 12 0.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             : 
      21             : #include "filterdet.hxx"
      22             : #include "inc/pdfihelper.hxx"
      23             : #include "inc/pdfparse.hxx"
      24             : 
      25             : #include <osl/diagnose.h>
      26             : #include <osl/file.h>
      27             : #include <osl/thread.h>
      28             : #include <rtl/digest.h>
      29             : #include <rtl/ref.hxx>
      30             : #include <com/sun/star/uno/RuntimeException.hpp>
      31             : #include <com/sun/star/io/XInputStream.hpp>
      32             : #include <com/sun/star/io/XStream.hpp>
      33             : #include <com/sun/star/io/XSeekable.hpp>
      34             : #include <com/sun/star/io/TempFile.hpp>
      35             : 
      36             : #include <boost/scoped_ptr.hpp>
      37             : #include <string.h>
      38             : 
      39             : using namespace com::sun::star;
      40             : 
      41             : namespace pdfi
      42             : {
      43             : 
      44             : // TODO(T3): locking/thread safety
      45             : 
      46             : class FileEmitContext : public pdfparse::EmitContext
      47             : {
      48             : private:
      49             :     oslFileHandle                        m_aReadHandle;
      50             :     unsigned int                         m_nReadLen;
      51             :     uno::Reference< io::XStream >        m_xContextStream;
      52             :     uno::Reference< io::XSeekable >      m_xSeek;
      53             :     uno::Reference< io::XOutputStream >  m_xOut;
      54             : 
      55             : public:
      56             :     FileEmitContext( const OUString&                            rOrigFile,
      57             :                      const uno::Reference< uno::XComponentContext >& xContext,
      58             :                      const pdfparse::PDFContainer*                   pTop );
      59             :     virtual ~FileEmitContext();
      60             : 
      61             :     virtual bool         write( const void* pBuf, unsigned int nLen ) SAL_OVERRIDE;
      62             :     virtual unsigned int getCurPos() SAL_OVERRIDE;
      63             :     virtual bool         copyOrigBytes( unsigned int nOrigOffset, unsigned int nLen ) SAL_OVERRIDE;
      64             :     virtual unsigned int readOrigBytes( unsigned int nOrigOffset, unsigned int nLen, void* pBuf ) SAL_OVERRIDE;
      65             : 
      66           0 :     const uno::Reference< io::XStream >& getContextStream() const { return m_xContextStream; }
      67             : };
      68             : 
      69           0 : FileEmitContext::FileEmitContext( const OUString&                            rOrigFile,
      70             :                                   const uno::Reference< uno::XComponentContext >& xContext,
      71             :                                   const pdfparse::PDFContainer*                   pTop ) :
      72             :     pdfparse::EmitContext( pTop ),
      73             :     m_aReadHandle(NULL),
      74             :     m_nReadLen(0),
      75             :     m_xContextStream(),
      76             :     m_xSeek(),
      77           0 :     m_xOut()
      78             : {
      79           0 :     m_xContextStream = uno::Reference< io::XStream >(
      80           0 :         io::TempFile::create(xContext), uno::UNO_QUERY_THROW );
      81           0 :     m_xOut = m_xContextStream->getOutputStream();
      82           0 :     m_xSeek = uno::Reference<io::XSeekable>(m_xOut, uno::UNO_QUERY_THROW );
      83             : 
      84           0 :     oslFileError aErr = osl_File_E_None;
      85           0 :     if( (aErr=osl_openFile( rOrigFile.pData,
      86             :                             &m_aReadHandle,
      87           0 :                             osl_File_OpenFlag_Read )) == osl_File_E_None )
      88             :     {
      89           0 :         if( (aErr=osl_setFilePos( m_aReadHandle,
      90             :                                   osl_Pos_End,
      91           0 :                                   0 )) == osl_File_E_None )
      92             :         {
      93           0 :             sal_uInt64 nFileSize = 0;
      94           0 :             if( (aErr=osl_getFilePos( m_aReadHandle,
      95           0 :                                       &nFileSize )) == osl_File_E_None )
      96             :             {
      97           0 :                 m_nReadLen = static_cast<unsigned int>(nFileSize);
      98             :             }
      99             :         }
     100           0 :         if( aErr != osl_File_E_None )
     101             :         {
     102           0 :             osl_closeFile( m_aReadHandle );
     103           0 :             m_aReadHandle = NULL;
     104             :         }
     105             :     }
     106           0 :     m_bDeflate = true;
     107           0 : }
     108             : 
     109           0 : FileEmitContext::~FileEmitContext()
     110             : {
     111           0 :     if( m_aReadHandle )
     112           0 :         osl_closeFile( m_aReadHandle );
     113           0 : }
     114             : 
     115           0 : bool FileEmitContext::write( const void* pBuf, unsigned int nLen )
     116             : {
     117           0 :     if( ! m_xOut.is() )
     118           0 :         return false;
     119             : 
     120           0 :     uno::Sequence< sal_Int8 > aSeq( nLen );
     121           0 :     memcpy( aSeq.getArray(), pBuf, nLen );
     122           0 :     m_xOut->writeBytes( aSeq );
     123           0 :     return true;
     124             : }
     125             : 
     126           0 : unsigned int FileEmitContext::getCurPos()
     127             : {
     128           0 :     unsigned int nPos = 0;
     129           0 :     if( m_xSeek.is() )
     130             :     {
     131           0 :         nPos = static_cast<unsigned int>( m_xSeek->getPosition() );
     132             :     }
     133           0 :     return nPos;
     134             : }
     135             : 
     136           0 : bool FileEmitContext::copyOrigBytes( unsigned int nOrigOffset, unsigned int nLen )
     137             : {
     138           0 :     if( nOrigOffset + nLen > m_nReadLen )
     139           0 :         return false;
     140             : 
     141           0 :     if( osl_setFilePos( m_aReadHandle, osl_Pos_Absolut, nOrigOffset ) != osl_File_E_None )
     142           0 :         return false;
     143             : 
     144           0 :     uno::Sequence< sal_Int8 > aSeq( nLen );
     145             : 
     146           0 :     sal_uInt64 nBytesRead = 0;
     147           0 :     if( osl_readFile( m_aReadHandle,
     148           0 :                       aSeq.getArray(),
     149             :                       nLen,
     150           0 :                       &nBytesRead ) != osl_File_E_None
     151           0 :         || nBytesRead != static_cast<sal_uInt64>(nLen) )
     152             :     {
     153           0 :         return false;
     154             :     }
     155             : 
     156           0 :     m_xOut->writeBytes( aSeq );
     157           0 :     return true;
     158             : }
     159             : 
     160           0 : unsigned int FileEmitContext::readOrigBytes( unsigned int nOrigOffset, unsigned int nLen, void* pBuf )
     161             : {
     162           0 :     if( nOrigOffset + nLen > m_nReadLen )
     163           0 :         return 0;
     164             : 
     165           0 :     if( osl_setFilePos( m_aReadHandle,
     166             :                         osl_Pos_Absolut,
     167           0 :                         nOrigOffset ) != osl_File_E_None )
     168             :     {
     169           0 :         return 0;
     170             :     }
     171             : 
     172           0 :     sal_uInt64 nBytesRead = 0;
     173           0 :     if( osl_readFile( m_aReadHandle,
     174             :                       pBuf,
     175             :                       nLen,
     176           0 :                       &nBytesRead ) != osl_File_E_None )
     177             :     {
     178           0 :         return 0;
     179             :     }
     180           0 :     return static_cast<unsigned int>(nBytesRead);
     181             : }
     182             : 
     183             : 
     184             : 
     185             : 
     186             : 
     187           0 : PDFDetector::PDFDetector( const uno::Reference< uno::XComponentContext >& xContext) :
     188             :     PDFDetectorBase( m_aMutex ),
     189           0 :     m_xContext( xContext )
     190           0 : {}
     191             : 
     192             : // XExtendedFilterDetection
     193           0 : OUString SAL_CALL PDFDetector::detect( uno::Sequence< beans::PropertyValue >& rFilterData ) throw( uno::RuntimeException, std::exception )
     194             : {
     195           0 :     osl::MutexGuard const guard( m_aMutex );
     196           0 :     bool bSuccess = false;
     197             : 
     198             :     // get the InputStream carrying the PDF content
     199           0 :     uno::Reference< io::XInputStream > xInput;
     200           0 :     uno::Reference< io::XStream > xEmbedStream;
     201           0 :     OUString aOutFilterName, aOutTypeName;
     202           0 :     OUString aURL;
     203           0 :     OUString aPwd;
     204           0 :     const beans::PropertyValue* pAttribs = rFilterData.getConstArray();
     205           0 :     sal_Int32 nAttribs = rFilterData.getLength();
     206           0 :     sal_Int32 nFilterNamePos = -1;
     207           0 :     sal_Int32 nPwdPos = -1;
     208           0 :     for( sal_Int32 i = 0; i < nAttribs; i++ )
     209             :     {
     210             : #if OSL_DEBUG_LEVEL > 1
     211             :         OUString aVal( "<no string>" );
     212             :         pAttribs[i].Value >>= aVal;
     213             :         OSL_TRACE( "doDetection: Attrib: %s = %s\n",
     214             :                    OUStringToOString( pAttribs[i].Name, RTL_TEXTENCODING_UTF8 ).getStr(),
     215             :                    OUStringToOString( aVal, RTL_TEXTENCODING_UTF8 ).getStr() );
     216             : #endif
     217           0 :         if ( pAttribs[i].Name == "InputStream" )
     218           0 :             pAttribs[i].Value >>= xInput;
     219           0 :         else if ( pAttribs[i].Name == "URL" )
     220           0 :             pAttribs[i].Value >>= aURL;
     221           0 :         else if ( pAttribs[i].Name == "FilterName" )
     222           0 :             nFilterNamePos = i;
     223           0 :         else if ( pAttribs[i].Name == "Password" )
     224             :         {
     225           0 :             nPwdPos = i;
     226           0 :             pAttribs[i].Value >>= aPwd;
     227             :         }
     228             :     }
     229           0 :     if( xInput.is() )
     230             :     {
     231           0 :         uno::Reference< io::XSeekable > xSeek( xInput, uno::UNO_QUERY );
     232           0 :         if( xSeek.is() )
     233           0 :             xSeek->seek( 0 );
     234             :         // read the first 1024 byte (see PDF reference implementation note 12)
     235           0 :         const sal_Int32 nHeaderSize = 1024;
     236           0 :         uno::Sequence< sal_Int8 > aBuf( nHeaderSize );
     237           0 :         sal_uInt64 nBytes = 0;
     238           0 :         nBytes = xInput->readBytes( aBuf, nHeaderSize );
     239           0 :         if( nBytes > 5 )
     240             :         {
     241           0 :             const sal_Int8* pBytes = aBuf.getConstArray();
     242           0 :             for( unsigned int i = 0; i < nBytes-5; i++ )
     243             :             {
     244           0 :                 if( pBytes[i]   == '%' &&
     245           0 :                     pBytes[i+1] == 'P' &&
     246           0 :                     pBytes[i+2] == 'D' &&
     247           0 :                     pBytes[i+3] == 'F' &&
     248           0 :                     pBytes[i+4] == '-' )
     249             :                 {
     250           0 :                     bSuccess = true;
     251           0 :                     break;
     252             :                 }
     253             :             }
     254             :         }
     255             : 
     256             :         // check for hybrid PDF
     257           0 :         oslFileHandle aFile = NULL;
     258           0 :         if( bSuccess &&
     259           0 :             ( aURL.isEmpty() || !aURL.startsWith( "file:" ) )
     260             :         )
     261             :         {
     262           0 :             sal_uInt64 nWritten = 0;
     263           0 :             if( osl_createTempFile( NULL, &aFile, &aURL.pData ) != osl_File_E_None )
     264             :             {
     265           0 :                 bSuccess = false;
     266             :             }
     267             :             else
     268             :             {
     269             : #if OSL_DEBUG_LEVEL > 1
     270             :                 OSL_TRACE( "created temp file %s\n",
     271             :                            OUStringToOString( aURL, RTL_TEXTENCODING_UTF8 ).getStr() );
     272             : #endif
     273           0 :                 osl_writeFile( aFile, aBuf.getConstArray(), nBytes, &nWritten );
     274             : 
     275             :                 OSL_ENSURE( nWritten == nBytes, "writing of header bytes failed" );
     276             : 
     277           0 :                 if( nWritten == nBytes )
     278             :                 {
     279           0 :                     const sal_uInt32 nBufSize = 4096;
     280           0 :                     aBuf = uno::Sequence<sal_Int8>(nBufSize);
     281             :                     // copy the bytes
     282           0 :                     do
     283             :                     {
     284           0 :                         nBytes = xInput->readBytes( aBuf, nBufSize );
     285           0 :                         if( nBytes > 0 )
     286             :                         {
     287           0 :                             osl_writeFile( aFile, aBuf.getConstArray(), nBytes, &nWritten );
     288           0 :                             if( nWritten != nBytes )
     289             :                             {
     290           0 :                                 bSuccess = false;
     291           0 :                                 break;
     292             :                             }
     293             :                         }
     294             :                     } while( nBytes == nBufSize );
     295             :                 }
     296             :             }
     297           0 :             osl_closeFile( aFile );
     298             :         }
     299           0 :         OUString aEmbedMimetype;
     300           0 :         xEmbedStream = getAdditionalStream( aURL, aEmbedMimetype, aPwd, m_xContext, rFilterData, false );
     301           0 :         if( aFile )
     302           0 :             osl_removeFile( aURL.pData );
     303           0 :         if( !aEmbedMimetype.isEmpty() )
     304             :         {
     305           0 :             if( aEmbedMimetype == "application/vnd.oasis.opendocument.text"
     306           0 :                 || aEmbedMimetype == "application/vnd.oasis.opendocument.text-master" )
     307           0 :                 aOutFilterName = "writer_pdf_addstream_import";
     308           0 :             else if ( aEmbedMimetype == "application/vnd.oasis.opendocument.presentation" )
     309           0 :                 aOutFilterName = "impress_pdf_addstream_import";
     310           0 :             else if( aEmbedMimetype == "application/vnd.oasis.opendocument.graphics"
     311           0 :                      || aEmbedMimetype == "application/vnd.oasis.opendocument.drawing" )
     312           0 :                 aOutFilterName = "draw_pdf_addstream_import";
     313           0 :             else if ( aEmbedMimetype == "application/vnd.oasis.opendocument.spreadsheet" )
     314           0 :                 aOutFilterName = "calc_pdf_addstream_import";
     315           0 :         }
     316             :     }
     317             : 
     318           0 :     if( bSuccess )
     319             :     {
     320           0 :         if( !aOutFilterName.isEmpty() )
     321             :         {
     322           0 :             if( nFilterNamePos == -1 )
     323             :             {
     324           0 :                 nFilterNamePos = nAttribs;
     325           0 :                 rFilterData.realloc( ++nAttribs );
     326           0 :                 rFilterData[ nFilterNamePos ].Name =
     327           0 :                     OUString( "FilterName" );
     328             :             }
     329           0 :             aOutTypeName = "pdf_Portable_Document_Format";
     330             : 
     331             :             OSL_TRACE( "setting filter name %s, input stream %s\n",
     332             :                        OUStringToOString( aOutFilterName, RTL_TEXTENCODING_UTF8 ).getStr(),
     333             :                        xEmbedStream.is() ? "present" : "not present" );
     334             : 
     335           0 :             rFilterData[nFilterNamePos].Value <<= aOutFilterName;
     336           0 :             if( xEmbedStream.is() )
     337             :             {
     338           0 :                 rFilterData.realloc( ++nAttribs );
     339           0 :                 rFilterData[nAttribs-1].Name = "EmbeddedSubstream";
     340           0 :                 rFilterData[nAttribs-1].Value <<= xEmbedStream;
     341             :             }
     342           0 :             if( !aPwd.isEmpty() )
     343             :             {
     344           0 :                 if( nPwdPos == -1 )
     345             :                 {
     346           0 :                     nPwdPos = nAttribs;
     347           0 :                     rFilterData.realloc( ++nAttribs );
     348           0 :                     rFilterData[ nPwdPos ].Name = "Password";
     349             :                 }
     350           0 :                 rFilterData[ nPwdPos ].Value <<= aPwd;
     351             :             }
     352             :         }
     353             :         else
     354             :         {
     355           0 :             if( nFilterNamePos == -1 )
     356             :             {
     357           0 :                 nFilterNamePos = nAttribs;
     358           0 :                 rFilterData.realloc( ++nAttribs );
     359           0 :                 rFilterData[ nFilterNamePos ].Name = "FilterName";
     360             :             }
     361             : 
     362           0 :             const sal_Int32 nDocumentType = 0; //const sal_Int32 nDocumentType = queryDocumentTypeDialog(m_xContext,aURL);
     363             :             if( nDocumentType < 0 )
     364             :             {
     365             :                 return OUString();
     366             :             }
     367             :             else switch( nDocumentType )
     368             :             {
     369             :                 case 0:
     370           0 :                     rFilterData[nFilterNamePos].Value <<= OUString( "draw_pdf_import" );
     371           0 :                     break;
     372             : 
     373             :                 case 1:
     374             :                     rFilterData[nFilterNamePos].Value <<= OUString( "impress_pdf_import" );
     375             :                     break;
     376             : 
     377             :                 case 2:
     378             :                     rFilterData[nFilterNamePos].Value <<= OUString( "writer_pdf_import" );
     379             :                     break;
     380             : 
     381             :                 default:
     382             :                     OSL_FAIL("Unexpected case");
     383             :             }
     384             : 
     385           0 :             aOutTypeName = "pdf_Portable_Document_Format";
     386             :         }
     387             :     }
     388             : 
     389           0 :     return aOutTypeName;
     390             : }
     391             : 
     392           0 : bool checkDocChecksum( const OUString& rInPDFFileURL,
     393             :                        sal_uInt32           nBytes,
     394             :                        const OUString& rChkSum )
     395             : {
     396           0 :     bool bRet = false;
     397           0 :     if( rChkSum.getLength() != 2* RTL_DIGEST_LENGTH_MD5 )
     398             :     {
     399             :         OSL_TRACE( "checksum of length %d, expected %d\n",
     400             :                    rChkSum.getLength(), 2*RTL_DIGEST_LENGTH_MD5 );
     401           0 :         return false;
     402             :     }
     403             : 
     404             :     // prepare checksum to test
     405             :     sal_uInt8 nTestChecksum[ RTL_DIGEST_LENGTH_MD5 ];
     406           0 :     const sal_Unicode* pChar = rChkSum.getStr();
     407           0 :     for( unsigned int i = 0; i < RTL_DIGEST_LENGTH_MD5; i++ )
     408             :     {
     409           0 :         sal_uInt8 nByte = sal_uInt8( ( (*pChar >= '0' && *pChar <= '9') ? *pChar - '0' :
     410           0 :                           ( (*pChar >= 'A' && *pChar <= 'F') ? *pChar - 'A' + 10 :
     411           0 :                           ( (*pChar >= 'a' && *pChar <= 'f') ? *pChar - 'a' + 10 :
     412           0 :                           0 ) ) ) );
     413           0 :         nByte <<= 4;
     414           0 :         pChar++;
     415           0 :         nByte |= ( (*pChar >= '0' && *pChar <= '9') ? *pChar - '0' :
     416           0 :                  ( (*pChar >= 'A' && *pChar <= 'F') ? *pChar - 'A' + 10 :
     417           0 :                  ( (*pChar >= 'a' && *pChar <= 'f') ? *pChar - 'a' + 10 :
     418           0 :                  0 ) ) );
     419           0 :         pChar++;
     420           0 :         nTestChecksum[i] = nByte;
     421             :     }
     422             : 
     423             :     // open file and calculate actual checksum up to index nBytes
     424             :     sal_uInt8 nActualChecksum[ RTL_DIGEST_LENGTH_MD5 ];
     425           0 :     memset( nActualChecksum, 0, sizeof(nActualChecksum) );
     426           0 :     rtlDigest aActualDigest = rtl_digest_createMD5();
     427           0 :     oslFileHandle aRead = NULL;
     428           0 :     oslFileError aErr = osl_File_E_None;
     429           0 :     if( (aErr = osl_openFile(rInPDFFileURL.pData,
     430             :                              &aRead,
     431           0 :                              osl_File_OpenFlag_Read )) == osl_File_E_None )
     432             :     {
     433             :         sal_Int8 aBuf[4096];
     434           0 :         sal_uInt32 nCur = 0;
     435           0 :         sal_uInt64 nBytesRead = 0;
     436           0 :         while( nCur < nBytes )
     437             :         {
     438           0 :             sal_uInt32 nPass = (nBytes - nCur) > sizeof( aBuf ) ? sizeof( aBuf ) : nBytes - nCur;
     439           0 :             if( (aErr = osl_readFile( aRead, aBuf, nPass, &nBytesRead)) != osl_File_E_None
     440           0 :                 || nBytesRead == 0 )
     441             :             {
     442           0 :                 break;
     443             :             }
     444           0 :             nPass = static_cast<sal_uInt32>(nBytesRead);
     445           0 :             nCur += nPass;
     446           0 :             rtl_digest_updateMD5( aActualDigest, aBuf, nPass );
     447             :         }
     448           0 :         rtl_digest_getMD5( aActualDigest, nActualChecksum, sizeof(nActualChecksum) );
     449           0 :         osl_closeFile( aRead );
     450             :     }
     451           0 :     rtl_digest_destroyMD5( aActualDigest );
     452             : 
     453             :     // compare the contents
     454           0 :     bRet = (0 == memcmp( nActualChecksum, nTestChecksum, sizeof( nActualChecksum ) ));
     455             : #if OSL_DEBUG_LEVEL > 1
     456             :     OSL_TRACE( "test checksum: " );
     457             :     for( unsigned int i = 0; i < sizeof(nTestChecksum); i++ )
     458             :         OSL_TRACE( "%.2X", int(nTestChecksum[i]) );
     459             :     OSL_TRACE( "\n" );
     460             :     OSL_TRACE( "file checksum: " );
     461             :     for( unsigned int i = 0; i < sizeof(nActualChecksum); i++ )
     462             :         OSL_TRACE( "%.2X", int(nActualChecksum[i]) );
     463             :     OSL_TRACE( "\n" );
     464             : #endif
     465           0 :     return bRet;
     466             : }
     467             : 
     468           0 : uno::Reference< io::XStream > getAdditionalStream( const OUString&                          rInPDFFileURL,
     469             :                                                    OUString&                                rOutMimetype,
     470             :                                                    OUString&                                io_rPwd,
     471             :                                                    const uno::Reference<uno::XComponentContext>& xContext,
     472             :                                                    const uno::Sequence<beans::PropertyValue>&    rFilterData,
     473             :                                                    bool                                          bMayUseUI )
     474             : {
     475           0 :     uno::Reference< io::XStream > xEmbed;
     476           0 :     OString aPDFFile;
     477           0 :     OUString aSysUPath;
     478           0 :     if( osl_getSystemPathFromFileURL( rInPDFFileURL.pData, &aSysUPath.pData ) != osl_File_E_None )
     479           0 :         return xEmbed;
     480           0 :     aPDFFile = OUStringToOString( aSysUPath, osl_getThreadTextEncoding() );
     481             : 
     482           0 :     pdfparse::PDFReader aParser;
     483           0 :     boost::scoped_ptr<pdfparse::PDFEntry> pEntry( aParser.read( aPDFFile.getStr() ));
     484           0 :     if( pEntry )
     485             :     {
     486           0 :         pdfparse::PDFFile* pPDFFile = dynamic_cast<pdfparse::PDFFile*>(pEntry.get());
     487           0 :         if( pPDFFile )
     488             :         {
     489           0 :             unsigned int nElements = pPDFFile->m_aSubElements.size();
     490           0 :             while( nElements-- > 0 )
     491             :             {
     492           0 :                 pdfparse::PDFTrailer* pTrailer = dynamic_cast<pdfparse::PDFTrailer*>(pPDFFile->m_aSubElements[nElements]);
     493           0 :                 if( pTrailer && pTrailer->m_pDict )
     494             :                 {
     495             :                     // search document checksum entry
     496             :                     boost::unordered_map< OString,
     497             :                                    pdfparse::PDFEntry*,
     498           0 :                                    OStringHash >::iterator chk;
     499           0 :                     chk = pTrailer->m_pDict->m_aMap.find( "DocChecksum" );
     500           0 :                     if( chk == pTrailer->m_pDict->m_aMap.end() )
     501             :                     {
     502             :                         OSL_TRACE( "no DocChecksum entry" );
     503           0 :                         continue;
     504             :                     }
     505           0 :                     pdfparse::PDFName* pChkSumName = dynamic_cast<pdfparse::PDFName*>(chk->second);
     506           0 :                     if( pChkSumName == NULL )
     507             :                     {
     508             :                         OSL_TRACE( "no name for DocChecksum entry" );
     509           0 :                         continue;
     510             :                     }
     511             : 
     512             :                     // search for AdditionalStreams entry
     513             :                     boost::unordered_map< OString,
     514             :                                    pdfparse::PDFEntry*,
     515           0 :                                    OStringHash >::iterator add_stream;
     516           0 :                     add_stream = pTrailer->m_pDict->m_aMap.find( "AdditionalStreams" );
     517           0 :                     if( add_stream == pTrailer->m_pDict->m_aMap.end() )
     518             :                     {
     519             :                         OSL_TRACE( "no AdditionalStreams entry" );
     520           0 :                         continue;
     521             :                     }
     522           0 :                     pdfparse::PDFArray* pStreams = dynamic_cast<pdfparse::PDFArray*>(add_stream->second);
     523           0 :                     if( ! pStreams || pStreams->m_aSubElements.size() < 2 )
     524             :                     {
     525             :                         OSL_TRACE( "AdditionalStreams array too small" );
     526           0 :                         continue;
     527             :                     }
     528             : 
     529             :                     // check checksum
     530           0 :                     OUString aChkSum = pChkSumName->getFilteredName();
     531           0 :                     if( ! checkDocChecksum( rInPDFFileURL, pTrailer->m_nOffset, aChkSum ) )
     532           0 :                         continue;
     533             : 
     534             :                     // extract addstream and mimetype
     535           0 :                     pdfparse::PDFName* pMimeType = dynamic_cast<pdfparse::PDFName*>(pStreams->m_aSubElements[0]);
     536           0 :                     pdfparse::PDFObjectRef* pStreamRef = dynamic_cast<pdfparse::PDFObjectRef*>(pStreams->m_aSubElements[1]);
     537             : 
     538             :                     OSL_ENSURE( pMimeType, "error: no mimetype element\n" );
     539             :                     OSL_ENSURE( pStreamRef, "error: no stream ref element\n" );
     540             : 
     541           0 :                     if( pMimeType && pStreamRef )
     542             :                     {
     543           0 :                         pdfparse::PDFObject* pObject = pPDFFile->findObject( pStreamRef->m_nNumber, pStreamRef->m_nGeneration );
     544             :                         OSL_ENSURE( pObject, "object not found\n" );
     545           0 :                         if( pObject )
     546             :                         {
     547           0 :                             if( pPDFFile->isEncrypted() )
     548             :                             {
     549           0 :                                 bool bAuthenticated = false;
     550           0 :                                 if( !io_rPwd.isEmpty() )
     551             :                                 {
     552             :                                     OString aIsoPwd = OUStringToOString( io_rPwd,
     553           0 :                                                                                    RTL_TEXTENCODING_ISO_8859_1 );
     554           0 :                                     bAuthenticated = pPDFFile->setupDecryptionData( aIsoPwd.getStr() );
     555             :                                 }
     556           0 :                                 if( ! bAuthenticated )
     557             :                                 {
     558           0 :                                     const beans::PropertyValue* pAttribs = rFilterData.getConstArray();
     559           0 :                                     sal_Int32 nAttribs = rFilterData.getLength();
     560           0 :                                     uno::Reference< task::XInteractionHandler > xIntHdl;
     561           0 :                                     for( sal_Int32 i = 0; i < nAttribs; i++ )
     562             :                                     {
     563           0 :                                         if ( pAttribs[i].Name == "InteractionHandler" )
     564           0 :                                             pAttribs[i].Value >>= xIntHdl;
     565             :                                     }
     566           0 :                                     if( ! bMayUseUI || ! xIntHdl.is() )
     567             :                                     {
     568           0 :                                         rOutMimetype = pMimeType->getFilteredName();
     569           0 :                                         xEmbed.clear();
     570           0 :                                         break;
     571             :                                     }
     572             : 
     573           0 :                                     OUString aDocName( rInPDFFileURL.copy( rInPDFFileURL.lastIndexOf( '/' )+1 ) );
     574             : 
     575           0 :                                     bool bEntered = false;
     576           0 :                                     do
     577             :                                     {
     578           0 :                                         bEntered = getPassword( xIntHdl, io_rPwd, ! bEntered, aDocName );
     579             :                                         OString aIsoPwd = OUStringToOString( io_rPwd,
     580           0 :                                                                                        RTL_TEXTENCODING_ISO_8859_1 );
     581           0 :                                         bAuthenticated = pPDFFile->setupDecryptionData( aIsoPwd.getStr() );
     582           0 :                                     } while( bEntered && ! bAuthenticated );
     583             :                                 }
     584             : 
     585             :                                 OSL_TRACE( "password: %s", bAuthenticated ? "matches" : "does not match" );
     586           0 :                                 if( ! bAuthenticated )
     587           0 :                                     continue;
     588             :                             }
     589           0 :                             rOutMimetype = pMimeType->getFilteredName();
     590             :                             FileEmitContext aContext( rInPDFFileURL,
     591             :                                                       xContext,
     592           0 :                                                       pPDFFile );
     593           0 :                             aContext.m_bDecrypt = pPDFFile->isEncrypted();
     594           0 :                             pObject->writeStream( aContext, pPDFFile );
     595           0 :                             xEmbed = aContext.getContextStream();
     596           0 :                             break; // success
     597             :                         }
     598           0 :                     }
     599             :                 }
     600             :             }
     601             :         }
     602             :     }
     603             : 
     604             :     OSL_TRACE( "extracted add stream: mimetype %s\n",
     605             :                OUStringToOString( rOutMimetype,
     606             :                                        RTL_TEXTENCODING_UTF8 ).getStr());
     607           0 :     return xEmbed;
     608             : }
     609             : 
     610             : }
     611             : 
     612             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10