mirror of
https://github.com/waku-org/go-multiaddr.git
synced 2025-02-22 19:18:14 +00:00
simplify variable-length argument handling
Instead of having the transformers deal with variable length argument prefixes, just do this in the bytesToString/stringToBytes functions.
This commit is contained in:
parent
b68c4a264d
commit
5416c663d5
32
codec.go
32
codec.go
@ -50,6 +50,9 @@ func stringToBytes(s string) ([]byte, error) {
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse %s: %s %s", p.Name, sp[0], err)
|
||||
}
|
||||
if p.Size < 0 { // varint size.
|
||||
b.Write(CodeToVarint(len(a)))
|
||||
}
|
||||
b.Write(a)
|
||||
sp = sp[1:]
|
||||
}
|
||||
@ -74,11 +77,13 @@ func validateBytes(b []byte) (err error) {
|
||||
continue
|
||||
}
|
||||
|
||||
size, err := sizeForAddr(p, b)
|
||||
n, size, err := sizeForAddr(p, b)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
b = b[n:]
|
||||
|
||||
if len(b) < size || size < 0 {
|
||||
return fmt.Errorf("invalid value for size")
|
||||
}
|
||||
@ -109,11 +114,13 @@ func bytesToString(b []byte) (ret string, err error) {
|
||||
continue
|
||||
}
|
||||
|
||||
size, err := sizeForAddr(p, b)
|
||||
n, size, err := sizeForAddr(p, b)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
b = b[n:]
|
||||
|
||||
if len(b) < size || size < 0 {
|
||||
return "", fmt.Errorf("invalid value for size")
|
||||
}
|
||||
@ -125,6 +132,9 @@ func bytesToString(b []byte) (ret string, err error) {
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if p.Path && len(a) > 0 && a[0] == '/' {
|
||||
a = a[1:]
|
||||
}
|
||||
if len(a) > 0 {
|
||||
s += "/" + a
|
||||
}
|
||||
@ -134,24 +144,24 @@ func bytesToString(b []byte) (ret string, err error) {
|
||||
return s, nil
|
||||
}
|
||||
|
||||
func sizeForAddr(p Protocol, b []byte) (int, error) {
|
||||
func sizeForAddr(p Protocol, b []byte) (skip, size int, err error) {
|
||||
switch {
|
||||
case p.Size > 0:
|
||||
return (p.Size / 8), nil
|
||||
return 0, (p.Size / 8), nil
|
||||
case p.Size == 0:
|
||||
return 0, nil
|
||||
return 0, 0, nil
|
||||
case p.Path:
|
||||
size, n, err := ReadVarintCode(b)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
return 0, 0, err
|
||||
}
|
||||
return size + n, nil
|
||||
return n, size, nil
|
||||
default:
|
||||
size, n, err := ReadVarintCode(b)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
return 0, 0, err
|
||||
}
|
||||
return size + n, nil
|
||||
return n, size, nil
|
||||
}
|
||||
}
|
||||
|
||||
@ -168,12 +178,12 @@ func bytesSplit(b []byte) ([][]byte, error) {
|
||||
return nil, fmt.Errorf("no protocol with code %d", b[0])
|
||||
}
|
||||
|
||||
size, err := sizeForAddr(p, b[n:])
|
||||
n2, size, err := sizeForAddr(p, b[n:])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
length := n + size
|
||||
length := n + n2 + size
|
||||
ret = append(ret, b[:length])
|
||||
b = b[length:]
|
||||
}
|
||||
|
@ -86,12 +86,12 @@ func (m *multiaddr) Protocols() []Protocol {
|
||||
ps = append(ps, p)
|
||||
b = b[n:]
|
||||
|
||||
size, err := sizeForAddr(p, b)
|
||||
n, size, err := sizeForAddr(p, b)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
b = b[size:]
|
||||
b = b[n+size:]
|
||||
}
|
||||
return ps
|
||||
}
|
||||
|
@ -3,7 +3,6 @@ package multiaddr
|
||||
import (
|
||||
"encoding/base32"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"strconv"
|
||||
@ -129,22 +128,10 @@ func p2pStB(s string) ([]byte, error) {
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse p2p addr: %s %s", s, err)
|
||||
}
|
||||
size := CodeToVarint(len(m))
|
||||
b := append(size, m...)
|
||||
return b, nil
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func p2pBtS(b []byte) (string, error) {
|
||||
// the address is a varint-prefixed multihash string representation
|
||||
size, n, err := ReadVarintCode(b)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
b = b[n:]
|
||||
if len(b) != size {
|
||||
return "", errors.New("inconsistent lengths")
|
||||
}
|
||||
m, err := mh.Cast(b)
|
||||
if err != nil {
|
||||
return "", err
|
||||
@ -155,27 +142,9 @@ func p2pBtS(b []byte) (string, error) {
|
||||
var TranscoderUnix = NewTranscoderFromFunctions(unixStB, unixBtS)
|
||||
|
||||
func unixStB(s string) ([]byte, error) {
|
||||
// the address is the whole remaining string, prefixed by a varint len
|
||||
size := CodeToVarint(len(s))
|
||||
b := append(size, []byte(s)...)
|
||||
return b, nil
|
||||
return []byte(s), nil
|
||||
}
|
||||
|
||||
func unixBtS(b []byte) (string, error) {
|
||||
// the address is a varint len prefixed string
|
||||
size, n, err := ReadVarintCode(b)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
b = b[n:]
|
||||
if len(b) != size {
|
||||
return "", errors.New("inconsistent lengths")
|
||||
}
|
||||
if size == 0 {
|
||||
return "", errors.New("invalid length")
|
||||
}
|
||||
s := string(b)
|
||||
s = s[1:] // remove starting slash
|
||||
return s, nil
|
||||
return string(b), nil
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user