1 /*
2  * Copyright (c) 1996, 2020, 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.lang.ref.Reference;
29 import java.nio.ByteBuffer;
30 import java.util.Objects;
31 
32 import sun.nio.ch.DirectBuffer;
33 import dalvik.annotation.optimization.CriticalNative;
34 import jdk.internal.vm.annotation.IntrinsicCandidate;
35 
36 /**
37  * A class that can be used to compute the CRC-32 of a data stream.
38  *
39  * <p> Passing a {@code null} argument to a method in this class will cause
40  * a {@link NullPointerException} to be thrown.</p>
41  *
42  * @author      David Connelly
43  * @since 1.1
44  */
45 public class CRC32 implements Checksum {
46     private int crc;
47 
48     /**
49      * Creates a new CRC32 object.
50      */
CRC32()51     public CRC32() {
52     }
53 
54 
55     /**
56      * Updates the CRC-32 checksum with the specified byte (the low
57      * eight bits of the argument b).
58      */
59     @Override
update(int b)60     public void update(int b) {
61         crc = update(crc, b);
62     }
63 
64     /**
65      * Updates the CRC-32 checksum with the specified array of bytes.
66      *
67      * @throws ArrayIndexOutOfBoundsException
68      *         if {@code off} is negative, or {@code len} is negative, or
69      *         {@code off+len} is negative or greater than the length of
70      *         the array {@code b}.
71      */
72     @Override
update(byte[] b, int off, int len)73     public void update(byte[] b, int off, int len) {
74         if (b == null) {
75             throw new NullPointerException();
76         }
77         if (off < 0 || len < 0 || off > b.length - len) {
78             throw new ArrayIndexOutOfBoundsException();
79         }
80         crc = updateBytes(crc, b, off, len);
81     }
82 
83     // Android-changed: method kept during jdk17u update for compatibility.
84     /**
85      * Updates the CRC-32 checksum with the specified array of bytes.
86      *
87      * @param b the array of bytes to update the checksum with
88      */
89     @Override
update(byte[] b)90     public void update(byte[] b) {
91         crc = updateBytes(crc, b, 0, b.length);
92     }
93 
94     /**
95      * Updates the CRC-32 checksum with the bytes from the specified buffer.
96      *
97      * The checksum is updated with the remaining bytes in the buffer, starting
98      * at the buffer's position. Upon return, the buffer's position will be
99      * updated to its limit; its limit will not have been changed.
100      *
101      * @since 1.8
102      */
103     @Override
update(ByteBuffer buffer)104     public void update(ByteBuffer buffer) {
105         int pos = buffer.position();
106         int limit = buffer.limit();
107         assert (pos <= limit);
108         int rem = limit - pos;
109         if (rem <= 0)
110             return;
111         if (buffer.isDirect()) {
112             try {
113                 crc = updateByteBuffer(crc, ((DirectBuffer)buffer).address(), pos, rem);
114             } finally {
115                 Reference.reachabilityFence(buffer);
116             }
117         } else if (buffer.hasArray()) {
118             crc = updateBytes(crc, buffer.array(), pos + buffer.arrayOffset(), rem);
119         } else {
120             byte[] b = new byte[Math.min(buffer.remaining(), 4096)];
121             while (buffer.hasRemaining()) {
122                 int length = Math.min(buffer.remaining(), b.length);
123                 buffer.get(b, 0, length);
124                 update(b, 0, length);
125             }
126         }
127         buffer.position(limit);
128     }
129 
130     /**
131      * Resets CRC-32 to initial value.
132      */
133     @Override
reset()134     public void reset() {
135         crc = 0;
136     }
137 
138     /**
139      * Returns CRC-32 value.
140      */
141     @Override
getValue()142     public long getValue() {
143         return (long)crc & 0xffffffffL;
144     }
145 
146     @CriticalNative
147     @IntrinsicCandidate
update(int crc, int b)148     private static native int update(int crc, int b);
149 
updateBytes(int crc, byte[] b, int off, int len)150     private static int updateBytes(int crc, byte[] b, int off, int len) {
151         updateBytesCheck(b, off, len);
152         return updateBytes0(crc, b, off, len);
153     }
154 
155     @IntrinsicCandidate
updateBytes0(int crc, byte[] b, int off, int len)156     private static native int updateBytes0(int crc, byte[] b, int off, int len);
157 
updateBytesCheck(byte[] b, int off, int len)158     private static void updateBytesCheck(byte[] b, int off, int len) {
159         if (len <= 0) {
160             return;  // not an error because updateBytesImpl won't execute if len <= 0
161         }
162 
163         Objects.requireNonNull(b);
164 
165         if (off < 0 || off >= b.length) {
166             throw new ArrayIndexOutOfBoundsException(off);
167         }
168 
169         int endIndex = off + len - 1;
170         if (endIndex < 0 || endIndex >= b.length) {
171             throw new ArrayIndexOutOfBoundsException(endIndex);
172         }
173     }
174 
updateByteBuffer(int alder, long addr, int off, int len)175     private static int updateByteBuffer(int alder, long addr,
176                                         int off, int len) {
177         updateByteBufferCheck(addr);
178         return updateByteBuffer0(alder, addr, off, len);
179     }
180 
181     @IntrinsicCandidate
updateByteBuffer0(int alder, long addr, int off, int len)182     private static native int updateByteBuffer0(int alder, long addr,
183                                                 int off, int len);
184 
updateByteBufferCheck(long addr)185     private static void updateByteBufferCheck(long addr) {
186         // Performs only a null check because bounds checks
187         // are not easy to do on raw addresses.
188         if (addr == 0L) {
189             throw new NullPointerException();
190         }
191     }
192 }
193