1/* 2 * Copyright (C) 2017 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 18package com.android.internal.util; 19 20import android.annotation.Nullable; 21import android.text.TextUtils; 22 23import libcore.util.Objects; 24 25import java.nio.ByteBuffer; 26import java.util.Arrays; 27import java.util.UUID; 28import java.util.function.IntFunction; 29 30/** 31 * A utility class for handling unsigned integers and unsigned arithmetics, as well as syntactic 32 * sugar methods for ByteBuffer. Useful for networking and packet manipulations. 33 * {@hide} 34 */ 35public final class BitUtils { 36 private BitUtils() {} 37 38 public static boolean maskedEquals(long a, long b, long mask) { 39 return (a & mask) == (b & mask); 40 } 41 42 public static boolean maskedEquals(byte a, byte b, byte mask) { 43 return (a & mask) == (b & mask); 44 } 45 46 public static boolean maskedEquals(byte[] a, byte[] b, @Nullable byte[] mask) { 47 if (a == null || b == null) return a == b; 48 Preconditions.checkArgument(a.length == b.length, "Inputs must be of same size"); 49 if (mask == null) return Arrays.equals(a, b); 50 Preconditions.checkArgument(a.length == mask.length, "Mask must be of same size as inputs"); 51 for (int i = 0; i < mask.length; i++) { 52 if (!maskedEquals(a[i], b[i], mask[i])) return false; 53 } 54 return true; 55 } 56 57 public static boolean maskedEquals(UUID a, UUID b, @Nullable UUID mask) { 58 if (mask == null) { 59 return Objects.equal(a, b); 60 } 61 return maskedEquals(a.getLeastSignificantBits(), b.getLeastSignificantBits(), 62 mask.getLeastSignificantBits()) 63 && maskedEquals(a.getMostSignificantBits(), b.getMostSignificantBits(), 64 mask.getMostSignificantBits()); 65 } 66 67 public static int[] unpackBits(long val) { 68 int size = Long.bitCount(val); 69 int[] result = new int[size]; 70 int index = 0; 71 int bitPos = 0; 72 while (val > 0) { 73 if ((val & 1) == 1) result[index++] = bitPos; 74 val = val >> 1; 75 bitPos++; 76 } 77 return result; 78 } 79 80 public static long packBits(int[] bits) { 81 long packed = 0; 82 for (int b : bits) { 83 packed |= (1 << b); 84 } 85 return packed; 86 } 87 88 public static int uint8(byte b) { 89 return b & 0xff; 90 } 91 92 public static int uint16(short s) { 93 return s & 0xffff; 94 } 95 96 public static long uint32(int i) { 97 return i & 0xffffffffL; 98 } 99 100 public static int bytesToBEInt(byte[] bytes) { 101 return (uint8(bytes[0]) << 24) 102 + (uint8(bytes[1]) << 16) 103 + (uint8(bytes[2]) << 8) 104 + (uint8(bytes[3])); 105 } 106 107 public static int bytesToLEInt(byte[] bytes) { 108 return Integer.reverseBytes(bytesToBEInt(bytes)); 109 } 110 111 public static int getUint8(ByteBuffer buffer, int position) { 112 return uint8(buffer.get(position)); 113 } 114 115 public static int getUint16(ByteBuffer buffer, int position) { 116 return uint16(buffer.getShort(position)); 117 } 118 119 public static long getUint32(ByteBuffer buffer, int position) { 120 return uint32(buffer.getInt(position)); 121 } 122 123 public static void put(ByteBuffer buffer, int position, byte[] bytes) { 124 final int original = buffer.position(); 125 buffer.position(position); 126 buffer.put(bytes); 127 buffer.position(original); 128 } 129 130 public static boolean isBitSet(long flags, int bitIndex) { 131 return (flags & bitAt(bitIndex)) != 0; 132 } 133 134 public static long bitAt(int bitIndex) { 135 return 1L << bitIndex; 136 } 137 138 public static String flagsToString(int flags, IntFunction<String> getFlagName) { 139 StringBuilder builder = new StringBuilder(); 140 int count = 0; 141 while (flags != 0) { 142 final int flag = 1 << Integer.numberOfTrailingZeros(flags); 143 flags &= ~flag; 144 if (count > 0) builder.append(", "); 145 builder.append(getFlagName.apply(flag)); 146 count++; 147 } 148 TextUtils.wrap(builder, "[", "]"); 149 return builder.toString(); 150 } 151} 152