Files
Aaru/Aaru.Core/Devices/Dumping/CompactDisc/Offset.cs

123 lines
4.8 KiB
C#
Raw Normal View History

// /***************************************************************************
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
2020-03-11 21:56:55 +00:00
// Filename : Offset.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
2020-03-11 21:56:55 +00:00
// Component : CompactDisc dumping.
//
// --[ Description ] ----------------------------------------------------------
//
2020-03-11 21:56:55 +00:00
// Calculates CompactDisc data offset.
//
// --[ License ] --------------------------------------------------------------
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
2022-02-18 10:02:53 +00:00
// Copyright © 2011-2022 Natalia Portillo
// ****************************************************************************/
2022-03-07 07:36:44 +00:00
// ReSharper disable JoinDeclarationAndInitializer
// ReSharper disable InlineOutVariableDeclaration
// ReSharper disable TooWideLocalVariableScope
2022-03-06 13:29:38 +00:00
namespace Aaru.Core.Devices.Dumping;
2022-03-07 07:36:44 +00:00
using System;
using Aaru.Devices;
2022-03-06 13:29:38 +00:00
partial class Dump
{
2022-03-06 13:29:38 +00:00
/// <summary>Fix offset in audio/scrambled sectors</summary>
/// <param name="offsetBytes">Offset in bytes</param>
/// <param name="sectorSize">Sector size in bytes</param>
/// <param name="sectorsForOffset">How many extra sectors we got for offset</param>
/// <param name="supportedSubchannel">Subchannel type</param>
/// <param name="blocksToRead">How many sectors did we got</param>
/// <param name="subSize">Subchannel size in bytes</param>
/// <param name="cmdBuf">Data buffer</param>
/// <param name="blockSize">Block size in bytes</param>
/// <param name="failedCrossingLeadOut">Set if we failed to cross into the Lead-Out</param>
2022-03-07 07:36:44 +00:00
static void FixOffsetData(int offsetBytes, uint sectorSize, int sectorsForOffset, MmcSubchannel supportedSubchannel,
ref uint blocksToRead, uint subSize, ref byte[] cmdBuf, uint blockSize,
bool failedCrossingLeadOut)
{
2022-03-06 13:29:38 +00:00
if(cmdBuf.Length == 0)
return;
2022-03-07 07:36:44 +00:00
int offsetFix = offsetBytes < 0 ? (int)(sectorSize * sectorsForOffset + offsetBytes) : offsetBytes;
2022-03-06 13:29:38 +00:00
byte[] tmpBuf;
if(supportedSubchannel != MmcSubchannel.None)
{
2022-03-06 13:29:38 +00:00
// De-interleave subchannel
2022-03-07 07:36:44 +00:00
var data = new byte[sectorSize * blocksToRead];
var sub = new byte[subSize * blocksToRead];
2022-03-06 13:29:38 +00:00
2022-03-07 07:36:44 +00:00
for(var b = 0; b < blocksToRead; b++)
2022-03-06 13:29:38 +00:00
{
2022-03-07 07:36:44 +00:00
Array.Copy(cmdBuf, (int)(0 + b * blockSize), data, sectorSize * b, sectorSize);
Array.Copy(cmdBuf, (int)(sectorSize + b * blockSize), sub, subSize * b, subSize);
2022-03-06 13:29:38 +00:00
}
if(failedCrossingLeadOut)
{
blocksToRead += (uint)sectorsForOffset;
tmpBuf = new byte[sectorSize * blocksToRead];
Array.Copy(data, 0, tmpBuf, 0, data.Length);
data = tmpBuf;
tmpBuf = new byte[subSize * blocksToRead];
Array.Copy(sub, 0, tmpBuf, 0, sub.Length);
sub = tmpBuf;
}
tmpBuf = new byte[sectorSize * (blocksToRead - sectorsForOffset)];
Array.Copy(data, offsetFix, tmpBuf, 0, tmpBuf.Length);
data = tmpBuf;
2022-03-06 13:29:38 +00:00
blocksToRead -= (uint)sectorsForOffset;
2022-03-06 13:29:38 +00:00
// Re-interleave subchannel
cmdBuf = new byte[blockSize * blocksToRead];
2022-03-07 07:36:44 +00:00
for(var b = 0; b < blocksToRead; b++)
{
2022-03-07 07:36:44 +00:00
Array.Copy(data, sectorSize * b, cmdBuf, (int)(0 + b * blockSize), sectorSize);
Array.Copy(sub, subSize * b, cmdBuf, (int)(sectorSize + b * blockSize), subSize);
}
2022-03-06 13:29:38 +00:00
}
else
{
if(failedCrossingLeadOut)
{
2022-03-06 13:29:38 +00:00
blocksToRead += (uint)sectorsForOffset;
tmpBuf = new byte[blockSize * blocksToRead];
Array.Copy(cmdBuf, 0, tmpBuf, 0, cmdBuf.Length);
cmdBuf = tmpBuf;
}
2022-03-06 13:29:38 +00:00
tmpBuf = new byte[blockSize * (blocksToRead - sectorsForOffset)];
Array.Copy(cmdBuf, offsetFix, tmpBuf, 0, tmpBuf.Length);
cmdBuf = tmpBuf;
blocksToRead -= (uint)sectorsForOffset;
}
}
}