// /***************************************************************************
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename       : Extent.cs
// Author(s)      : Natalia Portillo <claunia@claunia.com>
//
// Component      : IBM JFS filesystem plugin
//
// --[ License ] --------------------------------------------------------------
//
//     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
//     License, or (at your option) any later version.
//
//     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.
//
//     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/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2011-2026 Natalia Portillo
// ****************************************************************************/

namespace Aaru.Filesystems;

/// <inheritdoc />
/// <summary>Implements detection of IBM's Journaled File System</summary>
public sealed partial class JFS
{
    /// <summary>Extracts the 40-bit address from a physical extent descriptor (pxd_t)</summary>
    static ulong ExtentAddress(Extent pxd)
    {
        ulong n = pxd.len_addr & ~0xFFFFFFu;

        return (n << 8) + pxd.addr2;
    }

    /// <summary>Extracts the 24-bit length from a physical extent descriptor (pxd_t)</summary>
    static uint ExtentLength(Extent pxd) => pxd.len_addr & 0xFFFFFF;

    /// <summary>Extracts the 40-bit address from raw pxd_t fields</summary>
    /// <param name="lenAddr">The len_addr field (lower 24 bits = length, upper 8 bits = high address byte)</param>
    /// <param name="addr2">The addr2 field (lower 32 bits of address)</param>
    /// <returns>The 40-bit block address</returns>
    static long PxdAddress(uint lenAddr, uint addr2)
    {
        ulong n = lenAddr & ~0xFFFFFFu;

        return (long)((n << 8) + addr2);
    }
}