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

Generated by: LCOV version 1.10