1
0
mirror of https://github.com/golang/go synced 2024-11-22 10:04:42 -07:00

hash: implement the encoding.BinaryAppender interface

For #62384

Change-Id: Ia6de028741e43449bcf54ba73ec9b0cad4d4e88a
GitHub-Last-Rev: 192f389d46
GitHub-Pull-Request: golang/go#68738
Reviewed-on: https://go-review.googlesource.com/c/go/+/603255
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: David Chase <drchase@google.com>
This commit is contained in:
apocelipes 2024-08-06 21:58:38 +00:00 committed by Gopher Robot
parent 5a81570bf2
commit b696250e5f
12 changed files with 122 additions and 18 deletions

View File

@ -0,0 +1 @@
The value returned by [New] now also implements the [encoding.BinaryAppender] interface.

View File

@ -0,0 +1 @@
The values returned by [New] and [NewIEEE] now also implement the [encoding.BinaryAppender] interface.

View File

@ -0,0 +1 @@
The value returned by [New] now also implements the [encoding.BinaryAppender] interface.

View File

@ -0,0 +1 @@
The values returned by [New32], [New32a], [New64], [New64a], [New128] and [New128a] now also implement the [encoding.BinaryAppender] interface.

View File

@ -57,13 +57,16 @@ const (
marshaledSize = len(magic) + 4
)
func (d *digest) MarshalBinary() ([]byte, error) {
b := make([]byte, 0, marshaledSize)
func (d *digest) AppendBinary(b []byte) ([]byte, error) {
b = append(b, magic...)
b = byteorder.BeAppendUint32(b, uint32(*d))
return b, nil
}
func (d *digest) MarshalBinary() ([]byte, error) {
return d.AppendBinary(make([]byte, 0, marshaledSize))
}
func (d *digest) UnmarshalBinary(b []byte) error {
if len(b) < len(magic) || string(b[:len(magic)]) != magic {
return errors.New("hash/adler32: invalid hash state identifier")

View File

@ -103,11 +103,23 @@ func TestGoldenMarshal(t *testing.T) {
continue
}
stateAppend, err := h.(encoding.BinaryAppender).AppendBinary(make([]byte, 4, 32))
if err != nil {
t.Errorf("could not marshal: %v", err)
continue
}
stateAppend = stateAppend[4:]
if string(state) != g.halfState {
t.Errorf("checksum(%q) state = %q, want %q", g.in, state, g.halfState)
continue
}
if string(stateAppend) != g.halfState {
t.Errorf("checksum(%q) state = %q, want %q", g.in, stateAppend, g.halfState)
continue
}
if err := h2.(encoding.BinaryUnmarshaler).UnmarshalBinary(state); err != nil {
t.Errorf("could not unmarshal: %v", err)
continue

View File

@ -170,14 +170,18 @@ const (
marshaledSize = len(magic) + 4 + 4
)
func (d *digest) MarshalBinary() ([]byte, error) {
b := make([]byte, 0, marshaledSize)
func (d *digest) AppendBinary(b []byte) ([]byte, error) {
b = append(b, magic...)
b = byteorder.BeAppendUint32(b, tableSum(d.tab))
b = byteorder.BeAppendUint32(b, d.crc)
return b, nil
}
func (d *digest) MarshalBinary() ([]byte, error) {
return d.AppendBinary(make([]byte, 0, marshaledSize))
}
func (d *digest) UnmarshalBinary(b []byte) error {
if len(b) < len(magic) || string(b[:len(magic)]) != magic {
return errors.New("hash/crc32: invalid hash state identifier")

View File

@ -133,11 +133,23 @@ func TestGoldenMarshal(t *testing.T) {
continue
}
stateAppend, err := h.(encoding.BinaryAppender).AppendBinary(make([]byte, 4, 32))
if err != nil {
t.Errorf("could not marshal: %v", err)
continue
}
stateAppend = stateAppend[4:]
if string(state) != g.halfStateIEEE {
t.Errorf("IEEE(%q) state = %q, want %q", g.in, state, g.halfStateIEEE)
continue
}
if string(stateAppend) != g.halfStateIEEE {
t.Errorf("IEEE(%q) state = %q, want %q", g.in, stateAppend, g.halfStateIEEE)
continue
}
if err := h2.(encoding.BinaryUnmarshaler).UnmarshalBinary(state); err != nil {
t.Errorf("could not unmarshal: %v", err)
continue
@ -165,11 +177,23 @@ func TestGoldenMarshal(t *testing.T) {
continue
}
stateAppend, err := h.(encoding.BinaryAppender).AppendBinary(make([]byte, 4, 32))
if err != nil {
t.Errorf("could not marshal: %v", err)
continue
}
stateAppend = stateAppend[4:]
if string(state) != g.halfStateCastagnoli {
t.Errorf("Castagnoli(%q) state = %q, want %q", g.in, state, g.halfStateCastagnoli)
continue
}
if string(stateAppend) != g.halfStateCastagnoli {
t.Errorf("Castagnoli(%q) state = %q, want %q", g.in, stateAppend, g.halfStateCastagnoli)
continue
}
if err := h2.(encoding.BinaryUnmarshaler).UnmarshalBinary(state); err != nil {
t.Errorf("could not unmarshal: %v", err)
continue

View File

@ -111,14 +111,17 @@ const (
marshaledSize = len(magic) + 8 + 8
)
func (d *digest) MarshalBinary() ([]byte, error) {
b := make([]byte, 0, marshaledSize)
func (d *digest) AppendBinary(b []byte) ([]byte, error) {
b = append(b, magic...)
b = byteorder.BeAppendUint64(b, tableSum(d.tab))
b = byteorder.BeAppendUint64(b, d.crc)
return b, nil
}
func (d *digest) MarshalBinary() ([]byte, error) {
return d.AppendBinary(make([]byte, 0, marshaledSize))
}
func (d *digest) UnmarshalBinary(b []byte) error {
if len(b) < len(magic) || string(b[:len(magic)]) != magic {
return errors.New("hash/crc64: invalid hash state identifier")

View File

@ -88,11 +88,23 @@ func TestGoldenMarshal(t *testing.T) {
continue
}
stateAppend, err := h.(encoding.BinaryAppender).AppendBinary(make([]byte, 4, 32))
if err != nil {
t.Errorf("could not marshal: %v", err)
continue
}
stateAppend = stateAppend[4:]
if string(state) != g.halfStateISO {
t.Errorf("ISO crc64(%q) state = %q, want %q", g.in, state, g.halfStateISO)
continue
}
if string(stateAppend) != g.halfStateISO {
t.Errorf("ISO crc64(%q) state = %q, want %q", g.in, stateAppend, g.halfStateISO)
continue
}
if err := h2.(encoding.BinaryUnmarshaler).UnmarshalBinary(state); err != nil {
t.Errorf("could not unmarshal: %v", err)
continue
@ -120,11 +132,23 @@ func TestGoldenMarshal(t *testing.T) {
continue
}
stateAppend, err := h.(encoding.BinaryAppender).AppendBinary(make([]byte, 4, 32))
if err != nil {
t.Errorf("could not marshal: %v", err)
continue
}
stateAppend = stateAppend[4:]
if string(state) != g.halfStateECMA {
t.Errorf("ECMA crc64(%q) state = %q, want %q", g.in, state, g.halfStateECMA)
continue
}
if string(stateAppend) != g.halfStateECMA {
t.Errorf("ECMA crc64(%q) state = %q, want %q", g.in, stateAppend, g.halfStateECMA)
continue
}
if err := h2.(encoding.BinaryUnmarshaler).UnmarshalBinary(state); err != nil {
t.Errorf("could not unmarshal: %v", err)
continue

View File

@ -219,50 +219,68 @@ const (
marshaledSize128 = len(magic128) + 8*2
)
func (s *sum32) MarshalBinary() ([]byte, error) {
b := make([]byte, 0, marshaledSize32)
func (s *sum32) AppendBinary(b []byte) ([]byte, error) {
b = append(b, magic32...)
b = byteorder.BeAppendUint32(b, uint32(*s))
return b, nil
}
func (s *sum32a) MarshalBinary() ([]byte, error) {
b := make([]byte, 0, marshaledSize32)
func (s *sum32) MarshalBinary() ([]byte, error) {
return s.AppendBinary(make([]byte, 0, marshaledSize32))
}
func (s *sum32a) AppendBinary(b []byte) ([]byte, error) {
b = append(b, magic32a...)
b = byteorder.BeAppendUint32(b, uint32(*s))
return b, nil
}
func (s *sum64) MarshalBinary() ([]byte, error) {
b := make([]byte, 0, marshaledSize64)
func (s *sum32a) MarshalBinary() ([]byte, error) {
return s.AppendBinary(make([]byte, 0, marshaledSize32))
}
func (s *sum64) AppendBinary(b []byte) ([]byte, error) {
b = append(b, magic64...)
b = byteorder.BeAppendUint64(b, uint64(*s))
return b, nil
}
func (s *sum64a) MarshalBinary() ([]byte, error) {
b := make([]byte, 0, marshaledSize64)
func (s *sum64) MarshalBinary() ([]byte, error) {
return s.AppendBinary(make([]byte, 0, marshaledSize64))
}
func (s *sum64a) AppendBinary(b []byte) ([]byte, error) {
b = append(b, magic64a...)
b = byteorder.BeAppendUint64(b, uint64(*s))
return b, nil
}
func (s *sum128) MarshalBinary() ([]byte, error) {
b := make([]byte, 0, marshaledSize128)
func (s *sum64a) MarshalBinary() ([]byte, error) {
return s.AppendBinary(make([]byte, 0, marshaledSize64))
}
func (s *sum128) AppendBinary(b []byte) ([]byte, error) {
b = append(b, magic128...)
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)
func (s *sum128) MarshalBinary() ([]byte, error) {
return s.AppendBinary(make([]byte, 0, marshaledSize128))
}
func (s *sum128a) AppendBinary(b []byte) ([]byte, error) {
b = append(b, magic128a...)
b = byteorder.BeAppendUint64(b, s[0])
b = byteorder.BeAppendUint64(b, s[1])
return b, nil
}
func (s *sum128a) MarshalBinary() ([]byte, error) {
return s.AppendBinary(make([]byte, 0, marshaledSize128))
}
func (s *sum32) UnmarshalBinary(b []byte) error {
if len(b) < len(magic32) || string(b[:len(magic32)]) != magic32 {
return errors.New("hash/fnv: invalid hash state identifier")

View File

@ -128,11 +128,23 @@ func TestGoldenMarshal(t *testing.T) {
continue
}
stateAppend, err := h.(encoding.BinaryAppender).AppendBinary(make([]byte, 4, 32))
if err != nil {
t.Errorf("could not marshal: %v", err)
continue
}
stateAppend = stateAppend[4:]
if string(state) != g.halfState {
t.Errorf("checksum(%q) state = %q, want %q", g.in, state, g.halfState)
continue
}
if string(stateAppend) != g.halfState {
t.Errorf("checksum(%q) state = %q, want %q", g.in, stateAppend, g.halfState)
continue
}
if err := h2.(encoding.BinaryUnmarshaler).UnmarshalBinary(state); err != nil {
t.Errorf("could not unmarshal: %v", err)
continue