LCOV - code coverage report
Current view: top level - oox/source/ole - vbainputstream.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 68 89 76.4 %
Date: 2012-08-25 Functions: 4 10 40.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 60 110 54.5 %

           Branch data     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 "oox/ole/vbainputstream.hxx"
      21                 :            : #include <osl/diagnose.h>
      22                 :            : 
      23                 :            : namespace oox {
      24                 :            : namespace ole {
      25                 :            : 
      26                 :            : // ============================================================================
      27                 :            : 
      28                 :            : namespace {
      29                 :            : 
      30                 :            : const sal_uInt8 VBASTREAM_SIGNATURE         = 1;
      31                 :            : 
      32                 :            : const sal_uInt16 VBACHUNK_SIGMASK           = 0x7000;
      33                 :            : const sal_uInt16 VBACHUNK_SIG               = 0x3000;
      34                 :            : const sal_uInt16 VBACHUNK_COMPRESSED        = 0x8000;
      35                 :            : const sal_uInt16 VBACHUNK_LENMASK           = 0x0FFF;
      36                 :            : 
      37                 :            : } // namespace
      38                 :            : 
      39                 :            : // ============================================================================
      40                 :            : 
      41                 :        114 : VbaInputStream::VbaInputStream( BinaryInputStream& rInStrm ) :
      42                 :            :     BinaryStreamBase( false ),
      43                 :            :     mpInStrm( &rInStrm ),
      44 [ +  - ][ +  - ]:        114 :     mnChunkPos( 0 )
         [ #  # ][ #  # ]
      45                 :            : {
      46 [ +  - ][ #  # ]:        114 :     maChunk.reserve( 4096 );
      47                 :            : 
      48 [ +  - ][ #  # ]:        114 :     sal_uInt8 nSig = rInStrm.readuInt8();
      49                 :            :     OSL_ENSURE( nSig == VBASTREAM_SIGNATURE, "VbaInputStream::VbaInputStream - wrong signature" );
      50 [ +  - ][ +  - ]:        114 :     mbEof = mbEof || rInStrm.isEof() || (nSig != VBASTREAM_SIGNATURE);
         [ -  + ][ #  # ]
         [ #  # ][ #  # ]
      51                 :        114 : }
      52                 :            : 
      53                 :          0 : sal_Int64 VbaInputStream::size() const
      54                 :            : {
      55                 :          0 :     return -1;
      56                 :            : }
      57                 :            : 
      58                 :          0 : sal_Int64 VbaInputStream::tell() const
      59                 :            : {
      60                 :          0 :     return -1;
      61                 :            : }
      62                 :            : 
      63                 :          0 : void VbaInputStream::seek( sal_Int64 )
      64                 :            : {
      65                 :          0 : }
      66                 :            : 
      67                 :          0 : void VbaInputStream::close()
      68                 :            : {
      69                 :          0 :     mpInStrm = 0;
      70                 :          0 :     mbEof = true;
      71                 :          0 : }
      72                 :            : 
      73                 :       1959 : sal_Int32 VbaInputStream::readData( StreamDataSequence& orData, sal_Int32 nBytes, size_t nAtomSize )
      74                 :            : {
      75                 :       1959 :     sal_Int32 nRet = 0;
      76         [ +  - ]:       1959 :     if( !mbEof )
      77                 :            :     {
      78 [ +  - ][ +  - ]:       1959 :         orData.realloc( ::std::max< sal_Int32 >( nBytes, 0 ) );
      79         [ +  + ]:       1959 :         if( nBytes > 0 )
      80                 :            :         {
      81                 :       1456 :             nRet = readMemory( orData.getArray(), nBytes, nAtomSize );
      82         [ +  + ]:       1456 :             if( nRet < nBytes )
      83                 :         95 :                 orData.realloc( nRet );
      84                 :            :         }
      85                 :            :     }
      86                 :       1959 :     return nRet;
      87                 :            : }
      88                 :            : 
      89                 :       4478 : sal_Int32 VbaInputStream::readMemory( void* opMem, sal_Int32 nBytes, size_t /*nAtomSize*/ )
      90                 :            : {
      91                 :       4478 :     sal_Int32 nRet = 0;
      92                 :       4478 :     sal_uInt8* opnMem = reinterpret_cast< sal_uInt8* >( opMem );
      93 [ +  + ][ +  + ]:       8956 :     while( (nBytes > 0) && updateChunk() )
                 [ +  + ]
      94                 :            :     {
      95                 :       4478 :         sal_Int32 nChunkLeft = static_cast< sal_Int32 >( maChunk.size() - mnChunkPos );
      96         [ +  - ]:       4478 :         sal_Int32 nReadBytes = ::std::min( nBytes, nChunkLeft );
      97 [ +  - ][ +  - ]:       4478 :         memcpy( opnMem, &*(maChunk.begin() + mnChunkPos), nReadBytes );
      98                 :       4478 :         opnMem += nReadBytes;
      99                 :       4478 :         mnChunkPos += static_cast< size_t >( nReadBytes );
     100                 :       4478 :         nBytes -= nReadBytes;
     101                 :       4478 :         nRet += nReadBytes;
     102                 :            :     }
     103                 :       4478 :     return nRet;
     104                 :            : }
     105                 :            : 
     106                 :          0 : void VbaInputStream::skip( sal_Int32 nBytes, size_t /*nAtomSize*/ )
     107                 :            : {
     108 [ #  # ][ #  # ]:          0 :     while( (nBytes > 0) && updateChunk() )
                 [ #  # ]
     109                 :            :     {
     110                 :          0 :         sal_Int32 nChunkLeft = static_cast< sal_Int32 >( maChunk.size() - mnChunkPos );
     111         [ #  # ]:          0 :         sal_Int32 nSkipBytes = ::std::min( nBytes, nChunkLeft );
     112                 :          0 :         mnChunkPos += static_cast< size_t >( nSkipBytes );
     113                 :          0 :         nBytes -= nSkipBytes;
     114                 :            :     }
     115                 :          0 : }
     116                 :            : 
     117                 :            : // private --------------------------------------------------------------------
     118                 :            : 
     119                 :       4573 : bool VbaInputStream::updateChunk()
     120                 :            : {
     121 [ +  - ][ +  + ]:       4573 :     if( mbEof || (mnChunkPos < maChunk.size()) ) return !mbEof;
                 [ +  + ]
     122                 :            :     // try to read next chunk header, this may trigger EOF
     123                 :        218 :     sal_uInt16 nHeader = mpInStrm->readuInt16();
     124                 :            : 
     125                 :        218 :     mbEof = mpInStrm->isEof();
     126         [ +  + ]:        218 :     if( mbEof ) return false;
     127                 :            : 
     128                 :            :     // check header signature
     129                 :        123 :     bool bIgnoreBrokenSig = !( (nHeader & VBACHUNK_SIGMASK) == VBACHUNK_SIG );
     130                 :            : 
     131                 :            :     // decode length of chunk data and compression flag
     132                 :        123 :     bool bCompressed = getFlag( nHeader, VBACHUNK_COMPRESSED );
     133                 :        123 :     sal_uInt16 nChunkLen = (nHeader & VBACHUNK_LENMASK) + 1;
     134                 :            :     OSL_ENSURE( bCompressed || (nChunkLen == 4096), "VbaInputStream::updateChunk - invalid uncompressed chunk size" );
     135                 :            : 
     136                 :            :     // From the amazing bit detective work of Valek Filippov<frob@gnome.org>
     137                 :            :     // this tweak and the one at the bottom of the method to seek to the
     138                 :            :     // start of the next chunk we can read those strange broken
     139                 :            :     // ( I guess from a MSO bug ) commpessed streams > 4k
     140                 :            : 
     141         [ -  + ]:        123 :     if ( bIgnoreBrokenSig )
     142                 :            :     {
     143                 :          0 :         bCompressed = true;
     144                 :          0 :         nChunkLen = 4094;
     145                 :            :     }
     146                 :            : 
     147                 :        123 :     sal_Int64 target = mpInStrm->tell() + nChunkLen;
     148         [ +  - ]:        123 :     if( bCompressed )
     149                 :            :     {
     150                 :        123 :         maChunk.clear();
     151                 :        123 :         sal_uInt8 nBitCount = 4;
     152                 :        123 :         sal_uInt16 nChunkPos = 0;
     153 [ +  - ][ +  - ]:       5671 :         while( !mbEof && !mpInStrm->isEof() && (nChunkPos < nChunkLen) )
         [ +  + ][ +  + ]
     154                 :            :         {
     155                 :       5548 :             sal_uInt8 nTokenFlags = mpInStrm->readuInt8();
     156                 :       5548 :             ++nChunkPos;
     157 [ +  - ][ +  - ]:      49421 :             for( int nBit = 0; !mbEof && !mpInStrm->isEof() && (nBit < 8) && (nChunkPos < nChunkLen); ++nBit, nTokenFlags >>= 1 )
         [ +  + ][ +  + ]
                 [ +  + ]
     158                 :            :             {
     159         [ +  + ]:      43873 :                 if( nTokenFlags & 1 )
     160                 :            :                 {
     161                 :      11002 :                     sal_uInt16 nCopyToken = mpInStrm->readuInt16();
     162                 :      11002 :                     nChunkPos = nChunkPos + 2;
     163                 :            :                     // update bit count used for offset/length in the token
     164         [ +  + ]:      11694 :                     while( static_cast< size_t >( 1 << nBitCount ) < maChunk.size() ) ++nBitCount;
     165                 :            :                     // extract length from lower (16-nBitCount) bits, plus 3
     166                 :      11002 :                     sal_uInt16 nLength = extractValue< sal_uInt16 >( nCopyToken, 0, 16 - nBitCount ) + 3;
     167                 :            :                     // extract offset from high nBitCount bits, plus 1
     168                 :      11002 :                     sal_uInt16 nOffset = extractValue< sal_uInt16 >( nCopyToken, 16 - nBitCount, nBitCount ) + 1;
     169 [ -  + ][ +  - ]:      11002 :                     mbEof = (nOffset > maChunk.size()) || (maChunk.size() + nLength > 4096);
     170                 :            :                     OSL_ENSURE( !mbEof, "VbaInputStream::updateChunk - invalid offset or size in copy token" );
     171         [ +  - ]:      11002 :                     if( !mbEof )
     172                 :            :                     {
     173                 :            :                         // append data to buffer
     174         [ +  - ]:      11002 :                         maChunk.resize( maChunk.size() + nLength );
     175 [ +  - ][ +  - ]:      11002 :                         sal_uInt8* pnTo = &*(maChunk.end() - nLength);
     176                 :      11002 :                         const sal_uInt8* pnEnd = pnTo + nLength;
     177                 :      11002 :                         const sal_uInt8* pnFrom = pnTo - nOffset;
     178                 :            :                         // offset may be less than length, effectively duplicating source data several times
     179         [ +  - ]:      11002 :                         size_t nRunLen = ::std::min< size_t >( nLength, nOffset );
     180         [ +  + ]:      22402 :                         while( pnTo < pnEnd )
     181                 :            :                         {
     182         [ +  - ]:      11400 :                             size_t nStepLen = ::std::min< size_t >( nRunLen, pnEnd - pnTo );
     183                 :      11400 :                             memcpy( pnTo, pnFrom, nStepLen );
     184                 :      11400 :                             pnTo += nStepLen;
     185                 :            :                         }
     186                 :            :                     }
     187                 :            :                 }
     188                 :            :                 // we suspect this will never be called
     189                 :            :                 else
     190                 :            :                 {
     191                 :      32871 :                     maChunk.resize( maChunk.size() + 1 );
     192                 :      32871 :                     *mpInStrm >> maChunk.back();
     193                 :      32871 :                     ++nChunkPos;
     194                 :            :                 }
     195                 :            :             }
     196                 :            :         }
     197                 :            :     }
     198                 :            :     else
     199                 :            :     {
     200                 :          0 :         maChunk.resize( nChunkLen );
     201                 :          0 :         mpInStrm->readMemory( &maChunk.front(), nChunkLen );
     202                 :            :     }
     203                 :            :     // decompression sometimes leaves the stream pos offset 1 place ( at
     204                 :            :     // least ) past or before the expected stream pos.
     205                 :            :     // here we make sure we are on the chunk boundry
     206                 :        123 :     mpInStrm->seek( target );
     207                 :        123 :     mnChunkPos = 0;
     208                 :       4573 :     return !mbEof;
     209                 :            : }
     210                 :            : 
     211                 :            : // ============================================================================
     212                 :            : 
     213                 :            : } // namespace ole
     214                 :            : } // namespace oox
     215                 :            : 
     216                 :            : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10