Class: Omnizip::Formats::Rar::Rar5::Compression::Lzss::BitReader
- Inherits:
-
Object
- Object
- Omnizip::Formats::Rar::Rar5::Compression::Lzss::BitReader
- Defined in:
- lib/omnizip/formats/rar/rar5/compression/lzss.rb
Overview
Bit reader for reading individual bits from compressed data
Instance Attribute Summary collapse
-
#bit_pos ⇒ Object
Returns the value of attribute bit_pos.
-
#byte_pos ⇒ Object
Returns the value of attribute byte_pos.
Instance Method Summary collapse
- #end_of_data?(block_size) ⇒ Boolean
-
#initialize(data) ⇒ BitReader
constructor
A new instance of BitReader.
-
#read_bits(num_bits) ⇒ Object
Read up to 16 bits.
-
#read_bits_32(num_bits) ⇒ Object
Read 32 bits.
-
#skip_bits(num_bits) ⇒ Object
Skip specified number of bits.
Constructor Details
#initialize(data) ⇒ BitReader
Returns a new instance of BitReader.
120 121 122 123 124 |
# File 'lib/omnizip/formats/rar/rar5/compression/lzss.rb', line 120 def initialize(data) @data = data @byte_pos = 0 @bit_pos = 0 end |
Instance Attribute Details
#bit_pos ⇒ Object
Returns the value of attribute bit_pos.
199 200 201 |
# File 'lib/omnizip/formats/rar/rar5/compression/lzss.rb', line 199 def bit_pos @bit_pos end |
#byte_pos ⇒ Object
Returns the value of attribute byte_pos.
199 200 201 |
# File 'lib/omnizip/formats/rar/rar5/compression/lzss.rb', line 199 def byte_pos @byte_pos end |
Instance Method Details
#end_of_data?(block_size) ⇒ Boolean
195 196 197 |
# File 'lib/omnizip/formats/rar/rar5/compression/lzss.rb', line 195 def end_of_data?(block_size) @byte_pos >= block_size || (@byte_pos == block_size - 1 && @bit_pos >= 8) end |
#read_bits(num_bits) ⇒ Object
Read up to 16 bits
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 |
# File 'lib/omnizip/formats/rar/rar5/compression/lzss.rb', line 127 def read_bits(num_bits) return 0 if num_bits.zero? result = 0 bits_read = 0 while bits_read < num_bits return nil if @byte_pos >= @data.bytesize byte = @data.getbyte(@byte_pos) bits_available = 8 - @bit_pos bits_needed = num_bits - bits_read bits_to_read = [bits_available, bits_needed].min mask = ((1 << bits_to_read) - 1) << @bit_pos bits = (byte & mask) >> @bit_pos result |= bits << bits_read bits_read += bits_to_read @bit_pos += bits_to_read if @bit_pos >= 8 @bit_pos = 0 @byte_pos += 1 end end result end |
#read_bits_32(num_bits) ⇒ Object
Read 32 bits
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 |
# File 'lib/omnizip/formats/rar/rar5/compression/lzss.rb', line 167 def read_bits_32(num_bits) return 0 if num_bits.zero? result = 0 bits_read = 0 while bits_read < num_bits && @byte_pos < @data.bytesize byte = @data.getbyte(@byte_pos) bits_available = 8 - @bit_pos bits_needed = num_bits - bits_read bits_to_read = [bits_available, bits_needed].min mask = ((1 << bits_to_read) - 1) << @bit_pos bits = (byte & mask) >> @bit_pos result |= bits << bits_read bits_read += bits_to_read @bit_pos += bits_to_read if @bit_pos >= 8 @bit_pos = 0 @byte_pos += 1 end end result end |
#skip_bits(num_bits) ⇒ Object
Skip specified number of bits
158 159 160 161 162 163 164 |
# File 'lib/omnizip/formats/rar/rar5/compression/lzss.rb', line 158 def skip_bits(num_bits) @bit_pos += num_bits while @bit_pos >= 8 @bit_pos -= 8 @byte_pos += 1 end end |