/[mdap]/protocol/calc.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /protocol/calc.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 13 - (hide annotations)
Sun Apr 22 22:56:35 2007 UTC (17 years ago) by dpavlin
File MIME type: text/plain
File size: 650 byte(s)
small and hopefully efficiant checksummer
1 dpavlin 11 #!/usr/bin/perl -w
2    
3     # calc.pl
4     #
5     # 04/22/07 14:29:00 CEST Dobrica Pavlinusic <dpavlin@rot13.org>
6    
7     use strict;
8     use File::Slurp;
9    
10     my $file = shift @ARGV || die "usage: $0 packet\n";
11    
12     my $p = read_file( $file ) || die "can't read $file: $!";
13    
14 dpavlin 12 my $orig = $1 if ( $p =~ s/(..)$//s );
15 dpavlin 11
16     print "ORIGINAL checksum: $orig\n";
17    
18     my $sum = 0;
19     for my $i ( 0 .. length($p) ) {
20     my $c = substr($p,$i,1);
21     my $o = ord($c);
22 dpavlin 12 my $h = sprintf('%02x',$o);
23 dpavlin 11 # $sum += $o;
24     $sum ^= $o;
25 dpavlin 12 printf("# %-2d '%s' %2d %02x -- %2d %02d\n", $i,$c,$o,$o,$sum,$sum);
26 dpavlin 11 }
27    
28 dpavlin 13 my $xor = 0;
29     map { $xor ^= ord($_) } split(//,$p);
30    
31     printf("checksum: %s == %02x [xor=%02x]\n", $orig, $sum, $xor);

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26