From: Joe Tsai Date: Wed, 14 Sep 2022 05:20:06 +0000 (-0700) Subject: compress/zlib: use binary.BigEndian consistently X-Git-Tag: go1.21rc1~1459 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=9a199d42dab00636f503c47ac4e02595765373f2;p=gostls13.git compress/zlib: use binary.BigEndian consistently One major reason to avoid binary.BigEndian is because the binary package includes a transitive dependency on reflect. See #54097. Given that writer.go already depends on the binary package, embrace use of it consistently where sensible. We should either embrace use of binary or fully avoid it. Change-Id: I5f2d27d0ed8cab5ac54be02362c7d33276dd4b9a Reviewed-on: https://go-review.googlesource.com/c/go/+/452176 Reviewed-by: Dmitri Shuralyov Reviewed-by: Daniel Martí Run-TryBot: Joseph Tsai Auto-Submit: Joseph Tsai Reviewed-by: Ian Lance Taylor TryBot-Result: Gopher Robot --- diff --git a/src/compress/zlib/reader.go b/src/compress/zlib/reader.go index 343a18bf68..10954eaad7 100644 --- a/src/compress/zlib/reader.go +++ b/src/compress/zlib/reader.go @@ -26,6 +26,7 @@ package zlib import ( "bufio" "compress/flate" + "encoding/binary" "errors" "hash" "hash/adler32" @@ -110,7 +111,7 @@ func (z *reader) Read(p []byte) (int, error) { return n, z.err } // ZLIB (RFC 1950) is big-endian, unlike GZIP (RFC 1952). - checksum := uint32(z.scratch[0])<<24 | uint32(z.scratch[1])<<16 | uint32(z.scratch[2])<<8 | uint32(z.scratch[3]) + checksum := binary.BigEndian.Uint32(z.scratch[:4]) if checksum != z.digest.Sum32() { z.err = ErrChecksum return n, z.err @@ -145,7 +146,7 @@ func (z *reader) Reset(r io.Reader, dict []byte) error { } return z.err } - h := uint(z.scratch[0])<<8 | uint(z.scratch[1]) + h := binary.BigEndian.Uint16(z.scratch[:2]) if (z.scratch[0]&0x0f != zlibDeflate) || (z.scratch[0]>>4 > zlibMaxWindow) || (h%31 != 0) { z.err = ErrHeader return z.err @@ -159,7 +160,7 @@ func (z *reader) Reset(r io.Reader, dict []byte) error { } return z.err } - checksum := uint32(z.scratch[0])<<24 | uint32(z.scratch[1])<<16 | uint32(z.scratch[2])<<8 | uint32(z.scratch[3]) + checksum := binary.BigEndian.Uint32(z.scratch[:4]) if checksum != adler32.Checksum(dict) { z.err = ErrDictionary return z.err diff --git a/src/compress/zlib/writer.go b/src/compress/zlib/writer.go index 9986e3834d..c65e80f742 100644 --- a/src/compress/zlib/writer.go +++ b/src/compress/zlib/writer.go @@ -115,7 +115,7 @@ func (z *Writer) writeHeader() (err error) { if z.dict != nil { z.scratch[1] |= 1 << 5 } - z.scratch[1] += uint8(31 - (uint16(z.scratch[0])<<8+uint16(z.scratch[1]))%31) + z.scratch[1] += uint8(31 - binary.BigEndian.Uint16(z.scratch[:2])%31) if _, err = z.w.Write(z.scratch[0:2]); err != nil { return err }