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>
24 #include <oox/core/filterbase.hxx>
25 #include "biffinputstream.hxx"
30 using namespace ::com::sun::star::beans
;
31 using namespace ::com::sun::star::uno
;
33 using ::oox::core::FilterBase
;
35 BiffDecoderBase::BiffDecoderBase() :
40 BiffDecoderBase::~BiffDecoderBase()
44 ::comphelper::DocPasswordVerifierResult
BiffDecoderBase::verifyPassword( const OUString
& rPassword
, Sequence
< NamedValue
>& o_rEncryptionData
)
46 o_rEncryptionData
= implVerifyPassword( rPassword
);
47 mbValid
= o_rEncryptionData
.hasElements();
48 return mbValid
? ::comphelper::DocPasswordVerifierResult_OK
: ::comphelper::DocPasswordVerifierResult_WRONG_PASSWORD
;
51 ::comphelper::DocPasswordVerifierResult
BiffDecoderBase::verifyEncryptionData( const Sequence
< NamedValue
>& rEncryptionData
)
53 mbValid
= implVerifyEncryptionData( rEncryptionData
);
54 return mbValid
? ::comphelper::DocPasswordVerifierResult_OK
: ::comphelper::DocPasswordVerifierResult_WRONG_PASSWORD
;
57 void BiffDecoderBase::decode( sal_uInt8
* pnDestData
, const sal_uInt8
* pnSrcData
, sal_Int64 nStreamPos
, sal_uInt16 nBytes
)
59 if( pnDestData
&& pnSrcData
&& (nBytes
> 0) )
62 implDecode( pnDestData
, pnSrcData
, nStreamPos
, nBytes
);
64 memcpy( pnDestData
, pnSrcData
, nBytes
);
68 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 mnHash( rDecoder
.mnHash
)
76 maCodec
.initCodec( maEncryptionData
);
79 BiffDecoder_XOR
* BiffDecoder_XOR::implClone()
81 return new BiffDecoder_XOR( *this );
84 Sequence
< NamedValue
> BiffDecoder_XOR::implVerifyPassword( const OUString
& rPassword
)
86 maEncryptionData
.realloc( 0 );
88 /* Convert password to a byte string. TODO: this needs some finetuning
89 according to the spec... */
90 OString aBytePassword
= OUStringToOString( rPassword
, osl_getThreadTextEncoding() );
91 sal_Int32 nLen
= aBytePassword
.getLength();
92 if( (0 < nLen
) && (nLen
< 16) )
95 maCodec
.initKey( reinterpret_cast< const sal_uInt8
* >( aBytePassword
.getStr() ) );
97 if( maCodec
.verifyKey( mnKey
, mnHash
) )
98 maEncryptionData
= maCodec
.getEncryptionData();
101 return maEncryptionData
;
104 bool BiffDecoder_XOR::implVerifyEncryptionData( const Sequence
< NamedValue
>& rEncryptionData
)
106 maEncryptionData
.realloc( 0 );
108 if( rEncryptionData
.hasElements() )
111 maCodec
.initCodec( rEncryptionData
);
113 if( maCodec
.verifyKey( mnKey
, mnHash
) )
114 maEncryptionData
= rEncryptionData
;
117 return maEncryptionData
.hasElements();
120 void BiffDecoder_XOR::implDecode( sal_uInt8
* pnDestData
, const sal_uInt8
* pnSrcData
, sal_Int64 nStreamPos
, sal_uInt16 nBytes
)
122 maCodec
.startBlock();
123 maCodec
.skip( static_cast< sal_Int32
>( (nStreamPos
+ nBytes
) & 0x0F ) );
124 maCodec
.decode( pnDestData
, pnSrcData
, nBytes
);
129 /** Returns the block index of the passed stream position for RCF decryption. */
130 sal_Int32
lclGetRcfBlock( sal_Int64 nStreamPos
)
132 return static_cast< sal_Int32
>( nStreamPos
/ BIFF_RCF_BLOCKSIZE
);
135 /** Returns the offset of the passed stream position in a block for RCF decryption. */
136 sal_Int32
lclGetRcfOffset( sal_Int64 nStreamPos
)
138 return static_cast< sal_Int32
>( nStreamPos
% BIFF_RCF_BLOCKSIZE
);
143 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 maVerifierHash( rDecoder
.maVerifierHash
)
151 maCodec
.initCodec( maEncryptionData
);
154 BiffDecoder_RCF
* BiffDecoder_RCF::implClone()
156 return new BiffDecoder_RCF( *this );
159 Sequence
< NamedValue
> BiffDecoder_RCF::implVerifyPassword( const OUString
& rPassword
)
161 maEncryptionData
.realloc( 0 );
163 sal_Int32 nLen
= rPassword
.getLength();
164 if( (0 < nLen
) && (nLen
< 16) )
166 // copy string to sal_uInt16 array
167 ::std::vector
< sal_uInt16
> aPassVect( 16 );
168 const sal_Unicode
* pcChar
= rPassword
.getStr();
169 const sal_Unicode
* pcCharEnd
= pcChar
+ nLen
;
170 ::std::vector
< sal_uInt16
>::iterator aIt
= aPassVect
.begin();
171 for( ; pcChar
< pcCharEnd
; ++pcChar
, ++aIt
)
172 *aIt
= static_cast< sal_uInt16
>( *pcChar
);
175 maCodec
.initKey( &aPassVect
.front(), &maSalt
.front() );
176 if( maCodec
.verifyKey( &maVerifier
.front(), &maVerifierHash
.front() ) )
177 maEncryptionData
= maCodec
.getEncryptionData();
180 return maEncryptionData
;
183 bool BiffDecoder_RCF::implVerifyEncryptionData( const Sequence
< NamedValue
>& rEncryptionData
)
185 maEncryptionData
.realloc( 0 );
187 if( rEncryptionData
.hasElements() )
190 maCodec
.initCodec( rEncryptionData
);
192 if( maCodec
.verifyKey( &maVerifier
.front(), &maVerifierHash
.front() ) )
193 maEncryptionData
= rEncryptionData
;
196 return maEncryptionData
.hasElements();
199 void BiffDecoder_RCF::implDecode( sal_uInt8
* pnDestData
, const sal_uInt8
* pnSrcData
, sal_Int64 nStreamPos
, sal_uInt16 nBytes
)
201 sal_uInt8
* pnCurrDest
= pnDestData
;
202 const sal_uInt8
* pnCurrSrc
= pnSrcData
;
203 sal_Int64 nCurrPos
= nStreamPos
;
204 sal_uInt16 nBytesLeft
= nBytes
;
205 while( nBytesLeft
> 0 )
207 // initialize codec for current stream position
208 maCodec
.startBlock( lclGetRcfBlock( nCurrPos
) );
209 maCodec
.skip( lclGetRcfOffset( nCurrPos
) );
212 sal_uInt16 nBlockLeft
= static_cast< sal_uInt16
>( BIFF_RCF_BLOCKSIZE
- lclGetRcfOffset( nCurrPos
) );
213 sal_uInt16 nDecBytes
= ::std::min( nBytesLeft
, nBlockLeft
);
214 maCodec
.decode( pnCurrDest
, pnCurrSrc
, static_cast< sal_Int32
>( nDecBytes
) );
216 // prepare for next block
217 pnCurrDest
+= nDecBytes
;
218 pnCurrSrc
+= nDecBytes
;
219 nCurrPos
+= nDecBytes
;
220 nBytesLeft
= nBytesLeft
- nDecBytes
;
224 BiffCodecHelper::BiffCodecHelper( const WorkbookHelper
& rHelper
) :
225 WorkbookHelper( rHelper
)
229 void BiffCodecHelper::cloneDecoder( BiffInputStream
& rStrm
)
231 if( mxDecoder
.get() )
232 rStrm
.setDecoder( BiffDecoderRef( mxDecoder
->clone() ) );
238 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */