package no.nordicsemi.android.dfu.internal;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import no.nordicsemi.android.dfu.internal.exception.HexFileValidationException;

/* JADX INFO: loaded from: classes2.dex */
public class HexInputStream extends FilterInputStream {
    private final int LINE_LENGTH;
    private final int MBRSize;
    private final int available;
    private int bytesRead;
    private int lastAddress;
    private final byte[] localBuf;
    private int localPos;
    private int pos;
    private int size;

    public HexInputStream(InputStream inputStream, int i2) {
        super(new BufferedInputStream(inputStream));
        this.LINE_LENGTH = 128;
        byte[] bArr = new byte[128];
        this.localBuf = bArr;
        this.localPos = 128;
        this.size = bArr.length;
        this.lastAddress = 0;
        this.MBRSize = i2;
        this.available = calculateBinSize(i2);
    }

    public HexInputStream(byte[] bArr, int i2) {
        super(new ByteArrayInputStream(bArr));
        this.LINE_LENGTH = 128;
        byte[] bArr2 = new byte[128];
        this.localBuf = bArr2;
        this.localPos = 128;
        this.size = bArr2.length;
        this.lastAddress = 0;
        this.MBRSize = i2;
        this.available = calculateBinSize(i2);
    }

    private int asciiToInt(int i2) {
        if (i2 >= 65) {
            return i2 - 55;
        }
        if (i2 >= 48) {
            return i2 - 48;
        }
        return -1;
    }

    private int calculateBinSize(int i2) throws IOException {
        int address;
        InputStream inputStream = ((FilterInputStream) this).in;
        inputStream.mark(inputStream.available());
        try {
            int i3 = inputStream.read();
            int i4 = 0;
            int i5 = 0;
            while (true) {
                checkComma(i3);
                int i6 = readByte(inputStream);
                int address2 = readAddress(inputStream);
                int i7 = readByte(inputStream);
                if (i7 != 0) {
                    if (i7 == 1) {
                        return i5;
                    }
                    if (i7 == 2) {
                        address = readAddress(inputStream) << 4;
                        if (i5 > 0 && (address >> 16) != (i4 >> 16) + 1) {
                            return i5;
                        }
                        skip(inputStream, 2L);
                    } else if (i7 == 4) {
                        int address3 = readAddress(inputStream);
                        if (i5 > 0 && address3 != (i4 >> 16) + 1) {
                            return i5;
                        }
                        address = address3 << 16;
                        skip(inputStream, 2L);
                    }
                    i4 = address;
                    while (true) {
                        i3 = inputStream.read();
                        if (i3 != 10 || i3 == 13) {
                        }
                    }
                } else if (address2 + i4 >= i2) {
                    i5 += i6;
                }
                skip(inputStream, (((long) i6) * 2) + 2);
                while (true) {
                    i3 = inputStream.read();
                    if (i3 != 10) {
                    }
                }
            }
        } finally {
            inputStream.reset();
        }
    }

    private void checkComma(int i2) throws HexFileValidationException {
        if (i2 != 58) {
            throw new HexFileValidationException("Not a HEX file");
        }
    }

    private int readAddress(InputStream inputStream) {
        return readByte(inputStream) | (readByte(inputStream) << 8);
    }

    private int readByte(InputStream inputStream) {
        return asciiToInt(inputStream.read()) | (asciiToInt(inputStream.read()) << 4);
    }

    private int readLine() throws IOException {
        int address;
        int i2;
        int iSkip;
        if (this.pos == -1) {
            return 0;
        }
        InputStream inputStream = ((FilterInputStream) this).in;
        while (true) {
            int i3 = inputStream.read();
            this.pos++;
            if (i3 != 10 && i3 != 13) {
                checkComma(i3);
                int i4 = readByte(inputStream);
                this.pos += 2;
                int address2 = readAddress(inputStream);
                this.pos += 4;
                int i5 = readByte(inputStream);
                int i6 = this.pos + 2;
                this.pos = i6;
                if (i5 != 0) {
                    if (i5 == 1) {
                        this.pos = -1;
                        return 0;
                    }
                    if (i5 == 2) {
                        address = readAddress(inputStream) << 4;
                        i2 = this.pos + 4;
                        this.pos = i2;
                        if (this.bytesRead > 0 && (address >> 16) != (this.lastAddress >> 16) + 1) {
                            return 0;
                        }
                    } else if (i5 != 4) {
                        iSkip = (int) (((long) i6) + skip(inputStream, (((long) i4) * 2) + 2));
                        this.pos = iSkip;
                    } else {
                        int address3 = readAddress(inputStream);
                        i2 = this.pos + 4;
                        this.pos = i2;
                        if (this.bytesRead > 0 && address3 != (this.lastAddress >> 16) + 1) {
                            return 0;
                        }
                        address = address3 << 16;
                    }
                    this.lastAddress = address;
                    iSkip = (int) (((long) i2) + skip(inputStream, 2L));
                    this.pos = iSkip;
                } else if (this.lastAddress + address2 < this.MBRSize) {
                    this.pos = (int) (((long) i6) + skip(inputStream, (((long) i4) * 2) + 2));
                    i5 = -1;
                }
                if (i5 == 0) {
                    for (int i7 = 0; i7 < this.localBuf.length && i7 < i4; i7++) {
                        int i8 = readByte(inputStream);
                        this.pos += 2;
                        this.localBuf[i7] = (byte) i8;
                    }
                    this.pos = (int) (((long) this.pos) + skip(inputStream, 2L));
                    this.localPos = 0;
                    return i4;
                }
            }
        }
    }

    private long skip(InputStream inputStream, long j2) throws IOException {
        long jSkip = inputStream.skip(j2);
        return jSkip < j2 ? jSkip + inputStream.skip(j2 - jSkip) : jSkip;
    }

    @Override // java.io.FilterInputStream, java.io.InputStream
    public int available() {
        return this.available - this.bytesRead;
    }

    @Override // java.io.FilterInputStream, java.io.InputStream
    public synchronized void mark(int i2) {
        try {
            super.mark(((FilterInputStream) this).in.available());
        } catch (IOException unused) {
        }
    }

    @Override // java.io.FilterInputStream, java.io.InputStream
    public int read() {
        throw new UnsupportedOperationException("Please, use readPacket() method instead");
    }

    @Override // java.io.FilterInputStream, java.io.InputStream
    public int read(byte[] bArr) {
        return readPacket(bArr);
    }

    @Override // java.io.FilterInputStream, java.io.InputStream
    public int read(byte[] bArr, int i2, int i3) {
        throw new UnsupportedOperationException("Please, use readPacket() method instead");
    }

    public int readPacket(byte[] bArr) throws IOException {
        int i2 = 0;
        while (i2 < bArr.length) {
            int i3 = this.localPos;
            if (i3 < this.size) {
                byte[] bArr2 = this.localBuf;
                this.localPos = i3 + 1;
                bArr[i2] = bArr2[i3];
                i2++;
            } else {
                int i4 = this.bytesRead;
                int line = readLine();
                this.size = line;
                this.bytesRead = i4 + line;
                if (line == 0) {
                    break;
                }
            }
        }
        return i2;
    }

    @Override // java.io.FilterInputStream, java.io.InputStream
    public synchronized void reset() {
        super.reset();
        this.pos = 0;
        this.bytesRead = 0;
        this.localPos = 128;
    }

    public int sizeInBytes() {
        return this.available;
    }

    public int sizeInPackets(int i2) {
        int iSizeInBytes = sizeInBytes();
        return (iSizeInBytes / i2) + (iSizeInBytes % i2 > 0 ? 1 : 0);
    }
}
