Class: Omnizip::Algorithms::LZMA::Dictionary

Inherits:
Object
  • Object
show all
Defined in:
lib/omnizip/algorithms/lzma/dictionary.rb

Overview

Circular buffer dictionary for LZMA sliding window Ported from XZ Utils lzma_decoder.c

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(size) ⇒ Dictionary

Returns a new instance of Dictionary.



12
13
14
15
16
# File 'lib/omnizip/algorithms/lzma/dictionary.rb', line 12

def initialize(size)
  @size = size
  @buffer = String.new(encoding: Encoding::BINARY)
  @position = 0
end

Instance Attribute Details

#bufferObject

Returns the value of attribute buffer.



9
10
11
# File 'lib/omnizip/algorithms/lzma/dictionary.rb', line 9

def buffer
  @buffer
end

#positionObject

Returns the value of attribute position.



9
10
11
# File 'lib/omnizip/algorithms/lzma/dictionary.rb', line 9

def position
  @position
end

#sizeObject (readonly)

Returns the value of attribute size.



10
11
12
# File 'lib/omnizip/algorithms/lzma/dictionary.rb', line 10

def size
  @size
end

Instance Method Details

#append(data) ⇒ Object

Append bytes to dictionary



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/omnizip/algorithms/lzma/dictionary.rb', line 19

def append(data)
  data.each_byte do |byte|
    @buffer << byte
    @position += 1

    # Trim if exceeds size
    if @buffer.bytesize > @size
      excess = @buffer.bytesize - @size
      @buffer = @buffer.byteslice(excess..-1)
    end
  end
end

#cloneObject

Clone dictionary



61
62
63
64
65
66
# File 'lib/omnizip/algorithms/lzma/dictionary.rb', line 61

def clone
  dict = Dictionary.new(@size)
  dict.buffer = @buffer.dup
  dict.position = @position
  dict
end

#get_byte(distance) ⇒ Object

Get byte at distance back



48
49
50
51
52
# File 'lib/omnizip/algorithms/lzma/dictionary.rb', line 48

def get_byte(distance)
  raise "Invalid distance: #{distance}" if distance > @buffer.bytesize

  @buffer.getbyte(@buffer.bytesize - distance)
end

#read_bytes(distance, length) ⇒ Object

Read bytes from dictionary at a distance back



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/omnizip/algorithms/lzma/dictionary.rb', line 33

def read_bytes(distance, length)
  raise "Invalid distance: #{distance}" if distance > @buffer.bytesize

  result = String.new(encoding: Encoding::BINARY)
  src_pos = @buffer.bytesize - distance

  length.times do |i|
    byte = @buffer[(src_pos + i) % @buffer.bytesize]
    result << byte
  end

  result
end

#reset!Object

Reset dictionary



55
56
57
58
# File 'lib/omnizip/algorithms/lzma/dictionary.rb', line 55

def reset!
  @buffer.clear
  @position = 0
end