package org.bouncycastle.bcpg;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.bouncycastle.util.Strings;

/* JADX INFO: loaded from: classes.dex */
public class ArmoredOutputStream extends OutputStream {
    public static final String CHARSET_HDR = "Charset";
    public static final String COMMENT_HDR = "Comment";
    public static final String DEFAULT_VERSION = "BCPG v1.83";
    public static final String HASH_HDR = "Hash";
    public static final String MESSAGE_ID_HDR = "MessageID";
    public static final String VERSION_HDR = "Version";
    private static final byte[] encodingTable = {65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 43, 47};
    byte[] buf;
    int bufPtr;
    int chunkCount;
    boolean clearText;
    CRC24 crc;
    String footerStart;
    String footerTail;
    String headerStart;
    String headerTail;
    final Hashtable<String, List<String>> headers;
    int lastb;
    boolean newLine;
    String nl;
    OutputStream out;
    boolean start;
    String type;

    /* JADX INFO: loaded from: classes2.dex */
    public static class Builder {
        private boolean computeCRCSum;
        private final Map<String, List<String>> headers;

        private Builder() {
            this.headers = new HashMap();
            this.computeCRCSum = true;
        }

        private Builder addHeader(String str, String str2) {
            if (str2 != null && str2.trim().length() != 0) {
                List<String> arrayList = this.headers.get(str);
                if (arrayList == null) {
                    arrayList = new ArrayList<>();
                    this.headers.put(str, arrayList);
                }
                for (String str3 : str2.trim().split("\n")) {
                    String strTrim = str3.trim();
                    if (strTrim.length() != 0) {
                        arrayList.add(strTrim);
                    }
                }
            }
            return this;
        }

        private Builder replaceHeader(String str, String str2) {
            if (str2 != null && str2.trim().length() != 0) {
                ArrayList arrayList = new ArrayList();
                for (String str3 : str2.trim().split("\n")) {
                    String strTrim = str3.trim();
                    if (strTrim.length() != 0) {
                        arrayList.add(strTrim);
                    }
                }
                this.headers.put(str, arrayList);
            }
            return this;
        }

        private Builder setSingletonHeader(String str, String str2) {
            if (str2 == null || str2.trim().length() == 0) {
                this.headers.remove(str);
            } else {
                if (str2.trim().contains("\n")) {
                    throw new IllegalArgumentException("Armor header value for key " + str + " cannot contain newlines.");
                }
                this.headers.put(str, Collections.singletonList(str2));
            }
            return this;
        }

        public Builder addComment(String str) {
            return addHeader(ArmoredOutputStream.COMMENT_HDR, str);
        }

        public Builder addEllipsizedComment(String str) {
            int length = 64 - (ArmoredOutputStream.COMMENT_HDR.length() + 2);
            String strTrim = str.trim();
            if (strTrim.length() > length) {
                strTrim = strTrim.substring(0, length - 1) + (char) 8230;
            }
            addComment(strTrim);
            return this;
        }

        public Builder addSplitMultilineComment(String str) {
            int length = 64 - (ArmoredOutputStream.COMMENT_HDR.length() + 2);
            String strTrim = str.trim();
            String[] strArrSplit = strTrim.split("\n");
            int length2 = strArrSplit.length;
            for (int i = 0; i < length2; i++) {
                String strTrim2 = strArrSplit[i];
                while (strTrim2.length() > length) {
                    addComment(strTrim.substring(0, length));
                    strTrim2 = strTrim2.substring(length).trim();
                }
                if (strTrim2.length() != 0) {
                    addComment(strTrim2);
                }
            }
            return this;
        }

        public ArmoredOutputStream build(OutputStream outputStream) {
            return new ArmoredOutputStream(outputStream, this);
        }

        public Builder clearHeaders() {
            this.headers.clear();
            return this;
        }

        public Builder enableCRC(boolean z) {
            this.computeCRCSum = z;
            return this;
        }

        public Builder setCharset(String str) {
            return replaceHeader(ArmoredOutputStream.CHARSET_HDR, str);
        }

        public Builder setComment(String str) {
            return replaceHeader(ArmoredOutputStream.COMMENT_HDR, str);
        }

        public Builder setMessageId(String str) {
            return replaceHeader(ArmoredOutputStream.MESSAGE_ID_HDR, str);
        }

        public Builder setVersion(String str) {
            return setSingletonHeader(ArmoredOutputStream.VERSION_HDR, str);
        }
    }

