LCOV - code coverage report
Current view: top level - package/source/zippackage - zipfileaccess.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 83 215 38.6 %
Date: 2014-11-03 Functions: 11 21 52.4 %
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             : #include <com/sun/star/lang/DisposedException.hpp>
      21             : #include <com/sun/star/lang/IllegalArgumentException.hpp>
      22             : #include <com/sun/star/ucb/XCommandEnvironment.hpp>
      23             : #include <com/sun/star/io/XActiveDataSink.hpp>
      24             : #include <com/sun/star/io/XStream.hpp>
      25             : #include <com/sun/star/io/XSeekable.hpp>
      26             : #include <comphelper/processfactory.hxx>
      27             : #include <cppuhelper/supportsservice.hxx>
      28             : #include <zipfileaccess.hxx>
      29             : #include <ZipEnumeration.hxx>
      30             : #include <ZipPackageSink.hxx>
      31             : #include <EncryptionData.hxx>
      32             : 
      33             : #include <ucbhelper/content.hxx>
      34             : #include <rtl/ref.hxx>
      35             : 
      36             : using namespace ::com::sun::star;
      37             : 
      38             : #if OSL_DEBUG_LEVEL > 0
      39             : #define THROW_WHERE SAL_WHERE
      40             : #else
      41             : #define THROW_WHERE ""
      42             : #endif
      43             : 
      44        1182 : OZipFileAccess::OZipFileAccess( const uno::Reference< uno::XComponentContext >& rxContext )
      45        1182 : : m_aMutexHolder( new SotMutexHolder )
      46             : , m_xContext( rxContext )
      47             : , m_pZipFile( NULL )
      48             : , m_pListenersContainer( NULL )
      49             : , m_bDisposed( false )
      50        2364 : , m_bOwnContent( false )
      51             : {
      52        1182 :     if ( !rxContext.is() )
      53           0 :         throw uno::RuntimeException(THROW_WHERE );
      54        1182 : }
      55             : 
      56        3534 : OZipFileAccess::~OZipFileAccess()
      57             : {
      58             :     {
      59        1178 :         ::osl::MutexGuard aGuard( m_aMutexHolder->GetMutex() );
      60        1178 :         if ( !m_bDisposed )
      61             :         {
      62             :             try {
      63        1178 :                 m_refCount++; // dispose will use refcounting so the further destruction must be avoided
      64        1178 :                 dispose();
      65           0 :             } catch( uno::Exception& )
      66             :             {}
      67        1178 :         }
      68             :     }
      69        2356 : }
      70             : 
      71           0 : uno::Sequence< OUString > OZipFileAccess::GetPatternsFromString_Impl( const OUString& aString )
      72             : {
      73           0 :     if ( aString.isEmpty() )
      74           0 :         return uno::Sequence< OUString >();
      75             : 
      76           0 :     uno::Sequence< OUString > aPattern( 1 );
      77           0 :     sal_Int32 nInd = 0;
      78             : 
      79           0 :     const sal_Unicode* pString = aString.getStr();
      80           0 :     while( *pString )
      81             :     {
      82           0 :         if ( *pString == (sal_Unicode)'\\' )
      83             :         {
      84           0 :             pString++;
      85             : 
      86           0 :             if ( *pString == (sal_Unicode)'\\' )
      87             :             {
      88           0 :                 aPattern[nInd] += OUString( (sal_Unicode)'\\' );
      89           0 :                 pString++;
      90             :             }
      91           0 :             else if ( *pString == (sal_Unicode)'*' )
      92             :             {
      93           0 :                 aPattern[nInd] += OUString( (sal_Unicode)'*' );
      94           0 :                 pString++;
      95             :             }
      96             :             else
      97             :             {
      98             :                 OSL_FAIL( "The backslash is not guarded!\n" );
      99           0 :                 aPattern[nInd] += OUString( (sal_Unicode)'\\' );
     100             :             }
     101             :         }
     102           0 :         else if ( *pString == (sal_Unicode)'*' )
     103             :         {
     104           0 :             aPattern.realloc( ( ++nInd ) + 1 );
     105           0 :             pString++;
     106             :         }
     107             :         else
     108             :         {
     109           0 :             aPattern[nInd] += OUString( *pString );
     110           0 :             pString++;
     111             :         }
     112             :     }
     113             : 
     114           0 :     return aPattern;
     115             : }
     116             : 
     117           0 : bool OZipFileAccess::StringGoodForPattern_Impl( const OUString& aString,
     118             :                                                     const uno::Sequence< OUString >& aPattern )
     119             : {
     120           0 :     sal_Int32 nInd = aPattern.getLength() - 1;
     121           0 :     if ( nInd < 0 )
     122           0 :         return false;
     123             : 
     124           0 :     if ( nInd == 0 )
     125             :     {
     126           0 :         if ( aPattern[0].isEmpty() )
     127           0 :             return true;
     128             : 
     129           0 :         return aString.equals( aPattern[0] );
     130             :     }
     131             : 
     132           0 :     sal_Int32 nBeginInd = aPattern[0].getLength();
     133           0 :     sal_Int32 nEndInd = aString.getLength() - aPattern[nInd].getLength();
     134           0 :     if ( nEndInd >= nBeginInd
     135           0 :       && ( nEndInd == aString.getLength() || aString.copy( nEndInd ).equals( aPattern[nInd] ) )
     136           0 :       && ( nBeginInd == 0 || aString.copy( 0, nBeginInd ).equals( aPattern[0] ) ) )
     137             :     {
     138           0 :         for ( sal_Int32 nCurInd = aPattern.getLength() - 2; nCurInd > 0; nCurInd-- )
     139             :         {
     140           0 :             if ( aPattern[nCurInd].isEmpty() )
     141           0 :                 continue;
     142             : 
     143           0 :             if ( nEndInd == nBeginInd )
     144           0 :                 return false;
     145             : 
     146             :             // check that search does not use nEndInd position
     147           0 :             sal_Int32 nLastInd = aString.lastIndexOf( aPattern[nCurInd], nEndInd - 1 );
     148             : 
     149           0 :             if ( nLastInd == -1 )
     150           0 :                 return false;
     151             : 
     152           0 :             if ( nLastInd < nBeginInd )
     153           0 :                 return false;
     154             : 
     155           0 :             nEndInd = nLastInd;
     156             :         }
     157             : 
     158           0 :         return true;
     159             :     }
     160             : 
     161           0 :     return false;
     162             : }
     163             : 
     164             : // XInitialization
     165        1182 : void SAL_CALL OZipFileAccess::initialize( const uno::Sequence< uno::Any >& aArguments )
     166             :     throw ( uno::Exception,
     167             :             uno::RuntimeException, std::exception )
     168             : {
     169        1182 :     ::osl::MutexGuard aGuard( m_aMutexHolder->GetMutex() );
     170             : 
     171        1182 :     if ( m_bDisposed )
     172           0 :         throw lang::DisposedException(THROW_WHERE );
     173             : 
     174        1182 :     if ( m_pZipFile )
     175           0 :         throw uno::RuntimeException(THROW_WHERE ); // initialization is allowed only one time
     176             : 
     177        1182 :     if ( !aArguments.getLength() )
     178           0 :         throw lang::IllegalArgumentException(THROW_WHERE, uno::Reference< uno::XInterface >(), 1 );
     179             : 
     180             :     OSL_ENSURE( aArguments.getLength() == 1, "Too many arguments are provided, only the first one will be used!\n" );
     181             : 
     182        2364 :     OUString aParamURL;
     183        2364 :     uno::Reference< io::XStream > xStream;
     184        2364 :     uno::Reference< io::XSeekable > xSeekable;
     185             : 
     186        1182 :     if ( ( aArguments[0] >>= aParamURL ) )
     187             :     {
     188             :         ::ucbhelper::Content aContent(
     189             :             aParamURL,
     190             :             uno::Reference< ::com::sun::star::ucb::XCommandEnvironment >(),
     191         794 :             m_xContext );
     192        1588 :         uno::Reference < io::XActiveDataSink > xSink = new ZipPackageSink;
     193         794 :         if ( aContent.openStream ( xSink ) )
     194             :         {
     195         794 :             m_xContentStream = xSink->getInputStream();
     196         794 :             m_bOwnContent = true;
     197         794 :             xSeekable = uno::Reference< io::XSeekable >( m_xContentStream, uno::UNO_QUERY );
     198         794 :         }
     199             :     }
     200         388 :     else if ( (aArguments[0] >>= xStream ) )
     201             :     {
     202             :         // a writable stream can implement both XStream & XInputStream
     203           0 :         m_xContentStream = xStream->getInputStream();
     204           0 :         xSeekable = uno::Reference< io::XSeekable >( xStream, uno::UNO_QUERY );
     205             :     }
     206         388 :     else if ( aArguments[0] >>= m_xContentStream )
     207             :     {
     208         388 :         xSeekable = uno::Reference< io::XSeekable >( m_xContentStream, uno::UNO_QUERY );
     209             :     }
     210             :     else
     211           0 :         throw lang::IllegalArgumentException(THROW_WHERE, uno::Reference< uno::XInterface >(), 1 );
     212             : 
     213        1182 :     if ( !m_xContentStream.is() )
     214           0 :         throw io::IOException(THROW_WHERE );
     215             : 
     216        1182 :     if ( !xSeekable.is() )
     217             :     {
     218             :         // TODO: after fwkbugfix02 is integrated a helper class can be used to make the stream seekable
     219           0 :         throw io::IOException(THROW_WHERE );
     220             :     }
     221             : 
     222             :     // TODO: in case xSeekable is implemented on separated XStream implementation a wrapper is required
     223             :     m_pZipFile = new ZipFile(
     224             :                 m_xContentStream,
     225             :                 m_xContext,
     226        2726 :                 true );
     227         820 : }
     228             : 
     229             : // XNameAccess
     230       25241 : uno::Any SAL_CALL OZipFileAccess::getByName( const OUString& aName )
     231             :     throw ( container::NoSuchElementException,
     232             :             lang::WrappedTargetException,
     233             :             uno::RuntimeException, std::exception )
     234             : {
     235       25241 :     ::osl::MutexGuard aGuard( m_aMutexHolder->GetMutex() );
     236             : 
     237       25241 :     if ( m_bDisposed )
     238           0 :         throw lang::DisposedException(THROW_WHERE );
     239             : 
     240       25241 :     if ( !m_pZipFile )
     241           0 :         throw uno::RuntimeException(THROW_WHERE);
     242             : 
     243       25241 :     EntryHash::iterator aIter = m_pZipFile->GetEntryHash().find( aName );
     244       25241 :     if ( aIter == m_pZipFile->GetEntryHash().end() )
     245           0 :         throw container::NoSuchElementException(THROW_WHERE );
     246             : 
     247       50482 :     uno::Reference< io::XInputStream > xEntryStream;
     248             :     try
     249             :     {
     250       50482 :         xEntryStream  = m_pZipFile->getDataStream((*aIter).second,
     251             :                                                   ::rtl::Reference< EncryptionData >(),
     252             :                                                   false,
     253       25241 :                                                   m_aMutexHolder);
     254             :     }
     255           0 :     catch (const container::NoSuchElementException&)
     256             :     {
     257           0 :         throw;
     258             :     }
     259           0 :     catch (const lang::WrappedTargetException&)
     260             :     {
     261           0 :         throw;
     262             :     }
     263           0 :     catch (const uno::RuntimeException&)
     264             :     {
     265           0 :         throw;
     266             :     }
     267           0 :     catch (const uno::Exception& e)
     268             :     {
     269             :         throw lang::WrappedTargetException( "This package is unusable!",
     270             :                   static_cast < OWeakObject * > ( this ),
     271           0 :                                         makeAny(e));
     272             :     }
     273             : 
     274       25241 :     if ( !xEntryStream.is() )
     275           0 :         throw uno::RuntimeException(THROW_WHERE );
     276             : 
     277       50482 :     return uno::makeAny ( xEntryStream );
     278             : }
     279             : 
     280          84 : uno::Sequence< OUString > SAL_CALL OZipFileAccess::getElementNames()
     281             :     throw ( uno::RuntimeException, std::exception )
     282             : {
     283          84 :     ::osl::MutexGuard aGuard( m_aMutexHolder->GetMutex() );
     284             : 
     285          84 :     if ( m_bDisposed )
     286           0 :         throw lang::DisposedException(THROW_WHERE );
     287             : 
     288          84 :     if ( !m_pZipFile )
     289           0 :         throw uno::RuntimeException(THROW_WHERE);
     290             : 
     291          84 :     uno::Sequence< OUString > aNames( m_pZipFile->GetEntryHash().size() );
     292          84 :     sal_Int32 nLen = 0;
     293             : 
     294        1284 :     for ( EntryHash::iterator aIter = m_pZipFile->GetEntryHash().begin(); aIter != m_pZipFile->GetEntryHash().end(); ++aIter )
     295             :     {
     296        1200 :         if ( aNames.getLength() < ++nLen )
     297             :         {
     298             :             OSL_FAIL( "The size must be the same!\n" );
     299           0 :             aNames.realloc( nLen );
     300             :         }
     301             : 
     302        1200 :         aNames[nLen-1] = (*aIter).second.sPath;
     303             :     }
     304             : 
     305          84 :     if ( aNames.getLength() != nLen )
     306             :     {
     307             :         OSL_FAIL( "The size must be the same!\n" );
     308           0 :         aNames.realloc( nLen );
     309             :     }
     310             : 
     311          84 :     return aNames;
     312             : }
     313             : 
     314       67001 : sal_Bool SAL_CALL OZipFileAccess::hasByName( const OUString& aName )
     315             :     throw (uno::RuntimeException, std::exception)
     316             : {
     317       67001 :     ::osl::MutexGuard aGuard( m_aMutexHolder->GetMutex() );
     318             : 
     319       67001 :     if ( m_bDisposed )
     320           0 :         throw lang::DisposedException(THROW_WHERE );
     321             : 
     322       67001 :     if ( !m_pZipFile )
     323           0 :         throw uno::RuntimeException(THROW_WHERE);
     324             : 
     325       67001 :     EntryHash::iterator aIter = m_pZipFile->GetEntryHash().find( aName );
     326             : 
     327       67001 :     return ( aIter != m_pZipFile->GetEntryHash().end() );
     328             : }
     329             : 
     330           0 : uno::Type SAL_CALL OZipFileAccess::getElementType()
     331             :     throw ( uno::RuntimeException, std::exception )
     332             : {
     333           0 :     ::osl::MutexGuard aGuard( m_aMutexHolder->GetMutex() );
     334             : 
     335           0 :     if ( m_bDisposed )
     336           0 :         throw lang::DisposedException(THROW_WHERE );
     337             : 
     338           0 :     if ( !m_pZipFile )
     339           0 :         throw uno::RuntimeException(THROW_WHERE);
     340             : 
     341           0 :     return cppu::UnoType<io::XInputStream>::get();
     342             : }
     343             : 
     344           0 : sal_Bool SAL_CALL OZipFileAccess::hasElements()
     345             :     throw ( uno::RuntimeException, std::exception )
     346             : {
     347           0 :     ::osl::MutexGuard aGuard( m_aMutexHolder->GetMutex() );
     348             : 
     349           0 :     if ( m_bDisposed )
     350           0 :         throw lang::DisposedException(THROW_WHERE );
     351             : 
     352           0 :     if ( !m_pZipFile )
     353           0 :         throw uno::RuntimeException(THROW_WHERE);
     354             : 
     355           0 :     return ( m_pZipFile->GetEntryHash().size() != 0 );
     356             : }
     357             : 
     358             : // XZipFileAccess
     359           0 : uno::Reference< io::XInputStream > SAL_CALL OZipFileAccess::getStreamByPattern( const OUString& aPatternString )
     360             :     throw ( container::NoSuchElementException,
     361             :             io::IOException, packages::WrongPasswordException, packages::zip::ZipException,
     362             :             uno::RuntimeException, std::exception )
     363             : {
     364           0 :     ::osl::MutexGuard aGuard( m_aMutexHolder->GetMutex() );
     365             : 
     366           0 :     if ( m_bDisposed )
     367           0 :         throw lang::DisposedException(THROW_WHERE );
     368             : 
     369           0 :     if ( !m_pZipFile )
     370           0 :         throw io::NotConnectedException(THROW_WHERE );
     371             : 
     372             :     // Code to compare strings by patterns
     373           0 :     uno::Sequence< OUString > aPattern = GetPatternsFromString_Impl( aPatternString );
     374             : 
     375           0 :     for ( EntryHash::iterator aIter = m_pZipFile->GetEntryHash().begin(); aIter != m_pZipFile->GetEntryHash().end(); ++aIter )
     376             :     {
     377           0 :         if ( StringGoodForPattern_Impl( (*aIter).second.sPath, aPattern ) )
     378             :         {
     379           0 :             uno::Reference< io::XInputStream > xEntryStream( m_pZipFile->getDataStream( (*aIter).second,
     380             :                                                                                         ::rtl::Reference< EncryptionData >(),
     381             :                                                                                         false,
     382           0 :                                                                                         m_aMutexHolder ) );
     383             : 
     384           0 :             if ( !xEntryStream.is() )
     385           0 :                 throw uno::RuntimeException(THROW_WHERE );
     386           0 :             return xEntryStream;
     387             :         }
     388             :     }
     389             : 
     390           0 :     throw container::NoSuchElementException(THROW_WHERE );
     391             : }
     392             : 
     393             : // XComponent
     394        1178 : void SAL_CALL OZipFileAccess::dispose()
     395             :     throw ( uno::RuntimeException, std::exception )
     396             : {
     397        1178 :     ::osl::MutexGuard aGuard( m_aMutexHolder->GetMutex() );
     398             : 
     399        1178 :     if ( m_bDisposed )
     400           0 :         throw lang::DisposedException(THROW_WHERE );
     401             : 
     402        1178 :     if ( m_pListenersContainer )
     403             :     {
     404           0 :            lang::EventObject aSource( static_cast< ::cppu::OWeakObject* >(this) );
     405           0 :         m_pListenersContainer->disposeAndClear( aSource );
     406           0 :         delete m_pListenersContainer;
     407           0 :         m_pListenersContainer = NULL;
     408             :     }
     409             : 
     410        1178 :     if ( m_pZipFile )
     411             :     {
     412         816 :         delete m_pZipFile;
     413         816 :         m_pZipFile = NULL;
     414             :     }
     415             : 
     416        1178 :     if ( m_xContentStream.is() && m_bOwnContent )
     417             :         try {
     418         790 :             m_xContentStream->closeInput();
     419           0 :         } catch( uno::Exception& )
     420             :         {}
     421             : 
     422        1178 :     m_bDisposed = true;
     423        1178 : }
     424             : 
     425           0 : void SAL_CALL OZipFileAccess::addEventListener( const uno::Reference< lang::XEventListener >& xListener )
     426             :     throw ( uno::RuntimeException, std::exception )
     427             : {
     428           0 :     ::osl::MutexGuard aGuard( m_aMutexHolder->GetMutex() );
     429             : 
     430           0 :     if ( m_bDisposed )
     431           0 :         throw lang::DisposedException(THROW_WHERE );
     432             : 
     433           0 :     if ( !m_pListenersContainer )
     434           0 :         m_pListenersContainer = new ::cppu::OInterfaceContainerHelper( m_aMutexHolder->GetMutex() );
     435           0 :     m_pListenersContainer->addInterface( xListener );
     436           0 : }
     437             : 
     438           0 : void SAL_CALL OZipFileAccess::removeEventListener( const uno::Reference< lang::XEventListener >& xListener )
     439             :     throw ( uno::RuntimeException, std::exception )
     440             : {
     441           0 :     ::osl::MutexGuard aGuard( m_aMutexHolder->GetMutex() );
     442             : 
     443           0 :     if ( m_bDisposed )
     444           0 :         throw lang::DisposedException(THROW_WHERE );
     445             : 
     446           0 :     if ( m_pListenersContainer )
     447           0 :         m_pListenersContainer->removeInterface( xListener );
     448           0 : }
     449             : 
     450         170 : uno::Sequence< OUString > SAL_CALL OZipFileAccess::impl_staticGetSupportedServiceNames()
     451             : {
     452         170 :     uno::Sequence< OUString > aRet(2);
     453         170 :     aRet[0] = "com.sun.star.packages.zip.ZipFileAccess";
     454         170 :     aRet[1] = "com.sun.star.comp.packages.zip.ZipFileAccess";
     455         170 :     return aRet;
     456             : }
     457             : 
     458         340 : OUString SAL_CALL OZipFileAccess::impl_staticGetImplementationName()
     459             : {
     460         340 :     return OUString("com.sun.star.comp.package.zip.ZipFileAccess");
     461             : }
     462             : 
     463        1182 : uno::Reference< uno::XInterface > SAL_CALL OZipFileAccess::impl_staticCreateSelfInstance(
     464             :             const uno::Reference< lang::XMultiServiceFactory >& rxMSF )
     465             : {
     466        1182 :     return uno::Reference< uno::XInterface >( *new OZipFileAccess( comphelper::getComponentContext(rxMSF) ) );
     467             : }
     468             : 
     469           0 : OUString SAL_CALL OZipFileAccess::getImplementationName()
     470             :     throw ( uno::RuntimeException, std::exception )
     471             : {
     472           0 :     return impl_staticGetImplementationName();
     473             : }
     474             : 
     475           0 : sal_Bool SAL_CALL OZipFileAccess::supportsService( const OUString& ServiceName )
     476             :     throw ( uno::RuntimeException, std::exception )
     477             : {
     478           0 :     return cppu::supportsService(this, ServiceName);
     479             : }
     480             : 
     481           0 : uno::Sequence< OUString > SAL_CALL OZipFileAccess::getSupportedServiceNames()
     482             :     throw ( uno::RuntimeException, std::exception )
     483             : {
     484           0 :     return impl_staticGetSupportedServiceNames();
     485             : }
     486             : 
     487             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10