UNPKG

nufatfs

Version:

A new async-friendly library for accessing FAT16 and FAT32 filesystems

117 lines (116 loc) 4.66 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.serializeFatFSDirectoryEntry = exports.newFatFSDirectoryEntry = exports.createFatFSDirectoryEntry = exports.createFatFsInformation = exports.createFatBootInfo = exports.createFat32ExtendedInfo = exports.createBootSectorInfo = void 0; const utils_1 = require("./utils"); const textDecoder = new TextDecoder(); const textEncoder = new TextEncoder(); function createBootSectorInfo(input, offset) { const data = (0, utils_1.structFormatUnpack)("<3x8sHBHBHHBHHHII", input, offset); return { oemInfo: data[0], bytesPerLogicalSector: data[1], logicalSectorsPerCluster: data[2], reservedLogicalSectors: data[3], fatCount: data[4], deprecatedMaxRootDirEntries: data[5], deprecatedTotalLogicalSectors: data[6], mediaDescriptor: data[7], deprecatedLogicalSectorsPerFat: data[8], physicalSectorsPerTrack: data[9], numOfHeads: data[10], preceedingHiddenSectors: data[11], totalLogicalSectors: data[12], }; } exports.createBootSectorInfo = createBootSectorInfo; function createFat32ExtendedInfo(input, offset) { const data = (0, utils_1.structFormatUnpack)("<IHHIHH12x", input, offset); return { logicalSectorsPerFat: data[0], mirroringFlags: data[1], version: data[2], rootDirCluster: data[3], fsInformationSectorNum: data[4], backupSectorNum: data[5], }; } exports.createFat32ExtendedInfo = createFat32ExtendedInfo; function createFatBootInfo(input, offset) { const data = (0, utils_1.structFormatUnpack)("<BxBI11s8s", input, offset); return { physicalDriveNumber: data[0], extendedBootSignature: data[1], volumeId: data[2], label: data[3], fsType: data[4] }; } exports.createFatBootInfo = createFatBootInfo; function createFatFsInformation(input, offset) { const data = (0, utils_1.structFormatUnpack)("<4s480x4sII12x4s", input, offset); return { signature1: data[0], signature2: data[1], lastKnownFreeDataClusters: data[2], lastKnownAllocatedDataCluster: data[3], signature3: data[4], }; } exports.createFatFsInformation = createFatFsInformation; function createFatFSDirectoryEntry(input, offset) { const data = (0, utils_1.structFormatUnpack)("<11sBB5s2sH4sHI", input, offset); return { filename: data[0], attribs: data[1], reserved: data[2], creationDate: data[3], accessedDate: data[4], unsafeFirstClusterAddressHigh: data[5], writtenDate: data[6], unsafeFirstClusterAddressLow: data[7], fileSize: data[8], _firstCluster: ((data[5] << 16 >>> 0) | (data[7] >>> 0)) >>> 0, _filenameStr: textDecoder.decode(data[0]), _lfns: 0, }; } exports.createFatFSDirectoryEntry = createFatFSDirectoryEntry; function newFatFSDirectoryEntry(name83, attribs, rootCluster, fileSize) { const firstClusterAddressHigh = (rootCluster & 0xFFFF0000) >>> 16; const firstClusterAddressLow = (rootCluster & 0xFFFF) >>> 0; return { filename: textEncoder.encode(name83), attribs, reserved: 0, creationDate: new Uint8Array(5).fill(0), accessedDate: new Uint8Array(2).fill(0), unsafeFirstClusterAddressHigh: firstClusterAddressHigh, writtenDate: new Uint8Array(4).fill(0), unsafeFirstClusterAddressLow: firstClusterAddressLow, fileSize, _firstCluster: rootCluster, _filenameStr: name83, _lfns: 0, }; } exports.newFatFSDirectoryEntry = newFatFSDirectoryEntry; function serializeFatFSDirectoryEntry(input, overrideUnsafeAddresses = true) { if (overrideUnsafeAddresses) { input.unsafeFirstClusterAddressLow = (input._firstCluster & 0xFFFF) >>> 0; input.unsafeFirstClusterAddressHigh = (input._firstCluster & 0xFFFF0000) >>> 16; } const data = new Uint8Array(32); const dataView = new DataView(data.buffer); data.set(input._filenameStr === '' ? input.filename : textEncoder.encode(input._filenameStr), 0); data[11] = input.attribs; data[12] = input.reserved; data.set(input.creationDate, 13); data.set(input.accessedDate, 18); dataView.setUint16(20, input.unsafeFirstClusterAddressHigh, true); data.set(input.writtenDate, 22); dataView.setUint16(26, input.unsafeFirstClusterAddressLow, true); dataView.setUint32(28, input.fileSize, true); // 28 + 4 = 32 return data; } exports.serializeFatFSDirectoryEntry = serializeFatFSDirectoryEntry;