1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ZipFile.hxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
33 #include <com/sun/star/packages/zip/ZipException.hpp>
34 #include <com/sun/star/packages/zip/ZipIOException.hpp>
35 #include <com/sun/star/packages/NoEncryptionException.hpp>
36 #include <com/sun/star/packages/WrongPasswordException.hpp>
37 #include <ByteGrabber.hxx>
38 #include <HashMaps.hxx>
40 #include <Inflater.hxx>
43 #include <mutexholder.hxx>
45 namespace com
{ namespace sun
{ namespace star
{
46 namespace lang
{ class XMultiServiceFactory
; }
47 namespace ucb
{ class XProgressHandler
; }
51 template < class T
> class ORef
;
54 * We impose arbitrary but reasonable limit on ZIP files.
57 #define ZIP_MAXNAMELEN 512
58 #define ZIP_MAXEXTRA 256
59 #define ZIP_MAXENTRIES (0x10000 - 2)
61 typedef void* rtlCipher
;
68 ::rtl::OUString sComment
; /* zip file comment */
72 com::sun::star::uno::Reference
< com::sun::star::io::XInputStream
> xStream
;
73 com::sun::star::uno::Reference
< com::sun::star::io::XSeekable
> xSeek
;
74 const ::com::sun::star::uno::Reference
< com::sun::star::lang::XMultiServiceFactory
> xFactory
;
75 ::com::sun::star::uno::Reference
< ::com::sun::star::ucb::XProgressHandler
> xProgressHandler
;
77 sal_Bool bRecoveryMode
;
79 com::sun::star::uno::Reference
< com::sun::star::io::XInputStream
> createMemoryStream(
81 const vos::ORef
< EncryptionData
> &rData
,
85 com::sun::star::uno::Reference
< com::sun::star::io::XInputStream
> createFileStream(
87 const vos::ORef
< EncryptionData
> &rData
,
91 // aMediaType parameter is used only for raw stream header creation
92 com::sun::star::uno::Reference
< com::sun::star::io::XInputStream
> createUnbufferedStream(
93 SotMutexHolderRef aMutexHolder
,
95 const vos::ORef
< EncryptionData
> &rData
,
98 ::rtl::OUString aMediaType
= ::rtl::OUString() );
100 sal_Bool
hasValidPassword ( ZipEntry
& rEntry
, const vos::ORef
< EncryptionData
> &rData
);
102 sal_Bool
checkSizeAndCRC( const ZipEntry
& aEntry
);
104 sal_Int32
getCRC( sal_Int32 nOffset
, sal_Int32 nSize
);
106 void getSizeAndCRC( sal_Int32 nOffset
, sal_Int32 nCompressedSize
, sal_Int32
*nSize
, sal_Int32
*nCRC
);
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
,
114 throw(::com::sun::star::io::IOException
, com::sun::star::packages::zip::ZipException
, com::sun::star::uno::RuntimeException
);
116 ZipFile( com::sun::star::uno::Reference
< com::sun::star::io::XInputStream
> &xInput
,
117 const com::sun::star::uno::Reference
< com::sun::star::lang::XMultiServiceFactory
> &xNewFactory
,
118 sal_Bool bInitialise
,
119 sal_Bool bForceRecover
,
120 ::com::sun::star::uno::Reference
< ::com::sun::star::ucb::XProgressHandler
> xProgress
122 throw(::com::sun::star::io::IOException
, com::sun::star::packages::zip::ZipException
, com::sun::star::uno::RuntimeException
);
126 EntryHash
& GetEntryHash() { return aEntries
; }
128 void setInputStream ( com::sun::star::uno::Reference
< com::sun::star::io::XInputStream
> xNewStream
);
129 ::com::sun::star::uno::Reference
< ::com::sun::star::io::XInputStream
> SAL_CALL
getRawData(
131 const vos::ORef
< EncryptionData
> &rData
,
133 SotMutexHolderRef aMutexHolder
)
134 throw(::com::sun::star::io::IOException
, ::com::sun::star::packages::zip::ZipException
, ::com::sun::star::uno::RuntimeException
);
136 static sal_Bool
StaticGetCipher ( const vos::ORef
< EncryptionData
> & xEncryptionData
, rtlCipher
&rCipher
, sal_Bool bDecode
);
138 static void StaticFillHeader ( const vos::ORef
< EncryptionData
> & rData
,
140 const ::rtl::OUString
& aMediaType
,
141 sal_Int8
* & pHeader
);
143 static sal_Bool
StaticFillData ( vos::ORef
< EncryptionData
> & rData
,
145 ::rtl::OUString
& aMediaType
,
146 ::com::sun::star::uno::Reference
< com::sun::star::io::XInputStream
> &rStream
);
148 static ::com::sun::star::uno::Reference
< ::com::sun::star::io::XInputStream
> StaticGetDataFromRawStream(
149 const ::com::sun::star::uno::Reference
< ::com::sun::star::io::XInputStream
>& xStream
,
150 const vos::ORef
< EncryptionData
> &rData
)
151 throw ( ::com::sun::star::packages::WrongPasswordException
,
152 ::com::sun::star::packages::zip::ZipIOException
,
153 ::com::sun::star::uno::RuntimeException
);
155 static sal_Bool
StaticHasValidPassword ( const ::com::sun::star::uno::Sequence
< sal_Int8
> &aReadBuffer
,
156 const vos::ORef
< EncryptionData
> &rData
);
159 ::com::sun::star::uno::Reference
< ::com::sun::star::io::XInputStream
> SAL_CALL
getInputStream(
161 const vos::ORef
< EncryptionData
> &rData
,
163 SotMutexHolderRef aMutexHolder
)
164 throw(::com::sun::star::io::IOException
, ::com::sun::star::packages::zip::ZipException
, ::com::sun::star::uno::RuntimeException
);
166 ::com::sun::star::uno::Reference
< ::com::sun::star::io::XInputStream
> SAL_CALL
getDataStream(
168 const vos::ORef
< EncryptionData
> &rData
,
170 SotMutexHolderRef aMutexHolder
)
171 throw ( ::com::sun::star::packages::WrongPasswordException
,
172 ::com::sun::star::io::IOException
,
173 ::com::sun::star::packages::zip::ZipException
,
174 ::com::sun::star::uno::RuntimeException
);
176 ::com::sun::star::uno::Reference
< ::com::sun::star::io::XInputStream
> SAL_CALL
getWrappedRawStream(
178 const vos::ORef
< EncryptionData
> &rData
,
179 const ::rtl::OUString
& aMediaType
,
180 SotMutexHolderRef aMutexHolder
)
181 throw ( ::com::sun::star::packages::NoEncryptionException
,
182 ::com::sun::star::io::IOException
,
183 ::com::sun::star::packages::zip::ZipException
,
184 ::com::sun::star::uno::RuntimeException
);
186 ZipEnumeration
* SAL_CALL
entries( );
188 sal_Bool
readLOC ( ZipEntry
&rEntry
)
189 throw(::com::sun::star::io::IOException
, com::sun::star::packages::zip::ZipException
, com::sun::star::uno::RuntimeException
);
191 throw(::com::sun::star::io::IOException
, com::sun::star::packages::zip::ZipException
, com::sun::star::uno::RuntimeException
);
193 throw(::com::sun::star::io::IOException
, com::sun::star::packages::zip::ZipException
, com::sun::star::uno::RuntimeException
);
195 throw(::com::sun::star::io::IOException
, com::sun::star::packages::zip::ZipException
, com::sun::star::uno::RuntimeException
);