diff --git a/Cargo.lock b/Cargo.lock index 12dfb20..db6a759 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,5 +1,25 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +[[package]] +name = "aho-corasick" +version = "0.7.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8716408b8bc624ed7f65d223ddb9ac2d044c0547b6fa4b0d554f3a9540496ada" +dependencies = [ + "memchr", +] + +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi", + "libc", + "winapi", +] + [[package]] name = "bitfield" version = "0.13.2" @@ -27,6 +47,12 @@ dependencies = [ "jobserver", ] +[[package]] +name = "cfg-if" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" + [[package]] name = "derivative" version = "2.1.1" @@ -44,12 +70,43 @@ version = "1.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bb1f6b1ce1c140482ea30ddd3335fc0024ac7ee112895426e0a629a6c20adfe3" +[[package]] +name = "env_logger" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36" +dependencies = [ + "atty", + "humantime", + "log", + "regex", + "termcolor", +] + [[package]] name = "glob" version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574" +[[package]] +name = "hermit-abi" +version = "0.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91780f809e750b0a89f5544be56617ff6b1227ee485bcb06ebe10cdf89bd3b71" +dependencies = [ + "libc", +] + +[[package]] +name = "humantime" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df004cfca50ef23c36850aaaa59ad52cc70d0e90243c3c7737a4dd32dc7a3c4f" +dependencies = [ + "quick-error", +] + [[package]] name = "itertools" version = "0.9.0" @@ -68,12 +125,33 @@ dependencies = [ "libc", ] +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + [[package]] name = "libc" version = "0.2.71" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9457b06509d27052635f90d6466700c65095fdf75409b3fbdd903e988b886f49" +[[package]] +name = "log" +version = "0.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14b6052be84e6b71ab17edffc2eeabf5c2c3ae1fdb464aae35ac50c67a44e1f7" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "memchr" +version = "2.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3728d817d99e5ac407411fa471ff9800a778d88a24685968b36824eaf4bee400" + [[package]] name = "num_enum" version = "0.5.0" @@ -114,6 +192,12 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "quick-error" +version = "1.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0" + [[package]] name = "quote" version = "1.0.6" @@ -123,6 +207,24 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "regex" +version = "1.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c3780fcf44b193bc4d09f36d2a3c87b251da4a046c87795a0d35f4f927ad8e6" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", + "thread_local", +] + +[[package]] +name = "regex-syntax" +version = "0.6.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26412eb97c6b088a6997e05f69403a802a92d520de2f8e63c2b65f9e0f47c4e8" + [[package]] name = "serde" version = "1.0.111" @@ -136,6 +238,8 @@ dependencies = [ "bitfield", "bitflags", "byteorder", + "env_logger", + "log", "num_enum", "zstd", ] @@ -151,6 +255,24 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "termcolor" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb6bfa289a4d7c5766392812c0a1f4c1ba45afa1ad47803c11e1f407d846d75f" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "thread_local" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d40c6d1b69745a6ec6fb1ca717914848da4b44ae29d9b3080cbee91d72a69b14" +dependencies = [ + "lazy_static", +] + [[package]] name = "toml" version = "0.5.6" @@ -166,6 +288,37 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c" +[[package]] +name = "winapi" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8093091eeb260906a183e6ae1abdba2ef5ef2257a21801128899c3fc699229c6" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +dependencies = [ + "winapi", +] + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + [[package]] name = "zstd" version = "0.5.2+zstd.1.4.5" diff --git a/Cargo.toml b/Cargo.toml index af964dd..d33421e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,3 +12,5 @@ bitfield = "0.13.2" zstd = "0.5.2" byteorder = "1.3.4" num_enum = "0.5.0" +log = "0.4.8" +env_logger = "0.7.1" diff --git a/src/main.rs b/src/main.rs index 69ed0a1..e5cc53f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,8 +27,8 @@ impl Machine { } fn pop_stack(&mut self) -> u16 { - let v = self.SSK[self.RSK as usize]; self.RSK += 1; + let v = self.SSK[self.RSK as usize]; v } } @@ -460,7 +460,7 @@ impl Arg { impl Machine { fn step(&mut self) -> bool { let (ci, inc_by) = parse_instruction(&self.SCODE[(self.IP as usize)..]); - eprintln!("{:?}", ci); + log::debug!("{:?}", ci); //dbg!(&ci); let oc = ci.def.opcode(); @@ -519,7 +519,7 @@ impl Machine { let a = self.read(ci.args[0]); let b = self.read(ci.args[1]); - eprintln!("CMP({}, {})", a, b); + log::debug!("CMP({}, {})", a, b); self.RSTAT.set(Flags::FZERO, a == 0 || b == 0); self.RSTAT.set(Flags::FEQUL, a == b); @@ -574,6 +574,22 @@ impl Machine { self.push_stack(self.RTRGT); self.RTRGT = 0; } + OP::RTRV => { + self.RTRGT = 0; + self.RSK = self.RSR; + self.RX = self.pop_stack(); + self.IP = self.RCALL + self.RX; + should_inc_ip = false; + + self.RZ = self.pop_stack(); + self.RY = self.pop_stack(); + self.RX = self.pop_stack(); + self.RSTAT = Flags::from_bits(self.pop_stack()).unwrap(); + self.RCALL = self.pop_stack(); + + self.push_stack(self.RTRGT); + self.RTRGT = 0; + } OP::RCPT => { let by: i32 = if ci.def.sign() == 1 { self.RTRGT as i32 @@ -621,8 +637,14 @@ impl Machine { self.RSTAT.set(Flags::FZERO, ored == 0); } OP::WRIT => { - let arg = self.read(ci.args[0]); - println!("ch: `{}`", arg); + let arg = self.read(ci.args[0]) as u8 as char; + print!("{}", arg); + } + OP::BSWP => { + let mut val = self.read(ci.args[0]); + val = val.swap_bytes(); + self.write(ci.args[0], val); + self.RSTAT.set(Flags::FZERO, val == 0); } _ => { eprintln!("unsupported opcode {:?}, continuing", oc); return false; } } @@ -644,6 +666,8 @@ fn dump_instructions(mut x: &[u16]) { } fn main() { + env_logger::init(); + let bin = include_bytes!("../challenge.mctf"); let (header, body) = bin.split_at(4); assert_eq!(header, b"mCTZ"); @@ -658,7 +682,7 @@ fn main() { let mut m = Machine::default(); m.SIN[1..=sin.inner().len()].copy_from_slice(sin.inner()); m.SCODE[1..=code.inner().len()].copy_from_slice(code.inner()); - m.input = b"\x0a".to_vec(); + m.input = b"\x0a\xff\xff\xff\xff\xff\xff\xff".to_vec(); // dump_instructions(&m.SCODE[1..=code.inner().len()]);