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

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.Charset;
import java.util.Calendar;
import kotlin.jvm.internal.CharCompanionObject;
import okhttp3.internal.ws.WebSocketProtocol;

/* JADX INFO: loaded from: classes.dex */
class FatDirectoryEntry {
    private static final int ATTR_OFF = 11;
    private static final int CREATED_DATE_OFF = 16;
    private static final int CREATED_TIME_OFF = 14;
    static final int ENTRY_DELETED = 229;
    private static final int FILE_SIZE_OFF = 28;
    private static final int FLAG_ARCHIVE = 32;
    private static final int FLAG_DIRECTORY = 16;
    private static final int FLAG_HIDDEN = 2;
    private static final int FLAG_READONLY = 1;
    private static final int FLAG_SYSTEM = 4;
    private static final int FLAG_VOLUME_ID = 8;
    private static final int LAST_ACCESSED_DATE_OFF = 18;
    private static final int LAST_WRITE_DATE_OFF = 24;
    private static final int LAST_WRITE_TIME_OFF = 22;
    private static final int LSB_CLUSTER_OFF = 26;
    private static final int MSB_CLUSTER_OFF = 20;
    private static final int SHORTNAME_CASE_OFF = 12;
    static final int SIZE = 32;
    private ByteBuffer data;
    private ShortName shortName;

    private FatDirectoryEntry() {
    }

    private FatDirectoryEntry(ByteBuffer byteBuffer) {
        this.data = byteBuffer;
        byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
        this.shortName = ShortName.parse(byteBuffer);
        byteBuffer.clear();
    }

    static FatDirectoryEntry createNew() {
        FatDirectoryEntry fatDirectoryEntry = new FatDirectoryEntry();
        fatDirectoryEntry.data = ByteBuffer.allocate(32);
        long jCurrentTimeMillis = System.currentTimeMillis();
        fatDirectoryEntry.setCreatedDateTime(jCurrentTimeMillis);
        fatDirectoryEntry.setLastAccessedDateTime(jCurrentTimeMillis);
        fatDirectoryEntry.setLastModifiedDateTime(jCurrentTimeMillis);
        return fatDirectoryEntry;
    }

    static FatDirectoryEntry read(ByteBuffer byteBuffer) {
        byte[] bArr = new byte[32];
        if (byteBuffer.get(byteBuffer.position()) == 0) {
            return null;
        }
        byteBuffer.get(bArr);
        return new FatDirectoryEntry(ByteBuffer.wrap(bArr));
    }

    void serialize(ByteBuffer byteBuffer) {
        byteBuffer.put(this.data.array());
    }

    private int getFlags() {
        return this.data.get(11);
    }

    private void setFlag(int i) {
        this.data.put(11, (byte) (i | getFlags()));
    }

    private boolean isFlagSet(int i) {
        return (getFlags() & i) != 0;
    }

    boolean isShortNameLowerCase() {
        return (this.data.get(12) & 8) != 0;
    }

    boolean isShortNameExtLowerCase() {
        return (this.data.get(12) & 16) != 0;
    }

    boolean isLfnEntry() {
        return isHidden() && isVolume() && isReadOnly() && isSystem();
    }

    boolean isDirectory() {
        return (getFlags() & 24) == 16;
    }

    void setDirectory() {
        setFlag(16);
    }

    boolean isVolumeLabel() {
        return !isLfnEntry() && (getFlags() & 24) == 8;
    }

    boolean isSystem() {
        return isFlagSet(4);
    }

    boolean isHidden() {
        return isFlagSet(2);
    }

    boolean isArchive() {
        return isFlagSet(32);
    }

    boolean isReadOnly() {
        return isFlagSet(1);
    }

    boolean isVolume() {
        return isFlagSet(8);
    }

    boolean isDeleted() {
        return getUnsignedInt8(0) == ENTRY_DELETED;
    }

    long getCreatedDateTime() {
        return decodeDateTime(getUnsignedInt16(16), getUnsignedInt16(14));
    }

    void setCreatedDateTime(long j) {
        setUnsignedInt16(16, encodeDate(j));
        setUnsignedInt16(14, encodeTime(j));
    }

    long getLastModifiedDateTime() {
        return decodeDateTime(getUnsignedInt16(24), getUnsignedInt16(22));
    }

    void setLastModifiedDateTime(long j) {
        setUnsignedInt16(24, encodeDate(j));
        setUnsignedInt16(22, encodeTime(j));
    }

    long getLastAccessedDateTime() {
        return decodeDateTime(getUnsignedInt16(18), 0);
    }

    void setLastAccessedDateTime(long j) {
        setUnsignedInt16(18, encodeDate(j));
    }

    ShortName getShortName() {
        if (this.data.get(0) == 0) {
            return null;
        }
        return this.shortName;
    }

    void setShortName(ShortName shortName) {
        this.shortName = shortName;
        shortName.serialize(this.data);
        this.data.clear();
    }

    static FatDirectoryEntry createVolumeLabel(String str) {
        FatDirectoryEntry fatDirectoryEntry = new FatDirectoryEntry();
        ByteBuffer byteBufferAllocate = ByteBuffer.allocate(32);
        byteBufferAllocate.order(ByteOrder.LITTLE_ENDIAN);
        System.arraycopy(str.getBytes(Charset.forName("ASCII")), 0, byteBufferAllocate.array(), 0, str.length());
        fatDirectoryEntry.data = byteBufferAllocate;
        fatDirectoryEntry.setFlag(8);
        return fatDirectoryEntry;
    }

