LCOV - code coverage report
Current view: top level - oox/source/ole - axbinarywriter.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 89 102 87.3 %
Date: 2014-04-11 Functions: 19 24 79.2 %
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             : #include "oox/ole/axbinarywriter.hxx"
      10             : 
      11             : #include "oox/ole/olehelper.hxx"
      12             : 
      13             : namespace oox {
      14             : namespace ole {
      15             : 
      16             : 
      17             : 
      18             : namespace {
      19             : 
      20             : const sal_uInt32 AX_STRING_COMPRESSED       = 0x80000000;
      21             : 
      22             : } // namespace
      23             : 
      24             : 
      25             : 
      26           2 : AxAlignedOutputStream::AxAlignedOutputStream( BinaryOutputStream& rOutStrm ) :
      27             :     BinaryStreamBase( false ),
      28             :     mpOutStrm( &rOutStrm ),
      29             :     mnStrmPos( 0 ),
      30           2 :     mnStrmSize( rOutStrm.getRemaining() ),
      31           4 :     mnWrappedBeginPos( rOutStrm.tell() )
      32             : {
      33           2 :     mbEof = mbEof || rOutStrm.isEof();
      34           2 : }
      35             : 
      36           0 : sal_Int64 AxAlignedOutputStream::size() const
      37             : {
      38           0 :     return mpOutStrm ? mnStrmSize : -1;
      39             : }
      40             : 
      41           6 : sal_Int64 AxAlignedOutputStream::tell() const
      42             : {
      43           6 :     return mpOutStrm ? mnStrmPos : -1;
      44             : }
      45             : 
      46           4 : void AxAlignedOutputStream::seek( sal_Int64 nPos )
      47             : {
      48           4 :     mbEof = (nPos < 0);
      49           4 :     if( !mbEof )
      50             :     {
      51           4 :         mpOutStrm->seek( static_cast< sal_Int32 >( mnWrappedBeginPos + nPos  ) );
      52           4 :         mnStrmPos = mpOutStrm->tell() - mnWrappedBeginPos;
      53             :     }
      54           4 : }
      55             : 
      56           0 : void AxAlignedOutputStream::close()
      57             : {
      58           0 :     mpOutStrm = 0;
      59           0 :     mbEof = true;
      60           0 : }
      61             : 
      62           0 : void AxAlignedOutputStream::writeData( const StreamDataSequence& orData, size_t nAtomSize )
      63             : {
      64           0 :     mpOutStrm->writeData( orData, nAtomSize );
      65           0 :     mnStrmPos = mpOutStrm->tell() - mnWrappedBeginPos;
      66           0 : }
      67             : 
      68          26 : void AxAlignedOutputStream::writeMemory( const void* opMem, sal_Int32 nBytes, size_t nAtomSize )
      69             : {
      70          26 :     mpOutStrm->writeMemory( opMem, nBytes, nAtomSize );
      71          26 :     mnStrmPos = mpOutStrm->tell() - mnWrappedBeginPos;
      72          26 : }
      73             : 
      74          17 : void AxAlignedOutputStream::pad( sal_Int32 nBytes, size_t nAtomSize )
      75             : {
      76             :    //PRESUMABELY we need to pad with 0's here as appropriate
      77          17 :    com::sun::star::uno::Sequence< sal_Int8 > aData( nBytes );
      78             :    // ok we could be padding with rubbish here, but really that shouldn't matter
      79             :    // set to 0(s), easier to not get fooled by 0's when looking at
      80             :    // binary content......
      81          17 :    memset( static_cast<void*>( aData.getArray() ), 0, nBytes );
      82          17 :    mpOutStrm->writeData( aData, nAtomSize );
      83          17 :    mnStrmPos = mpOutStrm->tell() - mnWrappedBeginPos;
      84          17 : }
      85             : 
      86          17 : void AxAlignedOutputStream::align( size_t nSize )
      87             : {
      88          17 :     pad( static_cast< sal_Int32 >( (nSize - (mnStrmPos % nSize)) % nSize ) );
      89          17 : }
      90             : 
      91             : 
      92             : 
      93             : namespace {
      94             : 
      95           2 : void lclWriteString( AxAlignedOutputStream& rOutStrm, OUString& rValue, sal_uInt32 nSize, bool bArrayString )
      96             : {
      97           2 :     bool bCompressed = getFlag( nSize, AX_STRING_COMPRESSED );
      98           2 :     rOutStrm.writeCompressedUnicodeArray( rValue, bCompressed || bArrayString );
      99           2 : }
     100             : 
     101             : } // namespace
     102             : 
     103             : 
     104             : 
     105           3 : AxBinaryPropertyWriter::ComplexProperty::~ComplexProperty()
     106             : {
     107           3 : }
     108             : 
     109           1 : bool AxBinaryPropertyWriter::PairProperty::writeProperty( AxAlignedOutputStream& rOutStrm )
     110             : {
     111           1 :     rOutStrm << mrPairData.first << mrPairData.second;
     112           1 :     return true;
     113             : }
     114             : 
     115           2 : bool AxBinaryPropertyWriter::StringProperty::writeProperty( AxAlignedOutputStream& rOutStrm )
     116             : {
     117           2 :     lclWriteString( rOutStrm, mrValue, mnSize, false );
     118           2 :     return true;
     119             : }
     120             : 
     121             : 
     122             : 
     123           2 : AxBinaryPropertyWriter::AxBinaryPropertyWriter( BinaryOutputStream& rOutStrm, bool b64BitPropFlags ) :
     124             :     maOutStrm( rOutStrm ),
     125             :     mnPropFlags( 0x0 ),
     126             :     mbValid( true ),
     127           2 :     mb64BitPropFlags( b64BitPropFlags )
     128             : {
     129           2 :     sal_uInt16 nId( 0x0200 );
     130           2 :     maOutStrm << nId;
     131           2 :     mnBlockSize = 0; // will be filled in the finalize method
     132             : 
     133           2 :     maOutStrm << nId;
     134           2 :     mnPropFlagsStart = maOutStrm.tell();
     135             : 
     136           2 :     if( mb64BitPropFlags )
     137           1 :         maOutStrm << mnPropFlags;
     138             :     else
     139           1 :         maOutStrm << sal_uInt32( mnPropFlags );
     140           2 :     mnNextProp = 1;
     141           2 : }
     142             : 
     143           1 : void AxBinaryPropertyWriter::writeBoolProperty( bool orbValue, bool bReverse )
     144             : {
     145             :     // orbValue == bReverse false then we want to set the bit, e.g. don't skip
     146           1 :     startNextProperty( orbValue == bReverse );
     147           1 : }
     148             : 
     149           1 : void AxBinaryPropertyWriter::writePairProperty( AxPairData& orPairData )
     150             : {
     151           1 :     if( startNextProperty() )
     152           1 :         maLargeProps.push_back( ComplexPropVector::value_type( new PairProperty( orPairData ) ) );
     153           1 : }
     154             : 
     155           2 : void AxBinaryPropertyWriter::writeStringProperty( OUString& orValue, bool bCompressed )
     156             : {
     157           2 :     sal_uInt32 nSize = orValue.getLength();
     158           2 :     if ( bCompressed )
     159           2 :         setFlag(  nSize, AX_STRING_COMPRESSED );
     160             :     else
     161           0 :         nSize *= 2;
     162           2 :     maOutStrm.writeAligned< sal_uInt32 >( nSize );
     163           2 :     maLargeProps.push_back( ComplexPropVector::value_type( new StringProperty( orValue, nSize ) ) );
     164           2 :     startNextProperty();
     165           2 : }
     166             : 
     167           2 : bool AxBinaryPropertyWriter::finalizeExport()
     168             : {
     169             :     // write large properties
     170           2 :     maOutStrm.align( 4 );
     171           2 :     if( !maLargeProps.empty() )
     172             :     {
     173           5 :         for( ComplexPropVector::iterator aIt = maLargeProps.begin(), aEnd = maLargeProps.end(); ensureValid() && (aIt != aEnd); ++aIt )
     174             :         {
     175           3 :             (*aIt)->writeProperty( maOutStrm );
     176           3 :             maOutStrm.align( 4 );
     177             :         }
     178             :     }
     179             : 
     180           2 :     mnBlockSize = maOutStrm.tell() - mnPropFlagsStart;
     181             : 
     182             :     // write stream properties (no stream alignment between properties!)
     183           2 :     if( !maStreamProps.empty() )
     184           0 :         for( ComplexPropVector::iterator aIt = maStreamProps.begin(), aEnd = maStreamProps.end(); ensureValid() && (aIt != aEnd); ++aIt )
     185           0 :            (*aIt)->writeProperty( maOutStrm );
     186             : 
     187           2 :     sal_Int64 nPos = maOutStrm.tell();
     188           2 :     maOutStrm.seek( mnPropFlagsStart - sizeof( mnBlockSize ) );
     189             : 
     190           2 :     maOutStrm << mnBlockSize;
     191             : 
     192           2 :     if( mb64BitPropFlags )
     193           1 :         maOutStrm << mnPropFlags;
     194             :     else
     195           1 :         maOutStrm << sal_uInt32( mnPropFlags );
     196             : 
     197           2 :     maOutStrm.seek( nPos );
     198           2 :     return true;
     199             : }
     200             : 
     201           5 : bool AxBinaryPropertyWriter::ensureValid( bool bCondition )
     202             : {
     203           5 :     mbValid = mbValid && bCondition && !maOutStrm.isEof();
     204           5 :     return mbValid;
     205             : }
     206             : 
     207          41 : bool AxBinaryPropertyWriter::startNextProperty( bool bSkip )
     208             : {
     209             :     // if we are skipping then we clear the flag
     210          41 :     setFlag( mnPropFlags, mnNextProp, !bSkip );
     211          41 :     mnNextProp <<= 1;
     212          41 :     return true;
     213             : }
     214             : 
     215             : 
     216             : 
     217             : } // namespace exp
     218         177 : } // namespace ole
     219             : 
     220             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10