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:
Michael Haro 2016-06-08 09:10:03 -07:00 committed by Miek Gieben
parent a465e84f54
commit 48c8acaf0c
1 changed files with 7 additions and 37 deletions

View File

@ -19,7 +19,7 @@ func unpackDataA(msg []byte, off int) (net.IP, int, error) {
if off+net.IPv4len > len(msg) {
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
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"}
}
switch len(a) {
case net.IPv6len:
msg[off] = a[12]
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]
case net.IPv4len, net.IPv6len:
copy(msg[off:], a.To4())
off += net.IPv4len
case 0:
// Allowed, for dynamic updates.
@ -54,9 +45,7 @@ func unpackDataAAAA(msg []byte, off int) (net.IP, int, error) {
if off+net.IPv6len > len(msg) {
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],
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]}
aaaa := append(make(net.IP, 0, net.IPv6len), msg[off:off+net.IPv6len]...)
off += net.IPv6len
return aaaa, off, nil
}
@ -68,22 +57,7 @@ func packDataAAAA(aaaa net.IP, msg []byte, off int) (int, error) {
switch len(aaaa) {
case net.IPv6len:
msg[off] = aaaa[0]
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]
copy(msg[off:], aaaa)
off += net.IPv6len
case 0:
// Allowed, dynamic updates.
@ -289,12 +263,8 @@ func unpackString(msg []byte, off int) (string, int, error) {
switch b {
case '"', '\\':
s = append(s, '\\', b)
case '\t':
s = append(s, '\t')
case '\r':
s = append(s, '\r')
case '\n':
s = append(s, '\n')
case '\t', '\r', '\n':
s = append(s, b)
default:
if b < 32 || b > 127 { // unprintable
var buf [3]byte