Simplify packing and unpacking (#363)
* Simplify packing and unpacking * Make a copy of the buffer to allow buffer reuse. * Revert use of fmt.Sprintf I don't have time to benchmark this right now and I suspect a different cleanup will be of higher value.
This commit is contained in:
parent
a465e84f54
commit
48c8acaf0c
|
@ -19,7 +19,7 @@ func unpackDataA(msg []byte, off int) (net.IP, int, error) {
|
||||||
if off+net.IPv4len > len(msg) {
|
if off+net.IPv4len > len(msg) {
|
||||||
return nil, len(msg), &Error{err: "overflow unpacking a"}
|
return nil, len(msg), &Error{err: "overflow unpacking a"}
|
||||||
}
|
}
|
||||||
a := net.IPv4(msg[off], msg[off+1], msg[off+2], msg[off+3])
|
a := append(make(net.IP, 0, net.IPv4len), msg[off:off+net.IPv4len]...)
|
||||||
off += net.IPv4len
|
off += net.IPv4len
|
||||||
return a, off, nil
|
return a, off, nil
|
||||||
}
|
}
|
||||||
|
@ -30,17 +30,8 @@ func packDataA(a net.IP, msg []byte, off int) (int, error) {
|
||||||
return len(msg), &Error{err: "overflow packing a"}
|
return len(msg), &Error{err: "overflow packing a"}
|
||||||
}
|
}
|
||||||
switch len(a) {
|
switch len(a) {
|
||||||
case net.IPv6len:
|
case net.IPv4len, net.IPv6len:
|
||||||
msg[off] = a[12]
|
copy(msg[off:], a.To4())
|
||||||
msg[off+1] = a[13]
|
|
||||||
msg[off+2] = a[14]
|
|
||||||
msg[off+3] = a[15]
|
|
||||||
off += net.IPv4len
|
|
||||||
case net.IPv4len:
|
|
||||||
msg[off] = a[0]
|
|
||||||
msg[off+1] = a[1]
|
|
||||||
msg[off+2] = a[2]
|
|
||||||
msg[off+3] = a[3]
|
|
||||||
off += net.IPv4len
|
off += net.IPv4len
|
||||||
case 0:
|
case 0:
|
||||||
// Allowed, for dynamic updates.
|
// Allowed, for dynamic updates.
|
||||||
|
@ -54,9 +45,7 @@ func unpackDataAAAA(msg []byte, off int) (net.IP, int, error) {
|
||||||
if off+net.IPv6len > len(msg) {
|
if off+net.IPv6len > len(msg) {
|
||||||
return nil, len(msg), &Error{err: "overflow unpacking aaaa"}
|
return nil, len(msg), &Error{err: "overflow unpacking aaaa"}
|
||||||
}
|
}
|
||||||
aaaa := net.IP{msg[off], msg[off+1], msg[off+2], msg[off+3], msg[off+4],
|
aaaa := append(make(net.IP, 0, net.IPv6len), msg[off:off+net.IPv6len]...)
|
||||||
msg[off+5], msg[off+6], msg[off+7], msg[off+8], msg[off+9], msg[off+10],
|
|
||||||
msg[off+11], msg[off+12], msg[off+13], msg[off+14], msg[off+15]}
|
|
||||||
off += net.IPv6len
|
off += net.IPv6len
|
||||||
return aaaa, off, nil
|
return aaaa, off, nil
|
||||||
}
|
}
|
||||||
|
@ -68,22 +57,7 @@ func packDataAAAA(aaaa net.IP, msg []byte, off int) (int, error) {
|
||||||
|
|
||||||
switch len(aaaa) {
|
switch len(aaaa) {
|
||||||
case net.IPv6len:
|
case net.IPv6len:
|
||||||
msg[off] = aaaa[0]
|
copy(msg[off:], aaaa)
|
||||||
msg[off+1] = aaaa[1]
|
|
||||||
msg[off+2] = aaaa[2]
|
|
||||||
msg[off+3] = aaaa[3]
|
|
||||||
msg[off+4] = aaaa[4]
|
|
||||||
msg[off+5] = aaaa[5]
|
|
||||||
msg[off+6] = aaaa[6]
|
|
||||||
msg[off+7] = aaaa[7]
|
|
||||||
msg[off+8] = aaaa[8]
|
|
||||||
msg[off+9] = aaaa[9]
|
|
||||||
msg[off+10] = aaaa[10]
|
|
||||||
msg[off+11] = aaaa[11]
|
|
||||||
msg[off+12] = aaaa[12]
|
|
||||||
msg[off+13] = aaaa[13]
|
|
||||||
msg[off+14] = aaaa[14]
|
|
||||||
msg[off+15] = aaaa[15]
|
|
||||||
off += net.IPv6len
|
off += net.IPv6len
|
||||||
case 0:
|
case 0:
|
||||||
// Allowed, dynamic updates.
|
// Allowed, dynamic updates.
|
||||||
|
@ -289,12 +263,8 @@ func unpackString(msg []byte, off int) (string, int, error) {
|
||||||
switch b {
|
switch b {
|
||||||
case '"', '\\':
|
case '"', '\\':
|
||||||
s = append(s, '\\', b)
|
s = append(s, '\\', b)
|
||||||
case '\t':
|
case '\t', '\r', '\n':
|
||||||
s = append(s, '\t')
|
s = append(s, b)
|
||||||
case '\r':
|
|
||||||
s = append(s, '\r')
|
|
||||||
case '\n':
|
|
||||||
s = append(s, '\n')
|
|
||||||
default:
|
default:
|
||||||
if b < 32 || b > 127 { // unprintable
|
if b < 32 || b > 127 { // unprintable
|
||||||
var buf [3]byte
|
var buf [3]byte
|
||||||
|
|
Loading…
Reference in New Issue