badpacket/util/reader.go

61 lines
1.2 KiB
Go
Raw Normal View History

2022-03-07 05:56:48 +00:00
package util
import (
"encoding/binary"
"io"
)
var EndianNess = binary.LittleEndian
func ReadUint8(reader io.Reader) (val uint8, err error) {
if br, ok := reader.(io.ByteReader); ok {
return br.ReadByte()
}
var protocol [1]byte
_, err = reader.Read(protocol[:1])
val = protocol[0]
return
}
func ReadByte(reader io.Reader) (val byte, err error) {
return ReadUint8(reader)
}
func ReadUint16(reader io.Reader) (val uint16, err error) {
var protocol [2]byte
_, err = reader.Read(protocol[:2])
val = EndianNess.Uint16(protocol[:2])
return
}
func ReadInt16(reader io.Reader) (val int16, err error) {
uval, err := ReadUint16(reader)
val = int16(uval)
return
}
func ReadUint32(reader io.Reader) (val uint32, err error) {
var protocol [4]byte
_, err = reader.Read(protocol[:4])
val = EndianNess.Uint32(protocol[:4])
return
}
func ReadInt32(reader io.Reader) (val int32, err error) {
uval, err := ReadUint32(reader)
val = int32(uval)
return
}
func ReadUint64(reader io.Reader) (val uint64, err error) {
var protocol [8]byte
_, err = reader.Read(protocol[:8])
val = EndianNess.Uint64(protocol[:8])
return
}
func ReadInt64(reader io.Reader) (val int64, err error) {
uval, err := ReadUint64(reader)
val = int64(uval)
return
}