package com.dss.ups.sp51a.dongle;

import io.netty.handler.codec.dns.DnsRecord;
import kotlin.Metadata;
import kotlin.jvm.internal.o;

/* JADX INFO: loaded from: classes.dex */
@Metadata(bv = {1, 0, 3}, d1 = {"\u0000&\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\u0010\u0012\n\u0000\n\u0002\u0010\b\n\u0002\b\u000b\n\u0002\u0010\u000e\n\u0002\b\u0003\n\u0002\u0010\u000b\n\u0002\b\u0005\bÆ\u0002\u0018\u00002\u00020\u0001B\t\b\u0002¢\u0006\u0004\b\u0017\u0010\u0018J\u001f\u0010\u0006\u001a\u00020\u00042\u0006\u0010\u0003\u001a\u00020\u00022\u0006\u0010\u0005\u001a\u00020\u0004H\u0002¢\u0006\u0004\b\u0006\u0010\u0007J'\u0010\u000b\u001a\u00020\u00042\u0006\u0010\b\u001a\u00020\u00022\u0006\u0010\t\u001a\u00020\u00042\u0006\u0010\n\u001a\u00020\u0004H\u0002¢\u0006\u0004\b\u000b\u0010\fJ\u0015\u0010\r\u001a\u00020\u00022\u0006\u0010\b\u001a\u00020\u0002¢\u0006\u0004\b\r\u0010\u000eJ\u0015\u0010\u0011\u001a\u00020\u00102\u0006\u0010\u000f\u001a\u00020\u0002¢\u0006\u0004\b\u0011\u0010\u0012J\u001d\u0010\u0015\u001a\u00020\u00142\u0006\u0010\u0013\u001a\u00020\u00022\u0006\u0010\u0005\u001a\u00020\u0004¢\u0006\u0004\b\u0015\u0010\u0016¨\u0006\u0019"}, d2 = {"Lcom/dss/ups/sp51a/dongle/Crc16Util;", "", "", "bytes", "", "length", "crc16Ccitt", "([BI)I", "buf", "offset", "crcLen", "calcCRC", "([BII)I", "setParamCRC", "([B)[B", "byteArray", "", "toHexStr", "([B)Ljava/lang/String;", "srcByte", "", "isPassCRC", "([BI)Z", "<init>", "()V", "lib_release"}, k = 1, mv = {1, 4, 0})
public final class Crc16Util {
    public static final Crc16Util INSTANCE = new Crc16Util();

    private Crc16Util() {
    }

    private final int calcCRC(byte[] buf, int offset, int crcLen) {
        int i2 = crcLen + offset;
        int i3 = 0;
        while (offset < i2) {
            byte b2 = buf[offset];
            for (int i4 = 0; i4 <= 7; i4++) {
                boolean z2 = ((b2 >> (7 - i4)) & 1) == 1;
                boolean z3 = ((i3 >> 15) & 1) == 1;
                i3 <<= 1;
                if (z2 ^ z3) {
                    i3 ^= 4129;
                }
            }
            offset++;
        }
        return 65535 & i3;
    }

    private final int crc16Ccitt(byte[] bytes, int length) {
        int i2 = 0;
        for (byte b2 : bytes) {
            for (int i3 = 0; i3 <= 7; i3++) {
                boolean z2 = ((b2 >> (7 - i3)) & 1) == 1;
                boolean z3 = ((i2 >> 15) & 1) == 1;
                i2 <<= 1;
                if (z2 ^ z3) {
                    i2 ^= 4129;
                }
            }
        }
        int i4 = 65535 & i2;
        String hexString = Integer.toHexString(i4);
        o.b(hexString, "Integer.toHexString(crc)");
        String upperCase = hexString.toUpperCase();
        o.b(upperCase, "(this as java.lang.String).toUpperCase()");
        System.out.println((Object) upperCase);
        return i4;
    }

    public final boolean isPassCRC(byte[] srcByte, int length) {
        o.g(srcByte, "srcByte");
        int iCalcCRC = calcCRC(srcByte, 1, (srcByte.length - length) - 1);
        byte[] bArr = {(byte) ((iCalcCRC >> 8) & DnsRecord.CLASS_ANY), (byte) (iCalcCRC & DnsRecord.CLASS_ANY)};
        int length2 = srcByte.length;
        byte[] bArr2 = {srcByte[length2 - 2], srcByte[length2 - 1]};
        return bArr[0] == bArr2[0] && bArr[1] == bArr2[1];
    }

    public final byte[] setParamCRC(byte[] buf) {
        o.g(buf, "buf");
        int iCrc16Ccitt = crc16Ccitt(buf, buf.length);
        return new byte[]{(byte) ((iCrc16Ccitt >> 8) & DnsRecord.CLASS_ANY), (byte) (iCrc16Ccitt & DnsRecord.CLASS_ANY)};
    }

    public final String toHexStr(byte[] byteArray) {
        o.g(byteArray, "byteArray");
        StringBuilder sb = new StringBuilder();
        for (byte b2 : byteArray) {
            String hexString = Integer.toHexString(b2 & 255);
            if (hexString.length() == 1) {
                sb.append("0");
            }
            sb.append(hexString);
        }
        String string = sb.toString();
        o.b(string, "with(StringBuilder()) {\n…     toString()\n        }");
        return string;
    }
}
