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