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

Generated by: LCOV version 1.11