This commit is contained in:
2024-08-02 09:30:41 -05:00
parent 4fafc17b90
commit b399ff3291
20 changed files with 342 additions and 324 deletions

View File

@ -57,14 +57,13 @@ private:
bool use_s3_path_style_{false};
public:
[[nodiscard]] static auto construct_url(CURL *curl,
const std::string &relative_path,
const host_config &cfg)
-> std::string;
[[nodiscard]] static auto
construct_url(CURL *curl, const std::string &relative_path,
const host_config &cfg) -> std::string;
[[nodiscard]] static auto create_host_config(const s3_config &cfg,
bool use_s3_path_style)
-> host_config;
[[nodiscard]] static auto
create_host_config(const s3_config &cfg,
bool use_s3_path_style) -> host_config;
[[nodiscard]] static auto url_encode(CURL *curl, const std::string &data,
bool allow_slash) -> std::string;
@ -72,8 +71,8 @@ public:
template <typename request_type>
[[nodiscard]] static auto
make_encrypted_request(const host_config &cfg, const request_type &request,
long &response_code, stop_type &stop_requested)
-> bool {
long &response_code,
stop_type &stop_requested) -> bool {
response_code = 0;
if (not request.decryption_token.has_value() ||
@ -94,7 +93,7 @@ public:
utils::encryption::generate_key(request.decryption_token.value());
const auto result = utils::encryption::read_encrypted_range(
request.range.value(), key,
[&](std::vector<char> &ct, std::uint64_t start_offset,
[&](data_buffer &ct, std::uint64_t start_offset,
std::uint64_t end_offset) -> api_error {
auto encrypted_request = request;
encrypted_request.decryption_token = std::nullopt;
@ -212,30 +211,26 @@ public:
public:
void enable_s3_path_style(bool enable) override;
[[nodiscard]] auto make_request(const curl::requests::http_delete &del,
long &response_code,
stop_type &stop_requested) const
-> bool override;
[[nodiscard]] auto
make_request(const curl::requests::http_delete &del, long &response_code,
stop_type &stop_requested) const -> bool override;
[[nodiscard]] auto make_request(const curl::requests::http_get &get,
long &response_code,
stop_type &stop_requested) const
-> bool override;
[[nodiscard]] auto
make_request(const curl::requests::http_get &get, long &response_code,
stop_type &stop_requested) const -> bool override;
[[nodiscard]] auto make_request(const curl::requests::http_head &head,
long &response_code,
stop_type &stop_requested) const
-> bool override;
[[nodiscard]] auto
make_request(const curl::requests::http_head &head, long &response_code,
stop_type &stop_requested) const -> bool override;
[[nodiscard]] auto make_request(const curl::requests::http_post &post_file,
long &response_code,
stop_type &stop_requested) const
-> bool override;
[[nodiscard]] auto
make_request(const curl::requests::http_post &post_file, long &response_code,
stop_type &stop_requested) const -> bool override;
[[nodiscard]] auto make_request(const curl::requests::http_put_file &put_file,
long &response_code,
stop_type &stop_requested) const
-> bool override;
[[nodiscard]] auto
make_request(const curl::requests::http_put_file &put_file,
long &response_code,
stop_type &stop_requested) const -> bool override;
};
} // namespace repertory

View File

@ -41,8 +41,9 @@ inline const auto read_file_data = static_cast<read_callback>(
[](char *buffer, size_t size, size_t nitems, void *instream) -> size_t {
auto *read_info = reinterpret_cast<read_file_info *>(instream);
std::size_t bytes_read{};
auto ret = read_info->nf->read_bytes(buffer, size * nitems,
read_info->offset, bytes_read);
auto ret = read_info->nf->read_bytes(
reinterpret_cast<unsigned char *>(buffer), size * nitems,
read_info->offset, bytes_read);
if (ret) {
read_info->offset += bytes_read;
}
@ -73,9 +74,8 @@ struct http_request_base {
[[nodiscard]] virtual auto get_path() const -> std::string { return path; }
[[nodiscard]] virtual auto set_method(CURL *curl,
stop_type &stop_requested) const
-> bool = 0;
[[nodiscard]] virtual auto
set_method(CURL *curl, stop_type &stop_requested) const -> bool = 0;
};
} // namespace repertory::curl::requests

View File

@ -56,18 +56,18 @@ private:
std::size_t decode_offset_ = 0U;
public:
[[nodiscard]] static auto decode_json(packet &response, json &json_data)
-> int;
[[nodiscard]] static auto decode_json(packet &response,
json &json_data) -> int;
public:
void clear();
[[nodiscard]] auto current_pointer() -> char * {
[[nodiscard]] auto current_pointer() -> unsigned char * {
return (decode_offset_ < buffer_.size()) ? &buffer_[decode_offset_]
: nullptr;
}
[[nodiscard]] auto current_pointer() const -> const char * {
[[nodiscard]] auto current_pointer() const -> const unsigned char * {
return (decode_offset_ < buffer_.size()) ? &buffer_[decode_offset_]
: nullptr;
}
@ -110,19 +110,19 @@ public:
[[nodiscard]] auto decode(remote::file_info &val) -> error_type;
[[nodiscard]] auto decrypt(const std::string &token) -> error_type;
[[nodiscard]] auto decrypt(std::string_view token) -> error_type;
void encode(const void *buffer, std::size_t size, bool should_reserve = true);
void encode(const char *str) {
encode(std::string(str == nullptr ? "" : str));
encode(std::string{str == nullptr ? "" : str});
}
void encode(const std::string &str);
void encode(std::string_view str);
void encode(const wchar_t *str);
void encode(const std::wstring &str);
void encode(std::wstring_view str);
void encode(void *ptr) {
encode(static_cast<std::uint64_t>(reinterpret_cast<std::uintptr_t>(ptr)));
@ -161,9 +161,9 @@ public:
void encode_top(const void *buffer, std::size_t size,
bool should_reserve = true);
void encode_top(const std::string &str);
void encode_top(std::string_view str);
void encode_top(const std::wstring &str);
void encode_top(std::wstring_view str);
void encode_top(void *ptr) {
encode_top(
@ -200,7 +200,7 @@ public:
void encode_top(remote::file_info val);
void encrypt(const std::string &token);
void encrypt(std::string_view token);
[[nodiscard]] auto get_size() const -> std::uint32_t {
return static_cast<std::uint32_t>(buffer_.size());
@ -217,11 +217,12 @@ public:
auto operator=(packet &&pkt) noexcept -> packet &;
[[nodiscard]] auto operator[](std::size_t index) -> char & {
[[nodiscard]] auto operator[](std::size_t index) -> unsigned char & {
return buffer_[index];
}
[[nodiscard]] auto operator[](std::size_t index) const -> const char & {
[[nodiscard]] auto operator[](std::size_t index) const -> const
unsigned char & {
return buffer_.at(index);
}
};

View File

@ -1243,8 +1243,9 @@ public:
remote::file_handle handle{};
DECODE_OR_RETURN(request, handle);
ret = this->fuse_write(path.data(), buffer.data(), write_size,
write_offset, handle);
ret = this->fuse_write(
path.data(), reinterpret_cast<const char *>(buffer.data()),
write_size, write_offset, handle);
}
return ret;
}});
@ -1268,7 +1269,7 @@ public:
data_buffer buffer(write_size);
if ((ret = request->decode(buffer.data(), buffer.size())) == 0) {
buffer = macaron::Base64::Decode(
std::string(buffer.begin(), buffer.end()));
std::string{buffer.begin(), buffer.end()});
write_size = buffer.size();
remote::file_offset write_offset{};
@ -1277,8 +1278,9 @@ public:
remote::file_handle handle{};
DECODE_OR_RETURN(request, handle);
ret = this->fuse_write(path.data(), buffer.data(), write_size,
write_offset, handle);
ret = this->fuse_write(path.data(),
reinterpret_cast<char *>(buffer.data()),
write_size, write_offset, handle);
}
return ret;
}});

View File

@ -274,7 +274,7 @@ struct s3_config {
std::string bucket;
std::uint16_t cache_timeout_secs{60U};
std::string encryption_token;
std::string region = "any";
std::string region{"any"};
std::string secret_key;
std::uint32_t timeout_ms{60000U};
std::string url;
@ -282,7 +282,12 @@ struct s3_config {
bool use_region_in_url{false};
};
using data_buffer = std::vector<char>;
using data_buffer = std::vector<unsigned char>;
using key_type = std::array<unsigned char, 32U>;
using mutex_lock = std::lock_guard<std::mutex>;
using recur_mutex_lock = std::lock_guard<std::recursive_mutex>;
using unique_mutex_lock = std::unique_lock<std::mutex>;
using unique_recur_mutex_lock = std::unique_lock<std::recursive_mutex>;
using api_file_list = std::vector<api_file>;
using api_file_provider_callback = std::function<void(api_file &)>;
@ -292,11 +297,7 @@ using http_headers = std::unordered_map<std::string, std::string>;
using http_parameters = std::unordered_map<std::string, std::string>;
using http_ranges = std::vector<http_range>;
using meta_provider_callback = std::function<void(directory_item &)>;
using mutex_lock = std::lock_guard<std::mutex>;
using query_parameters = std::map<std::string, std::string>;
using recur_mutex_lock = std::lock_guard<std::recursive_mutex>;
using unique_mutex_lock = std::unique_lock<std::mutex>;
using unique_recur_mutex_lock = std::unique_lock<std::recursive_mutex>;
} // namespace repertory
#endif // INCLUDE_TYPES_REPERTORY_HPP_

View File

@ -38,43 +38,45 @@
#pragma GCC diagnostic ignored "-Wuseless-cast"
#endif
#include <array>
#include <string>
#include <vector>
namespace macaron::Base64 {
static std::string Encode(const char *data, const size_t &len) {
static constexpr char sEncodingTable[] = {
static std::string Encode(const unsigned char *data, std::size_t len) {
static constexpr std::array<unsigned char, 64U> sEncodingTable{
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'};
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/',
};
size_t in_len = len;
auto in_len{len};
std::string ret;
if (in_len > 0) {
size_t out_len = 4 * ((in_len + 2) / 3);
std::size_t out_len{4U * ((in_len + 2U) / 3U)};
ret = std::string(out_len, '\0');
size_t i;
char *p = const_cast<char *>(ret.c_str());
std::size_t i;
auto *p = reinterpret_cast<unsigned char *>(ret.data());
for (i = 0; i < in_len - 2; i += 3) {
*p++ = sEncodingTable[(data[i] >> 2) & 0x3F];
*p++ = sEncodingTable[((data[i] & 0x3) << 4) |
((int)(data[i + 1] & 0xF0) >> 4)];
for (i = 0U; i < in_len - 2U; i += 3U) {
*p++ = sEncodingTable[(data[i] >> 2U) & 0x3F];
*p++ = sEncodingTable[((data[i] & 0x3) << 4U) |
((int)(data[i + 1U] & 0xF0) >> 4U)];
*p++ = sEncodingTable[((data[i + 1] & 0xF) << 2) |
((int)(data[i + 2] & 0xC0) >> 6)];
*p++ = sEncodingTable[data[i + 2] & 0x3F];
((int)(data[i + 2U] & 0xC0) >> 6U)];
*p++ = sEncodingTable[data[i + 2U] & 0x3F];
}
if (i < in_len) {
*p++ = sEncodingTable[(data[i] >> 2) & 0x3F];
if (i == (in_len - 1)) {
*p++ = sEncodingTable[((data[i] & 0x3) << 4)];
*p++ = sEncodingTable[(data[i] >> 2U) & 0x3F];
if (i == (in_len - 1U)) {
*p++ = sEncodingTable[((data[i] & 0x3) << 4U)];
*p++ = '=';
} else {
*p++ = sEncodingTable[((data[i] & 0x3) << 4) |
((int)(data[i + 1] & 0xF0) >> 4)];
*p++ = sEncodingTable[((data[i + 1] & 0xF) << 2)];
*p++ = sEncodingTable[((data[i] & 0x3) << 4U) |
((int)(data[i + 1U] & 0xF0) >> 4U)];
*p++ = sEncodingTable[((data[i + 1U] & 0xF) << 2U)];
}
*p++ = '=';
}
@ -83,12 +85,14 @@ static std::string Encode(const char *data, const size_t &len) {
return ret;
}
[[maybe_unused]] static std::string Encode(const std::string &data) {
return Encode(&data[0], data.size());
[[maybe_unused]] static std::string Encode(std::string_view data) {
return Encode(reinterpret_cast<const unsigned char *>(data.data()),
data.size());
}
[[maybe_unused]] static std::vector<char> Decode(const std::string &input) {
static constexpr unsigned char kDecodingTable[] = {
[[maybe_unused]] static std::vector<unsigned char>
Decode(std::string_view input) {
static constexpr std::array<unsigned char, 256> kDecodingTable{
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63, 52, 53, 54, 55, 56, 57,
@ -103,45 +107,49 @@ static std::string Encode(const char *data, const size_t &len) {
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64};
64, 64, 64, 64,
};
std::vector<char> out;
std::vector<unsigned char> out;
if (not input.empty()) {
size_t in_len = input.size();
if (in_len % 4 != 0)
auto in_len{input.size()};
if (in_len % 4U != 0U) {
throw std::runtime_error("Input data size is not a multiple of 4");
}
size_t out_len = in_len / 4 * 3;
if (input[in_len - 1] == '=')
std::size_t out_len{in_len / 4U * 3U};
if (input[in_len - 1U] == '=') {
out_len--;
if (input[in_len - 2] == '=')
}
if (input[in_len - 2U] == '=') {
out_len--;
}
out.resize(out_len);
for (size_t i = 0, j = 0; i < in_len;) {
uint32_t a = input[i] == '='
? 0 & i++
: kDecodingTable[static_cast<int>(input[i++])];
uint32_t b = input[i] == '='
? 0 & i++
: kDecodingTable[static_cast<int>(input[i++])];
uint32_t c = input[i] == '='
? 0 & i++
: kDecodingTable[static_cast<int>(input[i++])];
uint32_t d = input[i] == '='
? 0 & i++
: kDecodingTable[static_cast<int>(input[i++])];
for (std::size_t i = 0U, j = 0U; i < in_len;) {
std::uint32_t a = input[i] == '='
? 0U & i++
: kDecodingTable[static_cast<int>(input[i++])];
std::uint32_t b = input[i] == '='
? 0U & i++
: kDecodingTable[static_cast<int>(input[i++])];
std::uint32_t c = input[i] == '='
? 0U & i++
: kDecodingTable[static_cast<int>(input[i++])];
std::uint32_t d = input[i] == '='
? 0U & i++
: kDecodingTable[static_cast<int>(input[i++])];
uint32_t triple =
(a << 3 * 6) + (b << 2 * 6) + (c << 1 * 6) + (d << 0 * 6);
std::uint32_t triple =
(a << 3U * 6U) + (b << 2U * 6U) + (c << 1U * 6U) + (d << 0U * 6U);
if (j < out_len)
out[j++] = (triple >> 2 * 8) & 0xFF;
out[j++] = (triple >> 2U * 8U) & 0xFF;
if (j < out_len)
out[j++] = (triple >> 1 * 8) & 0xFF;
out[j++] = (triple >> 1U * 8U) & 0xFF;
if (j < out_len)
out[j++] = (triple >> 0 * 8) & 0xFF;
out[j++] = (triple >> 0U * 8U) & 0xFF;
}
}

View File

@ -26,23 +26,20 @@
#include "utils/file_utils.hpp"
namespace repertory::utils::encryption {
using key_type = std::array<unsigned char, 32U>;
class encrypting_reader final {
public:
encrypting_reader(const std::string &file_name,
const std::string &source_path, stop_type &stop_requested,
const std::string &token,
std::optional<std::string> relative_parent_path,
encrypting_reader(std::string_view file_name, std::string_view source_path,
stop_type &stop_requested, std::string_view token,
std::optional<std::string_view> relative_parent_path,
std::size_t error_return = 0U);
encrypting_reader(const std::string &encrypted_file_path,
const std::string &source_path, stop_type &stop_requested,
const std::string &token, std::size_t error_return = 0U);
encrypting_reader(std::string_view encrypted_file_path,
std::string_view source_path, stop_type &stop_requested,
std::string_view token, std::size_t error_return = 0U);
encrypting_reader(
const std::string &encrypted_file_path, const std::string &source_path,
stop_type &stop_requested, const std::string &token,
std::string_view encrypted_file_path, std::string_view source_path,
stop_type &stop_requested, std::string_view token,
std::vector<std::array<unsigned char,
crypto_aead_xchacha20poly1305_IETF_NPUBBYTES>>
iv_list,
@ -61,9 +58,9 @@ public:
using streambuf = std::basic_streambuf<char, std::char_traits<char>>;
private:
const key_type key_;
key_type key_;
stop_type &stop_requested_;
const size_t error_return_;
size_t error_return_;
std::unordered_map<std::size_t, data_buffer> chunk_buffers_;
std::string encrypted_file_name_;
std::string encrypted_file_path_;
@ -85,16 +82,16 @@ private:
auto reader_function(char *buffer, size_t size, size_t nitems) -> size_t;
public:
[[nodiscard]] static auto calculate_decrypted_size(std::uint64_t total_size)
-> std::uint64_t;
[[nodiscard]] static auto
calculate_decrypted_size(std::uint64_t total_size) -> std::uint64_t;
[[nodiscard]] static auto
calculate_encrypted_size(const std::string &source_path) -> std::uint64_t;
calculate_encrypted_size(std::string_view source_path) -> std::uint64_t;
[[nodiscard]] auto create_iostream() const -> std::shared_ptr<iostream>;
[[nodiscard]] static constexpr auto get_encrypted_chunk_size()
-> std::size_t {
[[nodiscard]] static constexpr auto
get_encrypted_chunk_size() -> std::size_t {
return encrypted_chunk_size_;
}
@ -118,8 +115,9 @@ public:
return header_size_;
}
[[nodiscard]] auto get_iv_list() -> std::vector<
std::array<unsigned char, crypto_aead_xchacha20poly1305_IETF_NPUBBYTES>> {
[[nodiscard]] auto get_iv_list()
-> std::vector<std::array<unsigned char,
crypto_aead_xchacha20poly1305_IETF_NPUBBYTES>> {
return iv_list_;
}
@ -132,8 +130,8 @@ public:
}
[[nodiscard]] static auto reader_function(char *buffer, size_t size,
size_t nitems, void *instream)
-> size_t {
size_t nitems,
void *instream) -> size_t {
return reinterpret_cast<encrypting_reader *>(instream)->reader_function(
buffer, size, nitems);
}

View File

@ -30,25 +30,24 @@ using reader_func = std::function<api_error(data_buffer &cypher_text,
std::uint64_t start_offset,
std::uint64_t end_offset)>;
// Prototypes
[[nodiscard]] auto decrypt_file_path(const std::string &encryption_token,
[[nodiscard]] auto decrypt_file_path(std::string_view encryption_token,
std::string &file_path) -> api_error;
[[nodiscard]] auto decrypt_file_name(const std::string &encryption_token,
[[nodiscard]] auto decrypt_file_name(std::string_view encryption_token,
std::string &file_name) -> api_error;
[[nodiscard]] auto generate_key(const std::string &encryption_token)
-> key_type;
[[nodiscard]] auto read_encrypted_range(const http_range &range,
const key_type &key, reader_func reader,
std::uint64_t total_size,
data_buffer &data) -> api_error;
[[nodiscard]] auto generate_key(std::string_view encryption_token) -> key_type;
// Implementations
template <typename result>
[[nodiscard]] inline auto decrypt_data(const key_type &key, const char *buffer,
std::size_t buffer_size, result &res)
-> bool {
[[nodiscard]] inline auto
decrypt_data(const key_type &key, const unsigned char *buffer,
std::size_t buffer_size, result &res) -> bool {
const auto header_size =
static_cast<std::uint32_t>(encrypting_reader::get_header_size());
if (buffer_size > header_size) {
@ -56,14 +55,11 @@ template <typename result>
boost::endian::native_to_big(static_cast<std::uint32_t>(buffer_size));
res.resize(buffer_size - header_size);
return crypto_aead_xchacha20poly1305_ietf_decrypt_detached(
reinterpret_cast<unsigned char *>(&res[0u]), nullptr,
reinterpret_cast<const unsigned char *>(&buffer[header_size]),
res.size(),
reinterpret_cast<const unsigned char *>(
&buffer[crypto_aead_xchacha20poly1305_IETF_NPUBBYTES]),
reinterpret_cast<unsigned char *>(res.data()), nullptr,
&buffer[header_size], res.size(),
&buffer[crypto_aead_xchacha20poly1305_IETF_NPUBBYTES],
reinterpret_cast<const unsigned char *>(&size), sizeof(size),
reinterpret_cast<const unsigned char *>(buffer),
key.data()) == 0;
buffer, key.data()) == 0;
}
return false;
@ -72,20 +68,19 @@ template <typename result>
template <typename buffer, typename result>
[[nodiscard]] inline auto decrypt_data(const key_type &key, const buffer &buf,
result &res) -> bool {
return decrypt_data<result>(key, &buf[0u], buf.size(), res);
return decrypt_data<result>(key, buf.data(), buf.size(), res);
}
template <typename buffer, typename result>
[[nodiscard]] inline auto decrypt_data(const std::string &encryption_token,
[[nodiscard]] inline auto decrypt_data(std::string_view encryption_token,
const buffer &buf, result &res) -> bool {
return decrypt_data<buffer, result>(generate_key(encryption_token), buf, res);
}
template <typename result>
[[nodiscard]] inline auto decrypt_data(const std::string &encryption_token,
const char *buffer,
std::size_t buffer_size, result &res)
-> bool {
[[nodiscard]] inline auto
decrypt_data(std::string_view encryption_token, const unsigned char *buffer,
std::size_t buffer_size, result &res) -> bool {
return decrypt_data<result>(generate_key(encryption_token), buffer,
buffer_size, res);
}
@ -94,8 +89,8 @@ template <typename result>
inline void
encrypt_data(const std::array<unsigned char,
crypto_aead_xchacha20poly1305_IETF_NPUBBYTES> &iv,
const key_type &key, const char *buffer, std::size_t buffer_size,
result &res) {
const key_type &key, const unsigned char *buffer,
std::size_t buffer_size, result &res) {
std::array<unsigned char, crypto_aead_xchacha20poly1305_IETF_ABYTES> mac{};
const auto header_size =
@ -109,18 +104,18 @@ encrypt_data(const std::array<unsigned char,
unsigned long long mac_length{};
if (crypto_aead_xchacha20poly1305_ietf_encrypt_detached(
reinterpret_cast<unsigned char *>(&res[header_size]), mac.data(),
&mac_length, reinterpret_cast<const unsigned char *>(buffer),
buffer_size, reinterpret_cast<const unsigned char *>(&size),
sizeof(size), nullptr, iv.data(), key.data()) != 0) {
&mac_length, buffer, buffer_size,
reinterpret_cast<const unsigned char *>(&size), sizeof(size), nullptr,
iv.data(), key.data()) != 0) {
throw std::runtime_error("encryption failed");
}
std::memcpy(&res[0u], &iv[0u], iv.size());
std::memcpy(&res[iv.size()], &mac[0u], mac.size());
std::memcpy(res.data(), iv.data(), iv.size());
std::memcpy(&res[iv.size()], mac.data(), mac.size());
}
template <typename result>
inline void encrypt_data(const key_type &key, const char *buffer,
inline void encrypt_data(const key_type &key, const unsigned char *buffer,
std::size_t buffer_size, result &res) {
std::array<unsigned char, crypto_aead_xchacha20poly1305_IETF_NPUBBYTES> iv{};
randombytes_buf(iv.data(), iv.size());
@ -129,23 +124,23 @@ inline void encrypt_data(const key_type &key, const char *buffer,
}
template <typename result>
inline void encrypt_data(const std::string &encryption_token,
const char *buffer, std::size_t buffer_size,
inline void encrypt_data(std::string_view encryption_token,
const unsigned char *buffer, std::size_t buffer_size,
result &res) {
encrypt_data<result>(generate_key(encryption_token), buffer, buffer_size,
res);
}
template <typename buffer, typename result>
inline void encrypt_data(const std::string &encryption_token, const buffer &buf,
inline void encrypt_data(std::string_view encryption_token, const buffer &buf,
result &res) {
encrypt_data<result>(generate_key(encryption_token), &buf[0u], buf.size(),
encrypt_data<result>(generate_key(encryption_token), buf.data(), buf.size(),
res);
}
template <typename buffer, typename result>
inline void encrypt_data(const key_type &key, const buffer &buf, result &res) {
encrypt_data<result>(key, &buf[0u], buf.size(), res);
encrypt_data<result>(key, buf.data(), buf.size(), res);
}
template <typename buffer, typename result>
@ -153,7 +148,7 @@ inline void
encrypt_data(const std::array<unsigned char,
crypto_aead_xchacha20poly1305_IETF_NPUBBYTES> &iv,
const key_type &key, const buffer &buf, result &res) {
encrypt_data<result>(iv, key, &buf[0u], buf.size(), res);
encrypt_data<result>(iv, key, buf.data(), buf.size(), res);
}
} // namespace repertory::utils::encryption

View File

@ -27,21 +27,21 @@
namespace repertory::utils::file {
// Prototypes
[[nodiscard]] auto calculate_used_space(std::string path, bool recursive)
-> std::uint64_t;
[[nodiscard]] auto calculate_used_space(std::string path,
bool recursive) -> std::uint64_t;
void change_to_process_directory();
[[nodiscard]] auto copy_directory_recursively(std::string from_path,
std::string to_path) -> bool;
[[nodiscard]] auto copy_file(std::string from_path, std::string to_path)
-> bool;
[[nodiscard]] auto copy_file(std::string from_path,
std::string to_path) -> bool;
[[nodiscard]] auto create_full_directory_path(std::string path) -> bool;
[[nodiscard]] auto delete_directory(std::string path, bool recursive = false)
-> bool;
[[nodiscard]] auto delete_directory(std::string path,
bool recursive = false) -> bool;
[[nodiscard]] auto delete_directory_recursively(std::string path) -> bool;
@ -52,18 +52,18 @@ void change_to_process_directory();
[[nodiscard]] auto get_accessed_time(const std::string &path,
std::uint64_t &accessed) -> bool;
[[nodiscard]] auto get_directory_files(std::string path, bool oldest_first,
bool recursive = false)
-> std::deque<std::string>;
[[nodiscard]] auto
get_directory_files(std::string path, bool oldest_first,
bool recursive = false) -> std::deque<std::string>;
[[nodiscard]] auto get_free_drive_space(const std::string &path)
-> std::uint64_t;
[[nodiscard]] auto
get_free_drive_space(const std::string &path) -> std::uint64_t;
[[nodiscard]] auto get_total_drive_space(const std::string &path)
-> std::uint64_t;
[[nodiscard]] auto
get_total_drive_space(const std::string &path) -> std::uint64_t;
[[nodiscard]] auto get_file_size(std::string path, std::uint64_t &file_size)
-> bool;
[[nodiscard]] auto get_file_size(std::string_view path,
std::uint64_t &file_size) -> bool;
[[nodiscard]] auto get_modified_time(const std::string &path,
std::uint64_t &modified) -> bool;
@ -72,14 +72,14 @@ void change_to_process_directory();
[[nodiscard]] auto is_file(const std::string &path) -> bool;
[[nodiscard]] auto is_modified_date_older_than(const std::string &path,
const std::chrono::hours &hours)
-> bool;
[[nodiscard]] auto
is_modified_date_older_than(const std::string &path,
const std::chrono::hours &hours) -> bool;
[[nodiscard]] auto move_file(std::string from, std::string to) -> bool;
[[nodiscard]] auto read_file_lines(const std::string &path)
-> std::vector<std::string>;
[[nodiscard]] auto
read_file_lines(const std::string &path) -> std::vector<std::string>;
[[nodiscard]] auto read_json_file(const std::string &path, json &data) -> bool;
@ -89,8 +89,8 @@ void change_to_process_directory();
[[nodiscard]] auto retry_delete_file(const std::string &file) -> bool;
[[nodiscard]] auto write_json_file(const std::string &path, const json &j)
-> bool;
[[nodiscard]] auto write_json_file(const std::string &path,
const json &j) -> bool;
} // namespace repertory::utils::file
#endif // INCLUDE_UTILS_FILE_UTILS_HPP_

View File

@ -42,17 +42,17 @@ public:
[[nodiscard]] static auto
clone(const native_file_ptr &ptr) -> native_file_ptr;
[[nodiscard]] static auto create_or_open(const std::string &source_path,
[[nodiscard]] static auto create_or_open(std::string_view source_path,
bool read_only,
native_file_ptr &ptr) -> api_error;
[[nodiscard]] static auto create_or_open(const std::string &source_path,
[[nodiscard]] static auto create_or_open(std::string_view source_path,
native_file_ptr &ptr) -> api_error;
[[nodiscard]] static auto open(const std::string &source_path,
[[nodiscard]] static auto open(std::string_view source_path,
native_file_ptr &ptr) -> api_error;
[[nodiscard]] static auto open(const std::string &source_path, bool read_only,
[[nodiscard]] static auto open(std::string_view source_path, bool read_only,
native_file_ptr &ptr) -> api_error;
private:
@ -86,11 +86,11 @@ public:
[[nodiscard]] auto get_handle() -> native_handle;
#if defined(_WIN32)
[[nodiscard]] auto read_bytes(char *buffer, std::size_t read_size,
[[nodiscard]] auto read_bytes(unsigned char *buffer, std::size_t read_size,
std::uint64_t read_offset,
std::size_t &bytes_read) -> bool;
#else
[[nodiscard]] auto read_bytes(char *buffer, std::size_t read_size,
[[nodiscard]] auto read_bytes(unsigned char *buffer, std::size_t read_size,
std::uint64_t read_offset,
std::size_t &bytes_read) -> bool;
#endif
@ -99,11 +99,13 @@ public:
[[nodiscard]] auto truncate(std::uint64_t file_size) -> bool;
#if defined(_WIN32)
[[nodiscard]] auto write_bytes(const char *buffer, std::size_t write_size,
[[nodiscard]] auto write_bytes(const unsigned char *buffer,
std::size_t write_size,
std::uint64_t write_offset,
std::size_t &bytes_written) -> bool;
#else
[[nodiscard]] auto write_bytes(const char *buffer, std::size_t write_size,
[[nodiscard]] auto write_bytes(const unsigned char *buffer,
std::size_t write_size,
std::uint64_t write_offset,
std::size_t &bytes_written) -> bool;
#endif

View File

@ -32,29 +32,28 @@ constexpr auto contains(std::string_view str, std::string_view search) -> bool {
return (str.find(search) != std::string_view::npos);
}
[[nodiscard]] /* constexpr c++20 */ auto ends_with(std::string_view str,
std::string_view val)
-> bool;
[[nodiscard]] /* constexpr c++20 */ auto
ends_with(std::string_view str, std::string_view val) -> bool;
[[nodiscard]] auto from_bool(bool val) -> std::string;
[[nodiscard]] auto from_dynamic_bitset(const boost::dynamic_bitset<> &bitset)
-> std::string;
[[nodiscard]] auto
from_dynamic_bitset(const boost::dynamic_bitset<> &bitset) -> std::string;
[[nodiscard]] auto from_utf8(const std::string &str) -> std::wstring;
[[nodiscard]] auto from_utf8(std::string_view str) -> std::wstring;
[[nodiscard]] /* constexpr c++20 */ auto is_numeric(std::string_view str)
-> bool;
[[nodiscard]] /* constexpr c++20 */ auto
is_numeric(std::string_view str) -> bool;
[[nodiscard]] auto join(const std::vector<std::string> &arr, const char &delim)
-> std::string;
[[nodiscard]] auto join(const std::vector<std::string> &arr,
const char &delim) -> std::string;
auto left_trim(std::string &str) -> std::string &;
auto left_trim(std::string &str, const char &trim_ch) -> std::string &;
auto replace(std::string &src, const char &character, const char &with)
-> std::string &;
auto replace(std::string &src, const char &character,
const char &with) -> std::string &;
auto replace(std::string &src, const std::string &find, const std::string &with,
size_t start_pos = 0) -> std::string &;
@ -63,8 +62,8 @@ auto replace(std::string &src, const std::string &find, const std::string &with,
const char &with) -> std::string;
[[nodiscard]] auto replace_copy(std::string src, const std::string &find,
const std::string &with, size_t start_pos = 0)
-> std::string;
const std::string &with,
size_t start_pos = 0) -> std::string;
auto right_trim(std::string &str) -> std::string &;
@ -77,8 +76,8 @@ auto right_trim(std::string &str, const char &trim_ch) -> std::string &;
[[nodiscard]] auto to_double(const std::string &str) -> double;
[[nodiscard]] auto to_dynamic_bitset(const std::string &val)
-> boost::dynamic_bitset<>;
[[nodiscard]] auto
to_dynamic_bitset(const std::string &val) -> boost::dynamic_bitset<>;
[[nodiscard]] auto to_lower(std::string str) -> std::string;
@ -98,9 +97,9 @@ auto right_trim(std::string &str, const char &trim_ch) -> std::string &;
[[nodiscard]] auto to_upper(std::string str) -> std::string;
[[nodiscard]] auto to_utf8(std::string str) -> std::string;
[[nodiscard]] auto to_utf8(std::string_view str) -> std::string;
[[nodiscard]] auto to_utf8(const std::wstring &str) -> std::string;
[[nodiscard]] auto to_utf8(std::wstring_view str) -> std::string;
auto trim(std::string &str) -> std::string &;
@ -108,8 +107,8 @@ auto trim(std::string &str, const char &trim_ch) -> std::string &;
[[nodiscard]] auto trim_copy(std::string str) -> std::string;
[[nodiscard]] auto trim_copy(std::string str, const char &trim_ch)
-> std::string;
[[nodiscard]] auto trim_copy(std::string str,
const char &trim_ch) -> std::string;
} // namespace repertory::utils::string
#endif // INCLUDE_UTILS_STRING_UTILS_HPP_