LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/sc/source/filter/oox - biffcodec.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 1 102 1.0 %
Date: 2013-07-09 Functions: 2 22 9.1 %
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 "biffcodec.hxx"
      21             : 
      22             : #include <osl/thread.h>
      23             : #include <string.h>
      24             : #include "oox/core/filterbase.hxx"
      25             : #include "biffinputstream.hxx"
      26             : 
      27             : namespace oox {
      28             : namespace xls {
      29             : 
      30             : // ============================================================================
      31             : 
      32             : using namespace ::com::sun::star::beans;
      33             : using namespace ::com::sun::star::uno;
      34             : 
      35             : using ::oox::core::FilterBase;
      36             : 
      37             : // ============================================================================
      38             : 
      39           0 : BiffDecoderBase::BiffDecoderBase() :
      40           0 :     mbValid( false )
      41             : {
      42           0 : }
      43             : 
      44           0 : BiffDecoderBase::~BiffDecoderBase()
      45             : {
      46           0 : }
      47             : 
      48           0 : ::comphelper::DocPasswordVerifierResult BiffDecoderBase::verifyPassword( const OUString& rPassword, Sequence< NamedValue >& o_rEncryptionData )
      49             : {
      50           0 :     o_rEncryptionData = implVerifyPassword( rPassword );
      51           0 :     mbValid = o_rEncryptionData.hasElements();
      52           0 :     return mbValid ? ::comphelper::DocPasswordVerifierResult_OK : ::comphelper::DocPasswordVerifierResult_WRONG_PASSWORD;
      53             : }
      54             : 
      55           0 : ::comphelper::DocPasswordVerifierResult BiffDecoderBase::verifyEncryptionData( const Sequence< NamedValue >& rEncryptionData )
      56             : {
      57           0 :     mbValid = implVerifyEncryptionData( rEncryptionData );
      58           0 :     return mbValid ? ::comphelper::DocPasswordVerifierResult_OK : ::comphelper::DocPasswordVerifierResult_WRONG_PASSWORD;
      59             : }
      60             : 
      61           0 : void BiffDecoderBase::decode( sal_uInt8* pnDestData, const sal_uInt8* pnSrcData, sal_Int64 nStreamPos, sal_uInt16 nBytes )
      62             : {
      63           0 :     if( pnDestData && pnSrcData && (nBytes > 0) )
      64             :     {
      65           0 :         if( mbValid )
      66           0 :             implDecode( pnDestData, pnSrcData, nStreamPos, nBytes );
      67             :         else
      68           0 :             memcpy( pnDestData, pnSrcData, nBytes );
      69             :     }
      70           0 : }
      71             : 
      72             : // ============================================================================
      73             : 
      74           0 : BiffDecoder_XOR::BiffDecoder_XOR( const BiffDecoder_XOR& rDecoder ) :
      75             :     BiffDecoderBase(),  // must be called to prevent compiler warning
      76             :     maCodec( ::oox::core::BinaryCodec_XOR::CODEC_EXCEL ),
      77             :     maEncryptionData( rDecoder.maEncryptionData ),
      78             :     mnKey( rDecoder.mnKey ),
      79           0 :     mnHash( rDecoder.mnHash )
      80             : {
      81           0 :     if( isValid() )
      82           0 :         maCodec.initCodec( maEncryptionData );
      83           0 : }
      84             : 
      85           0 : BiffDecoder_XOR* BiffDecoder_XOR::implClone()
      86             : {
      87           0 :     return new BiffDecoder_XOR( *this );
      88             : }
      89             : 
      90           0 : Sequence< NamedValue > BiffDecoder_XOR::implVerifyPassword( const OUString& rPassword )
      91             : {
      92           0 :     maEncryptionData.realloc( 0 );
      93             : 
      94             :     /*  Convert password to a byte string. TODO: this needs some finetuning
      95             :         according to the spec... */
      96           0 :     OString aBytePassword = OUStringToOString( rPassword, osl_getThreadTextEncoding() );
      97           0 :     sal_Int32 nLen = aBytePassword.getLength();
      98           0 :     if( (0 < nLen) && (nLen < 16) )
      99             :     {
     100             :         // init codec
     101           0 :         maCodec.initKey( reinterpret_cast< const sal_uInt8* >( aBytePassword.getStr() ) );
     102             : 
     103           0 :         if( maCodec.verifyKey( mnKey, mnHash ) )
     104           0 :             maEncryptionData = maCodec.getEncryptionData();
     105             :     }
     106             : 
     107           0 :     return maEncryptionData;
     108             : }
     109             : 
     110           0 : bool BiffDecoder_XOR::implVerifyEncryptionData( const Sequence< NamedValue >& rEncryptionData )
     111             : {
     112           0 :     maEncryptionData.realloc( 0 );
     113             : 
     114           0 :     if( rEncryptionData.hasElements() )
     115             :     {
     116             :         // init codec
     117           0 :         maCodec.initCodec( rEncryptionData );
     118             : 
     119           0 :         if( maCodec.verifyKey( mnKey, mnHash ) )
     120           0 :             maEncryptionData = rEncryptionData;
     121             :     }
     122             : 
     123           0 :     return maEncryptionData.hasElements();
     124             : }
     125             : 
     126           0 : void BiffDecoder_XOR::implDecode( sal_uInt8* pnDestData, const sal_uInt8* pnSrcData, sal_Int64 nStreamPos, sal_uInt16 nBytes )
     127             : {
     128           0 :     maCodec.startBlock();
     129           0 :     maCodec.skip( static_cast< sal_Int32 >( (nStreamPos + nBytes) & 0x0F ) );
     130           0 :     maCodec.decode( pnDestData, pnSrcData, nBytes );
     131           0 : }
     132             : 
     133             : // ============================================================================
     134             : 
     135             : namespace {
     136             : 
     137             : /** Returns the block index of the passed stream position for RCF decryption. */
     138           0 : sal_Int32 lclGetRcfBlock( sal_Int64 nStreamPos )
     139             : {
     140           0 :     return static_cast< sal_Int32 >( nStreamPos / BIFF_RCF_BLOCKSIZE );
     141             : }
     142             : 
     143             : /** Returns the offset of the passed stream position in a block for RCF decryption. */
     144           0 : sal_Int32 lclGetRcfOffset( sal_Int64 nStreamPos )
     145             : {
     146           0 :     return static_cast< sal_Int32 >( nStreamPos % BIFF_RCF_BLOCKSIZE );
     147             : }
     148             : 
     149             : } // namespace
     150             : 
     151             : // ----------------------------------------------------------------------------
     152             : 
     153           0 : BiffDecoder_RCF::BiffDecoder_RCF( const BiffDecoder_RCF& rDecoder ) :
     154             :     BiffDecoderBase(),  // must be called to prevent compiler warning
     155             :     maEncryptionData( rDecoder.maEncryptionData ),
     156             :     maSalt( rDecoder.maSalt ),
     157             :     maVerifier( rDecoder.maVerifier ),
     158           0 :     maVerifierHash( rDecoder.maVerifierHash )
     159             : {
     160           0 :     if( isValid() )
     161           0 :         maCodec.initCodec( maEncryptionData );
     162           0 : }
     163             : 
     164           0 : BiffDecoder_RCF* BiffDecoder_RCF::implClone()
     165             : {
     166           0 :     return new BiffDecoder_RCF( *this );
     167             : }
     168             : 
     169           0 : Sequence< NamedValue > BiffDecoder_RCF::implVerifyPassword( const OUString& rPassword )
     170             : {
     171           0 :     maEncryptionData.realloc( 0 );
     172             : 
     173           0 :     sal_Int32 nLen = rPassword.getLength();
     174           0 :     if( (0 < nLen) && (nLen < 16) )
     175             :     {
     176             :         // copy string to sal_uInt16 array
     177           0 :         ::std::vector< sal_uInt16 > aPassVect( 16 );
     178           0 :         const sal_Unicode* pcChar = rPassword.getStr();
     179           0 :         const sal_Unicode* pcCharEnd = pcChar + nLen;
     180           0 :         ::std::vector< sal_uInt16 >::iterator aIt = aPassVect.begin();
     181           0 :         for( ; pcChar < pcCharEnd; ++pcChar, ++aIt )
     182           0 :             *aIt = static_cast< sal_uInt16 >( *pcChar );
     183             : 
     184             :         // init codec
     185           0 :         maCodec.initKey( &aPassVect.front(), &maSalt.front() );
     186           0 :         if( maCodec.verifyKey( &maVerifier.front(), &maVerifierHash.front() ) )
     187           0 :             maEncryptionData = maCodec.getEncryptionData();
     188             :     }
     189             : 
     190           0 :     return maEncryptionData;
     191             : }
     192             : 
     193           0 : bool BiffDecoder_RCF::implVerifyEncryptionData( const Sequence< NamedValue >& rEncryptionData )
     194             : {
     195           0 :     maEncryptionData.realloc( 0 );
     196             : 
     197           0 :     if( rEncryptionData.hasElements() )
     198             :     {
     199             :         // init codec
     200           0 :         maCodec.initCodec( rEncryptionData );
     201             : 
     202           0 :         if( maCodec.verifyKey( &maVerifier.front(), &maVerifierHash.front() ) )
     203           0 :             maEncryptionData = rEncryptionData;
     204             :     }
     205             : 
     206           0 :     return maEncryptionData.hasElements();
     207             : }
     208             : 
     209           0 : void BiffDecoder_RCF::implDecode( sal_uInt8* pnDestData, const sal_uInt8* pnSrcData, sal_Int64 nStreamPos, sal_uInt16 nBytes )
     210             : {
     211           0 :     sal_uInt8* pnCurrDest = pnDestData;
     212           0 :     const sal_uInt8* pnCurrSrc = pnSrcData;
     213           0 :     sal_Int64 nCurrPos = nStreamPos;
     214           0 :     sal_uInt16 nBytesLeft = nBytes;
     215           0 :     while( nBytesLeft > 0 )
     216             :     {
     217             :         // initialize codec for current stream position
     218           0 :         maCodec.startBlock( lclGetRcfBlock( nCurrPos ) );
     219           0 :         maCodec.skip( lclGetRcfOffset( nCurrPos ) );
     220             : 
     221             :         // decode the block
     222           0 :         sal_uInt16 nBlockLeft = static_cast< sal_uInt16 >( BIFF_RCF_BLOCKSIZE - lclGetRcfOffset( nCurrPos ) );
     223           0 :         sal_uInt16 nDecBytes = ::std::min( nBytesLeft, nBlockLeft );
     224           0 :         maCodec.decode( pnCurrDest, pnCurrSrc, static_cast< sal_Int32 >( nDecBytes ) );
     225             : 
     226             :         // prepare for next block
     227           0 :         pnCurrDest += nDecBytes;
     228           0 :         pnCurrSrc += nDecBytes;
     229           0 :         nCurrPos += nDecBytes;
     230           0 :         nBytesLeft = nBytesLeft - nDecBytes;
     231             :     }
     232           0 : }
     233             : 
     234             : // ----------------------------------------------------------------------------
     235             : 
     236           0 : BiffCodecHelper::BiffCodecHelper( const WorkbookHelper& rHelper ) :
     237           0 :     WorkbookHelper( rHelper )
     238             : {
     239           0 : }
     240             : 
     241           0 : void BiffCodecHelper::cloneDecoder( BiffInputStream& rStrm )
     242             : {
     243           0 :     if( mxDecoder.get() )
     244           0 :         rStrm.setDecoder( BiffDecoderRef( mxDecoder->clone() ) );
     245           0 : }
     246             : 
     247             : // ============================================================================
     248             : 
     249             : } // namespace xls
     250          15 : } // namespace oox
     251             : 
     252             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10