diff --git a/cpp/src/KIM_SharedLibrary.cpp b/cpp/src/KIM_SharedLibrary.cpp index 42943f9e..ac36696d 100644 --- a/cpp/src/KIM_SharedLibrary.cpp +++ b/cpp/src/KIM_SharedLibrary.cpp @@ -104,10 +104,45 @@ KIM::FILESYSTEM::Path PrivateGetORIGIN() namespace KIM { SharedLibrary::SharedLibrary::EmbeddedFile::EmbeddedFile() : - fileName(NULL), fileLength(0), filePointer(NULL) + fileName(NULL), fileLength(0), filePointer(NULL), decodedFileContent("") { } +void SharedLibrary::SharedLibrary::EmbeddedFile::decodeFileInMemory() const +{ + // empty files are not added to the library, so use empty() as flag + if (!decodedFileContent.empty()) { 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(); + } + // else {LOG} +} + +unsigned char const * +SharedLibrary::SharedLibrary::EmbeddedFile::getDecodedFileDataPointer() const +{ + decodeFileInMemory(); + return decodedFileContent.empty() ? NULL + : reinterpret_cast( + decodedFileContent.data()); +} + +unsigned int +SharedLibrary::SharedLibrary::EmbeddedFile::getDecodedFileDataLength() const +{ + decodeFileInMemory(); + return static_cast(decodedFileContent.length()); +} + SharedLibrary::SharedLibrary(Log * const log) : sharedLibraryHandle_(NULL), sharedLibrarySchemaVersion_(NULL), @@ -388,7 +423,10 @@ int SharedLibrary::Close() LOG_DEBUG("Exit 1=" + callString); return true; } - else { sharedLibraryHandle_ = NULL; } + else + { + sharedLibraryHandle_ = NULL; + } LOG_DEBUG("Exit 0=" + callString); return false; @@ -560,9 +598,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..ba8ea055 100644 --- a/cpp/src/KIM_SharedLibrary.hpp +++ b/cpp/src/KIM_SharedLibrary.hpp @@ -117,6 +117,14 @@ class SharedLibrary unsigned char const * filePointer; EmbeddedFile(); + + unsigned char const * getDecodedFileDataPointer() const; + unsigned int getDecodedFileDataLength() const; + + private: + mutable std::string decodedFileContent; + + void decodeFileInMemory() const; }; // struct EmbeddedFile static FILESYSTEM::Path const ORIGIN;