| 1 | //! Block header definitions. |
| 2 | |
| 3 | /// There are 4 different kinds of blocks, and the type of block influences the meaning of `Block_Size`. |
| 4 | #[derive (Debug, Clone, Copy, PartialEq, Eq)] |
| 5 | pub enum BlockType { |
| 6 | /// An uncompressed block. |
| 7 | Raw, |
| 8 | /// A single byte, repeated `Block_Size` times (Run Length Encoding). |
| 9 | RLE, |
| 10 | /// A Zstandard compressed block. `Block_Size` is the length of the compressed data. |
| 11 | Compressed, |
| 12 | /// This is not a valid block, and this value should not be used. |
| 13 | /// If this value is present, it should be considered corrupted data. |
| 14 | Reserved, |
| 15 | } |
| 16 | |
| 17 | impl core::fmt::Display for BlockType { |
| 18 | fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> { |
| 19 | match self { |
| 20 | BlockType::Compressed => write!(f, "Compressed" ), |
| 21 | BlockType::Raw => write!(f, "Raw" ), |
| 22 | BlockType::RLE => write!(f, "RLE" ), |
| 23 | BlockType::Reserved => write!(f, "Reserverd" ), |
| 24 | } |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | /// A representation of a single block header. As well as containing a frame header, |
| 29 | /// each Zstandard frame contains one or more blocks. |
| 30 | pub struct BlockHeader { |
| 31 | /// Whether this block is the last block in the frame. |
| 32 | /// It may be followed by an optional `Content_Checksum` if it is. |
| 33 | pub last_block: bool, |
| 34 | pub block_type: BlockType, |
| 35 | /// The size of the decompressed data. If the block type |
| 36 | /// is [BlockType::Reserved] or [BlockType::Compressed], |
| 37 | /// this value is set to zero and should not be referenced. |
| 38 | pub decompressed_size: u32, |
| 39 | /// The size of the block. If the block is [BlockType::RLE], |
| 40 | /// this value will be 1. |
| 41 | pub content_size: u32, |
| 42 | } |
| 43 | |