KHealthCertificate

zlib.cpp
1/*
2 * SPDX-FileCopyrightText: 2021 Volker Krause <vkrause@kde.org>
3 * SPDX-License-Identifier: LGPL-2.0-or-later
4 */
5
6#include "zlib_p.h"
7#include "logging.h"
8
9#include <QByteArray>
10
11#include <zlib.h>
12
13static QByteArray decompress(const QByteArray &data, int windowBits)
14{
15 QByteArray out;
16 out.resize(4096);
17 z_stream stream;
18 stream.zalloc = nullptr;
19 stream.zfree = nullptr;
20 stream.opaque = nullptr;
21 stream.avail_in = data.size();
22 stream.next_in = reinterpret_cast<unsigned char*>(const_cast<char*>(data.data()));
23 stream.avail_out = out.size();
24 stream.next_out = reinterpret_cast<unsigned char*>(out.data());
25
26 inflateInit2(&stream, windowBits);
27 const auto res = inflate(&stream, Z_NO_FLUSH);
28 switch (res) {
29 case Z_OK:
30 case Z_STREAM_END:
31 break; // all good
32 default:
33 qCWarning(Log) << "zlib decompression failed" << stream.msg;
34 return {};
35 }
36 inflateEnd(&stream);
37 out.truncate(out.size() - stream.avail_out);
38 return out;
39}
40
41QByteArray Zlib::decompressZlib(const QByteArray &data)
42{
43 return decompress(data, MAX_WBITS);
44}
45
46QByteArray Zlib::decompressDeflate(const QByteArray &data)
47{
48 return decompress(data, -MAX_WBITS);
49}
char * data()
void resize(qsizetype newSize, char c)
qsizetype size() const const
void truncate(qsizetype pos)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 29 2024 11:47:02 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.