package com.github.mjdev.libaums.fs.fat32;

import android.util.Log;
import com.github.mjdev.libaums.driver.BlockDeviceDriver;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.Arrays;

/* JADX INFO: loaded from: classes.dex */
public class FAT {
    private static final int FAT32_EOF_CLUSTER = 268435448;
    private static final String TAG = "FAT";
    private BlockDeviceDriver blockDevice;
    private int[] fatNumbers;
    private long[] fatOffset;
    private FsInfoStructure fsInfoStructure;

    FAT(BlockDeviceDriver blockDeviceDriver, Fat32BootSector fat32BootSector, FsInfoStructure fsInfoStructure) {
        this.blockDevice = blockDeviceDriver;
        this.fsInfoStructure = fsInfoStructure;
        int i = 0;
        if (!fat32BootSector.isFatMirrored()) {
            int validFat = fat32BootSector.getValidFat();
            this.fatNumbers = new int[]{validFat};
            Log.i(TAG, "fat is not mirrored, fat " + validFat + " is valid");
        } else {
            int fatCount = fat32BootSector.getFatCount();
            this.fatNumbers = new int[fatCount];
            for (int i2 = 0; i2 < fatCount; i2++) {
                this.fatNumbers[i2] = i2;
            }
            Log.i(TAG, "fat is mirrored, fat count: " + fatCount);
        }
        this.fatOffset = new long[this.fatNumbers.length];
        while (true) {
            long[] jArr = this.fatOffset;
            if (i >= jArr.length) {
                return;
            }
            jArr[i] = fat32BootSector.getFatOffset(this.fatNumbers[i]);
            i++;
        }
    }

    Long[] getChain(long j) throws IOException {
        if (j == 0) {
            return new Long[0];
        }
        ArrayList arrayList = new ArrayList();
        int blockSize = this.blockDevice.getBlockSize() * 2;
        ByteBuffer byteBufferAllocate = ByteBuffer.allocate(blockSize);
        byteBufferAllocate.order(ByteOrder.LITTLE_ENDIAN);
        long j2 = -1;
        do {
            arrayList.add(Long.valueOf(j));
            long j3 = this.fatOffset[0];
            long j4 = j * 4;
            long j5 = blockSize;
            long j6 = ((j3 + j4) / j5) * j5;
            long j7 = (j3 + j4) % j5;
            if (j2 != j6) {
                byteBufferAllocate.clear();
                this.blockDevice.read(j6, byteBufferAllocate);
                j2 = j6;
            }
            j = byteBufferAllocate.getInt((int) j7);
        } while (j < 268435448);
        return (Long[]) arrayList.toArray(new Long[0]);
    }

