001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.test.util;
019
020// Cribbed from
021// hadoop-tools/hadoop-azure/src/main/java/org/apache/hadoop/fs/azurebfs/utils/CRC64.java
022
023public class CRC64 {
024  private static final long POLY = 0x9a6c9329ac4bc9b5L;
025  private static final int TABLE_LENGTH = 256;
026  private static final long[] TABLE = new long[TABLE_LENGTH];
027  static {
028    /* Initialize a table constructed from POLY */
029    for (int n = 0; n < TABLE_LENGTH; ++n) {
030      long crc = n;
031      for (int i = 0; i < 8; ++i) {
032        if ((crc & 1) == 1) {
033          crc = (crc >>> 1) ^ POLY;
034        } else {
035          crc >>>= 1;
036        }
037      }
038      TABLE[n] = crc;
039    }
040  }
041
042  private long value = -1;
043
044  public void reset() {
045    value = -1;
046  }
047
048  public void update(byte[] input, int off, int len) {
049    for (int i = off; i < off + len; i++) {
050      value = TABLE[(input[i] ^ (int) value) & 0xFF] ^ (value >>> 8);
051    }
052  }
053
054  public void update(byte[] input) {
055    update(input, 0, input.length);
056  }
057
058  public long getValue() {
059    // Return the compliment of 'value' to complete the calculation.
060    return ~value;
061  }
062
063}