| 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 internal; |
| 31 | |
| 32 | mod decoder_7; |
| 33 | mod decoder_8; |
| 34 | mod encoder_7; |
| 35 | mod encoder_8; |
| 36 | |
| 37 | pub use decoder_7::Ppmd7Decoder; |
| 38 | pub use decoder_8::Ppmd8Decoder; |
| 39 | pub use encoder_7::Ppmd7Encoder; |
| 40 | pub use encoder_8::Ppmd8Encoder; |
| 41 | |
| 42 | /// The minimal order PPMd7 supports. |
| 43 | pub const PPMD7_MIN_ORDER: u32 = 2; |
| 44 | |
| 45 | /// The maximal order PPMd7 supports. |
| 46 | pub const PPMD7_MAX_ORDER: u32 = 64; |
| 47 | |
| 48 | /// The minimal memory that PPMd7 supports. |
| 49 | pub const 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_SIZE: u32 = 1 << 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_ORDER: u32 = 2; |
| 60 | |
| 61 | /// The maximal order PPMd8 supports. |
| 62 | pub const PPMD8_MAX_ORDER: u32 = 16; |
| 63 | |
| 64 | /// The minimal memory that PPMd8 supports. |
| 65 | pub const 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_SIZE: u32 = 1 << 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_END: i32 = -1; |
| 75 | const SYM_ERROR: i32 = -2; |
| 76 | |
| 77 | /// Error type of the crate. |
| 78 | pub type Result<T> = core::result::Result<T, Error>; |
| 79 | |
| 80 | /// The restore method used in PPMd8. |
| 81 | #[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)] |
| 82 | pub enum RestoreMethod { |
| 83 | Restart = 0 as _, |
| 84 | CutOff = 1 as _, |
| 85 | Unsupported = 2 as _, |
| 86 | } |
| 87 | |
| 88 | macro_rules! 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_method!(u8, u16, u32, u64, u128, usize); |
| 105 | impl_from_int_for_restore_method!(i8, i16, i32, i64, i128, isize); |
| 106 | |
| 107 | /// Crate error type. |
| 108 | pub enum Error { |
| 109 | /// The range decoder could not be properly initialized. |
| 110 | RangeDecoderInitialization, |
| 111 | /// Invalid parameters provided. |
| 112 | InvalidParameter, |
| 113 | /// General IO error. |
| 114 | IoError(std::io::Error), |
| 115 | /// The needed memory could not be allocated. |
| 116 | MemoryAllocation, |
| 117 | } |
| 118 | |
| 119 | impl std::fmt::Debug for Error { |
| 120 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 121 | match self { |
| 122 | Error::RangeDecoderInitialization => { |
| 123 | write!(f, "Could not initialize the range decoder") |
| 124 | } |
| 125 | Error::InvalidParameter => write!(f, "Wrong PPMd parameter"), |
| 126 | Error::IoError(err) => write!(f, "Io error: {err}"), |
| 127 | Error::MemoryAllocation => write!(f, "Memory allocation error (out of memory?)"), |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | impl std::fmt::Display for Error { |
| 133 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 134 | std::fmt::Debug::fmt(self, f) |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | impl std::error::Error for Error {} |