File: codec.go

package info (click to toggle)
golang-github-multiformats-go-multiaddr 0.16.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 536 kB
  • sloc: makefile: 2
file content (157 lines) | stat: -rw-r--r-- 3,342 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package multiaddr

import (
	"bytes"
	"fmt"
	"strings"

	"github.com/multiformats/go-varint"
)

func stringToBytes(s string) ([]byte, error) {
	// consume trailing slashes
	s = strings.TrimRight(s, "/")

	var b bytes.Buffer
	sp := strings.Split(s, "/")

	if sp[0] != "" {
		return nil, fmt.Errorf("failed to parse multiaddr %q: must begin with /", s)
	}

	// consume first empty elem
	sp = sp[1:]

	if len(sp) == 0 {
		return nil, fmt.Errorf("failed to parse multiaddr %q: empty multiaddr", s)
	}

	for len(sp) > 0 {
		name := sp[0]
		p := ProtocolWithName(name)
		if p.Code == 0 {
			return nil, fmt.Errorf("failed to parse multiaddr %q: unknown protocol %s", s, sp[0])
		}
		_, _ = b.Write(p.VCode)
		sp = sp[1:]

		if p.Size == 0 { // no length.
			continue
		}

		if len(sp) < 1 {
			return nil, fmt.Errorf("failed to parse multiaddr %q: unexpected end of multiaddr", s)
		}

		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 {
			return nil, fmt.Errorf("failed to parse multiaddr %q: invalid value %q for protocol %s: %s", s, sp[0], p.Name, err)
		}
		err = p.Transcoder.ValidateBytes(a)
		if err != nil {
			return nil, fmt.Errorf("failed to validate multiaddr %q: invalid value %q for protocol %s: %w", s, sp[0], p.Name, err)
		}
		if p.Size < 0 { // varint size.
			_, _ = b.Write(varint.ToUvarint(uint64(len(a))))
		}
		b.Write(a)
		sp = sp[1:]
	}

	return b.Bytes(), nil
}

func readComponent(b []byte) (int, *Component, error) {
	var offset int
	code, n, err := ReadVarintCode(b)
	if err != nil {
		return 0, nil, err
	}
	offset += n

	p := ProtocolWithCode(code)
	if p.Code == 0 {
		return 0, nil, fmt.Errorf("no protocol with code %d", code)
	}
	pPtr := protocolPtrByCode[code]
	if pPtr == nil {
		return 0, nil, fmt.Errorf("no protocol with code %d", code)
	}

	if p.Size == 0 {
		c := &Component{
			bytes:         string(b[:offset]),
			valueStartIdx: offset,
			protocol:      pPtr,
		}

		err := validateComponent(c)
		if err != nil {
			return 0, nil, err
		}

		return offset, c, nil
	}

	var size int
	if p.Size < 0 {
		// varint
		var n int
		size, n, err = ReadVarintCode(b[offset:])
		if err != nil {
			return 0, nil, err
		}
		offset += n
	} else {
		// Size is in bits, but we operate on bytes
		size = p.Size / 8
	}

	if len(b[offset:]) < size || size <= 0 {
		return 0, nil, fmt.Errorf("invalid value for size %d", len(b[offset:]))
	}

	c := &Component{
		bytes:         string(b[:offset+size]),
		protocol:      pPtr,
		valueStartIdx: offset,
	}
	err = validateComponent(c)
	if err != nil {
		return 0, nil, err
	}

	return offset + size, c, err
}

func readMultiaddr(b []byte) (int, Multiaddr, error) {
	if len(b) == 0 {
		return 0, nil, fmt.Errorf("empty multiaddr")
	}

	var res Multiaddr
	bytesRead := 0
	sawPathComponent := false
	for len(b) > 0 {
		n, c, err := readComponent(b)
		if err != nil {
			return 0, nil, err
		}
		b = b[n:]
		bytesRead += n

		if sawPathComponent {
			// It is an error to have another component after a path component.
			return bytesRead, nil, fmt.Errorf("unexpected component after path component")
		}
		sawPathComponent = c.protocol.Path
		res = append(res, *c)
	}
	return bytesRead, res, nil
}