Symphony Of Empires
compress.hpp
Go to the documentation of this file.
1 // Eng3D - General purpouse game engine
2 // Copyright (C) 2021, Eng3D contributors
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <https://www.gnu.org/licenses/>.
16 //
17 // ----------------------------------------------------------------------------
18 // Name:
19 // compress.hpp
20 //
21 // Abstract:
22 // Does some important stuff.
23 // ----------------------------------------------------------------------------
24 
25 #pragma once
26 
27 #include <zlib.h>
28 
29 namespace Eng3D::Zlib {
30  size_t compress(const void* src, size_t src_len, void* dest, size_t dest_len) {
31  z_stream info = {};
32  info.avail_in = src_len;
33  info.avail_out = dest_len;
34  info.next_in = (Bytef*)src;
35  info.next_out = (Bytef*)dest;
36  info.data_type = Z_BINARY;
37 
38  int r = deflateInit(&info, Z_DEFAULT_COMPRESSION);
39  if(r == Z_OK) {
40  r = deflate(&info, Z_FINISH);
41  if(r == Z_STREAM_END) {
42  deflateEnd(&info);
43  return info.total_out;
44  }
45  }
46  CXX_THROW(std::runtime_error, "Insufficient zlib output buffer size for deflate");
47  }
48 
49  size_t decompress(const void* src, size_t src_len, void* dest, size_t dest_len) {
50  z_stream info = {};
51  info.avail_in = src_len;
52  info.avail_out = dest_len;
53  info.next_in = (Bytef*)src;
54  info.next_out = (Bytef*)dest;
55  info.data_type = Z_BINARY;
56 
57  int r = inflateInit(&info);
58  if(r == Z_OK) {
59  r = inflate(&info, Z_FINISH);
60  if(r == Z_STREAM_END) {
61  inflateEnd(&info);
62  return info.total_out;
63  }
64  }
65  CXX_THROW(std::runtime_error, "Insufficient zlib output buffer size for inflate");
66  }
67 }
size_t decompress(const void *src, size_t src_len, void *dest, size_t dest_len)
Definition: compress.hpp:49
size_t compress(const void *src, size_t src_len, void *dest, size_t dest_len)
Definition: compress.hpp:30
#define CXX_THROW(class,...)
Definition: utils.hpp:98