diff --git a/Cargo.lock b/Cargo.lock index f30ec28..8a24bd1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,9 +2,15 @@ name = "picomp" version = "0.1.0" dependencies = [ + "byteorder 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)", "getopts 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "byteorder" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "getopts" version = "0.2.14" diff --git a/Cargo.toml b/Cargo.toml index 5e3e401..f662909 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,3 +5,4 @@ authors = ["logaritmisk "] [dependencies] getopts = "0.2" +byteorder = "0.5" diff --git a/src/main.rs b/src/main.rs index f385451..23cad86 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,15 @@ extern crate getopts; +extern crate byteorder; use std::env; use std::fs; use std::io::BufReader; use std::io::prelude::*; use std::i32; +use std::io::Cursor; use getopts::Options; +use byteorder::{ReadBytesExt, WriteBytesExt, LittleEndian}; fn main() { @@ -56,20 +59,31 @@ fn create(input: &String, output: &String) { } } - output_file.write_all(&[i as u8]).unwrap(); + let mut out : Vec = vec![]; + + out.write_i32::(i).unwrap(); + + output_file.write_all(&out).unwrap(); } } fn extract(input: &String, output: &String) { let mut output_file = fs::File::create(output).unwrap(); + let mut input_file = fs::File::open(input).unwrap(); - let input_file = fs::File::open(input).unwrap(); - let input_buf = BufReader::new(input_file); + let mut buf = vec![0; 4]; - for b in input_buf.bytes() { - let b = b.unwrap(); + loop { + let size = input_file.read(&mut buf).unwrap(); - output_file.write(&[get_byte(b as i32)]).unwrap(); + if size <= 0 { + break; + } + + let mut c = Cursor::new(&buf); + let index = c.read_i32::().unwrap(); + + output_file.write_all(&[get_byte(index)]).unwrap(); } }