-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3edaeaf
commit 4c00119
Showing
15 changed files
with
144 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using System.Text; | ||
|
||
namespace OrchardCore; | ||
|
||
public static class Str | ||
{ | ||
public static ReadOnlySpan<byte> FromBase64String(string base64) | ||
{ | ||
if (string.IsNullOrEmpty(base64)) | ||
{ | ||
return []; | ||
} | ||
var neededLength = GetByteArrayLengthFromBase64(base64); | ||
|
||
using var memoryStream = MemoryStreamFactory.GetStream(neededLength); | ||
Check failure on line 15 in src/OrchardCore/OrchardCore.Abstractions/Str.cs GitHub Actions / Build & Test (ubuntu-latest)
Check failure on line 15 in src/OrchardCore/OrchardCore.Abstractions/Str.cs GitHub Actions / Build & Test (ubuntu-latest)
Check failure on line 15 in src/OrchardCore/OrchardCore.Abstractions/Str.cs GitHub Actions / Build & Test (windows-latest)
|
||
var bytes = memoryStream.GetSpan(neededLength); | ||
|
||
if (!Convert.TryFromBase64String(base64, bytes, out var _)) | ||
{ | ||
throw new FormatException("Invalid Base64 string."); | ||
} | ||
|
||
return bytes; | ||
} | ||
|
||
public static bool TryFromBase64String(string base64, out ReadOnlySpan<byte> bytes) | ||
{ | ||
if (string.IsNullOrEmpty(base64)) | ||
{ | ||
bytes = []; | ||
|
||
return false; | ||
} | ||
|
||
Span<byte> data = new byte[GetByteArrayLengthFromBase64(base64)]; | ||
|
||
if (!Convert.TryFromBase64String(base64, data, out var _)) | ||
{ | ||
bytes = []; | ||
|
||
return false; | ||
} | ||
|
||
bytes = data; | ||
|
||
return true; | ||
} | ||
|
||
// Base64 string length gives us the original byte length. It encodes 3 bytes into 4 characters. | ||
// The formula to calculate the number of bytes from the base64 string length is: | ||
private static int GetByteArrayLengthFromBase64(string base64String) | ||
{ | ||
return (base64String.Length * 3) / 4; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using System.Text; | ||
|
||
namespace OrchardCore; | ||
|
||
public static class Str | ||
{ | ||
public static ReadOnlySpan<byte> FromBase64String(string base64) | ||
{ | ||
if (string.IsNullOrEmpty(base64)) | ||
{ | ||
return []; | ||
} | ||
var neededLength = GetByteArrayLengthFromBase64(base64); | ||
|
||
using var memoryStream = MemoryStreamFactory.GetStream(neededLength); | ||
var bytes = memoryStream.GetSpan(neededLength); | ||
|
||
if (!Convert.TryFromBase64String(base64, bytes, out var _)) | ||
{ | ||
throw new FormatException("Invalid Base64 string."); | ||
} | ||
|
||
return bytes; | ||
} | ||
|
||
public static bool TryFromBase64String(string base64, out ReadOnlySpan<byte> bytes) | ||
{ | ||
if (string.IsNullOrEmpty(base64)) | ||
{ | ||
bytes = []; | ||
|
||
return false; | ||
} | ||
|
||
Span<byte> data = new byte[GetByteArrayLengthFromBase64(base64)]; | ||
|
||
if (!Convert.TryFromBase64String(base64, data, out var _)) | ||
{ | ||
bytes = []; | ||
|
||
return false; | ||
} | ||
|
||
bytes = data; | ||
|
||
return true; | ||
} | ||
|
||
// Base64 string length gives us the original byte length. It encodes 3 bytes into 4 characters. | ||
// The formula to calculate the number of bytes from the base64 string length is: | ||
private static int GetByteArrayLengthFromBase64(string base64String) | ||
{ | ||
return (base64String.Length * 3) / 4; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters