Skip to content

Commit db52e23

Browse files
committed
Revert changes to binio.rs
1 parent 4d58df5 commit db52e23

2 files changed

Lines changed: 63 additions & 139 deletions

File tree

cz/src/binio.rs

Lines changed: 54 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -1,189 +1,116 @@
1-
use std::io::{self, Read, Write};
2-
3-
use byteorder::{ReadBytesExt, WriteBytesExt};
4-
5-
/// A simple way to write individual bits to an input implementing [Write].
6-
pub struct BitWriter<'a, O: Write + WriteBytesExt> {
7-
output: &'a mut O,
8-
9-
current_byte: u8,
10-
1+
pub struct BitIo {
2+
data: Vec<u8>,
113
byte_offset: usize,
124
bit_offset: usize,
135

146
byte_size: usize,
157
}
168

17-
impl<'a, O: Write + WriteBytesExt> BitWriter<'a, O> {
18-
/// Create a new BitWriter wrapper around something which
19-
/// implements [Write].
20-
pub fn new(output: &'a mut O) -> Self {
9+
impl BitIo {
10+
/// Create a new BitIO reader and writer over some data
11+
pub fn new(data: Vec<u8>) -> Self {
2112
Self {
22-
output,
23-
24-
current_byte: 0,
25-
13+
data,
2614
byte_offset: 0,
2715
bit_offset: 0,
28-
2916
byte_size: 0,
3017
}
3118
}
3219

33-
/// Get the number of whole bytes written to the stream.
34-
pub fn byte_size(&self) -> usize {
35-
self.byte_size
36-
}
37-
38-
/// Get the bit offset within the current byte.
39-
pub fn bit_offset(&self) -> u8 {
40-
self.bit_offset as u8
20+
/// Get the byte offset of the reader
21+
pub fn byte_offset(&self) -> usize {
22+
self.byte_offset
4123
}
4224

43-
/// Check if the stream is aligned to a byte.
44-
pub fn aligned(&self) -> bool {
45-
self.bit_offset() == 0
25+
/// Get the byte size of the reader
26+
pub fn byte_size(&self) -> usize {
27+
self.byte_size
4628
}
4729

48-
/// Align the writer to the nearest byte by padding with zero bits.
49-
///
50-
/// Returns the number of zero bits
51-
pub fn flush(&mut self) -> Result<usize, io::Error> {
52-
self.byte_offset += 1;
53-
54-
// Write out the current byte unfinished
55-
self.output.write_u8(self.current_byte).unwrap();
56-
self.current_byte = 0;
57-
self.bit_offset = 0;
58-
59-
Ok(8 - self.bit_offset)
30+
/// Get the current bytes up to `byte_size` in the reader
31+
pub fn bytes(&self) -> Vec<u8> {
32+
self.data[..self.byte_size].to_vec()
6033
}
6134

62-
/// Write some bits to the output.
63-
pub fn write_bit(&mut self, data: u64, bit_len: usize) {
64-
if bit_len > 64 {
65-
panic!("Cannot write more than 64 bits at once.");
66-
} else if bit_len == 0 {
67-
panic!("Must write 1 or more bits.")
35+
/// Read some bits from the buffer
36+
pub fn read_bit(&mut self, bit_len: usize) -> u64 {
37+
if bit_len > 8 * 8 {
38+
panic!("Cannot read more than 64 bits")
6839
}
6940

7041
if bit_len % 8 == 0 && self.bit_offset == 0 {
71-
self.write(data, bit_len / 8);
72-
return;
42+
return self.read(bit_len / 8);
7343
}
7444

45+
let mut result = 0;
7546
for i in 0..bit_len {
76-
let bit_value = (data >> i) & 1;
77-
78-
self.current_byte &= !(1 << self.bit_offset);
79-
80-
self.current_byte |= (bit_value << self.bit_offset) as u8;
81-
47+
let bit_value = ((self.data[self.byte_offset] as usize >> self.bit_offset) & 1) as u64;
8248
self.bit_offset += 1;
83-
if self.bit_offset >= 8 {
49+
50+
if self.bit_offset == 8 {
8451
self.byte_offset += 1;
8552
self.bit_offset = 0;
86-
87-
self.output.write_u8(self.current_byte).unwrap();
88-
self.current_byte = 0;
8953
}
54+
55+
result |= bit_value << i;
9056
}
9157

92-
self.byte_size = self.byte_offset + (self.bit_offset + 7) / 8;
58+
result
9359
}
9460

95-
/// Write some bytes to the output.
96-
pub fn write(&mut self, data: u64, byte_len: usize) {
61+
/// Read some bytes from the buffer
62+
pub fn read(&mut self, byte_len: usize) -> u64 {
9763
if byte_len > 8 {
98-
panic!("Cannot write more than 8 bytes at once.")
99-
} else if byte_len == 0 {
100-
panic!("Must write 1 or more bytes.")
64+
panic!("Cannot read more than 8 bytes")
10165
}
10266

103-
self.output
104-
.write_all(&data.to_le_bytes()[..byte_len])
105-
.unwrap();
67+
let mut padded_slice = [0u8; 8];
68+
padded_slice.copy_from_slice(&self.data[self.byte_offset..self.byte_offset + byte_len]);
10669
self.byte_offset += byte_len;
10770

108-
self.byte_size = self.byte_offset + (self.bit_offset + 7) / 8;
71+
u64::from_le_bytes(padded_slice)
10972
}
110-
}
111-
112-
/// A simple way to read individual bits from an input implementing [Read].
113-
pub struct BitReader<'a, I: Read + ReadBytesExt> {
114-
input: &'a mut I,
11573

116-
current_byte: Option<u8>,
117-
118-
byte_offset: usize,
119-
bit_offset: usize,
120-
}
121-
122-
impl<'a, I: Read + ReadBytesExt> BitReader<'a, I> {
123-
/// Create a new BitReader wrapper around something which
124-
/// implements [Write].
125-
pub fn new(input: &'a mut I) -> Self {
126-
let first = input.read_u8().unwrap();
127-
Self {
128-
input,
129-
130-
current_byte: Some(first),
131-
132-
byte_offset: 0,
133-
bit_offset: 0,
134-
}
135-
}
136-
137-
/// Get the number of whole bytes read from the stream.
138-
pub fn byte_offset(&self) -> usize {
139-
self.byte_offset
140-
}
141-
142-
/// Read some bits from the input.
143-
pub fn read_bit(&mut self, bit_len: usize) -> u64 {
144-
if bit_len > 64 {
145-
panic!("Cannot read more than 64 bits at once.")
146-
} else if bit_len == 0 {
147-
panic!("Must read 1 or more bits.")
74+
/// Write some bits to the buffer
75+
pub fn write_bit(&mut self, data: u64, bit_len: usize) {
76+
if bit_len > 8 * 8 {
77+
panic!("Cannot write more than 64 bits");
14878
}
14979

15080
if bit_len % 8 == 0 && self.bit_offset == 0 {
151-
return self.read(bit_len / 8);
81+
self.write(data, bit_len / 8);
82+
return;
15283
}
15384

154-
let mut result = 0;
15585
for i in 0..bit_len {
156-
let bit_value = ((self.current_byte.unwrap() as usize >> self.bit_offset) & 1) as u64;
157-
self.bit_offset += 1;
86+
let bit_value = (data >> i) & 1;
87+
88+
self.data[self.byte_offset] &= !(1 << self.bit_offset);
89+
90+
self.data[self.byte_offset] |= (bit_value << self.bit_offset) as u8;
15891

92+
self.bit_offset += 1;
15993
if self.bit_offset == 8 {
16094
self.byte_offset += 1;
16195
self.bit_offset = 0;
162-
163-
self.current_byte = Some(self.input.read_u8().unwrap());
16496
}
165-
166-
result |= bit_value << i;
16797
}
16898

169-
result
99+
self.byte_size = self.byte_offset + (self.bit_offset + 7) / 8;
170100
}
171101

172-
/// Read some bytes from the input.
173-
pub fn read(&mut self, byte_len: usize) -> u64 {
102+
pub fn write(&mut self, data: u64, byte_len: usize) {
174103
if byte_len > 8 {
175-
panic!("Cannot read more than 8 bytes at once.")
176-
} else if byte_len == 0 {
177-
panic!("Must read 1 or more bytes")
104+
panic!("Cannot write more than 8 bytes")
178105
}
179106

180-
let mut padded_slice = vec![0u8; byte_len];
181-
self.input.read_exact(&mut padded_slice).unwrap();
182-
self.byte_offset += byte_len;
107+
let mut padded_slice = [0u8; 8];
108+
padded_slice.copy_from_slice(&data.to_le_bytes());
183109

184-
let extra_length = padded_slice.len() - byte_len;
185-
padded_slice.extend_from_slice(&vec![0u8; extra_length]);
110+
self.data[self.byte_offset..self.byte_offset + byte_len]
111+
.copy_from_slice(&padded_slice[..byte_len]);
112+
self.byte_offset += byte_len;
186113

187-
u64::from_le_bytes(padded_slice.try_into().unwrap())
114+
self.byte_size = self.byte_offset + (self.bit_offset + 7) / 8;
188115
}
189116
}

cz/src/compression.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::{
44
io::{Cursor, Read, Seek, Write},
55
};
66

7-
use crate::binio::{BitReader, BitWriter};
7+
use crate::binio::BitIo;
88
use crate::common::CzError;
99

1010
/// The size of compressed data in each chunk
@@ -163,7 +163,7 @@ pub fn decompress2<T: Seek + ReadBytesExt + Read>(
163163
}
164164

165165
fn decompress_lzw2(input_data: &[u8], size: usize) -> Vec<u8> {
166-
let mut data = Cursor::new(input_data);
166+
let data = input_data;
167167
let mut dictionary = HashMap::new();
168168
for i in 0..256 {
169169
dictionary.insert(i as u64, vec![i as u8]);
@@ -172,7 +172,7 @@ fn decompress_lzw2(input_data: &[u8], size: usize) -> Vec<u8> {
172172
let mut result = Vec::with_capacity(size);
173173

174174
let data_size = input_data.len();
175-
let mut bit_io = BitReader::new(&mut data);
175+
let mut bit_io = BitIo::new(data.to_vec());
176176
let mut w = dictionary.get(&0).unwrap().clone();
177177

178178
let mut element;
@@ -362,9 +362,9 @@ fn compress_lzw2(data: &[u8], last: Vec<u8>) -> (usize, Vec<u8>, Vec<u8>) {
362362
element = last
363363
}
364364

365-
let mut output_buf = Vec::new();
366-
let mut bit_io = BitWriter::new(&mut output_buf);
367-
let write_bit = |bit_io: &mut BitWriter<Vec<u8>>, code: u64| {
365+
let output_buf = Vec::new();
366+
let mut bit_io = BitIo::new(output_buf);
367+
let write_bit = |bit_io: &mut BitIo, code: u64| {
368368
if code > 0x7FFF {
369369
bit_io.write_bit(1, 1);
370370
bit_io.write_bit(code, 18);
@@ -403,17 +403,14 @@ fn compress_lzw2(data: &[u8], last: Vec<u8>) -> (usize, Vec<u8>, Vec<u8>) {
403403
}
404404
}
405405

406-
bit_io.flush().unwrap();
407-
return (count, output_buf, Vec::new());
406+
return (count, bit_io.bytes(), Vec::new());
408407
} else if bit_io.byte_size() < 0x87BDF {
409408
if !last_element.is_empty() {
410409
write_bit(&mut bit_io, *dictionary.get(&last_element).unwrap());
411410
}
412411

413-
bit_io.flush().unwrap();
414-
return (count, output_buf, Vec::new());
412+
return (count, bit_io.bytes(), Vec::new());
415413
}
416414

417-
bit_io.flush().unwrap();
418-
(count, output_buf, last_element)
415+
(count, bit_io.bytes(), last_element)
419416
}

0 commit comments

Comments
 (0)