Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : #include "biffcodec.hxx"
30 : :
31 : : #include <osl/thread.h>
32 : : #include <string.h>
33 : : #include "oox/core/filterbase.hxx"
34 : : #include "biffinputstream.hxx"
35 : :
36 : : namespace oox {
37 : : namespace xls {
38 : :
39 : : // ============================================================================
40 : :
41 : : using namespace ::com::sun::star::beans;
42 : : using namespace ::com::sun::star::uno;
43 : :
44 : : using ::oox::core::FilterBase;
45 : : using ::rtl::OString;
46 : : using ::rtl::OUString;
47 : : using ::rtl::OStringToOUString;
48 : :
49 : : // ============================================================================
50 : :
51 : 0 : BiffDecoderBase::BiffDecoderBase() :
52 : 0 : mbValid( false )
53 : : {
54 : 0 : }
55 : :
56 : 0 : BiffDecoderBase::~BiffDecoderBase()
57 : : {
58 [ # # ]: 0 : }
59 : :
60 : 0 : ::comphelper::DocPasswordVerifierResult BiffDecoderBase::verifyPassword( const OUString& rPassword, Sequence< NamedValue >& o_rEncryptionData )
61 : : {
62 [ # # ]: 0 : o_rEncryptionData = implVerifyPassword( rPassword );
63 : 0 : mbValid = o_rEncryptionData.hasElements();
64 [ # # ]: 0 : return mbValid ? ::comphelper::DocPasswordVerifierResult_OK : ::comphelper::DocPasswordVerifierResult_WRONG_PASSWORD;
65 : : }
66 : :
67 : 0 : ::comphelper::DocPasswordVerifierResult BiffDecoderBase::verifyEncryptionData( const Sequence< NamedValue >& rEncryptionData )
68 : : {
69 : 0 : mbValid = implVerifyEncryptionData( rEncryptionData );
70 [ # # ]: 0 : return mbValid ? ::comphelper::DocPasswordVerifierResult_OK : ::comphelper::DocPasswordVerifierResult_WRONG_PASSWORD;
71 : : }
72 : :
73 : 0 : void BiffDecoderBase::decode( sal_uInt8* pnDestData, const sal_uInt8* pnSrcData, sal_Int64 nStreamPos, sal_uInt16 nBytes )
74 : : {
75 [ # # ][ # # ]: 0 : if( pnDestData && pnSrcData && (nBytes > 0) )
[ # # ]
76 : : {
77 [ # # ]: 0 : if( mbValid )
78 : 0 : implDecode( pnDestData, pnSrcData, nStreamPos, nBytes );
79 : : else
80 : 0 : memcpy( pnDestData, pnSrcData, nBytes );
81 : : }
82 : 0 : }
83 : :
84 : : // ============================================================================
85 : :
86 : 0 : BiffDecoder_XOR::BiffDecoder_XOR( const BiffDecoder_XOR& rDecoder ) :
87 : : BiffDecoderBase(), // must be called to prevent compiler warning
88 : : maCodec( ::oox::core::BinaryCodec_XOR::CODEC_EXCEL ),
89 : : maEncryptionData( rDecoder.maEncryptionData ),
90 : : mnKey( rDecoder.mnKey ),
91 [ # # ][ # # ]: 0 : mnHash( rDecoder.mnHash )
92 : : {
93 [ # # ]: 0 : if( isValid() )
94 [ # # ]: 0 : maCodec.initCodec( maEncryptionData );
95 : 0 : }
96 : :
97 : 0 : BiffDecoder_XOR* BiffDecoder_XOR::implClone()
98 : : {
99 [ # # ]: 0 : return new BiffDecoder_XOR( *this );
100 : : }
101 : :
102 : 0 : Sequence< NamedValue > BiffDecoder_XOR::implVerifyPassword( const OUString& rPassword )
103 : : {
104 [ # # ]: 0 : maEncryptionData.realloc( 0 );
105 : :
106 : : /* Convert password to a byte string. TODO: this needs some finetuning
107 : : according to the spec... */
108 [ # # ][ # # ]: 0 : OString aBytePassword = OUStringToOString( rPassword, osl_getThreadTextEncoding() );
109 : 0 : sal_Int32 nLen = aBytePassword.getLength();
110 [ # # ][ # # ]: 0 : if( (0 < nLen) && (nLen < 16) )
111 : : {
112 : : // init codec
113 [ # # ]: 0 : maCodec.initKey( reinterpret_cast< const sal_uInt8* >( aBytePassword.getStr() ) );
114 : :
115 [ # # ][ # # ]: 0 : if( maCodec.verifyKey( mnKey, mnHash ) )
116 [ # # ][ # # ]: 0 : maEncryptionData = maCodec.getEncryptionData();
[ # # ]
117 : : }
118 : :
119 [ # # ]: 0 : return maEncryptionData;
120 : : }
121 : :
122 : 0 : bool BiffDecoder_XOR::implVerifyEncryptionData( const Sequence< NamedValue >& rEncryptionData )
123 : : {
124 : 0 : maEncryptionData.realloc( 0 );
125 : :
126 [ # # ]: 0 : if( rEncryptionData.hasElements() )
127 : : {
128 : : // init codec
129 : 0 : maCodec.initCodec( rEncryptionData );
130 : :
131 [ # # ]: 0 : if( maCodec.verifyKey( mnKey, mnHash ) )
132 : 0 : maEncryptionData = rEncryptionData;
133 : : }
134 : :
135 : 0 : return maEncryptionData.hasElements();
136 : : }
137 : :
138 : 0 : void BiffDecoder_XOR::implDecode( sal_uInt8* pnDestData, const sal_uInt8* pnSrcData, sal_Int64 nStreamPos, sal_uInt16 nBytes )
139 : : {
140 : 0 : maCodec.startBlock();
141 : 0 : maCodec.skip( static_cast< sal_Int32 >( (nStreamPos + nBytes) & 0x0F ) );
142 : 0 : maCodec.decode( pnDestData, pnSrcData, nBytes );
143 : 0 : }
144 : :
145 : : // ============================================================================
146 : :
147 : : namespace {
148 : :
149 : : /** Returns the block index of the passed stream position for RCF decryption. */
150 : 0 : sal_Int32 lclGetRcfBlock( sal_Int64 nStreamPos )
151 : : {
152 : 0 : return static_cast< sal_Int32 >( nStreamPos / BIFF_RCF_BLOCKSIZE );
153 : : }
154 : :
155 : : /** Returns the offset of the passed stream position in a block for RCF decryption. */
156 : 0 : sal_Int32 lclGetRcfOffset( sal_Int64 nStreamPos )
157 : : {
158 : 0 : return static_cast< sal_Int32 >( nStreamPos % BIFF_RCF_BLOCKSIZE );
159 : : }
160 : :
161 : : } // namespace
162 : :
163 : : // ----------------------------------------------------------------------------
164 : :
165 : 0 : BiffDecoder_RCF::BiffDecoder_RCF( const BiffDecoder_RCF& rDecoder ) :
166 : : BiffDecoderBase(), // must be called to prevent compiler warning
167 : : maEncryptionData( rDecoder.maEncryptionData ),
168 : : maSalt( rDecoder.maSalt ),
169 : : maVerifier( rDecoder.maVerifier ),
170 [ # # ][ # # ]: 0 : maVerifierHash( rDecoder.maVerifierHash )
[ # # ][ # # ]
[ # # ]
171 : : {
172 [ # # ]: 0 : if( isValid() )
173 [ # # ]: 0 : maCodec.initCodec( maEncryptionData );
174 : 0 : }
175 : :
176 : 0 : BiffDecoder_RCF* BiffDecoder_RCF::implClone()
177 : : {
178 [ # # ]: 0 : return new BiffDecoder_RCF( *this );
179 : : }
180 : :
181 : 0 : Sequence< NamedValue > BiffDecoder_RCF::implVerifyPassword( const OUString& rPassword )
182 : : {
183 : 0 : maEncryptionData.realloc( 0 );
184 : :
185 : 0 : sal_Int32 nLen = rPassword.getLength();
186 [ # # ][ # # ]: 0 : if( (0 < nLen) && (nLen < 16) )
187 : : {
188 : : // copy string to sal_uInt16 array
189 [ # # ]: 0 : ::std::vector< sal_uInt16 > aPassVect( 16 );
190 : 0 : const sal_Unicode* pcChar = rPassword.getStr();
191 : 0 : const sal_Unicode* pcCharEnd = pcChar + nLen;
192 : 0 : ::std::vector< sal_uInt16 >::iterator aIt = aPassVect.begin();
193 [ # # ][ # # ]: 0 : for( ; pcChar < pcCharEnd; ++pcChar, ++aIt )
194 [ # # ]: 0 : *aIt = static_cast< sal_uInt16 >( *pcChar );
195 : :
196 : : // init codec
197 [ # # ][ # # ]: 0 : maCodec.initKey( &aPassVect.front(), &maSalt.front() );
[ # # ]
198 [ # # ][ # # ]: 0 : if( maCodec.verifyKey( &maVerifier.front(), &maVerifierHash.front() ) )
[ # # ][ # # ]
199 [ # # ][ # # ]: 0 : maEncryptionData = maCodec.getEncryptionData();
[ # # ]
200 : : }
201 : :
202 : 0 : return maEncryptionData;
203 : : }
204 : :
205 : 0 : bool BiffDecoder_RCF::implVerifyEncryptionData( const Sequence< NamedValue >& rEncryptionData )
206 : : {
207 : 0 : maEncryptionData.realloc( 0 );
208 : :
209 [ # # ]: 0 : if( rEncryptionData.hasElements() )
210 : : {
211 : : // init codec
212 : 0 : maCodec.initCodec( rEncryptionData );
213 : :
214 [ # # ]: 0 : if( maCodec.verifyKey( &maVerifier.front(), &maVerifierHash.front() ) )
215 : 0 : maEncryptionData = rEncryptionData;
216 : : }
217 : :
218 : 0 : return maEncryptionData.hasElements();
219 : : }
220 : :
221 : 0 : void BiffDecoder_RCF::implDecode( sal_uInt8* pnDestData, const sal_uInt8* pnSrcData, sal_Int64 nStreamPos, sal_uInt16 nBytes )
222 : : {
223 : 0 : sal_uInt8* pnCurrDest = pnDestData;
224 : 0 : const sal_uInt8* pnCurrSrc = pnSrcData;
225 : 0 : sal_Int64 nCurrPos = nStreamPos;
226 : 0 : sal_uInt16 nBytesLeft = nBytes;
227 [ # # ]: 0 : while( nBytesLeft > 0 )
228 : : {
229 : : // initialize codec for current stream position
230 [ # # ]: 0 : maCodec.startBlock( lclGetRcfBlock( nCurrPos ) );
231 [ # # ]: 0 : maCodec.skip( lclGetRcfOffset( nCurrPos ) );
232 : :
233 : : // decode the block
234 : 0 : sal_uInt16 nBlockLeft = static_cast< sal_uInt16 >( BIFF_RCF_BLOCKSIZE - lclGetRcfOffset( nCurrPos ) );
235 [ # # ]: 0 : sal_uInt16 nDecBytes = ::std::min( nBytesLeft, nBlockLeft );
236 [ # # ]: 0 : maCodec.decode( pnCurrDest, pnCurrSrc, static_cast< sal_Int32 >( nDecBytes ) );
237 : :
238 : : // prepare for next block
239 : 0 : pnCurrDest += nDecBytes;
240 : 0 : pnCurrSrc += nDecBytes;
241 : 0 : nCurrPos += nDecBytes;
242 : 0 : nBytesLeft = nBytesLeft - nDecBytes;
243 : : }
244 : 0 : }
245 : :
246 : : // ----------------------------------------------------------------------------
247 : :
248 : 0 : BiffCodecHelper::BiffCodecHelper( const WorkbookHelper& rHelper ) :
249 [ # # ]: 0 : WorkbookHelper( rHelper )
250 : : {
251 : 0 : }
252 : :
253 : 0 : void BiffCodecHelper::cloneDecoder( BiffInputStream& rStrm )
254 : : {
255 [ # # ]: 0 : if( mxDecoder.get() )
256 [ # # ]: 0 : rStrm.setDecoder( BiffDecoderRef( mxDecoder->clone() ) );
257 : 0 : }
258 : :
259 : : // ============================================================================
260 : :
261 : : } // namespace xls
262 [ + - ][ + - ]: 24 : } // namespace oox
263 : :
264 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|