1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.cts.vpnfirewall; 18 19 public final class Rfc1071 { checksum(byte[] data, int length)20 static int checksum(byte[] data, int length) { 21 int sum = 0; 22 23 // High bytes (even indices) 24 for (int i = 0; i < length; i += 2) { 25 sum += (data[i] & 0xFF) << 8; 26 sum = (sum & 0xFFFF) + (sum >> 16); 27 } 28 29 // Low bytes (odd indices) 30 for (int i = 1; i < length; i += 2) { 31 sum += (data[i] & 0xFF); 32 sum = (sum & 0xFFFF) + (sum >> 16); 33 } 34 35 // Fix any one's-complement errors- sometimes it is necessary to rotate twice. 36 sum = (sum & 0xFFFF) + (sum >> 16); 37 return sum ^ 0xFFFF; 38 } 39 } 40