forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/golang.org/x/text/secure/bidirule/bidirule.go

597 lines
9.6 KiB
Go
Raw Normal View History

2024-02-18 10:42:21 +00:00
// 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 bidirule implements the Bidi Rule defined by RFC 5893.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// This package is under development. The API may change without notice and
2024-02-18 10:42:21 +00:00
// without preserving backward compatibility.
2024-02-18 10:42:21 +00:00
package bidirule
import (
"errors"
"unicode/utf8"
"golang.org/x/text/transform"
"golang.org/x/text/unicode/bidi"
)
// This file contains an implementation of RFC 5893: Right-to-Left Scripts for
2024-02-18 10:42:21 +00:00
// Internationalized Domain Names for Applications (IDNA)
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// A label is an individual component of a domain name. Labels are usually
2024-02-18 10:42:21 +00:00
// shown separated by dots; for example, the domain name "www.example.com" is
2024-02-18 10:42:21 +00:00
// composed of three labels: "www", "example", and "com".
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// An RTL label is a label that contains at least one character of class R, AL,
2024-02-18 10:42:21 +00:00
// or AN. An LTR label is any label that is not an RTL label.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// A "Bidi domain name" is a domain name that contains at least one RTL label.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// The following guarantees can be made based on the above:
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// o In a domain name consisting of only labels that satisfy the rule,
2024-02-18 10:42:21 +00:00
// the requirements of Section 3 are satisfied. Note that even LTR
2024-02-18 10:42:21 +00:00
// labels and pure ASCII labels have to be tested.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// o In a domain name consisting of only LDH labels (as defined in the
2024-02-18 10:42:21 +00:00
// Definitions document [RFC5890]) and labels that satisfy the rule,
2024-02-18 10:42:21 +00:00
// the requirements of Section 3 are satisfied as long as a label
2024-02-18 10:42:21 +00:00
// that starts with an ASCII digit does not come after a
2024-02-18 10:42:21 +00:00
// right-to-left label.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// No guarantee is given for other combinations.
// ErrInvalid indicates a label is invalid according to the Bidi Rule.
2024-02-18 10:42:21 +00:00
var ErrInvalid = errors.New("bidirule: failed Bidi Rule")
type ruleState uint8
const (
ruleInitial ruleState = iota
2024-02-18 10:42:21 +00:00
ruleLTR
2024-02-18 10:42:21 +00:00
ruleLTRFinal
2024-02-18 10:42:21 +00:00
ruleRTL
2024-02-18 10:42:21 +00:00
ruleRTLFinal
2024-02-18 10:42:21 +00:00
ruleInvalid
)
type ruleTransition struct {
next ruleState
2024-02-18 10:42:21 +00:00
mask uint16
}
var transitions = [...][2]ruleTransition{
2024-02-18 10:42:21 +00:00
// [2.1] The first character must be a character with Bidi property L, R, or
2024-02-18 10:42:21 +00:00
// AL. If it has the R or AL property, it is an RTL label; if it has the L
2024-02-18 10:42:21 +00:00
// property, it is an LTR label.
2024-02-18 10:42:21 +00:00
ruleInitial: {
2024-02-18 10:42:21 +00:00
{ruleLTRFinal, 1 << bidi.L},
2024-02-18 10:42:21 +00:00
{ruleRTLFinal, 1<<bidi.R | 1<<bidi.AL},
},
2024-02-18 10:42:21 +00:00
ruleRTL: {
2024-02-18 10:42:21 +00:00
// [2.3] In an RTL label, the end of the label must be a character with
2024-02-18 10:42:21 +00:00
// Bidi property R, AL, EN, or AN, followed by zero or more characters
2024-02-18 10:42:21 +00:00
// with Bidi property NSM.
2024-02-18 10:42:21 +00:00
{ruleRTLFinal, 1<<bidi.R | 1<<bidi.AL | 1<<bidi.EN | 1<<bidi.AN},
// [2.2] In an RTL label, only characters with the Bidi properties R,
2024-02-18 10:42:21 +00:00
// AL, AN, EN, ES, CS, ET, ON, BN, or NSM are allowed.
2024-02-18 10:42:21 +00:00
// We exclude the entries from [2.3]
2024-02-18 10:42:21 +00:00
{ruleRTL, 1<<bidi.ES | 1<<bidi.CS | 1<<bidi.ET | 1<<bidi.ON | 1<<bidi.BN | 1<<bidi.NSM},
},
2024-02-18 10:42:21 +00:00
ruleRTLFinal: {
2024-02-18 10:42:21 +00:00
// [2.3] In an RTL label, the end of the label must be a character with
2024-02-18 10:42:21 +00:00
// Bidi property R, AL, EN, or AN, followed by zero or more characters
2024-02-18 10:42:21 +00:00
// with Bidi property NSM.
2024-02-18 10:42:21 +00:00
{ruleRTLFinal, 1<<bidi.R | 1<<bidi.AL | 1<<bidi.EN | 1<<bidi.AN | 1<<bidi.NSM},
// [2.2] In an RTL label, only characters with the Bidi properties R,
2024-02-18 10:42:21 +00:00
// AL, AN, EN, ES, CS, ET, ON, BN, or NSM are allowed.
2024-02-18 10:42:21 +00:00
// We exclude the entries from [2.3] and NSM.
2024-02-18 10:42:21 +00:00
{ruleRTL, 1<<bidi.ES | 1<<bidi.CS | 1<<bidi.ET | 1<<bidi.ON | 1<<bidi.BN},
},
2024-02-18 10:42:21 +00:00
ruleLTR: {
2024-02-18 10:42:21 +00:00
// [2.6] In an LTR label, the end of the label must be a character with
2024-02-18 10:42:21 +00:00
// Bidi property L or EN, followed by zero or more characters with Bidi
2024-02-18 10:42:21 +00:00
// property NSM.
2024-02-18 10:42:21 +00:00
{ruleLTRFinal, 1<<bidi.L | 1<<bidi.EN},
// [2.5] In an LTR label, only characters with the Bidi properties L,
2024-02-18 10:42:21 +00:00
// EN, ES, CS, ET, ON, BN, or NSM are allowed.
2024-02-18 10:42:21 +00:00
// We exclude the entries from [2.6].
2024-02-18 10:42:21 +00:00
{ruleLTR, 1<<bidi.ES | 1<<bidi.CS | 1<<bidi.ET | 1<<bidi.ON | 1<<bidi.BN | 1<<bidi.NSM},
},
2024-02-18 10:42:21 +00:00
ruleLTRFinal: {
2024-02-18 10:42:21 +00:00
// [2.6] In an LTR label, the end of the label must be a character with
2024-02-18 10:42:21 +00:00
// Bidi property L or EN, followed by zero or more characters with Bidi
2024-02-18 10:42:21 +00:00
// property NSM.
2024-02-18 10:42:21 +00:00
{ruleLTRFinal, 1<<bidi.L | 1<<bidi.EN | 1<<bidi.NSM},
// [2.5] In an LTR label, only characters with the Bidi properties L,
2024-02-18 10:42:21 +00:00
// EN, ES, CS, ET, ON, BN, or NSM are allowed.
2024-02-18 10:42:21 +00:00
// We exclude the entries from [2.6].
2024-02-18 10:42:21 +00:00
{ruleLTR, 1<<bidi.ES | 1<<bidi.CS | 1<<bidi.ET | 1<<bidi.ON | 1<<bidi.BN},
},
2024-02-18 10:42:21 +00:00
ruleInvalid: {
2024-02-18 10:42:21 +00:00
{ruleInvalid, 0},
2024-02-18 10:42:21 +00:00
{ruleInvalid, 0},
},
}
// [2.4] In an RTL label, if an EN is present, no AN may be present, and
2024-02-18 10:42:21 +00:00
// vice versa.
2024-02-18 10:42:21 +00:00
const exclusiveRTL = uint16(1<<bidi.EN | 1<<bidi.AN)
// From RFC 5893
2024-02-18 10:42:21 +00:00
// An RTL label is a label that contains at least one character of type
2024-02-18 10:42:21 +00:00
// R, AL, or AN.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// An LTR label is any label that is not an RTL label.
// Direction reports the direction of the given label as defined by RFC 5893.
2024-02-18 10:42:21 +00:00
// The Bidi Rule does not have to be applied to labels of the category
2024-02-18 10:42:21 +00:00
// LeftToRight.
2024-02-18 10:42:21 +00:00
func Direction(b []byte) bidi.Direction {
2024-02-18 10:42:21 +00:00
for i := 0; i < len(b); {
2024-02-18 10:42:21 +00:00
e, sz := bidi.Lookup(b[i:])
2024-02-18 10:42:21 +00:00
if sz == 0 {
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
c := e.Class()
2024-02-18 10:42:21 +00:00
if c == bidi.R || c == bidi.AL || c == bidi.AN {
2024-02-18 10:42:21 +00:00
return bidi.RightToLeft
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
i += sz
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return bidi.LeftToRight
2024-02-18 10:42:21 +00:00
}
// DirectionString reports the direction of the given label as defined by RFC
2024-02-18 10:42:21 +00:00
// 5893. The Bidi Rule does not have to be applied to labels of the category
2024-02-18 10:42:21 +00:00
// LeftToRight.
2024-02-18 10:42:21 +00:00
func DirectionString(s string) bidi.Direction {
2024-02-18 10:42:21 +00:00
for i := 0; i < len(s); {
2024-02-18 10:42:21 +00:00
e, sz := bidi.LookupString(s[i:])
2024-02-18 10:42:21 +00:00
if sz == 0 {
2024-02-18 10:42:21 +00:00
i++
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
c := e.Class()
2024-02-18 10:42:21 +00:00
if c == bidi.R || c == bidi.AL || c == bidi.AN {
2024-02-18 10:42:21 +00:00
return bidi.RightToLeft
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
i += sz
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return bidi.LeftToRight
2024-02-18 10:42:21 +00:00
}
// Valid reports whether b conforms to the BiDi rule.
2024-02-18 10:42:21 +00:00
func Valid(b []byte) bool {
2024-02-18 10:42:21 +00:00
var t Transformer
2024-02-18 10:42:21 +00:00
if n, ok := t.advance(b); !ok || n < len(b) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return t.isFinal()
2024-02-18 10:42:21 +00:00
}
// ValidString reports whether s conforms to the BiDi rule.
2024-02-18 10:42:21 +00:00
func ValidString(s string) bool {
2024-02-18 10:42:21 +00:00
var t Transformer
2024-02-18 10:42:21 +00:00
if n, ok := t.advanceString(s); !ok || n < len(s) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return t.isFinal()
2024-02-18 10:42:21 +00:00
}
// New returns a Transformer that verifies that input adheres to the Bidi Rule.
2024-02-18 10:42:21 +00:00
func New() *Transformer {
2024-02-18 10:42:21 +00:00
return &Transformer{}
2024-02-18 10:42:21 +00:00
}
// Transformer implements transform.Transform.
2024-02-18 10:42:21 +00:00
type Transformer struct {
state ruleState
2024-02-18 10:42:21 +00:00
hasRTL bool
seen uint16
2024-02-18 10:42:21 +00:00
}
// A rule can only be violated for "Bidi Domain names", meaning if one of the
2024-02-18 10:42:21 +00:00
// following categories has been observed.
2024-02-18 10:42:21 +00:00
func (t *Transformer) isRTL() bool {
2024-02-18 10:42:21 +00:00
const isRTL = 1<<bidi.R | 1<<bidi.AL | 1<<bidi.AN
2024-02-18 10:42:21 +00:00
return t.seen&isRTL != 0
2024-02-18 10:42:21 +00:00
}
// Reset implements transform.Transformer.
2024-02-18 10:42:21 +00:00
func (t *Transformer) Reset() { *t = Transformer{} }
// Transform implements transform.Transformer. This Transformer has state and
2024-02-18 10:42:21 +00:00
// needs to be reset between uses.
2024-02-18 10:42:21 +00:00
func (t *Transformer) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
2024-02-18 10:42:21 +00:00
if len(dst) < len(src) {
2024-02-18 10:42:21 +00:00
src = src[:len(dst)]
2024-02-18 10:42:21 +00:00
atEOF = false
2024-02-18 10:42:21 +00:00
err = transform.ErrShortDst
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
n, err1 := t.Span(src, atEOF)
2024-02-18 10:42:21 +00:00
copy(dst, src[:n])
2024-02-18 10:42:21 +00:00
if err == nil || err1 != nil && err1 != transform.ErrShortSrc {
2024-02-18 10:42:21 +00:00
err = err1
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return n, n, err
2024-02-18 10:42:21 +00:00
}
// Span returns the first n bytes of src that conform to the Bidi rule.
2024-02-18 10:42:21 +00:00
func (t *Transformer) Span(src []byte, atEOF bool) (n int, err error) {
2024-02-18 10:42:21 +00:00
if t.state == ruleInvalid && t.isRTL() {
2024-02-18 10:42:21 +00:00
return 0, ErrInvalid
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
n, ok := t.advance(src)
2024-02-18 10:42:21 +00:00
switch {
2024-02-18 10:42:21 +00:00
case !ok:
2024-02-18 10:42:21 +00:00
err = ErrInvalid
2024-02-18 10:42:21 +00:00
case n < len(src):
2024-02-18 10:42:21 +00:00
if !atEOF {
2024-02-18 10:42:21 +00:00
err = transform.ErrShortSrc
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
err = ErrInvalid
2024-02-18 10:42:21 +00:00
case !t.isFinal():
2024-02-18 10:42:21 +00:00
err = ErrInvalid
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return n, err
2024-02-18 10:42:21 +00:00
}
// Precomputing the ASCII values decreases running time for the ASCII fast path
2024-02-18 10:42:21 +00:00
// by about 30%.
2024-02-18 10:42:21 +00:00
var asciiTable [128]bidi.Properties
func init() {
2024-02-18 10:42:21 +00:00
for i := range asciiTable {
2024-02-18 10:42:21 +00:00
p, _ := bidi.LookupRune(rune(i))
2024-02-18 10:42:21 +00:00
asciiTable[i] = p
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
func (t *Transformer) advance(s []byte) (n int, ok bool) {
2024-02-18 10:42:21 +00:00
var e bidi.Properties
2024-02-18 10:42:21 +00:00
var sz int
2024-02-18 10:42:21 +00:00
for n < len(s) {
2024-02-18 10:42:21 +00:00
if s[n] < utf8.RuneSelf {
2024-02-18 10:42:21 +00:00
e, sz = asciiTable[s[n]], 1
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
e, sz = bidi.Lookup(s[n:])
2024-02-18 10:42:21 +00:00
if sz <= 1 {
2024-02-18 10:42:21 +00:00
if sz == 1 {
2024-02-18 10:42:21 +00:00
// We always consider invalid UTF-8 to be invalid, even if
2024-02-18 10:42:21 +00:00
// the string has not yet been determined to be RTL.
2024-02-18 10:42:21 +00:00
// TODO: is this correct?
2024-02-18 10:42:21 +00:00
return n, false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return n, true // incomplete UTF-8 encoding
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
// TODO: using CompactClass would result in noticeable speedup.
2024-02-18 10:42:21 +00:00
// See unicode/bidi/prop.go:Properties.CompactClass.
2024-02-18 10:42:21 +00:00
c := uint16(1 << e.Class())
2024-02-18 10:42:21 +00:00
t.seen |= c
2024-02-18 10:42:21 +00:00
if t.seen&exclusiveRTL == exclusiveRTL {
2024-02-18 10:42:21 +00:00
t.state = ruleInvalid
2024-02-18 10:42:21 +00:00
return n, false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
switch tr := transitions[t.state]; {
2024-02-18 10:42:21 +00:00
case tr[0].mask&c != 0:
2024-02-18 10:42:21 +00:00
t.state = tr[0].next
2024-02-18 10:42:21 +00:00
case tr[1].mask&c != 0:
2024-02-18 10:42:21 +00:00
t.state = tr[1].next
2024-02-18 10:42:21 +00:00
default:
2024-02-18 10:42:21 +00:00
t.state = ruleInvalid
2024-02-18 10:42:21 +00:00
if t.isRTL() {
2024-02-18 10:42:21 +00:00
return n, false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
n += sz
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return n, true
2024-02-18 10:42:21 +00:00
}
func (t *Transformer) advanceString(s string) (n int, ok bool) {
2024-02-18 10:42:21 +00:00
var e bidi.Properties
2024-02-18 10:42:21 +00:00
var sz int
2024-02-18 10:42:21 +00:00
for n < len(s) {
2024-02-18 10:42:21 +00:00
if s[n] < utf8.RuneSelf {
2024-02-18 10:42:21 +00:00
e, sz = asciiTable[s[n]], 1
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
e, sz = bidi.LookupString(s[n:])
2024-02-18 10:42:21 +00:00
if sz <= 1 {
2024-02-18 10:42:21 +00:00
if sz == 1 {
2024-02-18 10:42:21 +00:00
return n, false // invalid UTF-8
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return n, true // incomplete UTF-8 encoding
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
// TODO: using CompactClass results in noticeable speedup.
2024-02-18 10:42:21 +00:00
// See unicode/bidi/prop.go:Properties.CompactClass.
2024-02-18 10:42:21 +00:00
c := uint16(1 << e.Class())
2024-02-18 10:42:21 +00:00
t.seen |= c
2024-02-18 10:42:21 +00:00
if t.seen&exclusiveRTL == exclusiveRTL {
2024-02-18 10:42:21 +00:00
t.state = ruleInvalid
2024-02-18 10:42:21 +00:00
return n, false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
switch tr := transitions[t.state]; {
2024-02-18 10:42:21 +00:00
case tr[0].mask&c != 0:
2024-02-18 10:42:21 +00:00
t.state = tr[0].next
2024-02-18 10:42:21 +00:00
case tr[1].mask&c != 0:
2024-02-18 10:42:21 +00:00
t.state = tr[1].next
2024-02-18 10:42:21 +00:00
default:
2024-02-18 10:42:21 +00:00
t.state = ruleInvalid
2024-02-18 10:42:21 +00:00
if t.isRTL() {
2024-02-18 10:42:21 +00:00
return n, false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
n += sz
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return n, true
2024-02-18 10:42:21 +00:00
}