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 + " " + xEntry.mSize + " bytes : Attrib = " + xAttrib + ", Status = " + xStatus); xResult.Add(xEntry); @@ -683,7 +683,8 @@ namespace Cosmos.System.FileSystem.FAT.Listing if (xData.Length > 0) { var xValue = new byte[aEntryMetadata.DataLength]; - xValue = BitConverter.GetBytes(aValue); + byte[] data = BitConverter.GetBytes(aValue); + Array.Copy(data, 0, xValue, 0, data.Length); uint offset = mEntryHeaderDataOffset + aEntryMetadata.DataOffset; Array.Copy(xValue, 0, xData, offset, aEntryMetadata.DataLength); ((FatDirectoryEntry)mParent).SetDirectoryEntryData(xData); @@ -706,7 +707,8 @@ namespace Cosmos.System.FileSystem.FAT.Listing if (xData.Length > 0) { var xValue = new byte[aEntryMetadata.DataLength]; - xValue = BitConverter.GetBytes(aValue); + byte[] data = BitConverter.GetBytes(aValue); + Array.Copy(data, 0, xValue, 0, data.Length); uint offset = mEntryHeaderDataOffset + aEntryMetadata.DataOffset; Global.mFileSystemDebugger.SendInternal("offset ="); Global.mFileSystemDebugger.SendInternal(offset); @@ -751,7 +753,8 @@ namespace Cosmos.System.FileSystem.FAT.Listing if (xData.Length > 0) { var xValue = new byte[aEntryMetadata.DataLength]; - xValue = BitConverter.GetBytes(aValue); + byte[] data = BitConverter.GetBytes(aValue); + Array.Copy(data, 0, xValue, 0, data.Length); uint offset = aEntryHeaderDataOffset + aEntryMetadata.DataOffset; Array.Copy(xValue, 0, xData, (int)offset, (int)aEntryMetadata.DataLength); SetDirectoryEntryData(xData); @@ -769,7 +772,8 @@ namespace Cosmos.System.FileSystem.FAT.Listing if (xData.Length > 0) { var xValue = new byte[aEntryMetadata.DataLength]; - xValue = BitConverter.GetBytes(aValue); + byte[] data = BitConverter.GetBytes(aValue); + Array.Copy(data, 0, xValue, 0, data.Length); uint offset = aEntryHeaderDataOffset + aEntryMetadata.DataOffset; Global.mFileSystemDebugger.SendInternal("offset ="); Global.mFileSystemDebugger.SendInternal(offset); @@ -789,7 +793,7 @@ namespace Cosmos.System.FileSystem.FAT.Listing if (xData.Length > 0) { var xValue = new byte[aEntryMetadata.DataLength]; - xValue = Encoding.Unicode.GetBytes(aValue.ToCharArray(), 0, (int)aEntryMetadata.DataLength / 2); + xValue = Encoding.Unicode.GetBytes(aValue); uint offset = aEntryHeaderDataOffset + aEntryMetadata.DataOffset; Array.Copy(xValue, 0, xData, (int)offset, (int)aEntryMetadata.DataLength); diff --git a/source/Cosmos.System2/Graphics/Bitmap.cs b/source/Cosmos.System2/Graphics/Bitmap.cs index e79594a03..db3cec708 100644 --- a/source/Cosmos.System2/Graphics/Bitmap.cs +++ b/source/Cosmos.System2/Graphics/Bitmap.cs @@ -65,36 +65,36 @@ namespace Cosmos.System.Graphics //read size of BMP file - byte 2 -> 6 stream.Read(_int, 0, 4); - uint fileSize = (uint)BitConverter.ToInt32(_int, 0); + uint fileSize = BitConverter.ToUInt32(_int, 0); stream.Position = 10; //read header - bytes 10 -> 14 is the offset of the bitmap image data stream.Read(_int, 0, 4); - uint pixelTableOffset = (uint)BitConverter.ToInt32(_int, 0); + uint pixelTableOffset = BitConverter.ToUInt32(_int, 0); //now reading size of BITMAPINFOHEADER should be 40 - bytes 14 -> 18 stream.Read(_int, 0, 4); - uint infoHeaderSize = (uint)BitConverter.ToInt32(_int, 0); + uint infoHeaderSize = BitConverter.ToUInt32(_int, 0); if (infoHeaderSize != 40) { throw new Exception("Info header size has the wrong value!"); } //now reading width of image in pixels - bytes 18 -> 22 stream.Read(_int, 0, 4); - uint imageWidth = (uint)BitConverter.ToInt32(_int, 0); + uint imageWidth = BitConverter.ToUInt32(_int, 0); //now reading height of image in pixels - byte 22 -> 26 stream.Read(_int, 0, 4); - uint imageHeight = (uint)BitConverter.ToInt32(_int, 0); + uint imageHeight = BitConverter.ToUInt32(_int, 0); //now reading number of planes should be 1 - byte 26 -> 28 stream.Read(_short, 0, 2); - ushort planes = (ushort)BitConverter.ToInt16(_short, 0); + ushort planes = BitConverter.ToUInt16(_short, 0); if (planes != 1) throw new Exception("Number of planes is not 1! Can not read file!"); //now reading size of bits per pixel (1, 4, 8, 24, 32) - bytes 28 - 30 stream.Read(_short, 0, 2); - ushort pixelSize = (ushort)BitConverter.ToInt16(_short, 0); + ushort pixelSize = BitConverter.ToUInt16(_short, 0); //TODO: Be able to handle other pixel sizes if (!(pixelSize == 32 || pixelSize == 24)) { @@ -102,13 +102,13 @@ namespace Cosmos.System.Graphics } //now reading compression type - bytes 30 -> 34 stream.Read(_int, 0, 4); - uint compression = (uint)BitConverter.ToInt32(_int, 0); + uint compression = BitConverter.ToUInt32(_int, 0); //TODO: Be able to handle compressed files if (compression != 0) throw new NotImplementedException("Can only handle uncompressed files!"); //now reading total image data size(including padding) - bytes 34 -> 38 stream.Read(_int, 0, 4); - uint totalImageSize = (uint)BitConverter.ToInt32(_int, 0); + uint totalImageSize = BitConverter.ToUInt32(_int, 0); #endregion BMP Header