blob: 1b354d0d9df00649baaeb3b1fc8d6e075e3906bc [file] [log] [blame]
Eugene Susla8ab86a62017-02-23 18:24:39 -08001/*
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;
21
22import libcore.util.Objects;
23
Hugo Benichi495a17b2017-01-12 15:31:05 +090024import java.nio.ByteBuffer;
Eugene Susla8ab86a62017-02-23 18:24:39 -080025import java.util.Arrays;
26import java.util.UUID;
27
Hugo Benichi495a17b2017-01-12 15:31:05 +090028/**
29 * A utility class for handling unsigned integers and unsigned arithmetics, as well as syntactic
30 * sugar methods for ByteBuffer. Useful for networking and packet manipulations.
31 * {@hide}
32 */
33public final class BitUtils {
Eugene Susla8ab86a62017-02-23 18:24:39 -080034 private BitUtils() {}
35
36 public static boolean maskedEquals(long a, long b, long mask) {
37 return (a & mask) == (b & mask);
38 }
39
40 public static boolean maskedEquals(byte a, byte b, byte mask) {
41 return (a & mask) == (b & mask);
42 }
43
44 public static boolean maskedEquals(byte[] a, byte[] b, @Nullable byte[] mask) {
45 if (a == null || b == null) return a == b;
46 Preconditions.checkArgument(a.length == b.length, "Inputs must be of same size");
47 if (mask == null) return Arrays.equals(a, b);
48 Preconditions.checkArgument(a.length == mask.length, "Mask must be of same size as inputs");
49 for (int i = 0; i < mask.length; i++) {
50 if (!maskedEquals(a[i], b[i], mask[i])) return false;
51 }
52 return true;
53 }
54
55 public static boolean maskedEquals(UUID a, UUID b, @Nullable UUID mask) {
56 if (mask == null) {
57 return Objects.equal(a, b);
58 }
59 return maskedEquals(a.getLeastSignificantBits(), b.getLeastSignificantBits(),
60 mask.getLeastSignificantBits())
61 && maskedEquals(a.getMostSignificantBits(), b.getMostSignificantBits(),
62 mask.getMostSignificantBits());
63 }
Hugo Benichi9910dbc2017-03-22 18:29:58 +090064
65 public static int[] unpackBits(long val) {
66 int size = Long.bitCount(val);
67 int[] result = new int[size];
68 int index = 0;
69 int bitPos = 0;
70 while (val > 0) {
71 if ((val & 1) == 1) result[index++] = bitPos;
72 val = val >> 1;
73 bitPos++;
74 }
75 return result;
76 }
77
78 public static long packBits(int[] bits) {
79 long packed = 0;
80 for (int b : bits) {
81 packed |= (1 << b);
82 }
83 return packed;
84 }
Hugo Benichi495a17b2017-01-12 15:31:05 +090085
86 public static int uint8(byte b) {
87 return b & 0xff;
88 }
89
90 public static int uint16(short s) {
91 return s & 0xffff;
92 }
93
94 public static long uint32(int i) {
95 return i & 0xffffffffL;
96 }
97
98 public static int bytesToBEInt(byte[] bytes) {
99 return (uint8(bytes[0]) << 24)
100 + (uint8(bytes[1]) << 16)
101 + (uint8(bytes[2]) << 8)
102 + (uint8(bytes[3]));
103 }
104
105 public static int bytesToLEInt(byte[] bytes) {
106 return Integer.reverseBytes(bytesToBEInt(bytes));
107 }
108
109 public static int getUint8(ByteBuffer buffer, int position) {
110 return uint8(buffer.get(position));
111 }
112
113 public static int getUint16(ByteBuffer buffer, int position) {
114 return uint16(buffer.getShort(position));
115 }
116
117 public static long getUint32(ByteBuffer buffer, int position) {
118 return uint32(buffer.getInt(position));
119 }
120
121 public static void put(ByteBuffer buffer, int position, byte[] bytes) {
122 final int original = buffer.position();
123 buffer.position(position);
124 buffer.put(bytes);
125 buffer.position(original);
126 }
Eugene Susla8ab86a62017-02-23 18:24:39 -0800127}