package org.bouncycastle.util.encoders; import com.google.common.primitives.UnsignedBytes; import java.io.IOException; import java.io.OutputStream; import net.sf.scuba.smartcards.ISO7816; import net.sf.scuba.smartcards.ISOFileInfo; /* loaded from: classes6.dex */ public class HexEncoder implements Encoder { protected final byte[] encodingTable = {ISO7816.INS_DECREASE, 49, ISO7816.INS_INCREASE, 51, ISO7816.INS_DECREASE_STAMPED, 53, 54, 55, 56, 57, 97, ISOFileInfo.FCP_BYTE, 99, ISOFileInfo.FMD_BYTE, 101, 102}; protected final byte[] decodingTable = new byte[128]; private static boolean ignore(char c) { return c == '\n' || c == '\r' || c == '\t' || c == ' '; } protected void initialiseDecodingTable() { int i = 0; int i2 = 0; while (true) { byte[] bArr = this.decodingTable; if (i2 >= bArr.length) { break; } bArr[i2] = -1; i2++; } while (true) { byte[] bArr2 = this.encodingTable; if (i >= bArr2.length) { byte[] bArr3 = this.decodingTable; bArr3[65] = bArr3[97]; bArr3[66] = bArr3[98]; bArr3[67] = bArr3[99]; bArr3[68] = bArr3[100]; bArr3[69] = bArr3[101]; bArr3[70] = bArr3[102]; return; } this.decodingTable[bArr2[i]] = (byte) i; i++; } } @Override // org.bouncycastle.util.encoders.Encoder public int encode(byte[] bArr, int i, int i2, OutputStream outputStream) throws IOException { for (int i3 = i; i3 < i + i2; i3++) { byte b = bArr[i3]; outputStream.write(this.encodingTable[(b & UnsignedBytes.MAX_VALUE) >>> 4]); outputStream.write(this.encodingTable[b & 15]); } return i2 << 1; } @Override // org.bouncycastle.util.encoders.Encoder public int decode(byte[] bArr, int i, int i2, OutputStream outputStream) throws IOException { int i3 = i2 + i; while (i3 > i && ignore((char) bArr[i3 - 1])) { i3--; } int i4 = 0; while (i < i3) { while (i < i3 && ignore((char) bArr[i])) { i++; } int i5 = i + 1; byte b = this.decodingTable[bArr[i]]; while (i5 < i3 && ignore((char) bArr[i5])) { i5++; } byte b2 = this.decodingTable[bArr[i5]]; if ((b | b2) < 0) { throw new IOException("invalid characters encountered in Hex data"); } outputStream.write((b << 4) | b2); i4++; i = i5 + 1; } return i4; } @Override // org.bouncycastle.util.encoders.Encoder public int decode(String str, OutputStream outputStream) throws IOException { int length = str.length(); while (length > 0 && ignore(str.charAt(length - 1))) { length--; } int i = 0; int i2 = 0; while (i < length) { while (i < length && ignore(str.charAt(i))) { i++; } int i3 = i + 1; byte b = this.decodingTable[str.charAt(i)]; while (i3 < length && ignore(str.charAt(i3))) { i3++; } byte b2 = this.decodingTable[str.charAt(i3)]; if ((b | b2) < 0) { throw new IOException("invalid characters encountered in Hex string"); } outputStream.write((b << 4) | b2); i2++; i = i3 + 1; } return i2; } public HexEncoder() { initialiseDecodingTable(); } }