From 4ec4011b469d7ec84ebf7d859db5d6ddd644da9f Mon Sep 17 00:00:00 2001 From: Mike Nolan Date: Sat, 19 Apr 2025 10:24:24 -0500 Subject: [PATCH] Make path api more understandable, breaks certain code and have makeabsolute and makerelative --- CMakeLists.txt | 3 + .../TessesFramework/Crypto/MbedHelpers.hpp | 10 +++ .../TessesFramework/Filesystem/LocalFS.hpp | 1 + include/TessesFramework/Filesystem/VFS.hpp | 10 ++- src/Crypto/MbedHelpers.cpp | 75 +++++++++++++++++++ src/Filesystem/LocalFS.cpp | 1 + src/Filesystem/VFS.cpp | 61 ++++++++++++++- src/Serialization/Json.cpp | 4 +- 8 files changed, 161 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9a544bd..ceda1a4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -184,6 +184,9 @@ if(TESSESFRAMEWORK_ENABLE_EXAMPLES) add_executable(safesubpath examples/safesubpath.cpp) target_link_libraries(safesubpath PUBLIC tessesframework) + add_executable(pathtest examples/pathtest.cpp) + target_link_libraries(pathtest PUBLIC tessesframework) + add_executable(mountabletest examples/mountabletest.cpp) target_link_libraries(mountabletest PUBLIC tessesframework) diff --git a/include/TessesFramework/Crypto/MbedHelpers.hpp b/include/TessesFramework/Crypto/MbedHelpers.hpp index e2fd44d..3d16cb0 100644 --- a/include/TessesFramework/Crypto/MbedHelpers.hpp +++ b/include/TessesFramework/Crypto/MbedHelpers.hpp @@ -53,4 +53,14 @@ namespace Tesses::Framework::Crypto static std::vector ComputeHash(Tesses::Framework::Streams::Stream* strm,bool is384=false); static std::vector ComputeHash(Tesses::Framework::Streams::Stream& strm,bool is384=false); }; + typedef enum { + VERSION_SHA1=1, + VERSION_SHA224=224, + VERSION_SHA256=256, + VERSION_SHA384=384, + VERSION_SHA512=512 + } ShaVersion; + bool PBKDF2(std::vector& output,std::string pass, std::vector& salt, long itterations, ShaVersion version); + + bool RandomBytes(std::vector& output, std::string personal_str); } \ No newline at end of file diff --git a/include/TessesFramework/Filesystem/LocalFS.hpp b/include/TessesFramework/Filesystem/LocalFS.hpp index b98fcd3..e6227d8 100644 --- a/include/TessesFramework/Filesystem/LocalFS.hpp +++ b/include/TessesFramework/Filesystem/LocalFS.hpp @@ -34,4 +34,5 @@ namespace Tesses::Framework::Filesystem void GetDate(VFSPath path, time_t& lastWrite, time_t& lastAccess); void SetDate(VFSPath path, time_t lastWrite, time_t lastAccess); }; + extern LocalFilesystem LocalFS; } \ No newline at end of file diff --git a/include/TessesFramework/Filesystem/VFS.hpp b/include/TessesFramework/Filesystem/VFS.hpp index 1485156..96e8887 100644 --- a/include/TessesFramework/Filesystem/VFS.hpp +++ b/include/TessesFramework/Filesystem/VFS.hpp @@ -9,7 +9,7 @@ namespace Tesses::Framework::Filesystem { class VFSPath { public: - static VFSPath RelativeCurrentDirectory(); + static VFSPath CurrentDirectoryAsRelative(); bool relative; static std::vector SplitPath(std::string path); std::vector path; @@ -29,6 +29,14 @@ namespace Tesses::Framework::Filesystem void ChangeExtension(std::string ext); void RemoveExtension(); std::string ToString(); + + static VFSPath GetAbsoluteCurrentDirectory(); + static void SetAbsoluteCurrentDirectory(VFSPath path); + VFSPath MakeAbsolute(); + + VFSPath MakeAbsolute(VFSPath curDir); + VFSPath MakeRelative(); + VFSPath MakeRelative(VFSPath toMakeRelativeTo); }; VFSPath operator/(VFSPath p, VFSPath p2); VFSPath operator/(VFSPath p, std::string p2); diff --git a/src/Crypto/MbedHelpers.cpp b/src/Crypto/MbedHelpers.cpp index 6bcf5fc..8e8dc2b 100644 --- a/src/Crypto/MbedHelpers.cpp +++ b/src/Crypto/MbedHelpers.cpp @@ -5,6 +5,10 @@ #include #include #include + +#include +#include +#include #endif namespace Tesses::Framework::Crypto { @@ -320,4 +324,75 @@ namespace Tesses::Framework::Crypto if(!sha512.Update(strm)) return {}; return sha512.Finish(); } + + + bool PBKDF2(std::vector& output,std::string pass, std::vector& salt, long itterations, ShaVersion version) + { + #if defined(TESSESFRAMEWORK_ENABLE_MBED) + + mbedtls_md_context_t ctx; + mbedtls_md_init(&ctx); + const mbedtls_md_info_t* info = NULL; + switch(version) + { + case ShaVersion::VERSION_SHA1: + info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA1); + break; + case ShaVersion::VERSION_SHA224: + info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA224); + break; + case ShaVersion::VERSION_SHA256: + info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA256); + break; + default: + case ShaVersion::VERSION_SHA384: + info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA384); + break; + case ShaVersion::VERSION_SHA512: + info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA512); + break; + } + + + mbedtls_md_setup(&ctx, info, 1); + + + + if(mbedtls_pkcs5_pbkdf2_hmac(&ctx, (const unsigned char*)pass.c_str(), pass.size(), salt.data(), salt.size(), (int)itterations,(uint32_t)output.size(),output.data()) == 0) + { + mbedtls_md_free(&ctx); + return true; + } + mbedtls_md_free(&ctx); + return false; + #else + return false; + #endif + } + bool RandomBytes(std::vector& output, std::string personal_str) + { + mbedtls_entropy_context entropy; + mbedtls_ctr_drbg_context ctr_drbg; + + mbedtls_entropy_init(&entropy); + mbedtls_ctr_drbg_init(&ctr_drbg); + + int ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, (const unsigned char *) personal_str.c_str(), personal_str.size()); + if(ret != 0) + { + mbedtls_ctr_drbg_free(&ctr_drbg); + mbedtls_entropy_free(&entropy); + return false; + } + ret = mbedtls_ctr_drbg_random(&ctr_drbg, output.data(),output.size()); + if (ret != 0) + { + mbedtls_ctr_drbg_free(&ctr_drbg); + mbedtls_entropy_free(&entropy); + return false; + } + mbedtls_ctr_drbg_free(&ctr_drbg); + mbedtls_entropy_free(&entropy); + return true; + } } diff --git a/src/Filesystem/LocalFS.cpp b/src/Filesystem/LocalFS.cpp index 238e085..acc3779 100644 --- a/src/Filesystem/LocalFS.cpp +++ b/src/Filesystem/LocalFS.cpp @@ -188,6 +188,7 @@ namespace Tesses::Framework::Filesystem delete dir; }); } + LocalFilesystem LocalFS; } // C:/Users/Jim/Joel \ No newline at end of file diff --git a/src/Filesystem/VFS.cpp b/src/Filesystem/VFS.cpp index 29f7604..9f71dcc 100644 --- a/src/Filesystem/VFS.cpp +++ b/src/Filesystem/VFS.cpp @@ -1,5 +1,6 @@ #include "TessesFramework/Filesystem/VFS.hpp" #include "TessesFramework/Http/HttpUtils.hpp" +#include "TessesFramework/Filesystem/LocalFS.hpp" namespace Tesses::Framework::Filesystem { VFSPathEnumeratorItterator::VFSPathEnumeratorItterator() @@ -190,6 +191,64 @@ namespace Tesses::Framework::Filesystem { return VFSPath("/"); } + VFSPath VFSPath::GetAbsoluteCurrentDirectory() + { + auto p = std::filesystem::current_path(); + return LocalFS.SystemToVFSPath(p.string()); + } + void VFSPath::SetAbsoluteCurrentDirectory(VFSPath path) + { + auto res = LocalFS.VFSPathToSystem(path); + std::filesystem::path mpath=res; + std::filesystem::current_path(mpath); + } + VFSPath VFSPath::MakeAbsolute() + { + return MakeAbsolute(GetAbsoluteCurrentDirectory()); + } + VFSPath VFSPath::MakeAbsolute(VFSPath curDir) + { + VFSPath p2 = curDir / *this; + return p2.CollapseRelativeParents(); + } + VFSPath VFSPath::MakeRelative() + { + return MakeRelative(GetAbsoluteCurrentDirectory()); + } + VFSPath VFSPath::MakeRelative(VFSPath toMakeRelativeTo) + { + + if(this->relative) return *this; + + + + size_t i; + size_t len = std::min(toMakeRelativeTo.path.size(),this->path.size()); + for(i = 0; i < len; i++) + { + if(this->path[i] != toMakeRelativeTo.path[i]) break; + } + + if(i == this->path.size()-1 && i == toMakeRelativeTo.path.size()-1) + { + VFSPath path({this->path[this->path.size()-1]}); + path.relative = true; + return path; + } + + std::vector parts(this->path.begin()+i, this->path.end()); + + if(i < toMakeRelativeTo.path.size()) + { + for(; i < toMakeRelativeTo.path.size();i++) + { + parts.insert(parts.begin(),".."); + } + } + VFSPath p2(parts); + p2.relative = true; + return p2; + } VFSPath VFSPath::CollapseRelativeParents() { std::vector parts; @@ -217,7 +276,7 @@ namespace Tesses::Framework::Filesystem newpath.path = parts; return newpath; } - VFSPath VFSPath::RelativeCurrentDirectory() + VFSPath VFSPath::CurrentDirectoryAsRelative() { VFSPath path; path.relative=true; diff --git a/src/Serialization/Json.cpp b/src/Serialization/Json.cpp index 5d0206d..bce661d 100644 --- a/src/Serialization/Json.cpp +++ b/src/Serialization/Json.cpp @@ -459,7 +459,7 @@ namespace Tesses::Framework::Serialization::Json } else if((c >= 0 && c < 32) || c == 127 ) { - str2.append("\\\\u00"); + str2.append("\\u00"); uint8_t c2 = (uint8_t)c; str2.push_back(HttpUtils::NibbleToHex((c2 >> 4) & 0x0F)); @@ -524,4 +524,4 @@ namespace Tesses::Framework::Serialization::Json return ""; } -} \ No newline at end of file +}