diff options
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/CMakeLists.txt | 5 | ||||
-rw-r--r-- | src/core/GpgFrontendCore.h | 46 | ||||
-rw-r--r-- | src/core/file/FileOperator.cpp | 67 | ||||
-rw-r--r-- | src/core/file/FileOperator.h | 85 | ||||
-rw-r--r-- | src/core/function/GpgFileOpera.cpp | 62 |
5 files changed, 246 insertions, 19 deletions
diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 373927d1..27a4f81c 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -28,6 +28,7 @@ aux_source_directory(./result_analyse GPG_SOURCE) aux_source_directory(./function GPG_SOURCE) aux_source_directory(./model GPG_SOURCE) aux_source_directory(./aes GPG_SOURCE) +aux_source_directory(./file GPG_SOURCE) aux_source_directory(. GPG_SOURCE) add_library(gpgfrontend_core STATIC ${GPG_SOURCE}) @@ -52,7 +53,9 @@ target_link_libraries(gpgfrontend_core target_link_libraries(gpgfrontend_core Qt5::Core) # set up pch -target_precompile_headers(gpgfrontend_core PUBLIC ${CMAKE_SOURCE_DIR}/src/GpgFrontend.h) +target_precompile_headers(gpgfrontend_core + PUBLIC ${CMAKE_SOURCE_DIR}/src/GpgFrontend.h + PUBLIC GpgFrontendCore.h) if (MINGW) message(STATUS "Link GPG Static Library For MINGW") diff --git a/src/core/GpgFrontendCore.h b/src/core/GpgFrontendCore.h new file mode 100644 index 00000000..696f97bc --- /dev/null +++ b/src/core/GpgFrontendCore.h @@ -0,0 +1,46 @@ +/** +* Copyright (C) 2021 Saturneric +* +* This file is part of GpgFrontend. +* +* GpgFrontend is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* GpgFrontend is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with GpgFrontend. If not, see <https://www.gnu.org/licenses/>. +* +* The initial version of the source code is inherited from +* the gpg4usb project, which is under GPL-3.0-or-later. +* +* All the source code of GpgFrontend was modified and released by +* Saturneric<[email protected]> starting on May 12, 2021. +* +* SPDX-License-Identifier: GPL-3.0-or-later +* + */ + +#ifndef GPGFRONTEND_GPGFRONTENDCORE_H +#define GPGFRONTEND_GPGFRONTENDCORE_H + +#include "GpgFrontend.h" + +// std includes +#include <filesystem> + +// boost includes +#include <boost/format.hpp> + +// Qt includes +#include <QtCore> + +#include "core/GpgModel.h" + + +#endif // GPGFRONTEND_GPGFRONTENDCORE_H diff --git a/src/core/file/FileOperator.cpp b/src/core/file/FileOperator.cpp new file mode 100644 index 00000000..e721fc7e --- /dev/null +++ b/src/core/file/FileOperator.cpp @@ -0,0 +1,67 @@ +/** +* Copyright (C) 2021 Saturneric +* +* This file is part of GpgFrontend. +* +* GpgFrontend is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* GpgFrontend is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with GpgFrontend. If not, see <https://www.gnu.org/licenses/>. +* +* The initial version of the source code is inherited from +* the gpg4usb project, which is under GPL-3.0-or-later. +* +* All the source code of GpgFrontend was modified and released by +* Saturneric<[email protected]> starting on May 12, 2021. +* +* SPDX-License-Identifier: GPL-3.0-or-later +* +*/ + +#include "FileOperator.h" + +bool GpgFrontend::FileOperator::ReadFile(const QString& file_name, + QByteArray& data) { + QFile file(file_name); + if (!file.open(QIODevice::ReadOnly)) { + LOG(ERROR) << "failed to open file" << file_name.toStdString(); + return false; + } + data = file.readAll(); + file.close(); + return true; +} + +bool GpgFrontend::FileOperator::WriteFile(const QString& file_name, + const QByteArray& data) { + QFile file(file_name); + if (!file.open(QIODevice::WriteOnly)) { + LOG(ERROR) << "failed to open file" << file_name.toStdString(); + return false; + } + file.write(data); + file.close(); + return true; +} + +bool GpgFrontend::FileOperator::ReadFileStd( + const std::filesystem::path& file_name, std::string& data) { + QByteArray byte_data; + bool res = ReadFile(QString::fromStdString(file_name.string()), byte_data); + data = byte_data.toStdString(); + return res; +} + +bool GpgFrontend::FileOperator::WriteFileStd( + const std::filesystem::path& file_name, const std::string& data) { + return WriteFile(QString::fromStdString(file_name.string()), + QByteArray::fromStdString(data)); +} diff --git a/src/core/file/FileOperator.h b/src/core/file/FileOperator.h new file mode 100644 index 00000000..de195a96 --- /dev/null +++ b/src/core/file/FileOperator.h @@ -0,0 +1,85 @@ +/** + * Copyright (C) 2021 Saturneric + * + * This file is part of GpgFrontend. + * + * GpgFrontend is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GpgFrontend is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GpgFrontend. If not, see <https://www.gnu.org/licenses/>. + * + * The initial version of the source code is inherited from + * the gpg4usb project, which is under GPL-3.0-or-later. + * + * All the source code of GpgFrontend was modified and released by + * Saturneric<[email protected]> starting on May 12, 2021. + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + +#ifndef GPGFRONTEND_FILEOPERATOR_H +#define GPGFRONTEND_FILEOPERATOR_H + +#include "core/GpgFrontendCore.h" + +namespace GpgFrontend { + +/** + * @brief provides file operations + * + */ +class FileOperator { +public: + /** + * @brief read file content using std struct + * + * + * @param file_name file name + * @param data data read from file + * @return + */ + static bool ReadFileStd(const std::filesystem::path &file_name, + std::string &data); + + /** + * @brief write file content using std struct + * + * @param file_name file name + * @param data data to write to file + * @return + */ + static bool WriteFileStd(const std::filesystem::path &file_name, + const std::string &data); + + /** + * @brief read file content + * + * @param file_name file name + * @param data data read from file + * @return true if success + * @return false if failed + */ + static bool ReadFile(const QString &file_name, QByteArray &data); + + /** + * @brief write file content + * + * @param file_name file name + * @param data data to write to file + * @return true if success + * @return false if failed + */ + static bool WriteFile(const QString &file_name, const QByteArray &data); +}; +} // namespace GpgFrontend + +#endif // GPGFRONTEND_FILEOPERATOR_H diff --git a/src/core/function/GpgFileOpera.cpp b/src/core/function/GpgFileOpera.cpp index c9c4fc0e..15c359da 100644 --- a/src/core/function/GpgFileOpera.cpp +++ b/src/core/function/GpgFileOpera.cpp @@ -32,18 +32,23 @@ #include "GpgConstants.h" #include "core/function/GpgBasicOperator.h" +#include "core/file/FileOperator.h" GpgFrontend::GpgError GpgFrontend::GpgFileOpera::EncryptFile( KeyListPtr keys, const std::string& in_path, const std::string& out_path, GpgEncrResult& result, int _channel) { - std::string in_buffer = read_all_data_in_file(in_path); - std::unique_ptr<std::string> out_buffer; + + std::string in_buffer; + if(!FileOperator::ReadFileStd(in_path, in_buffer)) { + throw std::runtime_error("read file error"); + } + std::unique_ptr<std::string> out_buffer = nullptr; auto err = GpgBasicOperator::GetInstance(_channel).Encrypt( std::move(keys), in_buffer, out_buffer, result); if (check_gpg_error_2_err_code(err) == GPG_ERR_NO_ERROR) - if (!write_buffer_to_file(out_path, *out_buffer)) { + if (!FileOperator::WriteFileStd(out_path, *out_buffer)) { throw std::runtime_error("write_buffer_to_file error"); }; @@ -53,7 +58,10 @@ GpgFrontend::GpgError GpgFrontend::GpgFileOpera::EncryptFile( GpgFrontend::GpgError GpgFrontend::GpgFileOpera::DecryptFile( const std::string& in_path, const std::string& out_path, GpgDecrResult& result) { - std::string in_buffer = read_all_data_in_file(in_path); + std::string in_buffer; + if(!FileOperator::ReadFileStd(in_path, in_buffer)) { + throw std::runtime_error("read file error"); + } std::unique_ptr<std::string> out_buffer; auto err = @@ -62,7 +70,7 @@ GpgFrontend::GpgError GpgFrontend::GpgFileOpera::DecryptFile( assert(check_gpg_error_2_err_code(err) == GPG_ERR_NO_ERROR); if (check_gpg_error_2_err_code(err) == GPG_ERR_NO_ERROR) - if (!write_buffer_to_file(out_path, *out_buffer)) { + if (!FileOperator::WriteFileStd(out_path, *out_buffer)) { throw std::runtime_error("write_buffer_to_file error"); }; @@ -74,14 +82,17 @@ gpgme_error_t GpgFrontend::GpgFileOpera::SignFile(KeyListPtr keys, const std::string& out_path, GpgSignResult& result, int _channel) { - auto in_buffer = read_all_data_in_file(in_path); + std::string in_buffer; + if(!FileOperator::ReadFileStd(in_path, in_buffer)) { + throw std::runtime_error("read file error"); + } std::unique_ptr<std::string> out_buffer; auto err = GpgBasicOperator::GetInstance(_channel).Sign( std::move(keys), in_buffer, out_buffer, GPGME_SIG_MODE_DETACH, result); if (check_gpg_error_2_err_code(err) == GPG_ERR_NO_ERROR) - if (!write_buffer_to_file(out_path, *out_buffer)) { + if (!FileOperator::WriteFileStd(out_path, *out_buffer)) { throw std::runtime_error("write_buffer_to_file error"); }; @@ -91,11 +102,18 @@ gpgme_error_t GpgFrontend::GpgFileOpera::SignFile(KeyListPtr keys, gpgme_error_t GpgFrontend::GpgFileOpera::VerifyFile( const std::string& data_path, const std::string& sign_path, GpgVerifyResult& result, int _channel) { - auto in_buffer = read_all_data_in_file(data_path); + std::string in_buffer; + if(!FileOperator::ReadFileStd(data_path, in_buffer)) { + throw std::runtime_error("read file error"); + } std::unique_ptr<std::string> sign_buffer = nullptr; if (!sign_path.empty()) { + std::string sign_buffer_str; + if (!FileOperator::ReadFileStd(sign_path, sign_buffer_str)) { + throw std::runtime_error("read file error"); + } sign_buffer = - std::make_unique<std::string>(read_all_data_in_file(sign_path)); + std::make_unique<std::string>(sign_buffer_str); } auto err = GpgBasicOperator::GetInstance(_channel).Verify(in_buffer, sign_buffer, result); @@ -106,7 +124,10 @@ gpg_error_t GpgFrontend::GpgFileOpera::EncryptSignFile( KeyListPtr keys, KeyListPtr signer_keys, const std::string& in_path, const std::string& out_path, GpgEncrResult& encr_res, GpgSignResult& sign_res, int _channel) { - auto in_buffer = read_all_data_in_file(in_path); + std::string in_buffer; + if(!FileOperator::ReadFileStd(in_path, in_buffer)) { + throw std::runtime_error("read file error"); + } std::unique_ptr<std::string> out_buffer = nullptr; auto err = GpgBasicOperator::GetInstance(_channel).EncryptSign( @@ -114,7 +135,7 @@ gpg_error_t GpgFrontend::GpgFileOpera::EncryptSignFile( sign_res); if (check_gpg_error_2_err_code(err) == GPG_ERR_NO_ERROR) - if (!write_buffer_to_file(out_path, *out_buffer)) { + if (!FileOperator::WriteFileStd(out_path, *out_buffer)) { throw std::runtime_error("write_buffer_to_file error"); }; @@ -124,16 +145,18 @@ gpg_error_t GpgFrontend::GpgFileOpera::EncryptSignFile( gpg_error_t GpgFrontend::GpgFileOpera::DecryptVerifyFile( const std::string& in_path, const std::string& out_path, GpgDecrResult& decr_res, GpgVerifyResult& verify_res) { - auto in_buffer = read_all_data_in_file(in_path); + std::string in_buffer; + if(!FileOperator::ReadFileStd(in_path, in_buffer)) { + throw std::runtime_error("read file error"); + } std::unique_ptr<std::string> out_buffer = nullptr; - auto err = GpgBasicOperator::GetInstance().DecryptVerify(in_buffer, out_buffer, decr_res, verify_res); if (check_gpg_error_2_err_code(err) == GPG_ERR_NO_ERROR) - if (!write_buffer_to_file(out_path, *out_buffer)) { - throw std::runtime_error("write_buffer_to_file error"); + if (!FileOperator::WriteFileStd(out_path, *out_buffer)) { + throw std::runtime_error("write file error"); }; return err; @@ -141,14 +164,17 @@ gpg_error_t GpgFrontend::GpgFileOpera::DecryptVerifyFile( unsigned int GpgFrontend::GpgFileOpera::EncryptFileSymmetric( const std::string& in_path, const std::string& out_path, GpgFrontend::GpgEncrResult& result, int _channel) { - std::string in_buffer = read_all_data_in_file(in_path); - std::unique_ptr<std::string> out_buffer; + std::string in_buffer; + if(!FileOperator::ReadFileStd(in_path, in_buffer)) { + throw std::runtime_error("read file error"); + } + std::unique_ptr<std::string> out_buffer; auto err = GpgBasicOperator::GetInstance(_channel).EncryptSymmetric( in_buffer, out_buffer, result); if (check_gpg_error_2_err_code(err) == GPG_ERR_NO_ERROR) - if (!write_buffer_to_file(out_path, *out_buffer)) { + if (!FileOperator::WriteFileStd(out_path, *out_buffer)) { throw std::runtime_error("write_buffer_to_file error"); }; |