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
SEPARATOR =

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.

('-' * MAX_WIDTH << "\n").freeze
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

.convert_body_slice(bslice) ⇒ Object

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.



120
121
122
123
124
125
126
# File 'lib/packetgen/inspect.rb', line 120

def self.convert_body_slice(bslice)
  octets = bslice.unpack('C*')
  str = octets.map { |v| ' %02x' % v }.join
  str << ' ' * (48 - str.size) unless str.size >= 48
  str << '  ' << octets.map { |v| (32..127).cover?(v) ? v.chr : '.' }.join
  str << "\n"
end

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


26
27
28
29
# File 'lib/packetgen/inspect.rb', line 26

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

.enum_human_hex(str, int, 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:

  • str (String)
  • int (Integer)
  • hexsize (Integer)

Returns:

  • (String)


48
49
50
51
# File 'lib/packetgen/inspect.rb', line 48

def self.enum_human_hex(str, int, hexsize)
  fmt = "%-16s (0x%0#{hexsize}x)"
  fmt % [str, int]
end

.format(type, 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.

Simple formatter to inspect an attribute

Parameters:

  • type (String)

    attribute type

  • attr (String)

    attribute name

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

Returns:

  • (String)


59
60
61
62
# File 'lib/packetgen/inspect.rb', line 59

def self.format(type, attr, value, level=1)
  str = Inspect.shift_level(level)
  str << Inspect::FMT_ATTR % [type, attr, value]
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::BinStruct::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)


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

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
          fmt = "%-16s (0x%0#{hexsize}x)"
          fmt % [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. Call +#format_inspect} on value.

Parameters:

  • attr (Symbol)

    attribute name

  • value (Object)

    attribute value

  • level (Integer) (defaults to: 1)

Returns:

  • (String)


69
70
71
72
# File 'lib/packetgen/inspect.rb', line 69

def self.inspect_attribute(attr, value, level=1)
  type = value.type_name
  self.format(type, attr, value.format_inspect, level)
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)


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

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

  str = dashed_line(name, 2)
  0.upto(15) { |v| str << ' %02d' % v }
  str << "\n" << SEPARATOR
  unless body.empty?
    (body.size / 16 + 1).times do |i|
      str << self.convert_body_slice(body.to_s[i * 16, 16])
    end
  end
  str << SEPARATOR
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)


39
40
41
42
# File 'lib/packetgen/inspect.rb', line 39

def self.int_dec_hex(value, hexsize)
  fmt = "%-16s (0x%0#{hexsize}x)"
  fmt % [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)


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

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