// /***************************************************************************
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename       : Sun.cs
// Author(s)      : Natalia Portillo <claunia@claunia.com>
//
// Component      : Partitioning scheme plugins.
//
// --[ Description ] ----------------------------------------------------------
//
//     Manages Sun disklabels.
//
// --[ 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
// ****************************************************************************/

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
using System.Text;
using Aaru.CommonTypes;
using Aaru.CommonTypes.Attributes;
using Aaru.CommonTypes.Enums;
using Aaru.CommonTypes.Interfaces;
using Aaru.Helpers;
using Aaru.Logging;
using Marshal = Aaru.Helpers.Marshal;

namespace Aaru.Partitions;

/// <inheritdoc />
/// <summary>Implements decoding of Sun disklabels</summary>
[SuppressMessage("ReSharper", "InconsistentNaming")]
public sealed partial class SunDisklabel : IPartition
{
    /// <summary>Sun disklabel magic number</summary>
    const ushort DKL_MAGIC = 0xDABE;
    /// <summary>Sun VTOC magic number</summary>
    const uint VTOC_SANE = 0x600DDEEE;
    /// <summary>Sun disklabel magic number, byte-swapped</summary>
    const ushort DKL_CIGAM = 0xBEDA;
    /// <summary>Sun VTOC magic number, byte-swapped</summary>
    const uint VTOC_ENAS = 0xEEDE0D60;
    /// <summary># of logical partitions</summary>
    const int NDKMAP = 8;
    /// <summary># of logical partitions</summary>
    const int NDKMAP16 = 16;
    /// <summary>Disk label size</summary>
    const int DK_LABEL_SIZE = 512;
    /// <summary>Volume label size</summary>
    const int LEN_DKL_ASCII = 128;
    /// <summary>Length of v_volume</summary>
    const int LEN_DKL_VVOL = 8;
    /// <summary>Size of padding in SunOS disk label</summary>
    const int LEN_DKL_PAD = DK_LABEL_SIZE - (LEN_DKL_ASCII + NDKMAP * 8 + 14 * 2);
    /// <summary>Size of padding in Solaris disk label with 8 partitions</summary>
    const int LEN_DKL_PAD8 = DK_LABEL_SIZE -
                             (LEN_DKL_ASCII +
                              136           + // sizeof(dk_vtoc8)
                              NDKMAP * 8    +
                              14     * 2    +
                              2      * 2);
    const int LEN_DKL_PAD16 = DK_LABEL_SIZE -
                              (456    + // sizeof(dk_vtoc16)
                               4  * 4 +
                               12 * 2 +
                               2  * 2);
    const string MODULE_NAME = "Sun partitions plugin";

#region IPartition Members

    /// <inheritdoc />
    public string Name => Localization.SunDisklabel_Name;

    /// <inheritdoc />
    public Guid Id => new("50F35CC4-8375-4445-8DCB-1BA550C931A3");

    /// <inheritdoc />
    public string Author => Authors.NATALIA_PORTILLO;

