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 | // |
5 | |
6 | #include <stdint.h> |
7 | #include "icushim.h" |
8 | |
9 | const uint32_t AllowUnassigned = 0x1; |
10 | const uint32_t UseStd3AsciiRules = 0x2; |
11 | |
12 | uint32_t GetOptions(uint32_t flags) |
13 | { |
14 | // Using Nontransitional to Unicode and Check ContextJ to match the current behavior of .NET on Windows |
15 | uint32_t options = UIDNA_NONTRANSITIONAL_TO_UNICODE | UIDNA_CHECK_CONTEXTJ; |
16 | |
17 | if ((flags & AllowUnassigned) == AllowUnassigned) |
18 | { |
19 | options |= UIDNA_ALLOW_UNASSIGNED; |
20 | } |
21 | |
22 | if ((flags & UseStd3AsciiRules) == UseStd3AsciiRules) |
23 | { |
24 | options |= UIDNA_USE_STD3_RULES; |
25 | } |
26 | |
27 | return options; |
28 | } |
29 | |
30 | /* |
31 | Function: |
32 | ToASCII |
33 | |
34 | Used by System.Globalization.IdnMapping.GetAsciiCore to convert an Unicode |
35 | domain name to ASCII |
36 | |
37 | Return values: |
38 | 0: internal error during conversion. |
39 | >0: the length of the converted string (not including the null terminator). |
40 | */ |
41 | extern "C" int32_t GlobalizationNative_ToAscii( |
42 | uint32_t flags, const UChar* lpSrc, int32_t cwSrcLength, UChar* lpDst, int32_t cwDstLength) |
43 | { |
44 | UErrorCode err = U_ZERO_ERROR; |
45 | UIDNAInfo info = UIDNA_INFO_INITIALIZER; |
46 | |
47 | UIDNA* pIdna = uidna_openUTS46(GetOptions(flags), &err); |
48 | |
49 | int32_t asciiStrLen = uidna_nameToASCII(pIdna, lpSrc, cwSrcLength, lpDst, cwDstLength, &info, &err); |
50 | |
51 | uidna_close(pIdna); |
52 | |
53 | return ((U_SUCCESS(err) || (err == U_BUFFER_OVERFLOW_ERROR)) && (info.errors == 0)) ? asciiStrLen : 0; |
54 | } |
55 | |
56 | /* |
57 | Function: |
58 | ToUnicode |
59 | |
60 | Used by System.Globalization.IdnMapping.GetUnicodeCore to convert an ASCII name |
61 | to Unicode |
62 | |
63 | Return values: |
64 | 0: internal error during conversion. |
65 | >0: the length of the converted string (not including the null terminator). |
66 | */ |
67 | extern "C" int32_t GlobalizationNative_ToUnicode( |
68 | int32_t flags, const UChar* lpSrc, int32_t cwSrcLength, UChar* lpDst, int32_t cwDstLength) |
69 | { |
70 | UErrorCode err = U_ZERO_ERROR; |
71 | UIDNAInfo info = UIDNA_INFO_INITIALIZER; |
72 | |
73 | UIDNA* pIdna = uidna_openUTS46(GetOptions(flags), &err); |
74 | |
75 | int32_t unicodeStrLen = uidna_nameToUnicode(pIdna, lpSrc, cwSrcLength, lpDst, cwDstLength, &info, &err); |
76 | |
77 | uidna_close(pIdna); |
78 | |
79 | return ((U_SUCCESS(err) || (err == U_BUFFER_OVERFLOW_ERROR)) && (info.errors == 0)) ? unicodeStrLen : 0; |
80 | } |
81 | |