Module: RDB::LZF

Defined in:
lib/rdb/lzf.rb

Defined Under Namespace

Classes: DecompressionError

Class Method Summary collapse

Class Method Details

.decompress(rdb, compressed_length, expected_length) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rdb/lzf.rb', line 7

def decompress(rdb, compressed_length, expected_length)
  ipos = opos = 0
  input, output = rdb.read(compressed_length), ' ' * expected_length

  while ipos < compressed_length
    ctrl = input.getbyte(ipos)
    ipos += 1

    if ctrl < 32
      (ctrl + 1).times do
        output.setbyte(opos, input.getbyte(ipos))
        ipos += 1
        opos += 1
      end
    else
      length = ctrl >> 5

      if length == 7
        length = length + input.getbyte(ipos)
        ipos += 1
      end

      reference = opos - ((ctrl & 0x1f) << 8) - input.getbyte(ipos) - 1
      ipos += 1

      (length + 2).times do
        output.setbyte(opos, output.getbyte(reference))
        reference += 1
        opos += 1
      end
    end
  end

  if opos != expected_length
    raise DecompressionError, "Expected length #{expected_length} does not match #{opos}"
  end

  output
end