    Long[] alloc(Long[] lArr, int i) throws IOException {
        Long[] lArr2;
        ArrayList arrayList = new ArrayList(lArr.length + i);
        arrayList.addAll(Arrays.asList(lArr));
        int blockSize = this.blockDevice.getBlockSize() * 2;
        ByteBuffer byteBufferAllocate = ByteBuffer.allocate(blockSize);
        byteBufferAllocate.order(ByteOrder.LITTLE_ENDIAN);
        long jLongValue = lArr.length != 0 ? lArr[lArr.length - 1].longValue() : -1L;
        long lastAllocatedClusterHint = this.fsInfoStructure.getLastAllocatedClusterHint();
        if (lastAllocatedClusterHint == FsInfoStructure.INVALID_VALUE) {
            lastAllocatedClusterHint = 2;
        }
        int i2 = i;
        long j = -1;
        while (i2 > 0) {
            lastAllocatedClusterHint++;
            long j2 = this.fatOffset[0];
            long j3 = 4 * lastAllocatedClusterHint;
            long j4 = blockSize;
            long j5 = jLongValue;
            long j6 = ((j2 + j3) / j4) * j4;
            long j7 = (j2 + j3) % j4;
            if (j != j6) {
                byteBufferAllocate.clear();
                this.blockDevice.read(j6, byteBufferAllocate);
                j = j6;
            }
            if (byteBufferAllocate.getInt((int) j7) == 0) {
                arrayList.add(Long.valueOf(lastAllocatedClusterHint));
                i2--;
            }
            jLongValue = j5;
        }
        long j8 = jLongValue;
        if (j8 != -1) {
            long j9 = this.fatOffset[0];
            long j10 = j8 * 4;
            long j11 = blockSize;
            long j12 = ((j9 + j10) / j11) * j11;
            long j13 = (j9 + j10) % j11;
            if (j != j12) {
                byteBufferAllocate.clear();
                this.blockDevice.read(j12, byteBufferAllocate);
                j = j12;
            }
            lArr2 = lArr;
            byteBufferAllocate.putInt((int) j13, (int) ((Long) arrayList.get(lArr2.length)).longValue());
        } else {
            lArr2 = lArr;
        }
        int length = lArr2.length;
        while (length < arrayList.size() - 1) {
            long jLongValue2 = ((Long) arrayList.get(length)).longValue();
            long j14 = this.fatOffset[0];
            long j15 = jLongValue2 * 4;
            long j16 = blockSize;
            long j17 = ((j14 + j15) / j16) * j16;
            long j18 = (j14 + j15) % j16;
            if (j != j17) {
                byteBufferAllocate.clear();
                this.blockDevice.write(j, byteBufferAllocate);
                byteBufferAllocate.clear();
                this.blockDevice.read(j17, byteBufferAllocate);
                j = j17;
            }
            length++;
            byteBufferAllocate.putInt((int) j18, (int) ((Long) arrayList.get(length)).longValue());
        }
        long jLongValue3 = ((Long) arrayList.get(arrayList.size() - 1)).longValue();
        long j19 = this.fatOffset[0];
        long j20 = 4 * jLongValue3;
        long j21 = blockSize;
        long j22 = ((j19 + j20) / j21) * j21;
        long j23 = (j19 + j20) % j21;
        if (j != j22) {
            byteBufferAllocate.clear();
            this.blockDevice.write(j, byteBufferAllocate);
            byteBufferAllocate.clear();
            this.blockDevice.read(j22, byteBufferAllocate);
        }
        byteBufferAllocate.putInt((int) j23, FAT32_EOF_CLUSTER);
        byteBufferAllocate.clear();
        this.blockDevice.write(j22, byteBufferAllocate);
        this.fsInfoStructure.setLastAllocatedClusterHint(jLongValue3);
        this.fsInfoStructure.decreaseClusterCount(i);
        this.fsInfoStructure.write();
        Log.i(TAG, "allocating clusters finished");
        return (Long[]) arrayList.toArray(new Long[0]);
    }

    Long[] free(Long[] lArr, int i) throws IOException {
        int length = lArr.length - i;
        int blockSize = this.blockDevice.getBlockSize() * 2;
        ByteBuffer byteBufferAllocate = ByteBuffer.allocate(blockSize);
        byteBufferAllocate.order(ByteOrder.LITTLE_ENDIAN);
        if (length < 0) {
            throw new IllegalStateException("trying to remove more clusters in chain than currently exist!");
        }
        long j = -1;
        for (int i2 = length; i2 < lArr.length; i2++) {
            long jLongValue = lArr[i2].longValue();
            long j2 = this.fatOffset[0];
            long j3 = jLongValue * 4;
            long j4 = blockSize;
            long j5 = ((j2 + j3) / j4) * j4;
            long j6 = (j2 + j3) % j4;
            if (j != j5) {
                if (j != -1) {
                    byteBufferAllocate.clear();
                    this.blockDevice.write(j, byteBufferAllocate);
                }
                byteBufferAllocate.clear();
                this.blockDevice.read(j5, byteBufferAllocate);
                j = j5;
            }
            byteBufferAllocate.putInt((int) j6, 0);
        }
        if (length > 0) {
            long jLongValue2 = lArr[length - 1].longValue();
            long j7 = this.fatOffset[0];
            long j8 = jLongValue2 * 4;
            long j9 = blockSize;
            long j10 = ((j7 + j8) / j9) * j9;
            long j11 = (j7 + j8) % j9;
            if (j != j10) {
                byteBufferAllocate.clear();
                this.blockDevice.write(j, byteBufferAllocate);
                byteBufferAllocate.clear();
                this.blockDevice.read(j10, byteBufferAllocate);
            }
            byteBufferAllocate.putInt((int) j11, FAT32_EOF_CLUSTER);
            byteBufferAllocate.clear();
            this.blockDevice.write(j10, byteBufferAllocate);
        } else {
            byteBufferAllocate.clear();
            this.blockDevice.write(j, byteBufferAllocate);
        }
        Log.i(TAG, "freed " + i + " clusters");
        this.fsInfoStructure.decreaseClusterCount(-i);
        this.fsInfoStructure.write();
        return (Long[]) Arrays.copyOfRange(lArr, 0, length);
    }
}
