GCC Code Coverage Report
Directory: cvmfs/ Exec Total Coverage
File: cvmfs/compression.h Lines: 3 3 100.0 %
Date: 2019-02-03 02:48:13 Branches: 2 4 50.0 %

Line Branch Exec Source
1
/**
2
 * This file is part of the CernVM File System.
3
 */
4
5
#ifndef CVMFS_COMPRESSION_H_
6
#define CVMFS_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 "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
36
enum StreamStates {
37
  kStreamDataError = 0,
38
  kStreamIOError,
39
  kStreamContinue,
40
  kStreamEnd,
41
};
42
43
// Do not change order of algorithms.  Used as flags in the catalog
44
enum Algorithms {
45
  kZlibDefault = 0,
46
  kNoCompression,
47
};
48
49
/**
50
 * Abstract Compression class which is inherited by implementations of
51
 * compression engines such as zlib.
52
 *
53
 * In order to add a new compression method, you simply need to add a new class
54
 * which is a sub-class of the Compressor.  The subclass needs to implement the
55
 * Deflate, DeflateBound, Clone, and WillHandle functions.  For information on
56
 * the WillHandle function, read up on the PolymorphicConstruction class.
57
 * The new sub-class must be listed in the implemention of the
58
 * Compressor::RegisterPlugins function.
59
 *
60
 */
61
class Compressor: public PolymorphicConstruction<Compressor, Algorithms> {
62
 public:
63
6829
  explicit Compressor(const Algorithms &alg) { }
64
6829
  virtual ~Compressor() { }
65
  /**
66
   * Deflate function.  The arguments and returns closely match the input and
67
   * output of the zlib deflate function.
68
   * Input:
69
   *   - outbuf - Ouput buffer to write the compressed data.
70
   *   - outbufsize - Size of the output buffer
71
   *   - inbuf - Input data to be compressed
72
   *   - inbufsize - Size of the input buffer
73
   *   - flush - Whether the compression stream should be flushed / finished
74
   * Upon return:
75
   *   returns: true - if done compressing, false otherwise
76
   *   - outbuf - output buffer pointer (unchanged from input)
77
   *   - outbufsize - The number of bytes used in the outbuf
78
   *   - inbuf - Pointer to the next byte of input to read in
79
   *   - inbufsize - the remaining bytes of input to read in.
80
   *   - flush - unchanged from input
81
   */
82
  virtual bool Deflate(const bool flush,
83
                       unsigned char **inbuf, size_t *inbufsize,
84
                       unsigned char **outbuf, size_t *outbufsize) = 0;
85
86
  /**
87
   * Return an upper bound on the number of bytes required in order to compress
88
   * an input number of bytes.
89
   * Returns: Upper bound on the number of bytes required to compress.
90
   */
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
  explicit 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
8
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
137
StreamStates CompressZStream2Null(
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_H_