introduce protocol/version.go and remove big endian functions

This commit is contained in:
Lucas Clemente
2016-04-15 18:46:39 +02:00
parent 052822aea5
commit b6c9c9b6a4
8 changed files with 57 additions and 74 deletions

View File

@@ -38,25 +38,6 @@ func ReadUint32(b io.ByteReader) (uint32, error) {
return uint32(b1) + uint32(b2)<<8 + uint32(b3)<<16 + uint32(b4)<<24, nil
}
// ReadUint32BigEndian reads a uint32 Big Endian
func ReadUint32BigEndian(b io.ByteReader) (uint32, error) {
var b1, b2, b3, b4 uint8
var err error
if b1, err = b.ReadByte(); err != nil {
return 0, err
}
if b2, err = b.ReadByte(); err != nil {
return 0, err
}
if b3, err = b.ReadByte(); err != nil {
return 0, err
}
if b4, err = b.ReadByte(); err != nil {
return 0, err
}
return uint32(b4) + uint32(b3)<<8 + uint32(b2)<<16 + uint32(b1)<<24, nil
}
// ReadUint16 reads a uint16
func ReadUint16(b io.ByteReader) (uint16, error) {
var b1, b2 uint8
@@ -90,14 +71,6 @@ func WriteUint32(b *bytes.Buffer, i uint32) {
b.WriteByte(uint8((i >> 24) & 0xff))
}
// WriteUint32BigEndian writes a uint32
func WriteUint32BigEndian(b *bytes.Buffer, i uint32) {
b.WriteByte(uint8((i >> 24) & 0xff))
b.WriteByte(uint8((i >> 16) & 0xff))
b.WriteByte(uint8((i >> 8) & 0xff))
b.WriteByte(uint8(i & 0xff))
}
// WriteUint16 writes a uint16
func WriteUint16(b *bytes.Buffer, i uint16) {
b.WriteByte(uint8(i & 0xff))

View File

@@ -53,21 +53,6 @@ var _ = Describe("Utils", func() {
})
})
Context("ReadUint32BigEndian", func() {
It("reads a big endian", func() {
b := []byte{0x12, 0x35, 0xAB, 0xFF}
val, err := ReadUint32BigEndian(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{0x13, 0x34, 0xEA}
_, err := ReadUint32(bytes.NewReader(b))
Expect(err).To(HaveOccurred())
})
})
Context("WriteUint16", func() {
It("outputs 2 bytes", func() {
b := &bytes.Buffer{}
@@ -97,19 +82,4 @@ var _ = Describe("Utils", func() {
Expect(b.Bytes()).To(Equal([]byte{0x12, 0x35, 0xAC, 0xEF}))
})
})
Context("WriteUint32BigEndian", func() {
It("outputs 4 bytes", func() {
b := &bytes.Buffer{}
WriteUint32BigEndian(b, uint32(1))
Expect(b.Len()).To(Equal(4))
})
It("outputs a big endian", func() {
num := uint32(0xEFAC3512)
b := &bytes.Buffer{}
WriteUint32BigEndian(b, num)
Expect(b.Bytes()).To(Equal([]byte{0xEF, 0xAC, 0x35, 0x12}))
})
})
})