Skip to content

Commit

Permalink
Change function name and move bit detect from cmake to code
Browse files Browse the repository at this point in the history
  • Loading branch information
h3x4n1um committed Sep 30, 2019
1 parent c8910df commit cdfbcba
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 94 deletions.
9 changes: 0 additions & 9 deletions rton-json/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,6 @@ endif()
# require nlohmann_json
find_package(nlohmann_json 3.7.0 REQUIRED)

if(CMAKE_SIZEOF_VOID_P EQUAL 8)
# 64 bits
set(ARCHITECTURE "64-bit")
elseif(CMAKE_SIZEOF_VOID_P EQUAL 4)
# 32 bits
set(ARCHITECTURE "32-bit")
endif()
message(${ARCHITECTURE})

# semver
set(VERSION_MAJOR 2)
set(VERSION_MINOR 7)
Expand Down
4 changes: 2 additions & 2 deletions rton-json/include/RTON_number.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
using namespace std;

constexpr double log256(double q);
vector <uint8_t> int2unsigned_RTON_num(uint64_t q);
uint64_t unsigned_RTON_num2int(vector <uint8_t> q);
vector <uint8_t> uint64_to_uRTON(uint64_t q);
uint64_t uRTON_to_uint64(vector <uint8_t> q);
35 changes: 30 additions & 5 deletions rton-json/include/rton-json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
#include <nlohmann/json.hpp>
#include "lib/fifo_map.hpp"

#include "version.hpp"

using namespace std;

//a workaround to give to use fifo_map as map, we are just ignoring the 'less' compare
Expand All @@ -19,12 +17,39 @@ template<class K, class V, class dummy_compare, class A>
using workaround_fifo_map = nlohmann::fifo_map<K, V, nlohmann::fifo_map_compare<K>, A>;
using json = nlohmann::basic_json<workaround_fifo_map>;

const string ver = to_string(VERSION_MAJOR) + '.' + to_string(VERSION_MINOR) + '.' + to_string(VERSION_PATCH);
#if INTPTR_MAX == INT64_MAX
// 64-bit
# define ARCHITECTURE "64-bit"
#elif INTPTR_MAX == INT32_MAX
// 32-bit
# define ARCHITECTURE "32-bit"
#else
# define ARCHITECTURE "Unknown"
#endif
const string architecture = ARCHITECTURE;

#if __has_include("version.hpp")
# include "version.hpp"
const string ver = to_string(VERSION_MAJOR) + '.' + to_string(VERSION_MINOR) + '.' + to_string(VERSION_PATCH);
#else
const string ver = "Unknown";
#endif // VERSION_HPP

extern ifstream input;
extern ofstream output, debug;
extern json debug_js, rton_list, json_list;

extern string to_hex_string(uint64_t q);
extern string to_hex_string(vector <uint8_t> a);
inline string to_hex_string(uint64_t q){
stringstream ss;
ss << "0x" << hex << q;
return ss.str();
}

inline string to_hex_string(vector <uint8_t> a){
stringstream ss;
ss << "0x";
for (uint8_t i : a){
ss << setfill('0') << setw(2) << hex << (int) i;
}
return ss.str();
}
6 changes: 3 additions & 3 deletions rton-json/src/RTON_number.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ constexpr double log256(double q){
return log2(q) / 8;
}

