From db0b0e28d259cc3bae4597eda543bfef4d1585c2 Mon Sep 17 00:00:00 2001 From: David Garske Date: Thu, 22 May 2025 16:34:54 -0700 Subject: [PATCH] Fix issue with CSharp and Windows CE with conversion of ASCII->Unicode and Unicode->ASCII with odd length and extra null terminator. --- wrapper/CSharp/wolfSSL_CSharp/wolfSSL.cs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/wrapper/CSharp/wolfSSL_CSharp/wolfSSL.cs b/wrapper/CSharp/wolfSSL_CSharp/wolfSSL.cs index 0153b8ab0..624c1910e 100644 --- a/wrapper/CSharp/wolfSSL_CSharp/wolfSSL.cs +++ b/wrapper/CSharp/wolfSSL_CSharp/wolfSSL.cs @@ -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", ""); } /// @@ -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); } /// - /// 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 /// public static string PtrToStringAnsi(IntPtr ptr) {