Skip to content

Commit aa4fc0e

Browse files
committed
cmd/link,compress/zip,image/png: use binary.{Big,Little}Endian methods
Use the binary.{Big,Little}Endian integer encoding methods rather than variations found in local implementations. The functions in the binary package have been tested to ensure they inline correctly and don't add unnecessary bounds checking. Change-Id: Ie10111ca6edb7c11e8e5e21c58a5748ae99b7f87 Reviewed-on: https://go-review.googlesource.com/134375 Run-TryBot: Lynn Boger <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Michael Munday <[email protected]>
1 parent 9f24118 commit aa4fc0e

File tree

4 files changed

+14
-47
lines changed

4 files changed

+14
-47
lines changed

src/cmd/link/internal/ld/lib.go

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1757,26 +1757,6 @@ func addsection(arch *sys.Arch, seg *sym.Segment, name string, rwx int) *sym.Sec
17571757
return sect
17581758
}
17591759

1760-
func Le16(b []byte) uint16 {
1761-
return uint16(b[0]) | uint16(b[1])<<8
1762-
}
1763-
1764-
func Le32(b []byte) uint32 {
1765-
return uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24
1766-
}
1767-
1768-
func Le64(b []byte) uint64 {
1769-
return uint64(Le32(b)) | uint64(Le32(b[4:]))<<32
1770-
}
1771-
1772-
func Be16(b []byte) uint16 {
1773-
return uint16(b[0])<<8 | uint16(b[1])
1774-
}
1775-
1776-
func Be32(b []byte) uint32 {
1777-
return uint32(b[0])<<24 | uint32(b[1])<<16 | uint32(b[2])<<8 | uint32(b[3])
1778-
}
1779-
17801760
type chain struct {
17811761
sym *sym.Symbol
17821762
up *chain

src/cmd/link/internal/ppc64/asm.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -716,9 +716,9 @@ func archrelocvariant(ctxt *ld.Link, r *sym.Reloc, s *sym.Symbol, t int64) int64
716716
// overflow depends on the instruction
717717
var o1 uint32
718718
if ctxt.Arch.ByteOrder == binary.BigEndian {
719-
o1 = ld.Be32(s.P[r.Off-2:])
719+
o1 = binary.BigEndian.Uint32(s.P[r.Off-2:])
720720
} else {
721-
o1 = ld.Le32(s.P[r.Off:])
721+
o1 = binary.LittleEndian.Uint32(s.P[r.Off:])
722722
}
723723
switch o1 >> 26 {
724724
case 24, // ori
@@ -750,9 +750,9 @@ func archrelocvariant(ctxt *ld.Link, r *sym.Reloc, s *sym.Symbol, t int64) int64
750750
// overflow depends on the instruction
751751
var o1 uint32
752752
if ctxt.Arch.ByteOrder == binary.BigEndian {
753-
o1 = ld.Be32(s.P[r.Off-2:])
753+
o1 = binary.BigEndian.Uint32(s.P[r.Off-2:])
754754
} else {
755-
o1 = ld.Le32(s.P[r.Off:])
755+
o1 = binary.LittleEndian.Uint32(s.P[r.Off:])
756756
}
757757
switch o1 >> 26 {
758758
case 25, // oris
@@ -774,9 +774,9 @@ func archrelocvariant(ctxt *ld.Link, r *sym.Reloc, s *sym.Symbol, t int64) int64
774774
case sym.RV_POWER_DS:
775775
var o1 uint32
776776
if ctxt.Arch.ByteOrder == binary.BigEndian {
777-
o1 = uint32(ld.Be16(s.P[r.Off:]))
777+
o1 = uint32(binary.BigEndian.Uint16(s.P[r.Off:]))
778778
} else {
779-
o1 = uint32(ld.Le16(s.P[r.Off:]))
779+
o1 = uint32(binary.LittleEndian.Uint16(s.P[r.Off:]))
780780
}
781781
if t&3 != 0 {
782782
ld.Errorf(s, "relocation for %s+%d is not aligned: %d", r.Sym.Name, r.Off, t)

src/compress/zlib/writer.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package zlib
66

77
import (
88
"compress/flate"
9+
"encoding/binary"
910
"fmt"
1011
"hash"
1112
"hash/adler32"
@@ -120,11 +121,7 @@ func (z *Writer) writeHeader() (err error) {
120121
}
121122
if z.dict != nil {
122123
// The next four bytes are the Adler-32 checksum of the dictionary.
123-
checksum := adler32.Checksum(z.dict)
124-
z.scratch[0] = uint8(checksum >> 24)
125-
z.scratch[1] = uint8(checksum >> 16)
126-
z.scratch[2] = uint8(checksum >> 8)
127-
z.scratch[3] = uint8(checksum >> 0)
124+
binary.BigEndian.PutUint32(z.scratch[:], adler32.Checksum(z.dict))
128125
if _, err = z.w.Write(z.scratch[0:4]); err != nil {
129126
return err
130127
}
@@ -190,10 +187,7 @@ func (z *Writer) Close() error {
190187
}
191188
checksum := z.digest.Sum32()
192189
// ZLIB (RFC 1950) is big-endian, unlike GZIP (RFC 1952).
193-
z.scratch[0] = uint8(checksum >> 24)
194-
z.scratch[1] = uint8(checksum >> 16)
195-
z.scratch[2] = uint8(checksum >> 8)
196-
z.scratch[3] = uint8(checksum >> 0)
190+
binary.BigEndian.PutUint32(z.scratch[:], checksum)
197191
_, z.err = z.w.Write(z.scratch[0:4])
198192
return z.err
199193
}

src/image/png/writer.go

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package png
77
import (
88
"bufio"
99
"compress/zlib"
10+
"encoding/binary"
1011
"hash/crc32"
1112
"image"
1213
"image/color"
@@ -62,14 +63,6 @@ const (
6263
// compression level, although that is not implemented yet.
6364
)
6465

65-
// Big-endian.
66-
func writeUint32(b []uint8, u uint32) {
67-
b[0] = uint8(u >> 24)
68-
b[1] = uint8(u >> 16)
69-
b[2] = uint8(u >> 8)
70-
b[3] = uint8(u >> 0)
71-
}
72-
7366
type opaquer interface {
7467
Opaque() bool
7568
}
@@ -108,15 +101,15 @@ func (e *encoder) writeChunk(b []byte, name string) {
108101
e.err = UnsupportedError(name + " chunk is too large: " + strconv.Itoa(len(b)))
109102
return
110103
}
111-
writeUint32(e.header[:4], n)
104+
binary.BigEndian.PutUint32(e.header[:4], n)
112105
e.header[4] = name[0]
113106
e.header[5] = name[1]
114107
e.header[6] = name[2]
115108
e.header[7] = name[3]
116109
crc := crc32.NewIEEE()
117110
crc.Write(e.header[4:8])
118111
crc.Write(b)
119-
writeUint32(e.footer[:4], crc.Sum32())
112+
binary.BigEndian.PutUint32(e.footer[:4], crc.Sum32())
120113

121114
_, e.err = e.w.Write(e.header[:8])
122115
if e.err != nil {
@@ -131,8 +124,8 @@ func (e *encoder) writeChunk(b []byte, name string) {
131124

132125
func (e *encoder) writeIHDR() {
133126
b := e.m.Bounds()
134-
writeUint32(e.tmp[0:4], uint32(b.Dx()))
135-
writeUint32(e.tmp[4:8], uint32(b.Dy()))
127+
binary.BigEndian.PutUint32(e.tmp[0:4], uint32(b.Dx()))
128+
binary.BigEndian.PutUint32(e.tmp[4:8], uint32(b.Dy()))
136129
// Set bit depth and color type.
137130
switch e.cb {
138131
case cbG8:

0 commit comments

Comments
 (0)