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

108 lines
3.9 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/>.
//
// ----------------------------------------------------------------------------
2020-12-31 23:08:23 +00:00
// Copyright © 2011-2021 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.Console;
2020-02-27 00:33:26 +00:00
namespace Aaru.DiscImages
{
2020-07-22 13:20:25 +01:00
public sealed partial class Cdrdao
{
public bool Identify(IFilter imageFilter)
{
try
{
imageFilter.GetDataForkStream().Seek(0, SeekOrigin.Begin);
byte[] testArray = new byte[512];
imageFilter.GetDataForkStream().Read(testArray, 0, 512);
imageFilter.GetDataForkStream().Seek(0, SeekOrigin.Begin);
2020-02-29 18:03:35 +00:00
// Check for unexpected control characters that shouldn't be present in a text file and can crash this plugin
bool twoConsecutiveNulls = false;
2020-02-29 18:03:35 +00:00
for(int i = 0; i < 512; i++)
{
2020-02-29 18:03:35 +00:00
if(i >= imageFilter.GetDataForkStream().Length)
break;
if(testArray[i] == 0)
{
2020-02-29 18:03:35 +00:00
if(twoConsecutiveNulls)
return false;
twoConsecutiveNulls = true;
}
2020-02-29 18:03:35 +00:00
else
twoConsecutiveNulls = false;
2020-02-29 18:03:35 +00:00
if(testArray[i] < 0x20 &&
testArray[i] != 0x0A &&
testArray[i] != 0x0D &&
testArray[i] != 0x00)
return false;
}
2020-07-20 21:11:32 +01:00
_tocStream = new StreamReader(imageFilter.GetDataForkStream());
2020-02-29 18:03:35 +00:00
var cr = new Regex(REGEX_COMMENT);
var dr = new Regex(REGEX_DISCTYPE);
2020-07-20 21:11:32 +01:00
while(_tocStream.Peek() >= 0)
{
2020-07-20 21:11:32 +01:00
string line = _tocStream.ReadLine();
Match dm = dr.Match(line ?? throw new InvalidOperationException());
Match cm = cr.Match(line);
// Skip comments at start of file
2020-02-29 18:03:35 +00:00
if(cm.Success)
continue;
return dm.Success;
}
return false;
}
catch(Exception ex)
{
2020-07-20 21:11:32 +01:00
AaruConsole.ErrorWriteLine("Exception trying to identify image file {0}", _cdrdaoFilter.GetFilename());
2020-02-29 18:03:35 +00:00
AaruConsole.ErrorWriteLine("Exception: {0}", ex.Message);
AaruConsole.ErrorWriteLine("Stack trace: {0}", ex.StackTrace);
return false;
}
}
}
}