LCOV - code coverage report
Current view: top level - package/source/zipapi - XUnbufferedStream.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 83 133 62.4 %
Date: 2014-04-11 Functions: 7 9 77.8 %
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/packages/zip/ZipConstants.hpp>
      21             : #include <com/sun/star/packages/zip/ZipIOException.hpp>
      22             : #include <com/sun/star/xml/crypto/CipherID.hpp>
      23             : 
      24             : #include <XUnbufferedStream.hxx>
      25             : #include <EncryptionData.hxx>
      26             : #include <PackageConstants.hxx>
      27             : #include <ZipFile.hxx>
      28             : #include <EncryptedDataHeader.hxx>
      29             : #include <algorithm>
      30             : #include <string.h>
      31             : 
      32             : #include <osl/mutex.hxx>
      33             : 
      34             : using namespace ::com::sun::star;
      35             : using namespace com::sun::star::packages::zip::ZipConstants;
      36             : using namespace com::sun::star::io;
      37             : using namespace com::sun::star::uno;
      38             : using com::sun::star::lang::IllegalArgumentException;
      39             : using com::sun::star::packages::zip::ZipIOException;
      40             : 
      41       37053 : XUnbufferedStream::XUnbufferedStream(
      42             :                       const uno::Reference< uno::XComponentContext >& xContext,
      43             :                       SotMutexHolderRef aMutexHolder,
      44             :                       ZipEntry & rEntry,
      45             :                       Reference < XInputStream > xNewZipStream,
      46             :                       const ::rtl::Reference< EncryptionData >& rData,
      47             :                       sal_Int8 nStreamMode,
      48             :                       sal_Bool bIsEncrypted,
      49             :                       const OUString& aMediaType,
      50             :                       sal_Bool bRecoveryMode )
      51       37053 : : maMutexHolder( aMutexHolder.Is() ? aMutexHolder : SotMutexHolderRef( new SotMutexHolder ) )
      52             : , mxZipStream ( xNewZipStream )
      53             : , mxZipSeek ( xNewZipStream, UNO_QUERY )
      54             : , maEntry ( rEntry )
      55             : , mxData ( rData )
      56             : , mnBlockSize( 1 )
      57             : , maInflater ( true )
      58       37053 : , mbRawStream ( nStreamMode == UNBUFF_STREAM_RAW || nStreamMode == UNBUFF_STREAM_WRAPPEDRAW )
      59       37053 : , mbWrappedRaw ( nStreamMode == UNBUFF_STREAM_WRAPPEDRAW )
      60             : , mbFinished ( sal_False )
      61             : , mnHeaderToRead ( 0 )
      62             : , mnZipCurrent ( 0 )
      63             : , mnZipEnd ( 0 )
      64             : , mnZipSize ( 0 )
      65             : , mnMyCurrent ( 0 )
      66      148212 : , mbCheckCRC( !bRecoveryMode )
      67             : {
      68       37053 :     mnZipCurrent = maEntry.nOffset;
      69       37053 :     if ( mbRawStream )
      70             :     {
      71        4678 :         mnZipSize = maEntry.nMethod == DEFLATED ? maEntry.nCompressedSize : maEntry.nSize;
      72        4678 :         mnZipEnd = maEntry.nOffset + mnZipSize;
      73             :     }
      74             :     else
      75             :     {
      76       32375 :         mnZipSize = maEntry.nSize;
      77       32375 :         mnZipEnd = maEntry.nMethod == DEFLATED ? maEntry.nOffset + maEntry.nCompressedSize : maEntry.nOffset + maEntry.nSize;
      78             :     }
      79             : 
      80       37053 :     if (mnZipSize < 0)
      81           0 :         throw ZipIOException("The stream seems to be broken!", uno::Reference< XInterface >());
      82             : 
      83       37053 :     sal_Bool bHaveEncryptData = ( rData.is() && rData->m_aSalt.getLength() && rData->m_aInitVector.getLength() && rData->m_nIterationCount != 0 ) ? sal_True : sal_False;
      84       37053 :     sal_Bool bMustDecrypt = ( nStreamMode == UNBUFF_STREAM_DATA && bHaveEncryptData && bIsEncrypted ) ? sal_True : sal_False;
      85             : 
      86       37053 :     if ( bMustDecrypt )
      87             :     {
      88          17 :         m_xCipherContext = ZipFile::StaticGetCipher( xContext, rData, false );
      89          17 :         mnBlockSize = ( rData->m_nEncAlg == xml::crypto::CipherID::AES_CBC_W3C_PADDING ? 16 : 1 );
      90             :     }
      91             : 
      92       37053 :     if ( bHaveEncryptData && mbWrappedRaw && bIsEncrypted )
      93             :     {
      94             :         // if we have the data needed to decrypt it, but didn't want it decrypted (or
      95             :         // we couldn't decrypt it due to wrong password), then we prepend this
      96             :         // data to the stream
      97             : 
      98             :         // Make a buffer big enough to hold both the header and the data itself
      99           0 :         maHeader.realloc  ( n_ConstHeaderSize +
     100           0 :                             rData->m_aInitVector.getLength() +
     101           0 :                             rData->m_aSalt.getLength() +
     102           0 :                             rData->m_aDigest.getLength() +
     103           0 :                             aMediaType.getLength() * sizeof( sal_Unicode ) );
     104           0 :         sal_Int8 * pHeader = maHeader.getArray();
     105           0 :         ZipFile::StaticFillHeader( rData, rEntry.nSize, aMediaType, pHeader );
     106           0 :         mnHeaderToRead = static_cast < sal_Int16 > ( maHeader.getLength() );
     107             :     }
     108       37053 : }
     109             : 
     110             : // allows to read package raw stream
     111           0 : XUnbufferedStream::XUnbufferedStream(
     112             :                     const uno::Reference< uno::XComponentContext >& /*xContext*/,
     113             :                     const Reference < XInputStream >& xRawStream,
     114             :                     const ::rtl::Reference< EncryptionData >& rData )
     115           0 : : maMutexHolder( new SotMutexHolder )
     116             : , mxZipStream ( xRawStream )
     117             : , mxZipSeek ( xRawStream, UNO_QUERY )
     118             : , mxData ( rData )
     119             : , mnBlockSize( 1 )
     120             : , maInflater ( true )
     121             : , mbRawStream ( sal_False )
     122             : , mbWrappedRaw ( sal_False )
     123             : , mbFinished ( sal_False )
     124             : , mnHeaderToRead ( 0 )
     125             : , mnZipCurrent ( 0 )
     126             : , mnZipEnd ( 0 )
     127             : , mnZipSize ( 0 )
     128             : , mnMyCurrent ( 0 )
     129           0 : , mbCheckCRC( sal_False )
     130             : {
     131             :     // for this scenario maEntry is not set !!!
     132             :     OSL_ENSURE( mxZipSeek.is(), "The stream must be seekable!\n" );
     133             : 
     134             :     // skip raw header, it must be already parsed to rData
     135           0 :     mnZipCurrent = n_ConstHeaderSize + rData->m_aInitVector.getLength() +
     136           0 :                             rData->m_aSalt.getLength() + rData->m_aDigest.getLength();
     137             : 
     138             :     try {
     139           0 :         if ( mxZipSeek.is() )
     140           0 :             mnZipSize = mxZipSeek->getLength();
     141           0 :     } catch( Exception& e )
     142             :     {
     143             :         // in case of problem the size will stay set to 0
     144             :         SAL_WARN("package", "ignoring Exception " + e.Message);
     145             :     }
     146             : 
     147           0 :     mnZipEnd = mnZipCurrent + mnZipSize;
     148             : 
     149             :     // the raw data will not be decrypted, no need for the cipher
     150             :     // m_xCipherContext = ZipFile::StaticGetCipher( xContext, rData, false );
     151           0 : }
     152             : 
     153       74106 : XUnbufferedStream::~XUnbufferedStream()
     154             : {
     155       74106 : }
     156             : 
     157       45912 : sal_Int32 SAL_CALL XUnbufferedStream::readBytes( Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
     158             :         throw( NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception)
     159             : {
     160       45912 :     ::osl::MutexGuard aGuard( maMutexHolder->GetMutex() );
     161             : 
     162       45912 :     sal_Int32 nRequestedBytes = nBytesToRead;
     163             :     OSL_ENSURE( !mnHeaderToRead || mbWrappedRaw, "Only encrypted raw stream can be provided with header!" );
     164       45912 :     if ( mnMyCurrent + nRequestedBytes > mnZipSize + maHeader.getLength() )
     165       41913 :         nRequestedBytes = static_cast < sal_Int32 > ( mnZipSize + maHeader.getLength() - mnMyCurrent );
     166             : 
     167       45912 :     sal_Int32 nTotal = 0;
     168       45912 :     aData.realloc ( nRequestedBytes );
     169       45912 :     if ( nRequestedBytes )
     170             :     {
     171       31858 :         sal_Int32 nRead = 0;
     172       31858 :         sal_Int32 nLastRead = 0;
     173       31858 :         if ( mbRawStream )
     174             :         {
     175        5010 :             sal_Int64 nDiff = mnZipEnd - mnZipCurrent;
     176             : 
     177        5010 :             if ( mbWrappedRaw && mnHeaderToRead )
     178             :             {
     179             :                 sal_Int16 nHeadRead = static_cast< sal_Int16 >(( nRequestedBytes > mnHeaderToRead ?
     180           0 :                                                                                  mnHeaderToRead : nRequestedBytes ));
     181           0 :                 memcpy ( aData.getArray(), maHeader.getConstArray() + maHeader.getLength() - mnHeaderToRead, nHeadRead );
     182           0 :                 mnHeaderToRead = mnHeaderToRead - nHeadRead;
     183             : 
     184           0 :                 if ( nHeadRead < nRequestedBytes )
     185             :                 {
     186           0 :                     sal_Int32 nToRead = nRequestedBytes - nHeadRead;
     187           0 :                     nToRead = ( nDiff < nToRead ) ? sal::static_int_cast< sal_Int32 >( nDiff ) : nToRead;
     188             : 
     189           0 :                     Sequence< sal_Int8 > aPureData( nToRead );
     190           0 :                     mxZipSeek->seek ( mnZipCurrent );
     191           0 :                     nRead = mxZipStream->readBytes ( aPureData, nToRead );
     192           0 :                     mnZipCurrent += nRead;
     193             : 
     194           0 :                     aPureData.realloc( nRead );
     195           0 :                     if ( mbCheckCRC )
     196           0 :                         maCRC.update( aPureData );
     197             : 
     198           0 :                     aData.realloc( nHeadRead + nRead );
     199             : 
     200           0 :                     sal_Int8* pPureBuffer = aPureData.getArray();
     201           0 :                     sal_Int8* pBuffer = aData.getArray();
     202           0 :                     for ( sal_Int32 nInd = 0; nInd < nRead; nInd++ )
     203           0 :                         pBuffer[ nHeadRead + nInd ] = pPureBuffer[ nInd ];
     204             :                 }
     205             : 
     206           0 :                 nRead += nHeadRead;
     207             :             }
     208             :             else
     209             :             {
     210        5010 :                 mxZipSeek->seek ( mnZipCurrent );
     211             : 
     212        5010 :                 nRead = mxZipStream->readBytes (
     213             :                                         aData,
     214        5010 :                                         static_cast < sal_Int32 > ( nDiff < nRequestedBytes ? nDiff : nRequestedBytes ) );
     215             : 
     216        5010 :                 mnZipCurrent += nRead;
     217             : 
     218        5010 :                 aData.realloc( nRead );
     219        5010 :                 if ( mbWrappedRaw && mbCheckCRC )
     220           0 :                     maCRC.update( aData );
     221             :             }
     222             :         }
     223             :         else
     224             :         {
     225      127772 :             while ( 0 == ( nLastRead = maInflater.doInflateSegment( aData, nRead, aData.getLength() - nRead ) ) ||
     226       27195 :                   ( nRead + nLastRead != nRequestedBytes && mnZipCurrent < mnZipEnd ) )
     227             :             {
     228       23528 :                 nRead += nLastRead;
     229             : 
     230       23528 :                 if ( nRead > nRequestedBytes )
     231             :                     throw RuntimeException(
     232             :                         "Should not be possible to read more then requested!",
     233           0 :                         Reference< XInterface >() );
     234             : 
     235       23528 :                 if ( maInflater.finished() || maInflater.getLastInflateError() )
     236             :                     throw ZipIOException("The stream seems to be broken!",
     237           1 :                                         Reference< XInterface >() );
     238             : 
     239       23527 :                 if ( maInflater.needsDictionary() )
     240             :                     throw ZipIOException("Dictionaries are not supported!",
     241           0 :                                         Reference< XInterface >() );
     242             : 
     243       23527 :                 sal_Int32 nDiff = static_cast< sal_Int32 >( mnZipEnd - mnZipCurrent );
     244       23527 :                 if ( nDiff > 0 )
     245             :                 {
     246       23527 :                     mxZipSeek->seek ( mnZipCurrent );
     247             : 
     248       23527 :                     sal_Int32 nToRead = std::max( nRequestedBytes, static_cast< sal_Int32 >( 8192 ) );
     249       23527 :                     if ( mnBlockSize > 1 )
     250          13 :                         nToRead = nToRead + mnBlockSize - nToRead % mnBlockSize;
     251       23527 :                     nToRead = std::min( nDiff, nToRead );
     252             : 
     253       23527 :                     sal_Int32 nZipRead = mxZipStream->readBytes( maCompBuffer, nToRead );
     254       23527 :                     if ( nZipRead < nToRead )
     255             :                         throw ZipIOException("No expected data!",
     256           0 :                                             Reference< XInterface >() );
     257             : 
     258       23527 :                     mnZipCurrent += nZipRead;
     259             :                     // maCompBuffer now has the data, check if we need to decrypt
     260             :                     // before passing to the Inflater
     261       23527 :                     if ( m_xCipherContext.is() )
     262             :                     {
     263          17 :                         if ( mbCheckCRC )
     264          17 :                             maCRC.update( maCompBuffer );
     265             : 
     266          17 :                         maCompBuffer = m_xCipherContext->convertWithCipherContext( maCompBuffer );
     267          17 :                         if ( mnZipCurrent == mnZipEnd )
     268             :                         {
     269          17 :                             uno::Sequence< sal_Int8 > aSuffix = m_xCipherContext->finalizeCipherContextAndDispose();
     270          17 :                             if ( aSuffix.getLength() )
     271             :                             {
     272          12 :                                 sal_Int32 nOldLen = maCompBuffer.getLength();
     273          12 :                                 maCompBuffer.realloc( nOldLen + aSuffix.getLength() );
     274          12 :                                 memcpy( maCompBuffer.getArray() + nOldLen, aSuffix.getConstArray(), aSuffix.getLength() );
     275          17 :                             }
     276             :                         }
     277             :                     }
     278       23527 :                     maInflater.setInput ( maCompBuffer );
     279             :                 }
     280             :                 else
     281             :                 {
     282             :                     throw ZipIOException("The stream seems to be broken!",
     283           0 :                                         Reference< XInterface >() );
     284             :                 }
     285             :             }
     286             :         }
     287             : 
     288       31857 :         mnMyCurrent += nRead + nLastRead;
     289       31857 :         nTotal = nRead + nLastRead;
     290       31857 :         if ( nTotal < nRequestedBytes)
     291           0 :             aData.realloc ( nTotal );
     292             : 
     293       31857 :         if ( mbCheckCRC && ( !mbRawStream || mbWrappedRaw ) )
     294             :         {
     295       26847 :             if ( !m_xCipherContext.is() && !mbWrappedRaw )
     296       26830 :                 maCRC.update( aData );
     297             : 
     298       26847 :             if ( mnZipSize + maHeader.getLength() == mnMyCurrent && maCRC.getValue() != maEntry.nCrc )
     299             :                 throw ZipIOException("The stream seems to be broken!",
     300           0 :                                     Reference< XInterface >() );
     301             :         }
     302             :     }
     303             : 
     304       45912 :     return nTotal;
     305             : }
     306             : 
     307       26423 : sal_Int32 SAL_CALL XUnbufferedStream::readSomeBytes( Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead )
     308             :         throw( NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception)
     309             : {
     310       26423 :     return readBytes ( aData, nMaxBytesToRead );
     311             : }
     312           0 : void SAL_CALL XUnbufferedStream::skipBytes( sal_Int32 nBytesToSkip )
     313             :         throw( NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception)
     314             : {
     315           0 :     if ( nBytesToSkip )
     316             :     {
     317           0 :         Sequence < sal_Int8 > aSequence ( nBytesToSkip );
     318           0 :         readBytes ( aSequence, nBytesToSkip );
     319             :     }
     320           0 : }
     321             : 
     322        4920 : sal_Int32 SAL_CALL XUnbufferedStream::available(  )
     323             :         throw( NotConnectedException, IOException, RuntimeException, std::exception)
     324             : {
     325        4920 :     return static_cast < sal_Int32 > ( mnZipSize - mnMyCurrent );
     326             : }
     327             : 
     328       12122 : void SAL_CALL XUnbufferedStream::closeInput(  )
     329             :         throw( NotConnectedException, IOException, RuntimeException, std::exception)
     330             : {
     331       12122 : }
     332             : 
     333             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10