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