• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 5079590438

25 May 2023 12:28PM UTC coverage: 92.228% (+0.5%) from 91.723%
5079590438

Pull #3502

github

Pull Request #3502: Apply clang-format to the codebase

75589 of 81959 relevant lines covered (92.23%)

12139530.51 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

75.0
/src/lib/compression/compression.h
1
/*
2
* Compression Transform
3
* (C) 2014 Jack Lloyd
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7

8
#ifndef BOTAN_COMPRESSION_TRANSFORM_H_
9
#define BOTAN_COMPRESSION_TRANSFORM_H_
10

11
#include <botan/exceptn.h>
12
#include <botan/secmem.h>
13
#include <string>
14

15
namespace Botan {
16

17
/**
18
* Interface for a compression algorithm.
19
*/
20
class BOTAN_PUBLIC_API(2, 0) Compression_Algorithm {
33✔
21
   public:
22
      /**
23
      * Create an instance based on a name, or return null if the
24
      * algo combination cannot be found.
25
      */
26
      static std::unique_ptr<Compression_Algorithm> create(std::string_view algo_spec);
27

28
      /**
29
      * Create an instance based on a name
30
      * @param algo_spec algorithm name
31
      * Throws Lookup_Error if not found.
32
      */
33
      static std::unique_ptr<Compression_Algorithm> create_or_throw(std::string_view algo_spec);
34

35
      /**
36
      * Begin compressing. Most compression algorithms offer a tunable
37
      * time/compression tradeoff parameter generally represented by
38
      * an integer in the range of 1 to 9.
39
      *
40
      * If 0 or a value out of range is provided, a compression algorithm
41
      * specific default is used.
42
      */
43
      virtual void start(size_t comp_level = 0) = 0;
44

45
      /**
46
      * Process some data.
47
      * @param buf in/out parameter which will possibly be resized or swapped
48
      * @param offset an offset into blocks to begin processing
49
      * @param flush if true the compressor will be told to flush state
50
      */
51
      virtual void update(secure_vector<uint8_t>& buf, size_t offset = 0, bool flush = false) = 0;
52

53
      /**
54
      * Finish compressing
55
      *
56
      * @param final_block in/out parameter
57
      * @param offset an offset into final_block to begin processing
58
      */
59
      virtual void finish(secure_vector<uint8_t>& final_block, size_t offset = 0) = 0;
60

61
      /**
62
      * @return name of the compression algorithm
63
      */
64
      virtual std::string name() const = 0;
65

66
      /**
67
      * Reset the state and abort the current message; start can be
68
      * called again to process a new message.
69
      */
70
      virtual void clear() = 0;
71

72
      virtual ~Compression_Algorithm() = default;
73
};
74

75
/*
76
* Interface for a decompression algorithm.
77
*/
78
class BOTAN_PUBLIC_API(2, 0) Decompression_Algorithm {
18✔
79
   public:
80
      /**
81
      * Create an instance based on a name, or return null if the
82
      * algo combination cannot be found.
83
      */
84
      static std::unique_ptr<Decompression_Algorithm> create(std::string_view algo_spec);
85

86
      /**
87
      * Create an instance based on a name
88
      * @param algo_spec algorithm name
89
      * Throws Lookup_Error if not found.
90
      */
91
      static std::unique_ptr<Decompression_Algorithm> create_or_throw(std::string_view algo_spec);
92

93
      /**
94
      * Begin decompressing.
95
      * Decompression does not support levels, as compression does.
96
      */
97
      virtual void start() = 0;
98

99
      /**
100
      * Process some data.
101
      * @param buf in/out parameter which will possibly be resized or swapped
102
      * @param offset an offset into blocks to begin processing
103
      */
104
      virtual void update(secure_vector<uint8_t>& buf, size_t offset = 0) = 0;
105

106
      /**
107
      * Finish decompressing
108
      *
109
      * @param final_block in/out parameter
110
      * @param offset an offset into final_block to begin processing
111
      */
112
      virtual void finish(secure_vector<uint8_t>& final_block, size_t offset = 0) = 0;
113

114
      /**
115
      * @return name of the decompression algorithm
116
      */
117
      virtual std::string name() const = 0;
118

119
      /**
120
      * Reset the state and abort the current message; start can be
121
      * called again to process a new message.
122
      */
123
      virtual void clear() = 0;
124

125
      virtual ~Decompression_Algorithm() = default;
126
};
127

