Skip to content

Commit

Permalink
Add repack example
Browse files Browse the repository at this point in the history
E.g. this can be used to fix CRC checksums in files obtained
by fuzzing methods.
So if you do this:

RUSTFLAGS="--cfg fuzzing" cargo run --example repack <A> <B>

it will read from A and repack it to B while ignoring any CRC
errors.

Note that the repack example is atm not expected to reproduce
100% the same file: page boundaries are not being respected
so they might turn out differently in the generated file.
This might then in turn mess up the packet <-> absgp link.

But to fix all of this we'd have to add a "end of page" flag
to packets.
  • Loading branch information
est31 committed Sep 7, 2018
1 parent fbd68d1 commit f1fcf4b
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions examples/repack.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Ogg decoder and encoder written in Rust
//
// Copyright (c) 2018 est31 <MTest31@outlook.com>
// and contributors. All rights reserved.
// Licensed under MIT license, or Apache 2 license,
// at your option. Please see the LICENSE file
// attached to this source distribution for details.

extern crate ogg;

use std::env;
use ogg::{PacketReader, PacketWriter};
use ogg::writing::PacketWriteEndInfo;
use std::fs::File;

fn main() {
match run() {
Ok(_) =>(),
Err(err) => println!("Error: {}", err),
}
}

macro_rules! btry {
($e:expr) => {
match $e {
Ok(v) => v,
Err(e) => {
println!("Encountered Error: {:?}", e);
break;
},
}
};
}

fn run() -> Result<(), std::io::Error> {
let input_path = env::args().nth(1).expect("No arg for input path found. Please specify a file to open.");
let output_path = env::args().nth(2).expect("No arg for output path found. Please specify a file to save to.");
println!("Opening file: {}", input_path);
println!("Writing to: {}", output_path);
let mut f_i = try!(File::open(input_path));
let mut f_o = try!(File::create(output_path));
let mut pck_rdr = PacketReader::new(&mut f_i);
let mut pck_wtr = PacketWriter::new(&mut f_o);

loop {
let r = btry!(pck_rdr.read_packet());
match r {
Some(pck) => {
let inf = if pck.last_packet {
PacketWriteEndInfo::NormalPacket
} else {
PacketWriteEndInfo::EndStream
};
btry!(pck_wtr.write_packet(pck.data.into_boxed_slice(),
pck.stream_serial,
inf,
pck.absgp_page));
},
// End of stream
None => break,
}
}
Ok(())
}

0 comments on commit f1fcf4b

Please sign in to comment.