package org.snmp4j.asn1;

import java.io.IOException;
import java.io.OutputStream;
import okhttp3.internal.http2.Http2Stream;
import org.snmp4j.smi.Counter32;

/* JADX INFO: loaded from: classes.dex */
public class BER {
    public static final byte ASN_APPLICATION = 64;
    public static final byte ASN_BIT8 = -128;
    public static final byte ASN_BIT_STR = 3;
    public static final byte ASN_BOOLEAN = 1;
    public static final byte ASN_CONSTRUCTOR = 32;
    public static final byte ASN_CONTEXT = -128;
    public static final byte ASN_EXTENSION_ID = 31;
    public static final byte ASN_INTEGER = 2;
    public static final byte ASN_LONG_LEN = -128;
    public static final byte ASN_NULL = 5;
    public static final byte ASN_OBJECT_ID = 6;
    public static final byte ASN_OCTET_STR = 4;
    public static final byte ASN_PRIMITIVE = 0;
    public static final byte ASN_PRIVATE = -64;
    public static final byte ASN_SEQUENCE = 16;
    public static final byte ASN_SET = 17;
    public static final byte ASN_UNIVERSAL = 0;
    public static final byte BITSTRING = 3;
    public static final byte COUNTER = 65;
    public static final byte COUNTER32 = 65;
    public static final byte COUNTER64 = 70;
    public static final int ENDOFMIBVIEW = 130;
    public static final byte GAUGE = 66;
    public static final byte GAUGE32 = 66;
    public static final byte INTEGER = 2;
    public static final byte INTEGER32 = 2;
    public static final byte IPADDRESS = 64;
    private static final int LENMASK = 255;
    public static final int MAX_OID_LENGTH = 127;
    public static final int NOSUCHINSTANCE = 129;
    public static final int NOSUCHOBJECT = 128;
    public static final byte NULL = 5;
    public static final byte OCTETSTRING = 4;
    public static final byte OID = 6;
    public static final byte OPAQUE = 68;
    public static final byte SEQUENCE = 48;
    public static final byte TIMETICKS = 67;
    private static boolean checkSequenceLength = true;
    private static boolean checkValueLength = true;

    private static void checkLength(BERInputStream bERInputStream, int i2) throws IOException {
        if (checkValueLength) {
            if (i2 < 0 || i2 > bERInputStream.getAvailableBytes()) {
                throw new IOException("The encoded length " + i2 + " exceeds the number of bytes left in input" + getPositionMessage(bERInputStream) + " which actually is " + bERInputStream.getAvailableBytes());
            }
        }
    }

    public static void checkSequenceLength(int i2, BERSerializable bERSerializable) throws IOException {
        if (!isCheckSequenceLength() || i2 == bERSerializable.getBERPayloadLength()) {
            return;
        }
        throw new IOException("The actual length of the SEQUENCE object " + bERSerializable.getClass().getName() + " is " + bERSerializable.getBERPayloadLength() + ", but " + i2 + " was expected");
    }

    public static int decodeHeader(BERInputStream bERInputStream, MutableByte mutableByte, boolean z) throws IOException {
        byte b = (byte) bERInputStream.read();
        if ((b & 31) != 31) {
            mutableByte.setValue(b);
            return decodeLength(bERInputStream, z);
        }
        throw new IOException("Cannot process extension IDs" + getPositionMessage(bERInputStream));
    }

    public static int decodeInteger(BERInputStream bERInputStream, MutableByte mutableByte) throws IOException {
        mutableByte.setValue((byte) bERInputStream.read());
        byte b = mutableByte.value;
        if (b != 2 && b != 67 && b != 65) {
            throw new IOException("Wrong ASN.1 type. Not an integer: " + ((int) mutableByte.value) + getPositionMessage(bERInputStream));
        }
        int iDecodeLength = decodeLength(bERInputStream);
        if (iDecodeLength > 4) {
            throw new IOException("Length greater than 32bit are not supported  for integers: " + getPositionMessage(bERInputStream));
        }
        int i2 = bERInputStream.read() & 255;
        int i3 = (i2 & 128) > 0 ? -1 : 0;
        while (true) {
            int i4 = iDecodeLength - 1;
            if (iDecodeLength <= 0) {
                return i3;
            }
            i3 = (i3 << 8) | i2;
            if (i4 > 0) {
                i2 = bERInputStream.read();
            }
            iDecodeLength = i4;
        }
    }

