class PacketGen::Types::Int

Base integer class to handle binary integers @abstract @author Sylvain Daubert

Attributes

default[RW]

Integer default value @return [Integer]

endian[RW]

Integer endianness @return [:little,:big]

from_human[RW]

Integer value @return [Integer]

value[RW]

Integer value @return [Integer]

width[RW]

Integer size, in bytes @return [Integer]

Public Class Methods

new(value=nil, endian=nil, width=nil, default=0) click to toggle source

@param [Integer,nil] value @param [:little,:big,nil] endian @param [Integer,nil] width @param [Integer] default

# File lib/packetgen/types/int.rb, line 34
def initialize(value=nil, endian=nil, width=nil, default=0)
  @value = value
  @endian = endian
  @width = width
  @default = default
end

Public Instance Methods

format_inspect() click to toggle source

Format Int type when inspecting header or packet @return [String]

# File lib/packetgen/types/int.rb, line 88
def format_inspect
  format_str % [to_i.to_s, to_i]
end
read(value) click to toggle source

@abstract Read an Int from a binary string or an integer @param [Integer, to_s] value @return [self] @raise [ParseError] when reading #to_s objects with abstract Int class.

# File lib/packetgen/types/int.rb, line 46
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
sz() click to toggle source

Give size in bytes of self @return [Integer]

# File lib/packetgen/types/int.rb, line 82
def sz
  width
end
to_f() click to toggle source

Convert Int to Float @return [Float]

# File lib/packetgen/types/int.rb, line 76
def to_f
  to_i.to_f
end
to_human()
Alias for: to_i
to_i() click to toggle source

Convert Int to Integer @return [Integer]

# File lib/packetgen/types/int.rb, line 68
def to_i
  @value || @default
end
Also aliased as: to_human
to_s() click to toggle source

@abstract @return [::String] @raise [ParseError] This is an abstrat method and must be redefined

# File lib/packetgen/types/int.rb, line 60
def to_s
  raise ParseError, 'PacketGen::Types::Int#to_s is an abstract method' unless defined? @packstr

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

Private Instance Methods

format_str() click to toggle source
# File lib/packetgen/types/int.rb, line 94
def format_str
  "%-16s (0x%0#{width * 2}x)"
end