Coverage Report

Created: 2025-12-08 09:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/package/inc/ZipFile.hxx
Line
Count
Source
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
/*
3
 * This file is part of the LibreOffice project.
4
 *
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/.
8
 *
9
 * This file incorporates work covered by the following license notice:
10
 *
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 .
18
 */
19
#ifndef INCLUDED_PACKAGE_INC_ZIPFILE_HXX
20
#define INCLUDED_PACKAGE_INC_ZIPFILE_HXX
21
22
#include <com/sun/star/xml/crypto/XCipherContext.hpp>
23
#include <com/sun/star/xml/crypto/XDigestContext.hpp>
24
25
#include <comphelper/refcountedmutex.hxx>
26
#include <package/Inflater.hxx>
27
#include <memory>
28
#include <rtl/ref.hxx>
29
#include "ByteGrabber.hxx"
30
#include "HashMaps.hxx"
31
#include "EncryptionData.hxx"
32
33
#include <optional>
34
#include <span>
35
#include <unordered_set>
36
37
class MemoryByteGrabber;
38
namespace com::sun::star {
39
    namespace uno { class XComponentContext; }
40
}
41
42
/*
43
 * We impose arbitrary but reasonable limit on ZIP files.
44
 */
45
46
23.5k
#define ZIP_MAXNAMELEN 512
47
23.6k
#define ZIP_MAXENTRIES (0x10000 - 2)
48
49
class ZipEnumeration;
50
51
52
class ZipFile
53
{
54
public:
55
    enum class Checks { Default, CheckInsensitive, TryCheckInsensitive };
56
57
private:
58
    rtl::Reference<comphelper::RefCountedMutex> m_aMutexHolder;
59
60
    std::unordered_set<OUString> m_EntriesInsensitive;
61
    Checks m_Checks;
62
63
    EntryHash       aEntries;
64
    ByteGrabber     aGrabber;
65
    css::uno::Reference < css::io::XInputStream > xStream;
66
    const css::uno::Reference < css::uno::XComponentContext > m_xContext;
67
68
    bool bRecoveryMode;
69
70
    // aMediaType parameter is used only for raw stream header creation
71
    css::uno::Reference < css::io::XInputStream >  createStreamForZipEntry(
72
            const rtl::Reference<comphelper::RefCountedMutex>& aMutexHolder,
73
            ZipEntry const & rEntry,
74
            const ::rtl::Reference < EncryptionData > &rData,
75
            sal_Int8 nStreamMode,
76
            ::std::optional<sal_Int64> oDecryptedSize,
77
            const bool bUseBufferedStream = true,
78
            const OUString& aMediaType = OUString() );
79
80
    css::uno::Reference<css::io::XInputStream> checkValidPassword(
81
            ZipEntry const& rEntry, rtl::Reference<EncryptionData> const& rData,
82
            sal_Int64 nDecryptedSize,
83
            rtl::Reference<comphelper::RefCountedMutex> const& rMutexHolder);
84
85
    bool checkSizeAndCRC( const ZipEntry& aEntry );
86
87
    sal_Int32 getCRC( sal_Int64 nOffset, sal_Int64 nSize );
88
89
    void getSizeAndCRC( sal_Int64 nOffset, sal_Int64 nCompressedSize, sal_Int64 *nSize, sal_Int32 *nCRC );
90
91
    sal_uInt64 readLOC(ZipEntry &rEntry);
92
    sal_uInt64 readLOC_Impl(ZipEntry &rEntry, std::vector<sal_Int8>& rNameBuffer, std::vector<sal_Int8>& rExtraBuffer);
93
    sal_Int32 readCEN();
94
    std::tuple<sal_Int64, sal_Int64, sal_Int64> findCentralDirectory();
95
    bool TryDDImpl(sal_Int64 dataOffset, sal_Int32 nCRC32,
96
        sal_Int64 nCompressedSize, sal_Int64 nSize);
97
    bool TryDDEndAt(std::span<const sal_Int8> data, sal_Int64 dataOffset);
98
    bool HandlePK34(std::span<const sal_Int8> data, sal_Int64 dataOffset, sal_Int64 totalSize);
99
    void HandlePK78(std::span<const sal_Int8> data, sal_Int64 dataOffset);
100
    void recover();
101
    static bool readExtraFields(MemoryByteGrabber& aMemGrabber, sal_Int16 nExtraLen,
102
                                sal_uInt64& nSize, sal_uInt64& nCompressedSize,
103
                                ::std::optional<sal_uInt64> & roOffset,
104
                                std::string_view const * pCENFilenameToCheck);
105
106
public:
107
108
    ZipFile( rtl::Reference<comphelper::RefCountedMutex> aMutexHolder,
109
             css::uno::Reference < css::io::XInputStream > const &xInput,
110
             css::uno::Reference < css::uno::XComponentContext > xContext,
111
             bool bInitialise,
112
             bool bForceRecover,
113
             Checks checks);
114
115
    ~ZipFile();
116
117
0
    EntryHash& GetEntryHash() { return aEntries; }
118
119
    void setInputStream ( const css::uno::Reference < css::io::XInputStream >& xNewStream );
120
    css::uno::Reference< css::io::XInputStream > getRawData(
121
            ZipEntry& rEntry,
122
            const ::rtl::Reference < EncryptionData > &rData,
123
            ::std::optional<sal_Int64> oDecryptedSize,
124
            const rtl::Reference<comphelper::RefCountedMutex>& aMutexHolder,
125
            const bool bUseBufferedStream = true );
126
127
    static css::uno::Reference< css::xml::crypto::XDigestContext > StaticGetDigestContextForChecksum(
128
            const css::uno::Reference< css::uno::XComponentContext >& xArgContext,
129
            const ::rtl::Reference< EncryptionData >& xEncryptionData );
130
131
    static css::uno::Reference< css::xml::crypto::XCipherContext > StaticGetCipher(
132
            const css::uno::Reference< css::uno::XComponentContext >& xArgContext,
133
            const ::rtl::Reference< EncryptionData >& xEncryptionData,
134
            bool bEncrypt );
135
136
    static void StaticFillHeader ( const ::rtl::Reference < EncryptionData > & rData,
137
                                    sal_Int64 nSize,
138
                                    const OUString& aMediaType,
139
                                    sal_Int8 * & pHeader );
140
141
    static bool StaticFillData ( ::rtl::Reference < BaseEncryptionData > const & rData,
142
                                     sal_Int32 &rEncAlgorithm,
143
                                     sal_Int32 &rChecksumAlgorithm,
144
                                     sal_Int32 &rDerivedKeySize,
145
                                     sal_Int32 &rStartKeyGenID,
146
                                     sal_Int32 &rSize,
147
                                     OUString& aMediaType,
148
                                     const css::uno::Reference < css::io::XInputStream >& rStream );
149
150
    static css::uno::Reference< css::io::XInputStream > StaticGetDataFromRawStream(
151
            const rtl::Reference<comphelper::RefCountedMutex>& aMutexHolder,
152
            const css::uno::Reference< css::uno::XComponentContext >& rxContext,
153
            const css::uno::Reference< css::io::XInputStream >& xStream,
154
            const ::rtl::Reference < EncryptionData > &rData );
155
156
    static bool StaticHasValidPassword (
157
            const css::uno::Reference< css::uno::XComponentContext >& rxContext,
158
            const css::uno::Sequence< sal_Int8 > &aReadBuffer,
159
            const ::rtl::Reference < EncryptionData > &rData );
160
161
    css::uno::Reference< css::io::XInputStream > getInputStream(
162
            ZipEntry& rEntry,
163
            const ::rtl::Reference < EncryptionData > &rData,
164
            ::std::optional<sal_Int64> oDecryptedSize,
165
            const rtl::Reference<comphelper::RefCountedMutex>& aMutexHolder );
166
167
    css::uno::Reference< css::io::XInputStream > getDataStream(
168
            ZipEntry& rEntry,
169
            const ::rtl::Reference < EncryptionData > &rData,
170
            ::std::optional<sal_Int64> oEncryptedSize,
171
            const rtl::Reference<comphelper::RefCountedMutex>& aMutexHolder );
172
173
    css::uno::Reference< css::io::XInputStream > getWrappedRawStream(
174
            ZipEntry& rEntry,
175
            const ::rtl::Reference < EncryptionData > &rData,
176
            sal_Int64 nDecryptedSize,
177
            const OUString& aMediaType,
178
            const rtl::Reference<comphelper::RefCountedMutex>& aMutexHolder );
179
180
    ZipEnumeration entries();
181
};
182
183
#endif
184
185
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */