aboutsummaryrefslogtreecommitdiffstats
path: root/rlp
diff options
context:
space:
mode:
Diffstat (limited to 'rlp')
-rw-r--r--rlp/decode.go7
-rw-r--r--rlp/encode.go28
-rw-r--r--rlp/encode_test.go7
3 files changed, 34 insertions, 8 deletions
diff --git a/rlp/decode.go b/rlp/decode.go
index 55f7187a3..6d7e670c2 100644
--- a/rlp/decode.go
+++ b/rlp/decode.go
@@ -2,6 +2,7 @@ package rlp
import (
"bufio"
+ "bytes"
"encoding/binary"
"errors"
"fmt"
@@ -73,6 +74,12 @@ func Decode(r io.Reader, val interface{}) error {
return NewStream(r).Decode(val)
}
+// DecodeBytes parses RLP data from b into val.
+// Please see the documentation of Decode for the decoding rules.
+func DecodeBytes(b []byte, val interface{}) error {
+ return NewStream(bytes.NewReader(b)).Decode(val)
+}
+
type decodeError struct {
msg string
typ reflect.Type
diff --git a/rlp/encode.go b/rlp/encode.go
index 9d11d66bf..7ac74d8fb 100644
--- a/rlp/encode.go
+++ b/rlp/encode.go
@@ -203,8 +203,13 @@ func (w *encbuf) encodeStringHeader(size int) {
}
func (w *encbuf) encodeString(b []byte) {
- w.encodeStringHeader(len(b))
- w.str = append(w.str, b...)
+ if len(b) == 1 && b[0] <= 0x7F {
+ // fits single byte, no string header
+ w.str = append(w.str, b[0])
+ } else {
+ w.encodeStringHeader(len(b))
+ w.str = append(w.str, b...)
+ }
}
func (w *encbuf) list() *listhead {
@@ -386,7 +391,12 @@ func writeUint(val reflect.Value, w *encbuf) error {
}
func writeBigIntPtr(val reflect.Value, w *encbuf) error {
- return writeBigInt(val.Interface().(*big.Int), w)
+ ptr := val.Interface().(*big.Int)
+ if ptr == nil {
+ w.str = append(w.str, 0x80)
+ return nil
+ }
+ return writeBigInt(ptr, w)
}
func writeBigIntNoPtr(val reflect.Value, w *encbuf) error {
@@ -399,9 +409,6 @@ func writeBigInt(i *big.Int, w *encbuf) error {
return fmt.Errorf("rlp: cannot encode negative *big.Int")
} else if cmp == 0 {
w.str = append(w.str, 0x80)
- } else if bits := i.BitLen(); bits < 8 {
- // fits single byte
- w.str = append(w.str, byte(i.Uint64()))
} else {
w.encodeString(i.Bytes())
}
@@ -429,8 +436,13 @@ func writeByteArray(val reflect.Value, w *encbuf) error {
func writeString(val reflect.Value, w *encbuf) error {
s := val.String()
- w.encodeStringHeader(len(s))
- w.str = append(w.str, s...)
+ if len(s) == 1 && s[0] <= 0x7f {
+ // fits single byte, no string header
+ w.str = append(w.str, s[0])
+ } else {
+ w.encodeStringHeader(len(s))
+ w.str = append(w.str, s...)
+ }
return nil
}
diff --git a/rlp/encode_test.go b/rlp/encode_test.go
index c283fbd57..611514bda 100644
--- a/rlp/encode_test.go
+++ b/rlp/encode_test.go
@@ -103,12 +103,18 @@ var encTests = []encTest{
// byte slices, strings
{val: []byte{}, output: "80"},
+ {val: []byte{0x7E}, output: "7E"},
+ {val: []byte{0x7F}, output: "7F"},
+ {val: []byte{0x80}, output: "8180"},
{val: []byte{1, 2, 3}, output: "83010203"},
{val: []namedByteType{1, 2, 3}, output: "83010203"},
{val: [...]namedByteType{1, 2, 3}, output: "83010203"},
{val: "", output: "80"},
+ {val: "\x7E", output: "7E"},
+ {val: "\x7F", output: "7F"},
+ {val: "\x80", output: "8180"},
{val: "dog", output: "83646F67"},
{
val: "Lorem ipsum dolor sit amet, consectetur adipisicing eli",
@@ -196,6 +202,7 @@ var encTests = []encTest{
{val: (*uint)(nil), output: "80"},
{val: (*string)(nil), output: "80"},
{val: (*[]byte)(nil), output: "80"},
+ {val: (*big.Int)(nil), output: "80"},
{val: (*[]string)(nil), output: "C0"},
{val: (*[]interface{})(nil), output: "C0"},
{val: (*[]struct{ uint })(nil), output: "C0"},