org.jnetpcap.util.checksum
Class Checksum

java.lang.Object
  extended by org.jnetpcap.util.checksum.Checksum

public class Checksum
extends java.lang.Object

Main base and utility class that provides native methods for calculating various CRC on buffers.

Author:
Mark Bednarczyk, Sly Technologies, Inc.

Constructor Summary
Checksum()
           
 
Method Summary
static int crc16CCITT(JBuffer buffer, int offset, int length)
          Calculate CCITT CRC16 checksum using a CRC32 CCITT seed.
static int crc16CCITTContinue(JBuffer buffer, int offset, int length, int crc)
          Calculate CCITT 16-bit checksum using a partially calculated CRC16.
static int crc16CCITTSeed(JBuffer buffer, int offset, int length, int seed)
          Calculate CCITT 16-bit checksum using a custom seed.
static int crc16X25CCITT(JBuffer buffer, int offset, int length)
          Calculate CCITT CRC16 X.25 checksum using a CCITT seed.
static int crc32c(JBuffer buffer, int offset, int length, int crc)
          Calculate a standard CRC32C checksum using a custom seed.
static long crc32CCITT(JBuffer buffer, int offset, int length)
          Calculate CCITT CRC32 checksum using a CRC32 CCITT seed.
static int crc32CCITTContinue(JBuffer buffer, int offset, int length, int crc)
          Calculate a standard CRC32C checksum using a partially calculated CRC32.
static int crc32CCITTSeed(JBuffer buffer, int offset, int length, int seed)
          Calculate CCITT CRC32 checksum using a custom seed.
static long crc32IEEE802(JBuffer buffer, int offset, int length)
          Calculates IEEE 802 based checksums including ethernet/802.3.
static long flip(long c)
          Flips the bytes from LITTLE to BIG ENDIAN.
static int icmp(JBuffer buffer, int ipOffset, int icmpOffset)
          Icmp.
static int inChecksum(JBuffer buffer, int offset, int length)
          Calculate a CRC16 using one's complement of one's complement algorithm.
static int inChecksumShouldBe(int checksum, int calculateChecksum)
          Computes what the checksum should be based on calculated checksum and the checksum in the header's checksum field.
static int pseudoTcp(JBuffer buffer, int ipOffset, int tcpOffset)
          Pseudo tcp.
static int pseudoUdp(JBuffer buffer, int ipOffset, int udpOffset)
          Pseudo udp.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Checksum

public Checksum()
Method Detail

crc16CCITT

public static int crc16CCITT(JBuffer buffer,
                             int offset,
                             int length)
Calculate CCITT CRC16 checksum using a CRC32 CCITT seed.

Parameters:
buffer - buffer to calculate crc on
offset - offset into the buffer
length - length within the buffer
Returns:
calculated crc

crc16CCITTContinue

public static int crc16CCITTContinue(JBuffer buffer,
                                     int offset,
                                     int length,
                                     int crc)
Calculate CCITT 16-bit checksum using a partially calculated CRC16.

Parameters:
buffer - buffer to calculate crc on
offset - offset into the buffer
length - length within the buffer
crc - the preload value for the CRC16 computation
Returns:
calculated crc

crc16CCITTSeed

public static int crc16CCITTSeed(JBuffer buffer,
                                 int offset,
                                 int length,
                                 int seed)
Calculate CCITT 16-bit checksum using a custom seed.

Parameters:
buffer - buffer to calculate crc on
offset - offset into the buffer
length - length within the buffer
seed - starting seed
Returns:
calculated crc

crc16X25CCITT

public static int crc16X25CCITT(JBuffer buffer,
                                int offset,
                                int length)
Calculate CCITT CRC16 X.25 checksum using a CCITT seed.

Parameters:
buffer - buffer to calculate crc on
offset - offset into the buffer
length - length within the buffer
Returns:
calculated crc

crc32c

public static int crc32c(JBuffer buffer,
                         int offset,
                         int length,
                         int crc)
Calculate a standard CRC32C checksum using a custom seed.

Parameters:
buffer - buffer to calculate crc on
offset - offset into the buffer
length - length within the buffer
crc - the preload value for the CRC32C computation
Returns:
calculated crc

crc32CCITT

public static long crc32CCITT(JBuffer buffer,
                              int offset,
                              int length)
Calculate CCITT CRC32 checksum using a CRC32 CCITT seed.

Parameters:
buffer - buffer to calculate crc on
offset - offset into the buffer
length - length within the buffer
Returns:
calculated crc

crc32CCITTContinue

public static int crc32CCITTContinue(JBuffer buffer,
                                     int offset,
                                     int length,
                                     int crc)
Calculate a standard CRC32C checksum using a partially calculated CRC32.

Parameters:
buffer - buffer to calculate crc on
offset - offset into the buffer
length - length within the buffer
crc - the preload value for the CRC32 computation
Returns:
calculated crc

crc32CCITTSeed

public static int crc32CCITTSeed(JBuffer buffer,
                                 int offset,
                                 int length,
                                 int seed)
Calculate CCITT CRC32 checksum using a custom seed.

Parameters:
buffer - buffer to calculate crc on
offset - offset into the buffer
length - length within the buffer
seed - starting seed
Returns:
calculated crc

crc32IEEE802

public static long crc32IEEE802(JBuffer buffer,
                                int offset,
                                int length)
Calculates IEEE 802 based checksums including ethernet/802.3.

Parameters:
buffer - buffer to calculate for
offset - offset into the buffer in bytes
length - number of bytes to run calculation on
Returns:
calculated checksum

flip

public static long flip(long c)
Flips the bytes from LITTLE to BIG ENDIAN. For example 0x01020304 becomes 0x04030201.

Parameters:
c - source value
Returns:
converted value

icmp

public static int icmp(JBuffer buffer,
                       int ipOffset,
                       int icmpOffset)
Icmp.

Parameters:
buffer - the buffer
ipOffset - the ip offset
icmpOffset - the icmp offset
Returns:
the int

inChecksum

public static int inChecksum(JBuffer buffer,
                             int offset,
                             int length)
Calculate a CRC16 using one's complement of one's complement algorithm. This method computes the CRC16 on a single buffer chunk.

Parameters:
buffer - buffer to reach the chunk of data
offset - offset into the buffer
length - number of bytes to include in calculation
Returns:
computed CRC16

inChecksumShouldBe

public static int inChecksumShouldBe(int checksum,
                                     int calculateChecksum)
Computes what the checksum should be based on calculated checksum and the checksum in the header's checksum field.

Parameters:
checksum - checksum within the header's field
calculateChecksum - checksum that was calculated
Returns:
resulting checksum of the combination of the 2

pseudoTcp

public static int pseudoTcp(JBuffer buffer,
                            int ipOffset,
                            int tcpOffset)
Pseudo tcp.

Parameters:
buffer - the buffer
ipOffset - the ip offset
tcpOffset - the tcp offset
Returns:
the int

pseudoUdp

public static int pseudoUdp(JBuffer buffer,
                            int ipOffset,
                            int udpOffset)
Pseudo udp.

Parameters:
buffer - the buffer
ipOffset - the ip offset
udpOffset - the udp offset
Returns:
the int