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