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

Generated by: LCOV version 1.10