Skip to content

Commit bc17dba

Browse files
committed
rlp: add Split functions
These functions allow destructuring of raw rlp-encoded bytes without the overhead of reflection or copying.
1 parent ac32f52 commit bc17dba

File tree

3 files changed

+337
-1
lines changed

3 files changed

+337
-1
lines changed

rlp/decode_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"io"
2525
"math/big"
2626
"reflect"
27+
"strings"
2728
"testing"
2829
)
2930

@@ -725,7 +726,7 @@ func encodeTestSlice(n uint) []byte {
725726
}
726727

727728
func unhex(str string) []byte {
728-
b, err := hex.DecodeString(str)
729+
b, err := hex.DecodeString(strings.Replace(str, " ", "", -1))
729730
if err != nil {
730731
panic(fmt.Sprintf("invalid hex string: %q", str))
731732
}

rlp/raw.go

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
// Copyright 2015 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package rlp
18+
19+
import "io"
20+
21+
// Split returns the content of first RLP value and any
22+
// bytes after the value as subslices of b.
23+
func Split(b []byte) (k Kind, content, rest []byte, err error) {
24+
k, ts, cs, err := readKind(b)
25+
if err != nil {
26+
return 0, nil, b, err
27+
}
28+
return k, b[ts : ts+cs], b[ts+cs:], nil
29+
}
30+
31+
// SplitString splits b into the content of an RLP string
32+
// and any remaining bytes after the string.
33+
func SplitString(b []byte) (content, rest []byte, err error) {
34+
k, content, rest, err := Split(b)
35+
if err != nil {
36+
return nil, b, err
37+
}
38+
if k == List {
39+
return nil, b, ErrExpectedString
40+
}
41+
return content, rest, nil
42+
}
43+
44+
// SplitList splits b into the content of a list and any remaining
45+
// bytes after the list.
46+
func SplitList(b []byte) (content, rest []byte, err error) {
47+
k, content, rest, err := Split(b)
48+
if err != nil {
49+
return nil, b, err
50+
}
51+
if k != List {
52+
return nil, b, ErrExpectedList
53+
}
54+
return content, rest, nil
55+
}
56+
57+
// CountValues counts the number of encoded values in b.
58+
func CountValues(b []byte) (int, error) {
59+
i := 0
60+
for ; len(b) > 0; i++ {
61+
_, tagsize, size, err := readKind(b)
62+
if err != nil {
63+
return 0, err
64+
}
65+
b = b[tagsize+size:]
66+
}
67+
return i, nil
68+
}
69+
70+
func readKind(buf []byte) (k Kind, tagsize, contentsize uint64, err error) {
71+
if len(buf) == 0 {
72+
return 0, 0, 0, io.ErrUnexpectedEOF
73+
}
74+
b := buf[0]
75+
switch {
76+
case b < 0x80:
77+
k = Byte
78+
tagsize = 0
79+
contentsize = 1
80+
case b < 0xB8:
81+
k = String
82+
tagsize = 1
83+
contentsize = uint64(b - 0x80)
84+
// Reject strings that should've been single bytes.
85+
if contentsize == 1 && buf[1] < 128 {
86+
return 0, 0, 0, ErrCanonSize
87+
}
88+
case b < 0xC0:
89+
k = String
90+
tagsize = uint64(b-0xB7) + 1
91+
contentsize, err = readSize(buf[1:], b-0xB7)
92+
case b < 0xF8:
93+
k = List
94+
tagsize = 1
95+
contentsize = uint64(b - 0xC0)
96+
default:
97+
k = List
98+
tagsize = uint64(b-0xF7) + 1
99+
contentsize, err = readSize(buf[1:], b-0xF7)
100+
}
101+
if err != nil {
102+
return 0, 0, 0, err
103+
}
104+
// Reject values larger than the input slice.
105+
if contentsize > uint64(len(buf))-tagsize {
106+
return 0, 0, 0, ErrValueTooLarge
107+
}
108+
return k, tagsize, contentsize, err
109+
}
110+
111+
func readSize(b []byte, slen byte) (uint64, error) {
112+
if int(slen) > len(b) {
113+
return 0, io.ErrUnexpectedEOF
114+
}
115+
var s uint64
116+
switch slen {
117+
case 1:
118+
s = uint64(b[0])
119+
case 2:
120+
s = uint64(b[0])<<8 | uint64(b[1])
121+
case 3:
122+
s = uint64(b[0])<<16 | uint64(b[1])<<8 | uint64(b[2])
123+
case 4:
124+
s = uint64(b[0])<<24 | uint64(b[1])<<16 | uint64(b[2])<<8 | uint64(b[3])
125+
case 5:
126+
s = uint64(b[0])<<32 | uint64(b[1])<<24 | uint64(b[2])<<16 | uint64(b[3])<<8 | uint64(b[4])
127+
case 6:
128+
s = uint64(b[0])<<40 | uint64(b[1])<<32 | uint64(b[2])<<24 | uint64(b[3])<<16 | uint64(b[4])<<8 | uint64(b[5])
129+
case 7:
130+
s = uint64(b[0])<<48 | uint64(b[1])<<40 | uint64(b[2])<<32 | uint64(b[3])<<24 | uint64(b[4])<<16 | uint64(b[5])<<8 | uint64(b[6])
131+
case 8:
132+
s = uint64(b[0])<<56 | uint64(b[1])<<48 | uint64(b[2])<<40 | uint64(b[3])<<32 | uint64(b[4])<<24 | uint64(b[5])<<16 | uint64(b[6])<<8 | uint64(b[7])
133+
}
134+
// Reject sizes < 56 (shouldn't have separate size) and sizes with
135+
// leading zero bytes.
136+
if s < 56 || b[0] == 0 {
137+
return 0, ErrCanonSize
138+
}
139+
return s, nil
140+
}

rlp/raw_test.go

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
// Copyright 2015 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package rlp
18+
19+
import (
20+
"bytes"
21+
"io"
22+
"reflect"
23+
"testing"
24+
)
25+
26+
func TestCountValues(t *testing.T) {
27+
tests := []struct {
28+
input string // note: spaces in input are stripped by unhex
29+
count int
30+
err error
31+
}{
32+
// simple cases
33+
{"", 0, nil},
34+
{"00", 1, nil},
35+
{"80", 1, nil},
36+
{"C0", 1, nil},
37+
{"01 02 03", 3, nil},
38+
{"01 C406070809 02", 3, nil},
39+
{"820101 820202 8403030303 04", 4, nil},
40+
41+
// size errors
42+
{"8142", 0, ErrCanonSize},
43+
{"01 01 8142", 0, ErrCanonSize},
44+
{"02 84020202", 0, ErrValueTooLarge},
45+
46+
{
47+
input: "A12000BF49F440A1CD0527E4D06E2765654C0F56452257516D793A9B8D604DCFDF2AB853F851808D10000000000000000000000000A056E81F171BCC55A6FF8345E692C0F86E5B48E01B996CADC001622FB5E363B421A0C5D2460186F7233C927E7DB2DCC703C0E500B653CA82273B7BFAD8045D85A470",
48+
count: 2,
49+
},
50+
}
51+
for i, test := range tests {
52+
count, err := CountValues(unhex(test.input))
53+
if count != test.count {
54+
t.Errorf("test %d: count mismatch, got %d want %d\ninput: %s", i, count, test.count, test.input)
55+
}
56+
if !reflect.DeepEqual(err, test.err) {
57+
t.Errorf("test %d: err mismatch, got %q want %q\ninput: %s", i, err, test.err, test.input)
58+
}
59+
}
60+
}
61+
62+
func TestSplitTypes(t *testing.T) {
63+
if _, _, err := SplitString(unhex("C100")); err != ErrExpectedString {
64+
t.Error("SplitString returned %q, want %q", err, ErrExpectedString)
65+
}
66+
if _, _, err := SplitList(unhex("01")); err != ErrExpectedList {
67+
t.Error("SplitString returned %q, want %q", err, ErrExpectedList)
68+
}
69+
if _, _, err := SplitList(unhex("81FF")); err != ErrExpectedList {
70+
t.Error("SplitString returned %q, want %q", err, ErrExpectedList)
71+
}
72+
}
73+
74+
func TestSplit(t *testing.T) {
75+
tests := []struct {
76+
input string
77+
kind Kind
78+
val, rest string
79+
err error
80+
}{
81+
{input: "01FFFF", kind: Byte, val: "01", rest: "FFFF"},
82+
{input: "80FFFF", kind: String, val: "", rest: "FFFF"},
83+
{input: "C3010203", kind: List, val: "010203"},
84+
85+
// errors
86+
{input: "", err: io.ErrUnexpectedEOF},
87+
88+
{input: "8141", err: ErrCanonSize, rest: "8141"},
89+
{input: "B800", err: ErrCanonSize, rest: "B800"},
90+
{input: "B802FFFF", err: ErrCanonSize, rest: "B802FFFF"},
91+
{input: "B90000", err: ErrCanonSize, rest: "B90000"},
92+
{input: "B90055", err: ErrCanonSize, rest: "B90055"},
93+
{input: "BA0002FFFF", err: ErrCanonSize, rest: "BA0002FFFF"},
94+
{input: "F800", err: ErrCanonSize, rest: "F800"},
95+
{input: "F90000", err: ErrCanonSize, rest: "F90000"},
96+
{input: "F90055", err: ErrCanonSize, rest: "F90055"},
97+
{input: "FA0002FFFF", err: ErrCanonSize, rest: "FA0002FFFF"},
98+
99+
{input: "8501010101", err: ErrValueTooLarge, rest: "8501010101"},
100+
{input: "C60607080902", err: ErrValueTooLarge, rest: "C60607080902"},
101+
102+
// size check overflow
103+
{input: "BFFFFFFFFFFFFFFFFF", err: ErrValueTooLarge, rest: "BFFFFFFFFFFFFFFFFF"},
104+
{input: "FFFFFFFFFFFFFFFFFF", err: ErrValueTooLarge, rest: "FFFFFFFFFFFFFFFFFF"},
105+
106+
{
107+
input: "B838FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
108+
err: ErrValueTooLarge,
109+
rest: "B838FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
110+
},
111+
{
112+
input: "F838FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
113+
err: ErrValueTooLarge,
114+
rest: "F838FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
115+
},
116+
117+
// a few bigger values, just for kicks
118+
{
119+
input: "F839FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
120+
kind: List,
121+
val: "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
122+
rest: "",
123+
},
124+
{
125+
input: "F90211A060EF29F20CC1007AE6E9530AEE16F4B31F8F1769A2D1264EC995C6D1241868D6A07C62AB8AC9838F5F5877B20BB37B387BC2106E97A3D52172CBEDB5EE17C36008A00EAB6B7324AADC0F6047C6AFC8229F09F7CF451B51D67C8DFB08D49BA8C3C626A04453343B2F3A6E42FCF87948F88AF7C8FC16D0C2735CBA7F026836239AB2C15FA024635C7291C882CE4C0763760C1A362DFC3FFCD802A55722236DE058D74202ACA0A220C808DE10F55E40AB25255201CFF009EA181D3906638E944EE2BF34049984A08D325AB26796F1CCB470F69C0F842501DC35D368A0C2575B2D243CFD1E8AB0FDA0B5298FF60DA5069463D610513C9F04F24051348391A143AFFAB7197DFACDEA72A02D2A7058A4463F8FB69378369E11EF33AE3252E2DB86CB545B36D3C26DDECE5AA0888F97BCA8E0BD83DC5B3B91CFF5FAF2F66F9501010682D67EF4A3B4E66115FBA0E8175A60C93BE9ED02921958F0EA55DA0FB5E4802AF5846147BAD92BC2D8AF26A08B3376FF433F3A4250FA64B7F804004CAC5807877D91C4427BD1CD05CF912ED8A09B32EF0F03BD13C37FF950C0CCCEFCCDD6669F2E7F2AA5CB859928E84E29763EA09BBA5E46610C8C8B1F8E921E5691BF8C7E40D75825D5EA3217AA9C3A8A355F39A0EEB95BC78251CCCEC54A97F19755C4A59A293544EEE6119AFA50531211E53C4FA00B6E86FE150BF4A9E0FEEE9C90F5465E617A861BB5E357F942881EE762212E2580",
126+
kind: List,
127+
val: "A060EF29F20CC1007AE6E9530AEE16F4B31F8F1769A2D1264EC995C6D1241868D6A07C62AB8AC9838F5F5877B20BB37B387BC2106E97A3D52172CBEDB5EE17C36008A00EAB6B7324AADC0F6047C6AFC8229F09F7CF451B51D67C8DFB08D49BA8C3C626A04453343B2F3A6E42FCF87948F88AF7C8FC16D0C2735CBA7F026836239AB2C15FA024635C7291C882CE4C0763760C1A362DFC3FFCD802A55722236DE058D74202ACA0A220C808DE10F55E40AB25255201CFF009EA181D3906638E944EE2BF34049984A08D325AB26796F1CCB470F69C0F842501DC35D368A0C2575B2D243CFD1E8AB0FDA0B5298FF60DA5069463D610513C9F04F24051348391A143AFFAB7197DFACDEA72A02D2A7058A4463F8FB69378369E11EF33AE3252E2DB86CB545B36D3C26DDECE5AA0888F97BCA8E0BD83DC5B3B91CFF5FAF2F66F9501010682D67EF4A3B4E66115FBA0E8175A60C93BE9ED02921958F0EA55DA0FB5E4802AF5846147BAD92BC2D8AF26A08B3376FF433F3A4250FA64B7F804004CAC5807877D91C4427BD1CD05CF912ED8A09B32EF0F03BD13C37FF950C0CCCEFCCDD6669F2E7F2AA5CB859928E84E29763EA09BBA5E46610C8C8B1F8E921E5691BF8C7E40D75825D5EA3217AA9C3A8A355F39A0EEB95BC78251CCCEC54A97F19755C4A59A293544EEE6119AFA50531211E53C4FA00B6E86FE150BF4A9E0FEEE9C90F5465E617A861BB5E357F942881EE762212E2580",
128+
rest: "",
129+
},
130+
{
131+
input: "F877A12000BF49F440A1CD0527E4D06E2765654C0F56452257516D793A9B8D604DCFDF2AB853F851808D10000000000000000000000000A056E81F171BCC55A6FF8345E692C0F86E5B48E01B996CADC001622FB5E363B421A0C5D2460186F7233C927E7DB2DCC703C0E500B653CA82273B7BFAD8045D85A470",
132+
kind: List,
133+
val: "A12000BF49F440A1CD0527E4D06E2765654C0F56452257516D793A9B8D604DCFDF2AB853F851808D10000000000000000000000000A056E81F171BCC55A6FF8345E692C0F86E5B48E01B996CADC001622FB5E363B421A0C5D2460186F7233C927E7DB2DCC703C0E500B653CA82273B7BFAD8045D85A470",
134+
rest: "",
135+
},
136+
}
137+
138+
for i, test := range tests {
139+
kind, val, rest, err := Split(unhex(test.input))
140+
if kind != test.kind {
141+
t.Errorf("test %d: kind mismatch: got %v, want %v", i, kind, test.kind)
142+
}
143+
if !bytes.Equal(val, unhex(test.val)) {
144+
t.Errorf("test %d: val mismatch: got %x, want %s", i, val, test.val)
145+
}
146+
if !bytes.Equal(rest, unhex(test.rest)) {
147+
t.Errorf("test %d: rest mismatch: got %x, want %s", i, rest, test.rest)
148+
}
149+
if err != test.err {
150+
t.Errorf("test %d: error mismatch: got %q, want %q", i, err, test.err)
151+
}
152+
}
153+
}
154+
155+
func TestReadSize(t *testing.T) {
156+
tests := []struct {
157+
input string
158+
slen byte
159+
size uint64
160+
err error
161+
}{
162+
{input: "", slen: 1, err: io.ErrUnexpectedEOF},
163+
{input: "FF", slen: 2, err: io.ErrUnexpectedEOF},
164+
{input: "00", slen: 1, err: ErrCanonSize},
165+
{input: "36", slen: 1, err: ErrCanonSize},
166+
{input: "37", slen: 1, err: ErrCanonSize},
167+
{input: "38", slen: 1, size: 0x38},
168+
{input: "FF", slen: 1, size: 0xFF},
169+
{input: "FFFF", slen: 2, size: 0xFFFF},
170+
{input: "FFFFFF", slen: 3, size: 0xFFFFFF},
171+
{input: "FFFFFFFF", slen: 4, size: 0xFFFFFFFF},
172+
{input: "FFFFFFFFFF", slen: 5, size: 0xFFFFFFFFFF},
173+
{input: "FFFFFFFFFFFF", slen: 6, size: 0xFFFFFFFFFFFF},
174+
{input: "FFFFFFFFFFFFFF", slen: 7, size: 0xFFFFFFFFFFFFFF},
175+
{input: "FFFFFFFFFFFFFFFF", slen: 8, size: 0xFFFFFFFFFFFFFFFF},
176+
{input: "0102", slen: 2, size: 0x0102},
177+
{input: "010203", slen: 3, size: 0x010203},
178+
{input: "01020304", slen: 4, size: 0x01020304},
179+
{input: "0102030405", slen: 5, size: 0x0102030405},
180+
{input: "010203040506", slen: 6, size: 0x010203040506},
181+
{input: "01020304050607", slen: 7, size: 0x01020304050607},
182+
{input: "0102030405060708", slen: 8, size: 0x0102030405060708},
183+
}
184+
185+
for _, test := range tests {
186+
size, err := readSize(unhex(test.input), test.slen)
187+
if err != test.err {
188+
t.Errorf("readSize(%s, %d): error mismatch: got %q, want %q", test.input, test.slen, err, test.err)
189+
continue
190+
}
191+
if size != test.size {
192+
t.Errorf("readSize(%s, %d): size mismatch: got %#x, want %#x", test.input, test.slen, size, test.size)
193+
}
194+
}
195+
}

0 commit comments

Comments
 (0)