-
Notifications
You must be signed in to change notification settings - Fork 0
/
zlib.hpp
104 lines (93 loc) · 2.89 KB
/
zlib.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#ifndef __include_zlib
#define __include_zlib
#include <cstddef>
#include <cstdint>
#ifdef __STDC_HOSTED__
#include <type_traits>
#include <unordered_map>
#include <vector>
using std::conditional_t;
using std::unordered_map, std::vector;
#endif
using std::uint32_t;
namespace zlite {
unordered_map<unsigned short, const char *> method{{8, "Deflate"},
{15, "Reserved"}};
// TODO: encapsulate
class zlib {
public:
uint8_t l;
uint8_t flaggers;
/* Not really idiomatic C++, but this will only insert this member in the
class if the bit for a preset dictionary is set. If not, the compressed
data will succeed the flags. */
std::conditional_t<flaggers, uint32_t, bool> p;
vector<unsigned char> compressed_data;
uint32_t adler_32;
// public:
auto compression_info();
zlib() : l{}, flaggers{}, compressed_data{}, adler_32(0) {}
virtual zlib &compress(vector<std::byte> &data, unsigned char method = 8,
bool use_dictionary = false) = 0;
virtual vector<std::byte> &decompress() = 0;
};
zlib& read(vector<std::byte>);
}; // namespace zlite
extern "C" {
enum {
Z_NO_FLUSH,
Z_PARTIAL_FLUSH,
Z_SYNC_FLUSH,
Z_FULL_FLUSH,
Z_FINISH,
Z_BLOCK,
Z_TREES,
};
enum {
Z_OK = 0,
Z_STREAM_END = 1,
Z_NEED_DICT = 2,
Z_ERRNO = -1,
Z_STREAM_ERROR = -2,
Z_DATA_ERROR = -3,
Z_MEM_ERROR = -4,
Z_BUF_ERROR = -5,
Z_VERSION_ERROR = -6
};
enum {
Z_NO_COMPRESSION = 0,
Z_BEST_SPEED = 1,
Z_BEST_COMPRESSION = 9,
Z_DEFAULT_COMPRESSION = -1,
};
struct z_stream {
const unsigned char *next_in; /* next input byte */
unsigned avail_in; /* number of bytes available at next_in */
unsigned long total_in; /* total number of input bytes read so far */
unsigned char *next_out; /* next output byte will go here */
unsigned avail_out; /* remaining free space at next_out */
unsigned long total_out; /* total number of bytes output so far */
const char *msg; /* last error message, NULL if no error */
struct internal_state *state; /* not visible by applications */
void *(*zalloc)(void *, unsigned,
unsigned); /* used to allocate the internal state */
void (*zfree)(void *, void *); /* used to free the internal state */
void *opaque; /* private data object passed to zalloc and zfree */
int data_type; /* best guess about the data type: binary or text
for deflate, or the decoding state for inflate */
uint32_t adler; /* Adler-32 or CRC-32 value of the uncompressed data */
unsigned long reserved; /* reserved for future use */
};
int deflateInit(z_stream *, int);
int deflateInit2(z_stream *, int, int, int, int);
int deflate(z_stream *, int
#ifdef __cplusplus
= Z_NO_FLUSH
#endif
);
int deflateEnd(z_stream *);
int inflateInit(z_stream *);
int inflate(z_stream *, int);
int inflateEnd(z_stream *);
}
#endif