Address some analyzer findings

This commit is contained in:
Alex Barney 2023-02-05 22:43:08 -07:00
parent d2477356fc
commit a38d94fee3
17 changed files with 75 additions and 72 deletions

View file

@ -229,7 +229,7 @@ public class Package1
}
// If encrypted, check if the body can be decrypted
Crypto.Aes.DecryptCbc128(metaData2, metaData2, KeySet.MarikoBek, _metaData.Iv);
Aes.DecryptCbc128(metaData2, metaData2, KeySet.MarikoBek, _metaData.Iv);
IsDecrypted = metaData2.SequenceEqual(SpanHelpers.AsByteSpan(ref _metaData));
// Get a decrypted body storage if we have the correct key
@ -344,7 +344,7 @@ public class Package1
int start = IsModern ? 6 : 0;
int end = IsModern ? 0x20 : 6;
Decryptor decryptor = IsModern ? Crypto.Aes.DecryptCbc128 : Crypto.Aes.DecryptCtr128;
Decryptor decryptor = IsModern ? Aes.DecryptCbc128 : Aes.DecryptCtr128;
for (int i = start; i < end; i++)
{

View file

@ -161,15 +161,15 @@ public static class Utilities
return text;
}
public static void WriteUTF8(this BinaryWriter writer, string value)
public static void WriteUtf8(this BinaryWriter writer, string value)
{
byte[] text = Encoding.UTF8.GetBytes(value);
writer.Write(text);
}
public static void WriteUTF8Z(this BinaryWriter writer, string value)
public static void WriteUtf8Z(this BinaryWriter writer, string value)
{
writer.WriteUTF8(value);
writer.WriteUtf8(value);
writer.Write((byte)0);
}

View file

@ -26,7 +26,6 @@ public class FileSystemServer : IDisposable
public void Dispose()
{
Globals.Dispose();
Globals = default;
}
}

View file

