Marine Library  1.0
C++ library for Linux Networking Development
compress_snappy.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_SNAPPY_H_20131210
26 #define DOZERG_COMPRESSOR_SNAPPY_H_20131210
27 
28 #include <vector>
29 #include <snappy.h>
30 #include "impl/environment.hh"
31 
32 NS_SERVER_BEGIN
33 
40 {
41 public:
51  bool compress(const std::vector<char> & input, std::vector<char> & output) const{
52  return compressTemplate(input, output);
53  }
54  bool compress(const std::vector<signed char> & input, std::vector<signed char> & output) const{
55  return compressTemplate(input, output);
56  }
57  bool compress(const std::vector<unsigned char> & input, std::vector<unsigned char> & output) const{
58  return compressTemplate(input, output);
59  }
60  bool compress(const std::string & input, std::string & output) const{
61  return compressTemplate(input, output);
62  }
73  bool decompress(const std::vector<char> & input, std::vector<char> & output) const{
74  return decompressTemplate(input, output);
75  }
76  bool decompress(const std::vector<signed char> & input, std::vector<signed char> & output) const{
77  return decompressTemplate(input, output);
78  }
79  bool decompress(const std::vector<unsigned char> & input, std::vector<unsigned char> & output) const{
80  return decompressTemplate(input, output);
81  }
82  bool decompress(const std::string & input, std::string & output) const{
83  return decompressTemplate(input, output);
84  }
86 private:
87  template<class Buffer>
88  bool compressTemplate(const Buffer & input, Buffer & output) const{
89  size_t sz = snappy::MaxCompressedLength(input.size());
90  output.resize(sz);
91  snappy::RawCompress(reinterpret_cast<const char *>(&input[0]), input.size(), reinterpret_cast<char *>(&output[0]), &sz);
92  output.resize(sz);
93  return true;
94  }
95  template<class Buffer>
96  bool decompressTemplate(const Buffer & input, Buffer & output) const{
97  if(!snappy::IsValidCompressedBuffer(reinterpret_cast<const char *>(&input[0]), input.size()))
98  return false;
99  size_t sz = 0;
100  if(!snappy::GetUncompressedLength(reinterpret_cast<const char *>(&input[0]), input.size(), &sz))
101  return false;
102  output.resize(sz);
103  snappy::RawUncompress(reinterpret_cast<const char *>(&input[0]), input.size(), reinterpret_cast<char *>(&output[0]));
104  return true;
105  }
106 };
107 
108 NS_SERVER_END
109 
110 #endif
111 
A convenient interface for Snappy compression algorithm.
Definition: compress_snappy.hh:39