9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25 package java.util.zip;
26
27 import java.nio.ByteBuffer;
28 import java.nio.ByteOrder;
29 import sun.misc.Unsafe;
30 import sun.nio.ch.DirectBuffer;
31
32 /**
33 * A class that can be used to compute the CRC-32C of a data stream.
34 *
35 * <p>
36 * CRC-32C is defined in <a href="http://www.ietf.org/rfc/rfc3720.txt">RFC
37 * 3720</a>: Internet Small Computer Systems Interface (iSCSI).
38 * </p>
39 *
40 * <p>
41 * Passing a {@code null} argument to a method in this class will cause a
42 * {@link NullPointerException} to be thrown.
43 * </p>
44 *
45 * @since 1.9
46 */
47 public final class CRC32C implements Checksum {
48
187
188 /**
189 * Resets CRC-32C to initial value.
190 */
191 @Override
192 public void reset() {
193 crc = 0xFFFFFFFF;
194 }
195
196 /**
197 * Returns CRC-32C value.
198 */
199 @Override
200 public long getValue() {
201 return (~crc) & 0xFFFFFFFFL;
202 }
203
204 /**
205 * Updates the CRC-32C checksum with the specified array of bytes.
206 */
207 private static int updateBytes(int crc, byte[] b, int off, int end) {
208
209 // Do only byte reads for arrays so short they can't be aligned
210 // or if bytes are stored with a larger witdh than one byte.,%
211 if (end - off >= 8 && Unsafe.ARRAY_BYTE_INDEX_SCALE == 1) {
212
213 // align on 8 bytes
214 int alignLength
215 = (8 - ((Unsafe.ARRAY_BYTE_BASE_OFFSET + off) & 0x7)) & 0x7;
216 for (int alignEnd = off + alignLength; off < alignEnd; off++) {
217 crc = (crc >>> 8) ^ byteTable[(crc ^ b[off]) & 0xFF];
218 }
219
220 if (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN) {
221 crc = Integer.reverseBytes(crc);
222 }
223
224 // slicing-by-8
225 for (; off < (end - Long.BYTES); off += Long.BYTES) {
226 int firstHalf;
261 ^ byteTable7[crc >>> 24];
262 }
263 }
264
265 if (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN) {
266 crc = Integer.reverseBytes(crc);
267 }
268 }
269
270 // Tail
271 for (; off < end; off++) {
272 crc = (crc >>> 8) ^ byteTable[(crc ^ b[off]) & 0xFF];
273 }
274
275 return crc;
276 }
277
278 /**
279 * Updates the CRC-32C checksum reading from the specified address.
280 */
281 private static int updateDirectByteBuffer(int crc, long address,
282 int off, int end) {
283
284 // Do only byte reads for arrays so short they can't be aligned
285 if (end - off >= 8) {
286
287 // align on 8 bytes
288 int alignLength = (8 - (int) ((address + off) & 0x7)) & 0x7;
289 for (int alignEnd = off + alignLength; off < alignEnd; off++) {
290 crc = (crc >>> 8)
291 ^ byteTable[(crc ^ UNSAFE.getByte(address + off)) & 0xFF];
292 }
293
294 if (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN) {
295 crc = Integer.reverseBytes(crc);
296 }
297
298 // slicing-by-8
299 for (; off <= (end - Long.BYTES); off += Long.BYTES) {
300 // Always reading two ints as reading a long followed by
|
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25 package java.util.zip;
26
27 import java.nio.ByteBuffer;
28 import java.nio.ByteOrder;
29
30 import jdk.internal.HotSpotIntrinsicCandidate;
31 import sun.misc.Unsafe;
32 import sun.nio.ch.DirectBuffer;
33
34 /**
35 * A class that can be used to compute the CRC-32C of a data stream.
36 *
37 * <p>
38 * CRC-32C is defined in <a href="http://www.ietf.org/rfc/rfc3720.txt">RFC
39 * 3720</a>: Internet Small Computer Systems Interface (iSCSI).
40 * </p>
41 *
42 * <p>
43 * Passing a {@code null} argument to a method in this class will cause a
44 * {@link NullPointerException} to be thrown.
45 * </p>
46 *
47 * @since 1.9
48 */
49 public final class CRC32C implements Checksum {
50
189
190 /**
191 * Resets CRC-32C to initial value.
192 */
193 @Override
194 public void reset() {
195 crc = 0xFFFFFFFF;
196 }
197
198 /**
199 * Returns CRC-32C value.
200 */
201 @Override
202 public long getValue() {
203 return (~crc) & 0xFFFFFFFFL;
204 }
205
206 /**
207 * Updates the CRC-32C checksum with the specified array of bytes.
208 */
209 @HotSpotIntrinsicCandidate
210 private static int updateBytes(int crc, byte[] b, int off, int end) {
211
212 // Do only byte reads for arrays so short they can't be aligned
213 // or if bytes are stored with a larger witdh than one byte.,%
214 if (end - off >= 8 && Unsafe.ARRAY_BYTE_INDEX_SCALE == 1) {
215
216 // align on 8 bytes
217 int alignLength
218 = (8 - ((Unsafe.ARRAY_BYTE_BASE_OFFSET + off) & 0x7)) & 0x7;
219 for (int alignEnd = off + alignLength; off < alignEnd; off++) {
220 crc = (crc >>> 8) ^ byteTable[(crc ^ b[off]) & 0xFF];
221 }
222
223 if (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN) {
224 crc = Integer.reverseBytes(crc);
225 }
226
227 // slicing-by-8
228 for (; off < (end - Long.BYTES); off += Long.BYTES) {
229 int firstHalf;
264 ^ byteTable7[crc >>> 24];
265 }
266 }
267
268 if (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN) {
269 crc = Integer.reverseBytes(crc);
270 }
271 }
272
273 // Tail
274 for (; off < end; off++) {
275 crc = (crc >>> 8) ^ byteTable[(crc ^ b[off]) & 0xFF];
276 }
277
278 return crc;
279 }
280
281 /**
282 * Updates the CRC-32C checksum reading from the specified address.
283 */
284 @HotSpotIntrinsicCandidate
285 private static int updateDirectByteBuffer(int crc, long address,
286 int off, int end) {
287
288 // Do only byte reads for arrays so short they can't be aligned
289 if (end - off >= 8) {
290
291 // align on 8 bytes
292 int alignLength = (8 - (int) ((address + off) & 0x7)) & 0x7;
293 for (int alignEnd = off + alignLength; off < alignEnd; off++) {
294 crc = (crc >>> 8)
295 ^ byteTable[(crc ^ UNSAFE.getByte(address + off)) & 0xFF];
296 }
297
298 if (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN) {
299 crc = Integer.reverseBytes(crc);
300 }
301
302 // slicing-by-8
303 for (; off <= (end - Long.BYTES); off += Long.BYTES) {
304 // Always reading two ints as reading a long followed by
|