C++ Essence Library 0.1.0
A Utility Library for Modern C++ Programming
Loading...
Searching...
No Matches
chunk_processor.hpp
1/*
2 * Copyright (c) 2024 The RefValue Project
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#pragma once
24
25#include "../../abi/string.hpp"
26#include "../../rational.hpp"
27
28#include <concepts>
29#include <cstddef>
30#include <memory>
31#include <span>
32#include <type_traits>
33#include <utility>
34
35namespace essence::crypto::abstract {
41 public:
42 template <typename T>
43 requires(!std::same_as<std::decay_t<T>, chunk_processor>)
44 explicit chunk_processor(T&& value) : wrapper_{std::make_unique<wrapper<T>>(std::forward<T>(value))} {}
45
50 [[nodiscard]] bool transformer() const {
51 return wrapper_->transformer();
52 }
53
58 [[nodiscard]] abi::string cipher_name() const {
59 return wrapper_->cipher_name();
60 }
61
66 [[nodiscard]] std::size_t buffer_size() const {
67 return wrapper_->buffer_size();
68 }
69
74 [[nodiscard]] std::size_t extra_size() const {
75 return wrapper_->extra_size();
76 }
77
83 [[nodiscard]] rational size_factor() const {
84 return wrapper_->size_factor();
85 }
86
90 void init() const {
91 wrapper_->init();
92 }
93
99 void update(std::span<const std::byte> input, std::span<std::byte>& output) const {
100 wrapper_->update(input, output);
101 }
102
107 void finalize(std::span<std::byte>& output) const {
108 wrapper_->finalize(output);
109 }
110
111 private:
112 struct base {
113 virtual ~base() = default;
114 virtual bool transformer() = 0;
115 virtual abi::string cipher_name() = 0;
116 virtual std::size_t buffer_size() = 0;
117 virtual std::size_t extra_size() = 0;
118 virtual rational size_factor() = 0;
119 virtual void init() = 0;
120 virtual void update(std::span<const std::byte> input, std::span<std::byte>& output) = 0;
121 virtual void finalize(std::span<std::byte>& output) = 0;
122 };
123
124 template <typename T>
125 class wrapper final : public base {
126 public:
127 template <std::convertible_to<T> U>
128 explicit wrapper(U&& value) : value_{std::forward<U>(value)} {}
129
130 bool transformer() override {
131 return value_.transformer();
132 }
133
134 abi::string cipher_name() override {
135 return value_.cipher_name();
136 }
137
138 std::size_t buffer_size() override {
139 return value_.buffer_size();
140 }
141
142 std::size_t extra_size() override {
143 return value_.extra_size();
144 }
145
146 rational size_factor() override {
147 return value_.size_factor();
148 }
149
150 void init() override {
151 value_.init();
152 }
153
154 void update(std::span<const std::byte> input, std::span<std::byte>& output) override {
155 value_.update(input, output);
156 }
157
158 void finalize(std::span<std::byte>& output) override {
159 value_.finalize(output);
160 }
161
162 private:
163 T value_;
164 };
165
166 std::unique_ptr<base> wrapper_;
167 };
168} // namespace essence::crypto::abstract
Provides a uniform interface for processing crypto chunks.
Definition chunk_processor.hpp:40
abi::string cipher_name() const
Gets the name of the cipher.
Definition chunk_processor.hpp:58
void update(std::span< const std::byte > input, std::span< std::byte > &output) const
Processes a memory chunk.
Definition chunk_processor.hpp:99
rational size_factor() const
Gets the factor to be multiplied to the input buffer size and the result is the size of the output bu...
Definition chunk_processor.hpp:83
bool transformer() const
Indicates whether this is a forward transformer.
Definition chunk_processor.hpp:50
std::size_t buffer_size() const
Gets the size of the input buffer.
Definition chunk_processor.hpp:66
std::size_t extra_size() const
Gets the extra size of the output buffer.
Definition chunk_processor.hpp:74
void finalize(std::span< std::byte > &output) const
Gets the final chunk.
Definition chunk_processor.hpp:107
void init() const
Initializes the processor.
Definition chunk_processor.hpp:90
Represents a rational number consisting of its corresponding numerator and denominator.
Definition rational.hpp:53