diff --git a/scripts/test.sh b/scripts/test.sh index bbfde42..cc3aeb8 100755 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -39,6 +39,7 @@ echo "Building tests..." COMPILE_COMMANDS=( "$CXX_WITH_FLAGS src/test/nbt_helpers.cpp -Lbin/lib -l:nbt.so -o bin/test/nbt_helpers" "$CXX_WITH_FLAGS src/test/cli_argument_parser.cpp -Lbin/lib -l:cli.so -o bin/test/cli_argument_parser" + "$CXX_WITH_FLAGS src/test/javacompat.cpp -Idependencies/tiny-utf8-4.4.3/include -Lbin/lib -l:javacompat.so -o bin/test/javacompat" ) for command in ${!COMPILE_COMMANDS[@]}; do echo "${COMPILE_COMMANDS[command]}" diff --git a/src/lib/java_string.cpp b/src/lib/java_string.cpp deleted file mode 100644 index 527de7f..0000000 --- a/src/lib/java_string.cpp +++ /dev/null @@ -1,30 +0,0 @@ -//Copyright 2022, FOSS-VG Developers and Contributers -// -//This program is free software: you can redistribute it and/or modify it -//under the terms of the GNU Affero General Public License as published -//by the Free Software Foundation, version 3. -// -//This program is distributed in the hope that it will be useful, -//but WITHOUT ANY WARRANTY; without even the implied -//warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. -//See the GNU Affero General Public License for more details. -// -//You should have received a copy of the GNU Affero General Public License -//version 3 along with this program. -//If not, see https://www.gnu.org/licenses/agpl-3.0.en.html - -#include -#include "error.h++" - -#include "java_string.h++" - -namespace JavaCompat { - ErrorOr importJavaFormatToString(uint8_t data[]) { - tiny_utf8::string output; - // do magic - return ErrorOr(output); - } - ErrorOr exportStringToJavaFormat(tiny_utf8::string data) { - return ErrorOr(nullptr); - } -} diff --git a/src/lib/javacompat.cpp b/src/lib/javacompat.cpp new file mode 100644 index 0000000..b60bc88 --- /dev/null +++ b/src/lib/javacompat.cpp @@ -0,0 +1,88 @@ +//Copyright 2022, FOSS-VG Developers and Contributers +// +//This program is free software: you can redistribute it and/or modify it +//under the terms of the GNU Affero General Public License as published +//by the Free Software Foundation, version 3. +// +//This program is distributed in the hope that it will be useful, +//but WITHOUT ANY WARRANTY; without even the implied +//warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +//See the GNU Affero General Public License for more details. +// +//You should have received a copy of the GNU Affero General Public License +//version 3 along with this program. +//If not, see https://www.gnu.org/licenses/agpl-3.0.en.html + +#include +#include +#include "error.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 { + //FIXME: contrary to what I said, we need to explicitly pass the data + // size because files could have been tampered with or corrupted + tiny_utf8::string importJavaString(uint8_t data[]) { + std::string stdString; + uint16_t size = static_cast(data[0])<<8 | static_cast(data[1]); + + for(uint8_t i=2; i> exportJavaString(tiny_utf8::string data) { + uint16_t* size = new uint16_t; + uint8_t* sizeBytes = reinterpret_cast(size); + std::vector output = std::vector(); + 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 #include "error.h++" +#include namespace JavaCompat { - ErrorOr importJavaFormatToString(uint8_t data[]); - ErrorOr exportStringToJavaFormat(tiny_utf8::string data); + //FIXME: contrary to what I said, we need to explicitly pass the data + // size because files could have been tampered with or corrupted + tiny_utf8::string importJavaString(uint8_t data[]); + ErrorOr> exportJavaString(tiny_utf8::string data); } diff --git a/src/test/javacompat.cpp b/src/test/javacompat.cpp new file mode 100644 index 0000000..49b1154 --- /dev/null +++ b/src/test/javacompat.cpp @@ -0,0 +1,98 @@ +// Copyright 2022, FOSS-VG Developers and Contributers +// +// This program is free software: you can redistribute it and/or modify it +// under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, version 3. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied +// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +// See the GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// version 3 along with this program. +// If not, see https://www.gnu.org/licenses/agpl-3.0.en.html + +#include "assert.h++" +#include "../lib/javacompat.h++" +#include +#include +#include +#include + + +int main(){ + + // metadata for things in resources/unicode_data/ + // normal_utf-8: 116 bytes, 85 characters ?? + // java-style_unicode: 119 bytes, 85 characters ?? + + std::cout << "################################################################################" << std::endl; + std::cout << "Import String Tests" << std::endl; + std::cout << "################################################################################" << std::endl; + + char* nextChar = new char; + + std::streampos javaSize; + std::string javaStdString; + const char* javaFilePath = "./resources/unicode_data/java-style_unicode"; + std::ifstream javaFile(javaFilePath, std::ios::in | std::ios::binary | std::ios::ate); + if(javaFile.is_open()){ + javaSize = javaFile.tellg(); + javaFile.seekg(0, std::ios::beg); + + for (int i=0; i(javaStdString.data())); + + std::streampos normalSize; + std::string normalStdString; + const char* normalFilePath = "./resources/unicode_data/normal_utf-8"; + std::ifstream normalFile(normalFilePath, std::ios::in | std::ios::binary | std::ios::ate); + if(normalFile.is_open()){ + normalSize = normalFile.tellg(); + normalFile.seekg(0, std::ios::beg); + + for (int i=0; i data = JavaCompat::exportJavaString(normalString).value; + std::string exportedString = std::string(); + for(int i=0; i