    /// <inheritdoc />
    public bool GetInformation(IMediaImage imagePlugin, out List<Partition> partitions, ulong sectorOffset)
    {
        partitions = [];

        if(imagePlugin.Info.SectorSize < 512) return false;

        if(sectorOffset + 2 >= imagePlugin.Info.Sectors) return false;

        bool useDkl = false, useDkl8 = false, useDkl16 = false;

        ErrorNumber errno = imagePlugin.ReadSector(sectorOffset, false, out byte[] sunSector, out _);

        if(errno != ErrorNumber.NoError) return false;

        dk_label   dkl   = Marshal.ByteArrayToStructureLittleEndian<dk_label>(sunSector);
        dk_label8  dkl8  = Marshal.ByteArrayToStructureLittleEndian<dk_label8>(sunSector);
        dk_label16 dkl16 = Marshal.ByteArrayToStructureLittleEndian<dk_label16>(sunSector);

        AaruLogging.Debug(MODULE_NAME, "dkl.dkl_magic = 0x{0:X4}",           dkl.dkl_magic);
        AaruLogging.Debug(MODULE_NAME, "dkl8.dkl_vtoc.v_sanity = 0x{0:X8}",  dkl8.dkl_vtoc.v_sanity);
        AaruLogging.Debug(MODULE_NAME, "dkl16.dkl_vtoc.v_sanity = 0x{0:X8}", dkl16.dkl_vtoc.v_sanity);

        if(dkl.dkl_magic is DKL_MAGIC or DKL_CIGAM)
        {
            if(dkl16.dkl_vtoc.v_sanity is VTOC_SANE or VTOC_ENAS)
                useDkl16 = true;
            else if(dkl8.dkl_vtoc.v_sanity is VTOC_SANE or VTOC_ENAS)
                useDkl8 = true;
            else
                useDkl = true;
        }

        if(!useDkl && !useDkl8 && !useDkl16)
        {
            errno = imagePlugin.ReadSector(sectorOffset + 1, false, out sunSector, out _);

            if(errno == ErrorNumber.NoError)
            {
                dkl   = Marshal.ByteArrayToStructureLittleEndian<dk_label>(sunSector);
                dkl8  = Marshal.ByteArrayToStructureLittleEndian<dk_label8>(sunSector);
                dkl16 = Marshal.ByteArrayToStructureLittleEndian<dk_label16>(sunSector);

                if(dkl.dkl_magic is DKL_MAGIC or DKL_CIGAM)
                {
                    if(dkl16.dkl_vtoc.v_sanity is VTOC_SANE or VTOC_ENAS)
                        useDkl16 = true;
                    else if(dkl8.dkl_vtoc.v_sanity is VTOC_SANE or VTOC_ENAS)
                        useDkl8 = true;
                    else
                        useDkl = true;
                }
            }
        }

        if(!useDkl && !useDkl8 && !useDkl16) return false;

        if(useDkl16 && dkl16.dkl_magic == DKL_CIGAM)
        {
            AaruLogging.Debug(MODULE_NAME, Localization.Swapping_dk_label16);
            dkl16 = dkl16.SwapEndian();
        }
        else if(useDkl8 && dkl8.dkl_magic == DKL_CIGAM)
        {
            AaruLogging.Debug(MODULE_NAME, Localization.Swapping_dk_label8);
            dkl8 = dkl8.SwapEndian();
        }
        else if(useDkl && dkl.dkl_magic == DKL_CIGAM)
        {
            AaruLogging.Debug(MODULE_NAME, Localization.Swapping_dk_label);
            dkl = dkl.SwapEndian();
        }

        if(useDkl)
        {
            var sectorsPerCylinder = (ulong)(dkl.dkl_nsect * dkl.dkl_nhead);

            AaruLogging.Debug(MODULE_NAME,
                              "dkl.dkl_asciilabel = \"{0}\"",
                              StringHandlers.CToString(dkl.dkl_asciilabel));

            AaruLogging.Debug(MODULE_NAME, "dkl.dkl_rpm = {0}",    dkl.dkl_rpm);
            AaruLogging.Debug(MODULE_NAME, "dkl.dkl_pcyl = {0}",   dkl.dkl_pcyl);
            AaruLogging.Debug(MODULE_NAME, "dkl.dkl_apc = {0}",    dkl.dkl_apc);
            AaruLogging.Debug(MODULE_NAME, "dkl.dkl_gap1 = {0}",   dkl.dkl_gap1);
            AaruLogging.Debug(MODULE_NAME, "dkl.dkl_gap2 = {0}",   dkl.dkl_gap2);
            AaruLogging.Debug(MODULE_NAME, "dkl.dkl_intrlv = {0}", dkl.dkl_intrlv);
            AaruLogging.Debug(MODULE_NAME, "dkl.dkl_ncyl = {0}",   dkl.dkl_ncyl);
            AaruLogging.Debug(MODULE_NAME, "dkl.dkl_acyl = {0}",   dkl.dkl_acyl);
            AaruLogging.Debug(MODULE_NAME, "dkl.dkl_nhead = {0}",  dkl.dkl_nhead);
            AaruLogging.Debug(MODULE_NAME, "dkl.dkl_nsect = {0}",  dkl.dkl_nsect);
            AaruLogging.Debug(MODULE_NAME, "dkl.dkl_bhead = {0}",  dkl.dkl_bhead);
            AaruLogging.Debug(MODULE_NAME, "dkl.dkl_ppart = {0}",  dkl.dkl_ppart);

            for(var i = 0; i < NDKMAP; i++)
            {
                AaruLogging.Debug(MODULE_NAME, "dkl.dkl_map[{0}].dkl_cylno = {1}", i, dkl.dkl_map[i].dkl_cylno);

                AaruLogging.Debug(MODULE_NAME, "dkl.dkl_map[{0}].dkl_nblk = {1}", i, dkl.dkl_map[i].dkl_nblk);
            }

            AaruLogging.Debug(MODULE_NAME, "dkl.dkl_magic = 0x{0:X4}", dkl.dkl_magic);
            AaruLogging.Debug(MODULE_NAME, "dkl.dkl_cksum = 0x{0:X4}", dkl.dkl_cksum);
            AaruLogging.Debug(MODULE_NAME, "sectorsPerCylinder = {0}", sectorsPerCylinder);

            for(var i = 0; i < NDKMAP; i++)
            {
                if(dkl.dkl_map[i].dkl_cylno <= 0 || dkl.dkl_map[i].dkl_nblk <= 0) continue;

                var part = new Partition
                {
                    Size     = (ulong)dkl.dkl_map[i].dkl_nblk * DK_LABEL_SIZE,
                    Length   = (ulong)(dkl.dkl_map[i].dkl_nblk * DK_LABEL_SIZE / imagePlugin.Info.SectorSize),
                    Sequence = (ulong)i,
                    Offset   = ((ulong)dkl.dkl_map[i].dkl_cylno * sectorsPerCylinder + sectorOffset) * DK_LABEL_SIZE,
                    Start = ((ulong)dkl.dkl_map[i].dkl_cylno * sectorsPerCylinder + sectorOffset) *
                            DK_LABEL_SIZE /
                            imagePlugin.Info.SectorSize,
                    Type   = Localization.SunOS_partition,
                    Scheme = Name
                };

                if(part.Start < imagePlugin.Info.Sectors && part.End <= imagePlugin.Info.Sectors) partitions.Add(part);
            }
        }
        else if(useDkl8)
        {
            var sectorsPerCylinder = (ulong)(dkl8.dkl_nsect * dkl8.dkl_nhead);

            AaruLogging.Debug(MODULE_NAME,
                              "dkl8.dkl_asciilabel = \"{0}\"",
                              StringHandlers.CToString(dkl8.dkl_asciilabel));

            AaruLogging.Debug(MODULE_NAME, "dkl8.dkl_vtoc.v_version = {0}", dkl8.dkl_vtoc.v_version);

            AaruLogging.Debug(MODULE_NAME,
                              "dkl8.dkl_vtoc.v_volume = \"{0}\"",
                              StringHandlers.CToString(dkl8.dkl_vtoc.v_volume));

            AaruLogging.Debug(MODULE_NAME, "dkl8.dkl_vtoc.v_nparts = {0}",      dkl8.dkl_vtoc.v_nparts);
            AaruLogging.Debug(MODULE_NAME, "dkl8.dkl_vtoc.v_sanity = 0x{0:X8}", dkl8.dkl_vtoc.v_sanity);
            AaruLogging.Debug(MODULE_NAME, "dkl8.dkl_write_reinstruct = {0}",   dkl8.dkl_write_reinstruct);
            AaruLogging.Debug(MODULE_NAME, "dkl8.dkl_read_reinstruct = {0}",    dkl8.dkl_read_reinstruct);
            AaruLogging.Debug(MODULE_NAME, "dkl8.dkl_rpm = {0}",                dkl8.dkl_rpm);
            AaruLogging.Debug(MODULE_NAME, "dkl8.dkl_pcyl = {0}",               dkl8.dkl_pcyl);
            AaruLogging.Debug(MODULE_NAME, "dkl8.dkl_apc = {0}",                dkl8.dkl_apc);
            AaruLogging.Debug(MODULE_NAME, "dkl8.dkl_obs1 = {0}",               dkl8.dkl_obs1);
            AaruLogging.Debug(MODULE_NAME, "dkl8.dkl_obs2 = {0}",               dkl8.dkl_obs2);
            AaruLogging.Debug(MODULE_NAME, "dkl8.dkl_intrlv = {0}",             dkl8.dkl_intrlv);
            AaruLogging.Debug(MODULE_NAME, "dkl8.dkl_ncyl = {0}",               dkl8.dkl_ncyl);
            AaruLogging.Debug(MODULE_NAME, "dkl8.dkl_acyl = {0}",               dkl8.dkl_acyl);
            AaruLogging.Debug(MODULE_NAME, "dkl8.dkl_nhead = {0}",              dkl8.dkl_nhead);
            AaruLogging.Debug(MODULE_NAME, "dkl8.dkl_nsect = {0}",              dkl8.dkl_nsect);
            AaruLogging.Debug(MODULE_NAME, "dkl8.dkl_obs3 = {0}",               dkl8.dkl_obs3);
            AaruLogging.Debug(MODULE_NAME, "dkl8.dkl_obs4 = {0}",               dkl8.dkl_obs4);

            for(var i = 0; i < NDKMAP; i++)
            {
                AaruLogging.Debug(MODULE_NAME, "dkl8.dkl_map[{0}].dkl_cylno = {1}", i, dkl8.dkl_map[i].dkl_cylno);

                AaruLogging.Debug(MODULE_NAME, "dkl8.dkl_map[{0}].dkl_nblk = {1}", i, dkl8.dkl_map[i].dkl_nblk);

                AaruLogging.Debug(MODULE_NAME,
                                  "dkl8.dkl_vtoc.v_part[{0}].p_tag = {1} ({2})",
                                  i,
                                  dkl8.dkl_vtoc.v_part[i].p_tag,
                                  (ushort)dkl8.dkl_vtoc.v_part[i].p_tag);

                AaruLogging.Debug(MODULE_NAME,
                                  "dkl8.dkl_vtoc.v_part[{0}].p_flag = {1} ({2})",
                                  i,
                                  dkl8.dkl_vtoc.v_part[i].p_flag,
                                  (ushort)dkl8.dkl_vtoc.v_part[i].p_flag);

                AaruLogging.Debug(MODULE_NAME,
                                  "dkl8.dkl_vtoc.v_timestamp[{0}] = {1}",
                                  i,
                                  DateHandlers.UnixToDateTime(dkl8.dkl_vtoc.v_timestamp[i]));
            }

            AaruLogging.Debug(MODULE_NAME, "dkl8.dkl_magic = 0x{0:X4}", dkl8.dkl_magic);
            AaruLogging.Debug(MODULE_NAME, "dkl8.dkl_cksum = 0x{0:X4}", dkl8.dkl_cksum);
            AaruLogging.Debug(MODULE_NAME, "sectorsPerCylinder = {0}",  sectorsPerCylinder);

            if(dkl8.dkl_vtoc.v_nparts > NDKMAP) return false;

            for(var i = 0; i < dkl8.dkl_vtoc.v_nparts; i++)
            {
                if(dkl8.dkl_map[i].dkl_nblk      <= 0               ||
                   dkl8.dkl_vtoc.v_part[i].p_tag == SunTag.SunEmpty ||
                   dkl8.dkl_vtoc.v_part[i].p_tag == SunTag.SunWholeDisk)
                    continue;

                var part = new Partition
                {
                    Description = SunFlagsToString(dkl8.dkl_vtoc.v_part[i].p_flag),
                    Size = (ulong)dkl8.dkl_map[i].dkl_nblk * DK_LABEL_SIZE,
                    Length = (ulong)(dkl8.dkl_map[i].dkl_nblk * DK_LABEL_SIZE / imagePlugin.Info.SectorSize),
                    Sequence = (ulong)i,
                    Offset = ((ulong)dkl8.dkl_map[i].dkl_cylno * sectorsPerCylinder + sectorOffset) * DK_LABEL_SIZE,
                    Start = ((ulong)dkl8.dkl_map[i].dkl_cylno * sectorsPerCylinder + sectorOffset) *
                            DK_LABEL_SIZE /
                            imagePlugin.Info.SectorSize,
                    Type   = SunIdToString(dkl8.dkl_vtoc.v_part[i].p_tag),
                    Scheme = Name
                };

                if(dkl8.dkl_vtoc.v_timestamp[i] != 0)
                {
                    part.Description +=
                        "\n" +
                        string.Format(Localization.Partition_timestamped_on_0,
                                      DateHandlers.UnixToDateTime(dkl8.dkl_vtoc.v_timestamp[i]));
                }

                if(part.Start < imagePlugin.Info.Sectors && part.End <= imagePlugin.Info.Sectors) partitions.Add(part);
            }
        }
        else
        {
            AaruLogging.Debug(MODULE_NAME, "dkl16.dkl_vtoc.v_sanity = 0x{0:X8}", dkl16.dkl_vtoc.v_sanity);
            AaruLogging.Debug(MODULE_NAME, "dkl16.dkl_vtoc.v_version = {0}",     dkl16.dkl_vtoc.v_version);

            AaruLogging.Debug(MODULE_NAME,
                              "dkl16.dkl_vtoc.v_volume = \"{0}\"",
                              StringHandlers.CToString(dkl16.dkl_vtoc.v_volume));

            AaruLogging.Debug(MODULE_NAME, "dkl16.dkl_vtoc.v_sectorsz = {0}", dkl16.dkl_vtoc.v_sectorsz);
            AaruLogging.Debug(MODULE_NAME, "dkl16.dkl_vtoc.v_nparts = {0}",   dkl16.dkl_vtoc.v_nparts);

            AaruLogging.Debug(MODULE_NAME,
                              "dkl16.dkl_vtoc.v_asciilabel = \"{0}\"",
                              StringHandlers.CToString(dkl16.dkl_vtoc.v_asciilabel));

            AaruLogging.Debug(MODULE_NAME, "dkl16.dkl_pcyl = {0}",   dkl16.dkl_pcyl);
            AaruLogging.Debug(MODULE_NAME, "dkl16.dkl_ncyl = {0}",   dkl16.dkl_ncyl);
            AaruLogging.Debug(MODULE_NAME, "dkl16.dkl_acyl = {0}",   dkl16.dkl_acyl);
            AaruLogging.Debug(MODULE_NAME, "dkl16.dkl_bcyl = {0}",   dkl16.dkl_bcyl);
            AaruLogging.Debug(MODULE_NAME, "dkl16.dkl_nhead = {0}",  dkl16.dkl_nhead);
            AaruLogging.Debug(MODULE_NAME, "dkl16.dkl_nsect = {0}",  dkl16.dkl_nsect);
            AaruLogging.Debug(MODULE_NAME, "dkl16.dkl_intrlv = {0}", dkl16.dkl_intrlv);
            AaruLogging.Debug(MODULE_NAME, "dkl16.dkl_skew = {0}",   dkl16.dkl_skew);
            AaruLogging.Debug(MODULE_NAME, "dkl16.dkl_apc = {0}",    dkl16.dkl_apc);
            AaruLogging.Debug(MODULE_NAME, "dkl16.dkl_rpm = {0}",    dkl16.dkl_rpm);

            AaruLogging.Debug(MODULE_NAME, "dkl16.dkl_write_reinstruct = {0}", dkl16.dkl_write_reinstruct);

            AaruLogging.Debug(MODULE_NAME, "dkl16.dkl_read_reinstruct = {0}", dkl16.dkl_read_reinstruct);

            for(var i = 0; i < NDKMAP16; i++)
            {
                AaruLogging.Debug(MODULE_NAME,
                                  "dkl16.dkl_vtoc.v_part[{0}].p_start = {1}",
                                  i,
                                  dkl16.dkl_vtoc.v_part[i].p_start);

                AaruLogging.Debug(MODULE_NAME,
                                  "dkl16.dkl_vtoc.v_part[{0}].p_size = {1}",
                                  i,
                                  dkl16.dkl_vtoc.v_part[i].p_size);

                AaruLogging.Debug(MODULE_NAME,
                                  "dkl16.dkl_vtoc.v_part[{0}].p_tag = {1} ({2})",
                                  i,
                                  dkl16.dkl_vtoc.v_part[i].p_tag,
                                  (ushort)dkl16.dkl_vtoc.v_part[i].p_tag);

                AaruLogging.Debug(MODULE_NAME,
                                  "dkl16.dkl_vtoc.v_part[{0}].p_flag = {1} ({2})",
                                  i,
                                  dkl16.dkl_vtoc.v_part[i].p_flag,
                                  (ushort)dkl16.dkl_vtoc.v_part[i].p_flag);

                AaruLogging.Debug(MODULE_NAME,
                                  "dkl16.dkl_vtoc.v_timestamp[{0}] = {1}",
                                  i,
                                  DateHandlers.UnixToDateTime(dkl16.dkl_vtoc.v_timestamp[i]));
            }

            AaruLogging.Debug(MODULE_NAME, "dkl16.dkl_magic = 0x{0:X4}", dkl16.dkl_magic);
            AaruLogging.Debug(MODULE_NAME, "dkl16.dkl_cksum = 0x{0:X4}", dkl16.dkl_cksum);

            if(dkl16.dkl_vtoc.v_nparts > NDKMAP16) return false;

            for(var i = 0; i < dkl16.dkl_vtoc.v_nparts; i++)
            {
                if(dkl16.dkl_vtoc.v_part[i].p_size <= 0               ||
                   dkl16.dkl_vtoc.v_part[i].p_tag  == SunTag.SunEmpty ||
                   dkl16.dkl_vtoc.v_part[i].p_tag  == SunTag.SunWholeDisk)
                    continue;

                var part = new Partition
                {
                    Description = SunFlagsToString(dkl16.dkl_vtoc.v_part[i].p_flag),
                    Size        = (ulong)dkl16.dkl_vtoc.v_part[i].p_size * dkl16.dkl_vtoc.v_sectorsz,
                    Length =
                        (ulong)(dkl16.dkl_vtoc.v_part[i].p_size *
                                dkl16.dkl_vtoc.v_sectorsz /
                                imagePlugin.Info.SectorSize),
                    Sequence = (ulong)i,
                    Offset   = ((ulong)dkl16.dkl_vtoc.v_part[i].p_start + sectorOffset) * dkl16.dkl_vtoc.v_sectorsz,
                    Start = ((ulong)dkl16.dkl_vtoc.v_part[i].p_start + sectorOffset) *
                            dkl16.dkl_vtoc.v_sectorsz /
                            imagePlugin.Info.SectorSize,
                    Type   = SunIdToString(dkl16.dkl_vtoc.v_part[i].p_tag),
                    Scheme = Name
                };

                if(dkl16.dkl_vtoc.v_timestamp[i] != 0)
                {
                    part.Description +=
                        "\n" +
                        string.Format(Localization.Partition_timestamped_on_0,
                                      DateHandlers.UnixToDateTime(dkl16.dkl_vtoc.v_timestamp[i]));
                }

                if(part.Start < imagePlugin.Info.Sectors && part.End <= imagePlugin.Info.Sectors) partitions.Add(part);
            }
        }

        return partitions.Count > 0;
    }

#endregion

