Fixed requested changes

Added plugs for BitConverter ToUInt16, 32 and 64 methods
Used ToUInt methods
This commit is contained in:
Quajak 2018-05-20 17:46:46 +02:00
parent 7635fcdb77
commit ee4c09bcdb
7 changed files with 77 additions and 31 deletions

View file

@ -160,10 +160,10 @@ namespace Cosmos.Kernel.Tests.Fat.System.IO
xFile2.Position = 0; xFile2.Position = 0;
byte[] xReadBuff = new byte[xWriteBuff.Length]; byte[] xReadBuff = new byte[xWriteBuff.Length];
xFile2.Read(xReadBuff, 0, 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 xWriteBuffAsString = Encoding.UTF8.GetString(xWriteBuff);
string xReadBuffAsString = Encoding.UTF8.GetString(xReadBuff); string xReadBuffAsString = Encoding.UTF8.GetString(xReadBuff);
mDebugger.Send("xWriteBuff=" + xWriteBuffAsString);
mDebugger.Send("xReadBuff =" + xReadBuffAsString);
mDebugger.Send("xWriteBuffAsString=" + xWriteBuffAsString); mDebugger.Send("xWriteBuffAsString=" + xWriteBuffAsString);
mDebugger.Send("xReadBuffAsString =" + xReadBuffAsString); mDebugger.Send("xReadBuffAsString =" + xReadBuffAsString);
Assert.IsTrue(xWriteBuffAsString == xReadBuffAsString, "Failed to write and read file"); Assert.IsTrue(xWriteBuffAsString == xReadBuffAsString, "Failed to write and read file");

View file

@ -203,6 +203,45 @@ namespace Cosmos.Core_Plugs.System
return *(double*)&val; 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() private static void ThrowValueArgumentNull()
{ {
throw new ArgumentNullException("value"); throw new ArgumentNullException("value");

View file

@ -41,8 +41,8 @@ namespace Cosmos.HAL.BlockDevice
} }
else if (xSystemID != 0) else if (xSystemID != 0)
{ {
UInt32 xStartSector = (uint)BitConverter.ToInt32(aEBR, (int)aLoc + 8); UInt32 xStartSector = BitConverter.ToUInt32(aEBR, (int)aLoc + 8);
UInt32 xSectorCount = (uint)BitConverter.ToInt32(aEBR, (int)aLoc + 12); UInt32 xSectorCount = BitConverter.ToUInt32(aEBR, (int)aLoc + 12);
var xPartInfo = new PartInfo(xSystemID, xStartSector, xSectorCount); var xPartInfo = new PartInfo(xSystemID, xStartSector, xSectorCount);
Partitions.Add(xPartInfo); Partitions.Add(xPartInfo);

View file

@ -51,12 +51,12 @@ namespace Cosmos.HAL.BlockDevice
//DOS only knows about 05, Windows 95 introduced 0F, Linux introduced 85 //DOS only knows about 05, Windows 95 introduced 0F, Linux introduced 85
//Search for logical volumes //Search for logical volumes
//http://thestarman.pcministry.com/asm/mbr/PartTables2.htm //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) else if (xSystemID != 0)
{ {
UInt32 xStartSector = (uint)BitConverter.ToInt32(aMBR, (int)aLoc + 8); UInt32 xStartSector = BitConverter.ToUInt32(aMBR, (int)aLoc + 8);
UInt32 xSectorCount = (uint)BitConverter.ToInt32(aMBR, (int)aLoc + 12); UInt32 xSectorCount = BitConverter.ToUInt32(aMBR, (int)aLoc + 12);
var xPartInfo = new PartInfo(xSystemID, xStartSector, xSectorCount); var xPartInfo = new PartInfo(xSystemID, xStartSector, xSectorCount);
Partitions.Add(xPartInfo); Partitions.Add(xPartInfo);

View file

@ -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 // 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 // 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. // 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) if ((aEntryNumber & 0x01) == 0)
{ {
aValue = xResult & 0x0FFF; // Even aValue = xResult & 0x0FFF; // Even
@ -371,16 +371,19 @@ namespace Cosmos.System.FileSystem.FAT
public override string Type public override string Type
{ {
get get
{ {
switch (mFatType) switch (mFatType)
{ {
case FatTypeEnum.Fat12: case FatTypeEnum.Fat12:
return "FAT12"; return "FAT12";
case FatTypeEnum.Fat16: case FatTypeEnum.Fat16:
return "FAT16"; return "FAT16";
case FatTypeEnum.Fat32: case FatTypeEnum.Fat32:
return "FAT32"; return "FAT32";
default: default:
throw new Exception("Unknown FAT file system type."); throw new Exception("Unknown FAT file system type.");
} }
@ -725,7 +728,7 @@ namespace Cosmos.System.FileSystem.FAT
get get
{ {
Global.mFileSystemDebugger.SendInternal("-- FatFileSystem.mLabel --"); Global.mFileSystemDebugger.SendInternal("-- FatFileSystem.mLabel --");
var RootDirectory = (FatDirectoryEntry) GetRootDirectory(); var RootDirectory = (FatDirectoryEntry)GetRootDirectory();
var VolumeId = RootDirectory.FindVolumeId(); var VolumeId = RootDirectory.FindVolumeId();
if (VolumeId == null) if (VolumeId == null)
@ -741,7 +744,7 @@ namespace Cosmos.System.FileSystem.FAT
{ {
Global.mFileSystemDebugger.SendInternal($"Setting Volume label to {value}"); Global.mFileSystemDebugger.SendInternal($"Setting Volume label to {value}");
var RootDirectory = (FatDirectoryEntry) GetRootDirectory(); var RootDirectory = (FatDirectoryEntry)GetRootDirectory();
var VolumeId = RootDirectory.FindVolumeId(); var VolumeId = RootDirectory.FindVolumeId();
if (VolumeId != null) if (VolumeId != null)

View file

@ -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 // 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 // this by also looking at the length. Since we short circuit the or, the length
// is rarely evaluated. // 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); 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); 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) 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) if (xSize == 0 && xName.Length == 0)
{ {
@ -484,7 +484,7 @@ namespace Cosmos.System.FileSystem.FAT.Listing
else if (xTest == FatDirectoryEntryAttributeConsts.Directory) else if (xTest == FatDirectoryEntryAttributeConsts.Directory)
{ {
string xFullPath = Path.Combine(mFullPath, xName); 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); var xEntry = new FatDirectoryEntry(((FatFileSystem)mFileSystem), xParent, xFullPath, xName, xSize, xFirstCluster, i, DirectoryEntryTypeEnum.Directory);
Global.mFileSystemDebugger.SendInternal(xEntry.mName + " <DIR> " + xEntry.mSize + " bytes : Attrib = " + xAttrib + ", Status = " + xStatus); Global.mFileSystemDebugger.SendInternal(xEntry.mName + " <DIR> " + xEntry.mSize + " bytes : Attrib = " + xAttrib + ", Status = " + xStatus);
xResult.Add(xEntry); xResult.Add(xEntry);
@ -683,7 +683,8 @@ namespace Cosmos.System.FileSystem.FAT.Listing
if (xData.Length > 0) if (xData.Length > 0)
{ {
var xValue = new byte[aEntryMetadata.DataLength]; 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; uint offset = mEntryHeaderDataOffset + aEntryMetadata.DataOffset;
Array.Copy(xValue, 0, xData, offset, aEntryMetadata.DataLength); Array.Copy(xValue, 0, xData, offset, aEntryMetadata.DataLength);
((FatDirectoryEntry)mParent).SetDirectoryEntryData(xData); ((FatDirectoryEntry)mParent).SetDirectoryEntryData(xData);
@ -706,7 +707,8 @@ namespace Cosmos.System.FileSystem.FAT.Listing
if (xData.Length > 0) if (xData.Length > 0)
{ {
var xValue = new byte[aEntryMetadata.DataLength]; 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; uint offset = mEntryHeaderDataOffset + aEntryMetadata.DataOffset;
Global.mFileSystemDebugger.SendInternal("offset ="); Global.mFileSystemDebugger.SendInternal("offset =");
Global.mFileSystemDebugger.SendInternal(offset); Global.mFileSystemDebugger.SendInternal(offset);
@ -751,7 +753,8 @@ namespace Cosmos.System.FileSystem.FAT.Listing
if (xData.Length > 0) if (xData.Length > 0)
{ {
var xValue = new byte[aEntryMetadata.DataLength]; 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; uint offset = aEntryHeaderDataOffset + aEntryMetadata.DataOffset;
Array.Copy(xValue, 0, xData, (int)offset, (int)aEntryMetadata.DataLength); Array.Copy(xValue, 0, xData, (int)offset, (int)aEntryMetadata.DataLength);
SetDirectoryEntryData(xData); SetDirectoryEntryData(xData);
@ -769,7 +772,8 @@ namespace Cosmos.System.FileSystem.FAT.Listing
if (xData.Length > 0) if (xData.Length > 0)
{ {
var xValue = new byte[aEntryMetadata.DataLength]; 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; uint offset = aEntryHeaderDataOffset + aEntryMetadata.DataOffset;
Global.mFileSystemDebugger.SendInternal("offset ="); Global.mFileSystemDebugger.SendInternal("offset =");
Global.mFileSystemDebugger.SendInternal(offset); Global.mFileSystemDebugger.SendInternal(offset);
@ -789,7 +793,7 @@ namespace Cosmos.System.FileSystem.FAT.Listing
if (xData.Length > 0) if (xData.Length > 0)
{ {
var xValue = new byte[aEntryMetadata.DataLength]; 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; uint offset = aEntryHeaderDataOffset + aEntryMetadata.DataOffset;
Array.Copy(xValue, 0, xData, (int)offset, (int)aEntryMetadata.DataLength); Array.Copy(xValue, 0, xData, (int)offset, (int)aEntryMetadata.DataLength);

View file

@ -65,36 +65,36 @@ namespace Cosmos.System.Graphics
//read size of BMP file - byte 2 -> 6 //read size of BMP file - byte 2 -> 6
stream.Read(_int, 0, 4); stream.Read(_int, 0, 4);
uint fileSize = (uint)BitConverter.ToInt32(_int, 0); uint fileSize = BitConverter.ToUInt32(_int, 0);
stream.Position = 10; stream.Position = 10;
//read header - bytes 10 -> 14 is the offset of the bitmap image data //read header - bytes 10 -> 14 is the offset of the bitmap image data
stream.Read(_int, 0, 4); 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 //now reading size of BITMAPINFOHEADER should be 40 - bytes 14 -> 18
stream.Read(_int, 0, 4); stream.Read(_int, 0, 4);
uint infoHeaderSize = (uint)BitConverter.ToInt32(_int, 0); uint infoHeaderSize = BitConverter.ToUInt32(_int, 0);
if (infoHeaderSize != 40) if (infoHeaderSize != 40)
{ {
throw new Exception("Info header size has the wrong value!"); throw new Exception("Info header size has the wrong value!");
} }
//now reading width of image in pixels - bytes 18 -> 22 //now reading width of image in pixels - bytes 18 -> 22
stream.Read(_int, 0, 4); 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 //now reading height of image in pixels - byte 22 -> 26
stream.Read(_int, 0, 4); 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 //now reading number of planes should be 1 - byte 26 -> 28
stream.Read(_short, 0, 2); stream.Read(_short, 0, 2);
ushort planes = (ushort)BitConverter.ToInt16(_short, 0); ushort planes = BitConverter.ToUInt16(_short, 0);
if (planes != 1) if (planes != 1)
throw new Exception("Number of planes is not 1! Can not read file!"); 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 //now reading size of bits per pixel (1, 4, 8, 24, 32) - bytes 28 - 30
stream.Read(_short, 0, 2); 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 //TODO: Be able to handle other pixel sizes
if (!(pixelSize == 32 || pixelSize == 24)) if (!(pixelSize == 32 || pixelSize == 24))
{ {
@ -102,13 +102,13 @@ namespace Cosmos.System.Graphics
} }
//now reading compression type - bytes 30 -> 34 //now reading compression type - bytes 30 -> 34
stream.Read(_int, 0, 4); 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 //TODO: Be able to handle compressed files
if (compression != 0) if (compression != 0)
throw new NotImplementedException("Can only handle uncompressed files!"); throw new NotImplementedException("Can only handle uncompressed files!");
//now reading total image data size(including padding) - bytes 34 -> 38 //now reading total image data size(including padding) - bytes 34 -> 38
stream.Read(_int, 0, 4); stream.Read(_int, 0, 4);
uint totalImageSize = (uint)BitConverter.ToInt32(_int, 0); uint totalImageSize = BitConverter.ToUInt32(_int, 0);
#endregion BMP Header #endregion BMP Header