From 24bb68e7cf546153436f1d38a7227fdf75d73343 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Wed, 9 Sep 2015 03:34:15 +0200 Subject: rlp: add RawValue --- rlp/decode.go | 11 +++++++++++ rlp/decode_test.go | 5 +++++ rlp/encode.go | 7 +++++++ rlp/encode_test.go | 5 +++++ rlp/raw.go | 12 +++++++++++- 5 files changed, 39 insertions(+), 1 deletion(-) diff --git a/rlp/decode.go b/rlp/decode.go index 1381f5274..c4d42c6fc 100644 --- a/rlp/decode.go +++ b/rlp/decode.go @@ -173,6 +173,8 @@ var ( func makeDecoder(typ reflect.Type, tags tags) (dec decoder, err error) { kind := typ.Kind() switch { + case typ == rawValueType: + return decodeRawValue, nil case typ.Implements(decoderInterface): return decodeDecoder, nil case kind != reflect.Ptr && reflect.PtrTo(typ).Implements(decoderInterface): @@ -203,6 +205,15 @@ func makeDecoder(typ reflect.Type, tags tags) (dec decoder, err error) { } } +func decodeRawValue(s *Stream, val reflect.Value) error { + r, err := s.Raw() + if err != nil { + return err + } + val.SetBytes(r) + return nil +} + func decodeUint(s *Stream, val reflect.Value) error { typ := val.Type() num, err := s.uint(typ.Bits()) diff --git a/rlp/decode_test.go b/rlp/decode_test.go index ce4f9ecc8..408f1a5a9 100644 --- a/rlp/decode_test.go +++ b/rlp/decode_test.go @@ -438,6 +438,11 @@ var decodeTests = []decodeTest{ error: "rlp: expected input string or byte for uint, decoding into (rlp.recstruct).Child.I", }, + // RawValue + {input: "01", ptr: new(RawValue), value: RawValue(unhex("01"))}, + {input: "82FFFF", ptr: new(RawValue), value: RawValue(unhex("82FFFF"))}, + {input: "C20102", ptr: new([]RawValue), value: []RawValue{unhex("01"), unhex("02")}}, + // pointers {input: "00", ptr: new(*[]byte), value: &[]byte{0}}, {input: "80", ptr: new(*uint), value: uintp(0)}, diff --git a/rlp/encode.go b/rlp/encode.go index a0531af01..2aeee4721 100644 --- a/rlp/encode.go +++ b/rlp/encode.go @@ -354,6 +354,8 @@ var ( func makeWriter(typ reflect.Type) (writer, error) { kind := typ.Kind() switch { + case typ == rawValueType: + return writeRawValue, nil case typ.Implements(encoderInterface): return writeEncoder, nil case kind != reflect.Ptr && reflect.PtrTo(typ).Implements(encoderInterface): @@ -389,6 +391,11 @@ func isByte(typ reflect.Type) bool { return typ.Kind() == reflect.Uint8 && !typ.Implements(encoderInterface) } +func writeRawValue(val reflect.Value, w *encbuf) error { + w.str = append(w.str, val.Bytes()...) + return nil +} + func writeUint(val reflect.Value, w *encbuf) error { i := val.Uint() if i == 0 { diff --git a/rlp/encode_test.go b/rlp/encode_test.go index b550d4303..a3f30d804 100644 --- a/rlp/encode_test.go +++ b/rlp/encode_test.go @@ -204,6 +204,11 @@ var encTests = []encTest{ output: "F90200CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376", }, + // RawValue + {val: RawValue(unhex("01")), output: "01"}, + {val: RawValue(unhex("82FFFF")), output: "82FFFF"}, + {val: []RawValue{unhex("01"), unhex("02")}, output: "C20102"}, + // structs {val: simplestruct{}, output: "C28080"}, {val: simplestruct{A: 3, B: "foo"}, output: "C50383666F6F"}, diff --git a/rlp/raw.go b/rlp/raw.go index e93c4df40..fca445618 100644 --- a/rlp/raw.go +++ b/rlp/raw.go @@ -16,7 +16,17 @@ package rlp -import "io" +import ( + "io" + "reflect" +) + +// RawValue represents an encoded RLP value and can be used to delay +// RLP decoding or precompute an encoding. Note that the decoder does +// not verify whether the content of RawValues is valid RLP. +type RawValue []byte + +var rawValueType = reflect.TypeOf(RawValue{}) // Split returns the content of first RLP value and any // bytes after the value as subslices of b. -- cgit v1.2.3