diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2016-10-17 10:42:02 +0300 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2016-10-17 10:42:02 +0300 |
commit | b42c52475d6e86f6ae0ed01f502bd0b611974578 (patch) | |
tree | 30f4777c39f7d07aed6a85209a03bc8af44a415d | |
parent | fix compiler warnings (diff) | |
download | aes-tools-b42c52475d6e86f6ae0ed01f502bd0b611974578.tar.gz aes-tools-b42c52475d6e86f6ae0ed01f502bd0b611974578.zip |
utils: code dedupe
Diffstat (limited to '')
-rw-r--r-- | utils/CMakeLists.txt | 4 | ||||
-rw-r--r-- | utils/decrypt_bmp.cpp | 25 | ||||
-rw-r--r-- | utils/encrypt_bmp.cpp | 27 | ||||
-rw-r--r-- | utils/helpers/bmp.hpp | 51 | ||||
-rw-r--r-- | utils/helpers/file.hpp | 18 |
5 files changed, 82 insertions, 43 deletions
diff --git a/utils/CMakeLists.txt b/utils/CMakeLists.txt index 5db82b3..e13d7ce 100644 --- a/utils/CMakeLists.txt +++ b/utils/CMakeLists.txt @@ -1,3 +1,7 @@ +if(MSVC) + add_definitions(/DNOMINMAX) +endif() + find_package(Boost REQUIRED COMPONENTS filesystem program_options system) add_executable(util_encrypt_block encrypt_block.cpp block_cmd_parser.hpp block_dumper.hpp block_input.hpp data_parsers.hpp) diff --git a/utils/decrypt_bmp.cpp b/utils/decrypt_bmp.cpp index 4842fbd..1d287d2 100644 --- a/utils/decrypt_bmp.cpp +++ b/utils/decrypt_bmp.cpp @@ -4,6 +4,7 @@ // Distributed under the MIT License. #include "file_cmd_parser.hpp" +#include "helpers/bmp.hpp" #include "helpers/file.hpp" #include <aesxx/all.hpp> @@ -12,12 +13,9 @@ #include <boost/program_options.hpp> -#include <cstring> - #include <exception> #include <iostream> #include <string> -#include <vector> namespace { @@ -26,22 +24,11 @@ namespace const std::string& ciphertext_path, const std::string& plaintext_path) { - const auto ciphertext_buf = file::read_file(ciphertext_path); - - const auto bmp_header = reinterpret_cast<const BITMAPFILEHEADER*>(ciphertext_buf.data()); - - const auto header_size = bmp_header->bfOffBits; - const auto cipherpixels = ciphertext_buf.data() + header_size; - const auto cipherpixels_size = ciphertext_buf.size() - header_size; - - const auto pixels = box.decrypt_buffer( - cipherpixels, cipherpixels_size); - - std::vector<unsigned char> plaintext_buf(header_size + pixels.size()); - std::memcpy(plaintext_buf.data(), bmp_header, header_size); - std::memcpy(plaintext_buf.data() + header_size, pixels.data(), pixels.size()); - - file::write_file(plaintext_path, plaintext_buf); + bmp::BmpFile bmp{file::read_file(ciphertext_path)}; + bmp.replace_pixels(box.decrypt_buffer( + bmp.get_pixels(), + bmp.get_pixels_size())); + file::write_file(plaintext_path, bmp.get_buffer(), bmp.get_size()); } void decrypt_bmp(const Settings& settings) diff --git a/utils/encrypt_bmp.cpp b/utils/encrypt_bmp.cpp index c0fd688..c28dbf2 100644 --- a/utils/encrypt_bmp.cpp +++ b/utils/encrypt_bmp.cpp @@ -4,20 +4,16 @@ // Distributed under the MIT License. #include "file_cmd_parser.hpp" +#include "helpers/bmp.hpp" #include "helpers/file.hpp" #include <aesxx/all.hpp> -#include <Windows.h> - #include <boost/program_options.hpp> -#include <cstring> - #include <exception> #include <iostream> #include <string> -#include <vector> namespace { @@ -26,22 +22,11 @@ namespace const std::string& plaintext_path, const std::string& ciphertext_path) { - const auto plaintext_buf = file::read_file(plaintext_path); - - const auto bmp_header = reinterpret_cast<const BITMAPFILEHEADER*>(plaintext_buf.data()); - - const auto header_size = bmp_header->bfOffBits; - const auto pixels = plaintext_buf.data() + header_size; - const auto pixels_size = plaintext_buf.size() - header_size; - - const auto cipherpixels = box.encrypt_buffer( - pixels, pixels_size); - - std::vector<unsigned char> ciphertext_buf(header_size + cipherpixels.size()); - std::memcpy(ciphertext_buf.data(), bmp_header, header_size); - std::memcpy(ciphertext_buf.data() + header_size, cipherpixels.data(), cipherpixels.size()); - - file::write_file(ciphertext_path, ciphertext_buf); + bmp::BmpFile bmp{file::read_file(plaintext_path)}; + bmp.replace_pixels(box.encrypt_buffer( + bmp.get_pixels(), + bmp.get_pixels_size())); + file::write_file(ciphertext_path, bmp.get_buffer(), bmp.get_size()); } void encrypt_bmp(const Settings& settings) diff --git a/utils/helpers/bmp.hpp b/utils/helpers/bmp.hpp new file mode 100644 index 0000000..dc3cdac --- /dev/null +++ b/utils/helpers/bmp.hpp @@ -0,0 +1,51 @@ +// Copyright (c) 2016 Egor Tensin <Egor.Tensin@gmail.com> +// This file is part of the "AES tools" project. +// For details, see https://github.com/egor-tensin/aes-tools. +// Distributed under the MIT License. + +#include <Windows.h> + +#include <cstddef> +#include <cstring> + +#include <string> +#include <utility> +#include <vector> + +namespace bmp +{ + class BmpFile + { + public: + BmpFile(std::vector<char>&& buffer) + : buffer{std::move(buffer)} + , header_size{extract_pixels_offset()} + { } + + const void* get_buffer() const { return buffer.data(); } + + std::size_t get_size() const { return buffer.size(); } + + std::size_t get_header_size() const { return header_size; } + + const void* get_pixels() const { return buffer.data() + get_header_size(); } + + std::size_t get_pixels_size() const { return get_size() - get_header_size(); } + + void replace_pixels(std::vector<unsigned char>&& pixels) + { + buffer.resize(get_header_size() + pixels.size()); + std::memcpy(buffer.data() + get_header_size(), pixels.data(), pixels.size()); + } + + private: + std::size_t extract_pixels_offset() const + { + const auto header = reinterpret_cast<const BITMAPFILEHEADER*>(get_buffer()); + return header->bfOffBits; + } + + std::vector<char> buffer; + std::size_t header_size; + }; +} diff --git a/utils/helpers/file.hpp b/utils/helpers/file.hpp index a327c35..febe98f 100644 --- a/utils/helpers/file.hpp +++ b/utils/helpers/file.hpp @@ -3,10 +3,12 @@ // For details, see https://github.com/egor-tensin/aes-tools. // Distributed under the MIT License. +#include <cassert> #include <cstddef> #include <fstream> #include <iterator> +#include <limits> #include <string> #include <vector> @@ -17,7 +19,9 @@ namespace file std::ifstream ifs; ifs.exceptions(std::ifstream::badbit | std::ifstream::failbit); ifs.open(path, std::ifstream::binary | std::ifstream::ate); - return ifs.tellg(); + const auto size = static_cast<std::streamoff>(ifs.tellg()); + assert(size <= static_cast<std::streamoff>(std::numeric_limits<std::size_t>::max())); + return static_cast<std::size_t>(size); } inline std::vector<char> read_file(const std::string& path) @@ -38,11 +42,19 @@ namespace file inline void write_file( const std::string& path, - const std::vector<unsigned char>& src) + const void* buffer, + const std::size_t size) { std::ofstream ofs; ofs.exceptions(std::ofstream::badbit | std::ofstream::failbit); ofs.open(path, std::ofstream::binary); - ofs.write(reinterpret_cast<const char*>(src.data()), src.size()); + ofs.write(reinterpret_cast<const char*>(buffer), size); + } + + inline void write_file( + const std::string& path, + const std::vector<unsigned char>& src) + { + write_file(path, src.data(), src.size()); } } |