Class: CryptoconditionsRuby::Utils::Writer

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

Direct Known Subclasses

Hasher

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeWriter

Returns a new instance of Writer.



5
6
7
# File 'lib/cryptoconditions_ruby/utils/writer.rb', line 5

def initialize
  @components = []
end

Instance Attribute Details

#componentsObject

Returns the value of attribute components.



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

def components
  @components
end

Instance Method Details

#bufferObject



54
55
56
# File 'lib/cryptoconditions_ruby/utils/writer.rb', line 54

def buffer
  components.join
end

#write(in_bytes) ⇒ Object



50
51
52
# File 'lib/cryptoconditions_ruby/utils/writer.rb', line 50

def write(in_bytes)
  components.push(in_bytes)
end

#write_octet_string(_buffer, length) ⇒ Object

Raises:

  • (TypeError)


31
32
33
34
35
36
# File 'lib/cryptoconditions_ruby/utils/writer.rb', line 31

def write_octet_string(_buffer, length)
  raise TypeError, '_buffer must be an array of bytes' unless _buffer.respond_to?(:bytes)
  raise ArgumentError, "Incorrect length for octet string (actual: #{_buffer.length}, expected: #{length})" unless _buffer.length == length

  write(_buffer)
end

#write_uint(value, length) ⇒ Object

Raises:

  • (TypeError)


9
10
11
12
13
14
15
16
# File 'lib/cryptoconditions_ruby/utils/writer.rb', line 9

def write_uint(value, length)
  raise TypeError.new('UInt must be an integer') unless value.is_a?(Integer)
  raise TypeError.new('UInt must be positive') unless value >= 0
  raise TypeError.new("UInt '#{value}' does not fit in '#{length}' bytes") if sprintf('%02b', value).length > length * 8

  _buffer = (length - 1).times.map { 0 }.push(value).pack('C*')
  write(_buffer)
end

#write_uint16(value) ⇒ Object



62
63
64
# File 'lib/cryptoconditions_ruby/utils/writer.rb', line 62

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

#write_uint32(value) ⇒ Object



66
67
68
# File 'lib/cryptoconditions_ruby/utils/writer.rb', line 66

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

#write_uint64(value) ⇒ Object



70
71
72
# File 'lib/cryptoconditions_ruby/utils/writer.rb', line 70

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

#write_uint8(value) ⇒ Object



58
59
60
# File 'lib/cryptoconditions_ruby/utils/writer.rb', line 58

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

#write_var_octet_string(_buffer) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/cryptoconditions_ruby/utils/writer.rb', line 38

def write_var_octet_string(_buffer)
  msb = 0x80
  if _buffer.length <= 127
    write_uint8(_buffer.length)
  else
    length_of_length = (format('%02b', _buffer.length).length / 8.0).ceil.to_i
    write_uint8(msb | length_of_length)
    write_uint(_buffer.length, length_of_length)
  end
  write(_buffer)
end

#write_var_uint(value) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/cryptoconditions_ruby/utils/writer.rb', line 18

def write_var_uint(value)
  if value.is_a?(String)
    write_var_octet_string(value)
  else
    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
end