mirror of
https://github.com/fluencelabs/tendermint
synced 2025-06-12 04:41:22 +00:00
converting Binary struct model to native w/ global methods model
This commit is contained in:
@ -6,11 +6,20 @@ type Binary interface {
|
||||
WriteTo(w io.Writer) (int64, error)
|
||||
}
|
||||
|
||||
func WriteTo(b Binary, w io.Writer, n int64, err error) (int64, error) {
|
||||
if err != nil {
|
||||
return n, err
|
||||
func WriteTo(w io.Writer, bz []byte, n *int64, err *error) {
|
||||
if *err != nil {
|
||||
return
|
||||
}
|
||||
var n_ int64
|
||||
n_, err = b.WriteTo(w)
|
||||
return n + n_, err
|
||||
n_, err_ := w.Write(bz)
|
||||
*n += int64(n_)
|
||||
*err = err_
|
||||
}
|
||||
|
||||
func ReadFull(r io.Reader, buf []byte, n *int64, err *error) {
|
||||
if *err != nil {
|
||||
return
|
||||
}
|
||||
n_, err_ := io.ReadFull(r, buf)
|
||||
*n += int64(n_)
|
||||
*err = err_
|
||||
}
|
||||
|
@ -1,71 +1,22 @@
|
||||
package binary
|
||||
|
||||
import "io"
|
||||
import "bytes"
|
||||
import (
|
||||
"io"
|
||||
)
|
||||
|
||||
type ByteSlice []byte
|
||||
// ByteSlice
|
||||
|
||||
func (self ByteSlice) Equals(other interface{}) bool {
|
||||
if o, ok := other.(ByteSlice); ok {
|
||||
return bytes.Equal(self, o)
|
||||
} else {
|
||||
return false
|
||||
func WriteByteSlice(w io.Writer, bz []byte, n *int64, err *error) {
|
||||
WriteUInt32(w, uint32(len(bz)), n, err)
|
||||
WriteTo(w, bz, n, err)
|
||||
}
|
||||
|
||||
func ReadByteSlice(r io.Reader, n *int64, err *error) []byte {
|
||||
length := ReadUInt32(r, n, err)
|
||||
if *err != nil {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (self ByteSlice) Less(other interface{}) bool {
|
||||
if o, ok := other.(ByteSlice); ok {
|
||||
return bytes.Compare(self, o) < 0 // -1 if a < b
|
||||
} else {
|
||||
panic("Cannot compare unequal types")
|
||||
}
|
||||
}
|
||||
|
||||
func (self ByteSlice) ByteSize() int {
|
||||
return len(self) + 4
|
||||
}
|
||||
|
||||
func (self ByteSlice) WriteTo(w io.Writer) (n int64, err error) {
|
||||
var n_ int
|
||||
_, err = UInt32(len(self)).WriteTo(w)
|
||||
if err != nil {
|
||||
return n, err
|
||||
}
|
||||
n_, err = w.Write([]byte(self))
|
||||
return int64(n_ + 4), err
|
||||
}
|
||||
|
||||
func (self ByteSlice) Reader() io.Reader {
|
||||
return bytes.NewReader([]byte(self))
|
||||
}
|
||||
|
||||
func ReadByteSliceSafe(r io.Reader) (bytes ByteSlice, n int64, err error) {
|
||||
length, n_, err := ReadUInt32Safe(r)
|
||||
n += n_
|
||||
if err != nil {
|
||||
return nil, n, err
|
||||
}
|
||||
bytes = make([]byte, int(length))
|
||||
n__, err := io.ReadFull(r, bytes)
|
||||
n += int64(n__)
|
||||
if err != nil {
|
||||
return nil, n, err
|
||||
}
|
||||
return bytes, n, nil
|
||||
}
|
||||
|
||||
func ReadByteSliceN(r io.Reader) (bytes ByteSlice, n int64) {
|
||||
bytes, n, err := ReadByteSliceSafe(r)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return bytes, n
|
||||
}
|
||||
|
||||
func ReadByteSlice(r io.Reader) (bytes ByteSlice) {
|
||||
bytes, _, err := ReadByteSliceSafe(r)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return bytes
|
||||
buf := make([]byte, int(length))
|
||||
ReadFull(r, buf, n, err)
|
||||
return buf
|
||||
}
|
||||
|
195
binary/codec.go
195
binary/codec.go
@ -1,91 +1,138 @@
|
||||
package binary
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Codec interface {
|
||||
WriteTo(io.Writer, interface{}, *int64, *error)
|
||||
ReadFrom(io.Reader, *int64, *error) interface{}
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
const (
|
||||
TYPE_NIL = Byte(0x00)
|
||||
TYPE_BYTE = Byte(0x01)
|
||||
TYPE_INT8 = Byte(0x02)
|
||||
TYPE_UINT8 = Byte(0x03)
|
||||
TYPE_INT16 = Byte(0x04)
|
||||
TYPE_UINT16 = Byte(0x05)
|
||||
TYPE_INT32 = Byte(0x06)
|
||||
TYPE_UINT32 = Byte(0x07)
|
||||
TYPE_INT64 = Byte(0x08)
|
||||
TYPE_UINT64 = Byte(0x09)
|
||||
TYPE_STRING = Byte(0x10)
|
||||
TYPE_BYTESLICE = Byte(0x11)
|
||||
TYPE_TIME = Byte(0x20)
|
||||
typeNil = byte(0x00)
|
||||
typeByte = byte(0x01)
|
||||
typeInt8 = byte(0x02)
|
||||
// typeUInt8 = byte(0x03)
|
||||
typeInt16 = byte(0x04)
|
||||
typeUInt16 = byte(0x05)
|
||||
typeInt32 = byte(0x06)
|
||||
typeUInt32 = byte(0x07)
|
||||
typeInt64 = byte(0x08)
|
||||
typeUInt64 = byte(0x09)
|
||||
typeString = byte(0x10)
|
||||
typeByteSlice = byte(0x11)
|
||||
typeTime = byte(0x20)
|
||||
)
|
||||
|
||||
func GetBinaryType(o Binary) Byte {
|
||||
var BasicCodec = basicCodec{}
|
||||
|
||||
type basicCodec struct{}
|
||||
|
||||
func (bc basicCodec) WriteTo(w io.Writer, o interface{}, n *int64, err *error) {
|
||||
switch o.(type) {
|
||||
case nil:
|
||||
return TYPE_NIL
|
||||
case Byte:
|
||||
return TYPE_BYTE
|
||||
case Int8:
|
||||
return TYPE_INT8
|
||||
case UInt8:
|
||||
return TYPE_UINT8
|
||||
case Int16:
|
||||
return TYPE_INT16
|
||||
case UInt16:
|
||||
return TYPE_UINT16
|
||||
case Int32:
|
||||
return TYPE_INT32
|
||||
case UInt32:
|
||||
return TYPE_UINT32
|
||||
case Int64:
|
||||
return TYPE_INT64
|
||||
case UInt64:
|
||||
return TYPE_UINT64
|
||||
case String:
|
||||
return TYPE_STRING
|
||||
case ByteSlice:
|
||||
return TYPE_BYTESLICE
|
||||
case Time:
|
||||
return TYPE_TIME
|
||||
WriteByte(w, typeNil, n, err)
|
||||
case byte:
|
||||
WriteByte(w, typeByte, n, err)
|
||||
WriteByte(w, o.(byte), n, err)
|
||||
case int8:
|
||||
WriteByte(w, typeInt8, n, err)
|
||||
WriteInt8(w, o.(int8), n, err)
|
||||
//case uint8:
|
||||
// WriteByte(w, typeUInt8, n, err)
|
||||
// WriteUInt8(w, o.(uint8), n, err)
|
||||
case int16:
|
||||
WriteByte(w, typeInt16, n, err)
|
||||
WriteInt16(w, o.(int16), n, err)
|
||||
case uint16:
|
||||
WriteByte(w, typeUInt16, n, err)
|
||||
WriteUInt16(w, o.(uint16), n, err)
|
||||
case int32:
|
||||
WriteByte(w, typeInt32, n, err)
|
||||
WriteInt32(w, o.(int32), n, err)
|
||||
case uint32:
|
||||
WriteByte(w, typeUInt32, n, err)
|
||||
WriteUInt32(w, o.(uint32), n, err)
|
||||
case int64:
|
||||
WriteByte(w, typeInt64, n, err)
|
||||
WriteInt64(w, o.(int64), n, err)
|
||||
case uint64:
|
||||
WriteByte(w, typeUInt64, n, err)
|
||||
WriteUInt64(w, o.(uint64), n, err)
|
||||
case string:
|
||||
WriteByte(w, typeString, n, err)
|
||||
WriteString(w, o.(string), n, err)
|
||||
case []byte:
|
||||
WriteByte(w, typeByteSlice, n, err)
|
||||
WriteByteSlice(w, o.([]byte), n, err)
|
||||
case time.Time:
|
||||
WriteByte(w, typeTime, n, err)
|
||||
WriteTime(w, o.(time.Time), n, err)
|
||||
default:
|
||||
panic("Unsupported type")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (bc basicCodec) ReadFrom(r io.Reader, n *int64, err *error) interface{} {
|
||||
type_ := ReadByte(r, n, err)
|
||||
switch type_ {
|
||||
case typeNil:
|
||||
return nil
|
||||
case typeByte:
|
||||
return ReadByte(r, n, err)
|
||||
case typeInt8:
|
||||
return ReadInt8(r, n, err)
|
||||
//case typeUInt8:
|
||||
// return ReadUInt8(r, n, err)
|
||||
case typeInt16:
|
||||
return ReadInt16(r, n, err)
|
||||
case typeUInt16:
|
||||
return ReadUInt16(r, n, err)
|
||||
case typeInt32:
|
||||
return ReadInt32(r, n, err)
|
||||
case typeUInt32:
|
||||
return ReadUInt32(r, n, err)
|
||||
case typeInt64:
|
||||
return ReadInt64(r, n, err)
|
||||
case typeUInt64:
|
||||
return ReadUInt64(r, n, err)
|
||||
case typeString:
|
||||
return ReadString(r, n, err)
|
||||
case typeByteSlice:
|
||||
return ReadByteSlice(r, n, err)
|
||||
case typeTime:
|
||||
return ReadTime(r, n, err)
|
||||
default:
|
||||
panic("Unsupported type")
|
||||
}
|
||||
}
|
||||
|
||||
func ReadBinaryN(r io.Reader) (o Binary, n int64) {
|
||||
type_, n_ := ReadByteN(r)
|
||||
n += n_
|
||||
switch type_ {
|
||||
case TYPE_NIL:
|
||||
o, n_ = nil, 0
|
||||
case TYPE_BYTE:
|
||||
o, n_ = ReadByteN(r)
|
||||
case TYPE_INT8:
|
||||
o, n_ = ReadInt8N(r)
|
||||
case TYPE_UINT8:
|
||||
o, n_ = ReadUInt8N(r)
|
||||
case TYPE_INT16:
|
||||
o, n_ = ReadInt16N(r)
|
||||
case TYPE_UINT16:
|
||||
o, n_ = ReadUInt16N(r)
|
||||
case TYPE_INT32:
|
||||
o, n_ = ReadInt32N(r)
|
||||
case TYPE_UINT32:
|
||||
o, n_ = ReadUInt32N(r)
|
||||
case TYPE_INT64:
|
||||
o, n_ = ReadInt64N(r)
|
||||
case TYPE_UINT64:
|
||||
o, n_ = ReadUInt64N(r)
|
||||
case TYPE_STRING:
|
||||
o, n_ = ReadStringN(r)
|
||||
case TYPE_BYTESLICE:
|
||||
o, n_ = ReadByteSliceN(r)
|
||||
case TYPE_TIME:
|
||||
o, n_ = ReadTimeN(r)
|
||||
default:
|
||||
panic("Unsupported type")
|
||||
}
|
||||
n += n_
|
||||
return o, n
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
// Creates an adapter codec for Binary things.
|
||||
// Resulting Codec can be used with merkle/*.
|
||||
type BinaryCodec struct {
|
||||
decoder func(io.Reader, *int64, *error) interface{}
|
||||
}
|
||||
|
||||
func NewBinaryCodec(decoder func(io.Reader, *int64, *error) interface{}) *BinaryCodec {
|
||||
return &BinaryCodec{decoder}
|
||||
}
|
||||
|
||||
func (ca *BinaryCodec) WriteTo(w io.Writer, o interface{}, n *int64, err *error) {
|
||||
if bo, ok := o.(Binary); ok {
|
||||
WriteTo(w, BinaryBytes(bo), n, err)
|
||||
} else {
|
||||
*err = errors.New("BinaryCodec expected Binary object")
|
||||
}
|
||||
}
|
||||
|
||||
func (ca *BinaryCodec) ReadFrom(r io.Reader, n *int64, err *error) interface{} {
|
||||
return ca.decoder(r, n, err)
|
||||
}
|
||||
|
500
binary/int.go
500
binary/int.go
@ -5,494 +5,118 @@ import (
|
||||
"io"
|
||||
)
|
||||
|
||||
type Byte byte
|
||||
type Int8 int8
|
||||
type UInt8 uint8
|
||||
type Int16 int16
|
||||
type UInt16 uint16
|
||||
type Int32 int32
|
||||
type UInt32 uint32
|
||||
type Int64 int64
|
||||
type UInt64 uint64
|
||||
type Int int
|
||||
type UInt uint
|
||||
|
||||
// Byte
|
||||
|
||||
func (self Byte) Equals(other interface{}) bool {
|
||||
return self == other
|
||||
func WriteByte(w io.Writer, b byte, n *int64, err *error) {
|
||||
WriteTo(w, []byte{b}, n, err)
|
||||
}
|
||||
|
||||
func (self Byte) Less(other interface{}) bool {
|
||||
if o, ok := other.(Byte); ok {
|
||||
return self < o
|
||||
} else {
|
||||
panic("Cannot compare unequal types")
|
||||
}
|
||||
}
|
||||
|
||||
func (self Byte) ByteSize() int {
|
||||
return 1
|
||||
}
|
||||
|
||||
func (self Byte) WriteTo(w io.Writer) (int64, error) {
|
||||
n, err := w.Write([]byte{byte(self)})
|
||||
return int64(n), err
|
||||
}
|
||||
|
||||
func ReadByteSafe(r io.Reader) (Byte, int64, error) {
|
||||
buf := [1]byte{0}
|
||||
n, err := io.ReadFull(r, buf[:])
|
||||
if err != nil {
|
||||
return 0, int64(n), err
|
||||
}
|
||||
return Byte(buf[0]), int64(n), nil
|
||||
}
|
||||
|
||||
func ReadByteN(r io.Reader) (Byte, int64) {
|
||||
b, n, err := ReadByteSafe(r)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return b, n
|
||||
}
|
||||
|
||||
func ReadByte(r io.Reader) Byte {
|
||||
b, _, err := ReadByteSafe(r)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
func Readbyte(r io.Reader) byte {
|
||||
return byte(ReadByte(r))
|
||||
func ReadByte(r io.Reader, n *int64, err *error) byte {
|
||||
buf := make([]byte, 1)
|
||||
ReadFull(r, buf, n, err)
|
||||
return buf[0]
|
||||
}
|
||||
|
||||
// Int8
|
||||
|
||||
func (self Int8) Equals(other interface{}) bool {
|
||||
return self == other
|
||||
func WriteInt8(w io.Writer, i int8, n *int64, err *error) {
|
||||
WriteByte(w, byte(i), n, err)
|
||||
}
|
||||
|
||||
func (self Int8) Less(other interface{}) bool {
|
||||
if o, ok := other.(Int8); ok {
|
||||
return self < o
|
||||
} else {
|
||||
panic("Cannot compare unequal types")
|
||||
}
|
||||
}
|
||||
|
||||
func (self Int8) ByteSize() int {
|
||||
return 1
|
||||
}
|
||||
|
||||
func (self Int8) WriteTo(w io.Writer) (int64, error) {
|
||||
n, err := w.Write([]byte{byte(self)})
|
||||
return int64(n), err
|
||||
}
|
||||
|
||||
func ReadInt8Safe(r io.Reader) (Int8, int64, error) {
|
||||
buf := [1]byte{0}
|
||||
n, err := io.ReadFull(r, buf[:])
|
||||
if err != nil {
|
||||
return Int8(0), int64(n), err
|
||||
}
|
||||
return Int8(buf[0]), int64(n), nil
|
||||
}
|
||||
|
||||
func ReadInt8N(r io.Reader) (Int8, int64) {
|
||||
b, n, err := ReadInt8Safe(r)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return b, n
|
||||
}
|
||||
|
||||
func ReadInt8(r io.Reader) Int8 {
|
||||
b, _, err := ReadInt8Safe(r)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
func Readint8(r io.Reader) int8 {
|
||||
return int8(ReadInt8(r))
|
||||
func ReadInt8(r io.Reader, n *int64, err *error) int8 {
|
||||
return int8(ReadByte(r, n, err))
|
||||
}
|
||||
|
||||
// UInt8
|
||||
|
||||
func (self UInt8) Equals(other interface{}) bool {
|
||||
return self == other
|
||||
func WriteUInt8(w io.Writer, i uint8, n *int64, err *error) {
|
||||
WriteByte(w, byte(i), n, err)
|
||||
}
|
||||
|
||||
func (self UInt8) Less(other interface{}) bool {
|
||||
if o, ok := other.(UInt8); ok {
|
||||
return self < o
|
||||
} else {
|
||||
panic("Cannot compare unequal types")
|
||||
}
|
||||
}
|
||||
|
||||
func (self UInt8) ByteSize() int {
|
||||
return 1
|
||||
}
|
||||
|
||||
func (self UInt8) WriteTo(w io.Writer) (int64, error) {
|
||||
n, err := w.Write([]byte{byte(self)})
|
||||
return int64(n), err
|
||||
}
|
||||
|
||||
func ReadUInt8Safe(r io.Reader) (UInt8, int64, error) {
|
||||
buf := [1]byte{0}
|
||||
n, err := io.ReadFull(r, buf[:])
|
||||
if err != nil {
|
||||
return UInt8(0), int64(n), err
|
||||
}
|
||||
return UInt8(buf[0]), int64(n), nil
|
||||
}
|
||||
|
||||
func ReadUInt8N(r io.Reader) (UInt8, int64) {
|
||||
b, n, err := ReadUInt8Safe(r)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return b, n
|
||||
}
|
||||
|
||||
func ReadUInt8(r io.Reader) UInt8 {
|
||||
b, _, err := ReadUInt8Safe(r)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
func Readuint8(r io.Reader) uint8 {
|
||||
return uint8(ReadUInt8(r))
|
||||
func ReadUInt8(r io.Reader, n *int64, err *error) uint8 {
|
||||
return uint8(ReadByte(r, n, err))
|
||||
}
|
||||
|
||||
// Int16
|
||||
|
||||
func (self Int16) Equals(other interface{}) bool {
|
||||
return self == other
|
||||
func WriteInt16(w io.Writer, i int16, n *int64, err *error) {
|
||||
buf := make([]byte, 2)
|
||||
binary.LittleEndian.PutUint16(buf, uint16(i))
|
||||
WriteTo(w, buf, n, err)
|
||||
}
|
||||
|
||||
func (self Int16) Less(other interface{}) bool {
|
||||
if o, ok := other.(Int16); ok {
|
||||
return self < o
|
||||
} else {
|
||||
panic("Cannot compare unequal types")
|
||||
}
|
||||
}
|
||||
|
||||
func (self Int16) ByteSize() int {
|
||||
return 2
|
||||
}
|
||||
|
||||
func (self Int16) WriteTo(w io.Writer) (int64, error) {
|
||||
buf := []byte{0, 0}
|
||||
binary.LittleEndian.PutUint16(buf, uint16(self))
|
||||
n, err := w.Write(buf)
|
||||
return int64(n), err
|
||||
}
|
||||
|
||||
func ReadInt16Safe(r io.Reader) (Int16, int64, error) {
|
||||
buf := [2]byte{0}
|
||||
n, err := io.ReadFull(r, buf[:])
|
||||
if err != nil {
|
||||
return Int16(0), int64(n), err
|
||||
}
|
||||
return Int16(binary.LittleEndian.Uint16(buf[:])), int64(n), nil
|
||||
}
|
||||
|
||||
func ReadInt16N(r io.Reader) (Int16, int64) {
|
||||
b, n, err := ReadInt16Safe(r)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return b, n
|
||||
}
|
||||
|
||||
func ReadInt16(r io.Reader) Int16 {
|
||||
b, _, err := ReadInt16Safe(r)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
func Readint16(r io.Reader) int16 {
|
||||
return int16(ReadInt16(r))
|
||||
func ReadInt16(r io.Reader, n *int64, err *error) int16 {
|
||||
buf := make([]byte, 2)
|
||||
ReadFull(r, buf, n, err)
|
||||
return int16(binary.LittleEndian.Uint16(buf))
|
||||
}
|
||||
|
||||
// UInt16
|
||||
|
||||
func (self UInt16) Equals(other interface{}) bool {
|
||||
return self == other
|
||||
func WriteUInt16(w io.Writer, i uint16, n *int64, err *error) {
|
||||
buf := make([]byte, 2)
|
||||
binary.LittleEndian.PutUint16(buf, uint16(i))
|
||||
WriteTo(w, buf, n, err)
|
||||
}
|
||||
|
||||
func (self UInt16) Less(other interface{}) bool {
|
||||
if o, ok := other.(UInt16); ok {
|
||||
return self < o
|
||||
} else {
|
||||
panic("Cannot compare unequal types")
|
||||
}
|
||||
}
|
||||
|
||||
func (self UInt16) ByteSize() int {
|
||||
return 2
|
||||
}
|
||||
|
||||
func (self UInt16) WriteTo(w io.Writer) (int64, error) {
|
||||
buf := []byte{0, 0}
|
||||
binary.LittleEndian.PutUint16(buf, uint16(self))
|
||||
n, err := w.Write(buf)
|
||||
return int64(n), err
|
||||
}
|
||||
|
||||
func ReadUInt16Safe(r io.Reader) (UInt16, int64, error) {
|
||||
buf := [2]byte{0}
|
||||
n, err := io.ReadFull(r, buf[:])
|
||||
if err != nil {
|
||||
return UInt16(0), int64(n), err
|
||||
}
|
||||
return UInt16(binary.LittleEndian.Uint16(buf[:])), int64(n), nil
|
||||
}
|
||||
|
||||
func ReadUInt16N(r io.Reader) (UInt16, int64) {
|
||||
b, n, err := ReadUInt16Safe(r)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return b, n
|
||||
}
|
||||
|
||||
func ReadUInt16(r io.Reader) UInt16 {
|
||||
b, _, err := ReadUInt16Safe(r)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
func Readuint16(r io.Reader) uint16 {
|
||||
return uint16(ReadUInt16(r))
|
||||
func ReadUInt16(r io.Reader, n *int64, err *error) uint16 {
|
||||
buf := make([]byte, 2)
|
||||
ReadFull(r, buf, n, err)
|
||||
return uint16(binary.LittleEndian.Uint16(buf))
|
||||
}
|
||||
|
||||
// Int32
|
||||
|
||||
func (self Int32) Equals(other interface{}) bool {
|
||||
return self == other
|
||||
func WriteInt32(w io.Writer, i int32, n *int64, err *error) {
|
||||
buf := make([]byte, 4)
|
||||
binary.LittleEndian.PutUint32(buf, uint32(i))
|
||||
WriteTo(w, buf, n, err)
|
||||
}
|
||||
|
||||
func (self Int32) Less(other interface{}) bool {
|
||||
if o, ok := other.(Int32); ok {
|
||||
return self < o
|
||||
} else {
|
||||
panic("Cannot compare unequal types")
|
||||
}
|
||||
}
|
||||
|
||||
func (self Int32) ByteSize() int {
|
||||
return 4
|
||||
}
|
||||
|
||||
func (self Int32) WriteTo(w io.Writer) (int64, error) {
|
||||
buf := []byte{0, 0, 0, 0}
|
||||
binary.LittleEndian.PutUint32(buf, uint32(self))
|
||||
n, err := w.Write(buf)
|
||||
return int64(n), err
|
||||
}
|
||||
|
||||
func ReadInt32Safe(r io.Reader) (Int32, int64, error) {
|
||||
buf := [4]byte{0}
|
||||
n, err := io.ReadFull(r, buf[:])
|
||||
if err != nil {
|
||||
return Int32(0), int64(n), err
|
||||
}
|
||||
return Int32(binary.LittleEndian.Uint32(buf[:])), int64(n), nil
|
||||
}
|
||||
|
||||
func ReadInt32N(r io.Reader) (Int32, int64) {
|
||||
b, n, err := ReadInt32Safe(r)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return b, n
|
||||
}
|
||||
|
||||
func ReadInt32(r io.Reader) Int32 {
|
||||
b, _, err := ReadInt32Safe(r)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
func Readint32(r io.Reader) int32 {
|
||||
return int32(ReadInt32(r))
|
||||
func ReadInt32(r io.Reader, n *int64, err *error) int32 {
|
||||
buf := make([]byte, 4)
|
||||
ReadFull(r, buf, n, err)
|
||||
return int32(binary.LittleEndian.Uint32(buf))
|
||||
}
|
||||
|
||||
// UInt32
|
||||
|
||||
func (self UInt32) Equals(other interface{}) bool {
|
||||
return self == other
|
||||
func WriteUInt32(w io.Writer, i uint32, n *int64, err *error) {
|
||||
buf := make([]byte, 4)
|
||||
binary.LittleEndian.PutUint32(buf, uint32(i))
|
||||
WriteTo(w, buf, n, err)
|
||||
}
|
||||
|
||||
func (self UInt32) Less(other interface{}) bool {
|
||||
if o, ok := other.(UInt32); ok {
|
||||
return self < o
|
||||
} else {
|
||||
panic("Cannot compare unequal types")
|
||||
}
|
||||
}
|
||||
|
||||
func (self UInt32) ByteSize() int {
|
||||
return 4
|
||||
}
|
||||
|
||||
func (self UInt32) WriteTo(w io.Writer) (int64, error) {
|
||||
buf := []byte{0, 0, 0, 0}
|
||||
binary.LittleEndian.PutUint32(buf, uint32(self))
|
||||
n, err := w.Write(buf)
|
||||
return int64(n), err
|
||||
}
|
||||
|
||||
func ReadUInt32Safe(r io.Reader) (UInt32, int64, error) {
|
||||
buf := [4]byte{0}
|
||||
n, err := io.ReadFull(r, buf[:])
|
||||
if err != nil {
|
||||
return UInt32(0), int64(n), err
|
||||
}
|
||||
return UInt32(binary.LittleEndian.Uint32(buf[:])), int64(n), nil
|
||||
}
|
||||
|
||||
func ReadUInt32N(r io.Reader) (UInt32, int64) {
|
||||
b, n, err := ReadUInt32Safe(r)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return b, n
|
||||
}
|
||||
|
||||
func ReadUInt32(r io.Reader) UInt32 {
|
||||
b, _, err := ReadUInt32Safe(r)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
func Readuint32(r io.Reader) uint32 {
|
||||
return uint32(ReadUInt32(r))
|
||||
func ReadUInt32(r io.Reader, n *int64, err *error) uint32 {
|
||||
buf := make([]byte, 4)
|
||||
ReadFull(r, buf, n, err)
|
||||
return uint32(binary.LittleEndian.Uint32(buf))
|
||||
}
|
||||
|
||||
// Int64
|
||||
|
||||
func (self Int64) Equals(other interface{}) bool {
|
||||
return self == other
|
||||
func WriteInt64(w io.Writer, i int64, n *int64, err *error) {
|
||||
buf := make([]byte, 8)
|
||||
binary.LittleEndian.PutUint64(buf, uint64(i))
|
||||
WriteTo(w, buf, n, err)
|
||||
}
|
||||
|
||||
func (self Int64) Less(other interface{}) bool {
|
||||
if o, ok := other.(Int64); ok {
|
||||
return self < o
|
||||
} else {
|
||||
panic("Cannot compare unequal types")
|
||||
}
|
||||
}
|
||||
|
||||
func (self Int64) ByteSize() int {
|
||||
return 8
|
||||
}
|
||||
|
||||
func (self Int64) WriteTo(w io.Writer) (int64, error) {
|
||||
buf := []byte{0, 0, 0, 0, 0, 0, 0, 0}
|
||||
binary.LittleEndian.PutUint64(buf, uint64(self))
|
||||
n, err := w.Write(buf)
|
||||
return int64(n), err
|
||||
}
|
||||
|
||||
func ReadInt64Safe(r io.Reader) (Int64, int64, error) {
|
||||
buf := [8]byte{0}
|
||||
n, err := io.ReadFull(r, buf[:])
|
||||
if err != nil {
|
||||
return Int64(0), int64(n), err
|
||||
}
|
||||
return Int64(binary.LittleEndian.Uint64(buf[:])), int64(n), nil
|
||||
}
|
||||
|
||||
func ReadInt64N(r io.Reader) (Int64, int64) {
|
||||
b, n, err := ReadInt64Safe(r)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return b, n
|
||||
}
|
||||
|
||||
func ReadInt64(r io.Reader) Int64 {
|
||||
b, _, err := ReadInt64Safe(r)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
func Readint64(r io.Reader) int64 {
|
||||
return int64(ReadInt64(r))
|
||||
func ReadInt64(r io.Reader, n *int64, err *error) int64 {
|
||||
buf := make([]byte, 8)
|
||||
ReadFull(r, buf, n, err)
|
||||
return int64(binary.LittleEndian.Uint64(buf))
|
||||
}
|
||||
|
||||
// UInt64
|
||||
|
||||
func (self UInt64) Equals(other interface{}) bool {
|
||||
return self == other
|
||||
func WriteUInt64(w io.Writer, i uint64, n *int64, err *error) {
|
||||
buf := make([]byte, 8)
|
||||
binary.LittleEndian.PutUint64(buf, uint64(i))
|
||||
WriteTo(w, buf, n, err)
|
||||
}
|
||||
|
||||
func (self UInt64) Less(other interface{}) bool {
|
||||
if o, ok := other.(UInt64); ok {
|
||||
return self < o
|
||||
} else {
|
||||
panic("Cannot compare unequal types")
|
||||
}
|
||||
}
|
||||
|
||||
func (self UInt64) ByteSize() int {
|
||||
return 8
|
||||
}
|
||||
|
||||
func (self UInt64) WriteTo(w io.Writer) (int64, error) {
|
||||
buf := []byte{0, 0, 0, 0, 0, 0, 0, 0}
|
||||
binary.LittleEndian.PutUint64(buf, uint64(self))
|
||||
n, err := w.Write(buf)
|
||||
return int64(n), err
|
||||
}
|
||||
|
||||
func ReadUInt64Safe(r io.Reader) (UInt64, int64, error) {
|
||||
buf := [8]byte{0}
|
||||
n, err := io.ReadFull(r, buf[:])
|
||||
if err != nil {
|
||||
return UInt64(0), int64(n), err
|
||||
}
|
||||
return UInt64(binary.LittleEndian.Uint64(buf[:])), int64(n), nil
|
||||
}
|
||||
|
||||
func ReadUInt64N(r io.Reader) (UInt64, int64) {
|
||||
b, n, err := ReadUInt64Safe(r)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return b, n
|
||||
}
|
||||
|
||||
func ReadUInt64(r io.Reader) UInt64 {
|
||||
b, _, err := ReadUInt64Safe(r)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
func Readuint64(r io.Reader) uint64 {
|
||||
return uint64(ReadUInt64(r))
|
||||
func ReadUInt64(r io.Reader, n *int64, err *error) uint64 {
|
||||
buf := make([]byte, 8)
|
||||
ReadFull(r, buf, n, err)
|
||||
return uint64(binary.LittleEndian.Uint64(buf))
|
||||
}
|
||||
|
@ -2,67 +2,19 @@ package binary
|
||||
|
||||
import "io"
|
||||
|
||||
type String string
|
||||
|
||||
// String
|
||||
|
||||
func (self String) Equals(other interface{}) bool {
|
||||
return self == other
|
||||
func WriteString(w io.Writer, s string, n *int64, err *error) {
|
||||
WriteUInt32(w, uint32(len(s)), n, err)
|
||||
WriteTo(w, []byte(s), n, err)
|
||||
}
|
||||
|
||||
func (self String) Less(other interface{}) bool {
|
||||
if o, ok := other.(String); ok {
|
||||
return self < o
|
||||
} else {
|
||||
panic("Cannot compare unequal types")
|
||||
func ReadString(r io.Reader, n *int64, err *error) string {
|
||||
length := ReadUInt32(r, n, err)
|
||||
if *err != nil {
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
func (self String) ByteSize() int {
|
||||
return len(self) + 4
|
||||
}
|
||||
|
||||
func (self String) WriteTo(w io.Writer) (n int64, err error) {
|
||||
var n_ int
|
||||
_, err = UInt32(len(self)).WriteTo(w)
|
||||
if err != nil {
|
||||
return n, err
|
||||
}
|
||||
n_, err = w.Write([]byte(self))
|
||||
return int64(n_ + 4), err
|
||||
}
|
||||
|
||||
func ReadStringSafe(r io.Reader) (str String, n int64, err error) {
|
||||
length, n_, err := ReadUInt32Safe(r)
|
||||
n += n_
|
||||
if err != nil {
|
||||
return "", n, err
|
||||
}
|
||||
bytes := make([]byte, int(length))
|
||||
n__, err := io.ReadFull(r, bytes)
|
||||
n += int64(n__)
|
||||
if err != nil {
|
||||
return "", n, err
|
||||
}
|
||||
return String(bytes), n, nil
|
||||
}
|
||||
|
||||
func ReadStringN(r io.Reader) (str String, n int64) {
|
||||
str, n, err := ReadStringSafe(r)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return str, n
|
||||
}
|
||||
|
||||
func ReadString(r io.Reader) (str String) {
|
||||
str, _, err := ReadStringSafe(r)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return str
|
||||
}
|
||||
|
||||
func Readstring(r io.Reader) (str string) {
|
||||
return string(ReadString(r))
|
||||
buf := make([]byte, int(length))
|
||||
ReadFull(r, buf, n, err)
|
||||
return string(buf)
|
||||
}
|
||||
|
@ -5,58 +5,13 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type Time struct {
|
||||
time.Time
|
||||
// Time
|
||||
|
||||
func WriteTime(w io.Writer, t time.Time, n *int64, err *error) {
|
||||
WriteInt64(w, t.Unix(), n, err)
|
||||
}
|
||||
|
||||
func TimeFromUnix(secSinceEpoch int64) Time {
|
||||
return Time{time.Unix(secSinceEpoch, 0)}
|
||||
}
|
||||
|
||||
func (self Time) Equals(other interface{}) bool {
|
||||
if o, ok := other.(Time); ok {
|
||||
return self.Equal(o.Time)
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func (self Time) Less(other interface{}) bool {
|
||||
if o, ok := other.(Time); ok {
|
||||
return self.Before(o.Time)
|
||||
} else {
|
||||
panic("Cannot compare unequal types")
|
||||
}
|
||||
}
|
||||
|
||||
func (self Time) ByteSize() int {
|
||||
return 8
|
||||
}
|
||||
|
||||
func (self Time) WriteTo(w io.Writer) (int64, error) {
|
||||
return Int64(self.Unix()).WriteTo(w)
|
||||
}
|
||||
|
||||
func ReadTimeSafe(r io.Reader) (Time, int64, error) {
|
||||
t, n, err := ReadInt64Safe(r)
|
||||
if err != nil {
|
||||
return Time{}, n, err
|
||||
}
|
||||
return Time{time.Unix(int64(t), 0)}, n, nil
|
||||
}
|
||||
|
||||
func ReadTimeN(r io.Reader) (Time, int64) {
|
||||
t, n, err := ReadTimeSafe(r)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return t, n
|
||||
}
|
||||
|
||||
func ReadTime(r io.Reader) Time {
|
||||
t, _, err := ReadTimeSafe(r)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return t
|
||||
func ReadTime(r io.Reader, n *int64, err *error) time.Time {
|
||||
t := ReadInt64(r, n, err)
|
||||
return time.Unix(t, 0)
|
||||
}
|
||||
|
@ -5,10 +5,10 @@ import (
|
||||
"crypto/sha256"
|
||||
)
|
||||
|
||||
func BinaryBytes(b Binary) ByteSlice {
|
||||
func BinaryBytes(b Binary) []byte {
|
||||
buf := bytes.NewBuffer(nil)
|
||||
b.WriteTo(buf)
|
||||
return ByteSlice(buf.Bytes())
|
||||
return buf.Bytes()
|
||||
}
|
||||
|
||||
// NOTE: does not care about the type, only the binary representation.
|
||||
@ -25,11 +25,11 @@ func BinaryCompare(a, b Binary) int {
|
||||
return bytes.Compare(aBytes, bBytes)
|
||||
}
|
||||
|
||||
func BinaryHash(b Binary) ByteSlice {
|
||||
func BinaryHash(b Binary) []byte {
|
||||
hasher := sha256.New()
|
||||
_, err := b.WriteTo(hasher)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return ByteSlice(hasher.Sum(nil))
|
||||
return hasher.Sum(nil)
|
||||
}
|
||||
|
Reference in New Issue
Block a user