From 566a90ded50a03e70355075d526bdd8aa22644f4 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Fri, 6 Jan 2023 16:16:39 +0800 Subject: [PATCH] update: check code before zip and unzip --- klotski/main.cc | 9 +++------ klotski/short_code.cc | 15 +++++++-------- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/klotski/main.cc b/klotski/main.cc index f6aca29..2d5ad5b 100644 --- a/klotski/main.cc +++ b/klotski/main.cc @@ -46,12 +46,9 @@ int main() { // std::cout << s.zip_short_code(0x6EC0F8800) << std::endl; // std::cout << "complete" << std::endl; -// if (ShortCode::check(14323231)) { - if (ShortCode::check(87654321)) { - std::cout << "true" << std::endl; - } else { - std::cout << "false" << std::endl; - } + printf("%d\n", s.zip_short_code(0x6EC0F8800)); + printf("%09lX\n", s.unzip_short_code(14323231)); +// printf("%09lX\n", s.unzip_short_code(87654321)); return 0; } diff --git a/klotski/short_code.cc b/klotski/short_code.cc index 19ac48d..995d4c7 100644 --- a/klotski/short_code.cc +++ b/klotski/short_code.cc @@ -3,6 +3,7 @@ #include "all_cases.h" #include "short_code.h" #include "short_code_mark.h" +#include "common_code.h" ShortCode::ShortCode(ShortCode::Mode mode) { // class initialize speed_up(mode); @@ -106,9 +107,9 @@ uint32_t ShortCode::tiny_encode(uint64_t common_code) { } uint32_t ShortCode::zip_short_code(uint64_t common_code) { // common_code --zip--> short_code - - // TODO: confirm common_code valid - + if (!CommonCode::check(common_code)) { + throw std::runtime_error("invalid common code"); + } switch (check_mode()) { case ShortCode::Mode::NORMAL: return tiny_encode(common_code); @@ -120,11 +121,9 @@ uint32_t ShortCode::zip_short_code(uint64_t common_code) { // common_code --zip- } uint64_t ShortCode::unzip_short_code(uint32_t short_code) { // short_code --unzip--> common_code - - // TODO: confirm short_code valid - - - + if (!check(short_code)) { + throw std::runtime_error("invalid short code"); + } switch (check_mode()) { case ShortCode::Mode::NORMAL: return tiny_decode(short_code);