    static string SunFlagsToString(SunFlags flags)
    {
        var sb = new StringBuilder();

        if(flags.HasFlag(SunFlags.NoMount)) sb.AppendLine(Localization.Unmountable);

        if(flags.HasFlag(SunFlags.ReadOnly)) sb.AppendLine(Localization.Read_only);

        return sb.ToString();
    }

    static string SunIdToString(SunTag id) => id switch
                                              {
                                                  SunTag.Linux          => Localization.Linux,
                                                  SunTag.LinuxRaid      => Localization.Linux_RAID,
                                                  SunTag.LinuxSwap      => Localization.Linux_swap,
                                                  SunTag.LVM            => Localization.LVM,
                                                  SunTag.SunBoot        => Localization.Sun_boot,
                                                  SunTag.SunEmpty       => Localization.Empty,
                                                  SunTag.SunHome        => Localization.Sun_home,
                                                  SunTag.SunRoot        => Localization.Sun_root,
                                                  SunTag.SunStand       => Localization.Sun_stand,
                                                  SunTag.SunSwap        => Localization.Sun_swap,
                                                  SunTag.SunUsr         => Localization.Sun_usr,
                                                  SunTag.SunVar         => Localization.Sun_var,
                                                  SunTag.SunWholeDisk   => Localization.Whole_disk,
                                                  SunTag.SunAlt         => Localization.Replacement_sectors,
                                                  SunTag.SunCache       => Localization.Sun_cachefs,
                                                  SunTag.SunReserved    => Localization.Reserved_for_SMI,
                                                  SunTag.VxVmPublic     => Localization.Veritas_public,
                                                  SunTag.VxVmPrivate    => Localization.Veritas_private,
                                                  SunTag.NetBSD         => Localization.NetBSD,
                                                  SunTag.FreeBSD_Swap   => Localization.FreeBSD_swap,
                                                  SunTag.FreeBSD_UFS    => Localization.FreeBSD,
                                                  SunTag.FreeBSD_Vinum  => Localization.Vinum,
                                                  SunTag.FreeBSD_ZFS    => Localization.FreeBSD_ZFS,
                                                  SunTag.FreeBSD_NANDFS => Localization.FreeBSD_nandfs,
                                                  _                     => Localization.Unknown_partition_type
                                              };

#region Nested type: dk_label

