lz4-java-wrc 0.2.0

A fork of `lz4jb` to ensure it gives back access to the underlying writer (wrc = "write continue") `lz4jb` is a Rust implementation of the LZ4BlockOutputStream format from https://github.com/lz4/lz4-java. This is not compatible with the standard LZ4 Block format, and is useful for reading Minecraft region files.
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
use crate::common::{
    ErrorChecksum, ErrorCompressedSizeTooBig, ErrorCompressionMethod, ErrorDecompressedSizeTooBig,
    ErrorIncoherentSize, ErrorMagicNumber, ErrorNoCompressionDifferentSize, ErrorWrongBlockSize,
    IoErrorKind, Result,
};

use twox_hash::XxHash32;

use std::convert::TryInto;
use std::hash::Hasher;
use std::io::{Read, Write};
use std::ops::Range;
use std::result::Result as StdResult;

const MAGIC_HEADER: &[u8; 8] = b"LZ4Block";
const MAGIC_HEADER_RANGE: Range<usize> = 0..MAGIC_HEADER.len();
const TOKEN_INDEX: usize = MAGIC_HEADER_RANGE.end;
const COMPRESSED_LEN_RANGE: Range<usize> = (TOKEN_INDEX + 1)..(TOKEN_INDEX + 5);
const DECOMPRESSED_LEN_RANGE: Range<usize> =
    COMPRESSED_LEN_RANGE.end..(COMPRESSED_LEN_RANGE.end + 4);
const CHECKSUM_RANGE: Range<usize> = DECOMPRESSED_LEN_RANGE.end..(DECOMPRESSED_LEN_RANGE.end + 4);
const HEADER_LENGTH: usize = CHECKSUM_RANGE.end;

const COMPRESSION_LEVEL_BASE: usize = 10;
const MIN_BLOCK_SIZE: usize = 64;
const MAX_BLOCK_SIZE: usize = 1 << (COMPRESSION_LEVEL_BASE + 0x0f);
const DEFAULT_SEED: u32 = 0x9747b28c;
const COMPRESSION_BLOCKS: [usize; 0x10] = [
    1 << (COMPRESSION_LEVEL_BASE + 14),
    1 << (COMPRESSION_LEVEL_BASE + 13),
    1 << (COMPRESSION_LEVEL_BASE + 12),
    1 << (COMPRESSION_LEVEL_BASE + 11),
    1 << (COMPRESSION_LEVEL_BASE + 10),
    1 << (COMPRESSION_LEVEL_BASE + 9),
    1 << (COMPRESSION_LEVEL_BASE + 8),
    1 << (COMPRESSION_LEVEL_BASE + 7),
    1 << (COMPRESSION_LEVEL_BASE + 6),
    1 << (COMPRESSION_LEVEL_BASE + 5),
    1 << (COMPRESSION_LEVEL_BASE + 4),
    1 << (COMPRESSION_LEVEL_BASE + 3),
    1 << (COMPRESSION_LEVEL_BASE + 2),
    1 << (COMPRESSION_LEVEL_BASE + 1),
    1 << (COMPRESSION_LEVEL_BASE),
    0,
];

#[derive(Debug)]
pub(crate) struct Lz4BlockHeader {
    pub(crate) compression_method: CompressionMethod,
    pub(crate) compression_level: CompressionLevel,
    pub(crate) compressed_len: u32,
    pub(crate) decompressed_len: u32,
    pub(crate) checksum: u32,
}

impl Lz4BlockHeader {
    /// Implement the java's default checksum implementation
    ///
    /// This implementation includes the bug around the missing 4 first bits.
    pub(crate) fn default_checksum(buf: &[u8]) -> u32 {
        let mut hasher = XxHash32::with_seed(DEFAULT_SEED);
        hasher.write(buf);
        // Drop the 4 first bits: https://github.com/lz4/lz4-java/blob/1.8.0/src/java/net/jpountz/xxhash/StreamingXXHash32.java#L106
        (hasher.finish() & 0x0fffffff) as u32
    }

