/
JamCrc.java
65 lines (52 loc) · 1.66 KB
/
JamCrc.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package com.walkertribe.ian.util;
/**
* An implementation of the JamCRC algorithm. These checksums are used to allow
* Artemis to send an int instead of a full string.
* @author rjwut
*/
public final class JamCrc {
private static final int INITIAL_VALUE = 0xffffffff;
private static final int POLYNOMIAL = 0x04c11db7;
private static final int[] TABLE = new int[256];
static {
// pre-populate table
for (int i = 0; i <= 0xff; i++) {
TABLE[i] = reflect(i, (byte) 8) << 24;
for (int j = 0; j < 8; j++) {
TABLE[i] = (TABLE[i] << 1) ^ (((TABLE[i] & (1 << 31)) == 0) ? 0 : POLYNOMIAL);
}
TABLE[i] = reflect(TABLE[i], (byte) 32);
}
}
/**
* Computes a checksum for the given byte array.
*/
public static int compute(byte[] bytes) {
int crc = INITIAL_VALUE;
for (byte b : bytes) {
crc = (crc >>> 8) ^ TABLE[(crc & 0xff) ^ (b & 0xff)];
}
return crc;
}
/**
* Computes a checksum for the UTF-8 representation of the given string.
* Note that even though strings are transmitted in UTF-16LE in the Artemis
* protocol, checksums are computed in UTF-8. Weirdness.
*/
public static int compute(CharSequence str) {
return compute(str.toString().getBytes(Util.UTF8));
}
private static int reflect(int ref, byte ch) {
int value = 0;
for (int i = 1; i < (ch + 1); i++) {
if ((ref & 1) != 0) {
value |= 1 << (ch - i);
}
ref >>>= 1;
}
return value;
}
private JamCrc() {
// prevent instantiation
}
}