    /// <summary>SunOS disk label</summary>
    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    [SwapEndian]
    partial struct dk_label
    {
        /// <summary>Informative string</summary>
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = LEN_DKL_ASCII)]
        public byte[] dkl_asciilabel;
        /// <summary>Padding</summary>
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = LEN_DKL_PAD)]
        public byte[] dkl_pad;
        /// <summary>rotations per minute</summary>
        public ushort dkl_rpm;
        /// <summary># physical cylinders</summary>
        public ushort dkl_pcyl;
        /// <summary>alternates per cylinder</summary>
        public ushort dkl_apc;
        /// <summary>size of gap 1</summary>
        public ushort dkl_gap1;
        /// <summary>size of gap 2</summary>
        public ushort dkl_gap2;
        /// <summary>interleave factor</summary>
        public ushort dkl_intrlv;
        /// <summary># of data cylinders</summary>
        public ushort dkl_ncyl;
        /// <summary># of alternate cylinders</summary>
        public ushort dkl_acyl;
        /// <summary># of heads in this partition</summary>
        public ushort dkl_nhead;
        /// <summary># of 512 byte sectors per track</summary>
        public ushort dkl_nsect;
        /// <summary>identifies proper label location</summary>
        public ushort dkl_bhead;
        /// <summary>physical partition #</summary>
        public ushort dkl_ppart;
        /// <summary>Logical partitions</summary>
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = NDKMAP)]
        public dk_map[] dkl_map;
        /// <summary>identifies this label format</summary>
        public ushort dkl_magic;
        /// <summary>xor checksum of sector</summary>
        public ushort dkl_cksum;
    }

