package io.netty.handler.codec.base64;

import g.a.a.a.a;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.handler.codec.dns.DnsRecord;
import io.netty.handler.codec.http2.Http2CodecUtil;
import io.netty.handler.codec.memcache.binary.BinaryMemcacheOpcodes;
import io.netty.util.ByteProcessor;
import io.netty.util.internal.PlatformDependent;
import java.nio.ByteOrder;
import java.util.Objects;
import kotlinx.serialization.json.internal.AbstractJsonLexerKt;

/* JADX INFO: loaded from: classes.dex */
public final class Base64 {
    private static final byte EQUALS_SIGN = 61;
    private static final byte EQUALS_SIGN_ENC = -1;
    private static final int MAX_LINE_LENGTH = 76;
    private static final byte NEW_LINE = 10;
    private static final byte WHITE_SPACE_ENC = -5;

    public static final class Decoder implements ByteProcessor {
        private final byte[] b4;
        private int b4Posn;
        private byte[] decodabet;
        private ByteBuf dest;
        private int outBuffPosn;
        private byte sbiCrop;
        private byte sbiDecode;

        private Decoder() {
            this.b4 = new byte[4];
        }

        private static int decode4to3(byte[] bArr, ByteBuf byteBuf, int i2, byte[] bArr2) {
            int i3;
            int i4;
            int i5;
            byte b2 = bArr[0];
            byte b3 = bArr[1];
            byte b4 = bArr[2];
            if (b4 == 61) {
                try {
                    byteBuf.setByte(i2, ((bArr2[b2] & Base64.EQUALS_SIGN_ENC) << 2) | ((bArr2[b3] & Base64.EQUALS_SIGN_ENC) >>> 4));
                    return 1;
                } catch (IndexOutOfBoundsException unused) {
                    throw new IllegalArgumentException("not encoded in Base64");
                }
            }
            byte b5 = bArr[3];
            if (b5 == 61) {
                byte b6 = bArr2[b3];
                try {
                    if (byteBuf.order() == ByteOrder.BIG_ENDIAN) {
                        i5 = ((b6 & BinaryMemcacheOpcodes.PREPEND) << 4) | ((((bArr2[b2] & 63) << 2) | ((b6 & 240) >> 4)) << 8) | ((bArr2[b4] & 252) >>> 2);
                    } else {
                        i5 = ((((b6 & BinaryMemcacheOpcodes.PREPEND) << 4) | ((bArr2[b4] & 252) >>> 2)) << 8) | ((bArr2[b2] & 63) << 2) | ((b6 & 240) >> 4);
                    }
                    byteBuf.setShort(i2, i5);
                    return 2;
                } catch (IndexOutOfBoundsException unused2) {
                    throw new IllegalArgumentException("not encoded in Base64");
                }
            }
            try {
                if (byteBuf.order() == ByteOrder.BIG_ENDIAN) {
                    i3 = ((bArr2[b2] & 63) << 18) | ((bArr2[b3] & Base64.EQUALS_SIGN_ENC) << 12) | ((bArr2[b4] & Base64.EQUALS_SIGN_ENC) << 6);
                    i4 = bArr2[b5] & Base64.EQUALS_SIGN_ENC;
                } else {
                    byte b7 = bArr2[b3];
                    byte b8 = bArr2[b4];
                    i3 = ((bArr2[b2] & 63) << 2) | ((b7 & BinaryMemcacheOpcodes.PREPEND) << 12) | ((b7 & 240) >>> 4) | ((b8 & 3) << 22) | ((b8 & 252) << 6);
                    i4 = (bArr2[b5] & Base64.EQUALS_SIGN_ENC) << 16;
                }
                byteBuf.setMedium(i2, i4 | i3);
                return 3;
            } catch (IndexOutOfBoundsException unused3) {
                throw new IllegalArgumentException("not encoded in Base64");
            }
        }

        public ByteBuf decode(ByteBuf byteBuf, int i2, int i3, ByteBufAllocator byteBufAllocator, Base64Dialect base64Dialect) {
            this.dest = byteBufAllocator.buffer(Base64.decodedBufferSize(i3)).order(byteBuf.order());
            this.decodabet = Base64.decodabet(base64Dialect);
            try {
                byteBuf.forEachByte(i2, i3, this);
                return this.dest.slice(0, this.outBuffPosn);
            } catch (Throwable th) {
                this.dest.release();
                PlatformDependent.throwException(th);
                return null;
            }
        }

