Parse title key files with pipes as delimiters

This commit is contained in:
Alex Barney 2018-09-20 21:15:27 -05:00
parent 53680f3e5c
commit 8110000737
2 changed files with 71 additions and 25 deletions

View file

@ -1,5 +1,4 @@
 using System;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
@ -350,11 +349,31 @@ namespace LibHac
string line; string line;
while ((line = reader.ReadLine()) != null) while ((line = reader.ReadLine()) != null)
{ {
var a = line.Split(',', '='); string[] splitLine;
if (a.Length != 2) continue;
var rightsId = a[0].Trim().ToBytes(); // Some people use pipes as delimiters
var titleKey = a[1].Trim().ToBytes(); if (line.Contains('|'))
{
splitLine = line.Split('|');
}
else
{
splitLine = line.Split(',', '=');
}
if (splitLine.Length < 2) continue;
if(!splitLine[0].Trim().TryToBytes(out byte[] rightsId))
{
progress?.LogMessage($"Invalid rights ID \"{splitLine[0].Trim()}\" in title key file");
continue;
}
if(!splitLine[1].Trim().TryToBytes(out byte[] titleKey))
{
progress?.LogMessage($"Invalid title key \"{splitLine[1].Trim()}\" in title key file");
continue;
}
if (rightsId.Length != TitleKeySize) if (rightsId.Length != TitleKeySize)
{ {

View file

@ -161,51 +161,54 @@ namespace LibHac
return fullFile.Substring(fullDirectory.Length + 1); return fullFile.Substring(fullDirectory.Length + 1);
} }
private static int HexToInt(char c) private static bool TryHexToInt(char c, out int value)
{ {
switch (c) switch (c)
{ {
case '0': case '0':
return 0; value = 0; break;
case '1': case '1':
return 1; value = 1; break;
case '2': case '2':
return 2; value = 2; break;
case '3': case '3':
return 3; value = 3; break;
case '4': case '4':
return 4; value = 4; break;
case '5': case '5':
return 5; value = 5; break;
case '6': case '6':
return 6; value = 6; break;
case '7': case '7':
return 7; value = 7; break;
case '8': case '8':
return 8; value = 8; break;
case '9': case '9':
return 9; value = 9; break;
case 'a': case 'a':
case 'A': case 'A':
return 10; value = 10; break;
case 'b': case 'b':
case 'B': case 'B':
return 11; value = 11; break;
case 'c': case 'c':
case 'C': case 'C':
return 12; value = 12; break;
case 'd': case 'd':
case 'D': case 'D':
return 13; value = 13; break;
case 'e': case 'e':
case 'E': case 'E':
return 14; value = 14; break;
case 'f': case 'f':
case 'F': case 'F':
return 15; value = 15; break;
default: default:
throw new FormatException("Unrecognized hex char " + c); value = 0;
return false;
} }
return true;
} }
private static readonly byte[,] ByteLookup = { private static readonly byte[,] ByteLookup = {
@ -220,11 +223,35 @@ namespace LibHac
int lastchar = input.Length - 1; int lastchar = input.Length - 1;
for (int i = 0; i < input.Length; i++) for (int i = 0; i < input.Length; i++)
{ {
result[lastcell - (i >> 1)] |= ByteLookup[i & 1, HexToInt(input[lastchar - i])]; if (!TryHexToInt(input[lastchar - i], out int hexInt))
{
throw new FormatException($"Unrecognized hex char {input[lastchar - i]}");
}
result[lastcell - (i >> 1)] |= ByteLookup[i & 1, hexInt];
} }
return result; return result;
} }
public static bool TryToBytes(this string input, out byte[] bytes)
{
var result = new byte[(input.Length + 1) >> 1];
int lastcell = result.Length - 1;
int lastchar = input.Length - 1;
for (int i = 0; i < input.Length; i++)
{
if (!TryHexToInt(input[lastchar - i], out int hexInt))
{
bytes = null;
return false;
}
result[lastcell - (i >> 1)] |= ByteLookup[i & 1, hexInt];
}
bytes = result;
return true;
}
private static readonly uint[] Lookup32 = CreateLookup32(); private static readonly uint[] Lookup32 = CreateLookup32();
private static uint[] CreateLookup32() private static uint[] CreateLookup32()