Commit aa4fc0e7 authored by Lynn Boger's avatar Lynn Boger

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 <laboger@linux.vnet.ibm.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarMichael Munday <mike.munday@ibm.com>
parent 9f241189
...@@ -1757,26 +1757,6 @@ func addsection(arch *sys.Arch, seg *sym.Segment, name string, rwx int) *sym.Sec ...@@ -1757,26 +1757,6 @@ func addsection(arch *sys.Arch, seg *sym.Segment, name string, rwx int) *sym.Sec
return sect return sect
} }
func Le16(b []byte) uint16 {
return uint16(b[0]) | uint16(b[1])<<8
}
func Le32(b []byte) uint32 {
return uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24
}
func Le64(b []byte) uint64 {
return uint64(Le32(b)) | uint64(Le32(b[4:]))<<32
}
func Be16(b []byte) uint16 {
return uint16(b[0])<<8 | uint16(b[1])
}
func Be32(b []byte) uint32 {
return uint32(b[0])<<24 | uint32(b[1])<<16 | uint32(b[2])<<8 | uint32(b[3])
}
type chain struct { type chain struct {
sym *sym.Symbol sym *sym.Symbol
up *chain up *chain
......
...@@ -716,9 +716,9 @@ func archrelocvariant(ctxt *ld.Link, r *sym.Reloc, s *sym.Symbol, t int64) int64 ...@@ -716,9 +716,9 @@ func archrelocvariant(ctxt *ld.Link, r *sym.Reloc, s *sym.Symbol, t int64) int64
// overflow depends on the instruction // overflow depends on the instruction
var o1 uint32 var o1 uint32
if ctxt.Arch.ByteOrder == binary.BigEndian { if ctxt.Arch.ByteOrder == binary.BigEndian {
o1 = ld.Be32(s.P[r.Off-2:]) o1 = binary.BigEndian.Uint32(s.P[r.Off-2:])
} else { } else {
o1 = ld.Le32(s.P[r.Off:]) o1 = binary.LittleEndian.Uint32(s.P[r.Off:])
} }
switch o1 >> 26 { switch o1 >> 26 {
case 24, // ori case 24, // ori
...@@ -750,9 +750,9 @@ func archrelocvariant(ctxt *ld.Link, r *sym.Reloc, s *sym.Symbol, t int64) int64 ...@@ -750,9 +750,9 @@ func archrelocvariant(ctxt *ld.Link, r *sym.Reloc, s *sym.Symbol, t int64) int64
// overflow depends on the instruction // overflow depends on the instruction
var o1 uint32 var o1 uint32
if ctxt.Arch.ByteOrder == binary.BigEndian { if ctxt.Arch.ByteOrder == binary.BigEndian {
o1 = ld.Be32(s.P[r.Off-2:]) o1 = binary.BigEndian.Uint32(s.P[r.Off-2:])
} else { } else {
o1 = ld.Le32(s.P[r.Off:]) o1 = binary.LittleEndian.Uint32(s.P[r.Off:])
} }
switch o1 >> 26 { switch o1 >> 26 {
case 25, // oris case 25, // oris
...@@ -774,9 +774,9 @@ func archrelocvariant(ctxt *ld.Link, r *sym.Reloc, s *sym.Symbol, t int64) int64 ...@@ -774,9 +774,9 @@ func archrelocvariant(ctxt *ld.Link, r *sym.Reloc, s *sym.Symbol, t int64) int64
case sym.RV_POWER_DS: case sym.RV_POWER_DS:
var o1 uint32 var o1 uint32
if ctxt.Arch.ByteOrder == binary.BigEndian { if ctxt.Arch.ByteOrder == binary.BigEndian {
o1 = uint32(ld.Be16(s.P[r.Off:])) o1 = uint32(binary.BigEndian.Uint16(s.P[r.Off:]))
} else { } else {
o1 = uint32(ld.Le16(s.P[r.Off:])) o1 = uint32(binary.LittleEndian.Uint16(s.P[r.Off:]))
} }
if t&3 != 0 { if t&3 != 0 {
ld.Errorf(s, "relocation for %s+%d is not aligned: %d", r.Sym.Name, r.Off, t) ld.Errorf(s, "relocation for %s+%d is not aligned: %d", r.Sym.Name, r.Off, t)
......
...@@ -6,6 +6,7 @@ package zlib ...@@ -6,6 +6,7 @@ package zlib
import ( import (
"compress/flate" "compress/flate"
"encoding/binary"
"fmt" "fmt"
"hash" "hash"
"hash/adler32" "hash/adler32"
...@@ -120,11 +121,7 @@ func (z *Writer) writeHeader() (err error) { ...@@ -120,11 +121,7 @@ func (z *Writer) writeHeader() (err error) {
} }
if z.dict != nil { if z.dict != nil {
// The next four bytes are the Adler-32 checksum of the dictionary. // The next four bytes are the Adler-32 checksum of the dictionary.
checksum := adler32.Checksum(z.dict) binary.BigEndian.PutUint32(z.scratch[:], adler32.Checksum(z.dict))
z.scratch[0] = uint8(checksum >> 24)
z.scratch[1] = uint8(checksum >> 16)
z.scratch[2] = uint8(checksum >> 8)
z.scratch[3] = uint8(checksum >> 0)
if _, err = z.w.Write(z.scratch[0:4]); err != nil { if _, err = z.w.Write(z.scratch[0:4]); err != nil {
return err return err
} }
...@@ -190,10 +187,7 @@ func (z *Writer) Close() error { ...@@ -190,10 +187,7 @@ func (z *Writer) Close() error {
} }
checksum := z.digest.Sum32() checksum := z.digest.Sum32()
// ZLIB (RFC 1950) is big-endian, unlike GZIP (RFC 1952). // ZLIB (RFC 1950) is big-endian, unlike GZIP (RFC 1952).
z.scratch[0] = uint8(checksum >> 24) binary.BigEndian.PutUint32(z.scratch[:], checksum)
z.scratch[1] = uint8(checksum >> 16)
z.scratch[2] = uint8(checksum >> 8)
z.scratch[3] = uint8(checksum >> 0)
_, z.err = z.w.Write(z.scratch[0:4]) _, z.err = z.w.Write(z.scratch[0:4])
return z.err return z.err
} }
...@@ -7,6 +7,7 @@ package png ...@@ -7,6 +7,7 @@ package png
import ( import (
"bufio" "bufio"
"compress/zlib" "compress/zlib"
"encoding/binary"
"hash/crc32" "hash/crc32"
"image" "image"
"image/color" "image/color"
...@@ -62,14 +63,6 @@ const ( ...@@ -62,14 +63,6 @@ const (
// compression level, although that is not implemented yet. // compression level, although that is not implemented yet.
) )
// Big-endian.
func writeUint32(b []uint8, u uint32) {
b[0] = uint8(u >> 24)
b[1] = uint8(u >> 16)
b[2] = uint8(u >> 8)
b[3] = uint8(u >> 0)
}
type opaquer interface { type opaquer interface {
Opaque() bool Opaque() bool
} }
...@@ -108,7 +101,7 @@ func (e *encoder) writeChunk(b []byte, name string) { ...@@ -108,7 +101,7 @@ func (e *encoder) writeChunk(b []byte, name string) {
e.err = UnsupportedError(name + " chunk is too large: " + strconv.Itoa(len(b))) e.err = UnsupportedError(name + " chunk is too large: " + strconv.Itoa(len(b)))
return return
} }
writeUint32(e.header[:4], n) binary.BigEndian.PutUint32(e.header[:4], n)
e.header[4] = name[0] e.header[4] = name[0]
e.header[5] = name[1] e.header[5] = name[1]
e.header[6] = name[2] e.header[6] = name[2]
...@@ -116,7 +109,7 @@ func (e *encoder) writeChunk(b []byte, name string) { ...@@ -116,7 +109,7 @@ func (e *encoder) writeChunk(b []byte, name string) {
crc := crc32.NewIEEE() crc := crc32.NewIEEE()
crc.Write(e.header[4:8]) crc.Write(e.header[4:8])
crc.Write(b) crc.Write(b)
writeUint32(e.footer[:4], crc.Sum32()) binary.BigEndian.PutUint32(e.footer[:4], crc.Sum32())
_, e.err = e.w.Write(e.header[:8]) _, e.err = e.w.Write(e.header[:8])
if e.err != nil { if e.err != nil {
...@@ -131,8 +124,8 @@ func (e *encoder) writeChunk(b []byte, name string) { ...@@ -131,8 +124,8 @@ func (e *encoder) writeChunk(b []byte, name string) {
func (e *encoder) writeIHDR() { func (e *encoder) writeIHDR() {
b := e.m.Bounds() b := e.m.Bounds()
writeUint32(e.tmp[0:4], uint32(b.Dx())) binary.BigEndian.PutUint32(e.tmp[0:4], uint32(b.Dx()))
writeUint32(e.tmp[4:8], uint32(b.Dy())) binary.BigEndian.PutUint32(e.tmp[4:8], uint32(b.Dy()))
// Set bit depth and color type. // Set bit depth and color type.
switch e.cb { switch e.cb {
case cbG8: case cbG8:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment