Debian Codegraph @

internal/export/idna/punycode.go

raw symbols SCIP
1 // Copyright 2016 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package idnascip-go gomod golang.org/x/text . `golang.org/x/text/internal/export/idna`/
6
7 // This file implements the Punycode algorithm from RFC 3492.
8
9 import (
10 "mathscip-go gomod github.com/golang/go/src go1.25.0 math/"
11 "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/"
12 "unicode/utf8scip-go gomod github.com/golang/go/src go1.25.0 `unicode/utf8`/"
13 )
14
15 // These parameter values are specified in section 5.
16 //
17 // All computation is done with int32s, so that overflow behavior is identical
18 // regardless of whether int is 32-bit or 64-bit.
19 const (
20 basescip-go gomod golang.org/x/text . `golang.org/x/text/internal/export/idna`/base. int32 = 36
21 dampscip-go gomod golang.org/x/text . `golang.org/x/text/internal/export/idna`/damp. int32 = 700
22 initialBiasscip-go gomod golang.org/x/text . `golang.org/x/text/internal/export/idna`/initialBias. int32 = 72
23 initialNscip-go gomod golang.org/x/text . `golang.org/x/text/internal/export/idna`/initialN. int32 = 128
24 skewscip-go gomod golang.org/x/text . `golang.org/x/text/internal/export/idna`/skew. int32 = 38
25 tmaxscip-go gomod golang.org/x/text . `golang.org/x/text/internal/export/idna`/tmax. int32 = 26
26 tminscip-go gomod golang.org/x/text . `golang.org/x/text/internal/export/idna`/tmin. int32 = 1
27 )
28
29 func punyErrorscip-go gomod golang.org/x/text . `golang.org/x/text/internal/export/idna`/punyError().(s string) error { return &labelErrorscip-go gomod golang.org/x/text . `golang.org/x/text/internal/export/idna`/labelError#{s, code16scip-go gomod golang.org/x/text . `golang.org/x/text/internal/export/idna`/code16().("A3", "P4")} }
30
31 // decode decodes a string as specified in section 6.2.
32 func decodescip-go gomod golang.org/x/text . `golang.org/x/text/internal/export/idna`/decode().(encoded string) (string, error) {
33 if encoded == "" {
34 return "", nil
35 }
36 pos := 1 + stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.LastIndexscip-go gomod github.com/golang/go/src go1.25.0 strings/LastIndex().(encoded, "-")
37 if pos == 1 {
38 return "", punyErrorscip-go gomod golang.org/x/text . `golang.org/x/text/internal/export/idna`/punyError().(encoded)
39 }
40 if pos == len(encoded) {
41 return encoded[:len(encoded)-1], nil
42 }
43 output := make([]rune, 0, len(encoded))
44 if pos != 0 {
45 for _, r := range encoded[:pos-1] {
46 output = append(output, r)
47 }
48 }
49 i, n, bias := int32(0), initialNscip-go gomod golang.org/x/text . `golang.org/x/text/internal/export/idna`/initialN., initialBiasscip-go gomod golang.org/x/text . `golang.org/x/text/internal/export/idna`/initialBias.
50 overflow := false
51 for pos < len(encoded) {
52 oldI, w := i, int32(1)
53 for k := basescip-go gomod golang.org/x/text . `golang.org/x/text/internal/export/idna`/base.; ; k += basescip-go gomod golang.org/x/text . `golang.org/x/text/internal/export/idna`/base. {
54 if pos == len(encoded) {
55 return "", punyErrorscip-go gomod golang.org/x/text . `golang.org/x/text/internal/export/idna`/punyError().(encoded)
56 }
57 digit, ok := decodeDigitscip-go gomod golang.org/x/text . `golang.org/x/text/internal/export/idna`/decodeDigit().(encoded[pos])
58 if !ok {
59 return "", punyErrorscip-go gomod golang.org/x/text . `golang.org/x/text/internal/export/idna`/punyError().(encoded)
60 }
61 pos++
62 i, overflow = maddscip-go gomod golang.org/x/text . `golang.org/x/text/internal/export/idna`/madd().(i, digit, w)
63 if overflow {
64 return "", punyErrorscip-go gomod golang.org/x/text . `golang.org/x/text/internal/export/idna`/punyError().(encoded)
65 }
66 t := k - bias
67 if k <= bias {
68 t = tminscip-go gomod golang.org/x/text . `golang.org/x/text/internal/export/idna`/tmin.
69 } else if k >= bias+tmaxscip-go gomod golang.org/x/text . `golang.org/x/text/internal/export/idna`/tmax. {
70 t = tmaxscip-go gomod golang.org/x/text . `golang.org/x/text/internal/export/idna`/tmax.
71 }
72 if digit < t {
73 break
74 }
75 w, overflow = maddscip-go gomod golang.org/x/text . `golang.org/x/text/internal/export/idna`/madd().(0, w, basescip-go gomod golang.org/x/text . `golang.org/x/text/internal/export/idna`/base.-t)
76 if overflow {
77 return "", punyErrorscip-go gomod golang.org/x/text . `golang.org/x/text/internal/export/idna`/punyError().(encoded)
78 }
79 }
80 if len(output) >= 1024 {
81 return "", punyErrorscip-go gomod golang.org/x/text . `golang.org/x/text/internal/export/idna`/punyError().(encoded)
82 }
83 x := int32(len(output) + 1)
84 bias = adaptscip-go gomod golang.org/x/text . `golang.org/x/text/internal/export/idna`/adapt().(i-oldI, x, oldI == 0)
85 n += i / x
86 i %= x
87 if n < 0 || n > utf8scip-go gomod github.com/golang/go/src go1.25.0 `unicode/utf8`/.MaxRunescip-go gomod github.com/golang/go/src go1.25.0 `unicode/utf8`/MaxRune. {
88 return "", punyErrorscip-go gomod golang.org/x/text . `golang.org/x/text/internal/export/idna`/punyError().(encoded)
89 }
90 output = append(output, 0)
91 copy(output[i+1:], output[i:])
92 output[i] = n
93 i++
94 }
95 return string(output), nil
96 }
97
98 // encode encodes a string as specified in section 6.3 and prepends prefix to
99 // the result.
100 //
101 // The "while h < length(input)" line in the specification becomes "for
102 // remaining != 0" in the Go code, because len(s) in Go is in bytes, not runes.
103 func encodescip-go gomod golang.org/x/text . `golang.org/x/text/internal/export/idna`/encode().(prefix, s string) (string, error) {
104 output := make([]byte, len(prefix), len(prefix)+1+2*len(s))
105 copy(output, prefix)
106 delta, n, bias := int32(0), initialNscip-go gomod golang.org/x/text . `golang.org/x/text/internal/export/idna`/initialN., initialBiasscip-go gomod golang.org/x/text . `golang.org/x/text/internal/export/idna`/initialBias.
107 b, remaining := int32(0), int32(0)
108 for _, r := range s {
109 if unicode16scip-go gomod golang.org/x/text . `golang.org/x/text/internal/export/idna`/unicode16. && r == 0xfffd {
110 return s, &labelErrorscip-go gomod golang.org/x/text . `golang.org/x/text/internal/export/idna`/labelError#{s, "A3"}
111 }
112 if r < 0x80 {
113 b++
114 output = append(output, byte(r))
115 } else {
116 remaining++
117 }
118 }
119 h := b
120 if b > 0 {
121 output = append(output, '-')
122 }
123 overflow := false
124 for remaining != 0 {
125 m := int32(0x7fffffff)
126 for _, r := range s {
127 if m > r && r >= n {
128 m = r
129 }
130 }
131 delta, overflow = maddscip-go gomod golang.org/x/text . `golang.org/x/text/internal/export/idna`/madd().(delta, m-n, h+1)
132 if overflow {
133 return "", punyErrorscip-go gomod golang.org/x/text . `golang.org/x/text/internal/export/idna`/punyError().(s)
134 }
135 n = m
136 for _, r := range s {
137 if r < n {
138 delta++
139 if delta < 0 {
140 return "", punyErrorscip-go gomod golang.org/x/text . `golang.org/x/text/internal/export/idna`/punyError().(s)
141 }
142 continue
143 }
144 if r > n {
145 continue
146 }
147 q := delta
148 for k := basescip-go gomod golang.org/x/text . `golang.org/x/text/internal/export/idna`/base.; ; k += basescip-go gomod golang.org/x/text . `golang.org/x/text/internal/export/idna`/base. {
149 t := k - bias
150 if k <= bias {
151 t = tminscip-go gomod golang.org/x/text . `golang.org/x/text/internal/export/idna`/tmin.
152 } else if k >= bias+tmaxscip-go gomod golang.org/x/text . `golang.org/x/text/internal/export/idna`/tmax. {
153 t = tmaxscip-go gomod golang.org/x/text . `golang.org/x/text/internal/export/idna`/tmax.
154 }
155 if q < t {
156 break
157 }
158 output = append(output, encodeDigitscip-go gomod golang.org/x/text . `golang.org/x/text/internal/export/idna`/encodeDigit().(t+(q-t)%(basescip-go gomod golang.org/x/text . `golang.org/x/text/internal/export/idna`/base.-t)))
159 q = (q - t) / (basescip-go gomod golang.org/x/text . `golang.org/x/text/internal/export/idna`/base. - t)
160 }
161 output = append(output, encodeDigitscip-go gomod golang.org/x/text . `golang.org/x/text/internal/export/idna`/encodeDigit().(q))
162 bias = adaptscip-go gomod golang.org/x/text . `golang.org/x/text/internal/export/idna`/adapt().(delta, h+1, h == b)
163 delta = 0
164 h++
165 remaining--
166 }
167 delta++
168 n++
169 }
170 return string(output), nil
171 }
172
173 // madd computes a + (b * c), detecting overflow.
174 func maddscip-go gomod golang.org/x/text . `golang.org/x/text/internal/export/idna`/madd().(a, b, c int32) (next int32, overflow bool) {
175 p := int64(b) * int64(c)
176 if p > mathscip-go gomod github.com/golang/go/src go1.25.0 math/.MaxInt32scip-go gomod github.com/golang/go/src go1.25.0 math/MaxInt32.-int64(a) {
177 return 0, true
178 }
179 return a + int32(p), false
180 }
181
182 func decodeDigitscip-go gomod golang.org/x/text . `golang.org/x/text/internal/export/idna`/decodeDigit().(x byte) (digit int32, ok bool) {
183 switch {
184 case '0' <= x && x <= '9':
185 return int32(x - ('0' - 26)), true
186 case 'A' <= x && x <= 'Z':
187 return int32(x - 'A'), true
188 case 'a' <= x && x <= 'z':
189 return int32(x - 'a'), true
190 }
191 return 0, false
192 }
193
194 func encodeDigitscip-go gomod golang.org/x/text . `golang.org/x/text/internal/export/idna`/encodeDigit().(digit int32) byte {
195 switch {
196 case 0 <= digit && digit < 26:
197 return byte(digit + 'a')
198 case 26 <= digit && digit < 36:
199 return byte(digit + ('0' - 26))
200 }
201 panic("idna: internal error in punycode encoding")
202 }
203
204 // adapt is the bias adaptation function specified in section 6.1.
205 func adaptscip-go gomod golang.org/x/text . `golang.org/x/text/internal/export/idna`/adapt().(delta, numPoints int32, firstTime bool) int32 {
206 if firstTime {
207 delta /= dampscip-go gomod golang.org/x/text . `golang.org/x/text/internal/export/idna`/damp.
208 } else {
209 delta /= 2
210 }
211 delta += delta / numPoints
212 k := int32(0)
213 for delta > ((basescip-go gomod golang.org/x/text . `golang.org/x/text/internal/export/idna`/base.-tminscip-go gomod golang.org/x/text . `golang.org/x/text/internal/export/idna`/tmin.)*tmaxscip-go gomod golang.org/x/text . `golang.org/x/text/internal/export/idna`/tmax.)/2 {
214 delta /= basescip-go gomod golang.org/x/text . `golang.org/x/text/internal/export/idna`/base. - tminscip-go gomod golang.org/x/text . `golang.org/x/text/internal/export/idna`/tmin.
215 k += basescip-go gomod golang.org/x/text . `golang.org/x/text/internal/export/idna`/base.
216 }
217 return k + (basescip-go gomod golang.org/x/text . `golang.org/x/text/internal/export/idna`/base.-tminscip-go gomod golang.org/x/text . `golang.org/x/text/internal/export/idna`/tmin.+1)*delta/(delta+skewscip-go gomod golang.org/x/text . `golang.org/x/text/internal/export/idna`/skew.)
218 }