    public ArmoredOutputStream(OutputStream outputStream) {
        this.buf = new byte[3];
        this.bufPtr = 0;
        this.crc = new FastCRC24();
        this.chunkCount = 0;
        this.start = true;
        this.clearText = false;
        this.newLine = false;
        this.nl = Strings.lineSeparator();
        this.headerStart = "-----BEGIN PGP ";
        this.headerTail = "-----";
        this.footerStart = "-----END PGP ";
        this.footerTail = "-----";
        this.headers = new Hashtable<>();
        this.out = outputStream;
        if (this.nl == null) {
            this.nl = "\r\n";
        }
        setHeader(VERSION_HDR, DEFAULT_VERSION);
    }

    public ArmoredOutputStream(OutputStream outputStream, Hashtable<String, String> hashtable) {
        this(outputStream);
        Enumeration<String> enumerationKeys = hashtable.keys();
        while (enumerationKeys.hasMoreElements()) {
            String strNextElement = enumerationKeys.nextElement();
            ArrayList arrayList = new ArrayList();
            arrayList.add(hashtable.get(strNextElement));
            this.headers.put(strNextElement, arrayList);
        }
    }

    ArmoredOutputStream(OutputStream outputStream, Builder builder) {
        this(outputStream);
        if (!builder.computeCRCSum) {
            this.crc = null;
        }
        this.headers.clear();
        Map map = builder.headers;
        for (String str : map.keySet()) {
            this.headers.put(str, (List) map.get(str));
        }
    }

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

    private static void encode(OutputStream outputStream, byte[] bArr, int i) throws IOException {
        switch (i) {
            case 1:
                int i2 = bArr[0] & 255;
                outputStream.write(encodingTable[(i2 >>> 2) & 63]);
                outputStream.write(encodingTable[(i2 << 4) & 63]);
                outputStream.write(61);
                break;
            case 2:
                int i3 = bArr[0] & 255;
                int i4 = bArr[1] & 255;
                outputStream.write(encodingTable[(i3 >>> 2) & 63]);
                outputStream.write(encodingTable[((i3 << 4) | (i4 >>> 4)) & 63]);
                outputStream.write(encodingTable[(i4 << 2) & 63]);
                break;
            case 3:
                int i5 = bArr[0] & 255;
                int i6 = bArr[1] & 255;
                int i7 = bArr[2] & 255;
                outputStream.write(encodingTable[(i5 >>> 2) & 63]);
                outputStream.write(encodingTable[((i5 << 4) | (i6 >>> 4)) & 63]);
                outputStream.write(encodingTable[((i6 << 2) | (i7 >>> 6)) & 63]);
                outputStream.write(encodingTable[i7 & 63]);
                return;
            default:
                throw new IOException("unknown length in encode");
        }
        outputStream.write(61);
    }

    private static void encode3(OutputStream outputStream, byte[] bArr) throws IOException {
        int i = bArr[0] & 255;
        int i2 = bArr[1] & 255;
        int i3 = bArr[2] & 255;
        outputStream.write(encodingTable[(i >>> 2) & 63]);
        outputStream.write(encodingTable[((i << 4) | (i2 >>> 4)) & 63]);
        outputStream.write(encodingTable[((i2 << 2) | (i3 >>> 6)) & 63]);
        outputStream.write(encodingTable[i3 & 63]);
    }

    private void write(String str) throws IOException {
        this.out.write(Strings.toUTF8ByteArray(str));
    }

    private void writeHeaderEntry(String str, String str2) throws IOException {
        write(str);
        write(": ");
        write(str2);
        write(this.nl);
    }

    @Deprecated
    public void addHeader(String str, String str2) {
        if (str2 == null || str == null) {
            return;
        }
        List<String> arrayList = this.headers.get(str);
        if (arrayList == null) {
            arrayList = new ArrayList<>();
            this.headers.put(str, arrayList);
        }
        arrayList.add(str2);
    }

    public void beginClearText(int i) throws IOException {
        beginClearText(i);
    }

    public void beginClearText(int... iArr) throws IOException {
        String str;
        StringBuilder sb = new StringBuilder("-----BEGIN PGP SIGNED MESSAGE-----");
        sb.append(this.nl);
        for (int i : iArr) {
            switch (i) {
                case 1:
                    str = "MD5";
                    break;
                case 2:
                    str = "SHA1";
                    break;
                case 3:
                    str = "RIPEMD160";
                    break;
                case 5:
                    str = "MD2";
                    break;
                case 8:
                    str = "SHA256";
                    break;
                case 9:
                    str = "SHA384";
                    break;
                case 10:
                    str = "SHA512";
                    break;
                case 11:
                    str = "SHA224";
                    break;
                case 12:
                case HashAlgorithmTags.SHA3_256_OLD /* 313 */:
                    str = "SHA3-256";
                    break;
                case 14:
                case HashAlgorithmTags.SHA3_512_OLD /* 315 */:
                    str = "SHA3-512";
                    break;
                case HashAlgorithmTags.SHA3_224 /* 312 */:
                    str = "SHA3-224";
                    break;
                case HashAlgorithmTags.SHA3_384 /* 314 */:
                    str = "SHA3-384";
                    break;
                default:
                    throw new IOException("unknown hash algorithm tag in beginClearText: " + i);
            }
            sb.append(HASH_HDR).append(": ").append(str).append(this.nl);
        }
        sb.append(this.nl);
        write(sb.toString());
        this.clearText = true;
        this.newLine = true;
        this.lastb = 0;
    }

