Skip to content

Commit

Permalink
Resolved #7
Browse files Browse the repository at this point in the history
  • Loading branch information
h3x4n1um committed Oct 5, 2019
1 parent 103e36d commit 2528512
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 161 deletions.
2 changes: 1 addition & 1 deletion rton-json/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ find_package(nlohmann_json 3.7.0 REQUIRED)
# semver
set(VERSION_MAJOR 2)
set(VERSION_MINOR 7)
set(VERSION_PATCH 4)
set(VERSION_PATCH 5)

# configure a header file to pass some of the CMake settings
# to the source code
Expand Down
4 changes: 3 additions & 1 deletion rton-json/include/RTON_number.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@
using namespace std;

constexpr double log256(double q);
vector <uint8_t> uint64_t2uRTON_t(uint64_t q);

uint64_t uRTON_t2uint64_t(vector <uint8_t> q);

vector <uint8_t> uint64_t2uRTON_t(uint64_t q);
4 changes: 2 additions & 2 deletions rton-json/include/error.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

int bytecode_error(uint8_t bytecode);
int key_error();
int out_of_range_error(uint8_t bytecode);
int eof_error(char footer[5]);
int key_error();
int not_supported_json();
int out_of_range_error(uint8_t bytecode);
7 changes: 4 additions & 3 deletions rton-json/include/json2rton.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@

using namespace std;

int write_RTON(json js);
int get_utf8_size(string q);
int write_uRTON_t(vector <uint8_t> a);
int rton_encode();
int write_RTON(json js);
int write_RTON_block(json js);
int write_uRTON_t(vector <uint8_t> a);

size_t get_utf8_size(string q);
4 changes: 3 additions & 1 deletion rton-json/include/rton2json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ using json = nlohmann::basic_json<workaround_fifo_map>;

template <class T>
T read();
json json_decode();

vector <uint8_t> read_uRTON_t();

json json_decode();
json read_RTON();
json read_RTON_block();
95 changes: 53 additions & 42 deletions rton-json/src/json2rton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ unordered_map <string, uint64_t> map_0x91;
unordered_map <string, uint64_t> map_0x93;

