mscorlib(4.0.0.0) API with additions
IdnMapping.cs
2 using System.Security;
3 using System.Text;
4 
5 namespace System.Globalization
6 {
8  public sealed class IdnMapping
9  {
10  private const int M_labelLimit = 63;
11 
12  private const int M_defaultNameLimit = 255;
13 
14  private const string M_strAcePrefix = "xn--";
15 
16  private static char[] M_Dots = new char[4]
17  {
18  '.',
19  '。',
20  '.',
21  '。'
22  };
23 
24  private bool m_bAllowUnassigned;
25 
26  private bool m_bUseStd3AsciiRules;
27 
28  private const int punycodeBase = 36;
29 
30  private const int tmin = 1;
31 
32  private const int tmax = 26;
33 
34  private const int skew = 38;
35 
36  private const int damp = 700;
37 
38  private const int initial_bias = 72;
39 
40  private const int initial_n = 128;
41 
42  private const char delimiter = '-';
43 
44  private const int maxint = 134217727;
45 
46  private const int IDN_ALLOW_UNASSIGNED = 1;
47 
48  private const int IDN_USE_STD3_ASCII_RULES = 2;
49 
50  private const int ERROR_INVALID_NAME = 123;
51 
55  public bool AllowUnassigned
56  {
57  get
58  {
59  return m_bAllowUnassigned;
60  }
61  set
62  {
63  m_bAllowUnassigned = value;
64  }
65  }
66 
70  public bool UseStd3AsciiRules
71  {
72  get
73  {
74  return m_bUseStd3AsciiRules;
75  }
76  set
77  {
78  m_bUseStd3AsciiRules = value;
79  }
80  }
81 
89  public string GetAscii(string unicode)
90  {
91  return GetAscii(unicode, 0);
92  }
93 
105  public string GetAscii(string unicode, int index)
106  {
107  if (unicode == null)
108  {
109  throw new ArgumentNullException("unicode");
110  }
111  return GetAscii(unicode, index, unicode.Length - index);
112  }
113 
127  public string GetAscii(string unicode, int index, int count)
128  {
129  if (unicode == null)
130  {
131  throw new ArgumentNullException("unicode");
132  }
133  if (index < 0 || count < 0)
134  {
135  throw new ArgumentOutOfRangeException((index < 0) ? "index" : "count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
136  }
137  if (index > unicode.Length)
138  {
139  throw new ArgumentOutOfRangeException("byteIndex", Environment.GetResourceString("ArgumentOutOfRange_Index"));
140  }
141  if (index > unicode.Length - count)
142  {
143  throw new ArgumentOutOfRangeException("unicode", Environment.GetResourceString("ArgumentOutOfRange_IndexCountBuffer"));
144  }
145  unicode = unicode.Substring(index, count);
146  if (Environment.IsWindows8OrAbove)
147  {
148  return GetAsciiUsingOS(unicode);
149  }
150  if (ValidateStd3AndAscii(unicode, UseStd3AsciiRules, bCheckAscii: true))
151  {
152  return unicode;
153  }
154  if (unicode[unicode.Length - 1] <= '\u001f')
155  {
156  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidCharSequence", unicode.Length - 1), "unicode");
157  }
158  bool flag = unicode.Length > 0 && IsDot(unicode[unicode.Length - 1]);
159  unicode = unicode.Normalize(m_bAllowUnassigned ? ((NormalizationForm)13) : ((NormalizationForm)269));
160  if (!flag && unicode.Length > 0 && IsDot(unicode[unicode.Length - 1]))
161  {
162  throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadLabelSize"), "unicode");
163  }
164  if (UseStd3AsciiRules)
165  {
166  ValidateStd3AndAscii(unicode, bUseStd3: true, bCheckAscii: false);
167  }
168  return punycode_encode(unicode);
169  }
170 
171  [SecuritySafeCritical]
172  private string GetAsciiUsingOS(string unicode)
173  {
174  if (unicode.Length == 0)
175  {
176  throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadLabelSize"), "unicode");
177  }
178  if (unicode[unicode.Length - 1] == '\0')
179  {
180  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidCharSequence", unicode.Length - 1), "unicode");
181  }
182  uint dwFlags = (uint)((AllowUnassigned ? 1 : 0) | (UseStd3AsciiRules ? 2 : 0));
183  int num = IdnToAscii(dwFlags, unicode, unicode.Length, null, 0);
184  if (num == 0)
185  {
186  int lastWin32Error = Marshal.GetLastWin32Error();
187  if (lastWin32Error == 123)
188  {
189  throw new ArgumentException(Environment.GetResourceString("Argument_IdnIllegalName"), "unicode");
190  }
191  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidCharSequenceNoIndex"), "unicode");
192  }
193  char[] array = new char[num];
194  num = IdnToAscii(dwFlags, unicode, unicode.Length, array, num);
195  if (num == 0)
196  {
197  int lastWin32Error = Marshal.GetLastWin32Error();
198  if (lastWin32Error == 123)
199  {
200  throw new ArgumentException(Environment.GetResourceString("Argument_IdnIllegalName"), "unicode");
201  }
202  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidCharSequenceNoIndex"), "unicode");
203  }
204  return new string(array, 0, num);
205  }
206 
214  public string GetUnicode(string ascii)
215  {
216  return GetUnicode(ascii, 0);
217  }
218 
230  public string GetUnicode(string ascii, int index)
231  {
232  if (ascii == null)
233  {
234  throw new ArgumentNullException("ascii");
235  }
236  return GetUnicode(ascii, index, ascii.Length - index);
237  }
238 
252  public string GetUnicode(string ascii, int index, int count)
253  {
254  if (ascii == null)
255  {
256  throw new ArgumentNullException("ascii");
257  }
258  if (index < 0 || count < 0)
259  {
260  throw new ArgumentOutOfRangeException((index < 0) ? "index" : "count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
261  }
262  if (index > ascii.Length)
263  {
264  throw new ArgumentOutOfRangeException("byteIndex", Environment.GetResourceString("ArgumentOutOfRange_Index"));
265  }
266  if (index > ascii.Length - count)
267  {
268  throw new ArgumentOutOfRangeException("ascii", Environment.GetResourceString("ArgumentOutOfRange_IndexCountBuffer"));
269  }
270  if (count > 0 && ascii[index + count - 1] == '\0')
271  {
272  throw new ArgumentException("ascii", Environment.GetResourceString("Argument_IdnBadPunycode"));
273  }
274  ascii = ascii.Substring(index, count);
275  if (Environment.IsWindows8OrAbove)
276  {
277  return GetUnicodeUsingOS(ascii);
278  }
279  string text = punycode_decode(ascii);
280  if (!ascii.Equals(GetAscii(text), StringComparison.OrdinalIgnoreCase))
281  {
282  throw new ArgumentException(Environment.GetResourceString("Argument_IdnIllegalName"), "ascii");
283  }
284  return text;
285  }
286 
287  [SecuritySafeCritical]
288  private string GetUnicodeUsingOS(string ascii)
289  {
290  uint dwFlags = (uint)((AllowUnassigned ? 1 : 0) | (UseStd3AsciiRules ? 2 : 0));
291  int num = IdnToUnicode(dwFlags, ascii, ascii.Length, null, 0);
292  if (num == 0)
293  {
294  int lastWin32Error = Marshal.GetLastWin32Error();
295  if (lastWin32Error == 123)
296  {
297  throw new ArgumentException(Environment.GetResourceString("Argument_IdnIllegalName"), "ascii");
298  }
299  throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadPunycode"), "ascii");
300  }
301  char[] array = new char[num];
302  num = IdnToUnicode(dwFlags, ascii, ascii.Length, array, num);
303  if (num == 0)
304  {
305  int lastWin32Error = Marshal.GetLastWin32Error();
306  if (lastWin32Error == 123)
307  {
308  throw new ArgumentException(Environment.GetResourceString("Argument_IdnIllegalName"), "ascii");
309  }
310  throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadPunycode"), "ascii");
311  }
312  return new string(array, 0, num);
313  }
314 
319  public override bool Equals(object obj)
320  {
321  IdnMapping idnMapping = obj as IdnMapping;
322  if (idnMapping != null)
323  {
324  if (m_bAllowUnassigned == idnMapping.m_bAllowUnassigned)
325  {
326  return m_bUseStd3AsciiRules == idnMapping.m_bUseStd3AsciiRules;
327  }
328  return false;
329  }
330  return false;
331  }
332 
335  public override int GetHashCode()
336  {
337  return (m_bAllowUnassigned ? 100 : 200) + (m_bUseStd3AsciiRules ? 1000 : 2000);
338  }
339 
340  private static bool IsSupplementary(int cTest)
341  {
342  return cTest >= 65536;
343  }
344 
345  private static bool IsDot(char c)
346  {
347  if (c != '.' && c != '。' && c != '.')
348  {
349  return c == '。';
350  }
351  return true;
352  }
353 
354  private static bool ValidateStd3AndAscii(string unicode, bool bUseStd3, bool bCheckAscii)
355  {
356  if (unicode.Length == 0)
357  {
358  throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadLabelSize"), "unicode");
359  }
360  int num = -1;
361  for (int i = 0; i < unicode.Length; i++)
362  {
363  if (unicode[i] <= '\u001f')
364  {
365  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidCharSequence", i), "unicode");
366  }
367  if (bCheckAscii && unicode[i] >= '\u007f')
368  {
369  return false;
370  }
371  if (IsDot(unicode[i]))
372  {
373  if (i == num + 1)
374  {
375  throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadLabelSize"), "unicode");
376  }
377  if (i - num > 64)
378  {
379  throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadLabelSize"), "Unicode");
380  }
381  if (bUseStd3 && i > 0)
382  {
383  ValidateStd3(unicode[i - 1], bNextToDot: true);
384  }
385  num = i;
386  }
387  else if (bUseStd3)
388  {
389  ValidateStd3(unicode[i], i == num + 1);
390  }
391  }
392  if (num == -1 && unicode.Length > 63)
393  {
394  throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadLabelSize"), "unicode");
395  }
396  if (unicode.Length > 255 - ((!IsDot(unicode[unicode.Length - 1])) ? 1 : 0))
397  {
398  throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadNameSize", 255 - ((!IsDot(unicode[unicode.Length - 1])) ? 1 : 0)), "unicode");
399  }
400  if (bUseStd3 && !IsDot(unicode[unicode.Length - 1]))
401  {
402  ValidateStd3(unicode[unicode.Length - 1], bNextToDot: true);
403  }
404  return true;
405  }
406 
407  private static void ValidateStd3(char c, bool bNextToDot)
408  {
409  if (c > ',')
410  {
411  switch (c)
412  {
413  default:
414  if ((c < '[' || c > '`') && (c < '{' || c > '\u007f') && (c != '-' || !bNextToDot))
415  {
416  return;
417  }
418  break;
419  case '/':
420  case ':':
421  case ';':
422  case '<':
423  case '=':
424  case '>':
425  case '?':
426  case '@':
427  break;
428  }
429  }
430  throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadStd3", c), "Unicode");
431  }
432 
433  private static bool HasUpperCaseFlag(char punychar)
434  {
435  if (punychar >= 'A')
436  {
437  return punychar <= 'Z';
438  }
439  return false;
440  }
441 
442  private static bool basic(uint cp)
443  {
444  return cp < 128;
445  }
446 
447  private static int decode_digit(char cp)
448  {
449  if (cp >= '0' && cp <= '9')
450  {
451  return cp - 48 + 26;
452  }
453  if (cp >= 'a' && cp <= 'z')
454  {
455  return cp - 97;
456  }
457  if (cp >= 'A' && cp <= 'Z')
458  {
459  return cp - 65;
460  }
461  throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadPunycode"), "ascii");
462  }
463 
464  private static char encode_digit(int d)
465  {
466  if (d > 25)
467  {
468  return (char)(d - 26 + 48);
469  }
470  return (char)(d + 97);
471  }
472 
473  private static char encode_basic(char bcp)
474  {
475  if (HasUpperCaseFlag(bcp))
476  {
477  bcp = (char)(bcp + 32);
478  }
479  return bcp;
480  }
481 
482  private static int adapt(int delta, int numpoints, bool firsttime)
483  {
484  delta = (firsttime ? (delta / 700) : (delta / 2));
485  delta += delta / numpoints;
486  uint num = 0u;
487  while (delta > 455)
488  {
489  delta /= 35;
490  num += 36;
491  }
492  return (int)(num + 36 * delta / (delta + 38));
493  }
494 
495  private static string punycode_encode(string unicode)
496  {
497  if (unicode.Length == 0)
498  {
499  throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadLabelSize"), "unicode");
500  }
501  StringBuilder stringBuilder = new StringBuilder(unicode.Length);
502  int num = 0;
503  int num2 = 0;
504  int num3 = 0;
505  while (num < unicode.Length)
506  {
507  num = unicode.IndexOfAny(M_Dots, num2);
508  if (num < 0)
509  {
510  num = unicode.Length;
511  }
512  if (num == num2)
513  {
514  if (num == unicode.Length)
515  {
516  break;
517  }
518  throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadLabelSize"), "unicode");
519  }
520  stringBuilder.Append("xn--");
521  bool flag = false;
522  BidiCategory bidiCategory = CharUnicodeInfo.GetBidiCategory(unicode, num2);
523  if (bidiCategory == BidiCategory.RightToLeft || bidiCategory == BidiCategory.RightToLeftArabic)
524  {
525  flag = true;
526  int num4 = num - 1;
527  if (char.IsLowSurrogate(unicode, num4))
528  {
529  num4--;
530  }
531  bidiCategory = CharUnicodeInfo.GetBidiCategory(unicode, num4);
532  if (bidiCategory != BidiCategory.RightToLeft && bidiCategory != BidiCategory.RightToLeftArabic)
533  {
534  throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadBidi"), "unicode");
535  }
536  }
537  int num5 = 0;
538  for (int i = num2; i < num; i++)
539  {
540  BidiCategory bidiCategory2 = CharUnicodeInfo.GetBidiCategory(unicode, i);
541  if (flag && bidiCategory2 == BidiCategory.LeftToRight)
542  {
543  throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadBidi"), "unicode");
544  }
545  if (!flag && (bidiCategory2 == BidiCategory.RightToLeft || bidiCategory2 == BidiCategory.RightToLeftArabic))
546  {
547  throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadBidi"), "unicode");
548  }
549  if (basic(unicode[i]))
550  {
551  stringBuilder.Append(encode_basic(unicode[i]));
552  num5++;
553  }
554  else if (char.IsSurrogatePair(unicode, i))
555  {
556  i++;
557  }
558  }
559  int num6 = num5;
560  if (num6 == num - num2)
561  {
562  stringBuilder.Remove(num3, "xn--".Length);
563  }
564  else
565  {
566  if (unicode.Length - num2 >= "xn--".Length && unicode.Substring(num2, "xn--".Length).Equals("xn--", StringComparison.OrdinalIgnoreCase))
567  {
568  throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadPunycode"), "unicode");
569  }
570  int num7 = 0;
571  if (num6 > 0)
572  {
573  stringBuilder.Append('-');
574  }
575  int num8 = 128;
576  int num9 = 0;
577  int num10 = 72;
578  while (num5 < num - num2)
579  {
580  int num11 = 0;
581  int num12 = 134217727;
582  for (int j = num2; j < num; j += ((!IsSupplementary(num11)) ? 1 : 2))
583  {
584  num11 = char.ConvertToUtf32(unicode, j);
585  if (num11 >= num8 && num11 < num12)
586  {
587  num12 = num11;
588  }
589  }
590  num9 += (num12 - num8) * (num5 - num7 + 1);
591  num8 = num12;
592  for (int j = num2; j < num; j += ((!IsSupplementary(num11)) ? 1 : 2))
593  {
594  num11 = char.ConvertToUtf32(unicode, j);
595  if (num11 < num8)
596  {
597  num9++;
598  }
599  if (num11 != num8)
600  {
601  continue;
602  }
603  int num13 = num9;
604  int num14 = 36;
605  while (true)
606  {
607  int num15 = (num14 <= num10) ? 1 : ((num14 >= num10 + 26) ? 26 : (num14 - num10));
608  if (num13 < num15)
609  {
610  break;
611  }
612  stringBuilder.Append(encode_digit(num15 + (num13 - num15) % (36 - num15)));
613  num13 = (num13 - num15) / (36 - num15);
614  num14 += 36;
615  }
616  stringBuilder.Append(encode_digit(num13));
617  num10 = adapt(num9, num5 - num7 + 1, num5 == num6);
618  num9 = 0;
619  num5++;
620  if (IsSupplementary(num12))
621  {
622  num5++;
623  num7++;
624  }
625  }
626  num9++;
627  num8++;
628  }
629  }
630  if (stringBuilder.Length - num3 > 63)
631  {
632  throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadLabelSize"), "unicode");
633  }
634  if (num != unicode.Length)
635  {
636  stringBuilder.Append('.');
637  }
638  num2 = num + 1;
639  num3 = stringBuilder.Length;
640  }
641  if (stringBuilder.Length > 255 - ((!IsDot(unicode[unicode.Length - 1])) ? 1 : 0))
642  {
643  throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadNameSize", 255 - ((!IsDot(unicode[unicode.Length - 1])) ? 1 : 0)), "unicode");
644  }
645  return stringBuilder.ToString();
646  }
647 
648  private static string punycode_decode(string ascii)
649  {
650  if (ascii.Length == 0)
651  {
652  throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadLabelSize"), "ascii");
653  }
654  if (ascii.Length > 255 - ((!IsDot(ascii[ascii.Length - 1])) ? 1 : 0))
655  {
656  throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadNameSize", 255 - ((!IsDot(ascii[ascii.Length - 1])) ? 1 : 0)), "ascii");
657  }
658  StringBuilder stringBuilder = new StringBuilder(ascii.Length);
659  int num = 0;
660  int num2 = 0;
661  int num3 = 0;
662  while (num < ascii.Length)
663  {
664  num = ascii.IndexOf('.', num2);
665  if (num < 0 || num > ascii.Length)
666  {
667  num = ascii.Length;
668  }
669  if (num == num2)
670  {
671  if (num == ascii.Length)
672  {
673  break;
674  }
675  throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadLabelSize"), "ascii");
676  }
677  if (num - num2 > 63)
678  {
679  throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadLabelSize"), "ascii");
680  }
681  if (ascii.Length < "xn--".Length + num2 || !ascii.Substring(num2, "xn--".Length).Equals("xn--", StringComparison.OrdinalIgnoreCase))
682  {
683  stringBuilder.Append(ascii.Substring(num2, num - num2));
684  }
685  else
686  {
687  num2 += "xn--".Length;
688  int num4 = ascii.LastIndexOf('-', num - 1);
689  if (num4 == num - 1)
690  {
691  throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadPunycode"), "ascii");
692  }
693  int num5;
694  if (num4 <= num2)
695  {
696  num5 = 0;
697  }
698  else
699  {
700  num5 = num4 - num2;
701  for (int i = num2; i < num2 + num5; i++)
702  {
703  if (ascii[i] > '\u007f')
704  {
705  throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadPunycode"), "ascii");
706  }
707  stringBuilder.Append((char)((ascii[i] >= 'A' && ascii[i] <= 'Z') ? (ascii[i] - 65 + 97) : ascii[i]));
708  }
709  }
710  int num6 = num2 + ((num5 > 0) ? (num5 + 1) : 0);
711  int num7 = 128;
712  int num8 = 72;
713  int num9 = 0;
714  int num10 = 0;
715  while (num6 < num)
716  {
717  int num11 = num9;
718  int num12 = 1;
719  int num13 = 36;
720  while (true)
721  {
722  if (num6 >= num)
723  {
724  throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadPunycode"), "ascii");
725  }
726  int num15 = decode_digit(ascii[num6++]);
727  if (num15 > (134217727 - num9) / num12)
728  {
729  throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadPunycode"), "ascii");
730  }
731  num9 += num15 * num12;
732  int num16 = (num13 <= num8) ? 1 : ((num13 >= num8 + 26) ? 26 : (num13 - num8));
733  if (num15 < num16)
734  {
735  break;
736  }
737  if (num12 > 134217727 / (36 - num16))
738  {
739  throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadPunycode"), "ascii");
740  }
741  num12 *= 36 - num16;
742  num13 += 36;
743  }
744  num8 = adapt(num9 - num11, stringBuilder.Length - num3 - num10 + 1, num11 == 0);
745  if (num9 / (stringBuilder.Length - num3 - num10 + 1) > 134217727 - num7)
746  {
747  throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadPunycode"), "ascii");
748  }
749  num7 += num9 / (stringBuilder.Length - num3 - num10 + 1);
750  num9 %= stringBuilder.Length - num3 - num10 + 1;
751  if (num7 < 0 || num7 > 1114111 || (num7 >= 55296 && num7 <= 57343))
752  {
753  throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadPunycode"), "ascii");
754  }
755  string value = char.ConvertFromUtf32(num7);
756  int num18;
757  if (num10 > 0)
758  {
759  int num17 = num9;
760  num18 = num3;
761  while (num17 > 0)
762  {
763  if (num18 >= stringBuilder.Length)
764  {
765  throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadPunycode"), "ascii");
766  }
767  if (char.IsSurrogate(stringBuilder[num18]))
768  {
769  num18++;
770  }
771  num17--;
772  num18++;
773  }
774  }
775  else
776  {
777  num18 = num3 + num9;
778  }
779  stringBuilder.Insert(num18, value);
780  if (IsSupplementary(num7))
781  {
782  num10++;
783  }
784  num9++;
785  }
786  bool flag = false;
787  BidiCategory bidiCategory = CharUnicodeInfo.GetBidiCategory(stringBuilder.ToString(), num3);
788  if (bidiCategory == BidiCategory.RightToLeft || bidiCategory == BidiCategory.RightToLeftArabic)
789  {
790  flag = true;
791  }
792  for (int j = num3; j < stringBuilder.Length; j++)
793  {
794  if (!char.IsLowSurrogate(stringBuilder.ToString(), j))
795  {
796  bidiCategory = CharUnicodeInfo.GetBidiCategory(stringBuilder.ToString(), j);
797  if ((flag && bidiCategory == BidiCategory.LeftToRight) || (!flag && (bidiCategory == BidiCategory.RightToLeft || bidiCategory == BidiCategory.RightToLeftArabic)))
798  {
799  throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadBidi"), "ascii");
800  }
801  }
802  }
803  if (flag && bidiCategory != BidiCategory.RightToLeft && bidiCategory != BidiCategory.RightToLeftArabic)
804  {
805  throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadBidi"), "ascii");
806  }
807  }
808  if (num - num2 > 63)
809  {
810  throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadLabelSize"), "ascii");
811  }
812  if (num != ascii.Length)
813  {
814  stringBuilder.Append('.');
815  }
816  num2 = num + 1;
817  num3 = stringBuilder.Length;
818  }
819  if (stringBuilder.Length > 255 - ((!IsDot(stringBuilder[stringBuilder.Length - 1])) ? 1 : 0))
820  {
821  throw new ArgumentException(Environment.GetResourceString("Argument_IdnBadNameSize", 255 - ((!IsDot(stringBuilder[stringBuilder.Length - 1])) ? 1 : 0)), "ascii");
822  }
823  return stringBuilder.ToString();
824  }
825 
826  [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
827  [SecurityCritical]
828  [SuppressUnmanagedCodeSecurity]
829  private static extern int IdnToAscii(uint dwFlags, [In] [MarshalAs(UnmanagedType.LPWStr)] string lpUnicodeCharStr, int cchUnicodeChar, [Out] char[] lpASCIICharStr, int cchASCIIChar);
830 
831  [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
832  [SecurityCritical]
833  [SuppressUnmanagedCodeSecurity]
834  private static extern int IdnToUnicode(uint dwFlags, [In] [MarshalAs(UnmanagedType.LPWStr)] string lpASCIICharStr, int cchASCIIChar, [Out] char[] lpUnicodeCharStr, int cchUnicodeChar);
835  }
836 }
bool UseStd3AsciiRules
Gets or sets a value that indicates whether standard or relaxed naming conventions are used in operat...
Definition: IdnMapping.cs:71
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
string GetUnicode(string ascii)
Decodes a string of one or more domain name labels, encoded according to the IDNA standard,...
Definition: IdnMapping.cs:214
unsafe override string ToString()
Converts the value of this instance to a T:System.String.
string GetAscii(string unicode, int index, int count)
Encodes the specified number of characters in a substring of domain name labels that include Unicode ...
Definition: IdnMapping.cs:127
override int GetHashCode()
Returns a hash code for this T:System.Globalization.IdnMapping object.
Definition: IdnMapping.cs:335
unsafe StringBuilder Insert(int index, string value, int count)
Inserts one or more copies of a specified string into this instance at the specified character positi...
StringComparison
Specifies the culture, case, and sort rules to be used by certain overloads of the M:System....
Definition: __Canon.cs:3
The exception that is thrown when the value of an argument is outside the allowable range of values a...
string GetAscii(string unicode)
Encodes a string of domain name labels that consist of Unicode characters to a string of displayable ...
Definition: IdnMapping.cs:89
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
StringBuilder Append(char value, int repeatCount)
Appends a specified number of copies of the string representation of a Unicode character to this inst...
string GetAscii(string unicode, int index)
Encodes a substring of domain name labels that include Unicode characters outside the US-ASCII charac...
Definition: IdnMapping.cs:105
NormalizationForm
Defines the type of normalization to perform.
UnmanagedType
Identifies how to marshal parameters or fields to unmanaged code.
Definition: UnmanagedType.cs:7
int Length
Gets or sets the length of the current T:System.Text.StringBuilder object.
Provides a collection of methods for allocating unmanaged memory, copying unmanaged memory blocks,...
Definition: Marshal.cs:15
CharSet
Dictates which character set marshaled strings should use.
Definition: CharSet.cs:7
Represents a mutable string of characters. This class cannot be inherited.To browse the ....
The exception that is thrown when one of the arguments provided to a method is not valid.
bool AllowUnassigned
Gets or sets a value that indicates whether unassigned Unicode code points are used in operations per...
Definition: IdnMapping.cs:56
string GetUnicode(string ascii, int index, int count)
Decodes a substring of a specified length that contains one or more domain name labels,...
Definition: IdnMapping.cs:252
Supports the use of non-ASCII characters for Internet domain names. This class cannot be inherited.
Definition: IdnMapping.cs:8
static int GetLastWin32Error()
Returns the error code returned by the last unmanaged function that was called using platform invoke ...
string GetUnicode(string ascii, int index)
Decodes a substring of one or more domain name labels, encoded according to the IDNA standard,...
Definition: IdnMapping.cs:230
override bool Equals(object obj)
Indicates whether a specified object and the current T:System.Globalization.IdnMapping object are equ...
Definition: IdnMapping.cs:319
StringBuilder Remove(int startIndex, int length)
Removes the specified range of characters from this instance.