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
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
#[cfg(feature = "lz4_flex")]
use lz4_flex::block::{
    CompressError as Lz4FlexCompressError, DecompressError as Lz4FlexDecompressError,
};

use std::error::Error as StdError;
use std::fmt;
pub(crate) use std::io::{Error as IoError, ErrorKind as IoErrorKind};
use std::result::Result as StdResult;

pub(crate) type Result<T> = StdResult<T, Error>;

// ErrorInternal

#[derive(Debug)]
pub(crate) struct ErrorInternal {
    description: &'static str,
}
impl ErrorInternal {
    pub(crate) fn new(description: &'static str) -> Self {
        Self { description }
    }
    pub(crate) fn new_error<R, E: From<Self>>(description: &'static str) -> StdResult<R, E> {
        Err(Self::new(description).into())
    }
}
impl fmt::Display for ErrorInternal {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "internal error: {}", self.description)
    }
}
impl std::error::Error for ErrorInternal {}

// ErrorMagicNumber

#[derive(Debug)]
pub(crate) struct ErrorMagicNumber {
    expected: u64,
    actual: u64,
}
impl ErrorMagicNumber {
    pub(crate) fn new(expected: u64, actual: u64) -> Self {
        Self { expected, actual }
    }
    pub(crate) fn new_error<R, E: From<Self>>(expected: u64, actual: u64) -> StdResult<R, E> {
        Err(Self::new(expected, actual).into())
    }
}
impl fmt::Display for ErrorMagicNumber {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "wrong magic number from header: {:016x?} instead of {:016x?}",
            self.actual, self.expected
        )
    }
}
impl std::error::Error for ErrorMagicNumber {}

// ErrorCompressionMethod

#[derive(Debug)]
pub(crate) struct ErrorCompressionMethod {
    token: u8,
}
impl ErrorCompressionMethod {
    pub(crate) fn new(token: u8) -> Self {
        Self { token }
    }
    pub(crate) fn new_error<R, E: From<Self>>(token: u8) -> StdResult<R, E> {
        Err(Self::new(token).into())
    }
}
impl fmt::Display for ErrorCompressionMethod {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "wrong token {:02x?}: unknown compression method",
            self.token
        )
    }
}
impl std::error::Error for ErrorCompressionMethod {}

// ErrorDecompressedSizeTooBig

#[derive(Debug)]
pub(crate) struct ErrorDecompressedSizeTooBig {
    decompressed_size: u32,
    max_size: usize,
}
impl ErrorDecompressedSizeTooBig {
    pub(crate) fn new(decompressed_size: u32, max_size: usize) -> Self {
        Self {
            decompressed_size,
            max_size,
        }
    }
    pub(crate) fn new_error<R, E: From<Self>>(
        decompressed_size: u32,
        max_size: usize,
    ) -> StdResult<R, E> {
        Err(Self::new(decompressed_size, max_size).into())
    }
}
impl fmt::Display for ErrorDecompressedSizeTooBig {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "the decompressed size: {} is bigger than the maximum size: {}",
            self.decompressed_size, self.max_size,
        )
    }
}
impl std::error::Error for ErrorDecompressedSizeTooBig {}

// ErrorCompressedSizeTooBig

#[derive(Debug)]
pub(crate) struct ErrorCompressedSizeTooBig {
    compressed_size: u32,
    max_size: u32,
}
impl ErrorCompressedSizeTooBig {
    pub(crate) fn new(compressed_size: u32, max_size: u32) -> Self {
        Self {
            compressed_size,
            max_size,
        }
    }
    pub(crate) fn new_error<R, E: From<Self>>(
        compressed_size: u32,
        max_size: u32,
    ) -> StdResult<R, E> {
        Err(Self::new(compressed_size, max_size).into())
    }
}
impl fmt::Display for ErrorCompressedSizeTooBig {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "the compressed size: {} is bigger than the maximum size: {}",
            self.compressed_size, self.max_size,
        )
    }
}
impl std::error::Error for ErrorCompressedSizeTooBig {}

