Merge codec and tape packages
This commit is contained in:
127
tape/decode.go
Normal file
127
tape/decode.go
Normal file
@@ -0,0 +1,127 @@
|
||||
package tape
|
||||
|
||||
import "io"
|
||||
|
||||
// Decodable is any type that can decode itself from a decoder.
|
||||
type Decodable interface {
|
||||
// Decode reads data from decoder, replacing the data of the object. It
|
||||
// returns the amount of bytes written, and an error if the write
|
||||
// stopped early.
|
||||
Decode(decoder *Decoder) (n int, err error)
|
||||
}
|
||||
|
||||
// Decoder wraps an [io.Reader] and decodes data from it.
|
||||
type Decoder struct {
|
||||
io.Reader
|
||||
}
|
||||
|
||||
// ReadFull calls [io.ReadFull] on the reader.
|
||||
func (this *Decoder) ReadFull(buffer []byte) (n int, err error) {
|
||||
return io.ReadFull(this, buffer)
|
||||
}
|
||||
|
||||
// ReadByte decodes a single byte from the input reader.
|
||||
func (this *Decoder) ReadByte() (value byte, n int, err error) {
|
||||
uncasted, n, err := this.ReadUint8()
|
||||
return byte(uncasted), n, err
|
||||
}
|
||||
|
||||
// ReadInt8 decodes an 8-bit signed integer from the input reader.
|
||||
func (this *Decoder) ReadInt8() (value int8, n int, err error) {
|
||||
uncasted, n, err := this.ReadUint8()
|
||||
return int8(uncasted), n, err
|
||||
}
|
||||
|
||||
// ReadUint8 decodes an 8-bit unsigned integer from the input reader.
|
||||
func (this *Decoder) ReadUint8() (value uint8, n int, err error) {
|
||||
buffer := [1]byte { }
|
||||
n, err = this.ReadFull(buffer[:])
|
||||
return uint8(buffer[0]), n, err
|
||||
}
|
||||
|
||||
// ReadInt16 decodes an 16-bit signed integer from the input reader.
|
||||
func (this *Decoder) ReadInt16() (value int16, n int, err error) {
|
||||
uncasted, n, err := this.ReadUint16()
|
||||
return int16(uncasted), n, err
|
||||
}
|
||||
|
||||
// ReadUint16 decodes an 16-bit unsigned integer from the input reader.
|
||||
func (this *Decoder) ReadUint16() (value uint16, n int, err error) {
|
||||
buffer := [2]byte { }
|
||||
n, err = this.ReadFull(buffer[:])
|
||||
return uint16(buffer[0]) << 8 |
|
||||
uint16(buffer[1]), n, err
|
||||
}
|
||||
|
||||
// ReadInt32 decodes an 32-bit signed integer from the input reader.
|
||||
func (this *Decoder) ReadInt32() (value int32, n int, err error) {
|
||||
uncasted, n, err := this.ReadUint32()
|
||||
return int32(uncasted), n, err
|
||||
}
|
||||
|
||||
// ReadUint32 decodes an 32-bit unsigned integer from the input reader.
|
||||
func (this *Decoder) ReadUint32() (value uint32, n int, err error) {
|
||||
buffer := [4]byte { }
|
||||
n, err = this.ReadFull(buffer[:])
|
||||
return uint32(buffer[0]) << 24 |
|
||||
uint32(buffer[1]) << 16 |
|
||||
uint32(buffer[2]) << 8 |
|
||||
uint32(buffer[3]), n, err
|
||||
}
|
||||
|
||||
// ReadInt64 decodes an 64-bit signed integer from the input reader.
|
||||
func (this *Decoder) ReadInt64() (value int64, n int, err error) {
|
||||
uncasted, n, err := this.ReadUint64()
|
||||
return int64(uncasted), n, err
|
||||
}
|
||||
|
||||
// ReadUint64 decodes an 64-bit unsigned integer from the input reader.
|
||||
func (this *Decoder) ReadUint64() (value uint64, n int, err error) {
|
||||
buffer := [8]byte { }
|
||||
n, err = this.ReadFull(buffer[:])
|
||||
return uint64(buffer[0]) << 56 |
|
||||
uint64(buffer[1]) << 48 |
|
||||
uint64(buffer[2]) << 40 |
|
||||
uint64(buffer[3]) << 32 |
|
||||
uint64(buffer[4]) << 24 |
|
||||
uint64(buffer[5]) << 16 |
|
||||
uint64(buffer[6]) << 8 |
|
||||
uint64(buffer[7]), n, err
|
||||
}
|
||||
|
||||
// ReadIntN decodes an N-byte signed integer from the input reader.
|
||||
func (this *Decoder) ReadIntN(bytes int) (value int64, n int, err error) {
|
||||
uncasted, n, err := this.ReadUintN(bytes)
|
||||
return int64(uncasted), n, err
|
||||
}
|
||||
|
||||
// ReadUintN decodes an N-byte unsigned integer from the input reader.
|
||||
func (this *Decoder) ReadUintN(bytes int) (value uint64, n int, err error) {
|
||||
// TODO: don't make multiple read calls (without allocating)
|
||||
buffer := [1]byte { }
|
||||
for bytesLeft := bytes; bytesLeft > 0; bytesLeft -- {
|
||||
nn, err := this.ReadFull(buffer[:])
|
||||
n += nn; if err != nil { return 0, n, err }
|
||||
value |= uint64(buffer[0]) << ((bytesLeft - 1) * 8)
|
||||
}
|
||||
// *read* integers too big, but don't return them.
|
||||
if bytes > 8 { value = 0 }
|
||||
return value, n, nil
|
||||
}
|
||||
|
||||
// ReadGBEU decodes a growing unsigned integer of up to 64 bits from the input
|
||||
// reader.
|
||||
func (this *Decoder) ReadGBEU() (value uint64, n int, err error) {
|
||||
var fullValue uint64
|
||||
for {
|
||||
chunk, nn, err := this.ReadByte()
|
||||
n += nn; if err != nil { return 0, n, err }
|
||||
|
||||
fullValue *= 0x80
|
||||
fullValue += uint64(chunk & 0x7F)
|
||||
ccb := chunk >> 7
|
||||
if ccb == 0 {
|
||||
return fullValue, n, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user