1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
22 #include <com/sun/star/packages/zip/ZipException.hpp>
23 #include <com/sun/star/packages/zip/ZipIOException.hpp>
24 #include <com/sun/star/packages/NoEncryptionException.hpp>
25 #include <com/sun/star/packages/WrongPasswordException.hpp>
26 #include <com/sun/star/xml/crypto/XCipherContext.hpp>
27 #include <com/sun/star/xml/crypto/XDigestContext.hpp>
29 #include <package/Inflater.hxx>
30 #include <ByteGrabber.hxx>
31 #include <HashMaps.hxx>
32 #include <EncryptionData.hxx>
34 #include <mutexholder.hxx>
36 namespace com
{ namespace sun
{ namespace star
{
37 namespace uno
{ class XComponentContext
; }
38 namespace ucb
{ class XProgressHandler
; }
42 template < class T
> class Reference
;
46 * We impose arbitrary but reasonable limit on ZIP files.
49 #define ZIP_MAXNAMELEN 512
50 #define ZIP_MAXEXTRA 256
51 #define ZIP_MAXENTRIES (0x10000 - 2)
58 ::osl::Mutex m_aMutex
;
60 OUString sComment
; /* zip file comment */
63 ZipUtils::Inflater aInflater
;
64 com::sun::star::uno::Reference
< com::sun::star::io::XInputStream
> xStream
;
65 com::sun::star::uno::Reference
< com::sun::star::io::XSeekable
> xSeek
;
66 const ::com::sun::star::uno::Reference
< com::sun::star::uno::XComponentContext
> m_xContext
;
67 ::com::sun::star::uno::Reference
< ::com::sun::star::ucb::XProgressHandler
> xProgressHandler
;
69 sal_Bool bRecoveryMode
;
71 com::sun::star::uno::Reference
< com::sun::star::io::XInputStream
> createMemoryStream(
73 const ::rtl::Reference
< EncryptionData
> &rData
,
77 com::sun::star::uno::Reference
< com::sun::star::io::XInputStream
> createFileStream(
79 const ::rtl::Reference
< EncryptionData
> &rData
,
83 // aMediaType parameter is used only for raw stream header creation
84 com::sun::star::uno::Reference
< com::sun::star::io::XInputStream
> createUnbufferedStream(
85 SotMutexHolderRef aMutexHolder
,
87 const ::rtl::Reference
< EncryptionData
> &rData
,
90 OUString aMediaType
= OUString() );
92 sal_Bool
hasValidPassword ( ZipEntry
& rEntry
, const rtl::Reference
< EncryptionData
> &rData
);
94 sal_Bool
checkSizeAndCRC( const ZipEntry
& aEntry
);
96 sal_Int32
getCRC( sal_Int64 nOffset
, sal_Int64 nSize
);
98 void getSizeAndCRC( sal_Int64 nOffset
, sal_Int64 nCompressedSize
, sal_Int64
*nSize
, sal_Int32
*nCRC
);
102 ZipFile( com::sun::star::uno::Reference
< com::sun::star::io::XInputStream
> &xInput
,
103 const com::sun::star::uno::Reference
< com::sun::star::uno::XComponentContext
> &rxContext
,
106 throw(::com::sun::star::io::IOException
, com::sun::star::packages::zip::ZipException
, com::sun::star::uno::RuntimeException
);
108 ZipFile( com::sun::star::uno::Reference
< com::sun::star::io::XInputStream
> &xInput
,
109 const com::sun::star::uno::Reference
< com::sun::star::uno::XComponentContext
> &rxContext
,
110 sal_Bool bInitialise
,
111 sal_Bool bForceRecover
,
112 ::com::sun::star::uno::Reference
< ::com::sun::star::ucb::XProgressHandler
> xProgress
114 throw(::com::sun::star::io::IOException
, com::sun::star::packages::zip::ZipException
, com::sun::star::uno::RuntimeException
);
118 EntryHash
& GetEntryHash() { return aEntries
; }
120 void setInputStream ( com::sun::star::uno::Reference
< com::sun::star::io::XInputStream
> xNewStream
);
121 ::com::sun::star::uno::Reference
< ::com::sun::star::io::XInputStream
> SAL_CALL
getRawData(
123 const ::rtl::Reference
< EncryptionData
> &rData
,
125 SotMutexHolderRef aMutexHolder
)
126 throw(::com::sun::star::io::IOException
, ::com::sun::star::packages::zip::ZipException
, ::com::sun::star::uno::RuntimeException
);
129 static ::com::sun::star::uno::Reference
< ::com::sun::star::xml::crypto::XDigestContext
> StaticGetDigestContextForChecksum(
130 const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XComponentContext
>& xArgContext
,
131 const ::rtl::Reference
< EncryptionData
>& xEncryptionData
);
133 static ::com::sun::star::uno::Reference
< ::com::sun::star::xml::crypto::XCipherContext
> StaticGetCipher(
134 const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XComponentContext
>& xArgContext
,
135 const ::rtl::Reference
< EncryptionData
>& xEncryptionData
,
138 static void StaticFillHeader ( const ::rtl::Reference
< EncryptionData
> & rData
,
140 const OUString
& aMediaType
,
141 sal_Int8
* & pHeader
);
143 static sal_Bool
StaticFillData ( ::rtl::Reference
< BaseEncryptionData
> & rData
,
144 sal_Int32
&rEncAlgorithm
,
145 sal_Int32
&rChecksumAlgorithm
,
146 sal_Int32
&rDerivedKeySize
,
147 sal_Int32
&rStartKeyGenID
,
149 OUString
& aMediaType
,
150 const ::com::sun::star::uno::Reference
< com::sun::star::io::XInputStream
>& rStream
);
152 static ::com::sun::star::uno::Reference
< ::com::sun::star::io::XInputStream
> StaticGetDataFromRawStream(
153 const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XComponentContext
>& rxContext
,
154 const ::com::sun::star::uno::Reference
< ::com::sun::star::io::XInputStream
>& xStream
,
155 const ::rtl::Reference
< EncryptionData
> &rData
)
156 throw ( ::com::sun::star::packages::WrongPasswordException
,
157 ::com::sun::star::packages::zip::ZipIOException
,
158 ::com::sun::star::uno::RuntimeException
);
160 static sal_Bool
StaticHasValidPassword (
161 const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XComponentContext
>& rxContext
,
162 const ::com::sun::star::uno::Sequence
< sal_Int8
> &aReadBuffer
,
163 const ::rtl::Reference
< EncryptionData
> &rData
);
166 ::com::sun::star::uno::Reference
< ::com::sun::star::io::XInputStream
> SAL_CALL
getInputStream(
168 const ::rtl::Reference
< EncryptionData
> &rData
,
170 SotMutexHolderRef aMutexHolder
)
171 throw(::com::sun::star::io::IOException
, ::com::sun::star::packages::zip::ZipException
, ::com::sun::star::uno::RuntimeException
);
173 ::com::sun::star::uno::Reference
< ::com::sun::star::io::XInputStream
> SAL_CALL
getDataStream(
175 const ::rtl::Reference
< EncryptionData
> &rData
,
177 SotMutexHolderRef aMutexHolder
)
178 throw ( ::com::sun::star::packages::WrongPasswordException
,
179 ::com::sun::star::io::IOException
,
180 ::com::sun::star::packages::zip::ZipException
,
181 ::com::sun::star::uno::RuntimeException
);
183 ::com::sun::star::uno::Reference
< ::com::sun::star::io::XInputStream
> SAL_CALL
getWrappedRawStream(
185 const ::rtl::Reference
< EncryptionData
> &rData
,
186 const OUString
& aMediaType
,
187 SotMutexHolderRef aMutexHolder
)
188 throw ( ::com::sun::star::packages::NoEncryptionException
,
189 ::com::sun::star::io::IOException
,
190 ::com::sun::star::packages::zip::ZipException
,
191 ::com::sun::star::uno::RuntimeException
);
193 ZipEnumeration
* SAL_CALL
entries( );
195 sal_Bool
readLOC ( ZipEntry
&rEntry
)
196 throw(::com::sun::star::io::IOException
, com::sun::star::packages::zip::ZipException
, com::sun::star::uno::RuntimeException
);
198 throw(::com::sun::star::io::IOException
, com::sun::star::packages::zip::ZipException
, com::sun::star::uno::RuntimeException
);
200 throw(::com::sun::star::io::IOException
, com::sun::star::packages::zip::ZipException
, com::sun::star::uno::RuntimeException
);
202 throw(::com::sun::star::io::IOException
, com::sun::star::packages::zip::ZipException
, com::sun::star::uno::RuntimeException
);
208 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */