diff --git a/Tests/Cosmos.Kernel.Tests.Fat/System.IO/FileTest.cs b/Tests/Cosmos.Kernel.Tests.Fat/System.IO/FileTest.cs index 02ebaf444..1a25e02b3 100644 --- a/Tests/Cosmos.Kernel.Tests.Fat/System.IO/FileTest.cs +++ b/Tests/Cosmos.Kernel.Tests.Fat/System.IO/FileTest.cs @@ -160,10 +160,10 @@ namespace Cosmos.Kernel.Tests.Fat.System.IO xFile2.Position = 0; byte[] xReadBuff = new byte[xWriteBuff.Length]; xFile2.Read(xReadBuff, 0, xWriteBuff.Length); - mDebugger.Send("xWriteBuff=" + Encoding.UTF8.GetString(xWriteBuff)); - mDebugger.Send("xReadBuff =" + Encoding.UTF8.GetString(xReadBuff)); string xWriteBuffAsString = Encoding.UTF8.GetString(xWriteBuff); string xReadBuffAsString = Encoding.UTF8.GetString(xReadBuff); + mDebugger.Send("xWriteBuff=" + xWriteBuffAsString); + mDebugger.Send("xReadBuff =" + xReadBuffAsString); mDebugger.Send("xWriteBuffAsString=" + xWriteBuffAsString); mDebugger.Send("xReadBuffAsString =" + xReadBuffAsString); Assert.IsTrue(xWriteBuffAsString == xReadBuffAsString, "Failed to write and read file"); diff --git a/source/Cosmos.Core_Plugs/System/BitConverterImpl.cs b/source/Cosmos.Core_Plugs/System/BitConverterImpl.cs index 286232ef9..360ccdc66 100644 --- a/source/Cosmos.Core_Plugs/System/BitConverterImpl.cs +++ b/source/Cosmos.Core_Plugs/System/BitConverterImpl.cs @@ -203,6 +203,45 @@ namespace Cosmos.Core_Plugs.System return *(double*)&val; } + public static ushort ToUInt16(byte[] value, int startIndex) + { + if (value == null) + throw new ArgumentNullException("value"); + if ((uint)startIndex > value.Length) + throw new ArgumentOutOfRangeException("startIndex"); + if (startIndex > value.Length - 2) + throw new ArgumentException("Array with offset is too short"); + Contract.EndContractBlock(); + + return (ushort)ToInt16(value, startIndex); + } + + public static uint ToUInt32(byte[] value, int startIndex) + { + if (value == null) + throw new ArgumentNullException("value"); + if ((uint)startIndex > value.Length) + throw new ArgumentOutOfRangeException("startIndex"); + if (startIndex > value.Length - 4) + throw new ArgumentException("Array with offset is too short"); + Contract.EndContractBlock(); + + return (uint)ToInt32(value, startIndex); + } + + public static ulong ToUInt64(byte[] value, int startIndex) + { + if (value == null) + throw new ArgumentNullException("value"); + if ((uint)startIndex > value.Length) + throw new ArgumentOutOfRangeException("startIndex"); + if (startIndex > value.Length - 8) + throw new ArgumentException("Array with offset is too short"); + Contract.EndContractBlock(); + + return (ulong)ToInt64(value, startIndex); + } + private static void ThrowValueArgumentNull() { throw new ArgumentNullException("value"); diff --git a/source/Cosmos.HAL2/BlockDevice/EBR.cs b/source/Cosmos.HAL2/BlockDevice/EBR.cs index ec6199760..36458bc38 100644 --- a/source/Cosmos.HAL2/BlockDevice/EBR.cs +++ b/source/Cosmos.HAL2/BlockDevice/EBR.cs @@ -41,8 +41,8 @@ namespace Cosmos.HAL.BlockDevice } else if (xSystemID != 0) { - UInt32 xStartSector = (uint)BitConverter.ToInt32(aEBR, (int)aLoc + 8); - UInt32 xSectorCount = (uint)BitConverter.ToInt32(aEBR, (int)aLoc + 12); + UInt32 xStartSector = BitConverter.ToUInt32(aEBR, (int)aLoc + 8); + UInt32 xSectorCount = BitConverter.ToUInt32(aEBR, (int)aLoc + 12); var xPartInfo = new PartInfo(xSystemID, xStartSector, xSectorCount); Partitions.Add(xPartInfo); diff --git a/source/Cosmos.HAL2/BlockDevice/MBR.cs b/source/Cosmos.HAL2/BlockDevice/MBR.cs index 86246df72..a7754ffc7 100644 --- a/source/Cosmos.HAL2/BlockDevice/MBR.cs +++ b/source/Cosmos.HAL2/BlockDevice/MBR.cs @@ -51,12 +51,12 @@ namespace Cosmos.HAL.BlockDevice //DOS only knows about 05, Windows 95 introduced 0F, Linux introduced 85 //Search for logical volumes //http://thestarman.pcministry.com/asm/mbr/PartTables2.htm - EBRLocation = (uint)BitConverter.ToInt32(aMBR, (int)aLoc + 8); + EBRLocation = BitConverter.ToUInt32(aMBR, (int)aLoc + 8); } else if (xSystemID != 0) { - UInt32 xStartSector = (uint)BitConverter.ToInt32(aMBR, (int)aLoc + 8); - UInt32 xSectorCount = (uint)BitConverter.ToInt32(aMBR, (int)aLoc + 12); + UInt32 xStartSector = BitConverter.ToUInt32(aMBR, (int)aLoc + 8); + UInt32 xSectorCount = BitConverter.ToUInt32(aMBR, (int)aLoc + 12); var xPartInfo = new PartInfo(xSystemID, xStartSector, xSectorCount); Partitions.Add(xPartInfo); diff --git a/source/Cosmos.System2/FileSystem/FAT/FatFileSystem.cs b/source/Cosmos.System2/FileSystem/FAT/FatFileSystem.cs index c1800f0a8..e03bd882c 100644 --- a/source/Cosmos.System2/FileSystem/FAT/FatFileSystem.cs +++ b/source/Cosmos.System2/FileSystem/FAT/FatFileSystem.cs @@ -219,7 +219,7 @@ namespace Cosmos.System.FileSystem.FAT // We now access the FAT entry as a WORD just as we do for FAT16, but if the cluster number is // EVEN, we only want the low 12-bits of the 16-bits we fetch. If the cluster number is ODD // we want the high 12-bits of the 16-bits we fetch. - uint xResult = BitConverter.ToUInt32(xData, (int)xEntryOffset); + uint xResult = BitConverter.ToUInt16(xData, (int)xEntryOffset); if ((aEntryNumber & 0x01) == 0) { aValue = xResult & 0x0FFF; // Even @@ -371,16 +371,19 @@ namespace Cosmos.System.FileSystem.FAT public override string Type { - get + get { switch (mFatType) { case FatTypeEnum.Fat12: return "FAT12"; + case FatTypeEnum.Fat16: return "FAT16"; + case FatTypeEnum.Fat32: return "FAT32"; + default: throw new Exception("Unknown FAT file system type."); } @@ -721,11 +724,11 @@ namespace Cosmos.System.FileSystem.FAT { /* * In the FAT filesystem the name field of RootDirectory is - in reality - the Volume Label - */ + */ get { Global.mFileSystemDebugger.SendInternal("-- FatFileSystem.mLabel --"); - var RootDirectory = (FatDirectoryEntry) GetRootDirectory(); + var RootDirectory = (FatDirectoryEntry)GetRootDirectory(); var VolumeId = RootDirectory.FindVolumeId(); if (VolumeId == null) @@ -741,7 +744,7 @@ namespace Cosmos.System.FileSystem.FAT { Global.mFileSystemDebugger.SendInternal($"Setting Volume label to {value}"); - var RootDirectory = (FatDirectoryEntry) GetRootDirectory(); + var RootDirectory = (FatDirectoryEntry)GetRootDirectory(); var VolumeId = RootDirectory.FindVolumeId(); if (VolumeId != null) diff --git a/source/Cosmos.System2/FileSystem/FAT/Listing/FatDiretoryEntry.cs b/source/Cosmos.System2/FileSystem/FAT/Listing/FatDiretoryEntry.cs index 0920f3a41..c6319b3aa 100644 --- a/source/Cosmos.System2/FileSystem/FAT/Listing/FatDiretoryEntry.cs +++ b/source/Cosmos.System2/FileSystem/FAT/Listing/FatDiretoryEntry.cs @@ -375,11 +375,11 @@ namespace Cosmos.System.FileSystem.FAT.Listing // So we only want to stop if the 0xFFFF is AFTER a 0x0000. We can determin // this by also looking at the length. Since we short circuit the or, the length // is rarely evaluated. - if (BitConverter.ToInt32(xData, (int)i + 14) != 0xFFFF || xLongPart.Length == 5) + if (BitConverter.ToUInt16(xData, (int)i + 14) != 0xFFFF || xLongPart.Length == 5) { xLongPart = xLongPart + Encoding.Unicode.GetString(xData, (int)i + 14, 6); - if (BitConverter.ToInt32(xData, (int)i + 28) != 0xFFFF || xLongPart.Length == 11) + if (BitConverter.ToUInt16(xData, (int)i + 28) != 0xFFFF || xLongPart.Length == 11) { xLongPart = xLongPart + Encoding.Unicode.GetString(xData, (int)i + 28, 2); } @@ -465,11 +465,11 @@ namespace Cosmos.System.FileSystem.FAT.Listing } } - uint xFirstCluster = ((uint)BitConverter.ToInt32(xData, (int)i + 20) << 16 | (uint)BitConverter.ToInt32(xData, (int)i + 26)); + uint xFirstCluster = (BitConverter.ToUInt32(xData, (int)i + 20) << 16 | BitConverter.ToUInt32(xData, (int)i + 26)); if (xTest == 0) { - uint xSize = (uint)BitConverter.ToInt32(xData, (int)i + 28); + uint xSize = BitConverter.ToUInt32(xData, (int)i + 28); if (xSize == 0 && xName.Length == 0) { @@ -484,7 +484,7 @@ namespace Cosmos.System.FileSystem.FAT.Listing else if (xTest == FatDirectoryEntryAttributeConsts.Directory) { string xFullPath = Path.Combine(mFullPath, xName); - uint xSize = (uint)BitConverter.ToInt32(xData, (int)i + 28); + uint xSize = BitConverter.ToUInt32(xData, (int)i + 28); var xEntry = new FatDirectoryEntry(((FatFileSystem)mFileSystem), xParent, xFullPath, xName, xSize, xFirstCluster, i, DirectoryEntryTypeEnum.Directory); Global.mFileSystemDebugger.SendInternal(xEntry.mName + "