ConfigFile/Decode.pm

filter: Statementstmt 90.8% 139 / 153 Branchbran 60.9% 50 / 82 Conditioncond 42.8% 3 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 96.4% 27 / 28 Podpod 100% 16 / 16 totaltotal 81.0% 235 / 290 CC 50 SCAR 19.6
CC
50
Cov
79%
_byte_size
25.1
_snmp_oid
8.9
vendor
8.6
CRAP 7.1
« ConfigFile.pm ↑ Summary ConfigFile/Encode.pm »
1 package DOCSIS::ConfigFile::Decode;
2 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 17 ▶use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 17 ▶use Carp qw(confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 17 ▶use Math::BigInt;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 17 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 our %SNMP_TYPE = (
11 0x02 => ['INTEGER', \&int],
12 0x04 => ['STRING', \&string],
13 17 ▶ 0x05 => ['NULLOBJ', sub { }],
Subroutine __ANON__: not called CC=1 SCAR=0.0
14 0x40 => ['IPADDRESS', \&ip],
15 0x41 => ['COUNTER', \&uint],
16 0x42 => ['UNSIGNED', \&uint],
17 0x43 => ['TIMETICKS', \&uint],
18 0x44 => ['OPAQUE', \&uint],
19 0x46 => ['COUNTER64', \&bigint],
20 );
21  
22 2 ▶sub bigint {
Subroutine bigint: called CC=4 SCAR=13.9
Pod documented
23 2 my @bytes = unpack 'C*', _test_length(int => $_[0]);
24 2 my $negative = $bytes[0] & 0x80;
25 2 my $int64 = Math::BigInt->new(0);
26  
27 # setup int64
28 2 for my $chunk (@bytes) {
29 11 ▶ $chunk ^= 0xff if ($negative);
Branch: 1 branch
BranchTrueFalseTotal
if $negative 3 8 11
30 11 $int64 = ($int64 << 8) | $chunk;
31 }
32  
33 2 ▶ if ($negative) {
Branch: 1 branch
BranchTrueFalseTotal
if ($negative) 1 1 2
34 1 $int64 *= -1;
35 1 $int64 -= 1;
36 }
37  
38 2 return $int64;
39 }
40  
41 2 ▶sub ether {
Subroutine ether: called CC=3 SCAR=14.2
Pod documented
42 2 my $length = length $_[0];
43 2 ▶ return join '', unpack 'H2' x $length, $_[0] if $length == 6 or $length == 12;
Branch: 1 branch
BranchTrueFalseTotal
if $length == 6 or $length == 12 2 0 2
Condition33%1/3
$length == 6 or $length == 12
l!l&&r!l&&!r
200
MC/DC0%0/2
$length == 6 or $length == 12
$length == 6 $length == 12
Truth table33%1/3
A or B
A= $length == 6B= $length == 12
ABresult
1X1
011
000
44 0 confess "Invalid ether input. Invalid length ($length)";
45 }
46  
47 52 ▶sub hexstr { '0x' . join '', unpack 'H*', $_[0] }
Subroutine hexstr: called CC=1 SCAR=0.0
Pod documented
48  
49 12 ▶sub int {
Subroutine int: called CC=4 SCAR=13.9
Pod documented
50 12 my @bytes = unpack 'C*', _test_length(int => $_[0], 'int');
51 12 my $negative = $bytes[0] & 0x80;
52 12 my $int = 0;
53  
54 12 for my $chunk (@bytes) {
55 30 ▶ $chunk ^= 0xff if ($negative);
Branch: 1 branch
BranchTrueFalseTotal
if $negative 12 18 30
56 30 $int = ($int << 8) | $chunk;
57 }
58  
59 12 ▶ if ($negative) {
Branch: 1 branch
BranchTrueFalseTotal
if ($negative) 4 8 12
60 4 $int *= -1;
61 4 $int -= 1;
62 }
63  
64 12 return $int;
65 }
66  
67 6 ▶sub ip { inet_ntoa($_[0]) || confess 'inet_ntoa(...) failed to unpack binary string' }
Subroutine ip: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
inet_ntoa($_[0]) or confess("inet_ntoa(...) failed to unpack binary string") 6 0 6
68 35 ▶sub mic {&hexstr}
Subroutine mic: called CC=1 SCAR=0.0
Pod documented
69 1 ▶sub no_value {''}
Subroutine no_value: called CC=1 SCAR=0.0
Pod documented
70  
71 12 ▶sub snmp_object {
Subroutine snmp_object: called CC=1 SCAR=0.0
Pod documented
72 12 my $bin = $_[0];
73 12 my ($byte, $length, $oid, $type, $value);
74  
75 # message
76 12 $type = _truncate_and_unpack(\$bin, 'C1'); # 0x30
77 12 $length = _snmp_length(\$bin);
78  
79 # oid
80 12 $type = _truncate_and_unpack(\$bin, 'C1'); # 0x06
81 12 $length = _snmp_length(\$bin);
82 12 $oid = _snmp_oid(\$bin, $length);
83  
84 # value
85 12 $type = $SNMP_TYPE{_truncate_and_unpack(\$bin, 'C1')};
86 12 $length = _snmp_length(\$bin);
87 12 $value = $type->[1]->($bin);
88  
89 12 return {oid => $oid, type => $type->[0], value => $value};
90 }
91  
92 23 ▶sub string {
Subroutine string: called CC=3 SCAR=11.2
Pod documented
93  
94 # not sure why this is able to join - may be removed later
95 0 ▶ my $bin = @_ > 1 ? join '', map { chr $_ } @_ : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 0 23 23
96  
97 23 ▶ return hexstr($bin) if $bin =~ /^[^\t\n\r\x20-\xEF]/;
Branch: 1 branch
BranchTrueFalseTotal
if $bin =~ /^[^\t\n\r\x20-\xEF]/ 1 22 23
98 22 $bin =~ s/([^\x20-\x24\x26-\x7e])/{ sprintf "%%%02x", ord $1 }/ge;
99 22 return $bin;
100 }
101  
102 4 ▶sub stringz { my $str = string(@_); $str =~ s/%00$//; $str; }
Subroutine stringz: called CC=1 SCAR=0.0
Pod documented
103 56 ▶sub uchar { unpack 'C', _test_length(uchar => $_[0], 'char') }
Subroutine uchar: called CC=1 SCAR=0.0
Pod documented
104  
105 27 ▶sub uint {
Subroutine uint: called CC=2 SCAR=6.9
Pod documented
106 27 my @bytes = unpack 'C*', _test_length(uint => $_[0], 'int');
107 27 my $value = 0;
108  
109 27 $value = ($value << 8) | $_ for (@bytes);
110 27 return $value;
111 }
112  
113 48 ▶sub ushort { unpack 'n', _test_length(ushort => $_[0], 'short int') }
Subroutine ushort: called CC=1 SCAR=0.0
Pod documented
114  
115 2 ▶sub ushort_list {
Subroutine ushort_list: called CC=1 SCAR=0.0
Pod documented
116 2 [map { ushort($_) } $_[0] =~ /(..)/g]
117 }
118  
119 2 ▶sub vendorspec {
Subroutine vendorspec: called CC=7 SCAR=20.0
Pod documented
120 2 ▶ my $bin = $_[0] || '';
Condition50%1/2
$_[0] || ''
l!l
20
MC/DC0%0/1
$_[0] || ''
$_[0]
Truth table50%1/2
A
A= $_[0]
Aresult
11
00
121 2 my ($vendor, @ret, $length);
122  
123 # extract length (not sure what the first byte is...)
124 2 ▶ $bin =~ s/^.(.)//s or confess 'Invalid vendorspec input. Could not extract length';
Branch: 1 branch
BranchTrueFalseTotal
$bin =~ s/^.(.)//s or confess("Invalid vendorspec input. Could not extract length") 2 0 2
125 2 $length = unpack 'C', $1;
126  
127 # extract vendor
128 2 ▶ $bin =~ s/^(.{$length})//s or confess 'Invalid vendorspec input. Could not extract vendor';
Branch: 1 branch
BranchTrueFalseTotal
$bin =~ s/^(.{$length})//s or confess("Invalid vendorspec input. Could not extract vendor") 2 0 2
129 2 $vendor = sprintf '0x' . ('%02x' x $length), unpack 'C*', $1;
130  
131 # extract TLV
132 2 ▶ while ($bin =~ s/^(.)(.)//s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bin =~ s/^(.)(.)//s) 2 2 4
133 2 my $type = unpack 'C*', $1;
134 2 my $length = unpack 'C*', $2;
135  
136 2 ▶ if ($bin =~ s/^(.{$length})//s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bin =~ s/^(.{$length})//s) 2 0 2
137 2 push @ret, {type => $type, length => $length, value => hexstr($1)};
138 }
139 }
140  
141 2 ▶ confess "vendorspec('...') is left with ($length) bytes after decoding" if $length = length $bin;
Branch: 1 branch
BranchTrueFalseTotal
if $length = length $bin 0 2 2
142 2 return $vendor, \@ret;
143 }
144  
145 3 ▶sub vendor {
Subroutine vendor: called CC=8 SCAR=21.6
Pod documented
146 3 ▶ my $bin = shift || '';
Condition50%1/2
shift() || ''
l!l
30
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
147 3 ▶ my $length = $bin =~ s/^.(.)//s ? unpack 'C', $1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$bin =~ s/^.(.)//s ? : 3 0 3
148 3 my ($id, @options);
149  
150 3 ▶ if ($bin =~ s/^(.{$length})//s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bin =~ s/^(.{$length})//s) 3 0 3
151 3 $id = sprintf "0x@{['%02x' x $length]}", unpack 'C*', $1;
152 }
153  
154 3 ▶ while ($bin =~ s/^(.)(.)//s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bin =~ s/^(.)(.)//s) 7 3 10
155 7 my $type = unpack 'C*', $1;
156 7 my $length = unpack 'C*', $2;
157  
158 7 ▶ $bin =~ s/^(.{$length})//s or next;
Branch: 1 branch
BranchTrueFalseTotal
$bin =~ s/^(.{$length})//s or next 7 0 7
159 7 push @options, $type, hexstr($1);
160 }
161  
162 3 ▶ confess 'Bytes left in vendorspec' if length $bin;
Branch: 1 branch
BranchTrueFalseTotal
if length $bin 0 3 3
163 3 ▶ confess 'Invalid vendorspec' unless defined $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 3 3
164 3 return {id => $id, options => \@options};
165 }
166  
167 143 ▶sub _byte_size {
Subroutine _byte_size: called CC=9 SCAR=32.2
168 143 ▶ return 2 if lc $_[0] eq 'short int';
Branch: 1 branch
BranchTrueFalseTotal
if lc $_[0] eq "short int" 48 95 143
169 95 ▶ return 4 if lc $_[0] eq 'int';
Branch: 1 branch
BranchTrueFalseTotal
if lc $_[0] eq "int" 39 56 95
170 56 ▶ return 4 if lc $_[0] eq 'long int';
Branch: 1 branch
BranchTrueFalseTotal
if lc $_[0] eq "long int" 0 56 56
171 56 ▶ return 1 if lc $_[0] eq 'char';
Branch: 1 branch
BranchTrueFalseTotal
if lc $_[0] eq "char" 56 0 56
172 0 ▶ return 4 if lc $_[0] eq 'float';
Branch: 1 branch
BranchTrueFalseTotal
if lc $_[0] eq "float" 0 0 0
173 0 ▶ return 8 if lc $_[0] eq 'double';
Branch: 1 branch
BranchTrueFalseTotal
if lc $_[0] eq "double" 0 0 0
174 0 ▶ return 12 if lc $_[0] eq 'long double';
Branch: 1 branch
BranchTrueFalseTotal
if lc $_[0] eq "long double" 0 0 0
175 0 ▶ return 16 if lc $_[0] eq 'md5digest';
Branch: 1 branch
BranchTrueFalseTotal
if lc $_[0] eq "md5digest" 0 0 0
176 }
177  
178 36 ▶sub _snmp_length {
Subroutine _snmp_length: called CC=5 SCAR=17.0
179 36 my $length = _truncate_and_unpack($_[0], 'C1'); # length?
180  
181 36 ▶ if ($length <= 0x80) {
Branch: 3 branches
BranchTrueFalseTotal
if ($length <= 128) { } 32 4 36
elsif ($length == 129) { } 0 4 4
elsif ($length == 130) { } 4 0 4
182 32 return $length;
183 }
184 elsif ($length == 0x81) {
185 0 return _truncate_and_unpack($_[0], 'C1');
186 }
187 elsif ($length == 0x82) {
188 4 $length = 0;
189  
190 4 for my $byte (_truncate_and_unpack($_[0], 'C2')) {
191 8 $length = $length << 8 | $byte;
192 }
193  
194 4 return $length;
195 }
196  
197 0 confess "Too long SNMP length: ($length)";
198 }
199  
200 12 ▶sub _snmp_oid {
Subroutine _snmp_oid: called CC=8 SCAR=21.9
201 12 my @bytes = _truncate_and_unpack($_[0], 'C' . $_[1]);
202 12 my @oid = (0);
203 12 my $subid = 0;
204  
205 12 for my $id (@bytes) {
206 130 ▶ if ($subid & 0xfe000000) {
Branch: 1 branch
BranchTrueFalseTotal
if ($subid & 4261412864) 0 130 130
207 0 confess "_snmp_oid(@bytes): Sub-identifier too large: ($subid)";
208 }
209  
210 130 $subid = ($subid << 7) | ($id & 0x7f);
211  
212 130 ▶ unless ($id & 0x80) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($id & 128) 126 4 130
213 126 ▶ confess "_snmp_oid(@bytes): Exceeded max length" if (128 <= @oid);
Branch: 1 branch
BranchTrueFalseTotal
if 128 <= @oid 0 126 126
214 126 push @oid, $subid;
215 126 $subid = 0;
216 }
217 }
218  
219 # the first two sub-id are in the first id
220 12 ▶ if ($oid[1] == 0x2b) { # Handle the most common case
Branch: 3 branches
BranchTrueFalseTotal
if ($oid[1] == 43) { } 10 2 12
elsif ($oid[1] < 40) { } 0 2 2
elsif ($oid[1] < 80) { } 2 0 2
221 10 $oid[0] = 1;
222 10 $oid[1] = 3;
223 }
224 elsif ($oid[1] < 40) {
225 0 $oid[0] = 0;
226 }
227 elsif ($oid[1] < 80) {
228 2 $oid[0] = 1;
229 2 $oid[1] -= 40;
230 }
231 else {
232 0 $oid[0] = 2;
233 0 $oid[1] -= 80;
234 }
235  
236 12 return SNMP::translateObj(join '.', @oid) || join '.', @oid
237 if DOCSIS::ConfigFile::CAN_TRANSLATE_OID;
238 12 return join '.', @oid;
239 }
240  
241 145 ▶sub _test_length {
Subroutine _test_length: called CC=4 SCAR=14.2
242 145 my $name = $_[0];
243 145 my $length = length $_[1];
244  
245 145 ▶ if (!$length) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($length) 0 145 145
246 0 confess "$name(...) bytestring length is zero";
247 }
248 145 ▶ if ($_[2]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[2]) 143 2 145
249 143 my $max = _byte_size($_[2]);
250 143 ▶ confess "$name(...) bytestring length is invalid: $max < $length" if ($max < $length);
Branch: 1 branch
BranchTrueFalseTotal
if $max < $length 0 143 143
251 }
252  
253 145 return $_[1];
254 }
255  
256 88 ▶sub _truncate_and_unpack {
Subroutine _truncate_and_unpack: called CC=3 SCAR=12.5
257 88 my ($bin_ref, $type) = @_;
258 88 ▶ my $n = ($type =~ /C/ ? 1 : 2) * ($type =~ /(\d+)/)[0];
Branch: 1 branch
BranchTrueFalseTotal
$type =~ /C/ ? : 88 0 88
259  
260 88 ▶ return unpack $type, $1 if $$bin_ref =~ s/^(.{$n})//s;
Branch: 1 branch
BranchTrueFalseTotal
if $$bin_ref =~ s/^(.{$n})//s 88 0 88
261 0 confess "_truncate_and_unpack('...', $type) failed to truncate binary string";
262 }
263  
264 17 1;
265  
266 =encoding utf8
267  
268 =head1 NAME
269  
270 DOCSIS::ConfigFile::Decode - Decode functions for a DOCSIS config-file
271  
272 =head1 DESCRIPTION
273  
274 L<DOCSIS::ConfigFile::Decode> has functions which is used to decode binary data
275 into either plain strings or complex data structures, dependent on the function
276 called.
277  
278 =head1 FUNCTIONS
279  
280 =head2 bigint
281  
282 Returns a C<Math::BigInt> object.
283  
284 =head2 ether
285  
286 Will unpack the input string and return a MAC address in this format:
287 "00112233" or "00112233445566".
288  
289 =head2 hexstr
290  
291 Will unpack the input string and a string with leading "0x", followed
292 by hexidesimal characters.
293  
294 =head2 int
295  
296 Will unpack the input string and return an integer, from -2147483648
297 to 2147483647.
298  
299 =head2 ip
300  
301 Will unpack the input string and return a human readable IPv4 address.
302  
303 =head2 string
304  
305 Returns human-readable string, where special characters are "uri encoded".
306 Example: "%" = "%25" and " " = "%20". It can also return the value from
307 L</hexstr> if it starts with a weird character, such as C<\x00>.
308  
309 =head2 stringz
310  
311 Same as string above. However this string is zero-terminated in encoded
312 form, but this function remove the last "\0" seen in the string.
313  
314 =head2 mic
315  
316 Returns a value, printed as hex.
317  
318 =head2 no_value
319  
320 This method will return an empty string. It is used by DOCSIS types, which
321 has zero length.
322  
323 =head2 snmp_object
324  
325 Will take a binary string and decode it into a complex
326 datastructure, with "oid", "type" and "value".
327  
328 =head2 uchar
329  
330 Will unpack the input string and return a short integer, from 0 to 255.
331  
332 =head2 uint
333  
334 Will unpack the input string and return an integer, from 0 to 4294967295.
335  
336 =head2 ushort
337  
338 Will unpack the input string and return a short integer, from 0 to 65535.
339  
340 =head2 ushort_list
341  
342 Same as L</ushort>, but will return an array ref with the integers.
343  
344 =head2 ushort_list
345  
346 =head2 vendor
347  
348 Will byte-encode a complex vendorspec datastructure.
349  
350 =head2 vendorspec
351  
352 Will unpack the input string and return a complex datastructure,
353 representing the vendor specific data.
354  
355 =head1 SEE ALSO
356  
357 L<DOCSIS::ConfigFile>
358  
359 =cut
« ConfigFile.pm ↑ Summary ConfigFile/Encode.pm »