Class: PacketGen::Types::Int Abstract

Inherits:
Object
  • Object
show all
Includes:
Fieldable
Defined in:
lib/packetgen/types/int.rb

Overview

This class is abstract.

Base integer class to handle binary integers

Author:

  • Sylvain Daubert

Since:

  • 3.3.1 support native endianess

Direct Known Subclasses

Enum, Int16, Int24, Int32, Int64, Int8, SInt8

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Fieldable

#type_name

Constructor Details

#initialize(value = nil, endian = nil, width = nil, default = 0) ⇒ Int

Returns a new instance of Int.

Parameters:

  • value (Integer, nil) (defaults to: nil)
  • endian (:little, :big, nil) (defaults to: nil)
  • width (Integer, nil) (defaults to: nil)
  • default (Integer) (defaults to: 0)

Since:

  • 3.3.1 support native endianess



37
38
39
40
41
42
# File 'lib/packetgen/types/int.rb', line 37

def initialize(value=nil, endian=nil, width=nil, default=0)
  @value = value
  @endian = endian
  @width = width
  @default = default
end

Instance Attribute Details

#defaultInteger

Integer default value

Returns:

  • (Integer)

Since:

  • 3.3.1 support native endianess



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

def default
  @default
end

#endian:little, ...

Integer endianness

Returns:

  • (:little, :big, :native)

Since:

  • 3.3.1 add :native



25
26
27
# File 'lib/packetgen/types/int.rb', line 25

def endian
  @endian
end

#valueInteger

Integer value

Returns:

  • (Integer)

Since:

  • 3.3.1 support native endianess



21
22
23
# File 'lib/packetgen/types/int.rb', line 21

def value
  @value
end

#widthInteger

Integer size, in bytes

Returns:

  • (Integer)

Since:

  • 3.3.1 support native endianess



28
29
30
# File 'lib/packetgen/types/int.rb', line 28

def width
  @width
end

Instance Method Details

#format_inspectString

Format Int type when inspecting header or packet

Returns:

Since:

  • 3.3.1 support native endianess



91
92
93
# File 'lib/packetgen/types/int.rb', line 91

def format_inspect
  format_str % [to_i.to_s, to_i]
end

#nbitsInteger

Return the number of bits used to encode this Int

Returns:

  • (Integer)

Since:

  • 3.2.1



98
99
100
# File 'lib/packetgen/types/int.rb', line 98

def nbits
  width * 8
end

#read(value) ⇒ self

This method is abstract.

Read an Int from a binary string or an integer

Parameters:

  • value (Integer, #to_s)

Returns:

  • (self)

Raises:

  • (ParseError)

    when reading #to_s objects with abstract Int class.

Since:

  • 3.3.1 support native endianess



49
50
51
52
53
54
55
56
57
58
# File 'lib/packetgen/types/int.rb', line 49

def read(value)
  @value = if value.is_a?(Integer)
             value.to_i
           elsif defined? @packstr
             value.to_s.unpack1(@packstr[@endian])
           else
             raise ParseError, 'Int#read is abstract and cannot read'
           end
  self
end

#szInteger

Give size in bytes of self

Returns:

  • (Integer)

Since:

  • 3.3.1 support native endianess



85
86
87
# File 'lib/packetgen/types/int.rb', line 85

def sz
  width
end

#to_fFloat

Convert Int to Float

Returns:

  • (Float)

Since:

  • 3.3.1 support native endianess



79
80
81
# File 'lib/packetgen/types/int.rb', line 79

def to_f
  to_i.to_f
end

#to_iInteger Also known as: to_human

Convert Int to Integer

Returns:

  • (Integer)

Since:

  • 3.3.1 support native endianess



71
72
73
# File 'lib/packetgen/types/int.rb', line 71

def to_i
  @value || @default
end

#to_s::String

This method is abstract.

Returns:

  • (::String)

Raises:

  • (ParseError)

    This is an abstrat method and must be redefined

Since:

  • 3.3.1 support native endianess



63
64
65
66
67
# File 'lib/packetgen/types/int.rb', line 63

def to_s
  raise ParseError, 'PacketGen::Types::Int#to_s is an abstract method' unless defined? @packstr

  [to_i].pack(@packstr[@endian])
end