-
Notifications
You must be signed in to change notification settings - Fork 21
/
Punycode.cpp
261 lines (232 loc) · 6.86 KB
/
Punycode.cpp
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
//===--- Punycode.cpp - Unicode to Punycode transcoding -------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "Punycode.h"
#include <vector>
#include <cstdint>
using namespace swift;
using namespace Punycode;
// RFC 3492
// Section 5: Parameter values for Punycode
static const int base = 36;
static const int tmin = 1;
static const int tmax = 26;
static const int skew = 38;
static const int damp = 700;
static const int initial_bias = 72;
static const uint32_t initial_n = 128;
static const char delimiter = '_';
static char digit_value(int digit) {
assert(digit < base && "invalid punycode digit");
if (digit < 26)
return 'a' + digit;
return 'A' - 26 + digit;
}
static int digit_index(char value) {
if (value >= 'a' && value <= 'z')
return value - 'a';
if (value >= 'A' && value <= 'J')
return value - 'A' + 26;
return -1;
}
static bool isValidUnicodeScalar(uint32_t S) {
return (S < 0xD800) || (S >= 0xE000 && S <= 0x1FFFFF);
}
// Section 6.1: Bias adaptation function
static int adapt(int delta, int numpoints, bool firsttime) {
if (firsttime)
delta = delta / damp;
else
delta = delta / 2;
delta += delta / numpoints;
int k = 0;
while (delta > ((base - tmin) * tmax) / 2) {
delta /= base - tmin;
k += base;
}
return k + (((base - tmin + 1) * delta) / (delta + skew));
}
// Section 6.2: Decoding procedure
bool Punycode::decodePunycode(StringRef InputPunycode,
std::vector<uint32_t> &OutCodePoints) {
OutCodePoints.clear();
OutCodePoints.reserve(InputPunycode.size());
// -- Build the decoded string as UTF32 first because we need random access.
uint32_t n = initial_n;
int i = 0;
int bias = initial_bias;
/// let output = an empty string indexed from 0
// consume all code points before the last delimiter (if there is one)
// and copy them to output,
size_t lastDelimiter = InputPunycode.find_last_of(delimiter);
if (lastDelimiter != StringRef::npos) {
for (char c : InputPunycode.slice(0, lastDelimiter)) {
// fail on any non-basic code point
if (static_cast<unsigned char>(c) > 0x7f)
return true;
OutCodePoints.push_back(c);
}
// if more than zero code points were consumed then consume one more
// (which will be the last delimiter)
InputPunycode =
InputPunycode.slice(lastDelimiter + 1, InputPunycode.size());
}
while (!InputPunycode.empty()) {
int oldi = i;
int w = 1;
for (int k = base; ; k += base) {
// consume a code point, or fail if there was none to consume
if (InputPunycode.empty())
return true;
char codePoint = InputPunycode.front();
InputPunycode = InputPunycode.slice(1, InputPunycode.size());
// let digit = the code point's digit-value, fail if it has none
int digit = digit_index(codePoint);
if (digit < 0)
return true;
i = i + digit * w;
int t = k <= bias ? tmin
: k >= bias + tmax ? tmax
: k - bias;
if (digit < t)
break;
w = w * (base - t);
}
bias = adapt(i - oldi, OutCodePoints.size() + 1, oldi == 0);
n = n + i / (OutCodePoints.size() + 1);
i = i % (OutCodePoints.size() + 1);
// if n is a basic code point then fail
if (n < 0x80)
return true;
// insert n into output at position i
OutCodePoints.insert(OutCodePoints.begin() + i, n);
i++;
}
return true;
}
// Section 6.3: Encoding procedure
bool Punycode::encodePunycode(const std::vector<uint32_t> &InputCodePoints,
std::string &OutPunycode) {
OutPunycode.clear();
uint32_t n = initial_n;
int delta = 0;
int bias = initial_bias;
// let h = b = the number of basic code points in the input
// copy them to the output in order...
size_t h = 0;
for (auto C : InputCodePoints) {
if (C < 0x80) {
++h;
OutPunycode.push_back(C);
}
if (!isValidUnicodeScalar(C)) {
OutPunycode.clear();
return false;
}
}
size_t b = h;
// ...followed by a delimiter if b > 0
if (b > 0)
OutPunycode.push_back(delimiter);
while (h < InputCodePoints.size()) {
// let m = the minimum code point >= n in the input
uint32_t m = 0x10FFFF;
for (auto codePoint : InputCodePoints) {
if (codePoint >= n && codePoint < m)
m = codePoint;
}
delta = delta + (m - n) * (h + 1);
n = m;
for (auto c : InputCodePoints) {
if (c < n) ++delta;
if (c == n) {
int q = delta;
for (int k = base; ; k += base) {
int t = k <= bias ? tmin
: k >= bias + tmax ? tmax
: k - bias;
if (q < t) break;
OutPunycode.push_back(digit_value(t + ((q - t) % (base - t))));
q = (q - t) / (base - t);
}
OutPunycode.push_back(digit_value(q));
bias = adapt(delta, h + 1, h == b);
delta = 0;
++h;
}
}
++delta; ++n;
}
return true;
}
static bool encodeToUTF8(const std::vector<uint32_t> &Scalars, std::string &OutUTF8) {
for (auto S : Scalars) {
if (!isValidUnicodeScalar(S)) {
OutUTF8.clear();
return false;
}
unsigned Bytes = 0;
if (S < 0x80)
Bytes = 1;
else if (S < 0x800)
Bytes = 2;
else if (S < 0x10000)
Bytes = 3;
else
Bytes = 4;
switch (Bytes) {
case 1:
OutUTF8.push_back(S);
break;
case 2: {
uint8_t Byte2 = (S | 0x80) & 0xBF;
S >>= 6;
uint8_t Byte1 = S | 0xC0;
OutUTF8.push_back(Byte1);
OutUTF8.push_back(Byte2);
break;
}
case 3: {
uint8_t Byte3 = (S | 0x80) & 0xBF;
S >>= 6;
uint8_t Byte2 = (S | 0x80) & 0xBF;
S >>= 6;
uint8_t Byte1 = S | 0xE0;
OutUTF8.push_back(Byte1);
OutUTF8.push_back(Byte2);
OutUTF8.push_back(Byte3);
break;
}
case 4: {
uint8_t Byte4 = (S | 0x80) & 0xBF;
S >>= 6;
uint8_t Byte3 = (S | 0x80) & 0xBF;
S >>= 6;
uint8_t Byte2 = (S | 0x80) & 0xBF;
S >>= 6;
uint8_t Byte1 = S | 0xF0;
OutUTF8.push_back(Byte1);
OutUTF8.push_back(Byte2);
OutUTF8.push_back(Byte3);
OutUTF8.push_back(Byte4);
break;
}
}
}
return true;
}
bool Punycode::decodePunycodeUTF8(StringRef InputPunycode,
std::string &OutUTF8) {
std::vector<uint32_t> OutCodePoints;
if (!decodePunycode(InputPunycode, OutCodePoints))
return false;
return encodeToUTF8(OutCodePoints, OutUTF8);
}