Port some extensions from BOS

This omits anything that relies on caching, at the moment. There is no easy way of creating a cache for the required pieces in the static context of the extensions.
This commit is contained in:
Matt Nadareski
2023-09-15 22:46:06 -04:00
parent 56066877b1
commit 608edeb630
2 changed files with 69 additions and 0 deletions

36
Extensions.CFB.cs Normal file
View File

@@ -0,0 +1,36 @@
using System;
using SabreTools.Models.CFB;
namespace SabreTools.Serialization
{
public static partial class Extensions
{
/// <summary>
/// Sector size in bytes
/// </summary>
/// <param name="binary">Binary model to derive from</param>
/// <returns>Sector size on success, -1 on error</returns>
public static long SectorSize(this Binary binary)
{
// If we have an invalid header
if (binary.Header?.SectorShift == null)
return -1;
return (long)Math.Pow(2, binary.Header.SectorShift);
}
/// <summary>
/// Mini sector size in bytes
/// </summary>
/// <param name="binary">Binary model to derive from</param>
/// <returns>Sector size on success, -1 on error</returns>
public static long MiniSectorSize(this Binary binary)
{
// If we have an invalid header
if (binary.Header?.SectorShift == null)
return -1;
return (long)Math.Pow(2, binary.Header.MiniSectorShift);
}
}
}

View File

@@ -0,0 +1,33 @@
using SabreTools.Models.InstallShieldCabinet;
namespace SabreTools.Serialization
{
public static partial class Extensions
{
/// <summary>
/// The major version of the cabinet
/// </summary>
/// <param name="cabinet">Cabinet model to derive from</param>
/// <returns>Major version on success, -1 on error</returns>
public static int MajorVersion(this Cabinet cabinet)
{
// If we have an invalid header
if (cabinet.CommonHeader?.Version == null)
return -1;
uint majorVersion = cabinet.CommonHeader.Version;
if (majorVersion >> 24 == 1)
{
majorVersion = (majorVersion >> 12) & 0x0F;
}
else if (majorVersion >> 24 == 2 || majorVersion >> 24 == 4)
{
majorVersion = majorVersion & 0xFFFF;
if (majorVersion != 0)
majorVersion /= 100;
}
return (int)majorVersion;
}
}
}