Java Checksum Calculate checksum(byte[] message, int offset, int count)

Here you can find the source of checksum(byte[] message, int offset, int count)

Description

Standard internet checksum algorithm shared by IP, ICMP, UDP and TCP.

License

Open Source License

Declaration


public static short checksum(byte[] message, int offset, int count) 

Method Source Code

//package com.java2s;
/*//from   w w  w . j a v a 2s. c o  m
 * JSocket Wrench
 * 
 * Copyright (C) act365.com October 2003
 * 
 * Web site: http://www.act365.com/wrench
 * E-mail: developers@act365.com
 * 
 * The JSocket Wrench library adds support for low-level Internet protocols
 * to the Java programming language.
 * 
 * This program is free software; you can redistribute it and/or modify it 
 * under the terms of the GNU General Public License as published by the Free 
 * Software Foundation; either version 2 of the License, or (at your option) 
 * any later version.
 *  
 * This program is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 
 * Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with 
 * this program; if not, write to the Free Software Foundation, Inc., 
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

public class Main {
    /**
     Standard internet checksum algorithm shared by IP, ICMP, UDP and TCP.
    */

    public static short checksum(byte[] message, int offset, int count) {

        // Sum consecutive 16-bit words.

        int sum = 0, cursor = 0;

        while (cursor < count - 1) {

            sum += (int) integralFromBytes(message, offset + cursor, 2);

            cursor += 2;
        }

        if (cursor == count - 1) {

            sum += (message[offset + cursor] >= 0 ? message[offset + cursor]
                    : message[offset + cursor] ^ 0xffffff00) << 8;
        }

        // Add upper 16 bits to lower 16 bits.

        sum = (sum >>> 16) + (sum & 0xffff);

        // Add carry

        sum += sum >>> 16;

        // Ones complement and truncate.

        return (short) ~sum;
    }

    /**
     Specific checksum calculation used for the UDP and TCP pseudo-header.
    */

    public static short checksum(byte[] source, byte[] destination, byte protocol, byte[] message, int offset,
            int count) {

        int bufferlength = count + 12;

        boolean odd = count % 2 == 1;

        if (odd) {
            ++bufferlength;
        }

        byte[] buffer = new byte[bufferlength];

        buffer[0] = source[0];
        buffer[1] = source[1];
        buffer[2] = source[2];
        buffer[3] = source[3];

        buffer[4] = destination[0];
        buffer[5] = destination[1];
        buffer[6] = destination[2];
        buffer[7] = destination[3];

        buffer[8] = (byte) 0;
        buffer[9] = protocol;

        shortToBytes((short) count, buffer, 10);

        int i = 11;

        while (++i < count + 12) {
            buffer[i] = message[i + offset - 12];
        }

        if (odd) {
            buffer[i] = (byte) 0;
        }

        return checksum(buffer, 0, buffer.length);
    }

    /**
     Forms an integral type from consecutive bytes in a buffer
    */

    static long integralFromBytes(byte[] buffer, int offset, int length) {

        long answer = 0;

        while (--length >= 0) {
            answer = answer << 8;
            answer |= buffer[offset] >= 0 ? buffer[offset] : 0xffffff00 ^ buffer[offset];
            ++offset;
        }

        return answer;
    }

    /**
     Writes a short into a buffer.
    */

    public static int shortToBytes(short value, byte[] buffer, int offset) {
        buffer[offset + 1] = (byte) (value & 0xff);
        value = (short) (value >> 8);
        buffer[offset] = (byte) (value);

        return offset + 2;
    }
}

Related

  1. checksum(byte current, final byte[] data, final int offset, final int length)
  2. checkSum(byte value)
  3. checkSum(byte[] b, int offset, int length)
  4. checksum(byte[] buf, int off, int len)
  5. checksum(byte[] data)
  6. checksum(File file)
  7. checksum(File file)
  8. checksum(InputStream is)
  9. checksum(InputStream is)