class CryptoconditionsRuby::Utils::Reader
Constants
- HIGH_BIT
- LOWER_SEVEN_BITS
- MAX_INT_BYTES
Attributes
bookmarks[RW]
buffer[RW]
cursor[RW]
Public Class Methods
from_source(source)
click to toggle source
# File lib/cryptoconditions_ruby/utils/reader.rb, line 13 def self.from_source(source) source.is_a?(self) ? source : new(source) end
new(buffer)
click to toggle source
# File lib/cryptoconditions_ruby/utils/reader.rb, line 7 def initialize(buffer) @buffer = buffer @cursor = 0 @bookmarks = [] end
Public Instance Methods
bookmark()
click to toggle source
# File lib/cryptoconditions_ruby/utils/reader.rb, line 17 def bookmark self.bookmarks << cursor end
ensure_available(num_bytes)
click to toggle source
# File lib/cryptoconditions_ruby/utils/reader.rb, line 25 def ensure_available(num_bytes) if buffer.length < cursor + num_bytes raise RangeError.new("Tried to read #{num_bytes} bytes, but only #{buffer.length - cursor} bytes available") end end
peek(num_bytes)
click to toggle source
# File lib/cryptoconditions_ruby/utils/reader.rb, line 111 def peek(num_bytes) read(num_bytes, peek: true) end
peek_octet_string(length)
click to toggle source
# File lib/cryptoconditions_ruby/utils/reader.rb, line 71 def peek_octet_string(length) peek(length) end
peek_uint(length)
click to toggle source
# File lib/cryptoconditions_ruby/utils/reader.rb, line 41 def peek_uint(length) read_uint(length, peek: true) end
peek_uint16()
click to toggle source
# File lib/cryptoconditions_ruby/utils/reader.rb, line 140 def peek_uint16 peek_uint(2) end
peek_uint32()
click to toggle source
# File lib/cryptoconditions_ruby/utils/reader.rb, line 144 def peek_uint32 peek_uint(4) end
peek_uint64()
click to toggle source
# File lib/cryptoconditions_ruby/utils/reader.rb, line 148 def peek_uint64 peek_uint(8) end
peek_uint8()
click to toggle source
# File lib/cryptoconditions_ruby/utils/reader.rb, line 136 def peek_uint8 peek_uint(1) end
peek_var_octet_string()
click to toggle source
# File lib/cryptoconditions_ruby/utils/reader.rb, line 93 def peek_var_octet_string bookmark read_var_octet_string.tap { restore } end
peek_var_uint()
click to toggle source
# File lib/cryptoconditions_ruby/utils/reader.rb, line 58 def peek_var_uint bookmark read_var_uint.tap { restore } end
read(num_bytes, peek: false)
click to toggle source
# File lib/cryptoconditions_ruby/utils/reader.rb, line 103 def read(num_bytes, peek: false) ensure_available(num_bytes) value = buffer[cursor...(cursor + num_bytes)] self.cursor += num_bytes unless peek value end
read_length_prefix()
click to toggle source
# File lib/cryptoconditions_ruby/utils/reader.rb, line 79 def read_length_prefix length = read_uint8 if length & HIGH_BIT > 0 return read_uint(length & LOWER_SEVEN_BITS) end length end
read_octet_string(length)
click to toggle source
# File lib/cryptoconditions_ruby/utils/reader.rb, line 67 def read_octet_string(length) read(length) end
read_uint(length, peek: false)
click to toggle source
# File lib/cryptoconditions_ruby/utils/reader.rb, line 31 def read_uint(length, peek: false) if length > MAX_INT_BYTES raise RangeError.new("Tried to read too large integer (requested: #{length}, max: #{MAX_INT_BYTES})") end ensure_available(length) value = buffer[cursor...(cursor + length)] self.cursor += length unless peek CryptoconditionsRuby::Utils::Bytes.new(value).to_i(16) end
read_uint16()
click to toggle source
# File lib/cryptoconditions_ruby/utils/reader.rb, line 124 def read_uint16 read_uint(2) end
read_uint32()
click to toggle source
# File lib/cryptoconditions_ruby/utils/reader.rb, line 128 def read_uint32 read_uint(4) end
read_uint64()
click to toggle source
# File lib/cryptoconditions_ruby/utils/reader.rb, line 132 def read_uint64 read_uint(8) end
read_uint8()
click to toggle source
# File lib/cryptoconditions_ruby/utils/reader.rb, line 120 def read_uint8 read_uint(1) end
read_var_octet_string()
click to toggle source
# File lib/cryptoconditions_ruby/utils/reader.rb, line 88 def read_var_octet_string length = read_length_prefix read(length) end
read_var_uint()
click to toggle source
# File lib/cryptoconditions_ruby/utils/reader.rb, line 49 def read_var_uint buffer = read_var_octet_string if buffer.length > MAX_INT_BYTES raise RangeError.new("UInt of length #{butter.length} too large to parse as integer(#{MAX_INT_BYTES})") end value = buffer[0...buffer.length] CryptoconditionsRuby::Utils::Bytes.new(value).to_i(16) end
restore()
click to toggle source
# File lib/cryptoconditions_ruby/utils/reader.rb, line 21 def restore self.cursor = bookmarks.pop end
skip(num_bytes)
click to toggle source
# File lib/cryptoconditions_ruby/utils/reader.rb, line 115 def skip(num_bytes) ensure_available(num_bytes) self.cursor += num_bytes end
skip_octet_string(length)
click to toggle source
# File lib/cryptoconditions_ruby/utils/reader.rb, line 75 def skip_octet_string(length) skip(length) end
skip_uint(length)
click to toggle source
# File lib/cryptoconditions_ruby/utils/reader.rb, line 45 def skip_uint(length) skip(length) end
skip_uint16()
click to toggle source
# File lib/cryptoconditions_ruby/utils/reader.rb, line 156 def skip_uint16 skip_uint(2) end
skip_uint32()
click to toggle source
# File lib/cryptoconditions_ruby/utils/reader.rb, line 160 def skip_uint32 skip_uint(4) end
skip_uint64()
click to toggle source
# File lib/cryptoconditions_ruby/utils/reader.rb, line 164 def skip_uint64 skip_uint(8) end
skip_uint8()
click to toggle source
# File lib/cryptoconditions_ruby/utils/reader.rb, line 152 def skip_uint8 skip_uint(1) end
skip_var_octet_string()
click to toggle source
# File lib/cryptoconditions_ruby/utils/reader.rb, line 98 def skip_var_octet_string length = read_length_prefix skip(length) end
skip_var_uint()
click to toggle source
# File lib/cryptoconditions_ruby/utils/reader.rb, line 63 def skip_var_uint skip_var_octet_string end