Class: BER::Function

Inherits:
Object
  • Object
show all
Defined in:
lib/ber/function.rb

Overview

Used within refinements to identify…

See Also:

Instance Method Summary collapse

Instance Method Details

#parse_ber_object(syntax, id, data) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/ber/function.rb', line 11

def parse_ber_object(syntax, id, data)
  object_type = (syntax && syntax[id]) || IDENTIFIED[id]

  case object_type
  when :string
    s = BerIdentifiedString.new(data || EMPTY_STRING)
    s.ber_identifier = id
    s

  when :integer
    neg = !(data.unpack1('C') & 0x80).zero?
    int = 0

    data.each_byte do |b|
      int = (int << 8) + (neg ? 255 - b : b)
    end

    if neg
      (int + 1) * -1
    else
      int
    end

  when :oid
    oid = data.unpack('w*')
    f   = oid.shift
    g   = if f < 40
            [0, f]
          elsif f < 80
            [1, f - 40]
          else
            [2, f - 80]
          end
    oid.unshift g.last
    oid.unshift g.first
    BerIdentifiedOid.new(oid)

  when :array
    seq = BerIdentifiedArray.new
    seq.ber_identifier = id
    sio = StringIO.new(data || EMPTY_STRING)

    until (e = read_ber(sio, syntax)).nil?
      seq << e
    end
    seq

  when :boolean
    data != "\000"

  when :null
    n = BerIdentifiedNull.new
    n.ber_identifier = id
    n

  else
    raise Error, "Unsupported object type: id=#{id}"
  end
end

#read_ber(object, syntax) {|id, content_length| ... } ⇒ Object

Yields:

  • (id, content_length)

Raises:



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/ber/function.rb', line 90

def read_ber(object, syntax)
  return unless (id = object.getbyte)

  content_length = read_ber_length(object)

  yield id, content_length if block_given?

  raise Error, 'Indeterminite BER content length not implemented.' if content_length == -1

  data = object.read(content_length)

  parse_ber_object(syntax, id, data)
end

#read_ber_length(object) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/ber/function.rb', line 71

def read_ber_length(object)
  n = object.getbyte

  if n <= 0x7f
    n
  elsif n == 0x80
    -1
  elsif n == 0xff
    raise Error, 'Invalid BER length 0xFF detected.'
  else
    v = 0
    object.read(n & 0x7f).each_byte do |b|
      v = (v << 8) + b
    end

    v
  end
end