From c6a600f7ca290a9a11313a682592f867128f5dc9 Mon Sep 17 00:00:00 2001 From: Amit Gupta Date: Wed, 17 Sep 2025 20:23:06 +0200 Subject: [PATCH 1/4] Base64 decoded string for the Metadata --- cpp/src/KIM_SharedLibrary.cpp | 39 ++++++++++++++++++++++++++++++++--- cpp/src/KIM_SharedLibrary.hpp | 9 ++++++++ 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/cpp/src/KIM_SharedLibrary.cpp b/cpp/src/KIM_SharedLibrary.cpp index 42943f9e..3e64d600 100644 --- a/cpp/src/KIM_SharedLibrary.cpp +++ b/cpp/src/KIM_SharedLibrary.cpp @@ -104,8 +104,41 @@ KIM::FILESYSTEM::Path PrivateGetORIGIN() namespace KIM { SharedLibrary::SharedLibrary::EmbeddedFile::EmbeddedFile() : - fileName(NULL), fileLength(0), filePointer(NULL) + fileName(NULL), fileLength(0), filePointer(NULL), decodedStringAvailable(false) { + decodedFileContent = ""; +} + +void SharedLibrary::SharedLibrary::EmbeddedFile::decodeFileInMemory() const +{ + if (decodedStringAvailable){ + return; + } + + if (fileLength > 0 && filePointer != NULL){ + base64::decoder decoder = base64::decoder(); + std::istringstream encodedString( + std::string(reinterpret_cast(filePointer), fileLength), + std::ios::in | std::ios::binary); + std::ostringstream decodedString(std::ios::out | std::ios::binary); + decoder.decode(encodedString, decodedString); + + decodedFileContent = decodedString.str(); + decodedStringAvailable = true; + } + // else {LOG} +} + +unsigned char const * SharedLibrary::SharedLibrary::EmbeddedFile::getDecodedFileDataPointer() const +{ + decodeFileInMemory(); + return decodedStringAvailable? reinterpret_cast(decodedFileContent.data()) : NULL; +} + +unsigned int SharedLibrary::SharedLibrary::EmbeddedFile::getDecodedFileDataLength() const +{ + decodeFileInMemory(); + return static_cast(decodedFileContent.length()); } SharedLibrary::SharedLibrary(Log * const log) : @@ -560,9 +593,9 @@ int SharedLibrary::GetMetadataFile( if (metadataFileName != NULL) *metadataFileName = (metadataFiles_[index]).fileName; if (metadataFileLength != NULL) - *metadataFileLength = (metadataFiles_[index]).fileLength; + *metadataFileLength = (metadataFiles_[index]).getDecodedFileDataLength(); if (metadataFileData != NULL) - *metadataFileData = (metadataFiles_[index]).filePointer; + *metadataFileData = (metadataFiles_[index]).getDecodedFileDataPointer(); LOG_DEBUG("Exit 0=" + callString); return false; diff --git a/cpp/src/KIM_SharedLibrary.hpp b/cpp/src/KIM_SharedLibrary.hpp index 7b56d025..1afda83e 100644 --- a/cpp/src/KIM_SharedLibrary.hpp +++ b/cpp/src/KIM_SharedLibrary.hpp @@ -115,8 +115,17 @@ class SharedLibrary char const * fileName; unsigned int fileLength; unsigned char const * filePointer; + mutable bool decodedStringAvailable; EmbeddedFile(); + + void decodeFileInMemory() const; + unsigned char const * getDecodedFileDataPointer() const; + unsigned int getDecodedFileDataLength() const; + + private: + mutable std::string decodedFileContent; + }; // struct EmbeddedFile static FILESYSTEM::Path const ORIGIN; From 23f07831cad36a336631f6dc7e6d962ed9b07bd0 Mon Sep 17 00:00:00 2001 From: "Ryan S. Elliott" Date: Thu, 18 Sep 2025 14:06:15 -0500 Subject: [PATCH 2/4] clang-format the changes so far. --- cpp/src/KIM_SharedLibrary.cpp | 33 +++++++++++++++++++++------------ cpp/src/KIM_SharedLibrary.hpp | 2 +- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/cpp/src/KIM_SharedLibrary.cpp b/cpp/src/KIM_SharedLibrary.cpp index 3e64d600..4722f762 100644 --- a/cpp/src/KIM_SharedLibrary.cpp +++ b/cpp/src/KIM_SharedLibrary.cpp @@ -104,38 +104,44 @@ KIM::FILESYSTEM::Path PrivateGetORIGIN() namespace KIM { SharedLibrary::SharedLibrary::EmbeddedFile::EmbeddedFile() : - fileName(NULL), fileLength(0), filePointer(NULL), decodedStringAvailable(false) + fileName(NULL), + fileLength(0), + filePointer(NULL), + decodedStringAvailable(false) { decodedFileContent = ""; } void SharedLibrary::SharedLibrary::EmbeddedFile::decodeFileInMemory() const { - if (decodedStringAvailable){ - return; - } + if (decodedStringAvailable) { return; } - if (fileLength > 0 && filePointer != NULL){ + if (fileLength > 0 && filePointer != NULL) + { base64::decoder decoder = base64::decoder(); std::istringstream encodedString( - std::string(reinterpret_cast(filePointer), fileLength), - std::ios::in | std::ios::binary); + std::string(reinterpret_cast(filePointer), fileLength), + std::ios::in | std::ios::binary); std::ostringstream decodedString(std::ios::out | std::ios::binary); decoder.decode(encodedString, decodedString); decodedFileContent = decodedString.str(); decodedStringAvailable = true; - } + } // else {LOG} } -unsigned char const * SharedLibrary::SharedLibrary::EmbeddedFile::getDecodedFileDataPointer() const +unsigned char const * +SharedLibrary::SharedLibrary::EmbeddedFile::getDecodedFileDataPointer() const { decodeFileInMemory(); - return decodedStringAvailable? reinterpret_cast(decodedFileContent.data()) : NULL; + return decodedStringAvailable ? reinterpret_cast( + decodedFileContent.data()) + : NULL; } -unsigned int SharedLibrary::SharedLibrary::EmbeddedFile::getDecodedFileDataLength() const +unsigned int +SharedLibrary::SharedLibrary::EmbeddedFile::getDecodedFileDataLength() const { decodeFileInMemory(); return static_cast(decodedFileContent.length()); @@ -421,7 +427,10 @@ int SharedLibrary::Close() LOG_DEBUG("Exit 1=" + callString); return true; } - else { sharedLibraryHandle_ = NULL; } + else + { + sharedLibraryHandle_ = NULL; + } LOG_DEBUG("Exit 0=" + callString); return false; diff --git a/cpp/src/KIM_SharedLibrary.hpp b/cpp/src/KIM_SharedLibrary.hpp index 1afda83e..188b2849 100644 --- a/cpp/src/KIM_SharedLibrary.hpp +++ b/cpp/src/KIM_SharedLibrary.hpp @@ -123,7 +123,7 @@ class SharedLibrary unsigned char const * getDecodedFileDataPointer() const; unsigned int getDecodedFileDataLength() const; - private: + private: mutable std::string decodedFileContent; }; // struct EmbeddedFile From 3a179f186edfe5b36a79af4f816646b8358eebbb Mon Sep 17 00:00:00 2001 From: "Ryan S. Elliott" Date: Thu, 18 Sep 2025 14:33:09 -0500 Subject: [PATCH 3/4] refactor in memory decoded file implementation --- cpp/src/KIM_SharedLibrary.cpp | 16 ++++++---------- cpp/src/KIM_SharedLibrary.hpp | 3 +-- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/cpp/src/KIM_SharedLibrary.cpp b/cpp/src/KIM_SharedLibrary.cpp index 4722f762..2f95766d 100644 --- a/cpp/src/KIM_SharedLibrary.cpp +++ b/cpp/src/KIM_SharedLibrary.cpp @@ -104,17 +104,14 @@ KIM::FILESYSTEM::Path PrivateGetORIGIN() namespace KIM { SharedLibrary::SharedLibrary::EmbeddedFile::EmbeddedFile() : - fileName(NULL), - fileLength(0), - filePointer(NULL), - decodedStringAvailable(false) + fileName(NULL), fileLength(0), filePointer(NULL), decodedFileContent("") { - decodedFileContent = ""; } void SharedLibrary::SharedLibrary::EmbeddedFile::decodeFileInMemory() const { - if (decodedStringAvailable) { return; } + // empty files are not added to the library, so use empty() as flag + if (!decodedString.empty()) { return; } if (fileLength > 0 && filePointer != NULL) { @@ -126,7 +123,6 @@ void SharedLibrary::SharedLibrary::EmbeddedFile::decodeFileInMemory() const decoder.decode(encodedString, decodedString); decodedFileContent = decodedString.str(); - decodedStringAvailable = true; } // else {LOG} } @@ -135,9 +131,9 @@ unsigned char const * SharedLibrary::SharedLibrary::EmbeddedFile::getDecodedFileDataPointer() const { decodeFileInMemory(); - return decodedStringAvailable ? reinterpret_cast( - decodedFileContent.data()) - : NULL; + return decodedString.empty() ? NULL + : reinterpret_cast( + decodedFileContent.data()); } unsigned int diff --git a/cpp/src/KIM_SharedLibrary.hpp b/cpp/src/KIM_SharedLibrary.hpp index 188b2849..ba8ea055 100644 --- a/cpp/src/KIM_SharedLibrary.hpp +++ b/cpp/src/KIM_SharedLibrary.hpp @@ -115,17 +115,16 @@ class SharedLibrary char const * fileName; unsigned int fileLength; unsigned char const * filePointer; - mutable bool decodedStringAvailable; EmbeddedFile(); - void decodeFileInMemory() const; unsigned char const * getDecodedFileDataPointer() const; unsigned int getDecodedFileDataLength() const; private: mutable std::string decodedFileContent; + void decodeFileInMemory() const; }; // struct EmbeddedFile static FILESYSTEM::Path const ORIGIN; From 75d66842148878f866d9bd30c3e878a79099dfb0 Mon Sep 17 00:00:00 2001 From: "Ryan S. Elliott" Date: Thu, 18 Sep 2025 15:35:22 -0500 Subject: [PATCH 4/4] Fixup variable name --- cpp/src/KIM_SharedLibrary.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cpp/src/KIM_SharedLibrary.cpp b/cpp/src/KIM_SharedLibrary.cpp index 2f95766d..ac36696d 100644 --- a/cpp/src/KIM_SharedLibrary.cpp +++ b/cpp/src/KIM_SharedLibrary.cpp @@ -111,7 +111,7 @@ SharedLibrary::SharedLibrary::EmbeddedFile::EmbeddedFile() : void SharedLibrary::SharedLibrary::EmbeddedFile::decodeFileInMemory() const { // empty files are not added to the library, so use empty() as flag - if (!decodedString.empty()) { return; } + if (!decodedFileContent.empty()) { return; } if (fileLength > 0 && filePointer != NULL) { @@ -131,9 +131,9 @@ unsigned char const * SharedLibrary::SharedLibrary::EmbeddedFile::getDecodedFileDataPointer() const { decodeFileInMemory(); - return decodedString.empty() ? NULL - : reinterpret_cast( - decodedFileContent.data()); + return decodedFileContent.empty() ? NULL + : reinterpret_cast( + decodedFileContent.data()); } unsigned int