v1.14.0
ADDED: * iox package * mapsx package * netx/inetcksum package
This commit is contained in:
@@ -10,11 +10,20 @@ const (
|
||||
)
|
||||
|
||||
const (
|
||||
// cksumMask is AND'd with a checksum to get the "carried ones".
|
||||
/*
|
||||
cksumMask is AND'd with a checksum to get the "carried ones"
|
||||
(the lower 16 bits before folding carries).
|
||||
*/
|
||||
cksumMask uint32 = 0x0000ffff
|
||||
// cksumShift is used in the "carried-ones folding".
|
||||
/*
|
||||
cksumShift is used in the "carried-ones folding";
|
||||
it's the number of bits to right-shift the carry-over.
|
||||
*/
|
||||
cksumShift uint32 = 0x00000010
|
||||
// padShift is used to "pad out" a checksum for odd-length buffers by left-shifting.
|
||||
/*
|
||||
padShift is used to "pad out" a checksum for odd-length buffers by left-shifting.
|
||||
It positions the high-byte of a 16-byte "word" (big-endian, as per ord below).
|
||||
*/
|
||||
padShift uint32 = 0x00000008
|
||||
)
|
||||
|
||||
|
||||
@@ -25,6 +25,9 @@ safety and no data retention, which can be used as a:
|
||||
* [io.StringWriter]
|
||||
* [io.Writer]
|
||||
|
||||
If you don't need all these interfaces, a reasonable alternative may be
|
||||
to use gVisor's [gvisor.dev/gvisor/pkg/tcpip/checksum] instead.
|
||||
|
||||
[RFC 1071]: https://datatracker.ietf.org/doc/html/rfc1071
|
||||
[RFC 1141]: https://datatracker.ietf.org/doc/html/rfc1141
|
||||
[RFC 1624]: https://datatracker.ietf.org/doc/html/rfc1624
|
||||
|
||||
@@ -7,8 +7,9 @@ import (
|
||||
// New returns a new initialized [InetChecksum]. It will never panic.
|
||||
func New() (i *InetChecksum) {
|
||||
|
||||
i = &InetChecksum{}
|
||||
_ = i.Aligned()
|
||||
i = &InetChecksum{
|
||||
aligned: true,
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
@@ -21,15 +22,14 @@ b may be nil or 0-length; this will not cause an error.
|
||||
func NewFromBytes(b []byte) (i *InetChecksum, copied int, err error) {
|
||||
|
||||
var cksum InetChecksum
|
||||
var cptr *InetChecksum = &cksum
|
||||
|
||||
cksum.aligned = true
|
||||
|
||||
if b != nil && len(b) > 0 {
|
||||
if copied, err = cksum.Write(b); err != nil {
|
||||
if copied, err = cptr.Write(b); err != nil {
|
||||
return
|
||||
}
|
||||
_ = i.Aligned()
|
||||
} else {
|
||||
i = New()
|
||||
return
|
||||
}
|
||||
|
||||
i = &cksum
|
||||
@@ -48,7 +48,64 @@ func NewFromBuf(buf io.Reader) (i *InetChecksum, copied int64, err error) {
|
||||
|
||||
var cksum InetChecksum
|
||||
|
||||
_ = i.Aligned()
|
||||
cksum.aligned = true
|
||||
|
||||
if buf != nil {
|
||||
if copied, err = io.Copy(&cksum, buf); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
i = &cksum
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// NewSimple returns a new initialized [InetChecksumSimple]. It will never panic.
|
||||
func NewSimple() (i *InetChecksumSimple) {
|
||||
|
||||
i = &InetChecksumSimple{
|
||||
aligned: true,
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
/*
|
||||
NewSimpleFromBytes returns a new [InetChecksumSimple] initialized with explicit bytes.
|
||||
|
||||
b may be nil or 0-length; this will not cause an error.
|
||||
*/
|
||||
func NewSimpleFromBytes(b []byte) (i *InetChecksumSimple, copied int, err error) {
|
||||
|
||||
var cksum InetChecksumSimple
|
||||
var cptr *InetChecksumSimple = &cksum
|
||||
|
||||
cksum.aligned = true
|
||||
|
||||
if b != nil && len(b) > 0 {
|
||||
if copied, err = cptr.Write(b); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
i = &cksum
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
/*
|
||||
NewSimpleFromBuf returns an [InetChecksumSimple] from a specified [io.Reader].
|
||||
|
||||
buf may be nil. If it isn't, NewSimpleFromBuf will call [io.Copy] on buf.
|
||||
Note that this may exhaust your passed buf or advance its current seek position/offset,
|
||||
depending on its type.
|
||||
*/
|
||||
func NewSimpleFromBuf(buf io.Reader) (i *InetChecksumSimple, copied int64, err error) {
|
||||
|
||||
var cksum InetChecksumSimple
|
||||
|
||||
cksum.aligned = true
|
||||
|
||||
if buf != nil {
|
||||
if copied, err = io.Copy(&cksum, buf); err != nil {
|
||||
|
||||
@@ -22,7 +22,7 @@ func (i *InetChecksum) Aligned() (aligned bool) {
|
||||
defer i.alignLock.Unlock()
|
||||
|
||||
i.bufLock.RLock()
|
||||
aligned = i.buf.Len()&2 == 0
|
||||
aligned = i.buf.Len()%2 == 0
|
||||
i.bufLock.RUnlock()
|
||||
|
||||
i.aligned = aligned
|
||||
@@ -113,7 +113,7 @@ func (i *InetChecksum) Reset() {
|
||||
i.sumLock.Lock()
|
||||
i.lastLock.Lock()
|
||||
|
||||
i.aligned = false
|
||||
i.aligned = true
|
||||
i.alignLock.Unlock()
|
||||
|
||||
i.buf.Reset()
|
||||
@@ -308,7 +308,7 @@ func (i *InetChecksum) WriteByte(c byte) (err error) {
|
||||
}
|
||||
|
||||
if !i.disabledBuf {
|
||||
if err = i.WriteByte(c); err != nil {
|
||||
if err = i.buf.WriteByte(c); err != nil {
|
||||
i.sum = origSum
|
||||
i.aligned = origAligned
|
||||
i.last = origLast
|
||||
|
||||
@@ -27,7 +27,7 @@ func (i *InetChecksumSimple) Reset() {
|
||||
|
||||
i.last = 0x00
|
||||
i.sum = 0
|
||||
i.last = 0x00
|
||||
i.aligned = true
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -17,8 +17,8 @@ type (
|
||||
If [InetChecksum.Aligned] returns false, the checksum result of an
|
||||
[InetChecksum.Sum] or [InetChecksum.Sum16] (or any other operation
|
||||
returning a sum) will INCLUDE THE PAD NULL BYTE (which is only
|
||||
applied *at the time of the Sum/Sum32 call) and is NOT applied to
|
||||
the persistent underlying storage.
|
||||
applied *at the time of the Sum/Sum32 call* and is NOT applied to
|
||||
the persistent underlying storage).
|
||||
|
||||
InetChecksum differs from [InetChecksumSimple] in that it:
|
||||
|
||||
|
||||
Reference in New Issue
Block a user