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 129785 : 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 : bool bIsEncrypted,
49 : const OUString& aMediaType,
50 : bool bRecoveryMode )
51 129785 : : 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 129785 : , mbRawStream ( nStreamMode == UNBUFF_STREAM_RAW || nStreamMode == UNBUFF_STREAM_WRAPPEDRAW )
59 129785 : , mbWrappedRaw ( nStreamMode == UNBUFF_STREAM_WRAPPEDRAW )
60 : , mbFinished ( false )
61 : , mnHeaderToRead ( 0 )
62 : , mnZipCurrent ( 0 )
63 : , mnZipEnd ( 0 )
64 : , mnZipSize ( 0 )
65 : , mnMyCurrent ( 0 )
66 519140 : , mbCheckCRC( !bRecoveryMode )
67 : {
68 129785 : mnZipCurrent = maEntry.nOffset;
69 129785 : if ( mbRawStream )
70 : {
71 27343 : mnZipSize = maEntry.nMethod == DEFLATED ? maEntry.nCompressedSize : maEntry.nSize;
72 27343 : mnZipEnd = maEntry.nOffset + mnZipSize;
73 : }
74 : else
75 : {
76 102442 : mnZipSize = maEntry.nSize;
77 102442 : mnZipEnd = maEntry.nMethod == DEFLATED ? maEntry.nOffset + maEntry.nCompressedSize : maEntry.nOffset + maEntry.nSize;
78 : }
79 :
80 129785 : if (mnZipSize < 0)
81 0 : throw ZipIOException("The stream seems to be broken!");
82 :
83 129785 : bool bHaveEncryptData = ( rData.is() && rData->m_aSalt.getLength() && rData->m_aInitVector.getLength() && rData->m_nIterationCount != 0 ) ? sal_True : sal_False;
84 129785 : bool bMustDecrypt = ( nStreamMode == UNBUFF_STREAM_DATA && bHaveEncryptData && bIsEncrypted ) ? sal_True : sal_False;
85 :
86 129785 : if ( bMustDecrypt )
87 : {
88 38 : m_xCipherContext = ZipFile::StaticGetCipher( xContext, rData, false );
89 38 : mnBlockSize = ( rData->m_nEncAlg == xml::crypto::CipherID::AES_CBC_W3C_PADDING ? 16 : 1 );
90 : }
91 :
92 129785 : 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 2 : maHeader.realloc ( n_ConstHeaderSize +
100 4 : rData->m_aInitVector.getLength() +
101 4 : rData->m_aSalt.getLength() +
102 2 : rData->m_aDigest.getLength() +
103 2 : aMediaType.getLength() * sizeof( sal_Unicode ) );
104 2 : sal_Int8 * pHeader = maHeader.getArray();
105 2 : ZipFile::StaticFillHeader( rData, rEntry.nSize, aMediaType, pHeader );
106 2 : mnHeaderToRead = static_cast < sal_Int16 > ( maHeader.getLength() );
107 : }
108 129785 : }
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 ( false )
122 : , mbWrappedRaw ( false )
123 : , mbFinished ( false )
124 : , mnHeaderToRead ( 0 )
125 : , mnZipCurrent ( 0 )
126 : , mnZipEnd ( 0 )
127 : , mnZipSize ( 0 )
128 : , mnMyCurrent ( 0 )
129 0 : , mbCheckCRC( 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 259570 : XUnbufferedStream::~XUnbufferedStream()
154 : {
155 259570 : }
156 :
157 165265 : sal_Int32 SAL_CALL XUnbufferedStream::readBytes( Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
158 : throw( NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception)
159 : {
160 165265 : ::osl::MutexGuard aGuard( maMutexHolder->GetMutex() );
161 :
162 165265 : sal_Int32 nRequestedBytes = nBytesToRead;
163 : OSL_ENSURE( !mnHeaderToRead || mbWrappedRaw, "Only encrypted raw stream can be provided with header!" );
164 165265 : if ( mnMyCurrent + nRequestedBytes > mnZipSize + maHeader.getLength() )
165 153545 : nRequestedBytes = static_cast < sal_Int32 > ( mnZipSize + maHeader.getLength() - mnMyCurrent );
166 :
167 165265 : sal_Int32 nTotal = 0;
168 165265 : aData.realloc ( nRequestedBytes );
169 165265 : if ( nRequestedBytes )
170 : {
171 115279 : sal_Int32 nRead = 0;
172 115279 : sal_Int32 nLastRead = 0;
173 115279 : if ( mbRawStream )
174 : {
175 28027 : sal_Int64 nDiff = mnZipEnd - mnZipCurrent;
176 :
177 28027 : if ( mbWrappedRaw && mnHeaderToRead )
178 : {
179 : sal_Int16 nHeadRead = static_cast< sal_Int16 >(( nRequestedBytes > mnHeaderToRead ?
180 2 : mnHeaderToRead : nRequestedBytes ));
181 2 : memcpy ( aData.getArray(), maHeader.getConstArray() + maHeader.getLength() - mnHeaderToRead, nHeadRead );
182 2 : mnHeaderToRead = mnHeaderToRead - nHeadRead;
183 :
184 2 : if ( nHeadRead < nRequestedBytes )
185 : {
186 2 : sal_Int32 nToRead = nRequestedBytes - nHeadRead;
187 2 : nToRead = ( nDiff < nToRead ) ? sal::static_int_cast< sal_Int32 >( nDiff ) : nToRead;
188 :
189 2 : Sequence< sal_Int8 > aPureData( nToRead );
190 2 : mxZipSeek->seek ( mnZipCurrent );
191 2 : nRead = mxZipStream->readBytes ( aPureData, nToRead );
192 2 : mnZipCurrent += nRead;
193 :
194 2 : aPureData.realloc( nRead );
195 2 : if ( mbCheckCRC )
196 2 : maCRC.update( aPureData );
197 :
198 2 : aData.realloc( nHeadRead + nRead );
199 :
200 2 : sal_Int8* pPureBuffer = aPureData.getArray();
201 2 : sal_Int8* pBuffer = aData.getArray();
202 482 : for ( sal_Int32 nInd = 0; nInd < nRead; nInd++ )
203 482 : pBuffer[ nHeadRead + nInd ] = pPureBuffer[ nInd ];
204 : }
205 :
206 2 : nRead += nHeadRead;
207 : }
208 : else
209 : {
210 28025 : mxZipSeek->seek ( mnZipCurrent );
211 :
212 28025 : nRead = mxZipStream->readBytes (
213 : aData,
214 28025 : static_cast < sal_Int32 > ( nDiff < nRequestedBytes ? nDiff : nRequestedBytes ) );
215 :
216 28025 : mnZipCurrent += nRead;
217 :
218 28025 : aData.realloc( nRead );
219 28025 : if ( mbWrappedRaw && mbCheckCRC )
220 0 : maCRC.update( aData );
221 : }
222 : }
223 : else
224 : {
225 416758 : while ( 0 == ( nLastRead = maInflater.doInflateSegment( aData, nRead, aData.getLength() - nRead ) ) ||
226 88246 : ( nRead + nLastRead != nRequestedBytes && mnZipCurrent < mnZipEnd ) )
227 : {
228 77254 : nRead += nLastRead;
229 :
230 77254 : if ( nRead > nRequestedBytes )
231 : throw RuntimeException(
232 0 : "Should not be possible to read more than requested!" );
233 :
234 77254 : if ( maInflater.finished() || maInflater.getLastInflateError() )
235 2 : throw ZipIOException("The stream seems to be broken!" );
236 :
237 77252 : if ( maInflater.needsDictionary() )
238 0 : throw ZipIOException("Dictionaries are not supported!" );
239 :
240 77252 : sal_Int32 nDiff = static_cast< sal_Int32 >( mnZipEnd - mnZipCurrent );
241 77252 : if ( nDiff > 0 )
242 : {
243 77252 : mxZipSeek->seek ( mnZipCurrent );
244 :
245 77252 : sal_Int32 nToRead = std::max( nRequestedBytes, static_cast< sal_Int32 >( 8192 ) );
246 77252 : if ( mnBlockSize > 1 )
247 28 : nToRead = nToRead + mnBlockSize - nToRead % mnBlockSize;
248 77252 : nToRead = std::min( nDiff, nToRead );
249 :
250 77252 : sal_Int32 nZipRead = mxZipStream->readBytes( maCompBuffer, nToRead );
251 77252 : if ( nZipRead < nToRead )
252 0 : throw ZipIOException("No expected data!" );
253 :
254 77252 : mnZipCurrent += nZipRead;
255 : // maCompBuffer now has the data, check if we need to decrypt
256 : // before passing to the Inflater
257 77252 : if ( m_xCipherContext.is() )
258 : {
259 36 : if ( mbCheckCRC )
260 36 : maCRC.update( maCompBuffer );
261 :
262 36 : maCompBuffer = m_xCipherContext->convertWithCipherContext( maCompBuffer );
263 36 : if ( mnZipCurrent == mnZipEnd )
264 : {
265 36 : uno::Sequence< sal_Int8 > aSuffix = m_xCipherContext->finalizeCipherContextAndDispose();
266 36 : if ( aSuffix.getLength() )
267 : {
268 26 : sal_Int32 nOldLen = maCompBuffer.getLength();
269 26 : maCompBuffer.realloc( nOldLen + aSuffix.getLength() );
270 26 : memcpy( maCompBuffer.getArray() + nOldLen, aSuffix.getConstArray(), aSuffix.getLength() );
271 36 : }
272 : }
273 : }
274 77252 : maInflater.setInput ( maCompBuffer );
275 : }
276 : else
277 : {
278 0 : throw ZipIOException("The stream seems to be broken!" );
279 : }
280 : }
281 : }
282 :
283 115277 : mnMyCurrent += nRead + nLastRead;
284 115277 : nTotal = nRead + nLastRead;
285 115277 : if ( nTotal < nRequestedBytes)
286 0 : aData.realloc ( nTotal );
287 :
288 115277 : if ( mbCheckCRC && ( !mbRawStream || mbWrappedRaw ) )
289 : {
290 87252 : if ( !m_xCipherContext.is() && !mbWrappedRaw )
291 87214 : maCRC.update( aData );
292 :
293 87252 : if ( mnZipSize + maHeader.getLength() == mnMyCurrent && maCRC.getValue() != maEntry.nCrc )
294 0 : throw ZipIOException("The stream seems to be broken!" );
295 : }
296 : }
297 :
298 165265 : return nTotal;
299 : }
300 :
301 94454 : sal_Int32 SAL_CALL XUnbufferedStream::readSomeBytes( Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead )
302 : throw( NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception)
303 : {
304 94454 : return readBytes ( aData, nMaxBytesToRead );
305 : }
306 0 : void SAL_CALL XUnbufferedStream::skipBytes( sal_Int32 nBytesToSkip )
307 : throw( NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception)
308 : {
309 0 : if ( nBytesToSkip )
310 : {
311 0 : Sequence < sal_Int8 > aSequence ( nBytesToSkip );
312 0 : readBytes ( aSequence, nBytesToSkip );
313 : }
314 0 : }
315 :
316 18012 : sal_Int32 SAL_CALL XUnbufferedStream::available( )
317 : throw( NotConnectedException, IOException, RuntimeException, std::exception)
318 : {
319 18012 : return static_cast < sal_Int32 > ( mnZipSize - mnMyCurrent );
320 : }
321 :
322 41606 : void SAL_CALL XUnbufferedStream::closeInput( )
323 : throw( NotConnectedException, IOException, RuntimeException, std::exception)
324 : {
325 41606 : }
326 :
327 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|