From f1050df3d1f22070c74fc2936a6384e14ed9f19f Mon Sep 17 00:00:00 2001 From: h3x4n1um Date: Thu, 27 Jun 2019 14:07:34 +0700 Subject: [PATCH] Resolve issue #3 --- rton-json/CMakeLists.txt | 2 +- rton-json/src/json2rton.cpp | 35 +++++++++++++++++++++---------- rton-json/src/main.cpp | 2 +- rton-json/src/rton2json.cpp | 41 +++++++++++++++++++++++++++++-------- 4 files changed, 58 insertions(+), 22 deletions(-) diff --git a/rton-json/CMakeLists.txt b/rton-json/CMakeLists.txt index 7e6a342..c0bb8f8 100644 --- a/rton-json/CMakeLists.txt +++ b/rton-json/CMakeLists.txt @@ -17,7 +17,7 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) # semver set(VERSION_MAJOR 2) set(VERSION_MINOR 7) -set(VERSION_PATCH 1) +set(VERSION_PATCH 2) # configure a header file to pass some of the CMake settings # to the source code diff --git a/rton-json/src/json2rton.cpp b/rton-json/src/json2rton.cpp index 172bd7f..1972ceb 100644 --- a/rton-json/src/json2rton.cpp +++ b/rton-json/src/json2rton.cpp @@ -42,12 +42,8 @@ std::unordered_map map_0x91; std::unordered_map map_0x93; int write_RTON(json js); - -int not_json(){ - std::cerr << "Error! This file is not JSON format!!!" << std::endl; - debug << std::setw(4) << debug_js; - return 4; -} +int not_json(); +int not_supported_json(); //https://en.wikipedia.org/wiki/UTF-8#Examples int get_utf8_size(std::string q){ @@ -209,12 +205,17 @@ int write_RTON_block(json js){ } int write_RTON(json js){ - for (auto i : js.get >()){ - write_RTON_block(i.first); - write_RTON_block(i.second); + try{ + for (auto i : js.get >()){ + write_RTON_block(i.first); + write_RTON_block(i.second); + } + debug_js["RTON stats"]["List of bytecodes"][to_hex_string(output.tellp())] = to_hex_string(object_end); + output.write(reinterpret_cast (&object_end), 1); + } + catch(json::exception &e){ + throw not_supported_json(); } - debug_js["RTON stats"]["List of bytecodes"][to_hex_string(output.tellp())] = to_hex_string(object_end); - output.write(reinterpret_cast (&object_end), 1); return 0; } @@ -238,3 +239,15 @@ int rton_encode(){ return 0; } + +int not_json(){ + std::cerr << "Error! This file is not JSON format!!!" << std::endl; + debug << std::setw(4) << debug_js; + return 4; +} + +int not_supported_json(){ + std::cerr << "Error! This file is a JSON but format is not supported" << std::endl; + debug << std::setw(4) << debug_js; + return 6; +} diff --git a/rton-json/src/main.cpp b/rton-json/src/main.cpp index 16b2121..3c97ff9 100644 --- a/rton-json/src/main.cpp +++ b/rton-json/src/main.cpp @@ -143,7 +143,7 @@ int process_file(std::filesystem::path file_name, const int argc, const char *ar output.close(); debug.close(); - //remove unfinish file + //remove unfinished file std::filesystem::path out_file; if (file_type) out_file = (file_name.parent_path() / "rton2json" / file_name.stem()).string() + ".json"; else out_file = (file_name.parent_path() / "json2rton" / file_name.stem()).string() + ".rton"; diff --git a/rton-json/src/rton2json.cpp b/rton-json/src/rton2json.cpp index b717c8b..451ce18 100644 --- a/rton-json/src/rton2json.cpp +++ b/rton-json/src/rton2json.cpp @@ -28,6 +28,7 @@ std::vector stack_0x93; json read_RTON(); int bytecode_error(uint8_t bytecode); +int out_of_range_error(uint8_t bytecode); std::vector read_RTON_num(){ std::vector RTON_num; @@ -309,8 +310,12 @@ json read_RTON_block(){ } //recall case 0x91:{ - //TODO: check stack overflow - res.push_back(stack_0x91[unsigned_RTON_num2int(read_RTON_num())]); + try{ + res.push_back(stack_0x91.at(unsigned_RTON_num2int(read_RTON_num()))); + } + catch(const std::out_of_range &oor){ + throw out_of_range_error(bytecode); + } break; } //cached utf-8 string @@ -330,13 +335,16 @@ json read_RTON_block(){ } //recall case 0x93:{ - //TODO: check stack overflow - res.push_back(stack_0x93[unsigned_RTON_num2int(read_RTON_num())]); + try{ + res.push_back(stack_0x93.at(unsigned_RTON_num2int(read_RTON_num()))); + } + catch(const std::out_of_range &oor){ + throw out_of_range_error(bytecode); + } break; } //end of object case 0xFF:{ - //TODO: check for bracket break; } //else just exit error @@ -378,12 +386,21 @@ json json_decode(){ uint32_t RTON_ver; input.read(reinterpret_cast (&RTON_ver), sizeof RTON_ver); debug_js["RTON stats"]["RTON version"] = RTON_ver; + json js; js = read_RTON(); - char footer[5]; - input.read(footer, 4); - footer[4] = 0; - if (strcmp(footer, "DONE") != 0) std::clog << "Missing \"DONE\" at EOF?" << std::endl; + + if (input.eof()) std::clog << "Missing \"DONE\" at EOF?" << std::endl; + else{ + char footer[5]; + input.read(footer, 4); + footer[4] = 0; + + if (strcmp(footer, "DONE") != 0){ + input.seekg((uint8_t) input.tellg() - 3); + throw bytecode_error(footer[0]); + } + } return js; } @@ -393,3 +410,9 @@ int bytecode_error(uint8_t bytecode){ debug << std::setw(4) << debug_js; return 2; } + +int out_of_range_error(uint8_t bytecode){ + std::cerr << "Error! " << std::hex << std::showbase << (int) bytecode << " stack overflow at " << (uint64_t) input.tellg() - 1 << std::endl; + debug << std::setw(4) << debug_js; + return 5; +}