Class: Nl::Decoder

Inherits:
Object
  • Object
show all
Defined in:
nl/lib/nl/decoder.rb

Defined Under Namespace

Classes: Error, OutOfBounds, Unterminated

Instance Method Summary collapse

Constructor Details

#initialize(buffer, offset = 0, length = buffer.size - offset) ⇒ Decoder

Returns a new instance of Decoder.



11
12
13
14
15
# File 'nl/lib/nl/decoder.rb', line 11

def initialize(buffer, offset = 0, length = buffer.size - offset)
  @buffer = buffer
  @position = offset
  @limit = length
end

Instance Method Details

#align_to(alignment) ⇒ Object

Raises:



92
93
94
95
96
# File 'nl/lib/nl/decoder.rb', line 92

def align_to(alignment)
  nposition = (@position + alignment - 1) & ~(alignment - 1)
  raise OutOfBounds if nposition > @limit
  @position = nposition
end

#available?(size = 1) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
# File 'nl/lib/nl/decoder.rb', line 17

def available?(size = 1)
  @position + size <= @limit
end

#get_buffer(length = @limit - @position) ⇒ Object

Raises:



51
52
53
54
55
56
57
# File 'nl/lib/nl/decoder.rb', line 51

def get_buffer(length = @limit - @position)
  nposition = @position + length
  raise OutOfBounds if length.negative? || nposition > @limit
  value = @buffer.slice(@position, length)
  @position = nposition
  value
end

#get_string(length = @limit - @position) ⇒ Object

Raises:



43
44
45
46
47
48
49
# File 'nl/lib/nl/decoder.rb', line 43

def get_string(length = @limit - @position)
  nposition = @position + length
  raise OutOfBounds if length.negative? || nposition > @limit
  value = @buffer.get_string(@position, length)
  @position = nposition
  value
end

#get_value(type) ⇒ Object

Raises:



76
77
78
79
80
81
82
# File 'nl/lib/nl/decoder.rb', line 76

def get_value(type)
  nposition = @position + IO::Buffer.size_of(type)
  raise OutOfBounds if nposition > @limit
  value = @buffer.get_value(type, @position)
  @position = nposition
  value
end

#get_values(types) ⇒ Object

Raises:



84
85
86
87
88
89
90
# File 'nl/lib/nl/decoder.rb', line 84

def get_values(types)
  nposition = @position + IO::Buffer.size_of(types)
  raise OutOfBounds if nposition > @limit
  values = @buffer.get_values(types, @position)
  @position = nposition
  values
end

#get_zstring(unterminated_ok: false) ⇒ Object

Raises:



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'nl/lib/nl/decoder.rb', line 59

def get_zstring(unterminated_ok: false)
  nposition = @position
  nul_found = false
  while nposition < @limit
    c = @buffer.get_value(:U8, nposition)
    nposition += 1
    if c == 0
      nul_found = true
      break
    end
  end
  raise Unterminated if !nul_found && !unterminated_ok
  value = @buffer.get_string(@position, nposition - @position - (nul_found ? 1 : 0))
  @position = nposition
  value
end

#limit(size) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'nl/lib/nl/decoder.rb', line 25

def limit(size)
  orig_limit = @limit
  @limit = @position + size
  result = yield self
  if @limit != @position
    raise "Not all bytes upto specified limit is consumed"
  end
  result
ensure
  @limit = orig_limit
end

#remaining ⇒ Object



21
22
23
# File 'nl/lib/nl/decoder.rb', line 21

def remaining
  @limit - @position
end

#skip(length = @limit - @position) ⇒ Object

Raises:



37
38
39
40
41
# File 'nl/lib/nl/decoder.rb', line 37

def skip(length = @limit - @position)
  nposition = @position + length
  raise OutOfBounds if nposition > @limit
  @position = nposition
end