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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
use crate::{Bitstream, DecodeFailed, MAX_CHUNK_SIZE};

/// The window size is not stored in the compressed data stream and must be known before
/// decoding begins.
///
/// The window size should be the smallest power of two between 2^17 and 2^25 that is greater
/// than or equal to the sum of the size of the reference data rounded up to a multiple of
/// 32_768 and the size of the subject data. However, some implementations also seem to support
/// a window size of less than 2^17, and this one is no exception.
#[repr(u32)]
#[derive(Debug, Clone, Copy)]
pub enum WindowSize {
    /// Window size of 32 KB (2^15 bytes).
    KB32 = 0x0000_8000,
    /// Window size of 64 KB (2^16 bytes).
    KB64 = 0x0001_0000,
    /// Window size of 128 KB (2^17 bytes).
    KB128 = 0x0002_0000,
    /// Window size of 256 KB (2^18 bytes).
    KB256 = 0x0004_0000,
    /// Window size of 512 KB (2^19 bytes).
    KB512 = 0x0008_0000,
    /// Window size of 1 MB (2^20 bytes).
    MB1 = 0x0010_0000,
    /// Window size of 2 MB (2^21 bytes).
    MB2 = 0x0020_0000,
    /// Window size of 4 MB (2^22 bytes).
    MB4 = 0x0040_0000,
    /// Window size of 8 MB (2^23 bytes).
    MB8 = 0x0080_0000,
    /// Window size of 16 MB (2^24 bytes).
    MB16 = 0x0100_0000,
    /// Window size of 32 MB (2^25 bytes).
    MB32 = 0x0200_0000,
}

/// A sliding window of a certain size.
///
/// A `std::collections::VecDeque` is not used because the `deque_make_contiguous` feature
/// is [nightly-only experimental](https://github.com/rust-lang/rust/issues/70929).
pub struct Window {
    pos: usize,
    buffer: Box<[u8]>,
}

impl WindowSize {
    /// The window size determines the number of window subdivisions, or position slots.
    pub(crate) fn position_slots(&self) -> usize {
        use WindowSize::*;

        match self {
            KB32 => 30,
            KB64 => 32,
            KB128 => 34,
            KB256 => 36,
            KB512 => 38,
            MB1 => 42,
            MB2 => 50,
            MB4 => 66,
            MB8 => 98,
            MB16 => 162,
            MB32 => 290,
        }
    }

    fn value(&self) -> usize {
        *self as usize
    }

    pub(crate) fn create_buffer(&self) -> Window {
        // The window must be at least as big as the smallest chunk, or else we can't possibly
        // contain an entire chunk inside of the sliding window.
        assert!(self.value() >= MAX_CHUNK_SIZE);

        // We can use bit operations if we rely on this assumption so make sure it holds.
        assert!(self.value().is_power_of_two());

        Window {
            pos: 0,
            buffer: vec![0; self.value()].into_boxed_slice(),
        }
    }
}

impl Window {
    fn advance(&mut self, delta: usize) {
        self.pos += delta;
        if self.pos >= self.buffer.len() {
            self.pos -= self.buffer.len();
        }
    }

    pub fn push(&mut self, value: u8) {
        self.buffer[self.pos] = value;
        self.advance(1);
    }

    pub fn copy_from_self(&mut self, offset: usize, length: usize) {
        // For the fast path:
        // * Source cannot wrap around
        // * `copy_within` won't overwrite as we go but we need that
        // * Destination cannot wrap around
        if offset <= self.pos && length <= offset && self.pos + length < self.buffer.len() {
            // Best case: neither source or destination wrap around
            // TODO write a test for this because it used to fail
            let start = self.pos - offset;
            self.buffer.copy_within(start..start + length, self.pos);
        } else {
            // Either source or destination wrap around. We could expand this case into three
            // (one for only source wrapping, one for only destination wrapping, one for both)
            // but it's not really worth the effort.
            //
            // We could work out the ranges for use in `copy_within` but this is a lot simpler.
            let mask = self.buffer.len() - 1; // relying on power of two assumption

            for i in 0..length {
                let dst = (self.pos + i) & mask;
                let src = (self.buffer.len() + self.pos + i - offset) & mask;
                self.buffer[dst] = self.buffer[src];
            }
        }

        self.advance(length);
    }

