devker 0.1.6

Rust Core Project
Documentation
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
//! # Deflate/Inflate
//!
//! ## Examples
//!
//! ### Easy to use.
//! ```
//! use devker::prelude::{deflate, inflate, BlockType, Cache};
//!
//! let mut cache = Cache::new();
//! let v = String::from("Hello world, this is a wonderful world !");
//! let v_in = v.into_bytes();
//!
//! // Encode.
//! let encoded = deflate(&v_in, BlockType::Fixed, &mut cache);
//! // Decode.
//! let decoded = inflate(&encoded, &mut cache).unwrap();
//! assert_eq!(v_in, decoded);
//! ```
//!
//! ### Reusable cache.
//! ```
//! use devker::prelude::{deflate, inflate, BlockType, Cache};
//!
//! let mut cache = Cache::new();
//!
//! // First try.
//!
//! let v = String::from("Hello world, this is a wonderful world !");
//! let v_in = v.into_bytes();
//!
//! let encoded = deflate(&v_in, BlockType::Fixed, &mut cache);
//! let decoded = inflate(&encoded, &mut cache).unwrap();
//! assert_eq!(v_in, decoded);
//!
//! // Another try.
//!
//! let v = String::from("The cache can be reused !");
//! let v_in = v.into_bytes();
//!
//! let encoded = deflate(&v_in, BlockType::Fixed, &mut cache);
//! let decoded = inflate(&encoded, &mut cache).unwrap();
//! assert_eq!(v_in, decoded);
//! ```

