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

Author:

  • Sylvain Daubert

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

"%14s %16s: %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)


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

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. 4 cases are handled:

  • attribute value is a =RANS1::Types::Enumerated+: show named value and its integer value as hexdecimal format,

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

  • attribute value is a RASN1::Model: only show its root type,

  • 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)


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

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
          "%-16s (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)


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/packetgen/inspect.rb', line 50

def self.inspect_attribute(attr, value, level=1)
  str = shift_level(level)
  val = case value
        when Types::Enum
          "%-16s (0x%0#{value.sz * 2}x)" % [value.to_human, value.to_i]
        when Types::Int
          int_dec_hex(value, value.sz * 2)
        when Integer
          int_dec_hex(value, value.sz * 2)
        when String
          value.to_s.inspect
        else
          if value.respond_to? :to_human
            value.to_human
          else
            value.to_s.inspect
          end
        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)


101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/packetgen/inspect.rb', line 101

def self.inspect_body(body, name='Body')
  return '' if body.nil? || body.empty?

  str = dashed_line(name, 2)
  str << (0..15).to_a.map { |v| ' %02d' % v }.join << "\n"
  str << '-' * MAX_WIDTH << "\n"
  unless body.empty?
    (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 > 31 ? 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)


36
37
38
# File 'lib/packetgen/inspect.rb', line 36

def self.int_dec_hex(value, hexsize)
  "%-16s (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)


29
30
31
# File 'lib/packetgen/inspect.rb', line 29

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