aboutsummaryrefslogtreecommitdiffstats
path: root/rlp/encode.go
diff options
context:
space:
mode:
authorFelix Lange <fjl@twurst.com>2015-03-19 19:15:43 +0800
committerFelix Lange <fjl@twurst.com>2015-03-19 19:15:43 +0800
commit965c9babe336cfa8d5c740d5356acbc5f9ba4a72 (patch)
tree2d72947f28b4fe420b12ff76988e9cf692de126b /rlp/encode.go
parent064279c0ec2f048cbdd965c095ea332bb8666f94 (diff)
downloadgo-tangerine-965c9babe336cfa8d5c740d5356acbc5f9ba4a72.tar
go-tangerine-965c9babe336cfa8d5c740d5356acbc5f9ba4a72.tar.gz
go-tangerine-965c9babe336cfa8d5c740d5356acbc5f9ba4a72.tar.bz2
go-tangerine-965c9babe336cfa8d5c740d5356acbc5f9ba4a72.tar.lz
go-tangerine-965c9babe336cfa8d5c740d5356acbc5f9ba4a72.tar.xz
go-tangerine-965c9babe336cfa8d5c740d5356acbc5f9ba4a72.tar.zst
go-tangerine-965c9babe336cfa8d5c740d5356acbc5f9ba4a72.zip
rlp: fix encoding of one element strings and byte slices
The encoder was missing a special case for one element strings whose element is below 0x7f. Such strings must be encoded as a single byte without a string header.
Diffstat (limited to 'rlp/encode.go')
-rw-r--r--rlp/encode.go21
1 files changed, 14 insertions, 7 deletions
diff --git a/rlp/encode.go b/rlp/encode.go
index 42bbb876c..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 {
@@ -404,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())
}
@@ -434,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
}