implement an utils.BigEndian

This commit is contained in:
Marten Seemann
2017-08-02 15:34:34 +07:00
parent 05fde42e2d
commit 604c6ae406
3 changed files with 523 additions and 135 deletions

View File

@@ -0,0 +1,220 @@
package utils
import (
"bytes"
"io"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Big Endian encoding / decoding", func() {
Context("ReadUint16", func() {
It("reads a big endian", func() {
b := []byte{0x13, 0xEF}
val, err := BigEndian.ReadUint16(bytes.NewReader(b))
Expect(err).ToNot(HaveOccurred())
Expect(val).To(Equal(uint16(0x13EF)))
})
It("throws an error if less than 2 bytes are passed", func() {
b := []byte{0x13, 0xEF}
for i := 0; i < len(b); i++ {
_, err := BigEndian.ReadUint16(bytes.NewReader(b[:i]))
Expect(err).To(MatchError(io.EOF))
}
})
})
Context("ReadUint32", func() {
It("reads a big endian", func() {
b := []byte{0x12, 0x35, 0xAB, 0xFF}
val, err := BigEndian.ReadUint32(bytes.NewReader(b))
Expect(err).ToNot(HaveOccurred())
Expect(val).To(Equal(uint32(0x1235ABFF)))
})
It("throws an error if less than 4 bytes are passed", func() {
b := []byte{0x12, 0x35, 0xAB, 0xFF}
for i := 0; i < len(b); i++ {
_, err := BigEndian.ReadUint32(bytes.NewReader(b[:i]))
Expect(err).To(MatchError(io.EOF))
}
})
})
Context("ReadUint64", func() {
It("reads a big endian", func() {
b := []byte{0x12, 0x35, 0xAB, 0xFF, 0xEF, 0xBE, 0xAD, 0xDE}
val, err := BigEndian.ReadUint64(bytes.NewReader(b))
Expect(err).ToNot(HaveOccurred())
Expect(val).To(Equal(uint64(0x1235ABFFEFBEADDE)))
})
It("throws an error if less than 8 bytes are passed", func() {
b := []byte{0x12, 0x35, 0xAB, 0xFF, 0xEF, 0xBE, 0xAD, 0xDE}
for i := 0; i < len(b); i++ {
_, err := BigEndian.ReadUint64(bytes.NewReader(b[:i]))
Expect(err).To(MatchError(io.EOF))
}
})
})
Context("WriteUint16", func() {
It("outputs 2 bytes", func() {
b := &bytes.Buffer{}
BigEndian.WriteUint16(b, uint16(1))
Expect(b.Len()).To(Equal(2))
})
It("outputs a big endian", func() {
num := uint16(0xFF11)
b := &bytes.Buffer{}
BigEndian.WriteUint16(b, num)
Expect(b.Bytes()).To(Equal([]byte{0xFF, 0x11}))
})
})
Context("WriteUint24", func() {
It("outputs 3 bytes", func() {
b := &bytes.Buffer{}
BigEndian.WriteUint24(b, uint32(1))
Expect(b.Len()).To(Equal(3))
})
It("outputs a big endian", func() {
num := uint32(0x010203)
b := &bytes.Buffer{}
BigEndian.WriteUint24(b, num)
Expect(b.Bytes()).To(Equal([]byte{0x01, 0x02, 0x03}))
})
It("panics if the value doesn't fit into 24 bits", func() {
num := uint32(0x01020304)
b := &bytes.Buffer{}
Expect(func() { BigEndian.WriteUint24(b, num) }).Should(Panic())
})
})
Context("WriteUint32", func() {
It("outputs 4 bytes", func() {
b := &bytes.Buffer{}
BigEndian.WriteUint32(b, uint32(1))
Expect(b.Len()).To(Equal(4))
})
It("outputs a big endian", func() {
num := uint32(0xEFAC3512)
b := &bytes.Buffer{}
BigEndian.WriteUint32(b, num)
Expect(b.Bytes()).To(Equal([]byte{0xEF, 0xAC, 0x35, 0x12}))
})
})
Context("WriteUint40", func() {
It("outputs 5 bytes", func() {
b := &bytes.Buffer{}
BigEndian.WriteUint40(b, uint64(1))
Expect(b.Len()).To(Equal(5))
})
It("outputs a big endian", func() {
num := uint64(0xDECAFBAD42)
b := &bytes.Buffer{}
BigEndian.WriteUint40(b, num)
Expect(b.Bytes()).To(Equal([]byte{0xDE, 0xCA, 0xFB, 0xAD, 0x42}))
})
It("panics if the value doesn't fit into 40 bits", func() {
num := uint64(0x010203040506)
b := &bytes.Buffer{}
Expect(func() { BigEndian.WriteUint40(b, num) }).Should(Panic())
})
})
Context("WriteUint48", func() {
It("outputs 6 bytes", func() {
b := &bytes.Buffer{}
BigEndian.WriteUint48(b, uint64(1))
Expect(b.Len()).To(Equal(6))
})
It("outputs a big endian", func() {
num := uint64(0xDEADBEEFCAFE)
b := &bytes.Buffer{}
BigEndian.WriteUint48(b, num)
Expect(b.Bytes()).To(Equal([]byte{0xDE, 0xAD, 0xBE, 0xEF, 0xCA, 0xFE}))
})
It("panics if the value doesn't fit into 48 bits", func() {
num := uint64(0xDEADBEEFCAFE01)
b := &bytes.Buffer{}
Expect(func() { BigEndian.WriteUint48(b, num) }).Should(Panic())
})
})
Context("WriteUint56", func() {
It("outputs 7 bytes", func() {
b := &bytes.Buffer{}
BigEndian.WriteUint56(b, uint64(1))
Expect(b.Len()).To(Equal(7))
})
It("outputs a big endian", func() {
num := uint64(0xEEDDCCBBAA9988)
b := &bytes.Buffer{}
BigEndian.WriteUint56(b, num)
Expect(b.Bytes()).To(Equal([]byte{0xEE, 0xDD, 0xCC, 0xBB, 0xAA, 0x99, 0x88}))
})
It("panics if the value doesn't fit into 56 bits", func() {
num := uint64(0xEEDDCCBBAA998801)
b := &bytes.Buffer{}
Expect(func() { BigEndian.WriteUint56(b, num) }).Should(Panic())
})
})
Context("WriteUint64", func() {
It("outputs 8 bytes", func() {
b := &bytes.Buffer{}
BigEndian.WriteUint64(b, uint64(1))
Expect(b.Len()).To(Equal(8))
})
It("outputs a big endian", func() {
num := uint64(0xFFEEDDCCBBAA9988)
b := &bytes.Buffer{}
BigEndian.WriteUint64(b, num)
Expect(b.Bytes()).To(Equal([]byte{0xFF, 0xEE, 0xDD, 0xCC, 0xBB, 0xAA, 0x99, 0x88}))
})
})
Context("ReadUintN", func() {
It("reads n bytes", func() {
m := map[uint8]uint64{
0: 0x0,
1: 0x01,
2: 0x0102,
3: 0x010203,
4: 0x01020304,
5: 0x0102030405,
6: 0x010203040506,
7: 0x01020304050607,
8: 0x0102030405060708,
}
for n, expected := range m {
b := bytes.NewReader([]byte{0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8})
i, err := BigEndian.ReadUintN(b, n)
Expect(err).ToNot(HaveOccurred())
Expect(i).To(Equal(expected))
}
})
It("errors", func() {
b := bytes.NewReader([]byte{0x1, 0x2})
_, err := BigEndian.ReadUintN(b, 3)
Expect(err).To(HaveOccurred())
})
})
})

