diff options
Diffstat (limited to 'src/corefx/System.Globalization.Native/idna.cpp')
-rw-r--r-- | src/corefx/System.Globalization.Native/idna.cpp | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/src/corefx/System.Globalization.Native/idna.cpp b/src/corefx/System.Globalization.Native/idna.cpp new file mode 100644 index 0000000000..4820d2c3f2 --- /dev/null +++ b/src/corefx/System.Globalization.Native/idna.cpp @@ -0,0 +1,80 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. +// + +#include <stdint.h> +#include <unicode/uidna.h> + +const uint32_t AllowUnassigned = 0x1; +const uint32_t UseStd3AsciiRules = 0x2; + +uint32_t GetOptions(uint32_t flags) +{ + // Using Nontransitional to Unicode and Check ContextJ to match the current behavior of .NET on Windows + uint32_t options = UIDNA_NONTRANSITIONAL_TO_UNICODE | UIDNA_CHECK_CONTEXTJ; + + if ((flags & AllowUnassigned) == AllowUnassigned) + { + options |= UIDNA_ALLOW_UNASSIGNED; + } + + if ((flags & UseStd3AsciiRules) == UseStd3AsciiRules) + { + options |= UIDNA_USE_STD3_RULES; + } + + return options; +} + +/* +Function: +ToASCII + +Used by System.Globalization.IdnMapping.GetAsciiCore to convert an Unicode +domain name to ASCII + +Return values: +0: internal error during conversion. +>0: the length of the converted string (not including the null terminator). +*/ +extern "C" int32_t GlobalizationNative_ToAscii( + uint32_t flags, const UChar* lpSrc, int32_t cwSrcLength, UChar* lpDst, int32_t cwDstLength) +{ + UErrorCode err = U_ZERO_ERROR; + UIDNAInfo info = UIDNA_INFO_INITIALIZER; + + UIDNA* pIdna = uidna_openUTS46(GetOptions(flags), &err); + + int32_t asciiStrLen = uidna_nameToASCII(pIdna, lpSrc, cwSrcLength, lpDst, cwDstLength, &info, &err); + + uidna_close(pIdna); + + return ((U_SUCCESS(err) || (err == U_BUFFER_OVERFLOW_ERROR)) && (info.errors == 0)) ? asciiStrLen : 0; +} + +/* +Function: +ToUnicode + +Used by System.Globalization.IdnMapping.GetUnicodeCore to convert an ASCII name +to Unicode + +Return values: +0: internal error during conversion. +>0: the length of the converted string (not including the null terminator). +*/ +extern "C" int32_t GlobalizationNative_ToUnicode( + int32_t flags, const UChar* lpSrc, int32_t cwSrcLength, UChar* lpDst, int32_t cwDstLength) +{ + UErrorCode err = U_ZERO_ERROR; + UIDNAInfo info = UIDNA_INFO_INITIALIZER; + + UIDNA* pIdna = uidna_openUTS46(GetOptions(flags), &err); + + int32_t unicodeStrLen = uidna_nameToUnicode(pIdna, lpSrc, cwSrcLength, lpDst, cwDstLength, &info, &err); + + uidna_close(pIdna); + + return ((U_SUCCESS(err) || (err == U_BUFFER_OVERFLOW_ERROR)) && (info.errors == 0)) ? unicodeStrLen : 0; +} |