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 <com/sun/star/packages/zip/ZipConstants.hpp>
21 : #include <com/sun/star/packages/zip/ZipIOException.hpp>
22 : #include <com/sun/star/xml/crypto/CipherID.hpp>
23 :
24 : #include <XUnbufferedStream.hxx>
25 : #include <EncryptionData.hxx>
26 : #include <PackageConstants.hxx>
27 : #include <ZipFile.hxx>
28 : #include <EncryptedDataHeader.hxx>
29 : #include <algorithm>
30 : #include <string.h>
31 :
32 : #include <osl/mutex.hxx>
33 :
34 : using namespace ::com::sun::star;
35 : using namespace com::sun::star::packages::zip::ZipConstants;
36 : using namespace com::sun::star::io;
37 : using namespace com::sun::star::uno;
38 : using com::sun::star::lang::IllegalArgumentException;
39 : using com::sun::star::packages::zip::ZipIOException;
40 :
41 0 : XUnbufferedStream::XUnbufferedStream(
42 : const uno::Reference< uno::XComponentContext >& xContext,
43 : SotMutexHolderRef aMutexHolder,
44 : ZipEntry & rEntry,
45 : Reference < XInputStream > xNewZipStream,
46 : const ::rtl::Reference< EncryptionData >& rData,
47 : sal_Int8 nStreamMode,
48 : sal_Bool bIsEncrypted,
49 : const OUString& aMediaType,
50 : sal_Bool bRecoveryMode )
51 0 : : maMutexHolder( aMutexHolder.Is() ? aMutexHolder : SotMutexHolderRef( new SotMutexHolder ) )
52 : , mxZipStream ( xNewZipStream )
53 : , mxZipSeek ( xNewZipStream, UNO_QUERY )
54 : , maEntry ( rEntry )
55 : , mxData ( rData )
56 : , mnBlockSize( 1 )
57 : , maInflater ( true )
58 0 : , mbRawStream ( nStreamMode == UNBUFF_STREAM_RAW || nStreamMode == UNBUFF_STREAM_WRAPPEDRAW )
59 0 : , mbWrappedRaw ( nStreamMode == UNBUFF_STREAM_WRAPPEDRAW )
60 : , mbFinished ( sal_False )
61 : , mnHeaderToRead ( 0 )
62 : , mnZipCurrent ( 0 )
63 : , mnZipEnd ( 0 )
64 : , mnZipSize ( 0 )
65 : , mnMyCurrent ( 0 )
66 0 : , mbCheckCRC( !bRecoveryMode )
67 : {
68 0 : mnZipCurrent = maEntry.nOffset;
69 0 : if ( mbRawStream )
70 : {
71 0 : mnZipSize = maEntry.nMethod == DEFLATED ? maEntry.nCompressedSize : maEntry.nSize;
72 0 : mnZipEnd = maEntry.nOffset + mnZipSize;
73 : }
74 : else
75 : {
76 0 : mnZipSize = maEntry.nSize;
77 0 : mnZipEnd = maEntry.nMethod == DEFLATED ? maEntry.nOffset + maEntry.nCompressedSize : maEntry.nOffset + maEntry.nSize;
78 : }
79 :
80 0 : if (mnZipSize < 0)
81 0 : throw ZipIOException("The stream seems to be broken!", uno::Reference< XInterface >());
82 :
83 0 : sal_Bool bHaveEncryptData = ( rData.is() && rData->m_aSalt.getLength() && rData->m_aInitVector.getLength() && rData->m_nIterationCount != 0 ) ? sal_True : sal_False;
84 0 : sal_Bool bMustDecrypt = ( nStreamMode == UNBUFF_STREAM_DATA && bHaveEncryptData && bIsEncrypted ) ? sal_True : sal_False;
85 :
86 0 : if ( bMustDecrypt )
87 : {
88 0 : m_xCipherContext = ZipFile::StaticGetCipher( xContext, rData, false );
89 0 : mnBlockSize = ( rData->m_nEncAlg == xml::crypto::CipherID::AES_CBC_W3C_PADDING ? 16 : 1 );
90 : }
91 :
92 0 : if ( bHaveEncryptData && mbWrappedRaw && bIsEncrypted )
93 : {
94 : // if we have the data needed to decrypt it, but didn't want it decrypted (or
95 : // we couldn't decrypt it due to wrong password), then we prepend this
96 : // data to the stream
97 :
98 : // Make a buffer big enough to hold both the header and the data itself
99 0 : maHeader.realloc ( n_ConstHeaderSize +
100 0 : rData->m_aInitVector.getLength() +
101 0 : rData->m_aSalt.getLength() +
102 0 : rData->m_aDigest.getLength() +
103 0 : aMediaType.getLength() * sizeof( sal_Unicode ) );
104 0 : sal_Int8 * pHeader = maHeader.getArray();
105 0 : ZipFile::StaticFillHeader( rData, rEntry.nSize, aMediaType, pHeader );
106 0 : mnHeaderToRead = static_cast < sal_Int16 > ( maHeader.getLength() );
107 : }
108 0 : }
109 :
110 : // allows to read package raw stream
111 0 : XUnbufferedStream::XUnbufferedStream(
112 : const uno::Reference< uno::XComponentContext >& /*xContext*/,
113 : const Reference < XInputStream >& xRawStream,
114 : const ::rtl::Reference< EncryptionData >& rData )
115 0 : : maMutexHolder( new SotMutexHolder )
116 : , mxZipStream ( xRawStream )
117 : , mxZipSeek ( xRawStream, UNO_QUERY )
118 : , mxData ( rData )
119 : , mnBlockSize( 1 )
120 : , maInflater ( true )
121 : , mbRawStream ( sal_False )
122 : , mbWrappedRaw ( sal_False )
123 : , mbFinished ( sal_False )
124 : , mnHeaderToRead ( 0 )
125 : , mnZipCurrent ( 0 )
126 : , mnZipEnd ( 0 )
127 : , mnZipSize ( 0 )
128 : , mnMyCurrent ( 0 )
129 0 : , mbCheckCRC( sal_False )
130 : {
131 : // for this scenario maEntry is not set !!!
132 : OSL_ENSURE( mxZipSeek.is(), "The stream must be seekable!\n" );
133 :
134 : // skip raw header, it must be already parsed to rData
135 0 : mnZipCurrent = n_ConstHeaderSize + rData->m_aInitVector.getLength() +
136 0 : rData->m_aSalt.getLength() + rData->m_aDigest.getLength();
137 :
138 : try {
139 0 : if ( mxZipSeek.is() )
140 0 : mnZipSize = mxZipSeek->getLength();
141 0 : } catch( Exception& e )
142 : {
143 : // in case of problem the size will stay set to 0
144 : SAL_WARN("package", "ignoring Exception " + e.Message);
145 : }
146 :
147 0 : mnZipEnd = mnZipCurrent + mnZipSize;
148 :
149 : // the raw data will not be decrypted, no need for the cipher
150 : // m_xCipherContext = ZipFile::StaticGetCipher( xContext, rData, false );
151 0 : }
152 :
153 0 : XUnbufferedStream::~XUnbufferedStream()
154 : {
155 0 : }
156 :
157 0 : sal_Int32 SAL_CALL XUnbufferedStream::readBytes( Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
158 : throw( NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception)
159 : {
160 0 : ::osl::MutexGuard aGuard( maMutexHolder->GetMutex() );
161 :
162 0 : sal_Int32 nRequestedBytes = nBytesToRead;
163 : OSL_ENSURE( !mnHeaderToRead || mbWrappedRaw, "Only encrypted raw stream can be provided with header!" );
164 0 : if ( mnMyCurrent + nRequestedBytes > mnZipSize + maHeader.getLength() )
165 0 : nRequestedBytes = static_cast < sal_Int32 > ( mnZipSize + maHeader.getLength() - mnMyCurrent );
166 :
167 0 : sal_Int32 nTotal = 0;
168 0 : aData.realloc ( nRequestedBytes );
169 0 : if ( nRequestedBytes )
170 : {
171 0 : sal_Int32 nRead = 0;
172 0 : sal_Int32 nLastRead = 0;
173 0 : if ( mbRawStream )
174 : {
175 0 : sal_Int64 nDiff = mnZipEnd - mnZipCurrent;
176 :
177 0 : if ( mbWrappedRaw && mnHeaderToRead )
178 : {
179 : sal_Int16 nHeadRead = static_cast< sal_Int16 >(( nRequestedBytes > mnHeaderToRead ?
180 0 : mnHeaderToRead : nRequestedBytes ));
181 0 : memcpy ( aData.getArray(), maHeader.getConstArray() + maHeader.getLength() - mnHeaderToRead, nHeadRead );
182 0 : mnHeaderToRead = mnHeaderToRead - nHeadRead;
183 :
184 0 : if ( nHeadRead < nRequestedBytes )
185 : {
186 0 : sal_Int32 nToRead = nRequestedBytes - nHeadRead;
187 0 : nToRead = ( nDiff < nToRead ) ? sal::static_int_cast< sal_Int32 >( nDiff ) : nToRead;
188 :
189 0 : Sequence< sal_Int8 > aPureData( nToRead );
190 0 : mxZipSeek->seek ( mnZipCurrent );
191 0 : nRead = mxZipStream->readBytes ( aPureData, nToRead );
192 0 : mnZipCurrent += nRead;
193 :
194 0 : aPureData.realloc( nRead );
195 0 : if ( mbCheckCRC )
196 0 : maCRC.update( aPureData );
197 :
198 0 : aData.realloc( nHeadRead + nRead );
199 :
200 0 : sal_Int8* pPureBuffer = aPureData.getArray();
201 0 : sal_Int8* pBuffer = aData.getArray();
202 0 : for ( sal_Int32 nInd = 0; nInd < nRead; nInd++ )
203 0 : pBuffer[ nHeadRead + nInd ] = pPureBuffer[ nInd ];
204 : }
205 :
206 0 : nRead += nHeadRead;
207 : }
208 : else
209 : {
210 0 : mxZipSeek->seek ( mnZipCurrent );
211 :
212 0 : nRead = mxZipStream->readBytes (
213 : aData,
214 0 : static_cast < sal_Int32 > ( nDiff < nRequestedBytes ? nDiff : nRequestedBytes ) );
215 :
216 0 : mnZipCurrent += nRead;
217 :
218 0 : aData.realloc( nRead );
219 0 : if ( mbWrappedRaw && mbCheckCRC )
220 0 : maCRC.update( aData );
221 : }
222 : }
223 : else
224 : {
225 0 : while ( 0 == ( nLastRead = maInflater.doInflateSegment( aData, nRead, aData.getLength() - nRead ) ) ||
226 0 : ( nRead + nLastRead != nRequestedBytes && mnZipCurrent < mnZipEnd ) )
227 : {
228 0 : nRead += nLastRead;
229 :
230 0 : if ( nRead > nRequestedBytes )
231 : throw RuntimeException(
232 : "Should not be possible to read more then requested!",
233 0 : Reference< XInterface >() );
234 :
235 0 : if ( maInflater.finished() || maInflater.getLastInflateError() )
236 : throw ZipIOException("The stream seems to be broken!",
237 0 : Reference< XInterface >() );
238 :
239 0 : if ( maInflater.needsDictionary() )
240 : throw ZipIOException("Dictionaries are not supported!",
241 0 : Reference< XInterface >() );
242 :
243 0 : sal_Int32 nDiff = static_cast< sal_Int32 >( mnZipEnd - mnZipCurrent );
244 0 : if ( nDiff > 0 )
245 : {
246 0 : mxZipSeek->seek ( mnZipCurrent );
247 :
248 0 : sal_Int32 nToRead = std::max( nRequestedBytes, static_cast< sal_Int32 >( 8192 ) );
249 0 : if ( mnBlockSize > 1 )
250 0 : nToRead = nToRead + mnBlockSize - nToRead % mnBlockSize;
251 0 : nToRead = std::min( nDiff, nToRead );
252 :
253 0 : sal_Int32 nZipRead = mxZipStream->readBytes( maCompBuffer, nToRead );
254 0 : if ( nZipRead < nToRead )
255 : throw ZipIOException("No expected data!",
256 0 : Reference< XInterface >() );
257 :
258 0 : mnZipCurrent += nZipRead;
259 : // maCompBuffer now has the data, check if we need to decrypt
260 : // before passing to the Inflater
261 0 : if ( m_xCipherContext.is() )
262 : {
263 0 : if ( mbCheckCRC )
264 0 : maCRC.update( maCompBuffer );
265 :
266 0 : maCompBuffer = m_xCipherContext->convertWithCipherContext( maCompBuffer );
267 0 : if ( mnZipCurrent == mnZipEnd )
268 : {
269 0 : uno::Sequence< sal_Int8 > aSuffix = m_xCipherContext->finalizeCipherContextAndDispose();
270 0 : if ( aSuffix.getLength() )
271 : {
272 0 : sal_Int32 nOldLen = maCompBuffer.getLength();
273 0 : maCompBuffer.realloc( nOldLen + aSuffix.getLength() );
274 0 : memcpy( maCompBuffer.getArray() + nOldLen, aSuffix.getConstArray(), aSuffix.getLength() );
275 0 : }
276 : }
277 : }
278 0 : maInflater.setInput ( maCompBuffer );
279 : }
280 : else
281 : {
282 : throw ZipIOException("The stream seems to be broken!",
283 0 : Reference< XInterface >() );
284 : }
285 : }
286 : }
287 :
288 0 : mnMyCurrent += nRead + nLastRead;
289 0 : nTotal = nRead + nLastRead;
290 0 : if ( nTotal < nRequestedBytes)
291 0 : aData.realloc ( nTotal );
292 :
293 0 : if ( mbCheckCRC && ( !mbRawStream || mbWrappedRaw ) )
294 : {
295 0 : if ( !m_xCipherContext.is() && !mbWrappedRaw )
296 0 : maCRC.update( aData );
297 :
298 0 : if ( mnZipSize + maHeader.getLength() == mnMyCurrent && maCRC.getValue() != maEntry.nCrc )
299 : throw ZipIOException("The stream seems to be broken!",
300 0 : Reference< XInterface >() );
301 : }
302 : }
303 :
304 0 : return nTotal;
305 : }
306 :
307 0 : sal_Int32 SAL_CALL XUnbufferedStream::readSomeBytes( Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead )
308 : throw( NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception)
309 : {
310 0 : return readBytes ( aData, nMaxBytesToRead );
311 : }
312 0 : void SAL_CALL XUnbufferedStream::skipBytes( sal_Int32 nBytesToSkip )
313 : throw( NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception)
314 : {
315 0 : if ( nBytesToSkip )
316 : {
317 0 : Sequence < sal_Int8 > aSequence ( nBytesToSkip );
318 0 : readBytes ( aSequence, nBytesToSkip );
319 : }
320 0 : }
321 :
322 0 : sal_Int32 SAL_CALL XUnbufferedStream::available( )
323 : throw( NotConnectedException, IOException, RuntimeException, std::exception)
324 : {
325 0 : return static_cast < sal_Int32 > ( mnZipSize - mnMyCurrent );
326 : }
327 :
328 0 : void SAL_CALL XUnbufferedStream::closeInput( )
329 : throw( NotConnectedException, IOException, RuntimeException, std::exception)
330 : {
331 0 : }
332 :
333 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|