78 lines
3.0 KiB
C#
78 lines
3.0 KiB
C#
using System.Security.Cryptography;
|
|
using System.Text;
|
|
|
|
namespace TuneBoss.Core.IO;
|
|
|
|
/// <summary>
|
|
/// The on-disk envelope of a TuneBoss <c>.tst</c> map file:
|
|
///
|
|
/// MD5(tag)_____tag\nMD5(data)_____data
|
|
///
|
|
/// Two records separated by a newline; each record is <c>MD5hex_____payload</c>.
|
|
/// The MD5 is upper-case hex with no separators (matching the legacy
|
|
/// <c>BitConverter.ToString(...).Replace("-","")</c>). On load, both digests are
|
|
/// recomputed and must match or the file is rejected as corrupt/tampered.
|
|
/// </summary>
|
|
public static class MapFile
|
|
{
|
|
public const string Extension = ".tst";
|
|
public const string RecordSep = "_____";
|
|
|
|
/// <summary>Upper-case, separator-free MD5 hex of a UTF-8 string.</summary>
|
|
public static string Md5Hex(string s)
|
|
{
|
|
var hash = MD5.HashData(Encoding.UTF8.GetBytes(s));
|
|
return Convert.ToHexString(hash); // upper-case, no separators
|
|
}
|
|
|
|
/// <summary>Builds the full file text from a tag string and a data string.</summary>
|
|
public static string BuildEnvelope(string tag, string data)
|
|
=> $"{Md5Hex(tag)}{RecordSep}{tag}\n{Md5Hex(data)}{RecordSep}{data}";
|
|
|
|
public readonly record struct ParseResult(
|
|
bool Ok,
|
|
string Tag,
|
|
string Data,
|
|
string? Error)
|
|
{
|
|
public static ParseResult Fail(string error) => new(false, "", "", error);
|
|
public static ParseResult Success(string tag, string data) => new(true, tag, data, null);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Parses and integrity-checks a file's full text. Splits on <c>\r\n</c> or
|
|
/// <c>\n</c>, separates each record on <c>_____</c>, and verifies both MD5s.
|
|
/// </summary>
|
|
public static ParseResult TryParse(string fileText)
|
|
{
|
|
if (string.IsNullOrEmpty(fileText))
|
|
return ParseResult.Fail("File is empty.");
|
|
|
|
var lines = fileText.Split(new[] { "\r\n", "\n" }, StringSplitOptions.None);
|
|
if (lines.Length < 2)
|
|
return ParseResult.Fail("File is not a valid TuneBoss map (missing data record).");
|
|
|
|
var tagRec = SplitOnce(lines[0]);
|
|
var dataRec = SplitOnce(lines[1]);
|
|
if (tagRec is null || dataRec is null)
|
|
return ParseResult.Fail("File is not a valid TuneBoss map (bad record format).");
|
|
|
|
var (tagHash, tag) = tagRec.Value;
|
|
var (dataHash, data) = dataRec.Value;
|
|
|
|
if (!string.Equals(tagHash, Md5Hex(tag), StringComparison.OrdinalIgnoreCase))
|
|
return ParseResult.Fail("Tag checksum mismatch — the file may be corrupt or edited.");
|
|
if (!string.Equals(dataHash, Md5Hex(data), StringComparison.OrdinalIgnoreCase))
|
|
return ParseResult.Fail("Data checksum mismatch — the file may be corrupt or edited.");
|
|
|
|
return ParseResult.Success(tag, data);
|
|
}
|
|
|
|
private static (string Hash, string Payload)? SplitOnce(string record)
|
|
{
|
|
int i = record.IndexOf(RecordSep, StringComparison.Ordinal);
|
|
if (i < 0) return null;
|
|
return (record[..i], record[(i + RecordSep.Length)..]);
|
|
}
|
|
}
|