    String getVolumeLabel() {
        byte b;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 11 && (b = this.data.get(i)) != 0; i++) {
            sb.append((char) b);
        }
        return sb.toString();
    }

    long getStartCluster() {
        return getUnsignedInt16(26) | (getUnsignedInt16(20) << 16);
    }

    void setStartCluster(long j) {
        setUnsignedInt16(20, (int) ((j >> 16) & WebSocketProtocol.PAYLOAD_SHORT_MAX));
        setUnsignedInt16(26, (int) (j & WebSocketProtocol.PAYLOAD_SHORT_MAX));
    }

    long getFileSize() {
        return getUnsignedInt32(28);
    }

    void setFileSize(long j) {
        setUnsignedInt32(28, j);
    }

    static FatDirectoryEntry createLfnPart(String str, int i, byte b, int i2, boolean z) {
        int length;
        FatDirectoryEntry fatDirectoryEntry = new FatDirectoryEntry();
        if (z && (length = str.length() - i) < 13) {
            StringBuilder sb = new StringBuilder(13);
            sb.append((CharSequence) str, i, str.length());
            sb.append((char) 0);
            for (int i3 = 0; i3 < 13 - length; i3++) {
                sb.append(CharCompanionObject.MAX_VALUE);
            }
            str = sb.toString();
            i = 0;
        }
        ByteBuffer byteBufferAllocate = ByteBuffer.allocate(32);
        byteBufferAllocate.order(ByteOrder.LITTLE_ENDIAN);
        if (z) {
            i2 += 64;
        }
        byteBufferAllocate.put(0, (byte) i2);
        byteBufferAllocate.putShort(1, (short) str.charAt(i));
        byteBufferAllocate.putShort(3, (short) str.charAt(i + 1));
        byteBufferAllocate.putShort(5, (short) str.charAt(i + 2));
        byteBufferAllocate.putShort(7, (short) str.charAt(i + 3));
        byteBufferAllocate.putShort(9, (short) str.charAt(i + 4));
        byteBufferAllocate.put(11, (byte) 15);
        byteBufferAllocate.put(12, (byte) 0);
        byteBufferAllocate.put(13, b);
        byteBufferAllocate.putShort(14, (short) str.charAt(i + 5));
        byteBufferAllocate.putShort(16, (short) str.charAt(i + 6));
        byteBufferAllocate.putShort(18, (short) str.charAt(i + 7));
        byteBufferAllocate.putShort(20, (short) str.charAt(i + 8));
        byteBufferAllocate.putShort(22, (short) str.charAt(i + 9));
        byteBufferAllocate.putShort(24, (short) str.charAt(i + 10));
        byteBufferAllocate.putShort(26, (short) 0);
        byteBufferAllocate.putShort(28, (short) str.charAt(i + 11));
        byteBufferAllocate.putShort(30, (short) str.charAt(i + 12));
        fatDirectoryEntry.data = byteBufferAllocate;
        return fatDirectoryEntry;
    }

    void extractLfnPart(StringBuilder sb) {
        char[] cArr = {(char) this.data.getShort(1), (char) this.data.getShort(3), (char) this.data.getShort(5), (char) this.data.getShort(7), (char) this.data.getShort(9), (char) this.data.getShort(14), (char) this.data.getShort(16), (char) this.data.getShort(18), (char) this.data.getShort(20), (char) this.data.getShort(22), (char) this.data.getShort(24), (char) this.data.getShort(28), (char) this.data.getShort(30)};
        int i = 0;
        while (i < 13 && cArr[i] != 0) {
            i++;
        }
        sb.append(cArr, 0, i);
    }

    private int getUnsignedInt8(int i) {
        return this.data.get(i) & 255;
    }

    private int getUnsignedInt16(int i) {
        return ((this.data.get(i + 1) & 255) << 8) | (this.data.get(i) & 255);
    }

    private long getUnsignedInt32(int i) {
        return (((long) (this.data.get(i + 3) & 255)) << 24) | (((long) (this.data.get(i + 2) & 255)) << 16) | ((this.data.get(i + 1) & 255) << 8) | (this.data.get(i) & 255);
    }

    private void setUnsignedInt16(int i, int i2) {
        this.data.put(i, (byte) (i2 & 255));
        this.data.put(i + 1, (byte) ((i2 >>> 8) & 255));
    }

    private void setUnsignedInt32(int i, long j) {
        this.data.put(i, (byte) (j & 255));
        this.data.put(i + 1, (byte) ((j >>> 8) & 255));
        this.data.put(i + 2, (byte) ((j >>> 16) & 255));
        this.data.put(i + 3, (byte) ((j >>> 24) & 255));
    }

    private static long decodeDateTime(int i, int i2) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(1, (i >> 9) + 1980);
        calendar.set(2, ((i >> 5) & 15) - 1);
        calendar.set(5, i & 31);
        calendar.set(11, i2 >> 11);
        calendar.set(12, (i2 >> 5) & 63);
        calendar.set(13, (i2 & 31) * 2);
        return calendar.getTimeInMillis();
    }

    private static int encodeDate(long j) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(j);
        return ((calendar.get(1) - 1980) << 9) + ((calendar.get(2) + 1) << 5) + calendar.get(5);
    }

    private static int encodeTime(long j) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(j);
        return (calendar.get(11) << 11) + (calendar.get(12) << 5) + (calendar.get(13) / 2);
    }

    public String toString() {
        return "[FatDirectoryEntry shortName=" + this.shortName.getString() + "]";
    }
}
