github.com/gochain-io/gochain@v2.2.26+incompatible/rlp/decode.go (about) 1 // Copyright 2014 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 "bufio" 21 "bytes" 22 "encoding/binary" 23 "errors" 24 "fmt" 25 "io" 26 "math/big" 27 "reflect" 28 "strings" 29 "sync" 30 ) 31 32 var ( 33 errNoPointer = errors.New("rlp: interface given to Decode must be a pointer") 34 errDecodeIntoNil = errors.New("rlp: pointer given to Decode must not be nil") 35 ) 36 37 // Decoder is implemented by types that require custom RLP 38 // decoding rules or need to decode into private fields. 39 // 40 // The DecodeRLP method should read one value from the given 41 // Stream. It is not forbidden to read less or more, but it might 42 // be confusing. 43 type Decoder interface { 44 DecodeRLP(*Stream) error 45 } 46 47 // SliceDecoder is implemented by types that require custom RLP decoding for 48 // their elements. 49 type SliceDecoder interface { 50 // DecodeRLPElem decodes a single element from the stream and appends it. 51 DecodeRLPElem(*Stream) error 52 } 53 54 // Decode parses RLP-encoded data from r and stores the result in the 55 // value pointed to by val. Val must be a non-nil pointer. If r does 56 // not implement ByteReader, Decode will do its own buffering. 57 // 58 // Decode uses the following type-dependent decoding rules: 59 // 60 // If the type implements the Decoder interface, decode calls 61 // DecodeRLP. 62 // 63 // To decode into a pointer, Decode will decode into the value pointed 64 // to. If the pointer is nil, a new value of the pointer's element 65 // type is allocated. If the pointer is non-nil, the existing value 66 // will be reused. 67 // 68 // To decode into a struct, Decode expects the input to be an RLP 69 // list. The decoded elements of the list are assigned to each public 70 // field in the order given by the struct's definition. The input list 71 // must contain an element for each decoded field. Decode returns an 72 // error if there are too few or too many elements. 73 // 74 // The decoding of struct fields honours certain struct tags, "tail", 75 // "nil" and "-". 76 // 77 // The "-" tag ignores fields. 78 // 79 // For an explanation of "tail", see the example. 80 // 81 // The "nil" tag applies to pointer-typed fields and changes the decoding 82 // rules for the field such that input values of size zero decode as a nil 83 // pointer. This tag can be useful when decoding recursive types. 84 // 85 // type StructWithEmptyOK struct { 86 // Foo *[20]byte `rlp:"nil"` 87 // } 88 // 89 // To decode into a slice, the input must be a list and the resulting 90 // slice will contain the input elements in order. For byte slices, 91 // the input must be an RLP string. Array types decode similarly, with 92 // the additional restriction that the number of input elements (or 93 // bytes) must match the array's length. 94 // 95 // To decode into a Go string, the input must be an RLP string. The 96 // input bytes are taken as-is and will not necessarily be valid UTF-8. 97 // 98 // To decode into an unsigned integer type, the input must also be an RLP 99 // string. The bytes are interpreted as a big endian representation of 100 // the integer. If the RLP string is larger than the bit size of the 101 // type, Decode will return an error. Decode also supports *big.Int. 102 // There is no size limit for big integers. 103 // 104 // To decode into an interface value, Decode stores one of these 105 // in the value: 106 // 107 // []interface{}, for RLP lists 108 // []byte, for RLP strings 109 // 110 // Non-empty interface types are not supported, nor are booleans, 111 // signed integers, floating point numbers, maps, channels and 112 // functions. 113 // 114 // Note that Decode does not set an input limit for all readers 115 // and may be vulnerable to panics cause by huge value sizes. If 116 // you need an input limit, use 117 // 118 // NewStream(r, limit).Decode(val) 119 func Decode(r io.Reader, val interface{}) error { 120 s := NewStream(r, 0) 121 err := s.Decode(val) 122 Discard(s) 123 return err 124 } 125 126 // DecodeBytes parses RLP data from b into val. 127 // Please see the documentation of Decode for the decoding rules. 128 // The input must contain exactly one value and no trailing data. 129 func DecodeBytes(b []byte, val interface{}) error { 130 r := bytes.NewReader(b) 131 s := NewStream(r, uint64(len(b))) 132 defer Discard(s) 133 if err := s.Decode(val); err != nil { 134 return err 135 } 136 if r.Len() > 0 { 137 return ErrMoreThanOneValue 138 } 139 return nil 140 } 141 142 type decodeError struct { 143 msg string 144 typ reflect.Type 145 ctx []string 146 } 147 148 func (err *decodeError) Error() string { 149 ctx := "" 150 if len(err.ctx) > 0 { 151 ctx = ", decoding into " 152 for i := len(err.ctx) - 1; i >= 0; i-- { 153 ctx += err.ctx[i] 154 } 155 } 156 return fmt.Sprintf("rlp: %s for %v%s", err.msg, err.typ, ctx) 157 } 158 159 func wrapStreamError(err error, typ reflect.Type) error { 160 switch err { 161 case ErrCanonInt: 162 return &decodeError{msg: "non-canonical integer (leading zero bytes)", typ: typ} 163 case ErrCanonSize: 164 return &decodeError{msg: "non-canonical size information", typ: typ} 165 case ErrExpectedList: 166 return &decodeError{msg: "expected input list", typ: typ} 167 case ErrExpectedString: 168 return &decodeError{msg: "expected input string or byte", typ: typ} 169 case errUintOverflow: 170 return &decodeError{msg: "input string too long", typ: typ} 171 case errNotAtEOL: 172 return &decodeError{msg: "input list has too many elements", typ: typ} 173 } 174 return err 175 } 176 177 func addErrorContext(err error, ctx string) error { 178 if decErr, ok := err.(*decodeError); ok { 179 decErr.ctx = append(decErr.ctx, ctx) 180 } 181 return err 182 } 183 184 var ( 185 decoderInterface = reflect.TypeOf(new(Decoder)).Elem() 186 sliceDecoderInterface = reflect.TypeOf(new(SliceDecoder)).Elem() 187 bigInt = reflect.TypeOf(big.Int{}) 188 ) 189 190 func makeDecoder(typ reflect.Type, tags tags) (dec decoder, err error) { 191 kind := typ.Kind() 192 switch { 193 case typ == rawValueType: 194 return decodeRawValue, nil 195 case typ.Implements(decoderInterface): 196 return decodeDecoder, nil 197 case reflect.PtrTo(typ).Implements(sliceDecoderInterface): 198 return makeListSliceDecoder(tags), nil 199 case kind != reflect.Ptr && reflect.PtrTo(typ).Implements(decoderInterface): 200 return decodeDecoderNoPtr, nil 201 case typ.AssignableTo(reflect.PtrTo(bigInt)): 202 return decodeBigInt, nil 203 case typ.AssignableTo(bigInt): 204 return decodeBigIntNoPtr, nil 205 case isUint(kind): 206 return decodeUint, nil 207 case kind == reflect.Bool: 208 return decodeBool, nil 209 case kind == reflect.String: 210 return decodeString, nil 211 case kind == reflect.Slice || kind == reflect.Array: 212 return makeListDecoder(typ, tags) 213 case kind == reflect.Struct: 214 return makeStructDecoder(typ) 215 case kind == reflect.Ptr: 216 if tags.nilOK { 217 return makeOptionalPtrDecoder(typ) 218 } 219 return makePtrDecoder(typ) 220 case kind == reflect.Interface: 221 return decodeInterface, nil 222 default: 223 return nil, fmt.Errorf("rlp: type %v is not RLP-serializable", typ) 224 } 225 } 226 227 func decodeRawValue(s *Stream, val reflect.Value) error { 228 r, err := s.Raw() 229 if err != nil { 230 return err 231 } 232 val.SetBytes(r) 233 return nil 234 } 235 236 func decodeUint(s *Stream, val reflect.Value) error { 237 typ := val.Type() 238 num, err := s.uint(typ.Bits()) 239 if err != nil { 240 return wrapStreamError(err, val.Type()) 241 } 242 val.SetUint(num) 243 return nil 244 } 245 246 func decodeBool(s *Stream, val reflect.Value) error { 247 b, err := s.Bool() 248 if err != nil { 249 return wrapStreamError(err, val.Type()) 250 } 251 val.SetBool(b) 252 return nil 253 } 254 255 func decodeString(s *Stream, val reflect.Value) error { 256 b, err := s.Bytes() 257 if err != nil { 258 return wrapStreamError(err, val.Type()) 259 } 260 val.SetString(string(b)) 261 return nil 262 } 263 264 func decodeBigIntNoPtr(s *Stream, val reflect.Value) error { 265 return decodeBigInt(s, val.Addr()) 266 } 267 268 func decodeBigInt(s *Stream, val reflect.Value) error { 269 b, err := s.Bytes() 270 if err != nil { 271 return wrapStreamError(err, val.Type()) 272 } 273 i := val.Interface().(*big.Int) 274 if i == nil { 275 i = new(big.Int) 276 val.Set(reflect.ValueOf(i)) 277 } 278 // Reject leading zero bytes 279 if len(b) > 0 && b[0] == 0 { 280 return wrapStreamError(ErrCanonInt, val.Type()) 281 } 282 i.SetBytes(b) 283 return nil 284 } 285 286 func makeListDecoder(typ reflect.Type, tag tags) (decoder, error) { 287 etype := typ.Elem() 288 if etype.Kind() == reflect.Uint8 && !reflect.PtrTo(etype).Implements(decoderInterface) { 289 if typ.Kind() == reflect.Array { 290 return decodeByteArray, nil 291 } else { 292 return decodeByteSlice, nil 293 } 294 } 295 etypeinfo, err := cachedTypeInfo1(etype, tags{}) 296 if err != nil { 297 return nil, err 298 } 299 var dec decoder 300 switch { 301 case typ.Kind() == reflect.Array: 302 dec = func(s *Stream, val reflect.Value) error { 303 return decodeListArray(s, val, etypeinfo.decoder) 304 } 305 case tag.tail: 306 // A slice with "tail" tag can occur as the last field 307 // of a struct and is supposed to swallow all remaining 308 // list elements. The struct decoder already called s.List, 309 // proceed directly to decoding the elements. 310 dec = func(s *Stream, val reflect.Value) error { 311 return decodeSliceElems(s, val, etypeinfo.decoder) 312 } 313 default: 314 dec = func(s *Stream, val reflect.Value) error { 315 return decodeListSlice(s, val, etypeinfo.decoder) 316 } 317 } 318 return dec, nil 319 } 320 321 func decodeListSlice(s *Stream, val reflect.Value, elemdec decoder) error { 322 size, err := s.List() 323 if err != nil { 324 return wrapStreamError(err, val.Type()) 325 } 326 if size == 0 { 327 val.Set(reflect.MakeSlice(val.Type(), 0, 0)) 328 return s.ListEnd() 329 } 330 if err := decodeSliceElems(s, val, elemdec); err != nil { 331 return err 332 } 333 return s.ListEnd() 334 } 335 336 func decodeSliceElems(s *Stream, val reflect.Value, elemdec decoder) error { 337 i := 0 338 for ; ; i++ { 339 // grow slice if necessary 340 if i >= val.Cap() { 341 newcap := val.Cap() + val.Cap()/2 342 if newcap < 4 { 343 newcap = 4 344 } 345 newv := reflect.MakeSlice(val.Type(), val.Len(), newcap) 346 reflect.Copy(newv, val) 347 val.Set(newv) 348 } 349 if i >= val.Len() { 350 val.SetLen(i + 1) 351 } 352 // decode into element 353 if err := elemdec(s, val.Index(i)); err == EOL { 354 break 355 } else if err != nil { 356 return addErrorContext(err, fmt.Sprint("[", i, "]")) 357 } 358 } 359 if i < val.Len() { 360 val.SetLen(i) 361 } 362 return nil 363 } 364 365 func makeListSliceDecoder(tag tags) decoder { 366 var dec decoder 367 switch { 368 case tag.tail: 369 // A slice with "tail" tag can occur as the last field 370 // of a struct and is supposed to swallow all remaining 371 // list elements. The struct decoder already called s.List, 372 // proceed directly to decoding the elements. 373 dec = func(s *Stream, val reflect.Value) error { 374 return decodeSliceDecoderElems(s, val) 375 } 376 default: 377 dec = func(s *Stream, val reflect.Value) error { 378 return decodeListSliceDecoder(s, val) 379 } 380 } 381 return dec 382 } 383 384 func decodeListSliceDecoder(s *Stream, val reflect.Value) error { 385 if _, err := s.List(); err != nil { 386 return wrapStreamError(err, val.Type()) 387 } 388 if err := decodeSliceDecoderElems(s, val); err != nil { 389 return err 390 } 391 return s.ListEnd() 392 } 393 394 func decodeSliceDecoderElems(s *Stream, val reflect.Value) error { 395 dec := val.Addr().Interface().(SliceDecoder) 396 for i := 0; ; i++ { 397 // decode into element 398 if err := dec.DecodeRLPElem(s); err == EOL { 399 break 400 } else if err != nil { 401 return addErrorContext(err, fmt.Sprint("[", i, "]")) 402 } 403 } 404 return nil 405 } 406 407 func decodeListArray(s *Stream, val reflect.Value, elemdec decoder) error { 408 if _, err := s.List(); err != nil { 409 return wrapStreamError(err, val.Type()) 410 } 411 vlen := val.Len() 412 i := 0 413 for ; i < vlen; i++ { 414 if err := elemdec(s, val.Index(i)); err == EOL { 415 break 416 } else if err != nil { 417 return addErrorContext(err, fmt.Sprint("[", i, "]")) 418 } 419 } 420 if i < vlen { 421 return &decodeError{msg: "input list has too few elements", typ: val.Type()} 422 } 423 return wrapStreamError(s.ListEnd(), val.Type()) 424 } 425 426 func decodeByteSlice(s *Stream, val reflect.Value) error { 427 b, err := s.Bytes() 428 if err != nil { 429 return wrapStreamError(err, val.Type()) 430 } 431 val.SetBytes(b) 432 return nil 433 } 434 435 func decodeByteArray(s *Stream, val reflect.Value) error { 436 kind, size, err := s.Kind() 437 if err != nil { 438 return err 439 } 440 vlen := val.Len() 441 switch kind { 442 case Byte: 443 if vlen == 0 { 444 return &decodeError{msg: "input string too long", typ: val.Type()} 445 } 446 if vlen > 1 { 447 return &decodeError{msg: "input string too short", typ: val.Type()} 448 } 449 bv, _ := s.Uint() 450 val.Index(0).SetUint(bv) 451 case String: 452 if uint64(vlen) < size { 453 return &decodeError{msg: "input string too long", typ: val.Type()} 454 } 455 if uint64(vlen) > size { 456 return &decodeError{msg: "input string too short", typ: val.Type()} 457 } 458 slice := val.Slice(0, vlen).Interface().([]byte) 459 if err := s.readFull(slice); err != nil { 460 return err 461 } 462 // Reject cases where single byte encoding should have been used. 463 if size == 1 && slice[0] < 128 { 464 return wrapStreamError(ErrCanonSize, val.Type()) 465 } 466 case List: 467 return wrapStreamError(ErrExpectedString, val.Type()) 468 } 469 return nil 470 } 471 472 func makeStructDecoder(typ reflect.Type) (decoder, error) { 473 fields, err := structFields(typ) 474 if err != nil { 475 return nil, err 476 } 477 dec := func(s *Stream, val reflect.Value) (err error) { 478 if _, err := s.List(); err != nil { 479 return wrapStreamError(err, typ) 480 } 481 for _, f := range fields { 482 err := f.info.decoder(s, val.Field(f.index)) 483 if err == EOL { 484 return &decodeError{msg: "too few elements", typ: typ} 485 } else if err != nil { 486 return addErrorContext(err, "."+typ.Field(f.index).Name) 487 } 488 } 489 return wrapStreamError(s.ListEnd(), typ) 490 } 491 return dec, nil 492 } 493 494 // makePtrDecoder creates a decoder that decodes into 495 // the pointer's element type. 496 func makePtrDecoder(typ reflect.Type) (decoder, error) { 497 etype := typ.Elem() 498 etypeinfo, err := cachedTypeInfo1(etype, tags{}) 499 if err != nil { 500 return nil, err 501 } 502 dec := func(s *Stream, val reflect.Value) (err error) { 503 newval := val 504 if val.IsNil() { 505 newval = reflect.New(etype) 506 } 507 if err = etypeinfo.decoder(s, newval.Elem()); err == nil { 508 val.Set(newval) 509 } 510 return err 511 } 512 return dec, nil 513 } 514 515 // makeOptionalPtrDecoder creates a decoder that decodes empty values 516 // as nil. Non-empty values are decoded into a value of the element type, 517 // just like makePtrDecoder does. 518 // 519 // This decoder is used for pointer-typed struct fields with struct tag "nil". 520 func makeOptionalPtrDecoder(typ reflect.Type) (decoder, error) { 521 etype := typ.Elem() 522 etypeinfo, err := cachedTypeInfo1(etype, tags{}) 523 if err != nil { 524 return nil, err 525 } 526 dec := func(s *Stream, val reflect.Value) (err error) { 527 kind, size, err := s.Kind() 528 if err != nil || size == 0 && kind != Byte { 529 // rearm s.Kind. This is important because the input 530 // position must advance to the next value even though 531 // we don't read anything. 532 s.kind = -1 533 // set the pointer to nil. 534 val.Set(reflect.Zero(typ)) 535 return err 536 } 537 newval := val 538 if val.IsNil() { 539 newval = reflect.New(etype) 540 } 541 if err = etypeinfo.decoder(s, newval.Elem()); err == nil { 542 val.Set(newval) 543 } 544 return err 545 } 546 return dec, nil 547 } 548 549 var ifsliceType = reflect.TypeOf([]interface{}{}) 550 551 func decodeInterface(s *Stream, val reflect.Value) error { 552 if val.Type().NumMethod() != 0 { 553 return fmt.Errorf("rlp: type %v is not RLP-serializable", val.Type()) 554 } 555 kind, _, err := s.Kind() 556 if err != nil { 557 return err 558 } 559 if kind == List { 560 slice := reflect.New(ifsliceType).Elem() 561 if err := decodeListSlice(s, slice, decodeInterface); err != nil { 562 return err 563 } 564 val.Set(slice) 565 } else { 566 b, err := s.Bytes() 567 if err != nil { 568 return err 569 } 570 val.Set(reflect.ValueOf(b)) 571 } 572 return nil 573 } 574 575 // This decoder is used for non-pointer values of types 576 // that implement the Decoder interface using a pointer receiver. 577 func decodeDecoderNoPtr(s *Stream, val reflect.Value) error { 578 return val.Addr().Interface().(Decoder).DecodeRLP(s) 579 } 580 581 func decodeDecoder(s *Stream, val reflect.Value) error { 582 // Decoder instances are not handled using the pointer rule if the type 583 // implements Decoder with pointer receiver (i.e. always) 584 // because it might handle empty values specially. 585 // We need to allocate one here in this case, like makePtrDecoder does. 586 if val.Kind() == reflect.Ptr && val.IsNil() { 587 val.Set(reflect.New(val.Type().Elem())) 588 } 589 return val.Interface().(Decoder).DecodeRLP(s) 590 } 591 592 // Kind represents the kind of value contained in an RLP stream. 593 type Kind int 594 595 const ( 596 Byte Kind = iota 597 String 598 List 599 ) 600 601 func (k Kind) String() string { 602 switch k { 603 case Byte: 604 return "Byte" 605 case String: 606 return "String" 607 case List: 608 return "List" 609 default: 610 return fmt.Sprintf("Unknown(%d)", k) 611 } 612 } 613 614 var ( 615 // EOL is returned when the end of the current list 616 // has been reached during streaming. 617 EOL = errors.New("rlp: end of list") 618 619 // Actual Errors 620 ErrExpectedString = errors.New("rlp: expected String or Byte") 621 ErrExpectedList = errors.New("rlp: expected List") 622 ErrCanonInt = errors.New("rlp: non-canonical integer format") 623 ErrCanonSize = errors.New("rlp: non-canonical size information") 624 ErrElemTooLarge = errors.New("rlp: element is larger than containing list") 625 ErrValueTooLarge = errors.New("rlp: value size exceeds available input length") 626 627 // This error is reported by DecodeBytes if the slice contains 628 // additional data after the first RLP value. 629 ErrMoreThanOneValue = errors.New("rlp: input contains more than one value") 630 631 // internal errors 632 errNotInList = errors.New("rlp: call of ListEnd outside of any list") 633 errNotAtEOL = errors.New("rlp: call of ListEnd not positioned at EOL") 634 errUintOverflow = errors.New("rlp: uint overflow") 635 ) 636 637 // ByteReader must be implemented by any input reader for a Stream. It 638 // is implemented by e.g. bufio.Reader and bytes.Reader. 639 type ByteReader interface { 640 io.Reader 641 io.ByteReader 642 } 643 644 // Stream can be used for piecemeal decoding of an input stream. This 645 // is useful if the input is very large or if the decoding rules for a 646 // type depend on the input structure. Stream does not keep an 647 // internal buffer. After decoding a value, the input reader will be 648 // positioned just before the type information for the next value. 649 // 650 // When decoding a list and the input position reaches the declared 651 // length of the list, all operations will return error EOL. 652 // The end of the list must be acknowledged using ListEnd to continue 653 // reading the enclosing list. 654 // 655 // Stream is not safe for concurrent use. 656 type Stream struct { 657 r ByteReader 658 659 // number of bytes remaining to be read from r. 660 remaining uint64 661 limited bool 662 663 // auxiliary buffer for integer decoding 664 uintbuf []byte 665 666 kind Kind // kind of value ahead 667 size uint64 // size of value ahead 668 byteval byte // value of single byte in type tag 669 kinderr error // error from last readKind 670 stack []listpos 671 } 672 673 type listpos struct{ pos, size uint64 } 674 675 var streamPool = sync.Pool{ 676 New: func() interface{} { return new(Stream) }, 677 } 678 679 // Discard returns s to the pool. 680 func Discard(s *Stream) { 681 streamPool.Put(s) 682 } 683 684 // NewStream creates a new decoding stream reading from r. 685 // 686 // If r implements the ByteReader interface, Stream will 687 // not introduce any buffering. 688 // 689 // For non-toplevel values, Stream returns ErrElemTooLarge 690 // for values that do not fit into the enclosing list. 691 // 692 // Stream supports an optional input limit. If a limit is set, the 693 // size of any toplevel value will be checked against the remaining 694 // input length. Stream operations that encounter a value exceeding 695 // the remaining input length will return ErrValueTooLarge. The limit 696 // can be set by passing a non-zero value for inputLimit. 697 // 698 // If r is a bytes.Reader or strings.Reader, the input limit is set to 699 // the length of r's underlying data unless an explicit limit is 700 // provided. 701 // 702 // Discarded Streams should be returned to the pool via Discard(*Stream). 703 func NewStream(r io.Reader, inputLimit uint64) *Stream { 704 s := streamPool.Get().(*Stream) 705 s.Reset(r, inputLimit) 706 return s 707 } 708 709 // NewListStream creates a new stream that pretends to be positioned 710 // at an encoded list of the given length. 711 func NewListStream(r io.Reader, len uint64) *Stream { 712 s := streamPool.Get().(*Stream) 713 s.Reset(r, len) 714 s.kind = List 715 s.size = len 716 return s 717 } 718 719 // Bytes reads an RLP string and returns its contents as a byte slice. 720 // If the input does not contain an RLP string, the returned 721 // error will be ErrExpectedString. 722 func (s *Stream) Bytes() ([]byte, error) { 723 kind, size, err := s.Kind() 724 if err != nil { 725 return nil, err 726 } 727 switch kind { 728 case Byte: 729 s.kind = -1 // rearm Kind 730 return []byte{s.byteval}, nil 731 case String: 732 b := make([]byte, size) 733 if err = s.readFull(b); err != nil { 734 return nil, err 735 } 736 if size == 1 && b[0] < 128 { 737 return nil, ErrCanonSize 738 } 739 return b, nil 740 default: 741 return nil, ErrExpectedString 742 } 743 } 744 745 // Raw reads a raw encoded value including RLP type information. 746 func (s *Stream) Raw() ([]byte, error) { 747 kind, size, err := s.Kind() 748 if err != nil { 749 return nil, err 750 } 751 if kind == Byte { 752 s.kind = -1 // rearm Kind 753 return []byte{s.byteval}, nil 754 } 755 // the original header has already been read and is no longer 756 // available. read content and put a new header in front of it. 757 start := headsize(size) 758 buf := make([]byte, uint64(start)+size) 759 if err := s.readFull(buf[start:]); err != nil { 760 return nil, err 761 } 762 if kind == String { 763 puthead(buf, 0x80, 0xB7, size) 764 } else { 765 puthead(buf, 0xC0, 0xF7, size) 766 } 767 return buf, nil 768 } 769 770 // Uint reads an RLP string of up to 8 bytes and returns its contents 771 // as an unsigned integer. If the input does not contain an RLP string, the 772 // returned error will be ErrExpectedString. 773 func (s *Stream) Uint() (uint64, error) { 774 return s.uint(64) 775 } 776 777 func (s *Stream) uint(maxbits int) (uint64, error) { 778 kind, size, err := s.Kind() 779 if err != nil { 780 return 0, err 781 } 782 switch kind { 783 case Byte: 784 if s.byteval == 0 { 785 return 0, ErrCanonInt 786 } 787 s.kind = -1 // rearm Kind 788 return uint64(s.byteval), nil 789 case String: 790 if size > uint64(maxbits/8) { 791 return 0, errUintOverflow 792 } 793 v, err := s.readUint(byte(size)) 794 switch { 795 case err == ErrCanonSize: 796 // Adjust error because we're not reading a size right now. 797 return 0, ErrCanonInt 798 case err != nil: 799 return 0, err 800 case size > 0 && v < 128: 801 return 0, ErrCanonSize 802 default: 803 return v, nil 804 } 805 default: 806 return 0, ErrExpectedString 807 } 808 } 809 810 // Bool reads an RLP string of up to 1 byte and returns its contents 811 // as a boolean. If the input does not contain an RLP string, the 812 // returned error will be ErrExpectedString. 813 func (s *Stream) Bool() (bool, error) { 814 num, err := s.uint(8) 815 if err != nil { 816 return false, err 817 } 818 switch num { 819 case 0: 820 return false, nil 821 case 1: 822 return true, nil 823 default: 824 return false, fmt.Errorf("rlp: invalid boolean value: %d", num) 825 } 826 } 827 828 // List starts decoding an RLP list. If the input does not contain a 829 // list, the returned error will be ErrExpectedList. When the list's 830 // end has been reached, any Stream operation will return EOL. 831 func (s *Stream) List() (size uint64, err error) { 832 kind, size, err := s.Kind() 833 if err != nil { 834 return 0, err 835 } 836 if kind != List { 837 return 0, ErrExpectedList 838 } 839 s.stack = append(s.stack, listpos{0, size}) 840 s.kind = -1 841 s.size = 0 842 return size, nil 843 } 844 845 // ListEnd returns to the enclosing list. 846 // The input reader must be positioned at the end of a list. 847 func (s *Stream) ListEnd() error { 848 if len(s.stack) == 0 { 849 return errNotInList 850 } 851 tos := s.stack[len(s.stack)-1] 852 if tos.pos != tos.size { 853 return errNotAtEOL 854 } 855 s.stack = s.stack[:len(s.stack)-1] // pop 856 if len(s.stack) > 0 { 857 s.stack[len(s.stack)-1].pos += tos.size 858 } 859 s.kind = -1 860 s.size = 0 861 return nil 862 } 863 864 // Decode decodes a value and stores the result in the value pointed 865 // to by val. Please see the documentation for the Decode function 866 // to learn about the decoding rules. 867 func (s *Stream) Decode(val interface{}) error { 868 if val == nil { 869 return errDecodeIntoNil 870 } 871 rval := reflect.ValueOf(val) 872 rtyp := rval.Type() 873 if rtyp.Kind() != reflect.Ptr { 874 return errNoPointer 875 } 876 if rval.IsNil() { 877 return errDecodeIntoNil 878 } 879 info, err := cachedTypeInfo(rtyp.Elem(), tags{}) 880 if err != nil { 881 return err 882 } 883 884 err = info.decoder(s, rval.Elem()) 885 if decErr, ok := err.(*decodeError); ok && len(decErr.ctx) > 0 { 886 // add decode target type to error so context has more meaning 887 decErr.ctx = append(decErr.ctx, fmt.Sprint("(", rtyp.Elem(), ")")) 888 } 889 return err 890 } 891 892 // Reset discards any information about the current decoding context 893 // and starts reading from r. This method is meant to facilitate reuse 894 // of a preallocated Stream across many decoding operations. 895 // 896 // If r does not also implement ByteReader, Stream will do its own 897 // buffering. 898 func (s *Stream) Reset(r io.Reader, inputLimit uint64) { 899 if inputLimit > 0 { 900 s.remaining = inputLimit 901 s.limited = true 902 } else { 903 // Attempt to automatically discover 904 // the limit when reading from a byte slice. 905 switch br := r.(type) { 906 case *bytes.Reader: 907 s.remaining = uint64(br.Len()) 908 s.limited = true 909 case *strings.Reader: 910 s.remaining = uint64(br.Len()) 911 s.limited = true 912 default: 913 s.limited = false 914 } 915 } 916 // Wrap r with a buffer if it doesn't have one. 917 bufr, ok := r.(ByteReader) 918 if !ok { 919 bufr = bufio.NewReader(r) 920 } 921 s.r = bufr 922 // Reset the decoding context. 923 s.stack = s.stack[:0] 924 s.size = 0 925 s.kind = -1 926 s.kinderr = nil 927 if s.uintbuf == nil { 928 s.uintbuf = make([]byte, 8) 929 } 930 } 931 932 // Kind returns the kind and size of the next value in the 933 // input stream. 934 // 935 // The returned size is the number of bytes that make up the value. 936 // For kind == Byte, the size is zero because the value is 937 // contained in the type tag. 938 // 939 // The first call to Kind will read size information from the input 940 // reader and leave it positioned at the start of the actual bytes of 941 // the value. Subsequent calls to Kind (until the value is decoded) 942 // will not advance the input reader and return cached information. 943 func (s *Stream) Kind() (kind Kind, size uint64, err error) { 944 var tos *listpos 945 if len(s.stack) > 0 { 946 tos = &s.stack[len(s.stack)-1] 947 } 948 if s.kind < 0 { 949 s.kinderr = nil 950 // Don't read further if we're at the end of the 951 // innermost list. 952 if tos != nil && tos.pos == tos.size { 953 return 0, 0, EOL 954 } 955 s.kind, s.size, s.kinderr = s.readKind() 956 if s.kinderr == nil { 957 if tos == nil { 958 // At toplevel, check that the value is smaller 959 // than the remaining input length. 960 if s.limited && s.size > s.remaining { 961 s.kinderr = ErrValueTooLarge 962 } 963 } else { 964 // Inside a list, check that the value doesn't overflow the list. 965 if s.size > tos.size-tos.pos { 966 s.kinderr = ErrElemTooLarge 967 } 968 } 969 } 970 } 971 // Note: this might return a sticky error generated 972 // by an earlier call to readKind. 973 return s.kind, s.size, s.kinderr 974 } 975 976 func (s *Stream) readKind() (kind Kind, size uint64, err error) { 977 b, err := s.readByte() 978 if err != nil { 979 if len(s.stack) == 0 { 980 // At toplevel, Adjust the error to actual EOF. io.EOF is 981 // used by callers to determine when to stop decoding. 982 switch err { 983 case io.ErrUnexpectedEOF: 984 err = io.EOF 985 case ErrValueTooLarge: 986 err = io.EOF 987 } 988 } 989 return 0, 0, err 990 } 991 s.byteval = 0 992 switch { 993 case b < 0x80: 994 // For a single byte whose value is in the [0x00, 0x7F] range, that byte 995 // is its own RLP encoding. 996 s.byteval = b 997 return Byte, 0, nil 998 case b < 0xB8: 999 // Otherwise, if a string is 0-55 bytes long, 1000 // the RLP encoding consists of a single byte with value 0x80 plus the 1001 // length of the string followed by the string. The range of the first 1002 // byte is thus [0x80, 0xB7]. 1003 return String, uint64(b - 0x80), nil 1004 case b < 0xC0: 1005 // If a string is more than 55 bytes long, the 1006 // RLP encoding consists of a single byte with value 0xB7 plus the length 1007 // of the length of the string in binary form, followed by the length of 1008 // the string, followed by the string. For example, a length-1024 string 1009 // would be encoded as 0xB90400 followed by the string. The range of 1010 // the first byte is thus [0xB8, 0xBF]. 1011 size, err = s.readUint(b - 0xB7) 1012 if err == nil && size < 56 { 1013 err = ErrCanonSize 1014 } 1015 return String, size, err 1016 case b < 0xF8: 1017 // If the total payload of a list 1018 // (i.e. the combined length of all its items) is 0-55 bytes long, the 1019 // RLP encoding consists of a single byte with value 0xC0 plus the length 1020 // of the list followed by the concatenation of the RLP encodings of the 1021 // items. The range of the first byte is thus [0xC0, 0xF7]. 1022 return List, uint64(b - 0xC0), nil 1023 default: 1024 // If the total payload of a list is more than 55 bytes long, 1025 // the RLP encoding consists of a single byte with value 0xF7 1026 // plus the length of the length of the payload in binary 1027 // form, followed by the length of the payload, followed by 1028 // the concatenation of the RLP encodings of the items. The 1029 // range of the first byte is thus [0xF8, 0xFF]. 1030 size, err = s.readUint(b - 0xF7) 1031 if err == nil && size < 56 { 1032 err = ErrCanonSize 1033 } 1034 return List, size, err 1035 } 1036 } 1037 1038 func (s *Stream) readUint(size byte) (uint64, error) { 1039 switch size { 1040 case 0: 1041 s.kind = -1 // rearm Kind 1042 return 0, nil 1043 case 1: 1044 b, err := s.readByte() 1045 return uint64(b), err 1046 default: 1047 start := int(8 - size) 1048 for i := 0; i < start; i++ { 1049 s.uintbuf[i] = 0 1050 } 1051 if err := s.readFull(s.uintbuf[start:]); err != nil { 1052 return 0, err 1053 } 1054 if s.uintbuf[start] == 0 { 1055 // Note: readUint is also used to decode integer 1056 // values. The error needs to be adjusted to become 1057 // ErrCanonInt in this case. 1058 return 0, ErrCanonSize 1059 } 1060 return binary.BigEndian.Uint64(s.uintbuf), nil 1061 } 1062 } 1063 1064 func (s *Stream) readFull(buf []byte) (err error) { 1065 if err := s.willRead(uint64(len(buf))); err != nil { 1066 return err 1067 } 1068 var nn, n int 1069 for n < len(buf) && err == nil { 1070 nn, err = s.r.Read(buf[n:]) 1071 n += nn 1072 } 1073 if err == io.EOF { 1074 err = io.ErrUnexpectedEOF 1075 } 1076 return err 1077 } 1078 1079 func (s *Stream) readByte() (byte, error) { 1080 if err := s.willRead(1); err != nil { 1081 return 0, err 1082 } 1083 b, err := s.r.ReadByte() 1084 if err == io.EOF { 1085 err = io.ErrUnexpectedEOF 1086 } 1087 return b, err 1088 } 1089 1090 func (s *Stream) willRead(n uint64) error { 1091 s.kind = -1 // rearm Kind 1092 1093 if len(s.stack) > 0 { 1094 // check list overflow 1095 tos := s.stack[len(s.stack)-1] 1096 if n > tos.size-tos.pos { 1097 return ErrElemTooLarge 1098 } 1099 s.stack[len(s.stack)-1].pos += n 1100 } 1101 if s.limited { 1102 if n > s.remaining { 1103 return ErrValueTooLarge 1104 } 1105 s.remaining -= n 1106 } 1107 return nil 1108 }