Skip to content
This repository has been archived by the owner on May 11, 2020. It is now read-only.

Commit

Permalink
wasm/leb128: drop ReadXYZSize functions
Browse files Browse the repository at this point in the history
  • Loading branch information
sbinet committed Mar 11, 2019
1 parent 2c018b7 commit a76146c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 45 deletions.
70 changes: 25 additions & 45 deletions wasm/leb128/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,64 +10,51 @@ import (
"io"
)

// ReadVarUint32Size reads a LEB128 encoded unsigned 32-bit integer from r.
// It returns the integer value, the size of the encoded value (in bytes), and
// the error (if any).
func ReadVarUint32Size(r io.Reader) (res uint32, size uint, err error) {
b := make([]byte, 1)
var shift uint
// ReadVarUint32 reads a LEB128 encoded unsigned 32-bit integer from r, and
// returns the integer value, and the error (if any).
func ReadVarUint32(r io.Reader) (uint32, error) {
var (
b = make([]byte, 1)
shift uint
res uint32
err error
)
for {
if _, err = io.ReadFull(r, b); err != nil {
return
return res, err
}

size++

cur := uint32(b[0])
res |= (cur & 0x7f) << (shift)
if cur&0x80 == 0 {
return res, size, nil
return res, nil
}
shift += 7
}
}

// ReadVarUint32 reads a LEB128 encoded unsigned 32-bit integer from r, and
// returns the integer value, and the error (if any).
func ReadVarUint32(r io.Reader) (uint32, error) {
n, _, err := ReadVarUint32Size(r)
return n, err
}

// ReadVarint32Size reads a LEB128 encoded signed 32-bit integer from r, and
// returns the integer value, the size of the encoded value, and the error
// (if any)
func ReadVarint32Size(r io.Reader) (res int32, size uint, err error) {
res64, size, err := ReadVarint64Size(r)
res = int32(res64)
return
}

// ReadVarint32 reads a LEB128 encoded signed 32-bit integer from r, and
// returns the integer value, and the error (if any).
func ReadVarint32(r io.Reader) (int32, error) {
n, _, err := ReadVarint32Size(r)
return n, err
n, err := ReadVarint64(r)
return int32(n), err
}

// ReadVarint64Size reads a LEB128 encoded signed 64-bit integer from r, and
// returns the integer value, the size of the encoded value, and the error
// (if any)
func ReadVarint64Size(r io.Reader) (res int64, size uint, err error) {
var shift uint
var sign int64 = -1
b := make([]byte, 1)
// ReadVarint64 reads a LEB128 encoded signed 64-bit integer from r, and
// returns the integer value, and the error (if any).
func ReadVarint64(r io.Reader) (int64, error) {
var (
b = make([]byte, 1)
shift uint
sign int64 = -1
res int64
err error
)

for {
if _, err = io.ReadFull(r, b); err != nil {
return
return res, err
}
size++

cur := int64(b[0])
res |= (cur & 0x7f) << shift
Expand All @@ -81,12 +68,5 @@ func ReadVarint64Size(r io.Reader) (res int64, size uint, err error) {
if ((sign >> 1) & res) != 0 {
res |= sign
}
return res, size, nil
}

// ReadVarint64 reads a LEB128 encoded signed 64-bit integer from r, and
// returns the integer value, and the error (if any).
func ReadVarint64(r io.Reader) (int64, error) {
n, _, err := ReadVarint64Size(r)
return n, err
return res, nil
}
15 changes: 15 additions & 0 deletions wasm/leb128/read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package leb128
import (
"bytes"
"fmt"
"io"
"testing"
)

Expand All @@ -33,6 +34,13 @@ func TestReadVarUint32(t *testing.T) {
}
}

func TestReadVarUint32Err(t *testing.T) {
_, err := ReadVarUint32(bytes.NewReader(nil))
if got, want := err, io.EOF; got != want {
t.Fatalf("got err=%v, want=%v", got, want)
}
}

var casesInt = []struct {
v int64
b []byte
Expand All @@ -55,3 +63,10 @@ func TestReadVarint32(t *testing.T) {
})
}
}

func TestReadVarint32Err(t *testing.T) {
_, err := ReadVarint32(bytes.NewReader(nil))
if got, want := err, io.EOF; got != want {
t.Fatalf("got err=%v, want=%v", got, want)
}
}

0 comments on commit a76146c

Please sign in to comment.