1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "biffcodec.hxx"
22 #include <osl/thread.h>
23 #include <oox/helper/attributelist.hxx>
25 #include "biffinputstream.hxx"
30 using namespace ::com::sun::star::beans
;
31 using namespace ::com::sun::star::uno
;
34 BiffDecoderBase::BiffDecoderBase() :
39 BiffDecoderBase::~BiffDecoderBase()
43 ::comphelper::DocPasswordVerifierResult
BiffDecoderBase::verifyPassword( const OUString
& rPassword
, Sequence
< NamedValue
>& o_rEncryptionData
)
45 o_rEncryptionData
= implVerifyPassword( rPassword
);
46 mbValid
= o_rEncryptionData
.hasElements();
47 return mbValid
? ::comphelper::DocPasswordVerifierResult::OK
: ::comphelper::DocPasswordVerifierResult::WrongPassword
;
50 ::comphelper::DocPasswordVerifierResult
BiffDecoderBase::verifyEncryptionData( const Sequence
< NamedValue
>& rEncryptionData
)
52 mbValid
= implVerifyEncryptionData( rEncryptionData
);
53 return mbValid
? ::comphelper::DocPasswordVerifierResult::OK
: ::comphelper::DocPasswordVerifierResult::WrongPassword
;
56 void BiffDecoderBase::decode( sal_uInt8
* pnDestData
, const sal_uInt8
* pnSrcData
, sal_Int64 nStreamPos
, sal_uInt16 nBytes
)
58 if( pnDestData
&& pnSrcData
&& (nBytes
> 0) )
61 implDecode( pnDestData
, pnSrcData
, nStreamPos
, nBytes
);
63 memcpy( pnDestData
, pnSrcData
, nBytes
);
67 BiffDecoder_XOR::BiffDecoder_XOR( const BiffDecoder_XOR
& rDecoder
) :
68 BiffDecoderBase(), // must be called to prevent compiler warning
69 maCodec( ::oox::core::BinaryCodec_XOR::CODEC_EXCEL
),
70 maEncryptionData( rDecoder
.maEncryptionData
),
71 mnKey( rDecoder
.mnKey
),
72 mnHash( rDecoder
.mnHash
)
75 maCodec
.initCodec( maEncryptionData
);
78 BiffDecoder_XOR
* BiffDecoder_XOR::implClone()
80 return new BiffDecoder_XOR( *this );
83 Sequence
< NamedValue
> BiffDecoder_XOR::implVerifyPassword( const OUString
& rPassword
)
85 maEncryptionData
.realloc( 0 );
87 /* Convert password to a byte string. TODO: this needs some finetuning
88 according to the spec... */
89 OString aBytePassword
= OUStringToOString( rPassword
, osl_getThreadTextEncoding() );
90 sal_Int32 nLen
= aBytePassword
.getLength();
91 if( (0 < nLen
) && (nLen
< 16) )
94 maCodec
.initKey( reinterpret_cast< const sal_uInt8
* >( aBytePassword
.getStr() ) );
96 if( maCodec
.verifyKey( mnKey
, mnHash
) )
97 maEncryptionData
= maCodec
.getEncryptionData();
100 return maEncryptionData
;
103 bool BiffDecoder_XOR::implVerifyEncryptionData( const Sequence
< NamedValue
>& rEncryptionData
)
105 maEncryptionData
.realloc( 0 );
107 if( rEncryptionData
.hasElements() )
110 maCodec
.initCodec( rEncryptionData
);
112 if( maCodec
.verifyKey( mnKey
, mnHash
) )
113 maEncryptionData
= rEncryptionData
;
116 return maEncryptionData
.hasElements();
119 void BiffDecoder_XOR::implDecode( sal_uInt8
* pnDestData
, const sal_uInt8
* pnSrcData
, sal_Int64 nStreamPos
, sal_uInt16 nBytes
)
121 maCodec
.startBlock();
122 maCodec
.skip( static_cast< sal_Int32
>( (nStreamPos
+ nBytes
) & 0x0F ) );
123 maCodec
.decode( pnDestData
, pnSrcData
, nBytes
);
128 /** Returns the block index of the passed stream position for RCF decryption. */
129 sal_Int32
lclGetRcfBlock( sal_Int64 nStreamPos
)
131 return static_cast< sal_Int32
>( nStreamPos
/ BIFF_RCF_BLOCKSIZE
);
134 /** Returns the offset of the passed stream position in a block for RCF decryption. */
135 sal_Int32
lclGetRcfOffset( sal_Int64 nStreamPos
)
137 return static_cast< sal_Int32
>( nStreamPos
% BIFF_RCF_BLOCKSIZE
);
142 BiffDecoder_RCF::BiffDecoder_RCF( const BiffDecoder_RCF
& rDecoder
) :
143 BiffDecoderBase(), // must be called to prevent compiler warning
144 maEncryptionData( rDecoder
.maEncryptionData
),
145 maSalt( rDecoder
.maSalt
),
146 maVerifier( rDecoder
.maVerifier
),
147 maVerifierHash( rDecoder
.maVerifierHash
)
150 maCodec
.initCodec( maEncryptionData
);
153 BiffDecoder_RCF
* BiffDecoder_RCF::implClone()
155 return new BiffDecoder_RCF( *this );
158 Sequence
< NamedValue
> BiffDecoder_RCF::implVerifyPassword( const OUString
& rPassword
)
160 maEncryptionData
.realloc( 0 );
162 sal_Int32 nLen
= rPassword
.getLength();
163 if( (0 < nLen
) && (nLen
< 16) )
165 // copy string to sal_uInt16 array
166 ::std::vector
< sal_uInt16
> aPassVect( 16 );
167 const sal_Unicode
* pcChar
= rPassword
.getStr();
168 const sal_Unicode
* pcCharEnd
= pcChar
+ nLen
;
169 ::std::vector
< sal_uInt16
>::iterator aIt
= aPassVect
.begin();
170 for( ; pcChar
< pcCharEnd
; ++pcChar
, ++aIt
)
171 *aIt
= static_cast< sal_uInt16
>( *pcChar
);
174 maCodec
.initKey( &aPassVect
.front(), &maSalt
.front() );
175 if( maCodec
.verifyKey( &maVerifier
.front(), &maVerifierHash
.front() ) )
176 maEncryptionData
= maCodec
.getEncryptionData();
179 return maEncryptionData
;
182 bool BiffDecoder_RCF::implVerifyEncryptionData( const Sequence
< NamedValue
>& rEncryptionData
)
184 maEncryptionData
.realloc( 0 );
186 if( rEncryptionData
.hasElements() )
189 maCodec
.initCodec( rEncryptionData
);
191 if( maCodec
.verifyKey( &maVerifier
.front(), &maVerifierHash
.front() ) )
192 maEncryptionData
= rEncryptionData
;
195 return maEncryptionData
.hasElements();
198 void BiffDecoder_RCF::implDecode( sal_uInt8
* pnDestData
, const sal_uInt8
* pnSrcData
, sal_Int64 nStreamPos
, sal_uInt16 nBytes
)
200 sal_uInt8
* pnCurrDest
= pnDestData
;
201 const sal_uInt8
* pnCurrSrc
= pnSrcData
;
202 sal_Int64 nCurrPos
= nStreamPos
;
203 sal_uInt16 nBytesLeft
= nBytes
;
204 while( nBytesLeft
> 0 )
206 // initialize codec for current stream position
207 maCodec
.startBlock( lclGetRcfBlock( nCurrPos
) );
208 maCodec
.skip( lclGetRcfOffset( nCurrPos
) );
211 sal_uInt16 nBlockLeft
= static_cast< sal_uInt16
>( BIFF_RCF_BLOCKSIZE
- lclGetRcfOffset( nCurrPos
) );
212 sal_uInt16 nDecBytes
= ::std::min( nBytesLeft
, nBlockLeft
);
213 maCodec
.decode( pnCurrDest
, pnCurrSrc
, static_cast< sal_Int32
>( nDecBytes
) );
215 // prepare for next block
216 pnCurrDest
+= nDecBytes
;
217 pnCurrSrc
+= nDecBytes
;
218 nCurrPos
+= nDecBytes
;
219 nBytesLeft
= nBytesLeft
- nDecBytes
;
223 BiffCodecHelper::BiffCodecHelper( const WorkbookHelper
& rHelper
) :
224 WorkbookHelper( rHelper
)
228 void BiffCodecHelper::cloneDecoder( BiffInputStream
& rStrm
)
230 if( mxDecoder
.get() )
231 rStrm
.setDecoder( BiffDecoderRef( mxDecoder
->clone() ) );
237 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */