Module: PacketGen::Inspect Private

Defined in:
lib/packetgen/inspect.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Inspect module provides methods to help writing inspect

Constant Summary collapse

MAX_WIDTH =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Maximum number of characters on a line for INSPECT

70
FMT_ATTR =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Format to inspect attribute

"%12s %12s: %s\n"

Class Method Summary collapse

Class Method Details

.dashed_line(name, level = 1) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create a dashed line with obj class writing in it

Parameters:

  • name (String)
  • level (Integer) (defaults to: 1)

Returns:

  • (String)


18
19
20
21
# File 'lib/packetgen/inspect.rb', line 18

def self.dashed_line(name, level=1)
  str = '--' * level << " #{name} "
  str << '-' * (MAX_WIDTH - str.length) << "\n"
end

.inspect_asn1_attribute(name, attr, level = 1) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Format a ASN.1 attribute for #inspect. 3 cases are handled:

  • attribute value is a Types::Int: show value as integer and in hexdecimal format,

  • attribute value responds to #to_human: call it,

  • else, #to_s is used to format attribute value.

Parameters:

  • name (Symbol)

    attribute name

  • attr (RASN1::Types::Base, RASN1::Model)

    attribute

  • level (Integer) (defaults to: 1)

Returns:

  • (String)


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/packetgen/inspect.rb', line 67

def self.inspect_asn1_attribute(name, attr, level=1)
  str = shift_level(level)
  val = case attr
        when RASN1::Types::Enumerated
          hexsize = attr.value_size * 2
          "%-10s (0x%0#{hexsize}x)" % [attr.value, attr.to_i]
        when RASN1::Types::Integer
          int_dec_hex(attr.value, attr.value_size * 2)
        when RASN1::Model
          attr.root.type
        else
          attr.value.to_s.inspect
        end
  str << FMT_ATTR % [attr.type, name, val]
end

.inspect_attribute(attr, value, level = 1) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Format an attribute for #inspect. 3 cases are handled:

  • attribute value is a Types::Int: show value as integer and in hexdecimal format,

  • attribute value responds to #to_human: call it,

  • else, #to_s is used to format attribute value.

Parameters:

  • attr (Symbol)

    attribute name

  • value (Object)

    attribute value

  • level (Integer) (defaults to: 1)

Returns:

  • (String)


45
46
47
48
49
50
51
52
53
54
55
# File 'lib/packetgen/inspect.rb', line 45

def self.inspect_attribute(attr, value, level=1)
  str = shift_level(level)
  val = if value.is_a?(Types::Int) or value.is_a?(Integer)
          int_dec_hex(value, value.to_s.size * 2)
        elsif value.respond_to? :to_human
          value.to_human
        else
          value.to_s.inspect
        end
  str << FMT_ATTR % [value.class.to_s.sub(/.*::/, ''), attr, val]
end

.inspect_body(body, name = 'Body') ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • body (#to_s)

Returns:

  • (String)


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/packetgen/inspect.rb', line 85

def self.inspect_body(body, name='Body')
  return '' if body.nil? or body.empty?
  str = dashed_line(name, 2)
  str << (0..15).to_a.map { |v| " %02d" % v}.join << "\n"
  str << '-' * MAX_WIDTH << "\n"
  if body.size > 0
    (body.size / 16 + 1).times do |i|
      octets = body.to_s[i*16, 16].unpack('C*')
      o_str = octets.map { |v| " %02x" % v}.join
      str << o_str
      str << ' ' * (3*16 - o_str.size) unless o_str.size >= 3*16
      str << '  ' << octets.map { |v| v < 128 && v > 13 ? v.chr : '.' }.join
      str << "\n"
    end
  end
  str << '-' * MAX_WIDTH << "\n"
end

.int_dec_hex(value, hexsize) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • value (#to_i)
  • hexsize (Integer)

Returns:

  • (String)


31
32
33
# File 'lib/packetgen/inspect.rb', line 31

def self.int_dec_hex(value, hexsize)
  "%-10s (0x%0#{hexsize}x)" % [value.to_i, value.to_i]
end

.shift_level(level = 1) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (String)


24
25
26
# File 'lib/packetgen/inspect.rb', line 24

def self.shift_level(level=1)
  '  ' + '  ' * level
end