Skip to content

Commit a829a56

Browse files
committed
rlp: add Stream.Raw
1 parent 81800ca commit a829a56

3 files changed

Lines changed: 59 additions & 12 deletions

File tree

‎rlp/decode.go‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,31 @@ func (s *Stream) Bytes() ([]byte, error) {
540540
}
541541
}
542542

543+
// Raw reads a raw encoded value including RLP type information.
544+
func (s *Stream) Raw() ([]byte, error) {
545+
kind, size, err := s.Kind()
546+
if err != nil {
547+
return nil, err
548+
}
549+
if kind == Byte {
550+
s.kind = -1 // rearm Kind
551+
return []byte{s.byteval}, nil
552+
}
553+
// the original header has already been read and is no longer
554+
// available. read content and put a new header in front of it.
555+
start := headsize(size)
556+
buf := make([]byte, uint64(start)+size)
557+
if err := s.readFull(buf[start:]); err != nil {
558+
return nil, err
559+
}
560+
if kind == String {
561+
puthead(buf, 0x80, 0xB8, size)
562+
} else {
563+
puthead(buf, 0xC0, 0xF7, size)
564+
}
565+
return buf, nil
566+
}
567+
543568
var errUintOverflow = errors.New("rlp: uint overflow")
544569

545570
// Uint reads an RLP string of up to 8 bytes and returns its contents

‎rlp/decode_test.go‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,20 @@ func TestStreamList(t *testing.T) {
165165
}
166166
}
167167

168+
func TestStreamRaw(t *testing.T) {
169+
s := NewStream(bytes.NewReader(unhex("C58401010101")))
170+
s.List()
171+
172+
want := unhex("8401010101")
173+
raw, err := s.Raw()
174+
if err != nil {
175+
t.Fatal(err)
176+
}
177+
if !bytes.Equal(want, raw) {
178+
t.Errorf("raw mismatch: got %x, want %x", raw, want)
179+
}
180+
}
181+
168182
func TestDecodeErrors(t *testing.T) {
169183
r := bytes.NewReader(nil)
170184

@@ -331,7 +345,7 @@ var decodeTests = []decodeTest{
331345
{input: "C109", ptr: new(*[]uint), value: &[]uint{9}},
332346
{input: "C58403030303", ptr: new(*[][]byte), value: &[][]byte{{3, 3, 3, 3}}},
333347

334-
// check that input position is advanced also empty values.
348+
// check that input position is advanced also for empty values.
335349
{input: "C3808005", ptr: new([]*uint), value: []*uint{nil, nil, uintp(5)}},
336350

337351
// pointer should be reset to nil

‎rlp/encode.go‎

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func (e flatenc) EncodeRLP(out io.Writer) error {
7070
newhead := eb.lheads[prevnheads]
7171
copy(eb.lheads[prevnheads:], eb.lheads[prevnheads+1:])
7272
eb.lheads = eb.lheads[:len(eb.lheads)-1]
73-
eb.lhsize -= newhead.tagsize()
73+
eb.lhsize -= headsize(uint64(newhead.size))
7474
return nil
7575
}
7676

@@ -155,21 +155,29 @@ type listhead struct {
155155
// encode writes head to the given buffer, which must be at least
156156
// 9 bytes long. It returns the encoded bytes.
157157
func (head *listhead) encode(buf []byte) []byte {
158-
if head.size < 56 {
159-
buf[0] = 0xC0 + byte(head.size)
160-
return buf[:1]
161-
} else {
162-
sizesize := putint(buf[1:], uint64(head.size))
163-
buf[0] = 0xF7 + byte(sizesize)
164-
return buf[:sizesize+1]
158+
return buf[:puthead(buf, 0xC0, 0xF7, uint64(head.size))]
159+
}
160+
161+
// headsize returns the size of a list or string header
162+
// for a value of the given size.
163+
func headsize(size uint64) int {
164+
if size < 56 {
165+
return 1
165166
}
167+
return 1 + intsize(size)
166168
}
167169

168-
func (head *listhead) tagsize() int {
169-
if head.size < 56 {
170+
// puthead writes a list or string header to buf.
171+
// buf must be at least 9 bytes long.
172+
func puthead(buf []byte, smalltag, largetag byte, size uint64) int {
173+
if size < 56 {
174+
buf[0] = smalltag + byte(size)
170175
return 1
176+
} else {
177+
sizesize := putint(buf[1:], size)
178+
buf[0] = largetag + byte(sizesize)
179+
return sizesize + 1
171180
}
172-
return 1 + intsize(uint64(head.size))
173181
}
174182

175183
func newencbuf() *encbuf {

0 commit comments

Comments
 (0)