From 91e1e65aff0b06e0bd41b00db66e05a7f08e8b96 Mon Sep 17 00:00:00 2001 From: Alex Barney Date: Thu, 27 Jan 2022 15:06:03 -0700 Subject: [PATCH] Add IHash256Generator interfaces - FsSystem.IHash256Generator - FsSystem.IHash256GeneratorFactory - FsSystem.IHash256GeneratorFactorySelector --- src/LibHac/FsSystem/IHash256Generator.cs | 79 ++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 src/LibHac/FsSystem/IHash256Generator.cs diff --git a/src/LibHac/FsSystem/IHash256Generator.cs b/src/LibHac/FsSystem/IHash256Generator.cs new file mode 100644 index 00000000..3f2e26dc --- /dev/null +++ b/src/LibHac/FsSystem/IHash256Generator.cs @@ -0,0 +1,79 @@ +// ReSharper disable InconsistentNaming +using System; +using LibHac.Common; +using LibHac.Diag; + +namespace LibHac.FsSystem; + +/// +/// Generates a hash for a stream of data. The data can be given to the +/// as multiple, smaller sequential blocks of data. +/// +/// Based on FS 13.1.0 (nnSdk 13.4.0) +public abstract class IHash256Generator : IDisposable +{ + public static readonly long HashSize = 256 / 8; + + public virtual void Dispose() { } + + public void Initialize() + { + DoInitialize(); + } + + public void Update(ReadOnlySpan data) + { + DoUpdate(data); + } + + public void GetHash(Span hashBuffer) + { + Assert.SdkRequiresEqual(HashSize, hashBuffer.Length); + + DoGetHash(hashBuffer); + } + + protected abstract void DoInitialize(); + protected abstract void DoUpdate(ReadOnlySpan data); + protected abstract void DoGetHash(Span hashBuffer); +} + +/// +/// Creates objects and can generate a hash for a single, in-memory block of data. +/// +/// Based on FS 13.1.0 (nnSdk 13.4.0) +public abstract class IHash256GeneratorFactory : IDisposable +{ + public virtual void Dispose() { } + + public UniqueRef Create() + { + return DoCreate(); + } + + public void GenerateHash(Span hashBuffer, ReadOnlySpan data) + { + Assert.SdkRequiresEqual(IHash256Generator.HashSize, hashBuffer.Length); + + DoGenerateHash(hashBuffer, data); + } + + protected abstract UniqueRef DoCreate(); + protected abstract void DoGenerateHash(Span hashBuffer, ReadOnlySpan data); +} + +/// +/// Creates objects. +/// +/// Based on FS 13.1.0 (nnSdk 13.4.0) +public abstract class IHash256GeneratorFactorySelector : IDisposable +{ + public virtual void Dispose() { } + + public IHash256GeneratorFactory GetFactory() + { + return DoGetFactory(); + } + + protected abstract IHash256GeneratorFactory DoGetFactory(); +} \ No newline at end of file