    public static int decodeLength(BERInputStream bERInputStream) throws IOException {
        return decodeLength(bERInputStream, true);
    }

    public static void decodeNull(BERInputStream bERInputStream, MutableByte mutableByte) throws IOException {
        mutableByte.setValue((byte) (bERInputStream.read() & 255));
        byte b = mutableByte.value;
        if (b != 5 && b != -128 && b != -127 && b != -126) {
            throw new IOException("Wrong ASN.1 type. Is not null: " + ((int) mutableByte.value) + getPositionMessage(bERInputStream));
        }
        int iDecodeLength = decodeLength(bERInputStream);
        if (iDecodeLength == 0) {
            return;
        }
        throw new IOException("Invalid Null encoding, length is not zero: " + iDecodeLength + getPositionMessage(bERInputStream));
    }

    public static int[] decodeOID(BERInputStream bERInputStream, MutableByte mutableByte) throws IOException {
        int i2;
        mutableByte.setValue((byte) bERInputStream.read());
        if (mutableByte.value != 6) {
            throw new IOException("Wrong type. Not an OID: " + ((int) mutableByte.value) + getPositionMessage(bERInputStream));
        }
        int iDecodeLength = decodeLength(bERInputStream);
        int[] iArr = new int[iDecodeLength + 2];
        if (iDecodeLength == 0) {
            iArr[1] = 0;
            iArr[0] = 0;
        }
        int i3 = 1;
        while (iDecodeLength > 0) {
            int i4 = 0;
            do {
                int i5 = bERInputStream.read();
                if (i5 < 0) {
                    throw new IOException("Unexpected end of input stream" + getPositionMessage(bERInputStream));
                }
                i2 = i5 & 255;
                i4 = (i4 << 7) + (i2 & 127);
                iDecodeLength--;
                if (iDecodeLength > 0) {
                }
                iArr[i3] = i4;
                i3++;
            } while ((i2 & (-128)) != 0);
            iArr[i3] = i4;
            i3++;
        }
        int i6 = iArr[1];
        if (i6 == 43) {
            iArr[0] = 1;
            iArr[1] = 3;
        } else if (i6 < 0 || i6 >= 80) {
            iArr[0] = 2;
            iArr[1] = i6 - 80;
        } else if (i6 < 40) {
            iArr[0] = 0;
            iArr[1] = i6;
        } else {
            iArr[0] = 1;
            iArr[1] = i6 - 40;
        }
        if (i3 < 2) {
            i3 = 2;
        }
        int[] iArr2 = new int[i3];
        System.arraycopy(iArr, 0, iArr2, 0, i3);
        return iArr2;
    }

    public static byte[] decodeString(BERInputStream bERInputStream, MutableByte mutableByte) throws IOException {
        mutableByte.setValue((byte) bERInputStream.read());
        byte b = mutableByte.value;
        if (b != 4 && b != 36 && b != 64 && b != 68 && b != 3 && b != 69) {
            throw new IOException("Wrong ASN.1 type. Not a string: " + ((int) mutableByte.value) + getPositionMessage(bERInputStream));
        }
        int iDecodeLength = decodeLength(bERInputStream);
        byte[] bArr = new byte[iDecodeLength];
        int i2 = 0;
        while (i2 < iDecodeLength && bERInputStream.available() > 0) {
            int i3 = bERInputStream.read(bArr);
            if (i3 > 0) {
                i2 += i3;
            } else if (i3 < 0) {
                throw new IOException("Wrong string length " + i3 + " < " + iDecodeLength);
            }
        }
        return bArr;
    }

