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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
use std::io::*;
use std::marker::PhantomData;

pub trait Bit {
    // support for read_bit
    fn extract_bit(byte: u8) -> (bool,u8);

    // support for read_bits
    fn shift_bit(word: u32) -> u32;
    fn set_bit(word: u32) -> u32;
    fn shift_into_place(word: u32, nbits: usize) -> u32;
}

pub enum MSB {}
pub enum LSB {}

impl Bit for MSB {
    #[inline(always)]
    fn extract_bit(byte: u8) -> (bool,u8) {
        (byte & 0x80 != 0, byte << 1)
    }

    #[inline(always)]
    fn shift_bit(word: u32) -> u32 {
        word << 1
    }

    #[inline(always)]
    fn set_bit(word: u32) -> u32 {
        word | 0x1
    }

    #[inline(always)]
    fn shift_into_place(word: u32, _: usize) -> u32 {
        word
    }
}

impl Bit for LSB {
    #[inline(always)]
    fn extract_bit(byte: u8) -> (bool,u8) {
        (byte & 0x1 != 0, byte >> 1)
    }

    #[inline(always)]
    fn shift_bit(word: u32) -> u32 {
        word >> 1
    }

    #[inline(always)]
    fn set_bit(word: u32) -> u32 {
        word | 0x80000000
    }

    #[inline(always)]
    fn shift_into_place(word: u32, nbits: usize) -> u32 {
        word >> 32-nbits
    }
}

/// A `BitReader` gives a way to read single bits from a stream.
pub struct BitReader<R: Read, B: Bit> {
    _marker: PhantomData<*const B>,
    byte:    [u8; 1],
    shift:   usize,
    r:       R,
}

impl<R: Read, B: Bit> BitReader<R,B> {
    /// Constructs a new `BitReader`. Requires an implementation of  `Bit` type to determine which
    /// end of bytes to read bits from.
    ///
    /// # Arguments
    ///
    /// * reader - an existing open file or stream (implementing `Read`).
    ///
    /// # Examples
    ///
    /// ```ignore
    /// let r = try!(File::open("somefile"));
    /// let mut br: BitReader<_,MSB> = BitReader::new(r);
    /// ```
    pub fn new(reader: R) -> BitReader<R,B> {
        BitReader {
            _marker: PhantomData,
            r:       reader,
            byte:    [0],
            shift:   8,
        }
    }

    /// Reads a single bit from a `BitReader`. Returns `true` for a 1 bit, `false` for a 0 bit.
    ///
    /// # Examples
    ///
    /// ```ignore
    /// let is_one = try!(br.read_bit());
    /// ```
    pub fn read_bit(&mut self) -> Result<bool> {
        if self.shift == 8 {
            let n = try!(self.r.read(&mut self.byte));
            if n == 0 {
                return Err(Error::new(ErrorKind::UnexpectedEof, "Unexpected EOF"));
            }
            self.shift = 0;
        }
        let (bit, byte) = B::extract_bit(self.byte[0]);
        self.byte[0] = byte;
        self.shift = self.shift + 1;
        Ok(bit)
    }

    /// Reads 8 bits from a `BitReader` and returns them as a single byte.
    ///
    /// # Examples
    ///
    /// ```ignore
    /// let byte = try!(br.read_byte());
    /// ```
    pub fn read_byte(&mut self) -> Result<u8> {
        Ok(try!(self.read_bits(8)) as u8)
    }

    /// Read a number of bits from a `BitReader` and return them in a `u32`. Will not read more
    /// than 32 bits.
    ///
    /// # Arguments
    ///
    /// * bits - number of bits to read
    ///
    /// # Examples
    /// ```ignore
    /// let num = try!(br.read_bits(5));
    /// ```
    pub fn read_bits(&mut self, mut nbits: usize) -> Result<u32> {
        if nbits > 32 { nbits = 32 }
        let mut out: u32 = 0;
        for _ in 0..nbits {
            out = B::shift_bit(out);
            if try!(self.read_bit()) {
                out = B::set_bit(out);
            }
        }
        out = B::shift_into_place(out, nbits);
        Ok(out)
    }

    /// Gets a reference to the underlying stream.
    pub fn get_ref(&mut self) -> &R {
        &self.r
    }
}

#[cfg(test)]
mod test {
    use std::io::{Cursor, ErrorKind};
    use super::*;

