Class: MrbParser::Header

Inherits:
Object
  • Object
show all
Defined in:
lib/mrb_parser/header.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHeader

Returns a new instance of Header.



13
14
# File 'lib/mrb_parser/header.rb', line 13

def initialize
end

Instance Attribute Details

#compiler_nameObject (readonly)

Returns the value of attribute compiler_name.



5
6
7
# File 'lib/mrb_parser/header.rb', line 5

def compiler_name
  @compiler_name
end

#compiler_versionObject (readonly)

Returns the value of attribute compiler_version.



5
6
7
# File 'lib/mrb_parser/header.rb', line 5

def compiler_version
  @compiler_version
end

#crcObject (readonly)

Returns the value of attribute crc.



5
6
7
# File 'lib/mrb_parser/header.rb', line 5

def crc
  @crc
end

#signatureObject (readonly)

Returns the value of attribute signature.



5
6
7
# File 'lib/mrb_parser/header.rb', line 5

def signature
  @signature
end

#sizeObject (readonly)

Returns the value of attribute size.



5
6
7
# File 'lib/mrb_parser/header.rb', line 5

def size
  @size
end

#versionObject (readonly)

Returns the value of attribute version.



5
6
7
# File 'lib/mrb_parser/header.rb', line 5

def version
  @version
end

Class Method Details

.parse(parser) ⇒ Object



8
9
10
11
# File 'lib/mrb_parser/header.rb', line 8

def self.parse(parser)
  header = Header.new()
  header.parse(parser)
end

Instance Method Details

#check_crc(parser) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/mrb_parser/header.rb', line 34

def check_crc(parser)
  pos = parser.pos
  rest_data = parser.read(nil)
  parser.seek(pos)
  checksum = MrbParser::CRC.calc_crc_16_ccitt(rest_data, rest_data.size, 0)
  @crc == checksum
end

#dumpObject



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/mrb_parser/header.rb', line 47

def dump
  printf "*** BINARY HEADER ***\n"
  printf "secID: %s\n", @signature
  printf "ver  : %s\n", @version
  printf "crc  : 0x%04x (%s)\n", @crc, @crc_verified
  printf "size : 0x%08x\n", @size
  printf "compiler:\n"
  printf "  name: %s\n", @compiler_name
  printf "  ver : %s\n", @compiler_version
  printf "*** ***\n"
end

#parse(parser) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/mrb_parser/header.rb', line 16

def parse(parser)
  @signature = parser.read_chars(4)
  @version = parser.read_chars(4)
  @crc = parser.read_uint16
  @crc_verified = check_crc(parser)
  @size = parser.read_uint32
  @compiler_name = parser.read_chars(4)
  @compiler_version = parser.read_chars(4)

  if parser.verbose
    if !valid?
      STDERR.print "** [WARN] This header seems to be invalid. **\n"
    end
  end

  self
end

#valid?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/mrb_parser/header.rb', line 43

def valid?
  @signature == "RITE" && @version == "0002" && @crc_verified
end