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

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