package org.bouncycastle.bcpg;

import java.io.ByteArrayOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.bouncycastle.util.StringList;
import org.bouncycastle.util.Strings;

/* JADX INFO: loaded from: classes.dex */
public class ArmoredInputStream extends InputStream {
    private static final byte[] decodingTable = new byte[128];
    private List<String> allowedHeaders;
    int bufPtr;
    boolean clearText;
    private final CRC24 crc;
    boolean crcFound;
    private boolean detectMissingChecksum;
    boolean hasHeaders;
    String header;
    StringList headerList;
    InputStream in;
    boolean isEndOfStream;
    int lastC;
    boolean newLineFound;
    byte[] outBuf;
    boolean restart;
    boolean start;
    private boolean validateAllowedHeaders;

    /* JADX INFO: loaded from: classes2.dex */
    public static class Builder {
        private List<String> allowedHeaders;
        private boolean detectMissingCRC;
        private boolean hasHeaders;
        private boolean ignoreCRC;
        private boolean validateAllowedHeaders;

        private Builder() {
            this.hasHeaders = true;
            this.detectMissingCRC = false;
            this.ignoreCRC = false;
            this.validateAllowedHeaders = false;
            this.allowedHeaders = ArmoredInputStream.defaultAllowedHeaders();
        }

        public Builder addAllowedArmorHeader(String str) {
            this.allowedHeaders.add(str.trim());
            return this;
        }

        public ArmoredInputStream build(InputStream inputStream) throws IOException {
            return new ArmoredInputStream(inputStream, this);
        }

        public Builder setDetectMissingCRC(boolean z) {
            this.detectMissingCRC = z;
            return this;
        }

        public Builder setIgnoreCRC(boolean z) {
            this.ignoreCRC = z;
            return this;
        }

        public Builder setParseForHeaders(boolean z) {
            this.hasHeaders = z;
            return this;
        }

        public Builder setValidateClearsignedMessageHeaders(boolean z) {
            this.validateAllowedHeaders = z;
            return this;
        }
    }

    static {
        for (int i = 0; i < decodingTable.length; i++) {
            decodingTable[i] = -1;
        }
        for (int i2 = 65; i2 <= 90; i2++) {
            decodingTable[i2] = (byte) (i2 - 65);
        }
        for (int i3 = 97; i3 <= 122; i3++) {
            decodingTable[i3] = (byte) ((i3 - 97) + 26);
        }
        for (int i4 = 48; i4 <= 57; i4++) {
            decodingTable[i4] = (byte) ((i4 - 48) + 52);
        }
        decodingTable[43] = 62;
        decodingTable[47] = 63;
    }

    public ArmoredInputStream(InputStream inputStream) throws IOException {
        this(inputStream, true);
    }

    private ArmoredInputStream(InputStream inputStream, Builder builder) throws IOException {
        this.detectMissingChecksum = false;
        this.start = true;
        this.outBuf = new byte[3];
        this.bufPtr = 3;
        this.crcFound = false;
        this.hasHeaders = true;
        this.header = null;
        this.newLineFound = false;
        this.clearText = false;
        this.restart = false;
        this.headerList = Strings.newList();
        this.lastC = 0;
        this.validateAllowedHeaders = false;
        this.allowedHeaders = defaultAllowedHeaders();
        this.in = inputStream;
        this.hasHeaders = builder.hasHeaders;
        this.detectMissingChecksum = builder.detectMissingCRC;
        this.crc = builder.ignoreCRC ? null : new FastCRC24();
        this.validateAllowedHeaders = builder.validateAllowedHeaders;
        this.allowedHeaders = builder.allowedHeaders;
        if (this.hasHeaders) {
            parseHeaders();
        }
        if (this.validateAllowedHeaders) {
            rejectUnknownHeadersInCSFMessages();
        }
        this.start = false;
    }

    public ArmoredInputStream(InputStream inputStream, boolean z) throws IOException {
        this.detectMissingChecksum = false;
        this.start = true;
        this.outBuf = new byte[3];
        this.bufPtr = 3;
        this.crcFound = false;
        this.hasHeaders = true;
        this.header = null;
        this.newLineFound = false;
        this.clearText = false;
        this.restart = false;
        this.headerList = Strings.newList();
        this.lastC = 0;
        this.validateAllowedHeaders = false;
        this.allowedHeaders = defaultAllowedHeaders();
        this.in = inputStream;
        this.hasHeaders = z;
        this.crc = new FastCRC24();
        if (z) {
            parseHeaders();
        }
        this.start = false;
    }

    public static Builder builder() {
        return new Builder();
    }

    private void checkIndexSize(int i, int i2, int i3) {
        if (i2 < 0 || i3 < 0) {
            throw new IndexOutOfBoundsException("Offset and length cannot be negative.");
        }
        if (i2 > i - i3) {
            throw new IndexOutOfBoundsException("Invalid offset and length.");
        }
    }

