Marine Library  1.0
C++ library for Linux Networking Development
compress_lzo.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_LZO_H_20080221
26 #define DOZERG_COMPRESSOR_LZO_H_20080221
27 
28 #include <stdint.h>
29 #include <vector>
30 #include <string>
31 #include <cstring> //memcpy
32 #include "minilzo/minilzo.c"
33 #include "tools/net.hh" //Hton, Ntoh
34 
35 NS_SERVER_BEGIN
36 
43 {
44  typedef ::lzo_uint __Uint;
45  typedef uint64_t __Len;
46 public:
60  bool compress(const std::vector<char> & input, std::vector<char> & output) const{
61  return compressTemplate(input, output);
62  }
63  bool compress(const std::vector<signed char> & input, std::vector<signed char> & output) const{
64  return compressTemplate(input, output);
65  }
66  bool compress(const std::vector<unsigned char> & input, std::vector<unsigned char> & output) const{
67  return compressTemplate(input, output);
68  }
69  bool compress(const std::string & input, std::string & output) const{
70  return compressTemplate(input, output);
71  }
82  bool decompress(const std::vector<char> & input, std::vector<char> & output) const{
83  return decompressTemplate(input,output);
84  }
85  bool decompress(const std::vector<signed char> & input, std::vector<signed char> & output) const{
86  return decompressTemplate(input, output);
87  }
88  bool decompress(const std::vector<unsigned char> & input, std::vector<unsigned char> & output) const{
89  return decompressTemplate(input, output);
90  }
91  bool decompress(const std::string & input, std::string & output) const{
92  return decompressTemplate(input, output);
93  }
95 private:
96  static bool init(){
97  static const bool ok = (LZO_E_OK == ::lzo_init());
98  return ok;
99  }
100  template<class Buffer>
101  bool compressTemplate(const Buffer & input, Buffer & output) const{
102  if(!init())
103  return false;
104  __Uint out_len = __Uint(outLength(input.size()));
105  output.resize(sizeof(__Len) + size_t(out_len));
106  const __Uint in_len = __Uint(input.size());
107  if(workmem_.size() < LZO1X_1_MEM_COMPRESS)
108  workmem_.resize(LZO1X_1_MEM_COMPRESS);
109  if(LZO_E_OK != ::lzo1x_1_compress(
110  reinterpret_cast<const unsigned char *>(&input[0]),
111  in_len,
112  reinterpret_cast<unsigned char *>(&output[sizeof(__Len)]),
113  &out_len,
114  &workmem_[0]))
115  return false;
116  output.resize(sizeof(__Len) + out_len);
117  const __Len lsz = tools::Hton(__Len(in_len));
118  memcpy(&output[0], &lsz, sizeof lsz);
119  return true;
120  }
121  template<class Buffer>
122  bool decompressTemplate(const Buffer & input, Buffer & output) const{
123  if(input.size() < sizeof(__Len))
124  return false;
125  __Uint out_len = __Uint(tools::Ntoh(*reinterpret_cast<const __Len *>(&input[0])));
126  output.resize(out_len);
127  if(LZO_E_OK != ::lzo1x_decompress(
128  reinterpret_cast<const unsigned char *>(&input[sizeof(__Len)]),
129  __Uint(input.size() - sizeof(__Len)),
130  reinterpret_cast<unsigned char *>(&output[0]),
131  &out_len,
132  NULL))
133  return false;
134  output.resize(out_len);
135  return true;
136  }
137  //link: http://www.oberhumer.com/opensource/lzo/lzofaq.php
138  size_t outLength(size_t inLen) const{
139  return inLen + (inLen >> 4) + 67;
140  }
141  CCompressorLzo(const CCompressorLzo &); //disable copy and assignment
142  CCompressorLzo & operator =(const CCompressorLzo &);
143  //members
144  mutable std::vector<unsigned char> workmem_; //memory required for the wrkmem parameter
145 };
146 
147 NS_SERVER_END
148 
149 #endif
150 
A convenient interface for miniLZO compression algorithm.
Definition: compress_lzo.hh:42
CCompressorLzo()
Default constructor.
Definition: compress_lzo.hh:50