123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681 |
- package idna
- import (
- "fmt"
- "strings"
- "unicode/utf8"
- "golang.org/x/text/secure/bidirule"
- "golang.org/x/text/unicode/norm"
- )
- func ToASCII(s string) (string, error) {
- return Punycode.process(s, true)
- }
- func ToUnicode(s string) (string, error) {
- return Punycode.process(s, false)
- }
- type Option func(*options)
- func Transitional(transitional bool) Option {
- return func(o *options) { o.transitional = true }
- }
- func VerifyDNSLength(verify bool) Option {
- return func(o *options) { o.verifyDNSLength = verify }
- }
- func RemoveLeadingDots(remove bool) Option {
- return func(o *options) { o.removeLeadingDots = remove }
- }
- func ValidateLabels(enable bool) Option {
- return func(o *options) {
-
-
- if o.mapping == nil && enable {
- o.mapping = normalize
- }
- o.trie = trie
- o.validateLabels = enable
- o.fromPuny = validateFromPunycode
- }
- }
- func StrictDomainName(use bool) Option {
- return func(o *options) {
- o.trie = trie
- o.useSTD3Rules = use
- o.fromPuny = validateFromPunycode
- }
- }
- func BidiRule() Option {
- return func(o *options) { o.bidirule = bidirule.ValidString }
- }
- func ValidateForRegistration() Option {
- return func(o *options) {
- o.mapping = validateRegistration
- StrictDomainName(true)(o)
- ValidateLabels(true)(o)
- VerifyDNSLength(true)(o)
- BidiRule()(o)
- }
- }
- func MapForLookup() Option {
- return func(o *options) {
- o.mapping = validateAndMap
- StrictDomainName(true)(o)
- ValidateLabels(true)(o)
- RemoveLeadingDots(true)(o)
- }
- }
- type options struct {
- transitional bool
- useSTD3Rules bool
- validateLabels bool
- verifyDNSLength bool
- removeLeadingDots bool
- trie *idnaTrie
-
- fromPuny func(p *Profile, s string) error
-
-
- mapping func(p *Profile, s string) (string, error)
-
-
- bidirule func(s string) bool
- }
- type Profile struct {
- options
- }
- func apply(o *options, opts []Option) {
- for _, f := range opts {
- f(o)
- }
- }
- func New(o ...Option) *Profile {
- p := &Profile{}
- apply(&p.options, o)
- return p
- }
- func (p *Profile) ToASCII(s string) (string, error) {
- return p.process(s, true)
- }
- func (p *Profile) ToUnicode(s string) (string, error) {
- pp := *p
- pp.transitional = false
- return pp.process(s, false)
- }
- func (p *Profile) String() string {
- s := ""
- if p.transitional {
- s = "Transitional"
- } else {
- s = "NonTransitional"
- }
- if p.useSTD3Rules {
- s += ":UseSTD3Rules"
- }
- if p.validateLabels {
- s += ":ValidateLabels"
- }
- if p.verifyDNSLength {
- s += ":VerifyDNSLength"
- }
- return s
- }
- var (
-
-
- Punycode *Profile = punycode
-
-
-
- Lookup *Profile = lookup
-
-
- Display *Profile = display
-
-
- Registration *Profile = registration
- punycode = &Profile{}
- lookup = &Profile{options{
- transitional: true,
- useSTD3Rules: true,
- validateLabels: true,
- removeLeadingDots: true,
- trie: trie,
- fromPuny: validateFromPunycode,
- mapping: validateAndMap,
- bidirule: bidirule.ValidString,
- }}
- display = &Profile{options{
- useSTD3Rules: true,
- validateLabels: true,
- removeLeadingDots: true,
- trie: trie,
- fromPuny: validateFromPunycode,
- mapping: validateAndMap,
- bidirule: bidirule.ValidString,
- }}
- registration = &Profile{options{
- useSTD3Rules: true,
- validateLabels: true,
- verifyDNSLength: true,
- trie: trie,
- fromPuny: validateFromPunycode,
- mapping: validateRegistration,
- bidirule: bidirule.ValidString,
- }}
-
-
-
- )
- type labelError struct{ label, code_ string }
- func (e labelError) code() string { return e.code_ }
- func (e labelError) Error() string {
- return fmt.Sprintf("idna: invalid label %q", e.label)
- }
- type runeError rune
- func (e runeError) code() string { return "P1" }
- func (e runeError) Error() string {
- return fmt.Sprintf("idna: disallowed rune %U", e)
- }
- func (p *Profile) process(s string, toASCII bool) (string, error) {
- var err error
- if p.mapping != nil {
- s, err = p.mapping(p, s)
- }
-
- if p.removeLeadingDots {
- for ; len(s) > 0 && s[0] == '.'; s = s[1:] {
- }
- }
-
-
- if err == nil && p.verifyDNSLength && s == "" {
- err = &labelError{s, "A4"}
- }
- labels := labelIter{orig: s}
- for ; !labels.done(); labels.next() {
- label := labels.label()
- if label == "" {
-
-
- if err == nil && p.verifyDNSLength {
- err = &labelError{s, "A4"}
- }
- continue
- }
- if strings.HasPrefix(label, acePrefix) {
- u, err2 := decode(label[len(acePrefix):])
- if err2 != nil {
- if err == nil {
- err = err2
- }
-
- continue
- }
- labels.set(u)
- if err == nil && p.validateLabels {
- err = p.fromPuny(p, u)
- }
- if err == nil {
-
-
-
- err = p.validateLabel(u)
- }
- } else if err == nil {
- err = p.validateLabel(label)
- }
- }
- if toASCII {
- for labels.reset(); !labels.done(); labels.next() {
- label := labels.label()
- if !ascii(label) {
- a, err2 := encode(acePrefix, label)
- if err == nil {
- err = err2
- }
- label = a
- labels.set(a)
- }
- n := len(label)
- if p.verifyDNSLength && err == nil && (n == 0 || n > 63) {
- err = &labelError{label, "A4"}
- }
- }
- }
- s = labels.result()
- if toASCII && p.verifyDNSLength && err == nil {
-
- n := len(s)
- if n > 0 && s[n-1] == '.' {
- n--
- }
- if len(s) < 1 || n > 253 {
- err = &labelError{s, "A4"}
- }
- }
- return s, err
- }
- func normalize(p *Profile, s string) (string, error) {
- return norm.NFC.String(s), nil
- }
- func validateRegistration(p *Profile, s string) (string, error) {
- if !norm.NFC.IsNormalString(s) {
- return s, &labelError{s, "V1"}
- }
- for i := 0; i < len(s); {
- v, sz := trie.lookupString(s[i:])
-
- switch p.simplify(info(v).category()) {
-
-
- case valid, deviation:
- case disallowed, mapped, unknown, ignored:
- r, _ := utf8.DecodeRuneInString(s[i:])
- return s, runeError(r)
- }
- i += sz
- }
- return s, nil
- }
- func validateAndMap(p *Profile, s string) (string, error) {
- var (
- err error
- b []byte
- k int
- )
- for i := 0; i < len(s); {
- v, sz := trie.lookupString(s[i:])
- start := i
- i += sz
-
- switch p.simplify(info(v).category()) {
- case valid:
- continue
- case disallowed:
- if err == nil {
- r, _ := utf8.DecodeRuneInString(s[start:])
- err = runeError(r)
- }
- continue
- case mapped, deviation:
- b = append(b, s[k:start]...)
- b = info(v).appendMapping(b, s[start:i])
- case ignored:
- b = append(b, s[k:start]...)
-
- case unknown:
- b = append(b, s[k:start]...)
- b = append(b, "\ufffd"...)
- }
- k = i
- }
- if k == 0 {
-
- s = norm.NFC.String(s)
- } else {
- b = append(b, s[k:]...)
- if norm.NFC.QuickSpan(b) != len(b) {
- b = norm.NFC.Bytes(b)
- }
-
- s = string(b)
- }
- return s, err
- }
- type labelIter struct {
- orig string
- slice []string
- curStart int
- curEnd int
- i int
- }
- func (l *labelIter) reset() {
- l.curStart = 0
- l.curEnd = 0
- l.i = 0
- }
- func (l *labelIter) done() bool {
- return l.curStart >= len(l.orig)
- }
- func (l *labelIter) result() string {
- if l.slice != nil {
- return strings.Join(l.slice, ".")
- }
- return l.orig
- }
- func (l *labelIter) label() string {
- if l.slice != nil {
- return l.slice[l.i]
- }
- p := strings.IndexByte(l.orig[l.curStart:], '.')
- l.curEnd = l.curStart + p
- if p == -1 {
- l.curEnd = len(l.orig)
- }
- return l.orig[l.curStart:l.curEnd]
- }
- func (l *labelIter) next() {
- l.i++
- if l.slice != nil {
- if l.i >= len(l.slice) || l.i == len(l.slice)-1 && l.slice[l.i] == "" {
- l.curStart = len(l.orig)
- }
- } else {
- l.curStart = l.curEnd + 1
- if l.curStart == len(l.orig)-1 && l.orig[l.curStart] == '.' {
- l.curStart = len(l.orig)
- }
- }
- }
- func (l *labelIter) set(s string) {
- if l.slice == nil {
- l.slice = strings.Split(l.orig, ".")
- }
- l.slice[l.i] = s
- }
- const acePrefix = "xn--"
- func (p *Profile) simplify(cat category) category {
- switch cat {
- case disallowedSTD3Mapped:
- if p.useSTD3Rules {
- cat = disallowed
- } else {
- cat = mapped
- }
- case disallowedSTD3Valid:
- if p.useSTD3Rules {
- cat = disallowed
- } else {
- cat = valid
- }
- case deviation:
- if !p.transitional {
- cat = valid
- }
- case validNV8, validXV8:
-
- cat = valid
- }
- return cat
- }
- func validateFromPunycode(p *Profile, s string) error {
- if !norm.NFC.IsNormalString(s) {
- return &labelError{s, "V1"}
- }
- for i := 0; i < len(s); {
- v, sz := trie.lookupString(s[i:])
- if c := p.simplify(info(v).category()); c != valid && c != deviation {
- return &labelError{s, "V6"}
- }
- i += sz
- }
- return nil
- }
- const (
- zwnj = "\u200c"
- zwj = "\u200d"
- )
- type joinState int8
- const (
- stateStart joinState = iota
- stateVirama
- stateBefore
- stateBeforeVirama
- stateAfter
- stateFAIL
- )
- var joinStates = [][numJoinTypes]joinState{
- stateStart: {
- joiningL: stateBefore,
- joiningD: stateBefore,
- joinZWNJ: stateFAIL,
- joinZWJ: stateFAIL,
- joinVirama: stateVirama,
- },
- stateVirama: {
- joiningL: stateBefore,
- joiningD: stateBefore,
- },
- stateBefore: {
- joiningL: stateBefore,
- joiningD: stateBefore,
- joiningT: stateBefore,
- joinZWNJ: stateAfter,
- joinZWJ: stateFAIL,
- joinVirama: stateBeforeVirama,
- },
- stateBeforeVirama: {
- joiningL: stateBefore,
- joiningD: stateBefore,
- joiningT: stateBefore,
- },
- stateAfter: {
- joiningL: stateFAIL,
- joiningD: stateBefore,
- joiningT: stateAfter,
- joiningR: stateStart,
- joinZWNJ: stateFAIL,
- joinZWJ: stateFAIL,
- joinVirama: stateAfter,
- },
- stateFAIL: {
- 0: stateFAIL,
- joiningL: stateFAIL,
- joiningD: stateFAIL,
- joiningT: stateFAIL,
- joiningR: stateFAIL,
- joinZWNJ: stateFAIL,
- joinZWJ: stateFAIL,
- joinVirama: stateFAIL,
- },
- }
- func (p *Profile) validateLabel(s string) error {
- if s == "" {
- if p.verifyDNSLength {
- return &labelError{s, "A4"}
- }
- return nil
- }
- if p.bidirule != nil && !p.bidirule(s) {
- return &labelError{s, "B"}
- }
- if !p.validateLabels {
- return nil
- }
- trie := p.trie
- if len(s) > 4 && s[2] == '-' && s[3] == '-' {
- return &labelError{s, "V2"}
- }
- if s[0] == '-' || s[len(s)-1] == '-' {
- return &labelError{s, "V3"}
- }
-
- v, sz := trie.lookupString(s)
- x := info(v)
- if x.isModifier() {
- return &labelError{s, "V5"}
- }
-
- if strings.Index(s, zwj) == -1 && strings.Index(s, zwnj) == -1 {
- return nil
- }
- st := stateStart
- for i := 0; ; {
- jt := x.joinType()
- if s[i:i+sz] == zwj {
- jt = joinZWJ
- } else if s[i:i+sz] == zwnj {
- jt = joinZWNJ
- }
- st = joinStates[st][jt]
- if x.isViramaModifier() {
- st = joinStates[st][joinVirama]
- }
- if i += sz; i == len(s) {
- break
- }
- v, sz = trie.lookupString(s[i:])
- x = info(v)
- }
- if st == stateFAIL || st == stateAfter {
- return &labelError{s, "C"}
- }
- return nil
- }
- func ascii(s string) bool {
- for i := 0; i < len(s); i++ {
- if s[i] >= utf8.RuneSelf {
- return false
- }
- }
- return true
- }
|