Compare commits

...

8 Commits

Author SHA1 Message Date
Milan Suman d315c6fcfc Merge branch 'broken' 2022-07-20 10:39:16 +05:30
Milan Suman 2f38636a27 lib/javacompat: implement exportJavaString 2022-07-20 10:38:55 +05:30
Milan Suman 0462756451 lib: rename java_string to javacompat 2022-07-20 08:44:56 +05:30
Milan Suman c1b6c3005f lib/java_string: fix end character issues 2022-07-20 08:39:29 +05:30
BodgeMaster 45538e156a lib/java_string: replace std::vector with std::string 2022-07-20 04:23:06 +02:00
BodgeMaster 34e30c0bd4 test/java_string: replace tabs with spaces, fix how tiny_utf8::string is being instantiated 2022-07-20 04:02:44 +02:00
BodgeMaster e184acde00 lib/java_string: rename functions
JavaCompat::importJavaFormatToString() → JavaCompat::importJavaString()
JavaCompat::exportStringToJavaFormat() → JavaCompat::exportJavaString()
2022-07-20 04:00:17 +02:00
Milan Suman 42336c560a Birthed an abomination, what the fuck 2022-07-18 12:13:10 +05:30
5 changed files with 192 additions and 32 deletions

View File

@ -39,6 +39,7 @@ echo "Building tests..."
COMPILE_COMMANDS=( 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/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/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 for command in ${!COMPILE_COMMANDS[@]}; do
echo "${COMPILE_COMMANDS[command]}" echo "${COMPILE_COMMANDS[command]}"

View File

@ -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 <tinyutf8/tinyutf8.h>
#include "error.h++"
#include "java_string.h++"
namespace JavaCompat {
ErrorOr<tiny_utf8::string> importJavaFormatToString(uint8_t data[]) {
tiny_utf8::string output;
// do magic
return ErrorOr(output);
}
ErrorOr<uint8_t*> exportStringToJavaFormat(tiny_utf8::string data) {
return ErrorOr(nullptr);
}
}

88
src/lib/javacompat.cpp Normal file
View File

@ -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 <tinyutf8/tinyutf8.h>
#include <string>
#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<uint16_t>(data[0])<<8 | static_cast<uint16_t>(data[1]);
for(uint8_t i=2; i<size+2; i++){
if(i != 0){
if(data[i] == 0x80 && data[i-1] == 0xc0){
stdString[stdString.length() - 1] = '\0';
continue;
}
}
stdString.push_back((char) data[i]);
}
return tiny_utf8::string(stdString);
}
ErrorOr<std::vector<uint8_t>> exportJavaString(tiny_utf8::string data) {
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,8 +15,11 @@
#include <tinyutf8/tinyutf8.h> #include <tinyutf8/tinyutf8.h>
#include "error.h++" #include "error.h++"
#include <vector>
namespace JavaCompat { namespace JavaCompat {
ErrorOr<tiny_utf8::string> importJavaFormatToString(uint8_t data[]); //FIXME: contrary to what I said, we need to explicitly pass the data
ErrorOr<uint8_t*> exportStringToJavaFormat(tiny_utf8::string data); // size because files could have been tampered with or corrupted
tiny_utf8::string importJavaString(uint8_t data[]);
ErrorOr<std::vector<uint8_t>> exportJavaString(tiny_utf8::string data);
} }

98
src/test/javacompat.cpp Normal file
View File

@ -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 <iostream>
#include <tinyutf8/tinyutf8.h>
#include <fstream>
#include <vector>
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<javaSize; i++) {
javaFile.read(nextChar, 1);
javaStdString.push_back(*nextChar);
}
javaFile.close();
} else {
std::cerr << "Failed to open file: " << javaFilePath << std::endl;
return 2;
}
tiny_utf8::string importedString = JavaCompat::importJavaString(reinterpret_cast<uint8_t*>(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<normalSize; i++) {
normalFile.read(nextChar, 1);
normalStdString.push_back(*nextChar);
}
normalFile.close();
} else {
std::cerr << "Failed to open file: " << normalFilePath << std::endl;
return 2;
}
tiny_utf8::string normalString = tiny_utf8::string(normalStdString);
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<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;
}