| 1 | //! PPMd compression / decompression. It's a port of the PPMd C-code from 7-Zip to Rust. |
| 2 | //! |
| 3 | //! The following variants are provided: |
| 4 | //! |
| 5 | //! - The PPMd7 (PPMdH) as used by the 7z archive format |
| 6 | //! - The PPMd8 (PPMdI rev.1) as used by the zip archive format |
| 7 | //! |
| 8 | //! ## Notice |
| 9 | //! |
| 10 | //! There are two ways to properly bound the uncompressed data: |
| 11 | //! 1. Save the uncompressed size along the compressed data and use `std::io::Read::read_exact()` |
| 12 | //! to read the data (this is what is used in the 7z archive format). |
| 13 | //! 2. Encode an end marker by calling `finish(true)` on the encoder when finishing the encoder |
| 14 | //! process. You can then use `std::io::Read::read_to_end()` to read the data. You are of course |
| 15 | //! free to also use the `std::io::Read::read_exact()` if you have stored the uncompressed data |
| 16 | //! size (this is what is used in the zip archive format). |
| 17 | //! |
| 18 | //! Failing to do so will result in garbage symbols at the end of the actual data. |
| 19 | //! |
| 20 | //! ## Acknowledgement |
| 21 | //! |
| 22 | //! This port is based on the 7zip version of PPMd by Igor Pavlov, which in turn was based on the |
| 23 | //! PPMd var.H (2001) / PPMd var.I (2002) code by Dmitry Shkarin. The carryless range coder of |
| 24 | //! PPMd8 was originally written by Dmitry Subbotin (1999). |
| 25 | //! |
| 26 | //! ## License |
| 27 | //! |
| 28 | //! The code in this crate is in the public domain as the original code by their authors. |
| 29 | |
| 30 | mod internalrust-analyzer cargo ppmd-rust 1.2.1 internal/; |
| 31 | |
| 32 | mod decoder_7rust-analyzer cargo ppmd-rust 1.2.1 decoder_7/; |
| 33 | mod decoder_8rust-analyzer cargo ppmd-rust 1.2.1 decoder_8/; |
| 34 | mod encoder_7rust-analyzer cargo ppmd-rust 1.2.1 encoder_7/; |
| 35 | mod encoder_8rust-analyzer cargo ppmd-rust 1.2.1 encoder_8/; |
| 36 | |
| 37 | pub use decoder_7rust-analyzer cargo ppmd-rust 1.2.1 decoder_7/::Ppmd7Decoderrust-analyzer cargo ppmd-rust 1.2.1 decoder_7/Ppmd7Decoder#; |
| 38 | pub use decoder_8rust-analyzer cargo ppmd-rust 1.2.1 decoder_8/::Ppmd8Decoderrust-analyzer cargo ppmd-rust 1.2.1 decoder_8/Ppmd8Decoder#; |
| 39 | pub use encoder_7rust-analyzer cargo ppmd-rust 1.2.1 encoder_7/::Ppmd7Encoderrust-analyzer cargo ppmd-rust 1.2.1 encoder_7/Ppmd7Encoder#; |
| 40 | pub use encoder_8rust-analyzer cargo ppmd-rust 1.2.1 encoder_8/::Ppmd8Encoderrust-analyzer cargo ppmd-rust 1.2.1 encoder_8/Ppmd8Encoder#; |
| 41 | |
| 42 | /// The minimal order PPMd7 supports. |
| 43 | pub const PPMD7_MIN_ORDERrust-analyzer cargo ppmd-rust 1.2.1 PPMD7_MIN_ORDER.: u32 = 2; |
| 44 | |
| 45 | /// The maximal order PPMd7 supports. |
| 46 | pub const PPMD7_MAX_ORDERrust-analyzer cargo ppmd-rust 1.2.1 PPMD7_MAX_ORDER.: u32 = 64; |
| 47 | |
| 48 | /// The minimal memory that PPMd7 supports. |
| 49 | pub const PPMD7_MIN_MEM_SIZErust-analyzer cargo ppmd-rust 1.2.1 PPMD7_MIN_MEM_SIZE.: u32 = 2048; |
| 50 | |
| 51 | #[cfg(feature = "unstable-tagged-offsets")] |
| 52 | /// The maximal memory that PPMd7 supports (512 MiB). |
| 53 | pub const PPMD7_MAX_MEM_SIZErust-analyzer cargo ppmd-rust 1.2.1 PPMD7_MAX_MEM_SIZE.: u32 = 1 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`Shl<i32>`]shl().<<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`Shl<i32>`]shl(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`Shl<i32>`]shl().29; |
| 54 | #[cfg(not(feature = "unstable-tagged-offsets"))] |
| 55 | /// The maximal memory that PPMd7 supports (~4 GiB). |
| 56 | pub const PPMD7_MAX_MEM_SIZE: u32 = u32::MAX - 36; |
| 57 | |
| 58 | /// The minimal order PPMd8 supports. |
| 59 | pub const PPMD8_MIN_ORDERrust-analyzer cargo ppmd-rust 1.2.1 PPMD8_MIN_ORDER.: u32 = 2; |
| 60 | |
| 61 | /// The maximal order PPMd8 supports. |
| 62 | pub const PPMD8_MAX_ORDERrust-analyzer cargo ppmd-rust 1.2.1 PPMD8_MAX_ORDER.: u32 = 16; |
| 63 | |
| 64 | /// The minimal memory that PPMd8 supports. |
| 65 | pub const PPMD8_MIN_MEM_SIZErust-analyzer cargo ppmd-rust 1.2.1 PPMD8_MIN_MEM_SIZE.: u32 = 2048; |
| 66 | |
| 67 | #[cfg(feature = "unstable-tagged-offsets")] |
| 68 | /// The maximal memory that PPMd8 supports (512 MiB). |
| 69 | pub const PPMD8_MAX_MEM_SIZErust-analyzer cargo ppmd-rust 1.2.1 PPMD8_MAX_MEM_SIZE.: u32 = 1 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`Shl<i32>`]shl().<<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`Shl<i32>`]shl(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`Shl<i32>`]shl().29; |
| 70 | #[cfg(not(feature = "unstable-tagged-offsets"))] |
| 71 | /// The maximal memory that PPMd8 supports (4 GiB). |
| 72 | pub const PPMD8_MAX_MEM_SIZE: u32 = u32::MAX; |
| 73 | |
| 74 | const SYM_ENDrust-analyzer cargo ppmd-rust 1.2.1 SYM_END.: i32 = -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][Neg]neg().1; |
| 75 | const SYM_ERRORrust-analyzer cargo ppmd-rust 1.2.1 SYM_ERROR.: i32 = -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][Neg]neg().2; |
| 76 | |
| 77 | /// Error type of the crate. |
| 78 | pub type Resultrust-analyzer cargo ppmd-rust 1.2.1 Result#<T> = corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<T, Errorrust-analyzer cargo ppmd-rust 1.2.1 Error#>; |
| 79 | |
| 80 | /// The restore method used in PPMd8. |
| 81 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord:, PartialOrdrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/macros/Hash:)] |
| 82 | pub enum RestoreMethodrust-analyzer cargo ppmd-rust 1.2.1 RestoreMethod# { |
| 83 | Restartrust-analyzer cargo ppmd-rust 1.2.1 RestoreMethod#Restart# = 0 as _, |
| 84 | CutOffrust-analyzer cargo ppmd-rust 1.2.1 RestoreMethod#CutOff# = 1 as _, |
| 85 | Unsupportedrust-analyzer cargo ppmd-rust 1.2.1 RestoreMethod#Unsupported# = 2 as _, |
| 86 | } |
| 87 | |
| 88 | macro_rules! impl_from_int_for_restore_methodrust-analyzer cargo ppmd-rust 1.2.1 impl_from_int_for_restore_method! { |
| 89 | ($($int_type:ty),+ $(,)?) => { |
| 90 | $( |
| 91 | impl From<$int_type> for RestoreMethod { |
| 92 | fn from(value: $int_type) -> Self { |
| 93 | match value { |
| 94 | 0 => RestoreMethod::Restart, |
| 95 | 1 => RestoreMethod::CutOff, |
| 96 | _ => RestoreMethod::Unsupported, |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | )+ |
| 101 | }; |
| 102 | } |
| 103 | |
| 104 | impl_from_int_for_restore_methodrust-analyzer cargo ppmd-rust 1.2.1 impl_from_int_for_restore_method!!(u8, u16, u32, u64, u128, usize); |
| 105 | impl_from_int_for_restore_methodrust-analyzer cargo ppmd-rust 1.2.1 impl_from_int_for_restore_method!!(i8, i16, i32, i64, i128, isize); |
| 106 | |
| 107 | /// Crate error type. |
| 108 | pub enum Errorrust-analyzer cargo ppmd-rust 1.2.1 Error# { |
| 109 | /// The range decoder could not be properly initialized. |
| 110 | RangeDecoderInitializationrust-analyzer cargo ppmd-rust 1.2.1 Error#RangeDecoderInitialization#, |
| 111 | /// Invalid parameters provided. |
| 112 | InvalidParameterrust-analyzer cargo ppmd-rust 1.2.1 Error#InvalidParameter#, |
| 113 | /// General IO error. |
| 114 | IoErrorrust-analyzer cargo ppmd-rust 1.2.1 Error#IoError#(stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#), |
| 115 | /// The needed memory could not be allocated. |
| 116 | MemoryAllocationrust-analyzer cargo ppmd-rust 1.2.1 Error#MemoryAllocation#, |
| 117 | } |
| 118 | |
| 119 | impl stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug# for Errorrust-analyzer cargo ppmd-rust 1.2.1 Error# { |
| 120 | fn fmtrust-analyzer cargo ppmd-rust 1.2.1 impl#[Error][Debug]fmt().(&self, f: &mut stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#<'_>) -> stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# { |
| 121 | match self { |
| 122 | Errorrust-analyzer cargo ppmd-rust 1.2.1 Error#::RangeDecoderInitializationrust-analyzer cargo ppmd-rust 1.2.1 Error#RangeDecoderInitialization# => { |
| 123 | writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "Could not initialize the range decoder") |
| 124 | } |
| 125 | Errorrust-analyzer cargo ppmd-rust 1.2.1 Error#::InvalidParameterrust-analyzer cargo ppmd-rust 1.2.1 Error#InvalidParameter# => writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "Wrong PPMd parameter"), |
| 126 | Errorrust-analyzer cargo ppmd-rust 1.2.1 Error#::IoErrorrust-analyzer cargo ppmd-rust 1.2.1 Error#IoError#(err) => writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "Io error: {err}"), |
| 127 | Errorrust-analyzer cargo ppmd-rust 1.2.1 Error#::MemoryAllocationrust-analyzer cargo ppmd-rust 1.2.1 Error#MemoryAllocation# => writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "Memory allocation error (out of memory?)"), |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | impl stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display# for Errorrust-analyzer cargo ppmd-rust 1.2.1 Error# { |
| 133 | fn fmtrust-analyzer cargo ppmd-rust 1.2.1 impl#[Error][Display]fmt().(&self, f: &mut stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#<'_>) -> stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# { |
| 134 | stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug#::fmtrust-analyzer cargo ppmd-rust 1.2.1 impl#[Error][Debug]fmt().(self, f) |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | impl stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std error/::Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error# for Errorrust-analyzer cargo ppmd-rust 1.2.1 Error# {} |