]> Cypherpunks repositories - gostls13.git/commitdiff
hash: implement the encoding.BinaryAppender interface
authorapocelipes <seve3r@outlook.com>
Tue, 6 Aug 2024 21:58:38 +0000 (21:58 +0000)
committerGopher Robot <gobot@golang.org>
Wed, 7 Aug 2024 17:23:15 +0000 (17:23 +0000)
For #62384

Change-Id: Ia6de028741e43449bcf54ba73ec9b0cad4d4e88a
GitHub-Last-Rev: 192f389d463d372a338dca82827a871888a53bb0
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>
12 files changed:
doc/next/6-stdlib/99-minor/hash/adler32/62384.md [new file with mode: 0644]
doc/next/6-stdlib/99-minor/hash/crc32/62384.md [new file with mode: 0644]
doc/next/6-stdlib/99-minor/hash/crc64/62384.md [new file with mode: 0644]
doc/next/6-stdlib/99-minor/hash/fnv/62384.md [new file with mode: 0644]
src/hash/adler32/adler32.go
src/hash/adler32/adler32_test.go
src/hash/crc32/crc32.go
src/hash/crc32/crc32_test.go
src/hash/crc64/crc64.go
src/hash/crc64/crc64_test.go
src/hash/fnv/fnv.go
src/hash/fnv/fnv_test.go

diff --git a/doc/next/6-stdlib/99-minor/hash/adler32/62384.md b/doc/next/6-stdlib/99-minor/hash/adler32/62384.md
new file mode 100644 (file)
index 0000000..a584db8
--- /dev/null
@@ -0,0 +1 @@
+The value returned by [New] now also implements the [encoding.BinaryAppender] interface.
diff --git a/doc/next/6-stdlib/99-minor/hash/crc32/62384.md b/doc/next/6-stdlib/99-minor/hash/crc32/62384.md
new file mode 100644 (file)
index 0000000..0e835c2
--- /dev/null
@@ -0,0 +1 @@
+The values returned by [New] and [NewIEEE] now also implement the [encoding.BinaryAppender] interface.
diff --git a/doc/next/6-stdlib/99-minor/hash/crc64/62384.md b/doc/next/6-stdlib/99-minor/hash/crc64/62384.md
new file mode 100644 (file)
index 0000000..a584db8
--- /dev/null
@@ -0,0 +1 @@
+The value returned by [New] now also implements the [encoding.BinaryAppender] interface.
diff --git a/doc/next/6-stdlib/99-minor/hash/fnv/62384.md b/doc/next/6-stdlib/99-minor/hash/fnv/62384.md
new file mode 100644 (file)
index 0000000..68ec6f3
--- /dev/null
@@ -0,0 +1 @@
+The values returned by [New32], [New32a], [New64], [New64a], [New128] and [New128a] now also implement the [encoding.BinaryAppender] interface.
index ed9ccad910c098a6a4dbe9b11bf2e712236aa2a7..88b4ccf2fedbb683ebc65515da11f2af1c11d2d0 100644 (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")
index 6bac80250762652e0396a19403cda4a063005323..ebb9a438a63f878abc131225589271bfb46ab356 100644 (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
index 3964646b277352e403bd40896ff3dabf4cc25efe..b659959959c6fa0b9e88644a410c103f4e01d782 100644 (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")
index 5a3e134cf7a5957284042168d64744a9bf7b08a8..10c28f9533bb28c83a88635efc035844956c4bbf 100644 (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
index 4cdb4c7e77f777c96adf1fcf47b0d38a2e7c507b..bdfd82ed314dda14c947799c037d2d93a335ee84 100644 (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")
index 9cf602c82f3eebb466ef8ea65eb2036369f61ffe..06c428c81f007993ce1653455c10d7ccad7fae1c 100644 (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
index bf95bb32a3c6e040fcdf9bc051fefd6a59539715..e7463795cdd6eec695dd063f4a7459ad138b7c84 100644 (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")
index 7b1f7a32eaac090c6900d04abf0385e4c20516d1..4219460e46fc8556adc523c1a13f3127f2ad0594 100644 (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