fix toolbar import
[ooovba.git] / package / source / zipapi / XUnbufferedStream.cxx
blob1ca3f29d21741baa5849286c79d12e8dd6340d26
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: XUnbufferedStream.cxx,v $
10 * $Revision: 1.14 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_package.hxx"
33 #include <XUnbufferedStream.hxx>
34 #include <EncryptionData.hxx>
35 #include <com/sun/star/packages/zip/ZipConstants.hpp>
36 #include <com/sun/star/packages/zip/ZipIOException.hpp>
37 #include <PackageConstants.hxx>
38 #include <rtl/cipher.h>
39 #include <ZipFile.hxx>
40 #include <EncryptedDataHeader.hxx>
41 #include <algorithm>
42 #include <string.h>
44 #if 0
45 // for debugging purposes here
46 #include <com/sun/star/ucb/XSimpleFileAccess.hpp>
47 #include <comphelper/processfactory.hxx>
48 using namespace ::com::sun::star;
49 #endif
51 using namespace com::sun::star::packages::zip::ZipConstants;
52 using namespace com::sun::star::io;
53 using namespace com::sun::star::uno;
54 using com::sun::star::lang::IllegalArgumentException;
55 using com::sun::star::packages::zip::ZipIOException;
56 using ::rtl::OUString;
58 XUnbufferedStream::XUnbufferedStream( ZipEntry & rEntry,
59 Reference < XInputStream > xNewZipStream,
60 const vos::ORef < EncryptionData > &rData,
61 sal_Int8 nStreamMode,
62 sal_Bool bIsEncrypted,
63 const ::rtl::OUString& aMediaType,
64 sal_Bool bRecoveryMode )
65 : mxZipStream ( xNewZipStream )
66 , mxZipSeek ( xNewZipStream, UNO_QUERY )
67 , maEntry ( rEntry )
68 , mxData ( rData )
69 , maCipher ( NULL )
70 , maInflater ( sal_True )
71 , mbRawStream ( nStreamMode == UNBUFF_STREAM_RAW || nStreamMode == UNBUFF_STREAM_WRAPPEDRAW )
72 , mbWrappedRaw ( nStreamMode == UNBUFF_STREAM_WRAPPEDRAW )
73 , mbFinished ( sal_False )
74 , mnHeaderToRead ( 0 )
75 , mnZipCurrent ( 0 )
76 , mnZipEnd ( 0 )
77 , mnZipSize ( 0 )
78 , mnMyCurrent ( 0 )
79 , mbCheckCRC( !bRecoveryMode )
81 mnZipCurrent = maEntry.nOffset;
82 if ( mbRawStream )
84 mnZipSize = maEntry.nMethod == DEFLATED ? maEntry.nCompressedSize : maEntry.nSize;
85 mnZipEnd = maEntry.nOffset + mnZipSize;
87 else
89 mnZipSize = maEntry.nSize;
90 mnZipEnd = maEntry.nMethod == DEFLATED ? maEntry.nOffset + maEntry.nCompressedSize : maEntry.nOffset + maEntry.nSize;
92 sal_Bool bHaveEncryptData = ( !rData.isEmpty() && rData->aSalt.getLength() && rData->aInitVector.getLength() && rData->nIterationCount != 0 ) ? sal_True : sal_False;
93 sal_Bool bMustDecrypt = ( nStreamMode == UNBUFF_STREAM_DATA && bHaveEncryptData && bIsEncrypted ) ? sal_True : sal_False;
95 if ( bMustDecrypt )
96 ZipFile::StaticGetCipher ( rData, maCipher );
97 if ( bHaveEncryptData && mbWrappedRaw && bIsEncrypted )
99 // if we have the data needed to decrypt it, but didn't want it decrypted (or
100 // we couldn't decrypt it due to wrong password), then we prepend this
101 // data to the stream
103 // Make a buffer big enough to hold both the header and the data itself
104 maHeader.realloc ( n_ConstHeaderSize +
105 rData->aInitVector.getLength() +
106 rData->aSalt.getLength() +
107 rData->aDigest.getLength() +
108 aMediaType.getLength() * sizeof( sal_Unicode ) );
109 sal_Int8 * pHeader = maHeader.getArray();
110 ZipFile::StaticFillHeader ( rData, rEntry.nSize, aMediaType, pHeader );
111 mnHeaderToRead = static_cast < sal_Int16 > ( maHeader.getLength() );
115 // allows to read package raw stream
116 XUnbufferedStream::XUnbufferedStream( const Reference < XInputStream >& xRawStream,
117 const vos::ORef < EncryptionData > &rData )
118 : mxZipStream ( xRawStream )
119 , mxZipSeek ( xRawStream, UNO_QUERY )
120 , mxData ( rData )
121 , maCipher ( NULL )
122 , maInflater ( sal_True )
123 , mbRawStream ( sal_False )
124 , mbWrappedRaw ( sal_False )
125 , mbFinished ( sal_False )
126 , mnHeaderToRead ( 0 )
127 , mnZipCurrent ( 0 )
128 , mnZipEnd ( 0 )
129 , mnZipSize ( 0 )
130 , mnMyCurrent ( 0 )
131 , mbCheckCRC( sal_False )
133 // for this scenario maEntry is not set !!!
134 OSL_ENSURE( mxZipSeek.is(), "The stream must be seekable!\n" );
136 // skip raw header, it must be already parsed to rData
137 mnZipCurrent = n_ConstHeaderSize + rData->aInitVector.getLength() +
138 rData->aSalt.getLength() + rData->aDigest.getLength();
140 try {
141 if ( mxZipSeek.is() )
142 mnZipSize = mxZipSeek->getLength();
143 } catch( Exception& )
145 // in case of problem the size will stay set to 0
148 mnZipEnd = mnZipCurrent + mnZipSize;
150 ZipFile::StaticGetCipher ( rData, maCipher );
153 XUnbufferedStream::~XUnbufferedStream()
155 if ( maCipher )
156 rtl_cipher_destroy ( maCipher );
159 sal_Int32 SAL_CALL XUnbufferedStream::readBytes( Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
160 throw( NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
162 sal_Int32 nRequestedBytes = nBytesToRead;
163 OSL_ENSURE( !mnHeaderToRead || mbWrappedRaw, "Only encrypted raw stream can be provided with header!" );
164 if ( mnMyCurrent + nRequestedBytes > mnZipSize + maHeader.getLength() )
165 nRequestedBytes = static_cast < sal_Int32 > ( mnZipSize + maHeader.getLength() - mnMyCurrent );
167 sal_Int32 nRead = 0, nLastRead = 0, nTotal = 0;
168 aData.realloc ( nRequestedBytes );
169 if ( nRequestedBytes )
171 if ( mbRawStream )
173 sal_Int64 nDiff = mnZipEnd - mnZipCurrent;
175 if ( mbWrappedRaw && mnHeaderToRead )
177 sal_Int16 nHeadRead = static_cast< sal_Int16 >(( nRequestedBytes > mnHeaderToRead ?
178 mnHeaderToRead : nRequestedBytes ));
179 memcpy ( aData.getArray(), maHeader.getConstArray() + maHeader.getLength() - mnHeaderToRead, nHeadRead );
180 mnHeaderToRead = mnHeaderToRead - nHeadRead;
182 if ( nHeadRead < nRequestedBytes )
184 sal_Int32 nToRead = nRequestedBytes - nHeadRead;
185 nToRead = ( nDiff < nToRead ) ? sal::static_int_cast< sal_Int32 >( nDiff ) : nToRead;
187 Sequence< sal_Int8 > aPureData( nToRead );
188 mxZipSeek->seek ( mnZipCurrent );
189 nRead = mxZipStream->readBytes ( aPureData, nToRead );
190 mnZipCurrent += nRead;
192 aPureData.realloc( nRead );
193 if ( mbCheckCRC )
194 maCRC.update( aPureData );
196 aData.realloc( nHeadRead + nRead );
198 sal_Int8* pPureBuffer = aPureData.getArray();
199 sal_Int8* pBuffer = aData.getArray();
200 for ( sal_Int32 nInd = 0; nInd < nRead; nInd++ )
201 pBuffer[ nHeadRead + nInd ] = pPureBuffer[ nInd ];
204 nRead += nHeadRead;
206 else
208 mxZipSeek->seek ( mnZipCurrent );
210 nRead = mxZipStream->readBytes (
211 aData,
212 static_cast < sal_Int32 > ( nDiff < nRequestedBytes ? nDiff : nRequestedBytes ) );
214 mnZipCurrent += nRead;
216 aData.realloc( nRead );
217 if ( mbWrappedRaw && mbCheckCRC )
218 maCRC.update( aData );
221 else
223 while ( 0 == ( nLastRead = maInflater.doInflateSegment( aData, nRead, aData.getLength() - nRead ) ) ||
224 ( nRead + nLastRead != nRequestedBytes && mnZipCurrent < mnZipEnd ) )
226 nRead += nLastRead;
228 if ( nRead > nRequestedBytes )
229 throw RuntimeException(
230 OUString( RTL_CONSTASCII_USTRINGPARAM( "Should not be possible to read more then requested!" ) ),
231 Reference< XInterface >() );
233 if ( maInflater.finished() )
234 throw ZipIOException( OUString( RTL_CONSTASCII_USTRINGPARAM( "The stream seems to be broken!" ) ),
235 Reference< XInterface >() );
237 if ( maInflater.needsDictionary() )
238 throw ZipIOException( OUString( RTL_CONSTASCII_USTRINGPARAM( "Dictionaries are not supported!" ) ),
239 Reference< XInterface >() );
241 sal_Int32 nDiff = static_cast < sal_Int32 > ( mnZipEnd - mnZipCurrent );
242 if ( nDiff > 0 )
244 mxZipSeek->seek ( mnZipCurrent );
245 sal_Int32 nToRead = std::min ( nDiff, std::max ( nRequestedBytes, static_cast< sal_Int32 >( 8192 ) ) );
246 sal_Int32 nZipRead = mxZipStream->readBytes ( maCompBuffer, nToRead );
247 mnZipCurrent += nZipRead;
248 // maCompBuffer now has the data, check if we need to decrypt
249 // before passing to the Inflater
250 if ( maCipher )
252 if ( mbCheckCRC )
253 maCRC.update( maCompBuffer );
255 Sequence < sal_Int8 > aCryptBuffer ( nZipRead );
256 rtlCipherError aResult =
257 rtl_cipher_decode ( maCipher,
258 maCompBuffer.getConstArray(),
259 nZipRead,
260 reinterpret_cast < sal_uInt8 * > (aCryptBuffer.getArray()),
261 nZipRead);
262 if( aResult != rtl_Cipher_E_None ) {
263 OSL_ASSERT (aResult == rtl_Cipher_E_None);
265 maCompBuffer = aCryptBuffer; // Now it holds the decrypted data
268 maInflater.setInput ( maCompBuffer );
270 else
272 throw ZipIOException( OUString( RTL_CONSTASCII_USTRINGPARAM( "The stream seems to be broken!" ) ),
273 Reference< XInterface >() );
278 mnMyCurrent += nRead + nLastRead;
279 nTotal = nRead + nLastRead;
280 if ( nTotal < nRequestedBytes)
281 aData.realloc ( nTotal );
283 if ( mbCheckCRC && ( !mbRawStream || mbWrappedRaw ) )
285 if ( !maCipher && !mbWrappedRaw )
286 maCRC.update( aData );
288 #if 0
289 // for debugging purposes here
290 if ( mbWrappedRaw )
292 if ( 0 )
294 uno::Reference< lang::XMultiServiceFactory > xFactory = comphelper::getProcessServiceFactory();
295 uno::Reference< ucb::XSimpleFileAccess > xAccess( xFactory->createInstance( ::rtl::OUString::createFromAscii( "com.sun.star.ucb.SimpleFileAccess" ) ), uno::UNO_QUERY );
296 uno::Reference< io::XOutputStream > xOut = xAccess->openFileWrite( ::rtl::OUString::createFromAscii( "file:///d:/777/Encrypted/picture" ) );
297 xOut->writeBytes( aData );
298 xOut->closeOutput();
301 #endif
303 if ( mnZipSize + maHeader.getLength() == mnMyCurrent && maCRC.getValue() != maEntry.nCrc )
304 throw ZipIOException( OUString( RTL_CONSTASCII_USTRINGPARAM( "The stream seems to be broken!" ) ),
305 Reference< XInterface >() );
309 return nTotal;
312 sal_Int32 SAL_CALL XUnbufferedStream::readSomeBytes( Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead )
313 throw( NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
315 return readBytes ( aData, nMaxBytesToRead );
317 void SAL_CALL XUnbufferedStream::skipBytes( sal_Int32 nBytesToSkip )
318 throw( NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
320 if ( nBytesToSkip )
322 Sequence < sal_Int8 > aSequence ( nBytesToSkip );
323 readBytes ( aSequence, nBytesToSkip );
327 sal_Int32 SAL_CALL XUnbufferedStream::available( )
328 throw( NotConnectedException, IOException, RuntimeException)
330 return static_cast < sal_Int32 > ( mnZipSize - mnMyCurrent );
333 void SAL_CALL XUnbufferedStream::closeInput( )
334 throw( NotConnectedException, IOException, RuntimeException)
338 void SAL_CALL XUnbufferedStream::seek( sal_Int64 location )
339 throw( IllegalArgumentException, IOException, RuntimeException)
342 sal_Int64 SAL_CALL XUnbufferedStream::getPosition( )
343 throw(IOException, RuntimeException)
345 return mnMyCurrent;
347 sal_Int64 SAL_CALL XUnbufferedStream::getLength( )
348 throw(IOException, RuntimeException)
350 return mnZipSize;