File: | C4/SIP/Sip/Checksum.pm |
Coverage: | 97.1% |
line | stmt | bran | cond | sub | time | code |
---|---|---|---|---|---|---|
1 | package Sip::Checksum; | |||||
2 | ||||||
3 | 2 2 2 | 8 4 128 | use Exporter; | |||
4 | 2 2 2 | 10 2 48 | use strict; | |||
5 | 2 2 2 | 8 18 704 | use warnings; | |||
6 | ||||||
7 | our @ISA = qw(Exporter); | |||||
8 | our @EXPORT_OK = qw(checksum verify_cksum); | |||||
9 | our $debug = 0; | |||||
10 | ||||||
11 | sub checksum { | |||||
12 | 6 | 5270 | my $pkt = shift; | |||
13 | 6 | 45 | return (-unpack('%16C*', $pkt) & 0xFFFF); | |||
14 | } | |||||
15 | ||||||
16 | sub verify_cksum { | |||||
17 | 4 | 25 | my $pkt = shift; | |||
18 | 4 | 4 | my $cksum; | |||
19 | 4 | 6 | my $shortsum; | |||
20 | ||||||
21 | 4 | 31 | if ($pkt =~ /AZ(....)$/) { | |||
22 | 2 | 8 | $debug and warn "verify_cksum: sum ($1) detected"; | |||
23 | } else { | |||||
24 | 2 | 184 | warn "verify_cksum: no sum detected"; | |||
25 | 2 | 18 | return 0; # No checksum at end | |||
26 | } | |||||
27 | # return 0 if (substr($pkt, -6, 2) ne "AZ"); | |||||
28 | ||||||
29 | # Convert the checksum back to hex and calculate the sum of the | |||||
30 | # pack without the checksum. | |||||
31 | 2 | 59 | $cksum = hex($1); | |||
32 | 2 | 7 | $shortsum = unpack("%16C*", substr($pkt, 0, -4)); | |||
33 | ||||||
34 | # The checksum is valid if the hex sum, plus the checksum of the | |||||
35 | # base packet short when truncated to 16 bits. | |||||
36 | 2 | 39 | return (($cksum + $shortsum) & 0xFFFF) == 0; | |||
37 | } | |||||
38 | ||||||
39 | { | |||||
40 | 2 2 2 | 11 2 147 | no warnings qw(once); | |||
41 | eval join('',<main::DATA>) || die $@ unless caller(); | |||||
42 | # FIXME: what the heck is this? | |||||
43 | } | |||||
44 | ||||||
45 | 1; |