codec.go 3.69 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1 2
package multiaddr

Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
3
import (
4
	"bytes"
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
5 6
	"fmt"
	"strings"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
7 8
)

9
func stringToBytes(s string) ([]byte, error) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
10 11 12
	// consume trailing slashes
	s = strings.TrimRight(s, "/")

13
	var b bytes.Buffer
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
14
	sp := strings.Split(s, "/")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
15

16
	if sp[0] != "" {
Steven Allen's avatar
Steven Allen committed
17
		return nil, fmt.Errorf("failed to parse multiaddr %q: must begin with /", s)
18 19
	}

Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
20 21
	// consume first empty elem
	sp = sp[1:]
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
22

Steven Allen's avatar
Steven Allen committed
23 24 25 26
	if len(sp) == 0 {
		return nil, fmt.Errorf("failed to parse multiaddr %q: empty multiaddr", s)
	}

Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
27
	for len(sp) > 0 {
28 29
		name := sp[0]
		p := ProtocolWithName(name)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
30
		if p.Code == 0 {
Steven Allen's avatar
Steven Allen committed
31
			return nil, fmt.Errorf("failed to parse multiaddr %q: unknown protocol %s", s, sp[0])
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
32
		}
33
		_, _ = b.Write(CodeToVarint(p.Code))
34
		sp = sp[1:]
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
35

36 37 38 39 40
		if p.Size == 0 { // no length.
			continue
		}

		if len(sp) < 1 {
Steven Allen's avatar
Steven Allen committed
41
			return nil, fmt.Errorf("failed to parse multiaddr %q: unexpected end of multiaddr", s)
42
		}
43

jbenet's avatar
jbenet committed
44 45 46 47 48 49
		if p.Path {
			// it's a path protocol (terminal).
			// consume the rest of the address as the next component.
			sp = []string{"/" + strings.Join(sp, "/")}
		}

50
		a, err := p.Transcoder.StringToBytes(sp[0])
51
		if err != nil {
Steven Allen's avatar
Steven Allen committed
52
			return nil, fmt.Errorf("failed to parse multiaddr %q: invalid value %q for protocol %s: %s", s, sp[0], p.Name, err)
53
		}
54
		if p.Size < 0 { // varint size.
55
			_, _ = b.Write(CodeToVarint(len(a)))
56
		}
57
		b.Write(a)
58
		sp = sp[1:]
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
59
	}
60 61

	return b.Bytes(), nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
62 63
}

64
func validateBytes(b []byte) (err error) {
Steven Allen's avatar
Steven Allen committed
65 66 67
	if len(b) == 0 {
		return fmt.Errorf("empty multiaddr")
	}
68 69
	for len(b) > 0 {
		code, n, err := ReadVarintCode(b)
Jeromy's avatar
Jeromy committed
70 71 72 73
		if err != nil {
			return err
		}

74 75 76 77 78 79 80 81 82
		b = b[n:]
		p := ProtocolWithCode(code)
		if p.Code == 0 {
			return fmt.Errorf("no protocol with code %d", code)
		}

		if p.Size == 0 {
			continue
		}
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
83

84
		n, size, err := sizeForAddr(p, b)
85 86 87 88
		if err != nil {
			return err
		}

89 90
		b = b[n:]

Jeromy's avatar
Jeromy committed
91
		if len(b) < size || size < 0 {
92
			return fmt.Errorf("invalid value for size %d", len(b))
93
		}
Jeromy's avatar
Jeromy committed
94

95 96 97 98 99
		err = p.Transcoder.ValidateBytes(b[:size])
		if err != nil {
			return err
		}

100 101 102 103 104
		b = b[size:]
	}

	return nil
}
Jeromy's avatar
Jeromy committed
105

Steven Allen's avatar
Steven Allen committed
106 107 108 109 110 111 112
func readComponent(b []byte) (int, Component, error) {
	var offset int
	code, n, err := ReadVarintCode(b)
	if err != nil {
		return 0, Component{}, err
	}
	offset += n
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
113

Steven Allen's avatar
Steven Allen committed
114 115 116 117
	p := ProtocolWithCode(code)
	if p.Code == 0 {
		return 0, Component{}, fmt.Errorf("no protocol with code %d", code)
	}
118

Steven Allen's avatar
Steven Allen committed
119 120 121 122 123 124 125
	if p.Size == 0 {
		return offset, Component{
			bytes:    b[:offset],
			offset:   offset,
			protocol: p,
		}, nil
	}
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
126

Steven Allen's avatar
Steven Allen committed
127 128 129 130
	n, size, err := sizeForAddr(p, b[offset:])
	if err != nil {
		return 0, Component{}, err
	}
131

Steven Allen's avatar
Steven Allen committed
132
	offset += n
133

Steven Allen's avatar
Steven Allen committed
134
	if len(b[offset:]) < size || size < 0 {
135
		return 0, Component{}, fmt.Errorf("invalid value for size %d", len(b[offset:]))
Steven Allen's avatar
Steven Allen committed
136
	}
137

Steven Allen's avatar
Steven Allen committed
138 139 140 141 142 143
	return offset + size, Component{
		bytes:    b[:offset+size],
		protocol: p,
		offset:   offset,
	}, nil
}
144

Steven Allen's avatar
Steven Allen committed
145
func bytesToString(b []byte) (ret string, err error) {
Steven Allen's avatar
Steven Allen committed
146 147 148
	if len(b) == 0 {
		return "", fmt.Errorf("empty multiaddr")
	}
Steven Allen's avatar
Steven Allen committed
149 150 151 152
	var buf strings.Builder

	for len(b) > 0 {
		n, c, err := readComponent(b)
153 154 155
		if err != nil {
			return "", err
		}
Steven Allen's avatar
Steven Allen committed
156 157
		b = b[n:]
		c.writeTo(&buf)
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
158 159
	}

Steven Allen's avatar
Steven Allen committed
160
	return buf.String(), nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
161
}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
162

163
func sizeForAddr(p Protocol, b []byte) (skip, size int, err error) {
164 165
	switch {
	case p.Size > 0:
166
		return 0, (p.Size / 8), nil
167
	case p.Size == 0:
168
		return 0, 0, nil
169
	default:
170 171
		size, n, err := ReadVarintCode(b)
		if err != nil {
172
			return 0, 0, err
173
		}
174
		return n, size, nil
175 176 177
	}
}

178 179 180 181 182 183
func bytesSplit(b []byte) ([][]byte, error) {
	var ret [][]byte
	for len(b) > 0 {
		code, n, err := ReadVarintCode(b)
		if err != nil {
			return nil, err
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
184 185
		}

186
		p := ProtocolWithCode(code)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
187
		if p.Code == 0 {
188 189 190
			return nil, fmt.Errorf("no protocol with code %d", b[0])
		}

191
		n2, size, err := sizeForAddr(p, b[n:])
192 193
		if err != nil {
			return nil, err
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
194 195
		}

196
		length := n + n2 + size
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
197 198 199 200 201 202
		ret = append(ret, b[:length])
		b = b[length:]
	}

	return ret, nil
}