    macro_rules! assert_eof {
        ($br: ident) => {
            match $br.read_bit() {
                Err(e) => match e.kind() {
                    ErrorKind::UnexpectedEof => (),
                    k => panic!("Expected EOF, got IO error: {:?}", k),
                },
                v => panic!("Expected EOF, got: {:?}", v),
            }
        }
    }

    #[test]
    pub fn read_bit_msb() {
        let r = Cursor::new(vec![0x55,0xaa]);
        let mut br: BitReader<_,MSB> = BitReader::new(r);

        assert_eq!(br.read_bit().unwrap(), false);
        assert_eq!(br.read_bit().unwrap(), true);
        assert_eq!(br.read_bit().unwrap(), false);
        assert_eq!(br.read_bit().unwrap(), true);
        assert_eq!(br.read_bit().unwrap(), false);
        assert_eq!(br.read_bit().unwrap(), true);
        assert_eq!(br.read_bit().unwrap(), false);
        assert_eq!(br.read_bit().unwrap(), true);

        assert_eq!(br.read_bit().unwrap(), true);
        assert_eq!(br.read_bit().unwrap(), false);
        assert_eq!(br.read_bit().unwrap(), true);
        assert_eq!(br.read_bit().unwrap(), false);
        assert_eq!(br.read_bit().unwrap(), true);
        assert_eq!(br.read_bit().unwrap(), false);
        assert_eq!(br.read_bit().unwrap(), true);
        assert_eq!(br.read_bit().unwrap(), false);

        assert_eof!(br);
    }

    #[test]
    pub fn read_byte_msb() {
        let r = Cursor::new(vec![0x55,0xaa,0x55,0xaa]);
        let mut br: BitReader<_,MSB> = BitReader::new(r);

        assert_eq!(br.read_byte().unwrap(), 0x55);
        assert_eq!(br.read_byte().unwrap(), 0xaa);

        assert_eq!(br.read_bit().unwrap(), false);
        assert_eq!(br.read_bit().unwrap(), true);

        assert_eq!(br.read_byte().unwrap(), 0x56);

        assert_eq!(br.read_bit().unwrap(), true);
        assert_eq!(br.read_bit().unwrap(), false);
        assert_eq!(br.read_bit().unwrap(), true);
        assert_eq!(br.read_bit().unwrap(), false);
        assert_eq!(br.read_bit().unwrap(), true);
        assert_eq!(br.read_bit().unwrap(), false);

        assert_eof!(br);
    }

    #[test]
    pub fn read_bits_msb() {
        let r = Cursor::new(vec![0x55]);
        let mut br: BitReader<_,MSB> = BitReader::new(r);

        assert_eq!(br.read_bits(3).unwrap(), 0x2);
        assert_eq!(br.read_bits(5).unwrap(), 0x15);

        assert_eof!(br);
    }

    #[test]
    pub fn read_bit_lsb() {
        let r = Cursor::new(vec![0x55,0xaa]);
        let mut br: BitReader<_,LSB> = BitReader::new(r);

        assert_eq!(br.read_bit().unwrap(), true);
        assert_eq!(br.read_bit().unwrap(), false);
        assert_eq!(br.read_bit().unwrap(), true);
        assert_eq!(br.read_bit().unwrap(), false);
        assert_eq!(br.read_bit().unwrap(), true);
        assert_eq!(br.read_bit().unwrap(), false);
        assert_eq!(br.read_bit().unwrap(), true);
        assert_eq!(br.read_bit().unwrap(), false);

        assert_eq!(br.read_bit().unwrap(), false);
        assert_eq!(br.read_bit().unwrap(), true);
        assert_eq!(br.read_bit().unwrap(), false);
        assert_eq!(br.read_bit().unwrap(), true);
        assert_eq!(br.read_bit().unwrap(), false);
        assert_eq!(br.read_bit().unwrap(), true);
        assert_eq!(br.read_bit().unwrap(), false);
        assert_eq!(br.read_bit().unwrap(), true);

        assert_eof!(br);
    }

    #[test]
    pub fn read_bits_lsb() {
        let r = Cursor::new(vec![0x55]);
        let mut br: BitReader<_,LSB> = BitReader::new(r);

        assert_eq!(br.read_bits(3).unwrap(), 0x5);
        assert_eq!(br.read_bits(5).unwrap(), 0xa);

        assert_eof!(br);
    }
}