View File

@@ -0,0 +1,157 @@
package utils
import (
"bytes"
"fmt"
"io"
)
// BigEndian is the big-endian implementation of ByteOrder.
var BigEndian ByteOrder = bigEndian{}
type bigEndian struct{}
var _ ByteOrder = &bigEndian{}
// ReadUintN reads N bytes
func (bigEndian) ReadUintN(b io.ByteReader, length uint8) (uint64, error) {
var res uint64
for i := uint8(0); i < length; i++ {
bt, err := b.ReadByte()
if err != nil {
return 0, err
}
res ^= uint64(bt) << ((length - 1 - i) * 8)
}
return res, nil
}
// ReadUint64 reads a uint64
func (bigEndian) ReadUint64(b io.ByteReader) (uint64, error) {
var b1, b2, b3, b4, b5, b6, b7, b8 uint8
var err error
if b8, err = b.ReadByte(); err != nil {
return 0, err
}
if b7, err = b.ReadByte(); err != nil {
return 0, err
}
if b6, err = b.ReadByte(); err != nil {
return 0, err
}
if b5, err = b.ReadByte(); err != nil {
return 0, err
}
if b4, err = b.ReadByte(); err != nil {
return 0, err
}
if b3, err = b.ReadByte(); err != nil {
return 0, err
}
if b2, err = b.ReadByte(); err != nil {
return 0, err
}
if b1, err = b.ReadByte(); err != nil {
return 0, err
}
return uint64(b1) + uint64(b2)<<8 + uint64(b3)<<16 + uint64(b4)<<24 + uint64(b5)<<32 + uint64(b6)<<40 + uint64(b7)<<48 + uint64(b8)<<56, nil
}
// ReadUint32 reads a uint32
func (bigEndian) ReadUint32(b io.ByteReader) (uint32, error) {
var b1, b2, b3, b4 uint8
var err error
if b4, err = b.ReadByte(); err != nil {
return 0, err
}
if b3, err = b.ReadByte(); err != nil {
return 0, err
}
if b2, err = b.ReadByte(); err != nil {
return 0, err
}
if b1, err = b.ReadByte(); err != nil {
return 0, err
}
return uint32(b1) + uint32(b2)<<8 + uint32(b3)<<16 + uint32(b4)<<24, nil
}
// ReadUint16 reads a uint16
func (bigEndian) ReadUint16(b io.ByteReader) (uint16, error) {
var b1, b2 uint8
var err error
if b2, err = b.ReadByte(); err != nil {
return 0, err
}
if b1, err = b.ReadByte(); err != nil {
return 0, err
}
return uint16(b1) + uint16(b2)<<8, nil
}
// WriteUint64 writes a uint64
func (bigEndian) WriteUint64(b *bytes.Buffer, i uint64) {
b.Write([]byte{
uint8(i >> 56), uint8(i >> 48), uint8(i >> 40), uint8(i >> 32),
uint8(i >> 24), uint8(i >> 16), uint8(i >> 8), uint8(i),
})
}
// WriteUint56 writes 56 bit of a uint64
func (bigEndian) WriteUint56(b *bytes.Buffer, i uint64) {
if i >= (1 << 56) {
panic(fmt.Sprintf("%#x doesn't fit into 56 bits", i))
}
b.Write([]byte{
uint8(i >> 48), uint8(i >> 40), uint8(i >> 32),
uint8(i >> 24), uint8(i >> 16), uint8(i >> 8), uint8(i),
})
}
// WriteUint48 writes 48 bit of a uint64
func (bigEndian) WriteUint48(b *bytes.Buffer, i uint64) {
if i >= (1 << 48) {
panic(fmt.Sprintf("%#x doesn't fit into 48 bits", i))
}
b.Write([]byte{
uint8(i >> 40), uint8(i >> 32),
uint8(i >> 24), uint8(i >> 16), uint8(i >> 8), uint8(i),
})
}
// WriteUint40 writes 40 bit of a uint64
func (bigEndian) WriteUint40(b *bytes.Buffer, i uint64) {
if i >= (1 << 40) {
panic(fmt.Sprintf("%#x doesn't fit into 40 bits", i))
}
b.Write([]byte{
uint8(i >> 32),
uint8(i >> 24), uint8(i >> 16), uint8(i >> 8), uint8(i),
})
}
// WriteUint32 writes a uint32
func (bigEndian) WriteUint32(b *bytes.Buffer, i uint32) {
b.Write([]byte{uint8(i >> 24), uint8(i >> 16), uint8(i >> 8), uint8(i)})
}
// WriteUint24 writes 24 bit of a uint32
func (bigEndian) WriteUint24(b *bytes.Buffer, i uint32) {
if i >= (1 << 24) {
panic(fmt.Sprintf("%#x doesn't fit into 24 bits", i))
}
b.Write([]byte{uint8(i >> 16), uint8(i >> 8), uint8(i)})
}
// WriteUint16 writes a uint16
func (bigEndian) WriteUint16(b *bytes.Buffer, i uint16) {
b.Write([]byte{uint8(i >> 8), uint8(i)})
}
func (l bigEndian) ReadUfloat16(b io.ByteReader) (uint64, error) {
return readUfloat16(b, l)
}
func (l bigEndian) WriteUfloat16(b *bytes.Buffer, val uint64) {
writeUfloat16(b, l, val)
}

