From 42336c560a751297dfe5bb83f95b03a22ef2ac80 Mon Sep 17 00:00:00 2001 From: Milan Suman <> Date: Mon, 18 Jul 2022 12:13:10 +0530 Subject: [PATCH 1/7] Birthed an abomination, what the fuck --- scripts/test.sh | 1 + src/lib/java_string.cpp | 22 ++++++++++--- src/lib/java_string.h++ | 2 +- src/test/java_string.cpp | 70 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 90 insertions(+), 5 deletions(-) create mode 100644 src/test/java_string.cpp diff --git a/scripts/test.sh b/scripts/test.sh index bbfde42..5248526 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/java_string.cpp -Idependencies/tiny-utf8-4.4.3/include -Lbin/lib -l:java_string.so -o bin/test/java_string" ) for command in ${!COMPILE_COMMANDS[@]}; do echo "${COMPILE_COMMANDS[command]}" diff --git a/src/lib/java_string.cpp b/src/lib/java_string.cpp index 527de7f..5b4a2f0 100644 --- a/src/lib/java_string.cpp +++ b/src/lib/java_string.cpp @@ -14,17 +14,31 @@ //If not, see https://www.gnu.org/licenses/agpl-3.0.en.html #include +#include #include "error.h++" #include "java_string.h++" namespace JavaCompat { - ErrorOr importJavaFormatToString(uint8_t data[]) { - tiny_utf8::string output; - // do magic - return ErrorOr(output); + tiny_utf8::string importJavaFormatToString(uint8_t data[]) { + std::vector output; + tiny_utf8::string outputString; + uint16_t size = static_cast(data[0])<<8 | static_cast(data[1]); + + for(uint8_t i=2; i exportStringToJavaFormat(tiny_utf8::string data) { return ErrorOr(nullptr); } + */ } diff --git a/src/lib/java_string.h++ b/src/lib/java_string.h++ index a50c37b..b57f56b 100644 --- a/src/lib/java_string.h++ +++ b/src/lib/java_string.h++ @@ -17,6 +17,6 @@ #include "error.h++" namespace JavaCompat { - ErrorOr importJavaFormatToString(uint8_t data[]); + tiny_utf8::string importJavaFormatToString(uint8_t data[]); ErrorOr exportStringToJavaFormat(tiny_utf8::string data); } diff --git a/src/test/java_string.cpp b/src/test/java_string.cpp new file mode 100644 index 0000000..563fab7 --- /dev/null +++ b/src/test/java_string.cpp @@ -0,0 +1,70 @@ +// 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/java_string.h++" +#include +#include +#include +#include + + +int main(){ + + std::cout << "################################################################################" << std::endl; + std::cout << "Java String Tests" << std::endl; + std::cout << "################################################################################" << std::endl; + + std::streampos javaSize; + char* javaData; + std::ifstream javaFile("./resources/unicode_data/java-style_unicode", std::ios::in | std::ios::binary | std::ios::ate); + if(javaFile.is_open()){ + javaSize = javaFile.tellg(); + javaData = new char[javaSize]; + javaFile.seekg(0, std::ios::beg); + javaFile.read(javaData, javaSize); + javaFile.close(); + } + + tiny_utf8::string importString = JavaCompat::importJavaFormatToString((uint8_t*) javaData); + + std::streampos normalSize; + char* normalData; + std::ifstream normalFile("./resources/unicode_data/normal_utf-8", std::ios::in | std::ios::binary | std::ios::ate); + if(normalFile.is_open()){ + normalSize = normalFile.tellg(); + normalData = new char[normalSize]; + normalFile.seekg(0, std::ios::beg); + normalFile.read(normalData, normalSize); + normalFile.close(); + } + + std::vector normalDataVector = std::vector(); + for(int i=0; i Date: Wed, 20 Jul 2022 03:57:20 +0200 Subject: [PATCH 2/7] lib/java_string: rename functions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit JavaCompat::importJavaFormatToString() → JavaCompat::importJavaString() JavaCompat::exportStringToJavaFormat() → JavaCompat::exportJavaString() --- src/lib/java_string.cpp | 6 ++++-- src/lib/java_string.h++ | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/lib/java_string.cpp b/src/lib/java_string.cpp index 5b4a2f0..a96fe9d 100644 --- a/src/lib/java_string.cpp +++ b/src/lib/java_string.cpp @@ -20,7 +20,9 @@ #include "java_string.h++" namespace JavaCompat { - tiny_utf8::string importJavaFormatToString(uint8_t 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[]) { std::vector output; tiny_utf8::string outputString; uint16_t size = static_cast(data[0])<<8 | static_cast(data[1]); @@ -37,7 +39,7 @@ namespace JavaCompat { return tiny_utf8::string(output.begin(), output.end()); } /* - ErrorOr exportStringToJavaFormat(tiny_utf8::string data) { + ErrorOr exportJavaString(tiny_utf8::string data) { return ErrorOr(nullptr); } */ diff --git a/src/lib/java_string.h++ b/src/lib/java_string.h++ index b57f56b..24be799 100644 --- a/src/lib/java_string.h++ +++ b/src/lib/java_string.h++ @@ -17,6 +17,8 @@ #include "error.h++" namespace JavaCompat { - tiny_utf8::string 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); } From 34e30c0bd48590ee42a47d9d831f2f99120be24a Mon Sep 17 00:00:00 2001 From: BodgeMaster <> Date: Wed, 20 Jul 2022 04:02:44 +0200 Subject: [PATCH 3/7] test/java_string: replace tabs with spaces, fix how tiny_utf8::string is being instantiated --- src/test/java_string.cpp | 75 +++++++++++++++++++++++----------------- 1 file changed, 44 insertions(+), 31 deletions(-) diff --git a/src/test/java_string.cpp b/src/test/java_string.cpp index 563fab7..5e7cef5 100644 --- a/src/test/java_string.cpp +++ b/src/test/java_string.cpp @@ -23,48 +23,61 @@ 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 << "Java String Tests" << std::endl; std::cout << "################################################################################" << std::endl; - std::streampos javaSize; - char* javaData; - std::ifstream javaFile("./resources/unicode_data/java-style_unicode", std::ios::in | std::ios::binary | std::ios::ate); - if(javaFile.is_open()){ - javaSize = javaFile.tellg(); - javaData = new char[javaSize]; - javaFile.seekg(0, std::ios::beg); - javaFile.read(javaData, javaSize); - javaFile.close(); - } + char* nextChar = new char; - tiny_utf8::string importString = JavaCompat::importJavaFormatToString((uint8_t*) javaData); + 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); - std::streampos normalSize; - char* normalData; - std::ifstream normalFile("./resources/unicode_data/normal_utf-8", std::ios::in | std::ios::binary | std::ios::ate); - if(normalFile.is_open()){ - normalSize = normalFile.tellg(); - normalData = new char[normalSize]; - normalFile.seekg(0, std::ios::beg); - normalFile.read(normalData, normalSize); - normalFile.close(); - } + for (int i=0; i normalDataVector = std::vector(); - for(int i=0; i(javaStdString.data())); - std::cout << normalString.size() << std::endl; - std::cout << importString.size() << std::endl; + 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); - ASSERT(normalString == importString); + for (int i=0; i Date: Wed, 20 Jul 2022 04:23:06 +0200 Subject: [PATCH 4/7] lib/java_string: replace std::vector with std::string --- src/lib/java_string.cpp | 11 +++++------ src/test/java_string.cpp | 2 +- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/lib/java_string.cpp b/src/lib/java_string.cpp index a96fe9d..4d9d2c7 100644 --- a/src/lib/java_string.cpp +++ b/src/lib/java_string.cpp @@ -14,7 +14,7 @@ //If not, see https://www.gnu.org/licenses/agpl-3.0.en.html #include -#include +#include #include "error.h++" #include "java_string.h++" @@ -23,20 +23,19 @@ 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::vector output; - tiny_utf8::string outputString; + 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) { diff --git a/src/test/java_string.cpp b/src/test/java_string.cpp index 5e7cef5..9bd2ab9 100644 --- a/src/test/java_string.cpp +++ b/src/test/java_string.cpp @@ -52,7 +52,7 @@ int main(){ return 2; } - tiny_utf8::string importedString = JavaCompat::importJavaFormat(reinterpret_cast(javaStdString.data())); + tiny_utf8::string importedString = JavaCompat::importJavaString(reinterpret_cast(javaStdString.data())); std::streampos normalSize; std::string normalStdString; From c1b6c3005f2e7125849eec2326eb0ab2cd20be6f Mon Sep 17 00:00:00 2001 From: Milan Suman <> Date: Wed, 20 Jul 2022 08:39:29 +0530 Subject: [PATCH 5/7] lib/java_string: fix end character issues --- src/lib/java_string.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/java_string.cpp b/src/lib/java_string.cpp index 4d9d2c7..3c2797a 100644 --- a/src/lib/java_string.cpp +++ b/src/lib/java_string.cpp @@ -26,7 +26,7 @@ namespace JavaCompat { std::string stdString; uint16_t size = static_cast(data[0])<<8 | static_cast(data[1]); - for(uint8_t i=2; i Date: Wed, 20 Jul 2022 08:44:56 +0530 Subject: [PATCH 6/7] lib: rename java_string to javacompat --- scripts/test.sh | 2 +- src/lib/{java_string.cpp => javacompat.cpp} | 2 +- src/lib/{java_string.h++ => javacompat.h++} | 0 src/test/{java_string.cpp => javacompat.cpp} | 2 +- 4 files changed, 3 insertions(+), 3 deletions(-) rename src/lib/{java_string.cpp => javacompat.cpp} (98%) rename src/lib/{java_string.h++ => javacompat.h++} (100%) rename src/test/{java_string.cpp => javacompat.cpp} (98%) diff --git a/scripts/test.sh b/scripts/test.sh index 5248526..cc3aeb8 100755 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -39,7 +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/java_string.cpp -Idependencies/tiny-utf8-4.4.3/include -Lbin/lib -l:java_string.so -o bin/test/java_string" + "$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/javacompat.cpp similarity index 98% rename from src/lib/java_string.cpp rename to src/lib/javacompat.cpp index 3c2797a..05e5dee 100644 --- a/src/lib/java_string.cpp +++ b/src/lib/javacompat.cpp @@ -17,7 +17,7 @@ #include #include "error.h++" -#include "java_string.h++" +#include "javacompat.h++" namespace JavaCompat { //FIXME: contrary to what I said, we need to explicitly pass the data diff --git a/src/lib/java_string.h++ b/src/lib/javacompat.h++ similarity index 100% rename from src/lib/java_string.h++ rename to src/lib/javacompat.h++ diff --git a/src/test/java_string.cpp b/src/test/javacompat.cpp similarity index 98% rename from src/test/java_string.cpp rename to src/test/javacompat.cpp index 9bd2ab9..87d7862 100644 --- a/src/test/java_string.cpp +++ b/src/test/javacompat.cpp @@ -14,7 +14,7 @@ // If not, see https://www.gnu.org/licenses/agpl-3.0.en.html #include "assert.h++" -#include "../lib/java_string.h++" +#include "../lib/javacompat.h++" #include #include #include From 2f38636a277833d48bb093f27eb9db2fc194c5bc Mon Sep 17 00:00:00 2001 From: Milan Suman <> Date: Wed, 20 Jul 2022 10:38:55 +0530 Subject: [PATCH 7/7] lib/javacompat: implement exportJavaString --- src/lib/javacompat.cpp | 53 +++++++++++++++++++++++++++++++++++++---- src/lib/javacompat.h++ | 3 ++- src/test/javacompat.cpp | 17 ++++++++++++- 3 files changed, 66 insertions(+), 7 deletions(-) diff --git a/src/lib/javacompat.cpp b/src/lib/javacompat.cpp index 05e5dee..b60bc88 100644 --- a/src/lib/javacompat.cpp +++ b/src/lib/javacompat.cpp @@ -16,8 +16,18 @@ #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 @@ -37,9 +47,42 @@ namespace JavaCompat { } return tiny_utf8::string(stdString); } - /* - ErrorOr exportJavaString(tiny_utf8::string data) { - return ErrorOr(nullptr); + + ErrorOr> 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 { //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); + ErrorOr> exportJavaString(tiny_utf8::string data); } diff --git a/src/test/javacompat.cpp b/src/test/javacompat.cpp index 87d7862..49b1154 100644 --- a/src/test/javacompat.cpp +++ b/src/test/javacompat.cpp @@ -28,7 +28,7 @@ int main(){ // java-style_unicode: 119 bytes, 85 characters ?? std::cout << "################################################################################" << std::endl; - std::cout << "Java String Tests" << std::endl; + std::cout << "Import String Tests" << std::endl; std::cout << "################################################################################" << std::endl; char* nextChar = new char; @@ -78,6 +78,21 @@ int main(){ ASSERT(normalString == importedString); 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 data = JavaCompat::exportJavaString(normalString).value; + std::string exportedString = std::string(); + for(int i=0; i