Skip to content

Commit

Permalink
Add A2R flux format support
Browse files Browse the repository at this point in the history
  • Loading branch information
FakeShemp committed Jul 7, 2023
1 parent 7bf2118 commit bed66e6
Show file tree
Hide file tree
Showing 18 changed files with 3,330 additions and 4,843 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
94 changes: 94 additions & 0 deletions Aaru.Images/A2R/A2R.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// /***************************************************************************
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : A2R.cs
// Author(s) : Rebecca Wallander <sakcheen@gmail.com>
//
// 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;
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 <sakcheen@gmail.com>
//
// 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
};
}
18 changes: 18 additions & 0 deletions Aaru.Images/A2R/Enums.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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,
}
}
117 changes: 117 additions & 0 deletions Aaru.Images/A2R/Helpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// /***************************************************************************
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : Helpers.cs
// Author(s) : Rebecca Wallander <sakcheen@gmail.com>
//
// 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
{
static long HeadTrackSubToA2rLocation(uint head, ushort track, byte subTrack, MediaType mediaType)
{
if(mediaType == MediaType.Apple32SS)
return head + (track * 4) + subTrack;

return head + (track * 2);
}

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);
}

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;
}

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;
}

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;
}

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;
}
56 changes: 56 additions & 0 deletions Aaru.Images/A2R/Identify.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// /***************************************************************************
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : Identify.cs
// Author(s) : Rebecca Wallander <sakcheen@gmail.com>
//
// Component : Disk image plugins.
//
// --[ Description ] ----------------------------------------------------------
//
// Identifies 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.IO;
using System.Linq;
using Aaru.CommonTypes.Interfaces;
using Aaru.Helpers;

namespace Aaru.DiscImages;

public sealed partial class A2R
{
public bool Identify(IFilter imageFilter)
{
Stream stream = imageFilter.GetDataForkStream();
stream.Seek(0, SeekOrigin.Begin);

if(stream.Length < 8)
return false;

byte[] hdr = new byte[4];

stream.EnsureRead(hdr, 0, 4);

return _a2rV2Signature.SequenceEqual(hdr) || _a2rV3Signature.SequenceEqual(hdr);
}
}
Loading

0 comments on commit bed66e6

Please sign in to comment.