View File

@@ -2,6 +2,7 @@ package utils
import (
"bytes"
"fmt"
"io"
. "github.com/onsi/ginkgo"
@@ -9,142 +10,152 @@ import (
)
var _ = Describe("float16", func() {
It("reads", func() {
testcases := []struct {
expected uint64
binary uint16
}{
// There are fewer decoding test cases because encoding truncates, and
// decoding returns the smallest expansion.
// Small numbers represent themselves.
{0, 0},
{1, 1},
{2, 2},
{3, 3},
{4, 4},
{5, 5},
{6, 6},
{7, 7},
{15, 15},
{31, 31},
{42, 42},
{123, 123},
{1234, 1234},
// Check transition through 2^11.
{2046, 2046},
{2047, 2047},
{2048, 2048},
{2049, 2049},
// Running out of mantissa at 2^12.
{4094, 4094},
{4095, 4095},
{4096, 4096},
{4098, 4097},
{4100, 4098},
// Check transition through 2^13.
{8190, 6143},
{8192, 6144},
{8196, 6145},
// Half-way through the exponents.
{0x7FF8000, 0x87FF},
{0x8000000, 0x8800},
{0xFFF0000, 0x8FFF},
{0x10000000, 0x9000},
// Transition into the largest exponent.
{0x1FFE0000000, 0xF7FF},
{0x20000000000, 0xF800},
{0x20040000000, 0xF801},
// Transition into the max value.
{0x3FF80000000, 0xFFFE},
{0x3FFC0000000, 0xFFFF},
for _, v := range []ByteOrder{LittleEndian, BigEndian} {
bo := v
name := "little endian"
if bo == BigEndian {
name = "big endian"
}
for _, testcase := range testcases {
b := &bytes.Buffer{}
LittleEndian.WriteUint16(b, testcase.binary)
val, err := LittleEndian.ReadUfloat16(b)
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal(testcase.expected))
}
})
It("errors on eof", func() {
_, err := LittleEndian.ReadUfloat16(&bytes.Buffer{})
Expect(err).To(MatchError(io.EOF))
})
Context(fmt.Sprintf("in %s", name), func() {
It("reads", func() {
testcases := []struct {
expected uint64
binary uint16
}{
// There are fewer decoding test cases because encoding truncates, and
// decoding returns the smallest expansion.
// Small numbers represent themselves.
{0, 0},
{1, 1},
{2, 2},
{3, 3},
{4, 4},
{5, 5},
{6, 6},
{7, 7},
{15, 15},
{31, 31},
{42, 42},
{123, 123},
{1234, 1234},
// Check transition through 2^11.
{2046, 2046},
{2047, 2047},
{2048, 2048},
{2049, 2049},
// Running out of mantissa at 2^12.
{4094, 4094},
{4095, 4095},
{4096, 4096},
{4098, 4097},
{4100, 4098},
// Check transition through 2^13.
{8190, 6143},
{8192, 6144},
{8196, 6145},
// Half-way through the exponents.
{0x7FF8000, 0x87FF},
{0x8000000, 0x8800},
{0xFFF0000, 0x8FFF},
{0x10000000, 0x9000},
// Transition into the largest exponent.
{0x1FFE0000000, 0xF7FF},
{0x20000000000, 0xF800},
{0x20040000000, 0xF801},
// Transition into the max value.
{0x3FF80000000, 0xFFFE},
{0x3FFC0000000, 0xFFFF},
}
for _, testcase := range testcases {
b := &bytes.Buffer{}
bo.WriteUint16(b, testcase.binary)
val, err := bo.ReadUfloat16(b)
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal(testcase.expected))
}
})
It("writes", func() {
testcases := []struct {
decoded uint64
encoded uint16
}{
// Small numbers represent themselves.
{0, 0},
{1, 1},
{2, 2},
{3, 3},
{4, 4},
{5, 5},
{6, 6},
{7, 7},
{15, 15},
{31, 31},
{42, 42},
{123, 123},
{1234, 1234},
// Check transition through 2^11.
{2046, 2046},
{2047, 2047},
{2048, 2048},
{2049, 2049},
// Running out of mantissa at 2^12.
{4094, 4094},
{4095, 4095},
{4096, 4096},
{4097, 4096},
{4098, 4097},
{4099, 4097},
{4100, 4098},
{4101, 4098},
// Check transition through 2^13.
{8190, 6143},
{8191, 6143},
{8192, 6144},
{8193, 6144},
{8194, 6144},
{8195, 6144},
{8196, 6145},
{8197, 6145},
// Half-way through the exponents.
{0x7FF8000, 0x87FF},
{0x7FFFFFF, 0x87FF},
{0x8000000, 0x8800},
{0xFFF0000, 0x8FFF},
{0xFFFFFFF, 0x8FFF},
{0x10000000, 0x9000},
// Transition into the largest exponent.
{0x1FFFFFFFFFE, 0xF7FF},
{0x1FFFFFFFFFF, 0xF7FF},
{0x20000000000, 0xF800},
{0x20000000001, 0xF800},
{0x2003FFFFFFE, 0xF800},
{0x2003FFFFFFF, 0xF800},
{0x20040000000, 0xF801},
{0x20040000001, 0xF801},
// Transition into the max value and clamping.
{0x3FF80000000, 0xFFFE},
{0x3FFBFFFFFFF, 0xFFFE},
{0x3FFC0000000, 0xFFFF},
{0x3FFC0000001, 0xFFFF},
{0x3FFFFFFFFFF, 0xFFFF},
{0x40000000000, 0xFFFF},
{0xFFFFFFFFFFFFFFFF, 0xFFFF},
}
for _, testcase := range testcases {
b := &bytes.Buffer{}
LittleEndian.WriteUfloat16(b, testcase.decoded)
val, err := LittleEndian.ReadUint16(b)
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal(testcase.encoded))
}
})
It("errors on eof", func() {
_, err := bo.ReadUfloat16(&bytes.Buffer{})
Expect(err).To(MatchError(io.EOF))
})
It("writes", func() {
testcases := []struct {
decoded uint64
encoded uint16
}{
// Small numbers represent themselves.
{0, 0},
{1, 1},
{2, 2},
{3, 3},
{4, 4},
{5, 5},
{6, 6},
{7, 7},
{15, 15},
{31, 31},
{42, 42},
{123, 123},
{1234, 1234},
// Check transition through 2^11.
{2046, 2046},
{2047, 2047},
{2048, 2048},
{2049, 2049},
// Running out of mantissa at 2^12.
{4094, 4094},
{4095, 4095},
{4096, 4096},
{4097, 4096},
{4098, 4097},
{4099, 4097},
{4100, 4098},
{4101, 4098},
// Check transition through 2^13.
{8190, 6143},
{8191, 6143},
{8192, 6144},
{8193, 6144},
{8194, 6144},
{8195, 6144},
{8196, 6145},
{8197, 6145},
// Half-way through the exponents.
{0x7FF8000, 0x87FF},
{0x7FFFFFF, 0x87FF},
{0x8000000, 0x8800},
{0xFFF0000, 0x8FFF},
{0xFFFFFFF, 0x8FFF},
{0x10000000, 0x9000},
// Transition into the largest exponent.
{0x1FFFFFFFFFE, 0xF7FF},
{0x1FFFFFFFFFF, 0xF7FF},
{0x20000000000, 0xF800},
{0x20000000001, 0xF800},
{0x2003FFFFFFE, 0xF800},
{0x2003FFFFFFF, 0xF800},
{0x20040000000, 0xF801},
{0x20040000001, 0xF801},
// Transition into the max value and clamping.
{0x3FF80000000, 0xFFFE},
{0x3FFBFFFFFFF, 0xFFFE},
{0x3FFC0000000, 0xFFFF},
{0x3FFC0000001, 0xFFFF},
{0x3FFFFFFFFFF, 0xFFFF},
{0x40000000000, 0xFFFF},
{0xFFFFFFFFFFFFFFFF, 0xFFFF},
}
for _, testcase := range testcases {
b := &bytes.Buffer{}
bo.WriteUfloat16(b, testcase.decoded)
val, err := bo.ReadUint16(b)
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal(testcase.encoded))
}
})
})
}
})