// Imports.
use crate::bits::Bits;
use crate::code::Code;
use crate::code::{DISTANCE_TABLE, END_OF_BLOCK, LENGTH_TABLE};
use crate::huffman::huffman_encode;
use crate::lzss::{extend, lzss_encode};
use crate::prelude::{BlockType, Cache};
// Constants.
const ERROR_BUFFER: &str = "Buffer overflow.";
const ERROR_COMPLEMENT: &str = "LEN is not the one's complement of NLEN.";
const ERROR_LENGTH: &str = "Invalid length.";
const ERROR_POSITION: &str = "One distance is greater than current index.";
const ERROR_PREVIOUS: &str = "No previous value.";
const ERROR_RESERVED: &str = "Reserved btype.";
const ERROR_VALUE: &str = "Invalid value decoded.";
const ERROR_WIDTH: &str = "Invalid width decoded";
const ERROR_WIDTHES: &str = "Invalid code lengths.";
const WIDTH_CODE_ORDER: [usize; 19] = [
    16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15,
];
// Structures.
type IterU8 = dyn Iterator<Item = u8>;
#[derive(Debug)]
struct Reader<'a> {
    v_in: &'a [u8],
    last_read: u32,
    offset: u8,
    last_error: Option<&'a str>,
}
#[derive(Debug)]
struct HuffmanDecoder<'a> {
    literal: &'a mut [i32],
    distance: &'a mut [i32],
    eob_width: u8,
    max_lwidth: u8,
    max_dwidth: u8,
}
// Implementations.
impl<'a> Reader<'a> {
    fn new(v_in: &'a [u8]) -> Self {
        Self {
            v_in,
            last_read: 0,
            offset: 32,
            last_error: None,
        }
    }
    fn read(&mut self) -> u8 {
        if self.v_in.is_empty() {
            self.last_error = Some(ERROR_BUFFER);
            return 0;
        }
        let next = self.v_in[0];
        self.v_in = &self.v_in[1..];
        next
    }
    fn read_u16(&mut self) -> u16 {
        if self.v_in.len() < 2 {
            self.last_error = Some(ERROR_BUFFER);
            return 0;
        }
        let mut array = [0; 2];
        (&mut array).copy_from_slice(&self.v_in[0..2]);
        let next = u16::from_le_bytes(array);
        self.v_in = &self.v_in[2..];
        next
    }
    fn read_bits(&mut self, width: u8) -> u16 {
        let bits = self.peek_bits(width);
        self.skip_bits(width);
        bits
    }
    fn peek_bits(&mut self, width: u8) -> u16 {
        while self.offset > 32 - width {
            let next = self.read() as u32;
            self.offset -= 8;
            self.last_read >>= 8;
            self.last_read |= next << 24;
        }
        let mut bits = self.last_read.wrapping_shr(self.offset as u32) as u16;
        let mask = (1 << width) - 1;
        bits &= mask;
        bits
    }
    fn skip_bits(&mut self, width: u8) {
        self.offset += width;
    }
    fn reset(&mut self) {
        self.offset = 32;
    }
    fn get_code(&mut self, decoder: &[i32], max_width: u8) -> u16 {
        let code = self.peek_bits(max_width);
        let bits = Bits::from(decoder[code as usize]);
        if bits.width > max_width {
            self.last_error = Some(ERROR_WIDTH);
            return 0;
        }
        self.skip_bits(bits.width);
        bits.data
    }
    fn check_last_error(&self) -> Result<(), String> {
        if let Some(e) = self.last_error {
            return Err(e.into());
        }
        Ok(())
    }
    fn load_widthes(&mut self, code: u16, last: Option<u8>) -> Result<Box<IterU8>, String> {
        Ok(match code {
            0..=15 => Box::new(std::iter::once(code as u8)),
            16 => {
                let count = self.read_bits(2) + 3;
                let last = match last {
                    Some(x) => x,
                    None => return Err(ERROR_PREVIOUS.into()),
                };
                Box::new(std::iter::repeat(last).take(count as usize))
            }
            17 => {
                let zeros = self.read_bits(3) + 3;
                Box::new(std::iter::repeat(0).take(zeros as usize))
            }
            18 => {
                let zeros = self.read_bits(7) + 11;
                Box::new(std::iter::repeat(0).take(zeros as usize))
            }
            _ => return Err(ERROR_WIDTHES.into()),
        })
    }
    fn fill(&mut self, v_out: &mut Vec<u8>, len: usize) {
        if self.v_in.len() < len {
            self.last_error = Some(ERROR_BUFFER);
            return;
        }
        v_out.reserve(len);
        let mut array = vec![0; len];
        (&mut array).copy_from_slice(&self.v_in[..len]);
        v_out.extend(array);
    }
    fn fill_to(&mut self, v_out: &mut [u8], pos: usize, len: usize) {
        if self.v_in.len() < len || v_out.len() < pos + len {
            self.last_error = Some(ERROR_BUFFER);
            return;
        }
        (&mut v_out[pos..pos + len]).copy_from_slice(&self.v_in[..len]);
    }
}
impl<'a> HuffmanDecoder<'a> {
    fn new(
        btype: BlockType,
        reader: &mut Reader,
        buf: &'a mut [i32; 0x10000],
    ) -> Result<Self, String> {
        Ok(match btype {
            BlockType::Fixed => {
                let max_lwidth = 9;
                let max_dwidth = 5;
                let (literal, buf) = buf.split_at_mut(1 << max_lwidth);
                let (distance, _) = buf.split_at_mut(1 << max_dwidth);

                // Fixed Huffman Tree
                for i in 0..144 {
                    let (data, width) = (0b0_0011_0000 + i as u16, 8);
                    Self::set_mapping(literal, i, data, width, max_lwidth);
                }
                for (i, code) in (144..256).enumerate() {
                    let (data, width) = (0b1_1001_0000 + i as u16, 9);
                    Self::set_mapping(literal, code, data, width, max_lwidth);
                }
                for (i, code) in (256..280).enumerate() {
                    let (data, width) = (0b0_0000_0000 + i as u16, 7);
                    Self::set_mapping(literal, code, data, width, max_lwidth);
                }
                for (i, code) in (280..288).enumerate() {
                    let (data, width) = (0b0_1100_0000 + i as u16, 8);
                    Self::set_mapping(literal, code, data, width, max_lwidth);
                }
                for i in 0..30 {
                    let (data, width) = (i as u16, 5);
                    Self::set_mapping(distance, data, data, width, max_dwidth);
                }

                Self {
                    literal,
                    distance,
                    eob_width: 7,
                    max_lwidth,
                    max_dwidth,
                }
            }
            BlockType::Dynamic => {
                let lcount = reader.read_bits(5) as usize + 257;
                let dcount = reader.read_bits(5) as usize + 1;
                let wcount = reader.read_bits(4) as usize + 4;

                // Width decoder.
                let mut width_code_widthes = [0; 19];
                for i in WIDTH_CODE_ORDER.iter().take(wcount) {
                    width_code_widthes[*i] = reader.read_bits(3) as u8;
                }
                let (width_decoder, buf, max_wwidth, _) =
                    Self::from_widthes(buf, &width_code_widthes);

                // Literal.
                let mut literal_code_widthes = Vec::with_capacity(lcount);
                while literal_code_widthes.len() < lcount {
                    let code = reader.get_code(width_decoder, max_wwidth);
                    let last = literal_code_widthes.last().copied();
                    literal_code_widthes.extend(reader.load_widthes(code, last)?);
                }

                // Distance.
                let mut distance_code_widthes =
                    literal_code_widthes.drain(lcount..).collect::<Vec<_>>();
                distance_code_widthes.reserve(dcount);
                while distance_code_widthes.len() < dcount {
                    let code = reader.get_code(width_decoder, max_wwidth);
                    let last = distance_code_widthes
                        .last()
                        .copied()
                        .or_else(|| literal_code_widthes.last().copied());
                    distance_code_widthes.extend(reader.load_widthes(code, last)?);
                }
                if distance_code_widthes.len() > dcount {
                    return Err(ERROR_LENGTH.into());
                }
                let (literal, buf, max_lwidth, eob_width) =
                    Self::from_widthes(buf, &literal_code_widthes);
                let (distance, _, max_dwidth, _) = Self::from_widthes(buf, &distance_code_widthes);
                Self {
                    literal,
                    distance,
                    eob_width,
                    max_lwidth,
                    max_dwidth,
                }
            }
            _ => unimplemented!(),
        })
    }
    fn set_mapping(decoder: &mut [i32], code: u16, data: u16, width: u8, max_width: u8) {
        let bits = Bits { data, width }.reverse();
        for padding in 0..(1 << (max_width - width)) {
            decoder[padding << width | bits.data as usize] = Bits { data: code, width }.as_i32();
        }
    }
    fn from_widthes(buf: &'a mut [i32], widthes: &[u8]) -> (&'a mut [i32], &'a mut [i32], u8, u8) {
        let max_width = *widthes.iter().max().unwrap_or(&0);
        let (decoder, buf) = buf.split_at_mut(1 << max_width);
        let eob_width = Self::restore_canonical_huffman_codes(decoder, &widthes, max_width as u8);
        (decoder, buf, max_width, eob_width)
    }
    fn restore_canonical_huffman_codes(width: &mut [i32], widthes: &[u8], max_width: u8) -> u8 {
        let mut codes = widthes
            .iter()
            .enumerate()
            .filter(|(_, code_width)| **code_width > 0)
            .map(|(code, code_width)| (code as u16, *code_width))
            .collect::<Vec<_>>();
        codes.sort_by_key(|x| x.1);

        let mut code = 0;
        let mut prev_width = 0;
        let mut eob_width = 0;
        for (c, w) in codes {
            code <<= w - prev_width;
            Self::set_mapping(width, c, code, w, max_width);
            if c == END_OF_BLOCK {
                eob_width = w;
            }
            code += 1;
            prev_width = w;
        }
        eob_width
    }
    fn decode(&self, reader: &mut Reader) -> Code {
        let code = reader.peek_bits(self.eob_width);
        let mut bits = Bits::from(self.literal[code as usize]);
        if bits.width > self.eob_width {
            let code = reader.peek_bits(self.max_lwidth);
            bits = Bits::from(self.literal[code as usize]);
            if bits.width > self.max_lwidth {
                reader.last_error = Some(ERROR_WIDTH);
                return Code::EndOfBlock;
            }
        }
        reader.skip_bits(bits.width);

        match bits.data {
            0..=255 => Code::Literal(bits.data as u8),
            256 => Code::EndOfBlock,
            257..=285 => {
                let (code_base_length, width_length) = LENGTH_TABLE[bits.data as usize - 257];
                let bits_length = reader.read_bits(width_length) as u8;

                let code = reader.get_code(self.distance, self.max_dwidth);

                let (code_base_distance, width_distance) = DISTANCE_TABLE[code as usize];
                let bits_distance = reader.read_bits(width_distance);

                Code::Pointer {
                    length: code_base_length + bits_length,
                    distance: code_base_distance + bits_distance,
                }
            }
            _ => {
                reader.last_error = Some(ERROR_VALUE);
                Code::EndOfBlock
            }
        }
    }
}
// Functions.
fn extend_to(buf: &mut [u8], mut pos: usize, mut d: usize, mut l: usize) -> Result<(), String> {
    if pos < d {
        return Err(ERROR_POSITION.into());
    }
    if buf.len() < pos + l {
        return Err(ERROR_BUFFER.into());
    }
    let start = pos - d;

    // Copy bytes fastly
    while l >= d {
        let (left, right) = (&mut buf[start..pos + d]).split_at_mut(d);
        right.copy_from_slice(left);
        pos += d;
        l -= d;
        d *= 2;
    }

    // Copy the last remaining bytes
    let (left, right) = (&mut buf[start..pos + l]).split_at_mut(d);
    right.copy_from_slice(&left[..l]);
    Ok(())
}
// Main functions.
pub fn deflate(v_in: &[u8], btype: BlockType, cache: &mut Cache) -> Vec<u8> {
    // Variable Initialization.
    let buf = cache.inner_mut();
    // Algorithm.
    let mut encoded = lzss_encode(v_in, buf);
    encoded.push(Code::EndOfBlock);
    huffman_encode(&encoded, btype, buf)
}

