Make path api more understandable, breaks certain code and have makeabsolute and makerelative

This commit is contained in:
2025-04-19 10:24:24 -05:00
parent 3fcd7f81d6
commit 4ec4011b46
8 changed files with 161 additions and 4 deletions

View File

@ -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)

View File

@ -53,4 +53,14 @@ namespace Tesses::Framework::Crypto
static std::vector<uint8_t> ComputeHash(Tesses::Framework::Streams::Stream* strm,bool is384=false);
static std::vector<uint8_t> 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<uint8_t>& output,std::string pass, std::vector<uint8_t>& salt, long itterations, ShaVersion version);
bool RandomBytes(std::vector<uint8_t>& output, std::string personal_str);
}

View File

@ -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;
}

View File

@ -9,7 +9,7 @@ namespace Tesses::Framework::Filesystem
{
class VFSPath {
public:
static VFSPath RelativeCurrentDirectory();
static VFSPath CurrentDirectoryAsRelative();
bool relative;
static std::vector<std::string> SplitPath(std::string path);
std::vector<std::string> 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);

View File

@ -5,6 +5,10 @@
#include <mbedtls/sha256.h>
#include <mbedtls/sha512.h>
#include <mbedtls/base64.h>
#include <mbedtls/pkcs5.h>
#include <mbedtls/entropy.h>
#include <mbedtls/ctr_drbg.h>
#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<uint8_t>& output,std::string pass, std::vector<uint8_t>& 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<uint8_t>& 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;
}
}

View File

@ -188,6 +188,7 @@ namespace Tesses::Framework::Filesystem
delete dir;
});
}
LocalFilesystem LocalFS;
}
// C:/Users/Jim/Joel

View File

@ -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<std::string> 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<std::string> 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;

View File

@ -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));