IdnMapping.Windows.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Diagnostics;
  5. using System.Runtime.InteropServices;
  6. namespace System.Globalization
  7. {
  8. public sealed partial class IdnMapping
  9. {
  10. private unsafe string GetAsciiCore(string unicodeString, char* unicode, int count)
  11. {
  12. Debug.Assert(!GlobalizationMode.Invariant);
  13. Debug.Assert(unicodeString != null && unicodeString.Length >= count);
  14. uint flags = Flags;
  15. // Determine the required length
  16. int length = Interop.Normaliz.IdnToAscii(flags, unicode, count, null, 0);
  17. if (length == 0)
  18. {
  19. ThrowForZeroLength(unicode: true);
  20. }
  21. // Do the conversion
  22. const int StackAllocThreshold = 512; // arbitrary limit to switch from stack to heap allocation
  23. if (length < StackAllocThreshold)
  24. {
  25. char* output = stackalloc char[length];
  26. return GetAsciiCore(unicodeString, unicode, count, flags, output, length);
  27. }
  28. else
  29. {
  30. char[] output = new char[length];
  31. fixed (char* pOutput = &output[0])
  32. {
  33. return GetAsciiCore(unicodeString, unicode, count, flags, pOutput, length);
  34. }
  35. }
  36. }
  37. private unsafe string GetAsciiCore(string unicodeString, char* unicode, int count, uint flags, char* output, int outputLength)
  38. {
  39. Debug.Assert(!GlobalizationMode.Invariant);
  40. Debug.Assert(unicodeString != null && unicodeString.Length >= count);
  41. int length = Interop.Normaliz.IdnToAscii(flags, unicode, count, output, outputLength);
  42. if (length == 0)
  43. {
  44. ThrowForZeroLength(unicode: true);
  45. }
  46. Debug.Assert(length == outputLength);
  47. return GetStringForOutput(unicodeString, unicode, count, output, length);
  48. }
  49. private unsafe string GetUnicodeCore(string asciiString, char* ascii, int count)
  50. {
  51. Debug.Assert(!GlobalizationMode.Invariant);
  52. Debug.Assert(asciiString != null && asciiString.Length >= count);
  53. uint flags = Flags;
  54. // Determine the required length
  55. int length = Interop.Normaliz.IdnToUnicode(flags, ascii, count, null, 0);
  56. if (length == 0)
  57. {
  58. ThrowForZeroLength(unicode: false);
  59. }
  60. // Do the conversion
  61. const int StackAllocThreshold = 512; // arbitrary limit to switch from stack to heap allocation
  62. if (length < StackAllocThreshold)
  63. {
  64. char* output = stackalloc char[length];
  65. return GetUnicodeCore(asciiString, ascii, count, flags, output, length);
  66. }
  67. else
  68. {
  69. char[] output = new char[length];
  70. fixed (char* pOutput = &output[0])
  71. {
  72. return GetUnicodeCore(asciiString, ascii, count, flags, pOutput, length);
  73. }
  74. }
  75. }
  76. private unsafe string GetUnicodeCore(string asciiString, char* ascii, int count, uint flags, char* output, int outputLength)
  77. {
  78. Debug.Assert(!GlobalizationMode.Invariant);
  79. Debug.Assert(asciiString != null && asciiString.Length >= count);
  80. int length = Interop.Normaliz.IdnToUnicode(flags, ascii, count, output, outputLength);
  81. if (length == 0)
  82. {
  83. ThrowForZeroLength(unicode: false);
  84. }
  85. Debug.Assert(length == outputLength);
  86. return GetStringForOutput(asciiString, ascii, count, output, length);
  87. }
  88. // -----------------------------
  89. // ---- PAL layer ends here ----
  90. // -----------------------------
  91. private uint Flags
  92. {
  93. get
  94. {
  95. int flags =
  96. (AllowUnassigned ? Interop.Normaliz.IDN_ALLOW_UNASSIGNED : 0) |
  97. (UseStd3AsciiRules ? Interop.Normaliz.IDN_USE_STD3_ASCII_RULES : 0);
  98. return (uint)flags;
  99. }
  100. }
  101. private static void ThrowForZeroLength(bool unicode)
  102. {
  103. int lastError = Marshal.GetLastWin32Error();
  104. throw new ArgumentException(
  105. lastError == Interop.Errors.ERROR_INVALID_NAME ? SR.Argument_IdnIllegalName :
  106. (unicode ? SR.Argument_InvalidCharSequenceNoIndex : SR.Argument_IdnBadPunycode),
  107. unicode ? "unicode" : "ascii");
  108. }
  109. }
  110. }