hexdata 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/hexdata.rb +182 -0
  3. metadata +49 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 0266a1db395723ddaa44a482895c75513d7790c949a27635156ef99fb61578c6
4
+ data.tar.gz: 6add11f129b3610e7dc2fa5768084fa9ac40c2d8e1a533bb93d61d773139c704
5
+ SHA512:
6
+ metadata.gz: 37f47f6751bef3edd430e09c3f30a98289ca1259384c3c148fbaf384e7a19ca6229a5c838b6683d306abcd1a9429628b92a1cbff107eeb2eb659262890d451ce
7
+ data.tar.gz: 2fa7ae55ae148c31a74eb36b593f39aa23f98cd2188b3a0897533b9f221c860be6778dfba2aa46c3d675899e784ee521ea53ffea09afb58f28b8c4b5b93106f8
data/lib/hexdata.rb ADDED
@@ -0,0 +1,182 @@
1
+ # Author: Marek K.
2
+
3
+ =begin
4
+
5
+ This program is free software: you can redistribute it and/or modify
6
+ it under the terms of the GNU General Public License as published by
7
+ the Free Software Foundation, either version 3 of the License, or
8
+ (at your option) any later version.
9
+
10
+ This program is distributed in the hope that it will be useful,
11
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ GNU General Public License for more details.
14
+
15
+ You should have received a copy of the GNU General Public License
16
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
17
+
18
+ Dieses Programm ist Freie Software: Sie können es unter den Bedingungen
19
+ der GNU General Public License, wie von der Free Software Foundation,
20
+ Version 3 der Lizenz oder (nach Ihrer Wahl) jeder neueren
21
+ veröffentlichten Version, weiter verteilen und/oder modifizieren.
22
+
23
+ Dieses Programm wird in der Hoffnung bereitgestellt, dass es nützlich sein wird, jedoch
24
+ OHNE JEDE GEWÄHR,; sogar ohne die implizite
25
+ Gewähr der MARKTFÄHIGKEIT oder EIGNUNG FÜR EINEN BESTIMMTEN ZWECK.
26
+ Siehe die GNU General Public License für weitere Einzelheiten.
27
+
28
+ Sie sollten eine Kopie der GNU General Public License zusammen mit diesem
29
+ Programm erhalten haben. Wenn nicht, siehe <http://www.gnu.org/licenses/>.
30
+
31
+ =end
32
+
33
+ =begin
34
+ Examlple:
35
+ hex = HexData.new [":04001000746573742C", ":0D00000048656C6C6F20576F726C642100B6", ":10004000404142434445464748494A4B4C4D4E4F38", ":00000001FF"] # Open to read
36
+ hex.interpret # Interpret data
37
+ p hex.ascii # return string result of the interpret
38
+ p hex.checksum # return checksum from the first data
39
+ p hex.checksum 1 # return checksum from the second data
40
+
41
+ hex = HexData.new # Open to write/read
42
+ hex.push "test" # Write "test"
43
+ hex.push_end # Write End of File
44
+ hex.interpret # Interpret data
45
+ p hex.ascii 0 # return string result of the interpret of the first data
46
+ =end
47
+
48
+ class HexData
49
+
50
+ attr_accessor :push_address, :enddef
51
+
52
+ def initialize sets=[]
53
+ @sets = sets
54
+ @push_address = 16
55
+ @enddef = ":00000001FF"
56
+ end
57
+
58
+ def data
59
+ @sets
60
+ end
61
+
62
+ def interpret
63
+ @ascii = nil.to_a
64
+ @sums = nil.to_a
65
+ @sets.each { |data|
66
+ if data[0] != ":"
67
+ raise "Invalid data! Data: #{data}"
68
+ end
69
+
70
+ len = data[1 .. 2].to_i 16
71
+ address = data[3 .. 6]
72
+ type = data[7 .. 8]
73
+ field = data[9 .. len * 2 + 8]
74
+ sum = data[-2 .. -1].to_i 16
75
+ str = ""
76
+
77
+ csum = len + address.to_i(16) + type.to_i(16)
78
+ #p field
79
+ for i in (0..field.length-1).step(2)
80
+ v = field[i .. i + 1].to_i 16
81
+ csum += v
82
+ str += v.chr
83
+ end
84
+
85
+ csum &= 255
86
+ bsum = ""
87
+ csum.to_s(2).each_char { |c| bsum += (c=="0"?"1":"0") }
88
+ while bsum.length < 8
89
+ bsum = "1" + bsum
90
+ end
91
+
92
+ if bsum.to_i(2) + 1 != sum
93
+ raise "Invalid checksum! Data: #{data}"
94
+ end
95
+ @ascii << str if type == "00"
96
+ @sums << sum
97
+ }
98
+ end
99
+
100
+ def reset_address!
101
+ @push_address = 16
102
+ end
103
+
104
+ def push_user str
105
+ @sets << str
106
+ end
107
+
108
+ def delete_data! index
109
+ @sets.delete_at index
110
+ end
111
+
112
+ def clear!
113
+ @sets = nil.to_a
114
+ @sums = nil.to_a
115
+ @push_address = 16
116
+ end
117
+
118
+ def max_index?
119
+ @sets.length
120
+ end
121
+
122
+ def data_end?
123
+ for i in 0...@sets.length
124
+ return i if @sets[i] == @enddef
125
+ end
126
+ end
127
+
128
+ def checksum? index=0
129
+ return @sums[index]
130
+ end
131
+
132
+ def ascii index=nil
133
+ return (index ? @ascii[index] : @ascii.join(""))
134
+ end
135
+
136
+ def push_end
137
+ @sets << @enddef
138
+ end
139
+
140
+ def push str
141
+
142
+ if @push_address > 65535
143
+ raise "To many data!"
144
+ end
145
+
146
+ sum = str.length + @push_address
147
+ res = "#{str.length.to_s 16}"
148
+ while res.length < 2
149
+ res = "0" + res
150
+ end
151
+
152
+ res = ":" + res
153
+
154
+ tmp = @push_address.to_s 16
155
+ while tmp.length < 4
156
+ tmp = "0" + tmp
157
+ end
158
+ res += tmp + "00"
159
+
160
+ str.each_char { |c|
161
+ sum += c.ord
162
+ tmp = c.ord.to_s 16
163
+ while tmp.length < 2
164
+ tmp = "0" + tmp
165
+ end
166
+ res += tmp
167
+ }
168
+
169
+ sum &= 255
170
+ bsum = ""
171
+ sum.to_s(2).each_char { |c| bsum += (c=="0"?"1":"0") }
172
+ while bsum.length < 8
173
+ bsum = "1" + bsum
174
+ end
175
+
176
+ res += bsum.to_i(2).+(1).to_s 16
177
+ @sets << res
178
+ @push_address += 1
179
+ end
180
+
181
+ end
182
+
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hexdata
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Marek K.
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-11-23 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: |
14
+ With hexdata you can create and read (Intel) hex data.
15
+ hexdata also calculates the checksum and calls an exception if it gets wrong.
16
+ hexdata is available under the GNU GPL v3.0.
17
+ email: m.k@mk16.de
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - lib/hexdata.rb
23
+ homepage: http://test.mk16.de/projects/hexdata-gem
24
+ licenses:
25
+ - GPL-3.0
26
+ metadata:
27
+ documentation_uri: http://test.mk16.de/projects/hexdata-gem
28
+ source_code_uri: http://test.mk16.de/scriptFiles/hexdata.rb
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ requirements: []
44
+ rubyforge_project:
45
+ rubygems_version: 2.7.8
46
+ signing_key:
47
+ specification_version: 4
48
+ summary: Reads and writes (Intel) Hex files.
49
+ test_files: []