Class: ArrPM::V2::Lead
- Inherits:
-
Object
- Object
- ArrPM::V2::Lead
- Defined in:
- lib/arr-pm/v2/lead.rb
Constant Summary collapse
- LENGTH =
96
- MAGIC =
[ 0xed, 0xab, 0xee, 0xdb ]
- MAGIC_LENGTH =
MAGIC.count
- SIGNED_TYPE =
5
Instance Attribute Summary collapse
-
#architecture ⇒ Object
Returns the value of attribute architecture.
-
#major ⇒ Object
Returns the value of attribute major.
-
#minor ⇒ Object
Returns the value of attribute minor.
-
#name ⇒ Object
Returns the value of attribute name.
-
#os ⇒ Object
Returns the value of attribute os.
-
#reserved ⇒ Object
Returns the value of attribute reserved.
-
#signature_type ⇒ Object
Returns the value of attribute signature_type.
-
#type ⇒ Object
Returns the value of attribute type.
Class Method Summary collapse
- .parse_architecture(data) ⇒ Object
- .parse_magic(data) ⇒ Object
- .parse_name(data) ⇒ Object
- .parse_os(data) ⇒ Object
- .parse_reserved(data) ⇒ Object
- .parse_signature_type(data) ⇒ Object
- .parse_type(data) ⇒ Object
- .parse_version(data) ⇒ Object
- .valid_version?(version) ⇒ Boolean
- .validate_architecture(architecture) ⇒ Object
- .validate_magic(magic) ⇒ Object
- .validate_type(type) ⇒ Object
Instance Method Summary collapse
- #dump(io) ⇒ Object
- #load(io) ⇒ Object
- #parse(bytestring) ⇒ Object
- #serialize ⇒ Object
- #signature? ⇒ Boolean
- #validate ⇒ Object
Instance Attribute Details
#architecture ⇒ Object
Returns the value of attribute architecture.
14 15 16 |
# File 'lib/arr-pm/v2/lead.rb', line 14 def architecture @architecture end |
#major ⇒ Object
Returns the value of attribute major.
14 15 16 |
# File 'lib/arr-pm/v2/lead.rb', line 14 def major @major end |
#minor ⇒ Object
Returns the value of attribute minor.
14 15 16 |
# File 'lib/arr-pm/v2/lead.rb', line 14 def minor @minor end |
#name ⇒ Object
Returns the value of attribute name.
14 15 16 |
# File 'lib/arr-pm/v2/lead.rb', line 14 def name @name end |
#os ⇒ Object
Returns the value of attribute os.
14 15 16 |
# File 'lib/arr-pm/v2/lead.rb', line 14 def os @os end |
#reserved ⇒ Object
Returns the value of attribute reserved.
14 15 16 |
# File 'lib/arr-pm/v2/lead.rb', line 14 def reserved @reserved end |
#signature_type ⇒ Object
Returns the value of attribute signature_type.
14 15 16 |
# File 'lib/arr-pm/v2/lead.rb', line 14 def signature_type @signature_type end |
#type ⇒ Object
Returns the value of attribute type.
14 15 16 |
# File 'lib/arr-pm/v2/lead.rb', line 14 def type @type end |
Class Method Details
.parse_architecture(data) ⇒ Object
88 89 90 91 92 93 |
# 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) ⇒ Object
61 62 63 64 65 |
# 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) ⇒ Object
99 100 101 102 103 104 105 |
# 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) ⇒ Object
107 108 109 110 |
# 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) ⇒ Object
117 118 119 120 |
# 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) ⇒ Object
112 113 114 115 |
# 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) ⇒ Object
77 78 79 80 81 82 |
# 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) ⇒ Object
71 72 73 74 75 |
# 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) ⇒ Boolean
57 58 59 |
# File 'lib/arr-pm/v2/lead.rb', line 57 def self.valid_version?(version) version == 1 end |
.validate_architecture(architecture) ⇒ Object
95 96 97 |
# 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) ⇒ Object
67 68 69 |
# File 'lib/arr-pm/v2/lead.rb', line 67 def self.validate_magic(magic) raise ArrPM::V2::Error::InvalidMagicValue, magic unless magic == MAGIC end |
Instance Method Details
#dump(io) ⇒ Object
24 25 26 |
# File 'lib/arr-pm/v2/lead.rb', line 24 def dump(io) io.write(serialize) end |
#load(io) ⇒ Object
33 34 35 36 |
# File 'lib/arr-pm/v2/lead.rb', line 33 def load(io) data = io.read(LENGTH) parse(data) end |
#parse(bytestring) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# 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 ⇒ Object
28 29 30 31 |
# 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? ⇒ Boolean
53 54 55 |
# File 'lib/arr-pm/v2/lead.rb', line 53 def signature? @signature_type == SIGNED_TYPE end |
#validate ⇒ Object
16 17 18 19 20 21 22 |
# 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 |