Files
Aaru/Aaru.Filesystems/ISO9660/Mode2.cs

249 lines
11 KiB
C#
Raw Normal View History

2019-07-31 20:10:27 +01:00
// /***************************************************************************
2020-02-27 12:31:25 +00:00
// Aaru Data Preservation Suite
2019-07-31 20:10:27 +01:00
// ----------------------------------------------------------------------------
//
// Filename : Mode2.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : ISO9660 filesystem plugin.
//
// --[ Description ] ----------------------------------------------------------
//
// Handles reading sectors in MODE 0, 1 and 2.
//
// --[ 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/>.
//
// ----------------------------------------------------------------------------
2024-05-01 04:17:32 +01:00
// Copyright © 2011-2024 Natalia Portillo
// In the loving memory of Facunda "Tata" Suárez Domínguez, R.I.P. 2019/07/24
2019-07-31 20:10:27 +01:00
// ****************************************************************************/
using System;
using System.IO;
using Aaru.CommonTypes.Enums;
2020-03-10 22:47:10 +00:00
using Aaru.Console;
using Aaru.Decoders.CD;
namespace Aaru.Filesystems;
2022-03-06 13:29:38 +00:00
public sealed partial class ISO9660
{
2022-03-06 13:29:38 +00:00
ErrorNumber ReadSector(ulong sector, out byte[] buffer, bool interleaved = false, byte fileNumber = 0)
{
2022-03-06 13:29:38 +00:00
ErrorNumber errno;
buffer = null;
uint sectorCount = (uint)_blockSize / 2048;
2022-03-06 13:29:38 +00:00
2024-05-01 04:05:22 +01:00
if(_blockSize % 2048 > 0) sectorCount++;
2022-03-06 13:29:38 +00:00
ulong realSector = sector * _blockSize / 2048;
2022-03-06 13:29:38 +00:00
ulong offset = sector * _blockSize % 2048;
byte[] data;
if(sectorCount == 1)
{
2022-03-06 13:29:38 +00:00
errno = _image.ReadSectorLong(realSector, out data);
2024-05-01 04:05:22 +01:00
if(errno != ErrorNumber.NoError) errno = _image.ReadSector(realSector, out data);
2024-05-01 04:05:22 +01:00
if(errno != ErrorNumber.NoError) return errno;
2022-03-06 13:29:38 +00:00
if(_debug)
{
2022-03-06 13:29:38 +00:00
switch(data.Length)
{
case 2048:
AaruConsole.DebugWriteLine(MODULE_NAME,
Localization.tor_Sector_0_Cooked_Mode_zero_one_Mode_two_Form_one,
2022-03-06 13:29:38 +00:00
realSector);
break;
case 2324:
2024-05-01 04:05:22 +01:00
AaruConsole.DebugWriteLine(MODULE_NAME,
Localization.tor_Sector_0_Cooked_Mode_two_Form_two,
realSector);
2022-03-06 13:29:38 +00:00
break;
case 2336:
AaruConsole.DebugWriteLine(MODULE_NAME,
2024-05-01 04:05:22 +01:00
Localization
.tor_Sector_0_Cooked_Mode_two_Form_1_File_Number_2_Channel_Number_3_Submode_4_Coding_Information_5,
2022-03-06 13:29:38 +00:00
realSector,
2024-05-01 04:05:22 +01:00
((Mode2Submode)data[2]).HasFlag(Mode2Submode.Form2) ? 2 : 1,
data[0],
data[1],
(Mode2Submode)data[2],
data[3]);
2022-03-06 13:29:38 +00:00
break;
case 2352 when data[0] != 0x00 ||
data[1] != 0xFF ||
data[2] != 0xFF ||
data[3] != 0xFF ||
data[4] != 0xFF ||
data[5] != 0xFF ||
data[6] != 0xFF ||
data[7] != 0xFF ||
data[8] != 0xFF ||
data[9] != 0xFF ||
data[10] != 0xFF ||
data[11] != 0x00:
AaruConsole.DebugWriteLine(MODULE_NAME, Localization.tor_Sector_0_Raw_Audio, realSector);
2022-03-06 13:29:38 +00:00
break;
case 2352 when data[15] != 2:
2024-05-01 04:05:22 +01:00
AaruConsole.DebugWriteLine(MODULE_NAME,
Localization.tor_Sector_0_1_2_3_Raw_Mode_4,
realSector,
data[12],
data[13],
data[14],
data[15]);
2022-03-06 13:29:38 +00:00
break;
case 2352:
AaruConsole.DebugWriteLine(MODULE_NAME,
2024-05-01 04:05:22 +01:00
Localization
.tor_Sector_0_1_2_3_Raw_Mode_two_Form_4_File_Number_5_Channel_Number_6_Submode_7_Coding_Information_8,
realSector,
data[12],
data[13],
data[14],
2022-03-06 13:29:38 +00:00
((Mode2Submode)data[18]).HasFlag(Mode2Submode.Form2) ? 2 : 1,
2024-05-01 04:05:22 +01:00
data[16],
data[17],
(Mode2Submode)data[18],
data[19]);
2022-03-06 13:29:38 +00:00
break;
}
}
2022-03-06 13:29:38 +00:00
if(_blockSize == 2048)
{
buffer = Sector.GetUserData(data, interleaved, fileNumber);
return ErrorNumber.NoError;
}
var tmp = new byte[_blockSize];
2022-03-06 13:29:38 +00:00
Array.Copy(Sector.GetUserData(data, interleaved, fileNumber), (int)offset, tmp, 0, _blockSize);
2022-03-06 13:29:38 +00:00
buffer = tmp;
2022-03-06 13:29:38 +00:00
return errno;
}
else
{
var ms = new MemoryStream();
for(uint i = 0; i < sectorCount; i++)
{
2022-03-06 13:29:38 +00:00
ulong dstSector = realSector + 1;
errno = _image.ReadSectorLong(dstSector, out data);
2024-05-01 04:05:22 +01:00
if(errno != ErrorNumber.NoError) errno = _image.ReadSector(dstSector, out data);
2024-05-01 04:05:22 +01:00
if(errno != ErrorNumber.NoError) return errno;
if(_debug)
{
switch(data.Length)
{
case 2048:
AaruConsole.DebugWriteLine(MODULE_NAME,
Localization.tor_Sector_0_Cooked_Mode_zero_one_Mode_two_Form_one,
2022-03-07 07:36:44 +00:00
dstSector);
break;
case 2324:
2024-05-01 04:05:22 +01:00
AaruConsole.DebugWriteLine(MODULE_NAME,
Localization.tor_Sector_0_Cooked_Mode_two_Form_two,
dstSector);
break;
case 2336:
AaruConsole.DebugWriteLine(MODULE_NAME,
2024-05-01 04:05:22 +01:00
Localization
.tor_Sector_0_Cooked_Mode_two_Form_1_File_Number_2_Channel_Number_3_Submode_4_Coding_Information_5,
2022-03-06 13:29:38 +00:00
dstSector,
((Mode2Submode)data[2]).HasFlag(Mode2Submode.Form2) ? 2 : 1,
2024-05-01 04:05:22 +01:00
data[0],
data[1],
(Mode2Submode)data[2],
data[3]);
break;
case 2352 when data[0] != 0x00 ||
data[1] != 0xFF ||
data[2] != 0xFF ||
data[3] != 0xFF ||
data[4] != 0xFF ||
data[5] != 0xFF ||
data[6] != 0xFF ||
data[7] != 0xFF ||
data[8] != 0xFF ||
data[9] != 0xFF ||
data[10] != 0xFF ||
data[11] != 0x00:
AaruConsole.DebugWriteLine(MODULE_NAME, Localization.tor_Sector_0_Raw_Audio, dstSector);
break;
case 2352 when data[15] != 2:
2024-05-01 04:05:22 +01:00
AaruConsole.DebugWriteLine(MODULE_NAME,
Localization.tor_Sector_0_1_2_3_Raw_Mode_4,
dstSector,
data[12],
data[13],
data[14],
data[15]);
break;
case 2352:
AaruConsole.DebugWriteLine(MODULE_NAME,
2024-05-01 04:05:22 +01:00
Localization
.tor_Sector_0_1_2_3_Raw_Mode_two_Form_4_File_Number_5_Channel_Number_6_Submode_7_Coding_Information_8,
dstSector,
data[12],
data[13],
data[14],
((Mode2Submode)data[18]).HasFlag(Mode2Submode.Form2) ? 2 : 1,
2024-05-01 04:05:22 +01:00
data[16],
data[17],
(Mode2Submode)data[18],
data[19]);
break;
}
}
2022-03-06 13:29:38 +00:00
byte[] sectorData = Sector.GetUserData(data, interleaved, fileNumber);
2022-03-06 13:29:38 +00:00
ms.Write(sectorData, 0, sectorData.Length);
}
var tmp = new byte[_blockSize];
2022-03-06 13:29:38 +00:00
Array.Copy(Sector.GetUserData(ms.ToArray(), interleaved, fileNumber), 0, tmp, 0, _blockSize);
buffer = tmp;
2022-03-06 13:29:38 +00:00
return ErrorNumber.NoError;
}
}
}