GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/compression/compression.h
Date: 2025-06-22 02:36:02
Exec Total Coverage
Lines: 2 2 100.0%
Branches: 0 0 -%

Line Branch Exec Source
1 /**
2 * This file is part of the CernVM File System.
3 */
4
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 } // namespace shash
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, unsigned char **buffer,
30 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 implementation of the
58 * Compressor::RegisterPlugins function.
59 *
60 */
61 class Compressor : public PolymorphicConstruction<Compressor, Algorithms> {
62 public:
63 251477 explicit Compressor(const Algorithms & /* alg */) { }
64 504390 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 - Output 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, unsigned char **inbuf,
83 size_t *inbufsize, unsigned char **outbuf,
84 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 ZlibCompressor(const ZlibCompressor &other);
102 ~ZlibCompressor();
103
104 bool Deflate(const bool flush, unsigned char **inbuf, size_t *inbufsize,
105 unsigned char **outbuf, size_t *outbufsize);
106 size_t DeflateBound(const size_t bytes);
107 Compressor *Clone();
108 static bool WillHandle(const zlib::Algorithms &alg);
109
110 private:
111 z_stream stream_;
112 };
113
114
115 class EchoCompressor : public Compressor {
116 public:
117 explicit EchoCompressor(const Algorithms &alg);
118 bool Deflate(const bool flush, unsigned char **inbuf, size_t *inbufsize,
119 unsigned char **outbuf, size_t *outbufsize);
120 size_t DeflateBound(const size_t bytes);
121 Compressor *Clone();
122 static bool WillHandle(const zlib::Algorithms &alg);
123 };
124
125
126 Algorithms ParseCompressionAlgorithm(const std::string &algorithm_option);
127 std::string AlgorithmName(const zlib::Algorithms alg);
128
129
130 void CompressInit(z_stream *strm);
131 void DecompressInit(z_stream *strm);
132 void CompressFini(z_stream *strm);
133 void DecompressFini(z_stream *strm);
134
135 StreamStates CompressZStream2Null(const void *buf, const int64_t size,
136 const bool eof, z_stream *strm,
137 shash::ContextPtr *hash_context);
138 StreamStates DecompressZStream2File(const void *buf, const int64_t size,
139 z_stream *strm, FILE *f);
140 StreamStates DecompressZStream2Sink(const void *buf, const int64_t size,
141 z_stream *strm, cvmfs::Sink *sink);
142
143 bool CompressPath2Path(const std::string &src, const std::string &dest);
144 bool CompressPath2Path(const std::string &src, const std::string &dest,
145 shash::Any *compressed_hash);
146 bool DecompressPath2Path(const std::string &src, const std::string &dest);
147
148 bool CompressPath2Null(const std::string &src, shash::Any *compressed_hash);
149 bool CompressFile2Null(FILE *fsrc, shash::Any *compressed_hash);
150 bool CompressFd2Null(int fd_src, shash::Any *compressed_hash,
151 uint64_t *size = NULL);
152 bool CompressFile2File(FILE *fsrc, FILE *fdest);
153 bool CompressFile2File(FILE *fsrc, FILE *fdest, shash::Any *compressed_hash);
154 bool CompressPath2File(const std::string &src, FILE *fdest,
155 shash::Any *compressed_hash);
156 bool DecompressFile2File(FILE *fsrc, FILE *fdest);
157 bool DecompressPath2File(const std::string &src, FILE *fdest);
158
159 bool CompressMem2File(const unsigned char *buf, const size_t size, FILE *fdest,
160 shash::Any *compressed_hash);
161
162 // User of these functions has to free out_buf, if successful
163 bool CompressMem2Mem(const void *buf, const int64_t size, void **out_buf,
164 uint64_t *out_size);
165 bool DecompressMem2Mem(const void *buf, const int64_t size, void **out_buf,
166 uint64_t *out_size);
167
168 } // namespace zlib
169
170 #endif // CVMFS_COMPRESSION_COMPRESSION_H_
171