pub fn inflate(v_in: &[u8], cache: &mut Cache) -> Result<Vec<u8>, String> {
    // Variable Initialization.
    let buf = cache.inner_mut();
    let mut reader = Reader::new(v_in);
    let mut bfinal = 0;
    let mut v_out = Vec::new();

    // Algorithms.
    while bfinal == 0 {
        bfinal = reader.read_bits(1);
        let btype = reader.read_bits(2);
        reader.check_last_error()?;
        match btype {
            0b11 => return Err(ERROR_RESERVED.into()),
            0b00 => {
                reader.reset();
                let len = reader.read_u16();
                let nlen = reader.read_u16();
                if !len != nlen {
                    return Err(ERROR_COMPLEMENT.into());
                }
                reader.fill(&mut v_out, len as usize);
            }
            btype => {
                let decoder = HuffmanDecoder::new(BlockType::from(btype), &mut reader, buf)?;
                reader.check_last_error()?;
                loop {
                    let x = decoder.decode(&mut reader);
                    match x {
                        Code::EndOfBlock => break,
                        Code::Literal(a) => v_out.push(a),
                        Code::Pointer {
                            distance: d,
                            length: l,
                        } => extend(&mut v_out, d as usize, l as usize + 3)?,
                    }
                }
            }
        }
    }
    reader.check_last_error()?;
    Ok(v_out)
}

