LonghornPHP 2026

Voting

: min(eight, eight)?
(Example: nine)

The Note You're Voting On

philip at birk-jensen dot dk
20 years ago
I've been using the ICMP Checksum calculation function written by Khaless [at] bigpond [dot] com. But when having an odd length of data, it failed, so I made my own instead, which adds a 0 if the data length is odd:
<?php
function icmpChecksum($data)
{
    // Add a 0 to the end of the data, if it's an "odd length"
    if (strlen($data)%2)
        $data .= "\x00";
    
    // Let PHP do all the dirty work
    $bit = unpack('n*', $data);
    $sum = array_sum($bit);
    
    // Stolen from: Khaless [at] bigpond [dot] com
    // The code from the original ping program:
    //    sum = (sum >> 16) + (sum & 0xffff);    /* add hi 16 to low 16 */
    //    sum += (sum >> 16);            /* add carry */
    // which also works fine, but it seems to me that
    // Khaless will work on large data.
    while ($sum>>16)
        $sum = ($sum >> 16) + ($sum & 0xffff);
    
    return pack('n*', ~$sum);
}
?>

<< Back to user notes page

To Top