ConfigFile/Encode.pm

filter: Statementstmt 89.4% 152 / 170 Branchbran 59.5% 50 / 84 Conditioncond 37.5% 6 / 16 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 92.5% 25 / 27 Podpod 100% 17 / 17 totaltotal 77.1% 250 / 324 CC 53 SCAR 17.8
CC
53
Cov
77%
_snmp_length
16.2
int
10.3
uint
8.2
CRAP 5.9
« ConfigFile/Decode.pm ↑ Summary
1 package DOCSIS::ConfigFile::Encode;
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 17 ▶use Carp qw(confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 17 ▶use Math::BigInt;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 17 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 our %SNMP_TYPE = (
10 INTEGER => [0x02, \&int],
11 STRING => [0x04, \&string],
12 17 ▶ NULLOBJ => [0x05, sub { }],
Subroutine __ANON__: not called CC=1 SCAR=0.0
13 OBJECTID => [0x06, \&objectid],
14 IPADDRESS => [0x40, \&ip],
15 COUNTER => [0x41, \&uint],
16 UNSIGNED => [0x42, \&uint],
17 TIMETICKS => [0x43, \&uint],
18 OPAQUE => [0x44, \&uint],
19 COUNTER64 => [0x46, \&bigint],
20 );
21  
22 1 ▶sub bigint {
Subroutine bigint: called CC=6 SCAR=18.3
Pod documented
23 1 my $value = _test_value(bigint => $_[0]);
24 1 my $int64 = Math::BigInt->new($value);
25  
26 1 ▶ $int64->is_nan and confess "$value is not a number";
Branch: 1 branch
BranchTrueFalseTotal
$int64->is_nan and confess("$value is not a number") 0 1 1
27  
28 1 my $negative = $int64 < 0;
29 1 ▶ my @bytes = $negative ? (0x80) : ();
Branch: 1 branch
BranchTrueFalseTotal
$negative ? : 0 1 1
30  
31 1 ▶ while ($int64) {
Branch: 1 branch
BranchTrueFalseTotal
if ($int64) 8 1 9
32 8 my $value = $int64 & 0xff;
33 8 $int64 >>= 8;
34 8 ▶ $value ^= 0xff if ($negative);
Branch: 1 branch
BranchTrueFalseTotal
if $negative 0 8 8
35 8 unshift @bytes, $value;
36 }
37  
38 1 ▶ return @bytes ? @bytes : (0); # 0 is also a number ;-)
Branch: 1 branch
BranchTrueFalseTotal
@bytes ? : 1 0 1
39 }
40  
41 7 ▶sub ether {
Subroutine ether: called CC=3 SCAR=13.3
Pod documented
42 7 my $string = _test_value(ether => $_[0]);
43  
44 7 ▶ if ($string =~ qr{^\+?[0-4294967295]$}) { # numeric
Branch: 2 branches
BranchTrueFalseTotal
if ($string =~ qr/^\+?[0-4294967295]$/) { } 0 7 7
elsif ($string =~ /^(?:0x)?([0-9a-f]+)$/i) { } 7 0 7
45 0 return uint({value => $string});
46 }
47 elsif ($string =~ /^(?:0x)?([0-9a-f]+)$/i) { # hex
48 7 return hexstr({value => $1});
49 }
50  
51 0 confess "ether({ value => $string }) is invalid";
52 }
53  
54 23 ▶sub hexstr {
Subroutine hexstr: called CC=3 SCAR=11.0
Pod documented
55 23 my $string = _test_value(hexstr => $_[0], qr{(?:0x)?([a-f0-9]+)}i);
56 23 my @bytes;
57  
58 23 $string =~ s/^(?:0x)//;
59 23 ▶ unshift @bytes, hex $1 while $string =~ s/(\w{1,2})$//;
Branch: 1 branch
BranchTrueFalseTotal
$string =~ s/(\w{1,2})$// and do { unshift @bytes, hex $1 } 223 23 246
60 23 ▶ confess "hexstr({ value => ... }) is left with ($string) after decoding" if $string;
Branch: 1 branch
BranchTrueFalseTotal
if $string 0 23 23
61 23 return @bytes;
62 }
63  
64 6 ▶sub ip { split /\./, _test_value(ip => $_[0], qr{^(?:\d{1,3}\.){3}\d{1,3}$}) }
Subroutine ip: called CC=1 SCAR=0.0
Pod documented
65  
66 7 ▶sub int {
Subroutine int: called CC=10 SCAR=23.3
Pod documented
67 7 my $obj = $_[0];
68 7 my $int = _test_value(int => $obj, qr{^[+-]?\d{1,10}$});
69 7 my $negative = $int < 0;
70 7 my @bytes;
71  
72 # make sure we're working on 32bit
73 7 $int &= 0xffffffff;
74  
75 7 ▶ while ($int) {
Branch: 1 branch
BranchTrueFalseTotal
if ($int) 16 7 23
76 16 my $value = $int & 0xff;
77 16 $int >>= 8;
78 16 ▶ $value ^= 0xff if ($negative);
Branch: 1 branch
BranchTrueFalseTotal
if $negative 4 12 16
79 16 unshift @bytes, $value;
80 }
81  
82 7 ▶ if (!$obj->{snmp}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($obj->{"snmp"}) 2 5 7
83 2 ▶ $bytes[0] |= 0x80 if ($negative);
Branch: 1 branch
BranchTrueFalseTotal
if $negative 1 1 2
84 2 unshift @bytes, 0 for (1 .. 4 - @bytes);
85 }
86 7 ▶ if (@bytes == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@bytes == 0) 0 7 7
87 0 @bytes = (0);
88 }
89 7 ▶ if ($obj->{snmp}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->{"snmp"}) 5 2 7
90 5 ▶ unshift @bytes, 0 if (!$negative and $bytes[0] > 0x79);
Branch: 1 branch
BranchTrueFalseTotal
if not $negative and $bytes[0] > 121 0 5 5
Condition33%1/3
not $negative and $bytes[0] > 121
!ll&&!rl&&r
050
MC/DC0%0/2
not $negative and $bytes[0] > 121
not $negative $bytes[0] > 121
Truth table33%1/3
not(A) and B
A= not $negativeB= $bytes[0] > 121
ABresult
0X0
100
111
91 }
92  
93 7 return @bytes;
94 }
95  
96 9 ▶sub mic { }
Subroutine mic: called CC=1 SCAR=0.0
Pod documented
97 1 ▶sub no_value { }
Subroutine no_value: called CC=1 SCAR=0.0
Pod documented
98  
99 0 ▶sub objectid {
Subroutine objectid: not called CC=1 SCAR=6.9
Pod documented
100 0 my $oid = _test_value(objectid => $_[0], qr{^\.?\d+(\.\d+)+$});
101 0 $oid =~ s/^\.//;
102 0 return _snmp_oid($oid);
103 }
104  
105 11 ▶sub snmp_object {
Subroutine snmp_object: called CC=4 SCAR=15.5
Pod documented
106 11 my $obj = _test_value(snmp_object => $_[0]);
107 0 ▶ my $type = $SNMP_TYPE{uc($obj->{type})} or confess "Unknown SNMP type: @{[$obj->{type}||'']}";
Branch: 1 branch
BranchTrueFalseTotal
my $type = $DOCSIS::ConfigFile::Encode::SNMP_TYPE{uc $obj->{"type"}} or confess(qq[Unknown SNMP type: @{[$obj->{"type"} || ""];}]) 11 0 11
Condition0%0/2
$obj->{'type'} || ''
l!l
00
MC/DC0%0/1
$obj->{'type'} || ''
$obj->{'type'}
Truth table0%0/2
A
A= $obj->{'type'}
Aresult
11
00
108 11 my @value = $type->[1]->({value => $obj->{value}, snmp => 1});
109 11 my @oid = _snmp_oid($obj->{oid});
110  
111 11 ▶ unless (@value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@value) 0 11 11
112 0 confess 'Failed to decode SNMP value: ' . $obj->{value};
113 }
114  
115 11 my @oid_length = _snmp_length(0 + @oid);
116 11 my @value_length = _snmp_length(0 + @value);
117 11 my @total_length = _snmp_length(3 + @value + @oid + @value_length);
118  
119 return (
120 #-type--------length----------value-----type---
121 11 0x30, @total_length, # object
122 0x06, @oid_length, @oid, # oid
123 $type->[0], @value_length, @value, # value
124 );
125 }
126  
127 21 ▶sub string {
Subroutine string: called CC=2 SCAR=6.9
Pod documented
128 21 my $string = _test_value(string => $_[0]);
129 21 ▶ return hexstr(@_) if $string =~ /^0x[a-f0-9]+$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $string =~ /^0x[a-f0-9]+$/i 3 18 21
130 18 $string =~ s/%(\w\w)/{ chr hex $1 }/ge;
131 18 return map { ord $_ } split //, $string;
132 }
133  
134 2 ▶sub stringz {
Subroutine stringz: called CC=3 SCAR=12.5
Pod documented
135 2 my @bytes = string(@_);
136 2 ▶ push @bytes, 0 if (@bytes == 0 or $bytes[-1] ne "\0");
Branch: 1 branch
BranchTrueFalseTotal
if @bytes == 0 or $bytes[-1] ne "\0" 2 0 2
Condition33%1/3
@bytes == 0 or $bytes[-1] ne "\0"
l!l&&r!l&&!r
020
MC/DC0%0/2
@bytes == 0 or $bytes[-1] ne "\0"
@bytes == 0 $bytes[-1] ne "\0"
Truth table33%1/3
A or B
A= @bytes == 0B= $bytes[-1] ne "\0"
ABresult
1X1
011
000
137 2 return @bytes;
138 }
139  
140 59 ▶sub uchar { _test_value(uchar => $_[0], qr/\+?\d{1,3}$/) }
Subroutine uchar: called CC=1 SCAR=0.0
Pod documented
141  
142 52 ▶sub uint {
Subroutine uint: called CC=7 SCAR=21.1
Pod documented
143 52 my $obj = $_[0];
144 52 my $uint = _test_value(uint => $obj, qr{^\+?\d{1,10}$});
145 52 my @bytes;
146  
147 52 ▶ while ($uint) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uint) 146 52 198
148 146 my $value = $uint & 0xff;
149 146 $uint >>= 8;
150 146 unshift @bytes, $value;
151 }
152  
153 52 ▶ if (!$obj->{snmp}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($obj->{"snmp"}) 52 0 52
154 52 unshift @bytes, 0 for (1 .. 4 - @bytes);
155 }
156 52 ▶ if (@bytes == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@bytes == 0) 0 52 52
157 0 @bytes = (0);
158 }
159 52 ▶ if ($obj->{snmp}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->{"snmp"}) 0 52 52
160 0 ▶ unshift @bytes, 0 if ($bytes[0] > 0x79);
Branch: 1 branch
BranchTrueFalseTotal
if $bytes[0] > 121 0 0 0
161 }
162  
163 52 return @bytes;
164 }
165  
166 56 ▶sub ushort {
Subroutine ushort: called CC=6 SCAR=18.6
Pod documented
167 56 my $obj = $_[0];
168 56 my $ushort = _test_value(ushort => $obj, qr{^\+?\d{1,5}$});
169 56 my @bytes;
170  
171 56 ▶ unshift @bytes, 0 if $obj->{snmp} and $ushort > 0x79;
Branch: 1 branch
BranchTrueFalseTotal
if $obj->{"snmp"} and $ushort > 121 0 56 56
Condition33%1/3
$obj->{"snmp"} and $ushort > 121
!ll&&!rl&&r
5600
MC/DC0%0/2
$obj->{"snmp"} and $ushort > 121
$obj->{"snmp"} $ushort > 121
Truth table33%1/3
A and B
A= $obj->{"snmp"}B= $ushort > 121
ABresult
0X0
100
111
172  
173 56 ▶ while ($ushort) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ushort) 49 56 105
174 49 my $value = $ushort & 0xff;
175 49 $ushort >>= 8;
176 49 unshift @bytes, $value;
177 }
178  
179 56 ▶ map { unshift @bytes, 0 } 1 .. 2 - @bytes unless $obj->{snmp};
Branch: 1 branch
BranchTrueFalseTotal
unless $obj->{"snmp"} 56 0 56
180 56 ▶ return @bytes ? @bytes : (0);
Branch: 1 branch
BranchTrueFalseTotal
@bytes ? : 56 0 56
181 }
182  
183 1 ▶sub ushort_list {
Subroutine ushort_list: called CC=2 SCAR=7.1
Pod documented
184 1 ▶ map { ushort({value => $_}) } @{$_[0]->{value} || []};
Condition50%1/2
$_[0]{'value'} || []
l!l
10
MC/DC0%0/1
$_[0]{'value'} || []
$_[0]{'value'}
Truth table50%1/2
A
A= $_[0]{'value'}
Aresult
11
00
185 }
186  
187 3 ▶sub vendor {
Subroutine vendor: called CC=2 SCAR=6.9
Pod documented
188 3 my $options = $_[0]->{value}{options};
189 3 my @vendor = ether({value => $_[0]->{value}{id}});
190 3 my @bytes = (8, CORE::int(@vendor), @vendor);
191  
192 3 ▶ for (my $i = 0; $i < @$options; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$options) 7 3 10
193 7 my @value = hexstr({value => $options->[$i + 1]});
194 7 push @bytes, uchar({value => $options->[$i]});
195 7 push @bytes, CORE::int(@value);
196 7 push @bytes, @value;
197 }
198  
199 3 return @bytes;
200 }
201  
202 1 ▶sub vendorspec {
Subroutine vendorspec: called CC=3 SCAR=11.0
Pod documented
203 1 my $obj = $_[0];
204 1 my (@vendor, @bytes);
205  
206 1 ▶ confess "vendor({ nested => ... }) is not an array ref" unless ref $obj->{nested} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $obj->{"nested"} eq "ARRAY" 0 1 1
207  
208 1 @vendor = ether($obj); # will extract value=>$hexstr. might confess
209 1 @bytes = (8, CORE::int(@vendor), @vendor);
210  
211 1 for my $tlv (@{$obj->{nested}}) {
212 1 my @value = hexstr($tlv); # will extract value=>$hexstr. might confess
213 1 push @bytes, uchar({value => $tlv->{type}});
214 1 push @bytes, CORE::int(@value);
215 1 push @bytes, @value;
216 }
217  
218 1 return @bytes;
219 }
220  
221 33 ▶sub _snmp_length {
Subroutine _snmp_length: called CC=5 SCAR=27.8
222 33 my $length = $_[0];
223 33 my @bytes;
224  
225 33 ▶ return $length if $length < 0x80;
Branch: 1 branch
BranchTrueFalseTotal
if $length < 128 33 0 33
226 0 ▶ return 0x81, $length if $length < 0xff;
Branch: 1 branch
BranchTrueFalseTotal
if $length < 255 0 0 0
227 0 ▶ confess "Too long snmp length: ($length)" unless $length < 0xffff;
Branch: 1 branch
BranchTrueFalseTotal
unless $length < 65535 0 0 0
228  
229 0 ▶ while ($length) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length) 0 0 0
230 0 unshift @bytes, $length & 0xff;
231 0 $length >>= 8;
232 }
233  
234 0 return 0x82, @bytes;
235 }
236  
237 11 ▶sub _snmp_oid {
Subroutine _snmp_oid: called CC=7 SCAR=19.9
238 11 my $oid = $_[0];
239 11 my (@encoded_oid, @input_oid);
240 11 my $subid = 0;
241  
242 11 ▶ if ($_[0] =~ /[A-Za-z]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /[A-Za-z]/) 0 11 11
243 0 die "[DOCSIS] Need to install SNMP.pm http://www.net-snmp.org/ to encode non-numberic OID $oid"
244 unless DOCSIS::ConfigFile::CAN_TRANSLATE_OID;
245 0 ▶ $oid = SNMP::translateObj($oid) or confess "Could not translate OID '$_[0]'";
Branch: 1 branch
BranchTrueFalseTotal
$oid = SNMP::translateObj($oid) or confess("Could not translate OID '$_[0]'") 0 0 0
246 }
247  
248 11 @input_oid = split /\./, $oid;
249 11 ▶ shift @input_oid unless length $input_oid[0];
Branch: 1 branch
BranchTrueFalseTotal
unless length $input_oid[0] 0 11 11
250  
251 # the first two sub-id are in the first id
252 {
253 11 my $first = shift @input_oid;
254 11 my $second = shift @input_oid;
255 11 push @encoded_oid, $first * 40 + $second;
256 }
257  
258 SUB_OID:
259 11 for my $id (@input_oid) {
260 113 ▶ if ($id <= 0x7f) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id <= 127) { } 109 4 113
261 109 push @encoded_oid, $id;
262 }
263 else {
264 4 my @suboid;
265  
266 4 ▶ while ($id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id) 8 4 12
267 8 unshift @suboid, 0x80 | ($id & 0x7f);
268 8 $id >>= 7;
269 }
270  
271 4 $suboid[-1] &= 0x7f;
272 4 push @encoded_oid, @suboid;
273 }
274 }
275  
276 11 return @encoded_oid;
277 }
278  
279 243 ▶sub _test_value {
Subroutine _test_value: called CC=4 SCAR=14.6
280 243 my ($name, $obj, $test) = @_;
281  
282 243 ▶ confess "$name({ value => ... }) received undefined value" unless defined $obj->{value};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $obj->{"value"} 0 243 243
283 confess "$name({ value => " . $obj->{value} . " }) does not match $test"
284 243 ▶ if $test and not $obj->{value} =~ $test;
Branch: 1 branch
BranchTrueFalseTotal
if $test and not $obj->{"value"} =~ /$test/ 0 243 243
Condition66%2/3
$test and not $obj->{"value"} =~ /$test/
!ll&&!rl&&r
402030
MC/DC0%0/2
$test and not $obj->{"value"} =~ /$test/
$test not $obj->{"value"} =~ /$test/
Truth table66%2/3
A and not(B)
A= $testB= not $obj->{"value"} =~ /$test/
ABresult
0X0
100
111
285 243 $obj->{value};
286 }
287  
288 17 1;
289  
290 =encoding utf8
291  
292 =head1 NAME
293  
294 DOCSIS::ConfigFile::Encode - Encode functions for a DOCSIS config-file.
295  
296 =head1 DESCRIPTION
297  
298 L<DOCSIS::ConfigFile::Encode> has functions which is used to encode "human"
299 data into list of unsigned characters (0-255) (refered to as "bytes") later in
300 the pod. This list can then be encoded into binary data using:
301  
302 $bytestr = pack 'C*', @uchar;
303  
304 =head1 FUNCTIONS
305  
306 =head2 bigint
307  
308 Returns a list of bytes representing the C<$bigint>. This can be any
309 number (negative or positive) which can be representing using 64 bits.
310  
311 =head2 ether
312  
313 This function use either L</uint> or L</hexstr> to encode the
314 input value. It will figure out the function to use by checking
315 the input for either integer value or a string looking like
316 a hex-string.
317  
318 =head2 hexstr
319  
320 Will encode any hex encoded string into a list of bytes. The string
321 can have an optional leading "0x".
322  
323 =head2 int
324  
325 Returns a list of bytes representing the C<$int>. This can be any
326 number (negative or positive) which can be representing using 32 bits.
327  
328 =head2 ip
329  
330 Returns a list of four bytes representing the C<$ip>. The C<$ip> must
331 be in in the format "1.2.3.4".
332  
333 =head2 objectid
334  
335 Encodes MIB number as value of C<OBJECTID>
336 can be in format: 1.2.3.4, .1.2.3.4
337  
338 =head2 mic
339  
340 Cannot encode CM/CMTS mic without complete information about
341 the config file, so this function returns an empty list.
342  
343 =head2 no_value
344  
345 This method will return an empty list. It is used by DOCSIS types, which
346 has zero length.
347  
348 =head2 snmp_object
349  
350 This function encodes a human-readable SNMP oid into a list of bytes:
351  
352 @bytes = (
353 #-type---length---------value-----type---
354 0x30, $total_length, # object
355 0x06, int(@oid), @oid, # oid
356 $type, int(@value), @value, # value
357 );
358  
359 =head2 string
360  
361 Returns a list of bytes representing the C<$str>. Will use
362 L</hexstr> to decode it if it looks like a hex string (a
363 string starting with leading "0x"). In other cases, it will
364 decode it itself. The input string might also be encoded
365 with a simple uri-encode format: "%20" will be translated
366 to a space, and "%25" will be translated into "%", before
367 encoded using C<ord()>.
368  
369 =head2 stringz
370  
371 Returns a list of bytes representing the C<$str> with a zero
372 terminator at the end. The "\0" byte will be added unless
373 seen as the last element in the list.
374  
375 Only ServiceClassName needs this, see C<$DOCSIS::ConfigFile::TREE> for more
376 details.
377  
378 =head2 uchar
379  
380 Returns a list with one byte representing the C<$uchar>. This can be any
381 positive number which can be representing using 8 bits.
382  
383 =head2 uint
384  
385 Returns a list of bytes representing the C<$uint>. This can be any
386 positive number which can be representing using 32 bits.
387  
388 =head2 ushort
389  
390 Returns a list of bytes representing the C<$ushort>. This can be any
391 positive number which can be representing using 16 bits.
392  
393 =head2 ushort_list
394  
395 Returns a list of bytes representing the C<$ushort>. This can be any
396 positive number which can be representing using 16 bits.
397  
398 =head2 vendor
399  
400 Will byte-encode a complex vendorspec datastructure.
401  
402 =head2 vendorspec
403  
404 Will byte-encode a complex vendorspec datastructure.
405  
406 =head1 SEE ALSO
407  
408 L<DOCSIS::ConfigFile>
409  
410 =cut
« ConfigFile/Decode.pm ↑ Summary