gitee.com/liu-zhao234568/cntest@v1.0.0/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 i, f := range fields { 427 err := f.info.decoder(s, val.Field(f.index)) 428 if err == EOL { 429 if f.optional { 430 // The field is optional, so reaching the end of the list before 431 // reaching the last field is acceptable. All remaining undecoded 432 // fields are zeroed. 433 zeroFields(val, fields[i:]) 434 break 435 } 436 return &decodeError{msg: "too few elements", typ: typ} 437 } else if err != nil { 438 return addErrorContext(err, "."+typ.Field(f.index).Name) 439 } 440 } 441 return wrapStreamError(s.ListEnd(), typ) 442 } 443 return dec, nil 444 } 445 446 func zeroFields(structval reflect.Value, fields []field) { 447 for _, f := range fields { 448 fv := structval.Field(f.index) 449 fv.Set(reflect.Zero(fv.Type())) 450 } 451 } 452 453 // makePtrDecoder creates a decoder that decodes into the pointer's element type. 454 func makePtrDecoder(typ reflect.Type, tag tags) (decoder, error) { 455 etype := typ.Elem() 456 etypeinfo := theTC.infoWhileGenerating(etype, tags{}) 457 switch { 458 case etypeinfo.decoderErr != nil: 459 return nil, etypeinfo.decoderErr 460 case !tag.nilOK: 461 return makeSimplePtrDecoder(etype, etypeinfo), nil 462 default: 463 return makeNilPtrDecoder(etype, etypeinfo, tag.nilKind), nil 464 } 465 } 466 467 func makeSimplePtrDecoder(etype reflect.Type, etypeinfo *typeinfo) decoder { 468 return func(s *Stream, val reflect.Value) (err error) { 469 newval := val 470 if val.IsNil() { 471 newval = reflect.New(etype) 472 } 473 if err = etypeinfo.decoder(s, newval.Elem()); err == nil { 474 val.Set(newval) 475 } 476 return err 477 } 478 } 479 480 // makeNilPtrDecoder creates a decoder that decodes empty values as nil. Non-empty 481 // values are decoded into a value of the element type, just like makePtrDecoder does. 482 // 483 // This decoder is used for pointer-typed struct fields with struct tag "nil". 484 func makeNilPtrDecoder(etype reflect.Type, etypeinfo *typeinfo, nilKind Kind) decoder { 485 typ := reflect.PtrTo(etype) 486 nilPtr := reflect.Zero(typ) 487 return func(s *Stream, val reflect.Value) (err error) { 488 kind, size, err := s.Kind() 489 if err != nil { 490 val.Set(nilPtr) 491 return wrapStreamError(err, typ) 492 } 493 // Handle empty values as a nil pointer. 494 if kind != Byte && size == 0 { 495 if kind != nilKind { 496 return &decodeError{ 497 msg: fmt.Sprintf("wrong kind of empty value (got %v, want %v)", kind, nilKind), 498 typ: typ, 499 } 500 } 501 // rearm s.Kind. This is important because the input 502 // position must advance to the next value even though 503 // we don't read anything. 504 s.kind = -1 505 val.Set(nilPtr) 506 return nil 507 } 508 newval := val 509 if val.IsNil() { 510 newval = reflect.New(etype) 511 } 512 if err = etypeinfo.decoder(s, newval.Elem()); err == nil { 513 val.Set(newval) 514 } 515 return err 516 } 517 } 518 519 var ifsliceType = reflect.TypeOf([]interface{}{}) 520 521 func decodeInterface(s *Stream, val reflect.Value) error { 522 if val.Type().NumMethod() != 0 { 523 return fmt.Errorf("rlp: type %v is not RLP-serializable", val.Type()) 524 } 525 kind, _, err := s.Kind() 526 if err != nil { 527 return err 528 } 529 if kind == List { 530 slice := reflect.New(ifsliceType).Elem() 531 if err := decodeListSlice(s, slice, decodeInterface); err != nil { 532 return err 533 } 534 val.Set(slice) 535 } else { 536 b, err := s.Bytes() 537 if err != nil { 538 return err 539 } 540 val.Set(reflect.ValueOf(b)) 541 } 542 return nil 543 } 544 545 func decodeDecoder(s *Stream, val reflect.Value) error { 546 return val.Addr().Interface().(Decoder).DecodeRLP(s) 547 } 548 549 // Kind represents the kind of value contained in an RLP stream. 550 type Kind int8 551 552 const ( 553 Byte Kind = iota 554 String 555 List 556 ) 557 558 func (k Kind) String() string { 559 switch k { 560 case Byte: 561 return "Byte" 562 case String: 563 return "String" 564 case List: 565 return "List" 566 default: 567 return fmt.Sprintf("Unknown(%d)", k) 568 } 569 } 570 571 // ByteReader must be implemented by any input reader for a Stream. It 572 // is implemented by e.g. bufio.Reader and bytes.Reader. 573 type ByteReader interface { 574 io.Reader 575 io.ByteReader 576 } 577 578 // Stream can be used for piecemeal decoding of an input stream. This 579 // is useful if the input is very large or if the decoding rules for a 580 // type depend on the input structure. Stream does not keep an 581 // internal buffer. After decoding a value, the input reader will be 582 // positioned just before the type information for the next value. 583 // 584 // When decoding a list and the input position reaches the declared 585 // length of the list, all operations will return error EOL. 586 // The end of the list must be acknowledged using ListEnd to continue 587 // reading the enclosing list. 588 // 589 // Stream is not safe for concurrent use. 590 type Stream struct { 591 r ByteReader 592 593 remaining uint64 // number of bytes remaining to be read from r 594 size uint64 // size of value ahead 595 kinderr error // error from last readKind 596 stack []uint64 // list sizes 597 uintbuf [32]byte // auxiliary buffer for integer decoding 598 kind Kind // kind of value ahead 599 byteval byte // value of single byte in type tag 600 limited bool // true if input limit is in effect 601 } 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 757 // Remove size of inner list from outer list before pushing the new size 758 // onto the stack. This ensures that the remaining outer list size will 759 // be correct after the matching call to ListEnd. 760 if inList, limit := s.listLimit(); inList { 761 s.stack[len(s.stack)-1] = limit - size 762 } 763 s.stack = append(s.stack, size) 764 s.kind = -1 765 s.size = 0 766 return size, nil 767 } 768 769 // ListEnd returns to the enclosing list. 770 // The input reader must be positioned at the end of a list. 771 func (s *Stream) ListEnd() error { 772 // Ensure that no more data is remaining in the current list. 773 if inList, listLimit := s.listLimit(); !inList { 774 return errNotInList 775 } else if listLimit > 0 { 776 return errNotAtEOL 777 } 778 s.stack = s.stack[:len(s.stack)-1] // pop 779 s.kind = -1 780 s.size = 0 781 return nil 782 } 783 784 // Decode decodes a value and stores the result in the value pointed 785 // to by val. Please see the documentation for the Decode function 786 // to learn about the decoding rules. 787 func (s *Stream) Decode(val interface{}) error { 788 if val == nil { 789 return errDecodeIntoNil 790 } 791 rval := reflect.ValueOf(val) 792 rtyp := rval.Type() 793 if rtyp.Kind() != reflect.Ptr { 794 return errNoPointer 795 } 796 if rval.IsNil() { 797 return errDecodeIntoNil 798 } 799 decoder, err := cachedDecoder(rtyp.Elem()) 800 if err != nil { 801 return err 802 } 803 804 err = decoder(s, rval.Elem()) 805 if decErr, ok := err.(*decodeError); ok && len(decErr.ctx) > 0 { 806 // Add decode target type to error so context has more meaning. 807 decErr.ctx = append(decErr.ctx, fmt.Sprint("(", rtyp.Elem(), ")")) 808 } 809 return err 810 } 811 812 // Reset discards any information about the current decoding context 813 // and starts reading from r. This method is meant to facilitate reuse 814 // of a preallocated Stream across many decoding operations. 815 // 816 // If r does not also implement ByteReader, Stream will do its own 817 // buffering. 818 func (s *Stream) Reset(r io.Reader, inputLimit uint64) { 819 if inputLimit > 0 { 820 s.remaining = inputLimit 821 s.limited = true 822 } else { 823 // Attempt to automatically discover 824 // the limit when reading from a byte slice. 825 switch br := r.(type) { 826 case *bytes.Reader: 827 s.remaining = uint64(br.Len()) 828 s.limited = true 829 case *bytes.Buffer: 830 s.remaining = uint64(br.Len()) 831 s.limited = true 832 case *strings.Reader: 833 s.remaining = uint64(br.Len()) 834 s.limited = true 835 default: 836 s.limited = false 837 } 838 } 839 // Wrap r with a buffer if it doesn't have one. 840 bufr, ok := r.(ByteReader) 841 if !ok { 842 bufr = bufio.NewReader(r) 843 } 844 s.r = bufr 845 // Reset the decoding context. 846 s.stack = s.stack[:0] 847 s.size = 0 848 s.kind = -1 849 s.kinderr = nil 850 s.byteval = 0 851 s.uintbuf = [32]byte{} 852 } 853 854 // Kind returns the kind and size of the next value in the 855 // input stream. 856 // 857 // The returned size is the number of bytes that make up the value. 858 // For kind == Byte, the size is zero because the value is 859 // contained in the type tag. 860 // 861 // The first call to Kind will read size information from the input 862 // reader and leave it positioned at the start of the actual bytes of 863 // the value. Subsequent calls to Kind (until the value is decoded) 864 // will not advance the input reader and return cached information. 865 func (s *Stream) Kind() (kind Kind, size uint64, err error) { 866 if s.kind >= 0 { 867 return s.kind, s.size, s.kinderr 868 } 869 870 // Check for end of list. This needs to be done here because readKind 871 // checks against the list size, and would return the wrong error. 872 inList, listLimit := s.listLimit() 873 if inList && listLimit == 0 { 874 return 0, 0, EOL 875 } 876 // Read the actual size tag. 877 s.kind, s.size, s.kinderr = s.readKind() 878 if s.kinderr == nil { 879 // Check the data size of the value ahead against input limits. This 880 // is done here because many decoders require allocating an input 881 // buffer matching the value size. Checking it here protects those 882 // decoders from inputs declaring very large value size. 883 if inList && s.size > listLimit { 884 s.kinderr = ErrElemTooLarge 885 } else if s.limited && s.size > s.remaining { 886 s.kinderr = ErrValueTooLarge 887 } 888 } 889 return s.kind, s.size, s.kinderr 890 } 891 892 func (s *Stream) readKind() (kind Kind, size uint64, err error) { 893 b, err := s.readByte() 894 if err != nil { 895 if len(s.stack) == 0 { 896 // At toplevel, Adjust the error to actual EOF. io.EOF is 897 // used by callers to determine when to stop decoding. 898 switch err { 899 case io.ErrUnexpectedEOF: 900 err = io.EOF 901 case ErrValueTooLarge: 902 err = io.EOF 903 } 904 } 905 return 0, 0, err 906 } 907 s.byteval = 0 908 switch { 909 case b < 0x80: 910 // For a single byte whose value is in the [0x00, 0x7F] range, that byte 911 // is its own RLP encoding. 912 s.byteval = b 913 return Byte, 0, nil 914 case b < 0xB8: 915 // Otherwise, if a string is 0-55 bytes long, the RLP encoding consists 916 // of a single byte with value 0x80 plus the length of the string 917 // followed by the string. The range of the first byte is thus [0x80, 0xB7]. 918 return String, uint64(b - 0x80), nil 919 case b < 0xC0: 920 // If a string is more than 55 bytes long, the RLP encoding consists of a 921 // single byte with value 0xB7 plus the length of the length of the 922 // string in binary form, followed by the length of the string, followed 923 // by the string. For example, a length-1024 string would be encoded as 924 // 0xB90400 followed by the string. The range of the first byte is thus 925 // [0xB8, 0xBF]. 926 size, err = s.readUint(b - 0xB7) 927 if err == nil && size < 56 { 928 err = ErrCanonSize 929 } 930 return String, size, err 931 case b < 0xF8: 932 // If the total payload of a list (i.e. the combined length of all its 933 // items) is 0-55 bytes long, the RLP encoding consists of a single byte 934 // with value 0xC0 plus the length of the list followed by the 935 // concatenation of the RLP encodings of the items. The range of the 936 // first byte is thus [0xC0, 0xF7]. 937 return List, uint64(b - 0xC0), nil 938 default: 939 // If the total payload of a list is more than 55 bytes long, the RLP 940 // encoding consists of a single byte with value 0xF7 plus the length of 941 // the length of the payload in binary form, followed by the length of 942 // the payload, followed by the concatenation of the RLP encodings of 943 // the items. The range of the first byte is thus [0xF8, 0xFF]. 944 size, err = s.readUint(b - 0xF7) 945 if err == nil && size < 56 { 946 err = ErrCanonSize 947 } 948 return List, size, err 949 } 950 } 951 952 func (s *Stream) readUint(size byte) (uint64, error) { 953 switch size { 954 case 0: 955 s.kind = -1 // rearm Kind 956 return 0, nil 957 case 1: 958 b, err := s.readByte() 959 return uint64(b), err 960 default: 961 buffer := s.uintbuf[:8] 962 for i := range buffer { 963 buffer[i] = 0 964 } 965 start := int(8 - size) 966 if err := s.readFull(buffer[start:]); err != nil { 967 return 0, err 968 } 969 if buffer[start] == 0 { 970 // Note: readUint is also used to decode integer values. 971 // The error needs to be adjusted to become ErrCanonInt in this case. 972 return 0, ErrCanonSize 973 } 974 return binary.BigEndian.Uint64(buffer[:]), nil 975 } 976 } 977 978 // readFull reads into buf from the underlying stream. 979 func (s *Stream) readFull(buf []byte) (err error) { 980 if err := s.willRead(uint64(len(buf))); err != nil { 981 return err 982 } 983 var nn, n int 984 for n < len(buf) && err == nil { 985 nn, err = s.r.Read(buf[n:]) 986 n += nn 987 } 988 if err == io.EOF { 989 if n < len(buf) { 990 err = io.ErrUnexpectedEOF 991 } else { 992 // Readers are allowed to give EOF even though the read succeeded. 993 // In such cases, we discard the EOF, like io.ReadFull() does. 994 err = nil 995 } 996 } 997 return err 998 } 999 1000 // readByte reads a single byte from the underlying stream. 1001 func (s *Stream) readByte() (byte, error) { 1002 if err := s.willRead(1); err != nil { 1003 return 0, err 1004 } 1005 b, err := s.r.ReadByte() 1006 if err == io.EOF { 1007 err = io.ErrUnexpectedEOF 1008 } 1009 return b, err 1010 } 1011 1012 // willRead is called before any read from the underlying stream. It checks 1013 // n against size limits, and updates the limits if n doesn't overflow them. 1014 func (s *Stream) willRead(n uint64) error { 1015 s.kind = -1 // rearm Kind 1016 1017 if inList, limit := s.listLimit(); inList { 1018 if n > limit { 1019 return ErrElemTooLarge 1020 } 1021 s.stack[len(s.stack)-1] = limit - n 1022 } 1023 if s.limited { 1024 if n > s.remaining { 1025 return ErrValueTooLarge 1026 } 1027 s.remaining -= n 1028 } 1029 return nil 1030 } 1031 1032 // listLimit returns the amount of data remaining in the innermost list. 1033 func (s *Stream) listLimit() (inList bool, limit uint64) { 1034 if len(s.stack) == 0 { 1035 return false, 0 1036 } 1037 return true, s.stack[len(s.stack)-1] 1038 }