Skip to content
Snippets Groups Projects
Commit d6f70ccc authored by Henrik Tjäder's avatar Henrik Tjäder
Browse files

Decode seems to work, maybe not super tidy

parent 6095a746
No related branches found
No related tags found
No related merge requests found
#![allow(dead_code)] #![allow(dead_code)]
use std::str::from_utf8;
static ABC: [u32; 4] = [0x9fdd9158, static ABC: [u32; 4] = [0x9fdd9158,
0x85715808, 0x85715808,
0xac73323a, 0xac73323a,
...@@ -144,6 +146,7 @@ static CODED: [u32; 132] = ...@@ -144,6 +146,7 @@ static CODED: [u32; 132] =
/// ///
/// Count the number of zeroes /// Count the number of zeroes
/// Found exists built in
/// ///
fn count_zero(mut num: u32) -> u32 { fn count_zero(mut num: u32) -> u32 {
...@@ -184,7 +187,7 @@ fn codgen(seed: &mut u32) -> u32 { ...@@ -184,7 +187,7 @@ fn codgen(seed: &mut u32) -> u32 {
//n = count_zero(local_seed); //n = count_zero(local_seed);
n = local_seed.count_zeros(); n = local_seed.count_zeros();
println!("Number of zeroes: {}", n); //println!("Number of zeroes: {}", n);
//println!("Seed:\n{:b}", local_seed); //println!("Seed:\n{:b}", local_seed);
// Inte OK än // Inte OK än
...@@ -210,23 +213,68 @@ fn codgen(seed: &mut u32) -> u32 { ...@@ -210,23 +213,68 @@ fn codgen(seed: &mut u32) -> u32 {
/// ///
/// Decodes a string /// 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 { fn decode(coded: &[u32], plain: &mut[u8], seed: &mut u32) -> u32 {
let x = 3; let m: u32;
x 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() { fn main() {
println!("Time to decode!"); println!("Time to decode!");
//let mut seed = 0x0e0657c1; let mut seed = 0x0e0657c1;
let mut seed = 0x3e944b9f;
//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; let mut locseed;
// Testing seed
let mut seed = 0x3e944b9f;
locseed = codgen(&mut seed); locseed = codgen(&mut seed);
assert_eq!(locseed, 0x891432f9); assert_eq!(locseed, 0x891432f9);
println!("Ran codgen, new seed: {:x}", locseed); println!("Ran codgen, new seed: {:x}", locseed);
...@@ -239,6 +287,7 @@ fn main() { ...@@ -239,6 +287,7 @@ fn main() {
locseed = codgen(&mut seed); locseed = codgen(&mut seed);
assert_eq!(locseed, 0xce83d1b8); assert_eq!(locseed, 0xce83d1b8);
println!("Ran codgen, new seed: {:x}", locseed); println!("Ran codgen, new seed: {:x}", locseed);
*/
} }
...@@ -260,21 +309,4 @@ fn main() { ...@@ -260,21 +309,4 @@ fn main() {
# no optimizations. We're trying to teach you something. # no optimizations. We're trying to teach you something.
# remember: result returned in v0; preserve all except t regs) # 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;
*/ */
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment