From a88b3058e98ddfa46fb059651f197e32217bec4f Mon Sep 17 00:00:00 2001 From: Alex Barney Date: Sat, 18 Dec 2021 19:03:53 -0700 Subject: [PATCH] Move Ticket, SwitchFs, Cnmt, Nro, Xci again --- src/LibHac/BitReader.cs | 132 -------------------------- src/LibHac/BitTools.cs | 10 -- src/LibHac/FsSystem/FileReader.cs | 10 +- src/LibHac/{ => Tools/Es}/Ticket.cs | 4 +- src/LibHac/{ => Tools/Fs}/SwitchFs.cs | 19 ++-- src/LibHac/Tools/Fs/Xci.cs | 3 +- src/LibHac/{ => Tools/Ncm}/Cnmt.cs | 4 +- src/LibHac/{ => Tools/Ro}/Nro.cs | 4 +- src/hactoolnet/ProcessPfs.cs | 5 +- src/hactoolnet/ProcessSwitchFs.cs | 3 +- 10 files changed, 30 insertions(+), 164 deletions(-) delete mode 100644 src/LibHac/BitReader.cs delete mode 100644 src/LibHac/BitTools.cs rename src/LibHac/{ => Tools/Es}/Ticket.cs (99%) rename src/LibHac/{ => Tools/Fs}/SwitchFs.cs (96%) rename src/LibHac/{ => Tools/Ncm}/Cnmt.cs (99%) rename src/LibHac/{ => Tools/Ro}/Nro.cs (99%) diff --git a/src/LibHac/BitReader.cs b/src/LibHac/BitReader.cs deleted file mode 100644 index 7b768675..00000000 --- a/src/LibHac/BitReader.cs +++ /dev/null @@ -1,132 +0,0 @@ -using System; -using System.Diagnostics; - -namespace LibHac; - -public class BitReader -{ - public byte[] Buffer { get; private set; } - public int LengthBits { get; private set; } - public int Position { get; set; } - public int Remaining => LengthBits - Position; - - public BitReader(byte[] buffer) => SetBuffer(buffer); - - public void SetBuffer(byte[] buffer) - { - Buffer = buffer; - LengthBits = Buffer?.Length * 8 ?? 0; - Position = 0; - } - - public int ReadInt(int bitCount) - { - int value = PeekInt(bitCount); - Position += bitCount; - return value; - } - - //public int ReadSignedInt(int bitCount) - //{ - // int value = PeekInt(bitCount); - // Position += bitCount; - // return Bit.SignExtend32(value, bitCount); - //} - - public bool ReadBool() => ReadInt(1) == 1; - - public int ReadOffsetBinary(int bitCount, OffsetBias bias) - { - int offset = (1 << (bitCount - 1)) - (int)bias; - int value = PeekInt(bitCount) - offset; - Position += bitCount; - return value; - } - - //public void AlignPosition(int multiple) - //{ - // Position = Helpers.GetNextMultiple(Position, multiple); - //} - - public int PeekInt(int bitCount) - { - Debug.Assert(bitCount >= 0 && bitCount <= 32); - - if (bitCount > Remaining) - { - if (Position >= LengthBits) return 0; - - int extraBits = bitCount - Remaining; - return PeekIntFallback(Remaining) << extraBits; - } - - int byteIndex = Position / 8; - int bitIndex = Position % 8; - - if (bitCount <= 9 && Remaining >= 16) - { - int value = Buffer[byteIndex] << 8 | Buffer[byteIndex + 1]; - value &= 0xFFFF >> bitIndex; - value >>= 16 - bitCount - bitIndex; - return value; - } - - if (bitCount <= 17 && Remaining >= 24) - { - int value = Buffer[byteIndex] << 16 | Buffer[byteIndex + 1] << 8 | Buffer[byteIndex + 2]; - value &= 0xFFFFFF >> bitIndex; - value >>= 24 - bitCount - bitIndex; - return value; - } - - if (bitCount <= 25 && Remaining >= 32) - { - int value = Buffer[byteIndex] << 24 | Buffer[byteIndex + 1] << 16 | Buffer[byteIndex + 2] << 8 | Buffer[byteIndex + 3]; - value &= (int)(0xFFFFFFFF >> bitIndex); - value >>= 32 - bitCount - bitIndex; - return value; - } - return PeekIntFallback(bitCount); - } - - private int PeekIntFallback(int bitCount) - { - int value = 0; - int byteIndex = Position / 8; - int bitIndex = Position % 8; - - while (bitCount > 0) - { - if (bitIndex >= 8) - { - bitIndex = 0; - byteIndex++; - } - - int bitsToRead = Math.Min(bitCount, 8 - bitIndex); - int mask = 0xFF >> bitIndex; - int currentByte = (mask & Buffer[byteIndex]) >> (8 - bitIndex - bitsToRead); - - value = (value << bitsToRead) | currentByte; - bitIndex += bitsToRead; - bitCount -= bitsToRead; - } - return value; - } - - /// - /// Specifies the bias of an offset binary value. A positive bias can represent one more - /// positive value than negative value, and a negative bias can represent one more - /// negative value than positive value. - /// - /// Example: - /// A 4-bit offset binary value with a positive bias can store - /// the values 8 through -7 inclusive. - /// A 4-bit offset binary value with a negative bias can store - /// the values 7 through -8 inclusive. - public enum OffsetBias - { - Positive = 1, - Negative = 0 - } -} diff --git a/src/LibHac/BitTools.cs b/src/LibHac/BitTools.cs deleted file mode 100644 index 2afefc7d..00000000 --- a/src/LibHac/BitTools.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace LibHac; - -public static class BitTools -{ - public static int SignExtend32(int value, int bits) - { - int shift = 8 * sizeof(int) - bits; - return (value << shift) >> shift; - } -} diff --git a/src/LibHac/FsSystem/FileReader.cs b/src/LibHac/FsSystem/FileReader.cs index 74456dd4..23f26267 100644 --- a/src/LibHac/FsSystem/FileReader.cs +++ b/src/LibHac/FsSystem/FileReader.cs @@ -75,7 +75,7 @@ public class FileReader { FillBuffer(offset, 3, updatePosition); - return BitTools.SignExtend32(MemoryMarshal.Read(_buffer), 24); + return SignExtend32(MemoryMarshal.Read(_buffer), 24); } public uint ReadUInt32(long offset, bool updatePosition) @@ -176,4 +176,10 @@ public class FileReader public byte[] ReadBytes(int length) => ReadBytes(Position, length, true); public void ReadBytes(Span destination) => ReadBytes(destination, Position, true); public string ReadAscii(int length) => ReadAscii(Position, length, true); -} + + public static int SignExtend32(int value, int bits) + { + int shift = 8 * sizeof(int) - bits; + return (value << shift) >> shift; + } +} \ No newline at end of file diff --git a/src/LibHac/Ticket.cs b/src/LibHac/Tools/Es/Ticket.cs similarity index 99% rename from src/LibHac/Ticket.cs rename to src/LibHac/Tools/Es/Ticket.cs index 1b45143c..664b4eaa 100644 --- a/src/LibHac/Ticket.cs +++ b/src/LibHac/Tools/Es/Ticket.cs @@ -3,7 +3,7 @@ using System.IO; using LibHac.Common.Keys; using LibHac.Util; -namespace LibHac; +namespace LibHac.Tools.Es; public class Ticket { @@ -191,4 +191,4 @@ public enum PropertyFlags PreInstall = 1 << 0, SharedTitle = 1 << 1, AllowAllContent = 1 << 2 -} +} \ No newline at end of file diff --git a/src/LibHac/SwitchFs.cs b/src/LibHac/Tools/Fs/SwitchFs.cs similarity index 96% rename from src/LibHac/SwitchFs.cs rename to src/LibHac/Tools/Fs/SwitchFs.cs index 531cd2c7..062b44d0 100644 --- a/src/LibHac/SwitchFs.cs +++ b/src/LibHac/Tools/Fs/SwitchFs.cs @@ -12,9 +12,10 @@ using LibHac.FsSystem.NcaUtils; using LibHac.FsSystem.Save; using LibHac.Ncm; using LibHac.Ns; +using LibHac.Tools.Ncm; using LibHac.Util; -namespace LibHac; +namespace LibHac.Tools.Fs; public class SwitchFs : IDisposable { @@ -44,10 +45,10 @@ public class SwitchFs : IDisposable { var concatFs = new ConcatenationFileSystem(fileSystem); - using var contentDirPath = new Fs.Path(); + using var contentDirPath = new LibHac.Fs.Path(); PathFunctions.SetUpFixedPath(ref contentDirPath.Ref(), "/Nintendo/Contents".ToU8String()).ThrowIfFailure(); - using var saveDirPath = new Fs.Path(); + using var saveDirPath = new LibHac.Fs.Path(); PathFunctions.SetUpFixedPath(ref saveDirPath.Ref(), "/Nintendo/save".ToU8String()).ThrowIfFailure(); var contentDirFs = new SubdirectoryFileSystem(concatFs); @@ -75,14 +76,14 @@ public class SwitchFs : IDisposable if (concatFs.DirectoryExists("/save")) { - using var savePath = new Fs.Path(); + using var savePath = new LibHac.Fs.Path(); PathFunctions.SetUpFixedPath(ref savePath.Ref(), "/save".ToU8String()); saveDirFs = new SubdirectoryFileSystem(concatFs); saveDirFs.Initialize(in savePath).ThrowIfFailure(); } - using var contentsPath = new Fs.Path(); + using var contentsPath = new LibHac.Fs.Path(); PathFunctions.SetUpFixedPath(ref contentsPath.Ref(), "/Contents".ToU8String()); contentDirFs = new SubdirectoryFileSystem(concatFs); @@ -195,11 +196,11 @@ public class SwitchFs : IDisposable switch (content.Type) { - case Ncm.ContentType.Program: - case Ncm.ContentType.Data: + case LibHac.Ncm.ContentType.Program: + case LibHac.Ncm.ContentType.Data: title.MainNca = contentNca; break; - case Ncm.ContentType.Control: + case LibHac.Ncm.ContentType.Control: title.ControlNca = contentNca; break; } @@ -420,4 +421,4 @@ public class Application DisplayVersion = ""; } } -} +} \ No newline at end of file diff --git a/src/LibHac/Tools/Fs/Xci.cs b/src/LibHac/Tools/Fs/Xci.cs index ddc05c3f..eb61b66c 100644 --- a/src/LibHac/Tools/Fs/Xci.cs +++ b/src/LibHac/Tools/Fs/Xci.cs @@ -3,9 +3,8 @@ using LibHac.Common.Keys; using LibHac.Fs; using LibHac.Fs.Fsa; using LibHac.FsSystem; -using LibHac.Tools.Fs; -namespace LibHac; +namespace LibHac.Tools.Fs; public class Xci { diff --git a/src/LibHac/Cnmt.cs b/src/LibHac/Tools/Ncm/Cnmt.cs similarity index 99% rename from src/LibHac/Cnmt.cs rename to src/LibHac/Tools/Ncm/Cnmt.cs index 8b276415..2933bc5a 100644 --- a/src/LibHac/Cnmt.cs +++ b/src/LibHac/Tools/Ncm/Cnmt.cs @@ -4,7 +4,7 @@ using LibHac.FsSystem.NcaUtils; using LibHac.Ncm; using ContentType = LibHac.Ncm.ContentType; -namespace LibHac; +namespace LibHac.Tools.Ncm; public class Cnmt { @@ -322,4 +322,4 @@ public class FragmentMapEntry ContentIndex = reader.ReadInt16(); FragmentIndex = reader.ReadInt16(); } -} +} \ No newline at end of file diff --git a/src/LibHac/Nro.cs b/src/LibHac/Tools/Ro/Nro.cs similarity index 99% rename from src/LibHac/Nro.cs rename to src/LibHac/Tools/Ro/Nro.cs index 0f8ef6bf..caa097eb 100644 --- a/src/LibHac/Nro.cs +++ b/src/LibHac/Tools/Ro/Nro.cs @@ -2,7 +2,7 @@ using LibHac.Fs; using LibHac.FsSystem; -namespace LibHac; +namespace LibHac.Tools.Ro; public class Nro { @@ -153,4 +153,4 @@ public class NroAssetSection FileOffset = (uint)reader.ReadUInt64(); Size = (uint)reader.ReadUInt64(); } -} +} \ No newline at end of file diff --git a/src/hactoolnet/ProcessPfs.cs b/src/hactoolnet/ProcessPfs.cs index 5294f284..57399a7d 100644 --- a/src/hactoolnet/ProcessPfs.cs +++ b/src/hactoolnet/ProcessPfs.cs @@ -1,9 +1,10 @@ using System.IO; using System.Reflection; using System.Text; -using LibHac; using LibHac.Fs; using LibHac.FsSystem; +using LibHac.Tools.Es; +using LibHac.Tools.Fs; using LibHac.Util; using static hactoolnet.Print; @@ -100,4 +101,4 @@ internal static class ProcessPfs builtPfs.CopyToStream(outStream, pfsSize, ctx.Logger); } } -} +} \ No newline at end of file diff --git a/src/hactoolnet/ProcessSwitchFs.cs b/src/hactoolnet/ProcessSwitchFs.cs index a0f542ae..656a9bb6 100644 --- a/src/hactoolnet/ProcessSwitchFs.cs +++ b/src/hactoolnet/ProcessSwitchFs.cs @@ -11,6 +11,7 @@ using LibHac.FsSystem; using LibHac.FsSystem.NcaUtils; using LibHac.FsSystem.Save; using LibHac.Ns; +using LibHac.Tools.Fs; using Path = System.IO.Path; namespace hactoolnet; @@ -339,4 +340,4 @@ internal static class ProcessSwitchFs ctx.Logger.LogMessage($"{file.FullPath}"); } } -} +} \ No newline at end of file