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