Add ValidateMnemonic method (#1713)
This commit is contained in:
parent
ac5f683ac1
commit
2d2f2fbc51
|
@ -24,6 +24,7 @@ import (
|
||||||
// https://github.com/trezor/trezor-crypto/blob/master/bip39.c
|
// https://github.com/trezor/trezor-crypto/blob/master/bip39.c
|
||||||
// https://github.com/trezor/python-mnemonic/blob/master/mnemonic/mnemonic.py
|
// https://github.com/trezor/python-mnemonic/blob/master/mnemonic/mnemonic.py
|
||||||
// https://github.com/bitpay/bitcore-mnemonic/blob/master/lib/mnemonic.js (used in eth-lightwallet.js)
|
// https://github.com/bitpay/bitcore-mnemonic/blob/master/lib/mnemonic.js (used in eth-lightwallet.js)
|
||||||
|
// https://github.com/tyler-smith/go-bip39/blob/master/bip39.go
|
||||||
|
|
||||||
const defaultSalt = "mnemonic"
|
const defaultSalt = "mnemonic"
|
||||||
|
|
||||||
|
@ -73,6 +74,21 @@ var (
|
||||||
rightShift11BitsDivider = big.NewInt(2048)
|
rightShift11BitsDivider = big.NewInt(2048)
|
||||||
bigOne = big.NewInt(1)
|
bigOne = big.NewInt(1)
|
||||||
bigTwo = big.NewInt(2)
|
bigTwo = big.NewInt(2)
|
||||||
|
|
||||||
|
wordLengthChecksumMasksMapping = map[int]*big.Int{
|
||||||
|
12: big.NewInt(15),
|
||||||
|
15: big.NewInt(31),
|
||||||
|
18: big.NewInt(63),
|
||||||
|
21: big.NewInt(127),
|
||||||
|
24: big.NewInt(255),
|
||||||
|
}
|
||||||
|
|
||||||
|
wordLengthChecksumShiftMapping = map[int]*big.Int{
|
||||||
|
12: big.NewInt(16),
|
||||||
|
15: big.NewInt(8),
|
||||||
|
18: big.NewInt(4),
|
||||||
|
21: big.NewInt(2),
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
// Language is language identifier
|
// Language is language identifier
|
||||||
|
@ -201,11 +217,11 @@ func (m *Mnemonic) MnemonicPhrase(strength EntropyStrength, language Language) (
|
||||||
return strings.Join(words, wordSeperator), nil
|
return strings.Join(words, wordSeperator), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ValidMnemonic validates mnemonic string
|
// ValidateMnemonic validates that all words from a mnemonic string are in wordlist and that checksum is valid
|
||||||
func (m *Mnemonic) ValidMnemonic(mnemonic string, language Language) bool {
|
func (m *Mnemonic) ValidateMnemonic(mnemonic string, language Language) error {
|
||||||
wordList, err := m.WordList(language)
|
wordList, err := m.WordList(language)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false
|
return errors.New("invalid language specified")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a list of all the words in the mnemonic sentence
|
// Create a list of all the words in the mnemonic sentence
|
||||||
|
@ -216,17 +232,61 @@ func (m *Mnemonic) ValidMnemonic(mnemonic string, language Language) bool {
|
||||||
|
|
||||||
// The number of words should be 12, 15, 18, 21 or 24
|
// The number of words should be 12, 15, 18, 21 or 24
|
||||||
if numOfWords%3 != 0 || numOfWords < 12 || numOfWords > 24 {
|
if numOfWords%3 != 0 || numOfWords < 12 || numOfWords > 24 {
|
||||||
return false
|
return errors.New("mnemonic contains an invalid number of words")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create reverse lookup map for dictionary
|
||||||
|
wordMap := map[string]int{}
|
||||||
|
for i, v := range wordList {
|
||||||
|
wordMap[v] = i
|
||||||
|
}
|
||||||
|
|
||||||
|
b := big.NewInt(0)
|
||||||
|
|
||||||
// Check if all words belong in the wordlist
|
// Check if all words belong in the wordlist
|
||||||
for i := 0; i < numOfWords; i++ {
|
for i := 0; i < numOfWords; i++ {
|
||||||
if !contains(wordList, words[i]) {
|
|
||||||
return false
|
wordIndex, ok := wordMap[words[i]]
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("word %s not found in the dictionary", words[i])
|
||||||
}
|
}
|
||||||
|
var wordBytes [2]byte
|
||||||
|
binary.BigEndian.PutUint16(wordBytes[:], uint16(wordIndex))
|
||||||
|
b = b.Mul(b, rightShift11BitsDivider)
|
||||||
|
b = b.Or(b, big.NewInt(0).SetBytes(wordBytes[:]))
|
||||||
}
|
}
|
||||||
|
|
||||||
return true
|
checksum := big.NewInt(0)
|
||||||
|
checksumMask := wordLengthChecksumMasksMapping[numOfWords]
|
||||||
|
checksum = checksum.And(b, checksumMask)
|
||||||
|
|
||||||
|
b.Div(b, big.NewInt(0).Add(checksumMask, bigOne))
|
||||||
|
|
||||||
|
// Calculate entropy from mnemonic
|
||||||
|
entropy := b.Bytes()
|
||||||
|
entropy = padByteSlice(entropy, len(words)/3*4)
|
||||||
|
|
||||||
|
// Calculate checksum from entropy derived above
|
||||||
|
hasher := sha256.New()
|
||||||
|
hasher.Write(entropy)
|
||||||
|
computedChecksumBytes := hasher.Sum(nil)
|
||||||
|
computedChecksum := big.NewInt(int64(computedChecksumBytes[0]))
|
||||||
|
|
||||||
|
if l := len(words); l != 24 {
|
||||||
|
checksumShift := wordLengthChecksumShiftMapping[l]
|
||||||
|
computedChecksum.Div(computedChecksum, checksumShift)
|
||||||
|
}
|
||||||
|
|
||||||
|
if checksum.Cmp(computedChecksum) != 0 {
|
||||||
|
return errors.New("checksum for mnemonic seed is invalid")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ValidMnemonic validates mnemonic string
|
||||||
|
func (m *Mnemonic) ValidMnemonic(mnemonic string, language Language) bool {
|
||||||
|
return m.ValidateMnemonic(mnemonic, language) == nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// WordList returns list of words for a given language
|
// WordList returns list of words for a given language
|
||||||
|
|
3
go.mod
3
go.mod
|
@ -47,6 +47,9 @@ require (
|
||||||
github.com/syndtr/goleveldb v1.0.0
|
github.com/syndtr/goleveldb v1.0.0
|
||||||
go.uber.org/zap v1.13.0
|
go.uber.org/zap v1.13.0
|
||||||
golang.org/x/crypto v0.0.0-20191119213627-4f8c1d86b1ba
|
golang.org/x/crypto v0.0.0-20191119213627-4f8c1d86b1ba
|
||||||
|
golang.org/x/net v0.0.0-20190930134127-c5a3c61f89f3 // indirect
|
||||||
|
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e // indirect
|
||||||
|
golang.org/x/tools v0.0.0-20191213032237-7093a17b0467 // indirect
|
||||||
gopkg.in/go-playground/assert.v1 v1.2.1 // indirect
|
gopkg.in/go-playground/assert.v1 v1.2.1 // indirect
|
||||||
gopkg.in/go-playground/validator.v9 v9.29.1
|
gopkg.in/go-playground/validator.v9 v9.29.1
|
||||||
gopkg.in/natefinch/lumberjack.v2 v2.0.0
|
gopkg.in/natefinch/lumberjack.v2 v2.0.0
|
||||||
|
|
10
go.sum
10
go.sum
|
@ -206,6 +206,7 @@ github.com/gorilla/mux v1.7.1/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2z
|
||||||
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
|
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
|
||||||
github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM=
|
github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM=
|
||||||
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||||
|
github.com/goware/modvendor v0.1.0/go.mod h1:iTv+KoD55Rr5NW/P2WlDEJQ2Wa7+LRblD6/u/xJl/i0=
|
||||||
github.com/graph-gophers/graphql-go v0.0.0-20190724201507-010347b5f9e6/go.mod h1:Au3iQ8DvDis8hZ4q2OzRcaKYlAsPt+fYvib5q4nIqu4=
|
github.com/graph-gophers/graphql-go v0.0.0-20190724201507-010347b5f9e6/go.mod h1:Au3iQ8DvDis8hZ4q2OzRcaKYlAsPt+fYvib5q4nIqu4=
|
||||||
github.com/gxed/hashland/keccakpg v0.0.1/go.mod h1:kRzw3HkwxFU1mpmPP8v1WyQzwdGfmKFJ6tItnhQ67kU=
|
github.com/gxed/hashland/keccakpg v0.0.1/go.mod h1:kRzw3HkwxFU1mpmPP8v1WyQzwdGfmKFJ6tItnhQ67kU=
|
||||||
github.com/gxed/hashland/murmur3 v0.0.1/go.mod h1:KjXop02n4/ckmZSnY2+HKcLud/tcmvhST0bie/0lS48=
|
github.com/gxed/hashland/murmur3 v0.0.1/go.mod h1:KjXop02n4/ckmZSnY2+HKcLud/tcmvhST0bie/0lS48=
|
||||||
|
@ -295,6 +296,7 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||||
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
|
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
|
||||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||||
github.com/kshvakov/clickhouse v1.3.5/go.mod h1:DMzX7FxRymoNkVgizH0DWAL8Cur7wHLgx3MUnGwJqpE=
|
github.com/kshvakov/clickhouse v1.3.5/go.mod h1:DMzX7FxRymoNkVgizH0DWAL8Cur7wHLgx3MUnGwJqpE=
|
||||||
|
github.com/leodido/go-urn v1.1.0/go.mod h1:+cyI34gQWZcE1eQU7NVgKkkzdXDQHr1dBMtdAPozLkw=
|
||||||
github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
|
github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
|
||||||
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
|
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
|
||||||
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
|
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
|
||||||
|
@ -423,6 +425,8 @@ github.com/mattn/go-runewidth v0.0.4 h1:2BvfKmzob6Bmd4YsL0zygOqfdFnK7GR4QL06Do4/
|
||||||
github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
|
github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
|
||||||
github.com/mattn/go-sqlite3 v1.10.0 h1:jbhqpg7tQe4SupckyijYiy0mJJ/pRyHvXf7JdWK860o=
|
github.com/mattn/go-sqlite3 v1.10.0 h1:jbhqpg7tQe4SupckyijYiy0mJJ/pRyHvXf7JdWK860o=
|
||||||
github.com/mattn/go-sqlite3 v1.10.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
|
github.com/mattn/go-sqlite3 v1.10.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
|
||||||
|
github.com/mattn/go-zglob v0.0.0-20180803001819-2ea3427bfa53/go.mod h1:9fxibJccNxU2cnpIKLRRFA7zX7qhkJIQWBb449FYHOo=
|
||||||
|
github.com/mattn/go-zglob v0.0.1/go.mod h1:9fxibJccNxU2cnpIKLRRFA7zX7qhkJIQWBb449FYHOo=
|
||||||
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
|
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
|
||||||
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
|
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
|
||||||
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE=
|
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE=
|
||||||
|
@ -718,6 +722,8 @@ golang.org/x/net v0.0.0-20190424112056-4829fb13d2c6/go.mod h1:t9HGtf8HONx5eT2rtn
|
||||||
golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859 h1:R/3boaszxrf1GEUWTVDzSKVwLmSJpwZ1yqXm8j0v2QI=
|
golang.org/x/net v0.0.0-20190620200207-3b0461eec859 h1:R/3boaszxrf1GEUWTVDzSKVwLmSJpwZ1yqXm8j0v2QI=
|
||||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||||
|
golang.org/x/net v0.0.0-20190930134127-c5a3c61f89f3 h1:6KET3Sqa7fkVfD63QnAM81ZeYg5n4HwApOJkufONnHA=
|
||||||
|
golang.org/x/net v0.0.0-20190930134127-c5a3c61f89f3/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||||
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
|
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
|
||||||
golang.org/x/oauth2 v0.0.0-20181106182150-f42d05182288/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
|
golang.org/x/oauth2 v0.0.0-20181106182150-f42d05182288/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
|
||||||
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
|
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
|
||||||
|
@ -728,6 +734,8 @@ golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJ
|
||||||
golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58 h1:8gQV6CLnAEikrhgkHFbMAEhagSSnXWGV915qUMm9mrU=
|
golang.org/x/sync v0.0.0-20190423024810-112230192c58 h1:8gQV6CLnAEikrhgkHFbMAEhagSSnXWGV915qUMm9mrU=
|
||||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e h1:vcxGaoTs7kV8m5Np9uUNQin4BrLOthgV7252N8V+FwY=
|
||||||
|
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
|
@ -772,6 +780,8 @@ golang.org/x/tools v0.0.0-20191029041327-9cc4af7d6b2c/go.mod h1:b+2E5dAYhXwXZwtn
|
||||||
golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||||
golang.org/x/tools v0.0.0-20191109212701-97ad0ed33101 h1:LCmXVkvpQCDj724eX6irUTPCJP5GelFHxqGSWL2D1R0=
|
golang.org/x/tools v0.0.0-20191109212701-97ad0ed33101 h1:LCmXVkvpQCDj724eX6irUTPCJP5GelFHxqGSWL2D1R0=
|
||||||
golang.org/x/tools v0.0.0-20191109212701-97ad0ed33101/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
golang.org/x/tools v0.0.0-20191109212701-97ad0ed33101/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||||
|
golang.org/x/tools v0.0.0-20191213032237-7093a17b0467 h1:Jybbe55FT+YYZIJGWmJIA4ZGcglFuZOduakIW3+gHXY=
|
||||||
|
golang.org/x/tools v0.0.0-20191213032237-7093a17b0467/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
google.golang.org/api v0.3.1/go.mod h1:6wY9I6uQWHQ8EM57III9mq/AjF+i8G65rmVagqKMtkk=
|
google.golang.org/api v0.3.1/go.mod h1:6wY9I6uQWHQ8EM57III9mq/AjF+i8G65rmVagqKMtkk=
|
||||||
google.golang.org/api v0.3.2/go.mod h1:6wY9I6uQWHQ8EM57III9mq/AjF+i8G65rmVagqKMtkk=
|
google.golang.org/api v0.3.2/go.mod h1:6wY9I6uQWHQ8EM57III9mq/AjF+i8G65rmVagqKMtkk=
|
||||||
|
|
|
@ -12,6 +12,7 @@ import (
|
||||||
"github.com/status-im/status-go/api"
|
"github.com/status-im/status-go/api"
|
||||||
"github.com/status-im/status-go/eth-node/types"
|
"github.com/status-im/status-go/eth-node/types"
|
||||||
"github.com/status-im/status-go/exportlogs"
|
"github.com/status-im/status-go/exportlogs"
|
||||||
|
"github.com/status-im/status-go/extkeys"
|
||||||
"github.com/status-im/status-go/multiaccounts"
|
"github.com/status-im/status-go/multiaccounts"
|
||||||
"github.com/status-im/status-go/multiaccounts/accounts"
|
"github.com/status-im/status-go/multiaccounts/accounts"
|
||||||
"github.com/status-im/status-go/params"
|
"github.com/status-im/status-go/params"
|
||||||
|
@ -705,3 +706,9 @@ func Identicon(pk string) string {
|
||||||
identicon, _ := protocol.Identicon(pk)
|
identicon, _ := protocol.Identicon(pk)
|
||||||
return identicon
|
return identicon
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ValidateMnemonic(mnemonic string) string {
|
||||||
|
m := extkeys.NewMnemonic()
|
||||||
|
err := m.ValidateMnemonic(mnemonic, extkeys.Language(0))
|
||||||
|
return makeJSONResponse(err)
|
||||||
|
}
|
||||||
|
|
|
@ -32,8 +32,8 @@ matrix:
|
||||||
- os: osx
|
- os: osx
|
||||||
go: 1.12.x
|
go: 1.12.x
|
||||||
- os: linux
|
- os: linux
|
||||||
dist: bionic
|
dist: xenial
|
||||||
go: 1.13.x
|
go: 1.12.x
|
||||||
services:
|
services:
|
||||||
- docker
|
- docker
|
||||||
env:
|
env:
|
||||||
|
|
|
@ -22,8 +22,8 @@ environment:
|
||||||
|
|
||||||
install:
|
install:
|
||||||
- rmdir C:\go /s /q
|
- rmdir C:\go /s /q
|
||||||
- appveyor DownloadFile https://storage.googleapis.com/golang/go1.13.4.windows-%GOARCH%.zip
|
- appveyor DownloadFile https://storage.googleapis.com/golang/go1.12.9.windows-%GOARCH%.zip
|
||||||
- 7z x go1.13.4.windows-%GOARCH%.zip -y -oC:\ > NUL
|
- 7z x go1.12.9.windows-%GOARCH%.zip -y -oC:\ > NUL
|
||||||
- go version
|
- go version
|
||||||
- gcc --version
|
- gcc --version
|
||||||
|
|
||||||
|
|
|
@ -74,8 +74,6 @@ extern "C" {
|
||||||
#pragma warning(disable:4996)
|
#pragma warning(disable:4996)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#pragma GCC diagnostic ignored "-Wstringop-overflow"
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
@ -430,7 +428,7 @@ struct hid_device_info HID_API_EXPORT * HID_API_CALL hid_enumerate(unsigned shor
|
||||||
if (str) {
|
if (str) {
|
||||||
len = strlen(str);
|
len = strlen(str);
|
||||||
cur_dev->path = (char*) calloc(len+1, sizeof(char));
|
cur_dev->path = (char*) calloc(len+1, sizeof(char));
|
||||||
strncpy(cur_dev->path, str, len+1);
|
strncpy(cur_dev->path, str, sizeof(cur_dev->path));
|
||||||
cur_dev->path[len] = '\0';
|
cur_dev->path[len] = '\0';
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|
|
@ -24,6 +24,7 @@ import (
|
||||||
// https://github.com/trezor/trezor-crypto/blob/master/bip39.c
|
// https://github.com/trezor/trezor-crypto/blob/master/bip39.c
|
||||||
// https://github.com/trezor/python-mnemonic/blob/master/mnemonic/mnemonic.py
|
// https://github.com/trezor/python-mnemonic/blob/master/mnemonic/mnemonic.py
|
||||||
// https://github.com/bitpay/bitcore-mnemonic/blob/master/lib/mnemonic.js (used in eth-lightwallet.js)
|
// https://github.com/bitpay/bitcore-mnemonic/blob/master/lib/mnemonic.js (used in eth-lightwallet.js)
|
||||||
|
// https://github.com/tyler-smith/go-bip39/blob/master/bip39.go
|
||||||
|
|
||||||
const defaultSalt = "mnemonic"
|
const defaultSalt = "mnemonic"
|
||||||
|
|
||||||
|
@ -73,6 +74,21 @@ var (
|
||||||
rightShift11BitsDivider = big.NewInt(2048)
|
rightShift11BitsDivider = big.NewInt(2048)
|
||||||
bigOne = big.NewInt(1)
|
bigOne = big.NewInt(1)
|
||||||
bigTwo = big.NewInt(2)
|
bigTwo = big.NewInt(2)
|
||||||
|
|
||||||
|
wordLengthChecksumMasksMapping = map[int]*big.Int{
|
||||||
|
12: big.NewInt(15),
|
||||||
|
15: big.NewInt(31),
|
||||||
|
18: big.NewInt(63),
|
||||||
|
21: big.NewInt(127),
|
||||||
|
24: big.NewInt(255),
|
||||||
|
}
|
||||||
|
|
||||||
|
wordLengthChecksumShiftMapping = map[int]*big.Int{
|
||||||
|
12: big.NewInt(16),
|
||||||
|
15: big.NewInt(8),
|
||||||
|
18: big.NewInt(4),
|
||||||
|
21: big.NewInt(2),
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
// Language is language identifier
|
// Language is language identifier
|
||||||
|
@ -201,11 +217,11 @@ func (m *Mnemonic) MnemonicPhrase(strength EntropyStrength, language Language) (
|
||||||
return strings.Join(words, wordSeperator), nil
|
return strings.Join(words, wordSeperator), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ValidMnemonic validates mnemonic string
|
// ValidateMnemonic validates that all words from a mnemonic string are in wordlist and that checksum is valid
|
||||||
func (m *Mnemonic) ValidMnemonic(mnemonic string, language Language) bool {
|
func (m *Mnemonic) ValidateMnemonic(mnemonic string, language Language) error {
|
||||||
wordList, err := m.WordList(language)
|
wordList, err := m.WordList(language)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false
|
return errors.New("invalid language specified")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a list of all the words in the mnemonic sentence
|
// Create a list of all the words in the mnemonic sentence
|
||||||
|
@ -216,17 +232,61 @@ func (m *Mnemonic) ValidMnemonic(mnemonic string, language Language) bool {
|
||||||
|
|
||||||
// The number of words should be 12, 15, 18, 21 or 24
|
// The number of words should be 12, 15, 18, 21 or 24
|
||||||
if numOfWords%3 != 0 || numOfWords < 12 || numOfWords > 24 {
|
if numOfWords%3 != 0 || numOfWords < 12 || numOfWords > 24 {
|
||||||
return false
|
return errors.New("mnemonic contains an invalid number of words")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create reverse lookup map for dictionary
|
||||||
|
wordMap := map[string]int{}
|
||||||
|
for i, v := range wordList {
|
||||||
|
wordMap[v] = i
|
||||||
|
}
|
||||||
|
|
||||||
|
b := big.NewInt(0)
|
||||||
|
|
||||||
// Check if all words belong in the wordlist
|
// Check if all words belong in the wordlist
|
||||||
for i := 0; i < numOfWords; i++ {
|
for i := 0; i < numOfWords; i++ {
|
||||||
if !contains(wordList, words[i]) {
|
|
||||||
return false
|
wordIndex, ok := wordMap[words[i]]
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("word %s not found in the dictionary", words[i])
|
||||||
}
|
}
|
||||||
|
var wordBytes [2]byte
|
||||||
|
binary.BigEndian.PutUint16(wordBytes[:], uint16(wordIndex))
|
||||||
|
b = b.Mul(b, rightShift11BitsDivider)
|
||||||
|
b = b.Or(b, big.NewInt(0).SetBytes(wordBytes[:]))
|
||||||
}
|
}
|
||||||
|
|
||||||
return true
|
checksum := big.NewInt(0)
|
||||||
|
checksumMask := wordLengthChecksumMasksMapping[numOfWords]
|
||||||
|
checksum = checksum.And(b, checksumMask)
|
||||||
|
|
||||||
|
b.Div(b, big.NewInt(0).Add(checksumMask, bigOne))
|
||||||
|
|
||||||
|
// Calculate entropy from mnemonic
|
||||||
|
entropy := b.Bytes()
|
||||||
|
entropy = padByteSlice(entropy, len(words)/3*4)
|
||||||
|
|
||||||
|
// Calculate checksum from entropy derived above
|
||||||
|
hasher := sha256.New()
|
||||||
|
hasher.Write(entropy)
|
||||||
|
computedChecksumBytes := hasher.Sum(nil)
|
||||||
|
computedChecksum := big.NewInt(int64(computedChecksumBytes[0]))
|
||||||
|
|
||||||
|
if l := len(words); l != 24 {
|
||||||
|
checksumShift := wordLengthChecksumShiftMapping[l]
|
||||||
|
computedChecksum.Div(computedChecksum, checksumShift)
|
||||||
|
}
|
||||||
|
|
||||||
|
if checksum.Cmp(computedChecksum) != 0 {
|
||||||
|
return errors.New("checksum for mnemonic seed is invalid")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ValidMnemonic validates mnemonic string
|
||||||
|
func (m *Mnemonic) ValidMnemonic(mnemonic string, language Language) bool {
|
||||||
|
return m.ValidateMnemonic(mnemonic, language) == nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// WordList returns list of words for a given language
|
// WordList returns list of words for a given language
|
||||||
|
|
|
@ -55,6 +55,7 @@ const (
|
||||||
bloomFilterExCode = 3 // bloom filter exchange
|
bloomFilterExCode = 3 // bloom filter exchange
|
||||||
batchAcknowledgedCode = 11 // confirmation that batch of envelopes was received
|
batchAcknowledgedCode = 11 // confirmation that batch of envelopes was received
|
||||||
messageResponseCode = 12 // includes confirmation for delivery and information about errors
|
messageResponseCode = 12 // includes confirmation for delivery and information about errors
|
||||||
|
rateLimitingCode = 20 // includes peer's rate limiting settings
|
||||||
p2pSyncRequestCode = 123 // used to sync envelopes between two mail servers
|
p2pSyncRequestCode = 123 // used to sync envelopes between two mail servers
|
||||||
p2pSyncResponseCode = 124 // used to sync envelopes between two mail servers
|
p2pSyncResponseCode = 124 // used to sync envelopes between two mail servers
|
||||||
p2pRequestCompleteCode = 125 // peer-to-peer message, used by Dapp protocol
|
p2pRequestCompleteCode = 125 // peer-to-peer message, used by Dapp protocol
|
||||||
|
@ -259,3 +260,15 @@ func ErrorToEnvelopeError(hash common.Hash, err error) EnvelopeError {
|
||||||
Description: err.Error(),
|
Description: err.Error(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RateLimits contains information about rate limit settings.
|
||||||
|
// It is exchanged using rateLimitingCode packet or in the handshake.
|
||||||
|
type RateLimits struct {
|
||||||
|
IPLimits uint64 // messages per second from a single IP (default 0, no limits)
|
||||||
|
PeerIDLimits uint64 // messages per second from a single peer ID (default 0, no limits)
|
||||||
|
TopicLimits uint64 // messages per second from a single topic (default 0, no limits)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r RateLimits) IsZero() bool {
|
||||||
|
return r == (RateLimits{})
|
||||||
|
}
|
||||||
|
|
|
@ -89,6 +89,7 @@ github.com/rs/xhandler v0.0.0-20160618193221-ed27b6fd6521 h1:3hxavr+IHMsQBrYUPQM
|
||||||
github.com/rs/xhandler v0.0.0-20160618193221-ed27b6fd6521/go.mod h1:RvLn4FgxWubrpZHtQLnOf6EwhN2hEMusxZOhcW9H3UQ=
|
github.com/rs/xhandler v0.0.0-20160618193221-ed27b6fd6521/go.mod h1:RvLn4FgxWubrpZHtQLnOf6EwhN2hEMusxZOhcW9H3UQ=
|
||||||
github.com/status-im/go-ethereum v1.9.5-status.6 h1:ytuTO1yBIAuTVRtRQoc2mrdyngtP+XOQ9IHIibbz7/I=
|
github.com/status-im/go-ethereum v1.9.5-status.6 h1:ytuTO1yBIAuTVRtRQoc2mrdyngtP+XOQ9IHIibbz7/I=
|
||||||
github.com/status-im/go-ethereum v1.9.5-status.6/go.mod h1:08JvQWE+IOnAFSe4UD4ACLNe2fDd9XmWMCq5Yzy9mk0=
|
github.com/status-im/go-ethereum v1.9.5-status.6/go.mod h1:08JvQWE+IOnAFSe4UD4ACLNe2fDd9XmWMCq5Yzy9mk0=
|
||||||
|
github.com/status-im/go-ethereum v1.9.5-status.7 h1:DKH1GiF52LwaZaw6YDBliFEgm/JDsbIT+hn7ph6X94Q=
|
||||||
github.com/status-im/go-ethereum v1.9.5-status.7/go.mod h1:YyH5DKB6+z+Vaya7eIm67pnuPZ1oiUMbbsZW41ktN0g=
|
github.com/status-im/go-ethereum v1.9.5-status.7/go.mod h1:YyH5DKB6+z+Vaya7eIm67pnuPZ1oiUMbbsZW41ktN0g=
|
||||||
github.com/status-im/status-go/extkeys v1.0.0/go.mod h1:GdqJbrcpkNm5ZsSCpp+PdMxnXx+OcRBdm3PI0rs1FpU=
|
github.com/status-im/status-go/extkeys v1.0.0/go.mod h1:GdqJbrcpkNm5ZsSCpp+PdMxnXx+OcRBdm3PI0rs1FpU=
|
||||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
|
@ -108,6 +109,7 @@ golang.org/x/crypto v0.0.0-20170930174604-9419663f5a44/go.mod h1:6SG95UA2DQfeDnf
|
||||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
golang.org/x/crypto v0.0.0-20191029031824-8986dd9e96cf h1:fnPsqIDRbCSgumaMCRpoIoF2s4qxv0xSSS0BVZUE/ss=
|
golang.org/x/crypto v0.0.0-20191029031824-8986dd9e96cf h1:fnPsqIDRbCSgumaMCRpoIoF2s4qxv0xSSS0BVZUE/ss=
|
||||||
golang.org/x/crypto v0.0.0-20191029031824-8986dd9e96cf/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
golang.org/x/crypto v0.0.0-20191029031824-8986dd9e96cf/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||||
|
golang.org/x/crypto v0.0.0-20191119213627-4f8c1d86b1ba h1:9bFeDpN3gTqNanMVqNcoR/pJQuP5uroC3t1D7eXozTE=
|
||||||
golang.org/x/crypto v0.0.0-20191119213627-4f8c1d86b1ba/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
golang.org/x/crypto v0.0.0-20191119213627-4f8c1d86b1ba/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||||
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 h1:0GoQqolDA55aaLxZyTzK/Y2ePZzZTUrRacwib7cNsYQ=
|
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 h1:0GoQqolDA55aaLxZyTzK/Y2ePZzZTUrRacwib7cNsYQ=
|
||||||
|
|
|
@ -43,6 +43,8 @@ type Peer struct {
|
||||||
bloomFilter []byte
|
bloomFilter []byte
|
||||||
fullNode bool
|
fullNode bool
|
||||||
confirmationsEnabled bool
|
confirmationsEnabled bool
|
||||||
|
rateLimitsMu sync.Mutex
|
||||||
|
rateLimits RateLimits
|
||||||
|
|
||||||
known mapset.Set // Messages already known by the peer to avoid wasting bandwidth
|
known mapset.Set // Messages already known by the peer to avoid wasting bandwidth
|
||||||
|
|
||||||
|
@ -89,8 +91,9 @@ func (peer *Peer) handshake() error {
|
||||||
powConverted := math.Float64bits(pow)
|
powConverted := math.Float64bits(pow)
|
||||||
bloom := peer.host.BloomFilter()
|
bloom := peer.host.BloomFilter()
|
||||||
confirmationsEnabled := !peer.host.disableConfirmations
|
confirmationsEnabled := !peer.host.disableConfirmations
|
||||||
|
rateLimits := peer.host.RateLimits()
|
||||||
|
|
||||||
errc <- p2p.SendItems(peer.ws, statusCode, ProtocolVersion, powConverted, bloom, isLightNode, confirmationsEnabled)
|
errc <- p2p.SendItems(peer.ws, statusCode, ProtocolVersion, powConverted, bloom, isLightNode, confirmationsEnabled, rateLimits)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// Fetch the remote status packet and verify protocol match
|
// Fetch the remote status packet and verify protocol match
|
||||||
|
@ -145,6 +148,13 @@ func (peer *Peer) handshake() error {
|
||||||
peer.confirmationsEnabled = confirmationsEnabled
|
peer.confirmationsEnabled = confirmationsEnabled
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var rateLimits RateLimits
|
||||||
|
if err := s.Decode(&rateLimits); err != nil {
|
||||||
|
log.Info("rate limiting disabled", "err", err)
|
||||||
|
} else {
|
||||||
|
peer.setRateLimits(rateLimits)
|
||||||
|
}
|
||||||
|
|
||||||
if err := <-errc; err != nil {
|
if err := <-errc; err != nil {
|
||||||
return fmt.Errorf("peer [%x] failed to send status packet: %v", peer.ID(), err)
|
return fmt.Errorf("peer [%x] failed to send status packet: %v", peer.ID(), err)
|
||||||
}
|
}
|
||||||
|
@ -270,6 +280,12 @@ func (peer *Peer) setBloomFilter(bloom []byte) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (peer *Peer) setRateLimits(r RateLimits) {
|
||||||
|
peer.rateLimitsMu.Lock()
|
||||||
|
peer.rateLimits = r
|
||||||
|
peer.rateLimitsMu.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
func MakeFullNodeBloom() []byte {
|
func MakeFullNodeBloom() []byte {
|
||||||
bloom := make([]byte, BloomFilterSize)
|
bloom := make([]byte, BloomFilterSize)
|
||||||
for i := 0; i < BloomFilterSize; i++ {
|
for i := 0; i < BloomFilterSize; i++ {
|
||||||
|
|
|
@ -14,14 +14,14 @@ import (
|
||||||
type runLoop func(p *Peer, rw p2p.MsgReadWriter) error
|
type runLoop func(p *Peer, rw p2p.MsgReadWriter) error
|
||||||
|
|
||||||
type RateLimiterHandler interface {
|
type RateLimiterHandler interface {
|
||||||
ExceedPeerLimit()
|
ExceedPeerLimit() error
|
||||||
ExceedIPLimit()
|
ExceedIPLimit() error
|
||||||
}
|
}
|
||||||
|
|
||||||
type MetricsRateLimiterHandler struct{}
|
type MetricsRateLimiterHandler struct{}
|
||||||
|
|
||||||
func (MetricsRateLimiterHandler) ExceedPeerLimit() { rateLimiterPeerExceeded.Inc(1) }
|
func (MetricsRateLimiterHandler) ExceedPeerLimit() error { rateLimiterPeerExceeded.Inc(1); return nil }
|
||||||
func (MetricsRateLimiterHandler) ExceedIPLimit() { rateLimiterIPExceeded.Inc(1) }
|
func (MetricsRateLimiterHandler) ExceedIPLimit() error { rateLimiterIPExceeded.Inc(1); return nil }
|
||||||
|
|
||||||
type PeerRateLimiterConfig struct {
|
type PeerRateLimiterConfig struct {
|
||||||
LimitPerSecIP int64
|
LimitPerSecIP int64
|
||||||
|
@ -89,7 +89,10 @@ func (r *PeerRateLimiter) decorate(p *Peer, rw p2p.MsgReadWriter, runLoop runLoo
|
||||||
ip = p.peer.Node().IP().String()
|
ip = p.peer.Node().IP().String()
|
||||||
}
|
}
|
||||||
if halted := r.throttleIP(ip); halted {
|
if halted := r.throttleIP(ip); halted {
|
||||||
r.handler.ExceedIPLimit()
|
if err := r.handler.ExceedIPLimit(); err != nil {
|
||||||
|
errC <- fmt.Errorf("exceeded rate limit by IP: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var peerID []byte
|
var peerID []byte
|
||||||
|
@ -97,7 +100,10 @@ func (r *PeerRateLimiter) decorate(p *Peer, rw p2p.MsgReadWriter, runLoop runLoo
|
||||||
peerID = p.ID()
|
peerID = p.ID()
|
||||||
}
|
}
|
||||||
if halted := r.throttlePeer(peerID); halted {
|
if halted := r.throttlePeer(peerID); halted {
|
||||||
r.handler.ExceedPeerLimit()
|
if err := r.handler.ExceedPeerLimit(); err != nil {
|
||||||
|
errC <- fmt.Errorf("exceeded rate limit by peer: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := in.WriteMsg(packet); err != nil {
|
if err := in.WriteMsg(packet); err != nil {
|
||||||
|
|
|
@ -337,6 +337,8 @@ func (whisper *Whisper) SetLightClientMode(v bool) {
|
||||||
whisper.settings.Store(lightClientModeIdx, v)
|
whisper.settings.Store(lightClientModeIdx, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetRateLimiter sets an active rate limiter.
|
||||||
|
// It must be run before Whisper is started.
|
||||||
func (whisper *Whisper) SetRateLimiter(r *PeerRateLimiter) {
|
func (whisper *Whisper) SetRateLimiter(r *PeerRateLimiter) {
|
||||||
whisper.rateLimiter = r
|
whisper.rateLimiter = r
|
||||||
}
|
}
|
||||||
|
@ -361,6 +363,17 @@ func (whisper *Whisper) LightClientModeConnectionRestricted() bool {
|
||||||
return v && ok
|
return v && ok
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RateLimiting returns RateLimits information.
|
||||||
|
func (whisper *Whisper) RateLimits() RateLimits {
|
||||||
|
if whisper.rateLimiter == nil {
|
||||||
|
return RateLimits{}
|
||||||
|
}
|
||||||
|
return RateLimits{
|
||||||
|
IPLimits: uint64(whisper.rateLimiter.limitPerSecIP),
|
||||||
|
PeerIDLimits: uint64(whisper.rateLimiter.limitPerSecPeerID),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (whisper *Whisper) notifyPeersAboutPowRequirementChange(pow float64) {
|
func (whisper *Whisper) notifyPeersAboutPowRequirementChange(pow float64) {
|
||||||
arr := whisper.getPeers()
|
arr := whisper.getPeers()
|
||||||
for _, p := range arr {
|
for _, p := range arr {
|
||||||
|
@ -1027,6 +1040,13 @@ func (whisper *Whisper) runMessageLoop(p *Peer, rw p2p.MsgReadWriter) error {
|
||||||
return errors.New("invalid bloom filter exchange message")
|
return errors.New("invalid bloom filter exchange message")
|
||||||
}
|
}
|
||||||
p.setBloomFilter(bloom)
|
p.setBloomFilter(bloom)
|
||||||
|
case rateLimitingCode:
|
||||||
|
var rateLimits RateLimits
|
||||||
|
if err := packet.Decode(&rateLimits); err != nil {
|
||||||
|
log.Warn("invalid rate limits information", "peer", p.peer.ID(), "err", err)
|
||||||
|
return errors.New("invalid rate limits exchange message")
|
||||||
|
}
|
||||||
|
p.setRateLimits(rateLimits)
|
||||||
case p2pMessageCode:
|
case p2pMessageCode:
|
||||||
// peer-to-peer message, sent directly to peer bypassing PoW checks, etc.
|
// peer-to-peer message, sent directly to peer bypassing PoW checks, etc.
|
||||||
// this message is not supposed to be forwarded to other peers, and
|
// this message is not supposed to be forwarded to other peers, and
|
||||||
|
|
|
@ -1,159 +0,0 @@
|
||||||
// Copyright 2010 The Go Authors. All rights reserved.
|
|
||||||
// Use of this source code is governed by a BSD-style
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package blowfish
|
|
||||||
|
|
||||||
// getNextWord returns the next big-endian uint32 value from the byte slice
|
|
||||||
// at the given position in a circular manner, updating the position.
|
|
||||||
func getNextWord(b []byte, pos *int) uint32 {
|
|
||||||
var w uint32
|
|
||||||
j := *pos
|
|
||||||
for i := 0; i < 4; i++ {
|
|
||||||
w = w<<8 | uint32(b[j])
|
|
||||||
j++
|
|
||||||
if j >= len(b) {
|
|
||||||
j = 0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*pos = j
|
|
||||||
return w
|
|
||||||
}
|
|
||||||
|
|
||||||
// ExpandKey performs a key expansion on the given *Cipher. Specifically, it
|
|
||||||
// performs the Blowfish algorithm's key schedule which sets up the *Cipher's
|
|
||||||
// pi and substitution tables for calls to Encrypt. This is used, primarily,
|
|
||||||
// by the bcrypt package to reuse the Blowfish key schedule during its
|
|
||||||
// set up. It's unlikely that you need to use this directly.
|
|
||||||
func ExpandKey(key []byte, c *Cipher) {
|
|
||||||
j := 0
|
|
||||||
for i := 0; i < 18; i++ {
|
|
||||||
// Using inlined getNextWord for performance.
|
|
||||||
var d uint32
|
|
||||||
for k := 0; k < 4; k++ {
|
|
||||||
d = d<<8 | uint32(key[j])
|
|
||||||
j++
|
|
||||||
if j >= len(key) {
|
|
||||||
j = 0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
c.p[i] ^= d
|
|
||||||
}
|
|
||||||
|
|
||||||
var l, r uint32
|
|
||||||
for i := 0; i < 18; i += 2 {
|
|
||||||
l, r = encryptBlock(l, r, c)
|
|
||||||
c.p[i], c.p[i+1] = l, r
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := 0; i < 256; i += 2 {
|
|
||||||
l, r = encryptBlock(l, r, c)
|
|
||||||
c.s0[i], c.s0[i+1] = l, r
|
|
||||||
}
|
|
||||||
for i := 0; i < 256; i += 2 {
|
|
||||||
l, r = encryptBlock(l, r, c)
|
|
||||||
c.s1[i], c.s1[i+1] = l, r
|
|
||||||
}
|
|
||||||
for i := 0; i < 256; i += 2 {
|
|
||||||
l, r = encryptBlock(l, r, c)
|
|
||||||
c.s2[i], c.s2[i+1] = l, r
|
|
||||||
}
|
|
||||||
for i := 0; i < 256; i += 2 {
|
|
||||||
l, r = encryptBlock(l, r, c)
|
|
||||||
c.s3[i], c.s3[i+1] = l, r
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// This is similar to ExpandKey, but folds the salt during the key
|
|
||||||
// schedule. While ExpandKey is essentially expandKeyWithSalt with an all-zero
|
|
||||||
// salt passed in, reusing ExpandKey turns out to be a place of inefficiency
|
|
||||||
// and specializing it here is useful.
|
|
||||||
func expandKeyWithSalt(key []byte, salt []byte, c *Cipher) {
|
|
||||||
j := 0
|
|
||||||
for i := 0; i < 18; i++ {
|
|
||||||
c.p[i] ^= getNextWord(key, &j)
|
|
||||||
}
|
|
||||||
|
|
||||||
j = 0
|
|
||||||
var l, r uint32
|
|
||||||
for i := 0; i < 18; i += 2 {
|
|
||||||
l ^= getNextWord(salt, &j)
|
|
||||||
r ^= getNextWord(salt, &j)
|
|
||||||
l, r = encryptBlock(l, r, c)
|
|
||||||
c.p[i], c.p[i+1] = l, r
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := 0; i < 256; i += 2 {
|
|
||||||
l ^= getNextWord(salt, &j)
|
|
||||||
r ^= getNextWord(salt, &j)
|
|
||||||
l, r = encryptBlock(l, r, c)
|
|
||||||
c.s0[i], c.s0[i+1] = l, r
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := 0; i < 256; i += 2 {
|
|
||||||
l ^= getNextWord(salt, &j)
|
|
||||||
r ^= getNextWord(salt, &j)
|
|
||||||
l, r = encryptBlock(l, r, c)
|
|
||||||
c.s1[i], c.s1[i+1] = l, r
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := 0; i < 256; i += 2 {
|
|
||||||
l ^= getNextWord(salt, &j)
|
|
||||||
r ^= getNextWord(salt, &j)
|
|
||||||
l, r = encryptBlock(l, r, c)
|
|
||||||
c.s2[i], c.s2[i+1] = l, r
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := 0; i < 256; i += 2 {
|
|
||||||
l ^= getNextWord(salt, &j)
|
|
||||||
r ^= getNextWord(salt, &j)
|
|
||||||
l, r = encryptBlock(l, r, c)
|
|
||||||
c.s3[i], c.s3[i+1] = l, r
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func encryptBlock(l, r uint32, c *Cipher) (uint32, uint32) {
|
|
||||||
xl, xr := l, r
|
|
||||||
xl ^= c.p[0]
|
|
||||||
xr ^= ((c.s0[byte(xl>>24)] + c.s1[byte(xl>>16)]) ^ c.s2[byte(xl>>8)]) + c.s3[byte(xl)] ^ c.p[1]
|
|
||||||
xl ^= ((c.s0[byte(xr>>24)] + c.s1[byte(xr>>16)]) ^ c.s2[byte(xr>>8)]) + c.s3[byte(xr)] ^ c.p[2]
|
|
||||||
xr ^= ((c.s0[byte(xl>>24)] + c.s1[byte(xl>>16)]) ^ c.s2[byte(xl>>8)]) + c.s3[byte(xl)] ^ c.p[3]
|
|
||||||
xl ^= ((c.s0[byte(xr>>24)] + c.s1[byte(xr>>16)]) ^ c.s2[byte(xr>>8)]) + c.s3[byte(xr)] ^ c.p[4]
|
|
||||||
xr ^= ((c.s0[byte(xl>>24)] + c.s1[byte(xl>>16)]) ^ c.s2[byte(xl>>8)]) + c.s3[byte(xl)] ^ c.p[5]
|
|
||||||
xl ^= ((c.s0[byte(xr>>24)] + c.s1[byte(xr>>16)]) ^ c.s2[byte(xr>>8)]) + c.s3[byte(xr)] ^ c.p[6]
|
|
||||||
xr ^= ((c.s0[byte(xl>>24)] + c.s1[byte(xl>>16)]) ^ c.s2[byte(xl>>8)]) + c.s3[byte(xl)] ^ c.p[7]
|
|
||||||
xl ^= ((c.s0[byte(xr>>24)] + c.s1[byte(xr>>16)]) ^ c.s2[byte(xr>>8)]) + c.s3[byte(xr)] ^ c.p[8]
|
|
||||||
xr ^= ((c.s0[byte(xl>>24)] + c.s1[byte(xl>>16)]) ^ c.s2[byte(xl>>8)]) + c.s3[byte(xl)] ^ c.p[9]
|
|
||||||
xl ^= ((c.s0[byte(xr>>24)] + c.s1[byte(xr>>16)]) ^ c.s2[byte(xr>>8)]) + c.s3[byte(xr)] ^ c.p[10]
|
|
||||||
xr ^= ((c.s0[byte(xl>>24)] + c.s1[byte(xl>>16)]) ^ c.s2[byte(xl>>8)]) + c.s3[byte(xl)] ^ c.p[11]
|
|
||||||
xl ^= ((c.s0[byte(xr>>24)] + c.s1[byte(xr>>16)]) ^ c.s2[byte(xr>>8)]) + c.s3[byte(xr)] ^ c.p[12]
|
|
||||||
xr ^= ((c.s0[byte(xl>>24)] + c.s1[byte(xl>>16)]) ^ c.s2[byte(xl>>8)]) + c.s3[byte(xl)] ^ c.p[13]
|
|
||||||
xl ^= ((c.s0[byte(xr>>24)] + c.s1[byte(xr>>16)]) ^ c.s2[byte(xr>>8)]) + c.s3[byte(xr)] ^ c.p[14]
|
|
||||||
xr ^= ((c.s0[byte(xl>>24)] + c.s1[byte(xl>>16)]) ^ c.s2[byte(xl>>8)]) + c.s3[byte(xl)] ^ c.p[15]
|
|
||||||
xl ^= ((c.s0[byte(xr>>24)] + c.s1[byte(xr>>16)]) ^ c.s2[byte(xr>>8)]) + c.s3[byte(xr)] ^ c.p[16]
|
|
||||||
xr ^= c.p[17]
|
|
||||||
return xr, xl
|
|
||||||
}
|
|
||||||
|
|
||||||
func decryptBlock(l, r uint32, c *Cipher) (uint32, uint32) {
|
|
||||||
xl, xr := l, r
|
|
||||||
xl ^= c.p[17]
|
|
||||||
xr ^= ((c.s0[byte(xl>>24)] + c.s1[byte(xl>>16)]) ^ c.s2[byte(xl>>8)]) + c.s3[byte(xl)] ^ c.p[16]
|
|
||||||
xl ^= ((c.s0[byte(xr>>24)] + c.s1[byte(xr>>16)]) ^ c.s2[byte(xr>>8)]) + c.s3[byte(xr)] ^ c.p[15]
|
|
||||||
xr ^= ((c.s0[byte(xl>>24)] + c.s1[byte(xl>>16)]) ^ c.s2[byte(xl>>8)]) + c.s3[byte(xl)] ^ c.p[14]
|
|
||||||
xl ^= ((c.s0[byte(xr>>24)] + c.s1[byte(xr>>16)]) ^ c.s2[byte(xr>>8)]) + c.s3[byte(xr)] ^ c.p[13]
|
|
||||||
xr ^= ((c.s0[byte(xl>>24)] + c.s1[byte(xl>>16)]) ^ c.s2[byte(xl>>8)]) + c.s3[byte(xl)] ^ c.p[12]
|
|
||||||
xl ^= ((c.s0[byte(xr>>24)] + c.s1[byte(xr>>16)]) ^ c.s2[byte(xr>>8)]) + c.s3[byte(xr)] ^ c.p[11]
|
|
||||||
xr ^= ((c.s0[byte(xl>>24)] + c.s1[byte(xl>>16)]) ^ c.s2[byte(xl>>8)]) + c.s3[byte(xl)] ^ c.p[10]
|
|
||||||
xl ^= ((c.s0[byte(xr>>24)] + c.s1[byte(xr>>16)]) ^ c.s2[byte(xr>>8)]) + c.s3[byte(xr)] ^ c.p[9]
|
|
||||||
xr ^= ((c.s0[byte(xl>>24)] + c.s1[byte(xl>>16)]) ^ c.s2[byte(xl>>8)]) + c.s3[byte(xl)] ^ c.p[8]
|
|
||||||
xl ^= ((c.s0[byte(xr>>24)] + c.s1[byte(xr>>16)]) ^ c.s2[byte(xr>>8)]) + c.s3[byte(xr)] ^ c.p[7]
|
|
||||||
xr ^= ((c.s0[byte(xl>>24)] + c.s1[byte(xl>>16)]) ^ c.s2[byte(xl>>8)]) + c.s3[byte(xl)] ^ c.p[6]
|
|
||||||
xl ^= ((c.s0[byte(xr>>24)] + c.s1[byte(xr>>16)]) ^ c.s2[byte(xr>>8)]) + c.s3[byte(xr)] ^ c.p[5]
|
|
||||||
xr ^= ((c.s0[byte(xl>>24)] + c.s1[byte(xl>>16)]) ^ c.s2[byte(xl>>8)]) + c.s3[byte(xl)] ^ c.p[4]
|
|
||||||
xl ^= ((c.s0[byte(xr>>24)] + c.s1[byte(xr>>16)]) ^ c.s2[byte(xr>>8)]) + c.s3[byte(xr)] ^ c.p[3]
|
|
||||||
xr ^= ((c.s0[byte(xl>>24)] + c.s1[byte(xl>>16)]) ^ c.s2[byte(xl>>8)]) + c.s3[byte(xl)] ^ c.p[2]
|
|
||||||
xl ^= ((c.s0[byte(xr>>24)] + c.s1[byte(xr>>16)]) ^ c.s2[byte(xr>>8)]) + c.s3[byte(xr)] ^ c.p[1]
|
|
||||||
xr ^= c.p[0]
|
|
||||||
return xr, xl
|
|
||||||
}
|
|
|
@ -1,99 +0,0 @@
|
||||||
// Copyright 2010 The Go Authors. All rights reserved.
|
|
||||||
// Use of this source code is governed by a BSD-style
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
// Package blowfish implements Bruce Schneier's Blowfish encryption algorithm.
|
|
||||||
//
|
|
||||||
// Blowfish is a legacy cipher and its short block size makes it vulnerable to
|
|
||||||
// birthday bound attacks (see https://sweet32.info). It should only be used
|
|
||||||
// where compatibility with legacy systems, not security, is the goal.
|
|
||||||
//
|
|
||||||
// Deprecated: any new system should use AES (from crypto/aes, if necessary in
|
|
||||||
// an AEAD mode like crypto/cipher.NewGCM) or XChaCha20-Poly1305 (from
|
|
||||||
// golang.org/x/crypto/chacha20poly1305).
|
|
||||||
package blowfish // import "golang.org/x/crypto/blowfish"
|
|
||||||
|
|
||||||
// The code is a port of Bruce Schneier's C implementation.
|
|
||||||
// See https://www.schneier.com/blowfish.html.
|
|
||||||
|
|
||||||
import "strconv"
|
|
||||||
|
|
||||||
// The Blowfish block size in bytes.
|
|
||||||
const BlockSize = 8
|
|
||||||
|
|
||||||
// A Cipher is an instance of Blowfish encryption using a particular key.
|
|
||||||
type Cipher struct {
|
|
||||||
p [18]uint32
|
|
||||||
s0, s1, s2, s3 [256]uint32
|
|
||||||
}
|
|
||||||
|
|
||||||
type KeySizeError int
|
|
||||||
|
|
||||||
func (k KeySizeError) Error() string {
|
|
||||||
return "crypto/blowfish: invalid key size " + strconv.Itoa(int(k))
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewCipher creates and returns a Cipher.
|
|
||||||
// The key argument should be the Blowfish key, from 1 to 56 bytes.
|
|
||||||
func NewCipher(key []byte) (*Cipher, error) {
|
|
||||||
var result Cipher
|
|
||||||
if k := len(key); k < 1 || k > 56 {
|
|
||||||
return nil, KeySizeError(k)
|
|
||||||
}
|
|
||||||
initCipher(&result)
|
|
||||||
ExpandKey(key, &result)
|
|
||||||
return &result, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewSaltedCipher creates a returns a Cipher that folds a salt into its key
|
|
||||||
// schedule. For most purposes, NewCipher, instead of NewSaltedCipher, is
|
|
||||||
// sufficient and desirable. For bcrypt compatibility, the key can be over 56
|
|
||||||
// bytes.
|
|
||||||
func NewSaltedCipher(key, salt []byte) (*Cipher, error) {
|
|
||||||
if len(salt) == 0 {
|
|
||||||
return NewCipher(key)
|
|
||||||
}
|
|
||||||
var result Cipher
|
|
||||||
if k := len(key); k < 1 {
|
|
||||||
return nil, KeySizeError(k)
|
|
||||||
}
|
|
||||||
initCipher(&result)
|
|
||||||
expandKeyWithSalt(key, salt, &result)
|
|
||||||
return &result, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// BlockSize returns the Blowfish block size, 8 bytes.
|
|
||||||
// It is necessary to satisfy the Block interface in the
|
|
||||||
// package "crypto/cipher".
|
|
||||||
func (c *Cipher) BlockSize() int { return BlockSize }
|
|
||||||
|
|
||||||
// Encrypt encrypts the 8-byte buffer src using the key k
|
|
||||||
// and stores the result in dst.
|
|
||||||
// Note that for amounts of data larger than a block,
|
|
||||||
// it is not safe to just call Encrypt on successive blocks;
|
|
||||||
// instead, use an encryption mode like CBC (see crypto/cipher/cbc.go).
|
|
||||||
func (c *Cipher) Encrypt(dst, src []byte) {
|
|
||||||
l := uint32(src[0])<<24 | uint32(src[1])<<16 | uint32(src[2])<<8 | uint32(src[3])
|
|
||||||
r := uint32(src[4])<<24 | uint32(src[5])<<16 | uint32(src[6])<<8 | uint32(src[7])
|
|
||||||
l, r = encryptBlock(l, r, c)
|
|
||||||
dst[0], dst[1], dst[2], dst[3] = byte(l>>24), byte(l>>16), byte(l>>8), byte(l)
|
|
||||||
dst[4], dst[5], dst[6], dst[7] = byte(r>>24), byte(r>>16), byte(r>>8), byte(r)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Decrypt decrypts the 8-byte buffer src using the key k
|
|
||||||
// and stores the result in dst.
|
|
||||||
func (c *Cipher) Decrypt(dst, src []byte) {
|
|
||||||
l := uint32(src[0])<<24 | uint32(src[1])<<16 | uint32(src[2])<<8 | uint32(src[3])
|
|
||||||
r := uint32(src[4])<<24 | uint32(src[5])<<16 | uint32(src[6])<<8 | uint32(src[7])
|
|
||||||
l, r = decryptBlock(l, r, c)
|
|
||||||
dst[0], dst[1], dst[2], dst[3] = byte(l>>24), byte(l>>16), byte(l>>8), byte(l)
|
|
||||||
dst[4], dst[5], dst[6], dst[7] = byte(r>>24), byte(r>>16), byte(r>>8), byte(r)
|
|
||||||
}
|
|
||||||
|
|
||||||
func initCipher(c *Cipher) {
|
|
||||||
copy(c.p[0:], p[0:])
|
|
||||||
copy(c.s0[0:], s0[0:])
|
|
||||||
copy(c.s1[0:], s1[0:])
|
|
||||||
copy(c.s2[0:], s2[0:])
|
|
||||||
copy(c.s3[0:], s3[0:])
|
|
||||||
}
|
|
|
@ -1,199 +0,0 @@
|
||||||
// Copyright 2010 The Go Authors. All rights reserved.
|
|
||||||
// Use of this source code is governed by a BSD-style
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
// The startup permutation array and substitution boxes.
|
|
||||||
// They are the hexadecimal digits of PI; see:
|
|
||||||
// https://www.schneier.com/code/constants.txt.
|
|
||||||
|
|
||||||
package blowfish
|
|
||||||
|
|
||||||
var s0 = [256]uint32{
|
|
||||||
0xd1310ba6, 0x98dfb5ac, 0x2ffd72db, 0xd01adfb7, 0xb8e1afed, 0x6a267e96,
|
|
||||||
0xba7c9045, 0xf12c7f99, 0x24a19947, 0xb3916cf7, 0x0801f2e2, 0x858efc16,
|
|
||||||
0x636920d8, 0x71574e69, 0xa458fea3, 0xf4933d7e, 0x0d95748f, 0x728eb658,
|
|
||||||
0x718bcd58, 0x82154aee, 0x7b54a41d, 0xc25a59b5, 0x9c30d539, 0x2af26013,
|
|
||||||
0xc5d1b023, 0x286085f0, 0xca417918, 0xb8db38ef, 0x8e79dcb0, 0x603a180e,
|
|
||||||
0x6c9e0e8b, 0xb01e8a3e, 0xd71577c1, 0xbd314b27, 0x78af2fda, 0x55605c60,
|
|
||||||
0xe65525f3, 0xaa55ab94, 0x57489862, 0x63e81440, 0x55ca396a, 0x2aab10b6,
|
|
||||||
0xb4cc5c34, 0x1141e8ce, 0xa15486af, 0x7c72e993, 0xb3ee1411, 0x636fbc2a,
|
|
||||||
0x2ba9c55d, 0x741831f6, 0xce5c3e16, 0x9b87931e, 0xafd6ba33, 0x6c24cf5c,
|
|
||||||
0x7a325381, 0x28958677, 0x3b8f4898, 0x6b4bb9af, 0xc4bfe81b, 0x66282193,
|
|
||||||
0x61d809cc, 0xfb21a991, 0x487cac60, 0x5dec8032, 0xef845d5d, 0xe98575b1,
|
|
||||||
0xdc262302, 0xeb651b88, 0x23893e81, 0xd396acc5, 0x0f6d6ff3, 0x83f44239,
|
|
||||||
0x2e0b4482, 0xa4842004, 0x69c8f04a, 0x9e1f9b5e, 0x21c66842, 0xf6e96c9a,
|
|
||||||
0x670c9c61, 0xabd388f0, 0x6a51a0d2, 0xd8542f68, 0x960fa728, 0xab5133a3,
|
|
||||||
0x6eef0b6c, 0x137a3be4, 0xba3bf050, 0x7efb2a98, 0xa1f1651d, 0x39af0176,
|
|
||||||
0x66ca593e, 0x82430e88, 0x8cee8619, 0x456f9fb4, 0x7d84a5c3, 0x3b8b5ebe,
|
|
||||||
0xe06f75d8, 0x85c12073, 0x401a449f, 0x56c16aa6, 0x4ed3aa62, 0x363f7706,
|
|
||||||
0x1bfedf72, 0x429b023d, 0x37d0d724, 0xd00a1248, 0xdb0fead3, 0x49f1c09b,
|
|
||||||
0x075372c9, 0x80991b7b, 0x25d479d8, 0xf6e8def7, 0xe3fe501a, 0xb6794c3b,
|
|
||||||
0x976ce0bd, 0x04c006ba, 0xc1a94fb6, 0x409f60c4, 0x5e5c9ec2, 0x196a2463,
|
|
||||||
0x68fb6faf, 0x3e6c53b5, 0x1339b2eb, 0x3b52ec6f, 0x6dfc511f, 0x9b30952c,
|
|
||||||
0xcc814544, 0xaf5ebd09, 0xbee3d004, 0xde334afd, 0x660f2807, 0x192e4bb3,
|
|
||||||
0xc0cba857, 0x45c8740f, 0xd20b5f39, 0xb9d3fbdb, 0x5579c0bd, 0x1a60320a,
|
|
||||||
0xd6a100c6, 0x402c7279, 0x679f25fe, 0xfb1fa3cc, 0x8ea5e9f8, 0xdb3222f8,
|
|
||||||
0x3c7516df, 0xfd616b15, 0x2f501ec8, 0xad0552ab, 0x323db5fa, 0xfd238760,
|
|
||||||
0x53317b48, 0x3e00df82, 0x9e5c57bb, 0xca6f8ca0, 0x1a87562e, 0xdf1769db,
|
|
||||||
0xd542a8f6, 0x287effc3, 0xac6732c6, 0x8c4f5573, 0x695b27b0, 0xbbca58c8,
|
|
||||||
0xe1ffa35d, 0xb8f011a0, 0x10fa3d98, 0xfd2183b8, 0x4afcb56c, 0x2dd1d35b,
|
|
||||||
0x9a53e479, 0xb6f84565, 0xd28e49bc, 0x4bfb9790, 0xe1ddf2da, 0xa4cb7e33,
|
|
||||||
0x62fb1341, 0xcee4c6e8, 0xef20cada, 0x36774c01, 0xd07e9efe, 0x2bf11fb4,
|
|
||||||
0x95dbda4d, 0xae909198, 0xeaad8e71, 0x6b93d5a0, 0xd08ed1d0, 0xafc725e0,
|
|
||||||
0x8e3c5b2f, 0x8e7594b7, 0x8ff6e2fb, 0xf2122b64, 0x8888b812, 0x900df01c,
|
|
||||||
0x4fad5ea0, 0x688fc31c, 0xd1cff191, 0xb3a8c1ad, 0x2f2f2218, 0xbe0e1777,
|
|
||||||
0xea752dfe, 0x8b021fa1, 0xe5a0cc0f, 0xb56f74e8, 0x18acf3d6, 0xce89e299,
|
|
||||||
0xb4a84fe0, 0xfd13e0b7, 0x7cc43b81, 0xd2ada8d9, 0x165fa266, 0x80957705,
|
|
||||||
0x93cc7314, 0x211a1477, 0xe6ad2065, 0x77b5fa86, 0xc75442f5, 0xfb9d35cf,
|
|
||||||
0xebcdaf0c, 0x7b3e89a0, 0xd6411bd3, 0xae1e7e49, 0x00250e2d, 0x2071b35e,
|
|
||||||
0x226800bb, 0x57b8e0af, 0x2464369b, 0xf009b91e, 0x5563911d, 0x59dfa6aa,
|
|
||||||
0x78c14389, 0xd95a537f, 0x207d5ba2, 0x02e5b9c5, 0x83260376, 0x6295cfa9,
|
|
||||||
0x11c81968, 0x4e734a41, 0xb3472dca, 0x7b14a94a, 0x1b510052, 0x9a532915,
|
|
||||||
0xd60f573f, 0xbc9bc6e4, 0x2b60a476, 0x81e67400, 0x08ba6fb5, 0x571be91f,
|
|
||||||
0xf296ec6b, 0x2a0dd915, 0xb6636521, 0xe7b9f9b6, 0xff34052e, 0xc5855664,
|
|
||||||
0x53b02d5d, 0xa99f8fa1, 0x08ba4799, 0x6e85076a,
|
|
||||||
}
|
|
||||||
|
|
||||||
var s1 = [256]uint32{
|
|
||||||
0x4b7a70e9, 0xb5b32944, 0xdb75092e, 0xc4192623, 0xad6ea6b0, 0x49a7df7d,
|
|
||||||
0x9cee60b8, 0x8fedb266, 0xecaa8c71, 0x699a17ff, 0x5664526c, 0xc2b19ee1,
|
|
||||||
0x193602a5, 0x75094c29, 0xa0591340, 0xe4183a3e, 0x3f54989a, 0x5b429d65,
|
|
||||||
0x6b8fe4d6, 0x99f73fd6, 0xa1d29c07, 0xefe830f5, 0x4d2d38e6, 0xf0255dc1,
|
|
||||||
0x4cdd2086, 0x8470eb26, 0x6382e9c6, 0x021ecc5e, 0x09686b3f, 0x3ebaefc9,
|
|
||||||
0x3c971814, 0x6b6a70a1, 0x687f3584, 0x52a0e286, 0xb79c5305, 0xaa500737,
|
|
||||||
0x3e07841c, 0x7fdeae5c, 0x8e7d44ec, 0x5716f2b8, 0xb03ada37, 0xf0500c0d,
|
|
||||||
0xf01c1f04, 0x0200b3ff, 0xae0cf51a, 0x3cb574b2, 0x25837a58, 0xdc0921bd,
|
|
||||||
0xd19113f9, 0x7ca92ff6, 0x94324773, 0x22f54701, 0x3ae5e581, 0x37c2dadc,
|
|
||||||
0xc8b57634, 0x9af3dda7, 0xa9446146, 0x0fd0030e, 0xecc8c73e, 0xa4751e41,
|
|
||||||
0xe238cd99, 0x3bea0e2f, 0x3280bba1, 0x183eb331, 0x4e548b38, 0x4f6db908,
|
|
||||||
0x6f420d03, 0xf60a04bf, 0x2cb81290, 0x24977c79, 0x5679b072, 0xbcaf89af,
|
|
||||||
0xde9a771f, 0xd9930810, 0xb38bae12, 0xdccf3f2e, 0x5512721f, 0x2e6b7124,
|
|
||||||
0x501adde6, 0x9f84cd87, 0x7a584718, 0x7408da17, 0xbc9f9abc, 0xe94b7d8c,
|
|
||||||
0xec7aec3a, 0xdb851dfa, 0x63094366, 0xc464c3d2, 0xef1c1847, 0x3215d908,
|
|
||||||
0xdd433b37, 0x24c2ba16, 0x12a14d43, 0x2a65c451, 0x50940002, 0x133ae4dd,
|
|
||||||
0x71dff89e, 0x10314e55, 0x81ac77d6, 0x5f11199b, 0x043556f1, 0xd7a3c76b,
|
|
||||||
0x3c11183b, 0x5924a509, 0xf28fe6ed, 0x97f1fbfa, 0x9ebabf2c, 0x1e153c6e,
|
|
||||||
0x86e34570, 0xeae96fb1, 0x860e5e0a, 0x5a3e2ab3, 0x771fe71c, 0x4e3d06fa,
|
|
||||||
0x2965dcb9, 0x99e71d0f, 0x803e89d6, 0x5266c825, 0x2e4cc978, 0x9c10b36a,
|
|
||||||
0xc6150eba, 0x94e2ea78, 0xa5fc3c53, 0x1e0a2df4, 0xf2f74ea7, 0x361d2b3d,
|
|
||||||
0x1939260f, 0x19c27960, 0x5223a708, 0xf71312b6, 0xebadfe6e, 0xeac31f66,
|
|
||||||
0xe3bc4595, 0xa67bc883, 0xb17f37d1, 0x018cff28, 0xc332ddef, 0xbe6c5aa5,
|
|
||||||
0x65582185, 0x68ab9802, 0xeecea50f, 0xdb2f953b, 0x2aef7dad, 0x5b6e2f84,
|
|
||||||
0x1521b628, 0x29076170, 0xecdd4775, 0x619f1510, 0x13cca830, 0xeb61bd96,
|
|
||||||
0x0334fe1e, 0xaa0363cf, 0xb5735c90, 0x4c70a239, 0xd59e9e0b, 0xcbaade14,
|
|
||||||
0xeecc86bc, 0x60622ca7, 0x9cab5cab, 0xb2f3846e, 0x648b1eaf, 0x19bdf0ca,
|
|
||||||
0xa02369b9, 0x655abb50, 0x40685a32, 0x3c2ab4b3, 0x319ee9d5, 0xc021b8f7,
|
|
||||||
0x9b540b19, 0x875fa099, 0x95f7997e, 0x623d7da8, 0xf837889a, 0x97e32d77,
|
|
||||||
0x11ed935f, 0x16681281, 0x0e358829, 0xc7e61fd6, 0x96dedfa1, 0x7858ba99,
|
|
||||||
0x57f584a5, 0x1b227263, 0x9b83c3ff, 0x1ac24696, 0xcdb30aeb, 0x532e3054,
|
|
||||||
0x8fd948e4, 0x6dbc3128, 0x58ebf2ef, 0x34c6ffea, 0xfe28ed61, 0xee7c3c73,
|
|
||||||
0x5d4a14d9, 0xe864b7e3, 0x42105d14, 0x203e13e0, 0x45eee2b6, 0xa3aaabea,
|
|
||||||
0xdb6c4f15, 0xfacb4fd0, 0xc742f442, 0xef6abbb5, 0x654f3b1d, 0x41cd2105,
|
|
||||||
0xd81e799e, 0x86854dc7, 0xe44b476a, 0x3d816250, 0xcf62a1f2, 0x5b8d2646,
|
|
||||||
0xfc8883a0, 0xc1c7b6a3, 0x7f1524c3, 0x69cb7492, 0x47848a0b, 0x5692b285,
|
|
||||||
0x095bbf00, 0xad19489d, 0x1462b174, 0x23820e00, 0x58428d2a, 0x0c55f5ea,
|
|
||||||
0x1dadf43e, 0x233f7061, 0x3372f092, 0x8d937e41, 0xd65fecf1, 0x6c223bdb,
|
|
||||||
0x7cde3759, 0xcbee7460, 0x4085f2a7, 0xce77326e, 0xa6078084, 0x19f8509e,
|
|
||||||
0xe8efd855, 0x61d99735, 0xa969a7aa, 0xc50c06c2, 0x5a04abfc, 0x800bcadc,
|
|
||||||
0x9e447a2e, 0xc3453484, 0xfdd56705, 0x0e1e9ec9, 0xdb73dbd3, 0x105588cd,
|
|
||||||
0x675fda79, 0xe3674340, 0xc5c43465, 0x713e38d8, 0x3d28f89e, 0xf16dff20,
|
|
||||||
0x153e21e7, 0x8fb03d4a, 0xe6e39f2b, 0xdb83adf7,
|
|
||||||
}
|
|
||||||
|
|
||||||
var s2 = [256]uint32{
|
|
||||||
0xe93d5a68, 0x948140f7, 0xf64c261c, 0x94692934, 0x411520f7, 0x7602d4f7,
|
|
||||||
0xbcf46b2e, 0xd4a20068, 0xd4082471, 0x3320f46a, 0x43b7d4b7, 0x500061af,
|
|
||||||
0x1e39f62e, 0x97244546, 0x14214f74, 0xbf8b8840, 0x4d95fc1d, 0x96b591af,
|
|
||||||
0x70f4ddd3, 0x66a02f45, 0xbfbc09ec, 0x03bd9785, 0x7fac6dd0, 0x31cb8504,
|
|
||||||
0x96eb27b3, 0x55fd3941, 0xda2547e6, 0xabca0a9a, 0x28507825, 0x530429f4,
|
|
||||||
0x0a2c86da, 0xe9b66dfb, 0x68dc1462, 0xd7486900, 0x680ec0a4, 0x27a18dee,
|
|
||||||
0x4f3ffea2, 0xe887ad8c, 0xb58ce006, 0x7af4d6b6, 0xaace1e7c, 0xd3375fec,
|
|
||||||
0xce78a399, 0x406b2a42, 0x20fe9e35, 0xd9f385b9, 0xee39d7ab, 0x3b124e8b,
|
|
||||||
0x1dc9faf7, 0x4b6d1856, 0x26a36631, 0xeae397b2, 0x3a6efa74, 0xdd5b4332,
|
|
||||||
0x6841e7f7, 0xca7820fb, 0xfb0af54e, 0xd8feb397, 0x454056ac, 0xba489527,
|
|
||||||
0x55533a3a, 0x20838d87, 0xfe6ba9b7, 0xd096954b, 0x55a867bc, 0xa1159a58,
|
|
||||||
0xcca92963, 0x99e1db33, 0xa62a4a56, 0x3f3125f9, 0x5ef47e1c, 0x9029317c,
|
|
||||||
0xfdf8e802, 0x04272f70, 0x80bb155c, 0x05282ce3, 0x95c11548, 0xe4c66d22,
|
|
||||||
0x48c1133f, 0xc70f86dc, 0x07f9c9ee, 0x41041f0f, 0x404779a4, 0x5d886e17,
|
|
||||||
0x325f51eb, 0xd59bc0d1, 0xf2bcc18f, 0x41113564, 0x257b7834, 0x602a9c60,
|
|
||||||
0xdff8e8a3, 0x1f636c1b, 0x0e12b4c2, 0x02e1329e, 0xaf664fd1, 0xcad18115,
|
|
||||||
0x6b2395e0, 0x333e92e1, 0x3b240b62, 0xeebeb922, 0x85b2a20e, 0xe6ba0d99,
|
|
||||||
0xde720c8c, 0x2da2f728, 0xd0127845, 0x95b794fd, 0x647d0862, 0xe7ccf5f0,
|
|
||||||
0x5449a36f, 0x877d48fa, 0xc39dfd27, 0xf33e8d1e, 0x0a476341, 0x992eff74,
|
|
||||||
0x3a6f6eab, 0xf4f8fd37, 0xa812dc60, 0xa1ebddf8, 0x991be14c, 0xdb6e6b0d,
|
|
||||||
0xc67b5510, 0x6d672c37, 0x2765d43b, 0xdcd0e804, 0xf1290dc7, 0xcc00ffa3,
|
|
||||||
0xb5390f92, 0x690fed0b, 0x667b9ffb, 0xcedb7d9c, 0xa091cf0b, 0xd9155ea3,
|
|
||||||
0xbb132f88, 0x515bad24, 0x7b9479bf, 0x763bd6eb, 0x37392eb3, 0xcc115979,
|
|
||||||
0x8026e297, 0xf42e312d, 0x6842ada7, 0xc66a2b3b, 0x12754ccc, 0x782ef11c,
|
|
||||||
0x6a124237, 0xb79251e7, 0x06a1bbe6, 0x4bfb6350, 0x1a6b1018, 0x11caedfa,
|
|
||||||
0x3d25bdd8, 0xe2e1c3c9, 0x44421659, 0x0a121386, 0xd90cec6e, 0xd5abea2a,
|
|
||||||
0x64af674e, 0xda86a85f, 0xbebfe988, 0x64e4c3fe, 0x9dbc8057, 0xf0f7c086,
|
|
||||||
0x60787bf8, 0x6003604d, 0xd1fd8346, 0xf6381fb0, 0x7745ae04, 0xd736fccc,
|
|
||||||
0x83426b33, 0xf01eab71, 0xb0804187, 0x3c005e5f, 0x77a057be, 0xbde8ae24,
|
|
||||||
0x55464299, 0xbf582e61, 0x4e58f48f, 0xf2ddfda2, 0xf474ef38, 0x8789bdc2,
|
|
||||||
0x5366f9c3, 0xc8b38e74, 0xb475f255, 0x46fcd9b9, 0x7aeb2661, 0x8b1ddf84,
|
|
||||||
0x846a0e79, 0x915f95e2, 0x466e598e, 0x20b45770, 0x8cd55591, 0xc902de4c,
|
|
||||||
0xb90bace1, 0xbb8205d0, 0x11a86248, 0x7574a99e, 0xb77f19b6, 0xe0a9dc09,
|
|
||||||
0x662d09a1, 0xc4324633, 0xe85a1f02, 0x09f0be8c, 0x4a99a025, 0x1d6efe10,
|
|
||||||
0x1ab93d1d, 0x0ba5a4df, 0xa186f20f, 0x2868f169, 0xdcb7da83, 0x573906fe,
|
|
||||||
0xa1e2ce9b, 0x4fcd7f52, 0x50115e01, 0xa70683fa, 0xa002b5c4, 0x0de6d027,
|
|
||||||
0x9af88c27, 0x773f8641, 0xc3604c06, 0x61a806b5, 0xf0177a28, 0xc0f586e0,
|
|
||||||
0x006058aa, 0x30dc7d62, 0x11e69ed7, 0x2338ea63, 0x53c2dd94, 0xc2c21634,
|
|
||||||
0xbbcbee56, 0x90bcb6de, 0xebfc7da1, 0xce591d76, 0x6f05e409, 0x4b7c0188,
|
|
||||||
0x39720a3d, 0x7c927c24, 0x86e3725f, 0x724d9db9, 0x1ac15bb4, 0xd39eb8fc,
|
|
||||||
0xed545578, 0x08fca5b5, 0xd83d7cd3, 0x4dad0fc4, 0x1e50ef5e, 0xb161e6f8,
|
|
||||||
0xa28514d9, 0x6c51133c, 0x6fd5c7e7, 0x56e14ec4, 0x362abfce, 0xddc6c837,
|
|
||||||
0xd79a3234, 0x92638212, 0x670efa8e, 0x406000e0,
|
|
||||||
}
|
|
||||||
|
|
||||||
var s3 = [256]uint32{
|
|
||||||
0x3a39ce37, 0xd3faf5cf, 0xabc27737, 0x5ac52d1b, 0x5cb0679e, 0x4fa33742,
|
|
||||||
0xd3822740, 0x99bc9bbe, 0xd5118e9d, 0xbf0f7315, 0xd62d1c7e, 0xc700c47b,
|
|
||||||
0xb78c1b6b, 0x21a19045, 0xb26eb1be, 0x6a366eb4, 0x5748ab2f, 0xbc946e79,
|
|
||||||
0xc6a376d2, 0x6549c2c8, 0x530ff8ee, 0x468dde7d, 0xd5730a1d, 0x4cd04dc6,
|
|
||||||
0x2939bbdb, 0xa9ba4650, 0xac9526e8, 0xbe5ee304, 0xa1fad5f0, 0x6a2d519a,
|
|
||||||
0x63ef8ce2, 0x9a86ee22, 0xc089c2b8, 0x43242ef6, 0xa51e03aa, 0x9cf2d0a4,
|
|
||||||
0x83c061ba, 0x9be96a4d, 0x8fe51550, 0xba645bd6, 0x2826a2f9, 0xa73a3ae1,
|
|
||||||
0x4ba99586, 0xef5562e9, 0xc72fefd3, 0xf752f7da, 0x3f046f69, 0x77fa0a59,
|
|
||||||
0x80e4a915, 0x87b08601, 0x9b09e6ad, 0x3b3ee593, 0xe990fd5a, 0x9e34d797,
|
|
||||||
0x2cf0b7d9, 0x022b8b51, 0x96d5ac3a, 0x017da67d, 0xd1cf3ed6, 0x7c7d2d28,
|
|
||||||
0x1f9f25cf, 0xadf2b89b, 0x5ad6b472, 0x5a88f54c, 0xe029ac71, 0xe019a5e6,
|
|
||||||
0x47b0acfd, 0xed93fa9b, 0xe8d3c48d, 0x283b57cc, 0xf8d56629, 0x79132e28,
|
|
||||||
0x785f0191, 0xed756055, 0xf7960e44, 0xe3d35e8c, 0x15056dd4, 0x88f46dba,
|
|
||||||
0x03a16125, 0x0564f0bd, 0xc3eb9e15, 0x3c9057a2, 0x97271aec, 0xa93a072a,
|
|
||||||
0x1b3f6d9b, 0x1e6321f5, 0xf59c66fb, 0x26dcf319, 0x7533d928, 0xb155fdf5,
|
|
||||||
0x03563482, 0x8aba3cbb, 0x28517711, 0xc20ad9f8, 0xabcc5167, 0xccad925f,
|
|
||||||
0x4de81751, 0x3830dc8e, 0x379d5862, 0x9320f991, 0xea7a90c2, 0xfb3e7bce,
|
|
||||||
0x5121ce64, 0x774fbe32, 0xa8b6e37e, 0xc3293d46, 0x48de5369, 0x6413e680,
|
|
||||||
0xa2ae0810, 0xdd6db224, 0x69852dfd, 0x09072166, 0xb39a460a, 0x6445c0dd,
|
|
||||||
0x586cdecf, 0x1c20c8ae, 0x5bbef7dd, 0x1b588d40, 0xccd2017f, 0x6bb4e3bb,
|
|
||||||
0xdda26a7e, 0x3a59ff45, 0x3e350a44, 0xbcb4cdd5, 0x72eacea8, 0xfa6484bb,
|
|
||||||
0x8d6612ae, 0xbf3c6f47, 0xd29be463, 0x542f5d9e, 0xaec2771b, 0xf64e6370,
|
|
||||||
0x740e0d8d, 0xe75b1357, 0xf8721671, 0xaf537d5d, 0x4040cb08, 0x4eb4e2cc,
|
|
||||||
0x34d2466a, 0x0115af84, 0xe1b00428, 0x95983a1d, 0x06b89fb4, 0xce6ea048,
|
|
||||||
0x6f3f3b82, 0x3520ab82, 0x011a1d4b, 0x277227f8, 0x611560b1, 0xe7933fdc,
|
|
||||||
0xbb3a792b, 0x344525bd, 0xa08839e1, 0x51ce794b, 0x2f32c9b7, 0xa01fbac9,
|
|
||||||
0xe01cc87e, 0xbcc7d1f6, 0xcf0111c3, 0xa1e8aac7, 0x1a908749, 0xd44fbd9a,
|
|
||||||
0xd0dadecb, 0xd50ada38, 0x0339c32a, 0xc6913667, 0x8df9317c, 0xe0b12b4f,
|
|
||||||
0xf79e59b7, 0x43f5bb3a, 0xf2d519ff, 0x27d9459c, 0xbf97222c, 0x15e6fc2a,
|
|
||||||
0x0f91fc71, 0x9b941525, 0xfae59361, 0xceb69ceb, 0xc2a86459, 0x12baa8d1,
|
|
||||||
0xb6c1075e, 0xe3056a0c, 0x10d25065, 0xcb03a442, 0xe0ec6e0e, 0x1698db3b,
|
|
||||||
0x4c98a0be, 0x3278e964, 0x9f1f9532, 0xe0d392df, 0xd3a0342b, 0x8971f21e,
|
|
||||||
0x1b0a7441, 0x4ba3348c, 0xc5be7120, 0xc37632d8, 0xdf359f8d, 0x9b992f2e,
|
|
||||||
0xe60b6f47, 0x0fe3f11d, 0xe54cda54, 0x1edad891, 0xce6279cf, 0xcd3e7e6f,
|
|
||||||
0x1618b166, 0xfd2c1d05, 0x848fd2c5, 0xf6fb2299, 0xf523f357, 0xa6327623,
|
|
||||||
0x93a83531, 0x56cccd02, 0xacf08162, 0x5a75ebb5, 0x6e163697, 0x88d273cc,
|
|
||||||
0xde966292, 0x81b949d0, 0x4c50901b, 0x71c65614, 0xe6c6c7bd, 0x327a140a,
|
|
||||||
0x45e1d006, 0xc3f27b9a, 0xc9aa53fd, 0x62a80f00, 0xbb25bfe2, 0x35bdd2f6,
|
|
||||||
0x71126905, 0xb2040222, 0xb6cbcf7c, 0xcd769c2b, 0x53113ec0, 0x1640e3d3,
|
|
||||||
0x38abbd60, 0x2547adf0, 0xba38209c, 0xf746ce76, 0x77afa1c5, 0x20756060,
|
|
||||||
0x85cbfe4e, 0x8ae88dd8, 0x7aaaf9b0, 0x4cf9aa7e, 0x1948c25c, 0x02fb8a8c,
|
|
||||||
0x01c36ae4, 0xd6ebe1f9, 0x90d4f869, 0xa65cdea0, 0x3f09252d, 0xc208e69f,
|
|
||||||
0xb74e6132, 0xce77e25b, 0x578fdfe3, 0x3ac372e6,
|
|
||||||
}
|
|
||||||
|
|
||||||
var p = [18]uint32{
|
|
||||||
0x243f6a88, 0x85a308d3, 0x13198a2e, 0x03707344, 0xa4093822, 0x299f31d0,
|
|
||||||
0x082efa98, 0xec4e6c89, 0x452821e6, 0x38d01377, 0xbe5466cf, 0x34e90c6c,
|
|
||||||
0xc0ac29b7, 0xc97c50dd, 0x3f84d5b5, 0xb5470917, 0x9216d5d9, 0x8979fb1b,
|
|
||||||
}
|
|
|
@ -60,8 +60,7 @@ causes Load to run in LoadFiles mode, collecting minimal information.
|
||||||
See the documentation for type Config for details.
|
See the documentation for type Config for details.
|
||||||
|
|
||||||
As noted earlier, the Config.Mode controls the amount of detail
|
As noted earlier, the Config.Mode controls the amount of detail
|
||||||
reported about the loaded packages, with each mode returning all the data of the
|
reported about the loaded packages. See the documentation for type LoadMode
|
||||||
previous mode with some extra added. See the documentation for type LoadMode
|
|
||||||
for details.
|
for details.
|
||||||
|
|
||||||
Most tools should pass their command-line arguments (after any flags)
|
Most tools should pass their command-line arguments (after any flags)
|
||||||
|
|
|
@ -84,13 +84,14 @@ func findExternalDriver(cfg *Config) driver {
|
||||||
cmd.Stdin = bytes.NewReader(req)
|
cmd.Stdin = bytes.NewReader(req)
|
||||||
cmd.Stdout = buf
|
cmd.Stdout = buf
|
||||||
cmd.Stderr = stderr
|
cmd.Stderr = stderr
|
||||||
if len(stderr.Bytes()) != 0 && os.Getenv("GOPACKAGESPRINTDRIVERERRORS") != "" {
|
|
||||||
fmt.Fprintf(os.Stderr, "%s stderr: <<%s>>\n", cmdDebugStr(cmd, words...), stderr)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := cmd.Run(); err != nil {
|
if err := cmd.Run(); err != nil {
|
||||||
return nil, fmt.Errorf("%v: %v: %s", tool, err, cmd.Stderr)
|
return nil, fmt.Errorf("%v: %v: %s", tool, err, cmd.Stderr)
|
||||||
}
|
}
|
||||||
|
if len(stderr.Bytes()) != 0 && os.Getenv("GOPACKAGESPRINTDRIVERERRORS") != "" {
|
||||||
|
fmt.Fprintf(os.Stderr, "%s stderr: <<%s>>\n", cmdDebugStr(cmd, words...), stderr)
|
||||||
|
}
|
||||||
|
|
||||||
var response driverResponse
|
var response driverResponse
|
||||||
if err := json.Unmarshal(buf.Bytes(), &response); err != nil {
|
if err := json.Unmarshal(buf.Bytes(), &response); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
|
@ -26,7 +26,6 @@ import (
|
||||||
"golang.org/x/tools/go/internal/packagesdriver"
|
"golang.org/x/tools/go/internal/packagesdriver"
|
||||||
"golang.org/x/tools/internal/gopathwalk"
|
"golang.org/x/tools/internal/gopathwalk"
|
||||||
"golang.org/x/tools/internal/semver"
|
"golang.org/x/tools/internal/semver"
|
||||||
"golang.org/x/tools/internal/span"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// debug controls verbose logging.
|
// debug controls verbose logging.
|
||||||
|
@ -254,12 +253,7 @@ func addNeededOverlayPackages(cfg *Config, driver driver, response *responseDedu
|
||||||
if len(pkgs) == 0 {
|
if len(pkgs) == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
drivercfg := *cfg
|
dr, err := driver(cfg, pkgs...)
|
||||||
if getGoInfo().env.modulesOn {
|
|
||||||
drivercfg.BuildFlags = append(drivercfg.BuildFlags, "-mod=readonly")
|
|
||||||
}
|
|
||||||
dr, err := driver(&drivercfg, pkgs...)
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -270,10 +264,7 @@ func addNeededOverlayPackages(cfg *Config, driver driver, response *responseDedu
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := addNeededOverlayPackages(cfg, driver, response, needPkgs, getGoInfo); err != nil {
|
return addNeededOverlayPackages(cfg, driver, response, needPkgs, getGoInfo)
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func runContainsQueries(cfg *Config, driver driver, response *responseDeduper, queries []string, goInfo func() *goInfo) error {
|
func runContainsQueries(cfg *Config, driver driver, response *responseDeduper, queries []string, goInfo func() *goInfo) error {
|
||||||
|
@ -287,42 +278,43 @@ func runContainsQueries(cfg *Config, driver driver, response *responseDeduper, q
|
||||||
return fmt.Errorf("could not determine absolute path of file= query path %q: %v", query, err)
|
return fmt.Errorf("could not determine absolute path of file= query path %q: %v", query, err)
|
||||||
}
|
}
|
||||||
dirResponse, err := driver(cfg, pattern)
|
dirResponse, err := driver(cfg, pattern)
|
||||||
if err != nil {
|
if err != nil || (len(dirResponse.Packages) == 1 && len(dirResponse.Packages[0].Errors) == 1) {
|
||||||
|
// There was an error loading the package. Try to load the file as an ad-hoc package.
|
||||||
|
// Usually the error will appear in a returned package, but may not if we're in modules mode
|
||||||
|
// and the ad-hoc is located outside a module.
|
||||||
var queryErr error
|
var queryErr error
|
||||||
if dirResponse, queryErr = adHocPackage(cfg, driver, pattern, query); queryErr != nil {
|
dirResponse, queryErr = driver(cfg, query)
|
||||||
return err // return the original error
|
if queryErr != nil {
|
||||||
|
// Return the original error if the attempt to fall back failed.
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
// If we get nothing back from `go list`, try to make this file into its own ad-hoc package.
|
||||||
|
if len(dirResponse.Packages) == 0 && queryErr == nil {
|
||||||
|
dirResponse.Packages = append(dirResponse.Packages, &Package{
|
||||||
|
ID: "command-line-arguments",
|
||||||
|
PkgPath: query,
|
||||||
|
GoFiles: []string{query},
|
||||||
|
CompiledGoFiles: []string{query},
|
||||||
|
Imports: make(map[string]*Package),
|
||||||
|
})
|
||||||
|
dirResponse.Roots = append(dirResponse.Roots, "command-line-arguments")
|
||||||
}
|
}
|
||||||
// `go list` can report errors for files that are not listed as part of a package's GoFiles.
|
// Special case to handle issue #33482:
|
||||||
// In the case of an invalid Go file, we should assume that it is part of package if only
|
// If this is a file= query for ad-hoc packages where the file only exists on an overlay,
|
||||||
// one package is in the response. The file may have valid contents in an overlay.
|
// and exists outside of a module, add the file in for the package.
|
||||||
if len(dirResponse.Packages) == 1 {
|
if len(dirResponse.Packages) == 1 && (dirResponse.Packages[0].ID == "command-line-arguments" ||
|
||||||
pkg := dirResponse.Packages[0]
|
filepath.ToSlash(dirResponse.Packages[0].PkgPath) == filepath.ToSlash(query)) {
|
||||||
for i, err := range pkg.Errors {
|
if len(dirResponse.Packages[0].GoFiles) == 0 {
|
||||||
s := errorSpan(err)
|
filename := filepath.Join(pattern, filepath.Base(query)) // avoid recomputing abspath
|
||||||
if !s.IsValid() {
|
// TODO(matloob): check if the file is outside of a root dir?
|
||||||
break
|
for path := range cfg.Overlay {
|
||||||
}
|
if path == filename {
|
||||||
if len(pkg.CompiledGoFiles) == 0 {
|
dirResponse.Packages[0].Errors = nil
|
||||||
break
|
dirResponse.Packages[0].GoFiles = []string{path}
|
||||||
}
|
dirResponse.Packages[0].CompiledGoFiles = []string{path}
|
||||||
dir := filepath.Dir(pkg.CompiledGoFiles[0])
|
|
||||||
filename := filepath.Join(dir, filepath.Base(s.URI().Filename()))
|
|
||||||
if info, err := os.Stat(filename); err != nil || info.IsDir() {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
if !contains(pkg.CompiledGoFiles, filename) {
|
|
||||||
pkg.CompiledGoFiles = append(pkg.CompiledGoFiles, filename)
|
|
||||||
pkg.GoFiles = append(pkg.GoFiles, filename)
|
|
||||||
pkg.Errors = append(pkg.Errors[:i], pkg.Errors[i+1:]...)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// A final attempt to construct an ad-hoc package.
|
|
||||||
if len(dirResponse.Packages) == 1 && len(dirResponse.Packages[0].Errors) == 1 {
|
|
||||||
var queryErr error
|
|
||||||
if dirResponse, queryErr = adHocPackage(cfg, driver, pattern, query); queryErr != nil {
|
|
||||||
return err // return the original error
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
isRoot := make(map[string]bool, len(dirResponse.Roots))
|
isRoot := make(map[string]bool, len(dirResponse.Roots))
|
||||||
|
@ -350,74 +342,6 @@ func runContainsQueries(cfg *Config, driver driver, response *responseDeduper, q
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// adHocPackage attempts to construct an ad-hoc package given a query that failed.
|
|
||||||
func adHocPackage(cfg *Config, driver driver, pattern, query string) (*driverResponse, error) {
|
|
||||||
// There was an error loading the package. Try to load the file as an ad-hoc package.
|
|
||||||
// Usually the error will appear in a returned package, but may not if we're in modules mode
|
|
||||||
// and the ad-hoc is located outside a module.
|
|
||||||
dirResponse, err := driver(cfg, query)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
// If we get nothing back from `go list`, try to make this file into its own ad-hoc package.
|
|
||||||
if len(dirResponse.Packages) == 0 && err == nil {
|
|
||||||
dirResponse.Packages = append(dirResponse.Packages, &Package{
|
|
||||||
ID: "command-line-arguments",
|
|
||||||
PkgPath: query,
|
|
||||||
GoFiles: []string{query},
|
|
||||||
CompiledGoFiles: []string{query},
|
|
||||||
Imports: make(map[string]*Package),
|
|
||||||
})
|
|
||||||
dirResponse.Roots = append(dirResponse.Roots, "command-line-arguments")
|
|
||||||
}
|
|
||||||
// Special case to handle issue #33482:
|
|
||||||
// If this is a file= query for ad-hoc packages where the file only exists on an overlay,
|
|
||||||
// and exists outside of a module, add the file in for the package.
|
|
||||||
if len(dirResponse.Packages) == 1 && (dirResponse.Packages[0].ID == "command-line-arguments" || dirResponse.Packages[0].PkgPath == filepath.ToSlash(query)) {
|
|
||||||
if len(dirResponse.Packages[0].GoFiles) == 0 {
|
|
||||||
filename := filepath.Join(pattern, filepath.Base(query)) // avoid recomputing abspath
|
|
||||||
// TODO(matloob): check if the file is outside of a root dir?
|
|
||||||
for path := range cfg.Overlay {
|
|
||||||
if path == filename {
|
|
||||||
dirResponse.Packages[0].Errors = nil
|
|
||||||
dirResponse.Packages[0].GoFiles = []string{path}
|
|
||||||
dirResponse.Packages[0].CompiledGoFiles = []string{path}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return dirResponse, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func contains(files []string, filename string) bool {
|
|
||||||
for _, f := range files {
|
|
||||||
if f == filename {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// errorSpan attempts to parse a standard `go list` error message
|
|
||||||
// by stripping off the trailing error message.
|
|
||||||
//
|
|
||||||
// It works only on errors whose message is prefixed by colon,
|
|
||||||
// followed by a space (": "). For example:
|
|
||||||
//
|
|
||||||
// attributes.go:13:1: expected 'package', found 'type'
|
|
||||||
//
|
|
||||||
func errorSpan(err Error) span.Span {
|
|
||||||
if err.Pos == "" {
|
|
||||||
input := strings.TrimSpace(err.Msg)
|
|
||||||
msgIndex := strings.Index(input, ": ")
|
|
||||||
if msgIndex < 0 {
|
|
||||||
return span.Parse(input)
|
|
||||||
}
|
|
||||||
return span.Parse(input[:msgIndex])
|
|
||||||
}
|
|
||||||
return span.Parse(err.Pos)
|
|
||||||
}
|
|
||||||
|
|
||||||
// modCacheRegexp splits a path in a module cache into module, module version, and package.
|
// modCacheRegexp splits a path in a module cache into module, module version, and package.
|
||||||
var modCacheRegexp = regexp.MustCompile(`(.*)@([^/\\]*)(.*)`)
|
var modCacheRegexp = regexp.MustCompile(`(.*)@([^/\\]*)(.*)`)
|
||||||
|
|
||||||
|
@ -881,9 +805,14 @@ func golistDriver(cfg *Config, rootsDirs func() *goInfo, words ...string) (*driv
|
||||||
}
|
}
|
||||||
|
|
||||||
if p.Error != nil {
|
if p.Error != nil {
|
||||||
|
msg := strings.TrimSpace(p.Error.Err) // Trim to work around golang.org/issue/32363.
|
||||||
|
// Address golang.org/issue/35964 by appending import stack to error message.
|
||||||
|
if msg == "import cycle not allowed" && len(p.Error.ImportStack) != 0 {
|
||||||
|
msg += fmt.Sprintf(": import stack: %v", p.Error.ImportStack)
|
||||||
|
}
|
||||||
pkg.Errors = append(pkg.Errors, Error{
|
pkg.Errors = append(pkg.Errors, Error{
|
||||||
Pos: p.Error.Pos,
|
Pos: p.Error.Pos,
|
||||||
Msg: strings.TrimSpace(p.Error.Err), // Trim to work around golang.org/issue/32363.
|
Msg: msg,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,57 @@
|
||||||
|
// Copyright 2019 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package packages
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
var allModes = []LoadMode{
|
||||||
|
NeedName,
|
||||||
|
NeedFiles,
|
||||||
|
NeedCompiledGoFiles,
|
||||||
|
NeedImports,
|
||||||
|
NeedDeps,
|
||||||
|
NeedExportsFile,
|
||||||
|
NeedTypes,
|
||||||
|
NeedSyntax,
|
||||||
|
NeedTypesInfo,
|
||||||
|
NeedTypesSizes,
|
||||||
|
}
|
||||||
|
|
||||||
|
var modeStrings = []string{
|
||||||
|
"NeedName",
|
||||||
|
"NeedFiles",
|
||||||
|
"NeedCompiledGoFiles",
|
||||||
|
"NeedImports",
|
||||||
|
"NeedDeps",
|
||||||
|
"NeedExportsFile",
|
||||||
|
"NeedTypes",
|
||||||
|
"NeedSyntax",
|
||||||
|
"NeedTypesInfo",
|
||||||
|
"NeedTypesSizes",
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mod LoadMode) String() string {
|
||||||
|
m := mod
|
||||||
|
if m == 0 {
|
||||||
|
return fmt.Sprintf("LoadMode(0)")
|
||||||
|
}
|
||||||
|
var out []string
|
||||||
|
for i, x := range allModes {
|
||||||
|
if x > m {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if (m & x) != 0 {
|
||||||
|
out = append(out, modeStrings[i])
|
||||||
|
m = m ^ x
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if m != 0 {
|
||||||
|
out = append(out, "Unknown")
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("LoadMode(%s)", strings.Join(out, "|"))
|
||||||
|
}
|
|
@ -1,100 +0,0 @@
|
||||||
// Copyright 2019 The Go Authors. All rights reserved.
|
|
||||||
// Use of this source code is governed by a BSD-style
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package span
|
|
||||||
|
|
||||||
import (
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
"unicode/utf8"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Parse returns the location represented by the input.
|
|
||||||
// All inputs are valid locations, as they can always be a pure filename.
|
|
||||||
// The returned span will be normalized, and thus if printed may produce a
|
|
||||||
// different string.
|
|
||||||
func Parse(input string) Span {
|
|
||||||
// :0:0#0-0:0#0
|
|
||||||
valid := input
|
|
||||||
var hold, offset int
|
|
||||||
hadCol := false
|
|
||||||
suf := rstripSuffix(input)
|
|
||||||
if suf.sep == "#" {
|
|
||||||
offset = suf.num
|
|
||||||
suf = rstripSuffix(suf.remains)
|
|
||||||
}
|
|
||||||
if suf.sep == ":" {
|
|
||||||
valid = suf.remains
|
|
||||||
hold = suf.num
|
|
||||||
hadCol = true
|
|
||||||
suf = rstripSuffix(suf.remains)
|
|
||||||
}
|
|
||||||
switch {
|
|
||||||
case suf.sep == ":":
|
|
||||||
return New(NewURI(suf.remains), NewPoint(suf.num, hold, offset), Point{})
|
|
||||||
case suf.sep == "-":
|
|
||||||
// we have a span, fall out of the case to continue
|
|
||||||
default:
|
|
||||||
// separator not valid, rewind to either the : or the start
|
|
||||||
return New(NewURI(valid), NewPoint(hold, 0, offset), Point{})
|
|
||||||
}
|
|
||||||
// only the span form can get here
|
|
||||||
// at this point we still don't know what the numbers we have mean
|
|
||||||
// if have not yet seen a : then we might have either a line or a column depending
|
|
||||||
// on whether start has a column or not
|
|
||||||
// we build an end point and will fix it later if needed
|
|
||||||
end := NewPoint(suf.num, hold, offset)
|
|
||||||
hold, offset = 0, 0
|
|
||||||
suf = rstripSuffix(suf.remains)
|
|
||||||
if suf.sep == "#" {
|
|
||||||
offset = suf.num
|
|
||||||
suf = rstripSuffix(suf.remains)
|
|
||||||
}
|
|
||||||
if suf.sep != ":" {
|
|
||||||
// turns out we don't have a span after all, rewind
|
|
||||||
return New(NewURI(valid), end, Point{})
|
|
||||||
}
|
|
||||||
valid = suf.remains
|
|
||||||
hold = suf.num
|
|
||||||
suf = rstripSuffix(suf.remains)
|
|
||||||
if suf.sep != ":" {
|
|
||||||
// line#offset only
|
|
||||||
return New(NewURI(valid), NewPoint(hold, 0, offset), end)
|
|
||||||
}
|
|
||||||
// we have a column, so if end only had one number, it is also the column
|
|
||||||
if !hadCol {
|
|
||||||
end = NewPoint(suf.num, end.v.Line, end.v.Offset)
|
|
||||||
}
|
|
||||||
return New(NewURI(suf.remains), NewPoint(suf.num, hold, offset), end)
|
|
||||||
}
|
|
||||||
|
|
||||||
type suffix struct {
|
|
||||||
remains string
|
|
||||||
sep string
|
|
||||||
num int
|
|
||||||
}
|
|
||||||
|
|
||||||
func rstripSuffix(input string) suffix {
|
|
||||||
if len(input) == 0 {
|
|
||||||
return suffix{"", "", -1}
|
|
||||||
}
|
|
||||||
remains := input
|
|
||||||
num := -1
|
|
||||||
// first see if we have a number at the end
|
|
||||||
last := strings.LastIndexFunc(remains, func(r rune) bool { return r < '0' || r > '9' })
|
|
||||||
if last >= 0 && last < len(remains)-1 {
|
|
||||||
number, err := strconv.ParseInt(remains[last+1:], 10, 64)
|
|
||||||
if err == nil {
|
|
||||||
num = int(number)
|
|
||||||
remains = remains[:last+1]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// now see if we have a trailing separator
|
|
||||||
r, w := utf8.DecodeLastRuneInString(remains)
|
|
||||||
if r != ':' && r != '#' && r == '#' {
|
|
||||||
return suffix{input, "", -1}
|
|
||||||
}
|
|
||||||
remains = remains[:len(remains)-w]
|
|
||||||
return suffix{remains, string(r), num}
|
|
||||||
}
|
|
|
@ -1,285 +0,0 @@
|
||||||
// Copyright 2019 The Go Authors. All rights reserved.
|
|
||||||
// Use of this source code is governed by a BSD-style
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
// Package span contains support for representing with positions and ranges in
|
|
||||||
// text files.
|
|
||||||
package span
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"path"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Span represents a source code range in standardized form.
|
|
||||||
type Span struct {
|
|
||||||
v span
|
|
||||||
}
|
|
||||||
|
|
||||||
// Point represents a single point within a file.
|
|
||||||
// In general this should only be used as part of a Span, as on its own it
|
|
||||||
// does not carry enough information.
|
|
||||||
type Point struct {
|
|
||||||
v point
|
|
||||||
}
|
|
||||||
|
|
||||||
type span struct {
|
|
||||||
URI URI `json:"uri"`
|
|
||||||
Start point `json:"start"`
|
|
||||||
End point `json:"end"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type point struct {
|
|
||||||
Line int `json:"line"`
|
|
||||||
Column int `json:"column"`
|
|
||||||
Offset int `json:"offset"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// Invalid is a span that reports false from IsValid
|
|
||||||
var Invalid = Span{v: span{Start: invalidPoint.v, End: invalidPoint.v}}
|
|
||||||
|
|
||||||
var invalidPoint = Point{v: point{Line: 0, Column: 0, Offset: -1}}
|
|
||||||
|
|
||||||
// Converter is the interface to an object that can convert between line:column
|
|
||||||
// and offset forms for a single file.
|
|
||||||
type Converter interface {
|
|
||||||
//ToPosition converts from an offset to a line:column pair.
|
|
||||||
ToPosition(offset int) (int, int, error)
|
|
||||||
//ToOffset converts from a line:column pair to an offset.
|
|
||||||
ToOffset(line, col int) (int, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
func New(uri URI, start Point, end Point) Span {
|
|
||||||
s := Span{v: span{URI: uri, Start: start.v, End: end.v}}
|
|
||||||
s.v.clean()
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewPoint(line, col, offset int) Point {
|
|
||||||
p := Point{v: point{Line: line, Column: col, Offset: offset}}
|
|
||||||
p.v.clean()
|
|
||||||
return p
|
|
||||||
}
|
|
||||||
|
|
||||||
func Compare(a, b Span) int {
|
|
||||||
if r := CompareURI(a.URI(), b.URI()); r != 0 {
|
|
||||||
return r
|
|
||||||
}
|
|
||||||
if r := comparePoint(a.v.Start, b.v.Start); r != 0 {
|
|
||||||
return r
|
|
||||||
}
|
|
||||||
return comparePoint(a.v.End, b.v.End)
|
|
||||||
}
|
|
||||||
|
|
||||||
func ComparePoint(a, b Point) int {
|
|
||||||
return comparePoint(a.v, b.v)
|
|
||||||
}
|
|
||||||
|
|
||||||
func comparePoint(a, b point) int {
|
|
||||||
if !a.hasPosition() {
|
|
||||||
if a.Offset < b.Offset {
|
|
||||||
return -1
|
|
||||||
}
|
|
||||||
if a.Offset > b.Offset {
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
if a.Line < b.Line {
|
|
||||||
return -1
|
|
||||||
}
|
|
||||||
if a.Line > b.Line {
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
if a.Column < b.Column {
|
|
||||||
return -1
|
|
||||||
}
|
|
||||||
if a.Column > b.Column {
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s Span) HasPosition() bool { return s.v.Start.hasPosition() }
|
|
||||||
func (s Span) HasOffset() bool { return s.v.Start.hasOffset() }
|
|
||||||
func (s Span) IsValid() bool { return s.v.Start.isValid() }
|
|
||||||
func (s Span) IsPoint() bool { return s.v.Start == s.v.End }
|
|
||||||
func (s Span) URI() URI { return s.v.URI }
|
|
||||||
func (s Span) Start() Point { return Point{s.v.Start} }
|
|
||||||
func (s Span) End() Point { return Point{s.v.End} }
|
|
||||||
func (s *Span) MarshalJSON() ([]byte, error) { return json.Marshal(&s.v) }
|
|
||||||
func (s *Span) UnmarshalJSON(b []byte) error { return json.Unmarshal(b, &s.v) }
|
|
||||||
|
|
||||||
func (p Point) HasPosition() bool { return p.v.hasPosition() }
|
|
||||||
func (p Point) HasOffset() bool { return p.v.hasOffset() }
|
|
||||||
func (p Point) IsValid() bool { return p.v.isValid() }
|
|
||||||
func (p *Point) MarshalJSON() ([]byte, error) { return json.Marshal(&p.v) }
|
|
||||||
func (p *Point) UnmarshalJSON(b []byte) error { return json.Unmarshal(b, &p.v) }
|
|
||||||
func (p Point) Line() int {
|
|
||||||
if !p.v.hasPosition() {
|
|
||||||
panic(fmt.Errorf("position not set in %v", p.v))
|
|
||||||
}
|
|
||||||
return p.v.Line
|
|
||||||
}
|
|
||||||
func (p Point) Column() int {
|
|
||||||
if !p.v.hasPosition() {
|
|
||||||
panic(fmt.Errorf("position not set in %v", p.v))
|
|
||||||
}
|
|
||||||
return p.v.Column
|
|
||||||
}
|
|
||||||
func (p Point) Offset() int {
|
|
||||||
if !p.v.hasOffset() {
|
|
||||||
panic(fmt.Errorf("offset not set in %v", p.v))
|
|
||||||
}
|
|
||||||
return p.v.Offset
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p point) hasPosition() bool { return p.Line > 0 }
|
|
||||||
func (p point) hasOffset() bool { return p.Offset >= 0 }
|
|
||||||
func (p point) isValid() bool { return p.hasPosition() || p.hasOffset() }
|
|
||||||
func (p point) isZero() bool {
|
|
||||||
return (p.Line == 1 && p.Column == 1) || (!p.hasPosition() && p.Offset == 0)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *span) clean() {
|
|
||||||
//this presumes the points are already clean
|
|
||||||
if !s.End.isValid() || (s.End == point{}) {
|
|
||||||
s.End = s.Start
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *point) clean() {
|
|
||||||
if p.Line < 0 {
|
|
||||||
p.Line = 0
|
|
||||||
}
|
|
||||||
if p.Column <= 0 {
|
|
||||||
if p.Line > 0 {
|
|
||||||
p.Column = 1
|
|
||||||
} else {
|
|
||||||
p.Column = 0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if p.Offset == 0 && (p.Line > 1 || p.Column > 1) {
|
|
||||||
p.Offset = -1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Format implements fmt.Formatter to print the Location in a standard form.
|
|
||||||
// The format produced is one that can be read back in using Parse.
|
|
||||||
func (s Span) Format(f fmt.State, c rune) {
|
|
||||||
fullForm := f.Flag('+')
|
|
||||||
preferOffset := f.Flag('#')
|
|
||||||
// we should always have a uri, simplify if it is file format
|
|
||||||
//TODO: make sure the end of the uri is unambiguous
|
|
||||||
uri := string(s.v.URI)
|
|
||||||
if c == 'f' {
|
|
||||||
uri = path.Base(uri)
|
|
||||||
} else if !fullForm {
|
|
||||||
uri = s.v.URI.Filename()
|
|
||||||
}
|
|
||||||
fmt.Fprint(f, uri)
|
|
||||||
if !s.IsValid() || (!fullForm && s.v.Start.isZero() && s.v.End.isZero()) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
// see which bits of start to write
|
|
||||||
printOffset := s.HasOffset() && (fullForm || preferOffset || !s.HasPosition())
|
|
||||||
printLine := s.HasPosition() && (fullForm || !printOffset)
|
|
||||||
printColumn := printLine && (fullForm || (s.v.Start.Column > 1 || s.v.End.Column > 1))
|
|
||||||
fmt.Fprint(f, ":")
|
|
||||||
if printLine {
|
|
||||||
fmt.Fprintf(f, "%d", s.v.Start.Line)
|
|
||||||
}
|
|
||||||
if printColumn {
|
|
||||||
fmt.Fprintf(f, ":%d", s.v.Start.Column)
|
|
||||||
}
|
|
||||||
if printOffset {
|
|
||||||
fmt.Fprintf(f, "#%d", s.v.Start.Offset)
|
|
||||||
}
|
|
||||||
// start is written, do we need end?
|
|
||||||
if s.IsPoint() {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
// we don't print the line if it did not change
|
|
||||||
printLine = fullForm || (printLine && s.v.End.Line > s.v.Start.Line)
|
|
||||||
fmt.Fprint(f, "-")
|
|
||||||
if printLine {
|
|
||||||
fmt.Fprintf(f, "%d", s.v.End.Line)
|
|
||||||
}
|
|
||||||
if printColumn {
|
|
||||||
if printLine {
|
|
||||||
fmt.Fprint(f, ":")
|
|
||||||
}
|
|
||||||
fmt.Fprintf(f, "%d", s.v.End.Column)
|
|
||||||
}
|
|
||||||
if printOffset {
|
|
||||||
fmt.Fprintf(f, "#%d", s.v.End.Offset)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s Span) WithPosition(c Converter) (Span, error) {
|
|
||||||
if err := s.update(c, true, false); err != nil {
|
|
||||||
return Span{}, err
|
|
||||||
}
|
|
||||||
return s, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s Span) WithOffset(c Converter) (Span, error) {
|
|
||||||
if err := s.update(c, false, true); err != nil {
|
|
||||||
return Span{}, err
|
|
||||||
}
|
|
||||||
return s, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s Span) WithAll(c Converter) (Span, error) {
|
|
||||||
if err := s.update(c, true, true); err != nil {
|
|
||||||
return Span{}, err
|
|
||||||
}
|
|
||||||
return s, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Span) update(c Converter, withPos, withOffset bool) error {
|
|
||||||
if !s.IsValid() {
|
|
||||||
return fmt.Errorf("cannot add information to an invalid span")
|
|
||||||
}
|
|
||||||
if withPos && !s.HasPosition() {
|
|
||||||
if err := s.v.Start.updatePosition(c); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if s.v.End.Offset == s.v.Start.Offset {
|
|
||||||
s.v.End = s.v.Start
|
|
||||||
} else if err := s.v.End.updatePosition(c); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if withOffset && (!s.HasOffset() || (s.v.End.hasPosition() && !s.v.End.hasOffset())) {
|
|
||||||
if err := s.v.Start.updateOffset(c); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if s.v.End.Line == s.v.Start.Line && s.v.End.Column == s.v.Start.Column {
|
|
||||||
s.v.End.Offset = s.v.Start.Offset
|
|
||||||
} else if err := s.v.End.updateOffset(c); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *point) updatePosition(c Converter) error {
|
|
||||||
line, col, err := c.ToPosition(p.Offset)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
p.Line = line
|
|
||||||
p.Column = col
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *point) updateOffset(c Converter) error {
|
|
||||||
offset, err := c.ToOffset(p.Line, p.Column)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
p.Offset = offset
|
|
||||||
return nil
|
|
||||||
}
|
|
|
@ -1,151 +0,0 @@
|
||||||
// Copyright 2019 The Go Authors. All rights reserved.
|
|
||||||
// Use of this source code is governed by a BSD-style
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package span
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"go/token"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Range represents a source code range in token.Pos form.
|
|
||||||
// It also carries the FileSet that produced the positions, so that it is
|
|
||||||
// self contained.
|
|
||||||
type Range struct {
|
|
||||||
FileSet *token.FileSet
|
|
||||||
Start token.Pos
|
|
||||||
End token.Pos
|
|
||||||
}
|
|
||||||
|
|
||||||
// TokenConverter is a Converter backed by a token file set and file.
|
|
||||||
// It uses the file set methods to work out the conversions, which
|
|
||||||
// makes it fast and does not require the file contents.
|
|
||||||
type TokenConverter struct {
|
|
||||||
fset *token.FileSet
|
|
||||||
file *token.File
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewRange creates a new Range from a FileSet and two positions.
|
|
||||||
// To represent a point pass a 0 as the end pos.
|
|
||||||
func NewRange(fset *token.FileSet, start, end token.Pos) Range {
|
|
||||||
return Range{
|
|
||||||
FileSet: fset,
|
|
||||||
Start: start,
|
|
||||||
End: end,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewTokenConverter returns an implementation of Converter backed by a
|
|
||||||
// token.File.
|
|
||||||
func NewTokenConverter(fset *token.FileSet, f *token.File) *TokenConverter {
|
|
||||||
return &TokenConverter{fset: fset, file: f}
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewContentConverter returns an implementation of Converter for the
|
|
||||||
// given file content.
|
|
||||||
func NewContentConverter(filename string, content []byte) *TokenConverter {
|
|
||||||
fset := token.NewFileSet()
|
|
||||||
f := fset.AddFile(filename, -1, len(content))
|
|
||||||
f.SetLinesForContent(content)
|
|
||||||
return &TokenConverter{fset: fset, file: f}
|
|
||||||
}
|
|
||||||
|
|
||||||
// IsPoint returns true if the range represents a single point.
|
|
||||||
func (r Range) IsPoint() bool {
|
|
||||||
return r.Start == r.End
|
|
||||||
}
|
|
||||||
|
|
||||||
// Span converts a Range to a Span that represents the Range.
|
|
||||||
// It will fill in all the members of the Span, calculating the line and column
|
|
||||||
// information.
|
|
||||||
func (r Range) Span() (Span, error) {
|
|
||||||
f := r.FileSet.File(r.Start)
|
|
||||||
if f == nil {
|
|
||||||
return Span{}, fmt.Errorf("file not found in FileSet")
|
|
||||||
}
|
|
||||||
s := Span{v: span{URI: FileURI(f.Name())}}
|
|
||||||
var err error
|
|
||||||
s.v.Start.Offset, err = offset(f, r.Start)
|
|
||||||
if err != nil {
|
|
||||||
return Span{}, err
|
|
||||||
}
|
|
||||||
if r.End.IsValid() {
|
|
||||||
s.v.End.Offset, err = offset(f, r.End)
|
|
||||||
if err != nil {
|
|
||||||
return Span{}, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
s.v.Start.clean()
|
|
||||||
s.v.End.clean()
|
|
||||||
s.v.clean()
|
|
||||||
converter := NewTokenConverter(r.FileSet, f)
|
|
||||||
return s.WithPosition(converter)
|
|
||||||
}
|
|
||||||
|
|
||||||
// offset is a copy of the Offset function in go/token, but with the adjustment
|
|
||||||
// that it does not panic on invalid positions.
|
|
||||||
func offset(f *token.File, pos token.Pos) (int, error) {
|
|
||||||
if int(pos) < f.Base() || int(pos) > f.Base()+f.Size() {
|
|
||||||
return 0, fmt.Errorf("invalid pos")
|
|
||||||
}
|
|
||||||
return int(pos) - f.Base(), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Range converts a Span to a Range that represents the Span for the supplied
|
|
||||||
// File.
|
|
||||||
func (s Span) Range(converter *TokenConverter) (Range, error) {
|
|
||||||
s, err := s.WithOffset(converter)
|
|
||||||
if err != nil {
|
|
||||||
return Range{}, err
|
|
||||||
}
|
|
||||||
// go/token will panic if the offset is larger than the file's size,
|
|
||||||
// so check here to avoid panicking.
|
|
||||||
if s.Start().Offset() > converter.file.Size() {
|
|
||||||
return Range{}, fmt.Errorf("start offset %v is past the end of the file %v", s.Start(), converter.file.Size())
|
|
||||||
}
|
|
||||||
if s.End().Offset() > converter.file.Size() {
|
|
||||||
return Range{}, fmt.Errorf("end offset %v is past the end of the file %v", s.End(), converter.file.Size())
|
|
||||||
}
|
|
||||||
return Range{
|
|
||||||
FileSet: converter.fset,
|
|
||||||
Start: converter.file.Pos(s.Start().Offset()),
|
|
||||||
End: converter.file.Pos(s.End().Offset()),
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *TokenConverter) ToPosition(offset int) (int, int, error) {
|
|
||||||
if offset > l.file.Size() {
|
|
||||||
return 0, 0, fmt.Errorf("offset %v is past the end of the file %v", offset, l.file.Size())
|
|
||||||
}
|
|
||||||
pos := l.file.Pos(offset)
|
|
||||||
p := l.fset.Position(pos)
|
|
||||||
if offset == l.file.Size() {
|
|
||||||
return p.Line + 1, 1, nil
|
|
||||||
}
|
|
||||||
return p.Line, p.Column, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *TokenConverter) ToOffset(line, col int) (int, error) {
|
|
||||||
if line < 0 {
|
|
||||||
return -1, fmt.Errorf("line is not valid")
|
|
||||||
}
|
|
||||||
lineMax := l.file.LineCount() + 1
|
|
||||||
if line > lineMax {
|
|
||||||
return -1, fmt.Errorf("line is beyond end of file %v", lineMax)
|
|
||||||
} else if line == lineMax {
|
|
||||||
if col > 1 {
|
|
||||||
return -1, fmt.Errorf("column is beyond end of file")
|
|
||||||
}
|
|
||||||
// at the end of the file, allowing for a trailing eol
|
|
||||||
return l.file.Size(), nil
|
|
||||||
}
|
|
||||||
pos := lineStart(l.file, line)
|
|
||||||
if !pos.IsValid() {
|
|
||||||
return -1, fmt.Errorf("line is not in file")
|
|
||||||
}
|
|
||||||
// we assume that column is in bytes here, and that the first byte of a
|
|
||||||
// line is at column 1
|
|
||||||
pos += token.Pos(col - 1)
|
|
||||||
return offset(l.file, pos)
|
|
||||||
}
|
|
|
@ -1,39 +0,0 @@
|
||||||
// Copyright 2019 The Go Authors. All rights reserved.
|
|
||||||
// Use of this source code is governed by a BSD-style
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
// +build !go1.12
|
|
||||||
|
|
||||||
package span
|
|
||||||
|
|
||||||
import (
|
|
||||||
"go/token"
|
|
||||||
)
|
|
||||||
|
|
||||||
// lineStart is the pre-Go 1.12 version of (*token.File).LineStart. For Go
|
|
||||||
// versions <= 1.11, we borrow logic from the analysisutil package.
|
|
||||||
// TODO(rstambler): Delete this file when we no longer support Go 1.11.
|
|
||||||
func lineStart(f *token.File, line int) token.Pos {
|
|
||||||
// Use binary search to find the start offset of this line.
|
|
||||||
|
|
||||||
min := 0 // inclusive
|
|
||||||
max := f.Size() // exclusive
|
|
||||||
for {
|
|
||||||
offset := (min + max) / 2
|
|
||||||
pos := f.Pos(offset)
|
|
||||||
posn := f.Position(pos)
|
|
||||||
if posn.Line == line {
|
|
||||||
return pos - (token.Pos(posn.Column) - 1)
|
|
||||||
}
|
|
||||||
|
|
||||||
if min+1 >= max {
|
|
||||||
return token.NoPos
|
|
||||||
}
|
|
||||||
|
|
||||||
if posn.Line < line {
|
|
||||||
min = offset
|
|
||||||
} else {
|
|
||||||
max = offset
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,16 +0,0 @@
|
||||||
// Copyright 2019 The Go Authors. All rights reserved.
|
|
||||||
// Use of this source code is governed by a BSD-style
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
// +build go1.12
|
|
||||||
|
|
||||||
package span
|
|
||||||
|
|
||||||
import (
|
|
||||||
"go/token"
|
|
||||||
)
|
|
||||||
|
|
||||||
// TODO(rstambler): Delete this file when we no longer support Go 1.11.
|
|
||||||
func lineStart(f *token.File, line int) token.Pos {
|
|
||||||
return f.LineStart(line)
|
|
||||||
}
|
|
|
@ -1,152 +0,0 @@
|
||||||
// Copyright 2019 The Go Authors. All rights reserved.
|
|
||||||
// Use of this source code is governed by a BSD-style
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package span
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"net/url"
|
|
||||||
"os"
|
|
||||||
"path"
|
|
||||||
"path/filepath"
|
|
||||||
"runtime"
|
|
||||||
"strings"
|
|
||||||
"unicode"
|
|
||||||
)
|
|
||||||
|
|
||||||
const fileScheme = "file"
|
|
||||||
|
|
||||||
// URI represents the full URI for a file.
|
|
||||||
type URI string
|
|
||||||
|
|
||||||
// Filename returns the file path for the given URI.
|
|
||||||
// It is an error to call this on a URI that is not a valid filename.
|
|
||||||
func (uri URI) Filename() string {
|
|
||||||
filename, err := filename(uri)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
return filepath.FromSlash(filename)
|
|
||||||
}
|
|
||||||
|
|
||||||
func filename(uri URI) (string, error) {
|
|
||||||
if uri == "" {
|
|
||||||
return "", nil
|
|
||||||
}
|
|
||||||
u, err := url.ParseRequestURI(string(uri))
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
if u.Scheme != fileScheme {
|
|
||||||
return "", fmt.Errorf("only file URIs are supported, got %q from %q", u.Scheme, uri)
|
|
||||||
}
|
|
||||||
if isWindowsDriveURI(u.Path) {
|
|
||||||
u.Path = u.Path[1:]
|
|
||||||
}
|
|
||||||
return u.Path, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewURI returns a span URI for the string.
|
|
||||||
// It will attempt to detect if the string is a file path or uri.
|
|
||||||
func NewURI(s string) URI {
|
|
||||||
if u, err := url.PathUnescape(s); err == nil {
|
|
||||||
s = u
|
|
||||||
}
|
|
||||||
if strings.HasPrefix(s, fileScheme+"://") {
|
|
||||||
return URI(s)
|
|
||||||
}
|
|
||||||
return FileURI(s)
|
|
||||||
}
|
|
||||||
|
|
||||||
func CompareURI(a, b URI) int {
|
|
||||||
if equalURI(a, b) {
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
if a < b {
|
|
||||||
return -1
|
|
||||||
}
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
|
|
||||||
func equalURI(a, b URI) bool {
|
|
||||||
if a == b {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
// If we have the same URI basename, we may still have the same file URIs.
|
|
||||||
if !strings.EqualFold(path.Base(string(a)), path.Base(string(b))) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
fa, err := filename(a)
|
|
||||||
if err != nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
fb, err := filename(b)
|
|
||||||
if err != nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
// Stat the files to check if they are equal.
|
|
||||||
infoa, err := os.Stat(filepath.FromSlash(fa))
|
|
||||||
if err != nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
infob, err := os.Stat(filepath.FromSlash(fb))
|
|
||||||
if err != nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return os.SameFile(infoa, infob)
|
|
||||||
}
|
|
||||||
|
|
||||||
// FileURI returns a span URI for the supplied file path.
|
|
||||||
// It will always have the file scheme.
|
|
||||||
func FileURI(path string) URI {
|
|
||||||
if path == "" {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
// Handle standard library paths that contain the literal "$GOROOT".
|
|
||||||
// TODO(rstambler): The go/packages API should allow one to determine a user's $GOROOT.
|
|
||||||
const prefix = "$GOROOT"
|
|
||||||
if len(path) >= len(prefix) && strings.EqualFold(prefix, path[:len(prefix)]) {
|
|
||||||
suffix := path[len(prefix):]
|
|
||||||
path = runtime.GOROOT() + suffix
|
|
||||||
}
|
|
||||||
if !isWindowsDrivePath(path) {
|
|
||||||
if abs, err := filepath.Abs(path); err == nil {
|
|
||||||
path = abs
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Check the file path again, in case it became absolute.
|
|
||||||
if isWindowsDrivePath(path) {
|
|
||||||
path = "/" + path
|
|
||||||
}
|
|
||||||
path = filepath.ToSlash(path)
|
|
||||||
u := url.URL{
|
|
||||||
Scheme: fileScheme,
|
|
||||||
Path: path,
|
|
||||||
}
|
|
||||||
uri := u.String()
|
|
||||||
if unescaped, err := url.PathUnescape(uri); err == nil {
|
|
||||||
uri = unescaped
|
|
||||||
}
|
|
||||||
return URI(uri)
|
|
||||||
}
|
|
||||||
|
|
||||||
// isWindowsDrivePath returns true if the file path is of the form used by
|
|
||||||
// Windows. We check if the path begins with a drive letter, followed by a ":".
|
|
||||||
func isWindowsDrivePath(path string) bool {
|
|
||||||
if len(path) < 4 {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return unicode.IsLetter(rune(path[0])) && path[1] == ':'
|
|
||||||
}
|
|
||||||
|
|
||||||
// isWindowsDriveURI returns true if the file URI is of the format used by
|
|
||||||
// Windows URIs. The url.Parse package does not specially handle Windows paths
|
|
||||||
// (see https://golang.org/issue/6027). We check if the URI path has
|
|
||||||
// a drive prefix (e.g. "/C:"). If so, we trim the leading "/".
|
|
||||||
func isWindowsDriveURI(uri string) bool {
|
|
||||||
if len(uri) < 4 {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return uri[0] == '/' && unicode.IsLetter(rune(uri[1])) && uri[2] == ':'
|
|
||||||
}
|
|
|
@ -1,94 +0,0 @@
|
||||||
// Copyright 2019 The Go Authors. All rights reserved.
|
|
||||||
// Use of this source code is governed by a BSD-style
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package span
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"unicode/utf16"
|
|
||||||
"unicode/utf8"
|
|
||||||
)
|
|
||||||
|
|
||||||
// ToUTF16Column calculates the utf16 column expressed by the point given the
|
|
||||||
// supplied file contents.
|
|
||||||
// This is used to convert from the native (always in bytes) column
|
|
||||||
// representation and the utf16 counts used by some editors.
|
|
||||||
func ToUTF16Column(p Point, content []byte) (int, error) {
|
|
||||||
if content == nil {
|
|
||||||
return -1, fmt.Errorf("ToUTF16Column: missing content")
|
|
||||||
}
|
|
||||||
if !p.HasPosition() {
|
|
||||||
return -1, fmt.Errorf("ToUTF16Column: point is missing position")
|
|
||||||
}
|
|
||||||
if !p.HasOffset() {
|
|
||||||
return -1, fmt.Errorf("ToUTF16Column: point is missing offset")
|
|
||||||
}
|
|
||||||
offset := p.Offset() // 0-based
|
|
||||||
colZero := p.Column() - 1 // 0-based
|
|
||||||
if colZero == 0 {
|
|
||||||
// 0-based column 0, so it must be chr 1
|
|
||||||
return 1, nil
|
|
||||||
} else if colZero < 0 {
|
|
||||||
return -1, fmt.Errorf("ToUTF16Column: column is invalid (%v)", colZero)
|
|
||||||
}
|
|
||||||
// work out the offset at the start of the line using the column
|
|
||||||
lineOffset := offset - colZero
|
|
||||||
if lineOffset < 0 || offset > len(content) {
|
|
||||||
return -1, fmt.Errorf("ToUTF16Column: offsets %v-%v outside file contents (%v)", lineOffset, offset, len(content))
|
|
||||||
}
|
|
||||||
// Use the offset to pick out the line start.
|
|
||||||
// This cannot panic: offset > len(content) and lineOffset < offset.
|
|
||||||
start := content[lineOffset:]
|
|
||||||
|
|
||||||
// Now, truncate down to the supplied column.
|
|
||||||
start = start[:colZero]
|
|
||||||
|
|
||||||
// and count the number of utf16 characters
|
|
||||||
// in theory we could do this by hand more efficiently...
|
|
||||||
return len(utf16.Encode([]rune(string(start)))) + 1, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// FromUTF16Column advances the point by the utf16 character offset given the
|
|
||||||
// supplied line contents.
|
|
||||||
// This is used to convert from the utf16 counts used by some editors to the
|
|
||||||
// native (always in bytes) column representation.
|
|
||||||
func FromUTF16Column(p Point, chr int, content []byte) (Point, error) {
|
|
||||||
if !p.HasOffset() {
|
|
||||||
return Point{}, fmt.Errorf("FromUTF16Column: point is missing offset")
|
|
||||||
}
|
|
||||||
// if chr is 1 then no adjustment needed
|
|
||||||
if chr <= 1 {
|
|
||||||
return p, nil
|
|
||||||
}
|
|
||||||
if p.Offset() >= len(content) {
|
|
||||||
return p, fmt.Errorf("FromUTF16Column: offset (%v) greater than length of content (%v)", p.Offset(), len(content))
|
|
||||||
}
|
|
||||||
remains := content[p.Offset():]
|
|
||||||
// scan forward the specified number of characters
|
|
||||||
for count := 1; count < chr; count++ {
|
|
||||||
if len(remains) <= 0 {
|
|
||||||
return Point{}, fmt.Errorf("FromUTF16Column: chr goes beyond the content")
|
|
||||||
}
|
|
||||||
r, w := utf8.DecodeRune(remains)
|
|
||||||
if r == '\n' {
|
|
||||||
// Per the LSP spec:
|
|
||||||
//
|
|
||||||
// > If the character value is greater than the line length it
|
|
||||||
// > defaults back to the line length.
|
|
||||||
break
|
|
||||||
}
|
|
||||||
remains = remains[w:]
|
|
||||||
if r >= 0x10000 {
|
|
||||||
// a two point rune
|
|
||||||
count++
|
|
||||||
// if we finished in a two point rune, do not advance past the first
|
|
||||||
if count >= chr {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
p.v.Column += w
|
|
||||||
p.v.Offset += w
|
|
||||||
}
|
|
||||||
return p, nil
|
|
||||||
}
|
|
|
@ -500,7 +500,7 @@ golang.org/x/crypto/ssh/terminal
|
||||||
# golang.org/x/lint v0.0.0-20190930215403-16217165b5de
|
# golang.org/x/lint v0.0.0-20190930215403-16217165b5de
|
||||||
golang.org/x/lint
|
golang.org/x/lint
|
||||||
golang.org/x/lint/golint
|
golang.org/x/lint/golint
|
||||||
# golang.org/x/net v0.0.0-20190620200207-3b0461eec859
|
# golang.org/x/net v0.0.0-20190930134127-c5a3c61f89f3
|
||||||
golang.org/x/net/bpf
|
golang.org/x/net/bpf
|
||||||
golang.org/x/net/context
|
golang.org/x/net/context
|
||||||
golang.org/x/net/html
|
golang.org/x/net/html
|
||||||
|
@ -510,7 +510,7 @@ golang.org/x/net/idna
|
||||||
golang.org/x/net/internal/iana
|
golang.org/x/net/internal/iana
|
||||||
golang.org/x/net/internal/socket
|
golang.org/x/net/internal/socket
|
||||||
golang.org/x/net/ipv4
|
golang.org/x/net/ipv4
|
||||||
# golang.org/x/sync v0.0.0-20190423024810-112230192c58
|
# golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e
|
||||||
golang.org/x/sync/syncmap
|
golang.org/x/sync/syncmap
|
||||||
# golang.org/x/sys v0.0.0-20191113165036-4c7a9d0fe056
|
# golang.org/x/sys v0.0.0-20191113165036-4c7a9d0fe056
|
||||||
golang.org/x/sys/cpu
|
golang.org/x/sys/cpu
|
||||||
|
@ -537,7 +537,7 @@ golang.org/x/text/secure/bidirule
|
||||||
golang.org/x/text/transform
|
golang.org/x/text/transform
|
||||||
golang.org/x/text/unicode/bidi
|
golang.org/x/text/unicode/bidi
|
||||||
golang.org/x/text/unicode/norm
|
golang.org/x/text/unicode/norm
|
||||||
# golang.org/x/tools v0.0.0-20191109212701-97ad0ed33101
|
# golang.org/x/tools v0.0.0-20191213032237-7093a17b0467
|
||||||
golang.org/x/tools/go/analysis
|
golang.org/x/tools/go/analysis
|
||||||
golang.org/x/tools/go/analysis/passes/inspect
|
golang.org/x/tools/go/analysis/passes/inspect
|
||||||
golang.org/x/tools/go/ast/astutil
|
golang.org/x/tools/go/ast/astutil
|
||||||
|
@ -552,7 +552,6 @@ golang.org/x/tools/go/types/typeutil
|
||||||
golang.org/x/tools/internal/fastwalk
|
golang.org/x/tools/internal/fastwalk
|
||||||
golang.org/x/tools/internal/gopathwalk
|
golang.org/x/tools/internal/gopathwalk
|
||||||
golang.org/x/tools/internal/semver
|
golang.org/x/tools/internal/semver
|
||||||
golang.org/x/tools/internal/span
|
|
||||||
# gopkg.in/go-playground/validator.v9 v9.29.1
|
# gopkg.in/go-playground/validator.v9 v9.29.1
|
||||||
gopkg.in/go-playground/validator.v9
|
gopkg.in/go-playground/validator.v9
|
||||||
# gopkg.in/natefinch/lumberjack.v2 v2.0.0
|
# gopkg.in/natefinch/lumberjack.v2 v2.0.0
|
||||||
|
|
Loading…
Reference in New Issue