From 0c36512d04b2798614d6f2321a6bb33834d1102b Mon Sep 17 00:00:00 2001 From: eutro Date: Tue, 18 Feb 2025 12:34:33 +0000 Subject: [PATCH] Add sugar/spice/everything nice to Girl and snips/snails/puppy-dogs' tails to Boy --- src/main.rs | 110 ++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 94 insertions(+), 16 deletions(-) diff --git a/src/main.rs b/src/main.rs index 26bd1b5..ea744b0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -90,24 +90,76 @@ See: https://github.com/rust-lang/rust/issues/25860 This program is open-source! View the source for all these exploits here: https://github.com/Speykious/cve-rs "; -#[repr(C)] -#[derive(Debug)] -struct Boy { - age: u32, - name: String, - github_username: String, -} - -#[repr(C)] -#[derive(Debug)] -struct Girl { - age: u32, - name: String, - github_username: String, -} - fn transmute_demo(command: &str) -> std::io::Result<()> { + use std::hash::{DefaultHasher, Hash as _, Hasher as _}; use std::io::Write as _; + use std::str::FromStr; + + #[repr(C)] + #[derive(Debug)] + struct Boy { + age: u32, + name: String, + github_username: String, + snips: u32, + snails: u32, + puppy_dogs_tails: u64, + } + + #[repr(C)] + #[derive(Debug)] + struct Girl { + age: u32, + name: String, + github_username: String, + sugar: f32, + spice: f32, + everything_nice: f64, + } + + // Trait to obtain the ingredients from hashing arbitrary strings, + // if the user doesn't want to give us numbers. + trait FromHash: Sized { + fn from_hash(hash: u64) -> Self; + } + + impl FromHash for u64 { + fn from_hash(hash: u64) -> Self { + hash + } + } + + impl FromHash for u32 { + fn from_hash(hash: u64) -> Self { + (hash & Self::MAX as u64) as Self + } + } + + impl FromHash for f64 { + fn from_hash(hash: u64) -> Self { + // Cauchy distribution + let angle = (hash as f64 / u64::MAX as f64 - 0.5) * std::f64::consts::PI; + angle.tan() + } + } + + impl FromHash for f32 { + fn from_hash(hash: u64) -> Self { + f64::from_hash(hash) as f32 + } + } + + fn parse_or_hash(input: String) -> T + where + T: FromStr, + T: FromHash, + { + input.parse::().unwrap_or_else(|_| { + let mut h = DefaultHasher::new(); + input.hash(&mut h); + T::from_hash(h.finish()) + }) + } let stdin = std::io::stdin(); let mut stdout = std::io::stdout(); @@ -149,12 +201,35 @@ fn transmute_demo(command: &str) -> std::io::Result<()> { input_buf.trim().to_owned() }; + let (ingredient1, ingredient2, ingredient3) = { + let recipe = match command { + "boy-to-girl" => "snips, snails and puppy-dogs' tails", + "girl-to-boy" => "sugar and spice and everything nice", + _ => unreachable!(), + }; + print!("Enter {recipe} values, separated by commas: "); + stdout.flush()?; + + input_buf.clear(); + stdin.read_line(&mut input_buf)?; + + let mut iter = input_buf.split(",").map(str::trim); + ( + iter.next().unwrap_or("0").to_owned(), + iter.next().unwrap_or("0").to_owned(), + iter.next().unwrap_or("0").to_owned(), + ) + }; + match command { "boy-to-girl" => { let boy: Boy = Boy { age, name, github_username, + snips: parse_or_hash(ingredient1), + snails: parse_or_hash(ingredient2), + puppy_dogs_tails: parse_or_hash(ingredient3), }; println!("Before transmute: {boy:?}"); @@ -167,6 +242,9 @@ fn transmute_demo(command: &str) -> std::io::Result<()> { age, name, github_username, + sugar: parse_or_hash(ingredient1), + spice: parse_or_hash(ingredient2), + everything_nice: parse_or_hash(ingredient3), }; println!("Before transmute: {girl:?}");