StormByte-Crypto 2.0.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
48
49#include <span>
50
55namespace StormByte {
60 namespace Crypto {
65 namespace Compressor {
70 enum class Type {
71 Bzip2,
72 Zlib
73 };
74
80 public:
89 Generic(const Generic& other) = default;
90
95 Generic(Generic&& other) noexcept = default;
96
100 virtual ~Generic() noexcept;
101
107 Generic& operator=(const Generic& other) = default;
108
114 Generic& operator=(Generic&& other) noexcept = default;
127 inline bool Compress(const std::span<const std::byte> input, Buffer::WriteOnly& output) const noexcept {
128 return DoCompress(input, output);
129 }
130
138 template<StormByte::Type::ByteInputRange Range>
139 bool Compress(const Range& input, Buffer::WriteOnly& output) const {
141 for (const auto value: input) {
142 data.emplace_back(static_cast<std::byte>(value));
143 }
144 return Compress(std::span<const std::byte>(data), output);
145 }
146
153 inline bool Compress(const Buffer::ReadOnly& input, Buffer::WriteOnly& output) const noexcept {
154 return DoCompress(const_cast<Buffer::ReadOnly&>(input), output, ReadMode::Copy);
155 }
156
163 inline bool Compress(Buffer::ReadOnly& input, Buffer::WriteOnly& output) const noexcept {
164 return DoCompress(input, output, ReadMode::Move);
165 }
166
173 inline Buffer::Consumer Compress(Buffer::Consumer consumer, ReadMode mode = ReadMode::Move) const noexcept {
174 return DoCompress(consumer, mode);
175 }
188 inline bool Decompress(const std::span<const std::byte> input, Buffer::WriteOnly& output) const noexcept {
189 return DoDecompress(input, output);
190 }
191
199 template<StormByte::Type::ByteInputRange Range>
200 bool Decompress(const Range& input, Buffer::WriteOnly& output) const {
202 for (const auto value: input) {
203 data.emplace_back(static_cast<std::byte>(value));
204 }
205 return Decompress(std::span<const std::byte>(data), output);
206 }
207
214 inline bool Decompress(const Buffer::ReadOnly& input, Buffer::WriteOnly& output) const noexcept {
215 return DoDecompress(const_cast<Buffer::ReadOnly&>(input), output, ReadMode::Copy);
216 }
217
224 inline bool Decompress(Buffer::ReadOnly& input, Buffer::WriteOnly& output) const noexcept {
225 return DoDecompress(input, output, ReadMode::Move);
226 }
227
234 inline Buffer::Consumer Decompress(Buffer::Consumer consumer, ReadMode mode = ReadMode::Move) const noexcept {
235 return DoDecompress(consumer, mode);
236 }
243 unsigned short Level() const noexcept {
244 return m_level;
245 }
246
251 inline enum Type Type() const noexcept {
252 return m_type;
253 }
254
255 protected:
257 unsigned short m_level = 0;
258
264 inline Generic(enum Type type, unsigned short level = 5):
265 m_type(type), m_level(level) {}
266
267 private:
274 virtual bool DoCompress(std::span<const std::byte> input, Buffer::WriteOnly& output) const noexcept = 0;
275
283 bool DoCompress(Buffer::ReadOnly& input, Buffer::WriteOnly& output, ReadMode mode) const noexcept;
284
291 virtual Buffer::Consumer DoCompress(Buffer::Consumer consumer, ReadMode mode) const noexcept = 0;
292
299 virtual bool DoDecompress(std::span<const std::byte> input, Buffer::WriteOnly& output) const noexcept = 0;
300
308 bool DoDecompress(Buffer::ReadOnly& input, Buffer::WriteOnly& output, ReadMode mode) const noexcept;
309
316 virtual Buffer::Consumer DoDecompress(Buffer::Consumer consumer, ReadMode mode) const noexcept = 0;
317 };
318
325 STORMBYTE_CRYPTO_PUBLIC Generic::PointerType Create(Type type, unsigned short level) noexcept;
326 }
327 }
328}
reference emplace_back(T &&value)
bzip2 compressor.
Definition bzip2.hxx:64
Abstract compressor.
Definition generic.hxx:79
bool Compress(const Range &input, Buffer::WriteOnly &output) const
Compress an input range of byte-convertible values.
Definition generic.hxx:139
enum Type Type() const noexcept
Codec of this compressor.
Definition generic.hxx:251
bool Decompress(const Range &input, Buffer::WriteOnly &output) const
Decompress an input range of byte-convertible values.
Definition generic.hxx:200
bool Decompress(Buffer::ReadOnly &input, Buffer::WriteOnly &output) const noexcept
Decompress a buffer, consuming it.
Definition generic.hxx:224
Buffer::Consumer Compress(Buffer::Consumer consumer, ReadMode mode=ReadMode::Move) const noexcept
Compress a Consumer into another Consumer.
Definition generic.hxx:173
Buffer::Consumer Decompress(Buffer::Consumer consumer, ReadMode mode=ReadMode::Move) const noexcept
Decompress a Consumer into another Consumer.
Definition generic.hxx:234
bool Decompress(const std::span< const std::byte > input, Buffer::WriteOnly &output) const noexcept
Decompress a byte span.
Definition generic.hxx:188
Generic(Generic &&other) noexcept=default
Move constructor.
Generic(const Generic &other)=default
Copy constructor.
unsigned short Level() const noexcept
Compression level.
Definition generic.hxx:243
virtual ~Generic() noexcept
Destructor.
bool Compress(const Buffer::ReadOnly &input, Buffer::WriteOnly &output) const noexcept
Compress a read-only buffer (copy).
Definition generic.hxx:153
bool Decompress(const Buffer::ReadOnly &input, Buffer::WriteOnly &output) const noexcept
Decompress a read-only buffer (copy).
Definition generic.hxx:214
enum Type m_type
Codec.
Definition generic.hxx:256
Generic(enum Type type, unsigned short level=5)
Construct with a codec and level.
Definition generic.hxx:264
bool Compress(Buffer::ReadOnly &input, Buffer::WriteOnly &output) const noexcept
Compress a buffer, consuming it.
Definition generic.hxx:163
zlib compressor.
Definition zlib.hxx:64
Generic::PointerType Create(Type type, unsigned short level) noexcept
Factory for a compressor.
ReadMode
How a source buffer is consumed.
Definition typedefs.hxx:57
Root namespace of the StormByte suite.
STL namespace.
#define STORMBYTE_CRYPTO_PUBLIC
Definition visibility.h:53