ConsoleEncoding.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // --------------------------------------------------
  2. // UnityInjector - ConsoleEncoding.cs
  3. // Copyright (c) Usagirei 2015 - 2015
  4. // --------------------------------------------------
  5. using System;
  6. using System.Text;
  7. namespace UnityInjector.ConsoleUtil
  8. {
  9. // --------------------------------------------------
  10. // Code ported from
  11. // https://gist.github.com/asm256/9bfb88336a1433e2328a
  12. // Which in turn was seemingly ported from
  13. // http://jonskeet.uk/csharp/ebcdic/
  14. // using only safe (managed) code
  15. // --------------------------------------------------
  16. internal partial class ConsoleEncoding : Encoding
  17. {
  18. private readonly uint _codePage;
  19. public override int CodePage => (int)_codePage;
  20. public static Encoding OutputEncoding => new ConsoleEncoding(ConsoleCodePage);
  21. public static uint ConsoleCodePage
  22. {
  23. get { return GetConsoleOutputCP(); }
  24. set { SetConsoleOutputCP(value); }
  25. }
  26. public static uint GetActiveCodePage()
  27. {
  28. return GetACP();
  29. }
  30. private ConsoleEncoding(uint codePage)
  31. {
  32. _codePage = codePage;
  33. }
  34. public static ConsoleEncoding GetEncoding(uint codePage)
  35. {
  36. return new ConsoleEncoding(codePage);
  37. }
  38. public override int GetByteCount(char[] chars, int index, int count)
  39. {
  40. WriteCharBuffer(chars, index, count);
  41. int result = WideCharToMultiByte(_codePage, 0, _charBuffer, count, _zeroByte, 0, IntPtr.Zero, IntPtr.Zero);
  42. return result;
  43. }
  44. public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
  45. {
  46. var byteCount = GetMaxByteCount(charCount);
  47. WriteCharBuffer(chars, charIndex, charCount);
  48. ExpandByteBuffer(byteCount);
  49. int result = WideCharToMultiByte(_codePage, 0, chars, charCount, _byteBuffer, byteCount, IntPtr.Zero, IntPtr.Zero);
  50. ReadByteBuffer(bytes, byteIndex, byteCount);
  51. return result;
  52. }
  53. public override int GetCharCount(byte[] bytes, int index, int count)
  54. {
  55. WriteByteBuffer(bytes, index, count);
  56. int result = MultiByteToWideChar(_codePage, 0, bytes, count, _zeroChar, 0);
  57. return result;
  58. }
  59. public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
  60. {
  61. var charCount = GetMaxCharCount(byteCount);
  62. WriteByteBuffer(bytes, byteIndex, byteCount);
  63. ExpandCharBuffer(charCount);
  64. int result = MultiByteToWideChar(_codePage, 0, bytes, byteCount, _charBuffer, charCount);
  65. ReadCharBuffer(chars, charIndex, charCount);
  66. return result;
  67. }
  68. public override int GetMaxByteCount(int charCount) => charCount * 2;
  69. public override int GetMaxCharCount(int byteCount) => byteCount;
  70. }
  71. }