Files
SabreTools/RombaSharp/Features/Lookup.cs

131 lines
4.3 KiB
C#
Raw Normal View History

2020-08-01 13:25:32 -07:00
using System.Collections.Generic;
2020-12-08 13:23:59 -08:00
using SabreTools.Core;
2020-12-07 13:57:26 -08:00
using SabreTools.Help;
2020-08-01 13:25:32 -07:00
using Microsoft.Data.Sqlite;
namespace RombaSharp.Features
{
internal class Lookup : BaseFeature
{
public const string Value = "Lookup";
public Lookup()
{
Name = Value;
Flags = ["lookup"];
2020-08-01 13:25:32 -07:00
Description = "For each specified hash it looks up any available information.";
2020-12-07 13:57:26 -08:00
_featureType = ParameterType.Flag;
2020-08-01 13:25:32 -07:00
LongDescription = "For each specified hash it looks up any available information (dat or rom).";
Features = [];
2020-08-01 13:25:32 -07:00
2021-02-03 11:10:19 -08:00
// Common Features
AddCommonFeatures();
2020-08-01 13:25:32 -07:00
AddFeature(SizeInt64Input); // Defaults to -1
AddFeature(OutStringInput);
}
public override bool ProcessFeatures(Dictionary<string, Feature?> features)
2020-08-01 13:25:32 -07:00
{
2021-03-19 20:52:11 -07:00
// If the base fails, just fail out
if (!base.ProcessFeatures(features))
return false;
2020-08-01 13:25:32 -07:00
// Get feature flags
long size = GetInt64(features, SizeInt64Value);
string? outdat = GetString(features, OutStringValue);
2020-08-01 13:25:32 -07:00
// First, try to figure out what type of hash each is by length and clean it
List<string> crc = [];
List<string> md5 = [];
List<string> sha1 = [];
2020-08-01 13:25:32 -07:00
foreach (string input in Inputs)
{
if (input.Length == Constants.CRCLength)
crc.Add(input);
2020-08-01 13:25:32 -07:00
else if (input.Length == Constants.MD5Length)
md5.Add(input);
2020-08-01 13:25:32 -07:00
else if (input.Length == Constants.SHA1Length)
sha1.Add(input);
2020-08-01 13:25:32 -07:00
}
SqliteConnection dbc = new SqliteConnection(_connectionString);
dbc.Open();
// Now, search for each of them and return true or false for each
foreach (string input in crc)
{
string query = $"SELECT * FROM crc WHERE crc=\"{input}\"";
SqliteCommand slc = new SqliteCommand(query, dbc);
SqliteDataReader sldr = slc.ExecuteReader();
if (sldr.HasRows)
{
int count = 0;
while (sldr.Read())
{
count++;
}
logger.User($"For hash '{input}' there were {count} matches in the database");
2020-08-01 13:25:32 -07:00
}
else
{
logger.User($"Hash '{input}' had no matches in the database");
2020-08-01 13:25:32 -07:00
}
sldr.Dispose();
slc.Dispose();
}
foreach (string input in md5)
{
string query = $"SELECT * FROM md5 WHERE md5=\"{input}\"";
SqliteCommand slc = new SqliteCommand(query, dbc);
SqliteDataReader sldr = slc.ExecuteReader();
if (sldr.HasRows)
{
int count = 0;
while (sldr.Read())
{
count++;
}
logger.User($"For hash '{input}' there were {count} matches in the database");
2020-08-01 13:25:32 -07:00
}
else
{
logger.User($"Hash '{input}' had no matches in the database");
2020-08-01 13:25:32 -07:00
}
sldr.Dispose();
slc.Dispose();
}
foreach (string input in sha1)
{
string query = $"SELECT * FROM sha1 WHERE sha1=\"{input}\"";
SqliteCommand slc = new SqliteCommand(query, dbc);
SqliteDataReader sldr = slc.ExecuteReader();
if (sldr.HasRows)
{
int count = 0;
while (sldr.Read())
{
count++;
}
logger.User($"For hash '{input}' there were {count} matches in the database");
2020-08-01 13:25:32 -07:00
}
else
{
logger.User($"Hash '{input}' had no matches in the database");
2020-08-01 13:25:32 -07:00
}
sldr.Dispose();
slc.Dispose();
}
dbc.Dispose();
2021-03-19 20:52:11 -07:00
return true;
2020-08-01 13:25:32 -07:00
}
}
}