// ErrorIncoherentSize

#[derive(Debug)]
pub(crate) struct ErrorIncoherentSize {
    decompressed_size: u32,
    compressed_size: u32,
}
impl ErrorIncoherentSize {
    pub(crate) fn new(decompressed_size: u32, compressed_size: u32) -> Self {
        Self {
            decompressed_size,
            compressed_size,
        }
    }
    pub(crate) fn new_error<R, E: From<Self>>(
        decompressed_size: u32,
        compressed_size: u32,
    ) -> StdResult<R, E> {
        Err(Self::new(decompressed_size, compressed_size).into())
    }
}
impl fmt::Display for ErrorIncoherentSize {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "the decompressed size: {} no coherent with the compressed size: {}",
            self.decompressed_size, self.compressed_size,
        )
    }
}
impl std::error::Error for ErrorIncoherentSize {}

// ErrorNoCompressionDifferentSize

#[derive(Debug)]
pub(crate) struct ErrorNoCompressionDifferentSize {
    compressed_size: u32,
    decompressed_size: u32,
}
impl ErrorNoCompressionDifferentSize {
    pub(crate) fn new(compressed_size: u32, decompressed_size: u32) -> Self {
        Self {
            compressed_size,
            decompressed_size,
        }
    }
    pub(crate) fn new_error<R, E: From<Self>>(
        compressed_size: u32,
        decompressed_size: u32,
    ) -> StdResult<R, E> {
        Err(Self::new(compressed_size, decompressed_size).into())
    }
}
impl fmt::Display for ErrorNoCompressionDifferentSize {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "without compression, the compressed size: {} different than the decompressed size: {}",
            self.compressed_size, self.decompressed_size,
        )
    }
}
impl std::error::Error for ErrorNoCompressionDifferentSize {}

// ErrorChecksum

#[derive(Debug)]
pub(crate) struct ErrorChecksum {
    header_value: u32,
    computed_value: u32,
}
impl ErrorChecksum {
    pub(crate) fn new(header_value: u32, computed_value: u32) -> Self {
        Self {
            header_value,
            computed_value,
        }
    }
    pub(crate) fn new_error<R, E: From<Self>>(
        header_value: u32,
        computed_value: u32,
    ) -> StdResult<R, E> {
        Err(Self::new(header_value, computed_value).into())
    }
}
impl fmt::Display for ErrorChecksum {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "wrong checksum: header value={:08X} computed value={:08X}",
            self.header_value, self.computed_value
        )
    }
}
impl std::error::Error for ErrorChecksum {}

// ErrorLz4WrongDecompressedSize

#[derive(Debug)]
pub(crate) struct ErrorLz4WrongDecompressedSize {
    expected_uncompressed_size: usize,
    uncompressed_size: usize,
}
impl ErrorLz4WrongDecompressedSize {
    pub(crate) fn new(expected_uncompressed_size: usize, uncompressed_size: usize) -> Self {
        Self {
            expected_uncompressed_size,
            uncompressed_size,
        }
    }
    pub(crate) fn new_error<R, E: From<Self>>(
        expected_uncompressed_size: usize,
        uncompressed_size: usize,
    ) -> StdResult<R, E> {
        Err(Self::new(expected_uncompressed_size, uncompressed_size).into())
    }
}
impl fmt::Display for ErrorLz4WrongDecompressedSize {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "the uncompressed data size using LZ4={} is different than the value from the header={}",
            self.uncompressed_size, self.expected_uncompressed_size
        )
    }
}
impl std::error::Error for ErrorLz4WrongDecompressedSize {}

// Lz4Flex

