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

Generated by: LCOV version 1.10