1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use std::io::Read;
use std::u64;
use byteorder::{LittleEndian, ReadBytesExt};

use {Error};
use consts::MINIMUM_DICTIONARY_SIZE;

/// LZMA model properties.
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub struct Properties {
	/// Literal context bits.
	pub lc: u8,

	/// Literal position bits.
	pub lp: u8,

	/// Position bits.
	pub pb: u8,

	/// Dictionary size.
	pub dictionary: u32,

	/// Uncompressed size if present.
	pub uncompressed: Option<u64>,
}

/// Read the model properties from a stream.
pub fn read<T: Read>(mut stream: T) -> Result<Properties, Error> {
	let d = try!(stream.read_u8());

	if d >= (9 * 5 * 5) {
		return Err(Error::InvalidProperties);
	}

	let lc = d % 9;
	let d  = d / 9;
	let pb = d / 5;
	let lp = d % 5;

	let dictionary = match try!(stream.read_u32::<LittleEndian>()) {
		n if n < MINIMUM_DICTIONARY_SIZE =>
			MINIMUM_DICTIONARY_SIZE,

		n =>
			n
	};

	let uncompressed = match try!(stream.read_u64::<LittleEndian>()) {
		u64::MAX =>
			None,

		n =>
			Some(n)
	};

	Ok(Properties {
		lc: lc,
		lp: lp,
		pb: pb,

		dictionary:   dictionary,
		uncompressed: uncompressed,
	})
}