-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcompress_zlib.hh
161 lines (154 loc) · 5.81 KB
/
compress_zlib.hh
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* Copyright (c) 2016 Zhao DAI <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or any
* later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see accompanying file LICENSE.txt
* or <http://www.gnu.org/licenses/>.
*/
/**
* @file
* @brief [zlib](http://www.zlib.net) compression algorithm.
* @author Zhao DAI
*/
#ifndef DOZERG_COMPRESSOR_ZLIB_H_20090223
#define DOZERG_COMPRESSOR_ZLIB_H_20090223
#include <stdint.h>
#include <vector>
#include <string>
#include <cstring>
#include <zlib.h>
#include "tools/net.hh" //Hton, Ntoh
#include "data_stream.hh" //varint
NS_SERVER_BEGIN
/**
* @brief A convenient interface for [zlib](http://www.zlib.net) compression algorithm.
* You may need to link to zlib library (e.g. @c -lz) if you use @c CCompressorZlib.
* @note CCompressorZlib object can @em NOT be copied.
*/
class CCompressorZlib
{
typedef ::uLongf __ZSize;
typedef ::Bytef __ZChar;
public:
/**
* @brief Constructor.
* The compression @c level must be @c Z_DEFAULT_COMPRESSION, or between 0 and 9:
* @n @c 1 gives best speed, @c 9 gives best compression, @c 0 gives no compression at all (the
* input data is simply copied a block at a time). @c Z_DEFAULT_COMPRESSION requests a default
* compromise between speed and compression (currently equivalent to level 6).
* @param level Compression level (0 to 9, or @c Z_DEFAULT_COMPRESSION)
*/
CCompressorZlib(int level = Z_DEFAULT_COMPRESSION):level_(level){}
/**
* @brief Set compression level.
* @param lv Compression level (0 to 9, or @c Z_DEFAULT_COMPRESSION)
*/
void setLevel(int lv){level_ = lv;}
/**
* @brief Get compression level.
* @return Compression level
*/
int getLevel() const{return level_;}
/**
* @name Compression
* @{
* @brief Compress given data.
* If @c output is not empty, the result will append to @c output.
* @param[in] input Data to be compressed
* @param[out] output A buffer to receive the result
* @return @c true if succeeded; otherwise @c false
*/
bool compress(const std::vector<char> & input, std::vector<char> & output) const{
return compressTemplate(input, output);
}
bool compress(const std::vector<signed char> & input, std::vector<signed char> & output) const{
return compressTemplate(input, output);
}
bool compress(const std::vector<unsigned char> & input, std::vector<unsigned char> & output) const{
return compressTemplate(input, output);
}
bool compress(const std::string & input, std::string & output) const{
return compressTemplate(input, output);
}
/** @} */
/**
* @name Decompression
* @{
* @brief Decompress given data.
* If @c output is not empty, the result will append to @c output.
* @param[in] input Data to be decompressed
* @param[out] output A buffer to receive the result
* @return @c true if succeeded; otherwise @c false
*/
bool decompress(const std::vector<char> & input, std::vector<char> & output) const{
return decompressTemplate(input, output);
}
bool decompress(const std::vector<signed char> & input, std::vector<signed char> & output) const{
return decompressTemplate(input, output);
}
bool decompress(const std::vector<unsigned char> & input, std::vector<unsigned char> & output) const{
return decompressTemplate(input, output);
}
bool decompress(const std::string & input, std::string & output) const{
return decompressTemplate(input, output);
}
/** @} */
private:
template<class Buffer>
bool compressTemplate(const Buffer & input, Buffer & output) const{
output.resize(32);
const __ZSize in_len = __ZSize(input.size());
COutByteStreamBuf ds(reinterpret_cast<char *>(&output[0]), output.size());
size_t begin = 0;
if(!(ds<<Manip::varint(in_len)<<Manip::end(&begin)))
return false;
__ZSize out_len = ::compressBound(in_len);
output.resize(begin + out_len);
const int ret = ::compress2(
reinterpret_cast<__ZChar *>(&output[begin]),
&out_len,
reinterpret_cast<const __ZChar *>(&input[0]),
in_len,
level_);
if(Z_OK == ret){
output.resize(begin + out_len);
return true;
}
return false;
}
template<class Buffer>
bool decompressTemplate(const Buffer & input, Buffer & output) const{
__ZSize out_len = 0;
CInByteStream ds(reinterpret_cast<const char *>(&input[0]), input.size());
if(!(ds>>Manip::varint(out_len)))
return false;
const size_t begin = ds.cur();
output.resize(out_len);
const int ret = ::uncompress(
reinterpret_cast<__ZChar *>(&output[0]),
&out_len,
reinterpret_cast<const __ZChar *>(&input[begin]),
__ZSize(input.size() - begin));
if(Z_OK == ret){
output.resize(out_len);
return true;
}
return false;
}
CCompressorZlib(const CCompressorZlib &); //disable copy and assignment
CCompressorZlib & operator =(const CCompressorZlib &);
//members
int level_; //compress level of zlib, default to Z_DEFAULT_COMPRESSION, which means level 6
};
NS_SERVER_END
#endif