Skip to content

Commit fc61f42

Browse files
authored
Improve StorageBookmarkHelper.TryDecodeBookmark. (#21977)
1 parent 845b81d commit fc61f42

1 file changed

Lines changed: 10 additions & 11 deletions

File tree

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

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -86,20 +86,19 @@ public static DecodeResult TryDecodeBookmark(ReadOnlySpan<byte> platform, string
8686

8787
Span<byte> decodedBookmark;
8888

89-
// Each base64 character represents 6 bits, but to be safe,
90-
var arrayPool = ArrayPool<byte>.Shared.Rent(HeaderLength + base64bookmark.Length * 6);
91-
if (Convert.TryFromBase64Chars(base64bookmark, arrayPool, out int bytesWritten))
92-
{
93-
decodedBookmark = arrayPool.AsSpan().Slice(0, bytesWritten);
94-
}
95-
else
96-
{
97-
nativeBookmark = null;
98-
return DecodeResult.InvalidFormat;
99-
}
89+
var arrayPool = ArrayPool<byte>.Shared.Rent(HeaderLength + base64bookmark.Length / 4 * 3);
10090

10191
try
10292
{
93+
// Each base64 character represents 6 bits, but to be safe,
94+
if (!Convert.TryFromBase64Chars(base64bookmark, arrayPool, out int bytesWritten))
95+
{
96+
nativeBookmark = null;
97+
return DecodeResult.InvalidFormat;
98+
}
99+
100+
decodedBookmark = arrayPool.AsSpan(..bytesWritten);
101+
103102
if (decodedBookmark.Length < HeaderLength
104103
// Check if decoded string starts with the correct prefix, checking v1 at the same time.
105104
&& !AvaHeaderPrefix.SequenceEqual(decodedBookmark.Slice(0, AvaHeaderPrefix.Length)))

0 commit comments

Comments
 (0)