Skip to content

Commit db0b0e2

Browse files
committed
Fix issue with CSharp and Windows CE with conversion of ASCII->Unicode and Unicode->ASCII with odd length and extra null terminator.
1 parent 9fdb40c commit db0b0e2

1 file changed

Lines changed: 12 additions & 4 deletions

File tree

wrapper/CSharp/wolfSSL_CSharp/wolfSSL.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ public static string UnicodeToAscii(string msg)
5151
/* Convert Unicode to Bytes */
5252
byte[] bytes = Encoding.Unicode.GetBytes((string)msg.ToString());
5353
/* Convert to ASCII */
54-
return Encoding.ASCII.GetString(bytes, 0, bytes.Length);
54+
string ret = Encoding.ASCII.GetString(bytes, 0, bytes.Length);
55+
/* odd length unicode might have extra null terminator, so remove */
56+
return ret.Replace("\0", "");
5557
}
5658

5759
/// <summary>
@@ -61,14 +63,20 @@ public static string AsciiToUnicode(string msg)
6163
{
6264
if (msg == null)
6365
return null;
64-
/* Convert ASCII to Bytes */
65-
byte[] bytes = Encoding.ASCII.GetBytes((string)msg.ToString());
66+
/* Get length and round up to even unicode */
67+
int msgLen = msg.Length;
68+
msgLen = ((msgLen + 1) & ~1);
69+
byte[] bytes = new byte[msgLen];
70+
/* Convert Ascii to Bytes */
71+
byte[] msgBytes = Encoding.ASCII.GetBytes((string)msg.ToString());
72+
msgBytes.CopyTo(bytes, 0);
6673
/* Convert to Unicode */
6774
return Encoding.Unicode.GetString(bytes, 0, bytes.Length);
6875
}
6976

7077
/// <summary>
71-
/// WinCE version of Marshal for Unicode or Multi-byte pointer to ASCII string
78+
/// WinCE version of Marshal for Unicode or Multi-byte pointer to
79+
/// ASCII string
7280
/// </summary>
7381
public static string PtrToStringAnsi(IntPtr ptr)
7482
{

0 commit comments

Comments
 (0)