package io.netty.handler.codec.base64;

import com.seewo.sdk.model.SDKKeyboardCode;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;

/* 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;

    private Base64() {
    }

    private static byte[] alphabet(Base64Dialect base64Dialect) {
        if (base64Dialect != null) {
            return base64Dialect.alphabet;
        }
        throw new NullPointerException("dialect");
    }

    private static boolean breakLines(Base64Dialect base64Dialect) {
        if (base64Dialect != null) {
            return base64Dialect.breakLinesByDefault;
        }
        throw new NullPointerException("dialect");
    }

    private static byte[] decodabet(Base64Dialect base64Dialect) {
        if (base64Dialect != null) {
            return base64Dialect.decodabet;
        }
        throw new NullPointerException("dialect");
    }

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

    private static int decode4to3(byte[] bArr, int i2, ByteBuf byteBuf, int i3, Base64Dialect base64Dialect) {
        byte[] bArrDecodabet = decodabet(base64Dialect);
        int i4 = i2 + 2;
        if (bArr[i4] == 61) {
            byteBuf.setByte(i3, (byte) ((((bArrDecodabet[bArr[i2 + 1]] & EQUALS_SIGN_ENC) << 12) | ((bArrDecodabet[bArr[i2]] & EQUALS_SIGN_ENC) << 18)) >>> 16));
            return 1;
        }
        int i5 = i2 + 3;
        if (bArr[i5] == 61) {
            int i6 = ((bArrDecodabet[bArr[i4]] & EQUALS_SIGN_ENC) << 6) | ((bArrDecodabet[bArr[i2 + 1]] & EQUALS_SIGN_ENC) << 12) | ((bArrDecodabet[bArr[i2]] & EQUALS_SIGN_ENC) << 18);
            byteBuf.setByte(i3, (byte) (i6 >>> 16));
            byteBuf.setByte(i3 + 1, (byte) (i6 >>> 8));
            return 2;
        }
        try {
            int i7 = (bArrDecodabet[bArr[i5]] & EQUALS_SIGN_ENC) | ((bArrDecodabet[bArr[i2 + 1]] & EQUALS_SIGN_ENC) << 12) | ((bArrDecodabet[bArr[i2]] & EQUALS_SIGN_ENC) << 18) | ((bArrDecodabet[bArr[i4]] & EQUALS_SIGN_ENC) << 6);
            byteBuf.setByte(i3, (byte) (i7 >> 16));
            byteBuf.setByte(i3 + 1, (byte) (i7 >> 8));
            byteBuf.setByte(i3 + 2, (byte) i7);
            return 3;
        } catch (IndexOutOfBoundsException unused) {
            throw new IllegalArgumentException("not encoded in Base64");
        }
    }

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

    private static void encode3to4(ByteBuf byteBuf, int i2, int i3, ByteBuf byteBuf2, int i4, Base64Dialect base64Dialect) {
        byte[] bArrAlphabet = alphabet(base64Dialect);
        int i5 = (i3 > 0 ? (byteBuf.getByte(i2) << SDKKeyboardCode.FUNCTION_KEY_BOARD_U) >>> 8 : 0) | (i3 > 1 ? (byteBuf.getByte(i2 + 1) << SDKKeyboardCode.FUNCTION_KEY_BOARD_U) >>> 16 : 0) | (i3 > 2 ? (byteBuf.getByte(i2 + 2) << SDKKeyboardCode.FUNCTION_KEY_BOARD_U) >>> 24 : 0);
        if (i3 == 1) {
            byteBuf2.setByte(i4, bArrAlphabet[i5 >>> 18]);
            byteBuf2.setByte(i4 + 1, bArrAlphabet[(i5 >>> 12) & 63]);
            byteBuf2.setByte(i4 + 2, 61);
            byteBuf2.setByte(i4 + 3, 61);
            return;
        }
        if (i3 == 2) {
            byteBuf2.setByte(i4, bArrAlphabet[i5 >>> 18]);
            byteBuf2.setByte(i4 + 1, bArrAlphabet[(i5 >>> 12) & 63]);
            byteBuf2.setByte(i4 + 2, bArrAlphabet[(i5 >>> 6) & 63]);
            byteBuf2.setByte(i4 + 3, 61);
            return;
        }
        if (i3 != 3) {
            return;
        }
        byteBuf2.setByte(i4, bArrAlphabet[i5 >>> 18]);
        byteBuf2.setByte(i4 + 1, bArrAlphabet[(i5 >>> 12) & 63]);
        byteBuf2.setByte(i4 + 2, bArrAlphabet[(i5 >>> 6) & 63]);
        byteBuf2.setByte(i4 + 3, bArrAlphabet[i5 & 63]);
    }

    public static ByteBuf decode(ByteBuf byteBuf, Base64Dialect base64Dialect) {
        if (byteBuf == null) {
            throw new NullPointerException("src");
        }
        ByteBuf byteBufDecode = decode(byteBuf, byteBuf.readerIndex(), byteBuf.readableBytes(), base64Dialect);
        byteBuf.readerIndex(byteBuf.writerIndex());
        return byteBufDecode;
    }

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

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

    public static ByteBuf encode(ByteBuf byteBuf, boolean z, Base64Dialect base64Dialect) {
        if (byteBuf != null) {
            ByteBuf byteBufEncode = encode(byteBuf, byteBuf.readerIndex(), byteBuf.readableBytes(), z, base64Dialect);
            byteBuf.readerIndex(byteBuf.writerIndex());
            return byteBufEncode;
        }
        throw new NullPointerException("src");
    }

    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) {
        if (byteBuf == null) {
            throw new NullPointerException("src");
        }
        if (base64Dialect != null) {
            byte[] bArrDecodabet = decodabet(base64Dialect);
            ByteBuf byteBufOrder = byteBufAllocator.buffer((i3 * 3) / 4).order(byteBuf.order());
            byte[] bArr = new byte[4];
            int i4 = 0;
            int iDecode4to3 = 0;
            for (int i5 = i2; i5 < i2 + i3; i5++) {
                byte b = (byte) (byteBuf.getByte(i5) & SDKKeyboardCode.FUNCTION_KEY_BOARD_VOLUME_UP);
                byte b2 = bArrDecodabet[b];
                if (b2 < -5) {
                    throw new IllegalArgumentException("bad Base64 input character at " + i5 + ": " + ((int) byteBuf.getUnsignedByte(i5)) + " (decimal)");
                }
                if (b2 >= -1) {
                    int i6 = i4 + 1;
                    bArr[i4] = b;
                    if (i6 > 3) {
                        iDecode4to3 += decode4to3(bArr, 0, byteBufOrder, iDecode4to3, base64Dialect);
                        if (b == 61) {
                            break;
                        }
                        i4 = 0;
                    } else {
                        i4 = i6;
                    }
                }
            }
            return byteBufOrder.slice(0, iDecode4to3);
        }
        throw new NullPointerException("dialect");
    }

    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 z) {
        return encode(byteBuf, i2, i3, z, Base64Dialect.STANDARD);
    }

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

    public static ByteBuf encode(ByteBuf byteBuf, int i2, int i3, boolean z, Base64Dialect base64Dialect, ByteBufAllocator byteBufAllocator) {
        if (byteBuf == null) {
            throw new NullPointerException("src");
        }
        if (base64Dialect != null) {
            int i4 = (i3 * 4) / 3;
            ByteBuf byteBufOrder = byteBufAllocator.buffer((i3 % 3 > 0 ? 4 : 0) + i4 + (z ? i4 / 76 : 0)).order(byteBuf.order());
            int i5 = i3 - 2;
            int i6 = 0;
            int i7 = 0;
            int i8 = 0;
            while (i6 < i5) {
                encode3to4(byteBuf, i6 + i2, 3, byteBufOrder, i7, base64Dialect);
                i8 += 4;
                if (z && i8 == 76) {
                    byteBufOrder.setByte(i7 + 4, 10);
                    i7++;
                    i8 = 0;
                }
                i6 += 3;
                i7 += 4;
            }
            if (i6 < i3) {
                encode3to4(byteBuf, i6 + i2, i3 - i6, byteBufOrder, i7, base64Dialect);
                i7 += 4;
            }
            if (i7 > 1 && byteBufOrder.getByte(i7 - 1) == 10) {
                i7--;
            }
            return byteBufOrder.slice(0, i7);
        }
        throw new NullPointerException("dialect");
    }
}
