hash: simplify binary operations

Can not use encoding/binary in hash packages because of import cycles.

So just keep the appendUint and readUint helper functions same as that
in the encoding/binary standard package.

Updates #63719
This commit is contained in:
apocelipes 2023-10-26 10:02:33 +09:00
parent 555af99bcc
commit f334fee408
4 changed files with 53 additions and 38 deletions

View File

@ -74,16 +74,19 @@ func (d *digest) UnmarshalBinary(b []byte) error {
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 {
a := [4]byte{
byte(x >> 24),
byte(x >> 16),
byte(x >> 8),
return append(b,
byte(x>>24),
byte(x>>16),
byte(x>>8),
byte(x),
}
return append(b, a[:]...)
)
}
// 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

View File

@ -191,16 +191,19 @@ func (d *digest) UnmarshalBinary(b []byte) error {
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 {
a := [4]byte{
byte(x >> 24),
byte(x >> 16),
byte(x >> 8),
return append(b,
byte(x>>24),
byte(x>>16),
byte(x>>8),
byte(x),
}
return append(b, a[:]...)
)
}
// 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

View File

@ -132,20 +132,23 @@ func (d *digest) UnmarshalBinary(b []byte) error {
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 {
a := [8]byte{
byte(x >> 56),
byte(x >> 48),
byte(x >> 40),
byte(x >> 32),
byte(x >> 24),
byte(x >> 16),
byte(x >> 8),
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),
}
return append(b, a[:]...)
)
}
// 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 |

View File

@ -335,35 +335,41 @@ func (s *sum128a) UnmarshalBinary(b []byte) error {
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 {
a := [4]byte{
byte(x >> 24),
byte(x >> 16),
byte(x >> 8),
return append(b,
byte(x>>24),
byte(x>>16),
byte(x>>8),
byte(x),
}
return append(b, a[:]...)
)
}
// 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 {
a := [8]byte{
byte(x >> 56),
byte(x >> 48),
byte(x >> 40),
byte(x >> 32),
byte(x >> 24),
byte(x >> 16),
byte(x >> 8),
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),
}
return append(b, a[:]...)
)
}
// 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 |