2 # Copyright (C) 2006-2008 Georgia Public Library Service
\r
4 # Author: David J. Fiander
\r
6 # This program is free software; you can redistribute it and/or
\r
7 # modify it under the terms of version 2 of the GNU General Public
\r
8 # License as published by the Free Software Foundation.
\r
10 # This program is distributed in the hope that it will be useful,
\r
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 # GNU General Public License for more details.
\r
15 # You should have received a copy of the GNU General Public
\r
16 # License along with this program; if not, write to the Free
\r
17 # Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
\r
20 package Sip::Checksum;
\r
26 our @ISA = qw(Exporter);
\r
27 our @EXPORT_OK = qw(checksum verify_cksum);
\r
32 return (-unpack('%16C*', $pkt) & 0xFFFF);
\r
40 return 0 if (substr($pkt, -6, 2) ne "AZ"); # No checksum at end
\r
42 # Convert the checksum back to hex and calculate the sum of the
\r
43 # pack without the checksum.
\r
44 $cksum = hex(substr($pkt, -4));
\r
45 $shortsum = unpack("%16U*", substr($pkt, 0, -4));
\r
47 # The checksum is valid if the hex sum, plus the checksum of the
\r
48 # base packet short when truncated to 16 bits.
\r
49 return (($cksum + $shortsum) & 0xFFFF) == 0;
\r
53 no warnings qw(once);
\r
54 eval join('',<main::DATA>) || die $@ unless caller();
\r
59 # Some simple test data
\r
62 my $testpkt = shift;
\r
63 my $cksum = checksum($testpkt);
\r
64 my $fullpkt = sprintf("%s%4X", $testpkt, $cksum);
\r
66 print $fullpkt, "\n";
\r