Class: OpenPGP::Buffer

Inherits:
StringIO
  • Object
show all
Defined in:
lib/openpgp/buffer.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ Buffer

Returns a new instance of Buffer.



11
12
13
14
# File 'lib/openpgp/buffer.rb', line 11

def initialize(*args, &block)
  super
  block.call(self) if block_given?
end

Class Method Details

.write(*args, &block) ⇒ Object



6
7
8
9
# File 'lib/openpgp/buffer.rb', line 6

def self.write(*args, &block)
  buffer = self.new(*args, &block)
  buffer.string
end

Instance Method Details

#read_byteObject



92
93
94
# File 'lib/openpgp/buffer.rb', line 92

def read_byte
  getc
end

#read_bytes(count) ⇒ Object



84
85
86
# File 'lib/openpgp/buffer.rb', line 84

def read_bytes(count)
  read(count)
end

#read_mpiObject



58
59
60
61
62
# File 'lib/openpgp/buffer.rb', line 58

def read_mpi
  length = read_unpacked(2, 'n')      # length in bits
  length = ((length + 7) / 8.0).floor # length in bytes
  read_bytes(length)
end

#read_number(count, base = nil) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/openpgp/buffer.rb', line 42

def read_number(count, base = nil)
  number, shift = 0, count * 8
  read_bytes(count).each_byte do |octet|
    number += octet << (shift -= 8)
  end
  !base ? number : number.to_s(base).upcase
end

#read_s2kObject



72
# File 'lib/openpgp/buffer.rb', line 72

def read_s2k()     S2K.parse(self) end

#read_stringObject



17
18
19
# File 'lib/openpgp/buffer.rb', line 17

def read_string
  read_bytes(length = read_byte)
end

#read_timestampObject



30
31
32
# File 'lib/openpgp/buffer.rb', line 30

def read_timestamp
  read_unpacked(4, 'N')
end

#read_unpacked(count, format) ⇒ Object



76
77
78
# File 'lib/openpgp/buffer.rb', line 76

def read_unpacked(count, format)
  read_bytes(count).unpack(format).first
end

#write_byte(value) ⇒ Object



96
97
98
# File 'lib/openpgp/buffer.rb', line 96

def write_byte(value)
  self << (value.respond_to?(:chr) ? value : value.to_s[0]).chr
end

#write_bytes(value) ⇒ Object



88
89
90
# File 'lib/openpgp/buffer.rb', line 88

def write_bytes(value)
  self << value
end

#write_mpiObject



66
67
68
# File 'lib/openpgp/buffer.rb', line 66

def write_mpi
  # TODO
end

#write_numberObject



52
53
54
# File 'lib/openpgp/buffer.rb', line 52

def write_number
  # TODO
end

#write_s2k(s2k) ⇒ Object



74
# File 'lib/openpgp/buffer.rb', line 74

def write_s2k(s2k) s2k.write(self) end

#write_string(value) ⇒ Object



22
23
24
25
26
# File 'lib/openpgp/buffer.rb', line 22

def write_string(value)
  value = value.to_s
  self << [value.size].pack('C')
  self << value unless value.empty?
end

#write_timestamp(value) ⇒ Object



36
37
38
# File 'lib/openpgp/buffer.rb', line 36

def write_timestamp(value)
  self << [value.to_i].pack('N')
end

#write_unpackedObject



80
81
82
# File 'lib/openpgp/buffer.rb', line 80

def write_unpacked
  # TODO
end