1 package C4::SIP::Sip::Checksum;
7 our @ISA = qw(Exporter);
8 our @EXPORT_OK = qw(checksum verify_cksum);
12 return (-unpack('%16C*', $pkt) & 0xFFFF);
20 unless ($pkt =~ /AZ(....)$/) {
21 warn "verify_cksum: no sum detected";
22 return 0; # No checksum at end
24 # return 0 if (substr($pkt, -6, 2) ne "AZ");
26 # Convert the checksum back to hex and calculate the sum of the
27 # pack without the checksum.
29 $shortsum = unpack("%16C*", substr($pkt, 0, -4));
31 # The checksum is valid if the hex sum, plus the checksum of the
32 # base packet short when truncated to 16 bits.
33 return (($cksum + $shortsum) & 0xFFFF) == 0;
40 # Some simple test data
44 my $cksum = checksum($testpkt);
45 my $fullpkt = sprintf("%s%4X", $testpkt, $cksum);