Skip to content

Commit

Permalink
Implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
snksoft committed Jun 10, 2016
0 parents commit 2c65ff2
Show file tree
Hide file tree
Showing 6 changed files with 705 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target
24 changes: 24 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Copyright (c) 2016, S&K Software Development Ltd
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the S&K Software Development Ltd nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51 changes: 51 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
This package implements generic CRC calculations up to 64 bits wide.
It aims to be fairly fast and fairly complete, allowing users to match pretty much
any CRC algorithm used in the wild by choosing appropriate Parameters. This obviously
includes all popular CRC algorithms, such as CRC64-ISO, CRC64-ECMA, CRC32, CRC32C, CRC16, CCITT, XMODEM and many others.
See http://reveng.sourceforge.net/crc-catalogue/ for a good list of CRC algorithms and their parameters.

This package has been largely inspired by Ross Williams' 1993 paper "A Painless Guide to CRC Error Detection Algorithms".


## Usage

Using src is easy. Here is an example of calculating CCITT crc.
```java
import com.github.snksoft.crc.CRC;

public class Example
{
public static void main(String [] args) {
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.
```java
import com.github.snksoft.crc.CRC;

public class Example
{
public static void main(String [] args) {
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"
}
}
```
78 changes: 78 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.github.snksoft</groupId>
<artifactId>java-crc</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Generic CRC implementation for java</name>
<url>https://github.com/snksoft/java-crc</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>

<build>
<plugins>
<!-- Mandatory plugins for using Spock -->
<plugin>
<!-- The gmavenplus plugin is used to compile Groovy code. To learn more about this plugin,
visit https://github.com/groovy/GMavenPlus/wiki -->
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- Optional plugins for using Spock -->
<!-- Only required if names of spec classes don't match default Surefire patterns (`*Example` etc.) -->
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.6</version>
<configuration>
<useFile>false</useFile>
<includes>
<include>**/*Spec.java</include>
</includes>
</configuration>
</plugin>
<!-- Only required for spock-example build -->
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.5</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>

<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.4.1</version>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>
<version>1.0-groovy-2.4</version>
<scope>compile</scope>
</dependency>

</dependencies>

</project>

Loading

0 comments on commit 2c65ff2

Please sign in to comment.