xref: /aoo41x/main/package/inc/ZipFile.hxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 #ifndef _ZIP_FILE_HXX
28 #define _ZIP_FILE_HXX
29 
30 #include <com/sun/star/packages/zip/ZipException.hpp>
31 #include <com/sun/star/packages/zip/ZipIOException.hpp>
32 #include <com/sun/star/packages/NoEncryptionException.hpp>
33 #include <com/sun/star/packages/WrongPasswordException.hpp>
34 #include <com/sun/star/xml/crypto/XCipherContext.hpp>
35 #include <com/sun/star/xml/crypto/XDigestContext.hpp>
36 
37 #include <rtl/ref.hxx>
38 
39 #include <ByteGrabber.hxx>
40 #include <HashMaps.hxx>
41 #include <Inflater.hxx>
42 #include <EncryptionData.hxx>
43 
44 #include <mutexholder.hxx>
45 
46 namespace com { namespace sun { namespace star {
47 	namespace lang { class XMultiServiceFactory; }
48 	namespace ucb  { class XProgressHandler; }
49 } } }
50 
51 /*
52  * We impose arbitrary but reasonable limit on ZIP files.
53  */
54 
55 #define ZIP_MAXNAMELEN 512
56 #define ZIP_MAXEXTRA 256
57 #define ZIP_MAXENTRIES (0x10000 - 2)
58 
59 class ZipEnumeration;
60 
61 class ZipFile
62 {
63 protected:
64     ::osl::Mutex    m_aMutex;
65 
66     ::rtl::OUString	sComment; 	  	/* zip file comment */
67 	EntryHash		aEntries;
68 	ByteGrabber 	aGrabber;
69     Inflater        aInflater;
70 	com::sun::star::uno::Reference < com::sun::star::io::XInputStream > xStream;
71 	com::sun::star::uno::Reference < com::sun::star::io::XSeekable > xSeek;
72 	const ::com::sun::star::uno::Reference < com::sun::star::lang::XMultiServiceFactory > m_xFactory;
73 	::com::sun::star::uno::Reference < ::com::sun::star::ucb::XProgressHandler > xProgressHandler;
74 
75 	sal_Bool bRecoveryMode;
76 
77 	com::sun::star::uno::Reference < com::sun::star::io::XInputStream >  createMemoryStream(
78 			ZipEntry & rEntry,
79 			const ::rtl::Reference < EncryptionData > &rData,
80 			sal_Bool bRawStream,
81 			sal_Bool bDecrypt );
82 
83 	com::sun::star::uno::Reference < com::sun::star::io::XInputStream >  createFileStream(
84 			ZipEntry & rEntry,
85 			const ::rtl::Reference < EncryptionData > &rData,
86 			sal_Bool bRawStream,
87 			sal_Bool bDecrypt );
88 
89 	// aMediaType parameter is used only for raw stream header creation
90 	com::sun::star::uno::Reference < com::sun::star::io::XInputStream >  createUnbufferedStream(
91             SotMutexHolderRef aMutexHolder,
92 			ZipEntry & rEntry,
93 			const ::rtl::Reference < EncryptionData > &rData,
94 			sal_Int8 nStreamMode,
95 			sal_Bool bDecrypt,
96 			::rtl::OUString aMediaType = ::rtl::OUString() );
97 
98 	sal_Bool hasValidPassword ( ZipEntry & rEntry, const ::rtl::Reference < EncryptionData > &rData );
99 
100 	sal_Bool checkSizeAndCRC( const ZipEntry& aEntry );
101 
102 	sal_Int32 getCRC( sal_Int32 nOffset, sal_Int32 nSize );
103 
104 	void getSizeAndCRC( sal_Int32 nOffset, sal_Int32 nCompressedSize, sal_Int32 *nSize, sal_Int32 *nCRC );
105 
106 public:
107 
108 	ZipFile( com::sun::star::uno::Reference < com::sun::star::io::XInputStream > &xInput,
109 			 const com::sun::star::uno::Reference < com::sun::star::lang::XMultiServiceFactory > &xNewFactory,
110 			 sal_Bool bInitialise
111 			 )
112 		throw(::com::sun::star::io::IOException, com::sun::star::packages::zip::ZipException, com::sun::star::uno::RuntimeException);
113 
114 	ZipFile( com::sun::star::uno::Reference < com::sun::star::io::XInputStream > &xInput,
115 			 const com::sun::star::uno::Reference < com::sun::star::lang::XMultiServiceFactory > &xNewFactory,
116 			 sal_Bool bInitialise,
117 			 sal_Bool bForceRecover,
118 			 ::com::sun::star::uno::Reference < ::com::sun::star::ucb::XProgressHandler > xProgress
119 			 )
120 		throw(::com::sun::star::io::IOException, com::sun::star::packages::zip::ZipException, com::sun::star::uno::RuntimeException);
121 
122 	~ZipFile();
123 
124 	EntryHash& GetEntryHash() { return aEntries; }
125 
126 	void setInputStream ( com::sun::star::uno::Reference < com::sun::star::io::XInputStream > xNewStream );
127     ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > SAL_CALL getRawData(
128 			ZipEntry& rEntry,
129 			const ::rtl::Reference < EncryptionData > &rData,
130 			sal_Bool bDecrypt,
131             SotMutexHolderRef aMutexHolder )
132 		throw(::com::sun::star::io::IOException, ::com::sun::star::packages::zip::ZipException, ::com::sun::star::uno::RuntimeException);
133 
134 
135     static ::com::sun::star::uno::Reference< ::com::sun::star::xml::crypto::XDigestContext > StaticGetDigestContextForChecksum(
136             const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xArgFactory,
137             const ::rtl::Reference< EncryptionData >& xEncryptionData );
138 
139     static ::com::sun::star::uno::Reference< ::com::sun::star::xml::crypto::XCipherContext > StaticGetCipher(
140             const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xArgFactory,
141             const ::rtl::Reference< EncryptionData >& xEncryptionData,
142             bool bEncrypt );
143 
144 	static void StaticFillHeader ( const ::rtl::Reference < EncryptionData > & rData,
145 									sal_Int32 nSize,
146 									const ::rtl::OUString& aMediaType,
147 									sal_Int8 * & pHeader );
148 
149 	static sal_Bool StaticFillData ( ::rtl::Reference < BaseEncryptionData > & rData,
150                                      sal_Int32 &rEncAlgorithm,
151                                      sal_Int32 &rChecksumAlgorithm,
152                                      sal_Int32 &rDerivedKeySize,
153                                      sal_Int32 &rStartKeyGenID,
154 									 sal_Int32 &rSize,
155 									 ::rtl::OUString& aMediaType,
156 									 const ::com::sun::star::uno::Reference < com::sun::star::io::XInputStream >& rStream );
157 
158 	static ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > StaticGetDataFromRawStream(
159             const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xFactory,
160 			const ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream >& xStream,
161 			const ::rtl::Reference < EncryptionData > &rData )
162 		throw ( ::com::sun::star::packages::WrongPasswordException,
163 				::com::sun::star::packages::zip::ZipIOException,
164 				::com::sun::star::uno::RuntimeException );
165 
166 	static sal_Bool StaticHasValidPassword (
167             const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xFactory,
168             const ::com::sun::star::uno::Sequence< sal_Int8 > &aReadBuffer,
169             const ::rtl::Reference < EncryptionData > &rData );
170 
171 
172     ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > SAL_CALL getInputStream(
173 			ZipEntry& rEntry,
174 			const ::rtl::Reference < EncryptionData > &rData,
175 			sal_Bool bDecrypt,
176             SotMutexHolderRef aMutexHolder )
177 		throw(::com::sun::star::io::IOException, ::com::sun::star::packages::zip::ZipException, ::com::sun::star::uno::RuntimeException);
178 
179     ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > SAL_CALL getDataStream(
180 			ZipEntry& rEntry,
181 			const ::rtl::Reference < EncryptionData > &rData,
182 			sal_Bool bDecrypt,
183             SotMutexHolderRef aMutexHolder )
184 		throw ( ::com::sun::star::packages::WrongPasswordException,
185 				::com::sun::star::io::IOException,
186 				::com::sun::star::packages::zip::ZipException,
187 				::com::sun::star::uno::RuntimeException );
188 
189     ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > SAL_CALL getWrappedRawStream(
190 			ZipEntry& rEntry,
191 			const ::rtl::Reference < EncryptionData > &rData,
192 			const ::rtl::OUString& aMediaType,
193             SotMutexHolderRef aMutexHolder )
194 		throw ( ::com::sun::star::packages::NoEncryptionException,
195 				::com::sun::star::io::IOException,
196 				::com::sun::star::packages::zip::ZipException,
197 				::com::sun::star::uno::RuntimeException );
198 
199     ZipEnumeration * SAL_CALL entries(  );
200 protected:
201 	sal_Bool		readLOC ( ZipEntry &rEntry)
202 		throw(::com::sun::star::io::IOException, com::sun::star::packages::zip::ZipException, com::sun::star::uno::RuntimeException);
203 	sal_Int32		readCEN()
204 		throw(::com::sun::star::io::IOException, com::sun::star::packages::zip::ZipException, com::sun::star::uno::RuntimeException);
205 	sal_Int32		findEND()
206 		throw(::com::sun::star::io::IOException, com::sun::star::packages::zip::ZipException, com::sun::star::uno::RuntimeException);
207 	sal_Int32 		recover()
208 		throw(::com::sun::star::io::IOException, com::sun::star::packages::zip::ZipException, com::sun::star::uno::RuntimeException);
209 
210 };
211 
212 #endif
213