1/*******************************************************************************
2 * Copyright (c) 2009, 2015 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.internal.data;
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 */
23public final class CRC64 {
24
25	private static final long POLY64REV = 0xd800000000000000L;
26
27	private static final long[] LOOKUPTABLE;
28
29	static {
30		LOOKUPTABLE = new long[0x100];
31		for (int i = 0; i < 0x100; i++) {
32			long v = i;
33			for (int j = 0; j < 8; j++) {
34				if ((v & 1) == 1) {
35					v = (v >>> 1) ^ POLY64REV;
36				} else {
37					v = (v >>> 1);
38				}
39			}
40			LOOKUPTABLE[i] = v;
41		}
42	}
43
44	/**
45	 * Calculates the CRC64 checksum for the given data array.
46	 *
47	 * @param data
48	 *            data to calculate checksum for
49	 * @return checksum value
50	 */
51	public static long checksum(final byte[] data) {
52		long sum = 0;
53		for (final byte b : data) {
54			final int lookupidx = ((int) sum ^ b) & 0xff;
55			sum = (sum >>> 8) ^ LOOKUPTABLE[lookupidx];
56		}
57		return sum;
58	}
59
60	private CRC64() {
61	}
62
63}
64