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
/**************************************************************************
 *                                                                        *
 * Rust implementation of the X3 lossless audio compression protocol.     *
 *                                                                        *
 * Copyright (C) 2019 Simon M. Werner <simonwerner@gmail.com>             *
 *                                                                        *
 * This program is free software; you can redistribute it and/or modify   *
 * it under the terms of the GNU General Public License as published by   *
 * the Free Software Foundation, either version 3 of the License, or      *
 * (at your option) any later version.                                    *
 *                                                                        *
 * This program is distributed in the hope that it will be useful,        *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of         *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the           *
 * GNU General Public License for more details.                           *
 *                                                                        *
 * You should have received a copy of the GNU General Public License      *
 * along with this program. If not, see <http://www.gnu.org/licenses/>.   *
 *                                                                        *
 **************************************************************************/

// externs
use crate::bitpacker::BitPackError;
use crate::byteorder::{BigEndian, ByteOrder, LittleEndian};
use crate::crc::crc16;

//
// ######                       ######
// #     # #     # ##### ###### #     # ######   ##   #####  ###### #####
// #     #  #   #    #   #      #     # #       #  #  #    # #      #    #
// ######    # #     #   #####  ######  #####  #    # #    # #####  #    #
// #     #    #      #   #      #   #   #      ###### #    # #      #####
// #     #    #      #   #      #    #  #      #    # #    # #      #   #
// ######     #      #   ###### #     # ###### #    # #####  ###### #    #
//

///
/// BitReader allows individual bits to be read from an array of bytes.
///
pub struct ByteReader<'a> {
  array: &'a [u8],
  p_byte: usize, // Byte pointer
}

impl<'a> ByteReader<'a> {
  pub fn new(array: &'a [u8]) -> ByteReader {
    ByteReader { array, p_byte: 0 }
  }

  pub fn reset(&mut self) {
    self.p_byte = 0;
  }

  pub fn set_pos(&mut self, p_byte: usize) {
    self.p_byte = p_byte;
  }

  pub fn get_pos(&self) -> usize {
    self.p_byte
  }

  pub fn find_le_u16(&mut self, word: u16) -> bool {
    if self.p_byte >= self.array.len() {
      return false;
    }

    let b0 = (word >> 8) as u8;
    let b1 = (word & 255) as u8;

    for i in self.p_byte..self.array.len() - 1 {
      let a0 = self.array[i];
      let a1 = self.array[i + 1];
      if a0 == b0 && a1 == b1 {
        return true;
      }
      self.p_byte += 1;
    }
    false
  }

  pub fn extract(&self, p_start: usize, p_end: usize) -> Result<Vec<u8>, BitPackError> {
    if p_start > self.array.len() || p_end > self.array.len() {
      Err(BitPackError::ArrayEndReached)
    } else {
      Ok(self.array[p_start..p_end].to_vec())
    }
  }

  ///
  /// Check if `buf` and ByteReader array at the current read position contain the
  /// same information.
  ///
  /// Note:
  ///
  /// ### Arguments
  /// * `buf` - The array where the bytes will be written to.
  ///
  #[inline(always)]
  pub fn eq(&self, buf: &[u8]) -> bool {
    let mut p = self.p_byte;
    for b in buf {
      if *b != self.array[p] {
        return false;
      };
      p += 1;
    }
    true
  }

  ///
  /// Get the number of bytes remaining in the ByteReader buffer.
  ///
  #[inline(always)]
  pub fn remaining_bytes(&self) -> Result<usize, BitPackError> {
    if self.p_byte > self.array.len() {
      Err(BitPackError::ArrayEndReached)
    } else {
      Ok(self.array.len() - self.p_byte)
    }
  }

  ///
  /// This operates together with `write_packed_bits`.  It increments the
  /// `p_byte` value by `n_bytes`.
  ///
  #[inline(always)]
  pub fn inc_counter(&mut self, n_bytes: usize) -> Result<(), BitPackError> {
    if self.p_byte + n_bytes >= self.array.len() {
      return Err(BitPackError::ArrayEndReached);
    }
    self.p_byte += n_bytes;

    Ok(())
  }

  ///
  /// This operates together with `write_packed_bits`.  It decrements the
  /// `p_byte` value by `n_bytes`.
  ///
  #[inline(always)]
  pub fn dec_counter(&mut self, n_bytes: usize) -> Result<(), BitPackError> {
    if n_bytes > self.p_byte {
      return Err(BitPackError::BoundaryReached);
    }
    self.p_byte -= n_bytes;

    Ok(())
  }

  ///
  /// Read `buf.len()` bytes and write them to buf.
  ///
  /// ### Arguments
  /// * `buf` - The array where the bytes will be written to.
  ///
  pub fn read(&mut self, buf: &mut [u8]) -> Result<usize, BitPackError> {
    let bytes_written = if buf.len() > self.remaining_bytes()? {
      self.remaining_bytes()?
    } else {
      buf.len()
    };

    for (i, p_buf) in buf[..bytes_written].iter_mut().enumerate() {
      *p_buf = self.array[self.p_byte + i];
    }
    self.p_byte += bytes_written;

    Ok(bytes_written)
  }

  ///
  /// Read the next two bytes as big-endian u16.
  ///
  #[inline(always)]
  pub fn read_u8(&mut self) -> Result<u8, BitPackError> {
    let value = self.array[self.p_byte];
    self.p_byte += 1;
    Ok(value)
  }

  ///
  /// Read the next two bytes as big-endian u16.
  ///
  #[inline(always)]
  pub fn read_be_u16(&mut self) -> Result<u16, BitPackError> {
    let value = BigEndian::read_u16(&self.array[self.p_byte..]);
    self.p_byte += 2;
    Ok(value)
  }

  ///
  /// Read the next two bytes as big-endian i16.
  ///
  #[inline(always)]
  pub fn read_be_i16(&mut self) -> Result<i16, BitPackError> {
    let value = BigEndian::read_i16(&self.array[self.p_byte..]);
    self.p_byte += 2;
    Ok(value)
  }

  ///
  /// Read the next two bytes as little-endian i16.
  ///
  #[inline(always)]
  pub fn read_le_i16(&mut self) -> Result<i16, BitPackError> {
    let value = LittleEndian::read_i16(&self.array[self.p_byte..]);
    self.p_byte += 2;
    Ok(value)
  }

  pub fn crc16(&self, num_bytes: usize) -> Result<u16, BitPackError> {
    if self.p_byte + num_bytes > self.array.len() {
      Err(BitPackError::ArrayEndReached)
    } else {
      Ok(crc16(&self.array[self.p_byte..(self.p_byte + num_bytes)]))
    }
  }
}