class ArrPM::V2::Lead

Constants

LENGTH
MAGIC
MAGIC_LENGTH
SIGNED_TYPE

Attributes

architecture[RW]
major[RW]
minor[RW]
name[RW]
os[RW]
reserved[RW]
signature_type[RW]
type[RW]

Public Class Methods

parse_architecture(data) click to toggle source
# File lib/arr-pm/v2/lead.rb, line 88
def self.parse_architecture(data)
  offset = MAGIC_LENGTH + 4
  architecture = data[offset, 2].pack("C*").unpack("n").first
  validate_architecture(architecture)
  architecture
end
parse_magic(data) click to toggle source
# File lib/arr-pm/v2/lead.rb, line 61
def self.parse_magic(data)
  magic = data[0, MAGIC_LENGTH]
  validate_magic(magic)
  magic
end
parse_name(data) click to toggle source
# File lib/arr-pm/v2/lead.rb, line 99
def self.parse_name(data)
  offset = MAGIC_LENGTH + 6
  name = data[offset, 66]
  length = name.find_index(0) # find the first null byte
  raise ArrPM::V2::Error::InvalidName unless length
  return name[0, length].pack("C*")
end
parse_os(data) click to toggle source
# File lib/arr-pm/v2/lead.rb, line 107
def self.parse_os(data)
  offset = MAGIC_LENGTH + 72
  data[offset, 2].pack("C*").unpack("n").first
end
parse_reserved(data) click to toggle source
# File lib/arr-pm/v2/lead.rb, line 117
def self.parse_reserved(data)
  offset = MAGIC_LENGTH + 76
  data[offset, 16]
end
parse_signature_type(data) click to toggle source
# File lib/arr-pm/v2/lead.rb, line 112
def self.parse_signature_type(data)
  offset = MAGIC_LENGTH + 74
  data[offset, 2].pack("C*").unpack("n").first
end
parse_type(data) click to toggle source
# File lib/arr-pm/v2/lead.rb, line 77
def self.parse_type(data)
  offset = MAGIC_LENGTH + 2
  type = data[offset, 2].pack("CC").unpack("n").first
  validate_type(type)
  type
end
parse_version(data) click to toggle source
# File lib/arr-pm/v2/lead.rb, line 71
def self.parse_version(data)
  offset = MAGIC_LENGTH
  major, minor = data[offset, 2]
  return major, minor
end
valid_version?(version) click to toggle source
# File lib/arr-pm/v2/lead.rb, line 57
def self.valid_version?(version)
  version == 1
end
validate_architecture(architecture) click to toggle source
# File lib/arr-pm/v2/lead.rb, line 95
def self.validate_architecture(architecture)
  raise ArrPM::V2::Error::InvalidArchitecture unless ArrPM::V2::Architecture.valid?(architecture)
end
validate_magic(magic) click to toggle source
# File lib/arr-pm/v2/lead.rb, line 67
def self.validate_magic(magic)
  raise ArrPM::V2::Error::InvalidMagicValue, magic unless magic == MAGIC
end
validate_type(type) click to toggle source
# File lib/arr-pm/v2/lead.rb, line 84
def self.validate_type(type)
  raise ArrPM::V2::Error::InvalidType, type unless ArrPM::V2::Type.valid?(type)
end

Public Instance Methods

dump(io) click to toggle source
# File lib/arr-pm/v2/lead.rb, line 24
def dump(io)
  io.write(serialize)
end
load(io) click to toggle source
# File lib/arr-pm/v2/lead.rb, line 33
def load(io)
  data = io.read(LENGTH)
  parse(data)
end
parse(bytestring) click to toggle source
# File lib/arr-pm/v2/lead.rb, line 38
def parse(bytestring)
  raise ArrPM::V2::Error::EmptyFile if bytestring.nil?
  data = bytestring.bytes

  @magic = self.class.parse_magic(data)
  @major, @minor = self.class.parse_version(data)
  @type = self.class.parse_type(data)
  @architecture = self.class.parse_architecture(data)
  @name = self.class.parse_name(data)
  @os = self.class.parse_os(data)
  @signature_type = self.class.parse_signature_type(data)
  @reserved = self.class.parse_reserved(data)
  self
end
serialize() click to toggle source
# File lib/arr-pm/v2/lead.rb, line 28
def serialize
  validate
  [ *MAGIC, major, minor, type, architecture, name, os, signature_type, *reserved ].pack("C4CCnnZ66nnC16")
end
signature?() click to toggle source
# File lib/arr-pm/v2/lead.rb, line 53
def signature?
  @signature_type == SIGNED_TYPE
end
validate() click to toggle source
# File lib/arr-pm/v2/lead.rb, line 16
def validate
  self.class.validate_type(type)
  self.class.validate_architecture(architecture)
  if name.length > 65
    raise ArrPM::V2::Error::InvalidName, "Name is longer than 65 chracters. This is invalid."
  end
end