1// Copyright (C) 2020 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef DECOMPRESS_HELPER_P_H
5#define DECOMPRESS_HELPER_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists for the convenience
12// of the Network Access API. This header file may change from
13// version to version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtNetwork/private/qtnetworkglobal_p.h>
19#include <QtCore/private/qbytedata_p.h>
20
21#include <memory>
22
23QT_BEGIN_NAMESPACE
24
25class QIODevice;
26class Q_AUTOTEST_EXPORT QDecompressHelper
27{
28public:
29 enum ContentEncoding {
30 None,
31 Deflate,
32 GZip,
33 Brotli,
34 Zstandard,
35 };
36
37 QDecompressHelper() = default;
38 ~QDecompressHelper();
39
40 bool setEncoding(const QByteArray &contentEncoding);
41
42 bool isCountingBytes() const;
43 void setCountingBytesEnabled(bool shouldCount);
44
45 qint64 uncompressedSize() const;
46
47 bool hasData() const;
48 void feed(const QByteArray &data);
49 void feed(QByteArray &&data);
50 void feed(const QByteDataBuffer &buffer);
51 void feed(QByteDataBuffer &&buffer);
52 qsizetype read(char *data, qsizetype maxSize);
53
54 bool isValid() const;
55
56 void clear();
57
58 void setDecompressedSafetyCheckThreshold(qint64 threshold);
59
60 static bool isSupportedEncoding(const QByteArray &encoding);
61 static QByteArrayList acceptedEncoding();
62
63 QString errorString() const;
64
65private:
66 bool isPotentialArchiveBomb() const;
67 bool hasDataInternal() const;
68 qsizetype readInternal(char *data, qsizetype maxSize);
69
70 bool countInternal();
71 bool countInternal(const QByteArray &data);
72 bool countInternal(const QByteDataBuffer &buffer);
73
74 bool setEncoding(ContentEncoding ce);
75 qint64 encodedBytesAvailable() const;
76
77 qsizetype readZLib(char *data, qsizetype maxSize);
78 qsizetype readBrotli(char *data, qsizetype maxSize);
79 qsizetype readZstandard(char *data, qsizetype maxSize);
80
81 QByteDataBuffer compressedDataBuffer;
82 QByteDataBuffer decompressedDataBuffer;
83 const qsizetype MaxDecompressedDataBufferSize = 10 * 1024 * 1024;
84 bool decoderHasData = false;
85
86 bool countDecompressed = false;
87 std::unique_ptr<QDecompressHelper> countHelper;
88
89 QString errorStr;
90
91 // Used for calculating the ratio
92 qint64 archiveBombCheckThreshold = 10 * 1024 * 1024;
93 qint64 totalUncompressedBytes = 0;
94 qint64 totalCompressedBytes = 0;
95 qint64 totalBytesRead = 0;
96
97 ContentEncoding contentEncoding = None;
98
99 void *decoderPointer = nullptr;
100#if QT_CONFIG(brotli)
101 const uint8_t *brotliUnconsumedDataPtr = nullptr;
102 size_t brotliUnconsumedAmount = 0;
103#endif
104};
105
106QT_END_NAMESPACE
107
108#endif // DECOMPRESS_HELPER_P_H
109

source code of qtbase/src/network/access/qdecompresshelper_p.h