forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/golang.org/x/net/idna/punycode.go

404 lines
4.8 KiB
Go
Raw Normal View History

2024-02-18 10:42:21 +00:00
// Code generated by running "go generate" in golang.org/x/text. DO NOT EDIT.
// Copyright 2016 The Go Authors. All rights reserved.
2024-02-18 10:42:21 +00:00
// Use of this source code is governed by a BSD-style
2024-02-18 10:42:21 +00:00
// license that can be found in the LICENSE file.
package idna
// This file implements the Punycode algorithm from RFC 3492.
import (
"math"
"strings"
"unicode/utf8"
)
// These parameter values are specified in section 5.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// All computation is done with int32s, so that overflow behavior is identical
2024-02-18 10:42:21 +00:00
// regardless of whether int is 32-bit or 64-bit.
2024-02-18 10:42:21 +00:00
const (
base int32 = 36
damp int32 = 700
2024-02-18 10:42:21 +00:00
initialBias int32 = 72
initialN int32 = 128
skew int32 = 38
tmax int32 = 26
tmin int32 = 1
2024-02-18 10:42:21 +00:00
)
func punyError(s string) error { return &labelError{s, "A3"} }
// decode decodes a string as specified in section 6.2.
2024-02-18 10:42:21 +00:00
func decode(encoded string) (string, error) {
2024-02-18 10:42:21 +00:00
if encoded == "" {
2024-02-18 10:42:21 +00:00
return "", nil
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
pos := 1 + strings.LastIndex(encoded, "-")
2024-02-18 10:42:21 +00:00
if pos == 1 {
2024-02-18 10:42:21 +00:00
return "", punyError(encoded)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if pos == len(encoded) {
2024-02-18 10:42:21 +00:00
return encoded[:len(encoded)-1], nil
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
output := make([]rune, 0, len(encoded))
2024-02-18 10:42:21 +00:00
if pos != 0 {
2024-02-18 10:42:21 +00:00
for _, r := range encoded[:pos-1] {
2024-02-18 10:42:21 +00:00
output = append(output, r)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
i, n, bias := int32(0), initialN, initialBias
2024-02-18 10:42:21 +00:00
overflow := false
2024-02-18 10:42:21 +00:00
for pos < len(encoded) {
2024-02-18 10:42:21 +00:00
oldI, w := i, int32(1)
2024-02-18 10:42:21 +00:00
for k := base; ; k += base {
2024-02-18 10:42:21 +00:00
if pos == len(encoded) {
2024-02-18 10:42:21 +00:00
return "", punyError(encoded)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
digit, ok := decodeDigit(encoded[pos])
2024-02-18 10:42:21 +00:00
if !ok {
2024-02-18 10:42:21 +00:00
return "", punyError(encoded)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
pos++
2024-02-18 10:42:21 +00:00
i, overflow = madd(i, digit, w)
2024-02-18 10:42:21 +00:00
if overflow {
2024-02-18 10:42:21 +00:00
return "", punyError(encoded)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
t := k - bias
2024-02-18 10:42:21 +00:00
if k <= bias {
2024-02-18 10:42:21 +00:00
t = tmin
2024-02-18 10:42:21 +00:00
} else if k >= bias+tmax {
2024-02-18 10:42:21 +00:00
t = tmax
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if digit < t {
2024-02-18 10:42:21 +00:00
break
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
w, overflow = madd(0, w, base-t)
2024-02-18 10:42:21 +00:00
if overflow {
2024-02-18 10:42:21 +00:00
return "", punyError(encoded)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if len(output) >= 1024 {
2024-02-18 10:42:21 +00:00
return "", punyError(encoded)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
x := int32(len(output) + 1)
2024-02-18 10:42:21 +00:00
bias = adapt(i-oldI, x, oldI == 0)
2024-02-18 10:42:21 +00:00
n += i / x
2024-02-18 10:42:21 +00:00
i %= x
2024-02-18 10:42:21 +00:00
if n < 0 || n > utf8.MaxRune {
2024-02-18 10:42:21 +00:00
return "", punyError(encoded)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
output = append(output, 0)
2024-02-18 10:42:21 +00:00
copy(output[i+1:], output[i:])
2024-02-18 10:42:21 +00:00
output[i] = n
2024-02-18 10:42:21 +00:00
i++
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return string(output), nil
2024-02-18 10:42:21 +00:00
}
// encode encodes a string as specified in section 6.3 and prepends prefix to
2024-02-18 10:42:21 +00:00
// the result.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// The "while h < length(input)" line in the specification becomes "for
2024-02-18 10:42:21 +00:00
// remaining != 0" in the Go code, because len(s) in Go is in bytes, not runes.
2024-02-18 10:42:21 +00:00
func encode(prefix, s string) (string, error) {
2024-02-18 10:42:21 +00:00
output := make([]byte, len(prefix), len(prefix)+1+2*len(s))
2024-02-18 10:42:21 +00:00
copy(output, prefix)
2024-02-18 10:42:21 +00:00
delta, n, bias := int32(0), initialN, initialBias
2024-02-18 10:42:21 +00:00
b, remaining := int32(0), int32(0)
2024-02-18 10:42:21 +00:00
for _, r := range s {
2024-02-18 10:42:21 +00:00
if r < 0x80 {
2024-02-18 10:42:21 +00:00
b++
2024-02-18 10:42:21 +00:00
output = append(output, byte(r))
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
remaining++
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
h := b
2024-02-18 10:42:21 +00:00
if b > 0 {
2024-02-18 10:42:21 +00:00
output = append(output, '-')
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
overflow := false
2024-02-18 10:42:21 +00:00
for remaining != 0 {
2024-02-18 10:42:21 +00:00
m := int32(0x7fffffff)
2024-02-18 10:42:21 +00:00
for _, r := range s {
2024-02-18 10:42:21 +00:00
if m > r && r >= n {
2024-02-18 10:42:21 +00:00
m = r
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
delta, overflow = madd(delta, m-n, h+1)
2024-02-18 10:42:21 +00:00
if overflow {
2024-02-18 10:42:21 +00:00
return "", punyError(s)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
n = m
2024-02-18 10:42:21 +00:00
for _, r := range s {
2024-02-18 10:42:21 +00:00
if r < n {
2024-02-18 10:42:21 +00:00
delta++
2024-02-18 10:42:21 +00:00
if delta < 0 {
2024-02-18 10:42:21 +00:00
return "", punyError(s)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
continue
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if r > n {
2024-02-18 10:42:21 +00:00
continue
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
q := delta
2024-02-18 10:42:21 +00:00
for k := base; ; k += base {
2024-02-18 10:42:21 +00:00
t := k - bias
2024-02-18 10:42:21 +00:00
if k <= bias {
2024-02-18 10:42:21 +00:00
t = tmin
2024-02-18 10:42:21 +00:00
} else if k >= bias+tmax {
2024-02-18 10:42:21 +00:00
t = tmax
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if q < t {
2024-02-18 10:42:21 +00:00
break
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
output = append(output, encodeDigit(t+(q-t)%(base-t)))
2024-02-18 10:42:21 +00:00
q = (q - t) / (base - t)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
output = append(output, encodeDigit(q))
2024-02-18 10:42:21 +00:00
bias = adapt(delta, h+1, h == b)
2024-02-18 10:42:21 +00:00
delta = 0
2024-02-18 10:42:21 +00:00
h++
2024-02-18 10:42:21 +00:00
remaining--
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
delta++
2024-02-18 10:42:21 +00:00
n++
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return string(output), nil
2024-02-18 10:42:21 +00:00
}
// madd computes a + (b * c), detecting overflow.
2024-02-18 10:42:21 +00:00
func madd(a, b, c int32) (next int32, overflow bool) {
2024-02-18 10:42:21 +00:00
p := int64(b) * int64(c)
2024-02-18 10:42:21 +00:00
if p > math.MaxInt32-int64(a) {
2024-02-18 10:42:21 +00:00
return 0, true
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return a + int32(p), false
2024-02-18 10:42:21 +00:00
}
func decodeDigit(x byte) (digit int32, ok bool) {
2024-02-18 10:42:21 +00:00
switch {
2024-02-18 10:42:21 +00:00
case '0' <= x && x <= '9':
2024-02-18 10:42:21 +00:00
return int32(x - ('0' - 26)), true
2024-02-18 10:42:21 +00:00
case 'A' <= x && x <= 'Z':
2024-02-18 10:42:21 +00:00
return int32(x - 'A'), true
2024-02-18 10:42:21 +00:00
case 'a' <= x && x <= 'z':
2024-02-18 10:42:21 +00:00
return int32(x - 'a'), true
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return 0, false
2024-02-18 10:42:21 +00:00
}
func encodeDigit(digit int32) byte {
2024-02-18 10:42:21 +00:00
switch {
2024-02-18 10:42:21 +00:00
case 0 <= digit && digit < 26:
2024-02-18 10:42:21 +00:00
return byte(digit + 'a')
2024-02-18 10:42:21 +00:00
case 26 <= digit && digit < 36:
2024-02-18 10:42:21 +00:00
return byte(digit + ('0' - 26))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
panic("idna: internal error in punycode encoding")
2024-02-18 10:42:21 +00:00
}
// adapt is the bias adaptation function specified in section 6.1.
2024-02-18 10:42:21 +00:00
func adapt(delta, numPoints int32, firstTime bool) int32 {
2024-02-18 10:42:21 +00:00
if firstTime {
2024-02-18 10:42:21 +00:00
delta /= damp
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
delta /= 2
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
delta += delta / numPoints
2024-02-18 10:42:21 +00:00
k := int32(0)
2024-02-18 10:42:21 +00:00
for delta > ((base-tmin)*tmax)/2 {
2024-02-18 10:42:21 +00:00
delta /= base - tmin
2024-02-18 10:42:21 +00:00
k += base
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return k + (base-tmin+1)*delta/(delta+skew)
2024-02-18 10:42:21 +00:00
}