Moved core extensions to Net::BER. Documented.
This commit is contained in:
parent
e31af4bead
commit
c913bc6fb9
17 changed files with 310 additions and 248 deletions
51
lib/net/ber/core_ext/string.rb
Normal file
51
lib/net/ber/core_ext/string.rb
Normal file
|
@ -0,0 +1,51 @@
|
|||
require 'stringio'
|
||||
|
||||
module Net::BER::Extensions::String
|
||||
##
|
||||
# Converts a string to a BER string. Universal octet-strings are tagged
|
||||
# with 0x04, but other values are possible depending on the context, so we
|
||||
# let the caller give us one.
|
||||
#
|
||||
# User code should call either #to_ber_application_string or
|
||||
# #to_ber_contextspecific.
|
||||
def to_ber(code = 0x04)
|
||||
[code].pack('C') + length.to_ber_length_encoding + self
|
||||
end
|
||||
|
||||
##
|
||||
# Creates an application-specific BER string encoded value with the
|
||||
# provided syntax code value.
|
||||
def to_ber_application_string(code)
|
||||
to_ber(0x40 + code)
|
||||
end
|
||||
|
||||
##
|
||||
# Creates a context-specific BER string encoded value with the provided
|
||||
# syntax code value.
|
||||
def to_ber_contextspecific(code)
|
||||
to_ber(0x80 + code)
|
||||
end
|
||||
|
||||
##
|
||||
# Nondestructively reads a BER object from this string.
|
||||
def read_ber(syntax = nil)
|
||||
StringIO.new(self).read_ber(syntax)
|
||||
end
|
||||
|
||||
=begin
|
||||
# 20100319 AZ I've kept this here because I'm not yet sure if it's
|
||||
# necessary.
|
||||
|
||||
##
|
||||
# Destructively reads a BER object from this string.
|
||||
def read_ber!(syntax = nil)
|
||||
obj, consumed = read_ber_from_string(self, syntax)
|
||||
if consumed
|
||||
self.slice!(0...consumed)
|
||||
obj
|
||||
else
|
||||
nil
|
||||
end
|
||||
end
|
||||
=end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue