From 0459b43b649782d01827cfa0325dd4254a74cad5 Mon Sep 17 00:00:00 2001 From: Green Sky Date: Tue, 8 Aug 2023 20:13:10 +0200 Subject: [PATCH] highlevel sceleton --- src/CMakeLists.txt | 17 +++++++++++++++ src/hash_utils.cpp | 26 +++++++++++++++++++++++ src/hash_utils.hpp | 10 +++++++++ src/plugin_ngcft1.cpp | 20 ++++++++++++++++++ src/sha1_ngcft1.cpp | 49 +++++++++++++++++++++++++++++++++++++++++++ src/sha1_ngcft1.hpp | 30 ++++++++++++++++++++++++++ 6 files changed, 152 insertions(+) create mode 100644 src/hash_utils.cpp create mode 100644 src/hash_utils.hpp create mode 100644 src/sha1_ngcft1.cpp create mode 100644 src/sha1_ngcft1.hpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e08fe19..85dfd0a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -30,6 +30,22 @@ target_link_libraries(ngcft1 PUBLIC ######################################## +add_library(sha1_ngcft1 STATIC + ./hash_utils.hpp + ./hash_utils.cpp + + ./sha1_ngcft1.hpp + ./sha1_ngcft1.cpp +) +target_link_libraries(sha1_ngcft1 PUBLIC + ngcft1 + sha1::sha1 + solanaceae_tox_contacts + solanaceae_message3 +) + +######################################## + add_library(plugin_ngcft1 SHARED ./plugin_ngcft1.cpp ) @@ -38,6 +54,7 @@ target_link_libraries(plugin_ngcft1 PUBLIC solanaceae_plugin ngcext ngcft1 + sha1_ngcft1 ) ######################################## diff --git a/src/hash_utils.cpp b/src/hash_utils.cpp new file mode 100644 index 0000000..9039447 --- /dev/null +++ b/src/hash_utils.cpp @@ -0,0 +1,26 @@ +#include "./hash_utils.hpp" + +#include + +// returns the 20bytes sha1 hash +std::vector hash_sha1(const uint8_t* data, size_t size) { + SHA1_CTX ctx; + SHA1Init(&ctx); + + { // lib only takes uint32_t sizes, so chunk it + constexpr size_t hash_block_size {0xffffffff}; + size_t i = 0; + for (; i + hash_block_size < size; i += hash_block_size) { + SHA1Update(&ctx, reinterpret_cast(data) + i, hash_block_size); + } + + if (i < size) { + SHA1Update(&ctx, reinterpret_cast(data) + i, size - i); + } + } + + std::vector sha1_hash(20); + SHA1Final(sha1_hash.data(), &ctx); + return sha1_hash; +} + diff --git a/src/hash_utils.hpp b/src/hash_utils.hpp new file mode 100644 index 0000000..4de52ec --- /dev/null +++ b/src/hash_utils.hpp @@ -0,0 +1,10 @@ +#pragma once + +#include +#include + +// returns the 20bytes sha1 hash +std::vector hash_sha1(const uint8_t* data, size_t size); + +inline std::vector hash_sha1(const char* data, size_t size) { return hash_sha1(reinterpret_cast(data), size); } + diff --git a/src/plugin_ngcft1.cpp b/src/plugin_ngcft1.cpp index 8886269..f50faad 100644 --- a/src/plugin_ngcft1.cpp +++ b/src/plugin_ngcft1.cpp @@ -2,6 +2,7 @@ #include "./ngcext.hpp" #include "./ngcft1.hpp" +#include "./sha1_ngcft1.hpp" #include #include @@ -15,6 +16,7 @@ static std::unique_ptr g_ngcextep = nullptr; // TODO: make sep plug static std::unique_ptr g_ngcft1 = nullptr; +static std::unique_ptr g_sha1_ngcft1 = nullptr; extern "C" { @@ -35,10 +37,14 @@ SOLANA_PLUGIN_EXPORT uint32_t solana_plugin_start(struct SolanaAPI* solana_api) ToxI* tox_i = nullptr; ToxEventProviderI* tox_event_provider_i = nullptr; + RegistryMessageModel* rmm = nullptr; + ToxContactModel2* tcm = nullptr; { // make sure required types are loaded tox_i = RESOLVE_INSTANCE(ToxI); tox_event_provider_i = RESOLVE_INSTANCE(ToxEventProviderI); + rmm = RESOLVE_INSTANCE(RegistryMessageModel); + tcm = RESOLVE_INSTANCE(ToxContactModel2); if (tox_i == nullptr) { std::cerr << "PLUGIN NGCEXT missing ToxI\n"; @@ -49,12 +55,23 @@ SOLANA_PLUGIN_EXPORT uint32_t solana_plugin_start(struct SolanaAPI* solana_api) std::cerr << "PLUGIN NGCEXT missing ToxEventProviderI\n"; return 2; } + + if (rmm == nullptr) { + std::cerr << "PLUGIN NGCEXT missing RegistryMessageModel\n"; + return 2; + } + + if (tcm == nullptr) { + std::cerr << "PLUGIN NGCEXT missing ToxContactModel2\n"; + return 2; + } } // static store, could be anywhere tho // construct with fetched dependencies g_ngcextep = std::make_unique(*tox_event_provider_i); g_ngcft1 = std::make_unique(*tox_i, *tox_event_provider_i, *g_ngcextep.get()); + g_sha1_ngcft1 = std::make_unique(*rmm, *g_ngcft1.get(), *tcm); // register types PROVIDE_INSTANCE(NGCEXTEventProviderI, "NGCEXT", g_ngcextep.get()); @@ -62,12 +79,15 @@ SOLANA_PLUGIN_EXPORT uint32_t solana_plugin_start(struct SolanaAPI* solana_api) PROVIDE_INSTANCE(NGCFT1EventProviderI, "NGCEXT", g_ngcft1.get()); PROVIDE_INSTANCE(NGCFT1, "NGCEXT", g_ngcft1.get()); + PROVIDE_INSTANCE(SHA1_NGCFT1, "NGCEXT", g_sha1_ngcft1.get()); + return 0; } SOLANA_PLUGIN_EXPORT void solana_plugin_stop(void) { std::cout << "PLUGIN NGCEXT STOP()\n"; + g_sha1_ngcft1.reset(); g_ngcft1.reset(); g_ngcextep.reset(); } diff --git a/src/sha1_ngcft1.cpp b/src/sha1_ngcft1.cpp new file mode 100644 index 0000000..d0c6751 --- /dev/null +++ b/src/sha1_ngcft1.cpp @@ -0,0 +1,49 @@ +#include "./sha1_ngcft1.hpp" + +#include + +#include + +SHA1_NGCFT1::SHA1_NGCFT1( + RegistryMessageModel& rmm, + NGCFT1EventProviderI& nftep, + ToxContactModel2& tcm +) : + _rmm(rmm), + _nftep(nftep), + _tcm(tcm) +{ + _nftep.subscribe(this, NGCFT1_Event::recv_request); + _nftep.subscribe(this, NGCFT1_Event::recv_init); + _nftep.subscribe(this, NGCFT1_Event::recv_data); + _nftep.subscribe(this, NGCFT1_Event::send_data); +} + +bool SHA1_NGCFT1::onEvent(const Events::NGCFT1_recv_request& e) { + // only interested in sha1 + if (e.file_kind != NGCFT1_file_kind::HASH_SHA1_INFO && e.file_kind != NGCFT1_file_kind::HASH_SHA1_CHUNK) { + return false; + } + + //std::cout << "SHA1_NGCFT1: FT1_REQUEST fk:" << int(e.file_kind) << " [" << bin2hex({e.file_id, e.file_id+e.file_id_size}) << "]\n"; + + return false; +} + +bool SHA1_NGCFT1::onEvent(const Events::NGCFT1_recv_init& e) { + // only interested in sha1 + if (e.file_kind != NGCFT1_file_kind::HASH_SHA1_INFO && e.file_kind != NGCFT1_file_kind::HASH_SHA1_CHUNK) { + return false; + } + + return false; +} + +bool SHA1_NGCFT1::onEvent(const Events::NGCFT1_recv_data& e) { + return false; +} + +bool SHA1_NGCFT1::onEvent(const Events::NGCFT1_send_data& e) { + return false; +} + diff --git a/src/sha1_ngcft1.hpp b/src/sha1_ngcft1.hpp new file mode 100644 index 0000000..0fc0499 --- /dev/null +++ b/src/sha1_ngcft1.hpp @@ -0,0 +1,30 @@ +#pragma once + +// solanaceae port of sha1 fts for NGCFT1 + +#include +#include + +#include "./ngcft1.hpp" + +class SHA1_NGCFT1 : public RegistryMessageModelEventI, public NGCFT1EventI { + RegistryMessageModel& _rmm; + NGCFT1EventProviderI& _nftep; + ToxContactModel2& _tcm; + + public: + SHA1_NGCFT1( + RegistryMessageModel& rmm, + NGCFT1EventProviderI& nftep, + ToxContactModel2& tcm + ); + + //void iterate(float delta); + + protected: + bool onEvent(const Events::NGCFT1_recv_request&) override; + bool onEvent(const Events::NGCFT1_recv_init&) override; + bool onEvent(const Events::NGCFT1_recv_data&) override; + bool onEvent(const Events::NGCFT1_send_data&) override; // const? +}; +