120 lines
2.2 KiB
Go
120 lines
2.2 KiB
Go
package inetcksum
|
|
|
|
import (
|
|
`io`
|
|
)
|
|
|
|
// New returns a new initialized [InetChecksum]. It will never panic.
|
|
func New() (i *InetChecksum) {
|
|
|
|
i = &InetChecksum{
|
|
aligned: true,
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
/*
|
|
NewFromBytes returns a new [InetChecksum] initialized with explicit bytes.
|
|
|
|
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 = cptr.Write(b); err != nil {
|
|
return
|
|
}
|
|
}
|
|
|
|
i = &cksum
|
|
|
|
return
|
|
}
|
|
|
|
/*
|
|
NewFromBuf returns an [InetChecksum] from a specified [io.Reader].
|
|
|
|
buf may be nil. If it isn't, NewFromBuf 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 NewFromBuf(buf io.Reader) (i *InetChecksum, copied int64, err error) {
|
|
|
|
var cksum InetChecksum
|
|
|
|
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 {
|
|
return
|
|
}
|
|
}
|
|
|
|
i = &cksum
|
|
|
|
return
|
|
}
|