    @Deprecated
    public void clearHeaders() {
        this.headers.clear();
    }

    @Override // java.io.OutputStream, java.io.Closeable, java.lang.AutoCloseable
    public void close() throws IOException {
        if (this.type != null) {
            if (this.bufPtr > 0) {
                if (this.crc != null) {
                    for (int i = 0; i < this.bufPtr; i++) {
                        this.crc.update(this.buf[i] & 255);
                    }
                }
                encode(this.out, this.buf, this.bufPtr);
            }
            write(this.nl);
            if (this.crc != null) {
                this.out.write(61);
                int value = this.crc.getValue();
                this.buf[0] = (byte) (value >>> 16);
                this.buf[1] = (byte) (value >>> 8);
                this.buf[2] = (byte) value;
                encode3(this.out, this.buf);
                write(this.nl);
            }
            write(this.footerStart);
            write(this.type);
            write(this.footerTail);
            write(this.nl);
            this.out.flush();
            this.type = null;
            this.start = true;
        }
    }

    public void endClearText() {
        this.clearText = false;
    }

    @Override // java.io.OutputStream, java.io.Flushable
    public void flush() throws IOException {
    }

    @Deprecated
    public void resetHeaders() {
        List<String> list = this.headers.get(VERSION_HDR);
        this.headers.clear();
        if (list != null) {
            this.headers.put(VERSION_HDR, list);
        }
    }

    @Deprecated
    public void setHeader(String str, String str2) {
        Hashtable<String, List<String>> hashtable = this.headers;
        if (str2 == null) {
            hashtable.remove(str);
            return;
        }
        List<String> arrayList = hashtable.get(str);
        if (arrayList == null) {
            arrayList = new ArrayList<>();
            this.headers.put(str, arrayList);
        } else {
            arrayList.clear();
        }
        arrayList.add(str2);
    }

    @Override // java.io.OutputStream
    public void write(int i) throws IOException {
        String str;
        if (this.clearText) {
            this.out.write(i);
            if (this.newLine) {
                if (i != 10 || this.lastb != 13) {
                    this.newLine = false;
                }
                if (i == 45) {
                    this.out.write(32);
                    this.out.write(45);
                }
            }
            if (i == 13 || (i == 10 && this.lastb != 13)) {
                this.newLine = true;
            }
            this.lastb = i;
            return;
        }
        if (this.start) {
            switch ((i & 64) != 0 ? i & 63 : (i & 63) >> 2) {
                case 2:
                    str = "SIGNATURE";
                    break;
                case 3:
                case 4:
                default:
                    str = "MESSAGE";
                    break;
                case 5:
                    str = "PRIVATE KEY BLOCK";
                    break;
                case 6:
                    str = "PUBLIC KEY BLOCK";
                    break;
            }
            this.type = str;
            write(this.headerStart);
            write(this.type);
            write(this.headerTail);
            write(this.nl);
            if (this.headers.containsKey(VERSION_HDR)) {
                writeHeaderEntry(VERSION_HDR, this.headers.get(VERSION_HDR).get(0));
            }
            Enumeration<String> enumerationKeys = this.headers.keys();
            while (enumerationKeys.hasMoreElements()) {
                String strNextElement = enumerationKeys.nextElement();
                if (!strNextElement.equals(VERSION_HDR)) {
                    Iterator<String> it = this.headers.get(strNextElement).iterator();
                    while (it.hasNext()) {
                        writeHeaderEntry(strNextElement, it.next());
                    }
                }
            }
            write(this.nl);
            this.start = false;
        }
        if (this.bufPtr == 3) {
            if (this.crc != null) {
                this.crc.update3(this.buf, 0);
            }
            encode3(this.out, this.buf);
            this.bufPtr = 0;
            int i2 = this.chunkCount + 1;
            this.chunkCount = i2;
            if ((i2 & 15) == 0) {
                write(this.nl);
            }
        }
        byte[] bArr = this.buf;
        int i3 = this.bufPtr;
        this.bufPtr = i3 + 1;
        bArr[i3] = (byte) i;
    }
}
