CernVM-FS  2.12.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
compression.h
Go to the documentation of this file.
1 
5 #ifndef CVMFS_COMPRESSION_COMPRESSION_H_
6 #define CVMFS_COMPRESSION_COMPRESSION_H_
7 
8 #include <errno.h>
9 #include <stdint.h>
10 #include <stdio.h>
11 
12 #include <string>
13 
14 #include "duplex_zlib.h"
15 #include "network/sink.h"
16 #include "util/plugin.h"
17 
18 namespace shash {
19 struct Any;
20 class ContextPtr;
21 }
22 
23 bool CopyPath2Path(const std::string &src, const std::string &dest);
24 bool CopyPath2File(const std::string &src, FILE *fdest);
25 bool CopyMem2Path(const unsigned char *buffer, const unsigned buffer_size,
26  const std::string &path);
27 bool CopyMem2File(const unsigned char *buffer, const unsigned buffer_size,
28  FILE *fdest);
29 bool CopyPath2Mem(const std::string &path,
30  unsigned char **buffer, unsigned *buffer_size);
31 
32 namespace zlib {
33 
34 const unsigned kZChunk = 16384;
35 
41 };
42 
43 // Do not change order of algorithms. Used as flags in the catalog
44 enum Algorithms {
47 };
48 
61 class Compressor: public PolymorphicConstruction<Compressor, Algorithms> {
62  public:
63  explicit Compressor(const Algorithms & /* alg */) { }
64  virtual ~Compressor() { }
82  virtual bool Deflate(const bool flush,
83  unsigned char **inbuf, size_t *inbufsize,
84  unsigned char **outbuf, size_t *outbufsize) = 0;
85 
91  virtual size_t DeflateBound(const size_t bytes) = 0;
92  virtual Compressor* Clone() = 0;
93 
94  static void RegisterPlugins();
95 };
96 
97 
98 class ZlibCompressor: public Compressor {
99  public:
100  explicit ZlibCompressor(const Algorithms &alg);
101  ZlibCompressor(const ZlibCompressor &other);
102  ~ZlibCompressor();
103 
104  bool Deflate(const bool flush,
105  unsigned char **inbuf, size_t *inbufsize,
106  unsigned char **outbuf, size_t *outbufsize);
107  size_t DeflateBound(const size_t bytes);
108  Compressor* Clone();
109  static bool WillHandle(const zlib::Algorithms &alg);
110 
111  private:
112  z_stream stream_;
113 };
114 
115 
116 class EchoCompressor: public Compressor {
117  public:
118  explicit EchoCompressor(const Algorithms &alg);
119  bool Deflate(const bool flush,
120  unsigned char **inbuf, size_t *inbufsize,
121  unsigned char **outbuf, size_t *outbufsize);
122  size_t DeflateBound(const size_t bytes);
123  Compressor* Clone();
124  static bool WillHandle(const zlib::Algorithms &alg);
125 };
126 
127 
128 Algorithms ParseCompressionAlgorithm(const std::string &algorithm_option);
129 std::string AlgorithmName(const zlib::Algorithms alg);
130 
131 
132 void CompressInit(z_stream *strm);
133 void DecompressInit(z_stream *strm);
134 void CompressFini(z_stream *strm);
135 void DecompressFini(z_stream *strm);
136 
138  const void *buf, const int64_t size, const bool eof,
139  z_stream *strm, shash::ContextPtr *hash_context);
140 StreamStates DecompressZStream2File(const void *buf, const int64_t size,
141  z_stream *strm, FILE *f);
142 StreamStates DecompressZStream2Sink(const void *buf, const int64_t size,
143  z_stream *strm, cvmfs::Sink *sink);
144 
145 bool CompressPath2Path(const std::string &src, const std::string &dest);
146 bool CompressPath2Path(const std::string &src, const std::string &dest,
147  shash::Any *compressed_hash);
148 bool DecompressPath2Path(const std::string &src, const std::string &dest);
149 
150 bool CompressPath2Null(const std::string &src, shash::Any *compressed_hash);
151 bool CompressFile2Null(FILE *fsrc, shash::Any *compressed_hash);
152 bool CompressFd2Null(int fd_src, shash::Any *compressed_hash,
153  uint64_t* size = NULL);
154 bool CompressFile2File(FILE *fsrc, FILE *fdest);
155 bool CompressFile2File(FILE *fsrc, FILE *fdest, shash::Any *compressed_hash);
156 bool CompressPath2File(const std::string &src, FILE *fdest,
157  shash::Any *compressed_hash);
158 bool DecompressFile2File(FILE *fsrc, FILE *fdest);
159 bool DecompressPath2File(const std::string &src, FILE *fdest);
160 
161 bool CompressMem2File(const unsigned char *buf, const size_t size,
162  FILE *fdest, shash::Any *compressed_hash);
163 
164 // User of these functions has to free out_buf, if successful
165 bool CompressMem2Mem(const void *buf, const int64_t size,
166  void **out_buf, uint64_t *out_size);
167 bool DecompressMem2Mem(const void *buf, const int64_t size,
168  void **out_buf, uint64_t *out_size);
169 
170 } // namespace zlib
171 
172 #endif // CVMFS_COMPRESSION_COMPRESSION_H_
bool CompressPath2Null(const string &src, shash::Any *compressed_hash)
Definition: compression.cc:515
Compressor(const Algorithms &)
Definition: compression.h:63
Algorithms ParseCompressionAlgorithm(const std::string &algorithm_option)
Definition: compression.cc:148
StreamStates DecompressZStream2Sink(const void *buf, const int64_t size, z_stream *strm, cvmfs::Sink *sink)
Definition: compression.cc:234
virtual bool Deflate(const bool flush, unsigned char **inbuf, size_t *inbufsize, unsigned char **outbuf, size_t *outbufsize)=0
Compressor * Clone()
Definition: compression.cc:953
bool CompressPath2File(const string &src, FILE *fdest, shash::Any *compressed_hash)
Definition: compression.cc:573
EchoCompressor(const Algorithms &alg)
Definition: compression.cc:942
bool CopyMem2File(const unsigned char *buffer, const unsigned buffer_size, FILE *fdest)
Definition: compression.cc:86
static bool WillHandle(const zlib::Algorithms &alg)
Definition: compression.cc:948
void DecompressInit(z_stream *strm)
Definition: compression.cc:185
virtual size_t DeflateBound(const size_t bytes)=0
bool DecompressMem2Mem(const void *buf, const int64_t size, void **out_buf, uint64_t *out_size)
Definition: compression.cc:797
StreamStates DecompressZStream2File(const void *buf, const int64_t size, z_stream *strm, FILE *f)
Definition: compression.cc:275
std::string AlgorithmName(const zlib::Algorithms alg)
Definition: compression.cc:158
bool Deflate(const bool flush, unsigned char **inbuf, size_t *inbufsize, unsigned char **outbuf, size_t *outbufsize)
Definition: compression.cc:896
StreamStates
Definition: compression.h:36
Compressor * Clone()
Definition: compression.cc:885
void DecompressFini(z_stream *strm)
Definition: compression.cc:201
bool CopyPath2Path(const string &src, const string &dest)
Definition: compression.cc:63
bool CopyPath2Mem(const string &path, unsigned char **buffer, unsigned *buffer_size)
Definition: compression.cc:108
const unsigned kZChunk
Definition: compression.h:34
bool CopyPath2File(const std::string &src, FILE *fdest)
Definition: compression.cc:46
StreamStates CompressZStream2Null(const void *buf, const int64_t size, const bool eof, z_stream *strm, shash::ContextPtr *hash_context)
Definition: compression.cc:206
bool DecompressFile2File(FILE *fsrc, FILE *fdest)
Definition: compression.cc:640
bool CompressFile2File(FILE *fsrc, FILE *fdest)
Definition: compression.cc:525
size_t DeflateBound(const size_t bytes)
Definition: compression.cc:933
bool CopyMem2Path(const unsigned char *buffer, const unsigned buffer_size, const string &path)
Definition: compression.cc:94
Algorithms
Definition: compression.h:44
static void RegisterPlugins()
Definition: compression.cc:858
bool CompressFd2Null(int fd_src, shash::Any *compressed_hash, uint64_t *processed_bytes)
Definition: compression.cc:453
void CompressFini(z_stream *strm)
Definition: compression.cc:196
bool DecompressPath2File(const string &src, FILE *fdest)
Definition: compression.cc:667
void CompressInit(z_stream *strm)
Definition: compression.cc:174
bool Deflate(const bool flush, unsigned char **inbuf, size_t *inbufsize, unsigned char **outbuf, size_t *outbufsize)
Definition: compression.cc:958
static bool WillHandle(const zlib::Algorithms &alg)
Definition: compression.cc:867
virtual ~Compressor()
Definition: compression.h:64
bool CompressFile2Null(FILE *fsrc, shash::Any *compressed_hash)
Definition: compression.cc:401
bool CompressMem2File(const unsigned char *buf, const size_t size, FILE *fdest, shash::Any *compressed_hash)
Definition: compression.cc:678
bool DecompressPath2Path(const string &src, const string &dest)
Definition: compression.cc:381
bool CompressMem2Mem(const void *buf, const int64_t size, void **out_buf, uint64_t *out_size)
Definition: compression.cc:739
virtual Compressor * Clone()=0
static void size_t size
Definition: smalloc.h:54
ZlibCompressor(const Algorithms &alg)
Definition: compression.cc:872
size_t DeflateBound(const size_t bytes)
Definition: compression.cc:976
bool CompressPath2Path(const string &src, const string &dest)
Definition: compression.cc:318