idna10.0.0.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  1. // Code generated by running "go generate" in golang.org/x/text. DO NOT EDIT.
  2. // Copyright 2016 The Go Authors. All rights reserved.
  3. // Use of this source code is governed by a BSD-style
  4. // license that can be found in the LICENSE file.
  5. //go:build go1.10
  6. // +build go1.10
  7. // Package idna implements IDNA2008 using the compatibility processing
  8. // defined by UTS (Unicode Technical Standard) #46, which defines a standard to
  9. // deal with the transition from IDNA2003.
  10. //
  11. // IDNA2008 (Internationalized Domain Names for Applications), is defined in RFC
  12. // 5890, RFC 5891, RFC 5892, RFC 5893 and RFC 5894.
  13. // UTS #46 is defined in https://www.unicode.org/reports/tr46.
  14. // See https://unicode.org/cldr/utility/idna.jsp for a visualization of the
  15. // differences between these two standards.
  16. package idna // import "golang.org/x/net/idna"
  17. import (
  18. "fmt"
  19. "strings"
  20. "unicode/utf8"
  21. "golang.org/x/text/secure/bidirule"
  22. "golang.org/x/text/unicode/bidi"
  23. "golang.org/x/text/unicode/norm"
  24. )
  25. // NOTE: Unlike common practice in Go APIs, the functions will return a
  26. // sanitized domain name in case of errors. Browsers sometimes use a partially
  27. // evaluated string as lookup.
  28. // TODO: the current error handling is, in my opinion, the least opinionated.
  29. // Other strategies are also viable, though:
  30. // Option 1) Return an empty string in case of error, but allow the user to
  31. // specify explicitly which errors to ignore.
  32. // Option 2) Return the partially evaluated string if it is itself a valid
  33. // string, otherwise return the empty string in case of error.
  34. // Option 3) Option 1 and 2.
  35. // Option 4) Always return an empty string for now and implement Option 1 as
  36. // needed, and document that the return string may not be empty in case of
  37. // error in the future.
  38. // I think Option 1 is best, but it is quite opinionated.
  39. // ToASCII is a wrapper for Punycode.ToASCII.
  40. func ToASCII(s string) (string, error) {
  41. return Punycode.process(s, true)
  42. }
  43. // ToUnicode is a wrapper for Punycode.ToUnicode.
  44. func ToUnicode(s string) (string, error) {
  45. return Punycode.process(s, false)
  46. }
  47. // An Option configures a Profile at creation time.
  48. type Option func(*options)
  49. // Transitional sets a Profile to use the Transitional mapping as defined in UTS
  50. // #46. This will cause, for example, "ß" to be mapped to "ss". Using the
  51. // transitional mapping provides a compromise between IDNA2003 and IDNA2008
  52. // compatibility. It is used by most browsers when resolving domain names. This
  53. // option is only meaningful if combined with MapForLookup.
  54. func Transitional(transitional bool) Option {
  55. return func(o *options) { o.transitional = true }
  56. }
  57. // VerifyDNSLength sets whether a Profile should fail if any of the IDN parts
  58. // are longer than allowed by the RFC.
  59. func VerifyDNSLength(verify bool) Option {
  60. return func(o *options) { o.verifyDNSLength = verify }
  61. }
  62. // RemoveLeadingDots removes leading label separators. Leading runes that map to
  63. // dots, such as U+3002 IDEOGRAPHIC FULL STOP, are removed as well.
  64. //
  65. // This is the behavior suggested by the UTS #46 and is adopted by some
  66. // browsers.
  67. func RemoveLeadingDots(remove bool) Option {
  68. return func(o *options) { o.removeLeadingDots = remove }
  69. }
  70. // ValidateLabels sets whether to check the mandatory label validation criteria
  71. // as defined in Section 5.4 of RFC 5891. This includes testing for correct use
  72. // of hyphens ('-'), normalization, validity of runes, and the context rules.
  73. func ValidateLabels(enable bool) Option {
  74. return func(o *options) {
  75. // Don't override existing mappings, but set one that at least checks
  76. // normalization if it is not set.
  77. if o.mapping == nil && enable {
  78. o.mapping = normalize
  79. }
  80. o.trie = trie
  81. o.validateLabels = enable
  82. o.fromPuny = validateFromPunycode
  83. }
  84. }
  85. // StrictDomainName limits the set of permissible ASCII characters to those
  86. // allowed in domain names as defined in RFC 1034 (A-Z, a-z, 0-9 and the
  87. // hyphen). This is set by default for MapForLookup and ValidateForRegistration.
  88. //
  89. // This option is useful, for instance, for browsers that allow characters
  90. // outside this range, for example a '_' (U+005F LOW LINE). See
  91. // http://www.rfc-editor.org/std/std3.txt for more details This option
  92. // corresponds to the UseSTD3ASCIIRules option in UTS #46.
  93. func StrictDomainName(use bool) Option {
  94. return func(o *options) {
  95. o.trie = trie
  96. o.useSTD3Rules = use
  97. o.fromPuny = validateFromPunycode
  98. }
  99. }
  100. // NOTE: the following options pull in tables. The tables should not be linked
  101. // in as long as the options are not used.
  102. // BidiRule enables the Bidi rule as defined in RFC 5893. Any application
  103. // that relies on proper validation of labels should include this rule.
  104. func BidiRule() Option {
  105. return func(o *options) { o.bidirule = bidirule.ValidString }
  106. }
  107. // ValidateForRegistration sets validation options to verify that a given IDN is
  108. // properly formatted for registration as defined by Section 4 of RFC 5891.
  109. func ValidateForRegistration() Option {
  110. return func(o *options) {
  111. o.mapping = validateRegistration
  112. StrictDomainName(true)(o)
  113. ValidateLabels(true)(o)
  114. VerifyDNSLength(true)(o)
  115. BidiRule()(o)
  116. }
  117. }
  118. // MapForLookup sets validation and mapping options such that a given IDN is
  119. // transformed for domain name lookup according to the requirements set out in
  120. // Section 5 of RFC 5891. The mappings follow the recommendations of RFC 5894,
  121. // RFC 5895 and UTS 46. It does not add the Bidi Rule. Use the BidiRule option
  122. // to add this check.
  123. //
  124. // The mappings include normalization and mapping case, width and other
  125. // compatibility mappings.
  126. func MapForLookup() Option {
  127. return func(o *options) {
  128. o.mapping = validateAndMap
  129. StrictDomainName(true)(o)
  130. ValidateLabels(true)(o)
  131. }
  132. }
  133. type options struct {
  134. transitional bool
  135. useSTD3Rules bool
  136. validateLabels bool
  137. verifyDNSLength bool
  138. removeLeadingDots bool
  139. trie *idnaTrie
  140. // fromPuny calls validation rules when converting A-labels to U-labels.
  141. fromPuny func(p *Profile, s string) error
  142. // mapping implements a validation and mapping step as defined in RFC 5895
  143. // or UTS 46, tailored to, for example, domain registration or lookup.
  144. mapping func(p *Profile, s string) (mapped string, isBidi bool, err error)
  145. // bidirule, if specified, checks whether s conforms to the Bidi Rule
  146. // defined in RFC 5893.
  147. bidirule func(s string) bool
  148. }
  149. // A Profile defines the configuration of an IDNA mapper.
  150. type Profile struct {
  151. options
  152. }
  153. func apply(o *options, opts []Option) {
  154. for _, f := range opts {
  155. f(o)
  156. }
  157. }
  158. // New creates a new Profile.
  159. //
  160. // With no options, the returned Profile is the most permissive and equals the
  161. // Punycode Profile. Options can be passed to further restrict the Profile. The
  162. // MapForLookup and ValidateForRegistration options set a collection of options,
  163. // for lookup and registration purposes respectively, which can be tailored by
  164. // adding more fine-grained options, where later options override earlier
  165. // options.
  166. func New(o ...Option) *Profile {
  167. p := &Profile{}
  168. apply(&p.options, o)
  169. return p
  170. }
  171. // ToASCII converts a domain or domain label to its ASCII form. For example,
  172. // ToASCII("bücher.example.com") is "xn--bcher-kva.example.com", and
  173. // ToASCII("golang") is "golang". If an error is encountered it will return
  174. // an error and a (partially) processed result.
  175. func (p *Profile) ToASCII(s string) (string, error) {
  176. return p.process(s, true)
  177. }
  178. // ToUnicode converts a domain or domain label to its Unicode form. For example,
  179. // ToUnicode("xn--bcher-kva.example.com") is "bücher.example.com", and
  180. // ToUnicode("golang") is "golang". If an error is encountered it will return
  181. // an error and a (partially) processed result.
  182. func (p *Profile) ToUnicode(s string) (string, error) {
  183. pp := *p
  184. pp.transitional = false
  185. return pp.process(s, false)
  186. }
  187. // String reports a string with a description of the profile for debugging
  188. // purposes. The string format may change with different versions.
  189. func (p *Profile) String() string {
  190. s := ""
  191. if p.transitional {
  192. s = "Transitional"
  193. } else {
  194. s = "NonTransitional"
  195. }
  196. if p.useSTD3Rules {
  197. s += ":UseSTD3Rules"
  198. }
  199. if p.validateLabels {
  200. s += ":ValidateLabels"
  201. }
  202. if p.verifyDNSLength {
  203. s += ":VerifyDNSLength"
  204. }
  205. return s
  206. }
  207. var (
  208. // Punycode is a Profile that does raw punycode processing with a minimum
  209. // of validation.
  210. Punycode *Profile = punycode
  211. // Lookup is the recommended profile for looking up domain names, according
  212. // to Section 5 of RFC 5891. The exact configuration of this profile may
  213. // change over time.
  214. Lookup *Profile = lookup
  215. // Display is the recommended profile for displaying domain names.
  216. // The configuration of this profile may change over time.
  217. Display *Profile = display
  218. // Registration is the recommended profile for checking whether a given
  219. // IDN is valid for registration, according to Section 4 of RFC 5891.
  220. Registration *Profile = registration
  221. punycode = &Profile{}
  222. lookup = &Profile{options{
  223. transitional: true,
  224. useSTD3Rules: true,
  225. validateLabels: true,
  226. trie: trie,
  227. fromPuny: validateFromPunycode,
  228. mapping: validateAndMap,
  229. bidirule: bidirule.ValidString,
  230. }}
  231. display = &Profile{options{
  232. useSTD3Rules: true,
  233. validateLabels: true,
  234. trie: trie,
  235. fromPuny: validateFromPunycode,
  236. mapping: validateAndMap,
  237. bidirule: bidirule.ValidString,
  238. }}
  239. registration = &Profile{options{
  240. useSTD3Rules: true,
  241. validateLabels: true,
  242. verifyDNSLength: true,
  243. trie: trie,
  244. fromPuny: validateFromPunycode,
  245. mapping: validateRegistration,
  246. bidirule: bidirule.ValidString,
  247. }}
  248. // TODO: profiles
  249. // Register: recommended for approving domain names: don't do any mappings
  250. // but rather reject on invalid input. Bundle or block deviation characters.
  251. )
  252. type labelError struct{ label, code_ string }
  253. func (e labelError) code() string { return e.code_ }
  254. func (e labelError) Error() string {
  255. return fmt.Sprintf("idna: invalid label %q", e.label)
  256. }
  257. type runeError rune
  258. func (e runeError) code() string { return "P1" }
  259. func (e runeError) Error() string {
  260. return fmt.Sprintf("idna: disallowed rune %U", e)
  261. }
  262. // process implements the algorithm described in section 4 of UTS #46,
  263. // see https://www.unicode.org/reports/tr46.
  264. func (p *Profile) process(s string, toASCII bool) (string, error) {
  265. var err error
  266. var isBidi bool
  267. if p.mapping != nil {
  268. s, isBidi, err = p.mapping(p, s)
  269. }
  270. // Remove leading empty labels.
  271. if p.removeLeadingDots {
  272. for ; len(s) > 0 && s[0] == '.'; s = s[1:] {
  273. }
  274. }
  275. // TODO: allow for a quick check of the tables data.
  276. // It seems like we should only create this error on ToASCII, but the
  277. // UTS 46 conformance tests suggests we should always check this.
  278. if err == nil && p.verifyDNSLength && s == "" {
  279. err = &labelError{s, "A4"}
  280. }
  281. labels := labelIter{orig: s}
  282. for ; !labels.done(); labels.next() {
  283. label := labels.label()
  284. if label == "" {
  285. // Empty labels are not okay. The label iterator skips the last
  286. // label if it is empty.
  287. if err == nil && p.verifyDNSLength {
  288. err = &labelError{s, "A4"}
  289. }
  290. continue
  291. }
  292. if strings.HasPrefix(label, acePrefix) {
  293. u, err2 := decode(label[len(acePrefix):])
  294. if err2 != nil {
  295. if err == nil {
  296. err = err2
  297. }
  298. // Spec says keep the old label.
  299. continue
  300. }
  301. isBidi = isBidi || bidirule.DirectionString(u) != bidi.LeftToRight
  302. labels.set(u)
  303. if err == nil && p.validateLabels {
  304. err = p.fromPuny(p, u)
  305. }
  306. if err == nil {
  307. // This should be called on NonTransitional, according to the
  308. // spec, but that currently does not have any effect. Use the
  309. // original profile to preserve options.
  310. err = p.validateLabel(u)
  311. }
  312. } else if err == nil {
  313. err = p.validateLabel(label)
  314. }
  315. }
  316. if isBidi && p.bidirule != nil && err == nil {
  317. for labels.reset(); !labels.done(); labels.next() {
  318. if !p.bidirule(labels.label()) {
  319. err = &labelError{s, "B"}
  320. break
  321. }
  322. }
  323. }
  324. if toASCII {
  325. for labels.reset(); !labels.done(); labels.next() {
  326. label := labels.label()
  327. if !ascii(label) {
  328. a, err2 := encode(acePrefix, label)
  329. if err == nil {
  330. err = err2
  331. }
  332. label = a
  333. labels.set(a)
  334. }
  335. n := len(label)
  336. if p.verifyDNSLength && err == nil && (n == 0 || n > 63) {
  337. err = &labelError{label, "A4"}
  338. }
  339. }
  340. }
  341. s = labels.result()
  342. if toASCII && p.verifyDNSLength && err == nil {
  343. // Compute the length of the domain name minus the root label and its dot.
  344. n := len(s)
  345. if n > 0 && s[n-1] == '.' {
  346. n--
  347. }
  348. if len(s) < 1 || n > 253 {
  349. err = &labelError{s, "A4"}
  350. }
  351. }
  352. return s, err
  353. }
  354. func normalize(p *Profile, s string) (mapped string, isBidi bool, err error) {
  355. // TODO: consider first doing a quick check to see if any of these checks
  356. // need to be done. This will make it slower in the general case, but
  357. // faster in the common case.
  358. mapped = norm.NFC.String(s)
  359. isBidi = bidirule.DirectionString(mapped) == bidi.RightToLeft
  360. return mapped, isBidi, nil
  361. }
  362. func validateRegistration(p *Profile, s string) (idem string, bidi bool, err error) {
  363. // TODO: filter need for normalization in loop below.
  364. if !norm.NFC.IsNormalString(s) {
  365. return s, false, &labelError{s, "V1"}
  366. }
  367. for i := 0; i < len(s); {
  368. v, sz := trie.lookupString(s[i:])
  369. if sz == 0 {
  370. return s, bidi, runeError(utf8.RuneError)
  371. }
  372. bidi = bidi || info(v).isBidi(s[i:])
  373. // Copy bytes not copied so far.
  374. switch p.simplify(info(v).category()) {
  375. // TODO: handle the NV8 defined in the Unicode idna data set to allow
  376. // for strict conformance to IDNA2008.
  377. case valid, deviation:
  378. case disallowed, mapped, unknown, ignored:
  379. r, _ := utf8.DecodeRuneInString(s[i:])
  380. return s, bidi, runeError(r)
  381. }
  382. i += sz
  383. }
  384. return s, bidi, nil
  385. }
  386. func (c info) isBidi(s string) bool {
  387. if !c.isMapped() {
  388. return c&attributesMask == rtl
  389. }
  390. // TODO: also store bidi info for mapped data. This is possible, but a bit
  391. // cumbersome and not for the common case.
  392. p, _ := bidi.LookupString(s)
  393. switch p.Class() {
  394. case bidi.R, bidi.AL, bidi.AN:
  395. return true
  396. }
  397. return false
  398. }
  399. func validateAndMap(p *Profile, s string) (vm string, bidi bool, err error) {
  400. var (
  401. b []byte
  402. k int
  403. )
  404. // combinedInfoBits contains the or-ed bits of all runes. We use this
  405. // to derive the mayNeedNorm bit later. This may trigger normalization
  406. // overeagerly, but it will not do so in the common case. The end result
  407. // is another 10% saving on BenchmarkProfile for the common case.
  408. var combinedInfoBits info
  409. for i := 0; i < len(s); {
  410. v, sz := trie.lookupString(s[i:])
  411. if sz == 0 {
  412. b = append(b, s[k:i]...)
  413. b = append(b, "\ufffd"...)
  414. k = len(s)
  415. if err == nil {
  416. err = runeError(utf8.RuneError)
  417. }
  418. break
  419. }
  420. combinedInfoBits |= info(v)
  421. bidi = bidi || info(v).isBidi(s[i:])
  422. start := i
  423. i += sz
  424. // Copy bytes not copied so far.
  425. switch p.simplify(info(v).category()) {
  426. case valid:
  427. continue
  428. case disallowed:
  429. if err == nil {
  430. r, _ := utf8.DecodeRuneInString(s[start:])
  431. err = runeError(r)
  432. }
  433. continue
  434. case mapped, deviation:
  435. b = append(b, s[k:start]...)
  436. b = info(v).appendMapping(b, s[start:i])
  437. case ignored:
  438. b = append(b, s[k:start]...)
  439. // drop the rune
  440. case unknown:
  441. b = append(b, s[k:start]...)
  442. b = append(b, "\ufffd"...)
  443. }
  444. k = i
  445. }
  446. if k == 0 {
  447. // No changes so far.
  448. if combinedInfoBits&mayNeedNorm != 0 {
  449. s = norm.NFC.String(s)
  450. }
  451. } else {
  452. b = append(b, s[k:]...)
  453. if norm.NFC.QuickSpan(b) != len(b) {
  454. b = norm.NFC.Bytes(b)
  455. }
  456. // TODO: the punycode converters require strings as input.
  457. s = string(b)
  458. }
  459. return s, bidi, err
  460. }
  461. // A labelIter allows iterating over domain name labels.
  462. type labelIter struct {
  463. orig string
  464. slice []string
  465. curStart int
  466. curEnd int
  467. i int
  468. }
  469. func (l *labelIter) reset() {
  470. l.curStart = 0
  471. l.curEnd = 0
  472. l.i = 0
  473. }
  474. func (l *labelIter) done() bool {
  475. return l.curStart >= len(l.orig)
  476. }
  477. func (l *labelIter) result() string {
  478. if l.slice != nil {
  479. return strings.Join(l.slice, ".")
  480. }
  481. return l.orig
  482. }
  483. func (l *labelIter) label() string {
  484. if l.slice != nil {
  485. return l.slice[l.i]
  486. }
  487. p := strings.IndexByte(l.orig[l.curStart:], '.')
  488. l.curEnd = l.curStart + p
  489. if p == -1 {
  490. l.curEnd = len(l.orig)
  491. }
  492. return l.orig[l.curStart:l.curEnd]
  493. }
  494. // next sets the value to the next label. It skips the last label if it is empty.
  495. func (l *labelIter) next() {
  496. l.i++
  497. if l.slice != nil {
  498. if l.i >= len(l.slice) || l.i == len(l.slice)-1 && l.slice[l.i] == "" {
  499. l.curStart = len(l.orig)
  500. }
  501. } else {
  502. l.curStart = l.curEnd + 1
  503. if l.curStart == len(l.orig)-1 && l.orig[l.curStart] == '.' {
  504. l.curStart = len(l.orig)
  505. }
  506. }
  507. }
  508. func (l *labelIter) set(s string) {
  509. if l.slice == nil {
  510. l.slice = strings.Split(l.orig, ".")
  511. }
  512. l.slice[l.i] = s
  513. }
  514. // acePrefix is the ASCII Compatible Encoding prefix.
  515. const acePrefix = "xn--"
  516. func (p *Profile) simplify(cat category) category {
  517. switch cat {
  518. case disallowedSTD3Mapped:
  519. if p.useSTD3Rules {
  520. cat = disallowed
  521. } else {
  522. cat = mapped
  523. }
  524. case disallowedSTD3Valid:
  525. if p.useSTD3Rules {
  526. cat = disallowed
  527. } else {
  528. cat = valid
  529. }
  530. case deviation:
  531. if !p.transitional {
  532. cat = valid
  533. }
  534. case validNV8, validXV8:
  535. // TODO: handle V2008
  536. cat = valid
  537. }
  538. return cat
  539. }
  540. func validateFromPunycode(p *Profile, s string) error {
  541. if !norm.NFC.IsNormalString(s) {
  542. return &labelError{s, "V1"}
  543. }
  544. // TODO: detect whether string may have to be normalized in the following
  545. // loop.
  546. for i := 0; i < len(s); {
  547. v, sz := trie.lookupString(s[i:])
  548. if sz == 0 {
  549. return runeError(utf8.RuneError)
  550. }
  551. if c := p.simplify(info(v).category()); c != valid && c != deviation {
  552. return &labelError{s, "V6"}
  553. }
  554. i += sz
  555. }
  556. return nil
  557. }
  558. const (
  559. zwnj = "\u200c"
  560. zwj = "\u200d"
  561. )
  562. type joinState int8
  563. const (
  564. stateStart joinState = iota
  565. stateVirama
  566. stateBefore
  567. stateBeforeVirama
  568. stateAfter
  569. stateFAIL
  570. )
  571. var joinStates = [][numJoinTypes]joinState{
  572. stateStart: {
  573. joiningL: stateBefore,
  574. joiningD: stateBefore,
  575. joinZWNJ: stateFAIL,
  576. joinZWJ: stateFAIL,
  577. joinVirama: stateVirama,
  578. },
  579. stateVirama: {
  580. joiningL: stateBefore,
  581. joiningD: stateBefore,
  582. },
  583. stateBefore: {
  584. joiningL: stateBefore,
  585. joiningD: stateBefore,
  586. joiningT: stateBefore,
  587. joinZWNJ: stateAfter,
  588. joinZWJ: stateFAIL,
  589. joinVirama: stateBeforeVirama,
  590. },
  591. stateBeforeVirama: {
  592. joiningL: stateBefore,
  593. joiningD: stateBefore,
  594. joiningT: stateBefore,
  595. },
  596. stateAfter: {
  597. joiningL: stateFAIL,
  598. joiningD: stateBefore,
  599. joiningT: stateAfter,
  600. joiningR: stateStart,
  601. joinZWNJ: stateFAIL,
  602. joinZWJ: stateFAIL,
  603. joinVirama: stateAfter, // no-op as we can't accept joiners here
  604. },
  605. stateFAIL: {
  606. 0: stateFAIL,
  607. joiningL: stateFAIL,
  608. joiningD: stateFAIL,
  609. joiningT: stateFAIL,
  610. joiningR: stateFAIL,
  611. joinZWNJ: stateFAIL,
  612. joinZWJ: stateFAIL,
  613. joinVirama: stateFAIL,
  614. },
  615. }
  616. // validateLabel validates the criteria from Section 4.1. Item 1, 4, and 6 are
  617. // already implicitly satisfied by the overall implementation.
  618. func (p *Profile) validateLabel(s string) (err error) {
  619. if s == "" {
  620. if p.verifyDNSLength {
  621. return &labelError{s, "A4"}
  622. }
  623. return nil
  624. }
  625. if !p.validateLabels {
  626. return nil
  627. }
  628. trie := p.trie // p.validateLabels is only set if trie is set.
  629. if len(s) > 4 && s[2] == '-' && s[3] == '-' {
  630. return &labelError{s, "V2"}
  631. }
  632. if s[0] == '-' || s[len(s)-1] == '-' {
  633. return &labelError{s, "V3"}
  634. }
  635. // TODO: merge the use of this in the trie.
  636. v, sz := trie.lookupString(s)
  637. x := info(v)
  638. if x.isModifier() {
  639. return &labelError{s, "V5"}
  640. }
  641. // Quickly return in the absence of zero-width (non) joiners.
  642. if strings.Index(s, zwj) == -1 && strings.Index(s, zwnj) == -1 {
  643. return nil
  644. }
  645. st := stateStart
  646. for i := 0; ; {
  647. jt := x.joinType()
  648. if s[i:i+sz] == zwj {
  649. jt = joinZWJ
  650. } else if s[i:i+sz] == zwnj {
  651. jt = joinZWNJ
  652. }
  653. st = joinStates[st][jt]
  654. if x.isViramaModifier() {
  655. st = joinStates[st][joinVirama]
  656. }
  657. if i += sz; i == len(s) {
  658. break
  659. }
  660. v, sz = trie.lookupString(s[i:])
  661. x = info(v)
  662. }
  663. if st == stateFAIL || st == stateAfter {
  664. return &labelError{s, "C"}
  665. }
  666. return nil
  667. }
  668. func ascii(s string) bool {
  669. for i := 0; i < len(s); i++ {
  670. if s[i] >= utf8.RuneSelf {
  671. return false
  672. }
  673. }
  674. return true
  675. }