#endregion

#region Nested type: dk_label16

    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    [SwapEndian]
    partial struct dk_label16
    {
        /// <summary>vtoc inclusions from AT&amp;T SVr4</summary>
        public dk_vtoc16 dkl_vtoc;
        /// <summary># of physical cylinders</summary>
        public uint dkl_pcyl;
        /// <summary># of data cylinders</summary>
        public uint dkl_ncyl;
        /// <summary># of alternate cylinders</summary>
        public ushort dkl_acyl;
        /// <summary>cyl offset (for fixed head area)</summary>
        public ushort dkl_bcyl;
        /// <summary># of heads</summary>
        public uint dkl_nhead;
        /// <summary># of data sectors per track</summary>
        public uint dkl_nsect;
        /// <summary>interleave factor</summary>
        public ushort dkl_intrlv;
        /// <summary>skew factor</summary>
        public ushort dkl_skew;
        /// <summary>alternates per cyl (SCSI only)  </summary>
        public ushort dkl_apc;
        /// <summary>revolutions per minute</summary>
        public ushort dkl_rpm;
        /// <summary># sectors to skip, writes</summary>
        public ushort dkl_write_reinstruct;
        /// <summary># sectors to skip, reads </summary>
        public ushort dkl_read_reinstruct;
        /// <summary>for compatible expansion</summary>
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
        public ushort[] dkl_extra;
        /// <summary>unused part of 512 bytes</summary>
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = LEN_DKL_PAD16)]
        public byte[] dkl_pad;
        /// <summary>identifies this label format</summary>
        public ushort dkl_magic;
        /// <summary>xor checksum of sector</summary>
        public ushort dkl_cksum;
    }

