badpacket/util/writer.go

61 lines
1.3 KiB
Go
Raw Normal View History

2022-03-07 05:56:48 +00:00
package util
import (
"encoding/binary"
"io"
)
func WriteUint8(writer io.Writer, val uint8) (err error) {
var protocol [1]byte
protocol[0] = val
_, err = writer.Write(protocol[:1])
return
}
func WriteInt8(writer io.Writer, val int8) (err error) {
return WriteUint8(writer, uint8(val))
}
func WriteByte(writer io.Writer, val byte) (err error) {
return WriteUint8(writer, val)
}
func WriteInt16(writer io.Writer, val int16) (err error) {
err = WriteUint16(writer, uint16(val))
return
}
func WriteUint16(writer io.Writer, val uint16) (err error) {
var protocol [2]byte
binary.BigEndian.PutUint16(protocol[:2], val)
_, err = writer.Write(protocol[:2])
return
}
func WriteInt32(writer io.Writer, val int32) (err error) {
err = WriteUint32(writer, uint32(val))
return
}
func WriteInt(writer io.Writer, val int) (err error) {
return WriteInt32(writer, int32(val))
}
func WriteUint32(writer io.Writer, val uint32) (err error) {
var protocol [4]byte
binary.BigEndian.PutUint32(protocol[:4], val)
_, err = writer.Write(protocol[:4])
return
}
func WriteInt64(writer io.Writer, val int64) (err error) {
err = WriteUint64(writer, uint64(val))
return
}
func WriteUint64(writer io.Writer, val uint64) (err error) {
var protocol [8]byte
binary.BigEndian.PutUint64(protocol[:8], val)
_, err = writer.Write(protocol[:8])
return
}