1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#define LOG_TAG "CRC32"
19
20#include "JNIHelp.h"
21#include "JniConstants.h"
22#include "ScopedPrimitiveArray.h"
23#include "jni.h"
24#include "zlib.h"
25
26static jlong CRC32_updateImpl(JNIEnv* env, jobject, jbyteArray byteArray, int off, int len, jlong crc) {
27    ScopedByteArrayRO bytes(env, byteArray);
28    if (bytes.get() == NULL) {
29        return 0;
30    }
31    jlong result = crc32(crc, reinterpret_cast<const Bytef*>(bytes.get() + off), len);
32    return result;
33}
34
35static jlong CRC32_updateByteImpl(JNIEnv*, jobject, jbyte val, jlong crc) {
36    return crc32(crc, reinterpret_cast<const Bytef*>(&val), 1);
37}
38
39static JNINativeMethod gMethods[] = {
40    NATIVE_METHOD(CRC32, updateImpl, "([BIIJ)J"),
41    NATIVE_METHOD(CRC32, updateByteImpl, "(BJ)J"),
42};
43void register_java_util_zip_CRC32(JNIEnv* env) {
44    jniRegisterNativeMethods(env, "java/util/zip/CRC32", gMethods, NELEM(gMethods));
45}
46