xref: /AOO41X/main/package/source/zipapi/ZipFile.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_package.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
32*cdf0e10cSrcweir #include <com/sun/star/ucb/XProgressHandler.hpp>
33*cdf0e10cSrcweir #include <com/sun/star/packages/zip/ZipConstants.hpp>
34*cdf0e10cSrcweir #include <com/sun/star/xml/crypto/XCipherContext.hpp>
35*cdf0e10cSrcweir #include <com/sun/star/xml/crypto/XDigestContext.hpp>
36*cdf0e10cSrcweir #include <com/sun/star/xml/crypto/XCipherContextSupplier.hpp>
37*cdf0e10cSrcweir #include <com/sun/star/xml/crypto/XDigestContextSupplier.hpp>
38*cdf0e10cSrcweir #include <com/sun/star/xml/crypto/CipherID.hpp>
39*cdf0e10cSrcweir #include <com/sun/star/xml/crypto/DigestID.hpp>
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir #include <comphelper/storagehelper.hxx>
42*cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
43*cdf0e10cSrcweir #include <rtl/digest.h>
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir #include <vector>
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir #include "blowfishcontext.hxx"
48*cdf0e10cSrcweir #include "sha1context.hxx"
49*cdf0e10cSrcweir #include <ZipFile.hxx>
50*cdf0e10cSrcweir #include <ZipEnumeration.hxx>
51*cdf0e10cSrcweir #include <XUnbufferedStream.hxx>
52*cdf0e10cSrcweir #include <PackageConstants.hxx>
53*cdf0e10cSrcweir #include <EncryptedDataHeader.hxx>
54*cdf0e10cSrcweir #include <EncryptionData.hxx>
55*cdf0e10cSrcweir #include <MemoryByteGrabber.hxx>
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir #include <CRC32.hxx>
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir #define AES_CBC_BLOCK_SIZE 16
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir using namespace vos;
62*cdf0e10cSrcweir using namespace rtl;
63*cdf0e10cSrcweir using namespace com::sun::star;
64*cdf0e10cSrcweir using namespace com::sun::star::io;
65*cdf0e10cSrcweir using namespace com::sun::star::uno;
66*cdf0e10cSrcweir using namespace com::sun::star::ucb;
67*cdf0e10cSrcweir using namespace com::sun::star::lang;
68*cdf0e10cSrcweir using namespace com::sun::star::packages;
69*cdf0e10cSrcweir using namespace com::sun::star::packages::zip;
70*cdf0e10cSrcweir using namespace com::sun::star::packages::zip::ZipConstants;
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir /** This class is used to read entries from a zip file
74*cdf0e10cSrcweir  */
75*cdf0e10cSrcweir ZipFile::ZipFile( uno::Reference < XInputStream > &xInput, const uno::Reference < XMultiServiceFactory > &xNewFactory, sal_Bool bInitialise )
76*cdf0e10cSrcweir 	throw(IOException, ZipException, RuntimeException)
77*cdf0e10cSrcweir : aGrabber(xInput)
78*cdf0e10cSrcweir , aInflater (sal_True)
79*cdf0e10cSrcweir , xStream(xInput)
80*cdf0e10cSrcweir , xSeek(xInput, UNO_QUERY)
81*cdf0e10cSrcweir , m_xFactory ( xNewFactory )
82*cdf0e10cSrcweir , bRecoveryMode( sal_False )
83*cdf0e10cSrcweir {
84*cdf0e10cSrcweir 	if (bInitialise)
85*cdf0e10cSrcweir 	{
86*cdf0e10cSrcweir 		if ( readCEN() == -1 )
87*cdf0e10cSrcweir 		{
88*cdf0e10cSrcweir 			aEntries.clear();
89*cdf0e10cSrcweir 			throw ZipException( OUString( RTL_CONSTASCII_USTRINGPARAM ( "stream data looks to be broken" ) ), uno::Reference < XInterface > () );
90*cdf0e10cSrcweir 		}
91*cdf0e10cSrcweir 	}
92*cdf0e10cSrcweir }
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir ZipFile::ZipFile( uno::Reference < XInputStream > &xInput, const uno::Reference < XMultiServiceFactory > &xNewFactory, sal_Bool bInitialise, sal_Bool bForceRecovery, uno::Reference < XProgressHandler > xProgress )
97*cdf0e10cSrcweir 	throw(IOException, ZipException, RuntimeException)
98*cdf0e10cSrcweir : aGrabber(xInput)
99*cdf0e10cSrcweir , aInflater (sal_True)
100*cdf0e10cSrcweir , xStream(xInput)
101*cdf0e10cSrcweir , xSeek(xInput, UNO_QUERY)
102*cdf0e10cSrcweir , m_xFactory ( xNewFactory )
103*cdf0e10cSrcweir , xProgressHandler( xProgress )
104*cdf0e10cSrcweir , bRecoveryMode( bForceRecovery )
105*cdf0e10cSrcweir {
106*cdf0e10cSrcweir 	if (bInitialise)
107*cdf0e10cSrcweir 	{
108*cdf0e10cSrcweir 		if ( bForceRecovery )
109*cdf0e10cSrcweir 		{
110*cdf0e10cSrcweir 			recover();
111*cdf0e10cSrcweir 		}
112*cdf0e10cSrcweir 		else if ( readCEN() == -1 )
113*cdf0e10cSrcweir 		{
114*cdf0e10cSrcweir 			aEntries.clear();
115*cdf0e10cSrcweir 			throw ZipException( OUString( RTL_CONSTASCII_USTRINGPARAM ( "stream data looks to be broken" ) ), uno::Reference < XInterface > () );
116*cdf0e10cSrcweir 		}
117*cdf0e10cSrcweir 	}
118*cdf0e10cSrcweir }
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir ZipFile::~ZipFile()
121*cdf0e10cSrcweir {
122*cdf0e10cSrcweir     aEntries.clear();
123*cdf0e10cSrcweir }
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir void ZipFile::setInputStream ( uno::Reference < XInputStream > xNewStream )
126*cdf0e10cSrcweir {
127*cdf0e10cSrcweir     ::osl::MutexGuard aGuard( m_aMutex );
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir 	xStream = xNewStream;
130*cdf0e10cSrcweir 	xSeek = uno::Reference < XSeekable > ( xStream, UNO_QUERY );
131*cdf0e10cSrcweir 	aGrabber.setInputStream ( xStream );
132*cdf0e10cSrcweir }
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir uno::Reference< xml::crypto::XDigestContext > ZipFile::StaticGetDigestContextForChecksum( const uno::Reference< lang::XMultiServiceFactory >& xArgFactory, const ::rtl::Reference< EncryptionData >& xEncryptionData )
135*cdf0e10cSrcweir {
136*cdf0e10cSrcweir     uno::Reference< xml::crypto::XDigestContext > xDigestContext;
137*cdf0e10cSrcweir     if ( xEncryptionData->m_nCheckAlg == xml::crypto::DigestID::SHA256_1K )
138*cdf0e10cSrcweir     {
139*cdf0e10cSrcweir         uno::Reference< lang::XMultiServiceFactory > xFactory = xArgFactory;
140*cdf0e10cSrcweir         if ( !xFactory.is() )
141*cdf0e10cSrcweir             xFactory.set( comphelper::getProcessServiceFactory(), uno::UNO_SET_THROW );
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir         uno::Reference< xml::crypto::XDigestContextSupplier > xDigestContextSupplier(
144*cdf0e10cSrcweir             xFactory->createInstance( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.xml.crypto.NSSInitializer" ) ) ),
145*cdf0e10cSrcweir             uno::UNO_QUERY_THROW );
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir         xDigestContext.set( xDigestContextSupplier->getDigestContext( xEncryptionData->m_nCheckAlg, uno::Sequence< beans::NamedValue >() ), uno::UNO_SET_THROW );
148*cdf0e10cSrcweir     }
149*cdf0e10cSrcweir     else if ( xEncryptionData->m_nCheckAlg == xml::crypto::DigestID::SHA1_1K )
150*cdf0e10cSrcweir         xDigestContext.set( SHA1DigestContext::Create(), uno::UNO_SET_THROW );
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir     return xDigestContext;
153*cdf0e10cSrcweir }
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir uno::Reference< xml::crypto::XCipherContext > ZipFile::StaticGetCipher( const uno::Reference< lang::XMultiServiceFactory >& xArgFactory, const ::rtl::Reference< EncryptionData >& xEncryptionData, bool bEncrypt )
156*cdf0e10cSrcweir {
157*cdf0e10cSrcweir     uno::Reference< xml::crypto::XCipherContext > xResult;
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir     try
160*cdf0e10cSrcweir     {
161*cdf0e10cSrcweir         uno::Sequence< sal_Int8 > aDerivedKey( xEncryptionData->m_nDerivedKeySize );
162*cdf0e10cSrcweir         if ( rtl_Digest_E_None != rtl_digest_PBKDF2( reinterpret_cast< sal_uInt8* >( aDerivedKey.getArray() ),
163*cdf0e10cSrcweir                             aDerivedKey.getLength(),
164*cdf0e10cSrcweir 							reinterpret_cast< const sal_uInt8 * > (xEncryptionData->m_aKey.getConstArray() ),
165*cdf0e10cSrcweir 							xEncryptionData->m_aKey.getLength(),
166*cdf0e10cSrcweir 							reinterpret_cast< const sal_uInt8 * > ( xEncryptionData->m_aSalt.getConstArray() ),
167*cdf0e10cSrcweir 							xEncryptionData->m_aSalt.getLength(),
168*cdf0e10cSrcweir 							xEncryptionData->m_nIterationCount ) )
169*cdf0e10cSrcweir         {
170*cdf0e10cSrcweir             throw ZipIOException( ::rtl::OUString::createFromAscii( "Can not create derived key!\n" ),
171*cdf0e10cSrcweir                                   uno::Reference< XInterface >() );
172*cdf0e10cSrcweir         }
173*cdf0e10cSrcweir 
174*cdf0e10cSrcweir         if ( xEncryptionData->m_nEncAlg == xml::crypto::CipherID::AES_CBC_W3C_PADDING )
175*cdf0e10cSrcweir         {
176*cdf0e10cSrcweir             uno::Reference< lang::XMultiServiceFactory > xFactory = xArgFactory;
177*cdf0e10cSrcweir             if ( !xFactory.is() )
178*cdf0e10cSrcweir                 xFactory.set( comphelper::getProcessServiceFactory(), uno::UNO_SET_THROW );
179*cdf0e10cSrcweir 
180*cdf0e10cSrcweir             uno::Reference< xml::crypto::XCipherContextSupplier > xCipherContextSupplier(
181*cdf0e10cSrcweir                 xFactory->createInstance( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.xml.crypto.NSSInitializer" ) ) ),
182*cdf0e10cSrcweir                 uno::UNO_QUERY_THROW );
183*cdf0e10cSrcweir 
184*cdf0e10cSrcweir             xResult = xCipherContextSupplier->getCipherContext( xEncryptionData->m_nEncAlg, aDerivedKey, xEncryptionData->m_aInitVector, bEncrypt, uno::Sequence< beans::NamedValue >() );
185*cdf0e10cSrcweir         }
186*cdf0e10cSrcweir         else if ( xEncryptionData->m_nEncAlg == xml::crypto::CipherID::BLOWFISH_CFB_8 )
187*cdf0e10cSrcweir         {
188*cdf0e10cSrcweir             xResult = BlowfishCFB8CipherContext::Create( aDerivedKey, xEncryptionData->m_aInitVector, bEncrypt );
189*cdf0e10cSrcweir         }
190*cdf0e10cSrcweir         else
191*cdf0e10cSrcweir         {
192*cdf0e10cSrcweir             throw ZipIOException( ::rtl::OUString::createFromAscii( "Unknown cipher algorithm is requested!\n" ),
193*cdf0e10cSrcweir                                   uno::Reference< XInterface >() );
194*cdf0e10cSrcweir         }
195*cdf0e10cSrcweir     }
196*cdf0e10cSrcweir     catch( uno::Exception& )
197*cdf0e10cSrcweir     {
198*cdf0e10cSrcweir         OSL_ENSURE( sal_False, "Can not create cipher context!" );
199*cdf0e10cSrcweir     }
200*cdf0e10cSrcweir 
201*cdf0e10cSrcweir     return xResult;
202*cdf0e10cSrcweir }
203*cdf0e10cSrcweir 
204*cdf0e10cSrcweir void ZipFile::StaticFillHeader( const ::rtl::Reference< EncryptionData >& rData,
205*cdf0e10cSrcweir 								sal_Int32 nSize,
206*cdf0e10cSrcweir 								const ::rtl::OUString& aMediaType,
207*cdf0e10cSrcweir 								sal_Int8 * & pHeader )
208*cdf0e10cSrcweir {
209*cdf0e10cSrcweir 	// I think it's safe to restrict vector and salt length to 2 bytes !
210*cdf0e10cSrcweir 	sal_Int16 nIVLength = static_cast < sal_Int16 > ( rData->m_aInitVector.getLength() );
211*cdf0e10cSrcweir 	sal_Int16 nSaltLength = static_cast < sal_Int16 > ( rData->m_aSalt.getLength() );
212*cdf0e10cSrcweir 	sal_Int16 nDigestLength = static_cast < sal_Int16 > ( rData->m_aDigest.getLength() );
213*cdf0e10cSrcweir 	sal_Int16 nMediaTypeLength = static_cast < sal_Int16 > ( aMediaType.getLength() * sizeof( sal_Unicode ) );
214*cdf0e10cSrcweir 
215*cdf0e10cSrcweir 	// First the header
216*cdf0e10cSrcweir 	*(pHeader++) = ( n_ConstHeader >> 0 ) & 0xFF;
217*cdf0e10cSrcweir 	*(pHeader++) = ( n_ConstHeader >> 8 ) & 0xFF;
218*cdf0e10cSrcweir 	*(pHeader++) = ( n_ConstHeader >> 16 ) & 0xFF;
219*cdf0e10cSrcweir 	*(pHeader++) = ( n_ConstHeader >> 24 ) & 0xFF;
220*cdf0e10cSrcweir 
221*cdf0e10cSrcweir 	// Then the version
222*cdf0e10cSrcweir 	*(pHeader++) = ( n_ConstCurrentVersion >> 0 ) & 0xFF;
223*cdf0e10cSrcweir 	*(pHeader++) = ( n_ConstCurrentVersion >> 8 ) & 0xFF;
224*cdf0e10cSrcweir 
225*cdf0e10cSrcweir 	// Then the iteration Count
226*cdf0e10cSrcweir 	sal_Int32 nIterationCount = rData->m_nIterationCount;
227*cdf0e10cSrcweir 	*(pHeader++) = static_cast< sal_Int8 >(( nIterationCount >> 0 ) & 0xFF);
228*cdf0e10cSrcweir 	*(pHeader++) = static_cast< sal_Int8 >(( nIterationCount >> 8 ) & 0xFF);
229*cdf0e10cSrcweir 	*(pHeader++) = static_cast< sal_Int8 >(( nIterationCount >> 16 ) & 0xFF);
230*cdf0e10cSrcweir 	*(pHeader++) = static_cast< sal_Int8 >(( nIterationCount >> 24 ) & 0xFF);
231*cdf0e10cSrcweir 
232*cdf0e10cSrcweir 	// Then the size
233*cdf0e10cSrcweir 	*(pHeader++) = static_cast< sal_Int8 >(( nSize >> 0 ) & 0xFF);
234*cdf0e10cSrcweir 	*(pHeader++) = static_cast< sal_Int8 >(( nSize >> 8 ) & 0xFF);
235*cdf0e10cSrcweir 	*(pHeader++) = static_cast< sal_Int8 >(( nSize >> 16 ) & 0xFF);
236*cdf0e10cSrcweir 	*(pHeader++) = static_cast< sal_Int8 >(( nSize >> 24 ) & 0xFF);
237*cdf0e10cSrcweir 
238*cdf0e10cSrcweir 	// Then the encryption algorithm
239*cdf0e10cSrcweir     sal_Int32 nEncAlgID = rData->m_nEncAlg;
240*cdf0e10cSrcweir 	*(pHeader++) = static_cast< sal_Int8 >(( nEncAlgID >> 0 ) & 0xFF);
241*cdf0e10cSrcweir 	*(pHeader++) = static_cast< sal_Int8 >(( nEncAlgID >> 8 ) & 0xFF);
242*cdf0e10cSrcweir 	*(pHeader++) = static_cast< sal_Int8 >(( nEncAlgID >> 16 ) & 0xFF);
243*cdf0e10cSrcweir 	*(pHeader++) = static_cast< sal_Int8 >(( nEncAlgID >> 24 ) & 0xFF);
244*cdf0e10cSrcweir 
245*cdf0e10cSrcweir 	// Then the checksum algorithm
246*cdf0e10cSrcweir     sal_Int32 nChecksumAlgID = rData->m_nCheckAlg;
247*cdf0e10cSrcweir 	*(pHeader++) = static_cast< sal_Int8 >(( nChecksumAlgID >> 0 ) & 0xFF);
248*cdf0e10cSrcweir 	*(pHeader++) = static_cast< sal_Int8 >(( nChecksumAlgID >> 8 ) & 0xFF);
249*cdf0e10cSrcweir 	*(pHeader++) = static_cast< sal_Int8 >(( nChecksumAlgID >> 16 ) & 0xFF);
250*cdf0e10cSrcweir 	*(pHeader++) = static_cast< sal_Int8 >(( nChecksumAlgID >> 24 ) & 0xFF);
251*cdf0e10cSrcweir 
252*cdf0e10cSrcweir 	// Then the derived key size
253*cdf0e10cSrcweir     sal_Int32 nDerivedKeySize = rData->m_nDerivedKeySize;
254*cdf0e10cSrcweir 	*(pHeader++) = static_cast< sal_Int8 >(( nDerivedKeySize >> 0 ) & 0xFF);
255*cdf0e10cSrcweir 	*(pHeader++) = static_cast< sal_Int8 >(( nDerivedKeySize >> 8 ) & 0xFF);
256*cdf0e10cSrcweir 	*(pHeader++) = static_cast< sal_Int8 >(( nDerivedKeySize >> 16 ) & 0xFF);
257*cdf0e10cSrcweir 	*(pHeader++) = static_cast< sal_Int8 >(( nDerivedKeySize >> 24 ) & 0xFF);
258*cdf0e10cSrcweir 
259*cdf0e10cSrcweir 	// Then the start key generation algorithm
260*cdf0e10cSrcweir     sal_Int32 nKeyAlgID = rData->m_nStartKeyGenID;
261*cdf0e10cSrcweir 	*(pHeader++) = static_cast< sal_Int8 >(( nKeyAlgID >> 0 ) & 0xFF);
262*cdf0e10cSrcweir 	*(pHeader++) = static_cast< sal_Int8 >(( nKeyAlgID >> 8 ) & 0xFF);
263*cdf0e10cSrcweir 	*(pHeader++) = static_cast< sal_Int8 >(( nKeyAlgID >> 16 ) & 0xFF);
264*cdf0e10cSrcweir 	*(pHeader++) = static_cast< sal_Int8 >(( nKeyAlgID >> 24 ) & 0xFF);
265*cdf0e10cSrcweir 
266*cdf0e10cSrcweir 	// Then the salt length
267*cdf0e10cSrcweir 	*(pHeader++) = static_cast< sal_Int8 >(( nSaltLength >> 0 ) & 0xFF);
268*cdf0e10cSrcweir 	*(pHeader++) = static_cast< sal_Int8 >(( nSaltLength >> 8 ) & 0xFF);
269*cdf0e10cSrcweir 
270*cdf0e10cSrcweir 	// Then the IV length
271*cdf0e10cSrcweir 	*(pHeader++) = static_cast< sal_Int8 >(( nIVLength >> 0 ) & 0xFF);
272*cdf0e10cSrcweir 	*(pHeader++) = static_cast< sal_Int8 >(( nIVLength >> 8 ) & 0xFF);
273*cdf0e10cSrcweir 
274*cdf0e10cSrcweir 	// Then the digest length
275*cdf0e10cSrcweir 	*(pHeader++) = static_cast< sal_Int8 >(( nDigestLength >> 0 ) & 0xFF);
276*cdf0e10cSrcweir 	*(pHeader++) = static_cast< sal_Int8 >(( nDigestLength >> 8 ) & 0xFF);
277*cdf0e10cSrcweir 
278*cdf0e10cSrcweir 	// Then the mediatype length
279*cdf0e10cSrcweir 	*(pHeader++) = static_cast< sal_Int8 >(( nMediaTypeLength >> 0 ) & 0xFF);
280*cdf0e10cSrcweir 	*(pHeader++) = static_cast< sal_Int8 >(( nMediaTypeLength >> 8 ) & 0xFF);
281*cdf0e10cSrcweir 
282*cdf0e10cSrcweir 	// Then the salt content
283*cdf0e10cSrcweir 	rtl_copyMemory ( pHeader, rData->m_aSalt.getConstArray(), nSaltLength );
284*cdf0e10cSrcweir 	pHeader += nSaltLength;
285*cdf0e10cSrcweir 
286*cdf0e10cSrcweir 	// Then the IV content
287*cdf0e10cSrcweir 	rtl_copyMemory ( pHeader, rData->m_aInitVector.getConstArray(), nIVLength );
288*cdf0e10cSrcweir 	pHeader += nIVLength;
289*cdf0e10cSrcweir 
290*cdf0e10cSrcweir 	// Then the digest content
291*cdf0e10cSrcweir 	rtl_copyMemory ( pHeader, rData->m_aDigest.getConstArray(), nDigestLength );
292*cdf0e10cSrcweir 	pHeader += nDigestLength;
293*cdf0e10cSrcweir 
294*cdf0e10cSrcweir 	// Then the mediatype itself
295*cdf0e10cSrcweir 	rtl_copyMemory ( pHeader, aMediaType.getStr(), nMediaTypeLength );
296*cdf0e10cSrcweir 	pHeader += nMediaTypeLength;
297*cdf0e10cSrcweir }
298*cdf0e10cSrcweir 
299*cdf0e10cSrcweir sal_Bool ZipFile::StaticFillData (  ::rtl::Reference< BaseEncryptionData > & rData,
300*cdf0e10cSrcweir                                     sal_Int32 &rEncAlg,
301*cdf0e10cSrcweir                                     sal_Int32 &rChecksumAlg,
302*cdf0e10cSrcweir                                     sal_Int32 &rDerivedKeySize,
303*cdf0e10cSrcweir                                     sal_Int32 &rStartKeyGenID,
304*cdf0e10cSrcweir 									sal_Int32 &rSize,
305*cdf0e10cSrcweir 									::rtl::OUString& aMediaType,
306*cdf0e10cSrcweir 									const uno::Reference< XInputStream >& rStream )
307*cdf0e10cSrcweir {
308*cdf0e10cSrcweir 	sal_Bool bOk = sal_False;
309*cdf0e10cSrcweir 	const sal_Int32 nHeaderSize = n_ConstHeaderSize - 4;
310*cdf0e10cSrcweir 	Sequence < sal_Int8 > aBuffer ( nHeaderSize );
311*cdf0e10cSrcweir 	if ( nHeaderSize == rStream->readBytes ( aBuffer, nHeaderSize ) )
312*cdf0e10cSrcweir 	{
313*cdf0e10cSrcweir 		sal_Int16 nPos = 0;
314*cdf0e10cSrcweir 		sal_Int8 *pBuffer = aBuffer.getArray();
315*cdf0e10cSrcweir 		sal_Int16 nVersion = pBuffer[nPos++] & 0xFF;
316*cdf0e10cSrcweir 		nVersion |= ( pBuffer[nPos++] & 0xFF ) << 8;
317*cdf0e10cSrcweir 		if ( nVersion == n_ConstCurrentVersion )
318*cdf0e10cSrcweir 		{
319*cdf0e10cSrcweir 			sal_Int32 nCount = pBuffer[nPos++] & 0xFF;
320*cdf0e10cSrcweir 			nCount |= ( pBuffer[nPos++] & 0xFF ) << 8;
321*cdf0e10cSrcweir 			nCount |= ( pBuffer[nPos++] & 0xFF ) << 16;
322*cdf0e10cSrcweir 			nCount |= ( pBuffer[nPos++] & 0xFF ) << 24;
323*cdf0e10cSrcweir 			rData->m_nIterationCount = nCount;
324*cdf0e10cSrcweir 
325*cdf0e10cSrcweir 			rSize  =   pBuffer[nPos++] & 0xFF;
326*cdf0e10cSrcweir 			rSize |= ( pBuffer[nPos++] & 0xFF ) << 8;
327*cdf0e10cSrcweir 			rSize |= ( pBuffer[nPos++] & 0xFF ) << 16;
328*cdf0e10cSrcweir 			rSize |= ( pBuffer[nPos++] & 0xFF ) << 24;
329*cdf0e10cSrcweir 
330*cdf0e10cSrcweir 			rEncAlg   =   pBuffer[nPos++] & 0xFF;
331*cdf0e10cSrcweir 			rEncAlg  |= ( pBuffer[nPos++] & 0xFF ) << 8;
332*cdf0e10cSrcweir 			rEncAlg  |= ( pBuffer[nPos++] & 0xFF ) << 16;
333*cdf0e10cSrcweir 			rEncAlg  |= ( pBuffer[nPos++] & 0xFF ) << 24;
334*cdf0e10cSrcweir 
335*cdf0e10cSrcweir 			rChecksumAlg   =   pBuffer[nPos++] & 0xFF;
336*cdf0e10cSrcweir 			rChecksumAlg  |= ( pBuffer[nPos++] & 0xFF ) << 8;
337*cdf0e10cSrcweir 			rChecksumAlg  |= ( pBuffer[nPos++] & 0xFF ) << 16;
338*cdf0e10cSrcweir 			rChecksumAlg  |= ( pBuffer[nPos++] & 0xFF ) << 24;
339*cdf0e10cSrcweir 
340*cdf0e10cSrcweir 			rDerivedKeySize   =   pBuffer[nPos++] & 0xFF;
341*cdf0e10cSrcweir 			rDerivedKeySize  |= ( pBuffer[nPos++] & 0xFF ) << 8;
342*cdf0e10cSrcweir 			rDerivedKeySize  |= ( pBuffer[nPos++] & 0xFF ) << 16;
343*cdf0e10cSrcweir 			rDerivedKeySize  |= ( pBuffer[nPos++] & 0xFF ) << 24;
344*cdf0e10cSrcweir 
345*cdf0e10cSrcweir 			rStartKeyGenID   =   pBuffer[nPos++] & 0xFF;
346*cdf0e10cSrcweir 			rStartKeyGenID  |= ( pBuffer[nPos++] & 0xFF ) << 8;
347*cdf0e10cSrcweir 			rStartKeyGenID  |= ( pBuffer[nPos++] & 0xFF ) << 16;
348*cdf0e10cSrcweir 			rStartKeyGenID  |= ( pBuffer[nPos++] & 0xFF ) << 24;
349*cdf0e10cSrcweir 
350*cdf0e10cSrcweir 			sal_Int16 nSaltLength =   pBuffer[nPos++] & 0xFF;
351*cdf0e10cSrcweir 			nSaltLength          |= ( pBuffer[nPos++] & 0xFF ) << 8;
352*cdf0e10cSrcweir 			sal_Int16 nIVLength   = ( pBuffer[nPos++] & 0xFF );
353*cdf0e10cSrcweir 			nIVLength 			 |= ( pBuffer[nPos++] & 0xFF ) << 8;
354*cdf0e10cSrcweir 			sal_Int16 nDigestLength = pBuffer[nPos++] & 0xFF;
355*cdf0e10cSrcweir 			nDigestLength 	     |= ( pBuffer[nPos++] & 0xFF ) << 8;
356*cdf0e10cSrcweir 
357*cdf0e10cSrcweir 			sal_Int16 nMediaTypeLength = pBuffer[nPos++] & 0xFF;
358*cdf0e10cSrcweir 			nMediaTypeLength |= ( pBuffer[nPos++] & 0xFF ) << 8;
359*cdf0e10cSrcweir 
360*cdf0e10cSrcweir 			if ( nSaltLength == rStream->readBytes ( aBuffer, nSaltLength ) )
361*cdf0e10cSrcweir 			{
362*cdf0e10cSrcweir 				rData->m_aSalt.realloc ( nSaltLength );
363*cdf0e10cSrcweir 				rtl_copyMemory ( rData->m_aSalt.getArray(), aBuffer.getConstArray(), nSaltLength );
364*cdf0e10cSrcweir 				if ( nIVLength == rStream->readBytes ( aBuffer, nIVLength ) )
365*cdf0e10cSrcweir 				{
366*cdf0e10cSrcweir 					rData->m_aInitVector.realloc ( nIVLength );
367*cdf0e10cSrcweir 					rtl_copyMemory ( rData->m_aInitVector.getArray(), aBuffer.getConstArray(), nIVLength );
368*cdf0e10cSrcweir 					if ( nDigestLength == rStream->readBytes ( aBuffer, nDigestLength ) )
369*cdf0e10cSrcweir 					{
370*cdf0e10cSrcweir 						rData->m_aDigest.realloc ( nDigestLength );
371*cdf0e10cSrcweir 						rtl_copyMemory ( rData->m_aDigest.getArray(), aBuffer.getConstArray(), nDigestLength );
372*cdf0e10cSrcweir 
373*cdf0e10cSrcweir 						if ( nMediaTypeLength == rStream->readBytes ( aBuffer, nMediaTypeLength ) )
374*cdf0e10cSrcweir 						{
375*cdf0e10cSrcweir 							aMediaType = ::rtl::OUString( (sal_Unicode*)aBuffer.getConstArray(),
376*cdf0e10cSrcweir 															nMediaTypeLength / sizeof( sal_Unicode ) );
377*cdf0e10cSrcweir 							bOk = sal_True;
378*cdf0e10cSrcweir 						}
379*cdf0e10cSrcweir 					}
380*cdf0e10cSrcweir 				}
381*cdf0e10cSrcweir 			}
382*cdf0e10cSrcweir 		}
383*cdf0e10cSrcweir 	}
384*cdf0e10cSrcweir 	return bOk;
385*cdf0e10cSrcweir }
386*cdf0e10cSrcweir 
387*cdf0e10cSrcweir uno::Reference< XInputStream > ZipFile::StaticGetDataFromRawStream( const uno::Reference< lang::XMultiServiceFactory >& xFactory,
388*cdf0e10cSrcweir                                                                 const uno::Reference< XInputStream >& xStream,
389*cdf0e10cSrcweir 																const ::rtl::Reference< EncryptionData > &rData )
390*cdf0e10cSrcweir 		throw ( packages::WrongPasswordException, ZipIOException, RuntimeException )
391*cdf0e10cSrcweir {
392*cdf0e10cSrcweir 	if ( !rData.is() )
393*cdf0e10cSrcweir 		throw ZipIOException( OUString::createFromAscii( "Encrypted stream without encryption data!\n" ),
394*cdf0e10cSrcweir 							uno::Reference< XInterface >() );
395*cdf0e10cSrcweir 
396*cdf0e10cSrcweir 	if ( !rData->m_aKey.getLength() )
397*cdf0e10cSrcweir 		throw packages::WrongPasswordException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
398*cdf0e10cSrcweir 
399*cdf0e10cSrcweir 	uno::Reference< XSeekable > xSeek( xStream, UNO_QUERY );
400*cdf0e10cSrcweir 	if ( !xSeek.is() )
401*cdf0e10cSrcweir 		throw ZipIOException( OUString::createFromAscii( "The stream must be seekable!\n" ),
402*cdf0e10cSrcweir 							uno::Reference< XInterface >() );
403*cdf0e10cSrcweir 
404*cdf0e10cSrcweir 
405*cdf0e10cSrcweir 	// if we have a digest, then this file is an encrypted one and we should
406*cdf0e10cSrcweir 	// check if we can decrypt it or not
407*cdf0e10cSrcweir 	OSL_ENSURE( rData->m_aDigest.getLength(), "Can't detect password correctness without digest!\n" );
408*cdf0e10cSrcweir 	if ( rData->m_aDigest.getLength() )
409*cdf0e10cSrcweir 	{
410*cdf0e10cSrcweir         sal_Int32 nSize = sal::static_int_cast< sal_Int32 >( xSeek->getLength() );
411*cdf0e10cSrcweir         if ( nSize > n_ConstDigestLength + 32 )
412*cdf0e10cSrcweir             nSize = n_ConstDigestLength + 32;
413*cdf0e10cSrcweir 
414*cdf0e10cSrcweir 		// skip header
415*cdf0e10cSrcweir 		xSeek->seek( n_ConstHeaderSize + rData->m_aInitVector.getLength() +
416*cdf0e10cSrcweir 								rData->m_aSalt.getLength() + rData->m_aDigest.getLength() );
417*cdf0e10cSrcweir 
418*cdf0e10cSrcweir 		// Only want to read enough to verify the digest
419*cdf0e10cSrcweir 		Sequence < sal_Int8 > aReadBuffer ( nSize );
420*cdf0e10cSrcweir 
421*cdf0e10cSrcweir 		xStream->readBytes( aReadBuffer, nSize );
422*cdf0e10cSrcweir 
423*cdf0e10cSrcweir 		if ( !StaticHasValidPassword( xFactory, aReadBuffer, rData ) )
424*cdf0e10cSrcweir 			throw packages::WrongPasswordException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
425*cdf0e10cSrcweir 	}
426*cdf0e10cSrcweir 
427*cdf0e10cSrcweir 	return new XUnbufferedStream( xFactory, xStream, rData );
428*cdf0e10cSrcweir }
429*cdf0e10cSrcweir 
430*cdf0e10cSrcweir #if 0
431*cdf0e10cSrcweir // for debugging purposes
432*cdf0e10cSrcweir void CheckSequence( const uno::Sequence< sal_Int8 >& aSequence )
433*cdf0e10cSrcweir {
434*cdf0e10cSrcweir     if ( aSequence.getLength() )
435*cdf0e10cSrcweir     {
436*cdf0e10cSrcweir         sal_Int32* pPointer = *( (sal_Int32**)&aSequence );
437*cdf0e10cSrcweir         sal_Int32 nSize = *( pPointer + 1 );
438*cdf0e10cSrcweir         sal_Int32 nMemSize = *( pPointer - 2 );
439*cdf0e10cSrcweir         sal_Int32 nUsedMemSize = ( nSize + 4 * sizeof( sal_Int32 ) );
440*cdf0e10cSrcweir         OSL_ENSURE( nSize == aSequence.getLength() && nUsedMemSize + 7 - ( nUsedMemSize - 1 ) % 8 == nMemSize, "Broken Sequence!" );
441*cdf0e10cSrcweir     }
442*cdf0e10cSrcweir }
443*cdf0e10cSrcweir #endif
444*cdf0e10cSrcweir 
445*cdf0e10cSrcweir sal_Bool ZipFile::StaticHasValidPassword( const uno::Reference< lang::XMultiServiceFactory >& xFactory, const Sequence< sal_Int8 > &aReadBuffer, const ::rtl::Reference< EncryptionData > &rData )
446*cdf0e10cSrcweir {
447*cdf0e10cSrcweir 	if ( !rData.is() || !rData->m_aKey.getLength() )
448*cdf0e10cSrcweir 		return sal_False;
449*cdf0e10cSrcweir 
450*cdf0e10cSrcweir 	sal_Bool bRet = sal_False;
451*cdf0e10cSrcweir 
452*cdf0e10cSrcweir     uno::Reference< xml::crypto::XCipherContext > xCipher( StaticGetCipher( xFactory, rData, false ), uno::UNO_SET_THROW );
453*cdf0e10cSrcweir 
454*cdf0e10cSrcweir     uno::Sequence< sal_Int8 > aDecryptBuffer;
455*cdf0e10cSrcweir     uno::Sequence< sal_Int8 > aDecryptBuffer2;
456*cdf0e10cSrcweir     try
457*cdf0e10cSrcweir     {
458*cdf0e10cSrcweir         aDecryptBuffer = xCipher->convertWithCipherContext( aReadBuffer );
459*cdf0e10cSrcweir         aDecryptBuffer2 = xCipher->finalizeCipherContextAndDispose();
460*cdf0e10cSrcweir     }
461*cdf0e10cSrcweir     catch( uno::Exception& )
462*cdf0e10cSrcweir     {
463*cdf0e10cSrcweir         // decryption with padding will throw the exception in finalizing if the buffer represent only part of the stream
464*cdf0e10cSrcweir         // it is no problem, actually this is why we read 32 additional bytes ( two of maximal possible encryption blocks )
465*cdf0e10cSrcweir     }
466*cdf0e10cSrcweir 
467*cdf0e10cSrcweir     if ( aDecryptBuffer2.getLength() )
468*cdf0e10cSrcweir     {
469*cdf0e10cSrcweir         sal_Int32 nOldLen = aDecryptBuffer.getLength();
470*cdf0e10cSrcweir         aDecryptBuffer.realloc( nOldLen + aDecryptBuffer2.getLength() );
471*cdf0e10cSrcweir         rtl_copyMemory( aDecryptBuffer.getArray() + nOldLen, aDecryptBuffer2.getArray(), aDecryptBuffer2.getLength() );
472*cdf0e10cSrcweir     }
473*cdf0e10cSrcweir 
474*cdf0e10cSrcweir     if ( aDecryptBuffer.getLength() > n_ConstDigestLength )
475*cdf0e10cSrcweir         aDecryptBuffer.realloc( n_ConstDigestLength );
476*cdf0e10cSrcweir 
477*cdf0e10cSrcweir     uno::Sequence< sal_Int8 > aDigestSeq;
478*cdf0e10cSrcweir     uno::Reference< xml::crypto::XDigestContext > xDigestContext( StaticGetDigestContextForChecksum( xFactory, rData ), uno::UNO_SET_THROW );
479*cdf0e10cSrcweir 
480*cdf0e10cSrcweir     xDigestContext->updateDigest( aDecryptBuffer );
481*cdf0e10cSrcweir     aDigestSeq = xDigestContext->finalizeDigestAndDispose();
482*cdf0e10cSrcweir 
483*cdf0e10cSrcweir     // If we don't have a digest, then we have to assume that the password is correct
484*cdf0e10cSrcweir 	if (  rData->m_aDigest.getLength() != 0  &&
485*cdf0e10cSrcweir 	      ( aDigestSeq.getLength() != rData->m_aDigest.getLength() ||
486*cdf0e10cSrcweir 	        0 != rtl_compareMemory ( aDigestSeq.getConstArray(),
487*cdf0e10cSrcweir 		 					        rData->m_aDigest.getConstArray(),
488*cdf0e10cSrcweir 							        aDigestSeq.getLength() ) ) )
489*cdf0e10cSrcweir 	{
490*cdf0e10cSrcweir 		// We should probably tell the user that the password they entered was wrong
491*cdf0e10cSrcweir 	}
492*cdf0e10cSrcweir 	else
493*cdf0e10cSrcweir 		bRet = sal_True;
494*cdf0e10cSrcweir 
495*cdf0e10cSrcweir 	return bRet;
496*cdf0e10cSrcweir }
497*cdf0e10cSrcweir 
498*cdf0e10cSrcweir sal_Bool ZipFile::hasValidPassword ( ZipEntry & rEntry, const ::rtl::Reference< EncryptionData >& rData )
499*cdf0e10cSrcweir {
500*cdf0e10cSrcweir     ::osl::MutexGuard aGuard( m_aMutex );
501*cdf0e10cSrcweir 
502*cdf0e10cSrcweir 	sal_Bool bRet = sal_False;
503*cdf0e10cSrcweir 	if ( rData.is() && rData->m_aKey.getLength() )
504*cdf0e10cSrcweir 	{
505*cdf0e10cSrcweir 		xSeek->seek( rEntry.nOffset );
506*cdf0e10cSrcweir 		sal_Int32 nSize = rEntry.nMethod == DEFLATED ? rEntry.nCompressedSize : rEntry.nSize;
507*cdf0e10cSrcweir 
508*cdf0e10cSrcweir 		// Only want to read enough to verify the digest
509*cdf0e10cSrcweir         if ( nSize > n_ConstDigestDecrypt )
510*cdf0e10cSrcweir             nSize = n_ConstDigestDecrypt;
511*cdf0e10cSrcweir 
512*cdf0e10cSrcweir 		Sequence < sal_Int8 > aReadBuffer ( nSize );
513*cdf0e10cSrcweir 
514*cdf0e10cSrcweir 		xStream->readBytes( aReadBuffer, nSize );
515*cdf0e10cSrcweir 
516*cdf0e10cSrcweir 		bRet = StaticHasValidPassword( m_xFactory, aReadBuffer, rData );
517*cdf0e10cSrcweir 	}
518*cdf0e10cSrcweir 
519*cdf0e10cSrcweir 	return bRet;
520*cdf0e10cSrcweir }
521*cdf0e10cSrcweir 
522*cdf0e10cSrcweir uno::Reference< XInputStream > ZipFile::createUnbufferedStream(
523*cdf0e10cSrcweir             SotMutexHolderRef aMutexHolder,
524*cdf0e10cSrcweir 			ZipEntry & rEntry,
525*cdf0e10cSrcweir 			const ::rtl::Reference< EncryptionData > &rData,
526*cdf0e10cSrcweir 			sal_Int8 nStreamMode,
527*cdf0e10cSrcweir 			sal_Bool bIsEncrypted,
528*cdf0e10cSrcweir 			::rtl::OUString aMediaType )
529*cdf0e10cSrcweir {
530*cdf0e10cSrcweir     ::osl::MutexGuard aGuard( m_aMutex );
531*cdf0e10cSrcweir 
532*cdf0e10cSrcweir 	return new XUnbufferedStream ( m_xFactory, aMutexHolder, rEntry, xStream, rData, nStreamMode, bIsEncrypted, aMediaType, bRecoveryMode );
533*cdf0e10cSrcweir }
534*cdf0e10cSrcweir 
535*cdf0e10cSrcweir 
536*cdf0e10cSrcweir ZipEnumeration * SAL_CALL ZipFile::entries(  )
537*cdf0e10cSrcweir {
538*cdf0e10cSrcweir 	return new ZipEnumeration ( aEntries );
539*cdf0e10cSrcweir }
540*cdf0e10cSrcweir 
541*cdf0e10cSrcweir uno::Reference< XInputStream > SAL_CALL ZipFile::getInputStream( ZipEntry& rEntry,
542*cdf0e10cSrcweir 		const ::rtl::Reference< EncryptionData > &rData,
543*cdf0e10cSrcweir 		sal_Bool bIsEncrypted,
544*cdf0e10cSrcweir         SotMutexHolderRef aMutexHolder )
545*cdf0e10cSrcweir 	throw(IOException, ZipException, RuntimeException)
546*cdf0e10cSrcweir {
547*cdf0e10cSrcweir     ::osl::MutexGuard aGuard( m_aMutex );
548*cdf0e10cSrcweir 
549*cdf0e10cSrcweir 	if ( rEntry.nOffset <= 0 )
550*cdf0e10cSrcweir 		readLOC( rEntry );
551*cdf0e10cSrcweir 
552*cdf0e10cSrcweir 	// We want to return a rawStream if we either don't have a key or if the
553*cdf0e10cSrcweir 	// key is wrong
554*cdf0e10cSrcweir 
555*cdf0e10cSrcweir 	sal_Bool bNeedRawStream = rEntry.nMethod == STORED;
556*cdf0e10cSrcweir 
557*cdf0e10cSrcweir 	// if we have a digest, then this file is an encrypted one and we should
558*cdf0e10cSrcweir 	// check if we can decrypt it or not
559*cdf0e10cSrcweir 	if ( bIsEncrypted && rData.is() && rData->m_aDigest.getLength() )
560*cdf0e10cSrcweir 		bNeedRawStream = !hasValidPassword ( rEntry, rData );
561*cdf0e10cSrcweir 
562*cdf0e10cSrcweir 	return createUnbufferedStream ( aMutexHolder,
563*cdf0e10cSrcweir                                     rEntry,
564*cdf0e10cSrcweir 									rData,
565*cdf0e10cSrcweir 									bNeedRawStream ? UNBUFF_STREAM_RAW : UNBUFF_STREAM_DATA,
566*cdf0e10cSrcweir 									bIsEncrypted );
567*cdf0e10cSrcweir }
568*cdf0e10cSrcweir 
569*cdf0e10cSrcweir uno::Reference< XInputStream > SAL_CALL ZipFile::getDataStream( ZipEntry& rEntry,
570*cdf0e10cSrcweir 		const ::rtl::Reference< EncryptionData > &rData,
571*cdf0e10cSrcweir 		sal_Bool bIsEncrypted,
572*cdf0e10cSrcweir         SotMutexHolderRef aMutexHolder )
573*cdf0e10cSrcweir 	throw ( packages::WrongPasswordException,
574*cdf0e10cSrcweir 			IOException,
575*cdf0e10cSrcweir 			ZipException,
576*cdf0e10cSrcweir 			RuntimeException )
577*cdf0e10cSrcweir {
578*cdf0e10cSrcweir     ::osl::MutexGuard aGuard( m_aMutex );
579*cdf0e10cSrcweir 
580*cdf0e10cSrcweir 	if ( rEntry.nOffset <= 0 )
581*cdf0e10cSrcweir 		readLOC( rEntry );
582*cdf0e10cSrcweir 
583*cdf0e10cSrcweir 	// An exception must be thrown in case stream is encrypted and
584*cdf0e10cSrcweir 	// there is no key or the key is wrong
585*cdf0e10cSrcweir 	sal_Bool bNeedRawStream = sal_False;
586*cdf0e10cSrcweir 	if ( bIsEncrypted )
587*cdf0e10cSrcweir 	{
588*cdf0e10cSrcweir 		// in case no digest is provided there is no way
589*cdf0e10cSrcweir 		// to detect password correctness
590*cdf0e10cSrcweir 		if ( !rData.is() )
591*cdf0e10cSrcweir 			throw ZipException( OUString::createFromAscii( "Encrypted stream without encryption data!\n" ),
592*cdf0e10cSrcweir 								uno::Reference< XInterface >() );
593*cdf0e10cSrcweir 
594*cdf0e10cSrcweir 		// if we have a digest, then this file is an encrypted one and we should
595*cdf0e10cSrcweir 		// check if we can decrypt it or not
596*cdf0e10cSrcweir 		OSL_ENSURE( rData->m_aDigest.getLength(), "Can't detect password correctness without digest!\n" );
597*cdf0e10cSrcweir 		if ( rData->m_aDigest.getLength() && !hasValidPassword ( rEntry, rData ) )
598*cdf0e10cSrcweir 				throw packages::WrongPasswordException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
599*cdf0e10cSrcweir 	}
600*cdf0e10cSrcweir 	else
601*cdf0e10cSrcweir 		bNeedRawStream = ( rEntry.nMethod == STORED );
602*cdf0e10cSrcweir 
603*cdf0e10cSrcweir 	return createUnbufferedStream ( aMutexHolder,
604*cdf0e10cSrcweir                                     rEntry,
605*cdf0e10cSrcweir 									rData,
606*cdf0e10cSrcweir 									bNeedRawStream ? UNBUFF_STREAM_RAW : UNBUFF_STREAM_DATA,
607*cdf0e10cSrcweir 									bIsEncrypted );
608*cdf0e10cSrcweir }
609*cdf0e10cSrcweir 
610*cdf0e10cSrcweir uno::Reference< XInputStream > SAL_CALL ZipFile::getRawData( ZipEntry& rEntry,
611*cdf0e10cSrcweir 		const ::rtl::Reference< EncryptionData >& rData,
612*cdf0e10cSrcweir 		sal_Bool bIsEncrypted,
613*cdf0e10cSrcweir         SotMutexHolderRef aMutexHolder )
614*cdf0e10cSrcweir 	throw(IOException, ZipException, RuntimeException)
615*cdf0e10cSrcweir {
616*cdf0e10cSrcweir     ::osl::MutexGuard aGuard( m_aMutex );
617*cdf0e10cSrcweir 
618*cdf0e10cSrcweir 	if ( rEntry.nOffset <= 0 )
619*cdf0e10cSrcweir 		readLOC( rEntry );
620*cdf0e10cSrcweir 
621*cdf0e10cSrcweir 	return createUnbufferedStream ( aMutexHolder, rEntry, rData, UNBUFF_STREAM_RAW, bIsEncrypted );
622*cdf0e10cSrcweir }
623*cdf0e10cSrcweir 
624*cdf0e10cSrcweir uno::Reference< XInputStream > SAL_CALL ZipFile::getWrappedRawStream(
625*cdf0e10cSrcweir 		ZipEntry& rEntry,
626*cdf0e10cSrcweir 		const ::rtl::Reference< EncryptionData >& rData,
627*cdf0e10cSrcweir 		const ::rtl::OUString& aMediaType,
628*cdf0e10cSrcweir         SotMutexHolderRef aMutexHolder )
629*cdf0e10cSrcweir 	throw ( packages::NoEncryptionException,
630*cdf0e10cSrcweir 			IOException,
631*cdf0e10cSrcweir 			ZipException,
632*cdf0e10cSrcweir 			RuntimeException )
633*cdf0e10cSrcweir {
634*cdf0e10cSrcweir     ::osl::MutexGuard aGuard( m_aMutex );
635*cdf0e10cSrcweir 
636*cdf0e10cSrcweir 	if ( !rData.is() )
637*cdf0e10cSrcweir 		throw packages::NoEncryptionException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
638*cdf0e10cSrcweir 
639*cdf0e10cSrcweir 	if ( rEntry.nOffset <= 0 )
640*cdf0e10cSrcweir 		readLOC( rEntry );
641*cdf0e10cSrcweir 
642*cdf0e10cSrcweir 	return createUnbufferedStream ( aMutexHolder, rEntry, rData, UNBUFF_STREAM_WRAPPEDRAW, sal_True, aMediaType );
643*cdf0e10cSrcweir }
644*cdf0e10cSrcweir 
645*cdf0e10cSrcweir sal_Bool ZipFile::readLOC( ZipEntry &rEntry )
646*cdf0e10cSrcweir 	throw(IOException, ZipException, RuntimeException)
647*cdf0e10cSrcweir {
648*cdf0e10cSrcweir     ::osl::MutexGuard aGuard( m_aMutex );
649*cdf0e10cSrcweir 
650*cdf0e10cSrcweir 	sal_Int32 nTestSig, nTime, nCRC, nSize, nCompressedSize;
651*cdf0e10cSrcweir 	sal_Int16 nVersion, nFlag, nHow, nPathLen, nExtraLen;
652*cdf0e10cSrcweir 	sal_Int32 nPos = -rEntry.nOffset;
653*cdf0e10cSrcweir 
654*cdf0e10cSrcweir 	aGrabber.seek(nPos);
655*cdf0e10cSrcweir 	aGrabber >> nTestSig;
656*cdf0e10cSrcweir 
657*cdf0e10cSrcweir 	if (nTestSig != LOCSIG)
658*cdf0e10cSrcweir 		throw ZipIOException( OUString( RTL_CONSTASCII_USTRINGPARAM ( "Invalid LOC header (bad signature") ), uno::Reference < XInterface > () );
659*cdf0e10cSrcweir 	aGrabber >> nVersion;
660*cdf0e10cSrcweir 	aGrabber >> nFlag;
661*cdf0e10cSrcweir 	aGrabber >> nHow;
662*cdf0e10cSrcweir 	aGrabber >> nTime;
663*cdf0e10cSrcweir 	aGrabber >> nCRC;
664*cdf0e10cSrcweir 	aGrabber >> nCompressedSize;
665*cdf0e10cSrcweir 	aGrabber >> nSize;
666*cdf0e10cSrcweir 	aGrabber >> nPathLen;
667*cdf0e10cSrcweir 	aGrabber >> nExtraLen;
668*cdf0e10cSrcweir 	rEntry.nOffset = static_cast < sal_Int32 > (aGrabber.getPosition()) + nPathLen + nExtraLen;
669*cdf0e10cSrcweir 
670*cdf0e10cSrcweir     // read always in UTF8, some tools seem not to set UTF8 bit
671*cdf0e10cSrcweir     uno::Sequence < sal_Int8 > aNameBuffer( nPathLen );
672*cdf0e10cSrcweir     sal_Int32 nRead = aGrabber.readBytes( aNameBuffer, nPathLen );
673*cdf0e10cSrcweir     if ( nRead < aNameBuffer.getLength() )
674*cdf0e10cSrcweir             aNameBuffer.realloc( nRead );
675*cdf0e10cSrcweir 
676*cdf0e10cSrcweir     ::rtl::OUString sLOCPath = rtl::OUString::intern( (sal_Char *) aNameBuffer.getArray(),
677*cdf0e10cSrcweir                                                         aNameBuffer.getLength(),
678*cdf0e10cSrcweir                                                         RTL_TEXTENCODING_UTF8 );
679*cdf0e10cSrcweir 
680*cdf0e10cSrcweir 	if ( rEntry.nPathLen == -1 ) // the file was created
681*cdf0e10cSrcweir     {
682*cdf0e10cSrcweir 		rEntry.nPathLen = nPathLen;
683*cdf0e10cSrcweir         rEntry.sPath = sLOCPath;
684*cdf0e10cSrcweir     }
685*cdf0e10cSrcweir 
686*cdf0e10cSrcweir 	// the method can be reset for internal use so it is not checked
687*cdf0e10cSrcweir 	sal_Bool bBroken = rEntry.nVersion != nVersion
688*cdf0e10cSrcweir 					|| rEntry.nFlag != nFlag
689*cdf0e10cSrcweir 					|| rEntry.nTime != nTime
690*cdf0e10cSrcweir 					|| rEntry.nPathLen != nPathLen
691*cdf0e10cSrcweir                     || !rEntry.sPath.equals( sLOCPath );
692*cdf0e10cSrcweir 
693*cdf0e10cSrcweir 	if ( bBroken && !bRecoveryMode )
694*cdf0e10cSrcweir 		throw ZipIOException( OUString( RTL_CONSTASCII_USTRINGPARAM( "The stream seems to be broken!" ) ),
695*cdf0e10cSrcweir 							uno::Reference< XInterface >() );
696*cdf0e10cSrcweir 
697*cdf0e10cSrcweir 	return sal_True;
698*cdf0e10cSrcweir }
699*cdf0e10cSrcweir 
700*cdf0e10cSrcweir sal_Int32 ZipFile::findEND( )
701*cdf0e10cSrcweir 	throw(IOException, ZipException, RuntimeException)
702*cdf0e10cSrcweir {
703*cdf0e10cSrcweir     // this method is called in constructor only, no need for mutex
704*cdf0e10cSrcweir 	sal_Int32 nLength, nPos, nEnd;
705*cdf0e10cSrcweir 	Sequence < sal_Int8 > aBuffer;
706*cdf0e10cSrcweir 	try
707*cdf0e10cSrcweir 	{
708*cdf0e10cSrcweir 		nLength = static_cast <sal_Int32 > (aGrabber.getLength());
709*cdf0e10cSrcweir 		if (nLength == 0 || nLength < ENDHDR)
710*cdf0e10cSrcweir 			return -1;
711*cdf0e10cSrcweir 		nPos = nLength - ENDHDR - ZIP_MAXNAMELEN;
712*cdf0e10cSrcweir 		nEnd = nPos >= 0 ? nPos : 0 ;
713*cdf0e10cSrcweir 
714*cdf0e10cSrcweir 		aGrabber.seek( nEnd );
715*cdf0e10cSrcweir 		aGrabber.readBytes ( aBuffer, nLength - nEnd );
716*cdf0e10cSrcweir 
717*cdf0e10cSrcweir 		const sal_Int8 *pBuffer = aBuffer.getConstArray();
718*cdf0e10cSrcweir 
719*cdf0e10cSrcweir 		nPos = nLength - nEnd - ENDHDR;
720*cdf0e10cSrcweir 		while ( nPos >= 0 )
721*cdf0e10cSrcweir 		{
722*cdf0e10cSrcweir 			if (pBuffer[nPos] == 'P' && pBuffer[nPos+1] == 'K' && pBuffer[nPos+2] == 5 && pBuffer[nPos+3] == 6 )
723*cdf0e10cSrcweir 				return nPos + nEnd;
724*cdf0e10cSrcweir 			nPos--;
725*cdf0e10cSrcweir 		}
726*cdf0e10cSrcweir 	}
727*cdf0e10cSrcweir 	catch ( IllegalArgumentException& )
728*cdf0e10cSrcweir 	{
729*cdf0e10cSrcweir 		throw ZipException( OUString( RTL_CONSTASCII_USTRINGPARAM ( "Zip END signature not found!") ), uno::Reference < XInterface > () );
730*cdf0e10cSrcweir 	}
731*cdf0e10cSrcweir 	catch ( NotConnectedException& )
732*cdf0e10cSrcweir 	{
733*cdf0e10cSrcweir 		throw ZipException( OUString( RTL_CONSTASCII_USTRINGPARAM ( "Zip END signature not found!") ), uno::Reference < XInterface > () );
734*cdf0e10cSrcweir 	}
735*cdf0e10cSrcweir 	catch ( BufferSizeExceededException& )
736*cdf0e10cSrcweir 	{
737*cdf0e10cSrcweir 		throw ZipException( OUString( RTL_CONSTASCII_USTRINGPARAM ( "Zip END signature not found!") ), uno::Reference < XInterface > () );
738*cdf0e10cSrcweir 	}
739*cdf0e10cSrcweir 	throw ZipException( OUString( RTL_CONSTASCII_USTRINGPARAM ( "Zip END signature not found!") ), uno::Reference < XInterface > () );
740*cdf0e10cSrcweir }
741*cdf0e10cSrcweir 
742*cdf0e10cSrcweir sal_Int32 ZipFile::readCEN()
743*cdf0e10cSrcweir 	throw(IOException, ZipException, RuntimeException)
744*cdf0e10cSrcweir {
745*cdf0e10cSrcweir     // this method is called in constructor only, no need for mutex
746*cdf0e10cSrcweir 	sal_Int32 nCenLen, nCenPos = -1, nCenOff, nEndPos, nLocPos;
747*cdf0e10cSrcweir 	sal_uInt16 nCount, nTotal;
748*cdf0e10cSrcweir 
749*cdf0e10cSrcweir 	try
750*cdf0e10cSrcweir 	{
751*cdf0e10cSrcweir 		nEndPos = findEND();
752*cdf0e10cSrcweir 		if (nEndPos == -1)
753*cdf0e10cSrcweir 			return -1;
754*cdf0e10cSrcweir 		aGrabber.seek(nEndPos + ENDTOT);
755*cdf0e10cSrcweir 		aGrabber >> nTotal;
756*cdf0e10cSrcweir 		aGrabber >> nCenLen;
757*cdf0e10cSrcweir 		aGrabber >> nCenOff;
758*cdf0e10cSrcweir 
759*cdf0e10cSrcweir 		if ( nTotal * CENHDR > nCenLen )
760*cdf0e10cSrcweir 			throw ZipException(OUString( RTL_CONSTASCII_USTRINGPARAM ( "invalid END header (bad entry count)") ), uno::Reference < XInterface > () );
761*cdf0e10cSrcweir 
762*cdf0e10cSrcweir 		if ( nTotal > ZIP_MAXENTRIES )
763*cdf0e10cSrcweir 			throw ZipException(OUString( RTL_CONSTASCII_USTRINGPARAM ( "too many entries in ZIP File") ), uno::Reference < XInterface > () );
764*cdf0e10cSrcweir 
765*cdf0e10cSrcweir 		if ( nCenLen < 0 || nCenLen > nEndPos )
766*cdf0e10cSrcweir 			throw ZipException(OUString( RTL_CONSTASCII_USTRINGPARAM ( "Invalid END header (bad central directory size)") ), uno::Reference < XInterface > () );
767*cdf0e10cSrcweir 
768*cdf0e10cSrcweir 		nCenPos = nEndPos - nCenLen;
769*cdf0e10cSrcweir 
770*cdf0e10cSrcweir 		if ( nCenOff < 0 || nCenOff > nCenPos )
771*cdf0e10cSrcweir 			throw ZipException(OUString( RTL_CONSTASCII_USTRINGPARAM ( "Invalid END header (bad central directory size)") ), uno::Reference < XInterface > () );
772*cdf0e10cSrcweir 
773*cdf0e10cSrcweir 		nLocPos = nCenPos - nCenOff;
774*cdf0e10cSrcweir 		aGrabber.seek( nCenPos );
775*cdf0e10cSrcweir 		Sequence < sal_Int8 > aCENBuffer ( nCenLen );
776*cdf0e10cSrcweir 		sal_Int64 nRead = aGrabber.readBytes ( aCENBuffer, nCenLen );
777*cdf0e10cSrcweir 		if ( static_cast < sal_Int64 > ( nCenLen ) != nRead )
778*cdf0e10cSrcweir 			throw ZipException ( OUString ( RTL_CONSTASCII_USTRINGPARAM ( "Error reading CEN into memory buffer!") ), uno::Reference < XInterface > () );
779*cdf0e10cSrcweir 
780*cdf0e10cSrcweir 		MemoryByteGrabber aMemGrabber ( aCENBuffer );
781*cdf0e10cSrcweir 
782*cdf0e10cSrcweir 		ZipEntry aEntry;
783*cdf0e10cSrcweir 		sal_Int32 nTestSig;
784*cdf0e10cSrcweir 		sal_Int16 nCommentLen;
785*cdf0e10cSrcweir 
786*cdf0e10cSrcweir 		for (nCount = 0 ; nCount < nTotal; nCount++)
787*cdf0e10cSrcweir 		{
788*cdf0e10cSrcweir 			aMemGrabber >> nTestSig;
789*cdf0e10cSrcweir 			if ( nTestSig != CENSIG )
790*cdf0e10cSrcweir 				throw ZipException(OUString( RTL_CONSTASCII_USTRINGPARAM ( "Invalid CEN header (bad signature)") ), uno::Reference < XInterface > () );
791*cdf0e10cSrcweir 
792*cdf0e10cSrcweir 			aMemGrabber.skipBytes ( 2 );
793*cdf0e10cSrcweir 			aMemGrabber >> aEntry.nVersion;
794*cdf0e10cSrcweir 
795*cdf0e10cSrcweir 			if ( ( aEntry.nVersion & 1 ) == 1 )
796*cdf0e10cSrcweir 				throw ZipException(OUString( RTL_CONSTASCII_USTRINGPARAM ( "Invalid CEN header (encrypted entry)") ), uno::Reference < XInterface > () );
797*cdf0e10cSrcweir 
798*cdf0e10cSrcweir 			aMemGrabber >> aEntry.nFlag;
799*cdf0e10cSrcweir 			aMemGrabber >> aEntry.nMethod;
800*cdf0e10cSrcweir 
801*cdf0e10cSrcweir 			if ( aEntry.nMethod != STORED && aEntry.nMethod != DEFLATED)
802*cdf0e10cSrcweir 				throw ZipException(OUString( RTL_CONSTASCII_USTRINGPARAM ( "Invalid CEN header (bad compression method)") ), uno::Reference < XInterface > () );
803*cdf0e10cSrcweir 
804*cdf0e10cSrcweir 			aMemGrabber >> aEntry.nTime;
805*cdf0e10cSrcweir 			aMemGrabber >> aEntry.nCrc;
806*cdf0e10cSrcweir 			aMemGrabber >> aEntry.nCompressedSize;
807*cdf0e10cSrcweir 			aMemGrabber >> aEntry.nSize;
808*cdf0e10cSrcweir 			aMemGrabber >> aEntry.nPathLen;
809*cdf0e10cSrcweir 			aMemGrabber >> aEntry.nExtraLen;
810*cdf0e10cSrcweir 			aMemGrabber >> nCommentLen;
811*cdf0e10cSrcweir 			aMemGrabber.skipBytes ( 8 );
812*cdf0e10cSrcweir 			aMemGrabber >> aEntry.nOffset;
813*cdf0e10cSrcweir 
814*cdf0e10cSrcweir 			aEntry.nOffset += nLocPos;
815*cdf0e10cSrcweir 			aEntry.nOffset *= -1;
816*cdf0e10cSrcweir 
817*cdf0e10cSrcweir 			if ( aEntry.nPathLen < 0 )
818*cdf0e10cSrcweir 				throw ZipException( OUString( RTL_CONSTASCII_USTRINGPARAM ( "unexpected name length" ) ), uno::Reference < XInterface > () );
819*cdf0e10cSrcweir 
820*cdf0e10cSrcweir 			if ( nCommentLen < 0 )
821*cdf0e10cSrcweir 				throw ZipException( OUString( RTL_CONSTASCII_USTRINGPARAM ( "unexpected comment length" ) ), uno::Reference < XInterface > () );
822*cdf0e10cSrcweir 
823*cdf0e10cSrcweir 			if ( aEntry.nExtraLen < 0 )
824*cdf0e10cSrcweir 				throw ZipException( OUString( RTL_CONSTASCII_USTRINGPARAM ( "unexpected extra header info length") ), uno::Reference < XInterface > () );
825*cdf0e10cSrcweir 
826*cdf0e10cSrcweir             // read always in UTF8, some tools seem not to set UTF8 bit
827*cdf0e10cSrcweir 			aEntry.sPath = rtl::OUString::intern ( (sal_Char *) aMemGrabber.getCurrentPos(),
828*cdf0e10cSrcweir                                                    aEntry.nPathLen,
829*cdf0e10cSrcweir                                                    RTL_TEXTENCODING_UTF8 );
830*cdf0e10cSrcweir 
831*cdf0e10cSrcweir             if ( !::comphelper::OStorageHelper::IsValidZipEntryFileName( aEntry.sPath, sal_True ) )
832*cdf0e10cSrcweir 				throw ZipException( OUString( RTL_CONSTASCII_USTRINGPARAM ( "Zip entry has an invalid name.") ), uno::Reference < XInterface > () );
833*cdf0e10cSrcweir 
834*cdf0e10cSrcweir 			aMemGrabber.skipBytes( aEntry.nPathLen + aEntry.nExtraLen + nCommentLen );
835*cdf0e10cSrcweir 			aEntries[aEntry.sPath] = aEntry;
836*cdf0e10cSrcweir 		}
837*cdf0e10cSrcweir 
838*cdf0e10cSrcweir 		if (nCount != nTotal)
839*cdf0e10cSrcweir 			throw ZipException(OUString( RTL_CONSTASCII_USTRINGPARAM ( "Count != Total") ), uno::Reference < XInterface > () );
840*cdf0e10cSrcweir 	}
841*cdf0e10cSrcweir 	catch ( IllegalArgumentException & )
842*cdf0e10cSrcweir 	{
843*cdf0e10cSrcweir 		// seek can throw this...
844*cdf0e10cSrcweir 		nCenPos = -1; // make sure we return -1 to indicate an error
845*cdf0e10cSrcweir 	}
846*cdf0e10cSrcweir 	return nCenPos;
847*cdf0e10cSrcweir }
848*cdf0e10cSrcweir 
849*cdf0e10cSrcweir sal_Int32 ZipFile::recover()
850*cdf0e10cSrcweir 	throw(IOException, ZipException, RuntimeException)
851*cdf0e10cSrcweir {
852*cdf0e10cSrcweir     ::osl::MutexGuard aGuard( m_aMutex );
853*cdf0e10cSrcweir 
854*cdf0e10cSrcweir 	sal_Int32 nLength;
855*cdf0e10cSrcweir 	Sequence < sal_Int8 > aBuffer;
856*cdf0e10cSrcweir 	Sequence < sal_Int32 > aHeaderOffsets;
857*cdf0e10cSrcweir 
858*cdf0e10cSrcweir 	try
859*cdf0e10cSrcweir 	{
860*cdf0e10cSrcweir 		nLength = static_cast <sal_Int32 > (aGrabber.getLength());
861*cdf0e10cSrcweir 		if (nLength == 0 || nLength < ENDHDR)
862*cdf0e10cSrcweir 			return -1;
863*cdf0e10cSrcweir 
864*cdf0e10cSrcweir 		aGrabber.seek( 0 );
865*cdf0e10cSrcweir 
866*cdf0e10cSrcweir 		const sal_Int32 nToRead = 32000;
867*cdf0e10cSrcweir         for( sal_Int32 nGenPos = 0; aGrabber.readBytes( aBuffer, nToRead ) && aBuffer.getLength() > 16; )
868*cdf0e10cSrcweir         {
869*cdf0e10cSrcweir             const sal_Int8 *pBuffer = aBuffer.getConstArray();
870*cdf0e10cSrcweir             sal_Int32 nBufSize = aBuffer.getLength();
871*cdf0e10cSrcweir 
872*cdf0e10cSrcweir             sal_Int32 nPos = 0;
873*cdf0e10cSrcweir             // the buffer should contain at least one header,
874*cdf0e10cSrcweir             // or if it is end of the file, at least the postheader with sizes and hash
875*cdf0e10cSrcweir             while( nPos < nBufSize - 30
876*cdf0e10cSrcweir                 || ( aBuffer.getLength() < nToRead && nPos < nBufSize - 16 ) )
877*cdf0e10cSrcweir 
878*cdf0e10cSrcweir 			{
879*cdf0e10cSrcweir 				if ( nPos < nBufSize - 30 && pBuffer[nPos] == 'P' && pBuffer[nPos+1] == 'K' && pBuffer[nPos+2] == 3 && pBuffer[nPos+3] == 4 )
880*cdf0e10cSrcweir 				{
881*cdf0e10cSrcweir 					ZipEntry aEntry;
882*cdf0e10cSrcweir 					MemoryByteGrabber aMemGrabber ( Sequence< sal_Int8 >( ((sal_Int8*)(&(pBuffer[nPos+4]))), 26 ) );
883*cdf0e10cSrcweir 
884*cdf0e10cSrcweir 					aMemGrabber >> aEntry.nVersion;
885*cdf0e10cSrcweir 					if ( ( aEntry.nVersion & 1 ) != 1 )
886*cdf0e10cSrcweir 					{
887*cdf0e10cSrcweir 						aMemGrabber >> aEntry.nFlag;
888*cdf0e10cSrcweir 						aMemGrabber >> aEntry.nMethod;
889*cdf0e10cSrcweir 
890*cdf0e10cSrcweir 						if ( aEntry.nMethod == STORED || aEntry.nMethod == DEFLATED )
891*cdf0e10cSrcweir 						{
892*cdf0e10cSrcweir 							aMemGrabber >> aEntry.nTime;
893*cdf0e10cSrcweir 							aMemGrabber >> aEntry.nCrc;
894*cdf0e10cSrcweir 							aMemGrabber >> aEntry.nCompressedSize;
895*cdf0e10cSrcweir 							aMemGrabber >> aEntry.nSize;
896*cdf0e10cSrcweir 							aMemGrabber >> aEntry.nPathLen;
897*cdf0e10cSrcweir 							aMemGrabber >> aEntry.nExtraLen;
898*cdf0e10cSrcweir 
899*cdf0e10cSrcweir 							sal_Int32 nDescrLength =
900*cdf0e10cSrcweir 								( aEntry.nMethod == DEFLATED && ( aEntry.nFlag & 8 ) ) ?
901*cdf0e10cSrcweir 														16 : 0;
902*cdf0e10cSrcweir 
903*cdf0e10cSrcweir 
904*cdf0e10cSrcweir 							// This is a quick fix for OOo1.1RC
905*cdf0e10cSrcweir 							// For OOo2.0 the whole package must be switched to unsigned values
906*cdf0e10cSrcweir 							if ( aEntry.nCompressedSize < 0 ) aEntry.nCompressedSize = 0x7FFFFFFF;
907*cdf0e10cSrcweir 							if ( aEntry.nSize < 0 ) aEntry.nSize = 0x7FFFFFFF;
908*cdf0e10cSrcweir 							if ( aEntry.nPathLen < 0 ) aEntry.nPathLen = 0x7FFF;
909*cdf0e10cSrcweir 							if ( aEntry.nExtraLen < 0 ) aEntry.nExtraLen = 0x7FFF;
910*cdf0e10cSrcweir 							// End of quick fix
911*cdf0e10cSrcweir 
912*cdf0e10cSrcweir 							sal_Int32 nDataSize = ( aEntry.nMethod == DEFLATED ) ? aEntry.nCompressedSize : aEntry.nSize;
913*cdf0e10cSrcweir 							sal_Int32 nBlockLength = nDataSize + aEntry.nPathLen + aEntry.nExtraLen + 30 + nDescrLength;
914*cdf0e10cSrcweir 							if ( aEntry.nPathLen >= 0 && aEntry.nExtraLen >= 0
915*cdf0e10cSrcweir 								&& ( nGenPos + nPos + nBlockLength ) <= nLength )
916*cdf0e10cSrcweir 							{
917*cdf0e10cSrcweir                                 // read always in UTF8, some tools seem not to set UTF8 bit
918*cdf0e10cSrcweir 								if( nPos + 30 + aEntry.nPathLen <= nBufSize )
919*cdf0e10cSrcweir 									aEntry.sPath = OUString ( (sal_Char *) &pBuffer[nPos + 30],
920*cdf0e10cSrcweir 									  							aEntry.nPathLen,
921*cdf0e10cSrcweir 																RTL_TEXTENCODING_UTF8 );
922*cdf0e10cSrcweir 								else
923*cdf0e10cSrcweir 								{
924*cdf0e10cSrcweir 									Sequence < sal_Int8 > aFileName;
925*cdf0e10cSrcweir 									aGrabber.seek( nGenPos + nPos + 30 );
926*cdf0e10cSrcweir 									aGrabber.readBytes( aFileName, aEntry.nPathLen );
927*cdf0e10cSrcweir 									aEntry.sPath = OUString ( (sal_Char *) aFileName.getArray(),
928*cdf0e10cSrcweir 																aFileName.getLength(),
929*cdf0e10cSrcweir 																RTL_TEXTENCODING_UTF8 );
930*cdf0e10cSrcweir 									aEntry.nPathLen = static_cast< sal_Int16 >(aFileName.getLength());
931*cdf0e10cSrcweir 								}
932*cdf0e10cSrcweir 
933*cdf0e10cSrcweir 								aEntry.nOffset = nGenPos + nPos + 30 + aEntry.nPathLen + aEntry.nExtraLen;
934*cdf0e10cSrcweir 
935*cdf0e10cSrcweir 								if ( ( aEntry.nSize || aEntry.nCompressedSize ) && !checkSizeAndCRC( aEntry ) )
936*cdf0e10cSrcweir 								{
937*cdf0e10cSrcweir 									aEntry.nCrc = 0;
938*cdf0e10cSrcweir 									aEntry.nCompressedSize = 0;
939*cdf0e10cSrcweir 									aEntry.nSize = 0;
940*cdf0e10cSrcweir 								}
941*cdf0e10cSrcweir 
942*cdf0e10cSrcweir 								if ( aEntries.find( aEntry.sPath ) == aEntries.end() )
943*cdf0e10cSrcweir 									aEntries[aEntry.sPath] = aEntry;
944*cdf0e10cSrcweir 							}
945*cdf0e10cSrcweir 						}
946*cdf0e10cSrcweir 					}
947*cdf0e10cSrcweir 
948*cdf0e10cSrcweir 					nPos += 4;
949*cdf0e10cSrcweir 				}
950*cdf0e10cSrcweir 				else if (pBuffer[nPos] == 'P' && pBuffer[nPos+1] == 'K' && pBuffer[nPos+2] == 7 && pBuffer[nPos+3] == 8 )
951*cdf0e10cSrcweir 				{
952*cdf0e10cSrcweir 					sal_Int32 nCompressedSize, nSize, nCRC32;
953*cdf0e10cSrcweir 					MemoryByteGrabber aMemGrabber ( Sequence< sal_Int8 >( ((sal_Int8*)(&(pBuffer[nPos+4]))), 12 ) );
954*cdf0e10cSrcweir 					aMemGrabber >> nCRC32;
955*cdf0e10cSrcweir 					aMemGrabber >> nCompressedSize;
956*cdf0e10cSrcweir 					aMemGrabber >> nSize;
957*cdf0e10cSrcweir 
958*cdf0e10cSrcweir 					for( EntryHash::iterator aIter = aEntries.begin(); aIter != aEntries.end(); aIter++ )
959*cdf0e10cSrcweir 					{
960*cdf0e10cSrcweir 						ZipEntry aTmp = (*aIter).second;
961*cdf0e10cSrcweir 
962*cdf0e10cSrcweir                         // this is a broken package, accept this block not only for DEFLATED streams
963*cdf0e10cSrcweir 						if( (*aIter).second.nFlag & 8 )
964*cdf0e10cSrcweir 						{
965*cdf0e10cSrcweir 							sal_Int32 nStreamOffset = nGenPos + nPos - nCompressedSize;
966*cdf0e10cSrcweir 							if ( nStreamOffset == (*aIter).second.nOffset && nCompressedSize > (*aIter).second.nCompressedSize )
967*cdf0e10cSrcweir 							{
968*cdf0e10cSrcweir                                 // only DEFLATED blocks need to be checked
969*cdf0e10cSrcweir                                 sal_Bool bAcceptBlock = ( (*aIter).second.nMethod == STORED && nCompressedSize == nSize );
970*cdf0e10cSrcweir 
971*cdf0e10cSrcweir                                 if ( !bAcceptBlock )
972*cdf0e10cSrcweir                                 {
973*cdf0e10cSrcweir                                     sal_Int32 nRealSize = 0, nRealCRC = 0;
974*cdf0e10cSrcweir                                     getSizeAndCRC( nStreamOffset, nCompressedSize, &nRealSize, &nRealCRC );
975*cdf0e10cSrcweir                                     bAcceptBlock = ( nRealSize == nSize && nRealCRC == nCRC32 );
976*cdf0e10cSrcweir                                 }
977*cdf0e10cSrcweir 
978*cdf0e10cSrcweir                                 if ( bAcceptBlock )
979*cdf0e10cSrcweir 								{
980*cdf0e10cSrcweir 									(*aIter).second.nCrc = nCRC32;
981*cdf0e10cSrcweir 									(*aIter).second.nCompressedSize = nCompressedSize;
982*cdf0e10cSrcweir 									(*aIter).second.nSize = nSize;
983*cdf0e10cSrcweir 								}
984*cdf0e10cSrcweir 							}
985*cdf0e10cSrcweir #if 0
986*cdf0e10cSrcweir // for now ignore clearly broken streams
987*cdf0e10cSrcweir 							else if( !(*aIter).second.nCompressedSize )
988*cdf0e10cSrcweir 							{
989*cdf0e10cSrcweir 								(*aIter).second.nCrc = nCRC32;
990*cdf0e10cSrcweir 								sal_Int32 nRealStreamSize = nGenPos + nPos - (*aIter).second.nOffset;
991*cdf0e10cSrcweir 								(*aIter).second.nCompressedSize = nGenPos + nPos - (*aIter).second.nOffset;
992*cdf0e10cSrcweir 								(*aIter).second.nSize = nSize;
993*cdf0e10cSrcweir 							}
994*cdf0e10cSrcweir #endif
995*cdf0e10cSrcweir 						}
996*cdf0e10cSrcweir 					}
997*cdf0e10cSrcweir 
998*cdf0e10cSrcweir 					nPos += 4;
999*cdf0e10cSrcweir 				}
1000*cdf0e10cSrcweir 				else
1001*cdf0e10cSrcweir 					nPos++;
1002*cdf0e10cSrcweir 			}
1003*cdf0e10cSrcweir 
1004*cdf0e10cSrcweir 			nGenPos += nPos;
1005*cdf0e10cSrcweir 			aGrabber.seek( nGenPos );
1006*cdf0e10cSrcweir 		}
1007*cdf0e10cSrcweir 
1008*cdf0e10cSrcweir 		return 0;
1009*cdf0e10cSrcweir 	}
1010*cdf0e10cSrcweir 	catch ( IllegalArgumentException& )
1011*cdf0e10cSrcweir 	{
1012*cdf0e10cSrcweir 		throw ZipException( OUString( RTL_CONSTASCII_USTRINGPARAM ( "Zip END signature not found!") ), uno::Reference < XInterface > () );
1013*cdf0e10cSrcweir 	}
1014*cdf0e10cSrcweir 	catch ( NotConnectedException& )
1015*cdf0e10cSrcweir 	{
1016*cdf0e10cSrcweir 		throw ZipException( OUString( RTL_CONSTASCII_USTRINGPARAM ( "Zip END signature not found!") ), uno::Reference < XInterface > () );
1017*cdf0e10cSrcweir 	}
1018*cdf0e10cSrcweir 	catch ( BufferSizeExceededException& )
1019*cdf0e10cSrcweir 	{
1020*cdf0e10cSrcweir 		throw ZipException( OUString( RTL_CONSTASCII_USTRINGPARAM ( "Zip END signature not found!") ), uno::Reference < XInterface > () );
1021*cdf0e10cSrcweir 	}
1022*cdf0e10cSrcweir }
1023*cdf0e10cSrcweir 
1024*cdf0e10cSrcweir sal_Bool ZipFile::checkSizeAndCRC( const ZipEntry& aEntry )
1025*cdf0e10cSrcweir {
1026*cdf0e10cSrcweir     ::osl::MutexGuard aGuard( m_aMutex );
1027*cdf0e10cSrcweir 
1028*cdf0e10cSrcweir 	sal_Int32 nSize = 0, nCRC = 0;
1029*cdf0e10cSrcweir 
1030*cdf0e10cSrcweir 	if( aEntry.nMethod == STORED )
1031*cdf0e10cSrcweir 		return ( getCRC( aEntry.nOffset, aEntry.nSize ) == aEntry.nCrc );
1032*cdf0e10cSrcweir 
1033*cdf0e10cSrcweir 	getSizeAndCRC( aEntry.nOffset, aEntry.nCompressedSize, &nSize, &nCRC );
1034*cdf0e10cSrcweir 	return ( aEntry.nSize == nSize && aEntry.nCrc == nCRC );
1035*cdf0e10cSrcweir }
1036*cdf0e10cSrcweir 
1037*cdf0e10cSrcweir sal_Int32 ZipFile::getCRC( sal_Int32 nOffset, sal_Int32 nSize )
1038*cdf0e10cSrcweir {
1039*cdf0e10cSrcweir     ::osl::MutexGuard aGuard( m_aMutex );
1040*cdf0e10cSrcweir 
1041*cdf0e10cSrcweir 	Sequence < sal_Int8 > aBuffer;
1042*cdf0e10cSrcweir 	CRC32 aCRC;
1043*cdf0e10cSrcweir 	sal_Int32 nBlockSize = ::std::min( nSize, static_cast< sal_Int32 >( 32000 ) );
1044*cdf0e10cSrcweir 
1045*cdf0e10cSrcweir 	aGrabber.seek( nOffset );
1046*cdf0e10cSrcweir 	for ( int ind = 0;
1047*cdf0e10cSrcweir 		  aGrabber.readBytes( aBuffer, nBlockSize ) && ind * nBlockSize < nSize;
1048*cdf0e10cSrcweir 		  ind++ )
1049*cdf0e10cSrcweir 	{
1050*cdf0e10cSrcweir 		aCRC.updateSegment( aBuffer, 0, ::std::min( nBlockSize, nSize - ind * nBlockSize ) );
1051*cdf0e10cSrcweir 	}
1052*cdf0e10cSrcweir 
1053*cdf0e10cSrcweir 	return aCRC.getValue();
1054*cdf0e10cSrcweir }
1055*cdf0e10cSrcweir 
1056*cdf0e10cSrcweir void ZipFile::getSizeAndCRC( sal_Int32 nOffset, sal_Int32 nCompressedSize, sal_Int32 *nSize, sal_Int32 *nCRC )
1057*cdf0e10cSrcweir {
1058*cdf0e10cSrcweir     ::osl::MutexGuard aGuard( m_aMutex );
1059*cdf0e10cSrcweir 
1060*cdf0e10cSrcweir 	Sequence < sal_Int8 > aBuffer;
1061*cdf0e10cSrcweir 	CRC32 aCRC;
1062*cdf0e10cSrcweir 	sal_Int32 nRealSize = 0;
1063*cdf0e10cSrcweir 	Inflater aInflaterLocal( sal_True );
1064*cdf0e10cSrcweir 	sal_Int32 nBlockSize = ::std::min( nCompressedSize, static_cast< sal_Int32 >( 32000 ) );
1065*cdf0e10cSrcweir 
1066*cdf0e10cSrcweir 	aGrabber.seek( nOffset );
1067*cdf0e10cSrcweir 	for ( int ind = 0;
1068*cdf0e10cSrcweir 		  !aInflaterLocal.finished() && aGrabber.readBytes( aBuffer, nBlockSize ) && ind * nBlockSize < nCompressedSize;
1069*cdf0e10cSrcweir 		  ind++ )
1070*cdf0e10cSrcweir 	{
1071*cdf0e10cSrcweir 		Sequence < sal_Int8 > aData( nBlockSize );
1072*cdf0e10cSrcweir 		sal_Int32 nLastInflated = 0;
1073*cdf0e10cSrcweir 		sal_Int32 nInBlock = 0;
1074*cdf0e10cSrcweir 
1075*cdf0e10cSrcweir 		aInflaterLocal.setInput( aBuffer );
1076*cdf0e10cSrcweir 		do
1077*cdf0e10cSrcweir 		{
1078*cdf0e10cSrcweir 			nLastInflated = aInflaterLocal.doInflateSegment( aData, 0, nBlockSize );
1079*cdf0e10cSrcweir 			aCRC.updateSegment( aData, 0, nLastInflated );
1080*cdf0e10cSrcweir 			nInBlock += nLastInflated;
1081*cdf0e10cSrcweir 		} while( !aInflater.finished() && nLastInflated );
1082*cdf0e10cSrcweir 
1083*cdf0e10cSrcweir 		nRealSize += nInBlock;
1084*cdf0e10cSrcweir 	}
1085*cdf0e10cSrcweir 
1086*cdf0e10cSrcweir     *nSize = nRealSize;
1087*cdf0e10cSrcweir     *nCRC = aCRC.getValue();
1088*cdf0e10cSrcweir }
1089*cdf0e10cSrcweir 
1090