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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
|
package multiaddr
import (
"bytes"
"encoding/binary"
"encoding/json"
"fmt"
"strings"
"github.com/multiformats/go-varint"
)
// Component is a single multiaddr Component.
type Component struct {
// bytes is the raw bytes of the component. It includes the protocol code as
// varint, possibly the size of the value, and the value.
bytes string // string for immutability.
protocol *Protocol
valueStartIdx int // Index of the first byte of the Component's value in the bytes array
}
func (c *Component) Multiaddr() Multiaddr {
if c == nil {
return nil
}
return []Component{*c}
}
func (c *Component) Encapsulate(o Multiaddrer) Multiaddr {
return c.Multiaddr().Encapsulate(o)
}
func (c *Component) Decapsulate(o Multiaddrer) Multiaddr {
return c.Multiaddr().Decapsulate(o)
}
func (c *Component) Bytes() []byte {
if c == nil {
return nil
}
return []byte(c.bytes)
}
func (c *Component) MarshalBinary() ([]byte, error) {
if c == nil {
return nil, errNilPtr
}
return c.Bytes(), nil
}
func (c *Component) UnmarshalBinary(data []byte) error {
if c == nil {
return errNilPtr
}
_, comp, err := readComponent(data)
if err != nil {
return err
}
*c = *comp
return nil
}
func (c *Component) MarshalText() ([]byte, error) {
if c == nil {
return nil, errNilPtr
}
return []byte(c.String()), nil
}
func (c *Component) UnmarshalText(data []byte) error {
if c == nil {
return errNilPtr
}
bytes, err := stringToBytes(string(data))
if err != nil {
return err
}
_, comp, err := readComponent(bytes)
if err != nil {
return err
}
*c = *comp
return nil
}
func (c *Component) MarshalJSON() ([]byte, error) {
if c == nil {
return nil, errNilPtr
}
txt, err := c.MarshalText()
if err != nil {
return nil, err
}
return json.Marshal(string(txt))
}
func (c *Component) UnmarshalJSON(data []byte) error {
if c == nil {
return errNilPtr
}
var v string
if err := json.Unmarshal(data, &v); err != nil {
return err
}
return c.UnmarshalText([]byte(v))
}
func (c *Component) Equal(o *Component) bool {
if c == nil || o == nil {
return c == o
}
return c.bytes == o.bytes
}
func (c *Component) Compare(o *Component) int {
if c == nil && o == nil {
return 0
}
if c == nil {
return -1
}
if o == nil {
return 1
}
return strings.Compare(c.bytes, o.bytes)
}
func (c *Component) Protocols() []Protocol {
if c == nil {
return nil
}
if c.protocol == nil {
return nil
}
return []Protocol{*c.protocol}
}
func (c *Component) ValueForProtocol(code int) (string, error) {
if c == nil {
return "", fmt.Errorf("component is nil")
}
if c.protocol == nil {
return "", fmt.Errorf("component has nil protocol")
}
if c.protocol.Code != code {
return "", ErrProtocolNotFound
}
return c.Value(), nil
}
func (c *Component) Protocol() Protocol {
if c == nil {
return Protocol{}
}
if c.protocol == nil {
return Protocol{}
}
return *c.protocol
}
func (c *Component) Code() int {
if c == nil || c.protocol == nil {
return 0
}
return c.protocol.Code
}
func (c *Component) RawValue() []byte {
if c == nil {
return nil
}
return []byte(c.bytes[c.valueStartIdx:])
}
func (c *Component) Value() string {
if c == nil {
return ""
}
// This Component MUST have been checked by validateComponent when created
value, _ := c.valueAndErr()
return value
}
func (c *Component) valueAndErr() (string, error) {
if c == nil {
return "", errNilPtr
}
if c.protocol == nil {
return "", fmt.Errorf("component has nil protocol")
}
if c.protocol.Transcoder == nil {
return "", nil
}
value, err := c.protocol.Transcoder.BytesToString([]byte(c.bytes[c.valueStartIdx:]))
if err != nil {
return "", err
}
return value, nil
}
func (c *Component) String() string {
if c == nil {
return "<nil component>"
}
var b strings.Builder
c.writeTo(&b)
return b.String()
}
// writeTo is an efficient, private function for string-formatting a multiaddr.
// Trust me, we tend to allocate a lot when doing this.
func (c *Component) writeTo(b *strings.Builder) {
if c == nil {
return
}
if c.protocol == nil {
return
}
b.WriteByte('/')
b.WriteString(c.protocol.Name)
value := c.Value()
if len(value) == 0 {
return
}
if !(c.protocol.Path && value[0] == '/') {
b.WriteByte('/')
}
b.WriteString(value)
}
// NewComponent constructs a new multiaddr component
func NewComponent(protocol, value string) (*Component, error) {
p := ProtocolWithName(protocol)
if p.Code == 0 {
return nil, fmt.Errorf("unsupported protocol: %s", protocol)
}
if p.Transcoder != nil {
bts, err := p.Transcoder.StringToBytes(value)
if err != nil {
return nil, err
}
return newComponent(p, bts)
} else if value != "" {
return nil, fmt.Errorf("protocol %s doesn't take a value", p.Name)
}
return newComponent(p, nil)
}
func newComponent(protocol Protocol, bvalue []byte) (*Component, error) {
protocolPtr := protocolPtrByCode[protocol.Code]
if protocolPtr == nil {
protocolPtr = &protocol
}
size := len(bvalue)
size += len(protocol.VCode)
if protocol.Size < 0 {
size += varint.UvarintSize(uint64(len(bvalue)))
}
maddr := make([]byte, size)
var offset int
offset += copy(maddr[offset:], protocol.VCode)
if protocol.Size < 0 {
offset += binary.PutUvarint(maddr[offset:], uint64(len(bvalue)))
}
copy(maddr[offset:], bvalue)
// Shouldn't happen
if len(maddr) != offset+len(bvalue) {
return nil, fmt.Errorf("component size mismatch: %d != %d", len(maddr), offset+len(bvalue))
}
c := &Component{
bytes: string(maddr),
protocol: protocolPtr,
valueStartIdx: offset,
}
err := validateComponent(c)
if err != nil {
return nil, err
}
return c, nil
}
// validateComponent MUST be called after creating a non-zero Component.
// It ensures that we will be able to call all methods on Component without
// error.
func validateComponent(c *Component) error {
if c == nil {
return errNilPtr
}
if c.protocol == nil {
return fmt.Errorf("component is missing its protocol")
}
if c.valueStartIdx > len(c.bytes) {
return fmt.Errorf("component valueStartIdx is greater than the length of the component's bytes")
}
if len(c.protocol.VCode) == 0 {
return fmt.Errorf("Component is missing its protocol's VCode field")
}
if len(c.bytes) < len(c.protocol.VCode) {
return fmt.Errorf("component size mismatch: %d != %d", len(c.bytes), len(c.protocol.VCode))
}
if !bytes.Equal([]byte(c.bytes[:len(c.protocol.VCode)]), c.protocol.VCode) {
return fmt.Errorf("component's VCode field is invalid: %v != %v", []byte(c.bytes[:len(c.protocol.VCode)]), c.protocol.VCode)
}
if c.protocol.Size < 0 {
size, n, err := ReadVarintCode([]byte(c.bytes[len(c.protocol.VCode):]))
if err != nil {
return err
}
if size != len(c.bytes[c.valueStartIdx:]) {
return fmt.Errorf("component value size mismatch: %d != %d", size, len(c.bytes[c.valueStartIdx:]))
}
if len(c.protocol.VCode)+n+size != len(c.bytes) {
return fmt.Errorf("component size mismatch: %d != %d", len(c.protocol.VCode)+n+size, len(c.bytes))
}
} else {
// Fixed size value
size := c.protocol.Size / 8
if size != len(c.bytes[c.valueStartIdx:]) {
return fmt.Errorf("component value size mismatch: %d != %d", size, len(c.bytes[c.valueStartIdx:]))
}
if len(c.protocol.VCode)+size != len(c.bytes) {
return fmt.Errorf("component size mismatch: %d != %d", len(c.protocol.VCode)+size, len(c.bytes))
}
}
_, err := c.valueAndErr()
if err != nil {
return err
}
if c.protocol.Transcoder != nil {
err = c.protocol.Transcoder.ValidateBytes([]byte(c.bytes[c.valueStartIdx:]))
if err != nil {
return err
}
}
return nil
}
|