    public static long decodeUnsignedInt64(BERInputStream bERInputStream, MutableByte mutableByte) throws IOException {
        mutableByte.setValue((byte) bERInputStream.read());
        byte b = mutableByte.value;
        if (b != 2 && b != 70) {
            throw new IOException("Wrong type. Not an integer 64: " + ((int) mutableByte.value) + getPositionMessage(bERInputStream));
        }
        int iDecodeLength = decodeLength(bERInputStream);
        int i2 = bERInputStream.read() & 255;
        if (iDecodeLength > 9) {
            throw new IOException("Invalid 64bit unsigned integer length: " + iDecodeLength + getPositionMessage(bERInputStream));
        }
        if (i2 == 0) {
            if (iDecodeLength > 1) {
                i2 = bERInputStream.read();
            }
            iDecodeLength--;
        }
        long j2 = 0;
        int i3 = 0;
        while (i3 < iDecodeLength) {
            j2 = (j2 << 8) | ((long) (i2 & 255));
            i3++;
            if (i3 < iDecodeLength) {
                i2 = bERInputStream.read();
            }
        }
        return j2;
    }

    public static long decodeUnsignedInteger(BERInputStream bERInputStream, MutableByte mutableByte) throws IOException {
        mutableByte.setValue((byte) bERInputStream.read());
        byte b = mutableByte.value;
        if (b != 2 && b != 67 && b != 65 && b != 66 && b != 71) {
            throw new IOException("Wrong ASN.1 type. Not an unsigned integer: " + ((int) mutableByte.value) + getPositionMessage(bERInputStream));
        }
        int iDecodeLength = decodeLength(bERInputStream);
        int i2 = bERInputStream.read();
        if (iDecodeLength > 5 || (iDecodeLength > 4 && i2 != 0)) {
            throw new IOException("Only 32bit unsigned integers are supported" + getPositionMessage(bERInputStream));
        }
        if (i2 == 0) {
            if (iDecodeLength > 1) {
                i2 = bERInputStream.read();
            }
            iDecodeLength--;
        }
        int i3 = 0;
        long j2 = 0;
        while (i3 < iDecodeLength) {
            j2 = (j2 << 8) | ((long) (i2 & 255));
            i3++;
            if (i3 < iDecodeLength) {
                i2 = bERInputStream.read();
            }
        }
        return j2;
    }

    public static void encodeHeader(OutputStream outputStream, int i2, int i3) throws IOException {
        outputStream.write(i2);
        encodeLength(outputStream, i3);
    }

    public static void encodeInteger(OutputStream outputStream, byte b, int i2) throws IOException {
        int i3 = 4;
        while (true) {
            int i4 = i2 & (-8388608);
            if ((i4 != 0 && i4 != -8388608) || i3 <= 1) {
                break;
            }
            i3--;
            i2 <<= 8;
        }
        encodeHeader(outputStream, b, i3);
        while (true) {
            int i5 = i3 - 1;
            if (i3 <= 0) {
                return;
            }
            outputStream.write((i2 & (-16777216)) >> 24);
            i2 <<= 8;
            i3 = i5;
        }
    }

    public static void encodeLength(OutputStream outputStream, int i2) throws IOException {
        if (i2 < 0) {
            outputStream.write(-124);
            outputStream.write((i2 >> 24) & 255);
            outputStream.write((i2 >> 16) & 255);
            outputStream.write((i2 >> 8) & 255);
            outputStream.write(i2 & 255);
            return;
        }
        if (i2 < 128) {
            outputStream.write(i2);
            return;
        }
        if (i2 <= 255) {
            outputStream.write(-127);
            outputStream.write(i2);
            return;
        }
        if (i2 <= 65535) {
            outputStream.write(-126);
            outputStream.write((i2 >> 8) & 255);
            outputStream.write(i2 & 255);
        } else {
            if (i2 <= 16777215) {
                outputStream.write(-125);
                outputStream.write((i2 >> 16) & 255);
                outputStream.write((i2 >> 8) & 255);
                outputStream.write(i2 & 255);
                return;
            }
            outputStream.write(-124);
            outputStream.write((i2 >> 24) & 255);
            outputStream.write((i2 >> 16) & 255);
            outputStream.write((i2 >> 8) & 255);
            outputStream.write(i2 & 255);
        }
    }

