Source file src/vendor/golang.org/x/net/idna/idna10.0.0.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package idna
19
20 import (
21 "fmt"
22 "strings"
23 "unicode/utf8"
24
25 "golang.org/x/text/secure/bidirule"
26 "golang.org/x/text/unicode/bidi"
27 "golang.org/x/text/unicode/norm"
28 )
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 func ToASCII(s string) (string, error) {
47 return Punycode.process(s, true)
48 }
49
50
51 func ToUnicode(s string) (string, error) {
52 return Punycode.process(s, false)
53 }
54
55
56 type Option func(*options)
57
58
59
60
61
62
63 func Transitional(transitional bool) Option {
64 return func(o *options) { o.transitional = true }
65 }
66
67
68
69 func VerifyDNSLength(verify bool) Option {
70 return func(o *options) { o.verifyDNSLength = verify }
71 }
72
73
74
75
76
77
78 func RemoveLeadingDots(remove bool) Option {
79 return func(o *options) { o.removeLeadingDots = remove }
80 }
81
82
83
84
85 func ValidateLabels(enable bool) Option {
86 return func(o *options) {
87
88
89 if o.mapping == nil && enable {
90 o.mapping = normalize
91 }
92 o.trie = trie
93 o.validateLabels = enable
94 o.fromPuny = validateFromPunycode
95 }
96 }
97
98
99
100
101
102
103
104
105
106 func StrictDomainName(use bool) Option {
107 return func(o *options) {
108 o.trie = trie
109 o.useSTD3Rules = use
110 o.fromPuny = validateFromPunycode
111 }
112 }
113
114
115
116
117
118
119 func BidiRule() Option {
120 return func(o *options) { o.bidirule = bidirule.ValidString }
121 }
122
123
124
125 func ValidateForRegistration() Option {
126 return func(o *options) {
127 o.mapping = validateRegistration
128 StrictDomainName(true)(o)
129 ValidateLabels(true)(o)
130 VerifyDNSLength(true)(o)
131 BidiRule()(o)
132 }
133 }
134
135
136
137
138
139
140
141
142
143 func MapForLookup() Option {
144 return func(o *options) {
145 o.mapping = validateAndMap
146 StrictDomainName(true)(o)
147 ValidateLabels(true)(o)
148 }
149 }
150
151 type options struct {
152 transitional bool
153 useSTD3Rules bool
154 validateLabels bool
155 verifyDNSLength bool
156 removeLeadingDots bool
157
158 trie *idnaTrie
159
160
161 fromPuny func(p *Profile, s string) error
162
163
164
165 mapping func(p *Profile, s string) (mapped string, isBidi bool, err error)
166
167
168
169 bidirule func(s string) bool
170 }
171
172
173 type Profile struct {
174 options
175 }
176
177 func apply(o *options, opts []Option) {
178 for _, f := range opts {
179 f(o)
180 }
181 }
182
183
184
185
186
187
188
189
190
191 func New(o ...Option) *Profile {
192 p := &Profile{}
193 apply(&p.options, o)
194 return p
195 }
196
197
198
199
200
201 func (p *Profile) ToASCII(s string) (string, error) {
202 return p.process(s, true)
203 }
204
205
206
207
208
209 func (p *Profile) ToUnicode(s string) (string, error) {
210 pp := *p
211 pp.transitional = false
212 return pp.process(s, false)
213 }
214
215
216
217 func (p *Profile) String() string {
218 s := ""
219 if p.transitional {
220 s = "Transitional"
221 } else {
222 s = "NonTransitional"
223 }
224 if p.useSTD3Rules {
225 s += ":UseSTD3Rules"
226 }
227 if p.validateLabels {
228 s += ":ValidateLabels"
229 }
230 if p.verifyDNSLength {
231 s += ":VerifyDNSLength"
232 }
233 return s
234 }
235
236 var (
237
238
239 Punycode *Profile = punycode
240
241
242
243
244 Lookup *Profile = lookup
245
246
247
248 Display *Profile = display
249
250
251
252 Registration *Profile = registration
253
254 punycode = &Profile{}
255 lookup = &Profile{options{
256 transitional: true,
257 useSTD3Rules: true,
258 validateLabels: true,
259 trie: trie,
260 fromPuny: validateFromPunycode,
261 mapping: validateAndMap,
262 bidirule: bidirule.ValidString,
263 }}
264 display = &Profile{options{
265 useSTD3Rules: true,
266 validateLabels: true,
267 trie: trie,
268 fromPuny: validateFromPunycode,
269 mapping: validateAndMap,
270 bidirule: bidirule.ValidString,
271 }}
272 registration = &Profile{options{
273 useSTD3Rules: true,
274 validateLabels: true,
275 verifyDNSLength: true,
276 trie: trie,
277 fromPuny: validateFromPunycode,
278 mapping: validateRegistration,
279 bidirule: bidirule.ValidString,
280 }}
281
282
283
284
285 )
286
287 type labelError struct{ label, code_ string }
288
289 func (e labelError) code() string { return e.code_ }
290 func (e labelError) Error() string {
291 return fmt.Sprintf("idna: invalid label %q", e.label)
292 }
293
294 type runeError rune
295
296 func (e runeError) code() string { return "P1" }
297 func (e runeError) Error() string {
298 return fmt.Sprintf("idna: disallowed rune %U", e)
299 }
300
301
302
303 func (p *Profile) process(s string, toASCII bool) (string, error) {
304 var err error
305 var isBidi bool
306 if p.mapping != nil {
307 s, isBidi, err = p.mapping(p, s)
308 }
309
310 if p.removeLeadingDots {
311 for ; len(s) > 0 && s[0] == '.'; s = s[1:] {
312 }
313 }
314
315
316
317 if err == nil && p.verifyDNSLength && s == "" {
318 err = &labelError{s, "A4"}
319 }
320 labels := labelIter{orig: s}
321 for ; !labels.done(); labels.next() {
322 label := labels.label()
323 if label == "" {
324
325
326 if err == nil && p.verifyDNSLength {
327 err = &labelError{s, "A4"}
328 }
329 continue
330 }
331 if strings.HasPrefix(label, acePrefix) {
332 u, err2 := decode(label[len(acePrefix):])
333 if err2 != nil {
334 if err == nil {
335 err = err2
336 }
337
338 continue
339 }
340 isBidi = isBidi || bidirule.DirectionString(u) != bidi.LeftToRight
341 labels.set(u)
342 if err == nil && p.validateLabels {
343 err = p.fromPuny(p, u)
344 }
345 if err == nil {
346
347
348
349 err = p.validateLabel(u)
350 }
351 } else if err == nil {
352 err = p.validateLabel(label)
353 }
354 }
355 if isBidi && p.bidirule != nil && err == nil {
356 for labels.reset(); !labels.done(); labels.next() {
357 if !p.bidirule(labels.label()) {
358 err = &labelError{s, "B"}
359 break
360 }
361 }
362 }
363 if toASCII {
364 for labels.reset(); !labels.done(); labels.next() {
365 label := labels.label()
366 if !ascii(label) {
367 a, err2 := encode(acePrefix, label)
368 if err == nil {
369 err = err2
370 }
371 label = a
372 labels.set(a)
373 }
374 n := len(label)
375 if p.verifyDNSLength && err == nil && (n == 0 || n > 63) {
376 err = &labelError{label, "A4"}
377 }
378 }
379 }
380 s = labels.result()
381 if toASCII && p.verifyDNSLength && err == nil {
382
383 n := len(s)
384 if n > 0 && s[n-1] == '.' {
385 n--
386 }
387 if len(s) < 1 || n > 253 {
388 err = &labelError{s, "A4"}
389 }
390 }
391 return s, err
392 }
393
394 func normalize(p *Profile, s string) (mapped string, isBidi bool, err error) {
395
396
397
398 mapped = norm.NFC.String(s)
399 isBidi = bidirule.DirectionString(mapped) == bidi.RightToLeft
400 return mapped, isBidi, nil
401 }
402
403 func validateRegistration(p *Profile, s string) (idem string, bidi bool, err error) {
404
405 if !norm.NFC.IsNormalString(s) {
406 return s, false, &labelError{s, "V1"}
407 }
408 for i := 0; i < len(s); {
409 v, sz := trie.lookupString(s[i:])
410 if sz == 0 {
411 return s, bidi, runeError(utf8.RuneError)
412 }
413 bidi = bidi || info(v).isBidi(s[i:])
414
415 switch p.simplify(info(v).category()) {
416
417
418 case valid, deviation:
419 case disallowed, mapped, unknown, ignored:
420 r, _ := utf8.DecodeRuneInString(s[i:])
421 return s, bidi, runeError(r)
422 }
423 i += sz
424 }
425 return s, bidi, nil
426 }
427
428 func (c info) isBidi(s string) bool {
429 if !c.isMapped() {
430 return c&attributesMask == rtl
431 }
432
433
434 p, _ := bidi.LookupString(s)
435 switch p.Class() {
436 case bidi.R, bidi.AL, bidi.AN:
437 return true
438 }
439 return false
440 }
441
442 func validateAndMap(p *Profile, s string) (vm string, bidi bool, err error) {
443 var (
444 b []byte
445 k int
446 )
447
448
449
450
451 var combinedInfoBits info
452 for i := 0; i < len(s); {
453 v, sz := trie.lookupString(s[i:])
454 if sz == 0 {
455 b = append(b, s[k:i]...)
456 b = append(b, "\ufffd"...)
457 k = len(s)
458 if err == nil {
459 err = runeError(utf8.RuneError)
460 }
461 break
462 }
463 combinedInfoBits |= info(v)
464 bidi = bidi || info(v).isBidi(s[i:])
465 start := i
466 i += sz
467
468 switch p.simplify(info(v).category()) {
469 case valid:
470 continue
471 case disallowed:
472 if err == nil {
473 r, _ := utf8.DecodeRuneInString(s[start:])
474 err = runeError(r)
475 }
476 continue
477 case mapped, deviation:
478 b = append(b, s[k:start]...)
479 b = info(v).appendMapping(b, s[start:i])
480 case ignored:
481 b = append(b, s[k:start]...)
482
483 case unknown:
484 b = append(b, s[k:start]...)
485 b = append(b, "\ufffd"...)
486 }
487 k = i
488 }
489 if k == 0 {
490
491 if combinedInfoBits&mayNeedNorm != 0 {
492 s = norm.NFC.String(s)
493 }
494 } else {
495 b = append(b, s[k:]...)
496 if norm.NFC.QuickSpan(b) != len(b) {
497 b = norm.NFC.Bytes(b)
498 }
499
500 s = string(b)
501 }
502 return s, bidi, err
503 }
504
505
506 type labelIter struct {
507 orig string
508 slice []string
509 curStart int
510 curEnd int
511 i int
512 }
513
514 func (l *labelIter) reset() {
515 l.curStart = 0
516 l.curEnd = 0
517 l.i = 0
518 }
519
520 func (l *labelIter) done() bool {
521 return l.curStart >= len(l.orig)
522 }
523
524 func (l *labelIter) result() string {
525 if l.slice != nil {
526 return strings.Join(l.slice, ".")
527 }
528 return l.orig
529 }
530
531 func (l *labelIter) label() string {
532 if l.slice != nil {
533 return l.slice[l.i]
534 }
535 p := strings.IndexByte(l.orig[l.curStart:], '.')
536 l.curEnd = l.curStart + p
537 if p == -1 {
538 l.curEnd = len(l.orig)
539 }
540 return l.orig[l.curStart:l.curEnd]
541 }
542
543
544 func (l *labelIter) next() {
545 l.i++
546 if l.slice != nil {
547 if l.i >= len(l.slice) || l.i == len(l.slice)-1 && l.slice[l.i] == "" {
548 l.curStart = len(l.orig)
549 }
550 } else {
551 l.curStart = l.curEnd + 1
552 if l.curStart == len(l.orig)-1 && l.orig[l.curStart] == '.' {
553 l.curStart = len(l.orig)
554 }
555 }
556 }
557
558 func (l *labelIter) set(s string) {
559 if l.slice == nil {
560 l.slice = strings.Split(l.orig, ".")
561 }
562 l.slice[l.i] = s
563 }
564
565
566 const acePrefix = "xn--"
567
568 func (p *Profile) simplify(cat category) category {
569 switch cat {
570 case disallowedSTD3Mapped:
571 if p.useSTD3Rules {
572 cat = disallowed
573 } else {
574 cat = mapped
575 }
576 case disallowedSTD3Valid:
577 if p.useSTD3Rules {
578 cat = disallowed
579 } else {
580 cat = valid
581 }
582 case deviation:
583 if !p.transitional {
584 cat = valid
585 }
586 case validNV8, validXV8:
587
588 cat = valid
589 }
590 return cat
591 }
592
593 func validateFromPunycode(p *Profile, s string) error {
594 if !norm.NFC.IsNormalString(s) {
595 return &labelError{s, "V1"}
596 }
597
598
599 for i := 0; i < len(s); {
600 v, sz := trie.lookupString(s[i:])
601 if sz == 0 {
602 return runeError(utf8.RuneError)
603 }
604 if c := p.simplify(info(v).category()); c != valid && c != deviation {
605 return &labelError{s, "V6"}
606 }
607 i += sz
608 }
609 return nil
610 }
611
612 const (
613 zwnj = "\u200c"
614 zwj = "\u200d"
615 )
616
617 type joinState int8
618
619 const (
620 stateStart joinState = iota
621 stateVirama
622 stateBefore
623 stateBeforeVirama
624 stateAfter
625 stateFAIL
626 )
627
628 var joinStates = [][numJoinTypes]joinState{
629 stateStart: {
630 joiningL: stateBefore,
631 joiningD: stateBefore,
632 joinZWNJ: stateFAIL,
633 joinZWJ: stateFAIL,
634 joinVirama: stateVirama,
635 },
636 stateVirama: {
637 joiningL: stateBefore,
638 joiningD: stateBefore,
639 },
640 stateBefore: {
641 joiningL: stateBefore,
642 joiningD: stateBefore,
643 joiningT: stateBefore,
644 joinZWNJ: stateAfter,
645 joinZWJ: stateFAIL,
646 joinVirama: stateBeforeVirama,
647 },
648 stateBeforeVirama: {
649 joiningL: stateBefore,
650 joiningD: stateBefore,
651 joiningT: stateBefore,
652 },
653 stateAfter: {
654 joiningL: stateFAIL,
655 joiningD: stateBefore,
656 joiningT: stateAfter,
657 joiningR: stateStart,
658 joinZWNJ: stateFAIL,
659 joinZWJ: stateFAIL,
660 joinVirama: stateAfter,
661 },
662 stateFAIL: {
663 0: stateFAIL,
664 joiningL: stateFAIL,
665 joiningD: stateFAIL,
666 joiningT: stateFAIL,
667 joiningR: stateFAIL,
668 joinZWNJ: stateFAIL,
669 joinZWJ: stateFAIL,
670 joinVirama: stateFAIL,
671 },
672 }
673
674
675
676 func (p *Profile) validateLabel(s string) (err error) {
677 if s == "" {
678 if p.verifyDNSLength {
679 return &labelError{s, "A4"}
680 }
681 return nil
682 }
683 if !p.validateLabels {
684 return nil
685 }
686 trie := p.trie
687 if len(s) > 4 && s[2] == '-' && s[3] == '-' {
688 return &labelError{s, "V2"}
689 }
690 if s[0] == '-' || s[len(s)-1] == '-' {
691 return &labelError{s, "V3"}
692 }
693
694 v, sz := trie.lookupString(s)
695 x := info(v)
696 if x.isModifier() {
697 return &labelError{s, "V5"}
698 }
699
700 if strings.Index(s, zwj) == -1 && strings.Index(s, zwnj) == -1 {
701 return nil
702 }
703 st := stateStart
704 for i := 0; ; {
705 jt := x.joinType()
706 if s[i:i+sz] == zwj {
707 jt = joinZWJ
708 } else if s[i:i+sz] == zwnj {
709 jt = joinZWNJ
710 }
711 st = joinStates[st][jt]
712 if x.isViramaModifier() {
713 st = joinStates[st][joinVirama]
714 }
715 if i += sz; i == len(s) {
716 break
717 }
718 v, sz = trie.lookupString(s[i:])
719 x = info(v)
720 }
721 if st == stateFAIL || st == stateAfter {
722 return &labelError{s, "C"}
723 }
724 return nil
725 }
726
727 func ascii(s string) bool {
728 for i := 0; i < len(s); i++ {
729 if s[i] >= utf8.RuneSelf {
730 return false
731 }
732 }
733 return true
734 }
735
View as plain text