Move Ticket, SwitchFs, Cnmt, Nro, Xci again

This commit is contained in:
Alex Barney 2021-12-18 19:03:53 -07:00
parent da930dfa81
commit a88b3058e9
10 changed files with 30 additions and 164 deletions

View file

@ -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;
}
/// <summary>
/// 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.
/// </summary>
/// <remarks>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.</remarks>
public enum OffsetBias
{
Positive = 1,
Negative = 0
}
}

View file

@ -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;
}
}

View file

@ -75,7 +75,7 @@ public class FileReader
{ {
FillBuffer(offset, 3, updatePosition); FillBuffer(offset, 3, updatePosition);
return BitTools.SignExtend32(MemoryMarshal.Read<int>(_buffer), 24); return SignExtend32(MemoryMarshal.Read<int>(_buffer), 24);
} }
public uint ReadUInt32(long offset, bool updatePosition) public uint ReadUInt32(long offset, bool updatePosition)
@ -176,4 +176,10 @@ public class FileReader
public byte[] ReadBytes(int length) => ReadBytes(Position, length, true); public byte[] ReadBytes(int length) => ReadBytes(Position, length, true);
public void ReadBytes(Span<byte> destination) => ReadBytes(destination, Position, true); public void ReadBytes(Span<byte> destination) => ReadBytes(destination, Position, true);
public string ReadAscii(int length) => ReadAscii(Position, length, 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;
}
} }

View file

@ -3,7 +3,7 @@ using System.IO;
using LibHac.Common.Keys; using LibHac.Common.Keys;
using LibHac.Util; using LibHac.Util;
namespace LibHac; namespace LibHac.Tools.Es;
public class Ticket public class Ticket
{ {

View file

@ -12,9 +12,10 @@ using LibHac.FsSystem.NcaUtils;
using LibHac.FsSystem.Save; using LibHac.FsSystem.Save;
using LibHac.Ncm; using LibHac.Ncm;
using LibHac.Ns; using LibHac.Ns;
using LibHac.Tools.Ncm;
using LibHac.Util; using LibHac.Util;
namespace LibHac; namespace LibHac.Tools.Fs;
public class SwitchFs : IDisposable public class SwitchFs : IDisposable
{ {
@ -44,10 +45,10 @@ public class SwitchFs : IDisposable
{ {
var concatFs = new ConcatenationFileSystem(fileSystem); 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(); 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(); PathFunctions.SetUpFixedPath(ref saveDirPath.Ref(), "/Nintendo/save".ToU8String()).ThrowIfFailure();
var contentDirFs = new SubdirectoryFileSystem(concatFs); var contentDirFs = new SubdirectoryFileSystem(concatFs);
@ -75,14 +76,14 @@ public class SwitchFs : IDisposable
if (concatFs.DirectoryExists("/save")) if (concatFs.DirectoryExists("/save"))
{ {
using var savePath = new Fs.Path(); using var savePath = new LibHac.Fs.Path();
PathFunctions.SetUpFixedPath(ref savePath.Ref(), "/save".ToU8String()); PathFunctions.SetUpFixedPath(ref savePath.Ref(), "/save".ToU8String());
saveDirFs = new SubdirectoryFileSystem(concatFs); saveDirFs = new SubdirectoryFileSystem(concatFs);
saveDirFs.Initialize(in savePath).ThrowIfFailure(); 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()); PathFunctions.SetUpFixedPath(ref contentsPath.Ref(), "/Contents".ToU8String());
contentDirFs = new SubdirectoryFileSystem(concatFs); contentDirFs = new SubdirectoryFileSystem(concatFs);
@ -195,11 +196,11 @@ public class SwitchFs : IDisposable
switch (content.Type) switch (content.Type)
{ {
case Ncm.ContentType.Program: case LibHac.Ncm.ContentType.Program:
case Ncm.ContentType.Data: case LibHac.Ncm.ContentType.Data:
title.MainNca = contentNca; title.MainNca = contentNca;
break; break;
case Ncm.ContentType.Control: case LibHac.Ncm.ContentType.Control:
title.ControlNca = contentNca; title.ControlNca = contentNca;
break; break;
} }

View file

@ -3,9 +3,8 @@ using LibHac.Common.Keys;
using LibHac.Fs; using LibHac.Fs;
using LibHac.Fs.Fsa; using LibHac.Fs.Fsa;
using LibHac.FsSystem; using LibHac.FsSystem;
using LibHac.Tools.Fs;
namespace LibHac; namespace LibHac.Tools.Fs;
public class Xci public class Xci
{ {

View file

@ -4,7 +4,7 @@ using LibHac.FsSystem.NcaUtils;
using LibHac.Ncm; using LibHac.Ncm;
using ContentType = LibHac.Ncm.ContentType; using ContentType = LibHac.Ncm.ContentType;
namespace LibHac; namespace LibHac.Tools.Ncm;
public class Cnmt public class Cnmt
{ {

View file

@ -2,7 +2,7 @@
using LibHac.Fs; using LibHac.Fs;
using LibHac.FsSystem; using LibHac.FsSystem;
namespace LibHac; namespace LibHac.Tools.Ro;
public class Nro public class Nro
{ {

View file

@ -1,9 +1,10 @@
using System.IO; using System.IO;
using System.Reflection; using System.Reflection;
using System.Text; using System.Text;
using LibHac;
using LibHac.Fs; using LibHac.Fs;
using LibHac.FsSystem; using LibHac.FsSystem;
using LibHac.Tools.Es;
using LibHac.Tools.Fs;
using LibHac.Util; using LibHac.Util;
using static hactoolnet.Print; using static hactoolnet.Print;

View file

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