Skip to content

Commit

Permalink
Merge pull request #809 from aaru-dps/fakeshemp/a2r
Browse files Browse the repository at this point in the history
Add A2R flux format support
  • Loading branch information
claunia authored Jul 9, 2023
2 parents 7bf2118 + f6b8ba3 commit 1eb45e5
Show file tree
Hide file tree
Showing 17 changed files with 3,448 additions and 4,844 deletions.
2 changes: 1 addition & 1 deletion .idea/.idea.Aaru/.idea/runConfigurations/Aaru.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Aaru.CommonTypes
91 changes: 91 additions & 0 deletions Aaru.Images/A2R/A2R.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// /***************************************************************************
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : A2R.cs
// Author(s) : Rebecca Wallander <[email protected]>
//
// Component : Disk image plugins.
//
// --[ Description ] ----------------------------------------------------------
//
// Manages A2R flux images.
//
// --[ 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-2023 Rebecca Wallander
// ****************************************************************************/

// Version 2 documentation: https://web.archive.org/web/20200325131633/https://applesaucefdc.com/a2r/
// Version 3 documentation: https://web.archive.org/web/20220526215820/https://applesaucefdc.com/a2r/

using System.Collections.Generic;
using System.IO;
using Aaru.CommonTypes.Enums;
using Aaru.CommonTypes.Interfaces;
using Aaru.CommonTypes.Structs;

namespace Aaru.DiscImages;

/// <inheritdoc cref="Aaru.CommonTypes.Interfaces.IMediaImage" />
/// <summary>Implements reading A2R flux images</summary>
public sealed partial class A2R : IFluxImage, IMediaImage, IWritableImage, IWritableFluxImage
{
ImageInfo _imageInfo;
Stream _a2rStream;
IFilter _a2rFilter;
FileStream _writingStream;
A2rHeader Header;
Dictionary<string, string> Meta;
InfoChunkV2 _infoChunkV2;
InfoChunkV3 _infoChunkV3;
List<StreamCapture> _a2rCaptures;
uint _currentResolution;

// Offset from the start of the current RWCP to the next capture
uint _currentCaptureOffset = 16;

// 53 = A2R header, INFO header, INFO data
long _currentRwcpStart = 53;

public A2R()
{
_imageInfo = new ImageInfo
{
ReadableSectorTags = new List<SectorTagType>(),
ReadableMediaTags = new List<MediaTagType>(),
HasPartitions = false,
HasSessions = false,
Version = null,
Application = null,
ApplicationVersion = null,
Creator = null,
Comments = null,
MediaManufacturer = null,
MediaModel = null,
MediaSerialNumber = null,
MediaBarcode = null,
MediaPartNumber = null,
MediaSequence = 0,
LastMediaSequence = 0,
DriveManufacturer = null,
DriveModel = null,
DriveSerialNumber = null,
DriveFirmwareRevision = null
};
}
}
71 changes: 71 additions & 0 deletions Aaru.Images/A2R/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// /***************************************************************************
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : Constants.cs
// Author(s) : Rebecca Wallander <[email protected]>
//
// Component : Disk image plugins.
//
// --[ Description ] ----------------------------------------------------------
//
// Contains constants for A2R flux images.
//
// --[ 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-2023 Rebecca Wallander
// ****************************************************************************/

namespace Aaru.DiscImages;

public sealed partial class A2R
{
readonly byte[] _a2rV2Signature =
{
0x41, 0x32, 0x52, 0x32 // A2R2
};

readonly byte[] _a2rV3Signature =
{
0x41, 0x32, 0x52, 0x33 // A2R3
};

readonly byte[] _infoChunkSignature =
{
0x49, 0x4E, 0x46, 0x4F // INFO
};

readonly byte[] _rwcpChunkSignature =
{
0x52, 0x57, 0x43, 0x50 // RWCP
};

readonly byte[] _slvdChunkSignature =
{
0x53, 0x4C, 0x56, 0x44 // SLVD
};

readonly byte[] _metaChunkSignature =
{
0x4D, 0x45, 0x54, 0x41 // META
};

readonly byte[] _strmChunkSignature =
{
0x53, 0x54, 0x52, 0x4D // STRM
};
}
50 changes: 50 additions & 0 deletions Aaru.Images/A2R/Enums.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// /***************************************************************************
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : Enums.cs
// Author(s) : Rebecca Wallander <[email protected]>
//
// Component : Disk image plugins.
//
// --[ Description ] ----------------------------------------------------------
//
// Contains enumerations for A2R flux images.
//
// --[ 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-2023 Rebecca Wallander
// ****************************************************************************/

using System.Diagnostics.CodeAnalysis;

namespace Aaru.DiscImages;

