1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- using Utilities;
- namespace DiskAccessLibrary.VHD
- {
-
-
-
- public class BlockAllocationTable
- {
- public const uint UnusedEntry = 0xFFFFFFFF;
- private uint[] m_entries;
- public BlockAllocationTable(uint maxTableEntries)
- {
- m_entries = new uint[maxTableEntries];
- for (int index = 0; index < maxTableEntries; index++)
- {
- m_entries[index] = UnusedEntry;
- }
- }
- public BlockAllocationTable(byte[] buffer, uint maxTableEntries)
- {
- m_entries = new uint[maxTableEntries];
- for (int index = 0; index < maxTableEntries; index++)
- {
- m_entries[index] = BigEndianConverter.ToUInt32(buffer, index * 4);
- }
- }
- public byte[] GetBytes()
- {
-
- int bufferLength = (int)Math.Ceiling((double)m_entries.Length * 4 / VirtualHardDisk.BytesPerDiskSector) * VirtualHardDisk.BytesPerDiskSector;
- byte[] buffer = new byte[bufferLength];
- for (int index = 0; index < m_entries.Length; index++)
- {
- BigEndianWriter.WriteUInt32(buffer, index * 4, m_entries[index]);
- }
- return buffer;
- }
- public bool IsBlockInUse(uint blockIndex)
- {
- return m_entries[blockIndex] != UnusedEntry;
- }
- public bool IsBlockInUse(uint blockIndex, out uint blockStartSector)
- {
- blockStartSector = m_entries[blockIndex];
- return m_entries[blockIndex] != UnusedEntry;
- }
- public void SetBlockStartSector(uint blockIndex, uint blockStartSector)
- {
- if (m_entries[blockIndex] != UnusedEntry)
- {
- throw new InvalidOperationException("Block is already allocated");
- }
- m_entries[blockIndex] = blockStartSector;
- }
- public static BlockAllocationTable ReadBlockAllocationTable(string path, DynamicDiskHeader dynamicHeader)
- {
- uint maxTableEntries = dynamicHeader.MaxTableEntries;
- long sectorIndex = (long)(dynamicHeader.TableOffset / VirtualHardDisk.BytesPerDiskSector);
- int sectorCount = (int)Math.Ceiling((double)maxTableEntries * 4 / VirtualHardDisk.BytesPerDiskSector);
- byte[] buffer = new RawDiskImage(path, VirtualHardDisk.BytesPerDiskSector).ReadSectors(sectorIndex, sectorCount);
- return new BlockAllocationTable(buffer, maxTableEntries);
- }
- }
- }
|