Compare commits

...

4 Commits

Author SHA1 Message Date
an 1ddc5a43af internal cleanup 2019-03-12 16:29:03 -04:00
an 53b1e703ab fix load_zlib_header 2019-03-12 16:28:34 -04:00
an 1eb7088a8b make read_data use expr for $b 2019-03-12 16:28:24 -04:00
an 119a45310d rearrange tests 2019-03-12 16:28:08 -04:00
40 changed files with 152 additions and 103 deletions

View File

@ -7,80 +7,80 @@ use std::{fmt, num::NonZeroU16};
#[macro_export]
macro_rules! _durandal_read_impl {
// big endian
(BE $b:ident $nam:ident u16 $n:expr) => {
_durandal_read_impl!($b u16::from_be_bytes, $nam 2 $n);
(BE $b:expr; $nam:ident u16 $n:expr) => {
_durandal_read_impl!($b; u16::from_be_bytes, $nam 2 $n);
};
(BE $b:ident $nam:ident i16 $n:expr) => {
_durandal_read_impl!($b i16::from_be_bytes, $nam 2 $n);
(BE $b:expr; $nam:ident i16 $n:expr) => {
_durandal_read_impl!($b; i16::from_be_bytes, $nam 2 $n);
};
(BE $b:ident $nam:ident u32 $n:expr) => {
_durandal_read_impl!($b u32::from_be_bytes, $nam 4 $n);
(BE $b:expr; $nam:ident u32 $n:expr) => {
_durandal_read_impl!($b; u32::from_be_bytes, $nam 4 $n);
};
(BE $b:ident $nam:ident i32 $n:expr) => {
_durandal_read_impl!($b i32::from_be_bytes, $nam 4 $n);
(BE $b:expr; $nam:ident i32 $n:expr) => {
_durandal_read_impl!($b; i32::from_be_bytes, $nam 4 $n);
};
// little endian
(LE $b:ident $nam:ident u16 $n:expr) => {
_durandal_read_impl!($b u16::from_le_bytes, $nam 2 $n);
(LE $b:expr; $nam:ident u16 $n:expr) => {
_durandal_read_impl!($b; u16::from_le_bytes, $nam 2 $n);
};
(LE $b:ident $nam:ident i16 $n:expr) => {
_durandal_read_impl!($b i16::from_le_bytes, $nam 2 $n);
(LE $b:expr; $nam:ident i16 $n:expr) => {
_durandal_read_impl!($b; i16::from_le_bytes, $nam 2 $n);
};
(LE $b:ident $nam:ident u32 $n:expr) => {
_durandal_read_impl!($b u32::from_le_bytes, $nam 4 $n);
(LE $b:expr; $nam:ident u32 $n:expr) => {
_durandal_read_impl!($b; u32::from_le_bytes, $nam 4 $n);
};
(LE $b:ident $nam:ident i32 $n:expr) => {
_durandal_read_impl!($b i32::from_le_bytes, $nam 4 $n);
(LE $b:expr; $nam:ident i32 $n:expr) => {
_durandal_read_impl!($b; i32::from_le_bytes, $nam 4 $n);
};
// either endianness
($e:ident $b:ident $nam:ident Angle $n:expr) => {
_durandal_read_impl!($e $b $nam u16 $n);
($e:ident $b:expr; $nam:ident Angle $n:expr) => {
_durandal_read_impl!($e $b; $nam u16 $n);
let $nam = Angle::from_bits($nam);
};
($e:ident $b:ident $nam:ident Fixed $n:expr) => {
_durandal_read_impl!($e $b $nam u32 $n);
($e:ident $b:expr; $nam:ident Fixed $n:expr) => {
_durandal_read_impl!($e $b; $nam u32 $n);
let $nam = Fixed::from_bits($nam);
};
($e:ident $b:ident $nam:ident Unit $n:expr) => {
_durandal_read_impl!($e $b $nam u16 $n);
($e:ident $b:expr; $nam:ident Unit $n:expr) => {
_durandal_read_impl!($e $b; $nam u16 $n);
let $nam = Unit::from_bits($nam);
};
($e:ident $b:ident $nam:ident OptU16 $n:expr) => {
_durandal_read_impl!($e $b $nam u16 $n);
($e:ident $b:expr; $nam:ident OptU16 $n:expr) => {
_durandal_read_impl!($e $b; $nam u16 $n);
let $nam = OptU16::from_repr($nam);
};
($e:ident $b:ident $nam:ident usize u16 $n:expr) => {
_durandal_read_impl!($e $b $nam u16 $n);
($e:ident $b:expr; $nam:ident usize u16 $n:expr) => {
_durandal_read_impl!($e $b; $nam u16 $n);
let $nam = usize::from($nam);
};
($e:ident $b:ident $nam:ident usize u32 $n:expr) => {
_durandal_read_impl!($e $b $nam u32 $n);
($e:ident $b:expr; $nam:ident usize u32 $n:expr) => {
_durandal_read_impl!($e $b; $nam u32 $n);
let $nam = usize_from_u32($nam);
};
// no endianness
($_:ident $b:ident $nam:ident u8 $n:expr) => {let $nam = $b[$n];};
($_:ident $b:ident $nam:ident slice u8 $n:expr) => {let $nam = &$b[$n];};
($_:ident $b:ident $nam:ident i8 $n:expr) => {
($_:ident $b:expr; $nam:ident u8 $n:expr) => {let $nam = $b[$n];};
($_:ident $b:expr; $nam:ident slice u8 $n:expr) => {let $nam = &$b[$n];};
($_:ident $b:expr; $nam:ident i8 $n:expr) => {
let $nam = i8::from_ne_bytes([$b[$n]]);
};
($_:ident $b:ident $nam:ident Ident $n:expr) => {
($_:ident $b:expr; $nam:ident Ident $n:expr) => {
let $nam = Ident([$b[$n], $b[$n + 1], $b[$n + 2], $b[$n + 3]]);
};
($_:ident $b:ident $nam:ident $f:ident $n:expr) => {
($_:ident $b:expr; $nam:ident $f:ident $n:expr) => {
let $nam = $f(&$b[$n])?;
};
($_:ident $b:ident $nam:ident no_try $f:ident $n:expr) => {
($_:ident $b:expr; $nam:ident no_try $f:ident $n:expr) => {
let $nam = $f(&$b[$n]);
};
// worker - creates let statement
($b:ident $pth:path , $nam:ident 2 $n:expr) => {
($b:expr; $pth:path , $nam:ident 2 $n:expr) => {
let $nam = $pth([$b[$n], $b[$n + 1]]);
};
($b:ident $pth:path , $nam:ident 4 $n:expr) => {
($b:expr; $pth:path , $nam:ident 4 $n:expr) => {
let $nam = $pth([$b[$n], $b[$n + 1], $b[$n + 2], $b[$n + 3]]);
};
}
@ -159,11 +159,11 @@ macro_rules! _durandal_read_impl {
#[macro_export]
macro_rules! read_data {
(
$sz:expr , $ty:ident in $b:ident =>
$sz:expr , $ty:ident in $b:expr =>
$( $nam:ident = $t:ident [ $n:expr ] $( $ex:ident )* ; )*
) => {
$crate::check_data!($sz, $b);
$($crate::_durandal_read_impl!($ty $b $nam $($ex)* $t $n);)*
$($crate::_durandal_read_impl!($ty $b; $nam $($ex)* $t $n);)*
};
}
@ -171,7 +171,7 @@ macro_rules! read_data {
#[macro_export]
macro_rules! check_data {
(
$sz:expr , $b:ident
$sz:expr , $b:expr
) => {
if $b.len() < $sz {
return Err(err_msg("not enough data"));

View File

@ -18,7 +18,8 @@ fn crc_init() -> [u32; 256]
t
}
/// Creates a CRC-32 of all bytes in `b` with the starting sum `s`.
/// Creates a CRC-32 of all bytes in `b` with the starting sum `s`. The
/// polynomial used is the one from ISO-3309.
///
/// # Examples
///

View File

@ -18,10 +18,18 @@ pub fn load_zlib_header(b: &[u8]) -> ResultS<usize>
}
let cm = cmf & CM;
let cinfo = cmf & CINFO >> 4;
let cinfo = cmf & CINFO;
if cm != 8 || fcheck % 31 != 0 || cinfo > 7 {
bail!("not zlib format");
if cm != 8 {
bail!("unknown compression method");
}
if cinfo > 7 << 4 {
bail!("lz77 window size logarithm is invalid");
}
if fcheck % 31 != 0 {
bail!("invalid fcheck");
}
if flg & FDICT != 0 {

View File

@ -21,6 +21,7 @@ fn read_pm_header<'a>(b: &'a [u8],
}
let pack_t = PackType::from_repr(pack_t)?;
let depth = Depth::from_repr(depth)?;
if pt_fl & 0x8000 == 0 {
bail!("PICT1 not supported");
@ -48,8 +49,8 @@ fn read_pm_header<'a>(b: &'a [u8],
}
let rle = pack_t == PackType::Default ||
pack_t == PackType::Rle16 && depth == 16 ||
pack_t == PackType::Rle32 && depth == 32;
pack_t == PackType::Rle16 && depth == Depth::Bits16 ||
pack_t == PackType::Rle32 && depth == Depth::Bits32;
let pitch = usize::from(pt_fl & 0x3FFF);
@ -62,7 +63,7 @@ fn read_pm_ind(mut im: Image8, b: &[u8], hdr: Header) -> ResultS<Image8>
let clut = ok!(hdr.clut, "no CLUT in indexed mode")?;
let mut p = 0;
if hdr.pitch < 8 && hdr.depth == 8 {
if hdr.pitch < 8 && hdr.depth == Depth::Bits8 {
// uncompressed 8-bit colormap indices
for _ in 0..im.h() {
for _ in 0..im.w() {
@ -80,7 +81,11 @@ fn read_pm_ind(mut im: Image8, b: &[u8], hdr: Header) -> ResultS<Image8>
for _ in 0..im.h() {
let (d, pp) = read_rle::<u8>(&b[p..], hdr.pitch)?;
let d = if hdr.depth < 8 {expand_data(d, hdr.depth)?} else {d};
let d = if hdr.depth < Depth::Bits8 {
expand_data(d, hdr.depth)?
} else {
d
};
p += pp;
@ -191,10 +196,12 @@ fn read_pm_area(im: Image8, b: &[u8], pack: bool, clip: bool)
let (b, hdr) = read_pm_header(&b[p..], pack, clip, &im)?;
match hdr.depth {
1 | 2 | 4 | 8 => read_pm_ind(im, b, hdr),
16 => read_pm_16(im, b, hdr),
32 => read_pm_32(im, b, hdr),
_ => bail!("invalid bit depth"),
Depth::Bits1 |
Depth::Bits2 |
Depth::Bits4 |
Depth::Bits8 => read_pm_ind(im, b, hdr),
Depth::Bits16 => read_pm_16(im, b, hdr),
Depth::Bits32 => read_pm_32(im, b, hdr),
}
}
@ -429,20 +436,32 @@ impl ReadRleData for u8
}
/// Expand packed pixel data based on bit depth.
fn expand_data(b: Vec<u8>, depth: u16) -> ResultS<Vec<u8>>
fn expand_data(b: Vec<u8>, depth: Depth) -> ResultS<Vec<u8>>
{
let mut o = Vec::with_capacity(match depth {
4 => b.len() * 2,
2 => b.len() * 4,
1 => b.len() * 8,
Depth::Bits4 => b.len() * 2,
Depth::Bits2 => b.len() * 4,
Depth::Bits1 => b.len() * 8,
_ => bail!("invalid bit depth"),
});
for ch in b {
match depth {
4 => {for i in (0..=1).rev() {o.push(ch >> (i * 4) & 0xF_u8);}}
2 => {for i in (0..=3).rev() {o.push(ch >> (i * 2) & 0x3_u8);}}
1 => {for i in (0..=7).rev() {o.push(ch >> i & 0x1_u8);}}
Depth::Bits4 => {
for i in (0..=1).rev() {
o.push(ch >> (i * 4) & 0xF_u8);
}
}
Depth::Bits2 => {
for i in (0..=3).rev() {
o.push(ch >> (i * 2) & 0x3_u8);
}
}
Depth::Bits1 => {
for i in (0..=7).rev() {
o.push(ch >> i & 0x1_u8);
}
}
_ => bail!("invalid bit depth"),
}
}
@ -454,11 +473,24 @@ struct Header
{
pitch: usize,
pack_t: PackType,
depth: u16,
depth: Depth,
clut: Option<Vec<Color8>>,
rle: bool,
}
c_enum! {
#[derive(PartialEq, PartialOrd)]
enum Depth: u16
{
1 => Bits1,
2 => Bits2,
4 => Bits4,
8 => Bits8,
16 => Bits16,
32 => Bits32,
}
}
c_enum! {
#[derive(PartialEq)]
enum PackType: u16

View File

@ -1,11 +1,11 @@
const RANDOM: [&[u8]; 7] = [
include_bytes!("random1.bin"),
include_bytes!("random2.bin"),
include_bytes!("random3.bin"),
include_bytes!("random4.bin"),
include_bytes!("random5.bin"),
include_bytes!("random6.bin"),
include_bytes!("random7.bin")
include_bytes!("rand/random1.in"),
include_bytes!("rand/random2.in"),
include_bytes!("rand/random3.in"),
include_bytes!("rand/random4.in"),
include_bytes!("rand/random5.in"),
include_bytes!("rand/random6.in"),
include_bytes!("rand/random7.in")
];
// EOF

View File

@ -2,58 +2,66 @@ use maraiah::marathon::defl;
include!("data/rand.rs");
#[test]
fn defl_alice()
fn defl_gzip(inp: &[u8], out: &[u8])
{
const IN_1: &[u8] = include_bytes!("data/alice1.in");
const IN_2: &[u8] = include_bytes!("data/alice2.in");
const OUT: &[u8] = include_bytes!("data/alice.out");
let b = &inp[defl::load_gzip_header(inp).unwrap()..];
assert_eq!(defl::load_deflate(b).unwrap().1, out.to_vec());
}
let p = defl::load_gzip_header(IN_1).unwrap();
assert_eq!(p, 0x14);
fn defl_alice(inp: &[u8])
{
const OUTPUT: &[u8] = include_bytes!("data/defl/alice.out");
let b = &IN_1[p..];
assert_eq!(defl::load_deflate(b).unwrap().1, OUT.to_vec());
defl_gzip(inp, OUTPUT);
}
let p = defl::load_gzip_header(IN_2).unwrap();
assert_eq!(p, 0x14);
#[test]
fn defl_alice_1()
{
const INPUT: &[u8] = include_bytes!("data/defl/alice1.in");
let b = &IN_2[p..];
assert_eq!(defl::load_deflate(b).unwrap().1, OUT.to_vec());
defl_alice(INPUT);
}
#[test]
fn defl_alice_2()
{
const INPUT: &[u8] = include_bytes!("data/defl/alice2.in");
defl_alice(INPUT);
}
#[test]
fn defl_shapes()
{
const INPUT: &[u8] = include_bytes!("data/Shapes.in");
const OUTPUT: &[u8] = include_bytes!("data/Shapes.out");
const INPUT: &[u8] = include_bytes!("data/defl/Shapes.in");
const OUTPUT: &[u8] = include_bytes!("data/defl/Shapes.out");
let b = &INPUT[defl::load_gzip_header(INPUT).unwrap()..];
assert_eq!(defl::load_deflate(b).unwrap().1, OUTPUT.to_vec());
defl_gzip(INPUT, OUTPUT);
}
#[test]
fn defl_must_succeed()
fn defl_good_gzip_headers()
{
assert!(defl::load_gzip_header(include_bytes!("data/gzipok1.bin")).is_ok());
assert!(defl::load_gzip_header(include_bytes!("data/gzipok2.bin")).is_ok());
assert!(defl::load_gzip_header(include_bytes!("data/gzipok3.bin")).is_ok());
defl::load_gzip_header(include_bytes!("data/gz/ok1.in")).unwrap();
defl::load_gzip_header(include_bytes!("data/gz/ok2.in")).unwrap();
defl::load_gzip_header(include_bytes!("data/gz/ok3.in")).unwrap();
}
#[test]
fn defl_must_not_succeed()
fn defl_bad_gzip_headers()
{
for inp in &RANDOM {
assert!(defl::load_gzip_header(inp).is_err());
}
assert!(defl::load_gzip_header(include_bytes!("data/gzbad1.bin")).is_err());
assert!(defl::load_gzip_header(include_bytes!("data/gzbad2.bin")).is_err());
assert!(defl::load_gzip_header(include_bytes!("data/gzbad3.bin")).is_err());
assert!(defl::load_gzip_header(include_bytes!("data/gzbad4.bin")).is_err());
assert!(defl::load_gzip_header(include_bytes!("data/gzbad5.bin")).is_err());
assert!(defl::load_gzip_header(include_bytes!("data/gzbad6.bin")).is_err());
assert!(defl::load_gzip_header(include_bytes!("data/gzbad7.bin")).is_err());
assert!(defl::load_gzip_header(include_bytes!("data/gz/bad1.in")).is_err());
assert!(defl::load_gzip_header(include_bytes!("data/gz/bad2.in")).is_err());
assert!(defl::load_gzip_header(include_bytes!("data/gz/bad3.in")).is_err());
assert!(defl::load_gzip_header(include_bytes!("data/gz/bad4.in")).is_err());
assert!(defl::load_gzip_header(include_bytes!("data/gz/bad5.in")).is_err());
assert!(defl::load_gzip_header(include_bytes!("data/gz/bad6.in")).is_err());
assert!(defl::load_gzip_header(include_bytes!("data/gz/bad7.in")).is_err());
}
#[test]

View File

@ -5,7 +5,7 @@ include!("data/rand.rs");
#[test]
fn read_minf_must_process()
{
const INPUT: &[u8] = include_bytes!("data/minf.in");
const INPUT: &[u8] = include_bytes!("data/map/minf.in");
let out = map::Minf{texture_id: 0,
physics_id: 1,
@ -21,8 +21,8 @@ fn read_minf_must_process()
#[test]
fn read_epnt_must_process()
{
const INPUT: &[u8] = include_bytes!("data/epnt.in");
const OUTPUT: [map::Point; 54] = include!("data/epnt.out");
const INPUT: &[u8] = include_bytes!("data/map/epnt.in");
const OUTPUT: [map::Point; 54] = include!("data/map/epnt.out");
assert_eq!(bin::rd_array(INPUT, map::read_epnt).unwrap(), OUTPUT.to_vec());
}

View File

@ -5,7 +5,7 @@ include!("data/rand.rs");
#[test]
fn machdr_must_process()
{
const INPUT: &[u8] = include_bytes!("data/macbin.in");
const INPUT: &[u8] = include_bytes!("data/misc/macbin.in");
assert_eq!(machdr::check_macbin(INPUT), Some(128));
assert_eq!(machdr::try_mac_header(INPUT), 128);

View File

@ -5,8 +5,8 @@ include!("data/rand.rs");
#[test]
fn get_clut_must_process()
{
const INPUT: &[u8] = include_bytes!("data/clut.in");
const OUTPUT: [Color8; 256] = include!("data/clut.out");
const INPUT: &[u8] = include_bytes!("data/pict/clut.in");
const OUTPUT: [Color8; 256] = include!("data/pict/clut.out");
assert_eq!(pict::get_clut(INPUT).unwrap(), (OUTPUT.to_vec(), 2056));
}

View File

@ -5,10 +5,10 @@ include!("data/rand.rs");
#[test]
fn read_term_must_process()
{
const INPUT: &[u8] = include_bytes!("data/term.in");
const INPUT: &[u8] = include_bytes!("data/trm/term.in");
let inp = bin::rd_array(INPUT, trm::read_term).unwrap();
let out = include!("data/term.out");
let out = include!("data/trm/term.out");
// for better debug output, we iterate over each item
assert_eq!(inp.len(), out.len());