jdk/src/java.base/share/classes/java/util/zip/CRC32C.java
author avstepan
Thu, 06 Aug 2015 19:07:35 +0300
changeset 32037 ab4526f4ac10
parent 31671 362e0c0acece
child 33674 566777f73c32
permissions -rw-r--r--
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar Reviewed-by: lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
27737
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
     1
/*
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
     4
 *
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    10
 *
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    15
 * accompanied this code).
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    16
 *
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    20
 *
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    23
 * questions.
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    24
 */
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    25
package java.util.zip;
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    26
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    27
import java.nio.ByteBuffer;
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    28
import java.nio.ByteOrder;
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28064
diff changeset
    29
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28064
diff changeset
    30
import jdk.internal.HotSpotIntrinsicCandidate;
27737
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    31
import sun.misc.Unsafe;
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    32
import sun.nio.ch.DirectBuffer;
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    33
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    34
/**
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    35
 * A class that can be used to compute the CRC-32C of a data stream.
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    36
 *
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    37
 * <p>
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    38
 * CRC-32C is defined in <a href="http://www.ietf.org/rfc/rfc3720.txt">RFC
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    39
 * 3720</a>: Internet Small Computer Systems Interface (iSCSI).
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    40
 * </p>
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    41
 *
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    42
 * <p>
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    43
 * Passing a {@code null} argument to a method in this class will cause a
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    44
 * {@link NullPointerException} to be thrown.
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    45
 * </p>
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    46
 *
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    47
 * @since 1.9
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    48
 */
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    49
public final class CRC32C implements Checksum {
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    50
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    51
    /*
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    52
     * This CRC-32C implementation uses the 'slicing-by-8' algorithm described
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    53
     * in the paper "A Systematic Approach to Building High Performance
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    54
     * Software-Based CRC Generators" by Michael E. Kounavis and Frank L. Berry,
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    55
     * Intel Research and Development
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    56
     */
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    57
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    58
    /**
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    59
     * CRC-32C Polynomial
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    60
     */
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    61
    private static final int CRC32C_POLY = 0x1EDC6F41;
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    62
    private static final int REVERSED_CRC32C_POLY = Integer.reverse(CRC32C_POLY);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    63
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    64
    private static final Unsafe UNSAFE = Unsafe.getUnsafe();
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    65
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    66
    // Lookup tables
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    67
    // Lookup table for single byte calculations
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    68
    private static final int[] byteTable;
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    69
    // Lookup tables for bulk operations in 'slicing-by-8' algorithm
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    70
    private static final int[][] byteTables = new int[8][256];
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    71
    private static final int[] byteTable0 = byteTables[0];
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    72
    private static final int[] byteTable1 = byteTables[1];
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    73
    private static final int[] byteTable2 = byteTables[2];
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    74
    private static final int[] byteTable3 = byteTables[3];
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    75
    private static final int[] byteTable4 = byteTables[4];
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    76
    private static final int[] byteTable5 = byteTables[5];
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    77
    private static final int[] byteTable6 = byteTables[6];
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    78
    private static final int[] byteTable7 = byteTables[7];
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    79
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    80
    static {
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    81
        // Generate lookup tables
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    82
        // High-order polynomial term stored in LSB of r.
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    83
        for (int index = 0; index < byteTables[0].length; index++) {
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    84
           int r = index;
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    85
            for (int i = 0; i < Byte.SIZE; i++) {
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    86
                if ((r & 1) != 0) {
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    87
                    r = (r >>> 1) ^ REVERSED_CRC32C_POLY;
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    88
                } else {
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    89
                    r >>>= 1;
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    90
                }
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    91
            }
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    92
            byteTables[0][index] = r;
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    93
        }
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    94
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    95
        for (int index = 0; index < byteTables[0].length; index++) {
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    96
            int r = byteTables[0][index];
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    97
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    98
            for (int k = 1; k < byteTables.length; k++) {
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    99
                r = byteTables[0][r & 0xFF] ^ (r >>> 8);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   100
                byteTables[k][index] = r;
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   101
            }
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   102
        }
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   103
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   104
        if (ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN) {
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   105
            byteTable = byteTables[0];
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   106
        } else { // ByteOrder.BIG_ENDIAN
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   107
            byteTable = new int[byteTable0.length];
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   108
            System.arraycopy(byteTable0, 0, byteTable, 0, byteTable0.length);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   109
            for (int[] table : byteTables) {
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   110
                for (int index = 0; index < table.length; index++) {
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   111
                    table[index] = Integer.reverseBytes(table[index]);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   112
                }
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   113
            }
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   114
        }
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   115
    }
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   116
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   117
    /**
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   118
     * Calculated CRC-32C value
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   119
     */
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   120
    private int crc = 0xFFFFFFFF;
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   121
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   122
    /**
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   123
     * Creates a new CRC32C object.
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   124
     */
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   125
    public CRC32C() {
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   126
    }
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   127
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   128
    /**
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   129
     * Updates the CRC-32C checksum with the specified byte (the low eight bits
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   130
     * of the argument b).
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   131
     */
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   132
    @Override
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   133
    public void update(int b) {
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   134
        crc = (crc >>> 8) ^ byteTable[(crc ^ (b & 0xFF)) & 0xFF];
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   135
    }
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   136
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   137
    /**
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   138
     * Updates the CRC-32C checksum with the specified array of bytes.
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   139
     *
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   140
     * @throws ArrayIndexOutOfBoundsException
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   141
     *         if {@code off} is negative, or {@code len} is negative, or
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   142
     *         {@code off+len} is negative or greater than the length of
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   143
     *         the array {@code b}.
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   144
     */
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   145
    @Override
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   146
    public void update(byte[] b, int off, int len) {
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   147
        if (b == null) {
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   148
            throw new NullPointerException();
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   149
        }
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   150
        if (off < 0 || len < 0 || off > b.length - len) {
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   151
            throw new ArrayIndexOutOfBoundsException();
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   152
        }
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   153
        crc = updateBytes(crc, b, off, (off + len));
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   154
    }
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   155
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   156
    /**
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   157
     * Updates the CRC-32C checksum with the bytes from the specified buffer.
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   158
     *
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   159
     * The checksum is updated with the remaining bytes in the buffer, starting
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   160
     * at the buffer's position. Upon return, the buffer's position will be
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   161
     * updated to its limit; its limit will not have been changed.
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   162
     */
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   163
    @Override
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   164
    public void update(ByteBuffer buffer) {
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   165
        int pos = buffer.position();
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   166
        int limit = buffer.limit();
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   167
        assert (pos <= limit);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   168
        int rem = limit - pos;
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   169
        if (rem <= 0) {
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   170
            return;
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   171
        }
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   172
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   173
        if (buffer instanceof DirectBuffer) {
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   174
            crc = updateDirectByteBuffer(crc, ((DirectBuffer) buffer).address(),
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   175
                                         pos, limit);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   176
        } else if (buffer.hasArray()) {
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   177
            crc = updateBytes(crc, buffer.array(), pos + buffer.arrayOffset(),
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   178
                              limit + buffer.arrayOffset());
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   179
        } else {
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   180
            byte[] b = new byte[Math.min(buffer.remaining(), 4096)];
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   181
            while (buffer.hasRemaining()) {
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   182
                int length = Math.min(buffer.remaining(), b.length);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   183
                buffer.get(b, 0, length);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   184
                update(b, 0, length);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   185
            }
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   186
        }
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   187
        buffer.position(limit);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   188
    }
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   189
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   190
    /**
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   191
     * Resets CRC-32C to initial value.
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   192
     */
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   193
    @Override
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   194
    public void reset() {
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   195
        crc = 0xFFFFFFFF;
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   196
    }
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   197
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   198
    /**
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   199
     * Returns CRC-32C value.
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   200
     */
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   201
    @Override
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   202
    public long getValue() {
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   203
        return (~crc) & 0xFFFFFFFFL;
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   204
    }
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   205
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   206
    /**
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   207
     * Updates the CRC-32C checksum with the specified array of bytes.
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   208
     */
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28064
diff changeset
   209
    @HotSpotIntrinsicCandidate
27737
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   210
    private static int updateBytes(int crc, byte[] b, int off, int end) {
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   211
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   212
        // Do only byte reads for arrays so short they can't be aligned
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   213
        // or if bytes are stored with a larger witdh than one byte.,%
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   214
        if (end - off >= 8 && Unsafe.ARRAY_BYTE_INDEX_SCALE == 1) {
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   215
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   216
            // align on 8 bytes
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   217
            int alignLength
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   218
                    = (8 - ((Unsafe.ARRAY_BYTE_BASE_OFFSET + off) & 0x7)) & 0x7;
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   219
            for (int alignEnd = off + alignLength; off < alignEnd; off++) {
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   220
                crc = (crc >>> 8) ^ byteTable[(crc ^ b[off]) & 0xFF];
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   221
            }
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   222
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   223
            if (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN) {
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   224
                crc = Integer.reverseBytes(crc);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   225
            }
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   226
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   227
            // slicing-by-8
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   228
            for (; off < (end - Long.BYTES); off += Long.BYTES) {
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   229
                int firstHalf;
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   230
                int secondHalf;
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   231
                if (Unsafe.ADDRESS_SIZE == 4) {
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   232
                    // On 32 bit platforms read two ints instead of a single 64bit long
28064
cd22a60e9459 8067289: Fix deprecation warnings in java.base module - CRC32C
sherman
parents: 27804
diff changeset
   233
                    firstHalf = UNSAFE.getInt(b, (long)Unsafe.ARRAY_BYTE_BASE_OFFSET + off);
cd22a60e9459 8067289: Fix deprecation warnings in java.base module - CRC32C
sherman
parents: 27804
diff changeset
   234
                    secondHalf = UNSAFE.getInt(b, (long)Unsafe.ARRAY_BYTE_BASE_OFFSET + off
27737
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   235
                                               + Integer.BYTES);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   236
                } else {
28064
cd22a60e9459 8067289: Fix deprecation warnings in java.base module - CRC32C
sherman
parents: 27804
diff changeset
   237
                    long value = UNSAFE.getLong(b, (long)Unsafe.ARRAY_BYTE_BASE_OFFSET + off);
27737
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   238
                    if (ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN) {
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   239
                        firstHalf = (int) value;
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   240
                        secondHalf = (int) (value >>> 32);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   241
                    } else { // ByteOrder.BIG_ENDIAN
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   242
                        firstHalf = (int) (value >>> 32);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   243
                        secondHalf = (int) value;
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   244
                    }
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   245
                }
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   246
                crc ^= firstHalf;
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   247
                if (ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN) {
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   248
                    crc = byteTable7[crc & 0xFF]
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   249
                            ^ byteTable6[(crc >>> 8) & 0xFF]
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   250
                            ^ byteTable5[(crc >>> 16) & 0xFF]
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   251
                            ^ byteTable4[crc >>> 24]
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   252
                            ^ byteTable3[secondHalf & 0xFF]
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   253
                            ^ byteTable2[(secondHalf >>> 8) & 0xFF]
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   254
                            ^ byteTable1[(secondHalf >>> 16) & 0xFF]
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   255
                            ^ byteTable0[secondHalf >>> 24];
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   256
                } else { // ByteOrder.BIG_ENDIAN
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   257
                    crc = byteTable0[secondHalf & 0xFF]
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   258
                            ^ byteTable1[(secondHalf >>> 8) & 0xFF]
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   259
                            ^ byteTable2[(secondHalf >>> 16) & 0xFF]
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   260
                            ^ byteTable3[secondHalf >>> 24]
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   261
                            ^ byteTable4[crc & 0xFF]
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   262
                            ^ byteTable5[(crc >>> 8) & 0xFF]
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   263
                            ^ byteTable6[(crc >>> 16) & 0xFF]
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   264
                            ^ byteTable7[crc >>> 24];
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   265
                }
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   266
            }
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   267
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   268
            if (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN) {
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   269
                crc = Integer.reverseBytes(crc);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   270
            }
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   271
        }
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   272
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   273
        // Tail
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   274
        for (; off < end; off++) {
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   275
            crc = (crc >>> 8) ^ byteTable[(crc ^ b[off]) & 0xFF];
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   276
        }
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   277
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   278
        return crc;
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   279
    }
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   280
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   281
    /**
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   282
     * Updates the CRC-32C checksum reading from the specified address.
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   283
     */
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28064
diff changeset
   284
    @HotSpotIntrinsicCandidate
27737
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   285
    private static int updateDirectByteBuffer(int crc, long address,
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   286
                                              int off, int end) {
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   287
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   288
        // Do only byte reads for arrays so short they can't be aligned
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   289
        if (end - off >= 8) {
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   290
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   291
            // align on 8 bytes
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   292
            int alignLength = (8 - (int) ((address + off) & 0x7)) & 0x7;
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   293
            for (int alignEnd = off + alignLength; off < alignEnd; off++) {
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   294
                crc = (crc >>> 8)
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   295
                        ^ byteTable[(crc ^ UNSAFE.getByte(address + off)) & 0xFF];
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   296
            }
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   297
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   298
            if (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN) {
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   299
                crc = Integer.reverseBytes(crc);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   300
            }
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   301
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   302
            // slicing-by-8
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   303
            for (; off <= (end - Long.BYTES); off += Long.BYTES) {
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   304
                // Always reading two ints as reading a long followed by
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   305
                // shifting and casting was slower.
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   306
                int firstHalf = UNSAFE.getInt(address + off);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   307
                int secondHalf = UNSAFE.getInt(address + off + Integer.BYTES);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   308
                crc ^= firstHalf;
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   309
                if (ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN) {
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   310
                    crc = byteTable7[crc & 0xFF]
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   311
                            ^ byteTable6[(crc >>> 8) & 0xFF]
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   312
                            ^ byteTable5[(crc >>> 16) & 0xFF]
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   313
                            ^ byteTable4[crc >>> 24]
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   314
                            ^ byteTable3[secondHalf & 0xFF]
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   315
                            ^ byteTable2[(secondHalf >>> 8) & 0xFF]
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   316
                            ^ byteTable1[(secondHalf >>> 16) & 0xFF]
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   317
                            ^ byteTable0[secondHalf >>> 24];
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   318
                } else { // ByteOrder.BIG_ENDIAN
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   319
                    crc = byteTable0[secondHalf & 0xFF]
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   320
                            ^ byteTable1[(secondHalf >>> 8) & 0xFF]
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   321
                            ^ byteTable2[(secondHalf >>> 16) & 0xFF]
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   322
                            ^ byteTable3[secondHalf >>> 24]
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   323
                            ^ byteTable4[crc & 0xFF]
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   324
                            ^ byteTable5[(crc >>> 8) & 0xFF]
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   325
                            ^ byteTable6[(crc >>> 16) & 0xFF]
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   326
                            ^ byteTable7[crc >>> 24];
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   327
                }
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   328
            }
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   329
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   330
            if (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN) {
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   331
                crc = Integer.reverseBytes(crc);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   332
            }
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   333
        }
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   334
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   335
        // Tail
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   336
        for (; off < end; off++) {
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   337
            crc = (crc >>> 8)
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   338
                    ^ byteTable[(crc ^ UNSAFE.getByte(address + off)) & 0xFF];
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   339
        }
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   340
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   341
        return crc;
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   342
    }
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   343
}