Exemplo n.º 1
0
        /// <summary>
        /// Read the contents of this FAT from the given device at the given offset.
        /// </summary>
        /// <exception cref="IOException">IOException on read error</exception>
        private void Read()
        {
            var data = new byte[sectorCount * sectorSize];

            device.Read(offset, new MemoryStream(data));

            for (var i = 0; i < entries.Length; i++)
            {
                entries[i] = fatType.ReadEntry(data, i);
            }
        }
Exemplo n.º 2
0
        public static BootSector Read(IBlockDevice device)
        {
            var b  = new byte[512];
            var bb = new MemoryStream(b);

            device.Read(0, bb);

            bb.Position = 510;
            if (bb.ReadByte() != 0x55)
            {
                throw new IOException(
                          "missing boot sector signature");
            }
            bb.Position = 511;
            if (bb.ReadByte() != 0xaa)
            {
                throw new IOException(
                          "missing boot sector signature");
            }

            bb.Position = SECTORS_PER_CLUSTER_OFFSET;
            var sectorsPerCluster = (byte)bb.ReadByte();

            if (sectorsPerCluster <= 0)
            {
                throw new IOException(
                          "suspicious sectors per cluster count " + sectorsPerCluster);
            }

            var rootDirEntriesB = new byte[2];

            bb.Position = Fat16BootSector.ROOT_DIR_ENTRIES_OFFSET;
            bb.Read(rootDirEntriesB, 0, 2);
            if (!BitConverter.IsLittleEndian)
            {
                Array.Reverse(rootDirEntriesB, 0, 2);
            }
            int rootDirEntries = BitConverter.ToUInt16(rootDirEntriesB, 0);

            var rootDirSectors = ((rootDirEntries * 32) +
                                  (device.GetSectorSize() - 1)) / device.GetSectorSize();

            var total16B = new byte[2];

            bb.Position = TOTAL_SECTORS_16_OFFSET;
            bb.Read(total16B, 0, 2);
            if (!BitConverter.IsLittleEndian)
            {
                Array.Reverse(total16B, 0, 2);
            }
            int total16 = BitConverter.ToUInt16(total16B, 0);

            var total32B = new byte[4];

            bb.Position = TOTAL_SECTORS_32_OFFSET;
            bb.Read(total32B, 0, 4);
            if (!BitConverter.IsLittleEndian)
            {
                Array.Reverse(total32B, 0, 4);
            }
            long total32 = BitConverter.ToUInt32(total32B, 0);

            var totalSectors = total16 == 0 ? total32 : total16;

            var fatSz16B = new byte[2];

            bb.Position = Fat16BootSector.SECTORS_PER_FAT_OFFSET;
            bb.Read(fatSz16B, 0, 2);
            if (!BitConverter.IsLittleEndian)
            {
                Array.Reverse(fatSz16B, 0, 2);
            }
            int fatSz16 = BitConverter.ToUInt16(fatSz16B, 0);

            var fatSz32B = new byte[4];

            bb.Position = Fat32BootSector.SECTORS_PER_FAT_OFFSET;
            bb.Read(fatSz32B, 0, 4);
            if (!BitConverter.IsLittleEndian)
            {
                Array.Reverse(fatSz32B, 0, 4);
            }
            long fatSz32 = BitConverter.ToUInt32(fatSz32B, 0);

            var fatSz = fatSz16 == 0 ? fatSz32 : fatSz16;

            var reservedSectorsB = new byte[2];

            bb.Position = RESERVED_SECTORS_OFFSET;
            bb.Read(reservedSectorsB, 0, 2);
            if (!BitConverter.IsLittleEndian)
            {
                Array.Reverse(reservedSectorsB, 0, 2);
            }
            int reservedSectors = BitConverter.ToUInt16(reservedSectorsB, 0);

            bb.Position = FAT_COUNT_OFFSET;
            var fatCount    = bb.ReadByte();
            var dataSectors = totalSectors - (reservedSectors +
                                              (fatCount * fatSz) + rootDirSectors);

            var clusterCount = dataSectors / sectorsPerCluster;

            var result =
                (clusterCount > Fat16BootSector.MAX_FAT16_CLUSTERS) ?
                (BootSector) new Fat32BootSector(device) : (BootSector) new Fat16BootSector(device);

            result.Read();
            return(result);
        }
Exemplo n.º 3
0
 /// <summary>
 /// Reads the contents of this <see cref="Sector"/> from the device into
 /// internal buffer and resets the "dirty" state.
 /// </summary>
 /// <exception cref="IOException">IOException on read error</exception>
 /// <seealso cref="IsDirty"/>
 protected void Read()
 {
     Buffer.Position = 0;
     device.Read(offset, Buffer);
     dirty = false;
 }
Exemplo n.º 4
0
 protected override void Read(MemoryStream data)
 {
     device.Read(deviceOffset, data);
 }