Class: Omnizip::Algorithms::Zstandard::Frame::Block

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/omnizip/algorithms/zstandard/frame/block.rb

Overview

Zstandard block header parser (RFC 8878 Section 3.1.1.2)

Block_Header structure (3 bytes, little-endian):

  • Last_Block: bit 0
  • Block_Type: bits 1-2
  • Block_Size: bits 3-23

Constant Summary

Constants included from Constants

Constants::BLOCK_HEADER_SIZE, Constants::BLOCK_MAX_SIZE, Constants::BLOCK_TYPE_COMPRESSED, Constants::BLOCK_TYPE_RAW, Constants::BLOCK_TYPE_RESERVED, Constants::BLOCK_TYPE_RLE, Constants::BUFFER_SIZE, Constants::DEFAULT_LEVEL, Constants::DEFAULT_REPEAT_OFFSETS, Constants::FSE_MAX_ACCURACY_LOG, Constants::FSE_MIN_ACCURACY_LOG, Constants::HUFFMAN_MAX_BITS, Constants::HUFFMAN_MAX_CODE_LENGTH, Constants::HUFFMAN_MAX_LOG, Constants::HUFFMAN_STANDARD_TABLE_SIZE, Constants::LITERALS_BLOCK_COMPRESSED, Constants::LITERALS_BLOCK_RAW, Constants::LITERALS_BLOCK_RLE, Constants::LITERALS_BLOCK_TREELESS, Constants::LITERALS_LENGTH_ACCURACY_LOG, Constants::LITERAL_LENGTH_TABLE, Constants::MAGIC_BYTES, Constants::MAGIC_NUMBER, Constants::MATCH_LENGTH_ACCURACY_LOG, Constants::MATCH_LENGTH_TABLE, Constants::MAX_LEVEL, Constants::MIN_LEVEL, Constants::MODE_FSE, Constants::MODE_PREDEFINED, Constants::MODE_REPEAT, Constants::MODE_RLE, Constants::OFFSET_ACCURACY_LOG, Constants::PREDEFINED_LL_DISTRIBUTION, Constants::PREDEFINED_ML_DISTRIBUTION, Constants::PREDEFINED_OFFSET_DISTRIBUTION, Constants::REPEAT_OFFSET_1, Constants::REPEAT_OFFSET_2, Constants::REPEAT_OFFSET_3, Constants::SKIPPABLE_MAGIC_BASE, Constants::SKIPPABLE_MAGIC_MASK, Constants::WINDOW_LOG_MAX, Constants::WINDOW_LOG_MIN

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(last_block, block_type, block_size, raw_header) ⇒ Block

Initialize with parsed values

Parameters:

  • last_block (Boolean)
  • block_type (Integer)
  • block_size (Integer)
  • raw_header (Integer)


69
70
71
72
73
74
# File 'lib/omnizip/algorithms/zstandard/frame/block.rb', line 69

def initialize(last_block, block_type, block_size, raw_header)
  @last_block = last_block
  @block_type = block_type
  @block_size = block_size
  @raw_header = raw_header
end

Instance Attribute Details

#block_sizeInteger (readonly)

Returns Block size in bytes.

Returns:

  • (Integer)

    Block size in bytes



43
44
45
# File 'lib/omnizip/algorithms/zstandard/frame/block.rb', line 43

def block_size
  @block_size
end

#block_typeInteger (readonly)

Returns Block type (0=Raw, 1=RLE, 2=Compressed, 3=Reserved).

Returns:

  • (Integer)

    Block type (0=Raw, 1=RLE, 2=Compressed, 3=Reserved)



40
41
42
# File 'lib/omnizip/algorithms/zstandard/frame/block.rb', line 40

def block_type
  @block_type
end

#last_blockBoolean (readonly)

Returns True if this is the last block.

Returns:

  • (Boolean)

    True if this is the last block



37
38
39
# File 'lib/omnizip/algorithms/zstandard/frame/block.rb', line 37

def last_block
  @last_block
end

#raw_headerInteger (readonly)

Returns Block header bytes.

Returns:

  • (Integer)

    Block header bytes



46
47
48
# File 'lib/omnizip/algorithms/zstandard/frame/block.rb', line 46

def raw_header
  @raw_header
end

Class Method Details

.parse(input) ⇒ Block

Parse block header from input

Parameters:

  • input (IO)

    Input stream positioned at block header

Returns:

  • (Block)

    Parsed block header



52
53
54
55
56
57
58
59
60
61
# File 'lib/omnizip/algorithms/zstandard/frame/block.rb', line 52

def self.parse(input)
  bytes = input.read(3)
  # Read 3 bytes as little-endian 24-bit value
  raw = bytes.nil? ? 0 : (bytes.getbyte(0) | (bytes.getbyte(1) << 8) | (bytes.getbyte(2) << 16))
  last_block = raw.allbits?(0x01)
  block_type = (raw >> 1) & 0x03
  block_size = (raw >> 3) & 0x1FFFFF

  new(last_block, block_type, block_size, raw)
end

Instance Method Details

#compressed?Boolean

Check if this is a compressed block

Returns:

  • (Boolean)


93
94
95
# File 'lib/omnizip/algorithms/zstandard/frame/block.rb', line 93

def compressed?
  @block_type == BLOCK_TYPE_COMPRESSED
end

#raw?Boolean

Check if this is a raw (uncompressed) block

Returns:

  • (Boolean)


79
80
81
# File 'lib/omnizip/algorithms/zstandard/frame/block.rb', line 79

def raw?
  @block_type == BLOCK_TYPE_RAW
end

#reserved?Boolean

Check if block type is reserved

Returns:

  • (Boolean)


100
101
102
# File 'lib/omnizip/algorithms/zstandard/frame/block.rb', line 100

def reserved?
  @block_type == BLOCK_TYPE_RESERVED
end

#rle?Boolean

Check if this is an RLE block

Returns:

  • (Boolean)


86
87
88
# File 'lib/omnizip/algorithms/zstandard/frame/block.rb', line 86

def rle?
  @block_type == BLOCK_TYPE_RLE
end

#type_nameSymbol

Get block type name

Returns:

  • (Symbol)


114
115
116
117
118
119
120
121
# File 'lib/omnizip/algorithms/zstandard/frame/block.rb', line 114

def type_name
  case @block_type
  when BLOCK_TYPE_RAW then :raw
  when BLOCK_TYPE_RLE then :rle
  when BLOCK_TYPE_COMPRESSED then :compressed
  when BLOCK_TYPE_RESERVED then :reserved
  end
end

#valid?Boolean

Validate block type

Returns:

  • (Boolean)

    True if block type is valid



107
108
109
# File 'lib/omnizip/algorithms/zstandard/frame/block.rb', line 107

def valid?
  !reserved?
end