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