#endregion

#region Nested type: dk_label8

    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    [SwapEndian]
    partial struct dk_label8
    {
        /// <summary>for compatibility</summary>
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = LEN_DKL_ASCII)]
        public byte[] dkl_asciilabel;
        /// <summary>vtoc inclusions from AT&amp;T SVr4</summary>
        public dk_vtoc8 dkl_vtoc;
        /// <summary># sectors to skip, writes</summary>
        public ushort dkl_write_reinstruct;
        /// <summary># sectors to skip, reads</summary>
        public ushort dkl_read_reinstruct;
        /// <summary>unused part of 512 bytes</summary>
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = LEN_DKL_PAD8)]
        public byte[] dkl_pad;
        /// <summary>rotations per minute</summary>
        public ushort dkl_rpm;
        /// <summary># physical cylinders</summary>
        public ushort dkl_pcyl;
        /// <summary>alternates per cylinder</summary>
        public ushort dkl_apc;
        /// <summary>obsolete</summary>
        public ushort dkl_obs1;
        /// <summary>obsolete</summary>
        public ushort dkl_obs2;
        /// <summary>interleave factor</summary>
        public ushort dkl_intrlv;
        /// <summary># of data cylinders</summary>
        public ushort dkl_ncyl;
        /// <summary># of alternate cylinders</summary>
        public ushort dkl_acyl;
        /// <summary># of heads in this partition</summary>
        public ushort dkl_nhead;
        /// <summary># of 512 byte sectors per track</summary>
        public ushort dkl_nsect;
        /// <summary>obsolete</summary>
        public ushort dkl_obs3;
        /// <summary>obsolete</summary>
        public ushort dkl_obs4;
        /// <summary>logical partition headers</summary>
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = NDKMAP)]
        public dk_map[] dkl_map;
        /// <summary>identifies this label format</summary>
        public ushort dkl_magic;
        /// <summary>xor checksum of sector</summary>
        public ushort dkl_cksum;
    }