pub fn inflate_to(v_in: &[u8], cache: &mut Cache, v_out: &mut [u8]) -> Result<(), String> {
    // Variable Initialization.
    let buf = cache.inner_mut();
    let mut reader = Reader::new(v_in);
    let mut bfinal = 0;
    let mut i = 0;

    // Algorithms.
    while bfinal == 0 {
        bfinal = reader.read_bits(1);
        let btype = reader.read_bits(2);
        reader.check_last_error()?;
        match btype {
            0b11 => return Err(ERROR_RESERVED.into()),
            0b00 => {
                reader.reset();
                let len = reader.read_u16();
                let nlen = reader.read_u16();
                if !len != nlen {
                    return Err(ERROR_COMPLEMENT.into());
                }
                let len = len as usize;
                reader.fill_to(v_out, i, len);
                i += len;
            }
            btype => {
                let decoder = HuffmanDecoder::new(BlockType::from(btype), &mut reader, buf)?;
                reader.check_last_error()?;
                loop {
                    let x = decoder.decode(&mut reader);
                    match x {
                        Code::EndOfBlock => break,
                        Code::Literal(a) => {
                            if i >= v_out.len() {
                                return Err(ERROR_BUFFER.into());
                            }
                            v_out[i] = a;
                            i += 1;
                        }
                        Code::Pointer {
                            distance: d,
                            length: l,
                        } => {
                            let l = l as usize + 3;
                            extend_to(v_out, i, d as usize, l)?;
                            i += l;
                        }
                    }
                }
            }
        }
    }
    reader.check_last_error()
}