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

Generated by: LCOV version 1.10