2018-07-04 10:51:47 +00:00
|
|
|
|
package multiaddr
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"fmt"
|
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func stringToBytes(s string) ([]byte, error) {
|
|
|
|
|
// consume trailing slashes
|
|
|
|
|
s = strings.TrimRight(s, "/")
|
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
|
var b bytes.Buffer
|
2018-07-04 10:51:47 +00:00
|
|
|
|
sp := strings.Split(s, "/")
|
|
|
|
|
|
|
|
|
|
if sp[0] != "" {
|
2019-06-09 07:24:20 +00:00
|
|
|
|
return nil, fmt.Errorf("failed to parse multiaddr %q: must begin with /", s)
|
2018-07-04 10:51:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// consume first empty elem
|
|
|
|
|
sp = sp[1:]
|
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
|
if len(sp) == 0 {
|
|
|
|
|
return nil, fmt.Errorf("failed to parse multiaddr %q: empty multiaddr", s)
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-04 10:51:47 +00:00
|
|
|
|
for len(sp) > 0 {
|
2019-06-09 07:24:20 +00:00
|
|
|
|
name := sp[0]
|
|
|
|
|
p := ProtocolWithName(name)
|
2018-07-04 10:51:47 +00:00
|
|
|
|
if p.Code == 0 {
|
2019-06-09 07:24:20 +00:00
|
|
|
|
return nil, fmt.Errorf("failed to parse multiaddr %q: unknown protocol %s", s, sp[0])
|
2018-07-04 10:51:47 +00:00
|
|
|
|
}
|
2019-06-09 07:24:20 +00:00
|
|
|
|
_, _ = b.Write(CodeToVarint(p.Code))
|
2018-07-04 10:51:47 +00:00
|
|
|
|
sp = sp[1:]
|
|
|
|
|
|
|
|
|
|
if p.Size == 0 { // no length.
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(sp) < 1 {
|
2019-06-09 07:24:20 +00:00
|
|
|
|
return nil, fmt.Errorf("failed to parse multiaddr %q: unexpected end of multiaddr", s)
|
2018-07-04 10:51:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if p.Path {
|
|
|
|
|
// it's a path protocol (terminal).
|
|
|
|
|
// consume the rest of the address as the next component.
|
|
|
|
|
sp = []string{"/" + strings.Join(sp, "/")}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
a, err := p.Transcoder.StringToBytes(sp[0])
|
|
|
|
|
if err != nil {
|
2019-06-09 07:24:20 +00:00
|
|
|
|
return nil, fmt.Errorf("failed to parse multiaddr %q: invalid value %q for protocol %s: %s", s, sp[0], p.Name, err)
|
|
|
|
|
}
|
|
|
|
|
if p.Size < 0 { // varint size.
|
|
|
|
|
_, _ = b.Write(CodeToVarint(len(a)))
|
2018-07-04 10:51:47 +00:00
|
|
|
|
}
|
|
|
|
|
b.Write(a)
|
|
|
|
|
sp = sp[1:]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return b.Bytes(), nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func validateBytes(b []byte) (err error) {
|
2019-06-09 07:24:20 +00:00
|
|
|
|
if len(b) == 0 {
|
|
|
|
|
return fmt.Errorf("empty multiaddr")
|
|
|
|
|
}
|
2018-07-04 10:51:47 +00:00
|
|
|
|
for len(b) > 0 {
|
|
|
|
|
code, n, err := ReadVarintCode(b)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
b = b[n:]
|
|
|
|
|
p := ProtocolWithCode(code)
|
|
|
|
|
if p.Code == 0 {
|
|
|
|
|
return fmt.Errorf("no protocol with code %d", code)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if p.Size == 0 {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
|
n, size, err := sizeForAddr(p, b)
|
2018-07-04 10:51:47 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
|
b = b[n:]
|
|
|
|
|
|
2018-07-04 10:51:47 +00:00
|
|
|
|
if len(b) < size || size < 0 {
|
2019-06-09 07:24:20 +00:00
|
|
|
|
return fmt.Errorf("invalid value for size %d", len(b))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = p.Transcoder.ValidateBytes(b[:size])
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
2018-07-04 10:51:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
b = b[size:]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
|
func readComponent(b []byte) (int, Component, error) {
|
|
|
|
|
var offset int
|
|
|
|
|
code, n, err := ReadVarintCode(b)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return 0, Component{}, err
|
|
|
|
|
}
|
|
|
|
|
offset += n
|
2018-07-04 10:51:47 +00:00
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
|
p := ProtocolWithCode(code)
|
|
|
|
|
if p.Code == 0 {
|
|
|
|
|
return 0, Component{}, fmt.Errorf("no protocol with code %d", code)
|
|
|
|
|
}
|
2018-07-04 10:51:47 +00:00
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
|
if p.Size == 0 {
|
|
|
|
|
return offset, Component{
|
|
|
|
|
bytes: b[:offset],
|
|
|
|
|
offset: offset,
|
|
|
|
|
protocol: p,
|
|
|
|
|
}, nil
|
|
|
|
|
}
|
2018-07-04 10:51:47 +00:00
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
|
n, size, err := sizeForAddr(p, b[offset:])
|
|
|
|
|
if err != nil {
|
|
|
|
|
return 0, Component{}, err
|
|
|
|
|
}
|
2018-07-04 10:51:47 +00:00
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
|
offset += n
|
2018-07-04 10:51:47 +00:00
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
|
if len(b[offset:]) < size || size < 0 {
|
|
|
|
|
return 0, Component{}, fmt.Errorf("invalid value for size %d", len(b[offset:]))
|
|
|
|
|
}
|
2018-07-04 10:51:47 +00:00
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
|
return offset + size, Component{
|
|
|
|
|
bytes: b[:offset+size],
|
|
|
|
|
protocol: p,
|
|
|
|
|
offset: offset,
|
|
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func bytesToString(b []byte) (ret string, err error) {
|
|
|
|
|
if len(b) == 0 {
|
|
|
|
|
return "", fmt.Errorf("empty multiaddr")
|
|
|
|
|
}
|
|
|
|
|
var buf strings.Builder
|
|
|
|
|
|
|
|
|
|
for len(b) > 0 {
|
|
|
|
|
n, c, err := readComponent(b)
|
2018-07-04 10:51:47 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
2019-06-09 07:24:20 +00:00
|
|
|
|
b = b[n:]
|
|
|
|
|
c.writeTo(&buf)
|
2018-07-04 10:51:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
|
return buf.String(), nil
|
2018-07-04 10:51:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
|
func sizeForAddr(p Protocol, b []byte) (skip, size int, err error) {
|
2018-07-04 10:51:47 +00:00
|
|
|
|
switch {
|
|
|
|
|
case p.Size > 0:
|
2019-06-09 07:24:20 +00:00
|
|
|
|
return 0, (p.Size / 8), nil
|
2018-07-04 10:51:47 +00:00
|
|
|
|
case p.Size == 0:
|
2019-06-09 07:24:20 +00:00
|
|
|
|
return 0, 0, nil
|
2018-07-04 10:51:47 +00:00
|
|
|
|
default:
|
|
|
|
|
size, n, err := ReadVarintCode(b)
|
|
|
|
|
if err != nil {
|
2019-06-09 07:24:20 +00:00
|
|
|
|
return 0, 0, err
|
2018-07-04 10:51:47 +00:00
|
|
|
|
}
|
2019-06-09 07:24:20 +00:00
|
|
|
|
return n, size, nil
|
2018-07-04 10:51:47 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func bytesSplit(b []byte) ([][]byte, error) {
|
|
|
|
|
var ret [][]byte
|
|
|
|
|
for len(b) > 0 {
|
|
|
|
|
code, n, err := ReadVarintCode(b)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
p := ProtocolWithCode(code)
|
|
|
|
|
if p.Code == 0 {
|
|
|
|
|
return nil, fmt.Errorf("no protocol with code %d", b[0])
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
|
n2, size, err := sizeForAddr(p, b[n:])
|
2018-07-04 10:51:47 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
|
length := n + n2 + size
|
2018-07-04 10:51:47 +00:00
|
|
|
|
ret = append(ret, b[:length])
|
|
|
|
|
b = b[length:]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
|
}
|