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