#[derive(Debug)]
pub enum Lz4Error {
    #[cfg(feature = "lz4_flex")]
    Lz4FlexCompressError(Lz4FlexCompressError),
    #[cfg(feature = "lz4_flex")]
    Lz4FlexDecompressError(Lz4FlexDecompressError),
    #[cfg(feature = "lz4-sys")]
    Lz4SysCompressError,
    #[cfg(feature = "lz4-sys")]
    Lz4SysDecompressError,
}
impl fmt::Display for Lz4Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            #[cfg(feature = "lz4_flex")]
            Self::Lz4FlexCompressError(e) => write!(f, "lz4_flex compression failed: {}", e),
            #[cfg(feature = "lz4_flex")]
            Self::Lz4FlexDecompressError(e) => write!(f, "lz4_flex decompression failed: {}", e),
            #[cfg(feature = "lz4-sys")]
            Self::Lz4SysCompressError => write!(f, "lz4-sys compression failed"),
            #[cfg(feature = "lz4-sys")]
            Self::Lz4SysDecompressError => write!(f, "lz4-sys decompression failed"),
        }
    }
}
impl StdError for Lz4Error {
    fn source(&self) -> Option<&(dyn StdError + 'static)> {
        match self {
            #[cfg(feature = "lz4_flex")]
            Self::Lz4FlexCompressError(e) => Some(e),
            #[cfg(feature = "lz4_flex")]
            Self::Lz4FlexDecompressError(e) => Some(e),
            #[cfg(feature = "lz4-sys")]
            Self::Lz4SysCompressError => None,
            #[cfg(feature = "lz4-sys")]
            Self::Lz4SysDecompressError => None,
        }
    }
}
#[cfg(feature = "lz4_flex")]
impl From<Lz4FlexCompressError> for Lz4Error {
    fn from(error: Lz4FlexCompressError) -> Self {
        Self::Lz4FlexCompressError(error)
    }
}
#[cfg(feature = "lz4_flex")]
impl From<Lz4FlexDecompressError> for Lz4Error {
    fn from(error: Lz4FlexDecompressError) -> Self {
        Self::Lz4FlexDecompressError(error)
    }
}

// Error