    private static int decode(int i, int i2, int i3, int i4, byte[] bArr) throws IOException {
        if (i4 < 0) {
            throw new EOFException("unexpected end of file in armored stream.");
        }
        if (i3 == 61) {
            int i5 = decodingTable[i] & 255;
            int i6 = decodingTable[i2] & 255;
            if ((i5 | i6) < 0) {
                throw new ArmoredInputException("invalid armor");
            }
            bArr[2] = (byte) ((i5 << 2) | (i6 >> 4));
            return 2;
        }
        if (i4 == 61) {
            byte b = decodingTable[i];
            byte b2 = decodingTable[i2];
            byte b3 = decodingTable[i3];
            if ((b | b2 | b3) < 0) {
                throw new ArmoredInputException("invalid armor");
            }
            bArr[1] = (byte) ((b << 2) | (b2 >> 4));
            bArr[2] = (byte) ((b2 << 4) | (b3 >> 2));
            return 1;
        }
        byte b4 = decodingTable[i];
        byte b5 = decodingTable[i2];
        byte b6 = decodingTable[i3];
        byte b7 = decodingTable[i4];
        if ((b4 | b5 | b6 | b7) < 0) {
            throw new ArmoredInputException("invalid armor");
        }
        bArr[0] = (byte) ((b4 << 2) | (b5 >> 4));
        bArr[1] = (byte) ((b5 << 4) | (b6 >> 2));
        bArr[2] = (byte) ((b6 << 6) | b7);
        return 0;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static List<String> defaultAllowedHeaders() {
        ArrayList arrayList = new ArrayList();
        arrayList.add(ArmoredOutputStream.COMMENT_HDR);
        arrayList.add(ArmoredOutputStream.VERSION_HDR);
        arrayList.add(ArmoredOutputStream.CHARSET_HDR);
        arrayList.add(ArmoredOutputStream.HASH_HDR);
        arrayList.add(ArmoredOutputStream.MESSAGE_ID_HDR);
        return arrayList;
    }

    private boolean parseHeaders() throws IOException {
        int i;
        boolean z;
        this.header = null;
        this.headerList = Strings.newList();
        if (!this.restart) {
            i = 0;
            while (true) {
                int i2 = this.in.read();
                if (i2 < 0) {
                    z = false;
                    break;
                }
                if (i2 == 45 && (i == 0 || i == 10 || i == 13)) {
                    z = true;
                    break;
                }
                i = i2;
            }
        } else {
            z = true;
            i = 0;
        }
        if (z) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            byteArrayOutputStream.write(45);
            if (this.restart) {
                byteArrayOutputStream.write(45);
            }
            boolean z2 = false;
            boolean z3 = false;
            while (true) {
                int i3 = this.in.read();
                if (i3 >= 0) {
                    if (i == 13 && i3 == 10) {
                        z3 = true;
                    }
                    if ((z2 && i != 13 && i3 == 10) || (z2 && i3 == 13)) {
                        break;
                    }
                    if (i3 == 13 || (i != 13 && i3 == 10)) {
                        try {
                            String strFromUTF8ByteArray = Strings.fromUTF8ByteArray(byteArrayOutputStream.toByteArray());
                            if (strFromUTF8ByteArray.trim().length() == 0) {
                                break;
                            }
                            if (this.headerList.size() != 0 && strFromUTF8ByteArray.indexOf(58) < 0) {
                                throw new ArmoredInputException("invalid armor header");
                            }
                            this.headerList.add(strFromUTF8ByteArray);
                            byteArrayOutputStream.reset();
                        } catch (Exception e) {
                            throw new ArmoredInputException(e.getMessage());
                        }
                    }
                    if (i3 != 10 && i3 != 13) {
                        byteArrayOutputStream.write(i3);
                        z2 = false;
                    } else if (i3 == 13 || (i != 13 && i3 == 10)) {
                        z2 = true;
                    }
                    i = i3;
                } else {
                    break;
                }
            }
            if (z3 && this.in.read() != 10) {
                throw new ArmoredInputException("inconsistent line endings in headers");
            }
        }
        if (this.headerList.size() > 0) {
            this.header = this.headerList.get(0);
        }
        this.clearText = "-----BEGIN PGP SIGNED MESSAGE-----".equals(this.header);
        this.newLineFound = true;
        return z;
    }

    private int readIgnoreSpace() throws IOException {
        int i;
        while (true) {
            i = this.in.read();
            if (i != 32 && i != 9 && i != 12 && i != 11) {
                break;
            }
        }
        if (i < 128) {
            return i;
        }
        throw new ArmoredInputException("invalid armor");
    }

