CRC64.java
1/*******************************************************************************
2 * Copyright (c) 2009, 2010 Mountainminds GmbH & Co. KG and Contributors
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * Marc R. Hoffmann - initial API and implementation
10 *
11 *******************************************************************************/
12package org.jacoco.core.instr;
13
14/**
15 * CRC64 checksum calculator based on the polynom specified in ISO 3309. The
16 * implementation is based on the following publications:
17 *
18 * <ul>
19 * <li>http://en.wikipedia.org/wiki/Cyclic_redundancy_check</li>
20 * <li>http://www.geocities.com/SiliconValley/Pines/8659/crc.htm</li>
21 * </ul>
22 *
23 * @author Marc R. Hoffmann
24 * @version 0.4.1.20101007204400
25 */
26public final class CRC64 {
27
28 private static final long POLY64REV = 0xd800000000000000L;
29
30 private static final long[] LOOKUPTABLE;
31
32 static {
33 LOOKUPTABLE = new long[0x100];
34 for (int i = 0; i < 0x100; i++) {
35 long v = i;
36 for (int j = 0; j < 8; j++) {
37 if ((v & 1) == 1) {
38 v = (v >>> 1) ^ POLY64REV;
39 } else {
40 v = (v >>> 1);
41 }
42 }
43 LOOKUPTABLE[i] = v;
44 }
45 }
46
47 /**
48 * Calculates the CRC64 checksum for the given data array.
49 *
50 * @param data
51 * data to calculate checksum for
52 * @return checksum value
53 */
54 public static long checksum(final byte[] data) {
55 long sum = 0;
56 for (int i = 0; i < data.length; i++) {
57 final int lookupidx = ((int) sum ^ data[i]) & 0xff;
58 sum = (sum >>> 8) ^ LOOKUPTABLE[lookupidx];
59 }
60 return sum;
61 }
62
63 private CRC64() {
64 }
65
66}