From 5c495359c13f925c272b4b5644b75ffe1203b6f6 Mon Sep 17 00:00:00 2001 From: Wim Hueskes Date: Thu, 21 Jul 2016 10:27:02 +0200 Subject: [PATCH] od: refactor: convert bytes using byteorder crate --- Cargo.lock | 6 ++++++ src/od/Cargo.toml | 1 + src/od/od.rs | 39 +++++++++++++++++++++++++++------------ 3 files changed, 34 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index aafb0f1cf..32e89bfb9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -176,6 +176,11 @@ name = "bitflags" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "byteorder" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "cat" version = "0.0.1" @@ -658,6 +663,7 @@ dependencies = [ name = "od" version = "0.0.1" dependencies = [ + "byteorder 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)", "getopts 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", "unindent 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/src/od/Cargo.toml b/src/od/Cargo.toml index 43bd178c5..555c82875 100644 --- a/src/od/Cargo.toml +++ b/src/od/Cargo.toml @@ -11,6 +11,7 @@ path = "od.rs" getopts = "*" libc = "*" unindent = "*" +byteorder = "*" [[bin]] name = "od" diff --git a/src/od/od.rs b/src/od/od.rs index 39a09964d..c515efa13 100644 --- a/src/od/od.rs +++ b/src/od/od.rs @@ -11,14 +11,15 @@ extern crate getopts; extern crate unindent; +extern crate byteorder; use std::fs::File; use std::io::Read; -use std::mem; use std::io::BufReader; use std::io::Write; use std::io; use unindent::*; +use byteorder::*; //This is available in some versions of std, but not all that we target. macro_rules! hashmap { @@ -233,21 +234,35 @@ fn odfunc(input_offset_base: &Radix, fnames: &[InputSource], formats: &[OdFormat first = false; print!("{:>width$}", "", width = f.offmarg);// 4 spaces after offset - we print 2 more before each word - for b in 0..n / f.itembytes { - let mut p: u64 = 0; - for i in 0..f.itembytes { - p |= (bytes[(f.itembytes * b) + i] as u64) << (8 * i); - } - (f.writer)(p, f.itembytes); - } // not enough byte for a whole element, this should only happen on the last line. if n % f.itembytes != 0 { let b = n / f.itembytes; - let mut p2: u64 = 0; - for i in 0..(n % f.itembytes) { - p2 |= (bytes[(f.itembytes * b) + i] as u64) << (8 * i); + // set zero bytes in the part of the buffer that will be used, but is not filled. + for i in n..(b + 1) * f.itembytes { + bytes[i] = 0; } - (f.writer)(p2, f.itembytes); + } + + let mut b = 0; + while b < n { + let nextb = b + f.itembytes; + let p: u64 = match f.itembytes { + 1 => { + bytes[b] as u64 + } + 2 => { + LittleEndian::read_u16(&bytes[b..nextb]) as u64 + } + 4 => { + LittleEndian::read_u32(&bytes[b..nextb]) as u64 + } + 8 => { + LittleEndian::read_u64(&bytes[b..nextb]) + } + _ => { panic!("Invalid itembytes: {}", f.itembytes); } + }; + (f.writer)(p, f.itembytes); + b = nextb; } // Add extra spaces to pad out the short, presumably last, line. if n < LINEBYTES {