auth: use encoding.binary

This commit is contained in:
Paul Sbarra 2012-05-28 17:04:02 -05:00
parent 4ea94ca0fe
commit cd22f99b20
1 changed files with 16 additions and 26 deletions

View File

@ -7,7 +7,7 @@ It is largely unmodified from the original XGB package that I forked.
*/ */
import ( import (
"bufio" "encoding/binary"
"errors" "errors"
"io" "io"
"os" "os"
@ -21,7 +21,7 @@ func readAuthority(hostname, display string) (
// b is a scratch buffer to use and should be at least 256 bytes long // b is a scratch buffer to use and should be at least 256 bytes long
// (i.e. it should be able to hold a hostname). // (i.e. it should be able to hold a hostname).
var b [256]byte b := make([]byte, 256)
// As per /usr/include/X11/Xauth.h. // As per /usr/include/X11/Xauth.h.
const familyLocal = 256 const familyLocal = 256
@ -49,29 +49,28 @@ func readAuthority(hostname, display string) (
} }
defer r.Close() defer r.Close()
br := bufio.NewReader(r)
for { for {
family, err := getU16BE(br, b[0:2]) var family uint16
if err := binary.Read(r, binary.BigEndian, &family); err != nil {
return "", nil, err
}
addr, err := getString(r, b)
if err != nil { if err != nil {
return "", nil, err return "", nil, err
} }
addr, err := getString(br, b[0:]) disp, err := getString(r, b)
if err != nil { if err != nil {
return "", nil, err return "", nil, err
} }
disp, err := getString(br, b[0:]) name0, err := getString(r, b)
if err != nil { if err != nil {
return "", nil, err return "", nil, err
} }
name0, err := getString(br, b[0:]) data0, err := getBytes(r, b)
if err != nil {
return "", nil, err
}
data0, err := getBytes(br, b[0:])
if err != nil { if err != nil {
return "", nil, err return "", nil, err
} }
@ -83,24 +82,15 @@ func readAuthority(hostname, display string) (
panic("unreachable") panic("unreachable")
} }
func getU16BE(r io.Reader, b []byte) (uint16, error) {
_, err := io.ReadFull(r, b[0:2])
if err != nil {
return 0, err
}
return uint16(b[0])<<8 + uint16(b[1]), nil
}
func getBytes(r io.Reader, b []byte) ([]byte, error) { func getBytes(r io.Reader, b []byte) ([]byte, error) {
n, err := getU16BE(r, b) var n uint16
if err != nil { if err := binary.Read(r, binary.BigEndian, &n); err != nil {
return nil, err return nil, err
} } else if n > uint16(len(b)) {
if int(n) > len(b) {
return nil, errors.New("bytes too long for buffer") return nil, errors.New("bytes too long for buffer")
} }
_, err = io.ReadFull(r, b[0:n])
if err != nil { if _, err := io.ReadFull(r, b[0:n]); err != nil {
return nil, err return nil, err
} }
return b[0:n], nil return b[0:n], nil