Skip to content

Commit 9a199d4

Browse files
dsnetgopherbot
authored andcommitted
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 <[email protected]> Reviewed-by: Daniel Martí <[email protected]> Run-TryBot: Joseph Tsai <[email protected]> Auto-Submit: Joseph Tsai <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent 8538477 commit 9a199d4

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

src/compress/zlib/reader.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ package zlib
2626
import (
2727
"bufio"
2828
"compress/flate"
29+
"encoding/binary"
2930
"errors"
3031
"hash"
3132
"hash/adler32"
@@ -110,7 +111,7 @@ func (z *reader) Read(p []byte) (int, error) {
110111
return n, z.err
111112
}
112113
// ZLIB (RFC 1950) is big-endian, unlike GZIP (RFC 1952).
113-
checksum := uint32(z.scratch[0])<<24 | uint32(z.scratch[1])<<16 | uint32(z.scratch[2])<<8 | uint32(z.scratch[3])
114+
checksum := binary.BigEndian.Uint32(z.scratch[:4])
114115
if checksum != z.digest.Sum32() {
115116
z.err = ErrChecksum
116117
return n, z.err
@@ -145,7 +146,7 @@ func (z *reader) Reset(r io.Reader, dict []byte) error {
145146
}
146147
return z.err
147148
}
148-
h := uint(z.scratch[0])<<8 | uint(z.scratch[1])
149+
h := binary.BigEndian.Uint16(z.scratch[:2])
149150
if (z.scratch[0]&0x0f != zlibDeflate) || (z.scratch[0]>>4 > zlibMaxWindow) || (h%31 != 0) {
150151
z.err = ErrHeader
151152
return z.err
@@ -159,7 +160,7 @@ func (z *reader) Reset(r io.Reader, dict []byte) error {
159160
}
160161
return z.err
161162
}
162-
checksum := uint32(z.scratch[0])<<24 | uint32(z.scratch[1])<<16 | uint32(z.scratch[2])<<8 | uint32(z.scratch[3])
163+
checksum := binary.BigEndian.Uint32(z.scratch[:4])
163164
if checksum != adler32.Checksum(dict) {
164165
z.err = ErrDictionary
165166
return z.err

src/compress/zlib/writer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func (z *Writer) writeHeader() (err error) {
115115
if z.dict != nil {
116116
z.scratch[1] |= 1 << 5
117117
}
118-
z.scratch[1] += uint8(31 - (uint16(z.scratch[0])<<8+uint16(z.scratch[1]))%31)
118+
z.scratch[1] += uint8(31 - binary.BigEndian.Uint16(z.scratch[:2])%31)
119119
if _, err = z.w.Write(z.scratch[0:2]); err != nil {
120120
return err
121121
}

0 commit comments

Comments
 (0)