vector <uint8_t> int2unsigned_RTON_num(uint64_t q){
vector <uint8_t> uint64_to_uRTON(uint64_t q){
vector <uint8_t> res;
if (q <= 0x7f){
res.push_back(q);
Expand All @@ -18,12 +18,12 @@ vector <uint8_t> int2unsigned_RTON_num(uint64_t q){
q = q / 0x100 * 2;
if (temp > 0x7f) ++q;
else temp += 0x80; //reverse & 0x7f
res = int2unsigned_RTON_num(q);
res = uint64_to_uRTON(q);
res.insert(res.begin(), temp);
return res;
}

uint64_t unsigned_RTON_num2int(vector <uint8_t> q){
uint64_t uRTON_to_uint64(vector <uint8_t> q){
if (q.size() == 1){
if (q[0] > 0x7f) return UINT_MAX; //return max when RTON number has 1 byte and > 0x7f
else return q[0];
Expand Down
32 changes: 16 additions & 16 deletions rton-json/src/json2rton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ int write_RTON_block(json js){
debug_js["RTON stats"]["List of bytecodes"][to_hex_string((uint64_t) output.tellp() - 1)] = to_hex_string(rtid*0x100 + subset);
output.write(reinterpret_cast<const char*> (&subset), sizeof subset);

write_unsigned_RTON_num(int2unsigned_RTON_num(get_utf8_size(first_string)));
write_unsigned_RTON_num(int2unsigned_RTON_num(first_string.size()));
write_unsigned_RTON_num(uint64_to_uRTON(get_utf8_size(first_string)));
write_unsigned_RTON_num(uint64_to_uRTON(first_string.size()));
output << first_string;
if (subset == 0x2){
uint64_t first_uid = strtoull(second_string.c_str(), NULL, 10);
Expand All @@ -83,13 +83,13 @@ int write_RTON_block(json js){
second_string = second_string.substr(second_string.find(".") + 1);
int32_t third_uid = stoi(second_string, nullptr, 16);

write_unsigned_RTON_num(int2unsigned_RTON_num(second_uid));
write_unsigned_RTON_num(int2unsigned_RTON_num(first_uid));
write_unsigned_RTON_num(uint64_to_uRTON(second_uid));
write_unsigned_RTON_num(uint64_to_uRTON(first_uid));
output.write(reinterpret_cast<const char*> (&third_uid), sizeof third_uid);
}
else if (subset == 0x3){
write_unsigned_RTON_num(int2unsigned_RTON_num(get_utf8_size(second_string)));
write_unsigned_RTON_num(int2unsigned_RTON_num(second_string.size()));
write_unsigned_RTON_num(uint64_to_uRTON(get_utf8_size(second_string)));
write_unsigned_RTON_num(uint64_to_uRTON(second_string.size()));
output << second_string;
}
}
Expand All @@ -101,34 +101,34 @@ int write_RTON_block(json js){
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_unsigned_RTON_num(int2unsigned_RTON_num(temp.size()));
write_unsigned_RTON_num(uint64_to_uRTON(temp.size()));
output << temp;

debug_js["RTON stats"]["0x91 stack"][to_hex_string(int2unsigned_RTON_num(map_0x91.size() - 1))] = temp;
debug_js["RTON stats"]["0x91 stack"][to_hex_string(uint64_to_uRTON(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_unsigned_RTON_num(int2unsigned_RTON_num(map_0x91[temp] - 1));
write_unsigned_RTON_num(uint64_to_uRTON(map_0x91[temp] - 1));
}
}
//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_unsigned_RTON_num(int2unsigned_RTON_num(utf8_size));
write_unsigned_RTON_num(int2unsigned_RTON_num(temp.size()));
write_unsigned_RTON_num(uint64_to_uRTON(utf8_size));
write_unsigned_RTON_num(uint64_to_uRTON(temp.size()));
output << temp;

debug_js["RTON stats"]["0x93 stack"][to_hex_string(int2unsigned_RTON_num(map_0x93.size() - 1))] = temp;
debug_js["RTON stats"]["0x93 stack"][to_hex_string(uint64_to_uRTON(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_unsigned_RTON_num(int2unsigned_RTON_num(map_0x93[temp] - 1));
write_unsigned_RTON_num(uint64_to_uRTON(map_0x93[temp] - 1));
}
}
}
Expand All @@ -138,14 +138,14 @@ int write_RTON_block(json js){
debug_js["RTON stats"]["List of bytecodes"][to_hex_string(output.tellp())] = to_hex_string(RTON_t);
output.write(reinterpret_cast<const char*> (&RTON_t), sizeof RTON_t);
int64_t temp = js.get<int64_t>();
write_unsigned_RTON_num(int2unsigned_RTON_num(temp < 0 ? -2 * temp - 1 : 2 * temp));
write_unsigned_RTON_num(uint64_to_uRTON(temp < 0 ? -2 * temp - 1 : 2 * temp));
break;
}
case json::value_t::number_unsigned:{
debug_js["RTON stats"]["List of bytecodes"][to_hex_string(output.tellp())] = to_hex_string(uRTON_t);
output.write(reinterpret_cast<const char*> (&uRTON_t), sizeof uRTON_t);
uint64_t temp = js.get<uint64_t>();
write_unsigned_RTON_num(int2unsigned_RTON_num(temp));
write_unsigned_RTON_num(uint64_to_uRTON(temp));
break;
}
case json::value_t::number_float:{
Expand All @@ -168,7 +168,7 @@ int write_RTON_block(json js){
debug_js["RTON stats"]["List of bytecodes"][to_hex_string(output.tellp())] = to_hex_string(arr_begin);
output.write(reinterpret_cast<const char*> (&arr_begin), sizeof arr_begin);

write_unsigned_RTON_num(int2unsigned_RTON_num(js.size()));
write_unsigned_RTON_num(uint64_to_uRTON(js.size()));
for (auto i : js) write_RTON_block(i);

debug_js["RTON stats"]["List of bytecodes"][to_hex_string(output.tellp())] = to_hex_string(arr_end);
Expand Down
52 changes: 22 additions & 30 deletions rton-json/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,15 @@ ifstream input;
ofstream output, debug;
json debug_js, rton_list, json_list;

string to_hex_string(uint64_t q){
stringstream ss;
ss << "0x" << hex << q;
return ss.str();
}

string to_hex_string(vector <uint8_t> a){
stringstream ss;
ss << "0x";
for (uint8_t i : a){
ss << setfill('0') << setw(2) << hex << (int) i;
}
return ss.str();
}

int help(const char *argv[]){
cerr << "Usage:" << endl
<< '\t' << argv[0] << " <file-or-folder-path>" << endl
<< '\t' << argv[0] << " [options]" << " <file-or-folder-path>" << endl << endl
<< "Options:" << endl
<< "\t--help\t\tShow help (the thing you're looking at)" << endl
<< "\t--rton2json\tForce covert RTON to JSON" << endl
<< "\t--json2rton\tForce covert JSON to RTON" << endl;
<< '\t' << argv[0] << " <file-or-folder-path>" << endl
<< '\t' << argv[0] << " [options]" << " <file-or-folder-path>" << endl
<< endl
<< "Options:" << endl
<< "\t--help\t\tShow help (the thing you're looking at)" << endl
<< "\t--rton2json\tForce covert RTON to JSON" << endl
<< "\t--json2rton\tForce covert JSON to RTON" << endl;
cin.get();
return 1;
}
Expand Down Expand Up @@ -106,7 +92,8 @@ int process_file(filesystem::path file_name, const int argc, const char *argv[])
//log at the end
debug << setw(4) << debug_js;
debug.close();
clog << "Done" << endl << endl;
clog << "Done" << endl
<< endl;

break;
}
Expand All @@ -128,13 +115,15 @@ int process_file(filesystem::path file_name, const int argc, const char *argv[])
//log at the end
debug << setw(4) << debug_js;
debug.close();
clog << "Done" << endl << endl;
clog << "Done" << endl
<< endl;

break;
}
default:{
clog << " - Unknown" << endl
<< "Skipped" << endl << endl;
<< "Skipped" << endl
<< endl;
}
}
}
Expand All @@ -150,18 +139,21 @@ int process_file(filesystem::path file_name, const int argc, const char *argv[])
else out_file = (file_name.parent_path() / "json2rton" / file_name.stem()).string() + ".rton";
filesystem::remove(out_file);

clog << "Error code: " << e << endl << endl;
clog << "Error code: " << e << endl
<< endl;
return e;
}
return 0;
}

int main(const int argc, const char *argv[]){
clog << endl << "rton-json made by H3x4n1um" << endl
<< "Version: " << ver << endl
<< "Architecture: " << architecture << endl
<< "Compiled on " << __DATE__ << " at " << __TIME__ << endl
<< "Credits: nlohmann for his awesome JSON parser and fifo_map" << endl << endl;
clog << endl
<< "rton-json made by H3x4n1um" << endl
<< "Version: " << ver << endl
<< architecture << " executable" << endl
<< "Compiled on " << __DATE__ << " at " << __TIME__ << endl
<< "Credits: nlohmann for his awesome JSON parser and fifo_map" << endl
<< endl;

filesystem::path path;
switch (argc){
Expand Down
Loading

0 comments on commit cdfbcba

Please sign in to comment.