    pub(crate) fn read<R: Read>(reader: &mut R) -> Result<Option<Self>> {
        let mut header = [0u8; HEADER_LENGTH];
        if let Err(err) = reader.read_exact(&mut header[..]) {
            return if matches!(err.kind(), IoErrorKind::UnexpectedEof) {
                Ok(None)
            } else {
                Err(err.into())
            };
        }
        Self::check_magic_header(&header)?;
        let compression_method = CompressionMethod::from_token(header[TOKEN_INDEX])?;
        let compression_level = CompressionLevel::from_token(header[TOKEN_INDEX]);
        let compressed_len = u32::from_le_bytes(header[COMPRESSED_LEN_RANGE].try_into().unwrap());
        let decompressed_len =
            u32::from_le_bytes(header[DECOMPRESSED_LEN_RANGE].try_into().unwrap());
        let checksum = u32::from_le_bytes(header[CHECKSUM_RANGE].try_into().unwrap());
        if decompressed_len > compression_level.get_max_decompressed_buffer_len() as u32 {
            return ErrorDecompressedSizeTooBig::new_error(
                decompressed_len,
                compression_level.get_max_decompressed_buffer_len(),
            );
        }
        if compressed_len > i32::MAX as u32 {
            // java uses signed integers
            return ErrorCompressedSizeTooBig::new_error(compressed_len, i32::MAX as u32);
        }
        if (compressed_len == 0) != (decompressed_len == 0) {
            return ErrorIncoherentSize::new_error(decompressed_len, compressed_len);
        }
        if matches!(compression_method, CompressionMethod::Raw)
            && compressed_len != decompressed_len
        {
            return ErrorNoCompressionDifferentSize::new_error(compressed_len, decompressed_len);
        }
        if compressed_len == 0 && decompressed_len == 0 && checksum != 0 {
            return ErrorChecksum::new_error(checksum, 0);
        }
        Ok(Some(Self {
            compression_method,
            compression_level,
            compressed_len,
            decompressed_len,
            checksum,
        }))
    }

    pub(crate) fn write<W: Write>(&self, writer: &mut W) -> Result<usize> {
        let mut buf = [0u8; HEADER_LENGTH];
        buf[MAGIC_HEADER_RANGE].clone_from_slice(MAGIC_HEADER);
        buf[TOKEN_INDEX] = self.compression_level.get_token() | self.compression_method.get_token();
        buf[COMPRESSED_LEN_RANGE].clone_from_slice(&(self.compressed_len).to_le_bytes());
        buf[DECOMPRESSED_LEN_RANGE].clone_from_slice(&(self.decompressed_len).to_le_bytes());
        buf[CHECKSUM_RANGE].clone_from_slice(&(self.checksum).to_le_bytes());
        Ok(writer.write(&buf)?)
    }

    fn check_magic_header(header: &[u8; HEADER_LENGTH]) -> StdResult<(), ErrorMagicNumber> {
        let magic = &header[MAGIC_HEADER_RANGE];
        if magic != MAGIC_HEADER {
            return ErrorMagicNumber::new_error(
                u64::from_be_bytes(*MAGIC_HEADER),
                u64::from_be_bytes(magic.try_into().unwrap()),
            );
        }
        Ok(())
    }
}

// CompressionLevel

#[derive(Debug, Clone, Copy)]
pub(crate) struct CompressionLevel {
    compression_level: u8,
}

impl CompressionLevel {
    pub(crate) fn from_block_size(block_size: usize) -> StdResult<Self, ErrorWrongBlockSize> {
        if (MIN_BLOCK_SIZE..=MAX_BLOCK_SIZE).contains(&block_size) {
            let index = COMPRESSION_BLOCKS
                .iter()
                .position(|block| *block < block_size)
                .ok_or_else(|| {
                    ErrorWrongBlockSize::new(block_size, MIN_BLOCK_SIZE, MAX_BLOCK_SIZE)
                })?;
            Ok(Self {
                compression_level: (0x0f - index) as u8,
            })
        } else {
            ErrorWrongBlockSize::new_error(block_size, MIN_BLOCK_SIZE, MAX_BLOCK_SIZE)
        }
    }

    pub(crate) fn get_max_decompressed_buffer_len(&self) -> usize {
        1 << (self.compression_level as usize + COMPRESSION_LEVEL_BASE)
    }

