Class CrcAlgorithmImpl

java.lang.Object
org.refcodes.numerical.CrcAlgorithmImpl
All Implemented Interfaces:
org.refcodes.mixin.NameAccessor, CrcAlgorithm, CrcWidthAccessor

public class CrcAlgorithmImpl extends Object implements CrcAlgorithm
Implementation of the CrcAlgorithm interface. Credits to [Anton Isakov (http://crccalc.com) of the "Online CRC-8 CRC-16 CRC-32 Calculator" (https://crccalc.com)'s GitHub (https://github.com/meetanthony/crcjava.git) repository.
  • Nested Class Summary

    Nested classes/interfaces inherited from interface org.refcodes.mixin.NameAccessor

    org.refcodes.mixin.NameAccessor.NameBuilder<B extends org.refcodes.mixin.NameAccessor.NameBuilder<B>>, org.refcodes.mixin.NameAccessor.NameMutator, org.refcodes.mixin.NameAccessor.NameProperty
  • Constructor Summary

    Constructors
    Constructor
    Description
    CrcAlgorithmImpl(String aName, int aCrcBitWidth, long aPolynomial, long aInitCrc, boolean aRefIn, boolean aRefOut, long aXorOut)
    Constructs a CrcAlgorithm instance.
  • Method Summary

    Modifier and Type
    Method
    Description
    Returns the CrcSize (describing the checksum's "CRC byte length" for this CrcAlgorithmConfig.
    int
    Retrieves the value from the CRC byte width (number of bytes used to store a CRC checksum) property.
    byte[]
    toCrcBytes(byte[] aData, int aOffset, int aLength, Endianess aEndianess)
    Calculates the according checksum from the supplied data data and returns the according byte array of the CRC checksum.
    byte[]
    toCrcBytes(byte[] aData, Endianess aEndianess)
    Calculates the according checksum from the supplied data data and returns the according byte array of the CRC checksum.
    byte[]
    toCrcBytes(byte aData, Endianess aEndianess)
    Calculates the according checksum from the supplied data and returns the according byte array of the CRC checksum.
    byte[]
    toCrcBytes(long aCrc, byte[] aData, int aOffset, int aLength, Endianess aEndianess)
    Updates the according checksum with he supplied data data and returns the according byte array of the CRC checksum.
    byte[]
    toCrcBytes(long aCrc, byte[] aData, Endianess aEndianess)
    Updates the according checksum with he supplied data data and returns the according byte array of the CRC checksum.
    byte[]
    toCrcBytes(long aCrc, byte aData, Endianess aEndianess)
    Updates the according checksum with he supplied data data and returns the according byte array of the CRC checksum.
    long
    toCrcChecksum(byte aData)
    Calculates the according checksum from the supplied data.
    long
    toCrcChecksum(byte[] aData)
    Calculates the according checksum from the supplied data.
    long
    toCrcChecksum(byte[] aData, int aOffset, int aLength)
    Calculates the according checksum from the supplied data.
    long
    toCrcChecksum(long aCrc, byte aData)
    Updates the according checksum with he supplied data.
    long
    toCrcChecksum(long aCrc, byte[] aData)
    Updates the according checksum with he supplied data.
    long
    toCrcChecksum(long aCrc, byte[] aData, int aOffset, int aLength)
    Updates the according checksum with he supplied data.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • CrcAlgorithmImpl

      public CrcAlgorithmImpl(String aName, int aCrcBitWidth, long aPolynomial, long aInitCrc, boolean aRefIn, boolean aRefOut, long aXorOut)
      Constructs a CrcAlgorithm instance. For sound init arguments please use predefined CrcAlgorithm instances defined in the CrcAlgorithmConfig enumeration.
      Parameters:
      aName - The descriptive name of the algorithm.
      aCrcBitWidth - "This is hash size." [Anton Isakov http://crccalc.com]
      aPolynomial - "This parameter is the poly. This is a binary value that should be specified as a hexadecimal number.The top bit of the poly should be omitted.For example, if the poly is 10110, you should specify 06. An important aspect of this parameter is that it represents the unreflected poly; the bottom bit of this parameter is always the LSB of the divisor during the division regardless of whether the algorithm being modelled is reflected." [Anton Isakov http://crccalc.com]
      aInitCrc - "This parameter specifies the initial value of the register when the algorithm starts.This is the value that is to be assigned to the register in the direct table algorithm. In the table algorithm, we may think of the register always commencing with the value zero, and this value being XORed into the register after the N'th bit iteration. This parameter should be specified as a hexadecimal number." [Anton Isakov http://crccalc.com]
      aRefIn - "This parameter is the poly. This is a binary value that should be specified as a hexadecimal number.The top bit of the poly should be omitted.For example, if the poly is 10110, you should specify 06. An important aspect of this parameter is that it represents the unreflected poly; the bottom bit of this parameter is always the LSB of the divisor during the division regardless of whether the algorithm being modelled is reflected." [Anton Isakov http://crccalc.com]
      aRefOut - "This is a boolean parameter. If it is set to FALSE, the final value in the register is fed into the XOROUT stage directly, otherwise, if this parameter is TRUE, the final register value is reflected first." [Anton Isakov http://crccalc.com]
      aXorOut - "This is an W-bit value that should be specified as a hexadecimal number.It is XORed to the final register value (after the REFOUT) stage before the value is returned as the official checksum." [Anton Isakov http://crccalc.com]
  • Method Details

    • toCrcChecksum

      public long toCrcChecksum(byte aData)
      Calculates the according checksum from the supplied data.
      Specified by:
      toCrcChecksum in interface CrcAlgorithm
      Parameters:
      aData - The data for which to calculate the checksum.
      Returns:
      The according checksum.
    • toCrcChecksum

      public long toCrcChecksum(long aCrc, byte aData)
      Updates the according checksum with he supplied data.
      Specified by:
      toCrcChecksum in interface CrcAlgorithm
      Parameters:
      aCrc - The CRC checksum to be updated.
      aData - The data for which to calculate the checksum.
      Returns:
      The accordingly updated checksum.
    • toCrcChecksum

      public long toCrcChecksum(byte[] aData)
      Calculates the according checksum from the supplied data.
      Specified by:
      toCrcChecksum in interface CrcAlgorithm
      Parameters:
      aData - The data for which to calculate the checksum.
      Returns:
      The according checksum.
    • toCrcChecksum

      public long toCrcChecksum(long aCrc, byte[] aData)
      Updates the according checksum with he supplied data.
      Specified by:
      toCrcChecksum in interface CrcAlgorithm
      Parameters:
      aCrc - The CRC checksum to be updated.
      aData - The data for which to calculate the checksum.
      Returns:
      The accordingly updated checksum.
    • toCrcChecksum

      public long toCrcChecksum(byte[] aData, int aOffset, int aLength)
      Calculates the according checksum from the supplied data.
      Specified by:
      toCrcChecksum in interface CrcAlgorithm
      Parameters:
      aData - The data for which to calculate the checksum.
      aOffset - The offset from where to start.
      aLength - The number of bytes to cover.
      Returns:
      The according checksum.
    • toCrcChecksum

      public long toCrcChecksum(long aCrc, byte[] aData, int aOffset, int aLength)
      Updates the according checksum with he supplied data.
      Specified by:
      toCrcChecksum in interface CrcAlgorithm
      Parameters:
      aCrc - The CRC checksum to be updated.
      aData - The data for which to calculate the checksum.
      aOffset - The offset from where to start.
      aLength - The number of bytes to cover.
      Returns:
      The accordingly updated checksum.
    • toCrcBytes

      public byte[] toCrcBytes(byte aData, Endianess aEndianess)
      Calculates the according checksum from the supplied data and returns the according byte array of the CRC checksum.
      Specified by:
      toCrcBytes in interface CrcAlgorithm
      Parameters:
      aData - The data for which to calculate the checksum.
      aEndianess - The Endianess (big endian or little endian) of the resulting bytes representing the CRC checksum.
      Returns:
      The according checksum.
    • toCrcBytes

      public byte[] toCrcBytes(long aCrc, byte aData, Endianess aEndianess)
      Updates the according checksum with he supplied data data and returns the according byte array of the CRC checksum.
      Specified by:
      toCrcBytes in interface CrcAlgorithm
      Parameters:
      aCrc - The CRC checksum to be updated.
      aData - The data for which to calculate the checksum.
      aEndianess - The Endianess (big endian or little endian) of the resulting bytes representing the CRC checksum.
      Returns:
      The accordingly updated checksum.
    • toCrcBytes

      public byte[] toCrcBytes(byte[] aData, Endianess aEndianess)
      Calculates the according checksum from the supplied data data and returns the according byte array of the CRC checksum.
      Specified by:
      toCrcBytes in interface CrcAlgorithm
      Parameters:
      aData - The data for which to calculate the checksum.
      aEndianess - The Endianess (big endian or little endian) of the resulting bytes representing the CRC checksum.
      Returns:
      The according checksum.
    • toCrcBytes

      public byte[] toCrcBytes(long aCrc, byte[] aData, Endianess aEndianess)
      Updates the according checksum with he supplied data data and returns the according byte array of the CRC checksum.
      Specified by:
      toCrcBytes in interface CrcAlgorithm
      Parameters:
      aCrc - The CRC checksum to be updated.
      aData - The data for which to calculate the checksum.
      aEndianess - The Endianess (big endian or little endian) of the resulting bytes representing the CRC checksum.
      Returns:
      The accordingly updated checksum.
    • toCrcBytes

      public byte[] toCrcBytes(byte[] aData, int aOffset, int aLength, Endianess aEndianess)
      Calculates the according checksum from the supplied data data and returns the according byte array of the CRC checksum.
      Specified by:
      toCrcBytes in interface CrcAlgorithm
      Parameters:
      aData - The data for which to calculate the checksum.
      aOffset - The offset from where to start.
      aLength - The number of bytes to cover.
      aEndianess - The Endianess (big endian or little endian) of the resulting bytes representing the CRC checksum.
      Returns:
      The according checksum.
    • toCrcBytes

      public byte[] toCrcBytes(long aCrc, byte[] aData, int aOffset, int aLength, Endianess aEndianess)
      Updates the according checksum with he supplied data data and returns the according byte array of the CRC checksum.
      Specified by:
      toCrcBytes in interface CrcAlgorithm
      Parameters:
      aCrc - The CRC checksum to be updated.
      aData - The data for which to calculate the checksum.
      aOffset - The offset from where to start.
      aLength - The number of bytes to cover.
      aEndianess - The Endianess (big endian or little endian) of the resulting bytes representing the CRC checksum.
      Returns:
      The accordingly updated checksum.
    • getCrcWidth

      public int getCrcWidth()
      Retrieves the value from the CRC byte width (number of bytes used to store a CRC checksum) property.
      Specified by:
      getCrcWidth in interface CrcWidthAccessor
      Returns:
      The name stored by the CRC byte width (number of bytes used to store a CRC checksum) property.
    • getCrcSize

      public CrcSize getCrcSize()
      Returns the CrcSize (describing the checksum's "CRC byte length" for this CrcAlgorithmConfig.
      Specified by:
      getCrcSize in interface CrcAlgorithm
      Returns:
      The according CrcSize.
    • getName

      public String getName()
      Specified by:
      getName in interface org.refcodes.mixin.NameAccessor