    public static void encodeOID(OutputStream outputStream, byte b, int[] iArr) throws IOException {
        encodeHeader(outputStream, b, getOIDLength(iArr));
        int length = iArr.length;
        int i2 = 2;
        int i3 = 0;
        if (iArr.length < 2) {
            outputStream.write(0);
            i2 = 0;
        } else {
            int i4 = iArr[0];
            if (i4 < 0 || i4 > 2) {
                throw new IOException("Invalid first sub-identifier (must be 0, 1, or 2)");
            }
            encodeSubID(outputStream, iArr[1] + (i4 * 40));
            i3 = length - 2;
        }
        while (true) {
            int i5 = i3 - 1;
            if (i3 <= 0) {
                return;
            }
            encodeSubID(outputStream, iArr[i2]);
            i3 = i5;
            i2++;
        }
    }

    public static void encodeSequence(OutputStream outputStream, byte b, int i2) throws IOException {
        outputStream.write(b);
        encodeLength(outputStream, i2);
    }

    public static void encodeString(OutputStream outputStream, byte b, byte[] bArr) throws IOException {
        encodeHeader(outputStream, b, bArr.length);
        outputStream.write(bArr);
    }

    private static void encodeSubID(OutputStream outputStream, int i2) throws IOException {
        long j2 = ((long) i2) & Counter32.MAX_COUNTER32_VALUE;
        if (j2 < 127) {
            outputStream.write(((int) j2) & 255);
            return;
        }
        long j3 = 127;
        long j4 = 127;
        long j5 = 0;
        long j6 = 0;
        while (j3 != 0) {
            if ((j2 & j3) > 0) {
                j4 = j3;
                j6 = j5;
            }
            j3 <<= 7;
            j5 += 7;
        }
        while (j4 != 127) {
            if (j4 == 31457280) {
                j4 = 266338304;
            }
            outputStream.write((int) (((j2 & j4) >> ((int) j6)) | (-128)));
            j4 >>= 7;
            j6 -= 7;
        }
        outputStream.write((int) (j2 & j4));
    }

    public static void encodeUnsignedInt64(OutputStream outputStream, byte b, long j2) throws IOException {
        int i2 = 8;
        while (i2 > 1 && ((j2 >> ((i2 - 1) * 8)) & 255) == 0) {
            i2--;
        }
        if (((j2 >> ((i2 - 1) * 8)) & 128) != 0) {
            i2++;
        }
        encodeHeader(outputStream, b, i2);
        if (i2 == 9) {
            outputStream.write(0);
            i2--;
        }
        for (int i3 = 0; i3 < i2; i3++) {
            outputStream.write((int) (j2 >> ((((i2 - 1) - i3) * 8) & 255)));
        }
    }

    public static void encodeUnsignedInteger(OutputStream outputStream, byte b, long j2) throws IOException {
        int i2 = ((j2 >> 24) & 255) != 0 ? 4 : ((j2 >> 16) & 255) != 0 ? 3 : ((j2 >> 8) & 255) != 0 ? 2 : 1;
        if (((j2 >> ((i2 - 1) * 8)) & 128) != 0) {
            i2++;
        }
        encodeHeader(outputStream, b, i2);
        if (i2 != 5) {
            for (int i3 = 0; i3 < i2; i3++) {
                outputStream.write((int) (j2 >> ((((i2 - 1) - i3) * 8) & 255)));
            }
        } else {
            outputStream.write(0);
            for (int i4 = 1; i4 < i2; i4++) {
                outputStream.write((int) (j2 >> (((4 - i4) * 8) & 255)));
            }
        }
    }

