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
use crate::{LzssError, Read, Write};

mod compress;
mod decompress;

/// Dynamic parameters for de-/compression (see [Lzss](crate::Lzss) for compile-time parameters).
///
/// # Parameters
/// * `ei` - The number of bits in the offset, usualy `10..13`
/// * `ej` - The number of bits in the length, usually `4..5`
/// * `c` - The initial fill byte of the buffer, usually `0x20` (space)
///
/// # Restrictions
/// * `ej` must be larger than `0`
/// * `ei` must be larger than `ej`
/// * `ei + ej` must be at least 8
/// * `ei + ej` must be 24 or less
///
/// # Example
/// ```rust
/// # use lzss::{LzssDyn, LzssDynError, ResultLzssErrorVoidExt, SliceReader, VecWriter};
/// let my_lzss = LzssDyn::new(10, 4, 0x20)?;
/// let input = b"Example Data";
/// let result = my_lzss.compress(
///   SliceReader::new(input),
///   VecWriter::with_capacity(30),
/// );
/// assert_eq!(result.void_unwrap().len(), 14); // the output is 14 bytes long
/// # Ok::<(), LzssDynError>(())
/// ```
#[derive(Copy, Clone, Eq, PartialEq)]
pub struct LzssDyn {
  ei: usize,
  ej: usize,
  c: u8,
}

impl LzssDyn {
  /// Create new Lzss parameters.
  ///
  /// If the parameter are not valid (see above) an error is returned.
  pub fn new(ei: usize, ej: usize, c: u8) -> Result<Self, LzssDynError> {
    if ej == 0 {
      Err(LzssDynError::EjIsZero)
    } else if ej >= ei {
      Err(LzssDynError::EiNotLargerThanEj)
    } else if ei + ej < 8 {
      Err(LzssDynError::EiEjToSmall)
    } else if ei + ej > 24 {
      Err(LzssDynError::EiEjToLarge)
    } else {
      Ok(LzssDyn { ei, ej, c })
    }
  }

  /// Get the ei parameter.
  #[inline(always)]
  pub const fn ei(&self) -> usize {
    self.ei
  }

  /// Get the ej parameter.
  #[inline(always)]
  pub const fn ej(&self) -> usize {
    self.ej
  }

  /// Get the c parameter.
  #[inline(always)]
  pub const fn c(&self) -> u8 {
    self.c
  }

  #[inline(always)]
  pub(crate) const fn n(&self) -> usize {
    1 << self.ei
  }

  #[inline(always)]
  pub(crate) const fn p(&self) -> usize {
    (1 + self.ei + self.ej) / 9
  }

  #[inline(always)]
  pub(crate) const fn f(&self) -> usize {
    (1 << self.ej) + self.p()
  }

  /// `alloc/std` Compress the input data into the output.
  ///
  /// The buffer, with `2 * (1 << EI)` bytes, is allocated on the heap.
  #[cfg(any(test, feature = "alloc"))]
  pub fn compress<R: Read, W: Write>(
    &self,
    mut reader: R,
    mut writer: W,
  ) -> Result<W::Output, LzssError<R::Error, W::Error>> {
    let mut buffer = vec![self.c; 2 * self.n()];
    self.compress_internal(&mut reader, &mut writer, &mut buffer)?;
    writer.finish().map_err(LzssError::WriteError)
  }

  /// Compress the input data into the output.
  ///
  /// It will be asserted at runtime that the buffer is at least `2 * (1 << EI)`.
  pub fn compress_with_buffer<R: Read, W: Write>(
    &self,
    mut reader: R,
    mut writer: W,
    buffer: &mut [u8],
  ) -> Result<W::Output, LzssError<R::Error, W::Error>> {
    assert!(buffer.len() >= 2 * self.n());
    unsafe { ::core::ptr::write_bytes(buffer.as_mut_ptr(), self.c, self.n() - self.f()) };
    self.compress_internal(&mut reader, &mut writer, buffer)?;
    writer.finish().map_err(LzssError::WriteError)
  }

  /// `alloc/std` Decompress the input data into the output.
  ///
  /// The buffer, with `1 << EI` bytes, is allocated on the heap.
  #[cfg(any(test, feature = "alloc"))]
  pub fn decompress<R: Read, W: Write>(
    &self,
    mut reader: R,
    mut writer: W,
  ) -> Result<W::Output, LzssError<R::Error, W::Error>> {
    let mut buffer = vec![self.c; self.n()];
    self.decompress_internal(&mut reader, &mut writer, &mut buffer)?;
    writer.finish().map_err(LzssError::WriteError)
  }

