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