2016-01-15 07:00:43 +00:00
// /***************************************************************************
// The Disc Image Chef
// ----------------------------------------------------------------------------
//
// Filename : BlockLimits.cs
2016-07-28 18:13:49 +01:00
// Author(s) : Natalia Portillo <claunia@claunia.com>
2016-01-15 07:00:43 +00:00
//
2016-07-28 18:13:49 +01:00
// Component : Device structures decoders.
2016-01-15 07:00:43 +00:00
//
// --[ Description ] ----------------------------------------------------------
//
2016-07-28 18:13:49 +01:00
// Decodes SCSI's SSC block limits structures.
2016-01-15 07:00:43 +00:00
//
// --[ License ] --------------------------------------------------------------
//
2016-07-28 18:13:49 +01:00
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; either version 2.1 of the
2016-01-15 07:00:43 +00:00
// License, or (at your option) any later version.
//
2016-07-28 18:13:49 +01:00
// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
2016-01-15 07:00:43 +00:00
//
2016-07-28 18:13:49 +01:00
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, see <http://www.gnu.org/licenses/>.
2016-01-15 07:00:43 +00:00
//
// ----------------------------------------------------------------------------
2016-07-28 18:13:49 +01:00
// Copyright © 2011-2016 Natalia Portillo
2016-01-15 07:00:43 +00:00
// ****************************************************************************/
2016-07-28 18:13:49 +01:00
2016-01-15 07:00:43 +00:00
using System ;
using System.Text ;
namespace DiscImageChef.Decoders.SCSI.SSC
{
public static class BlockLimits
{
public struct BlockLimitsData
{
/// <summary>
/// All blocks size must be multiple of 2^<cref name="granularity"/>
/// </summary>
public byte granularity ;
/// <summary>
/// Maximum block length in bytes
/// </summary>
public uint maxBlockLen ;
/// <summary>
/// Minimum block length in bytes
/// </summary>
public ushort minBlockLen ;
}
public static BlockLimitsData ? Decode ( byte [ ] response )
{
2016-04-19 02:11:47 +01:00
if ( response = = null )
2016-01-15 07:00:43 +00:00
return null ;
2016-04-19 02:11:47 +01:00
if ( response . Length ! = 6 )
2016-01-15 07:00:43 +00:00
return null ;
BlockLimitsData dec = new BlockLimitsData ( ) ;
dec . granularity = ( byte ) ( response [ 0 ] & 0x1F ) ;
dec . maxBlockLen = ( uint ) ( ( response [ 1 ] < < 16 ) + ( response [ 2 ] < < 8 ) + response [ 3 ] ) ;
dec . minBlockLen = ( ushort ) ( ( response [ 4 ] < < 8 ) + response [ 5 ] ) ;
return dec ;
}
public static string Prettify ( BlockLimitsData ? decoded )
{
2016-04-19 02:11:47 +01:00
if ( decoded = = null )
2016-01-15 07:00:43 +00:00
return null ;
StringBuilder sb = new StringBuilder ( ) ;
2016-04-19 02:11:47 +01:00
if ( decoded . Value . maxBlockLen = = decoded . Value . minBlockLen )
2016-01-15 07:00:43 +00:00
sb . AppendFormat ( "Device's block size is fixed at {0} bytes" , decoded . Value . minBlockLen ) . AppendLine ( ) ;
else
{
2016-04-19 02:11:47 +01:00
if ( decoded . Value . maxBlockLen > 0 )
2016-01-15 07:00:43 +00:00
sb . AppendFormat ( "Device's maximum block size is {0} bytes" , decoded . Value . maxBlockLen ) . AppendLine ( ) ;
else
sb . AppendLine ( "Device does not specify a maximum block size" ) ;
sb . AppendFormat ( "Device's minimum block size is {0} bytes" , decoded . Value . minBlockLen ) . AppendLine ( ) ;
2016-04-19 02:11:47 +01:00
if ( decoded . Value . granularity > 0 )
2016-01-15 07:00:43 +00:00
sb . AppendFormat ( "Device's needs a block size granularity of 2^{0} ({1}) bytes" , decoded . Value . granularity , Math . Pow ( 2 , ( double ) decoded . Value . granularity ) ) . AppendLine ( ) ;
}
return sb . ToString ( ) ;
}
public static string Prettify ( byte [ ] response )
{
return Prettify ( Decode ( response ) ) ;
}
}
}