Skip to content

Commit 2c65ff2

Browse files
committed
Implemented
0 parents commit 2c65ff2

File tree

6 files changed

+705
-0
lines changed

6 files changed

+705
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
target

LICENSE

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Copyright (c) 2016, S&K Software Development Ltd
2+
All rights reserved.
3+
4+
Redistribution and use in source and binary forms, with or without
5+
modification, are permitted provided that the following conditions are met:
6+
* Redistributions of source code must retain the above copyright
7+
notice, this list of conditions and the following disclaimer.
8+
* Redistributions in binary form must reproduce the above copyright
9+
notice, this list of conditions and the following disclaimer in the
10+
documentation and/or other materials provided with the distribution.
11+
* Neither the name of the S&K Software Development Ltd nor the
12+
names of its contributors may be used to endorse or promote products
13+
derived from this software without specific prior written permission.
14+
15+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18+
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
19+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
This package implements generic CRC calculations up to 64 bits wide.
2+
It aims to be fairly fast and fairly complete, allowing users to match pretty much
3+
any CRC algorithm used in the wild by choosing appropriate Parameters. This obviously
4+
includes all popular CRC algorithms, such as CRC64-ISO, CRC64-ECMA, CRC32, CRC32C, CRC16, CCITT, XMODEM and many others.
5+
See http://reveng.sourceforge.net/crc-catalogue/ for a good list of CRC algorithms and their parameters.
6+
7+
This package has been largely inspired by Ross Williams' 1993 paper "A Painless Guide to CRC Error Detection Algorithms".
8+
9+
10+
## Usage
11+
12+
Using src is easy. Here is an example of calculating CCITT crc.
13+
```java
14+
import com.github.snksoft.crc.CRC;
15+
16+
public class Example
17+
{
18+
public static void main(String [] args) {
19+
String data = "123456789";
20+
long ccittCrc = CRC.calculateCRC(CRC.Parameters.CCITT, data.getBytes());
21+
System.out.printf("CRC is 0x%04X\n", ccittCrc); // prints "CRC is 0x29B1"
22+
23+
}
24+
}
25+
```
26+
27+
For larger data, table driven implementation is faster. Here is how to use it.
28+
```java
29+
import com.github.snksoft.crc.CRC;
30+
31+
public class Example
32+
{
33+
public static void main(String [] args) {
34+
String data = "123456789";
35+
CRC tableDriven = new CRC(CRC.Parameters.XMODEM);
36+
long xmodemCrc = tableDriven.calculateCRC(data.getBytes());
37+
System.out.printf("CRC is 0x%04X\n", xmodemCrc); // prints "CRC is 0x31C3"
38+
39+
// You can also reuse CRC object instance for another crc calculation.
40+
// Given that the only state for a CRC calculation is the "intermediate value"
41+
// and it is stored in your code, you can even use same CRC instance to calculate CRC
42+
// of multiple data sets in parallel.
43+
// And if data is too big, you may feed it in chunks
44+
long curValue = tableDriven.init(); // initialize intermediate value
45+
curValue = tableDriven.update(curValue, "123456789".getBytes()); // feed first chunk
46+
curValue = tableDriven.update(curValue, "01234567890".getBytes()); // feed next chunk
47+
long xmodemCrc2 = tableDriven.finalCRC(curValue); // gets CRC of whole data ("12345678901234567890")
48+
System.out.printf("CRC is 0x%04X\n", xmodemCrc2); // prints "CRC is 0x2C89"
49+
}
50+
}
51+
```

pom.xml

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>com.github.snksoft</groupId>
6+
<artifactId>java-crc</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>Generic CRC implementation for java</name>
11+
<url>https://github.com/snksoft/java-crc</url>
12+
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
16+
</properties>
17+
18+
<build>
19+
<plugins>
20+
<!-- Mandatory plugins for using Spock -->
21+
<plugin>
22+
<!-- The gmavenplus plugin is used to compile Groovy code. To learn more about this plugin,
23+
visit https://github.com/groovy/GMavenPlus/wiki -->
24+
<groupId>org.codehaus.gmavenplus</groupId>
25+
<artifactId>gmavenplus-plugin</artifactId>
26+
<version>1.4</version>
27+
<executions>
28+
<execution>
29+
<goals>
30+
<goal>compile</goal>
31+
<goal>testCompile</goal>
32+
</goals>
33+
</execution>
34+
</executions>
35+
</plugin>
36+
<!-- Optional plugins for using Spock -->
37+
<!-- Only required if names of spec classes don't match default Surefire patterns (`*Example` etc.) -->
38+
<plugin>
39+
<artifactId>maven-surefire-plugin</artifactId>
40+
<version>2.6</version>
41+
<configuration>
42+
<useFile>false</useFile>
43+
<includes>
44+
<include>**/*Spec.java</include>
45+
</includes>
46+
</configuration>
47+
</plugin>
48+
<!-- Only required for spock-example build -->
49+
<plugin>
50+
<artifactId>maven-deploy-plugin</artifactId>
51+
<version>2.5</version>
52+
<configuration>
53+
<skip>true</skip>
54+
</configuration>
55+
</plugin>
56+
</plugins>
57+
</build>
58+
59+
<dependencies>
60+
61+
<dependency>
62+
<groupId>org.codehaus.groovy</groupId>
63+
<artifactId>groovy-all</artifactId>
64+
<version>2.4.1</version>
65+
<scope>compile</scope>
66+
</dependency>
67+
68+
<dependency>
69+
<groupId>org.spockframework</groupId>
70+
<artifactId>spock-core</artifactId>
71+
<version>1.0-groovy-2.4</version>
72+
<scope>compile</scope>
73+
</dependency>
74+
75+
</dependencies>
76+
77+
</project>
78+

0 commit comments

Comments
 (0)