  /// Decompress the input data into the output.
  ///
  /// It will be asserted at runtime that the buffer is at least `1 << EI`.
  pub fn decompress_with_buffer<R: Read, W: Write>(
    &self,
    mut reader: R,
    mut writer: W,
    buffer: &mut [u8],
  ) -> Result<W::Output, LzssError<R::Error, W::Error>> {
    assert!(buffer.len() >= self.n());
    unsafe { ::core::ptr::write_bytes(buffer.as_mut_ptr(), self.c, self.n()) };
    self.decompress_internal(&mut reader, &mut writer, buffer)?;
    writer.finish().map_err(LzssError::WriteError)
  }
}

/// The error returned by [LzssDyn::new].
#[derive(Debug)]
pub enum LzssDynError {
  /// Invalid EJ, must be larger than 0.
  EjIsZero,
  /// Invalid EI, must be larger than EJ.
  EiNotLargerThanEj,
  /// Invalid EI, EJ, both together must be 8 or more.
  EiEjToSmall,
  /// Invalid EI, EJ, both together must be 24 or less.
  EiEjToLarge,
}

impl core::fmt::Display for LzssDynError {
  fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
    match self {
      LzssDynError::EjIsZero => f.write_str("Invalid EJ, must be larger than 0"),
      LzssDynError::EiNotLargerThanEj => f.write_str("Invalid EI, must be larger than EJ"),
      LzssDynError::EiEjToSmall => f.write_str("Invalid EI, EJ, both together must be 8 or more"),
      LzssDynError::EiEjToLarge => f.write_str("Invalid EI, EJ, both together must be 24 or less"),
    }
  }
}

/// `std` Implementation of [Error](std::error::Error) for [LzssDynError]
#[cfg(any(test, feature = "std"))]
impl std::error::Error for LzssDynError {}

#[cfg(test)]
mod tests {
  use crate::dynamic::LzssDyn;
  use crate::slice::SliceReader;
  use crate::vec::VecWriter;
  use crate::ResultLzssErrorVoidExt;

  fn test_lzss() -> LzssDyn {
    LzssDyn::new(10, 4, 0x20).unwrap()
  }

  const TEST_DATA: &[u8; 27] = b"Sample   Data   11221233123";
  const COMPRESSED_DATA: [u8; 26] = [
    169, 216, 109, 183, 11, 101, 149, 246, 13, 18, 195, 116, 176, 191, 81, 152, 204, 102, 83, 32,
    0, 19, 57, 152, 3, 16,
  ];

  #[test]
  fn test_decompress() {
    let output = test_lzss()
      .decompress(
        SliceReader::new(&COMPRESSED_DATA),
        VecWriter::with_capacity(TEST_DATA.len()),
      )
      .void_unwrap();
    assert_eq!(output.as_slice(), TEST_DATA);
  }

  #[test]
  fn test_compress() {
    let output = test_lzss()
      .compress(
        SliceReader::new(TEST_DATA),
        VecWriter::with_capacity(COMPRESSED_DATA.len()),
      )
      .void_unwrap();
    assert_eq!(output.as_slice(), COMPRESSED_DATA);
  }

  #[test]
  fn test_compress_big() {
    let big_test_data = include_bytes!("mod.rs");
    // compress
    let output1 = test_lzss()
      .compress(
        SliceReader::new(big_test_data),
        VecWriter::with_capacity(big_test_data.len()),
      )
      .void_unwrap();
    // decompress
    let output2 = test_lzss()
      .decompress(
        SliceReader::new(&output1),
        VecWriter::with_capacity(big_test_data.len()),
      )
      .void_unwrap();
    assert_eq!(output2.as_slice(), big_test_data);
  }

  #[test]
  fn test_decompress_with_buffer() {
    let mut buffer = [0u8; 1111];
    let output = test_lzss()
      .decompress_with_buffer(
        SliceReader::new(&COMPRESSED_DATA),
        VecWriter::with_capacity(TEST_DATA.len()),
        &mut buffer,
      )
      .void_unwrap();
    assert_eq!(output.as_slice(), TEST_DATA);
  }

  #[test]
  fn test_compress_with_buffer() {
    let mut buffer = [0u8; 2222];
    let output = test_lzss()
      .compress_with_buffer(
        SliceReader::new(TEST_DATA),
        VecWriter::with_capacity(COMPRESSED_DATA.len()),
        &mut buffer,
      )
      .void_unwrap();
    assert_eq!(output.as_slice(), COMPRESSED_DATA);
  }
}