/
SimpleIdnaMappingTable.kt
267 lines (230 loc) · 6.8 KB
/
SimpleIdnaMappingTable.kt
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
261
262
263
264
265
266
267
/*
* Copyright (C) 2023 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package okhttp3.internal.idn
import java.io.IOException
import okio.Buffer
import okio.BufferedSink
import okio.BufferedSource
import okio.ByteString
import okio.ByteString.Companion.encodeUtf8
import okio.Options
/**
* A decoded [mapping table] that can perform the [mapping step] of IDNA processing.
*
* This implementation is optimized for readability over efficiency.
*
* This implements non-transitional processing by preserving deviation characters.
*
* This implementation's STD3 rules are configured to `UseSTD3ASCIIRules=false`. This is
* permissive and permits the `_` character.
*
* [mapping table]: https://www.unicode.org/reports/tr46/#IDNA_Mapping_Table
* [mapping step]: https://www.unicode.org/reports/tr46/#ProcessingStepMap
*/
class SimpleIdnaMappingTable internal constructor(
internal val mappings: List<Mapping>,
) {
/**
* Returns true if the [codePoint] was applied successfully. Returns false if it was disallowed.
*/
fun map(
codePoint: Int,
sink: BufferedSink,
): Boolean {
val index =
mappings.binarySearch {
when {
it.sourceCodePoint1 < codePoint -> -1
it.sourceCodePoint0 > codePoint -> 1
else -> 0
}
}
// Code points must be in 0..0x10ffff.
require(index in mappings.indices) { "unexpected code point: $codePoint" }
val mapping = mappings[index]
var result = true
when (mapping.type) {
TYPE_IGNORED -> Unit
TYPE_MAPPED, TYPE_DISALLOWED_STD3_MAPPED -> {
sink.write(mapping.mappedTo)
}
TYPE_DEVIATION, TYPE_DISALLOWED_STD3_VALID, TYPE_VALID -> {
sink.writeUtf8CodePoint(codePoint)
}
TYPE_DISALLOWED -> {
sink.writeUtf8CodePoint(codePoint)
result = false
}
}
return result
}
}
private val optionsDelimiter =
Options.of(
// 0.
".".encodeUtf8(),
// 1.
" ".encodeUtf8(),
// 2.
";".encodeUtf8(),
// 3.
"#".encodeUtf8(),
// 4.
"\n".encodeUtf8(),
)
private val optionsDot =
Options.of(
// 0.
".".encodeUtf8(),
)
private const val DELIMITER_DOT = 0
private const val DELIMITER_SPACE = 1
private const val DELIMITER_SEMICOLON = 2
private const val DELIMITER_HASH = 3
private const val DELIMITER_NEWLINE = 4
private val optionsType =
Options.of(
// 0.
"deviation ".encodeUtf8(),
// 1.
"disallowed ".encodeUtf8(),
// 2.
"disallowed_STD3_mapped ".encodeUtf8(),
// 3.
"disallowed_STD3_valid ".encodeUtf8(),
// 4.
"ignored ".encodeUtf8(),
// 5.
"mapped ".encodeUtf8(),
// 6.
"valid ".encodeUtf8(),
)
internal const val TYPE_DEVIATION = 0
internal const val TYPE_DISALLOWED = 1
internal const val TYPE_DISALLOWED_STD3_MAPPED = 2
internal const val TYPE_DISALLOWED_STD3_VALID = 3
internal const val TYPE_IGNORED = 4
internal const val TYPE_MAPPED = 5
internal const val TYPE_VALID = 6
private fun BufferedSource.skipWhitespace() {
while (!exhausted()) {
if (buffer[0] != ' '.code.toByte()) return
skip(1L)
}
}
private fun BufferedSource.skipRestOfLine() {
when (val newline = indexOf('\n'.code.toByte())) {
-1L -> skip(buffer.size) // Exhaust this source.
else -> skip(newline + 1)
}
}
/**
* Reads lines from `IdnaMappingTable.txt`.
*
* Comment lines are either blank or start with a `#` character. Lines may also end with a comment.
* All comments are ignored.
*
* Regular lines contain fields separated by semicolons.
*
* The first element on each line is a single hex code point (like 0041) or a hex code point range
* (like 0030..0039).
*
* The second element on each line is a mapping type, like `valid` or `mapped`.
*
* For lines that contain a mapping target, the next thing is a sequence of hex code points (like
* 0031 2044 0034).
*
* All other data is ignored.
*/
fun BufferedSource.readPlainTextIdnaMappingTable(): SimpleIdnaMappingTable {
val mappedTo = Buffer()
val result = mutableListOf<Mapping>()
while (!exhausted()) {
// Skip comment and empty lines.
when (select(optionsDelimiter)) {
DELIMITER_HASH -> {
skipRestOfLine()
continue
}
DELIMITER_NEWLINE -> {
continue
}
DELIMITER_DOT, DELIMITER_SPACE, DELIMITER_SEMICOLON -> {
throw IOException("unexpected delimiter")
}
}
// "002F" or "0000..002C"
val sourceCodePoint0 = readHexadecimalUnsignedLong()
val sourceCodePoint1 =
when (select(optionsDot)) {
DELIMITER_DOT -> {
if (readByte() != '.'.code.toByte()) throw IOException("expected '..'")
readHexadecimalUnsignedLong()
}
else -> sourceCodePoint0
}
skipWhitespace()
if (readByte() != ';'.code.toByte()) throw IOException("expected ';'")
// "valid" or "mapped"
skipWhitespace()
val type = select(optionsType)
when (type) {
TYPE_DEVIATION, TYPE_MAPPED, TYPE_DISALLOWED_STD3_MAPPED -> {
skipWhitespace()
if (readByte() != ';'.code.toByte()) throw IOException("expected ';'")
// Like "0061" or "0031 2044 0034".
while (true) {
skipWhitespace()
when (select(optionsDelimiter)) {
DELIMITER_HASH -> {
break
}
DELIMITER_DOT, DELIMITER_SEMICOLON, DELIMITER_NEWLINE -> {
throw IOException("unexpected delimiter")
}
}
mappedTo.writeUtf8CodePoint(readHexadecimalUnsignedLong().toInt())
}
}
TYPE_DISALLOWED, TYPE_DISALLOWED_STD3_VALID, TYPE_IGNORED, TYPE_VALID -> Unit
else -> throw IOException("unexpected type")
}
skipRestOfLine()
result +=
Mapping(
sourceCodePoint0.toInt(),
sourceCodePoint1.toInt(),
type,
mappedTo.readByteString(),
)
}
return SimpleIdnaMappingTable(result)
}
internal data class Mapping(
val sourceCodePoint0: Int,
val sourceCodePoint1: Int,
val type: Int,
val mappedTo: ByteString,
) {
val section: Int
get() = sourceCodePoint0 and 0x1fff80
val rangeStart: Int
get() = sourceCodePoint0 and 0x7f
val hasSingleSourceCodePoint: Boolean
get() = sourceCodePoint0 == sourceCodePoint1
val spansSections: Boolean
get() = (sourceCodePoint0 and 0x1fff80) != (sourceCodePoint1 and 0x1fff80)
}