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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
use std::io::{self, Write};
pub enum Bits {
Some(usize, u16),
None(usize),
}
pub trait BitReader {
fn read_bits(&mut self, buf: &[u8], n: u8) -> Bits;
}
pub trait BitWriter: Write {
fn write_bits(&mut self, v: u16, n: u8) -> io::Result<()>;
}
macro_rules! define_bit_readers {
{$(
$name:ident, #[$doc:meta];
)*} => {
$(
#[$doc]
#[derive(Debug)]
pub struct $name {
bits: u8,
acc: u32,
}
impl $name {
pub fn new() -> $name {
$name {
bits: 0,
acc: 0,
}
}
}
)*
}
}
define_bit_readers!{
LsbReader, #[doc = "Reads bits from a byte stream, LSB first."];
MsbReader, #[doc = "Reads bits from a byte stream, MSB first."];
}
impl BitReader for LsbReader {
fn read_bits(&mut self, mut buf: &[u8], n: u8) -> Bits {
if n > 16 {
panic!("Cannot read more than 16 bits")
}
let mut consumed = 0;
while self.bits < n {
let byte = if buf.len() > 0 {
let byte = buf[0];
buf = &buf[1..];
byte
} else {
return Bits::None(consumed)
};
self.acc |= (byte as u32) << self.bits;
self.bits += 8;
consumed += 1;
}
let res = self.acc & ((1 << n) - 1);
self.acc >>= n;
self.bits -= n;
Bits::Some(consumed, res as u16)
}
}
impl BitReader for MsbReader {
fn read_bits(&mut self, mut buf: &[u8], n: u8) -> Bits {
if n > 16 {
panic!("Cannot read more than 16 bits")
}
let mut consumed = 0;
while self.bits < n {
let byte = if buf.len() > 0 {
let byte = buf[0];
buf = &buf[1..];
byte
} else {
return Bits::None(consumed)
};
self.acc |= (byte as u32) << (24 - self.bits);
self.bits += 8;
consumed += 1;
}
let res = self.acc >> (32 - n);
self.acc <<= n;
self.bits -= n;
Bits::Some(consumed, res as u16)
}
}
macro_rules! define_bit_writers {
{$(
$name:ident, #[$doc:meta];
)*} => {
$(
#[$doc]
#[allow(dead_code)]
pub struct $name<W: Write> {
w: W,
bits: u8,
acc: u32,
}
impl<W: Write> $name<W> {
#[allow(dead_code)]
pub fn new(writer: W) -> $name<W> {
$name {
w: writer,
bits: 0,
acc: 0,
}
}
}
impl<W: Write> Write for $name<W> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
if self.acc == 0 {
self.w.write(buf)
} else {
for &byte in buf.iter() {
try!(self.write_bits(byte as u16, 8))
}
Ok(buf.len())
}
}
fn flush(&mut self) -> io::Result<()> {
let missing = 8 - self.bits;
if missing > 0 {
try!(self.write_bits(0, missing));
}
self.w.flush()
}
}
)*
}
}
define_bit_writers!{
LsbWriter, #[doc = "Writes bits to a byte stream, LSB first."];
MsbWriter, #[doc = "Writes bits to a byte stream, MSB first."];
}
impl<W: Write> BitWriter for LsbWriter<W> {
fn write_bits(&mut self, v: u16, n: u8) -> io::Result<()> {
self.acc |= (v as u32) << self.bits;
self.bits += n;
while self.bits >= 8 {
try!(self.w.write_all(&[self.acc as u8]));
self.acc >>= 8;
self.bits -= 8
}
Ok(())
}
}
impl<W: Write> BitWriter for MsbWriter<W> {
fn write_bits(&mut self, v: u16, n: u8) -> io::Result<()> {
self.acc |= (v as u32) << (32 - n - self.bits);
self.bits += n;
while self.bits >= 8 {
try!(self.w.write_all(&[(self.acc >> 24) as u8]));
self.acc <<= 8;
self.bits -= 8
}
Ok(())
}
}
#[cfg(test)]
mod test {
use super::{BitReader, BitWriter, Bits};
#[test]
fn reader_writer() {
let data = [255, 20, 40, 120, 128];
let mut offset = 0;
let mut expanded_data = Vec::new();
let mut reader = super::LsbReader::new();
while let Bits::Some(consumed, b) = reader.read_bits(&data[offset..], 10) {
offset += consumed;
expanded_data.push(b)
}
let mut compressed_data = Vec::new();
{
let mut writer = super::LsbWriter::new(&mut compressed_data);
for &datum in expanded_data.iter() {
let _ = writer.write_bits(datum, 10);
}
}
assert_eq!(&data[..], &compressed_data[..])
}
}