diff -r 9911c0d34bbd InSimDotNet/InSimDotNet.csproj --- a/InSimDotNet/InSimDotNet.csproj Fri Sep 21 15:45:01 2012 +0100 +++ b/InSimDotNet/InSimDotNet.csproj Mon Mar 25 07:36:11 2013 +0000 @@ -64,7 +64,6 @@ - diff -r 9911c0d34bbd InSimDotNet/LfsEncoding.cs --- a/InSimDotNet/LfsEncoding.cs Fri Sep 21 15:45:01 2012 +0100 +++ b/InSimDotNet/LfsEncoding.cs Mon Mar 25 07:36:11 2013 +0000 @@ -7,19 +7,19 @@ internal static class LfsEncoding { private const char ControlChar = '^'; private const char FallbackChar = '?'; - private static readonly Encoding DefaultEncoding = Encoding.GetEncoding(1252); + private static readonly Encoding DefaultEncoding = Encoding.GetEncoding(1252, EncoderExceptionFallback.ExceptionFallback, DecoderExceptionFallback.ExceptionFallback); private static readonly Dictionary EncodingMap = new Dictionary { - { 'L', Encoding.GetEncoding(1252) }, - { 'G', Encoding.GetEncoding(1253) }, - { 'C', Encoding.GetEncoding(1251) }, - { 'J', Encoding.GetEncoding(932) }, - { 'E', Encoding.GetEncoding(1250) }, - { 'T', Encoding.GetEncoding(1254) }, - { 'B', Encoding.GetEncoding(1257) }, - { 'H', Encoding.GetEncoding(950) }, - { 'S', Encoding.GetEncoding(936) }, - { 'K', Encoding.GetEncoding(949) }, + { 'L', Encoding.GetEncoding(1252, EncoderExceptionFallback.ExceptionFallback, DecoderExceptionFallback.ExceptionFallback) }, + { 'G', Encoding.GetEncoding(1253, EncoderExceptionFallback.ExceptionFallback, DecoderExceptionFallback.ExceptionFallback) }, + { 'C', Encoding.GetEncoding(1251, EncoderExceptionFallback.ExceptionFallback, DecoderExceptionFallback.ExceptionFallback) }, + { 'J', Encoding.GetEncoding(932, EncoderExceptionFallback.ExceptionFallback, DecoderExceptionFallback.ExceptionFallback) }, + { 'E', Encoding.GetEncoding(1250, EncoderExceptionFallback.ExceptionFallback, DecoderExceptionFallback.ExceptionFallback) }, + { 'T', Encoding.GetEncoding(1254, EncoderExceptionFallback.ExceptionFallback, DecoderExceptionFallback.ExceptionFallback) }, + { 'B', Encoding.GetEncoding(1257, EncoderExceptionFallback.ExceptionFallback, DecoderExceptionFallback.ExceptionFallback) }, + { 'H', Encoding.GetEncoding(950, EncoderExceptionFallback.ExceptionFallback, DecoderExceptionFallback.ExceptionFallback) }, + { 'S', Encoding.GetEncoding(936, EncoderExceptionFallback.ExceptionFallback, DecoderExceptionFallback.ExceptionFallback) }, + { 'K', Encoding.GetEncoding(949, EncoderExceptionFallback.ExceptionFallback, DecoderExceptionFallback.ExceptionFallback) }, }; private static readonly Dictionary EscapeMap = new Dictionary { @@ -74,53 +74,42 @@ public static int GetByteCount(string value, int maxLength) { Encoding encoding = DefaultEncoding; - int tempCount, result = 0; + int result = 0; for (int i = 0; i < value.Length && i < maxLength; i++) { if (value[i] <= 127) { result++; } - else if (TryGetByteCount(encoding, value[i], out tempCount)) { - result += tempCount; - } else { - bool found = false; - foreach (KeyValuePair map in EncodingMap) { - if (map.Value == encoding) continue; + try { + result += encoding.GetByteCount(value.Substring(i, 1)); + } + catch (EncoderFallbackException) { + bool found = false; + foreach (KeyValuePair map in EncodingMap) { + if (map.Value == encoding) continue; - if (TryGetByteCount(map.Value, value[i], out tempCount)) { - encoding = map.Value; - result += tempCount + 2; - found = true; - break; + try { + result += map.Value.GetByteCount(value.Substring(i, 1)) + 2; + encoding = map.Value; + found = true; + break; + } + catch (EncoderFallbackException) { + } + } + if (!found) { + result++; } } - if (!found) result++; } } return result; } - [DebuggerStepThrough] - private static bool TryGetByteCount(Encoding encoding, char value, out int count) { - bool usedDefault = false; - count = NativeMethods.WideCharToMultiByte( - (uint)encoding.CodePage, - NativeMethods.WC_NO_BEST_FIT_CHARS, - value.ToString(), - 1, - null, - 0, - IntPtr.Zero, - out usedDefault); - return !usedDefault; - } - public static int GetBytes(string value, byte[] buffer, int index, int length) { Encoding encoding = DefaultEncoding; - byte[] tempBytes = new byte[2]; - int tempCount; int start = index; int totalLength = index + (length - 1); @@ -128,48 +117,38 @@ if (value[i] <= 127) { buffer[index++] = (byte)value[i]; } - else if (TryGetBytes(encoding, value[i], tempBytes, out tempCount)) { - Buffer.BlockCopy(tempBytes, 0, buffer, index, tempCount); - index += tempCount; - } else { - bool found = false; - foreach (KeyValuePair map in EncodingMap) { - if (map.Value == encoding) continue; + try { + byte[] tempBytes = encoding.GetBytes(value.Substring(i, 1)); + Buffer.BlockCopy(tempBytes, 0, buffer, index, tempBytes.Length); + index += tempBytes.Length; + } + catch (EncoderFallbackException) { + bool found = false; + foreach (KeyValuePair map in EncodingMap) { + if (map.Value == encoding) continue; - if (TryGetBytes(map.Value, value[i], tempBytes, out tempCount)) { - encoding = map.Value; - buffer[index++] = (byte)ControlChar; - buffer[index++] = (byte)map.Key; - Buffer.BlockCopy(tempBytes, 0, buffer, index, tempCount); - index += tempCount; - found = true; - break; + try { + byte[] tempBytes = map.Value.GetBytes(value.Substring(i, 1)); + encoding = map.Value; + buffer[index++] = (byte)ControlChar; + buffer[index++] = (byte)map.Key; + Buffer.BlockCopy(tempBytes, 0, buffer, index, tempBytes.Length); + index += tempBytes.Length; + found = true; + break; + } + catch (EncoderFallbackException) { + } } - } - - if (!found) { - buffer[index++] = (byte)FallbackChar; + if (!found) { + buffer[index++] = (byte)FallbackChar; + } } } } return index - start; } - - [DebuggerStepThrough] - private static bool TryGetBytes(Encoding encoding, char value, byte[] bytes, out int count) { - bool usedDefault = false; - count = NativeMethods.WideCharToMultiByte( - (uint)encoding.CodePage, - NativeMethods.WC_NO_BEST_FIT_CHARS, - value.ToString(), - 1, - bytes, - 2, - IntPtr.Zero, - out usedDefault); - return !usedDefault; - } } } diff -r 9911c0d34bbd InSimDotNet/NativeMethods.cs --- a/InSimDotNet/NativeMethods.cs Fri Sep 21 15:45:01 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,19 +0,0 @@ -using System; -using System.Runtime.InteropServices; - -namespace InSimDotNet { - internal static class NativeMethods { - public const uint WC_NO_BEST_FIT_CHARS = 0x400; - - [DllImport("kernel32.dll")] - public static extern int WideCharToMultiByte( - uint CodePage, // windows codepage e.g. 1251 - uint dwFlags, // optional flags - [MarshalAs(UnmanagedType.LPWStr)] string lpWideCharStr, // unicode string to convert - int cchWideChar, // length of unicode string to convert - [MarshalAs(UnmanagedType.LPArray)] byte[] lpMultiByteStr, // destination buffer - int cbMultiByte, // length of destination buffer - IntPtr lpDefaultChar, // optinal fallback char - [MarshalAs(UnmanagedType.Bool)] out bool lpUsedDefaultChar); // optinal set if fallback char was used - } -}