Class: CryptoconditionsRuby::Utils::Predictor

Inherits:
Object
  • Object
show all
Defined in:
lib/cryptoconditions_ruby/utils/predictor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePredictor

Returns a new instance of Predictor.



4
5
6
# File 'lib/cryptoconditions_ruby/utils/predictor.rb', line 4

def initialize
  @size = 0
end

Instance Attribute Details

#sizeObject

Returns the value of attribute size.



2
3
4
# File 'lib/cryptoconditions_ruby/utils/predictor.rb', line 2

def size
  @size
end

Instance Method Details

#skip(byte_count) ⇒ Object



39
40
41
# File 'lib/cryptoconditions_ruby/utils/predictor.rb', line 39

def skip(byte_count)
  self.size += byte_count
end

#write(in_bytes) ⇒ Object



35
36
37
# File 'lib/cryptoconditions_ruby/utils/predictor.rb', line 35

def write(in_bytes)
  self.size += in_bytes.length
end

#write_octet_string(_value, length) ⇒ Object



22
23
24
# File 'lib/cryptoconditions_ruby/utils/predictor.rb', line 22

def write_octet_string(_value, length)
  skip(length)
end

#write_uint(_value, length) ⇒ Object



8
9
10
# File 'lib/cryptoconditions_ruby/utils/predictor.rb', line 8

def write_uint(_value, length)
  skip(length)
end

#write_uint16(value) ⇒ Object



47
48
49
# File 'lib/cryptoconditions_ruby/utils/predictor.rb', line 47

def write_uint16(value)
  self.write_uint(value, 2)
end

#write_uint32(value) ⇒ Object



51
52
53
# File 'lib/cryptoconditions_ruby/utils/predictor.rb', line 51

def write_uint32(value)
  self.write_uint(value, 4)
end

#write_uint64(value) ⇒ Object



55
56
57
# File 'lib/cryptoconditions_ruby/utils/predictor.rb', line 55

def write_uint64(value)
  self.write_uint(value, 8)
end

#write_uint8(value) ⇒ Object



43
44
45
# File 'lib/cryptoconditions_ruby/utils/predictor.rb', line 43

def write_uint8(value)
  self.write_uint(value, 1)
end

#write_var_octet_string(value) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/cryptoconditions_ruby/utils/predictor.rb', line 26

def write_var_octet_string(value)
  skip(1)
  if value.length > 127
    length_of_length = (sprintf('%02b', value.length).length / 8.0).ceil.to_i
    skip(length_of_length)
  end
  skip(value.length)
end

#write_var_uint(value) ⇒ Object

Raises:

  • (TypeError)


12
13
14
15
16
17
18
19
20
# File 'lib/cryptoconditions_ruby/utils/predictor.rb', line 12

def write_var_uint(value)
  return write_var_octet_string(value) if value.is_a?(String)
  raise TypeError.new('UInt must be an integer') unless value.is_a?(Integer)
  raise TypeError.new('UInt must be positive') unless value > 0

  length_of_value = (sprintf('%02b', value).length / 8.0).ceil.to_i
  buffer = (length_of_value - 1).times.map { 0 }.push(value).pack('C*')
  write_var_octet_string(buffer)
end