aboutsummaryrefslogtreecommitdiffstats
path: root/utils
diff options
context:
space:
mode:
Diffstat (limited to 'utils')
-rw-r--r--utils/binary/read.go124
-rw-r--r--utils/binary/read_test.go87
-rw-r--r--utils/binary/write.go30
-rw-r--r--utils/binary/writer_test.go43
4 files changed, 284 insertions, 0 deletions
diff --git a/utils/binary/read.go b/utils/binary/read.go
new file mode 100644
index 0000000..90ae41f
--- /dev/null
+++ b/utils/binary/read.go
@@ -0,0 +1,124 @@
+// Package binary implements sintax-sugar functions on top of the standard
+// library binary package
+package binary
+
+import (
+ "encoding/binary"
+ "io"
+
+ "gopkg.in/src-d/go-git.v4/core"
+)
+
+// Read reads structured binary data from r into data. Bytes are read and
+// decoded in BigEndian order
+// https://golang.org/pkg/encoding/binary/#Read
+func Read(r io.Reader, data ...interface{}) error {
+ for _, v := range data {
+ if err := binary.Read(r, binary.BigEndian, v); err != nil {
+ return err
+ }
+ }
+
+ return nil
+}
+
+// ReadUntil reads from r untin delim is found
+func ReadUntil(r io.Reader, delim byte) ([]byte, error) {
+ var buf [1]byte
+ value := make([]byte, 0, 16)
+ for {
+ if _, err := r.Read(buf[:]); err != nil {
+ if err == io.EOF {
+ return nil, err
+ }
+
+ return nil, err
+ }
+
+ if buf[0] == delim {
+ return value, nil
+ }
+
+ value = append(value, buf[0])
+ }
+}
+
+// ReadVariableWidthInt reads and returns an int in Git VLQ special format:
+//
+// Ordinary VLQ has some redundancies, example: the number 358 can be
+// encoded as the 2-octet VLQ 0x8166 or the 3-octet VLQ 0x808166 or the
+// 4-octet VLQ 0x80808166 and so forth.
+//
+// To avoid these redundancies, the VLQ format used in Git removes this
+// prepending redundancy and extends the representable range of shorter
+// VLQs by adding an offset to VLQs of 2 or more octets in such a way
+// that the lowest possible value for such an (N+1)-octet VLQ becomes
+// exactly one more than the maximum possible value for an N-octet VLQ.
+// In particular, since a 1-octet VLQ can store a maximum value of 127,
+// the minimum 2-octet VLQ (0x8000) is assigned the value 128 instead of
+// 0. Conversely, the maximum value of such a 2-octet VLQ (0xff7f) is
+// 16511 instead of just 16383. Similarly, the minimum 3-octet VLQ
+// (0x808000) has a value of 16512 instead of zero, which means
+// that the maximum 3-octet VLQ (0xffff7f) is 2113663 instead of
+// just 2097151. And so forth.
+//
+// This is how the offset is saved in C:
+//
+// dheader[pos] = ofs & 127;
+// while (ofs >>= 7)
+// dheader[--pos] = 128 | (--ofs & 127);
+//
+func ReadVariableWidthInt(r io.Reader) (int64, error) {
+ var c byte
+ if err := Read(r, &c); err != nil {
+ return 0, err
+ }
+
+ var v = int64(c & maskLength)
+ for c&maskContinue > 0 {
+ v++
+ if err := Read(r, &c); err != nil {
+ return 0, err
+ }
+
+ v = (v << lengthBits) + int64(c&maskLength)
+ }
+
+ return v, nil
+}
+
+const (
+ maskContinue = uint8(128) // 1000 000
+ maskLength = uint8(127) // 0111 1111
+ lengthBits = uint8(7) // subsequent bytes has 7 bits to store the length
+)
+
+// ReadUint32 reads 4 bytes and returns them as a Big ndian uint32
+func ReadUint32(r io.Reader) (uint32, error) {
+ var v uint32
+ if err := binary.Read(r, binary.BigEndian, &v); err != nil {
+ return 0, err
+ }
+
+ return v, nil
+}
+
+// ReadUint16 reads 2 bytes and returns them as a BigEndian uint16
+func ReadUint16(r io.Reader) (uint16, error) {
+ var v uint16
+ if err := binary.Read(r, binary.BigEndian, &v); err != nil {
+ return 0, err
+ }
+
+ return v, nil
+}
+
+// ReadHash reads a core.Hash from r
+func ReadHash(r io.Reader) (core.Hash, error) {
+ var h core.Hash
+ if err := binary.Read(r, binary.BigEndian, h[:]); err != nil {
+ return core.ZeroHash, err
+ }
+
+ return h, nil
+}
diff --git a/utils/binary/read_test.go b/utils/binary/read_test.go
new file mode 100644
index 0000000..6579ffb
--- /dev/null
+++ b/utils/binary/read_test.go
@@ -0,0 +1,87 @@
+package binary
+
+import (
+ "bytes"
+ "encoding/binary"
+ "testing"
+
+ . "gopkg.in/check.v1"
+ "gopkg.in/src-d/go-git.v3/core"
+)
+
+func Test(t *testing.T) { TestingT(t) }
+
+type BinarySuite struct{}
+
+var _ = Suite(&BinarySuite{})
+
+func (s *BinarySuite) TestRead(c *C) {
+ buf := bytes.NewBuffer(nil)
+ err := binary.Write(buf, binary.BigEndian, int64(42))
+ c.Assert(err, IsNil)
+ err = binary.Write(buf, binary.BigEndian, int32(42))
+ c.Assert(err, IsNil)
+
+ var i64 int64
+ var i32 int32
+ err = Read(buf, &i64, &i32)
+ c.Assert(err, IsNil)
+ c.Assert(i64, Equals, int64(42))
+ c.Assert(i32, Equals, int32(42))
+}
+
+func (s *BinarySuite) TestReadUntil(c *C) {
+ buf := bytes.NewBuffer([]byte("foo bar"))
+
+ b, err := ReadUntil(buf, ' ')
+ c.Assert(err, IsNil)
+ c.Assert(b, HasLen, 3)
+ c.Assert(string(b), Equals, "foo")
+}
+
+func (s *BinarySuite) TestReadVariableWidthInt(c *C) {
+ buf := bytes.NewBuffer([]byte{129, 110})
+
+ i, err := ReadVariableWidthInt(buf)
+ c.Assert(err, IsNil)
+ c.Assert(i, Equals, int64(366))
+}
+
+func (s *BinarySuite) TestReadVariableWidthIntShort(c *C) {
+ buf := bytes.NewBuffer([]byte{19})
+
+ i, err := ReadVariableWidthInt(buf)
+ c.Assert(err, IsNil)
+ c.Assert(i, Equals, int64(19))
+}
+
+func (s *BinarySuite) TestReadUint32(c *C) {
+ buf := bytes.NewBuffer(nil)
+ err := binary.Write(buf, binary.BigEndian, uint32(42))
+ c.Assert(err, IsNil)
+
+ i32, err := ReadUint32(buf)
+ c.Assert(err, IsNil)
+ c.Assert(i32, Equals, uint32(42))
+}
+
+func (s *BinarySuite) TestReadUint16(c *C) {
+ buf := bytes.NewBuffer(nil)
+ err := binary.Write(buf, binary.BigEndian, uint16(42))
+ c.Assert(err, IsNil)
+
+ i32, err := ReadUint16(buf)
+ c.Assert(err, IsNil)
+ c.Assert(i32, Equals, uint16(42))
+}
+
+func (s *BinarySuite) TestReadHash(c *C) {
+ expected := core.NewHash("43aec75c611f22c73b27ece2841e6ccca592f285")
+ buf := bytes.NewBuffer(nil)
+ err := binary.Write(buf, binary.BigEndian, expected)
+ c.Assert(err, IsNil)
+
+ hash, err := ReadHash(buf)
+ c.Assert(err, IsNil)
+ c.Assert(hash.String(), Equals, expected.String())
+}
diff --git a/utils/binary/write.go b/utils/binary/write.go
new file mode 100644
index 0000000..3ea1d91
--- /dev/null
+++ b/utils/binary/write.go
@@ -0,0 +1,30 @@
+package binary
+
+import (
+ "encoding/binary"
+ "io"
+)
+
+// Write writes the binary representation of data into w, using BigEndian order
+// https://golang.org/pkg/encoding/binary/#Write
+func Write(w io.Writer, data ...interface{}) error {
+ for _, v := range data {
+ if err := binary.Write(w, binary.BigEndian, v); err != nil {
+ return err
+ }
+ }
+
+ return nil
+}
+
+// WriteUint32 writes the binary representation of a uint32 into w, in BigEndian
+// order
+func WriteUint32(w io.Writer, value uint32) error {
+ return binary.Write(w, binary.BigEndian, value)
+}
+
+// WriteUint16 writes the binary representation of a uint16 into w, in BigEndian
+// order
+func WriteUint16(w io.Writer, value uint16) error {
+ return binary.Write(w, binary.BigEndian, value)
+}
diff --git a/utils/binary/writer_test.go b/utils/binary/writer_test.go
new file mode 100644
index 0000000..88140a1
--- /dev/null
+++ b/utils/binary/writer_test.go
@@ -0,0 +1,43 @@
+package binary
+
+import (
+ "bytes"
+ "encoding/binary"
+
+ . "gopkg.in/check.v1"
+)
+
+func (s *BinarySuite) TestWrite(c *C) {
+ expected := bytes.NewBuffer(nil)
+ err := binary.Write(expected, binary.BigEndian, int64(42))
+ c.Assert(err, IsNil)
+ err = binary.Write(expected, binary.BigEndian, int32(42))
+ c.Assert(err, IsNil)
+
+ buf := bytes.NewBuffer(nil)
+ err = Write(buf, int64(42), int32(42))
+ c.Assert(err, IsNil)
+ c.Assert(buf, DeepEquals, expected)
+}
+
+func (s *BinarySuite) TestWriteUint32(c *C) {
+ expected := bytes.NewBuffer(nil)
+ err := binary.Write(expected, binary.BigEndian, int32(42))
+ c.Assert(err, IsNil)
+
+ buf := bytes.NewBuffer(nil)
+ err = WriteUint32(buf, 42)
+ c.Assert(err, IsNil)
+ c.Assert(buf, DeepEquals, expected)
+}
+
+func (s *BinarySuite) TestWriteUint16(c *C) {
+ expected := bytes.NewBuffer(nil)
+ err := binary.Write(expected, binary.BigEndian, int16(42))
+ c.Assert(err, IsNil)
+
+ buf := bytes.NewBuffer(nil)
+ err = WriteUint16(buf, 42)
+ c.Assert(err, IsNil)
+ c.Assert(buf, DeepEquals, expected)
+}