#endregion

#region Nested type: dk_map

    /// <summary>SunOS logical partitions</summary>
    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    [SwapEndian]
    partial struct dk_map
    {
        /// <summary>starting cylinder</summary>
        public int dkl_cylno;
        /// <summary>number of blocks</summary>
        public int dkl_nblk;
    }

#endregion

#region Nested type: dk_map2

    /// <summary>Solaris logical partition for small disk label</summary>
    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    [SwapEndian]
    partial struct dk_map2
    {
        /// <summary> ID tag of partition</summary>
        public SunTag p_tag;
        /// <summary> permission flag</summary>
        public SunFlags p_flag;
    }

#endregion

#region Nested type: dk_vtoc16

    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    [SwapEndian]
    partial struct dk_vtoc16
    {
        /// <summary>info needed by mboot</summary>
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
        public uint[] v_bootinfo;
        /// <summary>to verify vtoc sanity</summary>
        public uint v_sanity;
        /// <summary>layout version</summary>
        public uint v_version;
        /// <summary>volume name</summary>
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = LEN_DKL_VVOL)]
        public byte[] v_volume;
        /// <summary>sector size in bytes</summary>
        public ushort v_sectorsz;
        /// <summary>number of partitions</summary>
        public ushort v_nparts;
        /// <summary>free space</summary>
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
        public uint[] v_reserved;
        /// <summary>partition headers</summary>
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = NDKMAP16)]
        public dkl_partition[] v_part;
        /// <summary>partition timestamp</summary>
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = NDKMAP16)]
        public int[] v_timestamp;
        /// <summary>for compatibility</summary>
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = LEN_DKL_ASCII)]
        public byte[] v_asciilabel;
    }

