intel_hex 0.5.1 → 0.5.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -2
- data/bin/intel_hex_reader +13 -15
- data/lib/intel_hex/file_reader.rb +6 -4
- data/lib/intel_hex/record.rb +83 -63
- data/lib/intel_hex/version.rb +3 -1
- data/lib/intel_hex.rb +3 -1
- metadata +35 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5f4dfc549e66964b205d64f063f38c1dc091e8bfc92755076c9f267d72f50422
|
4
|
+
data.tar.gz: 87e987fd8decf8520c5648ea524d0e79da09c0950a8713c887e8afc66ece629c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ec5dd2ac67f7387f4602f2806633f0a26e717ed9a27b35f0ac01baee8a70856a485cbd63fb6a597841b0c29c14653d50daf8c1bb5a225bbd03d2106b73b65317
|
7
|
+
data.tar.gz: cfdace7c69d4f52cea5728b9d38851c6f0da2dc8f6e6e0669d05f99829296c17668e6fb0298d69b38673d5197c2c23a2205500e405a94cdce30b27b9efc2d819
|
data/README.md
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
# An Intel hex file parser, for Ruby
|
2
2
|
|
3
|
-
|
3
|
+
[![rspec test status](https://github.com/jeremycole/intel_hex/actions/workflows/rspec.yml/badge.svg)](https://github.com/jeremycole/intel_hex/actions/workflows/rspec.yml)
|
4
4
|
|
5
|
-
[
|
5
|
+
See the [Wikipedia page on Intel HEX](https://en.wikipedia.org/wiki/Intel_HEX).
|
data/bin/intel_hex_reader
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
2
3
|
|
3
4
|
require 'optparse'
|
4
5
|
require 'intel_hex'
|
@@ -9,30 +10,27 @@ require 'pp'
|
|
9
10
|
}
|
10
11
|
|
11
12
|
OptionParser.new do |opts|
|
12
|
-
opts.on('--help', 'Print this help.')
|
13
|
-
|
13
|
+
opts.on('--help', 'Print this help.') do
|
14
|
+
puts
|
15
|
+
puts opts
|
16
|
+
puts
|
17
|
+
exit
|
18
|
+
end
|
19
|
+
opts.on('-f', '--filename=FILE', 'Load the specified file.') do |o|
|
20
|
+
@options[:file] = o
|
21
|
+
end
|
14
22
|
end.parse!
|
15
23
|
|
16
|
-
raise
|
24
|
+
raise 'No file specified' unless @options[:file]
|
17
25
|
|
18
26
|
intel_hex_file = IntelHex::FileReader.new(@options[:file])
|
19
27
|
|
20
28
|
intel_hex_file.each_record do |record|
|
21
|
-
|
22
|
-
when :data
|
23
|
-
record_data = "data=" + record.data.map { |b| "%02x" % b }.join(" ")
|
24
|
-
when :eof
|
25
|
-
record_data = ""
|
26
|
-
else
|
27
|
-
record_data = "value=" + record.send(data.type)
|
28
|
-
end
|
29
|
-
|
30
|
-
puts "Record type=%-4s offset=%-4d length=%-4d checksum=%-4d %s" % [
|
29
|
+
puts 'Record type=%-4s offset=%-4d length=%-4d checksum=%-4d %s' % [
|
31
30
|
record.type,
|
32
31
|
record.offset,
|
33
32
|
record.length,
|
34
33
|
record.checksum,
|
35
|
-
|
34
|
+
record.value_s,
|
36
35
|
]
|
37
36
|
end
|
38
|
-
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module IntelHex
|
2
4
|
class FileReader
|
3
5
|
def initialize(filename)
|
@@ -12,7 +14,7 @@ module IntelHex
|
|
12
14
|
|
13
15
|
def each_record
|
14
16
|
return to_enum(:each_record) unless block_given?
|
15
|
-
|
17
|
+
|
16
18
|
file = File.open(@filename, 'r')
|
17
19
|
|
18
20
|
begin
|
@@ -25,10 +27,10 @@ module IntelHex
|
|
25
27
|
|
26
28
|
nil
|
27
29
|
end
|
28
|
-
|
30
|
+
|
29
31
|
def each_byte_with_address
|
30
32
|
return to_enum(:each_byte_with_address) unless block_given?
|
31
|
-
|
33
|
+
|
32
34
|
each_record do |record|
|
33
35
|
case record.type
|
34
36
|
when :data
|
@@ -49,4 +51,4 @@ module IntelHex
|
|
49
51
|
nil
|
50
52
|
end
|
51
53
|
end
|
52
|
-
end
|
54
|
+
end
|
data/lib/intel_hex/record.rb
CHANGED
@@ -1,42 +1,51 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module IntelHex
|
2
4
|
class MisformattedFileError < RuntimeError; end
|
3
|
-
|
4
|
-
class
|
5
|
-
|
6
|
-
class
|
7
|
-
|
5
|
+
|
6
|
+
class ValidationError < StandardError; end
|
7
|
+
|
8
|
+
class InvalidTypeError < ValidationError; end
|
9
|
+
|
10
|
+
class InvalidLengthError < ValidationError; end
|
11
|
+
|
12
|
+
class InvalidOffsetError < ValidationError; end
|
13
|
+
|
14
|
+
class InvalidDataError < ValidationError; end
|
15
|
+
|
16
|
+
class InvalidChecksumError < ValidationError; end
|
8
17
|
|
9
18
|
class Record
|
10
|
-
TYPES = [
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
]
|
18
|
-
|
19
|
-
TYPE_MAP = TYPES.each_with_index.
|
20
|
-
|
19
|
+
TYPES = %i[
|
20
|
+
data
|
21
|
+
eof
|
22
|
+
esa
|
23
|
+
ssa
|
24
|
+
ela
|
25
|
+
sla
|
26
|
+
].freeze
|
27
|
+
|
28
|
+
TYPE_MAP = TYPES.each_with_index.each_with_object({}) { |(k, i), h| h[k] = i }
|
29
|
+
|
21
30
|
TYPE_DATA_LENGTH = {
|
22
31
|
data: (1..255),
|
23
32
|
eof: 0,
|
24
33
|
esa: 2,
|
25
|
-
ssa:
|
34
|
+
ssa: 4,
|
26
35
|
ela: 2,
|
27
36
|
sla: 4,
|
28
|
-
}
|
37
|
+
}.freeze
|
29
38
|
|
30
39
|
def self.parse(line)
|
31
|
-
raise MisformattedFileError
|
32
|
-
raise MisformattedFileError
|
40
|
+
raise MisformattedFileError, 'Expected \':\' at start of line' unless line[0] == ':'
|
41
|
+
raise MisformattedFileError, 'Line length incorrect' unless line.size >= (1 + 2 + 4 + 2)
|
33
42
|
|
34
43
|
length = line[1..2].to_i(16)
|
35
|
-
data_end = (9 + length*2)
|
44
|
+
data_end = (9 + length * 2)
|
36
45
|
|
37
46
|
offset = line[3..6].to_i(16)
|
38
47
|
type = TYPES[line[7..8].to_i(16)]
|
39
|
-
data = line[9...data_end].
|
48
|
+
data = line[9...data_end].chars.each_slice(2).map { |a| a.join.to_i(16) }
|
40
49
|
checksum = line[data_end..(data_end + 2)].to_i(16)
|
41
50
|
|
42
51
|
Record.new(type, length, offset, data, checksum, line: line, validate: true)
|
@@ -54,7 +63,7 @@ module IntelHex
|
|
54
63
|
|
55
64
|
def self.type_with_value(type, value)
|
56
65
|
record = Record.new(type)
|
57
|
-
record.send(
|
66
|
+
record.send("#{type}=".to_sym, value)
|
58
67
|
record
|
59
68
|
end
|
60
69
|
|
@@ -74,14 +83,10 @@ module IntelHex
|
|
74
83
|
type_with_value(:sla, value)
|
75
84
|
end
|
76
85
|
|
77
|
-
attr_reader :length
|
78
|
-
attr_reader :offset
|
79
|
-
attr_reader :type
|
80
|
-
attr_reader :data
|
81
|
-
attr_reader :checksum
|
82
|
-
attr_reader :line
|
86
|
+
attr_reader :length, :offset, :type, :data, :checksum, :line
|
83
87
|
|
84
|
-
|
88
|
+
# rubocop:disable Metrics/ParameterLists
|
89
|
+
def initialize(type, length = 0, offset = 0, data = [], checksum = nil, line: nil, validate: false)
|
85
90
|
@type = type
|
86
91
|
@length = length
|
87
92
|
@offset = offset
|
@@ -92,17 +97,28 @@ module IntelHex
|
|
92
97
|
|
93
98
|
self.validate if validate
|
94
99
|
end
|
100
|
+
# rubocop:enable Metrics/ParameterLists
|
101
|
+
|
102
|
+
def data_s
|
103
|
+
"[#{data.map { |b| '%02x' % b }.join(' ')}]"
|
104
|
+
end
|
105
|
+
|
106
|
+
def value_s
|
107
|
+
return '' if type == :eof
|
108
|
+
|
109
|
+
"#{type}=#{type == :data ? data_s : send(type)}"
|
110
|
+
end
|
95
111
|
|
96
112
|
def to_s
|
97
|
-
"#<#{self.class.name} type=#{type} offset=#{offset} length=#{length}>"
|
113
|
+
"#<#{self.class.name} type=#{type} offset=#{offset} length=#{length} #{value_s}>"
|
98
114
|
end
|
99
115
|
|
100
116
|
def to_ascii
|
101
|
-
|
117
|
+
':%02X%04X%02X%s%02X' % [
|
102
118
|
length,
|
103
119
|
offset,
|
104
120
|
TYPE_MAP[type],
|
105
|
-
data.map { |b|
|
121
|
+
data.map { |b| '%02X' % b }.join,
|
106
122
|
checksum,
|
107
123
|
]
|
108
124
|
end
|
@@ -111,7 +127,7 @@ module IntelHex
|
|
111
127
|
return @calculated_checksum if @calculated_checksum
|
112
128
|
|
113
129
|
sum = length + ((offset & 0xff00) >> 8) + (offset & 0x00ff) + TYPE_MAP[type]
|
114
|
-
sum += data.
|
130
|
+
sum += data.sum
|
115
131
|
|
116
132
|
@calculated_checksum = (((sum & 0xff) ^ 0xff) + 1) & 0xff
|
117
133
|
end
|
@@ -122,12 +138,10 @@ module IntelHex
|
|
122
138
|
end
|
123
139
|
|
124
140
|
def valid?
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
return false
|
130
|
-
end
|
141
|
+
validate
|
142
|
+
true
|
143
|
+
rescue ValidationError
|
144
|
+
false
|
131
145
|
end
|
132
146
|
|
133
147
|
def validate
|
@@ -139,12 +153,14 @@ module IntelHex
|
|
139
153
|
end
|
140
154
|
|
141
155
|
def validate_type
|
142
|
-
|
156
|
+
return if TYPE_MAP.include?(type)
|
157
|
+
|
158
|
+
raise InvalidTypeError, "Type #{type} is invalid"
|
143
159
|
end
|
144
160
|
|
145
161
|
def validate_offset
|
146
|
-
raise InvalidOffsetError
|
147
|
-
raise InvalidOffsetError
|
162
|
+
raise InvalidOffsetError, "Offset #{offset} is negative" unless offset >= 0
|
163
|
+
raise InvalidOffsetError, "Offset #{offset} is too large" unless offset < 2**16
|
148
164
|
end
|
149
165
|
|
150
166
|
def validate_length
|
@@ -157,20 +173,24 @@ module IntelHex
|
|
157
173
|
return if valid_length.include?(length)
|
158
174
|
end
|
159
175
|
|
160
|
-
raise InvalidLengthError
|
176
|
+
raise InvalidLengthError, "Length for type #{type} must be #{valid_length}; #{length} given"
|
161
177
|
end
|
162
178
|
|
163
179
|
def validate_data
|
164
|
-
|
180
|
+
return if data.size == length
|
181
|
+
|
182
|
+
raise InvalidDataError, "Data length #{data.size} does not match length #{length}"
|
165
183
|
end
|
166
184
|
|
167
185
|
def validate_checksum
|
168
|
-
|
186
|
+
return if calculate_checksum == checksum
|
187
|
+
|
188
|
+
raise InvalidChecksumError, "Checksum value #{checksum} does not match expected checksum #{calculate_checksum}"
|
169
189
|
end
|
170
190
|
|
171
191
|
def each_byte_with_address
|
172
192
|
return Enumerator.new(self, :each_byte_with_address) unless block_given?
|
173
|
-
|
193
|
+
|
174
194
|
data.each_with_index do |byte, i|
|
175
195
|
yield byte, offset + i
|
176
196
|
end
|
@@ -179,12 +199,12 @@ module IntelHex
|
|
179
199
|
end
|
180
200
|
|
181
201
|
def data=(value)
|
182
|
-
raise
|
183
|
-
raise InvalidLengthError
|
202
|
+
raise 'Incorrect data type' unless value.is_a?(Array)
|
203
|
+
raise InvalidLengthError, "Data length #{value.size} too large" unless value.size <= 255
|
204
|
+
|
184
205
|
@data = value
|
185
206
|
@length = data.size
|
186
207
|
@checksum = recalculate_checksum
|
187
|
-
nil
|
188
208
|
end
|
189
209
|
|
190
210
|
def data_to_uint16(offset = 0)
|
@@ -192,9 +212,9 @@ module IntelHex
|
|
192
212
|
end
|
193
213
|
|
194
214
|
def uint16_to_data(value, offset = 0)
|
195
|
-
|
215
|
+
data[(offset...(offset + 2))] = [
|
196
216
|
(value & 0xff00) >> 8,
|
197
|
-
value & 0x00ff
|
217
|
+
value & 0x00ff,
|
198
218
|
]
|
199
219
|
@length = data.size
|
200
220
|
@checksum = recalculate_checksum
|
@@ -205,24 +225,24 @@ module IntelHex
|
|
205
225
|
end
|
206
226
|
|
207
227
|
def uint32_to_data(value, offset = 0)
|
208
|
-
|
228
|
+
data[(offset...(offset + 4))] = [
|
209
229
|
(value & 0xff000000) >> 24,
|
210
230
|
(value & 0x00ff0000) >> 16,
|
211
231
|
(value & 0x0000ff00) >> 8,
|
212
|
-
value & 0x000000ff
|
232
|
+
value & 0x000000ff,
|
213
233
|
]
|
214
234
|
@length = data.size
|
215
235
|
@checksum = recalculate_checksum
|
216
236
|
end
|
217
237
|
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
238
|
+
alias esa data_to_uint16
|
239
|
+
alias ssa data_to_uint32
|
240
|
+
alias ela data_to_uint16
|
241
|
+
alias sla data_to_uint32
|
222
242
|
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
243
|
+
alias esa= uint16_to_data
|
244
|
+
alias ssa= uint32_to_data
|
245
|
+
alias ela= uint16_to_data
|
246
|
+
alias sla= uint32_to_data
|
227
247
|
end
|
228
|
-
end
|
248
|
+
end
|
data/lib/intel_hex/version.rb
CHANGED
data/lib/intel_hex.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: intel_hex
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jeremy Cole
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-09-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -24,6 +24,34 @@ dependencies:
|
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rubocop
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rubocop-rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
27
55
|
description: An Intel hex file parser for (e.g. AVR) working with firmware files
|
28
56
|
email: jeremy@jcole.us
|
29
57
|
executables:
|
@@ -50,14 +78,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
50
78
|
requirements:
|
51
79
|
- - ">="
|
52
80
|
- !ruby/object:Gem::Version
|
53
|
-
version: '
|
81
|
+
version: '2.6'
|
82
|
+
- - "<"
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '4'
|
54
85
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
86
|
requirements:
|
56
87
|
- - ">="
|
57
88
|
- !ruby/object:Gem::Version
|
58
89
|
version: '0'
|
59
90
|
requirements: []
|
60
|
-
rubygems_version: 3.
|
91
|
+
rubygems_version: 3.1.2
|
61
92
|
signing_key:
|
62
93
|
specification_version: 4
|
63
94
|
summary: Parser for Intel hex files
|