Make Nca.SectionExists return false for invalid type

This commit is contained in:
Alex Barney 2019-06-03 16:27:43 -05:00
parent dc8aad1e71
commit fbeaff8d88
3 changed files with 75 additions and 19 deletions

View file

@ -69,7 +69,15 @@ namespace LibHac.Fs.NcaUtils
return Header.HasRightsId ? GetDecryptedTitleKey() : GetDecryptedKey((int)type); return Header.HasRightsId ? GetDecryptedTitleKey() : GetDecryptedKey((int)type);
} }
public bool CanOpenSection(NcaSectionType type) => CanOpenSection(GetSectionIndexFromType(type)); public bool CanOpenSection(NcaSectionType type)
{
if (!TryGetSectionIndexFromType(type, Header.ContentType, out int index))
{
return false;
}
return CanOpenSection(index);
}
public bool CanOpenSection(int index) public bool CanOpenSection(int index)
{ {
@ -87,7 +95,15 @@ namespace LibHac.Fs.NcaUtils
return !Keyset.KeyAreaKeys[keyRevision][Header.KeyAreaKeyIndex].IsEmpty(); return !Keyset.KeyAreaKeys[keyRevision][Header.KeyAreaKeyIndex].IsEmpty();
} }
public bool SectionExists(NcaSectionType type) => SectionExists(GetSectionIndexFromType(type)); public bool SectionExists(NcaSectionType type)
{
if (!TryGetSectionIndexFromType(type, Header.ContentType, out int index))
{
return false;
}
return SectionExists(index);
}
public bool SectionExists(int index) public bool SectionExists(int index)
{ {
@ -311,30 +327,70 @@ namespace LibHac.Fs.NcaUtils
private int GetSectionIndexFromType(NcaSectionType type) private int GetSectionIndexFromType(NcaSectionType type)
{ {
return SectionIndexFromType(type, Header.ContentType); return GetSectionIndexFromType(type, Header.ContentType);
} }
public static int SectionIndexFromType(NcaSectionType type, ContentType contentType) public static int GetSectionIndexFromType(NcaSectionType type, ContentType contentType)
{
if (!TryGetSectionIndexFromType(type, contentType, out int index))
{
throw new ArgumentOutOfRangeException(nameof(type), "NCA does not contain this section type.");
}
return index;
}
public static bool TryGetSectionIndexFromType(NcaSectionType type, ContentType contentType, out int index)
{ {
switch (type) switch (type)
{ {
case NcaSectionType.Code when contentType == ContentType.Program: return 0; case NcaSectionType.Code when contentType == ContentType.Program:
case NcaSectionType.Data when contentType == ContentType.Program: return 1; index = 0;
case NcaSectionType.Logo when contentType == ContentType.Program: return 2; return true;
case NcaSectionType.Data: return 0; case NcaSectionType.Data when contentType == ContentType.Program:
default: throw new ArgumentOutOfRangeException(nameof(type), "NCA does not contain this section type."); index = 1;
return true;
case NcaSectionType.Logo when contentType == ContentType.Program:
index = 2;
return true;
case NcaSectionType.Data:
index = 0;
return true;
default:
index = 0;
return false;
} }
} }
public static NcaSectionType SectionTypeFromIndex(int index, ContentType contentType) public static NcaSectionType GetSectionTypeFromIndex(int index, ContentType contentType)
{
if (!TryGetSectionTypeFromIndex(index, contentType, out NcaSectionType type))
{
throw new ArgumentOutOfRangeException(nameof(type), "NCA type does not contain this index.");
}
return type;
}
public static bool TryGetSectionTypeFromIndex(int index, ContentType contentType, out NcaSectionType type)
{ {
switch (index) switch (index)
{ {
case 0 when contentType == ContentType.Program: return NcaSectionType.Code; case 0 when contentType == ContentType.Program:
case 1 when contentType == ContentType.Program: return NcaSectionType.Data; type = NcaSectionType.Code;
case 2 when contentType == ContentType.Program: return NcaSectionType.Logo; return true;
case 0: return NcaSectionType.Data; case 1 when contentType == ContentType.Program:
default: throw new ArgumentOutOfRangeException(nameof(index), "NCA type does not contain this index."); type = NcaSectionType.Data;
return true;
case 2 when contentType == ContentType.Program:
type = NcaSectionType.Logo;
return true;
case 0:
type = NcaSectionType.Data;
return true;
default:
type = default;
return false;
} }
} }

View file

@ -264,12 +264,12 @@ namespace LibHac
public IStorage OpenStorage(NcaSectionType type, IntegrityCheckLevel integrityCheckLevel) public IStorage OpenStorage(NcaSectionType type, IntegrityCheckLevel integrityCheckLevel)
{ {
return OpenStorage(Nca.SectionIndexFromType(type, Nca.Header.ContentType), integrityCheckLevel); return OpenStorage(Nca.GetSectionIndexFromType(type, Nca.Header.ContentType), integrityCheckLevel);
} }
public IFileSystem OpenFileSystem(NcaSectionType type, IntegrityCheckLevel integrityCheckLevel) public IFileSystem OpenFileSystem(NcaSectionType type, IntegrityCheckLevel integrityCheckLevel)
{ {
return OpenFileSystem(Nca.SectionIndexFromType(type, Nca.Header.ContentType), integrityCheckLevel); return OpenFileSystem(Nca.GetSectionIndexFromType(type, Nca.Header.ContentType), integrityCheckLevel);
} }
public Validity VerifyNca(IProgressReport logger = null, bool quiet = false) public Validity VerifyNca(IProgressReport logger = null, bool quiet = false)

View file

@ -166,12 +166,12 @@ namespace hactoolnet
IStorage OpenStorageByType(NcaSectionType type) IStorage OpenStorageByType(NcaSectionType type)
{ {
return OpenStorage(Nca.SectionIndexFromType(type, nca.Header.ContentType)); return OpenStorage(Nca.GetSectionIndexFromType(type, nca.Header.ContentType));
} }
IFileSystem OpenFileSystemByType(NcaSectionType type) IFileSystem OpenFileSystemByType(NcaSectionType type)
{ {
return OpenFileSystem(Nca.SectionIndexFromType(type, nca.Header.ContentType)); return OpenFileSystem(Nca.GetSectionIndexFromType(type, nca.Header.ContentType));
} }
} }
} }