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


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

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)

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

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)

46
47
48
# File 'lib/packetgen/inspect.rb', line 46

def self.enum_human_hex(str, int, hexsize)
  "%-16s (0x%0#{hexsize}x)" % [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)

56
57
58
59
# File 'lib/packetgen/inspect.rb', line 56

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

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

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)

71
72
73
74
# File 'lib/packetgen/inspect.rb', line 71

def self.inspect_attribute(attr, value, level=1)
  type = value.class.to_s.sub(/.*::/, '')
  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)

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

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)

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

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)

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

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