Package com.prineside.tdi2.utils
Class CRC
java.lang.Object
com.prineside.tdi2.utils.CRC
This class provides utility functions for CRC calculation using either canonical straight forward approach
 or using "fast" table-driven implementation. Note, that even though table-driven implementation is much faster
 for processing large amounts of data and is commonly referred as fast algorithm, sometimes it might be quicker to
 calculate CRC using canonical algorithm then to prepare the table for table-driven implementation.
 
Using src is easy. Here is an example of calculating CCITT crc in one call using canonical approach.
 
 String data = "123456789";
 long ccittCrc = CRC.calculateCRC(CRC.Parameters.CCITT, data.getBytes());
 System.out.printf("CRC is 0x%04X\n", ccittCrc); // prints "CRC is 0x29B1"
 
 
 For larger data, table driven implementation is faster. Here is how to use it.
 
 String data = "123456789";
 CRC tableDriven = new CRC(CRC.Parameters.XMODEM);
 long xmodemCrc = tableDriven.calculateCRC(data.getBytes());
 System.out.printf("CRC is 0x%04X\n", xmodemCrc); // prints "CRC is 0x31C3"
 
 
 You can also reuse CRC object instance for another crc calculation.
Given that the only state for a CRC calculation is the "intermediate value" and it is stored in your code, you can even use same CRC instance to calculate CRC of multiple data sets in parallel. And if data is too big, you may feed it in chunks
 
 long curValue = tableDriven.init(); // initialize intermediate value
 curValue = tableDriven.update(curValue, "123456789".getBytes()); // feed first chunk
 curValue = tableDriven.update(curValue, "01234567890".getBytes()); // feed next chunk
 long xmodemCrc2 = tableDriven.finalCRC(curValue); // gets CRC of whole data ("12345678901234567890")
 System.out.printf("CRC is 0x%04X\n", xmodemCrc2); // prints "CRC is 0x2C89"
 
 - 
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classParameters represents set of parameters defining a particular CRC algorithm. - 
Constructor Summary
ConstructorsConstructorDescriptionCRC(CRC.Parameters crcParams) Constructs a new CRC processor for table based CRC calculations. - 
Method Summary
Modifier and TypeMethodDescriptionlongcalculateCRC(byte[] data) A convenience method allowing to calculate CRC in one call.longcalculateCRC(byte[] data, int offset, int length) static longcalculateCRC(CRC.Parameters crcParams, byte[] data) This method implements simple straight forward bit by bit calculation.static longcalculateCRC(CRC.Parameters crcParams, byte[] data, int offset, int length) longfinalCRC(long curValue) This method should be called to retrieve actual CRC for the data processed so far.shortfinalCRC16(long curValue) Is a convenience method to spare end users from explicit type conversion every time this package is used.intfinalCRC32(long curValue) Is a convenience method to spare end users from explicit type conversion every time this package is used.bytefinalCRC8(long curValue) Is a convenience method to spare end users from explicit type conversion every time this package is used.longinit()Returns initial value for this CRC intermediate value This method is used when starting a new iterative CRC calculation (using init, update and finalCRC methods, possibly supplying data in chunks).longupdate(long curValue, byte[] chunk) A convenience method for feeding a complete byte array of data.longupdate(long curValue, byte[] chunk, int offset, int length) This method is used to feed data when performing iterative CRC calculation (using init, update and finalCRC methods, possibly supplying data in chunks). 
- 
Constructor Details
- 
CRC
Constructs a new CRC processor for table based CRC calculations. Underneath, it just calls finalCRC() method.- Parameters:
 crcParams- CRC algorithm parameters- Throws:
 RuntimeException- if CRC sum width is not divisible by 8
 
 - 
 - 
Method Details
- 
calculateCRC
This method implements simple straight forward bit by bit calculation. It is relatively slow for large amounts of data, but does not require any preparation steps. As a result, it might be faster in some cases then building a table required for faster calculation. Note: this implementation follows section 8 ("A Straightforward CRC Implementation") of Ross N. Williams paper as even though final/sample implementation of this algorithm provided near the end of that paper (and followed by most other implementations) is a bit faster, it does not work for polynomials shorter then 8 bits.- Parameters:
 crcParams- CRC algorithm parametersdata- data for the CRC calculation- Returns:
 - the CRC value of the data provided
 
 - 
calculateCRC
 - 
init
public long init()Returns initial value for this CRC intermediate value This method is used when starting a new iterative CRC calculation (using init, update and finalCRC methods, possibly supplying data in chunks).- Returns:
 - initial value for this CRC intermediate value
 
 - 
update
public long update(long curValue, byte[] chunk, int offset, int length) This method is used to feed data when performing iterative CRC calculation (using init, update and finalCRC methods, possibly supplying data in chunks). It can be called multiple times per CRC calculation to feed data to be processed in chunks.- Parameters:
 curValue- CRC intermediate value so farchunk- data chunk to b processed by this calloffset- is 0-based offset of the data to be processed in the array suppliedlength- indicates number of bytes to be processed.- Returns:
 - updated intermediate value for this CRC
 
 - 
update
public long update(long curValue, byte[] chunk) A convenience method for feeding a complete byte array of data.- Parameters:
 curValue- CRC intermediate value so farchunk- data chunk to b processed by this call- Returns:
 - updated intermediate value for this CRC
 
 - 
finalCRC
public long finalCRC(long curValue) This method should be called to retrieve actual CRC for the data processed so far.- Parameters:
 curValue- CRC intermediate value so far- Returns:
 - calculated CRC
 
 - 
calculateCRC
public long calculateCRC(byte[] data) A convenience method allowing to calculate CRC in one call.- Parameters:
 data- is data to calculate CRC on- Returns:
 - calculated CRC
 
 - 
calculateCRC
public long calculateCRC(byte[] data, int offset, int length)  - 
finalCRC8
public byte finalCRC8(long curValue) Is a convenience method to spare end users from explicit type conversion every time this package is used. Underneath, it just calls finalCRC() method.- Parameters:
 curValue- current intermediate crc state value- Returns:
 - the final CRC value
 - Throws:
 RuntimeException- if crc being calculated is not 8-bit
 - 
finalCRC16
public short finalCRC16(long curValue) Is a convenience method to spare end users from explicit type conversion every time this package is used. Underneath, it just calls finalCRC() method.- Parameters:
 curValue- current intermediate crc state value- Returns:
 - the final CRC value
 - Throws:
 RuntimeException- if crc being calculated is not 16-bit
 - 
finalCRC32
public int finalCRC32(long curValue) Is a convenience method to spare end users from explicit type conversion every time this package is used. Underneath, it just calls finalCRC() method.- Parameters:
 curValue- current intermediate crc state value- Returns:
 - the final CRC value
 - Throws:
 RuntimeException- if crc being calculated is not 32-bit
 
 -