-
Notifications
You must be signed in to change notification settings - Fork 2
/
compress_lzo.hh
150 lines (142 loc) · 5.31 KB
/
compress_lzo.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
/*
* 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 [miniLZO](http://www.oberhumer.com/opensource/lzo/#minilzo) compression algorithm.
* @author Zhao DAI
*/
#ifndef DOZERG_COMPRESSOR_LZO_H_20080221
#define DOZERG_COMPRESSOR_LZO_H_20080221
#include <stdint.h>
#include <vector>
#include <string>
#include <cstring> //memcpy
#include "minilzo/minilzo.c"
#include "tools/net.hh" //Hton, Ntoh
NS_SERVER_BEGIN
/**
* @brief A convenient interface for [miniLZO](http://www.oberhumer.com/opensource/lzo/#minilzo)
* compression algorithm.
* @note CCompressorLzo object can @em NOT be copied.
*/
class CCompressorLzo
{
typedef ::lzo_uint __Uint;
typedef uint64_t __Len;
public:
/**
* @brief Default constructor.
*/
CCompressorLzo(){}
/**
* @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:
static bool init(){
static const bool ok = (LZO_E_OK == ::lzo_init());
return ok;
}
template<class Buffer>
bool compressTemplate(const Buffer & input, Buffer & output) const{
if(!init())
return false;
__Uint out_len = __Uint(outLength(input.size()));
output.resize(sizeof(__Len) + size_t(out_len));
const __Uint in_len = __Uint(input.size());
if(workmem_.size() < LZO1X_1_MEM_COMPRESS)
workmem_.resize(LZO1X_1_MEM_COMPRESS);
if(LZO_E_OK != ::lzo1x_1_compress(
reinterpret_cast<const unsigned char *>(&input[0]),
in_len,
reinterpret_cast<unsigned char *>(&output[sizeof(__Len)]),
&out_len,
&workmem_[0]))
return false;
output.resize(sizeof(__Len) + out_len);
const __Len lsz = tools::Hton(__Len(in_len));
memcpy(&output[0], &lsz, sizeof lsz);
return true;
}
template<class Buffer>
bool decompressTemplate(const Buffer & input, Buffer & output) const{
if(input.size() < sizeof(__Len))
return false;
__Uint out_len = __Uint(tools::Ntoh(*reinterpret_cast<const __Len *>(&input[0])));
output.resize(out_len);
if(LZO_E_OK != ::lzo1x_decompress(
reinterpret_cast<const unsigned char *>(&input[sizeof(__Len)]),
__Uint(input.size() - sizeof(__Len)),
reinterpret_cast<unsigned char *>(&output[0]),
&out_len,
NULL))
return false;
output.resize(out_len);
return true;
}
//link: http://www.oberhumer.com/opensource/lzo/lzofaq.php
size_t outLength(size_t inLen) const{
return inLen + (inLen >> 4) + 67;
}
CCompressorLzo(const CCompressorLzo &); //disable copy and assignment
CCompressorLzo & operator =(const CCompressorLzo &);
//members
mutable std::vector<unsigned char> workmem_; //memory required for the wrkmem parameter
};
NS_SERVER_END
#endif