| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
package org.tukaani.xz.check; |
| 7 |
|
| 8 |
import org.tukaani.xz.common.ByteArrayView; |
| 9 |
|
| 10 |
public class CRC64 extends Check { |
| 11 |
private static final long[][] TABLE = new long[4][256]; |
| 12 |
|
| 13 |
static { |
| 14 |
final long poly64 = 0xC96C5795D7870F42L; |
| 15 |
|
| 16 |
for (int s = 0; s < 4; ++s) { |
| 17 |
for (int b = 0; b < 256; ++b) { |
| 18 |
long r = s == 0 ? b : TABLE[s - 1][b]; |
| 19 |
for (int i = 0; i < 8; ++i) { |
| 20 |
if ((r & 1) == 1) { |
| 21 |
r = (r >>> 1) ^ poly64; |
| 22 |
} else { |
| 23 |
r >>>= 1; |
| 24 |
} |
| 25 |
} |
| 26 |
TABLE[s][b] = r; |
| 27 |
} |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
private long crc = -1; |
| 32 |
|
| 33 |
public CRC64() { |
| 34 |
size = 8; |
| 35 |
name = "CRC64"; |
| 36 |
} |
| 37 |
|
| 38 |
@Override |
| 39 |
public void update(byte[] buf, int off, int len) { |
| 40 |
final int end = off + len; |
| 41 |
int i = off; |
| 42 |
|
| 43 |
for (int end4 = end - 3; i < end4; i += 4) { |
| 44 |
final int tmp = (int)crc; |
| 45 |
crc = TABLE[3][(tmp & 0xFF) ^ (buf[i] & 0xFF)] ^ |
| 46 |
TABLE[2][((tmp >>> 8) & 0xFF) ^ (buf[i + 1] & 0xFF)] ^ |
| 47 |
(crc >>> 32) ^ |
| 48 |
TABLE[1][((tmp >>> 16) & 0xFF) ^ (buf[i + 2] & 0xFF)] ^ |
| 49 |
TABLE[0][((tmp >>> 24) & 0xFF) ^ (buf[i + 3] & 0xFF)]; |
| 50 |
} |
| 51 |
|
| 52 |
while (i < end) |
| 53 |
crc = TABLE[0][(buf[i++] & 0xFF) ^ ((int)crc & 0xFF)] ^ |
| 54 |
(crc >>> 8); |
| 55 |
} |
| 56 |
|
| 57 |
@Override |
| 58 |
public byte[] finish() { |
| 59 |
byte[] buf = new byte[8]; |
| 60 |
ByteArrayView.setLongLE(buf, 0, ~crc); |
| 61 |
crc = -1; |
| 62 |
return buf; |
| 63 |
} |
| 64 |
} |