#endregion

#region Nested type: dk_vtoc8

    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    [SwapEndian]
    partial struct dk_vtoc8
    {
        /// <summary> layout version</summary>
        public uint v_version;
        /// <summary> volume name</summary>
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = LEN_DKL_VVOL)]
        public byte[] v_volume;
        /// <summary> number of partitions </summary>
        public ushort v_nparts;
        /// <summary> partition hdrs, sec 2</summary>
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = NDKMAP)]
        public dk_map2[] v_part;
        /// <summary>Alignment</summary>
        public ushort padding;
        /// <summary> info needed by mboot</summary>
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
        public uint[] v_bootinfo;
        /// <summary> to verify vtoc sanity</summary>
        public uint v_sanity;
        /// <summary> free space</summary>
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
        public uint[] v_reserved;
        /// <summary> partition timestamp</summary>
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = NDKMAP)]
        public int[] v_timestamp;
    }

#endregion

#region Nested type: dkl_partition

    /// <summary>Solaris logical partition</summary>
    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    [SwapEndian]
    partial struct dkl_partition
    {
        /// <summary>ID tag of partition</summary>
        public SunTag p_tag;
        /// <summary>permision flags</summary>
        public SunFlags p_flag;
        /// <summary>start sector no of partition</summary>
        public int p_start;
        /// <summary># of blocks in partition</summary>
        public int p_size;
    }

#endregion

#region Nested type: SunFlags

    [Flags]
    enum SunFlags : ushort
    {
        NoMount  = 0x0001,
        ReadOnly = 0x0010
    }

#endregion

#region Nested type: SunTag

    enum SunTag : ushort
    {
        SunEmpty       = 0x0000,
        SunBoot        = 0x0001,
        SunRoot        = 0x0002,
        SunSwap        = 0x0003,
        SunUsr         = 0x0004,
        SunWholeDisk   = 0x0005,
        SunStand       = 0x0006,
        SunVar         = 0x0007,
        SunHome        = 0x0008,
        SunAlt         = 0x0009,
        SunCache       = 0x000A,
        SunReserved    = 0x000B,
        VxVmPublic     = 0x000E,
        VxVmPrivate    = 0x000F,
        LinuxSwap      = 0x0082,
        Linux          = 0x0083,
        LVM            = 0x008E,
        LinuxRaid      = 0x00FD,
        NetBSD         = 0x00FF,
        FreeBSD_Swap   = 0x0901,
        FreeBSD_UFS    = 0x0902,
        FreeBSD_Vinum  = 0x0903,
        FreeBSD_ZFS    = 0x0904,
        FreeBSD_NANDFS = 0x0905
    }

#endregion
}