Files
Aaru/Aaru.Images/GDI/Identify.cs

122 lines
4.4 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 Dreamcast GDI disc 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/>.
//
// ----------------------------------------------------------------------------
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 Gdi
{
2021-09-21 04:55:28 +01:00
// Due to .gdi format, this method must parse whole file, ignoring errors (those will be returned by OpenImage()).
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
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
_gdiStream = new StreamReader(imageFilter.GetDataForkStream());
int lineNumber = 0;
int tracksFound = 0;
int tracks = 0;
2020-07-20 21:11:32 +01:00
while(_gdiStream.Peek() >= 0)
{
lineNumber++;
2020-07-20 21:11:32 +01:00
string line = _gdiStream.ReadLine();
if(lineNumber == 1)
{
2020-02-29 18:03:35 +00:00
if(!int.TryParse(line, out tracks))
return false;
}
else
{
2020-02-29 18:03:35 +00:00
var regexTrack = new Regex(REGEX_TRACK);
2021-09-21 04:55:28 +01:00
Match trackMatch = regexTrack.Match(line ?? "");
2020-02-29 18:03:35 +00:00
if(!trackMatch.Success)
return false;
tracksFound++;
}
}
2020-02-29 18:03:35 +00:00
if(tracks == 0)
return false;
return tracks == tracksFound;
}
catch(Exception ex)
{
AaruConsole.ErrorWriteLine("Exception trying to identify image file {0}", imageFilter.BasePath);
2020-02-29 18:03:35 +00:00
AaruConsole.ErrorWriteLine("Exception: {0}", ex.Message);
AaruConsole.ErrorWriteLine("Stack trace: {0}", ex.StackTrace);
return false;
}
}
}
}