Browse Source

Improve StorageBookmarkHelper.TryDecodeBookmark. (#21977)

pull/21988/head
jp2masa 2 months ago
committed by GitHub
parent
commit
fc61f42e38
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 21
      src/Avalonia.Base/Platform/Storage/FileIO/StorageBookmarkHelper.cs

21
src/Avalonia.Base/Platform/Storage/FileIO/StorageBookmarkHelper.cs

@ -86,20 +86,19 @@ internal static class StorageBookmarkHelper
Span<byte> decodedBookmark;
// Each base64 character represents 6 bits, but to be safe,
var arrayPool = ArrayPool<byte>.Shared.Rent(HeaderLength + base64bookmark.Length * 6);
if (Convert.TryFromBase64Chars(base64bookmark, arrayPool, out int bytesWritten))
{
decodedBookmark = arrayPool.AsSpan().Slice(0, bytesWritten);
}
else
{
nativeBookmark = null;
return DecodeResult.InvalidFormat;
}
var arrayPool = ArrayPool<byte>.Shared.Rent(HeaderLength + base64bookmark.Length / 4 * 3);
try
{
// Each base64 character represents 6 bits, but to be safe,
if (!Convert.TryFromBase64Chars(base64bookmark, arrayPool, out int bytesWritten))
{
nativeBookmark = null;
return DecodeResult.InvalidFormat;
}
decodedBookmark = arrayPool.AsSpan(..bytesWritten);
if (decodedBookmark.Length < HeaderLength
// Check if decoded string starts with the correct prefix, checking v1 at the same time.
&& !AvaHeaderPrefix.SequenceEqual(decodedBookmark.Slice(0, AvaHeaderPrefix.Length)))

Loading…
Cancel
Save