From 50687d658b7d5b8e85f8392110344b501cfe5399 Mon Sep 17 00:00:00 2001 From: Michele Rodolfi Date: Mon, 14 Dec 2020 17:09:54 +0100 Subject: [PATCH] Code formatting --- src/crypto.cpp | 3 ++- src/crypto.h | 30 ++++++++++++++++++------------ src/horcrux.cpp | 6 ++++-- src/horcrux.h | 5 +++++ src/io.cpp | 35 +++-------------------------------- src/io.h | 46 ++++++++++++++++++++++++++++++++++++---------- 6 files changed, 68 insertions(+), 57 deletions(-) diff --git a/src/crypto.cpp b/src/crypto.cpp index 6b3a442..58fd701 100644 --- a/src/crypto.cpp +++ b/src/crypto.cpp @@ -67,7 +67,8 @@ size_t AES256_CBC::process_start(Mode mode, current += kIvSize; } init(mode, encryption_key, iv); - return output_offset + process_chunk(mode, current, end, output, output_offset, resize_in, resize_out); + return output_offset + process_chunk(mode, current, end, output, + output_offset, resize_in, resize_out); } size_t AES256_CBC::process_chunk(Mode mode, diff --git a/src/crypto.h b/src/crypto.h index 5b6d147..b623785 100644 --- a/src/crypto.h +++ b/src/crypto.h @@ -64,8 +64,9 @@ public: * @param flags flag bitset. Default (kBegin | kEnd) * @return processed data */ - virtual raw_data process( - const raw_data& inputdata, const std::bitset& flags = Flags(kBegin | kEnd)) = 0; + virtual raw_data process(const raw_data& inputdata, + const std::bitset& flags = + Flags(kBegin | kEnd)) = 0; /** @brief encrypt the content of a buffer using Cipher key * @details @@ -75,8 +76,9 @@ public: * @param flags flag bitset. Default (kBegin | kEnd) * @return encrypted data */ - virtual raw_data encrypt( - const raw_data& plaintext, const std::bitset& flags = Flags(kBegin | kEnd)) = 0; + virtual raw_data encrypt(const raw_data& plaintext, + const std::bitset& flags = + Flags(kBegin | kEnd)) = 0; /** @brief decrypt the content of a buffer using Cipher key * @details @@ -86,8 +88,9 @@ public: * @param flags flag bitset. Default (kBegin | kEnd) * @return decrypted data */ - virtual raw_data decrypt( - const raw_data& ciphertext, const std::bitset& flags = Flags(kBegin | kEnd)) = 0; + virtual raw_data decrypt(const raw_data& ciphertext, + const std::bitset& flags = + Flags(kBegin | kEnd)) = 0; }; /** Cipher context class @@ -155,8 +158,9 @@ public: * @param flags flag bitset. Default (kBegin | kEnd) * @return processed data */ - raw_data process( - const raw_data& inputdata, const std::bitset& flags = Flags(kBegin | kEnd)) override; + raw_data process(const raw_data& inputdata, + const std::bitset& flags = + Flags(kBegin | kEnd)) override; /** @brief encrypt the content of a buffer using Cipher key * @details @@ -166,8 +170,9 @@ public: * @param flags flag bitset. Default (kBegin | kEnd) * @return encrypted data */ - raw_data encrypt( - const raw_data& plaintext, const std::bitset& flags = Flags(kBegin | kEnd)) override; + raw_data encrypt(const raw_data& plaintext, + const std::bitset& flags = + Flags(kBegin | kEnd)) override; /** @brief decrypt the content of a buffer using Cipher key * @details @@ -177,8 +182,9 @@ public: * @param flags flag bitset. Default (kBegin | kEnd) * @return decrypted data */ - raw_data decrypt( - const raw_data& ciphertext, const std::bitset& flags = Flags(kBegin | kEnd)) override; + raw_data decrypt(const raw_data& ciphertext, + const std::bitset& flags = + Flags(kBegin | kEnd)) override; private: /** @brief Initialize the Cipher context object needed for enc/dec operations diff --git a/src/horcrux.cpp b/src/horcrux.cpp index 905924c..fefcdc4 100644 --- a/src/horcrux.cpp +++ b/src/horcrux.cpp @@ -8,7 +8,8 @@ namespace horcrux { void Horcrux::process_chunks(size_t size) { raw_data buf = input->read(size); //std::cout << "read " << buf.size() << " " << size << std::endl; - output->write_chunk(cipher->process(buf, Cipher::Flags(Cipher::flag::kBegin))); + output->write_chunk(cipher->process(buf, + Cipher::Flags(Cipher::flag::kBegin))); while ((buf = input->read(size)).size() == size) { //std::cout << "read " << buf.size() << std::endl; output->write_chunk(cipher->process(buf, Cipher::Flags())); @@ -26,7 +27,8 @@ void Horcrux::init() { input = std::make_unique(options.input[0]); output = std::make_unique(options.output, options.count, std::filesystem::path(options.input[0]).filename().string(), - static_cast(input.get())->get_file_size() / options.count); + static_cast(input.get())->get_file_size() / + options.count); options.base64_key = to_base64(cipher->get_encryption_key()); break; case Mode::kDecrypt: diff --git a/src/horcrux.h b/src/horcrux.h index 0316fc5..1cbd551 100644 --- a/src/horcrux.h +++ b/src/horcrux.h @@ -38,6 +38,11 @@ class Horcrux { * Used cipher, input and output are generic and the actual implementation * can be selected in this function */ void init(); + + /** @brief read from Input, process with Cipher and write to Output + * one chunk at a time + * @ size chunk buffer size. + */ void process_chunks(size_t size); public: diff --git a/src/io.cpp b/src/io.cpp index c707039..f874cd0 100644 --- a/src/io.cpp +++ b/src/io.cpp @@ -18,10 +18,6 @@ FsPlainInput::FsPlainInput(const std::string& path) raw_data FsPlainInput::read() { return read(fs::file_size(file_path)); -// raw_data result(file_size); -// file_stream.open(file_path, std::ios::binary); -// file_stream.read(reinterpret_cast(result.data()), file_size); -// return result; } raw_data FsPlainInput::read(size_t size) { raw_data result(size); @@ -29,7 +25,6 @@ raw_data FsPlainInput::read(size_t size) { file_stream.open(file_path, std::ios::binary); file_stream.read(reinterpret_cast(result.data()), size); result.resize(file_stream.gcount()); - //std::cout << size << " read " << file_stream.gcount() << std::endl; return result; } @@ -55,15 +50,8 @@ raw_data FsCryptoInput::read() { current_file = 0; file_stream.close(); return read(total_size); -// raw_data result(total_size); -// size_t data_read{0}; -// for (auto& f : file_paths) { -// std::ifstream ifstream(f.first, std::ios::binary); -// ifstream.read(reinterpret_cast(result.data()) + data_read, f.second); -// data_read += f.second; -// } -// return result; } + raw_data FsCryptoInput::read(size_t size) { raw_data result(size); size_t data_read{0}; @@ -80,7 +68,6 @@ raw_data FsCryptoInput::read(size_t size) { } } result.resize(data_read); - //std::cout << "data_read: " << data_read << std::endl; return result; } @@ -91,12 +78,11 @@ FsPlainOutput::FsPlainOutput(const std::string& filename) throw std::invalid_argument("Output file is not a regular file"); } } + size_t FsPlainOutput::write(const raw_data& to_write) { - //file_stream.open(file_path, std::ios::binary); - //file_stream.write(reinterpret_cast(to_write.data()), to_write.size()); - //return to_write.size(); return write_chunk(to_write); } + size_t FsPlainOutput::write_chunk(const raw_data& to_write) { if(!file_stream.is_open()) file_stream.open(file_path, std::ios::binary); @@ -122,20 +108,6 @@ size_t FsCryptoOutput::write(const raw_data& to_write) { size = to_write.size() / num; created_files.clear(); return write_chunk(to_write); -// std::ofstream f; -// created_files.clear(); -// size_t data_written{0}; -// for (int i = 0; i < num; ++i) { -// std::string name = base_name + '.' + std::to_string(i) + ".enc"; -// created_files.emplace_back(folder_path / name); -// f = std::ofstream(created_files.back(), -// std::ios::binary | std::ios::trunc); -// auto chunk_size = i + 1 != num ? size : size + to_write.size() % size; -// f.write(reinterpret_cast(to_write.data()) + data_written, -// chunk_size); -// data_written += chunk_size; -// } -// return data_written; } size_t FsCryptoOutput::write_chunk(const raw_data& to_write) { @@ -143,7 +115,6 @@ size_t FsCryptoOutput::write_chunk(const raw_data& to_write) { if (size <= 0) { throw std::invalid_argument("Invalid horcrux size"); } - //std::cout << "writing " << to_write.size() << std::endl; size_t data_written{0}; while (data_written < to_write.size()){ if(!file_stream.is_open()){ diff --git a/src/io.h b/src/io.h index 5c7b544..8f151d6 100644 --- a/src/io.h +++ b/src/io.h @@ -14,11 +14,14 @@ namespace horcrux { class Input { public: virtual ~Input() = default; - /** @brief Read from the input + /** @brief Read and consume the whole input * @return new buffer containing all read data */ virtual raw_data read() = 0; - virtual raw_data read(size_t size) = 0; + /** @brief Read from the input + * @param size the amount of data to extract from the input + * @return a new buffer containing at most size bytes */ + virtual raw_data read(size_t size) = 0; }; @@ -27,10 +30,15 @@ public: class Output { public: virtual ~Output() = default; - /** @brief Write to the output - * @param data to write + + /** @brief Write everything to the output + * @param to_write the one and only data buffer to write * @return written data */ virtual size_t write(const raw_data& data) = 0; + + /** @brief Write single chunks to the output + * @param to_write the data chunk to write + * @return written data */ virtual size_t write_chunk(const raw_data& data) = 0; }; @@ -52,11 +60,17 @@ public: * @param path input file path, must be a regular file */ explicit FsPlainInput(const std::string& path); + /** @brief get the size of the underlying input file + * @return the file size */ const size_t& get_file_size() { return file_size; }; - /** @brief Read from the input + /** @brief Read and consume the whole input * @return new buffer containing all read data */ raw_data read() override; + + /** @brief Read from the input + * @param size the amount of data to extract from the input + * @return a new buffer containing at most size bytes */ raw_data read(size_t size) override; }; @@ -80,10 +94,14 @@ public: * @param filenames paths of input files. They will be read in this order */ explicit FsCryptoInput(const std::vector& filenames); - /** @brief Read from the input + /** @brief Read and consume the whole input * @details basically concatenate the input files. * @return new buffer containing all read data */ raw_data read() override; + + /** @brief Read from the input + * @param size the amount of data to extract from the input + * @return a new buffer containing at most size bytes */ raw_data read(size_t size) override; }; @@ -103,10 +121,14 @@ public: * @param filename output file. It can be a new file, it must be writable */ explicit FsPlainOutput(const std::string& filename); - /** @brief Write to the output - * @param data to write + /** @brief Write everything to the output + * @param to_write the one and only data buffer to write * @return written data */ size_t write(const raw_data& to_write) override; + + /** @brief Write single chunks to the output + * @param to_write the data chunk to write + * @return written data */ size_t write_chunk(const raw_data& to_write) override; }; @@ -143,10 +165,14 @@ public: const std::string& filename = "horcrux", const size_t horcrux_size = 0); - /** @brief Write to the output - * @param data to write + /** @brief Write everything to the output + * @param to_write the one and only data buffer to write * @return written data */ size_t write(const raw_data& to_write) override; + + /** @brief Write single chunks to the output + * @param to_write the data chunk to write + * @return written data */ size_t write_chunk(const raw_data& to_write) override; }; }; // namespace horcrux