Fix issue with CSharp and Windows CE with conversion of ASCII->Unicode and Unicode->ASCII with odd length and extra null terminator.

pull/8799/head
David Garske 2025-05-22 16:34:54 -07:00
parent 9fdb40caa4
commit db0b0e28d2
1 changed files with 12 additions and 4 deletions

View File

@ -51,7 +51,9 @@ namespace wolfSSL.CSharp
/* Convert Unicode to Bytes */
byte[] bytes = Encoding.Unicode.GetBytes((string)msg.ToString());
/* Convert to ASCII */
return Encoding.ASCII.GetString(bytes, 0, bytes.Length);
string ret = Encoding.ASCII.GetString(bytes, 0, bytes.Length);
/* odd length unicode might have extra null terminator, so remove */
return ret.Replace("\0", "");
}
/// <summary>
@ -61,14 +63,20 @@ namespace wolfSSL.CSharp
{
if (msg == null)
return null;
/* Convert ASCII to Bytes */
byte[] bytes = Encoding.ASCII.GetBytes((string)msg.ToString());
/* Get length and round up to even unicode */
int msgLen = msg.Length;
msgLen = ((msgLen + 1) & ~1);
byte[] bytes = new byte[msgLen];
/* Convert Ascii to Bytes */
byte[] msgBytes = Encoding.ASCII.GetBytes((string)msg.ToString());
msgBytes.CopyTo(bytes, 0);
/* Convert to Unicode */
return Encoding.Unicode.GetString(bytes, 0, bytes.Length);
}
/// <summary>
/// WinCE version of Marshal for Unicode or Multi-byte pointer to ASCII string
/// WinCE version of Marshal for Unicode or Multi-byte pointer to
/// ASCII string
/// </summary>
public static string PtrToStringAnsi(IntPtr ptr)
{