Marine Library  1.0
C++ library for Linux Networking Development
compress_zlib.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_ZLIB_H_20090223
26 #define DOZERG_COMPRESSOR_ZLIB_H_20090223
27 
28 #include <stdint.h>
29 #include <vector>
30 #include <string>
31 #include <cstring>
32 #include <zlib.h>
33 #include "tools/net.hh" //Hton, Ntoh
34 #include "data_stream.hh" //varint
35 
36 NS_SERVER_BEGIN
37 
44 {
45  typedef ::uLongf __ZSize;
46  typedef ::Bytef __ZChar;
47 public:
56  CCompressorZlib(int level = Z_DEFAULT_COMPRESSION):level_(level){}
61  void setLevel(int lv){level_ = lv;}
66  int getLevel() const{return level_;}
76  bool compress(const std::vector<char> & input, std::vector<char> & output) const{
77  return compressTemplate(input, output);
78  }
79  bool compress(const std::vector<signed char> & input, std::vector<signed char> & output) const{
80  return compressTemplate(input, output);
81  }
82  bool compress(const std::vector<unsigned char> & input, std::vector<unsigned char> & output) const{
83  return compressTemplate(input, output);
84  }
85  bool compress(const std::string & input, std::string & output) const{
86  return compressTemplate(input, output);
87  }
98  bool decompress(const std::vector<char> & input, std::vector<char> & output) const{
99  return decompressTemplate(input, output);
100  }
101  bool decompress(const std::vector<signed char> & input, std::vector<signed char> & output) const{
102  return decompressTemplate(input, output);
103  }
104  bool decompress(const std::vector<unsigned char> & input, std::vector<unsigned char> & output) const{
105  return decompressTemplate(input, output);
106  }
107  bool decompress(const std::string & input, std::string & output) const{
108  return decompressTemplate(input, output);
109  }
111 private:
112  template<class Buffer>
113  bool compressTemplate(const Buffer & input, Buffer & output) const{
114  output.resize(32);
115  const __ZSize in_len = __ZSize(input.size());
116  COutByteStreamBuf ds(reinterpret_cast<char *>(&output[0]), output.size());
117  size_t begin = 0;
118  if(!(ds<<Manip::varint(in_len)<<Manip::end(&begin)))
119  return false;
120  __ZSize out_len = ::compressBound(in_len);
121  output.resize(begin + out_len);
122  const int ret = ::compress2(
123  reinterpret_cast<__ZChar *>(&output[begin]),
124  &out_len,
125  reinterpret_cast<const __ZChar *>(&input[0]),
126  in_len,
127  level_);
128  if(Z_OK == ret){
129  output.resize(begin + out_len);
130  return true;
131  }
132  return false;
133  }
134  template<class Buffer>
135  bool decompressTemplate(const Buffer & input, Buffer & output) const{
136  __ZSize out_len = 0;
137  CInByteStream ds(reinterpret_cast<const char *>(&input[0]), input.size());
138  if(!(ds>>Manip::varint(out_len)))
139  return false;
140  const size_t begin = ds.cur();
141  output.resize(out_len);
142  const int ret = ::uncompress(
143  reinterpret_cast<__ZChar *>(&output[0]),
144  &out_len,
145  reinterpret_cast<const __ZChar *>(&input[begin]),
146  __ZSize(input.size() - begin));
147  if(Z_OK == ret){
148  output.resize(out_len);
149  return true;
150  }
151  return false;
152  }
153  CCompressorZlib(const CCompressorZlib &); //disable copy and assignment
154  CCompressorZlib & operator =(const CCompressorZlib &);
155  //members
156  int level_; //compress level of zlib, default to Z_DEFAULT_COMPRESSION, which means level 6
157 };
158 
159 NS_SERVER_END
160 
161 #endif
A convenient interface for zlib compression algorithm.
Definition: compress_zlib.hh:43
int getLevel() const
Get compression level.
Definition: compress_zlib.hh:66
Data unpacking interfaces.
Definition: data_stream.hh:1033
size_t cur() const
Get current pointer.
Definition: data_stream.hh:1191
Data packing library using stream style interfaces.
NS_IMPL::CManipulatorVarint< const T > varint(const T &val)
Pack integer using Base 128 Varints encoding.
Definition: data_stream.hh:793
void setLevel(int lv)
Set compression level.
Definition: compress_zlib.hh:61
NS_IMPL::CManipulatorEnd< void, void > end()
End operations for CInByteStream, COutByteStreamStrRef, COutByteStreamVecRef.
Definition: data_stream.hh:929
Data packing interfaces.
Definition: data_stream.hh:1527
CCompressorZlib(int level=Z_DEFAULT_COMPRESSION)
Constructor.
Definition: compress_zlib.hh:56