    pub fn copy_from_bitstream(
        &mut self,
        bitstream: &mut Bitstream,
        len: usize,
    ) -> Result<(), DecodeFailed> {
        if self.pos + len > self.buffer.len() {
            let shift = self.pos + len - self.buffer.len();
            self.pos -= shift;

            // No need to actually save the part we're about to overwrite because when reading
            // with the bitstream we would also overwrite it anyway.
            self.buffer.copy_within(shift.., 0);
        }

        bitstream.read_raw(&mut self.buffer[self.pos..self.pos + len])?;
        self.advance(len);
        Ok(())
    }

    pub fn past_view(&mut self, len: usize) -> Result<&[u8], DecodeFailed> {
        if len > MAX_CHUNK_SIZE {
            return Err(DecodeFailed::ChunkTooLong);
        }

        // Being at zero means we're actually at max length where is impossible for `len` to be
        // bigger and we would not want to bother shifting the entire array to end where it was.
        if self.pos != 0 && len > self.pos {
            let shift = len - self.pos;
            self.advance(shift);

            let tmp = self.buffer[self.buffer.len() - shift..]
                .into_iter()
                .copied()
                .collect::<Vec<_>>();

            self.buffer.copy_within(0..self.buffer.len() - shift, shift);
            self.buffer[..shift].copy_from_slice(&tmp);
        }

        // Because we want to read behind us, being at zero means we're at the end
        let pos = if self.pos == 0 {
            self.buffer.len()
        } else {
            self.pos
        };

        Ok(&self.buffer[pos - len..pos])
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn check_push() {
        let mut window = WindowSize::KB32.create_buffer();
        window.push(1);
        window.push(2);
        window.push(3);
        assert_eq!(window.pos, 3);
        assert_eq!(&window.buffer[..3], &[1, 2, 3]);
        assert!(window.buffer[3..].iter().all(|&x| x == 0));
    }

    #[test]
    fn check_push_before_boundary() {
        let mut window = WindowSize::KB32.create_buffer();
        window.pos = window.buffer.len() - 1;
        window.push(1);
        assert_eq!(window.pos, 0);
    }

    #[test]
    fn check_push_at_boundary() {
        let mut window = WindowSize::KB32.create_buffer();
        for _ in 0..((1 << 15) - 2) {
            window.push(0);
        }
        window.push(1);
        window.push(2);
        window.push(3);
        window.push(4);
        assert_eq!(window.pos, 2);
        assert_eq!(&window.buffer[window.buffer.len() - 2..], &[1, 2]);
        assert_eq!(&window.buffer[..2], &[3, 4]);
        assert!(window.buffer[2..window.buffer.len() - 2]
            .iter()
            .all(|&x| x == 0));
    }

    #[test]
    fn check_copy_from_self() {
        let mut window = WindowSize::KB32.create_buffer();
        window.buffer[0] = 1;
        window.buffer[1] = 2;
        window.buffer[2] = 3;
        window.pos = 3;
        window.copy_from_self(3, 2);
        assert_eq!(window.pos, 5);
        assert_eq!(&window.buffer[..5], &[1, 2, 3, 1, 2]);
        assert!(window.buffer[5..].iter().all(|&x| x == 0));
    }

    #[test]
    fn check_copy_from_self_overlap() {
        let mut window = WindowSize::KB32.create_buffer();
        window.buffer[0] = 1;
        window.buffer[1] = 2;
        window.buffer[2] = 3;
        window.pos = 3;
        window.copy_from_self(2, 3);
        assert_eq!(window.pos, 6);
        assert_eq!(&window.buffer[..6], &[1, 2, 3, 2, 3, 2]);
        assert!(window.buffer[6..].iter().all(|&x| x == 0));
    }

    #[test]
    fn check_copy_at_boundary_from_self() {
        let mut window = WindowSize::KB32.create_buffer();
        window.buffer[window.buffer.len() - 3] = 1;
        window.buffer[window.buffer.len() - 2] = 2;
        window.pos = window.buffer.len() - 1;
        window.copy_from_self(2, 2);
        assert_eq!(window.pos, 1);
        assert_eq!(window.buffer[0], 2);
        assert_eq!(&window.buffer[window.buffer.len() - 3..], &[1, 2, 1]);
        assert!(window.buffer[1..window.buffer.len() - 3]
            .iter()
            .all(|&x| x == 0));
    }

    #[test]
    fn check_copy_from_self_before_boundary() {
        let mut window = WindowSize::KB32.create_buffer();
        window.buffer[window.buffer.len() - 4] = 1;
        window.buffer[window.buffer.len() - 3] = 2;
        window.pos = window.buffer.len() - 2;
        window.copy_from_self(2, 2);
        assert_eq!(window.pos, 0);
    }

    #[test]
    fn check_copy_from_self_at_boundary() {
        let mut window = WindowSize::KB32.create_buffer();
        window.buffer[window.buffer.len() - 2] = 1;
        window.buffer[window.buffer.len() - 1] = 2;
        window.buffer[0] = 3;
        window.buffer[1] = 4;
        window.pos = 2;
        window.copy_from_self(4, 3);
        assert_eq!(window.pos, 5);
        assert_eq!(&window.buffer[..5], &[3, 4, 1, 2, 3]);
        assert_eq!(&window.buffer[window.buffer.len() - 2..], &[1, 2]);
        assert!(window.buffer[5..window.buffer.len() - 2]
            .iter()
            .all(|&x| x == 0));
    }

    #[test]
    fn check_bitstream() {
        let buffer = [1, 2, 3, 4];
        let mut bitstream = Bitstream::new(&buffer);
        let mut window = WindowSize::KB32.create_buffer();
        window.copy_from_bitstream(&mut bitstream, 4).unwrap();
        assert_eq!(window.pos, 4);
        assert_eq!(&window.buffer[..4], &[1, 2, 3, 4]);
        assert!(window.buffer[4..].iter().all(|&x| x == 0));
    }

    #[test]
    fn check_bitstream_before_boundary() {
        let buffer = [1, 2, 3, 4];
        let mut bitstream = Bitstream::new(&buffer);
        let mut window = WindowSize::KB32.create_buffer();
        window.pos = window.buffer.len() - 4;
        window.copy_from_bitstream(&mut bitstream, 4).unwrap();
        assert_eq!(window.pos, 0);
    }

    #[test]
    fn check_bitstream_at_boundary() {
        let buffer = [1, 2, 3, 4];
        let mut bitstream = Bitstream::new(&buffer);
        let mut window = WindowSize::KB32.create_buffer();
        window.pos = window.buffer.len() - 2;
        window.copy_from_bitstream(&mut bitstream, 4).unwrap();
        assert_eq!(window.pos, 0);
        assert_eq!(&window.buffer[window.buffer.len() - 4..], &[1, 2, 3, 4]);
        assert!(window.buffer[..window.buffer.len() - 4]
            .iter()
            .all(|&x| x == 0));
    }

    #[test]
    fn check_past_view() {
        let mut window = WindowSize::KB32.create_buffer();
        window.buffer[0] = 1;
        window.buffer[1] = 2;
        window.buffer[2] = 3;
        window.pos = 3;
        assert_eq!(window.past_view(2).unwrap(), &[2, 3]);
        assert_eq!(window.past_view(3).unwrap(), &[1, 2, 3]);
    }

    #[test]
    fn check_past_view_at_boundary() {
        let mut window = WindowSize::KB32.create_buffer();
        window.buffer[window.buffer.len() - 2] = 1;
        window.buffer[window.buffer.len() - 1] = 2;
        window.buffer[0] = 3;
        window.buffer[1] = 4;
        window.pos = 2;
        assert_eq!(window.past_view(4).unwrap(), &[1, 2, 3, 4]);
    }

    #[test]
    fn check_past_view_too_long() {
        let mut window = WindowSize::KB32.create_buffer();
        assert_eq!(
            window.past_view(1 << 15 + 1),
            Err(DecodeFailed::ChunkTooLong)
        );
    }

    #[test]
    fn check_past_view_new_max_size() {
        let mut window = WindowSize::KB32.create_buffer();
        assert!(matches!(window.past_view(1 << 15), Ok(_)));
    }

    #[test]
    fn check_past_view_shifted_max_size() {
        let mut window = WindowSize::KB32.create_buffer();
        window.pos = 123;
        assert!(matches!(window.past_view(1 << 15), Ok(_)));
    }
}