    pub(crate) fn from_token(token: u8) -> Self {
        Self {
            compression_level: token & 0x0f,
        }
    }

    pub(crate) fn get_token(&self) -> u8 {
        self.compression_level
    }
}

// CompressionMethod

#[derive(Clone, Copy, Debug)]
pub(crate) enum CompressionMethod {
    Raw = 1,
    Lz4 = 2,
}

impl CompressionMethod {
    pub(crate) fn from_token(token: u8) -> StdResult<Self, ErrorCompressionMethod> {
        let compression_method = (token as usize & 0xf0) >> 4;
        match compression_method {
            x if x == Self::Raw as usize => Ok(Self::Raw),
            x if x == Self::Lz4 as usize => Ok(Self::Lz4),
            _ => ErrorCompressionMethod::new_error(token),
        }
    }

    pub(crate) fn get_token(&self) -> u8 {
        (*self as u8) << 4
    }
}

#[cfg(test)]
pub(crate) mod data {
    use super::{HEADER_LENGTH, MAGIC_HEADER};

    pub(crate) const VALID_DATA: [u8; HEADER_LENGTH + 3] = [
        MAGIC_HEADER[0],
        MAGIC_HEADER[1],
        MAGIC_HEADER[2],
        MAGIC_HEADER[3],
        MAGIC_HEADER[4],
        MAGIC_HEADER[5],
        MAGIC_HEADER[6],
        MAGIC_HEADER[7],
        // token
        0x10,
        // compressed_len
        0x03,
        0x00,
        0x00,
        0x00,
        // decompressed_len
        0x03,
        0x00,
        0x00,
        0x00,
        // hash
        0x52,
        0xe4,
        0x77,
        0x06,
        // data
        '.' as u8,
        '.' as u8,
        '.' as u8,
    ];
    pub(crate) const VALID_EMPTY: [u8; HEADER_LENGTH] = [
        MAGIC_HEADER[0],
        MAGIC_HEADER[1],
        MAGIC_HEADER[2],
        MAGIC_HEADER[3],
        MAGIC_HEADER[4],
        MAGIC_HEADER[5],
        MAGIC_HEADER[6],
        MAGIC_HEADER[7],
        // token
        0x10,
        // compressed_len
        0x00,
        0x00,
        0x00,
        0x00,
        // decompressed_len
        0x00,
        0x00,
        0x00,
        0x00,
        // hash
        0x00,
        0x00,
        0x00,
        0x00,
    ];
}

#[cfg(test)]
mod test_lz4_block_header {
    use super::data::{VALID_DATA, VALID_EMPTY};
    use super::{
        CompressionMethod, Lz4BlockHeader, DECOMPRESSED_LEN_RANGE, HEADER_LENGTH, TOKEN_INDEX,
    };

    #[test]
    fn default_checksum_basic() {
        let mut v = VALID_DATA[HEADER_LENGTH..].to_vec();
        assert_eq!(Lz4BlockHeader::default_checksum(v.as_mut()), 0x0677e452);
    }

    #[test]
    fn read_too_small() {
        for s in 0..HEADER_LENGTH {
            let mut v = VALID_DATA[..s].to_vec();
            let mut d: &[u8] = v.as_mut();
            assert!(Lz4BlockHeader::read(&mut d).unwrap().is_none());
        }
    }

    #[test]
    fn read_empty() {
        let mut v = VALID_EMPTY[..].to_vec();
        let mut d: &[u8] = v.as_mut();
        let header = Lz4BlockHeader::read(&mut d).unwrap().unwrap();

        assert!(matches!(header.compression_method, CompressionMethod::Raw));
        assert_eq!(header.compression_level.compression_level, 0);
        assert_eq!(header.compressed_len, 0);
        assert_eq!(header.decompressed_len, 0);
        assert_eq!(header.checksum, 0);
    }

    #[test]
    fn read_valid() {
        let mut v = VALID_DATA[..HEADER_LENGTH].to_vec();
        let mut d: &[u8] = v.as_mut();
        let header = Lz4BlockHeader::read(&mut d).unwrap().unwrap();

        assert!(matches!(header.compression_method, CompressionMethod::Raw));
        assert_eq!(header.compression_level.compression_level, 0);
        assert_eq!(header.compressed_len, 3);
        assert_eq!(header.decompressed_len, 3);
        assert_eq!(header.checksum, 0x0677e452);
    }

