package com.android.org.conscrypt.ct;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;

/* JADX INFO: loaded from: classes.dex */
public class Serialization {
    private static final int DER_LENGTH_LONG_FORM_FLAG = 128;
    private static final int DER_TAG_MASK = 63;
    private static final int DER_TAG_OCTET_STRING = 4;

    private Serialization() {
    }

    public static byte readByte(InputStream inputStream) throws SerializationException {
        try {
            int i3 = inputStream.read();
            if (i3 != -1) {
                return (byte) i3;
            }
            throw new SerializationException("Premature end of input, could not read byte.");
        } catch (IOException e3) {
            throw new SerializationException(e3);
        }
    }

    public static byte[] readDEROctetString(byte[] bArr) throws SerializationException {
        return readDEROctetString(new ByteArrayInputStream(bArr));
    }

    public static byte[] readFixedBytes(InputStream inputStream, int i3) throws SerializationException {
        try {
            if (i3 < 0) {
                throw new SerializationException("Negative length: " + i3);
            }
            byte[] bArr = new byte[i3];
            int i4 = inputStream.read(bArr);
            if (i4 >= i3) {
                return bArr;
            }
            throw new SerializationException("Premature end of input, expected " + i3 + " bytes, only read " + i4);
        } catch (IOException e3) {
            throw new SerializationException(e3);
        }
    }

    public static byte[][] readList(byte[] bArr, int i3, int i4) throws SerializationException {
        return readList(new ByteArrayInputStream(bArr), i3, i4);
    }

    public static long readLong(InputStream inputStream, int i3) throws SerializationException {
        if (i3 > 8 || i3 < 0) {
            throw new IllegalArgumentException("Invalid width: " + i3);
        }
        long j3 = 0;
        for (int i4 = 0; i4 < i3; i4++) {
            j3 = (j3 << 8) | ((long) (readByte(inputStream) & 255));
        }
        return j3;
    }

    public static int readNumber(InputStream inputStream, int i3) throws SerializationException {
        if (i3 > DER_TAG_OCTET_STRING || i3 < 0) {
            throw new SerializationException("Invalid width: " + i3);
        }
        int i4 = 0;
        for (int i5 = 0; i5 < i3; i5++) {
            i4 = (i4 << 8) | (readByte(inputStream) & 255);
        }
        return i4;
    }

    public static byte[] readVariableBytes(InputStream inputStream, int i3) throws SerializationException {
        return readFixedBytes(inputStream, readNumber(inputStream, i3));
    }

    public static void writeFixedBytes(OutputStream outputStream, byte[] bArr) throws SerializationException {
        try {
            outputStream.write(bArr);
        } catch (IOException e3) {
            throw new SerializationException(e3);
        }
    }

    public static void writeNumber(OutputStream outputStream, long j3, int i3) throws SerializationException {
        if (i3 < 0) {
            throw new SerializationException("Negative width: " + i3);
        }
        if (i3 < 8 && j3 >= (1 << (i3 * 8))) {
            throw new SerializationException("Number too large, " + j3 + " does not fit in " + i3 + " bytes");
        }
        while (i3 > 0) {
            if (((long) (i3 - 1)) * 8 < 64) {
                try {
                    outputStream.write((byte) ((j3 >> ((int) r0)) & 255));
                } catch (IOException e3) {
                    throw new SerializationException(e3);
                }
            } else {
                outputStream.write(0);
            }
            i3--;
        }
    }

    public static void writeVariableBytes(OutputStream outputStream, byte[] bArr, int i3) throws SerializationException {
        writeNumber(outputStream, bArr.length, i3);
        writeFixedBytes(outputStream, bArr);
    }

    public static byte[] readDEROctetString(InputStream inputStream) throws SerializationException {
        int i3 = readByte(inputStream) & 63;
        if (i3 == DER_TAG_OCTET_STRING) {
            int number = readNumber(inputStream, 1);
            if ((number & 128) != 0) {
                number = readNumber(inputStream, number & (-129));
            }
            return readFixedBytes(inputStream, number);
        }
        throw new SerializationException("Wrong DER tag, expected OCTET STRING, got " + i3);
    }

    public static byte[][] readList(InputStream inputStream, int i3, int i4) throws SerializationException {
        ArrayList arrayList = new ArrayList();
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(readVariableBytes(inputStream, i3));
        while (byteArrayInputStream.available() > 0) {
            try {
                arrayList.add(readVariableBytes(byteArrayInputStream, i4));
            } catch (IOException e3) {
                throw new SerializationException(e3);
            }
        }
        return (byte[][]) arrayList.toArray(new byte[arrayList.size()][]);
    }
}
