xref: /AOO41X/main/package/inc/ZipFile.hxx (revision f319bb99b6b251a37b98027a84e8048ce3778b1f)
1*f319bb99SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*f319bb99SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*f319bb99SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*f319bb99SAndrew Rist  * distributed with this work for additional information
6*f319bb99SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*f319bb99SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*f319bb99SAndrew Rist  * "License"); you may not use this file except in compliance
9*f319bb99SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*f319bb99SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*f319bb99SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*f319bb99SAndrew Rist  * software distributed under the License is distributed on an
15*f319bb99SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*f319bb99SAndrew Rist  * KIND, either express or implied.  See the License for the
17*f319bb99SAndrew Rist  * specific language governing permissions and limitations
18*f319bb99SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*f319bb99SAndrew Rist  *************************************************************/
21*f319bb99SAndrew Rist 
22*f319bb99SAndrew Rist 
23cdf0e10cSrcweir #ifndef _ZIP_FILE_HXX
24cdf0e10cSrcweir #define _ZIP_FILE_HXX
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include <com/sun/star/packages/zip/ZipException.hpp>
27cdf0e10cSrcweir #include <com/sun/star/packages/zip/ZipIOException.hpp>
28cdf0e10cSrcweir #include <com/sun/star/packages/NoEncryptionException.hpp>
29cdf0e10cSrcweir #include <com/sun/star/packages/WrongPasswordException.hpp>
30cdf0e10cSrcweir #include <com/sun/star/xml/crypto/XCipherContext.hpp>
31cdf0e10cSrcweir #include <com/sun/star/xml/crypto/XDigestContext.hpp>
32cdf0e10cSrcweir 
33cdf0e10cSrcweir #include <rtl/ref.hxx>
34cdf0e10cSrcweir 
35cdf0e10cSrcweir #include <ByteGrabber.hxx>
36cdf0e10cSrcweir #include <HashMaps.hxx>
37cdf0e10cSrcweir #include <Inflater.hxx>
38cdf0e10cSrcweir #include <EncryptionData.hxx>
39cdf0e10cSrcweir 
40cdf0e10cSrcweir #include <mutexholder.hxx>
41cdf0e10cSrcweir 
42cdf0e10cSrcweir namespace com { namespace sun { namespace star {
43cdf0e10cSrcweir 	namespace lang { class XMultiServiceFactory; }
44cdf0e10cSrcweir 	namespace ucb  { class XProgressHandler; }
45cdf0e10cSrcweir } } }
46cdf0e10cSrcweir 
47cdf0e10cSrcweir /*
48cdf0e10cSrcweir  * We impose arbitrary but reasonable limit on ZIP files.
49cdf0e10cSrcweir  */
50cdf0e10cSrcweir 
51cdf0e10cSrcweir #define ZIP_MAXNAMELEN 512
52cdf0e10cSrcweir #define ZIP_MAXEXTRA 256
53cdf0e10cSrcweir #define ZIP_MAXENTRIES (0x10000 - 2)
54cdf0e10cSrcweir 
55cdf0e10cSrcweir class ZipEnumeration;
56cdf0e10cSrcweir 
57cdf0e10cSrcweir class ZipFile
58cdf0e10cSrcweir {
59cdf0e10cSrcweir protected:
60cdf0e10cSrcweir     ::osl::Mutex    m_aMutex;
61cdf0e10cSrcweir 
62cdf0e10cSrcweir     ::rtl::OUString	sComment; 	  	/* zip file comment */
63cdf0e10cSrcweir 	EntryHash		aEntries;
64cdf0e10cSrcweir 	ByteGrabber 	aGrabber;
65cdf0e10cSrcweir     Inflater        aInflater;
66cdf0e10cSrcweir 	com::sun::star::uno::Reference < com::sun::star::io::XInputStream > xStream;
67cdf0e10cSrcweir 	com::sun::star::uno::Reference < com::sun::star::io::XSeekable > xSeek;
68cdf0e10cSrcweir 	const ::com::sun::star::uno::Reference < com::sun::star::lang::XMultiServiceFactory > m_xFactory;
69cdf0e10cSrcweir 	::com::sun::star::uno::Reference < ::com::sun::star::ucb::XProgressHandler > xProgressHandler;
70cdf0e10cSrcweir 
71cdf0e10cSrcweir 	sal_Bool bRecoveryMode;
72cdf0e10cSrcweir 
73cdf0e10cSrcweir 	com::sun::star::uno::Reference < com::sun::star::io::XInputStream >  createMemoryStream(
74cdf0e10cSrcweir 			ZipEntry & rEntry,
75cdf0e10cSrcweir 			const ::rtl::Reference < EncryptionData > &rData,
76cdf0e10cSrcweir 			sal_Bool bRawStream,
77cdf0e10cSrcweir 			sal_Bool bDecrypt );
78cdf0e10cSrcweir 
79cdf0e10cSrcweir 	com::sun::star::uno::Reference < com::sun::star::io::XInputStream >  createFileStream(
80cdf0e10cSrcweir 			ZipEntry & rEntry,
81cdf0e10cSrcweir 			const ::rtl::Reference < EncryptionData > &rData,
82cdf0e10cSrcweir 			sal_Bool bRawStream,
83cdf0e10cSrcweir 			sal_Bool bDecrypt );
84cdf0e10cSrcweir 
85cdf0e10cSrcweir 	// aMediaType parameter is used only for raw stream header creation
86cdf0e10cSrcweir 	com::sun::star::uno::Reference < com::sun::star::io::XInputStream >  createUnbufferedStream(
87cdf0e10cSrcweir             SotMutexHolderRef aMutexHolder,
88cdf0e10cSrcweir 			ZipEntry & rEntry,
89cdf0e10cSrcweir 			const ::rtl::Reference < EncryptionData > &rData,
90cdf0e10cSrcweir 			sal_Int8 nStreamMode,
91cdf0e10cSrcweir 			sal_Bool bDecrypt,
92cdf0e10cSrcweir 			::rtl::OUString aMediaType = ::rtl::OUString() );
93cdf0e10cSrcweir 
94cdf0e10cSrcweir 	sal_Bool hasValidPassword ( ZipEntry & rEntry, const ::rtl::Reference < EncryptionData > &rData );
95cdf0e10cSrcweir 
96cdf0e10cSrcweir 	sal_Bool checkSizeAndCRC( const ZipEntry& aEntry );
97cdf0e10cSrcweir 
98cdf0e10cSrcweir 	sal_Int32 getCRC( sal_Int32 nOffset, sal_Int32 nSize );
99cdf0e10cSrcweir 
100cdf0e10cSrcweir 	void getSizeAndCRC( sal_Int32 nOffset, sal_Int32 nCompressedSize, sal_Int32 *nSize, sal_Int32 *nCRC );
101cdf0e10cSrcweir 
102cdf0e10cSrcweir public:
103cdf0e10cSrcweir 
104cdf0e10cSrcweir 	ZipFile( com::sun::star::uno::Reference < com::sun::star::io::XInputStream > &xInput,
105cdf0e10cSrcweir 			 const com::sun::star::uno::Reference < com::sun::star::lang::XMultiServiceFactory > &xNewFactory,
106cdf0e10cSrcweir 			 sal_Bool bInitialise
107cdf0e10cSrcweir 			 )
108cdf0e10cSrcweir 		throw(::com::sun::star::io::IOException, com::sun::star::packages::zip::ZipException, com::sun::star::uno::RuntimeException);
109cdf0e10cSrcweir 
110cdf0e10cSrcweir 	ZipFile( com::sun::star::uno::Reference < com::sun::star::io::XInputStream > &xInput,
111cdf0e10cSrcweir 			 const com::sun::star::uno::Reference < com::sun::star::lang::XMultiServiceFactory > &xNewFactory,
112cdf0e10cSrcweir 			 sal_Bool bInitialise,
113cdf0e10cSrcweir 			 sal_Bool bForceRecover,
114cdf0e10cSrcweir 			 ::com::sun::star::uno::Reference < ::com::sun::star::ucb::XProgressHandler > xProgress
115cdf0e10cSrcweir 			 )
116cdf0e10cSrcweir 		throw(::com::sun::star::io::IOException, com::sun::star::packages::zip::ZipException, com::sun::star::uno::RuntimeException);
117cdf0e10cSrcweir 
118cdf0e10cSrcweir 	~ZipFile();
119cdf0e10cSrcweir 
GetEntryHash()120cdf0e10cSrcweir 	EntryHash& GetEntryHash() { return aEntries; }
121cdf0e10cSrcweir 
122cdf0e10cSrcweir 	void setInputStream ( com::sun::star::uno::Reference < com::sun::star::io::XInputStream > xNewStream );
123cdf0e10cSrcweir     ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > SAL_CALL getRawData(
124cdf0e10cSrcweir 			ZipEntry& rEntry,
125cdf0e10cSrcweir 			const ::rtl::Reference < EncryptionData > &rData,
126cdf0e10cSrcweir 			sal_Bool bDecrypt,
127cdf0e10cSrcweir             SotMutexHolderRef aMutexHolder )
128cdf0e10cSrcweir 		throw(::com::sun::star::io::IOException, ::com::sun::star::packages::zip::ZipException, ::com::sun::star::uno::RuntimeException);
129cdf0e10cSrcweir 
130cdf0e10cSrcweir 
131cdf0e10cSrcweir     static ::com::sun::star::uno::Reference< ::com::sun::star::xml::crypto::XDigestContext > StaticGetDigestContextForChecksum(
132cdf0e10cSrcweir             const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xArgFactory,
133cdf0e10cSrcweir             const ::rtl::Reference< EncryptionData >& xEncryptionData );
134cdf0e10cSrcweir 
135cdf0e10cSrcweir     static ::com::sun::star::uno::Reference< ::com::sun::star::xml::crypto::XCipherContext > StaticGetCipher(
136cdf0e10cSrcweir             const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xArgFactory,
137cdf0e10cSrcweir             const ::rtl::Reference< EncryptionData >& xEncryptionData,
138cdf0e10cSrcweir             bool bEncrypt );
139cdf0e10cSrcweir 
140cdf0e10cSrcweir 	static void StaticFillHeader ( const ::rtl::Reference < EncryptionData > & rData,
141cdf0e10cSrcweir 									sal_Int32 nSize,
142cdf0e10cSrcweir 									const ::rtl::OUString& aMediaType,
143cdf0e10cSrcweir 									sal_Int8 * & pHeader );
144cdf0e10cSrcweir 
145cdf0e10cSrcweir 	static sal_Bool StaticFillData ( ::rtl::Reference < BaseEncryptionData > & rData,
146cdf0e10cSrcweir                                      sal_Int32 &rEncAlgorithm,
147cdf0e10cSrcweir                                      sal_Int32 &rChecksumAlgorithm,
148cdf0e10cSrcweir                                      sal_Int32 &rDerivedKeySize,
149cdf0e10cSrcweir                                      sal_Int32 &rStartKeyGenID,
150cdf0e10cSrcweir 									 sal_Int32 &rSize,
151cdf0e10cSrcweir 									 ::rtl::OUString& aMediaType,
152cdf0e10cSrcweir 									 const ::com::sun::star::uno::Reference < com::sun::star::io::XInputStream >& rStream );
153cdf0e10cSrcweir 
154cdf0e10cSrcweir 	static ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > StaticGetDataFromRawStream(
155cdf0e10cSrcweir             const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xFactory,
156cdf0e10cSrcweir 			const ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream >& xStream,
157cdf0e10cSrcweir 			const ::rtl::Reference < EncryptionData > &rData )
158cdf0e10cSrcweir 		throw ( ::com::sun::star::packages::WrongPasswordException,
159cdf0e10cSrcweir 				::com::sun::star::packages::zip::ZipIOException,
160cdf0e10cSrcweir 				::com::sun::star::uno::RuntimeException );
161cdf0e10cSrcweir 
162cdf0e10cSrcweir 	static sal_Bool StaticHasValidPassword (
163cdf0e10cSrcweir             const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xFactory,
164cdf0e10cSrcweir             const ::com::sun::star::uno::Sequence< sal_Int8 > &aReadBuffer,
165cdf0e10cSrcweir             const ::rtl::Reference < EncryptionData > &rData );
166cdf0e10cSrcweir 
167cdf0e10cSrcweir 
168cdf0e10cSrcweir     ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > SAL_CALL getInputStream(
169cdf0e10cSrcweir 			ZipEntry& rEntry,
170cdf0e10cSrcweir 			const ::rtl::Reference < EncryptionData > &rData,
171cdf0e10cSrcweir 			sal_Bool bDecrypt,
172cdf0e10cSrcweir             SotMutexHolderRef aMutexHolder )
173cdf0e10cSrcweir 		throw(::com::sun::star::io::IOException, ::com::sun::star::packages::zip::ZipException, ::com::sun::star::uno::RuntimeException);
174cdf0e10cSrcweir 
175cdf0e10cSrcweir     ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > SAL_CALL getDataStream(
176cdf0e10cSrcweir 			ZipEntry& rEntry,
177cdf0e10cSrcweir 			const ::rtl::Reference < EncryptionData > &rData,
178cdf0e10cSrcweir 			sal_Bool bDecrypt,
179cdf0e10cSrcweir             SotMutexHolderRef aMutexHolder )
180cdf0e10cSrcweir 		throw ( ::com::sun::star::packages::WrongPasswordException,
181cdf0e10cSrcweir 				::com::sun::star::io::IOException,
182cdf0e10cSrcweir 				::com::sun::star::packages::zip::ZipException,
183cdf0e10cSrcweir 				::com::sun::star::uno::RuntimeException );
184cdf0e10cSrcweir 
185cdf0e10cSrcweir     ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > SAL_CALL getWrappedRawStream(
186cdf0e10cSrcweir 			ZipEntry& rEntry,
187cdf0e10cSrcweir 			const ::rtl::Reference < EncryptionData > &rData,
188cdf0e10cSrcweir 			const ::rtl::OUString& aMediaType,
189cdf0e10cSrcweir             SotMutexHolderRef aMutexHolder )
190cdf0e10cSrcweir 		throw ( ::com::sun::star::packages::NoEncryptionException,
191cdf0e10cSrcweir 				::com::sun::star::io::IOException,
192cdf0e10cSrcweir 				::com::sun::star::packages::zip::ZipException,
193cdf0e10cSrcweir 				::com::sun::star::uno::RuntimeException );
194cdf0e10cSrcweir 
195cdf0e10cSrcweir     ZipEnumeration * SAL_CALL entries(  );
196cdf0e10cSrcweir protected:
197cdf0e10cSrcweir 	sal_Bool		readLOC ( ZipEntry &rEntry)
198cdf0e10cSrcweir 		throw(::com::sun::star::io::IOException, com::sun::star::packages::zip::ZipException, com::sun::star::uno::RuntimeException);
199cdf0e10cSrcweir 	sal_Int32		readCEN()
200cdf0e10cSrcweir 		throw(::com::sun::star::io::IOException, com::sun::star::packages::zip::ZipException, com::sun::star::uno::RuntimeException);
201cdf0e10cSrcweir 	sal_Int32		findEND()
202cdf0e10cSrcweir 		throw(::com::sun::star::io::IOException, com::sun::star::packages::zip::ZipException, com::sun::star::uno::RuntimeException);
203cdf0e10cSrcweir 	sal_Int32 		recover()
204cdf0e10cSrcweir 		throw(::com::sun::star::io::IOException, com::sun::star::packages::zip::ZipException, com::sun::star::uno::RuntimeException);
205cdf0e10cSrcweir 
206cdf0e10cSrcweir };
207cdf0e10cSrcweir 
208cdf0e10cSrcweir #endif
209