StormByte-Crypto 3.1.0
C++26 cryptography module of the StormByte suite
Loading...
Searching...
No Matches
generic.hxx
Go to the documentation of this file.
1/*
2 * Copyright (C) 2024-2026 David C. Manuelda (StormBytePP)
3 *
4 * This file is part of StormByte-Crypto.
5 *
6 * StormByte-Crypto original source is dual-licensed:
7 *
8 * 1. GNU Lesser General Public License v3.0 (or later)
9 * You may redistribute and/or modify this file under the terms of the
10 * GNU Lesser General Public License as published by the Free Software
11 * Foundation, either version 3 of the License, or (at your option)
12 * any later version.
13 *
14 * 2. Commercial license
15 * Alternatively, this file may be used under the terms of a commercial
16 * license agreement with the copyright holder
17 * (David C. Manuelda <StormByte@gmail.com>).
18 *
19 * Both licenses apply only to original StormByte-Crypto source in this
20 * repository. They do not cover other StormByte modules or any third-party
21 * material shipped with this repository (including everything under
22 * thirdparty/, in particular Crypto++, bundled libbzip2, the StormByte-Buffer
23 * tree and the StormByte suite it vendors), which remains under its own license.
24 *
25 * Neither license grants any patent rights. Any patent licenses required
26 * to use this software or third-party components must be obtained separately
27 * from the patent holders.
28 *
29 * StormByte-Crypto is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU Lesser General Public License for more details.
33 *
34 * You should have received a copy of the GNU Lesser General Public License
35 * version 3 along with StormByte-Crypto. If not, see
36 * <https://www.gnu.org/licenses/lgpl-3.0.html>.
37 *
38 * SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-StormByte-Commercial
39 */
40
41#pragma once
42
43#include <StormByte/buffer/consumer.hxx>
46#include <StormByte/safe/binary.hxx>
47#include <StormByte/safe/clonable.hxx>
48#include <StormByte/type_traits.hxx>
49#include <StormByte/type_traits/safe.hxx>
50
51#include <span>
52
57namespace StormByte {
62 namespace Crypto {
67 namespace Compressor {
72 enum class Type {
75 };
76
89 public:
98 Generic(const Generic& other) = default;
99
104 Generic(Generic&& other) noexcept = default;
105
109 virtual ~Generic() noexcept;
110
116 Generic& operator=(const Generic& other) = default;
117
123 Generic& operator=(Generic&& other) noexcept = default;
125
136 inline bool Compress(const std::span<const std::byte> input, Buffer::WriteOnly& output) const noexcept {
137 return DoCompress(input, output);
138 }
139
147 template<StormByte::Type::ByteInputRange Range>
148 bool Compress(const Range& input, Buffer::WriteOnly& output) const {
149 StormByte::Safe::Binary data(input);
150 return Compress(std::span<const std::byte>{data.span()}, output);
151 }
152
159 inline bool Compress(const Buffer::ReadOnly& input, Buffer::WriteOnly& output) const noexcept {
160 return DoCompress(const_cast<Buffer::ReadOnly&>(input), output, ReadMode::Copy);
161 }
162
169 inline bool Compress(Buffer::ReadOnly& input, Buffer::WriteOnly& output) const noexcept {
170 return DoCompress(input, output, ReadMode::Move);
171 }
172
179 inline Buffer::Consumer Compress(Buffer::Consumer consumer, ReadMode mode = ReadMode::Move) const noexcept {
180 return DoCompress(consumer, mode);
181 }
182
183
194 inline bool Decompress(const std::span<const std::byte> input, Buffer::WriteOnly& output) const noexcept {
195 return DoDecompress(input, output);
196 }
197
205 template<StormByte::Type::ByteInputRange Range>
206 bool Decompress(const Range& input, Buffer::WriteOnly& output) const {
207 StormByte::Safe::Binary data(input);
208 return Decompress(std::span<const std::byte>{data.span()}, output);
209 }
210
217 inline bool Decompress(const Buffer::ReadOnly& input, Buffer::WriteOnly& output) const noexcept {
218 return DoDecompress(const_cast<Buffer::ReadOnly&>(input), output, ReadMode::Copy);
219 }
220
227 inline bool Decompress(Buffer::ReadOnly& input, Buffer::WriteOnly& output) const noexcept {
228 return DoDecompress(input, output, ReadMode::Move);
229 }
230
237 inline Buffer::Consumer Decompress(Buffer::Consumer consumer, ReadMode mode = ReadMode::Move) const noexcept {
238 return DoDecompress(consumer, mode);
239 }
240
241
246 unsigned short Level() const noexcept {
247 return m_level;
248 }
249
254 inline enum Type Type() const noexcept {
255 return m_type;
256 }
257
258 protected:
260 unsigned short m_level = 0;
261
267 inline Generic(enum Type type, unsigned short level = 5):
268 m_type(type), m_level(level) {}
269
270 private:
277 virtual bool DoCompress(std::span<const std::byte> input, Buffer::WriteOnly& output) const noexcept = 0;
278
286 bool DoCompress(Buffer::ReadOnly& input, Buffer::WriteOnly& output, ReadMode mode) const noexcept;
287
294 virtual Buffer::Consumer DoCompress(Buffer::Consumer consumer, ReadMode mode) const noexcept = 0;
295
302 virtual bool DoDecompress(std::span<const std::byte> input, Buffer::WriteOnly& output) const noexcept = 0;
303
311 bool DoDecompress(Buffer::ReadOnly& input, Buffer::WriteOnly& output, ReadMode mode) const noexcept;
312
319 virtual Buffer::Consumer DoDecompress(Buffer::Consumer consumer, ReadMode mode) const noexcept = 0;
320 };
321
328 STORMBYTE_CRYPTO_PUBLIC Generic::PointerType Create(Type type, unsigned short level) noexcept;
329 }
330 }
331}
332
bzip2 compressor.
Definition bzip2.hxx:66
Abstract compressor.
Definition generic.hxx:88
bool Compress(const Range &input, Buffer::WriteOnly &output) const
Compress an input range of byte-convertible values.
Definition generic.hxx:148
enum Type Type() const noexcept
Codec of this compressor.
Definition generic.hxx:254
bool Decompress(const Range &input, Buffer::WriteOnly &output) const
Decompress an input range of byte-convertible values.
Definition generic.hxx:206
bool Decompress(Buffer::ReadOnly &input, Buffer::WriteOnly &output) const noexcept
Decompress a buffer, consuming it.
Definition generic.hxx:227
Buffer::Consumer Compress(Buffer::Consumer consumer, ReadMode mode=ReadMode::Move) const noexcept
Compress a Consumer into another Consumer.
Definition generic.hxx:179
Buffer::Consumer Decompress(Buffer::Consumer consumer, ReadMode mode=ReadMode::Move) const noexcept
Decompress a Consumer into another Consumer.
Definition generic.hxx:237
bool Decompress(const std::span< const std::byte > input, Buffer::WriteOnly &output) const noexcept
Decompress a byte span.
Definition generic.hxx:194
Generic(Generic &&other) noexcept=default
Move constructor.
Generic(const Generic &other)=default
Copy constructor.
unsigned short Level() const noexcept
Compression level.
Definition generic.hxx:246
virtual ~Generic() noexcept
Destructor.
bool Compress(const std::span< const std::byte > input, Buffer::WriteOnly &output) const noexcept
Compress a byte span.
Definition generic.hxx:136
bool Compress(const Buffer::ReadOnly &input, Buffer::WriteOnly &output) const noexcept
Compress a read-only buffer (copy).
Definition generic.hxx:159
bool Decompress(const Buffer::ReadOnly &input, Buffer::WriteOnly &output) const noexcept
Decompress a read-only buffer (copy).
Definition generic.hxx:217
unsigned short m_level
Compression level.
Definition generic.hxx:260
enum Type m_type
Codec.
Definition generic.hxx:259
Generic(enum Type type, unsigned short level=5)
Construct with a codec and level.
Definition generic.hxx:267
bool Compress(Buffer::ReadOnly &input, Buffer::WriteOnly &output) const noexcept
Compress a buffer, consuming it.
Definition generic.hxx:169
zlib compressor.
Definition zlib.hxx:66
STORMBYTE_FORCE_INLINE std::span< std::byte > span() noexcept
STORMBYTE_DECLARE_MAYBE_SAFE(class StormByte::System::Device::Access)
Compressors of the Crypto module.
Generic::PointerType Create(Type type, unsigned short level) noexcept
Factory for a compressor.
Crypto module of the StormByte suite.
Definition aead.hxx:53
ReadMode
How a source buffer is consumed.
Definition typedefs.hxx:57
@ Copy
Copy the input.
Definition typedefs.hxx:58
@ Move
Consume the input.
Definition typedefs.hxx:59
Root namespace of the StormByte suite.
STL namespace.
#define STORMBYTE_CRYPTO_PUBLIC
Definition visibility.h:53