Skip to content

Commit b908732

Browse files
committed
Fix logical block extension
1 parent 4ee4a6e commit b908732

File tree

1 file changed

+31
-17
lines changed

1 file changed

+31
-17
lines changed

SabreTools.Serialization/Extensions/ISO9660.cs

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using SabreTools.Data.Models.ISO9660;
3+
using SabreTools.Numerics;
34

45
namespace SabreTools.Data.Extensions
56
{
@@ -8,36 +9,49 @@ public static class ISO9660
89
/// <summary>
910
/// Get the logical block size from a sector length
1011
/// </summary>
11-
/// <param name="bvd">Volume descriptor containing block information</param>
12+
/// <param name="vd">Volume descriptor containing block information</param>
1213
/// <param name="sectorLength">Defined sector length</param>
1314
/// <returns>Size of a logical block</returns>
14-
public static short GetLogicalBlockSize(this BaseVolumeDescriptor bvd, short sectorLength)
15+
public static short GetLogicalBlockSize(this VolumeDescriptor vd, short sectorLength)
1516
{
16-
short blockLength = sectorLength;
17-
if (bvd.LogicalBlockSize.IsValid)
18-
{
19-
// Validate logical block length
20-
if (bvd.LogicalBlockSize >= 512 && bvd.LogicalBlockSize <= sectorLength && (bvd.LogicalBlockSize & (bvd.LogicalBlockSize - 1)) == 0)
21-
blockLength = bvd.LogicalBlockSize;
22-
}
17+
BothInt16 blockLength;
18+
if (vd is PrimaryVolumeDescriptor pvd)
19+
blockLength = pvd.LogicalBlockSize;
20+
else if (vd is SupplementaryVolumeDescriptor svd)
21+
blockLength = svd.LogicalBlockSize;
2322
else
24-
{
25-
// If logical block size is ambiguous check if only one is valid, otherwise default to sector length
26-
short le = bvd.LogicalBlockSize.LittleEndian;
27-
bool leValid = le >= 512 && le <= sectorLength && (le & (le - 1)) == 0;
23+
return sectorLength;
2824

29-
short be = bvd.LogicalBlockSize.LittleEndian;
30-
bool beValid = be >= 512 && be <= sectorLength && (be & (be - 1)) == 0;
25+
// If the block size is inconsistent
26+
if (!blockLength.IsValid)
27+
{
28+
bool leValid = BlockLengthValid(blockLength.LittleEndian, sectorLength);
29+
bool beValid = BlockLengthValid(blockLength.BigEndian, sectorLength);
3130

3231
if (leValid && !beValid)
33-
blockLength = le;
32+
blockLength = blockLength.LittleEndian;
3433
else if (beValid && !leValid)
35-
blockLength = be;
34+
blockLength = blockLength.BigEndian;
35+
else
36+
return sectorLength;
3637
}
3738

39+
// Validate logical block length
40+
if (!BlockLengthValid(blockLength, sectorLength))
41+
blockLength = sectorLength;
42+
3843
return blockLength;
3944
}
4045

46+
/// <summary>
47+
/// Indicates if a block length is valid
48+
/// </summary>
49+
/// <param name="blockLength">Block length to check</param>
50+
/// <param name="sectorLength">Defined sector length</param>
51+
/// <returns>True if the block length is valid, false otherwise</returns>
52+
private static bool BlockLengthValid(short blockLength, short sectorLength)
53+
=> blockLength >= 512 && blockLength <= sectorLength && (blockLength & (blockLength - 1)) == 0;
54+
4155
/// <summary>
4256
/// Indicates if an array contains all ASCII numeric digits
4357
/// </summary>

0 commit comments

Comments
 (0)