package io.netty.handler.codec.compression;

import g.a.a.a.a;
import io.netty.buffer.ByteBuf;
import io.netty.handler.codec.dns.DnsRecord;

/* JADX INFO: loaded from: classes.dex */
public final class Snappy {
    private static final int COPY_1_BYTE_OFFSET = 1;
    private static final int COPY_2_BYTE_OFFSET = 2;
    private static final int COPY_4_BYTE_OFFSET = 3;
    private static final int LITERAL = 0;
    private static final int MAX_HT_SIZE = 16384;
    private static final int MIN_COMPRESSIBLE_BYTES = 15;
    private static final int NOT_ENOUGH_INPUT = -1;
    private static final int PREAMBLE_NOT_FULL = -1;
    private State state = State.READY;
    private byte tag;
    private int written;

    /* JADX INFO: renamed from: io.netty.handler.codec.compression.Snappy$1, reason: invalid class name */
    public static /* synthetic */ class AnonymousClass1 {
        public static final /* synthetic */ int[] $SwitchMap$io$netty$handler$codec$compression$Snappy$State;

        static {
            State.values();
            int[] iArr = new int[5];
            $SwitchMap$io$netty$handler$codec$compression$Snappy$State = iArr;
            try {
                iArr[State.READY.ordinal()] = 1;
            } catch (NoSuchFieldError unused) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$compression$Snappy$State[State.READING_PREAMBLE.ordinal()] = 2;
            } catch (NoSuchFieldError unused2) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$compression$Snappy$State[State.READING_TAG.ordinal()] = 3;
            } catch (NoSuchFieldError unused3) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$compression$Snappy$State[State.READING_LITERAL.ordinal()] = 4;
            } catch (NoSuchFieldError unused4) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$compression$Snappy$State[State.READING_COPY.ordinal()] = 5;
            } catch (NoSuchFieldError unused5) {
            }
        }
    }

    public enum State {
        READY,
        READING_PREAMBLE,
        READING_TAG,
        READING_LITERAL,
        READING_COPY
    }

    private static int bitsToEncode(int i2) {
        int iHighestOneBit = Integer.highestOneBit(i2);
        int i3 = 0;
        while (true) {
            iHighestOneBit >>= 1;
            if (iHighestOneBit == 0) {
                return i3;
            }
            i3++;
        }
    }

    public static int calculateChecksum(ByteBuf byteBuf) {
        return calculateChecksum(byteBuf, byteBuf.readerIndex(), byteBuf.readableBytes());
    }

    public static int calculateChecksum(ByteBuf byteBuf, int i2, int i3) {
        Crc32c crc32c = new Crc32c();
        try {
            crc32c.update(byteBuf, i2, i3);
            return maskChecksum((int) crc32c.getValue());
        } finally {
            crc32c.reset();
        }
    }

    private static int decodeCopyWith1ByteOffset(byte b2, ByteBuf byteBuf, ByteBuf byteBuf2, int i2) {
        if (!byteBuf.isReadable()) {
            return -1;
        }
        int iWriterIndex = byteBuf2.writerIndex();
        int i3 = ((b2 & 28) >> 2) + 4;
        int unsignedByte = (((b2 & 224) << 8) >> 5) | byteBuf.readUnsignedByte();
        validateOffset(unsignedByte, i2);
        byteBuf2.markReaderIndex();
        if (unsignedByte < i3) {
            for (int i4 = i3 / unsignedByte; i4 > 0; i4--) {
                byteBuf2.readerIndex(iWriterIndex - unsignedByte);
                byteBuf2.readBytes(byteBuf2, unsignedByte);
            }
            int i5 = i3 % unsignedByte;
            if (i5 != 0) {
                byteBuf2.readerIndex(iWriterIndex - unsignedByte);
                byteBuf2.readBytes(byteBuf2, i5);
            }
        } else {
            byteBuf2.readerIndex(iWriterIndex - unsignedByte);
            byteBuf2.readBytes(byteBuf2, i3);
        }
        byteBuf2.resetReaderIndex();
        return i3;
    }

    private static int decodeCopyWith2ByteOffset(byte b2, ByteBuf byteBuf, ByteBuf byteBuf2, int i2) {
        if (byteBuf.readableBytes() < 2) {
            return -1;
        }
        int iWriterIndex = byteBuf2.writerIndex();
        int i3 = ((b2 >> 2) & 63) + 1;
        int unsignedShortLE = byteBuf.readUnsignedShortLE();
        validateOffset(unsignedShortLE, i2);
        byteBuf2.markReaderIndex();
        if (unsignedShortLE < i3) {
            for (int i4 = i3 / unsignedShortLE; i4 > 0; i4--) {
                byteBuf2.readerIndex(iWriterIndex - unsignedShortLE);
                byteBuf2.readBytes(byteBuf2, unsignedShortLE);
            }
            int i5 = i3 % unsignedShortLE;
            if (i5 != 0) {
                byteBuf2.readerIndex(iWriterIndex - unsignedShortLE);
                byteBuf2.readBytes(byteBuf2, i5);
            }
        } else {
            byteBuf2.readerIndex(iWriterIndex - unsignedShortLE);
            byteBuf2.readBytes(byteBuf2, i3);
        }
        byteBuf2.resetReaderIndex();
        return i3;
    }

    private static int decodeCopyWith4ByteOffset(byte b2, ByteBuf byteBuf, ByteBuf byteBuf2, int i2) {
        if (byteBuf.readableBytes() < 4) {
            return -1;
        }
        int iWriterIndex = byteBuf2.writerIndex();
        int i3 = ((b2 >> 2) & 63) + 1;
        int intLE = byteBuf.readIntLE();
        validateOffset(intLE, i2);
        byteBuf2.markReaderIndex();
        if (intLE < i3) {
            for (int i4 = i3 / intLE; i4 > 0; i4--) {
                byteBuf2.readerIndex(iWriterIndex - intLE);
                byteBuf2.readBytes(byteBuf2, intLE);
            }
            int i5 = i3 % intLE;
            if (i5 != 0) {
                byteBuf2.readerIndex(iWriterIndex - intLE);
                byteBuf2.readBytes(byteBuf2, i5);
            }
        } else {
            byteBuf2.readerIndex(iWriterIndex - intLE);
            byteBuf2.readBytes(byteBuf2, i3);
        }
        byteBuf2.resetReaderIndex();
        return i3;
    }

    public static int decodeLiteral(byte b2, ByteBuf byteBuf, ByteBuf byteBuf2) {
        byteBuf.markReaderIndex();
        int unsignedByte = (b2 >> 2) & 63;
        switch (unsignedByte) {
            case 60:
                if (!byteBuf.isReadable()) {
                    return -1;
                }
                unsignedByte = byteBuf.readUnsignedByte();
                break;
                break;
            case 61:
                if (byteBuf.readableBytes() < 2) {
                    return -1;
                }
                unsignedByte = byteBuf.readUnsignedShortLE();
                break;
            case 62:
                if (byteBuf.readableBytes() < 3) {
                    return -1;
                }
                unsignedByte = byteBuf.readUnsignedMediumLE();
                break;
            case 63:
                if (byteBuf.readableBytes() < 4) {
                    return -1;
                }
                unsignedByte = byteBuf.readIntLE();
                break;
        }
        int i2 = unsignedByte + 1;
        if (byteBuf.readableBytes() < i2) {
            byteBuf.resetReaderIndex();
            return -1;
        }
        byteBuf2.writeBytes(byteBuf, i2);
        return i2;
    }

    private static void encodeCopy(ByteBuf byteBuf, int i2, int i3) {
        while (i3 >= 68) {
            encodeCopyWithOffset(byteBuf, i2, 64);
            i3 -= 64;
        }
        if (i3 > 64) {
            encodeCopyWithOffset(byteBuf, i2, 60);
            i3 -= 60;
        }
        encodeCopyWithOffset(byteBuf, i2, i3);
    }

    private static void encodeCopyWithOffset(ByteBuf byteBuf, int i2, int i3) {
        if (i3 >= 12 || i2 >= 2048) {
            byteBuf.writeByte(((i3 - 1) << 2) | 2);
            byteBuf.writeByte(i2 & DnsRecord.CLASS_ANY);
            i2 >>= 8;
        } else {
            byteBuf.writeByte(((i3 - 4) << 2) | 1 | ((i2 >> 8) << 5));
        }
        byteBuf.writeByte(i2 & DnsRecord.CLASS_ANY);
    }

    public static void encodeLiteral(ByteBuf byteBuf, ByteBuf byteBuf2, int i2) {
        if (i2 < 61) {
            byteBuf2.writeByte((i2 - 1) << 2);
        } else {
            int i3 = i2 - 1;
            int iBitsToEncode = (bitsToEncode(i3) / 8) + 1;
            byteBuf2.writeByte((iBitsToEncode + 59) << 2);
            for (int i4 = 0; i4 < iBitsToEncode; i4++) {
                byteBuf2.writeByte((i3 >> (i4 * 8)) & DnsRecord.CLASS_ANY);
            }
        }
        byteBuf2.writeBytes(byteBuf, i2);
    }

    private static int findMatchingLength(ByteBuf byteBuf, int i2, int i3, int i4) {
        int i5 = 0;
        while (i3 <= i4 - 4 && byteBuf.getInt(i3) == byteBuf.getInt(i2 + i5)) {
            i3 += 4;
            i5 += 4;
        }
        while (i3 < i4 && byteBuf.getByte(i2 + i5) == byteBuf.getByte(i3)) {
            i3++;
            i5++;
        }
        return i5;
    }

    private static short[] getHashTable(int i2) {
        int i3 = 256;
        while (i3 < 16384 && i3 < i2) {
            i3 <<= 1;
        }
        return new short[i3];
    }

    private static int hash(ByteBuf byteBuf, int i2, int i3) {
        return (byteBuf.getInt(i2) * 506832829) >>> i3;
    }

    public static int maskChecksum(int i2) {
        return ((i2 << 17) | (i2 >> 15)) - 1568478504;
    }

    private static int readPreamble(ByteBuf byteBuf) {
        int i2 = 0;
        int i3 = 0;
        while (byteBuf.isReadable()) {
            short unsignedByte = byteBuf.readUnsignedByte();
            int i4 = i3 + 1;
            i2 |= (unsignedByte & 127) << (i3 * 7);
            if ((unsignedByte & 128) == 0) {
                return i2;
            }
            if (i4 >= 4) {
                throw new DecompressionException("Preamble is greater than 4 bytes");
            }
            i3 = i4;
        }
        return 0;
    }

    public static void validateChecksum(int i2, ByteBuf byteBuf) {
        validateChecksum(i2, byteBuf, byteBuf.readerIndex(), byteBuf.readableBytes());
    }

    public static void validateChecksum(int i2, ByteBuf byteBuf, int i3, int i4) {
        int iCalculateChecksum = calculateChecksum(byteBuf, i3, i4);
        if (iCalculateChecksum == i2) {
            return;
        }
        StringBuilder sbF = a.F("mismatching checksum: ");
        sbF.append(Integer.toHexString(iCalculateChecksum));
        sbF.append(" (expected: ");
        sbF.append(Integer.toHexString(i2));
        sbF.append(')');
        throw new DecompressionException(sbF.toString());
    }

    private static void validateOffset(int i2, int i3) {
        if (i2 == 0) {
            throw new DecompressionException("Offset is less than minimum permissible value");
        }
        if (i2 < 0) {
            throw new DecompressionException("Offset is greater than maximum value supported by this implementation");
        }
        if (i2 > i3) {
            throw new DecompressionException("Offset exceeds size of chunk");
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:44:0x0078  */
    /* JADX WARN: Removed duplicated region for block: B:70:0x0077 A[SYNTHETIC] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public void decode(io.netty.buffer.ByteBuf r7, io.netty.buffer.ByteBuf r8) {
        /*
            r6 = this;
        L0:
            boolean r0 = r7.isReadable()
            if (r0 == 0) goto L92
            io.netty.handler.codec.compression.Snappy$State r0 = r6.state
            int r0 = r0.ordinal()
            r1 = 2
            r2 = 1
            r3 = -1
            r4 = 3
            if (r0 == 0) goto L58
            if (r0 == r2) goto L5c
            if (r0 == r1) goto L71
            if (r0 == r4) goto L45
            r5 = 4
            if (r0 == r5) goto L1c
            goto L0
        L1c:
            byte r0 = r6.tag
            r5 = r0 & 3
            if (r5 == r2) goto L3b
            if (r5 == r1) goto L31
            if (r5 == r4) goto L27
            goto L0
        L27:
            int r1 = r6.written
            int r0 = decodeCopyWith4ByteOffset(r0, r7, r8, r1)
            if (r0 == r3) goto L30
            goto L4d
        L30:
            return
        L31:
            int r1 = r6.written
            int r0 = decodeCopyWith2ByteOffset(r0, r7, r8, r1)
            if (r0 == r3) goto L3a
            goto L4d
        L3a:
            return
        L3b:
            int r1 = r6.written
            int r0 = decodeCopyWith1ByteOffset(r0, r7, r8, r1)
            if (r0 == r3) goto L44
            goto L4d
        L44:
            return
        L45:
            byte r0 = r6.tag
            int r0 = decodeLiteral(r0, r7, r8)
            if (r0 == r3) goto L57
        L4d:
            io.netty.handler.codec.compression.Snappy$State r1 = io.netty.handler.codec.compression.Snappy.State.READING_TAG
            r6.state = r1
            int r1 = r6.written
            int r1 = r1 + r0
            r6.written = r1
            goto L0
        L57:
            return
        L58:
            io.netty.handler.codec.compression.Snappy$State r0 = io.netty.handler.codec.compression.Snappy.State.READING_PREAMBLE
            r6.state = r0
        L5c:
            int r0 = readPreamble(r7)
            if (r0 != r3) goto L63
            return
        L63:
            if (r0 != 0) goto L6a
            io.netty.handler.codec.compression.Snappy$State r7 = io.netty.handler.codec.compression.Snappy.State.READY
            r6.state = r7
            return
        L6a:
            r8.ensureWritable(r0)
            io.netty.handler.codec.compression.Snappy$State r0 = io.netty.handler.codec.compression.Snappy.State.READING_TAG
            r6.state = r0
        L71:
            boolean r0 = r7.isReadable()
            if (r0 != 0) goto L78
            return
        L78:
            byte r0 = r7.readByte()
            r6.tag = r0
            r0 = r0 & r4
            if (r0 == 0) goto L8c
            if (r0 == r2) goto L89
            if (r0 == r1) goto L89
            if (r0 == r4) goto L89
            goto L0
        L89:
            io.netty.handler.codec.compression.Snappy$State r0 = io.netty.handler.codec.compression.Snappy.State.READING_COPY
            goto L8e
        L8c:
            io.netty.handler.codec.compression.Snappy$State r0 = io.netty.handler.codec.compression.Snappy.State.READING_LITERAL
        L8e:
            r6.state = r0
            goto L0
        L92:
            return
        */
        throw new UnsupportedOperationException("Method not decompiled: io.netty.handler.codec.compression.Snappy.decode(io.netty.buffer.ByteBuf, io.netty.buffer.ByteBuf):void");
    }

    /* JADX WARN: Code restructure failed: missing block: B:15:0x0055, code lost:
    
        encodeLiteral(r13, r14, r3 - r5);
     */
    /* JADX WARN: Code restructure failed: missing block: B:16:0x005a, code lost:
    
        r4 = findMatchingLength(r13, r10 + 4, r3 + 4, r15) + 4;
        r5 = r3 + r4;
        encodeCopy(r14, r3 - r10, r4);
        r13.readerIndex(r13.readerIndex() + r4);
        r3 = r5 - 1;
     */
    /* JADX WARN: Code restructure failed: missing block: B:17:0x0074, code lost:
    
        if (r5 < r8) goto L19;
     */
    /* JADX WARN: Code restructure failed: missing block: B:19:0x0077, code lost:
    
        r6 = r5 - r0;
        r1[hash(r13, r3, r2)] = (short) (r6 - 1);
        r4 = r3 + 1;
        r7 = hash(r13, r4, r2);
        r10 = r0 + r1[r7];
        r1[r7] = (short) r6;
     */
    /* JADX WARN: Code restructure failed: missing block: B:20:0x0097, code lost:
    
        if (r13.getInt(r4) == r13.getInt(r10)) goto L22;
     */
    /* JADX WARN: Code restructure failed: missing block: B:22:0x00a2, code lost:
    
        r3 = r5;
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public void encode(io.netty.buffer.ByteBuf r13, io.netty.buffer.ByteBuf r14, int r15) {
        /*
            r12 = this;
            r0 = 0
        L1:
            int r1 = r0 * 7
            int r1 = r15 >>> r1
            r2 = r1 & (-128(0xffffffffffffff80, float:NaN))
            if (r2 == 0) goto L13
            r1 = r1 & 127(0x7f, float:1.78E-43)
            r1 = r1 | 128(0x80, float:1.8E-43)
            r14.writeByte(r1)
            int r0 = r0 + 1
            goto L1
        L13:
            r14.writeByte(r1)
            int r0 = r13.readerIndex()
            short[] r1 = getHashTable(r15)
            int r2 = r1.length
            int r2 = java.lang.Integer.numberOfLeadingZeros(r2)
            int r2 = r2 + 1
            int r3 = r15 - r0
            r4 = 15
            if (r3 < r4) goto La8
            int r3 = r0 + 1
            int r4 = hash(r13, r3, r2)
            r5 = r0
        L32:
            r6 = 32
        L34:
            int r7 = r6 + 1
            int r6 = r6 >> 5
            int r6 = r6 + r3
            int r8 = r15 + (-4)
            if (r6 <= r8) goto L3f
        L3d:
            r0 = r5
            goto La8
        L3f:
            int r9 = hash(r13, r6, r2)
            short r10 = r1[r4]
            int r10 = r10 + r0
            int r11 = r3 - r0
            short r11 = (short) r11
            r1[r4] = r11
            int r4 = r13.getInt(r3)
            int r11 = r13.getInt(r10)
            if (r4 != r11) goto La4
            int r4 = r3 - r5
            encodeLiteral(r13, r14, r4)
        L5a:
            int r4 = r10 + 4
            int r5 = r3 + 4
            int r4 = findMatchingLength(r13, r4, r5, r15)
            int r4 = r4 + 4
            int r5 = r3 + r4
            int r3 = r3 - r10
            encodeCopy(r14, r3, r4)
            int r3 = r13.readerIndex()
            int r3 = r3 + r4
            r13.readerIndex(r3)
            int r3 = r5 + (-1)
            if (r5 < r8) goto L77
            goto L3d
        L77:
            int r4 = hash(r13, r3, r2)
            int r6 = r5 - r0
            int r7 = r6 + (-1)
            short r7 = (short) r7
            r1[r4] = r7
            int r4 = r3 + 1
            int r7 = hash(r13, r4, r2)
            short r9 = r1[r7]
            int r10 = r0 + r9
            short r6 = (short) r6
            r1[r7] = r6
            int r4 = r13.getInt(r4)
            int r6 = r13.getInt(r10)
            if (r4 == r6) goto La2
            int r3 = r3 + 2
            int r4 = hash(r13, r3, r2)
            int r3 = r5 + 1
            goto L32
        La2:
            r3 = r5
            goto L5a
        La4:
            r3 = r6
            r6 = r7
            r4 = r9
            goto L34
        La8:
            if (r0 >= r15) goto Lae
            int r15 = r15 - r0
            encodeLiteral(r13, r14, r15)
        Lae:
            return
        */
        throw new UnsupportedOperationException("Method not decompiled: io.netty.handler.codec.compression.Snappy.encode(io.netty.buffer.ByteBuf, io.netty.buffer.ByteBuf, int):void");
    }

    public void reset() {
        this.state = State.READY;
        this.tag = (byte) 0;
        this.written = 0;
    }
}
