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