        @Override // io.netty.util.ByteProcessor
        public boolean process(byte b2) {
            byte b3 = (byte) (b2 & AbstractJsonLexerKt.TC_INVALID);
            this.sbiCrop = b3;
            byte[] bArr = this.decodabet;
            byte b4 = bArr[b3];
            this.sbiDecode = b4;
            if (b4 < -5) {
                throw new IllegalArgumentException(a.y(a.F("invalid bad Base64 input character: "), (short) (b2 & Base64.EQUALS_SIGN_ENC), " (decimal)"));
            }
            if (b4 < -1) {
                return true;
            }
            byte[] bArr2 = this.b4;
            int i2 = this.b4Posn;
            int i3 = i2 + 1;
            this.b4Posn = i3;
            bArr2[i2] = b3;
            if (i3 <= 3) {
                return true;
            }
            int i4 = this.outBuffPosn;
            this.outBuffPosn = i4 + decode4to3(bArr2, this.dest, i4, bArr);
            this.b4Posn = 0;
            return this.sbiCrop != 61;
        }
    }

    private Base64() {
    }

    private static byte[] alphabet(Base64Dialect base64Dialect) {
        Objects.requireNonNull(base64Dialect, "dialect");
        return base64Dialect.alphabet;
    }

    private static boolean breakLines(Base64Dialect base64Dialect) {
        Objects.requireNonNull(base64Dialect, "dialect");
        return base64Dialect.breakLinesByDefault;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static byte[] decodabet(Base64Dialect base64Dialect) {
        Objects.requireNonNull(base64Dialect, "dialect");
        return base64Dialect.decodabet;
    }

    public static ByteBuf decode(ByteBuf byteBuf) {
        return decode(byteBuf, Base64Dialect.STANDARD);
    }

    public static ByteBuf decode(ByteBuf byteBuf, int i2, int i3) {
        return decode(byteBuf, i2, i3, Base64Dialect.STANDARD);
    }

    public static ByteBuf decode(ByteBuf byteBuf, int i2, int i3, Base64Dialect base64Dialect) {
        return decode(byteBuf, i2, i3, base64Dialect, byteBuf.alloc());
    }

    public static ByteBuf decode(ByteBuf byteBuf, int i2, int i3, Base64Dialect base64Dialect, ByteBufAllocator byteBufAllocator) {
        Objects.requireNonNull(byteBuf, "src");
        Objects.requireNonNull(base64Dialect, "dialect");
        return new Decoder().decode(byteBuf, i2, i3, byteBufAllocator, base64Dialect);
    }

    public static ByteBuf decode(ByteBuf byteBuf, Base64Dialect base64Dialect) {
        Objects.requireNonNull(byteBuf, "src");
        ByteBuf byteBufDecode = decode(byteBuf, byteBuf.readerIndex(), byteBuf.readableBytes(), base64Dialect);
        byteBuf.readerIndex(byteBuf.writerIndex());
        return byteBufDecode;
    }

    public static int decodedBufferSize(int i2) {
        return i2 - (i2 >>> 2);
    }

    public static ByteBuf encode(ByteBuf byteBuf) {
        return encode(byteBuf, Base64Dialect.STANDARD);
    }

    public static ByteBuf encode(ByteBuf byteBuf, int i2, int i3) {
        return encode(byteBuf, i2, i3, Base64Dialect.STANDARD);
    }

    public static ByteBuf encode(ByteBuf byteBuf, int i2, int i3, Base64Dialect base64Dialect) {
        return encode(byteBuf, i2, i3, breakLines(base64Dialect), base64Dialect);
    }

    public static ByteBuf encode(ByteBuf byteBuf, int i2, int i3, boolean z2) {
        return encode(byteBuf, i2, i3, z2, Base64Dialect.STANDARD);
    }

    public static ByteBuf encode(ByteBuf byteBuf, int i2, int i3, boolean z2, Base64Dialect base64Dialect) {
        return encode(byteBuf, i2, i3, z2, base64Dialect, byteBuf.alloc());
    }

    public static ByteBuf encode(ByteBuf byteBuf, int i2, int i3, boolean z2, Base64Dialect base64Dialect, ByteBufAllocator byteBufAllocator) {
        Objects.requireNonNull(byteBuf, "src");
        Objects.requireNonNull(base64Dialect, "dialect");
        ByteBuf byteBufOrder = byteBufAllocator.buffer(encodedBufferSize(i3, z2)).order(byteBuf.order());
        byte[] bArrAlphabet = alphabet(base64Dialect);
        int i4 = i3 - 2;
        int i5 = 0;
        int i6 = 0;
        int i7 = 0;
        while (i5 < i4) {
            encode3to4(byteBuf, i5 + i2, 3, byteBufOrder, i6, bArrAlphabet);
            i7 += 4;
            if (z2 && i7 == 76) {
                byteBufOrder.setByte(i6 + 4, 10);
                i6++;
                i7 = 0;
            }
            i5 += 3;
            i6 += 4;
        }
        if (i5 < i3) {
            encode3to4(byteBuf, i5 + i2, i3 - i5, byteBufOrder, i6, bArrAlphabet);
            i6 += 4;
        }
        if (i6 > 1 && byteBufOrder.getByte(i6 - 1) == 10) {
            i6--;
        }
        return byteBufOrder.slice(0, i6);
    }

    public static ByteBuf encode(ByteBuf byteBuf, Base64Dialect base64Dialect) {
        return encode(byteBuf, breakLines(base64Dialect), base64Dialect);
    }

    public static ByteBuf encode(ByteBuf byteBuf, boolean z2) {
        return encode(byteBuf, z2, Base64Dialect.STANDARD);
    }

    public static ByteBuf encode(ByteBuf byteBuf, boolean z2, Base64Dialect base64Dialect) {
        Objects.requireNonNull(byteBuf, "src");
        ByteBuf byteBufEncode = encode(byteBuf, byteBuf.readerIndex(), byteBuf.readableBytes(), z2, base64Dialect);
        byteBuf.readerIndex(byteBuf.writerIndex());
        return byteBufEncode;
    }

    private static void encode3to4(ByteBuf byteBuf, int i2, int i3, ByteBuf byteBuf2, int i4, byte[] bArr) {
        int intLE = 0;
        if (byteBuf.order() == ByteOrder.BIG_ENDIAN) {
            if (i3 == 1) {
                intLE = toInt(byteBuf.getByte(i2));
            } else if (i3 == 2) {
                intLE = toIntBE(byteBuf.getShort(i2));
            } else if (i3 > 0) {
                intLE = toIntBE(byteBuf.getMedium(i2));
            }
            encode3to4BigEndian(intLE, i3, byteBuf2, i4, bArr);
            return;
        }
        if (i3 == 1) {
            intLE = toInt(byteBuf.getByte(i2));
        } else if (i3 == 2) {
            intLE = toIntLE(byteBuf.getShort(i2));
        } else if (i3 > 0) {
            intLE = toIntLE(byteBuf.getMedium(i2));
        }
        encode3to4LittleEndian(intLE, i3, byteBuf2, i4, bArr);
    }

    private static void encode3to4BigEndian(int i2, int i3, ByteBuf byteBuf, int i4, byte[] bArr) {
        int i5;
        int i6;
        if (i3 == 1) {
            i5 = (bArr[(i2 >>> 12) & 63] << 16) | (bArr[i2 >>> 18] << BinaryMemcacheOpcodes.FLUSHQ) | 15616;
        } else {
            if (i3 != 2) {
                if (i3 != 3) {
                    return;
                }
                i6 = bArr[i2 & 63] | (bArr[i2 >>> 18] << BinaryMemcacheOpcodes.FLUSHQ) | (bArr[(i2 >>> 12) & 63] << 16) | (bArr[(i2 >>> 6) & 63] << 8);
                byteBuf.setInt(i4, i6);
            }
            i5 = (bArr[(i2 >>> 6) & 63] << 8) | (bArr[i2 >>> 18] << BinaryMemcacheOpcodes.FLUSHQ) | (bArr[(i2 >>> 12) & 63] << 16);
        }
        i6 = i5 | 61;
        byteBuf.setInt(i4, i6);
    }

    private static void encode3to4LittleEndian(int i2, int i3, ByteBuf byteBuf, int i4, byte[] bArr) {
        int i5;
        int i6;
        if (i3 == 1) {
            i5 = (bArr[(i2 >>> 12) & 63] << 8) | bArr[i2 >>> 18] | 3997696;
        } else {
            if (i3 != 2) {
                if (i3 != 3) {
                    return;
                }
                i6 = (bArr[i2 & 63] << BinaryMemcacheOpcodes.FLUSHQ) | bArr[i2 >>> 18] | (bArr[(i2 >>> 12) & 63] << 8) | (bArr[(i2 >>> 6) & 63] << 16);
                byteBuf.setInt(i4, i6);
            }
            i5 = (bArr[(i2 >>> 6) & 63] << 16) | bArr[i2 >>> 18] | (bArr[(i2 >>> 12) & 63] << 8);
        }
        i6 = i5 | 1023410176;
        byteBuf.setInt(i4, i6);
    }

    public static int encodedBufferSize(int i2, boolean z2) {
        long j2 = (((long) i2) << 2) / 3;
        long j3 = (3 + j2) & (-4);
        if (z2) {
            j3 += j2 / 76;
        }
        if (j3 < 2147483647L) {
            return (int) j3;
        }
        return Integer.MAX_VALUE;
    }

    private static int toInt(byte b2) {
        return (b2 & EQUALS_SIGN_ENC) << 16;
    }

    private static int toIntBE(int i2) {
        return (i2 & DnsRecord.CLASS_ANY) | (16711680 & i2) | (65280 & i2);
    }

    private static int toIntBE(short s2) {
        return ((s2 & Http2CodecUtil.MAX_UNSIGNED_BYTE) << 8) | ((65280 & s2) << 8);
    }

    private static int toIntLE(int i2) {
        return ((i2 & 16711680) >>> 16) | ((i2 & DnsRecord.CLASS_ANY) << 16) | (65280 & i2);
    }

    private static int toIntLE(short s2) {
        return (s2 & 65280) | ((s2 & Http2CodecUtil.MAX_UNSIGNED_BYTE) << 16);
    }
}
