diff --git a/src/main.rs b/src/main.rs index 6171a8297c3ecbd01433c4c168bf596e961a8695..cb9386975904e970a93ef04df7ed26652a288312 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,7 @@ #![allow(dead_code)] +use std::str::from_utf8; + static ABC: [u32; 4] = [0x9fdd9158, 0x85715808, 0xac73323a, @@ -144,6 +146,7 @@ static CODED: [u32; 132] = /// /// Count the number of zeroes +/// Found exists built in /// fn count_zero(mut num: u32) -> u32 { @@ -184,7 +187,7 @@ fn codgen(seed: &mut u32) -> u32 { //n = count_zero(local_seed); n = local_seed.count_zeros(); - println!("Number of zeroes: {}", n); + //println!("Number of zeroes: {}", n); //println!("Seed:\n{:b}", local_seed); // Inte OK än @@ -210,23 +213,68 @@ fn codgen(seed: &mut u32) -> u32 { /// /// Decodes a string /// +/// FUNCTION decode( wordarr, bytearr ): UNSIGNED INTEGER; +/// (wordarr, bytearr passed by reference) +/// LOCAL UNSIGNED INTEGER m, r, x, y; +/// BEGIN +/// x := ONE'S-COMPLEMENT of codgen(); +/// IF ([contents of word at "wordarr"] = 0) THEN +/// [byte pointed to by "bytearr"] := 0; +/// r := x; +/// ELSE +/// y := decode( wordarr+, bytearr+ ); # "k+" means "successor in k" +/// m := ( x - y ) - [contents of word at "wordarr"]; +/// [byte pointed to by "bytearr"] := [the eight bits at "m"<20:13>]; +/// r := TWO'S-COMPLEMENT OF codgen(); +/// r := x + y + m + r + 5; +/// ENDIF; +/// RETURN( r ); +/// END; +/// fn decode(coded: &[u32], plain: &mut[u8], seed: &mut u32) -> u32 { - let x = 3; - x + let m: u32; + let mut r: u32; + let x: u32; + let y: u32; + + // Ones complement, just negate + x = !codgen(seed); + + if coded[0] == 0 { + plain[0] = 0; + r = x; + + } else { + + // We need to remove the first element since it is already processed + y = decode(&coded[1..], &mut plain[1..], seed); + m = x.wrapping_sub(y).wrapping_sub(coded[0]); + + plain[0] = (m >> 13) as u8; + r = !codgen(seed) + 1; + r = r.wrapping_add(x).wrapping_add(y).wrapping_add(m).wrapping_add(5); + } + r } fn main() { println!("Time to decode!"); - //let mut seed = 0x0e0657c1; - let mut seed = 0x3e944b9f; + let mut seed = 0x0e0657c1; + + decode(&CODED, unsafe { &mut PLAIN }, &mut seed); - //decode(&CODED, unsafe { &mut PLAIN }, &mut seed); + println!("Decoded:\n {:#?}", from_utf8(unsafe { &PLAIN }).unwrap()); + /* let mut locseed; + // Testing seed + let mut seed = 0x3e944b9f; + + locseed = codgen(&mut seed); assert_eq!(locseed, 0x891432f9); println!("Ran codgen, new seed: {:x}", locseed); @@ -239,6 +287,7 @@ fn main() { locseed = codgen(&mut seed); assert_eq!(locseed, 0xce83d1b8); println!("Ran codgen, new seed: {:x}", locseed); + */ } @@ -252,29 +301,12 @@ fn main() { # the first five calls will generate these values: # 0x891432f9, 0x4aa1dccc, 0xc54270fa, 0x9885155f, 0xce83d1b8, ... # your code is to be written farther down (see comment below). - - + + # Group 1's Recursive Decoding Subroutine (pseudocode) # (for "decode", all four local variables must be implemented ON THE # STACK, and NOT in registers; implement the code literally,. # no optimizations. We're trying to teach you something. # remember: result returned in v0; preserve all except t regs) # -# FUNCTION decode( wordarr, bytearr ): UNSIGNED INTEGER; -# (wordarr, bytearr passed by reference) -# LOCAL UNSIGNED INTEGER m, r, x, y; -# BEGIN -# x := ONE'S-COMPLEMENT of codgen(); -# IF ([contents of word at "wordarr"] = 0) THEN -# [byte pointed to by "bytearr"] := 0; -# r := x; -# ELSE -# y := decode( wordarr+, bytearr+ ); # "k+" means "successor in k" -# m := ( x - y ) - [contents of word at "wordarr"]; -# [byte pointed to by "bytearr"] := [the eight bits at "m"<20:13>]; -# r := TWO'S-COMPLEMENT OF codgen(); -# r := x + y + m + r + 5; -# ENDIF; -# RETURN( r ); -# END; */