diff options
author | obscuren <geffobscura@gmail.com> | 2014-07-30 05:33:18 +0800 |
---|---|---|
committer | obscuren <geffobscura@gmail.com> | 2014-07-30 05:33:18 +0800 |
commit | 6fd2401cdf792996c0183f896412831dd335377a (patch) | |
tree | e337868224aaaf024641b5465b2e2c78106c8573 | |
parent | 6e94c024e476a0e96b81d1cdd60dbb88b723593a (diff) | |
download | go-tangerine-6fd2401cdf792996c0183f896412831dd335377a.tar go-tangerine-6fd2401cdf792996c0183f896412831dd335377a.tar.gz go-tangerine-6fd2401cdf792996c0183f896412831dd335377a.tar.bz2 go-tangerine-6fd2401cdf792996c0183f896412831dd335377a.tar.lz go-tangerine-6fd2401cdf792996c0183f896412831dd335377a.tar.xz go-tangerine-6fd2401cdf792996c0183f896412831dd335377a.tar.zst go-tangerine-6fd2401cdf792996c0183f896412831dd335377a.zip |
Fixed issue with var int reading.
Reading uneven byte slices were broken.
-rw-r--r-- | ethutil/bytes.go | 30 | ||||
-rw-r--r-- | ethutil/value.go | 1 |
2 files changed, 27 insertions, 4 deletions
diff --git a/ethutil/bytes.go b/ethutil/bytes.go index 53b8cf645..eca2cc366 100644 --- a/ethutil/bytes.go +++ b/ethutil/bytes.go @@ -45,15 +45,15 @@ func BytesToNumber(b []byte) uint64 { // // Read a variable length number in big endian byte order func ReadVarint(reader *bytes.Reader) (ret uint64) { - if reader.Len() == 8 { + if reader.Len() > 4 { var num uint64 binary.Read(reader, binary.BigEndian, &num) ret = uint64(num) - } else if reader.Len() == 4 { + } else if reader.Len() > 2 { var num uint32 binary.Read(reader, binary.BigEndian, &num) ret = uint64(num) - } else if reader.Len() == 2 { + } else if reader.Len() > 0 { var num uint16 binary.Read(reader, binary.BigEndian, &num) ret = uint64(num) @@ -66,6 +66,30 @@ func ReadVarint(reader *bytes.Reader) (ret uint64) { return ret } +func ReadVarInt(buff []byte) (ret uint64) { + switch l := len(buff); { + case l > 4: + d := LeftPadBytes(buff, 8) + binary.Read(bytes.NewReader(d), binary.BigEndian, &ret) + case l > 2: + var num uint32 + d := LeftPadBytes(buff, 4) + binary.Read(bytes.NewReader(d), binary.BigEndian, &num) + ret = uint64(num) + case l > 1: + var num uint16 + d := LeftPadBytes(buff, 2) + binary.Read(bytes.NewReader(d), binary.BigEndian, &num) + ret = uint64(num) + default: + var num uint8 + binary.Read(bytes.NewReader(buff), binary.BigEndian, &num) + ret = uint64(num) + } + + return +} + // Binary length // // Returns the true binary length of the given number diff --git a/ethutil/value.go b/ethutil/value.go index 635683e66..85dc44ed6 100644 --- a/ethutil/value.go +++ b/ethutil/value.go @@ -67,7 +67,6 @@ func (val *Value) Uint() uint64 { return uint64(Val) } else if Val, ok := val.Val.([]byte); ok { return new(big.Int).SetBytes(Val).Uint64() - //return ReadVarint(bytes.NewReader(Val)) } else if Val, ok := val.Val.(*big.Int); ok { return Val.Uint64() } |