hex_file 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: aee016538fc7729772c64f95536e22e8a8d89628
4
+ data.tar.gz: 772609a03caa2d743c8feff434152919bf0bd3a5
5
+ SHA512:
6
+ metadata.gz: 55057f303c055979da2ba2373534934e4ff680e9e935fc0b4b791f1339ad23a6884400db9a2e8ada1eeb80f61812c2e37eeedc53e1ad473e91f2636ba563623a
7
+ data.tar.gz: 2b0b02349aec901b85a0d48bb31f67c86dd5c4fa2a1ba99240ed81daef1170678f940f3dc84afd844cb40010915d32b345abfb17435473c6b8502d6c93c2d6bb
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in hex_file.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Sean McCarthy
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,49 @@
1
+ # HexFile
2
+
3
+ Parses Intel HEX files and gives metadata about the file and records.
4
+
5
+ See http://en.wikipedia.org/wiki/Intel_HEX for information on the file format
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'hex_file'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install hex_file
22
+
23
+ ## Usage
24
+
25
+ info = HexFile::Info.new(File.open('myfile.hex', 'r'))
26
+
27
+ info.format
28
+ # I8HEX
29
+
30
+ info.binary_size
31
+ # 12134
32
+
33
+ info.records.each do |record|
34
+ puts record.data_size
35
+ puts record.byte_count
36
+ puts record.address
37
+ puts record.type
38
+ puts record.data
39
+ puts record.checksum
40
+ puts record.ok?
41
+ end
42
+
43
+ ## Contributing
44
+
45
+ 1. Fork it ( https://github.com/seandmccarthy/hex_file/fork )
46
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
47
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
48
+ 4. Push to the branch (`git push origin my-new-feature`)
49
+ 5. Create a new Pull Request
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.pattern = 'test/*.rb'
6
+ end
@@ -0,0 +1,31 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'hex_file'
4
+ require 'optparse'
5
+
6
+ OptionParser.new do |opts|
7
+ opts.banner = "Usage: #{$0}"
8
+ opts.on_tail("-h", "--help", "Show this message") do
9
+ puts "Usage: #{$0}"
10
+ exit
11
+ end
12
+ end.parse!
13
+
14
+ opt = ARGV.pop
15
+ if (opt.nil? || opt == '-h' || opt == '--help')
16
+ puts "Usage: #{$0} <hexfile.hex>"
17
+ puts
18
+ exit
19
+ end
20
+
21
+ unless File.exists?(opt)
22
+ $stderr.puts "#{opt} not found"
23
+ exit(1)
24
+ end
25
+
26
+ info = HexFile::Info.new(File.open(opt, 'r'))
27
+ puts <<EOD
28
+ Format: #{info.format}
29
+ Records: #{info.records.count}
30
+ Binary Size (bytes): #{info.binary_size}
31
+ EOD
@@ -0,0 +1,4 @@
1
+ $LOAD_PATH.unshift("#{File.dirname(__FILE__)}/../lib")
2
+ require 'hex_file_reader'
3
+
4
+ HexFileReader::Parse.new(File.open(ARGV[0]))
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'hex_file/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "hex_file"
8
+ spec.version = HexFile::VERSION
9
+ spec.authors = ["Sean McCarthy"]
10
+ spec.email = ["sean@clanmccarthy.net"]
11
+ spec.summary = %q{Parses Intel HEX files and gives metadata about the file and records.}
12
+ spec.description = %q{Parses Intel HEX files and gives metadata about the file and records.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ end
@@ -0,0 +1,6 @@
1
+ require 'hex_file/version'
2
+ require 'hex_file/info'
3
+ require 'hex_file/record'
4
+
5
+ module HexFile
6
+ end
@@ -0,0 +1,42 @@
1
+ module HexFile
2
+ class Info
3
+ def initialize(input_stream)
4
+ @records = input_stream.readlines.map do |record_line|
5
+ Record.new(record_line.strip)
6
+ end
7
+ end
8
+
9
+ def records
10
+ @records.each
11
+ end
12
+
13
+ def binary_size
14
+ records.map { |r| r.data_size }.reduce(:+)
15
+ end
16
+
17
+ def format
18
+ if linear_record_types?(record_types)
19
+ 'I32HEX'
20
+ elsif start_segment_record_type?(record_types)
21
+ 'I16HEX'
22
+ else
23
+ 'I8HEX'
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ def record_types
30
+ @record_types ||= records.map { |r| r.type }.uniq
31
+ end
32
+
33
+ def linear_record_types?(record_types)
34
+ record_types.include?(Record::EXTENDED_LINEAR_ADDRESS) ||
35
+ record_types.include?(Record::START_LINEAR_ADDRESS)
36
+ end
37
+
38
+ def start_segment_record_type?(record_types)
39
+ record_types.include?(Record::START_SEGMENT_ADDRESS)
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,3 @@
1
+ module HexFile
2
+ class InvalidRecord < Error; end
3
+ end
@@ -0,0 +1,46 @@
1
+ module HexFile
2
+ class InvalidRecord < StandardError; end
3
+
4
+ class Record
5
+ DATA = '00'
6
+ END_OF_FILE = '01'
7
+ EXTENDED_SEGMENT_ADDRESS = '02'
8
+ START_SEGMENT_ADDRESS = '03'
9
+ EXTENDED_LINEAR_ADDRESS = '04'
10
+ START_LINEAR_ADDRESS = '05'
11
+
12
+ attr_reader :type, :byte_count, :address, :data, :checksum
13
+
14
+ def initialize(record_ascii)
15
+ parse(record_ascii)
16
+ end
17
+
18
+ def data_size
19
+ @byte_count.to_i(16)
20
+ end
21
+
22
+ def ok?
23
+ @checksum.to_i(16) == calculate_checksum
24
+ end
25
+
26
+ private
27
+
28
+ def parse(record_ascii)
29
+ @raw = record_ascii
30
+ fail HexFile::InvalidRecord, 'Missing start code' unless @raw.start_with?(':')
31
+ @byte_count = record_ascii[1..2]
32
+ @address = record_ascii[3..6]
33
+ @type = record_ascii[7..8]
34
+ @data = record_ascii[9..-3]
35
+ @checksum = record_ascii[-2..-1]
36
+ end
37
+
38
+ def calculate_checksum
39
+ 256 -
40
+ (@raw[1...-2]
41
+ .scan(/../)
42
+ .map { |x| x.to_i(16) }
43
+ .inject(:+)) & 0xFF
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,3 @@
1
+ module HexFile
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,42 @@
1
+ require 'minitest/autorun'
2
+ require 'hex_file/info'
3
+ require 'hex_file/record'
4
+ require 'stringio'
5
+
6
+ class TestInfo < MiniTest::Unit::TestCase
7
+ def setup
8
+ string_io = StringIO.new(<<EOS)
9
+ :100083000227EFE5C754F84401F5C77901220000C0
10
+ :0300300002337A1E
11
+ EOS
12
+ @info = HexFile::Info.new(string_io)
13
+ end
14
+
15
+ def test_records
16
+ assert_equal @info.records.count, 2
17
+ end
18
+
19
+ def test_record_iteration
20
+ @info.records.each do |record|
21
+ assert record.ok?
22
+ end
23
+ end
24
+
25
+ def test_binary_size
26
+ assert_equal @info.binary_size, 19
27
+ end
28
+
29
+ def test_format_i32hex
30
+ info = HexFile::Info.new(StringIO.new(':02000004FFFFFC'))
31
+ assert_equal info.format, 'I32HEX'
32
+ end
33
+
34
+ def test_format_i16hex
35
+ info = HexFile::Info.new(StringIO.new(':0400000300003800C1'))
36
+ assert_equal info.format, 'I16HEX'
37
+ end
38
+
39
+ def test_format_i8hex
40
+ assert_equal @info.format, 'I8HEX'
41
+ end
42
+ end
@@ -0,0 +1,46 @@
1
+ require 'minitest/autorun'
2
+ require 'hex_file/record'
3
+
4
+ class TestRecord < MiniTest::Unit::TestCase
5
+ def setup
6
+ @record = HexFile::Record.new(':0300300002337A1E')
7
+ end
8
+
9
+ def test_type
10
+ assert_equal HexFile::Record::DATA, @record.type
11
+ end
12
+
13
+ def test_record_ok
14
+ assert @record.ok?
15
+ end
16
+
17
+ def test_data_size
18
+ assert_equal @record.data_size, 3
19
+ end
20
+
21
+ def test_byte_count
22
+ assert_equal @record.byte_count, '03'
23
+ end
24
+
25
+ def test_data
26
+ assert_equal @record.data, '02337A'
27
+ end
28
+
29
+ def test_address
30
+ assert_equal @record.address, '0030'
31
+ end
32
+
33
+ def test_checksum
34
+ assert_equal @record.checksum, '1E'
35
+ end
36
+
37
+ def test_bad_checksum
38
+ assert !HexFile::Record.new(':0300300002337AFF').ok?
39
+ end
40
+
41
+ def test_invalid_record
42
+ assert_raises HexFile::InvalidRecord do
43
+ HexFile::Record.new('')
44
+ end
45
+ end
46
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hex_file
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Sean McCarthy
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: Parses Intel HEX files and gives metadata about the file and records.
42
+ email:
43
+ - sean@clanmccarthy.net
44
+ executables:
45
+ - hex_file_info
46
+ - hex_file_info.rb
47
+ extensions: []
48
+ extra_rdoc_files: []
49
+ files:
50
+ - .gitignore
51
+ - Gemfile
52
+ - LICENSE.txt
53
+ - README.md
54
+ - Rakefile
55
+ - bin/hex_file_info
56
+ - bin/hex_file_info.rb
57
+ - hex_file.gemspec
58
+ - lib/hex_file.rb
59
+ - lib/hex_file/info.rb
60
+ - lib/hex_file/invalid_record.rb
61
+ - lib/hex_file/record.rb
62
+ - lib/hex_file/version.rb
63
+ - test/test_info.rb
64
+ - test/test_record.rb
65
+ homepage: ''
66
+ licenses:
67
+ - MIT
68
+ metadata: {}
69
+ post_install_message:
70
+ rdoc_options: []
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubyforge_project:
85
+ rubygems_version: 2.0.14
86
+ signing_key:
87
+ specification_version: 4
88
+ summary: Parses Intel HEX files and gives metadata about the file and records.
89
+ test_files:
90
+ - test/test_info.rb
91
+ - test/test_record.rb