KHealthCertificate

zlib.cpp
1 /*
2  * SPDX-FileCopyrightText: 2021 Volker Krause <[email protected]>
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 
13 static 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 
41 QByteArray Zlib::decompressZlib(const QByteArray &data)
42 {
43  return decompress(data, MAX_WBITS);
44 }
45 
46 QByteArray Zlib::decompressDeflate(const QByteArray &data)
47 {
48  return decompress(data, -MAX_WBITS);
49 }
void resize(int size)
void truncate(int pos)
char * data()
int size() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Mon Oct 18 2021 23:21:56 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.