1 use crate::bitstream::LsbWriter;
2 use byteorder::{LittleEndian, WriteBytesExt};
3 use std::io;
4 use std::io::Write;
5 use std::u16;
6
7 #[cfg(test)]
8 const BLOCK_SIZE: u16 = 32000;
9
10 const STORED_FIRST_BYTE: u8 = 0b0000_0000;
11 pub const STORED_FIRST_BYTE_FINAL: u8 = 0b0000_0001;
12 pub const MAX_STORED_BLOCK_LENGTH: usize = (u16::MAX as usize) / 2;
13
write_stored_header(writer: &mut LsbWriter, final_block: bool)14 pub fn write_stored_header(writer: &mut LsbWriter, final_block: bool) {
15 let header = if final_block {
16 STORED_FIRST_BYTE_FINAL
17 } else {
18 STORED_FIRST_BYTE
19 };
20 // Write the block header
21 writer.write_bits(header.into(), 3);
22 // Flush the writer to make sure we are aligned to the byte boundary.
23 writer.flush_raw();
24 }
25
26 // Compress one stored block (excluding the header)
compress_block_stored<W: Write>(input: &[u8], writer: &mut W) -> io::Result<usize>27 pub fn compress_block_stored<W: Write>(input: &[u8], writer: &mut W) -> io::Result<usize> {
28 if input.len() > u16::max_value() as usize {
29 return Err(io::Error::new(
30 io::ErrorKind::InvalidInput,
31 "Stored block too long!",
32 ));
33 };
34 // The header is written before this function.
35 // The next two bytes indicates the length
36 writer.write_u16::<LittleEndian>(input.len() as u16)?;
37 // the next two after the length is the ones complement of the length
38 writer.write_u16::<LittleEndian>(!input.len() as u16)?;
39 // After this the data is written directly with no compression
40 writer.write(input)
41 }
42
43 #[cfg(test)]
compress_data_stored(input: &[u8]) -> Vec<u8>44 pub fn compress_data_stored(input: &[u8]) -> Vec<u8> {
45 let block_length = BLOCK_SIZE as usize;
46
47 let mut output = Vec::with_capacity(input.len() + 2);
48 let mut i = input.chunks(block_length).peekable();
49 while let Some(chunk) = i.next() {
50 let last_chunk = i.peek().is_none();
51 // First bit tells us if this is the final chunk
52 // the next two details compression type (none in this case)
53 let first_byte = if last_chunk {
54 STORED_FIRST_BYTE_FINAL
55 } else {
56 STORED_FIRST_BYTE
57 };
58 output.write(&[first_byte]).unwrap();
59
60 compress_block_stored(chunk, &mut output).unwrap();
61 }
62 output
63 }
64
65 #[cfg(test)]
66 mod test {
67 use super::*;
68 use crate::test_utils::decompress_to_end;
69
70 #[test]
no_compression_one_chunk()71 fn no_compression_one_chunk() {
72 let test_data = vec![1u8, 2, 3, 4, 5, 6, 7, 8];
73 let compressed = compress_data_stored(&test_data);
74 let result = decompress_to_end(&compressed);
75 assert_eq!(test_data, result);
76 }
77
78 #[test]
no_compression_multiple_chunks()79 fn no_compression_multiple_chunks() {
80 let test_data = vec![32u8; 40000];
81 let compressed = compress_data_stored(&test_data);
82 let result = decompress_to_end(&compressed);
83 assert_eq!(test_data, result);
84 }
85
86 #[test]
no_compression_string()87 fn no_compression_string() {
88 let test_data = String::from(
89 "This is some text, this is some more text, this is even \
90 more text, lots of text here.",
91 )
92 .into_bytes();
93 let compressed = compress_data_stored(&test_data);
94 let result = decompress_to_end(&compressed);
95 assert_eq!(test_data, result);
96 }
97 }
98