Record/Parser.pm

filter: Statementstmt 95.8% 69 / 72 Branchbran 70.8% 17 / 24 Conditioncond 77.7% 7 / 9 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 8 / 8 Podpod 0.0% 0 / 2 totaltotal 84.8% 101 / 119 CC 19 SCAR 18.1
CC
19
Cov
89%
parse_srec_hex
8.5
_get_merged_parts
7.2
parse_intel_hex
6.0
CRAP 6.1
« Record.pm ↑ Summary
1 package Hex::Record::Parser;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use parent 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 our @EXPORT_OK = qw(
9 parse_intel_hex
10 parse_srec_hex);
11  
12 3 our $VERSION = '0.04';
13  
14 3 ▶use Hex::Record;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 sub parse_intel_hex {
17 6 ▶ my ($hex_string) = @_;
Subroutine parse_intel_hex: called CC=6 SCAR=17.9
Pod undocumented
18  
19 6 my $addr_high_dec = 0;
20  
21 6 my @hex_parts;
22 6 for my $line (split m{\n\r?}, $hex_string) {
23 33 ▶ $line =~ m{
Branch: 1 branch
BranchTrueFalseTotal
$line =~ / : # intel hex start [[:xdigit:]]{2} # bytecount ([[:xdigit:]]{4}) # addr ([[:xdigit:]]{2}) # type ([[:xdigit:]] * ) # databytes [[:xdigit:]]{2} # checksum /xi or next 0 33 33
24 : # intel hex start
25 [[:xdigit:]]{2} # bytecount
26 ([[:xdigit:]]{4}) # addr
27 ([[:xdigit:]]{2}) # type
28 ([[:xdigit:]] * ) # databytes
29 [[:xdigit:]]{2} # checksum
30 }ix or next;
31  
32 33 my $intel_type_dec = $2;
33 33 my @bytes_hex = unpack('(A2)*', $3);
34  
35 # data line?
36 33 ▶ if ($intel_type_dec == 0){
Branch: 3 branches
BranchTrueFalseTotal
if ($intel_type_dec == 0) { } 24 9 33
elsif ($intel_type_dec == 4) { } 3 6 9
elsif ($intel_type_dec == 2) { } 3 3 6
37 24 push @hex_parts, [$addr_high_dec + hex($1), \@bytes_hex];
38 }
39  
40 # extended linear address type?
41 elsif ($intel_type_dec == 4){
42 3 $addr_high_dec = hex( join '', @bytes_hex ) << 16;
43 }
44  
45 # extended segment address type?
46 elsif ($intel_type_dec == 2){
47 3 $addr_high_dec = hex( join '', @bytes_hex ) << 4;
48 }
49 }
50  
51 6 return _get_merged_parts(\@hex_parts);
52 }
53  
54 sub parse_srec_hex {
55 6 ▶ my ($hex_string) = @_;
Subroutine parse_srec_hex: called CC=8 SCAR=21.3
Pod undocumented
56  
57 6 my %address_length_of_srec_type = (
58 0 => '4',
59 1 => '4',
60 2 => '6',
61 3 => '8',
62 4 => undef,
63 5 => '4',
64 6 => '6',
65 7 => '8',
66 8 => '6',
67 9 => '4',
68 );
69  
70 6 my @hex_parts;
71 6 for my $line (split m{\n\r?}, $hex_string) {
72 18 ▶ next unless substr( $line, 0, 1 ) =~ m{s}i;
Branch: 1 branch
BranchTrueFalseTotal
unless substr($line, 0, 1) =~ /s/i 0 18 18
73  
74 18 my $type = substr $line, 1, 1;
75  
76 18 my $addr_length = $address_length_of_srec_type{$type};
77  
78 18 ▶ $line =~ m{
Branch: 1 branch
BranchTrueFalseTotal
$line =~ / s #srec hex start ([[:xdigit:]]{1}) #type [[:xdigit:]]{2} #bytecount ([[:xdigit:]]{$addr_length}) #addr ([[:xdigit:]] * ) #databytes [[:xdigit:]]{2} #checksum /xi or next 0 18 18
79 s #srec hex start
80 ([[:xdigit:]]{1}) #type
81 [[:xdigit:]]{2} #bytecount
82 ([[:xdigit:]]{$addr_length}) #addr
83 ([[:xdigit:]] * ) #databytes
84 [[:xdigit:]]{2} #checksum
85 }ix or next;
86  
87 #data line?
88 18 ▶ if ($1 == 0 || $1 == 1 || $1 == 2 || $1 == 3){
Branch: 1 branch
BranchTrueFalseTotal
if ($1 == 0 or $1 == 1 or $1 == 2 or $1 == 3) 6 0 6
Condition77%7/9
$1 == 0 or $1 == 1 or $1 == 2 or $1 == 3
l!l&&r!l&&!r
1530
$1 == 0 or $1 == 1 or $1 == 2
l!l&&r!l&&!r
1233
$1 == 0 or $1 == 1
l!l&&r!l&&!r
0126
MC/DC0%0/4
$1 == 0 or $1 == 1 or $1 == 2 or $1 == 3
$1 == 0 $1 == 1 $1 == 2 $1 == 3
Truth table60%3/5
A or B or C or D
A= $1 == 0B= $1 == 1C= $1 == 2D= $1 == 3
ABCDresult
1XXX1
01XX1
001X1
00011
00000
89 18 push @hex_parts, [ hex $2, [ unpack '(A2)*', $3 ] ];
90 }
91 }
92  
93 #sort the bytes of the record
94 6 return _get_merged_parts(\@hex_parts);
95 }
96  
97 sub _get_merged_parts {
98 12 ▶ my ($hex_parts_ref) = @_;
Subroutine _get_merged_parts: called CC=7 SCAR=19.7
99  
100 12 ▶ return unless @$hex_parts_ref;
Branch: 1 branch
BranchTrueFalseTotal
unless @$hex_parts_ref 0 12 12
101  
102 12 @$hex_parts_ref = sort { $a->[0] <=> $b->[0] } @$hex_parts_ref;
103  
104 # set first part
105 12 my @merged_parts = shift @$hex_parts_ref;
106 12 my $merged_parts_end_addr = $merged_parts[-1]->[0] + @{ $merged_parts[-1]->[1] };
107  
108 12 for my $hex_part_ref (@$hex_parts_ref) {
109  
110 30 my $hex_part_start_addr = $hex_part_ref->[0];
111 30 my $hex_part_byte_count = @{ $hex_part_ref->[1] };
112 30 my $hex_part_end_addr = $hex_part_start_addr + $hex_part_byte_count;
113  
114 # overwrite?
115 30 ▶ if ($hex_part_start_addr < $merged_parts_end_addr){
Branch: 2 branches
BranchTrueFalseTotal
if ($hex_part_start_addr < $merged_parts_end_addr) { } 6 24 30
elsif ($hex_part_start_addr == $merged_parts_end_addr) { } 14 10 24
116 # remove parts completly inlcuded
117 6 my $warning = "colliding parts: ";
118  
119 6 ▶ while ($hex_part_start_addr < $merged_parts[-1]->[0]){
Branch: 1 branch
BranchTrueFalseTotal
if ($hex_part_start_addr < $merged_parts[-1][0]) 0 6 6
120 0 my $removed_part = pop @merged_parts;
121 0 $warning .= $removed_part->[0] . ' .. ' . ($removed_part->[0] + @{$removed_part->[1]}) . ', ';
122 }
123  
124 $warning .=
125 6 $merged_parts[-1]->[0] . ' .. ' . ( $merged_parts[-1]->[0] + @{$merged_parts[-1]->[1]} )
126 . ' with part: '
127 . $hex_part_start_addr . ' .. ' . $hex_part_end_addr
128 . " ... overwriting";
129  
130 6 carp $warning;
131  
132 6 my $part_offset = $hex_part_start_addr - $merged_parts[-1]->[0];
133  
134 6 @{$merged_parts[-1]->[1]}[$part_offset .. $part_offset + $hex_part_byte_count - 1]
135 6 = @{$hex_part_ref->[1]};
136 }
137  
138 #append?
139 elsif ($hex_part_start_addr == $merged_parts_end_addr){
140 14 push @{$merged_parts[-1]->[1]}, @{$hex_part_ref->[1]};
141 }
142  
143 #new part!
144 else {
145 10 push @merged_parts, $hex_part_ref;
146 }
147  
148 #set new addr
149 30 ▶ $merged_parts_end_addr = $hex_part_end_addr if $hex_part_end_addr > $merged_parts_end_addr;
Branch: 1 branch
BranchTrueFalseTotal
if $hex_part_end_addr > $merged_parts_end_addr 30 0 30
150 }
151  
152 12 return \@merged_parts;
153 }
154  
155  
156  
157 3 1;
158  
159  
160 =head1 NAME
161  
162 Hex::Record::Parser - parse intel and srec hex records
163  
164 =head1 SYNOPSIS
165  
166 use Hex::Record::Parser qw(parse_intel_hex parse_srec_hex);
167  
168 # for intel hex record
169 my $hex_parts_ref = parse_intel_hex($intel_hex_record_as_string);
170  
171 # for srec hex record
172 my $hex_parts_ref = parse_srec_hex($srec_hex_record_as_string);
173  
174 # the sctucture returned by the parser will look like this
175 # the part start addresses (0x100, 0xFFFFF in example) are sorted
176 my $hex_parts_ref = [
177 0x100 => [qw(11 22 33 44 55 66)],
178 0xFFFFF => [qw(77 88 99 AA BB CC)],
179 ];
180  
181 # create hex records, to manipulate and dump hex data
182 use Hex::Record;
183  
184 my $hex_record = Hex::Record->new(
185 hex_parts => $hex_parts_ref
186 );
187  
188  
189 =head1 DESCRIPTION
190  
191 parse intel/srec hex files.
192  
193 =head1 LICENSE
194  
195 This is released under the Artistic License.
196  
197 =head1 AUTHOR
198  
199 spebern <bernhard@specht.net>
200  
201 =cut
« Record.pm ↑ Summary