-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
pal_idna.c
99 lines (75 loc) · 2.72 KB
/
pal_idna.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
//
#include <stdint.h>
#include "pal_icushim_internal.h"
#include "pal_idna.h"
#if defined(TARGET_WINDOWS)
// Windows icu headers doesn't define this member as it is marked as deprecated as of ICU 55.
enum {
UIDNA_ALLOW_UNASSIGNED=1
};
#endif
static const uint32_t AllowUnassigned = 0x1;
static const uint32_t UseStd3AsciiRules = 0x2;
static uint32_t GetOptions(uint32_t flags, uint32_t useToAsciiFlags)
{
uint32_t options = UIDNA_CHECK_CONTEXTJ;
if ((flags & AllowUnassigned) == AllowUnassigned)
{
options |= UIDNA_ALLOW_UNASSIGNED;
}
if ((flags & UseStd3AsciiRules) == UseStd3AsciiRules)
{
options |= UIDNA_USE_STD3_RULES;
}
if (useToAsciiFlags)
{
options |= UIDNA_NONTRANSITIONAL_TO_ASCII;
}
else
{
options |= UIDNA_NONTRANSITIONAL_TO_UNICODE;
}
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).
*/
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, /* useToAsciiFlags */ 1), &err);
int32_t asciiStrLen = uidna_nameToASCII(pIdna, lpSrc, cwSrcLength, lpDst, cwDstLength, &info, &err);
// To have a consistent behavior with Windows, we mask out the error when having 2 hyphens in the third and fourth place.
info.errors &= (uint32_t)~UIDNA_ERROR_HYPHEN_3_4;
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).
*/
int32_t GlobalizationNative_ToUnicode(
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, /* useToAsciiFlags */ 0), &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;
}