1
0
mirror of https://github.com/golang/go synced 2024-11-26 03:57:57 -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 marshaledSize = len(magic) + 4
) )
func (d *digest) MarshalBinary() ([]byte, error) { func (d *digest) AppendBinary(b []byte) ([]byte, error) {
b := make([]byte, 0, marshaledSize)
b = append(b, magic...) b = append(b, magic...)
b = byteorder.BeAppendUint32(b, uint32(*d)) b = byteorder.BeAppendUint32(b, uint32(*d))
return b, nil return b, nil
} }
func (d *digest) MarshalBinary() ([]byte, error) {
return d.AppendBinary(make([]byte, 0, marshaledSize))
}
func (d *digest) UnmarshalBinary(b []byte) error { func (d *digest) UnmarshalBinary(b []byte) error {
if len(b) < len(magic) || string(b[:len(magic)]) != magic { if len(b) < len(magic) || string(b[:len(magic)]) != magic {
return errors.New("hash/adler32: invalid hash state identifier") return errors.New("hash/adler32: invalid hash state identifier")

View File

@ -103,11 +103,23 @@ func TestGoldenMarshal(t *testing.T) {
continue 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 { if string(state) != g.halfState {
t.Errorf("checksum(%q) state = %q, want %q", g.in, state, g.halfState) t.Errorf("checksum(%q) state = %q, want %q", g.in, state, g.halfState)
continue 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 { if err := h2.(encoding.BinaryUnmarshaler).UnmarshalBinary(state); err != nil {
t.Errorf("could not unmarshal: %v", err) t.Errorf("could not unmarshal: %v", err)
continue continue

View File

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

View File

@ -133,11 +133,23 @@ func TestGoldenMarshal(t *testing.T) {
continue 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 { if string(state) != g.halfStateIEEE {
t.Errorf("IEEE(%q) state = %q, want %q", g.in, state, g.halfStateIEEE) t.Errorf("IEEE(%q) state = %q, want %q", g.in, state, g.halfStateIEEE)
continue 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 { if err := h2.(encoding.BinaryUnmarshaler).UnmarshalBinary(state); err != nil {
t.Errorf("could not unmarshal: %v", err) t.Errorf("could not unmarshal: %v", err)
continue continue
@ -165,11 +177,23 @@ func TestGoldenMarshal(t *testing.T) {
continue 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 { if string(state) != g.halfStateCastagnoli {
t.Errorf("Castagnoli(%q) state = %q, want %q", g.in, state, g.halfStateCastagnoli) t.Errorf("Castagnoli(%q) state = %q, want %q", g.in, state, g.halfStateCastagnoli)
continue 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 { if err := h2.(encoding.BinaryUnmarshaler).UnmarshalBinary(state); err != nil {
t.Errorf("could not unmarshal: %v", err) t.Errorf("could not unmarshal: %v", err)
continue continue

View File

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

View File

@ -88,11 +88,23 @@ func TestGoldenMarshal(t *testing.T) {
continue 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 { if string(state) != g.halfStateISO {
t.Errorf("ISO crc64(%q) state = %q, want %q", g.in, state, g.halfStateISO) t.Errorf("ISO crc64(%q) state = %q, want %q", g.in, state, g.halfStateISO)
continue 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 { if err := h2.(encoding.BinaryUnmarshaler).UnmarshalBinary(state); err != nil {
t.Errorf("could not unmarshal: %v", err) t.Errorf("could not unmarshal: %v", err)
continue continue
@ -120,11 +132,23 @@ func TestGoldenMarshal(t *testing.T) {
continue 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 { if string(state) != g.halfStateECMA {
t.Errorf("ECMA crc64(%q) state = %q, want %q", g.in, state, g.halfStateECMA) t.Errorf("ECMA crc64(%q) state = %q, want %q", g.in, state, g.halfStateECMA)
continue 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 { if err := h2.(encoding.BinaryUnmarshaler).UnmarshalBinary(state); err != nil {
t.Errorf("could not unmarshal: %v", err) t.Errorf("could not unmarshal: %v", err)
continue continue

View File

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

View File

@ -128,11 +128,23 @@ func TestGoldenMarshal(t *testing.T) {
continue 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 { if string(state) != g.halfState {
t.Errorf("checksum(%q) state = %q, want %q", g.in, state, g.halfState) t.Errorf("checksum(%q) state = %q, want %q", g.in, state, g.halfState)
continue 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 { if err := h2.(encoding.BinaryUnmarshaler).UnmarshalBinary(state); err != nil {
t.Errorf("could not unmarshal: %v", err) t.Errorf("could not unmarshal: %v", err)
continue continue