Coverage Report

Created: 2026-03-31 11:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/package/inc/ZipOutputEntry.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_ZIPOUTPUTENTRY_HXX
20
#define INCLUDED_PACKAGE_INC_ZIPOUTPUTENTRY_HXX
21
22
#include <com/sun/star/io/XOutputStream.hpp>
23
#include <com/sun/star/io/XTempFile.hpp>
24
#include <com/sun/star/uno/Reference.hxx>
25
#include <com/sun/star/uno/XComponentContext.hpp>
26
#include <com/sun/star/xml/crypto/XCipherContext.hpp>
27
#include <com/sun/star/xml/crypto/XDigestContext.hpp>
28
29
#include <package/Deflater.hxx>
30
#include <comphelper/threadpool.hxx>
31
#include <unotools/tempfile.hxx>
32
#include "CRC32.hxx"
33
#include "ZipEntry.hxx"
34
#include <atomic>
35
#include <exception>
36
37
struct ZipEntry;
38
class ZipPackageBuffer;
39
class ZipPackageStream;
40
41
class ZipOutputEntryBase
42
{
43
protected:
44
    css::uno::Reference< css::uno::XComponentContext > m_xContext;
45
    css::uno::Reference< css::io::XOutputStream > m_xOutStream;
46
47
    css::uno::Reference< css::xml::crypto::XCipherContext > m_xCipherContext;
48
    css::uno::Reference< css::xml::crypto::XDigestContext > m_xDigestContext;
49
50
    CRC32               m_aCRC;
51
    ZipEntry            *m_pCurrentEntry;
52
    sal_Int16           m_nDigested;
53
    ZipPackageStream*   m_pCurrentStream;
54
    bool                m_bEncryptCurrentEntry;
55
56
public:
57
0
    virtual ~ZipOutputEntryBase() = default;
58
59
    virtual void writeStream(const css::uno::Reference< css::io::XInputStream >& xInStream) = 0;
60
61
0
    ZipEntry* getZipEntry() { return m_pCurrentEntry; }
62
0
    ZipPackageStream* getZipPackageStream() { return m_pCurrentStream; }
63
0
    bool isEncrypt() const { return m_bEncryptCurrentEntry; }
64
65
    void closeEntry();
66
67
protected:
68
    ZipOutputEntryBase(
69
        css::uno::Reference< css::io::XOutputStream > xOutStream,
70
        css::uno::Reference< css::uno::XComponentContext > xContext,
71
        ZipEntry* pEntry, ZipPackageStream* pStream, bool bEncrypt, bool checkStream);
72
73
    // Inherited classes call this with deflated data buffer.
74
    void processDeflated( const css::uno::Sequence< sal_Int8 >& deflateBuffer, sal_Int32 nLength );
75
    // Inherited classes call this with the input buffer.
76
    void processInput( const css::uno::Sequence< sal_Int8 >& rBuffer );
77
78
    virtual void finishDeflater() = 0;
79
    virtual sal_Int64 getDeflaterTotalIn() const = 0;
80
    virtual sal_Int64 getDeflaterTotalOut() const = 0;
81
    virtual void deflaterReset() = 0;
82
    virtual bool isDeflaterFinished() const = 0;
83
};
84
85
// Normal non-threaded case.
86
class ZipOutputEntry : public ZipOutputEntryBase
87
{
88
    css::uno::Sequence< sal_Int8 > m_aDeflateBuffer;
89
    ZipUtils::Deflater m_aDeflater;
90
91
public:
92
    ZipOutputEntry(
93
        const css::uno::Reference< css::io::XOutputStream >& rxOutStream,
94
        const css::uno::Reference< css::uno::XComponentContext >& rxContext,
95
        ZipEntry* pEntry, ZipPackageStream* pStream, bool bEncrypt);
96
    void writeStream(const css::uno::Reference< css::io::XInputStream >& xInStream) override;
97
    void write(const css::uno::Sequence< sal_Int8 >& rBuffer);
98
99
protected:
100
    ZipOutputEntry(
101
        const css::uno::Reference< css::io::XOutputStream >& rxOutStream,
102
        const css::uno::Reference< css::uno::XComponentContext >& rxContext,
103
        ZipEntry* pEntry, ZipPackageStream* pStream, bool bEncrypt, bool checkStream);
104
    virtual void finishDeflater() override;
105
    virtual sal_Int64 getDeflaterTotalIn() const override;
106
    virtual sal_Int64 getDeflaterTotalOut() const override;
107
    virtual void deflaterReset() override;
108
    virtual bool isDeflaterFinished() const override;
109
    void doDeflate();
110
};
111
112
// Class that runs the compression in a background thread.
113
class ZipOutputEntryInThread final : public ZipOutputEntry
114
{
115
    class Task;
116
    std::unique_ptr<ZipEntry> m_pOwnedZipEntry;
117
    rtl::Reference<utl::TempFileFastService> m_xTempFile;
118
    std::exception_ptr m_aParallelDeflateException;
119
    std::atomic<bool>   m_bFinished;
120
121
public:
122
    ZipOutputEntryInThread(
123
        const css::uno::Reference< css::uno::XComponentContext >& rxContext,
124
        std::unique_ptr<ZipEntry>&& pEntry, ZipPackageStream* pStream, bool bEncrypt);
125
    std::unique_ptr<comphelper::ThreadTask> createTask(
126
        const std::shared_ptr<comphelper::ThreadTaskTag>& pTag,
127
        const css::uno::Reference< css::io::XInputStream >& xInStream );
128
    /* This block of methods is for threaded zipping, where we compress to a temp stream, whose
129
       data is retrieved via getData */
130
    void createBufferFile();
131
0
    void setParallelDeflateException(const std::exception_ptr& exception) { m_aParallelDeflateException = exception; }
132
    css::uno::Reference< css::io::XInputStream > getData() const;
133
0
    const std::exception_ptr& getParallelDeflateException() const { return m_aParallelDeflateException; }
134
    void closeBufferFile();
135
    void deleteBufferFile();
136
0
    bool isFinished() const { return m_bFinished; }
137
0
    std::unique_ptr<ZipEntry>&& moveZipEntry() { return std::move(m_pOwnedZipEntry); }
138
139
private:
140
0
    void setFinished() { m_bFinished = true; }
141
};
142
143
// Class that synchronously runs the compression in multiple threads (using ThreadDeflater).
144
class ZipOutputEntryParallel final : public ZipOutputEntryBase
145
{
146
    sal_Int64 totalIn;
147
    sal_Int64 totalOut;
148
    bool finished;
149
public:
150
    ZipOutputEntryParallel(
151
        const css::uno::Reference< css::io::XOutputStream >& rxOutStream,
152
        const css::uno::Reference< css::uno::XComponentContext >& rxContext,
153
        ZipEntry* pEntry, ZipPackageStream* pStream, bool bEncrypt);
154
    void writeStream(const css::uno::Reference< css::io::XInputStream >& xInStream) override;
155
private:
156
    virtual void finishDeflater() override;
157
    virtual sal_Int64 getDeflaterTotalIn() const override;
158
    virtual sal_Int64 getDeflaterTotalOut() const override;
159
    virtual void deflaterReset() override;
160
    virtual bool isDeflaterFinished() const override;
161
};
162
163
#endif
164
165
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */