Files
Aaru/Aaru.Images/CDRDAO/Identify.cs

106 lines
3.5 KiB
C#
Raw Normal View History

// /***************************************************************************
2020-02-27 12:31:25 +00:00
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : Identify.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Disk image plugins.
//
// --[ Description ] ----------------------------------------------------------
//
// Identifies cdrdao cuesheets (toc/bin).
//
// --[ 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-12-19 10:45:18 +00:00
// Copyright © 2011-2025 Natalia Portillo
// ****************************************************************************/
using System;
using System.IO;
using System.Text.RegularExpressions;
2020-02-27 00:33:26 +00:00
using Aaru.CommonTypes.Interfaces;
using Aaru.Helpers;
using Aaru.Logging;
namespace Aaru.Images;
2022-03-06 13:29:38 +00:00
public sealed partial class Cdrdao
{
2023-10-03 23:34:59 +01:00
#region IWritableOpticalImage Members
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public bool Identify(IFilter imageFilter)
{
2022-03-06 13:29:38 +00:00
try
{
2022-03-06 13:29:38 +00:00
imageFilter.GetDataForkStream().Seek(0, SeekOrigin.Begin);
byte[] testArray = new byte[512];
imageFilter.GetDataForkStream().EnsureRead(testArray, 0, 512);
2022-03-06 13:29:38 +00:00
imageFilter.GetDataForkStream().Seek(0, SeekOrigin.Begin);
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
// Check for unexpected control characters that shouldn't be present in a text file and can crash this plugin
bool twoConsecutiveNulls = false;
2022-03-06 13:29:38 +00:00
for(int i = 0; i < 512; i++)
2022-03-06 13:29:38 +00:00
{
2024-05-01 04:05:22 +01:00
if(i >= imageFilter.GetDataForkStream().Length) break;
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if(testArray[i] == 0)
{
2024-05-01 04:05:22 +01:00
if(twoConsecutiveNulls) return false;
2022-03-06 13:29:38 +00:00
twoConsecutiveNulls = true;
}
2022-03-06 13:29:38 +00:00
else
twoConsecutiveNulls = false;
if(testArray[i] < 0x20 && testArray[i] != 0x0A && testArray[i] != 0x0D && testArray[i] != 0x00)
2022-03-06 13:29:38 +00:00
return false;
}
2022-03-06 13:29:38 +00:00
_tocStream = new StreamReader(imageFilter.GetDataForkStream());
2022-03-06 13:29:38 +00:00
var cr = new Regex(REGEX_COMMENT);
var dr = new Regex(REGEX_DISCTYPE);
2022-03-06 13:29:38 +00:00
while(_tocStream.Peek() >= 0)
{
string line = _tocStream.ReadLine();
2022-03-06 13:29:38 +00:00
Match dm = dr.Match(line ?? "");
2022-11-15 01:35:06 +00:00
Match cm = cr.Match(line ?? "");
2022-03-06 13:29:38 +00:00
// Skip comments at start of file
2024-05-01 04:05:22 +01:00
if(cm.Success) continue;
2022-03-06 13:29:38 +00:00
return dm.Success;
}
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
return false;
}
catch(Exception ex)
{
AaruConsole.ErrorWriteLine(Localization.Exception_trying_to_identify_image_file_0, _cdrdaoFilter.Filename);
AaruConsole.WriteException(ex);
2022-03-06 13:29:38 +00:00
return false;
}
}
2023-10-03 23:34:59 +01:00
#endregion
}