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