#[derive(Debug)]
pub(crate) enum Error {
    Internal(ErrorInternal),
    MagicNumber(ErrorMagicNumber),
    CompressionMethod(ErrorCompressionMethod),
    DecompressedSizeTooBig(ErrorDecompressedSizeTooBig),
    CompressedSizeTooBig(ErrorCompressedSizeTooBig),
    IncoherentSize(ErrorIncoherentSize),
    NoCompressionDifferentSize(ErrorNoCompressionDifferentSize),
    Checksum(ErrorChecksum),
    Lz4WrongDecompressedSize(ErrorLz4WrongDecompressedSize),
    Lz4(Lz4Error),
    Io(IoError),
}
impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::Internal(e) => e.fmt(f),
            Self::MagicNumber(e) => e.fmt(f),
            Self::CompressionMethod(e) => e.fmt(f),
            Self::DecompressedSizeTooBig(e) => e.fmt(f),
            Self::CompressedSizeTooBig(e) => e.fmt(f),
            Self::IncoherentSize(e) => e.fmt(f),
            Self::NoCompressionDifferentSize(e) => e.fmt(f),
            Self::Checksum(e) => e.fmt(f),
            Self::Lz4WrongDecompressedSize(e) => e.fmt(f),
            Self::Lz4(e) => e.fmt(f),
            Self::Io(e) => e.fmt(f),
        }
    }
}
impl From<&'static str> for Error {
    fn from(error: &'static str) -> Self {
        Self::Internal(ErrorInternal { description: error })
    }
}
impl From<ErrorInternal> for Error {
    fn from(error: ErrorInternal) -> Self {
        Self::Internal(error)
    }
}
impl From<ErrorMagicNumber> for Error {
    fn from(error: ErrorMagicNumber) -> Self {
        Self::MagicNumber(error)
    }
}
impl From<ErrorCompressionMethod> for Error {
    fn from(error: ErrorCompressionMethod) -> Self {
        Self::CompressionMethod(error)
    }
}
impl From<ErrorDecompressedSizeTooBig> for Error {
    fn from(error: ErrorDecompressedSizeTooBig) -> Self {
        Self::DecompressedSizeTooBig(error)
    }
}
impl From<ErrorCompressedSizeTooBig> for Error {
    fn from(error: ErrorCompressedSizeTooBig) -> Self {
        Self::CompressedSizeTooBig(error)
    }
}
impl From<ErrorIncoherentSize> for Error {
    fn from(error: ErrorIncoherentSize) -> Self {
        Self::IncoherentSize(error)
    }
}
impl From<ErrorNoCompressionDifferentSize> for Error {
    fn from(error: ErrorNoCompressionDifferentSize) -> Self {
        Self::NoCompressionDifferentSize(error)
    }
}
impl From<ErrorChecksum> for Error {
    fn from(error: ErrorChecksum) -> Self {
        Self::Checksum(error)
    }
}
impl From<ErrorLz4WrongDecompressedSize> for Error {
    fn from(error: ErrorLz4WrongDecompressedSize) -> Self {
        Self::Lz4WrongDecompressedSize(error)
    }
}
impl From<Lz4Error> for Error {
    fn from(error: Lz4Error) -> Self {
        Self::Lz4(error)
    }
}
impl From<IoError> for Error {
    fn from(error: IoError) -> Self {
        Self::Io(error)
    }
}
impl From<Error> for IoError {
    fn from(error: Error) -> Self {
        match error {
            Error::Internal(err) => Self::new(IoErrorKind::Other, err),
            Error::MagicNumber(err) => Self::new(IoErrorKind::InvalidData, err),
            Error::CompressionMethod(err) => Self::new(IoErrorKind::InvalidData, err),
            Error::DecompressedSizeTooBig(err) => Self::new(IoErrorKind::InvalidData, err),
            Error::CompressedSizeTooBig(err) => Self::new(IoErrorKind::InvalidData, err),
            Error::IncoherentSize(err) => Self::new(IoErrorKind::InvalidData, err),
            Error::NoCompressionDifferentSize(err) => Self::new(IoErrorKind::InvalidData, err),
            Error::Checksum(err) => Self::new(IoErrorKind::InvalidData, err),
            Error::Lz4WrongDecompressedSize(err) => Self::new(IoErrorKind::InvalidData, err),
            Error::Lz4(err) => Self::new(IoErrorKind::Other, err),
            Error::Io(err) => err,
        }
    }
}

// Checksum

pub(crate) struct Checksum {
    f: fn(&[u8]) -> u32,
}

impl Checksum {
    pub(crate) fn new(f: fn(&[u8]) -> u32) -> Self {
        Self { f }
    }

    pub(crate) fn run(&self, buf: &[u8]) -> u32 {
        let f = self.f;
        f(buf)
    }
}

impl fmt::Debug for Checksum {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Pointer::fmt(&(self.f as *const ()), f)
    }
}

// ErrorWrongBlockSize

#[derive(Debug)]
pub(crate) struct ErrorWrongBlockSize {
    size: usize,
    min_size: usize,
    max_size: usize,
}
impl ErrorWrongBlockSize {
    pub(crate) fn new(size: usize, min_size: usize, max_size: usize) -> Self {
        Self {
            size,
            min_size,
            max_size,
        }
    }
    pub(crate) fn new_error<R, E: From<Self>>(
        size: usize,
        min_size: usize,
        max_size: usize,
    ) -> StdResult<R, E> {
        Err(Self::new(size, min_size, max_size).into())
    }
}
impl fmt::Display for ErrorWrongBlockSize {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "wrong block size {}. It should be between {} and {}",
            self.size, self.min_size, self.max_size
        )
    }
}
impl std::error::Error for ErrorWrongBlockSize {}
impl From<ErrorWrongBlockSize> for IoError {
    fn from(error: ErrorWrongBlockSize) -> Self {
        Self::new(IoErrorKind::InvalidData, error)
    }
}