1 /*
2  * Copyright (c) 1996, 2014, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
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 
26 package java.util.zip;
27 
28 import java.nio.ByteBuffer;
29 import java.util.Objects;
30 
31 import sun.nio.ch.DirectBuffer;
32 import jdk.internal.HotSpotIntrinsicCandidate;
33 
34 /**
35  * A class that can be used to compute the CRC-32 of a data stream.
36  *
37  * <p> Passing a {@code null} argument to a method in this class will cause
38  * a {@link NullPointerException} to be thrown.</p>
39  *
40  * @author      David Connelly
41  * @since 1.1
42  */
43 public class CRC32 implements Checksum {
44     private int crc;
45 
46     /**
47      * Creates a new CRC32 object.
48      */
CRC32()49     public CRC32() {
50     }
51 
52 
53     /**
54      * Updates the CRC-32 checksum with the specified byte (the low
55      * eight bits of the argument b).
56      */
57     @Override
update(int b)58     public void update(int b) {
59         crc = update(crc, b);
60     }
61 
62     /**
63      * Updates the CRC-32 checksum with the specified array of bytes.
64      *
65      * @throws ArrayIndexOutOfBoundsException
66      *         if {@code off} is negative, or {@code len} is negative, or
67      *         {@code off+len} is negative or greater than the length of
68      *         the array {@code b}.
69      */
70     @Override
update(byte[] b, int off, int len)71     public void update(byte[] b, int off, int len) {
72         if (b == null) {
73             throw new NullPointerException();
74         }
75         if (off < 0 || len < 0 || off > b.length - len) {
76             throw new ArrayIndexOutOfBoundsException();
77         }
78         crc = updateBytes(crc, b, off, len);
79     }
80 
81     /**
82      * Updates the CRC-32 checksum with the bytes from the specified buffer.
83      *
84      * The checksum is updated with the remaining bytes in the buffer, starting
85      * at the buffer's position. Upon return, the buffer's position will be
86      * updated to its limit; its limit will not have been changed.
87      *
88      * @since 1.8
89      */
90     @Override
update(ByteBuffer buffer)91     public void update(ByteBuffer buffer) {
92         int pos = buffer.position();
93         int limit = buffer.limit();
94         assert (pos <= limit);
95         int rem = limit - pos;
96         if (rem <= 0)
97             return;
98         if (buffer instanceof DirectBuffer) {
99             crc = updateByteBuffer(crc, ((DirectBuffer)buffer).address(), pos, rem);
100         } else if (buffer.hasArray()) {
101             crc = updateBytes(crc, buffer.array(), pos + buffer.arrayOffset(), rem);
102         } else {
103             byte[] b = new byte[Math.min(buffer.remaining(), 4096)];
104             while (buffer.hasRemaining()) {
105                 int length = Math.min(buffer.remaining(), b.length);
106                 buffer.get(b, 0, length);
107                 update(b, 0, length);
108             }
109         }
110         buffer.position(limit);
111     }
112 
113     /**
114      * Resets CRC-32 to initial value.
115      */
116     @Override
reset()117     public void reset() {
118         crc = 0;
119     }
120 
121     /**
122      * Returns CRC-32 value.
123      */
124     @Override
getValue()125     public long getValue() {
126         return (long)crc & 0xffffffffL;
127     }
128 
129     @HotSpotIntrinsicCandidate
update(int crc, int b)130     private static native int update(int crc, int b);
131 
updateBytes(int crc, byte[] b, int off, int len)132     private static int updateBytes(int crc, byte[] b, int off, int len) {
133         updateBytesCheck(b, off, len);
134         return updateBytes0(crc, b, off, len);
135     }
136 
137     @HotSpotIntrinsicCandidate
updateBytes0(int crc, byte[] b, int off, int len)138     private static native int updateBytes0(int crc, byte[] b, int off, int len);
139 
updateBytesCheck(byte[] b, int off, int len)140     private static void updateBytesCheck(byte[] b, int off, int len) {
141         if (len <= 0) {
142             return;  // not an error because updateBytesImpl won't execute if len <= 0
143         }
144 
145         Objects.requireNonNull(b);
146 
147         if (off < 0 || off >= b.length) {
148             throw new ArrayIndexOutOfBoundsException(off);
149         }
150 
151         int endIndex = off + len - 1;
152         if (endIndex < 0 || endIndex >= b.length) {
153             throw new ArrayIndexOutOfBoundsException(endIndex);
154         }
155     }
156 
updateByteBuffer(int alder, long addr, int off, int len)157     private static int updateByteBuffer(int alder, long addr,
158                                         int off, int len) {
159         updateByteBufferCheck(addr);
160         return updateByteBuffer0(alder, addr, off, len);
161     }
162 
163     @HotSpotIntrinsicCandidate
updateByteBuffer0(int alder, long addr, int off, int len)164     private static native int updateByteBuffer0(int alder, long addr,
165                                                 int off, int len);
166 
updateByteBufferCheck(long addr)167     private static void updateByteBufferCheck(long addr) {
168         // Performs only a null check because bounds checks
169         // are not easy to do on raw addresses.
170         if (addr == 0L) {
171             throw new NullPointerException();
172         }
173     }
174 
175     static {
ZipUtils.loadLibrary()176         ZipUtils.loadLibrary();
177     }
178 }
179