162 lines
4.9 KiB
Java
162 lines
4.9 KiB
Java
package androidx.browser.trusted;
|
|
|
|
import androidx.annotation.NonNull;
|
|
import androidx.annotation.Nullable;
|
|
import java.io.ByteArrayInputStream;
|
|
import java.io.ByteArrayOutputStream;
|
|
import java.io.DataInputStream;
|
|
import java.io.DataOutputStream;
|
|
import java.io.IOException;
|
|
import java.util.ArrayList;
|
|
import java.util.Arrays;
|
|
import java.util.Collections;
|
|
import java.util.List;
|
|
import y.a.b.b;
|
|
public final class TokenContents {
|
|
@NonNull
|
|
private final byte[] mContents;
|
|
@Nullable
|
|
private List<byte[]> mFingerprints;
|
|
@Nullable
|
|
private String mPackageName;
|
|
|
|
private TokenContents(@NonNull byte[] bArr) {
|
|
this.mContents = bArr;
|
|
}
|
|
|
|
private TokenContents(@NonNull byte[] bArr, @NonNull String str, @NonNull List<byte[]> list) {
|
|
this.mContents = bArr;
|
|
this.mPackageName = str;
|
|
this.mFingerprints = new ArrayList(list.size());
|
|
for (byte[] bArr2 : list) {
|
|
this.mFingerprints.add(Arrays.copyOf(bArr2, bArr2.length));
|
|
}
|
|
}
|
|
|
|
public static /* synthetic */ int a(byte[] bArr, byte[] bArr2) {
|
|
return compareByteArrays(bArr, bArr2);
|
|
}
|
|
|
|
private static int compareByteArrays(byte[] bArr, byte[] bArr2) {
|
|
int i;
|
|
int i2;
|
|
if (bArr == bArr2) {
|
|
return 0;
|
|
}
|
|
if (bArr == null) {
|
|
return -1;
|
|
}
|
|
if (bArr2 == null) {
|
|
return 1;
|
|
}
|
|
int i3 = 0;
|
|
while (true) {
|
|
if (i3 < Math.min(bArr.length, bArr2.length)) {
|
|
if (bArr[i3] != bArr2[i3]) {
|
|
i2 = bArr[i3];
|
|
i = bArr2[i3];
|
|
break;
|
|
}
|
|
i3++;
|
|
} else if (bArr.length == bArr2.length) {
|
|
return 0;
|
|
} else {
|
|
i2 = bArr.length;
|
|
i = bArr2.length;
|
|
}
|
|
}
|
|
return i2 - i;
|
|
}
|
|
|
|
@NonNull
|
|
public static TokenContents create(String str, List<byte[]> list) throws IOException {
|
|
return new TokenContents(createToken(str, list), str, list);
|
|
}
|
|
|
|
@NonNull
|
|
private static byte[] createToken(@NonNull String str, @NonNull List<byte[]> list) throws IOException {
|
|
Collections.sort(list, b.i);
|
|
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
|
DataOutputStream dataOutputStream = new DataOutputStream(byteArrayOutputStream);
|
|
dataOutputStream.writeUTF(str);
|
|
dataOutputStream.writeInt(list.size());
|
|
for (byte[] bArr : list) {
|
|
dataOutputStream.writeInt(bArr.length);
|
|
dataOutputStream.write(bArr);
|
|
}
|
|
dataOutputStream.flush();
|
|
return byteArrayOutputStream.toByteArray();
|
|
}
|
|
|
|
@NonNull
|
|
public static TokenContents deserialize(@NonNull byte[] bArr) {
|
|
return new TokenContents(bArr);
|
|
}
|
|
|
|
private void parseIfNeeded() throws IOException {
|
|
if (this.mPackageName == null) {
|
|
DataInputStream dataInputStream = new DataInputStream(new ByteArrayInputStream(this.mContents));
|
|
this.mPackageName = dataInputStream.readUTF();
|
|
int readInt = dataInputStream.readInt();
|
|
this.mFingerprints = new ArrayList(readInt);
|
|
for (int i = 0; i < readInt; i++) {
|
|
int readInt2 = dataInputStream.readInt();
|
|
byte[] bArr = new byte[readInt2];
|
|
if (dataInputStream.read(bArr) == readInt2) {
|
|
this.mFingerprints.add(bArr);
|
|
} else {
|
|
throw new IllegalStateException("Could not read fingerprint");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public boolean equals(Object obj) {
|
|
if (this == obj) {
|
|
return true;
|
|
}
|
|
if (obj == null || TokenContents.class != obj.getClass()) {
|
|
return false;
|
|
}
|
|
return Arrays.equals(this.mContents, ((TokenContents) obj).mContents);
|
|
}
|
|
|
|
@NonNull
|
|
public byte[] getFingerprint(int i) throws IOException {
|
|
parseIfNeeded();
|
|
List<byte[]> list = this.mFingerprints;
|
|
if (list != null) {
|
|
return Arrays.copyOf(list.get(i), this.mFingerprints.get(i).length);
|
|
}
|
|
throw new IllegalStateException();
|
|
}
|
|
|
|
public int getFingerprintCount() throws IOException {
|
|
parseIfNeeded();
|
|
List<byte[]> list = this.mFingerprints;
|
|
if (list != null) {
|
|
return list.size();
|
|
}
|
|
throw new IllegalStateException();
|
|
}
|
|
|
|
@NonNull
|
|
public String getPackageName() throws IOException {
|
|
parseIfNeeded();
|
|
String str = this.mPackageName;
|
|
if (str != null) {
|
|
return str;
|
|
}
|
|
throw new IllegalStateException();
|
|
}
|
|
|
|
public int hashCode() {
|
|
return Arrays.hashCode(this.mContents);
|
|
}
|
|
|
|
@NonNull
|
|
public byte[] serialize() {
|
|
byte[] bArr = this.mContents;
|
|
return Arrays.copyOf(bArr, bArr.length);
|
|
}
|
|
}
|