import (
"errors"
"hash"
+ "internal/byteorder"
)
const (
func (d *digest) MarshalBinary() ([]byte, error) {
b := make([]byte, 0, marshaledSize)
b = append(b, magic...)
- b = appendUint32(b, uint32(*d))
+ b = byteorder.BeAppendUint32(b, uint32(*d))
return b, nil
}
if len(b) != marshaledSize {
return errors.New("hash/adler32: invalid hash state size")
}
- *d = digest(readUint32(b[len(magic):]))
+ *d = digest(byteorder.BeUint32(b[len(magic):]))
return nil
}
-// appendUint32 is semantically the same as [binary.BigEndian.AppendUint32]
-// We copied this function because we can not import "encoding/binary" here.
-func appendUint32(b []byte, x uint32) []byte {
- return append(b,
- byte(x>>24),
- byte(x>>16),
- byte(x>>8),
- byte(x),
- )
-}
-
-// readUint32 is semantically the same as [binary.BigEndian.Uint32]
-// We copied this function because we can not import "encoding/binary" here.
-func readUint32(b []byte) uint32 {
- _ = b[3]
- return uint32(b[3]) | uint32(b[2])<<8 | uint32(b[1])<<16 | uint32(b[0])<<24
-}
-
// Add p to the running checksum d.
func update(d digest, p []byte) digest {
s1, s2 := uint32(d&0xffff), uint32(d>>16)
import (
"errors"
"hash"
+ "internal/byteorder"
"sync"
"sync/atomic"
)
func (d *digest) MarshalBinary() ([]byte, error) {
b := make([]byte, 0, marshaledSize)
b = append(b, magic...)
- b = appendUint32(b, tableSum(d.tab))
- b = appendUint32(b, d.crc)
+ b = byteorder.BeAppendUint32(b, tableSum(d.tab))
+ b = byteorder.BeAppendUint32(b, d.crc)
return b, nil
}
if len(b) != marshaledSize {
return errors.New("hash/crc32: invalid hash state size")
}
- if tableSum(d.tab) != readUint32(b[4:]) {
+ if tableSum(d.tab) != byteorder.BeUint32(b[4:]) {
return errors.New("hash/crc32: tables do not match")
}
- d.crc = readUint32(b[8:])
+ d.crc = byteorder.BeUint32(b[8:])
return nil
}
-// appendUint32 is semantically the same as [binary.BigEndian.AppendUint32]
-// We copied this function because we can not import "encoding/binary" here.
-func appendUint32(b []byte, x uint32) []byte {
- return append(b,
- byte(x>>24),
- byte(x>>16),
- byte(x>>8),
- byte(x),
- )
-}
-
-// readUint32 is semantically the same as [binary.BigEndian.Uint32]
-// We copied this function because we can not import "encoding/binary" here.
-func readUint32(b []byte) uint32 {
- _ = b[3]
- return uint32(b[3]) | uint32(b[2])<<8 | uint32(b[1])<<16 | uint32(b[0])<<24
-}
-
func update(crc uint32, tab *Table, p []byte, checkInitIEEE bool) uint32 {
switch {
case haveCastagnoli.Load() && tab == castagnoliTable:
b := a[:0]
if t != nil {
for _, x := range t {
- b = appendUint32(b, x)
+ b = byteorder.BeAppendUint32(b, x)
}
}
return ChecksumIEEE(b)
import (
"errors"
"hash"
+ "internal/byteorder"
"sync"
)
func (d *digest) MarshalBinary() ([]byte, error) {
b := make([]byte, 0, marshaledSize)
b = append(b, magic...)
- b = appendUint64(b, tableSum(d.tab))
- b = appendUint64(b, d.crc)
+ b = byteorder.BeAppendUint64(b, tableSum(d.tab))
+ b = byteorder.BeAppendUint64(b, d.crc)
return b, nil
}
if len(b) != marshaledSize {
return errors.New("hash/crc64: invalid hash state size")
}
- if tableSum(d.tab) != readUint64(b[4:]) {
+ if tableSum(d.tab) != byteorder.BeUint64(b[4:]) {
return errors.New("hash/crc64: tables do not match")
}
- d.crc = readUint64(b[12:])
+ d.crc = byteorder.BeUint64(b[12:])
return nil
}
-// appendUint64 is semantically the same as [binary.BigEndian.AppendUint64]
-// We copied this function because we can not import "encoding/binary" here.
-func appendUint64(b []byte, x uint64) []byte {
- return append(b,
- byte(x>>56),
- byte(x>>48),
- byte(x>>40),
- byte(x>>32),
- byte(x>>24),
- byte(x>>16),
- byte(x>>8),
- byte(x),
- )
-}
-
-// readUint64 is semantically the same as [binary.BigEndian.Uint64]
-// We copied this function because we can not import "encoding/binary" here.
-func readUint64(b []byte) uint64 {
- _ = b[7]
- 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 update(crc uint64, tab *Table, p []byte) uint64 {
buildSlicing8TablesOnce()
crc = ^crc
b := a[:0]
if t != nil {
for _, x := range t {
- b = appendUint64(b, x)
+ b = byteorder.BeAppendUint64(b, x)
}
}
return Checksum(b, MakeTable(ISO))
import (
"errors"
"hash"
+ "internal/byteorder"
"math/bits"
)
func (s *sum32) MarshalBinary() ([]byte, error) {
b := make([]byte, 0, marshaledSize32)
b = append(b, magic32...)
- b = appendUint32(b, uint32(*s))
+ b = byteorder.BeAppendUint32(b, uint32(*s))
return b, nil
}
func (s *sum32a) MarshalBinary() ([]byte, error) {
b := make([]byte, 0, marshaledSize32)
b = append(b, magic32a...)
- b = appendUint32(b, uint32(*s))
+ b = byteorder.BeAppendUint32(b, uint32(*s))
return b, nil
}
func (s *sum64) MarshalBinary() ([]byte, error) {
b := make([]byte, 0, marshaledSize64)
b = append(b, magic64...)
- b = appendUint64(b, uint64(*s))
+ b = byteorder.BeAppendUint64(b, uint64(*s))
return b, nil
}
func (s *sum64a) MarshalBinary() ([]byte, error) {
b := make([]byte, 0, marshaledSize64)
b = append(b, magic64a...)
- b = appendUint64(b, uint64(*s))
+ b = byteorder.BeAppendUint64(b, uint64(*s))
return b, nil
}
func (s *sum128) MarshalBinary() ([]byte, error) {
b := make([]byte, 0, marshaledSize128)
b = append(b, magic128...)
- b = appendUint64(b, s[0])
- b = appendUint64(b, s[1])
+ b = byteorder.BeAppendUint64(b, s[0])
+ b = byteorder.BeAppendUint64(b, s[1])
return b, nil
}
func (s *sum128a) MarshalBinary() ([]byte, error) {
b := make([]byte, 0, marshaledSize128)
b = append(b, magic128a...)
- b = appendUint64(b, s[0])
- b = appendUint64(b, s[1])
+ b = byteorder.BeAppendUint64(b, s[0])
+ b = byteorder.BeAppendUint64(b, s[1])
return b, nil
}
if len(b) != marshaledSize32 {
return errors.New("hash/fnv: invalid hash state size")
}
- *s = sum32(readUint32(b[4:]))
+ *s = sum32(byteorder.BeUint32(b[4:]))
return nil
}
if len(b) != marshaledSize32 {
return errors.New("hash/fnv: invalid hash state size")
}
- *s = sum32a(readUint32(b[4:]))
+ *s = sum32a(byteorder.BeUint32(b[4:]))
return nil
}
if len(b) != marshaledSize64 {
return errors.New("hash/fnv: invalid hash state size")
}
- *s = sum64(readUint64(b[4:]))
+ *s = sum64(byteorder.BeUint64(b[4:]))
return nil
}
if len(b) != marshaledSize64 {
return errors.New("hash/fnv: invalid hash state size")
}
- *s = sum64a(readUint64(b[4:]))
+ *s = sum64a(byteorder.BeUint64(b[4:]))
return nil
}
if len(b) != marshaledSize128 {
return errors.New("hash/fnv: invalid hash state size")
}
- s[0] = readUint64(b[4:])
- s[1] = readUint64(b[12:])
+ s[0] = byteorder.BeUint64(b[4:])
+ s[1] = byteorder.BeUint64(b[12:])
return nil
}
if len(b) != marshaledSize128 {
return errors.New("hash/fnv: invalid hash state size")
}
- s[0] = readUint64(b[4:])
- s[1] = readUint64(b[12:])
+ s[0] = byteorder.BeUint64(b[4:])
+ s[1] = byteorder.BeUint64(b[12:])
return nil
}
-
-// readUint32 is semantically the same as [binary.BigEndian.Uint32]
-// We copied this function because we can not import "encoding/binary" here.
-func readUint32(b []byte) uint32 {
- _ = b[3]
- return uint32(b[3]) | uint32(b[2])<<8 | uint32(b[1])<<16 | uint32(b[0])<<24
-}
-
-// appendUint32 is semantically the same as [binary.BigEndian.AppendUint32]
-// We copied this function because we can not import "encoding/binary" here.
-func appendUint32(b []byte, x uint32) []byte {
- return append(b,
- byte(x>>24),
- byte(x>>16),
- byte(x>>8),
- byte(x),
- )
-}
-
-// appendUint64 is semantically the same as [binary.BigEndian.AppendUint64]
-// We copied this function because we can not import "encoding/binary" here.
-func appendUint64(b []byte, x uint64) []byte {
- return append(b,
- byte(x>>56),
- byte(x>>48),
- byte(x>>40),
- byte(x>>32),
- byte(x>>24),
- byte(x>>16),
- byte(x>>8),
- byte(x),
- )
-}
-
-// readUint64 is semantically the same as [binary.BigEndian.Uint64]
-// We copied this function because we can not import "encoding/binary" here.
-func readUint64(b []byte) uint64 {
- _ = b[7]
- 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
-}
import (
"crypto/rand"
+ "internal/byteorder"
"math/bits"
)
func randUint64() uint64 {
buf := make([]byte, 8)
_, _ = rand.Read(buf)
- return leUint64(buf)
+ return byteorder.LeUint64(buf)
}
// This is a port of wyhash implementation in runtime/hash64.go,
}
func r4(p []byte) uint64 {
- return uint64(leUint32(p))
+ return uint64(byteorder.LeUint32(p))
}
func r8(p []byte) uint64 {
- return leUint64(p)
+ return byteorder.LeUint64(p)
}
func mix(a, b uint64) uint64 {
hi, lo := bits.Mul64(a, b)
return hi ^ lo
}
-
-func leUint32(b []byte) uint32 {
- _ = b[3] // bounds check hint to compiler; see golang.org/issue/14808
- return uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24
-}
-
-func leUint64(b []byte) uint64 {
- _ = b[7] // bounds check hint to compiler; see golang.org/issue/14808
- return uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 |
- uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56
-}