Module: LEB128

Defined in:
lib/leb128.rb

Overview

Includes a pair of functions to encode and decode the LEB128 integer compression type.

Class Method Summary collapse

Class Method Details

.decode(io, signed, index = 0) ⇒ Integer

Decode LEB128-compressed data into an integer.

Examples:

sio = StringIO.new
sio.putc(0xc1)
sio.putc(0x57)
LEB128.decode(sio, false) #=> 11201

Parameters:

  • io (StringIO)
  • signed (Boolean)
  • index (Integer) (defaults to: 0)

Returns:

  • (Integer)

Raises:

  • (EOFError)


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/leb128.rb', line 47

def self.decode(io, signed, index = 0)
  io.pos = index
  raise(EOFError) if io.eof?
  value = 0
  offset = 0
  loop do
    byte = io.read(1).unpack('C')[0]
    value |= (byte & 0x7F) << offset
    offset += 7
    if byte & 0x80 == 0
      value |= -(1 << offset) if signed && byte & 0x40 != 0
      break
    end
  end
  value
end

.encode(value, signed) ⇒ StringIO

Encode an integer into LEB128-compressed data.

Examples:

LEB128.encode(-0x143f, true) #=> #<StringIO:0x00000003533e20>

Parameters:

  • value (Integer)
  • signed (Boolean)

Returns:

  • (StringIO)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/leb128.rb', line 14

def self.encode(value, signed)
  if value < 0 && !signed
    raise(ArgumentError, 'Cannot encode a negative integer to an unsigned LEB128')
  end
  io = StringIO.new
  loop do
    byte = value & 0x7F
    value >>= 7
    if signed
      byte |= 0x80 unless (value == 0 && byte & 0x40 == 0) || (value == -1 && byte & 0x40 != 0)
    elsif value != 0
      byte |= 0x80
    end
    io.putc(byte)
    break if (signed && byte & 0x80 == 0) || (!signed && value == 0)
  end
  io.pos = 0
  io
end