Marine Library  1.0
C++ library for Linux Networking Development
compress_quicklz.hh
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 Zhao DAI <daidodo@gmail.com>
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 any
7  * 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 accompanying file LICENSE.txt
16  * or <http://www.gnu.org/licenses/>.
17  */
18 
25 #ifndef DOZERG_COMPRESSOR_QUICKLZ_H_20090222
26 #define DOZERG_COMPRESSOR_QUICKLZ_H_20090222
27 
28 #include <vector>
29 #include <string>
30 
31 #ifndef QLZ_COMPRESSION_LEVEL
32 //# define QLZ_COMPRESSION_LEVEL 1
33 //# define QLZ_COMPRESSION_LEVEL 2
34 # define QLZ_COMPRESSION_LEVEL 3
35 #endif
36 
37 #ifndef QLZ_STREAMING_BUFFER
38 # define QLZ_STREAMING_BUFFER 0
39 //# define QLZ_STREAMING_BUFFER 100000
40 //# define QLZ_STREAMING_BUFFER 1000000
41 #endif
42 
43 //#define QLZ_MEMORY_SAFE
44 
45 #include "quicklz/quicklz.c"
46 #include "impl/environment.hh"
47 
48 NS_SERVER_BEGIN
49 
82 {
83  typedef ::qlz_state_compress __CompBuf;
84  typedef ::qlz_state_decompress __DecompBuf;
85 public:
99  bool compress(const std::vector<char> & input, std::vector<char> & output) const{
100  return compressTemplate(input, output);
101  }
102  bool compress(const std::vector<unsigned char> & input, std::vector<unsigned char> & output) const{
103  return compressTemplate(input, output);
104  }
105  bool compress(const std::vector<signed char> & input, std::vector<signed char> & output) const{
106  return compressTemplate(input, output);
107  }
108  bool compress(const std::string & input, std::string & output) const{
109  return compressTemplate(input, output);
110  }
121  bool decompress(const std::vector<char> & input, std::vector<char> & output) const{
122  return decompressTemplate(input, output);
123  }
124  bool decompress(const std::vector<unsigned char> & input, std::vector<unsigned char> & output) const{
125  return decompressTemplate(input, output);
126  }
127  bool decompress(const std::vector<signed char> & input, std::vector<signed char> & output) const{
128  return decompressTemplate(input, output);
129  }
130  bool decompress(const std::string & input, std::string & output) const{
131  return decompressTemplate(input, output);
132  }
134 private:
135  template<class Buffer>
136  bool compressTemplate(const Buffer & input, Buffer & output) const{
137  output.resize(input.size() + 400);
138  if(compBuf_.size() < sizeof(__CompBuf))
139  compBuf_.resize(sizeof(__CompBuf));
140  const size_t out_len = ::qlz_compress(
141  &input[0],
142  reinterpret_cast<char *>(&output[0]),
143  input.size(),
144  reinterpret_cast<__CompBuf *>(&compBuf_[0]));
145  output.resize(out_len);
146  return true;
147  }
148  template<class Buffer>
149  bool decompressTemplate(const Buffer & input, Buffer & output) const{
150  if(input.size() < 9)
151  return false;
152  const char * const buf = reinterpret_cast<const char *>(&input[0]);
153  size_t out_len = ::qlz_size_decompressed(buf);
154  output.resize(out_len);
155  if(decompBuf_.size() < sizeof(__DecompBuf))
156  decompBuf_.resize(sizeof(__DecompBuf));
157  out_len = ::qlz_decompress(buf, &output[0], reinterpret_cast<__DecompBuf *>(&decompBuf_[0]));
158  output.resize(out_len);
159  return true;
160  }
161  CCompressorQuickLZ(const CCompressorQuickLZ &); //disable copy and assignment
162  CCompressorQuickLZ & operator =(const CCompressorQuickLZ &);
163  //members
164  mutable std::vector<char> compBuf_;
165  mutable std::vector<char> decompBuf_;
166 };
167 
168 NS_SERVER_END
169 
170 #endif
171 
A convenient interface for QuickLZ compression algorithm.
Definition: compress_quicklz.hh:81
CCompressorQuickLZ()
Default constructor.
Definition: compress_quicklz.hh:89