mirror of
https://github.com/golang/go
synced 2024-11-15 04:50:31 -07:00
net/netip: use binary/byteorder
This also as a side effect fixes #56136 because internal/netip-use-byteorder package has a special treatment in the inliner. Fixes #56136 Change-Id: I113185207186c3fa780240de009192096d3c77a6
This commit is contained in:
parent
74a49188d3
commit
a78d8f6fee
@ -1,45 +0,0 @@
|
|||||||
// Copyright 2021 The Go Authors. All rights reserved.
|
|
||||||
// Use of this source code is governed by a BSD-style
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
// Stuff that exists in std, but we can't use due to being a dependency
|
|
||||||
// of net, for go/build deps_test policy reasons.
|
|
||||||
|
|
||||||
package netip
|
|
||||||
|
|
||||||
func beUint64(b []byte) uint64 {
|
|
||||||
_ = b[7] // bounds check hint to compiler; see golang.org/issue/14808
|
|
||||||
return uint64(b[7]) | uint64(b[6])<<8 | uint64(b[5])<<16 | uint64(b[4])<<24 |
|
|
||||||
uint64(b[3])<<32 | uint64(b[2])<<40 | uint64(b[1])<<48 | uint64(b[0])<<56
|
|
||||||
}
|
|
||||||
|
|
||||||
func bePutUint64(b []byte, v uint64) {
|
|
||||||
_ = b[7] // early bounds check to guarantee safety of writes below
|
|
||||||
b[0] = byte(v >> 56)
|
|
||||||
b[1] = byte(v >> 48)
|
|
||||||
b[2] = byte(v >> 40)
|
|
||||||
b[3] = byte(v >> 32)
|
|
||||||
b[4] = byte(v >> 24)
|
|
||||||
b[5] = byte(v >> 16)
|
|
||||||
b[6] = byte(v >> 8)
|
|
||||||
b[7] = byte(v)
|
|
||||||
}
|
|
||||||
|
|
||||||
func bePutUint32(b []byte, v uint32) {
|
|
||||||
_ = b[3] // early bounds check to guarantee safety of writes below
|
|
||||||
b[0] = byte(v >> 24)
|
|
||||||
b[1] = byte(v >> 16)
|
|
||||||
b[2] = byte(v >> 8)
|
|
||||||
b[3] = byte(v)
|
|
||||||
}
|
|
||||||
|
|
||||||
func leUint16(b []byte) uint16 {
|
|
||||||
_ = b[1] // bounds check hint to compiler; see golang.org/issue/14808
|
|
||||||
return uint16(b[0]) | uint16(b[1])<<8
|
|
||||||
}
|
|
||||||
|
|
||||||
func lePutUint16(b []byte, v uint16) {
|
|
||||||
_ = b[1] // early bounds check to guarantee safety of writes below
|
|
||||||
b[0] = byte(v)
|
|
||||||
b[1] = byte(v >> 8)
|
|
||||||
}
|
|
@ -14,6 +14,7 @@ package netip
|
|||||||
import (
|
import (
|
||||||
"cmp"
|
"cmp"
|
||||||
"errors"
|
"errors"
|
||||||
|
"internal/byteorder"
|
||||||
"math"
|
"math"
|
||||||
"strconv"
|
"strconv"
|
||||||
"unique"
|
"unique"
|
||||||
@ -102,8 +103,8 @@ func AddrFrom4(addr [4]byte) Addr {
|
|||||||
func AddrFrom16(addr [16]byte) Addr {
|
func AddrFrom16(addr [16]byte) Addr {
|
||||||
return Addr{
|
return Addr{
|
||||||
addr: uint128{
|
addr: uint128{
|
||||||
beUint64(addr[:8]),
|
byteorder.BeUint64(addr[:8]),
|
||||||
beUint64(addr[8:]),
|
byteorder.BeUint64(addr[8:]),
|
||||||
},
|
},
|
||||||
z: z6noz,
|
z: z6noz,
|
||||||
}
|
}
|
||||||
@ -676,8 +677,8 @@ func (ip Addr) Prefix(b int) (Prefix, error) {
|
|||||||
// [Addr.Zone] method to get it).
|
// [Addr.Zone] method to get it).
|
||||||
// The ip zero value returns all zeroes.
|
// The ip zero value returns all zeroes.
|
||||||
func (ip Addr) As16() (a16 [16]byte) {
|
func (ip Addr) As16() (a16 [16]byte) {
|
||||||
bePutUint64(a16[:8], ip.addr.hi)
|
byteorder.BePutUint64(a16[:8], ip.addr.hi)
|
||||||
bePutUint64(a16[8:], ip.addr.lo)
|
byteorder.BePutUint64(a16[8:], ip.addr.lo)
|
||||||
return a16
|
return a16
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -686,7 +687,7 @@ func (ip Addr) As16() (a16 [16]byte) {
|
|||||||
// Note that 0.0.0.0 is not the zero Addr.
|
// Note that 0.0.0.0 is not the zero Addr.
|
||||||
func (ip Addr) As4() (a4 [4]byte) {
|
func (ip Addr) As4() (a4 [4]byte) {
|
||||||
if ip.z == z4 || ip.Is4In6() {
|
if ip.z == z4 || ip.Is4In6() {
|
||||||
bePutUint32(a4[:], uint32(ip.addr.lo))
|
byteorder.BePutUint32(a4[:], uint32(ip.addr.lo))
|
||||||
return a4
|
return a4
|
||||||
}
|
}
|
||||||
if ip.z == z0 {
|
if ip.z == z0 {
|
||||||
@ -702,12 +703,12 @@ func (ip Addr) AsSlice() []byte {
|
|||||||
return nil
|
return nil
|
||||||
case z4:
|
case z4:
|
||||||
var ret [4]byte
|
var ret [4]byte
|
||||||
bePutUint32(ret[:], uint32(ip.addr.lo))
|
byteorder.BePutUint32(ret[:], uint32(ip.addr.lo))
|
||||||
return ret[:]
|
return ret[:]
|
||||||
default:
|
default:
|
||||||
var ret [16]byte
|
var ret [16]byte
|
||||||
bePutUint64(ret[:8], ip.addr.hi)
|
byteorder.BePutUint64(ret[:8], ip.addr.hi)
|
||||||
bePutUint64(ret[8:], ip.addr.lo)
|
byteorder.BePutUint64(ret[8:], ip.addr.lo)
|
||||||
return ret[:]
|
return ret[:]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -987,12 +988,12 @@ func (ip Addr) marshalBinaryWithTrailingBytes(trailingBytes int) []byte {
|
|||||||
b = make([]byte, trailingBytes)
|
b = make([]byte, trailingBytes)
|
||||||
case z4:
|
case z4:
|
||||||
b = make([]byte, 4+trailingBytes)
|
b = make([]byte, 4+trailingBytes)
|
||||||
bePutUint32(b, uint32(ip.addr.lo))
|
byteorder.BePutUint32(b, uint32(ip.addr.lo))
|
||||||
default:
|
default:
|
||||||
z := ip.Zone()
|
z := ip.Zone()
|
||||||
b = make([]byte, 16+len(z)+trailingBytes)
|
b = make([]byte, 16+len(z)+trailingBytes)
|
||||||
bePutUint64(b[:8], ip.addr.hi)
|
byteorder.BePutUint64(b[:8], ip.addr.hi)
|
||||||
bePutUint64(b[8:], ip.addr.lo)
|
byteorder.BePutUint64(b[8:], ip.addr.lo)
|
||||||
copy(b[16:], z)
|
copy(b[16:], z)
|
||||||
}
|
}
|
||||||
return b
|
return b
|
||||||
@ -1209,7 +1210,7 @@ func (p *AddrPort) UnmarshalText(text []byte) error {
|
|||||||
// containing the port in little-endian.
|
// containing the port in little-endian.
|
||||||
func (p AddrPort) MarshalBinary() ([]byte, error) {
|
func (p AddrPort) MarshalBinary() ([]byte, error) {
|
||||||
b := p.Addr().marshalBinaryWithTrailingBytes(2)
|
b := p.Addr().marshalBinaryWithTrailingBytes(2)
|
||||||
lePutUint16(b[len(b)-2:], p.Port())
|
byteorder.LePutUint16(b[len(b)-2:], p.Port())
|
||||||
return b, nil
|
return b, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1224,7 +1225,7 @@ func (p *AddrPort) UnmarshalBinary(b []byte) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
*p = AddrPortFrom(addr, leUint16(b[len(b)-2:]))
|
*p = AddrPortFrom(addr, byteorder.LeUint16(b[len(b)-2:]))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user