//https://en.wikipedia.org/wiki/UTF-8#Examples
int get_utf8_size(string q){
int utf8_size = 0;
size_t get_utf8_size(string q){
size_t utf8_size = 0;
for (uint8_t i : q){
if (i <= 0177) utf8_size += 1;
if (i >= 0302 && i <= 0337) utf8_size += 1;
Expand Down Expand Up @@ -65,7 +65,7 @@ int write_RTON_block(json js){
temp.erase(0, 5);

string first_string = temp.substr(temp.find("@") + 1),
second_string = temp.substr(0, temp.find("@"));
second_string = temp.substr(0, temp.find("@"));

uint8_t subset;
if (regex_match(second_string, regex("\\d+\\.\\d+\\.[0-9a-fA-F]+"))) subset = 0x2;
Expand All @@ -77,11 +77,13 @@ int write_RTON_block(json js){
write_uRTON_t(uint64_t2uRTON_t(first_string.size()));
output << first_string;
if (subset == 0x2){
uint64_t first_uid = strtoull(second_string.c_str(), NULL, 10);
second_string = second_string.substr(second_string.find(".") + 1);
uint64_t second_uid = strtoull(second_string.c_str(), NULL, 10);
second_string = second_string.substr(second_string.find(".") + 1);
int32_t third_uid = stoi(second_string, nullptr, 16);
stringstream raw_ss(second_string), ss;
string tmp_str;
uint64_t first_uid, second_uid;
uint32_t third_uid;

while(getline(raw_ss, tmp_str, '.')) ss << tmp_str << " ";
ss >> first_uid >> second_uid >> hex >> third_uid;

write_uRTON_t(uint64_t2uRTON_t(second_uid));
write_uRTON_t(uint64_t2uRTON_t(first_uid));
Expand All @@ -93,43 +95,52 @@ int write_RTON_block(json js){
output << second_string;
}
}
//normal string
//NaN
else if (temp.find("NaN") != string::npos){
double dnan = numeric_limits<double>::signaling_NaN();
output.write(reinterpret_cast<const char*> (&float64), sizeof float64);
output.write(reinterpret_cast<const char*> (&dnan), sizeof dnan);
}
//Infinity
else if (temp.find("Infinity") != string::npos){
double dinf = numeric_limits<double>::infinity();
if (temp[0] == '-') dinf = -dinf;
output.write(reinterpret_cast<const char*> (&float64), sizeof float64);
output.write(reinterpret_cast<const char*> (&dinf), sizeof dinf);
}
//normal ascii string
else if (get_utf8_size(temp) == temp.size()){
if (map_0x91[temp] == 0){
debug_js["RTON stats"]["List of bytecodes"][to_hex_string(output.tellp())] = to_hex_string(ascii);
output.write(reinterpret_cast<const char*> (&ascii), sizeof ascii);
write_uRTON_t(uint64_t2uRTON_t(temp.size()));
output << temp;

debug_js["RTON stats"]["0x91 stack"][to_hex_string(uint64_t2uRTON_t(map_0x91.size() - 1))] = temp;
map_0x91[temp] = map_0x91.size();
}
else{
debug_js["RTON stats"]["List of bytecodes"][to_hex_string(output.tellp())] = to_hex_string(ascii_stack);
output.write(reinterpret_cast<const char*> (&ascii_stack), sizeof ascii_stack);
write_uRTON_t(uint64_t2uRTON_t(map_0x91[temp] - 1));
}
}
//normal utf-8 string
else{
size_t utf8_size = get_utf8_size(temp);
//ascii
if (utf8_size == temp.size()){
if (map_0x91[temp] == 0){
debug_js["RTON stats"]["List of bytecodes"][to_hex_string(output.tellp())] = to_hex_string(ascii);
output.write(reinterpret_cast<const char*> (&ascii), sizeof ascii);
write_uRTON_t(uint64_t2uRTON_t(temp.size()));
output << temp;

debug_js["RTON stats"]["0x91 stack"][to_hex_string(uint64_t2uRTON_t(map_0x91.size() - 1))] = temp;
map_0x91[temp] = map_0x91.size();
}
else{
debug_js["RTON stats"]["List of bytecodes"][to_hex_string(output.tellp())] = to_hex_string(ascii_stack);
output.write(reinterpret_cast<const char*> (&ascii_stack), sizeof ascii_stack);
write_uRTON_t(uint64_t2uRTON_t(map_0x91[temp] - 1));
}
if (map_0x93[temp] == 0){
debug_js["RTON stats"]["List of bytecodes"][to_hex_string(output.tellp())] = to_hex_string(utf8);
output.write(reinterpret_cast<const char*> (&utf8), sizeof utf8);
write_uRTON_t(uint64_t2uRTON_t(get_utf8_size(temp)));
write_uRTON_t(uint64_t2uRTON_t(temp.size()));
output << temp;

debug_js["RTON stats"]["0x93 stack"][to_hex_string(uint64_t2uRTON_t(map_0x93.size() - 1))] = temp;
map_0x93[temp] = map_0x93.size();
}
//utf-8
else{
if (map_0x93[temp] == 0){
debug_js["RTON stats"]["List of bytecodes"][to_hex_string(output.tellp())] = to_hex_string(utf8);
output.write(reinterpret_cast<const char*> (&utf8), sizeof utf8);
write_uRTON_t(uint64_t2uRTON_t(utf8_size));
write_uRTON_t(uint64_t2uRTON_t(temp.size()));
output << temp;

debug_js["RTON stats"]["0x93 stack"][to_hex_string(uint64_t2uRTON_t(map_0x93.size() - 1))] = temp;
map_0x93[temp] = map_0x93.size();
}
else{
debug_js["RTON stats"]["List of bytecodes"][to_hex_string(output.tellp())] = to_hex_string(utf8_stack);
output.write(reinterpret_cast<const char*> (&utf8_stack), sizeof utf8_stack);
write_uRTON_t(uint64_t2uRTON_t(map_0x93[temp] - 1));
}
debug_js["RTON stats"]["List of bytecodes"][to_hex_string(output.tellp())] = to_hex_string(utf8_stack);
output.write(reinterpret_cast<const char*> (&utf8_stack), sizeof utf8_stack);
write_uRTON_t(uint64_t2uRTON_t(map_0x93[temp] - 1));
}
}
break;
Expand Down
4 changes: 2 additions & 2 deletions rton-json/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,10 @@ int main(const int argc, const char *argv[]){
json_list["Total files"] = json_list["JSON files"].size();

//log processed json & rton
debug.open(path / "json2rton" / "json2rton.json");
debug.open(path / "log" / "json2rton.json");
debug << setw(4) << json_list;
debug.close();
debug.open(path / "rton2json" / "rton2json.json");
debug.open(path / "log" / "rton2json.json");
debug << setw(4) << rton_list;
debug.close();

Expand Down
Loading

0 comments on commit 2528512

Please sign in to comment.