Make path parser check for a null terminator

This commit is contained in:
Alex Barney 2020-03-23 15:54:11 -07:00
parent 30b0c748f8
commit 0bb3446a8b
3 changed files with 5 additions and 40 deletions

View file

@ -27,7 +27,7 @@ namespace LibHac.FsSystem
_path = path;
_offset = 0;
_length = 0;
_finished = path.Length == 1;
_finished = path.Length == 1 || path[1] == '\0';
}
/// <summary>
@ -57,12 +57,12 @@ namespace LibHac.FsSystem
_offset = _offset + _length + 1;
int end = _offset;
while (end < _path.Length && _path[end] != '/')
while (end < _path.Length && _path[end] != '\0' && _path[end] != '/')
{
end++;
}
_finished = end + 1 >= _path.Length;
_finished = end + 1 >= _path.Length || _path[end + 1] == '\0';
_length = end - _offset;
return true;

View file

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using LibHac.Common;
using LibHac.Fs;
namespace LibHac.FsSystem.Save
@ -44,7 +45,7 @@ namespace LibHac.FsSystem.Save
ReadEntry(index, out entry);
if (entry.Parent == key.Parent && Util.StringSpansEqual(name, key.Name))
if (entry.Parent == key.Parent && StringUtils.Compare(name, key.Name) == 0)
{
return (index, prevIndex);
}

View file

@ -64,42 +64,6 @@ namespace LibHac
return true;
}
/// <summary>
/// Compares two strings stored int byte spans. For the strings to be equal,
/// they must terminate in the same place.
/// A string can be terminated by either a null character or the end of the span.
/// </summary>
/// <param name="s1">The first string to be compared.</param>
/// <param name="s2">The first string to be compared.</param>
/// <returns><see langword="true"/> if the strings are equal;
/// otherwise <see langword="false"/>.</returns>
public static bool StringSpansEqual(ReadOnlySpan<byte> s1, ReadOnlySpan<byte> s2)
{
// Make s1 the long string for simplicity
if (s1.Length < s2.Length)
{
ReadOnlySpan<byte> tmp = s1;
s1 = s2;
s2 = tmp;
}
int shortLength = s2.Length;
int i;
for (i = 0; i < shortLength; i++)
{
if (s1[i] != s2[i]) return false;
// Both strings are null-terminated
if (s1[i] == 0) return true;
}
// The bytes in the short string equal those in the long.
// Check if the strings are the same length or if the next
// character in the long string is a null character
return s1.Length == s2.Length || s1[i] == 0;
}
public static ReadOnlySpan<byte> GetUtf8Bytes(string value)
{
return Encoding.UTF8.GetBytes(value).AsSpan();