    public static int getBERLengthOfLength(int i2) {
        if (i2 < 0) {
            return 5;
        }
        if (i2 < 128) {
            return 1;
        }
        if (i2 <= 255) {
            return 2;
        }
        if (i2 <= 65535) {
            return 3;
        }
        return i2 <= 16777215 ? 4 : 5;
    }

    public static int getOIDLength(int[] iArr) {
        int subIDLength = iArr.length > 1 ? getSubIDLength((iArr[0] * 40) + iArr[1]) : 1;
        for (int i2 = 2; i2 < iArr.length; i2++) {
            subIDLength += getSubIDLength(iArr[i2]);
        }
        return subIDLength;
    }

    private static String getPositionMessage(BERInputStream bERInputStream) {
        return " at position " + bERInputStream.getPosition();
    }

    private static int getSubIDLength(int i2) {
        long j2 = ((long) i2) & Counter32.MAX_COUNTER32_VALUE;
        if (j2 < 128) {
            return 1;
        }
        if (j2 < Http2Stream.EMIT_BUFFER_SIZE) {
            return 2;
        }
        if (j2 < 2097152) {
            return 3;
        }
        return j2 < 268435456 ? 4 : 5;
    }

    public static boolean isCheckSequenceLength() {
        return checkSequenceLength;
    }

    public static void setCheckSequenceLength(boolean z) {
        checkSequenceLength = z;
    }

    public boolean isCheckValueLength() {
        return checkValueLength;
    }

    public void setCheckValueLength(boolean z) {
        checkValueLength = z;
    }

    public static class MutableByte {
        byte value = 0;

        public MutableByte() {
        }

        public byte getValue() {
            return this.value;
        }

        public void setValue(byte b) {
            this.value = b;
        }

        public MutableByte(byte b) {
            setValue(b);
        }
    }

    public static int decodeLength(BERInputStream bERInputStream, boolean z) throws IOException {
        int i2;
        int i3 = bERInputStream.read();
        if ((i3 & (-128)) > 0) {
            int i4 = i3 & 127;
            if (i4 == 0) {
                throw new IOException("Indefinite lengths are not supported");
            }
            if (i4 > 4) {
                throw new IOException("Data length > 4 bytes are not supported!");
            }
            i2 = 0;
            for (int i5 = 0; i5 < i4; i5++) {
                i2 |= (bERInputStream.read() & 255) << (((i4 - 1) - i5) * 8);
            }
            if (i2 < 0) {
                throw new IOException("SNMP does not support data lengths > 2^31");
            }
        } else {
            i2 = i3 & 255;
        }
        if (z) {
            checkLength(bERInputStream, i2);
        }
        return i2;
    }

    public static void encodeHeader(OutputStream outputStream, int i2, int i3, int i4) throws IOException {
        outputStream.write(i2);
        encodeLength(outputStream, i3, i4);
    }

    public static void checkSequenceLength(int i2, int i3, BERSerializable bERSerializable) throws IOException {
        if (!isCheckSequenceLength() || i2 == i3) {
            return;
        }
        throw new IOException("The actual length of the SEQUENCE object " + bERSerializable.getClass().getName() + " is " + i3 + ", but " + i2 + " was expected");
    }

    public static int decodeHeader(BERInputStream bERInputStream, MutableByte mutableByte) throws IOException {
        return decodeHeader(bERInputStream, mutableByte, true);
    }

    public static void encodeLength(OutputStream outputStream, int i2, int i3) throws IOException {
        outputStream.write(i3 | (-128));
        for (int i4 = (i3 - 1) * 8; i4 >= 0; i4 -= 8) {
            outputStream.write((i2 >> i4) & 255);
        }
    }
}
