Use SkipInit for initializing path string types

This commit is contained in:
Alex Barney 2020-11-17 21:22:28 -07:00
parent e82d3e039b
commit 369784ff12
9 changed files with 59 additions and 99 deletions

View File

@ -1,6 +1,7 @@
// ReSharper disable AssignmentIsFullyDiscarded
using System;
using System.Runtime.CompilerServices;
using LibHac.Common;
using LibHac.Crypto.Detail;
using LibHac.Diag;
@ -106,8 +107,7 @@ namespace LibHac.Crypto
{
if (IsAesNiSupported() && !preferDotNetCrypto)
{
AesEcbModeNi cipherNi;
unsafe { _ = &cipherNi; } // workaround for CS0165
Unsafe.SkipInit(out AesEcbModeNi cipherNi);
cipherNi.Initialize(key, false);
cipherNi.Encrypt(input, output);
@ -119,14 +119,12 @@ namespace LibHac.Crypto
cipher.Transform(input, output);
}
[MethodImpl(MethodImplOptions.NoInlining)]
public static void DecryptEcb128(ReadOnlySpan<byte> input, Span<byte> output, ReadOnlySpan<byte> key,
bool preferDotNetCrypto = false)
{
if (IsAesNiSupported() && !preferDotNetCrypto)
{
AesEcbModeNi cipherNi;
unsafe { _ = &cipherNi; } // workaround for CS0165
Unsafe.SkipInit(out AesEcbModeNi cipherNi);
cipherNi.Initialize(key, true);
cipherNi.Decrypt(input, output);
@ -143,8 +141,7 @@ namespace LibHac.Crypto
{
if (IsAesNiSupported() && !preferDotNetCrypto)
{
AesCbcModeNi cipherNi;
unsafe { _ = &cipherNi; } // workaround for CS0165
Unsafe.SkipInit(out AesCbcModeNi cipherNi);
cipherNi.Initialize(key, iv, false);
cipherNi.Encrypt(input, output);
@ -161,8 +158,7 @@ namespace LibHac.Crypto
{
if (IsAesNiSupported() && !preferDotNetCrypto)
{
AesCbcModeNi cipherNi;
unsafe { _ = &cipherNi; } // workaround for CS0165
Unsafe.SkipInit(out AesCbcModeNi cipherNi);
cipherNi.Initialize(key, iv, true);
cipherNi.Decrypt(input, output);
@ -179,8 +175,7 @@ namespace LibHac.Crypto
{
if (IsAesNiSupported() && !preferDotNetCrypto)
{
AesCtrModeNi cipherNi;
unsafe { _ = &cipherNi; } // workaround for CS0165
Unsafe.SkipInit(out AesCtrModeNi cipherNi);
cipherNi.Initialize(key, iv);
cipherNi.Transform(input, output);
@ -197,8 +192,7 @@ namespace LibHac.Crypto
{
if (IsAesNiSupported() && !preferDotNetCrypto)
{
AesCtrModeNi cipherNi;
unsafe { _ = &cipherNi; } // workaround for CS0165
Unsafe.SkipInit(out AesCtrModeNi cipherNi);
cipherNi.Initialize(key, iv);
cipherNi.Transform(input, output);
@ -215,8 +209,7 @@ namespace LibHac.Crypto
{
if (IsAesNiSupported() && !preferDotNetCrypto)
{
AesXtsModeNi cipherNi;
unsafe { _ = &cipherNi; } // workaround for CS0165
Unsafe.SkipInit(out AesXtsModeNi cipherNi);
cipherNi.Initialize(key1, key2, iv, false);
cipherNi.Encrypt(input, output);
@ -233,8 +226,7 @@ namespace LibHac.Crypto
{
if (IsAesNiSupported() && !preferDotNetCrypto)
{
AesXtsModeNi cipherNi;
unsafe { _ = &cipherNi; } // workaround for CS0165
Unsafe.SkipInit(out AesXtsModeNi cipherNi);
cipherNi.Initialize(key1, key2, iv, true);
cipherNi.Decrypt(input, output);
@ -255,7 +247,7 @@ namespace LibHac.Crypto
/// <remarks>https://tools.ietf.org/html/rfc4493</remarks>
public static void CalculateCmac(Span<byte> mac, ReadOnlySpan<byte> data, ReadOnlySpan<byte> key)
{
ReadOnlySpan<byte> zero = stackalloc byte[16];
ReadOnlySpan<byte> zero = new Buffer16();
int len = data.Length;
// Step 1, AES-128 with key K is applied to an all-zero input block.
@ -289,6 +281,8 @@ namespace LibHac.Crypto
{
int paddedLength = len + (16 - len % 16);
paddedMessage = paddedLength < 0x800 ? stackalloc byte[paddedLength] : new byte[paddedLength];
paddedMessage.Slice(len).Clear();
paddedMessage[len] = 0x80;
data.CopyTo(paddedMessage);

View File

@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using LibHac.Common;
using LibHac.Fs;
using LibHac.Fs.Fsa;
@ -160,8 +161,7 @@ namespace LibHac.FsSystem
for (int i = currentSubFileCount; i < newSubFileCount; i++)
{
FsPath newSubFilePath;
unsafe { _ = &newSubFilePath; } // workaround for CS0165
Unsafe.SkipInit(out FsPath newSubFilePath);
rc = ConcatenationFileSystem.GetSubFilePath(newSubFilePath.Str, FilePath, i);
if (rc.IsFailure()) return rc;
@ -184,8 +184,7 @@ namespace LibHac.FsSystem
Sources[i].Dispose();
Sources.RemoveAt(i);
FsPath subFilePath;
unsafe { _ = &subFilePath; } // workaround for CS0165
Unsafe.SkipInit(out FsPath subFilePath);
rc = ConcatenationFileSystem.GetSubFilePath(subFilePath.Str, FilePath, i);
if (rc.IsFailure()) return rc;

View File

@ -2,6 +2,7 @@
using System.Buffers;
using System.Buffers.Text;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using LibHac.Common;
using LibHac.Fs;
@ -138,8 +139,7 @@ namespace LibHac.FsSystem
{
long fileSize = Math.Min(SubFileSize, remaining);
FsPath fileName;
unsafe { _ = &fileName; } // workaround for CS0165
Unsafe.SkipInit(out FsPath fileName);
rc = GetSubFilePath(fileName.Str, path, i);
if (rc.IsFailure()) return rc;
@ -194,8 +194,7 @@ namespace LibHac.FsSystem
for (int i = 0; i < count; i++)
{
FsPath subFilePath;
unsafe { _ = &subFilePath; } // workaround for CS0165
Unsafe.SkipInit(out FsPath subFilePath);
rc = GetSubFilePath(subFilePath.Str, path, i);
if (rc.IsFailure()) return rc;
@ -239,8 +238,7 @@ namespace LibHac.FsSystem
for (int i = 0; i < fileCount; i++)
{
FsPath subFilePath;
unsafe { _ = &subFilePath; } // workaround for CS0165
Unsafe.SkipInit(out FsPath subFilePath);
rc = GetSubFilePath(subFilePath.Str, path, i);
if (rc.IsFailure()) return rc;
@ -330,8 +328,7 @@ namespace LibHac.FsSystem
{
fileCount = default;
FsPath buffer;
unsafe { _ = &buffer; } // workaround for CS0165
Unsafe.SkipInit(out FsPath buffer);
int pathLen = StringUtils.Copy(buffer.Str, dirPath);
@ -381,8 +378,7 @@ namespace LibHac.FsSystem
{
size = default;
FsPath buffer;
unsafe { _ = &buffer; } // workaround for CS0165
Unsafe.SkipInit(out FsPath buffer);
int pathLen = StringUtils.Copy(buffer.Str, path);

View File

@ -1,4 +1,5 @@
using System;
using System.Runtime.CompilerServices;
using LibHac.Common;
using LibHac.Fs;
using LibHac.Fs.Fsa;
@ -101,8 +102,7 @@ namespace LibHac.FsSystem
protected override Result DoCreateDirectory(U8Span path)
{
FsPath fullPath;
unsafe { _ = &fullPath; } // workaround for CS0165
Unsafe.SkipInit(out FsPath fullPath);
Result rc = ResolveFullPath(fullPath.Str, path);
if (rc.IsFailure()) return rc;
@ -115,8 +115,7 @@ namespace LibHac.FsSystem
protected override Result DoCreateFile(U8Span path, long size, CreateFileOptions options)
{
FsPath fullPath;
unsafe { _ = &fullPath; } // workaround for CS0165
Unsafe.SkipInit(out FsPath fullPath);
Result rc = ResolveFullPath(fullPath.Str, path);
if (rc.IsFailure()) return rc;
@ -129,8 +128,7 @@ namespace LibHac.FsSystem
protected override Result DoDeleteDirectory(U8Span path)
{
FsPath fullPath;
unsafe { _ = &fullPath; } // workaround for CS0165
Unsafe.SkipInit(out FsPath fullPath);
Result rc = ResolveFullPath(fullPath.Str, path);
if (rc.IsFailure()) return rc;
@ -143,8 +141,7 @@ namespace LibHac.FsSystem
protected override Result DoDeleteDirectoryRecursively(U8Span path)
{
FsPath fullPath;
unsafe { _ = &fullPath; } // workaround for CS0165
Unsafe.SkipInit(out FsPath fullPath);
Result rc = ResolveFullPath(fullPath.Str, path);
if (rc.IsFailure()) return rc;
@ -157,8 +154,7 @@ namespace LibHac.FsSystem
protected override Result DoCleanDirectoryRecursively(U8Span path)
{
FsPath fullPath;
unsafe { _ = &fullPath; } // workaround for CS0165
Unsafe.SkipInit(out FsPath fullPath);
Result rc = ResolveFullPath(fullPath.Str, path);
if (rc.IsFailure()) return rc;
@ -171,8 +167,7 @@ namespace LibHac.FsSystem
protected override Result DoDeleteFile(U8Span path)
{
FsPath fullPath;
unsafe { _ = &fullPath; } // workaround for CS0165
Unsafe.SkipInit(out FsPath fullPath);
Result rc = ResolveFullPath(fullPath.Str, path);
if (rc.IsFailure()) return rc;
@ -185,8 +180,7 @@ namespace LibHac.FsSystem
protected override Result DoOpenDirectory(out IDirectory directory, U8Span path, OpenDirectoryMode mode)
{
FsPath fullPath;
unsafe { _ = &fullPath; } // workaround for CS0165
Unsafe.SkipInit(out FsPath fullPath);
Result rc = ResolveFullPath(fullPath.Str, path);
if (rc.IsFailure())
@ -205,8 +199,7 @@ namespace LibHac.FsSystem
{
file = default;
FsPath fullPath;
unsafe { _ = &fullPath; } // workaround for CS0165
Unsafe.SkipInit(out FsPath fullPath);
Result rc = ResolveFullPath(fullPath.Str, path);
if (rc.IsFailure()) return rc;
@ -229,10 +222,8 @@ namespace LibHac.FsSystem
protected override Result DoRenameDirectory(U8Span oldPath, U8Span newPath)
{
FsPath fullCurrentPath;
FsPath fullNewPath;
unsafe { _ = &fullCurrentPath; } // workaround for CS0165
unsafe { _ = &fullNewPath; } // workaround for CS0165
Unsafe.SkipInit(out FsPath fullCurrentPath);
Unsafe.SkipInit(out FsPath fullNewPath);
Result rc = ResolveFullPath(fullCurrentPath.Str, oldPath);
if (rc.IsFailure()) return rc;
@ -248,10 +239,8 @@ namespace LibHac.FsSystem
protected override Result DoRenameFile(U8Span oldPath, U8Span newPath)
{
FsPath fullCurrentPath;
FsPath fullNewPath;
unsafe { _ = &fullCurrentPath; } // workaround for CS0165
unsafe { _ = &fullNewPath; } // workaround for CS0165
Unsafe.SkipInit(out FsPath fullCurrentPath);
Unsafe.SkipInit(out FsPath fullNewPath);
Result rc = ResolveFullPath(fullCurrentPath.Str, oldPath);
if (rc.IsFailure()) return rc;
@ -267,8 +256,7 @@ namespace LibHac.FsSystem
protected override Result DoGetEntryType(out DirectoryEntryType entryType, U8Span path)
{
FsPath fullPath;
unsafe { _ = &fullPath; } // workaround for CS0165
Unsafe.SkipInit(out FsPath fullPath);
Result rc = ResolveFullPath(fullPath.Str, path);
if (rc.IsFailure())
@ -338,8 +326,7 @@ namespace LibHac.FsSystem
{
freeSpace = default;
FsPath fullPath;
unsafe { _ = &fullPath; } // workaround for CS0165
Unsafe.SkipInit(out FsPath fullPath);
Result rc = ResolveFullPath(fullPath.Str, path);
if (rc.IsFailure()) return rc;
@ -354,8 +341,7 @@ namespace LibHac.FsSystem
{
totalSpace = default;
FsPath fullPath;
unsafe { _ = &fullPath; } // workaround for CS0165
Unsafe.SkipInit(out FsPath fullPath);
Result rc = ResolveFullPath(fullPath.Str, path);
if (rc.IsFailure()) return rc;

View File

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;
using System.Threading;
using LibHac.Common;
using LibHac.Fs;
@ -36,8 +37,7 @@ namespace LibHac.FsSystem
{
fullPath = default;
FsPath normalizedPath;
unsafe { _ = &normalizedPath; } // workaround for CS0165
Unsafe.SkipInit(out FsPath normalizedPath);
Result rc = PathTool.Normalize(normalizedPath.Str, out _, path, false, false);
if (rc.IsFailure()) return rc;
@ -48,10 +48,8 @@ namespace LibHac.FsSystem
private Result CheckSubPath(U8Span path1, U8Span path2)
{
FsPath normalizedPath1;
FsPath normalizedPath2;
unsafe { _ = &normalizedPath1; } // workaround for CS0165
unsafe { _ = &normalizedPath2; } // workaround for CS0165
Unsafe.SkipInit(out FsPath normalizedPath1);
Unsafe.SkipInit(out FsPath normalizedPath2);
Result rc = PathTool.Normalize(normalizedPath1.Str, out _, path1, false, false);
if (rc.IsFailure()) return rc;

View File

@ -62,7 +62,7 @@ namespace LibHac.FsSystem
end++;
}
_finished = end + 1 >= _path.Length || _path[end + 1] == '\0';
_finished = end + 1 >= _path.Length || _path[end] == '\0';
_length = end - _offset;
return true;

View File

@ -1,4 +1,5 @@
using System.IO;
using System.Runtime.CompilerServices;
using LibHac.Common;
using LibHac.Fs;
using LibHac.Fs.Fsa;
@ -31,8 +32,7 @@ namespace LibHac.FsSystem.Save
protected override Result DoCreateDirectory(U8Span path)
{
FsPath normalizedPath;
unsafe { _ = &normalizedPath; } // workaround for CS0165
Unsafe.SkipInit(out FsPath normalizedPath);
Result rc = PathTool.Normalize(normalizedPath.Str, out _, path, false, false);
if (rc.IsFailure()) return rc;
@ -44,8 +44,7 @@ namespace LibHac.FsSystem.Save
protected override Result DoCreateFile(U8Span path, long size, CreateFileOptions options)
{
FsPath normalizedPath;
unsafe { _ = &normalizedPath; } // workaround for CS0165
Unsafe.SkipInit(out FsPath normalizedPath);
Result rc = PathTool.Normalize(normalizedPath.Str, out _, path, false, false);
if (rc.IsFailure()) return rc;
@ -75,8 +74,7 @@ namespace LibHac.FsSystem.Save
protected override Result DoDeleteDirectory(U8Span path)
{
FsPath normalizedPath;
unsafe { _ = &normalizedPath; } // workaround for CS0165
Unsafe.SkipInit(out FsPath normalizedPath);
Result rc = PathTool.Normalize(normalizedPath.Str, out _, path, false, false);
if (rc.IsFailure()) return rc;
@ -88,8 +86,7 @@ namespace LibHac.FsSystem.Save
protected override Result DoDeleteDirectoryRecursively(U8Span path)
{
FsPath normalizedPath;
unsafe { _ = &normalizedPath; } // workaround for CS0165
Unsafe.SkipInit(out FsPath normalizedPath);
Result rc = PathTool.Normalize(normalizedPath.Str, out _, path, false, false);
if (rc.IsFailure()) return rc;
@ -105,8 +102,7 @@ namespace LibHac.FsSystem.Save
protected override Result DoCleanDirectoryRecursively(U8Span path)
{
FsPath normalizedPath;
unsafe { _ = &normalizedPath; } // workaround for CS0165
Unsafe.SkipInit(out FsPath normalizedPath);
Result rc = PathTool.Normalize(normalizedPath.Str, out _, path, false, false);
if (rc.IsFailure()) return rc;
@ -118,8 +114,7 @@ namespace LibHac.FsSystem.Save
protected override Result DoDeleteFile(U8Span path)
{
FsPath normalizedPath;
unsafe { _ = &normalizedPath; } // workaround for CS0165
Unsafe.SkipInit(out FsPath normalizedPath);
Result rc = PathTool.Normalize(normalizedPath.Str, out _, path, false, false);
if (rc.IsFailure()) return rc;
@ -143,8 +138,7 @@ namespace LibHac.FsSystem.Save
{
directory = default;
FsPath normalizedPath;
unsafe { _ = &normalizedPath; } // workaround for CS0165
Unsafe.SkipInit(out FsPath normalizedPath);
Result rc = PathTool.Normalize(normalizedPath.Str, out _, path, false, false);
if (rc.IsFailure()) return rc;
@ -163,8 +157,7 @@ namespace LibHac.FsSystem.Save
{
file = default;
FsPath normalizedPath;
unsafe { _ = &normalizedPath; } // workaround for CS0165
Unsafe.SkipInit(out FsPath normalizedPath);
Result rc = PathTool.Normalize(normalizedPath.Str, out _, path, false, false);
if (rc.IsFailure()) return rc;
@ -183,10 +176,8 @@ namespace LibHac.FsSystem.Save
protected override Result DoRenameDirectory(U8Span oldPath, U8Span newPath)
{
FsPath normalizedCurrentPath;
FsPath normalizedNewPath;
unsafe { _ = &normalizedCurrentPath; } // workaround for CS0165
unsafe { _ = &normalizedNewPath; } // workaround for CS0165
Unsafe.SkipInit(out FsPath normalizedCurrentPath);
Unsafe.SkipInit(out FsPath normalizedNewPath);
Result rc = PathTool.Normalize(normalizedCurrentPath.Str, out _, oldPath, false, false);
if (rc.IsFailure()) return rc;
@ -199,10 +190,8 @@ namespace LibHac.FsSystem.Save
protected override Result DoRenameFile(U8Span oldPath, U8Span newPath)
{
FsPath normalizedCurrentPath;
FsPath normalizedNewPath;
unsafe { _ = &normalizedCurrentPath; } // workaround for CS0165
unsafe { _ = &normalizedNewPath; } // workaround for CS0165
Unsafe.SkipInit(out FsPath normalizedCurrentPath);
Unsafe.SkipInit(out FsPath normalizedNewPath);
Result rc = PathTool.Normalize(normalizedCurrentPath.Str, out _, oldPath, false, false);
if (rc.IsFailure()) return rc;
@ -219,8 +208,7 @@ namespace LibHac.FsSystem.Save
{
entryType = default;
FsPath normalizedPath;
unsafe { _ = &normalizedPath; } // workaround for CS0165
Unsafe.SkipInit(out FsPath normalizedPath);
Result rc = PathTool.Normalize(normalizedPath.Str, out _, path, false, false);
if (rc.IsFailure()) return rc;

View File

@ -1,5 +1,6 @@
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using LibHac.Common;
using LibHac.Fs;
using LibHac.Fs.Fsa;
@ -200,8 +201,7 @@ namespace LibHac.FsSystem
{
entryType = default;
FsPath fullPath;
unsafe { _ = &fullPath; } // workaround for CS0165
Unsafe.SkipInit(out FsPath fullPath);
Result rc = ResolveFullPath(fullPath.Str, path);
if (rc.IsFailure()) return rc;

View File

@ -189,8 +189,7 @@ namespace LibHac.FsSystem
using (sourceFile)
{
// Open dest file.
FsPath destPath;
unsafe { _ = &destPath; } // workaround for CS0165
Unsafe.SkipInit(out FsPath destPath);
var sb = new U8StringBuilder(destPath.Str);
sb.Append(destParentPath).Append(entry.Name);