public sealed partial class A2R
{
[SuppressMessage("ReSharper", "InconsistentNaming")]
public enum A2rDriveType : byte
{
SS_525_40trk_quarterStep = 0x1, DS_35_80trk_appleCLV = 0x2, DS_525_80trk = 0x3,
DS_525_40trk = 0x4, DS_35_80trk = 0x5, DS_8 = 0x6
}

public enum A2rDiskType : byte
{
_525 = 0x01, _35 = 0x2,
}
}
165 changes: 165 additions & 0 deletions Aaru.Images/A2R/Helpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
// /***************************************************************************
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : Helpers.cs
// Author(s) : Rebecca Wallander <[email protected]>
//
// Component : Disk image plugins.
//
// --[ Description ] ----------------------------------------------------------
//
// Contains helpers for A2R flux images.
//
// --[ 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-2023 Rebecca Wallander
// ****************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using Aaru.CommonTypes;

namespace Aaru.DiscImages;

public sealed partial class A2R
{
/// <summary>
/// Takes a Head, Track and Sub-Track representation, as well as the <c>MediaType</c>,
/// and converts it to the Track representation used by A2R.
/// </summary>
/// <param name="head">The head number</param>
/// <param name="track">The track number</param>
/// <param name="subTrack">The sub-track number</param>
/// <param name="mediaType">The media type of the image</param>
/// <returns>A2R format location</returns>
static long HeadTrackSubToA2rLocation(uint head, ushort track, byte subTrack, MediaType mediaType)
{
if(mediaType == MediaType.Apple32SS)
return head + (track * 4) + subTrack;

return head + (track * 2);
}

/// <summary>
/// Takes a Head, Track and Sub-Track representation, as well as the <c>A2rDriveType</c>,
/// and converts it to the Track representation used by A2R.
/// </summary>
/// <param name="head">The head number</param>
/// <param name="track">The track number</param>
/// <param name="subTrack">The sub-track number</param>
/// <param name="driveType">The drive type enum of the A2R image</param>
/// <returns>A2R format location</returns>
static long HeadTrackSubToA2rLocation(uint head, ushort track, byte subTrack, A2rDriveType driveType)
{
if(driveType == A2rDriveType.SS_525_40trk_quarterStep)
return head + (track * 4) + subTrack;

return head + (track * 2);
}

/// <summary>
/// Takes an A2R location and a <c>MediaType</c>, and converts it to a Head, Track and Sub-Track representation
/// used by the internal representation. The <c>MediaType</c> is needed because the track location is different
/// for different types of media sources.
/// </summary>
/// <param name="location">A2R format location</param>
/// <param name="mediaType"></param>
/// <param name="head">The head number</param>
/// <param name="track">The track number</param>
/// <param name="subTrack">The sub-track number</param>
static void A2rLocationToHeadTrackSub(uint location, MediaType mediaType, out uint head, out ushort track,
out byte subTrack)
{
if(mediaType == MediaType.Apple32SS)
{
head = 0;
track = (ushort)(location / 4);
subTrack = (byte)(location % 4);

return;
}

head = location % 2;
track = (ushort)((location - head) / 2);
subTrack = 0;
}

/// <summary>
/// Takes a single number flux (uint length) and converts it to a flux in the
/// internal representation format (byte length)
/// </summary>
/// <param name="ticks">The <c>uint</c> flux representation</param>
/// <returns>The <c>byte[]</c> flux representation</returns>
static byte[] UInt32ToFluxRepresentation(uint ticks)
{
uint over = ticks / 255;

if(over == 0)
return new[]
{
(byte)ticks
};

byte[] expanded = new byte[over + 1];

Array.Fill(expanded, (byte)255, 0, (int)over);
expanded[^1] = (byte)(ticks % 255);

return expanded;
}

/// <summary>
/// Takes a flux representation in the internal format (byte length) and converts it to
/// an array of single number fluxes (uint length)
/// </summary>
/// <param name="flux">The <c>byte[]</c> flux representation</param>
/// <returns>The <c>uint</c> flux representation</returns>
static List<uint> FluxRepresentationsToUInt32List(IEnumerable<byte> flux)
{
List<uint> scpData = new();
uint tick = 0;

foreach(byte b in flux)
{
if(b == 255)
tick += 255;
else
{
tick += b;
scpData.Add(tick);
tick = 0;
}
}

return scpData;
}

/// <summary>
/// A2R has two types of flux capture types; "timing" and "xtiming". The only difference is the length of the
/// capture, with "timing" being about 1¼ revolutions. This function returns <c>true</c> if the flux buffer is "timing"
/// and <c>false</c> otherwise.
/// </summary>
/// <param name="resolution">The resolution of the flux capture</param>
/// <param name="buffer">The flux data</param>
/// <returns><c>true</c> if "timing", <c>false</c> if "xtiming"</returns>
static bool IsCaptureTypeTiming(ulong resolution, byte[] buffer) =>

// TODO: This is only accurate for 300rpm
buffer.Select(static x => (int)x).Sum() * (long)resolution is > 230000000000 and < 270000000000;
}
Loading

0 comments on commit 1eb45e5

Please sign in to comment.