1/*
2 * CRC64
3 *
4 * Author: Lasse Collin <lasse.collin@tukaani.org>
5 *
6 * This file has been put into the public domain.
7 * You can do whatever you want with this file.
8 */
9
10package org.tukaani.xz.check;
11
12public class CRC64 extends Check {
13    private static final long poly = 0xC96C5795D7870F42L;
14    private static final long[] crcTable = new long[256];
15
16    private long crc = -1;
17
18    static {
19        for (int b = 0; b < crcTable.length; ++b) {
20                long r = b;
21                for (int i = 0; i < 8; ++i) {
22                        if ((r & 1) == 1)
23                                r = (r >>> 1) ^ poly;
24                        else
25                                r >>>= 1;
26                }
27
28                crcTable[b] = r;
29        }
30    }
31
32    public CRC64() {
33        size = 8;
34        name = "CRC64";
35    }
36
37    public void update(byte[] buf, int off, int len) {
38        int end = off + len;
39
40        while (off < end)
41            crc = crcTable[(buf[off++] ^ (int)crc) & 0xFF] ^ (crc >>> 8);
42    }
43
44    public byte[] finish() {
45        long value = ~crc;
46        crc = -1;
47
48        byte[] buf = new byte[8];
49        for (int i = 0; i < buf.length; ++i)
50            buf[i] = (byte)(value >> (i * 8));
51
52        return buf;
53    }
54}
55