Record.pm

filter: Statementstmt 88.2% 181 / 205 Branchbran 85.3% 70 / 82 Conditioncond 83.3% 5 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 10 / 10 Podpod 83.3% 5 / 6 totaltotal 87.2% 273 / 313 CC 46 SCAR 21.2
CC
46
Cov
87%
remove
12.4
get
10.2
_srec_hex_line_of
9.1
CRAP 8.3
↑ Summary Record/Parser.pm »
1 package Hex::Record;
2  
3 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 8 our $VERSION = '0.04';
7  
8 sub new {
9 6 ▶ my ($class, %args) = @_;
Subroutine new: called CC=2 SCAR=6.9
Pod undocumented
10  
11 6 ▶ $args{hex_parts} = [] unless exists $args{hex_parts};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"hex_parts"} 1 5 6
12  
13 6 return bless \%args, $class;
14 }
15  
16 sub write {
17 7 ▶ my ($self, $from, $bytes_hex_ref) = @_;
Subroutine write: called CC=7 SCAR=21.9
Pod documented
18  
19 7 $self->remove($from, scalar @$bytes_hex_ref);
20  
21 7 my $to = $from + @$bytes_hex_ref;
22  
23 # insert part
24 7 ▶ for (my $hex_part_i = 0; $hex_part_i < @{$self->{hex_parts}}; $hex_part_i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($hex_part_i < @{$self->{"hex_parts"};}) 10 5 15
25 10 my $hex_part = $self->{hex_parts}->[$hex_part_i];
26  
27 10 my $start_addr = $hex_part->[0];
28 10 my $end_addr = $hex_part->[0] + $#{$hex_part->[1]};
29  
30 # merge with this part
31 10 ▶ if ($to == $start_addr){
Branch: 3 branches
BranchTrueFalseTotal
if ($to == $start_addr) { } 1 9 10
elsif ($from == $end_addr + 1) { } 1 8 9
elsif ($from < $start_addr) { } 0 8 8
32 1 $hex_part->[0] = $from;
33 1 unshift @{$hex_part->[1]}, @$bytes_hex_ref;
34 1 return;
35 }
36 elsif ($from == $end_addr + 1){
37 1 push @{$hex_part->[1]}, @$bytes_hex_ref;
38  
39 1 ▶ return if $hex_part_i+1 == @{$self->{hex_parts}};
Branch: 1 branch
BranchTrueFalseTotal
if $hex_part_i + 1 == @{$self->{"hex_parts"};} 1 0 1
40  
41 0 my $next_part = $self->{hex_parts}->[$hex_part_i+1];
42 # merge with next part
43 0 ▶ if ($to == $next_part->[0] - 1){
Branch: 1 branch
BranchTrueFalseTotal
if ($to == $next_part->[0] - 1) 0 0 0
44 0 push @{$hex_part->[1]}, @{$next_part->[1]};
45 0 splice @{$self->{hex_parts}}, $hex_part_i+1, 1;
46 }
47 0 return;
48 }
49 elsif ($from < $start_addr){
50 0 splice @{$self->{hex_parts}}, $hex_part_i, 0, [$from, $bytes_hex_ref];
51 0 return;
52 }
53 }
54  
55 5 push @{$self->{hex_parts}}, [$from, $bytes_hex_ref];
56 5 return;
57 }
58  
59 sub get {
60 7 ▶ my ($self, $from, $length) = @_;
Subroutine get: called CC=10 SCAR=23.2
Pod documented
61  
62 7 my $to = $from + $length - 1;
63  
64 7 my @bytes_hex;
65 7 ▶ for (my $hex_part_i = 0; $hex_part_i < @{$self->{hex_parts}}; $hex_part_i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($hex_part_i < @{$self->{"hex_parts"};}) 25 3 28
66 25 my $hex_part = $self->{hex_parts}->[$hex_part_i];
67  
68 25 my $start_addr = $hex_part->[0];
69 25 my $end_addr = $hex_part->[0] + $#{$hex_part->[1]};
70  
71 # from inside this part
72 25 ▶ if ($from >= $start_addr && $from <= $end_addr){
Branch: 2 branches
BranchTrueFalseTotal
if ($from >= $start_addr and $from <= $end_addr) { } 5 20 25
elsif ($to <= $end_addr and $to >= $start_addr) { } 0 36 36
Condition100%3/3
$from >= $start_addr and $from <= $end_addr
!ll&&!rl&&r
4165
MC/DC100%2/2
$from >= $start_addr and $from <= $end_addr
$from >= $start_addr $from <= $end_addr
Truth table100%3/3
A and B
A= $from >= $start_addrB= $from <= $end_addr
ABresult
0X0
100
111
73  
74 # this part also includes end
75 5 ▶ if ($to <= $end_addr){
Branch: 1 branch
BranchTrueFalseTotal
if ($to <= $end_addr) { } 3 2 5
76 3 @bytes_hex = @{$hex_part->[1]}[ $from - $start_addr .. $to - $start_addr ];
77 3 return \@bytes_hex;
78 }
79 else {
80 2 @bytes_hex = @{$hex_part->[1]}[ $from - $start_addr .. $#{$hex_part->[1]} ];
81  
82 2 ▶ while (++$hex_part_i < @{$self->{hex_parts}}){
Branch: 1 branch
BranchTrueFalseTotal
if (++$hex_part_i < @{$self->{"hex_parts"};}) 2 1 3
83 2 my $hex_part = $self->{hex_parts}->[$hex_part_i];
84  
85 2 my $start_addr = $hex_part->[0];
86 2 my $end_addr = $hex_part->[0] + $#{$hex_part->[1]};
87 # desired part ended before this one
88 2 ▶ if ( $to < $start_addr ){
Branch: 1 branch
BranchTrueFalseTotal
if ($to < $start_addr) { } 0 2 2
89 0 return [@bytes_hex, (undef) x ($length - @bytes_hex)];
90 }
91  
92 else {
93 # fill gap betwenn this part and part before with undef
94 2 my $hex_part_before = $self->{hex_parts}->[$hex_part_i - 1];
95 2 my $hex_part_before_end_addr = $hex_part_before->[0] + $#{$hex_part_before->[1]};
96  
97 2 push @bytes_hex, (undef) x ($start_addr - $hex_part_before_end_addr);
98  
99 2 ▶ if ( $to <= $end_addr ){
Branch: 1 branch
BranchTrueFalseTotal
if ($to <= $end_addr) 1 1 2
100 1 push @bytes_hex, @{$hex_part->[1]}[ 0 .. $to - $start_addr ];
101 1 return \@bytes_hex;
102 }
103  
104 1 push @bytes_hex, @{$hex_part->[1]};
105 }
106 }
107 }
108 }
109  
110 # did not find start, but did find end
111 ▶ elsif ($to <= $end_addr && $to >= $start_addr){
Condition66%2/3
$to <= $end_addr and $to >= $start_addr
!ll&&!rl&&r
1640
MC/DC0%0/2
$to <= $end_addr and $to >= $start_addr
$to <= $end_addr $to >= $start_addr
Truth table66%2/3
A and B
A= $to <= $end_addrB= $to >= $start_addr
ABresult
0X0
100
111
112 @bytes_hex = ( (undef) x ($start_addr - $from),
113 0 @{$hex_part->[1]}[ 0 .. $to - $start_addr ] );
114 0 return \@bytes_hex;
115 }
116 }
117 3 return [@bytes_hex, (undef) x ($length - @bytes_hex)];
118 }
119  
120 sub remove {
121 18 ▶ my ($self, $from, $length) = @_;
Subroutine remove: called CC=12 SCAR=25.2
Pod documented
122  
123 18 my $to = $from + $length-1;
124  
125 18 ▶ for (my $hex_part_i = 0; $hex_part_i < @{ $self->{hex_parts} }; $hex_part_i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($hex_part_i < @{$self->{"hex_parts"};}) 31 7 38
126 31 my $hex_part = $self->{hex_parts}->[$hex_part_i];
127  
128 31 my $start_addr = $hex_part->[0];
129 31 my $end_addr = $hex_part->[0] + $#{$hex_part->[1]};
130  
131 31 ▶ if ($from <= $end_addr){
Branch: 2 branches
BranchTrueFalseTotal
if ($from <= $end_addr) { } 13 18 31
elsif ($to <= $end_addr) { } 0 18 18
132 13 ▶ if ($to <= $end_addr){
Branch: 1 branch
BranchTrueFalseTotal
if ($to <= $end_addr) { } 7 6 13
133 7 ▶ if ($from <= $start_addr){
Branch: 2 branches
BranchTrueFalseTotal
if ($from <= $start_addr) { } 4 3 7
elsif ($to == $end_addr) { } 1 2 3
134 4 ▶ if ($to == $end_addr){
Branch: 1 branch
BranchTrueFalseTotal
if ($to == $end_addr) { } 2 2 4
135 2 splice @{$self->{hex_parts}}, $hex_part_i, 1;
136 }
137 else {
138 2 splice @{$hex_part->[1]}, 0, @{$hex_part->[1]} - $end_addr+$to;;
139 2 $hex_part->[0] = $to+1;
140 }
141 4 return;
142 }
143 elsif ($to == $end_addr){
144 1 splice @{$hex_part->[1]}, $from-$start_addr, $length;
145 }
146 else {
147 2 splice @{$self->{hex_parts}}, $hex_part_i, 1, (
148 [
149 $start_addr,
150 2 [@{$hex_part->[1]}[ 0 .. $from - $start_addr - 1]],
151 ],
152 [
153 $from + $length,
154 2 [@{$hex_part->[1]}[ $from - $start_addr + $length .. $#{$hex_part->[1]}]],
155 ],
156 );
157 }
158 3 return;
159 }
160 else {
161 6 splice @{$self->{hex_parts}}, $hex_part_i, 1;
162 6 --$hex_part_i;
163 }
164 6 ▶ while (++$hex_part_i < @{$self->{hex_parts}}){
Branch: 1 branch
BranchTrueFalseTotal
if (++$hex_part_i < @{$self->{"hex_parts"};}) 12 2 14
165 12 my $hex_part = $self->{hex_parts}->[$hex_part_i];
166  
167 12 my $start_addr = $hex_part->[0];
168 12 ▶ return if $to < $start_addr;
Branch: 1 branch
BranchTrueFalseTotal
if $to < $start_addr 1 11 12
169  
170 11 my $end_addr = $hex_part->[0] + $#{$hex_part->[1]};
171  
172 11 ▶ if ($to < $end_addr){
Branch: 1 branch
BranchTrueFalseTotal
if ($to < $end_addr) 0 11 11
173 0 splice @{$hex_part->[1]}, 0, $to-$start_addr+1;
174 0 $hex_part->[0] = $to + 1;
175 0 return;
176 }
177 11 splice @{$self->{hex_parts}}, $hex_part_i, 1;
178 11 --$hex_part_i;
179  
180 11 ▶ return if $to == $end_addr;
Branch: 1 branch
BranchTrueFalseTotal
if $to == $end_addr 3 8 11
181 }
182 }
183 elsif ($to <= $end_addr){
184 0 splice @{$hex_part->[1]}, 0, $to-$start_addr+1;
185 0 $hex_part->[1] = $to + 1;
186 0 return;
187 }
188 }
189 }
190  
191 sub as_intel_hex {
192 2 ▶ my ($self, $bytes_hex_a_line) = @_;
Subroutine as_intel_hex: called CC=5 SCAR=16.1
Pod documented
193  
194 2 my $intel_hex_string = '';
195 2 ▶ for (my $hex_part_i = 0; $hex_part_i < @{ $self->{hex_parts} }; $hex_part_i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($hex_part_i < @{$self->{"hex_parts"};}) 7 2 9
196 7 my $hex_part = $self->{hex_parts}->[$hex_part_i];
197  
198 7 my $start_addr = $hex_part->[0];
199 7 my $end_addr = $hex_part->[0] + $#{$hex_part->[1]};
200  
201 7 my $cur_high_addr_hex = '0000';
202  
203 7 ▶ for (my $slice_i = 0; $slice_i * $bytes_hex_a_line < @{$hex_part->[1]}; $slice_i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($slice_i * $bytes_hex_a_line < @{$hex_part->[1];}) 12 7 19
204 12 my $total_addr = $start_addr + $slice_i*$bytes_hex_a_line;
205  
206 12 my ($addr_high_hex, $addr_low_hex) = unpack '(A4)*', sprintf('%08X', $total_addr);
207  
208 12 ▶ if ($cur_high_addr_hex ne $addr_high_hex){
Branch: 1 branch
BranchTrueFalseTotal
if ($cur_high_addr_hex ne $addr_high_hex) 3 9 12
209 3 $cur_high_addr_hex = $addr_high_hex;
210 3 $intel_hex_string .= _intel_hex_line_of( '0000', 4, [unpack '(A2)*', $cur_high_addr_hex]);
211 }
212  
213 12 ▶ if (($slice_i + 1) * $bytes_hex_a_line <= $#{$hex_part->[1]}){
Branch: 1 branch
BranchTrueFalseTotal
if (($slice_i + 1) * $bytes_hex_a_line <= $#{$$hex_part[1];}) { } 5 7 12
214 $intel_hex_string .= _intel_hex_line_of(
215 $addr_low_hex, 0,
216 5 [ @{$hex_part->[1]}[ $slice_i * $bytes_hex_a_line .. ($slice_i + 1) * $bytes_hex_a_line - 1 ] ]
217 );
218 }
219 else {
220 $intel_hex_string .= _intel_hex_line_of(
221 7 $addr_low_hex, 0, [ @{$hex_part->[1]}[ $slice_i * $bytes_hex_a_line .. $#{$hex_part->[1]} ] ]
222 );
223 }
224 }
225 }
226 # intel hex eof
227 2 return $intel_hex_string . ":00000001FF\n";
228 }
229  
230 sub _intel_hex_line_of {
231 15 ▶ my ($addr_low_hex, $type, $bytes_hex_ref) = @_;
Subroutine _intel_hex_line_of: called CC=4 SCAR=13.9
232  
233 15 ▶ my $byte_count = defined $bytes_hex_ref ? scalar @$bytes_hex_ref : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $bytes_hex_ref ? : 15 0 15
234  
235 15 my $sum = 0;
236 15 $sum += $_ for ( $byte_count, (map { hex $_ } unpack '(A2)*', $addr_low_hex),
237 121 $type, (map { hex $_ } @$bytes_hex_ref) );
238  
239 #convert to hex, take lsb
240 15 $sum = substr(sprintf( '%02X', $sum ), -2);
241  
242 15 my $checksum_hex = sprintf '%02X', (hex $sum ^ 255) + 1;
243 15 ▶ $checksum_hex = '00' if length $checksum_hex != 2;
Branch: 1 branch
BranchTrueFalseTotal
if length $checksum_hex != 2 1 14 15
244  
245 15 return join '',
246 (
247 ':',
248 sprintf( '%02X', $byte_count ),
249 $addr_low_hex,
250 sprintf( '%02X', $type ),
251 @$bytes_hex_ref,
252 $checksum_hex,
253 "\n"
254 );
255 }
256  
257 sub as_srec_hex {
258 1 ▶ my ($self, $bytes_hex_a_line) = @_;
Subroutine as_srec_hex: called CC=4 SCAR=13.9
Pod documented
259  
260 1 my $srec_hex_string = '';
261 1 ▶ for (my $hex_part_i = 0; $hex_part_i < @{ $self->{hex_parts} }; $hex_part_i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($hex_part_i < @{$self->{"hex_parts"};}) 7 1 8
262 7 my $hex_part = $self->{hex_parts}->[$hex_part_i];
263  
264 7 my $start_addr = $hex_part->[0];
265 7 my $end_addr = $hex_part->[0] + $#{$hex_part->[1]};
266  
267 7 ▶ for (my $slice_i = 0; $slice_i * $bytes_hex_a_line < @{$hex_part->[1]}; $slice_i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($slice_i * $bytes_hex_a_line < @{$hex_part->[1];}) 12 7 19
268 12 my $total_addr = $start_addr + $slice_i*$bytes_hex_a_line;
269  
270 12 ▶ if (($slice_i + 1) * $bytes_hex_a_line <= $#{$hex_part->[1]}){
Branch: 1 branch
BranchTrueFalseTotal
if (($slice_i + 1) * $bytes_hex_a_line <= $#{$$hex_part[1];}) { } 5 7 12
271 $srec_hex_string .= _srec_hex_line_of(
272 $total_addr,
273 5 [@{$hex_part->[1]}[$slice_i * $bytes_hex_a_line .. ($slice_i + 1) * $bytes_hex_a_line - 1]]
274 );
275 }
276 else {
277 $srec_hex_string .= _srec_hex_line_of(
278 $total_addr,
279 7 [@{$hex_part->[1]}[$slice_i * $bytes_hex_a_line .. $#{$hex_part->[1]}]]
280 );
281 }
282 }
283 }
284  
285 1 return $srec_hex_string;
286 }
287  
288 sub _srec_hex_line_of {
289 12 ▶ my ($total_addr, $bytes_hex_ref) = @_;
Subroutine _srec_hex_line_of: called CC=9 SCAR=22.1
290  
291 12 my $total_addr_hex = sprintf '%04X', $total_addr;
292  
293 12 my $type;
294 # 16 bit addr
295 12 ▶ if (length $total_addr_hex == 4){
Branch: 3 branches
BranchTrueFalseTotal
if (length $total_addr_hex == 4) { } 5 7 12
elsif (length $total_addr_hex <= 6) { } 4 3 7
elsif (length $total_addr_hex <= 8) { } 3 0 3
296 5 $type = 1;
297 }
298 # 24 bit addr
299 elsif (length $total_addr_hex <= 6){
300 4 ▶ $total_addr_hex = "0$total_addr_hex" if length $total_addr_hex == 5;
Branch: 1 branch
BranchTrueFalseTotal
if length $total_addr_hex == 5 3 1 4
301 4 $type = 2;
302 }
303 # 32 bit addr
304 elsif (length $total_addr_hex <= 8){
305 3 ▶ $total_addr_hex = "0$total_addr_hex" if length $total_addr_hex == 7;
Branch: 1 branch
BranchTrueFalseTotal
if length $total_addr_hex == 7 1 2 3
306 3 $type = 3;
307 }
308 else {
309 0 die "$total_addr_hex to big for 32 bit address";
310 }
311  
312 # count of data bytes + address bytes
313 12 ▶ my $byte_count = defined $bytes_hex_ref ? scalar @$bytes_hex_ref : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $bytes_hex_ref ? : 12 0 12
314 12 $byte_count += length($total_addr_hex) / 2;
315  
316 12 my $sum = 0;
317 12 $sum += $_ for ( $byte_count,
318 34 (map { hex $_ } unpack '(A2)*', $total_addr_hex),
319 93 (map { hex $_ } @$bytes_hex_ref) );
320  
321 #convert to hex, take lsb
322 12 $sum = substr(sprintf( '%02X', $sum ), -2);
323  
324 12 my $checksum_hex = sprintf '%02X', (hex $sum ^ 255);
325 12 ▶ $checksum_hex = '00' if length $checksum_hex != 2;
Branch: 1 branch
BranchTrueFalseTotal
if length $checksum_hex != 2 0 12 12
326  
327 12 return join '',
328 (
329 "S$type",
330 sprintf('%02X', $byte_count),
331 $total_addr_hex,
332 @$bytes_hex_ref,
333 $checksum_hex,
334 "\n"
335 );
336 }
337  
338 8 1;
339  
340 =head1 NAME
341  
342 Hex::Record - manipulate intel and srec hex records
343  
344 =head1 SYNOPSIS
345  
346 use Hex::Record;
347  
348 # create your own hex records
349 my $hex_record = Hex::Record->new;
350  
351 # write/overwrite 3 bytes starting at address 0x100
352 $hex_record->write(0x100, [qw(AA BB CC)]);
353  
354 # or use the parser
355 use Hex::Record::Parser qw(parse_intel_hex parse_srec_hex);
356  
357 my $hex_record = Hex::Record->new(
358 hex_parts => parse_intel_hex($intel_hex_record_as_string)
359 );
360  
361 # get 100 bytes (hex format) starting at address 0x100
362 # every single byte that is not found is returned as undef
363 my $bytes_ref = $hex_record->get(0x100, 10);
364  
365 # remove 100 bytes starting at address 0x100
366 $hex_record->remove(0x100, 10);
367  
368 # dump as intel hex (will use extended linear addresses for offset)
369 # maximum of 10 bytes in data field
370 my $intel_hex_string = $hex_record->as_intel_hex(10);
371  
372 # dump as srec hex (always tries to use smallest address)
373 # maximum of 10 bytes in data field
374 my $srec_hex_string = $hex_record->as_screc_hex(10);
375  
376 =head1 DESCRIPTION
377  
378 Manipulate intel/srec hex files.
379  
380 =head2 Methods of Hex
381  
382 =over 12
383  
384 =item C<get( $from, $count )>
385  
386 Returns $count hex bytes in array reference starting at address $from.
387 If hex byte is not found, undef instead.
388  
389 ['AA', '00', undef, undef, 'BC', undef]
390  
391 =item C<remove( $from, $count )>
392  
393 Removes $count bytes starting at address $from.
394  
395 =item C<write( $from, $bytes_ref )>
396  
397 (Over)writes bytes starting at address $from with bytes in $bytes_ref.
398  
399 =item C<as_intel_hex( $bytes_hex_a_line )>
400  
401 Returns a string containing hex bytes formated as intel hex.
402 Extended linear addresses as offset are used if needed.
403 Extended segment addresses are not supported.
404  
405 =item C<as_srec_hex( $bytes_hex_a_line )>
406  
407 Returns a string containing hex bytes formated as srec hex.
408 Maximum of $hytes_hex_a_line in data field.
409 Tries to use the smallest address field.
410  
411 =back
412  
413 =head1 LICENSE
414  
415 This is released under the Artistic License.
416  
417 =head1 AUTHOR
418  
419 spebern <bernhard@specht.net>
420  
421 =cut
↑ Summary Record/Parser.pm »