Debian Codegraph @

Hardware/ZLib.cpp

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /*
2 RailControl - Model Railway Control Software
3
4 Copyright (c) 2017-2025 by Teddy / Dominik Mahrer - www.railcontrol.org
5
6 RailControl is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any
9 later version.
10
11 RailControl is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 See the GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with RailControl; see the file LICENCE. If not see
18 <http://www.gnu.org/licenses/>.
19 */
20
21 #include <string>
•22 #include <zlib.h>
23
24 #include "Hardware/ZLib.h"
25 #include "Utils/Utils.h"
26
27 using std::string;
28
29 string ZLib::Compress(const string& input)
30 {
31 long inputSize = input.size();
32
33 unsigned long outputSize = inputSize + 12;
34 unsigned char* outputBuffer = reinterpret_cast<unsigned char*>(malloc(outputSize));
35
36 int nResult = compress2(outputBuffer, &outputSize, reinterpret_cast<const unsigned char*>(input.c_str()), inputSize, 9);
37
38 if (nResult != Z_OK)
39 {
40 return "";
41 }
42
43 string outputData(reinterpret_cast<char*>(outputBuffer), outputSize);
44 free(outputBuffer);
45 return outputData;
46 }
47
48 string ZLib::UnCompress(const char* input, const size_t inputSize, const size_t outputSize)
49 {
50 z_stream strm;
51 strm.zalloc = Z_NULL;
52 strm.zfree = Z_NULL;
53 strm.opaque = Z_NULL;
54 strm.avail_in = 0;
55 strm.next_in = Z_NULL;
56 int ret = inflateInit(&strm);
57 if (ret != Z_OK)
58 {
59 return "";
60 }
61 strm.avail_in = inputSize;
62 strm.avail_out = outputSize;
63 strm.next_in = reinterpret_cast<unsigned char*>(const_cast<char*>(input));
64 char* outputBuffer = reinterpret_cast<char*>(malloc(outputSize));
65 strm.next_out = reinterpret_cast<unsigned char*>(outputBuffer);
66 ret = inflate(&strm, Z_NO_FLUSH);
67 inflateEnd(&strm);
68 if (ret != Z_STREAM_END)
69 {
70 free(outputBuffer);
71 return "";
72 }
73
74 std::string output(outputBuffer, outputSize);
75 free(outputBuffer);
76 return output;
77 }