    private void rejectUnknownHeadersInCSFMessages() throws ArmoredInputException {
        Iterator<String> it = this.headerList.iterator();
        if (it.next().startsWith("-----BEGIN PGP SIGNED MESSAGE-----")) {
            while (it.hasNext()) {
                String next = it.next();
                Iterator<String> it2 = this.allowedHeaders.iterator();
                do {
                    if (!it2.hasNext()) {
                        throw new ArmoredInputException("Illegal ASCII armor header line in clearsigned message encountered: " + next);
                    }
                } while (!next.startsWith(it2.next() + ": "));
            }
        }
    }

    @Override // java.io.InputStream
    public int available() throws IOException {
        return this.in.available();
    }

    @Override // java.io.InputStream, java.io.Closeable, java.lang.AutoCloseable
    public void close() throws IOException {
        this.in.close();
    }

    public String getArmorHeaderLine() {
        return this.header;
    }

    public String[] getArmorHeaders() {
        if (this.headerList.size() <= 1) {
            return null;
        }
        return this.headerList.toStringArray(1, this.headerList.size());
    }

    public boolean isClearText() {
        return this.clearText;
    }

    public boolean isEndOfStream() {
        return this.isEndOfStream;
    }

    @Override // java.io.InputStream
    public int read() throws IOException {
        int i;
        if (this.start) {
            if (this.hasHeaders) {
                parseHeaders();
            }
            if (this.crc != null) {
                this.crc.reset();
            }
            this.start = false;
        }
        if (this.clearText) {
            int i2 = this.in.read();
            if (i2 == 13 || (i2 == 10 && this.lastC != 13)) {
                this.newLineFound = true;
            } else {
                if (this.newLineFound && i2 == 45) {
                    i2 = this.in.read();
                    if (i2 == 45) {
                        this.clearText = false;
                        this.start = true;
                        this.restart = true;
                    } else {
                        i2 = this.in.read();
                    }
                } else if (i2 != 10 && this.lastC != 13) {
                }
                this.newLineFound = false;
            }
            this.lastC = i2;
            if (i2 < 0) {
                this.isEndOfStream = true;
            }
            return i2;
        }
        if (this.bufPtr > 2 || this.crcFound) {
            int ignoreSpace = readIgnoreSpace();
            if (ignoreSpace == 13 || ignoreSpace == 10) {
                while (true) {
                    ignoreSpace = readIgnoreSpace();
                    if (ignoreSpace != 10 && ignoreSpace != 13) {
                        break;
                    }
                }
                if (ignoreSpace == 61) {
                    this.bufPtr = decode(readIgnoreSpace(), readIgnoreSpace(), readIgnoreSpace(), readIgnoreSpace(), this.outBuf);
                    if (this.bufPtr != 0) {
                        throw new ArmoredInputException("malformed crc in armored message");
                    }
                    this.crcFound = true;
                    if (this.crc == null || (((this.outBuf[0] & 255) << 16) | ((this.outBuf[1] & 255) << 8) | (this.outBuf[2] & 255)) == this.crc.getValue()) {
                        return read();
                    }
                    throw new ArmoredInputException("crc check failed in armored message");
                }
                if (ignoreSpace == 45) {
                    do {
                        i = this.in.read();
                        if (i < 0 || i == 10) {
                            break;
                        }
                    } while (i != 13);
                    if (!this.crcFound && this.detectMissingChecksum) {
                        throw new ArmoredInputException("crc check not found");
                    }
                    this.crcFound = false;
                    this.start = true;
                    this.bufPtr = 3;
                    if (i < 0) {
                        this.isEndOfStream = true;
                    }
                    return -1;
                }
            }
            if (ignoreSpace < 0) {
                this.isEndOfStream = true;
                return -1;
            }
            this.bufPtr = decode(ignoreSpace, readIgnoreSpace(), readIgnoreSpace(), readIgnoreSpace(), this.outBuf);
            if (this.crc != null) {
                if (this.bufPtr == 0) {
                    this.crc.update3(this.outBuf, 0);
                } else {
                    for (int i3 = this.bufPtr; i3 < 3; i3++) {
                        this.crc.update(this.outBuf[i3] & 255);
                    }
                }
            }
        }
        byte[] bArr = this.outBuf;
        int i4 = this.bufPtr;
        this.bufPtr = i4 + 1;
        return bArr[i4] & 255;
    }

    @Override // java.io.InputStream
    public int read(byte[] bArr, int i, int i2) throws IOException {
        checkIndexSize(bArr.length, i, i2);
        if (i2 == 0) {
            return 0;
        }
        int i3 = read();
        if (i3 == -1) {
            return -1;
        }
        bArr[i] = (byte) i3;
        int i4 = 1;
        while (i4 < i2) {
            int i5 = read();
            if (i5 == -1) {
                break;
            }
            bArr[i + i4] = (byte) i5;
            i4++;
        }
        return i4;
    }

    public void setDetectMissingCRC(boolean z) {
        this.detectMissingChecksum = z;
    }
}
