From 65fbdeb3dbac607b91c72e4d0bc2f53a28c3d1c2 Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Thu, 8 Oct 2026 14:51:39 +0100 Subject: [PATCH] [AaruFormat] Read, write and clear Data Position Measurement, stored in the libaaruformat DPM block with its status, layer ends and rotation calibrations, also on resumed images. --- Aaru.Images/AaruFormat/Dpm.cs | 215 ++++++++++++++++++++++++++++++++ Aaru.Images/AaruFormat/Read.cs | 8 -- Aaru.Images/AaruFormat/Write.cs | 9 -- 3 files changed, 215 insertions(+), 17 deletions(-) create mode 100644 Aaru.Images/AaruFormat/Dpm.cs diff --git a/Aaru.Images/AaruFormat/Dpm.cs b/Aaru.Images/AaruFormat/Dpm.cs new file mode 100644 index 000000000..13c112c2a --- /dev/null +++ b/Aaru.Images/AaruFormat/Dpm.cs @@ -0,0 +1,215 @@ +using System; +using System.Buffers.Binary; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using Aaru.Checksums; +using Aaru.CommonTypes.Enums; +using Aaru.CommonTypes.Structs; +using Aaru.Helpers; + +namespace Aaru.Images; + +public sealed partial class AaruFormat +{ + /// Size of the DPM block header + const int DPM_HEADER_SIZE = 40; + /// Size of a DPM entry + const int DPM_ENTRY_SIZE = 17; + /// Size of a DPM calibration + const int DPM_CALIBRATION_SIZE = 24; + /// DPM header flag set when the layers use opposite track path + const byte DPM_FLAG_OPPOSITE_TRACK_PATH = 0x01; + + // AARU_EXPORT int32_t AARU_CALL aaruf_get_dpm(const void *context, uint8_t *buffer, size_t *length) + [LibraryImport("libaaruformat", EntryPoint = "aaruf_get_dpm", SetLastError = true)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvStdcall)])] + private static partial Status aaruf_get_dpm(IntPtr context, byte[] buffer, ref nuint length); + + // AARU_EXPORT int32_t AARU_CALL aaruf_set_dpm(void *context, const uint8_t *data, size_t length) + [LibraryImport("libaaruformat", EntryPoint = "aaruf_set_dpm", SetLastError = true)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvStdcall)])] + private static partial Status aaruf_set_dpm(IntPtr context, [In] byte[] data, nuint length); + + // AARU_EXPORT int32_t AARU_CALL aaruf_clear_dpm(void *context) + [LibraryImport("libaaruformat", EntryPoint = "aaruf_clear_dpm", SetLastError = true)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvStdcall)])] + private static partial Status aaruf_clear_dpm(IntPtr context); + + /// + public ErrorNumber ReadDpm(out DataPositionMeasurement dpm) + { + dpm = default(DataPositionMeasurement); + + nuint length = 0; + Status res = aaruf_get_dpm(_context, null, ref length); + + if(res == Status.MetadataNotPresent) return ErrorNumber.NoData; + + if(res != Status.BufferTooSmall) return StatusToErrorNumber(res); + + var block = new byte[length]; + res = aaruf_get_dpm(_context, block, ref length); + + if(res != Status.Ok) return StatusToErrorNumber(res); + + return DecodeDpm(block, out dpm) ? ErrorNumber.NoError : ErrorNumber.InvalidArgument; + } + + /// + public bool SetDpm(DataPositionMeasurement dpm) + { + if(!Dpm.Validate(dpm)) + { + ErrorMessage = Localization.Invalid_DPM; + + return false; + } + + byte[] block = EncodeDpm(dpm); + Status res = aaruf_set_dpm(_context, block, (nuint)block.Length); + + ErrorMessage = StatusToErrorMessage(res); + + return res == Status.Ok; + } + + /// Removes the Data Position Measurement from an image being written or resumed + /// true if operating completed successfully, false otherwise + public bool ClearDpm() + { + Status res = aaruf_clear_dpm(_context); + + ErrorMessage = StatusToErrorMessage(res); + + return res == Status.Ok; + } + + /// Serializes a DPM as a libaaruformat DPM block, header included + /// DPM + /// Serialized block + static byte[] EncodeDpm(DataPositionMeasurement dpm) + { + ulong[] layerEnds = dpm.LayerEnds ?? []; + DpmCalibration[] calibrations = dpm.Calibrations ?? []; + + int payloadLength = layerEnds.Length * sizeof(ulong) + + dpm.Entries.Length * DPM_ENTRY_SIZE + + calibrations.Length * DPM_CALIBRATION_SIZE; + + var block = new byte[DPM_HEADER_SIZE + payloadLength]; + Span payload = block.AsSpan(DPM_HEADER_SIZE); + int offset = 0; + + foreach(ulong layerEnd in layerEnds) + { + BinaryPrimitives.WriteUInt64LittleEndian(payload[offset..], layerEnd); + offset += sizeof(ulong); + } + + foreach(DpmEntry entry in dpm.Entries) + { + BinaryPrimitives.WriteUInt64LittleEndian(payload[offset..], entry.Lba); + BinaryPrimitives.WriteUInt64LittleEndian(payload[(offset + 8)..], entry.Angle); + payload[offset + 16] = (byte)entry.Status; + offset += DPM_ENTRY_SIZE; + } + + foreach(DpmCalibration calibration in calibrations) + { + BinaryPrimitives.WriteUInt64LittleEndian(payload[offset..], calibration.Lba); + BinaryPrimitives.WriteUInt64LittleEndian(payload[(offset + 8)..], calibration.RotationPeriod); + BinaryPrimitives.WriteUInt64LittleEndian(payload[(offset + 16)..], calibration.SectorsPerTurn); + offset += DPM_CALIBRATION_SIZE; + } + + Crc64Context.Data(payload.ToArray(), out byte[] crc); + + Span header = block.AsSpan(0, DPM_HEADER_SIZE); + BinaryPrimitives.WriteUInt32LittleEndian(header, (uint)BlockType.DataPositionMeasurementBlock); + BinaryPrimitives.WriteUInt32LittleEndian(header[4..], (uint)dpm.Entries.Length); + BinaryPrimitives.WriteUInt32LittleEndian(header[8..], (uint)calibrations.Length); + BinaryPrimitives.WriteUInt32LittleEndian(header[12..], dpm.NominalSpacing); + BinaryPrimitives.WriteUInt16LittleEndian(header[16..], dpm.TimingUnit); + BinaryPrimitives.WriteUInt16LittleEndian(header[18..], dpm.Speed); + header[20] = (byte)layerEnds.Length; + header[21] = dpm.OppositeTrackPath ? DPM_FLAG_OPPOSITE_TRACK_PATH : (byte)0; + BinaryPrimitives.WriteUInt64LittleEndian(header[24..], (ulong)payloadLength); + BinaryPrimitives.WriteUInt64LittleEndian(header[32..], Swapping.Swap(BitConverter.ToUInt64(crc, 0))); + + return block; + } + + /// Deserializes a libaaruformat DPM block, header included + /// Serialized block + /// DPM + /// true if the block is consistent, false otherwise + static bool DecodeDpm(byte[] block, out DataPositionMeasurement dpm) + { + dpm = default(DataPositionMeasurement); + + if(block.Length < DPM_HEADER_SIZE) return false; + + ReadOnlySpan header = block.AsSpan(0, DPM_HEADER_SIZE); + + if(BinaryPrimitives.ReadUInt32LittleEndian(header) != (uint)BlockType.DataPositionMeasurementBlock) + return false; + + uint entries = BinaryPrimitives.ReadUInt32LittleEndian(header[4..]); + uint calibrations = BinaryPrimitives.ReadUInt32LittleEndian(header[8..]); + byte layerEnds = header[20]; + ulong payloadLength = BinaryPrimitives.ReadUInt64LittleEndian(header[24..]); + + if(payloadLength != + (ulong)layerEnds * sizeof(ulong) + + (ulong)entries * DPM_ENTRY_SIZE + + (ulong)calibrations * DPM_CALIBRATION_SIZE || + payloadLength != (ulong)(block.Length - DPM_HEADER_SIZE)) + return false; + + ReadOnlySpan payload = block.AsSpan(DPM_HEADER_SIZE); + int offset = 0; + + dpm = new DataPositionMeasurement + { + NominalSpacing = BinaryPrimitives.ReadUInt32LittleEndian(header[12..]), + TimingUnit = BinaryPrimitives.ReadUInt16LittleEndian(header[16..]), + Speed = BinaryPrimitives.ReadUInt16LittleEndian(header[18..]), + OppositeTrackPath = (header[21] & DPM_FLAG_OPPOSITE_TRACK_PATH) != 0, + LayerEnds = new ulong[layerEnds], + Entries = new DpmEntry[entries], + Calibrations = new DpmCalibration[calibrations] + }; + + for(int i = 0; i < layerEnds; i++) + { + dpm.LayerEnds[i] = BinaryPrimitives.ReadUInt64LittleEndian(payload[offset..]); + offset += sizeof(ulong); + } + + for(int i = 0; i < entries; i++) + { + dpm.Entries[i] = new DpmEntry + { + Lba = BinaryPrimitives.ReadUInt64LittleEndian(payload[offset..]), + Angle = BinaryPrimitives.ReadUInt64LittleEndian(payload[(offset + 8)..]), + Status = (DpmEntryStatus)payload[offset + 16] + }; + + offset += DPM_ENTRY_SIZE; + } + + for(int i = 0; i < calibrations; i++) + { + dpm.Calibrations[i] = new DpmCalibration + { + Lba = BinaryPrimitives.ReadUInt64LittleEndian(payload[offset..]), + RotationPeriod = BinaryPrimitives.ReadUInt64LittleEndian(payload[(offset + 8)..]), + SectorsPerTurn = BinaryPrimitives.ReadUInt64LittleEndian(payload[(offset + 16)..]) + }; + + offset += DPM_CALIBRATION_SIZE; + } + + return true; + } +} \ No newline at end of file diff --git a/Aaru.Images/AaruFormat/Read.cs b/Aaru.Images/AaruFormat/Read.cs index e44ed422b..012b3ee82 100644 --- a/Aaru.Images/AaruFormat/Read.cs +++ b/Aaru.Images/AaruFormat/Read.cs @@ -111,14 +111,6 @@ public sealed partial class AaruFormat return StatusToErrorNumber(res); } - /// - public ErrorNumber ReadDpm(out DataPositionMeasurement dpm) - { - dpm = default(DataPositionMeasurement); - - return ErrorNumber.NotSupported; - } - /// public ErrorNumber ReadSector(ulong sectorAddress, uint track, out byte[] buffer, out SectorStatus sectorStatus) { diff --git a/Aaru.Images/AaruFormat/Write.cs b/Aaru.Images/AaruFormat/Write.cs index 1956cbac6..57d9aec13 100644 --- a/Aaru.Images/AaruFormat/Write.cs +++ b/Aaru.Images/AaruFormat/Write.cs @@ -7,7 +7,6 @@ using System.Runtime.InteropServices; using System.Text; using Aaru.CommonTypes; using Aaru.CommonTypes.Enums; -using Aaru.CommonTypes.Structs; using Aaru.Helpers; using Aaru.Logging; using Marshal = System.Runtime.InteropServices.Marshal; @@ -66,14 +65,6 @@ public sealed partial class AaruFormat #region IWritableOpticalImage Members - /// - public bool SetDpm(DataPositionMeasurement dpm) - { - ErrorMessage = Localization.Unsupported_feature; - - return false; - } - /// public bool WriteSector(byte[] data, ulong sectorAddress, bool negative, SectorStatus sectorStatus) {