From 39440522111abf3adeef6b56cb23722119fbf3c2 Mon Sep 17 00:00:00 2001 From: Saturneric Date: Sun, 5 Sep 2021 15:02:19 +0000 Subject: Rewrite the core. Adjust the structure. --- src/gpg/function/GpgFileOpera.cpp | 262 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 262 insertions(+) create mode 100644 src/gpg/function/GpgFileOpera.cpp (limited to 'src/gpg/function/GpgFileOpera.cpp') diff --git a/src/gpg/function/GpgFileOpera.cpp b/src/gpg/function/GpgFileOpera.cpp new file mode 100644 index 00000000..50e4e932 --- /dev/null +++ b/src/gpg/function/GpgFileOpera.cpp @@ -0,0 +1,262 @@ +/** + * 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. + * + * Foobar 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 Foobar. If not, see . + * + * The initial version of the source code is inherited from gpg4usb-team. + * Their source code version also complies with GNU General Public License. + * + * The source code version of this software was modified and released + * by Saturneric starting on May 12, 2021. + * + */ +#include "gpg/function/GpgFileOpera.h" +#include "gpg/function/BasicOperator.h" +#include + +GpgFrontend::GpgError GpgFrontend::GpgFileOpera::EncryptFile( + KeyArgsList &keys, const std::string &path, GpgEncrResult &result) { + + QFileInfo file_info(path.c_str()); + + if (!file_info.isFile() || !file_info.isReadable()) + throw std::runtime_error("no permission"); + + QFile in_file(path.c_str()); + if (!in_file.open(QIODevice::ReadOnly)) + throw std::runtime_error("cannot open file"); + + QByteArray in_buffer = in_file.readAll(); + auto out_buffer = std::make_unique(); + in_file.close(); + + auto err = + BasicOperator::GetInstance().Encrypt(keys, in_buffer, out_buffer, result); + + if (gpg_err_code(err) != GPG_ERR_NO_ERROR) + return err; + + QFile out_file((path + ".asc").c_str()); + + if (!out_file.open(QFile::WriteOnly)) + throw std::runtime_error("cannot open file"); + + QDataStream out(&out_file); + out.writeRawData(out_buffer->data(), out_buffer->length()); + out_file.close(); + return err; +} + +GpgFrontend::GpgError +GpgFrontend::GpgFileOpera::DecryptFile(const QString &path, + GpgDecrResult &result) { + + QFileInfo file_info(path); + + if (!file_info.isFile() || !file_info.isReadable()) + throw std::runtime_error("no permission"); + + QFile in_file(path); + if (!in_file.open(QIODevice::ReadOnly)) + throw std::runtime_error("cannot open file"); + + QByteArray in_buffer = in_file.readAll(); + auto out_buffer = std::make_unique(); + in_file.close(); + + auto err = + BasicOperator::GetInstance().Decrypt(in_buffer, out_buffer, result); + + assert(gpgme_err_code(err) == GPG_ERR_NO_ERROR); + + QString out_file_name, file_extension = file_info.suffix(); + + if (file_extension == "asc" || file_extension == "gpg") { + int pos = path.lastIndexOf('.'); + out_file_name = path.left(pos); + } else + out_file_name = path + ".out"; + + QFile out_file(out_file_name); + + if (!out_file.open(QFile::WriteOnly)) + throw std::runtime_error("cannot open file"); + + QDataStream out(&out_file); + out.writeRawData(out_buffer->data(), out_buffer->length()); + out_file.close(); + + return err; +} + +gpgme_error_t GpgFrontend::GpgFileOpera::SignFile(KeyArgsList &keys, + const std::string &path, + GpgSignResult &result) { + + QFileInfo file_info(path.c_str()); + + if (!file_info.isFile() || !file_info.isReadable()) + throw std::runtime_error("no permission"); + + QFile in_file(path.c_str()); + if (!in_file.open(QIODevice::ReadOnly)) + throw std::runtime_error("cannot open file"); + + QByteArray in_buffer = in_file.readAll(); + auto out_buffer = std::make_unique(); + in_file.close(); + + auto err = BasicOperator::GetInstance().Sign(keys, in_buffer, out_buffer, + GPGME_SIG_MODE_DETACH, result); + assert(gpgme_err_code(err) == GPG_ERR_NO_ERROR); + + QFile out_file((path + ".sig").c_str()); + + if (!out_file.open(QFile::WriteOnly)) + throw std::runtime_error("cannot open file"); + + QDataStream out(&out_file); + out.writeRawData(out_buffer->data(), out_buffer->length()); + out_file.close(); + + return err; +} + +gpgme_error_t GpgFrontend::GpgFileOpera::VerifyFile(const std::string &path, + GpgVerifyResult &result) { + + qDebug() << "Verify File Path" << path.c_str(); + + QFileInfo file_info(path.c_str()); + + if (!file_info.isFile() || !file_info.isReadable()) + throw std::runtime_error("no permission"); + + QFile in_file(path.c_str()); + if (!in_file.open(QIODevice::ReadOnly)) + throw std::runtime_error("cannot open file"); + + QByteArray in_buffer = in_file.readAll(); + std::unique_ptr sign_buffer = nullptr; + + if (file_info.suffix() == "gpg") { + auto err = + BasicOperator::GetInstance().Verify(in_buffer, sign_buffer, result); + return err; + } else { + QFile sign_file; + sign_file.setFileName((path + ".sig").c_str()); + if (!sign_file.open(QIODevice::ReadOnly)) { + throw std::runtime_error("cannot open file"); + } + + auto sign_buffer = std::make_unique(sign_file.readAll()); + in_file.close(); + + auto err = + BasicOperator::GetInstance().Verify(in_buffer, sign_buffer, result); + return err; + } +} + +// TODO + +gpg_error_t GpgFrontend::GpgFileOpera::EncryptSignFile( + KeyArgsList &keys, const std::string &path, GpgEncrResult &encr_res, + GpgSignResult &sign_res) { + + qDebug() << "Encrypt Sign File Path" << path.c_str(); + + QFileInfo file_info(path.c_str()); + + if (!file_info.isFile() || !file_info.isReadable()) + throw std::runtime_error("no permission"); + + QFile in_file; + in_file.setFileName(path.c_str()); + if (!in_file.open(QIODevice::ReadOnly)) + throw std::runtime_error("cannot open file"); + + QByteArray in_buffer = in_file.readAll(); + in_file.close(); + std::unique_ptr out_buffer = nullptr; + + // TODO Fill the vector + std::vector signerKeys; + + // TODO dealing with signer keys + auto err = BasicOperator::GetInstance().EncryptSign( + keys, signerKeys, in_buffer, out_buffer, encr_res, sign_res); + + if (gpg_err_code(err) != GPG_ERR_NO_ERROR) + return err; + + QFile out_file((path + ".gpg").c_str()); + + if (!out_file.open(QFile::WriteOnly)) + throw std::runtime_error("cannot open file"); + + QDataStream out(&out_file); + out.writeRawData(out_buffer->data(), out_buffer->length()); + out_file.close(); + + return err; +} + +gpg_error_t +GpgFrontend::GpgFileOpera::DecryptVerifyFile(const std::string &path, + GpgDecrResult &decr_res, + GpgVerifyResult &verify_res) { + + qDebug() << "Decrypt Verify File Path" << path.c_str(); + + QFileInfo file_info(path.c_str()); + + if (!file_info.isFile() || !file_info.isReadable()) + throw std::runtime_error("no permission"); + + QFile in_file; + in_file.setFileName(path.c_str()); + if (!in_file.open(QIODevice::ReadOnly)) + throw std::runtime_error("cannot open file"); + + QByteArray in_buffer = in_file.readAll(); + in_file.close(); + std::unique_ptr out_buffer = nullptr; + + auto err = BasicOperator::GetInstance().DecryptVerify(in_buffer, out_buffer, + decr_res, verify_res); + if (gpg_err_code(err) != GPG_ERR_NO_ERROR) + return err; + + std::string out_file_name, + file_extension = file_info.suffix().toUtf8().constData(); + + if (file_extension == "asc" || file_extension == "gpg") { + int pos = path.find_last_of('.'); + out_file_name = path.substr(0, pos); + } else + out_file_name = (path + ".out").c_str(); + + QFile out_file(out_file_name.c_str()); + + if (!out_file.open(QFile::WriteOnly)) + throw std::runtime_error("cannot open file"); + + QDataStream out(&out_file); + out.writeRawData(out_buffer->data(), out_buffer->length()); + out_file.close(); + + return err; +} -- cgit v1.2.3 From e2d30cc0194db74b77e3c06dbaf9c597bb82c860 Mon Sep 17 00:00:00 2001 From: Saturneric Date: Sun, 5 Sep 2021 21:41:00 +0000 Subject: Adjust the code structure. Introduce log library. Remove Qt from the core code. --- src/gpg/function/GpgFileOpera.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'src/gpg/function/GpgFileOpera.cpp') diff --git a/src/gpg/function/GpgFileOpera.cpp b/src/gpg/function/GpgFileOpera.cpp index 50e4e932..726c2695 100644 --- a/src/gpg/function/GpgFileOpera.cpp +++ b/src/gpg/function/GpgFileOpera.cpp @@ -23,14 +23,17 @@ */ #include "gpg/function/GpgFileOpera.h" #include "gpg/function/BasicOperator.h" + +#include #include GpgFrontend::GpgError GpgFrontend::GpgFileOpera::EncryptFile( KeyArgsList &keys, const std::string &path, GpgEncrResult &result) { - QFileInfo file_info(path.c_str()); + using namespace std::filesystem; + class path file_info(path.c_str()); - if (!file_info.isFile() || !file_info.isReadable()) + if (!exists(file_info) || !is_regular_file(path)) throw std::runtime_error("no permission"); QFile in_file(path.c_str()); @@ -59,7 +62,7 @@ GpgFrontend::GpgError GpgFrontend::GpgFileOpera::EncryptFile( } GpgFrontend::GpgError -GpgFrontend::GpgFileOpera::DecryptFile(const QString &path, +GpgFrontend::GpgFileOpera::DecryptFile(const std::string &path, GpgDecrResult &result) { QFileInfo file_info(path); -- cgit v1.2.3 From 4e076fa1982812ea7d1222c7e981879799ca8853 Mon Sep 17 00:00:00 2001 From: Saturneric Date: Fri, 10 Sep 2021 23:58:09 +0000 Subject: Continue to optimize and improve the code. --- src/gpg/function/GpgFileOpera.cpp | 227 ++++++++++++++------------------------ 1 file changed, 84 insertions(+), 143 deletions(-) (limited to 'src/gpg/function/GpgFileOpera.cpp') diff --git a/src/gpg/function/GpgFileOpera.cpp b/src/gpg/function/GpgFileOpera.cpp index 726c2695..71e60f76 100644 --- a/src/gpg/function/GpgFileOpera.cpp +++ b/src/gpg/function/GpgFileOpera.cpp @@ -22,83 +22,101 @@ * */ #include "gpg/function/GpgFileOpera.h" +#include "GpgConstants.h" #include "gpg/function/BasicOperator.h" +#include #include +#include #include +#include -GpgFrontend::GpgError GpgFrontend::GpgFileOpera::EncryptFile( - KeyArgsList &keys, const std::string &path, GpgEncrResult &result) { - +std::string read_all_data_in_file(const std::string &path) { using namespace std::filesystem; class path file_info(path.c_str()); if (!exists(file_info) || !is_regular_file(path)) throw std::runtime_error("no permission"); - QFile in_file(path.c_str()); - if (!in_file.open(QIODevice::ReadOnly)) + std::ifstream in_file; + in_file.open(path, std::ios::in); + if (!in_file.good()) throw std::runtime_error("cannot open file"); - - QByteArray in_buffer = in_file.readAll(); - auto out_buffer = std::make_unique(); + std::istreambuf_iterator begin(in_file); + std::istreambuf_iterator end; + std::string in_buffer(begin, end); in_file.close(); + return in_buffer; +} + +void write_buufer_to_file(const std::string &path, + const std::string &out_buffer) { + std::ofstream out_file(path); + out_file.open(path.c_str(), std::ios::out); + if (!out_file.good()) + throw std::runtime_error("cannot open file"); + out_file.write(out_buffer.c_str(), out_buffer.size()); + out_file.close(); +} + +GpgFrontend::GpgError GpgFrontend::GpgFileOpera::EncryptFile( + KeyArgsList &keys, const std::string &path, GpgEncrResult &result) { + + std::string in_buffer = read_all_data_in_file(path); + std::unique_ptr out_buffer; auto err = BasicOperator::GetInstance().Encrypt(keys, in_buffer, out_buffer, result); - if (gpg_err_code(err) != GPG_ERR_NO_ERROR) - return err; + assert(check_gpg_error_2_err_code(err) == GPG_ERR_NO_ERROR); - QFile out_file((path + ".asc").c_str()); + write_buufer_to_file(path + ".asc", *out_buffer); + return err; +} - if (!out_file.open(QFile::WriteOnly)) - throw std::runtime_error("cannot open file"); +std::string get_file_extension(const std::string &path) { + // Create a Path object from given string + std::filesystem::path path_obj(path); + // Check if file name in the path object has extension + if (path_obj.has_extension()) { + // Fetch the extension from path object and return + return path_obj.extension().string(); + } + // In case of no extension return empty string + return std::string(); +} - QDataStream out(&out_file); - out.writeRawData(out_buffer->data(), out_buffer->length()); - out_file.close(); - return err; +std::string get_file_name_with_path(const std::string &path) { + // Create a Path object from given string + std::filesystem::path path_obj(path); + // Check if file name in the path object has extension + if (path_obj.has_filename()) { + // Fetch the extension from path object and return + return path_obj.parent_path() / path_obj.filename(); + } + // In case of no extension return empty string + throw std::runtime_error("invalid file path"); } GpgFrontend::GpgError GpgFrontend::GpgFileOpera::DecryptFile(const std::string &path, GpgDecrResult &result) { - QFileInfo file_info(path); - - if (!file_info.isFile() || !file_info.isReadable()) - throw std::runtime_error("no permission"); - - QFile in_file(path); - if (!in_file.open(QIODevice::ReadOnly)) - throw std::runtime_error("cannot open file"); - - QByteArray in_buffer = in_file.readAll(); - auto out_buffer = std::make_unique(); - in_file.close(); + std::string in_buffer = read_all_data_in_file(path); + std::unique_ptr out_buffer; auto err = BasicOperator::GetInstance().Decrypt(in_buffer, out_buffer, result); - assert(gpgme_err_code(err) == GPG_ERR_NO_ERROR); + assert(check_gpg_error_2_err_code(err) == GPG_ERR_NO_ERROR); - QString out_file_name, file_extension = file_info.suffix(); + std::string out_file_name = get_file_name_with_path(path), + file_extension = get_file_extension(path); - if (file_extension == "asc" || file_extension == "gpg") { - int pos = path.lastIndexOf('.'); - out_file_name = path.left(pos); - } else - out_file_name = path + ".out"; - - QFile out_file(out_file_name); + if (!(file_extension == ".asc" || file_extension == ".gpg")) + out_file_name += ".out"; - if (!out_file.open(QFile::WriteOnly)) - throw std::runtime_error("cannot open file"); - - QDataStream out(&out_file); - out.writeRawData(out_buffer->data(), out_buffer->length()); - out_file.close(); + write_buufer_to_file(out_file_name, *out_buffer); return err; } @@ -107,31 +125,15 @@ gpgme_error_t GpgFrontend::GpgFileOpera::SignFile(KeyArgsList &keys, const std::string &path, GpgSignResult &result) { - QFileInfo file_info(path.c_str()); - - if (!file_info.isFile() || !file_info.isReadable()) - throw std::runtime_error("no permission"); - - QFile in_file(path.c_str()); - if (!in_file.open(QIODevice::ReadOnly)) - throw std::runtime_error("cannot open file"); - - QByteArray in_buffer = in_file.readAll(); - auto out_buffer = std::make_unique(); - in_file.close(); + auto in_buffer = read_all_data_in_file(path); + std::unique_ptr out_buffer; auto err = BasicOperator::GetInstance().Sign(keys, in_buffer, out_buffer, GPGME_SIG_MODE_DETACH, result); - assert(gpgme_err_code(err) == GPG_ERR_NO_ERROR); - QFile out_file((path + ".sig").c_str()); + assert(check_gpg_error_2_err_code(err) == GPG_ERR_NO_ERROR); - if (!out_file.open(QFile::WriteOnly)) - throw std::runtime_error("cannot open file"); - - QDataStream out(&out_file); - out.writeRawData(out_buffer->data(), out_buffer->length()); - out_file.close(); + write_buufer_to_file(path + ".sig", *out_buffer); return err; } @@ -139,33 +141,17 @@ gpgme_error_t GpgFrontend::GpgFileOpera::SignFile(KeyArgsList &keys, gpgme_error_t GpgFrontend::GpgFileOpera::VerifyFile(const std::string &path, GpgVerifyResult &result) { - qDebug() << "Verify File Path" << path.c_str(); - - QFileInfo file_info(path.c_str()); - - if (!file_info.isFile() || !file_info.isReadable()) - throw std::runtime_error("no permission"); - - QFile in_file(path.c_str()); - if (!in_file.open(QIODevice::ReadOnly)) - throw std::runtime_error("cannot open file"); + auto in_buffer = read_all_data_in_file(path); + std::unique_ptr sign_buffer = nullptr; - QByteArray in_buffer = in_file.readAll(); - std::unique_ptr sign_buffer = nullptr; - - if (file_info.suffix() == "gpg") { + if (get_file_extension(path) == ".gpg") { auto err = BasicOperator::GetInstance().Verify(in_buffer, sign_buffer, result); + assert(check_gpg_error_2_err_code(err) == GPG_ERR_NO_ERROR); return err; } else { - QFile sign_file; - sign_file.setFileName((path + ".sig").c_str()); - if (!sign_file.open(QIODevice::ReadOnly)) { - throw std::runtime_error("cannot open file"); - } - - auto sign_buffer = std::make_unique(sign_file.readAll()); - in_file.close(); + auto sign_buffer = + std::make_unique(read_all_data_in_file(path + ".sig")); auto err = BasicOperator::GetInstance().Verify(in_buffer, sign_buffer, result); @@ -179,21 +165,8 @@ gpg_error_t GpgFrontend::GpgFileOpera::EncryptSignFile( KeyArgsList &keys, const std::string &path, GpgEncrResult &encr_res, GpgSignResult &sign_res) { - qDebug() << "Encrypt Sign File Path" << path.c_str(); - - QFileInfo file_info(path.c_str()); - - if (!file_info.isFile() || !file_info.isReadable()) - throw std::runtime_error("no permission"); - - QFile in_file; - in_file.setFileName(path.c_str()); - if (!in_file.open(QIODevice::ReadOnly)) - throw std::runtime_error("cannot open file"); - - QByteArray in_buffer = in_file.readAll(); - in_file.close(); - std::unique_ptr out_buffer = nullptr; + auto in_buffer = read_all_data_in_file(path); + std::unique_ptr out_buffer = nullptr; // TODO Fill the vector std::vector signerKeys; @@ -202,17 +175,9 @@ gpg_error_t GpgFrontend::GpgFileOpera::EncryptSignFile( auto err = BasicOperator::GetInstance().EncryptSign( keys, signerKeys, in_buffer, out_buffer, encr_res, sign_res); - if (gpg_err_code(err) != GPG_ERR_NO_ERROR) - return err; - - QFile out_file((path + ".gpg").c_str()); + assert(check_gpg_error_2_err_code(err) == GPG_ERR_NO_ERROR); - if (!out_file.open(QFile::WriteOnly)) - throw std::runtime_error("cannot open file"); - - QDataStream out(&out_file); - out.writeRawData(out_buffer->data(), out_buffer->length()); - out_file.close(); + write_buufer_to_file(path + ".gpg", *out_buffer); return err; } @@ -222,44 +187,20 @@ GpgFrontend::GpgFileOpera::DecryptVerifyFile(const std::string &path, GpgDecrResult &decr_res, GpgVerifyResult &verify_res) { - qDebug() << "Decrypt Verify File Path" << path.c_str(); - - QFileInfo file_info(path.c_str()); - - if (!file_info.isFile() || !file_info.isReadable()) - throw std::runtime_error("no permission"); - - QFile in_file; - in_file.setFileName(path.c_str()); - if (!in_file.open(QIODevice::ReadOnly)) - throw std::runtime_error("cannot open file"); - - QByteArray in_buffer = in_file.readAll(); - in_file.close(); - std::unique_ptr out_buffer = nullptr; + auto in_buffer = read_all_data_in_file(path); + std::unique_ptr out_buffer = nullptr; auto err = BasicOperator::GetInstance().DecryptVerify(in_buffer, out_buffer, decr_res, verify_res); - if (gpg_err_code(err) != GPG_ERR_NO_ERROR) - return err; - - std::string out_file_name, - file_extension = file_info.suffix().toUtf8().constData(); + assert(check_gpg_error_2_err_code(err) == GPG_ERR_NO_ERROR); - if (file_extension == "asc" || file_extension == "gpg") { - int pos = path.find_last_of('.'); - out_file_name = path.substr(0, pos); - } else - out_file_name = (path + ".out").c_str(); + std::string out_file_name = get_file_name_with_path(path), + file_extension = get_file_extension(path); - QFile out_file(out_file_name.c_str()); - - if (!out_file.open(QFile::WriteOnly)) - throw std::runtime_error("cannot open file"); + if (!(file_extension == ".asc" || file_extension == ".gpg")) + out_file_name = path + ".out"; - QDataStream out(&out_file); - out.writeRawData(out_buffer->data(), out_buffer->length()); - out_file.close(); + write_buufer_to_file(out_file_name, *out_buffer); return err; } -- cgit v1.2.3 From 5874147d9ec0f94a5058241a06c69f586b766a3b Mon Sep 17 00:00:00 2001 From: Saturneric Date: Sun, 12 Sep 2021 21:56:18 +0800 Subject: Continue to write core test code. --- src/gpg/function/GpgFileOpera.cpp | 104 +++++++++----------------------------- 1 file changed, 25 insertions(+), 79 deletions(-) (limited to 'src/gpg/function/GpgFileOpera.cpp') diff --git a/src/gpg/function/GpgFileOpera.cpp b/src/gpg/function/GpgFileOpera.cpp index 71e60f76..79214d2d 100644 --- a/src/gpg/function/GpgFileOpera.cpp +++ b/src/gpg/function/GpgFileOpera.cpp @@ -26,42 +26,15 @@ #include "gpg/function/BasicOperator.h" #include -#include + #include #include #include -std::string read_all_data_in_file(const std::string &path) { - using namespace std::filesystem; - class path file_info(path.c_str()); - - if (!exists(file_info) || !is_regular_file(path)) - throw std::runtime_error("no permission"); - - std::ifstream in_file; - in_file.open(path, std::ios::in); - if (!in_file.good()) - throw std::runtime_error("cannot open file"); - std::istreambuf_iterator begin(in_file); - std::istreambuf_iterator end; - std::string in_buffer(begin, end); - in_file.close(); - return in_buffer; -} - -void write_buufer_to_file(const std::string &path, - const std::string &out_buffer) { - std::ofstream out_file(path); - out_file.open(path.c_str(), std::ios::out); - if (!out_file.good()) - throw std::runtime_error("cannot open file"); - out_file.write(out_buffer.c_str(), out_buffer.size()); - out_file.close(); -} - GpgFrontend::GpgError GpgFrontend::GpgFileOpera::EncryptFile( - KeyArgsList &keys, const std::string &path, GpgEncrResult &result) { - + KeyArgsList& keys, + const std::string& path, + GpgEncrResult& result) { std::string in_buffer = read_all_data_in_file(path); std::unique_ptr out_buffer; @@ -70,38 +43,13 @@ GpgFrontend::GpgError GpgFrontend::GpgFileOpera::EncryptFile( assert(check_gpg_error_2_err_code(err) == GPG_ERR_NO_ERROR); - write_buufer_to_file(path + ".asc", *out_buffer); + write_buffer_to_file(path + ".asc", *out_buffer); return err; } -std::string get_file_extension(const std::string &path) { - // Create a Path object from given string - std::filesystem::path path_obj(path); - // Check if file name in the path object has extension - if (path_obj.has_extension()) { - // Fetch the extension from path object and return - return path_obj.extension().string(); - } - // In case of no extension return empty string - return std::string(); -} - -std::string get_file_name_with_path(const std::string &path) { - // Create a Path object from given string - std::filesystem::path path_obj(path); - // Check if file name in the path object has extension - if (path_obj.has_filename()) { - // Fetch the extension from path object and return - return path_obj.parent_path() / path_obj.filename(); - } - // In case of no extension return empty string - throw std::runtime_error("invalid file path"); -} - -GpgFrontend::GpgError -GpgFrontend::GpgFileOpera::DecryptFile(const std::string &path, - GpgDecrResult &result) { - +GpgFrontend::GpgError GpgFrontend::GpgFileOpera::DecryptFile( + const std::string& path, + GpgDecrResult& result) { std::string in_buffer = read_all_data_in_file(path); std::unique_ptr out_buffer; @@ -116,15 +64,14 @@ GpgFrontend::GpgFileOpera::DecryptFile(const std::string &path, if (!(file_extension == ".asc" || file_extension == ".gpg")) out_file_name += ".out"; - write_buufer_to_file(out_file_name, *out_buffer); + write_buffer_to_file(out_file_name, *out_buffer); return err; } -gpgme_error_t GpgFrontend::GpgFileOpera::SignFile(KeyArgsList &keys, - const std::string &path, - GpgSignResult &result) { - +gpgme_error_t GpgFrontend::GpgFileOpera::SignFile(KeyArgsList& keys, + const std::string& path, + GpgSignResult& result) { auto in_buffer = read_all_data_in_file(path); std::unique_ptr out_buffer; @@ -133,14 +80,13 @@ gpgme_error_t GpgFrontend::GpgFileOpera::SignFile(KeyArgsList &keys, assert(check_gpg_error_2_err_code(err) == GPG_ERR_NO_ERROR); - write_buufer_to_file(path + ".sig", *out_buffer); + write_buffer_to_file(path + ".sig", *out_buffer); return err; } -gpgme_error_t GpgFrontend::GpgFileOpera::VerifyFile(const std::string &path, - GpgVerifyResult &result) { - +gpgme_error_t GpgFrontend::GpgFileOpera::VerifyFile(const std::string& path, + GpgVerifyResult& result) { auto in_buffer = read_all_data_in_file(path); std::unique_ptr sign_buffer = nullptr; @@ -162,9 +108,10 @@ gpgme_error_t GpgFrontend::GpgFileOpera::VerifyFile(const std::string &path, // TODO gpg_error_t GpgFrontend::GpgFileOpera::EncryptSignFile( - KeyArgsList &keys, const std::string &path, GpgEncrResult &encr_res, - GpgSignResult &sign_res) { - + KeyArgsList& keys, + const std::string& path, + GpgEncrResult& encr_res, + GpgSignResult& sign_res) { auto in_buffer = read_all_data_in_file(path); std::unique_ptr out_buffer = nullptr; @@ -177,16 +124,15 @@ gpg_error_t GpgFrontend::GpgFileOpera::EncryptSignFile( assert(check_gpg_error_2_err_code(err) == GPG_ERR_NO_ERROR); - write_buufer_to_file(path + ".gpg", *out_buffer); + write_buffer_to_file(path + ".gpg", *out_buffer); return err; } -gpg_error_t -GpgFrontend::GpgFileOpera::DecryptVerifyFile(const std::string &path, - GpgDecrResult &decr_res, - GpgVerifyResult &verify_res) { - +gpg_error_t GpgFrontend::GpgFileOpera::DecryptVerifyFile( + const std::string& path, + GpgDecrResult& decr_res, + GpgVerifyResult& verify_res) { auto in_buffer = read_all_data_in_file(path); std::unique_ptr out_buffer = nullptr; @@ -200,7 +146,7 @@ GpgFrontend::GpgFileOpera::DecryptVerifyFile(const std::string &path, if (!(file_extension == ".asc" || file_extension == ".gpg")) out_file_name = path + ".out"; - write_buufer_to_file(out_file_name, *out_buffer); + write_buffer_to_file(out_file_name, *out_buffer); return err; } -- cgit v1.2.3 From 8544bb3ed9ccf09f3082c3032381a1b61dc5e122 Mon Sep 17 00:00:00 2001 From: Saturneric Date: Mon, 20 Sep 2021 17:35:57 +0800 Subject: The basic functions of the core pass the test. Adjust and improve the core part of the interface. --- src/gpg/function/GpgFileOpera.cpp | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) (limited to 'src/gpg/function/GpgFileOpera.cpp') diff --git a/src/gpg/function/GpgFileOpera.cpp b/src/gpg/function/GpgFileOpera.cpp index 79214d2d..0f78c30c 100644 --- a/src/gpg/function/GpgFileOpera.cpp +++ b/src/gpg/function/GpgFileOpera.cpp @@ -32,14 +32,14 @@ #include GpgFrontend::GpgError GpgFrontend::GpgFileOpera::EncryptFile( - KeyArgsList& keys, + KeyArgsList&& keys, const std::string& path, GpgEncrResult& result) { std::string in_buffer = read_all_data_in_file(path); std::unique_ptr out_buffer; - auto err = - BasicOperator::GetInstance().Encrypt(keys, in_buffer, out_buffer, result); + auto err = BasicOperator::GetInstance().Encrypt(std::move(keys), in_buffer, + out_buffer, result); assert(check_gpg_error_2_err_code(err) == GPG_ERR_NO_ERROR); @@ -69,14 +69,14 @@ GpgFrontend::GpgError GpgFrontend::GpgFileOpera::DecryptFile( return err; } -gpgme_error_t GpgFrontend::GpgFileOpera::SignFile(KeyArgsList& keys, +gpgme_error_t GpgFrontend::GpgFileOpera::SignFile(KeyArgsList&& keys, const std::string& path, GpgSignResult& result) { auto in_buffer = read_all_data_in_file(path); std::unique_ptr out_buffer; - auto err = BasicOperator::GetInstance().Sign(keys, in_buffer, out_buffer, - GPGME_SIG_MODE_DETACH, result); + auto err = BasicOperator::GetInstance().Sign( + std::move(keys), in_buffer, out_buffer, GPGME_SIG_MODE_DETACH, result); assert(check_gpg_error_2_err_code(err) == GPG_ERR_NO_ERROR); @@ -108,7 +108,7 @@ gpgme_error_t GpgFrontend::GpgFileOpera::VerifyFile(const std::string& path, // TODO gpg_error_t GpgFrontend::GpgFileOpera::EncryptSignFile( - KeyArgsList& keys, + KeyArgsList&& keys, const std::string& path, GpgEncrResult& encr_res, GpgSignResult& sign_res) { @@ -120,7 +120,8 @@ gpg_error_t GpgFrontend::GpgFileOpera::EncryptSignFile( // TODO dealing with signer keys auto err = BasicOperator::GetInstance().EncryptSign( - keys, signerKeys, in_buffer, out_buffer, encr_res, sign_res); + std::move(keys), std::move(signerKeys), in_buffer, out_buffer, encr_res, + sign_res); assert(check_gpg_error_2_err_code(err) == GPG_ERR_NO_ERROR); -- cgit v1.2.3 From 9c1446c97ffff92a6c90442f9148239f24ac9a54 Mon Sep 17 00:00:00 2001 From: Saturneric Date: Sun, 28 Nov 2021 21:38:59 +0800 Subject: Fix some known issues in basic operations and file operations. --- src/gpg/function/GpgFileOpera.cpp | 37 +++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 20 deletions(-) (limited to 'src/gpg/function/GpgFileOpera.cpp') diff --git a/src/gpg/function/GpgFileOpera.cpp b/src/gpg/function/GpgFileOpera.cpp index 0f78c30c..b8e204b8 100644 --- a/src/gpg/function/GpgFileOpera.cpp +++ b/src/gpg/function/GpgFileOpera.cpp @@ -22,19 +22,15 @@ * */ #include "gpg/function/GpgFileOpera.h" -#include "GpgConstants.h" -#include "gpg/function/BasicOperator.h" - -#include -#include #include #include +#include "GpgConstants.h" +#include "gpg/function/BasicOperator.h" + GpgFrontend::GpgError GpgFrontend::GpgFileOpera::EncryptFile( - KeyArgsList&& keys, - const std::string& path, - GpgEncrResult& result) { + KeyArgsList&& keys, const std::string& path, GpgEncrResult& result) { std::string in_buffer = read_all_data_in_file(path); std::unique_ptr out_buffer; @@ -48,8 +44,7 @@ GpgFrontend::GpgError GpgFrontend::GpgFileOpera::EncryptFile( } GpgFrontend::GpgError GpgFrontend::GpgFileOpera::DecryptFile( - const std::string& path, - GpgDecrResult& result) { + const std::string& path, GpgDecrResult& result) { std::string in_buffer = read_all_data_in_file(path); std::unique_ptr out_buffer; @@ -96,7 +91,7 @@ gpgme_error_t GpgFrontend::GpgFileOpera::VerifyFile(const std::string& path, assert(check_gpg_error_2_err_code(err) == GPG_ERR_NO_ERROR); return err; } else { - auto sign_buffer = + sign_buffer = std::make_unique(read_all_data_in_file(path + ".sig")); auto err = @@ -108,9 +103,7 @@ gpgme_error_t GpgFrontend::GpgFileOpera::VerifyFile(const std::string& path, // TODO gpg_error_t GpgFrontend::GpgFileOpera::EncryptSignFile( - KeyArgsList&& keys, - const std::string& path, - GpgEncrResult& encr_res, + KeyArgsList&& keys, const std::string& path, GpgEncrResult& encr_res, GpgSignResult& sign_res) { auto in_buffer = read_all_data_in_file(path); std::unique_ptr out_buffer = nullptr; @@ -123,16 +116,20 @@ gpg_error_t GpgFrontend::GpgFileOpera::EncryptSignFile( std::move(keys), std::move(signerKeys), in_buffer, out_buffer, encr_res, sign_res); - assert(check_gpg_error_2_err_code(err) == GPG_ERR_NO_ERROR); - - write_buffer_to_file(path + ".gpg", *out_buffer); + auto out_path = path + ".gpg"; + LOG(INFO) << "EncryptSignFile out_path" << out_path; + LOG(INFO) << "EncryptSignFile out_buffer size" << out_buffer->size(); - return err; + bool result = write_buffer_to_file(out_path, *out_buffer); + LOG(INFO) << "EncryptSignFile write_buffer_to_file result" << result; + if (result) + return err; + else + throw std::runtime_error("write_buffer_to_file failed."); } gpg_error_t GpgFrontend::GpgFileOpera::DecryptVerifyFile( - const std::string& path, - GpgDecrResult& decr_res, + const std::string& path, GpgDecrResult& decr_res, GpgVerifyResult& verify_res) { auto in_buffer = read_all_data_in_file(path); std::unique_ptr out_buffer = nullptr; -- cgit v1.2.3 From 839878e0a2c5dff8ed9d9422aaeebeee0e5ba964 Mon Sep 17 00:00:00 2001 From: Saturneric Date: Mon, 29 Nov 2021 12:43:00 +0800 Subject: Solve uid management, related update and other known issues. --- src/gpg/function/GpgFileOpera.cpp | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) (limited to 'src/gpg/function/GpgFileOpera.cpp') diff --git a/src/gpg/function/GpgFileOpera.cpp b/src/gpg/function/GpgFileOpera.cpp index b8e204b8..ae912ada 100644 --- a/src/gpg/function/GpgFileOpera.cpp +++ b/src/gpg/function/GpgFileOpera.cpp @@ -53,7 +53,7 @@ GpgFrontend::GpgError GpgFrontend::GpgFileOpera::DecryptFile( assert(check_gpg_error_2_err_code(err) == GPG_ERR_NO_ERROR); - std::string out_file_name = get_file_name_with_path(path), + std::string out_file_name = get_only_file_name_with_path(path), file_extension = get_file_extension(path); if (!(file_extension == ".asc" || file_extension == ".gpg")) @@ -131,20 +131,27 @@ gpg_error_t GpgFrontend::GpgFileOpera::EncryptSignFile( gpg_error_t GpgFrontend::GpgFileOpera::DecryptVerifyFile( const std::string& path, GpgDecrResult& decr_res, GpgVerifyResult& verify_res) { + LOG(INFO) << "GpgFrontend::GpgFileOpera::DecryptVerifyFile Called"; + auto in_buffer = read_all_data_in_file(path); + + LOG(INFO) << "GpgFrontend::GpgFileOpera::DecryptVerifyFile in_buffer" + << in_buffer.size(); std::unique_ptr out_buffer = nullptr; auto err = BasicOperator::GetInstance().DecryptVerify(in_buffer, out_buffer, decr_res, verify_res); - assert(check_gpg_error_2_err_code(err) == GPG_ERR_NO_ERROR); - std::string out_file_name = get_file_name_with_path(path), + std::string out_file_name = get_only_file_name_with_path(path), file_extension = get_file_extension(path); if (!(file_extension == ".asc" || file_extension == ".gpg")) out_file_name = path + ".out"; - - write_buffer_to_file(out_file_name, *out_buffer); + LOG(INFO) << "GpgFrontend::GpgFileOpera::DecryptVerifyFile out_file_name" + << out_file_name; + if (!write_buffer_to_file(out_file_name, *out_buffer)) { + throw std::runtime_error("write_buffer_to_file error"); + }; return err; } -- cgit v1.2.3 From 1ae8663decb3163b92d32b80cefb46eb678a5af6 Mon Sep 17 00:00:00 2001 From: Saturneric Date: Thu, 2 Dec 2021 01:25:46 +0800 Subject: Add i18n Support 1. Remove Qt Linguist. 2. Add GNU gettext libraries. 3. Modified source codes to meet with i18n support. --- src/gpg/function/GpgFileOpera.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/gpg/function/GpgFileOpera.cpp') diff --git a/src/gpg/function/GpgFileOpera.cpp b/src/gpg/function/GpgFileOpera.cpp index ae912ada..67504c0d 100644 --- a/src/gpg/function/GpgFileOpera.cpp +++ b/src/gpg/function/GpgFileOpera.cpp @@ -1,7 +1,7 @@ /** - * This file is part of GPGFrontend. + * This file is part of GpgFrontend. * - * GPGFrontend is free software: you can redistribute it and/or modify + * 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. -- cgit v1.2.3 From 1e3f1d13a6fb1bfc8f24be83032df1e92350dbcc Mon Sep 17 00:00:00 2001 From: Saturneric Date: Fri, 3 Dec 2021 05:35:16 +0800 Subject: Fixed. 1. Fixed known issue in File Operations. --- src/gpg/function/GpgFileOpera.cpp | 53 +++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 24 deletions(-) (limited to 'src/gpg/function/GpgFileOpera.cpp') diff --git a/src/gpg/function/GpgFileOpera.cpp b/src/gpg/function/GpgFileOpera.cpp index 67504c0d..c3f75cf8 100644 --- a/src/gpg/function/GpgFileOpera.cpp +++ b/src/gpg/function/GpgFileOpera.cpp @@ -30,16 +30,18 @@ #include "gpg/function/BasicOperator.h" GpgFrontend::GpgError GpgFrontend::GpgFileOpera::EncryptFile( - KeyArgsList&& keys, const std::string& path, GpgEncrResult& result) { + KeyListPtr keys, const std::string& path, GpgEncrResult& result) { std::string in_buffer = read_all_data_in_file(path); std::unique_ptr out_buffer; auto err = BasicOperator::GetInstance().Encrypt(std::move(keys), in_buffer, out_buffer, result); - assert(check_gpg_error_2_err_code(err) == GPG_ERR_NO_ERROR); - - write_buffer_to_file(path + ".asc", *out_buffer); + if (check_gpg_error_2_err_code(err) == GPG_ERR_NO_ERROR) + if (!write_buffer_to_file(path + ".asc", *out_buffer)) { + throw std::runtime_error("write_buffer_to_file error"); + }; + return err; } @@ -59,12 +61,15 @@ GpgFrontend::GpgError GpgFrontend::GpgFileOpera::DecryptFile( if (!(file_extension == ".asc" || file_extension == ".gpg")) out_file_name += ".out"; - write_buffer_to_file(out_file_name, *out_buffer); + if (check_gpg_error_2_err_code(err) == GPG_ERR_NO_ERROR) + if (!write_buffer_to_file(out_file_name, *out_buffer)) { + throw std::runtime_error("write_buffer_to_file error"); + }; return err; } -gpgme_error_t GpgFrontend::GpgFileOpera::SignFile(KeyArgsList&& keys, +gpgme_error_t GpgFrontend::GpgFileOpera::SignFile(KeyListPtr keys, const std::string& path, GpgSignResult& result) { auto in_buffer = read_all_data_in_file(path); @@ -73,9 +78,10 @@ gpgme_error_t GpgFrontend::GpgFileOpera::SignFile(KeyArgsList&& keys, auto err = BasicOperator::GetInstance().Sign( std::move(keys), in_buffer, out_buffer, GPGME_SIG_MODE_DETACH, result); - assert(check_gpg_error_2_err_code(err) == GPG_ERR_NO_ERROR); - - write_buffer_to_file(path + ".sig", *out_buffer); + if (check_gpg_error_2_err_code(err) == GPG_ERR_NO_ERROR) + if (!write_buffer_to_file(path + ".sig", *out_buffer)) { + throw std::runtime_error("write_buffer_to_file error"); + }; return err; } @@ -103,29 +109,26 @@ gpgme_error_t GpgFrontend::GpgFileOpera::VerifyFile(const std::string& path, // TODO gpg_error_t GpgFrontend::GpgFileOpera::EncryptSignFile( - KeyArgsList&& keys, const std::string& path, GpgEncrResult& encr_res, - GpgSignResult& sign_res) { + KeyListPtr keys, KeyListPtr signer_keys, const std::string& path, + GpgEncrResult& encr_res, GpgSignResult& sign_res) { auto in_buffer = read_all_data_in_file(path); std::unique_ptr out_buffer = nullptr; - // TODO Fill the vector - std::vector signerKeys; - // TODO dealing with signer keys auto err = BasicOperator::GetInstance().EncryptSign( - std::move(keys), std::move(signerKeys), in_buffer, out_buffer, encr_res, + std::move(keys), std::move(signer_keys), in_buffer, out_buffer, encr_res, sign_res); auto out_path = path + ".gpg"; LOG(INFO) << "EncryptSignFile out_path" << out_path; LOG(INFO) << "EncryptSignFile out_buffer size" << out_buffer->size(); - bool result = write_buffer_to_file(out_path, *out_buffer); - LOG(INFO) << "EncryptSignFile write_buffer_to_file result" << result; - if (result) - return err; - else - throw std::runtime_error("write_buffer_to_file failed."); + 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"); + }; + + return err; } gpg_error_t GpgFrontend::GpgFileOpera::DecryptVerifyFile( @@ -149,9 +152,11 @@ gpg_error_t GpgFrontend::GpgFileOpera::DecryptVerifyFile( out_file_name = path + ".out"; LOG(INFO) << "GpgFrontend::GpgFileOpera::DecryptVerifyFile out_file_name" << out_file_name; - if (!write_buffer_to_file(out_file_name, *out_buffer)) { - throw std::runtime_error("write_buffer_to_file error"); - }; + + if (check_gpg_error_2_err_code(err) == GPG_ERR_NO_ERROR) + if (!write_buffer_to_file(out_file_name, *out_buffer)) { + throw std::runtime_error("write_buffer_to_file error"); + }; return err; } -- cgit v1.2.3