@ -15,6 +15,7 @@ using LibHac.Tools.FsSystem.NcaUtils;
using LibHac.Util;
using static LibHac.Fs.Impl.CommonMountNames;
using NcaFsHeader = LibHac.Tools.FsSystem.NcaUtils.NcaFsHeader;
using NcaHeader = LibHac.FsSystem.NcaHeader;
using RightsId = LibHac.Fs.RightsId;
using Utility = LibHac.FsSystem.Utility;
@ -698,7 +699,7 @@ public class NcaFileSystemServiceImpl
{
UnsafeHelpers.SkipParamInit(out fsType);
NcaContentType contentType = nca.Header.ContentType;
NcaHeader.ContentType contentType = (NcaHeader.ContentType)nca.Header.ContentType;
switch (fsProxyType)
{
@ -706,31 +707,31 @@ public class NcaFileSystemServiceImpl
case FileSystemProxyType.Rom:
case FileSystemProxyType.Logo:
case FileSystemProxyType.RegisteredUpdate:
if (contentType != NcaContentType.Program)
if (contentType != NcaHeader.ContentType.Program)
return ResultFs.PreconditionViolation.Log();
break;
case FileSystemProxyType.Control:
if (contentType != NcaContentType.Control)
if (contentType != NcaHeader.ContentType.Control)
return ResultFs.PreconditionViolation.Log();
break;
case FileSystemProxyType.Manual:
if (contentType != NcaContentType.Manual)
if (contentType != NcaHeader.ContentType.Manual)
return ResultFs.PreconditionViolation.Log();
break;
case FileSystemProxyType.Meta:
if (contentType != NcaContentType.Meta)
if (contentType != NcaHeader.ContentType.Meta)
return ResultFs.PreconditionViolation.Log();
break;
case FileSystemProxyType.Data:
if (contentType != NcaContentType.Data && contentType != NcaContentType.PublicData)
if (contentType != NcaHeader.ContentType.Data && contentType != NcaHeader.ContentType.PublicData)
return ResultFs.PreconditionViolation.Log();
if (contentType == NcaContentType.Data && !canMountSystemDataPrivate)
if (contentType == NcaHeader.ContentType.Data && !canMountSystemDataPrivate)
return ResultFs.PermissionDenied.Log();
break;

View file

@ -239,7 +239,7 @@ public unsafe class FileSystemBuddyHeap : IDisposable
// Determine page sizes
nuint maxPageSize = BlockSize << OrderMax;
nuint maxPageCount = (nuint)Alignment.AlignUp(HeapSize, (uint)maxPageSize) / maxPageSize;
nuint maxPageCount = Alignment.AlignUp(HeapSize, (uint)maxPageSize) / maxPageSize;
Assert.SdkGreater(maxPageCount, nuint.Zero);
// Setup the free lists

View file

@ -1,47 +0,0 @@
namespace LibHac.FsSystem;
public enum NcaSectionType
{
Code,
Data,
Logo
}
public enum NcaContentType
{
Program,
Meta,
Control,
Manual,
Data,
PublicData
}
public enum DistributionType
{
Download,
GameCard
}
public enum NcaEncryptionType
{
Auto,
None,
XTS,
AesCtr,
AesCtrEx
}
public enum NcaHashType
{
Auto,
None,
Sha256,
Ivfc
}
public enum NcaFormatType
{
Romfs,
Pfs0
}

View file

@ -126,7 +126,7 @@ public class Ticket
}
stream.Position = bodyStart;
if (Issuer != null) writer.WriteUTF8(Issuer);
if (Issuer != null) writer.WriteUtf8(Issuer);
stream.Position = bodyStart + 0x40;
if (TitleKeyBlock?.Length <= 0x100) writer.Write(TitleKeyBlock);
stream.Position = bodyStart + 0x140;

View file

@ -229,7 +229,7 @@ public class Nca
{
case NcaEncryptionType.None:
return baseStorage;
case NcaEncryptionType.XTS:
case NcaEncryptionType.AesXts:
return OpenAesXtsStorage(baseStorage, index, decrypting);
case NcaEncryptionType.AesCtr:
return OpenAesCtrStorage(baseStorage, index, Header.GetSectionStartOffset(index), header.Counter);

View file

@ -5,7 +5,6 @@ using LibHac.Common;
using LibHac.Crypto;
using LibHac.Fs;
using LibHac.Fs.Fsa;
using LibHac.FsSystem;
namespace LibHac.Tools.FsSystem.NcaUtils;

View file

@ -7,7 +7,6 @@ using LibHac.Common.Keys;
using LibHac.Crypto;
using LibHac.Diag;
using LibHac.Fs;
using LibHac.FsSystem;
using LibHac.Tools.Crypto;
using LibHac.Util;

View file

@ -32,4 +32,50 @@ public class TitleVersion
{
return $"{Major}.{Minor}.{Patch}.{Revision}";
}
}
public enum NcaSectionType
{
Code,
Data,
Logo
}
public enum NcaContentType
{
Program,
Meta,
Control,
Manual,
Data,
PublicData
}
public enum DistributionType
{
Download,
GameCard
}
public enum NcaEncryptionType
{
Auto,
None,
AesXts,
AesCtr,
AesCtrEx
}
public enum NcaHashType
{
Auto,
None,
Sha256,
Ivfc
}
public enum NcaFormatType
{
Romfs,
Pfs0
}

View file

@ -82,7 +82,7 @@ public class PartitionFileSystemBuilder
byte[] metaData = new byte[metaDataSize];
var writer = new BinaryWriter(new MemoryStream(metaData));
writer.WriteUTF8(GetMagicValue(type));
writer.WriteUtf8(GetMagicValue(type));
writer.Write(Entries.Count);
writer.Write(stringTableSize);
writer.Write(0);
@ -111,7 +111,7 @@ public class PartitionFileSystemBuilder
foreach (Entry entry in Entries)
{
writer.WriteUTF8Z(entry.Name);
writer.WriteUtf8Z(entry.Name);
}
return metaData;

View file

@ -264,7 +264,9 @@ public class SaveDataFileSystem : IFileSystem
byte[] hashData = new byte[0x3d00];
headerStream.Position = 0x300;
headerStream.Read(hashData, 0, hashData.Length);
int bytesRead = headerStream.Read(hashData, 0, hashData.Length);
if (bytesRead != hashData.Length)
return ResultFs.OutOfRange.Log();
byte[] hash = new byte[Sha256.DigestSize];
Sha256.GenerateSha256Hash(hashData, hash);
@ -278,7 +280,9 @@ public class SaveDataFileSystem : IFileSystem
byte[] cmac = new byte[0x10];
headerStream.Position = 0x100;
headerStream.Read(cmacData, 0, 0x200);
bytesRead = headerStream.Read(cmacData, 0, cmacData.Length);
if (bytesRead != cmacData.Length)
return ResultFs.OutOfRange.Log();
Aes.CalculateCmac(cmac, cmacData, keySet.DeviceUniqueSaveMacKeys[0]);

View file

@ -29,7 +29,9 @@ public class StreamStorage : IStorage
BaseStream.Position = offset;
}
BaseStream.Read(destination);
int bytesRead = BaseStream.Read(destination);
if (bytesRead != destination.Length)
return ResultFs.OutOfRange.Log();
}
return Result.Success;

View file

@ -1,8 +1,8 @@
using LibHac.Common;
using LibHac.Fs;
using LibHac.FsSystem;
using LibHac.Ncm;
using LibHac.Tools.Fs;
using LibHac.Tools.FsSystem.NcaUtils;
using ContentType = LibHac.Ncm.ContentType;
namespace hactoolnet;

View file

@ -2,7 +2,6 @@
using LibHac.Common;
using LibHac.Fs;
using LibHac.Fs.Fsa;
using LibHac.FsSystem;
using LibHac.Ncm;
using LibHac.Spl;
using LibHac.Tools.Es;

View file

@ -11,6 +11,7 @@ using LibHac.FsSystem;
using LibHac.Ns;
using LibHac.Tools.Fs;
using LibHac.Tools.FsSystem;
using LibHac.Tools.FsSystem.NcaUtils;
using LibHac.Tools.FsSystem.Save;
using Path = System.IO.Path;