Class: LZ4

Inherits:
Object
  • Object
show all
Defined in:
lib/lz4-ruby.rb

Class Method Summary collapse

Class Method Details

._compress(input, in_size, high_compression) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/lz4-ruby.rb', line 18

def self._compress(input, in_size, high_compression)
  in_size = input.length if in_size == nil
  header = encode_varbyte(in_size)

  if high_compression
    return LZ4Internal.compressHC(header, input, in_size)
  else
    return LZ4Internal.compress(header, input, in_size)
  end
end

.compress(input, in_size = nil) ⇒ Object



10
11
12
# File 'lib/lz4-ruby.rb', line 10

def self.compress(input, in_size = nil)
  return _compress(input, in_size, false)
end

.compressHC(input, in_size = nil) ⇒ Object



14
15
16
# File 'lib/lz4-ruby.rb', line 14

def self.compressHC(input, in_size = nil)
  return _compress(input, in_size, true)
end

.decode_varbyte(text) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/lz4-ruby.rb', line 58

def self.decode_varbyte(text)
  len = [text.length, 5].min
  bytes = text[0, len].unpack("C*")

  varbyte_len = 0
  val = 0
  bytes.each do |b|
    val |= (b & 0x7f) << (7 * varbyte_len)
    varbyte_len += 1
    return val, varbyte_len if b & 0x80 == 0
  end

  return -1, -1
end

.encode_varbyte(val) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/lz4-ruby.rb', line 40

def self.encode_varbyte(val)
  varbytes = []

  loop do
    byte = val & 0x7f
    val >>= 7

    if val == 0
      varbytes.push(byte)
      break
    else
      varbytes.push(byte | 0x80)
    end
  end

  return varbytes.pack("C*")
end

.uncompress(input, in_size = nil) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/lz4-ruby.rb', line 29

def self.uncompress(input, in_size = nil)
  in_size = input.length if in_size == nil
  out_size, varbyte_len = decode_varbyte(input)

  if out_size < 0 || varbyte_len < 0
    raise "Compressed data is maybe corrupted"
  end
  
  return LZ4Internal::uncompress(input, in_size, varbyte_len, out_size)
end