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 * $Id: $
   12 *******************************************************************************/
   13package org.jacoco.core.instr;
   14
   15/**
   16 * CRC64 checksum calculator based on the polynom specified in ISO 3309. The
   17 * implementation is based on the following publications:
   18 * 
   19 * <ul>
   20 * <li>http://en.wikipedia.org/wiki/Cyclic_redundancy_check</li>
   21 * <li>http://www.geocities.com/SiliconValley/Pines/8659/crc.htm</li>
   22 * </ul>
   23 * 
   24 * @author Marc R. Hoffmann
   25 * @version $Revision: $
   26 */
   27public final class CRC64 {
   28
   29    private static final long POLY64REV = 0xd800000000000000L;
   30
   31    private static final long[] LOOKUPTABLE;
   32
   33    static {
   34        LOOKUPTABLE = new long[0x100];
   35        for (int i = 0; i < 0x100; i++) {
   36            long v = i;
   37            for (int j = 0; j < 8; j++) {
   38                if ((v & 1) == 1) {
   39                    v = (v >>> 1) ^ POLY64REV;
   40                } else {
   41                    v = (v >>> 1);
   42                }
   43            }
   44            LOOKUPTABLE[i] = v;
   45        }
   46    }
   47
   48    /**
   49     * Calculates the CRC64 checksum for the given data array.
   50     * 
   51     * @param data
   52     *            data to calculate checksum for
   53     * @return checksum value
   54     */
   55    public static long checksum(final byte[] data) {
   56        long sum = 0;
   57        for (int i = 0; i < data.length; i++) {
   58            final int lookupidx = ((int) sum ^ data[i]) & 0xff;
   59            sum = (sum >>> 8) ^ LOOKUPTABLE[lookupidx];
   60        }
   61        return sum;
   62    }
   63
   64    private CRC64() {
   65    }
   66
   67}