Module: LEB128

Defined in:
lib/leb128.rb

Overview

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

Class Method Summary collapse

Class Method Details

.decode_signed(io, index = 0) ⇒ Integer

Decode signed LEB128-compressed data into an integer.

Examples:

sio = StringIO.new
sio.putc(0xc1)
sio.putc(0x57)
LEB128.decode_signed(sio) #=> -5183

Parameters:

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

Returns:

  • (Integer)

Raises:

  • (EOFError)


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/leb128.rb', line 90

def self.decode_signed(io, 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) unless byte & 0x40 == 0
      break
    end
  end
  value
end

.decode_unsigned(io, index = 0) ⇒ Integer

Decode unsigned LEB128-compressed data into an integer.

Examples:

sio = StringIO.new
sio.putc(0xc1)
sio.putc(0x57)
LEB128.decode_unsigned(sio) #=> 11201

Parameters:

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

Returns:

  • (Integer)

Raises:

  • (EOFError)


64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/leb128.rb', line 64

def self.decode_unsigned(io, 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
    break if byte & 0x80 == 0
    offset += 7
  end
  value
end

.encode_signed(value) ⇒ StringIO

Encode a signed integer into LEB128-compressed data.

Examples:

LEB128.encode_signed(-0x143f) #=> #<StringIO:0x00000000000000>

Parameters:

  • value (Integer)

Returns:

  • (StringIO)


38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/leb128.rb', line 38

def self.encode_signed(value)
  io = StringIO.new
  loop do
    byte = value & 0x7F
    value >>= 7
    byte |= 0x80 unless (value == 0 && byte & 0x40 == 0) ||
                        (value == -1 && byte & 0x40 != 0)
    io.putc(byte)
    break if byte & 0x80 == 0
  end
  io.pos = 0
  io
end

.encode_unsigned(value) ⇒ StringIO

Encode an unsigned integer into LEB128-compressed data.

Examples:

LEB128.encode_unsigned(0x2bc1) #=> #<StringIO:0x00000000000000>

Parameters:

  • value (Integer)

Returns:

  • (StringIO)


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

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