128
BOTAN_DEPRECATED("Use Compression_Algorithm::create")
129

130
inline Compression_Algorithm* make_compressor(std::string_view type) {
131
   return Compression_Algorithm::create(type).release();
132
}
133

134
BOTAN_DEPRECATED("Use Decompression_Algorithm::create")
135

136
inline Decompression_Algorithm* make_decompressor(std::string_view type) {
137
   return Decompression_Algorithm::create(type).release();
138
}
139

140
/**
141
* An error that occurred during compression (or decompression)
142
*/
143
class BOTAN_PUBLIC_API(2, 9) Compression_Error final : public Exception {
144
   public:
145
      /**
146
      * @param func_name the name of the compression API that was called
147
      * (eg "BZ2_bzCompressInit" or "lzma_code")
148
      * @param type what library this came from
149
      * @param rc the error return code from the compression API. The
150
      * interpretation of this value will depend on the library.
151
      */
152
      Compression_Error(const char* func_name, ErrorType type, int rc);
153

154
      ErrorType error_type() const noexcept override { return m_type; }
×
155

156
      int error_code() const noexcept override { return m_rc; }
×
157

158
   private:
159
      ErrorType m_type;
160
      int m_rc;
161
};
162

163
/**
164
* Adapts a zlib style API
165
*/
166
class Compression_Stream {
229✔
167
   public:
168
      virtual ~Compression_Stream() = default;
229✔
169

170
      virtual void next_in(uint8_t* b, size_t len) = 0;
171

172
      virtual void next_out(uint8_t* b, size_t len) = 0;
173

174
      virtual size_t avail_in() const = 0;
175

176
      virtual size_t avail_out() const = 0;
177

178
      virtual uint32_t run_flag() const = 0;
179
      virtual uint32_t flush_flag() const = 0;
180
      virtual uint32_t finish_flag() const = 0;
181

182
      virtual bool run(uint32_t flags) = 0;
183
};
184

185
/**
186
* Used to implement compression using Compression_Stream
187
*/
188
class Stream_Compression : public Compression_Algorithm {
33✔
189
   public:
190
      void update(secure_vector<uint8_t>& buf, size_t offset, bool flush) final override;
191

192
      void finish(secure_vector<uint8_t>& buf, size_t offset) final override;
193

194
      void clear() final override;
195

196
   private:
197
      void start(size_t level) final override;
198

199
      void process(secure_vector<uint8_t>& buf, size_t offset, uint32_t flags);
200

201
      virtual std::unique_ptr<Compression_Stream> make_stream(size_t level) const = 0;
202

203
      secure_vector<uint8_t> m_buffer;
204
      std::unique_ptr<Compression_Stream> m_stream;
205
};
206

207
/**
208
* FIXME add doc
209
*/
210
class Stream_Decompression : public Decompression_Algorithm {
18✔
211
   public:
212
      void update(secure_vector<uint8_t>& buf, size_t offset) final override;
213

214
      void finish(secure_vector<uint8_t>& buf, size_t offset) final override;
215

216
      void clear() final override;
217

218
   private:
219
      void start() final override;
220

221
      void process(secure_vector<uint8_t>& buf, size_t offset, uint32_t flags);
222

223
      virtual std::unique_ptr<Compression_Stream> make_stream() const = 0;
224

225
      secure_vector<uint8_t> m_buffer;
226
      std::unique_ptr<Compression_Stream> m_stream;
227
};
228

229
}
230

231
#endif
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc