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 .
19 #ifndef INCLUDED_PACKAGE_INC_ZIPFILE_HXX
20 #define INCLUDED_PACKAGE_INC_ZIPFILE_HXX
22 #include <com/sun/star/xml/crypto/XCipherContext.hpp>
23 #include <com/sun/star/xml/crypto/XDigestContext.hpp>
25 #include <comphelper/refcountedmutex.hxx>
26 #include <package/Inflater.hxx>
27 #include <rtl/ref.hxx>
28 #include "ByteGrabber.hxx"
29 #include "HashMaps.hxx"
30 #include "EncryptionData.hxx"
34 #include <unordered_set>
36 class MemoryByteGrabber
;
37 namespace com::sun::star
{
38 namespace uno
{ class XComponentContext
; }
39 namespace ucb
{ class XProgressHandler
; }
43 template < class T
> class Reference
;
47 * We impose arbitrary but reasonable limit on ZIP files.
50 #define ZIP_MAXNAMELEN 512
51 #define ZIP_MAXENTRIES (0x10000 - 2)
59 enum class Checks
{ Default
, CheckInsensitive
, TryCheckInsensitive
};
62 rtl::Reference
<comphelper::RefCountedMutex
> m_aMutexHolder
;
64 std::unordered_set
<OUString
> m_EntriesInsensitive
;
69 ZipUtils::Inflater aInflater
;
70 css::uno::Reference
< css::io::XInputStream
> xStream
;
71 const css::uno::Reference
< css::uno::XComponentContext
> m_xContext
;
75 // aMediaType parameter is used only for raw stream header creation
76 css::uno::Reference
< css::io::XInputStream
> createStreamForZipEntry(
77 const rtl::Reference
<comphelper::RefCountedMutex
>& aMutexHolder
,
78 ZipEntry
const & rEntry
,
79 const ::rtl::Reference
< EncryptionData
> &rData
,
81 ::std::optional
<sal_Int64
> oDecryptedSize
,
82 const bool bUseBufferedStream
= true,
83 const OUString
& aMediaType
= OUString() );
85 css::uno::Reference
<css::io::XInputStream
> checkValidPassword(
86 ZipEntry
const& rEntry
, rtl::Reference
<EncryptionData
> const& rData
,
87 sal_Int64 nDecryptedSize
,
88 rtl::Reference
<comphelper::RefCountedMutex
> const& rMutexHolder
);
90 bool checkSizeAndCRC( const ZipEntry
& aEntry
);
92 sal_Int32
getCRC( sal_Int64 nOffset
, sal_Int64 nSize
);
94 void getSizeAndCRC( sal_Int64 nOffset
, sal_Int64 nCompressedSize
, sal_Int64
*nSize
, sal_Int32
*nCRC
);
96 sal_uInt64
readLOC(ZipEntry
&rEntry
);
97 sal_uInt64
readLOC_Impl(ZipEntry
&rEntry
, std::vector
<sal_Int8
>& rNameBuffer
, std::vector
<sal_Int8
>& rExtraBuffer
);
99 std::tuple
<sal_Int64
, sal_Int64
, sal_Int64
> findCentralDirectory();
100 void HandlePK34(std::span
<const sal_Int8
> data
, sal_Int64 dataOffset
, sal_Int64 totalSize
);
101 void HandlePK78(std::span
<const sal_Int8
> data
, sal_Int64 dataOffset
);
103 static bool readExtraFields(MemoryByteGrabber
& aMemGrabber
, sal_Int16 nExtraLen
,
104 sal_uInt64
& nSize
, sal_uInt64
& nCompressedSize
,
105 ::std::optional
<sal_uInt64
> & roOffset
,
106 std::string_view
const * pCENFilenameToCheck
);
110 ZipFile( rtl::Reference
<comphelper::RefCountedMutex
> aMutexHolder
,
111 css::uno::Reference
< css::io::XInputStream
> const &xInput
,
112 css::uno::Reference
< css::uno::XComponentContext
> xContext
,
119 EntryHash
& GetEntryHash() { return aEntries
; }
121 void setInputStream ( const css::uno::Reference
< css::io::XInputStream
>& xNewStream
);
122 css::uno::Reference
< css::io::XInputStream
> getRawData(
124 const ::rtl::Reference
< EncryptionData
> &rData
,
125 ::std::optional
<sal_Int64
> oDecryptedSize
,
126 const rtl::Reference
<comphelper::RefCountedMutex
>& aMutexHolder
,
127 const bool bUseBufferedStream
= true );
129 static css::uno::Reference
< css::xml::crypto::XDigestContext
> StaticGetDigestContextForChecksum(
130 const css::uno::Reference
< css::uno::XComponentContext
>& xArgContext
,
131 const ::rtl::Reference
< EncryptionData
>& xEncryptionData
);
133 static css::uno::Reference
< css::xml::crypto::XCipherContext
> StaticGetCipher(
134 const css::uno::Reference
< css::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 bool StaticFillData ( ::rtl::Reference
< BaseEncryptionData
> const & rData
,
144 sal_Int32
&rEncAlgorithm
,
145 sal_Int32
&rChecksumAlgorithm
,
146 sal_Int32
&rDerivedKeySize
,
147 sal_Int32
&rStartKeyGenID
,
149 OUString
& aMediaType
,
150 const css::uno::Reference
< css::io::XInputStream
>& rStream
);
152 static css::uno::Reference
< css::io::XInputStream
> StaticGetDataFromRawStream(
153 const rtl::Reference
<comphelper::RefCountedMutex
>& aMutexHolder
,
154 const css::uno::Reference
< css::uno::XComponentContext
>& rxContext
,
155 const css::uno::Reference
< css::io::XInputStream
>& xStream
,
156 const ::rtl::Reference
< EncryptionData
> &rData
);
158 static bool StaticHasValidPassword (
159 const css::uno::Reference
< css::uno::XComponentContext
>& rxContext
,
160 const css::uno::Sequence
< sal_Int8
> &aReadBuffer
,
161 const ::rtl::Reference
< EncryptionData
> &rData
);
163 css::uno::Reference
< css::io::XInputStream
> getInputStream(
165 const ::rtl::Reference
< EncryptionData
> &rData
,
166 ::std::optional
<sal_Int64
> oDecryptedSize
,
167 const rtl::Reference
<comphelper::RefCountedMutex
>& aMutexHolder
);
169 css::uno::Reference
< css::io::XInputStream
> getDataStream(
171 const ::rtl::Reference
< EncryptionData
> &rData
,
172 ::std::optional
<sal_Int64
> oEncryptedSize
,
173 const rtl::Reference
<comphelper::RefCountedMutex
>& aMutexHolder
);
175 css::uno::Reference
< css::io::XInputStream
> getWrappedRawStream(
177 const ::rtl::Reference
< EncryptionData
> &rData
,
178 sal_Int64 nDecryptedSize
,
179 const OUString
& aMediaType
,
180 const rtl::Reference
<comphelper::RefCountedMutex
>& aMutexHolder
);
182 ZipEnumeration
entries();
187 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */