Move to file scoped namespaces.

This commit is contained in:
2022-03-06 13:29:37 +00:00
parent c7178d94c6
commit 3b6091c02c
119 changed files with 38048 additions and 38172 deletions

View File

@@ -36,327 +36,326 @@ using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Aaru.CommonTypes.Structs.Devices.SCSI;
namespace Aaru.Decoders.SCSI
namespace Aaru.Decoders.SCSI;
[SuppressMessage("ReSharper", "InconsistentNaming"), SuppressMessage("ReSharper", "MemberCanBeInternal"),
SuppressMessage("ReSharper", "MemberCanBePrivate.Global")]
public static partial class Modes
{
[SuppressMessage("ReSharper", "InconsistentNaming"), SuppressMessage("ReSharper", "MemberCanBeInternal"),
SuppressMessage("ReSharper", "MemberCanBePrivate.Global")]
public static partial class Modes
public static ModeHeader? DecodeModeHeader10(byte[] modeResponse, PeripheralDeviceTypes deviceType)
{
public static ModeHeader? DecodeModeHeader10(byte[] modeResponse, PeripheralDeviceTypes deviceType)
if(modeResponse == null ||
modeResponse.Length < 8)
return null;
ushort modeLength = (ushort)((modeResponse[0] << 8) + modeResponse[1]);
ushort blockDescLength = (ushort)((modeResponse[6] << 8) + modeResponse[7]);
if(modeResponse.Length < modeLength)
return null;
var header = new ModeHeader
{
if(modeResponse == null ||
modeResponse.Length < 8)
return null;
MediumType = (MediumTypes)modeResponse[2]
};
ushort modeLength = (ushort)((modeResponse[0] << 8) + modeResponse[1]);
ushort blockDescLength = (ushort)((modeResponse[6] << 8) + modeResponse[7]);
bool longLBA = (modeResponse[4] & 0x01) == 0x01;
if(modeResponse.Length < modeLength)
return null;
var header = new ModeHeader
if(blockDescLength > 0)
if(longLBA)
{
MediumType = (MediumTypes)modeResponse[2]
};
header.BlockDescriptors = new BlockDescriptor[blockDescLength / 16];
bool longLBA = (modeResponse[4] & 0x01) == 0x01;
if(blockDescLength > 0)
if(longLBA)
for(int i = 0; i < header.BlockDescriptors.Length; i++)
{
header.BlockDescriptors = new BlockDescriptor[blockDescLength / 16];
if(12 + (i * 16) + 8 >= modeResponse.Length)
break;
for(int i = 0; i < header.BlockDescriptors.Length; i++)
header.BlockDescriptors[i] = new BlockDescriptor
{
if(12 + (i * 16) + 8 >= modeResponse.Length)
break;
Density = DensityType.Default
};
header.BlockDescriptors[i] = new BlockDescriptor
{
Density = DensityType.Default
};
byte[] temp = new byte[8];
temp[0] = modeResponse[7 + (i * 16) + 8];
temp[1] = modeResponse[6 + (i * 16) + 8];
temp[2] = modeResponse[5 + (i * 16) + 8];
temp[3] = modeResponse[4 + (i * 16) + 8];
temp[4] = modeResponse[3 + (i * 16) + 8];
temp[5] = modeResponse[2 + (i * 16) + 8];
temp[6] = modeResponse[1 + (i * 16) + 8];
temp[7] = modeResponse[0 + (i * 16) + 8];
header.BlockDescriptors[i].Blocks = BitConverter.ToUInt64(temp, 0);
header.BlockDescriptors[i].BlockLength += (uint)(modeResponse[15 + (i * 16) + 8] << 24);
header.BlockDescriptors[i].BlockLength += (uint)(modeResponse[14 + (i * 16) + 8] << 16);
header.BlockDescriptors[i].BlockLength += (uint)(modeResponse[13 + (i * 16) + 8] << 8);
header.BlockDescriptors[i].BlockLength += modeResponse[12 + (i * 16) + 8];
}
byte[] temp = new byte[8];
temp[0] = modeResponse[7 + (i * 16) + 8];
temp[1] = modeResponse[6 + (i * 16) + 8];
temp[2] = modeResponse[5 + (i * 16) + 8];
temp[3] = modeResponse[4 + (i * 16) + 8];
temp[4] = modeResponse[3 + (i * 16) + 8];
temp[5] = modeResponse[2 + (i * 16) + 8];
temp[6] = modeResponse[1 + (i * 16) + 8];
temp[7] = modeResponse[0 + (i * 16) + 8];
header.BlockDescriptors[i].Blocks = BitConverter.ToUInt64(temp, 0);
header.BlockDescriptors[i].BlockLength += (uint)(modeResponse[15 + (i * 16) + 8] << 24);
header.BlockDescriptors[i].BlockLength += (uint)(modeResponse[14 + (i * 16) + 8] << 16);
header.BlockDescriptors[i].BlockLength += (uint)(modeResponse[13 + (i * 16) + 8] << 8);
header.BlockDescriptors[i].BlockLength += modeResponse[12 + (i * 16) + 8];
}
else
{
header.BlockDescriptors = new BlockDescriptor[blockDescLength / 8];
for(int i = 0; i < header.BlockDescriptors.Length; i++)
{
if(7 + (i * 8) + 8 >= modeResponse.Length)
break;
header.BlockDescriptors[i] = new BlockDescriptor();
if(deviceType != PeripheralDeviceTypes.DirectAccess)
header.BlockDescriptors[i].Density = (DensityType)modeResponse[0 + (i * 8) + 8];
else
{
header.BlockDescriptors[i].Density = DensityType.Default;
header.BlockDescriptors[i].Blocks += (ulong)(modeResponse[0 + (i * 8) + 8] << 24);
}
header.BlockDescriptors[i].Blocks += (ulong)(modeResponse[1 + (i * 8) + 8] << 16);
header.BlockDescriptors[i].Blocks += (ulong)(modeResponse[2 + (i * 8) + 8] << 8);
header.BlockDescriptors[i].Blocks += modeResponse[3 + (i * 8) + 8];
header.BlockDescriptors[i].BlockLength += (uint)(modeResponse[5 + (i * 8) + 8] << 16);
header.BlockDescriptors[i].BlockLength += (uint)(modeResponse[6 + (i * 8) + 8] << 8);
header.BlockDescriptors[i].BlockLength += modeResponse[7 + (i * 8) + 8];
}
}
switch(deviceType)
}
else
{
case PeripheralDeviceTypes.DirectAccess:
case PeripheralDeviceTypes.MultiMediaDevice:
header.WriteProtected = (modeResponse[3] & 0x80) == 0x80;
header.DPOFUA = (modeResponse[3] & 0x10) == 0x10;
header.BlockDescriptors = new BlockDescriptor[blockDescLength / 8];
break;
case PeripheralDeviceTypes.SequentialAccess:
header.WriteProtected = (modeResponse[3] & 0x80) == 0x80;
header.Speed = (byte)(modeResponse[3] & 0x0F);
header.BufferedMode = (byte)((modeResponse[3] & 0x70) >> 4);
for(int i = 0; i < header.BlockDescriptors.Length; i++)
{
if(7 + (i * 8) + 8 >= modeResponse.Length)
break;
break;
case PeripheralDeviceTypes.PrinterDevice:
header.BufferedMode = (byte)((modeResponse[3] & 0x70) >> 4);
header.BlockDescriptors[i] = new BlockDescriptor();
break;
case PeripheralDeviceTypes.OpticalDevice:
header.WriteProtected = (modeResponse[3] & 0x80) == 0x80;
header.EBC = (modeResponse[3] & 0x01) == 0x01;
header.DPOFUA = (modeResponse[3] & 0x10) == 0x10;
if(deviceType != PeripheralDeviceTypes.DirectAccess)
header.BlockDescriptors[i].Density = (DensityType)modeResponse[0 + (i * 8) + 8];
else
{
header.BlockDescriptors[i].Density = DensityType.Default;
header.BlockDescriptors[i].Blocks += (ulong)(modeResponse[0 + (i * 8) + 8] << 24);
}
break;
header.BlockDescriptors[i].Blocks += (ulong)(modeResponse[1 + (i * 8) + 8] << 16);
header.BlockDescriptors[i].Blocks += (ulong)(modeResponse[2 + (i * 8) + 8] << 8);
header.BlockDescriptors[i].Blocks += modeResponse[3 + (i * 8) + 8];
header.BlockDescriptors[i].BlockLength += (uint)(modeResponse[5 + (i * 8) + 8] << 16);
header.BlockDescriptors[i].BlockLength += (uint)(modeResponse[6 + (i * 8) + 8] << 8);
header.BlockDescriptors[i].BlockLength += modeResponse[7 + (i * 8) + 8];
}
}
return header;
switch(deviceType)
{
case PeripheralDeviceTypes.DirectAccess:
case PeripheralDeviceTypes.MultiMediaDevice:
header.WriteProtected = (modeResponse[3] & 0x80) == 0x80;
header.DPOFUA = (modeResponse[3] & 0x10) == 0x10;
break;
case PeripheralDeviceTypes.SequentialAccess:
header.WriteProtected = (modeResponse[3] & 0x80) == 0x80;
header.Speed = (byte)(modeResponse[3] & 0x0F);
header.BufferedMode = (byte)((modeResponse[3] & 0x70) >> 4);
break;
case PeripheralDeviceTypes.PrinterDevice:
header.BufferedMode = (byte)((modeResponse[3] & 0x70) >> 4);
break;
case PeripheralDeviceTypes.OpticalDevice:
header.WriteProtected = (modeResponse[3] & 0x80) == 0x80;
header.EBC = (modeResponse[3] & 0x01) == 0x01;
header.DPOFUA = (modeResponse[3] & 0x10) == 0x10;
break;
}
public static string PrettifyModeHeader10(byte[] modeResponse, PeripheralDeviceTypes deviceType) =>
PrettifyModeHeader(DecodeModeHeader10(modeResponse, deviceType), deviceType);
return header;
}
public static DecodedMode? DecodeMode10(byte[] modeResponse, PeripheralDeviceTypes deviceType)
public static string PrettifyModeHeader10(byte[] modeResponse, PeripheralDeviceTypes deviceType) =>
PrettifyModeHeader(DecodeModeHeader10(modeResponse, deviceType), deviceType);
public static DecodedMode? DecodeMode10(byte[] modeResponse, PeripheralDeviceTypes deviceType)
{
ModeHeader? hdr = DecodeModeHeader10(modeResponse, deviceType);
if(!hdr.HasValue)
return null;
var decoded = new DecodedMode
{
ModeHeader? hdr = DecodeModeHeader10(modeResponse, deviceType);
Header = hdr.Value
};
if(!hdr.HasValue)
return null;
bool longlba = (modeResponse[4] & 0x01) == 0x01;
int offset;
int blkDrLength = 0;
var decoded = new DecodedMode
if(decoded.Header.BlockDescriptors != null)
blkDrLength = decoded.Header.BlockDescriptors.Length;
if(longlba)
offset = 8 + (blkDrLength * 16);
else
offset = 8 + (blkDrLength * 8);
int length = modeResponse[0] << 8;
length += modeResponse[1];
length += 2;
if(length != modeResponse.Length)
return decoded;
List<ModePage> listpages = new();
while(offset < modeResponse.Length)
{
bool isSubpage = (modeResponse[offset] & 0x40) == 0x40;
var pg = new ModePage();
byte pageNo = (byte)(modeResponse[offset] & 0x3F);
if(pageNo == 0)
{
Header = hdr.Value
};
bool longlba = (modeResponse[4] & 0x01) == 0x01;
int offset;
int blkDrLength = 0;
if(decoded.Header.BlockDescriptors != null)
blkDrLength = decoded.Header.BlockDescriptors.Length;
if(longlba)
offset = 8 + (blkDrLength * 16);
pg.PageResponse = new byte[modeResponse.Length - offset];
Array.Copy(modeResponse, offset, pg.PageResponse, 0, pg.PageResponse.Length);
pg.Page = 0;
pg.Subpage = 0;
offset += pg.PageResponse.Length;
}
else
offset = 8 + (blkDrLength * 8);
int length = modeResponse[0] << 8;
length += modeResponse[1];
length += 2;
if(length != modeResponse.Length)
return decoded;
List<ModePage> listpages = new();
while(offset < modeResponse.Length)
{
bool isSubpage = (modeResponse[offset] & 0x40) == 0x40;
var pg = new ModePage();
byte pageNo = (byte)(modeResponse[offset] & 0x3F);
if(pageNo == 0)
if(isSubpage && offset + 3 < modeResponse.Length)
{
pg.PageResponse = new byte[modeResponse.Length - offset];
Array.Copy(modeResponse, offset, pg.PageResponse, 0, pg.PageResponse.Length);
pg.Page = 0;
pg.PageResponse = new byte[(modeResponse[offset + 2] << 8) + modeResponse[offset + 3] + 4];
int copyLen = pg.PageResponse.Length;
if(pg.PageResponse.Length + offset > modeResponse.Length)
copyLen = modeResponse.Length - offset;
Array.Copy(modeResponse, offset, pg.PageResponse, 0, copyLen);
pg.Page = (byte)(modeResponse[offset] & 0x3F);
pg.Subpage = modeResponse[offset + 1];
offset += pg.PageResponse.Length;
}
else if(isSubpage && offset + 1 < modeResponse.Length)
{
pg.PageResponse = new byte[modeResponse[offset + 1] + 2];
int copyLen = pg.PageResponse.Length;
if(pg.PageResponse.Length + offset > modeResponse.Length)
copyLen = modeResponse.Length - offset;
Array.Copy(modeResponse, offset, pg.PageResponse, 0, copyLen);
pg.Page = (byte)(modeResponse[offset] & 0x3F);
pg.Subpage = 0;
offset += pg.PageResponse.Length;
}
else
{
if(isSubpage && offset + 3 < modeResponse.Length)
{
pg.PageResponse = new byte[(modeResponse[offset + 2] << 8) + modeResponse[offset + 3] + 4];
int copyLen = pg.PageResponse.Length;
if(pg.PageResponse.Length + offset > modeResponse.Length)
copyLen = modeResponse.Length - offset;
Array.Copy(modeResponse, offset, pg.PageResponse, 0, copyLen);
pg.Page = (byte)(modeResponse[offset] & 0x3F);
pg.Subpage = modeResponse[offset + 1];
offset += pg.PageResponse.Length;
}
else if(isSubpage && offset + 1 < modeResponse.Length)
{
pg.PageResponse = new byte[modeResponse[offset + 1] + 2];
int copyLen = pg.PageResponse.Length;
if(pg.PageResponse.Length + offset > modeResponse.Length)
copyLen = modeResponse.Length - offset;
Array.Copy(modeResponse, offset, pg.PageResponse, 0, copyLen);
pg.Page = (byte)(modeResponse[offset] & 0x3F);
pg.Subpage = 0;
offset += pg.PageResponse.Length;
}
else
offset = modeResponse.Length;
}
listpages.Add(pg);
offset = modeResponse.Length;
}
decoded.Pages = listpages.ToArray();
return decoded;
listpages.Add(pg);
}
public static byte[] EncodeModeHeader10(ModeHeader header, PeripheralDeviceTypes deviceType,
bool longLBA = false)
decoded.Pages = listpages.ToArray();
return decoded;
}
public static byte[] EncodeModeHeader10(ModeHeader header, PeripheralDeviceTypes deviceType,
bool longLBA = false)
{
byte[] hdr;
if(header.BlockDescriptors != null)
hdr = longLBA ? new byte[8 + (header.BlockDescriptors.Length * 16)]
: new byte[8 + (header.BlockDescriptors.Length * 8)];
else
hdr = new byte[8];
hdr[2] = (byte)header.MediumType;
switch(deviceType)
{
byte[] hdr;
case PeripheralDeviceTypes.DirectAccess:
case PeripheralDeviceTypes.MultiMediaDevice:
if(header.WriteProtected)
hdr[3] += 0x80;
if(header.BlockDescriptors != null)
hdr = longLBA ? new byte[8 + (header.BlockDescriptors.Length * 16)]
: new byte[8 + (header.BlockDescriptors.Length * 8)];
else
hdr = new byte[8];
if(header.DPOFUA)
hdr[3] += 0x10;
hdr[2] = (byte)header.MediumType;
break;
case PeripheralDeviceTypes.SequentialAccess:
if(header.WriteProtected)
hdr[3] += 0x80;
switch(deviceType)
{
case PeripheralDeviceTypes.DirectAccess:
case PeripheralDeviceTypes.MultiMediaDevice:
if(header.WriteProtected)
hdr[3] += 0x80;
hdr[3] += (byte)(header.Speed & 0x0F);
hdr[3] += (byte)((header.BufferedMode << 4) & 0x70);
if(header.DPOFUA)
hdr[3] += 0x10;
break;
case PeripheralDeviceTypes.PrinterDevice:
hdr[3] += (byte)((header.BufferedMode << 4) & 0x70);
break;
case PeripheralDeviceTypes.SequentialAccess:
if(header.WriteProtected)
hdr[3] += 0x80;
break;
case PeripheralDeviceTypes.OpticalDevice:
if(header.WriteProtected)
hdr[3] += 0x80;
hdr[3] += (byte)(header.Speed & 0x0F);
hdr[3] += (byte)((header.BufferedMode << 4) & 0x70);
if(header.EBC)
hdr[3] += 0x01;
break;
case PeripheralDeviceTypes.PrinterDevice:
hdr[3] += (byte)((header.BufferedMode << 4) & 0x70);
if(header.DPOFUA)
hdr[3] += 0x10;
break;
case PeripheralDeviceTypes.OpticalDevice:
if(header.WriteProtected)
hdr[3] += 0x80;
break;
}
if(header.EBC)
hdr[3] += 0x01;
if(header.DPOFUA)
hdr[3] += 0x10;
break;
}
if(longLBA)
hdr[4] += 0x01;
if(header.BlockDescriptors == null)
return hdr;
if(longLBA)
for(int i = 0; i < header.BlockDescriptors.Length; i++)
{
byte[] temp = BitConverter.GetBytes(header.BlockDescriptors[i].Blocks);
hdr[7 + (i * 16) + 8] = temp[0];
hdr[6 + (i * 16) + 8] = temp[1];
hdr[5 + (i * 16) + 8] = temp[2];
hdr[4 + (i * 16) + 8] = temp[3];
hdr[3 + (i * 16) + 8] = temp[4];
hdr[2 + (i * 16) + 8] = temp[5];
hdr[1 + (i * 16) + 8] = temp[6];
hdr[0 + (i * 16) + 8] = temp[7];
hdr[12 + (i * 16) + 8] = (byte)((header.BlockDescriptors[i].BlockLength & 0xFF000000) >> 24);
hdr[13 + (i * 16) + 8] = (byte)((header.BlockDescriptors[i].BlockLength & 0xFF0000) >> 16);
hdr[14 + (i * 16) + 8] = (byte)((header.BlockDescriptors[i].BlockLength & 0xFF00) >> 8);
hdr[15 + (i * 16) + 8] = (byte)(header.BlockDescriptors[i].BlockLength & 0xFF);
}
else
for(int i = 0; i < header.BlockDescriptors.Length; i++)
{
if(deviceType != PeripheralDeviceTypes.DirectAccess)
hdr[0 + (i * 8) + 8] = (byte)header.BlockDescriptors[i].Density;
else
hdr[0 + (i * 8) + 8] = (byte)((header.BlockDescriptors[i].Blocks & 0xFF000000) >> 24);
hdr[1 + (i * 8) + 8] = (byte)((header.BlockDescriptors[i].Blocks & 0xFF0000) >> 16);
hdr[2 + (i * 8) + 8] = (byte)((header.BlockDescriptors[i].Blocks & 0xFF00) >> 8);
hdr[3 + (i * 8) + 8] = (byte)(header.BlockDescriptors[i].Blocks & 0xFF);
hdr[5 + (i * 8) + 8] = (byte)((header.BlockDescriptors[i].BlockLength & 0xFF0000) >> 16);
hdr[6 + (i * 8) + 8] = (byte)((header.BlockDescriptors[i].BlockLength & 0xFF00) >> 8);
hdr[7 + (i * 8) + 8] = (byte)(header.BlockDescriptors[i].BlockLength & 0xFF);
}
if(longLBA)
hdr[4] += 0x01;
if(header.BlockDescriptors == null)
return hdr;
}
public static byte[] EncodeMode10(DecodedMode mode, PeripheralDeviceTypes deviceType)
{
int modeSize = 0;
if(mode.Pages != null)
modeSize += mode.Pages.Sum(page => page.PageResponse.Length);
byte[] hdr = EncodeModeHeader10(mode.Header, deviceType);
modeSize += hdr.Length;
byte[] md = new byte[modeSize];
Array.Copy(hdr, 0, md, 0, hdr.Length);
if(mode.Pages == null)
return md;
if(longLBA)
for(int i = 0; i < header.BlockDescriptors.Length; i++)
{
int offset = hdr.Length;
byte[] temp = BitConverter.GetBytes(header.BlockDescriptors[i].Blocks);
hdr[7 + (i * 16) + 8] = temp[0];
hdr[6 + (i * 16) + 8] = temp[1];
hdr[5 + (i * 16) + 8] = temp[2];
hdr[4 + (i * 16) + 8] = temp[3];
hdr[3 + (i * 16) + 8] = temp[4];
hdr[2 + (i * 16) + 8] = temp[5];
hdr[1 + (i * 16) + 8] = temp[6];
hdr[0 + (i * 16) + 8] = temp[7];
hdr[12 + (i * 16) + 8] = (byte)((header.BlockDescriptors[i].BlockLength & 0xFF000000) >> 24);
hdr[13 + (i * 16) + 8] = (byte)((header.BlockDescriptors[i].BlockLength & 0xFF0000) >> 16);
hdr[14 + (i * 16) + 8] = (byte)((header.BlockDescriptors[i].BlockLength & 0xFF00) >> 8);
hdr[15 + (i * 16) + 8] = (byte)(header.BlockDescriptors[i].BlockLength & 0xFF);
}
else
for(int i = 0; i < header.BlockDescriptors.Length; i++)
{
if(deviceType != PeripheralDeviceTypes.DirectAccess)
hdr[0 + (i * 8) + 8] = (byte)header.BlockDescriptors[i].Density;
else
hdr[0 + (i * 8) + 8] = (byte)((header.BlockDescriptors[i].Blocks & 0xFF000000) >> 24);
foreach(ModePage page in mode.Pages)
{
Array.Copy(page.PageResponse, 0, md, offset, page.PageResponse.Length);
offset += page.PageResponse.Length;
}
hdr[1 + (i * 8) + 8] = (byte)((header.BlockDescriptors[i].Blocks & 0xFF0000) >> 16);
hdr[2 + (i * 8) + 8] = (byte)((header.BlockDescriptors[i].Blocks & 0xFF00) >> 8);
hdr[3 + (i * 8) + 8] = (byte)(header.BlockDescriptors[i].Blocks & 0xFF);
hdr[5 + (i * 8) + 8] = (byte)((header.BlockDescriptors[i].BlockLength & 0xFF0000) >> 16);
hdr[6 + (i * 8) + 8] = (byte)((header.BlockDescriptors[i].BlockLength & 0xFF00) >> 8);
hdr[7 + (i * 8) + 8] = (byte)(header.BlockDescriptors[i].BlockLength & 0xFF);
}
return hdr;
}
public static byte[] EncodeMode10(DecodedMode mode, PeripheralDeviceTypes deviceType)
{
int modeSize = 0;
if(mode.Pages != null)
modeSize += mode.Pages.Sum(page => page.PageResponse.Length);
byte[] hdr = EncodeModeHeader10(mode.Header, deviceType);
modeSize += hdr.Length;
byte[] md = new byte[modeSize];
Array.Copy(hdr, 0, md, 0, hdr.Length);
if(mode.Pages == null)
return md;
{
int offset = hdr.Length;
foreach(ModePage page in mode.Pages)
{
Array.Copy(page.PageResponse, 0, md, offset, page.PageResponse.Length);
offset += page.PageResponse.Length;
}
}
return md;
}
}