    #[test]
    fn read_raw_different_sizes() {
        let mut v = VALID_DATA[..HEADER_LENGTH].to_vec();
        // update decompressed_len 3->4
        v[DECOMPRESSED_LEN_RANGE.start] += 1;
        let mut d: &[u8] = v.as_mut();
        assert!(Lz4BlockHeader::read(&mut d).is_err());
    }

    #[test]
    fn read_lz4_different_sizes() {
        let mut v = VALID_DATA[..HEADER_LENGTH].to_vec();
        // update decompressed_len 3->4 + token
        v[TOKEN_INDEX] = (v[TOKEN_INDEX] & 0x0f) | CompressionMethod::Lz4.get_token();
        v[DECOMPRESSED_LEN_RANGE.start] += 1;
        let mut d: &[u8] = v.as_mut();
        let header = Lz4BlockHeader::read(&mut d).unwrap().unwrap();

        assert!(matches!(header.compression_method, CompressionMethod::Lz4));
        assert_eq!(header.compression_level.compression_level, 0);
        assert_eq!(header.compressed_len, 3);
        assert_eq!(header.decompressed_len, 4);
        assert_eq!(header.checksum, 0x0677e452);
    }
}

#[cfg(test)]
mod test_compression_level {
    use super::{CompressionLevel, COMPRESSION_LEVEL_BASE, MAX_BLOCK_SIZE, MIN_BLOCK_SIZE};

    #[test]
    fn from_block_size_min() {
        assert_eq!(
            CompressionLevel::from_block_size(MIN_BLOCK_SIZE)
                .unwrap()
                .compression_level,
            0
        );
    }

    #[test]
    fn from_block_size_max() {
        assert_eq!(
            CompressionLevel::from_block_size(MAX_BLOCK_SIZE)
                .unwrap()
                .compression_level,
            0x0f
        );
    }

    #[test]
    fn from_block_size_valid() {
        for i in 0x00..0x0f {
            assert_eq!(
                CompressionLevel::from_block_size(1 << (COMPRESSION_LEVEL_BASE + i))
                    .unwrap()
                    .compression_level,
                i as u8
            );
            assert_eq!(
                CompressionLevel::from_block_size(1 << (COMPRESSION_LEVEL_BASE + i) + 1)
                    .unwrap()
                    .compression_level,
                (i + 1) as u8
            );
        }
    }

    #[test]
    fn from_block_size_too_small() {
        assert!(CompressionLevel::from_block_size(MIN_BLOCK_SIZE - 1).is_err());
    }

    #[test]
    fn from_block_size_too_big() {
        assert!(CompressionLevel::from_block_size(MAX_BLOCK_SIZE + 1).is_err());
    }

    #[test]
    fn from_token() {
        for token in 0x00..=0xff {
            assert_eq!(
                CompressionLevel::from_token(token).compression_level,
                token & 0x0f
            );
        }
    }
}

#[cfg(test)]
mod test_compression_method {
    use super::CompressionMethod;

    #[test]
    fn from_token_raw() {
        for i in 0x00..=0x0f {
            assert!(matches!(
                CompressionMethod::from_token(0x10 | i).unwrap(),
                CompressionMethod::Raw
            ));
        }
    }

    #[test]
    fn from_token_lz4() {
        for i in 0x00..=0x0f {
            assert!(matches!(
                CompressionMethod::from_token(0x20 | i).unwrap(),
                CompressionMethod::Lz4
            ));
        }
    }

    #[test]
    fn from_token_invalid() {
        for i in 0x00..=0xff {
            if i & 0xf0 != 0x10 && i & 0xf0 != 0x20 {
                assert!(CompressionMethod::from_token(i).is_err());
            }
        }
    }

    #[test]
    fn to_token_raw() {
        assert_eq!(CompressionMethod::Raw.get_token(), 0x10);
    }

    #[test]
    fn to_token_lz4() {
        assert_eq!(CompressionMethod::Lz4.get_token(), 0x20);
    }
}