From 17af660117c4ec6015f8829d90770e96b91af9e9 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Mon, 8 Aug 2022 15:06:36 +0200 Subject: [PATCH] Fixed overflow bug --- src/main.rs | 3 ++- src/permutations.rs | 9 +++++---- src/util.rs | 24 +++++++++++++++++------- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/main.rs b/src/main.rs index 53a248c..c2d1b68 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,5 +12,6 @@ fn main() { // scanner::start_scan(100); // permutations::ipv4(None); - let ip = permutations::IPv4::new(0); + let ip = permutations::IPv4::new(256); + println!("{:?}", ip); } diff --git a/src/permutations.rs b/src/permutations.rs index 163501a..d980f16 100644 --- a/src/permutations.rs +++ b/src/permutations.rs @@ -11,18 +11,19 @@ let i = 0 .. u32:max_value() # This is waaaay better than a stupid loop */ +#[derive(Debug)] pub struct IPv4 { - pub id: u32, - pub ip: Vec + pub id: u64, + pub ip: Vec } impl IPv4 { - pub fn new(id: u32) -> Self { + pub fn new(id: u64) -> Self { let mut base = Convert::new(10, 256); let id_vec = util::number_to_vec(id); // push all digits into a vec println!("########## {:?}", id_vec); - let ip = base.convert::(&id_vec); + let ip = base.convert::(&id_vec); Self { id, ip } } diff --git a/src/util.rs b/src/util.rs index e891c9d..71cc93b 100644 --- a/src/util.rs +++ b/src/util.rs @@ -10,13 +10,18 @@ uint ulong_len(ulong n) { // get the digit length of a number } */ -fn digit(num: u32, idx: u32) -> u8 { +fn digit(num: u64, idx: u32) -> u8 { // ((num % (10**(idx+1))) - (num % (10**idx)))/(10**idx) - (((num % (10_u32.pow(idx+1))) - (num % (10_u32.pow(idx))))/(10_u32.pow(idx))) as u8 + (((num % (10_u64.pow(idx+1))) - (num % (10_u64.pow(idx))))/(10_u64.pow(idx))) as u8 } -fn numlen(num: u32) -> u8 { - let len: u8 = 0; +fn numlen(mut num: u64) -> u8 { + let mut len: u8 = 0; + + if num == 0 { + return 1; + } + while num != 0 { num = num / 10; len += 1; @@ -24,14 +29,19 @@ fn numlen(num: u32) -> u8 { len } -pub fn number_to_vec(num: u32) -> Vec { - let out: Vec = Vec::new(); +pub fn number_to_vec(num: u64) -> Vec { + let mut out: Vec = Vec::new(); let len = numlen(num); + println!("len={len}"); for idx in 0..len { - out.push(digit(num, idx as u32)); + let digit = digit(num, idx as u32); + println!("\t{:?}", digit); + out.push(digit); } + println!("{:?}", out); + out }