lib/javacompat: implement exportJavaString

BodgeMaster-unfinished
Milan Suman 2022-07-20 10:38:55 +05:30
parent 0462756451
commit 2f38636a27
3 changed files with 66 additions and 7 deletions

View File

@ -16,8 +16,18 @@
#include <tinyutf8/tinyutf8.h> #include <tinyutf8/tinyutf8.h>
#include <string> #include <string>
#include "error.h++" #include "error.h++"
#include "javacompat.h++" #include "javacompat.h++"
#include "../../.endianness"
#ifdef FOSSVG_ENDIAN_BIG_WORD
#error "Honeywell-316-style endianness is not supported. If you feel like it should, feel free to participate in the project to maintain it."
#endif
#ifdef FOSSVG_ENDIAN_LITTLE_WORD
#error "PDP-11-style endianness is not supported. If you feel like it should, feel free to participate in the project to maintain it."
#endif
#ifdef FOSSVG_ENDIAN_UNKNOWN
#error "The endianness of your system could not be determined. Please set it manually. FOSS-VG is currently implemented using some endian-specific functions."
#endif
namespace JavaCompat { namespace JavaCompat {
//FIXME: contrary to what I said, we need to explicitly pass the data //FIXME: contrary to what I said, we need to explicitly pass the data
@ -37,9 +47,42 @@ namespace JavaCompat {
} }
return tiny_utf8::string(stdString); return tiny_utf8::string(stdString);
} }
/*
ErrorOr<uint8_t*> exportJavaString(tiny_utf8::string data) { ErrorOr<std::vector<uint8_t>> exportJavaString(tiny_utf8::string data) {
return ErrorOr(nullptr); uint16_t* size = new uint16_t;
uint8_t* sizeBytes = reinterpret_cast<uint8_t*>(size);
std::vector<uint8_t> output = std::vector<uint8_t>();
std::string stdString = data.cpp_str();
*size = (uint16_t) stdString.size();
//placeholder size bytes
output.push_back(0x00);
output.push_back(0x00);
for(int i=0; i<stdString.size(); i++){
if((uint8_t) stdString[i] == 0x00){
*size += 1;
output.push_back(0xc0);
output.push_back(0x80);
continue;
} }
*/ output.push_back(stdString[i]);
}
#ifdef FOSSVG_BIG_ENDIAN
output[0] = *sizeBytes;
output[1] = *(sizeBytes+1);
#else
#ifdef FOSSVG_LITTLE_ENDIAN
output[0] = *(sizeBytes+1);
output[1] = *sizeBytes;
#else
#error "NBT::helper::writeInt16: An implementation for your endianness is unavailable."
#endif
#endif
return ErrorOr(output);
}
} }

View File

@ -15,10 +15,11 @@
#include <tinyutf8/tinyutf8.h> #include <tinyutf8/tinyutf8.h>
#include "error.h++" #include "error.h++"
#include <vector>
namespace JavaCompat { namespace JavaCompat {
//FIXME: contrary to what I said, we need to explicitly pass the data //FIXME: contrary to what I said, we need to explicitly pass the data
// size because files could have been tampered with or corrupted // size because files could have been tampered with or corrupted
tiny_utf8::string importJavaString(uint8_t data[]); tiny_utf8::string importJavaString(uint8_t data[]);
ErrorOr<uint8_t*> exportJavaString(tiny_utf8::string data); ErrorOr<std::vector<uint8_t>> exportJavaString(tiny_utf8::string data);
} }

View File

@ -28,7 +28,7 @@ int main(){
// java-style_unicode: 119 bytes, 85 characters ?? // java-style_unicode: 119 bytes, 85 characters ??
std::cout << "################################################################################" << std::endl; std::cout << "################################################################################" << std::endl;
std::cout << "Java String Tests" << std::endl; std::cout << "Import String Tests" << std::endl;
std::cout << "################################################################################" << std::endl; std::cout << "################################################################################" << std::endl;
char* nextChar = new char; char* nextChar = new char;
@ -78,6 +78,21 @@ int main(){
ASSERT(normalString == importedString); ASSERT(normalString == importedString);
std::cout << "Passed Import Java string test." << std::endl; std::cout << "Passed Import Java string test." << std::endl;
std::cout << "################################################################################" << std::endl;
std::cout << "Export String Tests" << std::endl;
std::cout << "################################################################################" << std::endl;
//using normalString from when we read the file earlier
//there's no need to read the same file twice
std::vector<uint8_t> data = JavaCompat::exportJavaString(normalString).value;
std::string exportedString = std::string();
for(int i=0; i<data.size(); i++){
exportedString.push_back(data[i]);
}
ASSERT(exportedString == javaStdString);
std::cout << "Passed Export Java string test." << std::endl;
return 0; return 0;
} }