Just noticed this...
In both the original, and my version, there seems to be some wasted logic. See comments that start with ## below.
if (length($payload)%2) { my $padded = substr $payload, 0, -1; $padded .= "\0"; $padded .= substr $payload, -1; ## above: ok, so the next to the last byte ## is guaranteed to be null since we just ## explicitly set it if (ord(substr $padded, -1) >= 128) { my $end = ord(substr $padded, -2, 1); ## ## above: now we're grabbing the next to the last ## byte, even though we already know it's a null? ## my $endend = ord(substr $padded, -1); $end--; ## now we decrement it, knowing full well it's ## going to be -1? $endend--; ###DEBUG: printf "CMPSAT = %i %i\n", $end, $endend; # if 0 before --, then -1 results in warning to pack below # -1 is in essense \xff or 255 if ($end == -1) { $end = 255 } ## ## above,of course, it's going to == -1, and thus ## at this point, always reset to 255 ## if ($endend == -1) { $endend = 255 } $padded = substr $padded, 0, -2; $padded .= CORE::pack('CC', $end, $endend); } $payload = $padded; }
It would seem then, that you could simplify all of that out, and just decrement the last byte by one?
sub prepchksum { my ($payload) = @_; printf "BEFORE = %s\n", (CORE::unpack "H*", $payload); if (length($payload)%2) { $payload =~ s#(.)$#\0$1#; printf "MID1 = %s\n", (CORE::unpack "H*", $payload); # Compensate off-by-one error if (ord(substr $payload, -1) >= 128) { $payload=~s#(.)$#pack('C',(ord($2)-1)&255)#e; } } printf "AFTER = %s\n", (CORE::unpack "H*", $payload); }
Or perhaps there's a bug somewhere, either in the 'C' implementation you based this on, or your original port?

In reply to Re^2: Compensate for bad inet checksum routine by kschwab
in thread Compensate for bad inet checksum routine by VinsWorldcom

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.