Skip to content

Commit

Permalink
Base64fixed (#407)
Browse files Browse the repository at this point in the history
* fixed bug

* add comments

---------

Co-authored-by: Jimmy <[email protected]>
  • Loading branch information
chenzhitong and Jim8y authored Dec 5, 2023
1 parent d1b7367 commit 01f093e
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/neoxp/Extensions/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
using Neo.Wallets;
using NeoExpress.Models;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Numerics;
using System.Text;
using System.Text.RegularExpressions;

namespace NeoExpress
{
Expand Down Expand Up @@ -108,6 +110,7 @@ public static bool IsValidHexString(this string text)
public static bool TryGetBytesFromBase64String(this string text, out Span<byte> bytes)
{
bytes = null;
text = Base64Fixed(text);
Span<byte> buffer = new byte[text.Length * 3 / 4];
if (Convert.TryFromBase64String(text, buffer, out var bytesWritten))
{
Expand All @@ -117,7 +120,24 @@ public static bool TryGetBytesFromBase64String(this string text, out Span<byte>
return false;
}


/// <summary>
/// Convert Unicode Characters to AscII Characters by Regular Expressions.
/// </summary>
/// <param name="str">Base64 string with Unicode escaping. e.g. DCECbzTesnBofh/Xng1SofChKkBC7jhVmLxCN1vk\u002B49xa2pBVuezJw==</param>
/// <returns>Base64 strings without Unicode escaping. e.g. DCECbzTesnBofh/Xng1SofChKkBC7jhVmLxCN1vk+49xa2pBVuezJw==</returns>
public static string Base64Fixed(string str)
{
//Unicode e.g. \u002B
MatchCollection mc = Regex.Matches(str, @"\\u([\w]{2})([\w]{2})", RegexOptions.Compiled | RegexOptions.IgnoreCase);
byte[] bts = new byte[2];
foreach (Match m in mc)
{
bts[0] = (byte)int.Parse(m.Groups[2].Value, NumberStyles.HexNumber);
bts[1] = (byte)int.Parse(m.Groups[1].Value, NumberStyles.HexNumber);
str = str.Replace(m.ToString(), Encoding.Unicode.GetString(bts));
}
return str;
}


public static string EscapeString(this string text)
Expand Down

0 comments on commit 01f093e

Please sign in to comment.