github.com/geraldss/go/src@v0.0.0-20210511222824-ac7d0ebfc235/encoding/gob/decode.go (about) 1 // Copyright 2009 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 //go:generate go run decgen.go -output dec_helpers.go 6 7 package gob 8 9 import ( 10 "errors" 11 "io" 12 "math" 13 "math/bits" 14 "reflect" 15 16 "github.com/geraldss/go/src/encoding" 17 ) 18 19 var ( 20 errBadUint = errors.New("gob: encoded unsigned integer out of range") 21 errBadType = errors.New("gob: unknown type id or corrupted data") 22 errRange = errors.New("gob: bad data: field numbers out of bounds") 23 ) 24 25 type decHelper func(state *decoderState, v reflect.Value, length int, ovfl error) bool 26 27 // decoderState is the execution state of an instance of the decoder. A new state 28 // is created for nested objects. 29 type decoderState struct { 30 dec *Decoder 31 // The buffer is stored with an extra indirection because it may be replaced 32 // if we load a type during decode (when reading an interface value). 33 b *decBuffer 34 fieldnum int // the last field number read. 35 next *decoderState // for free list 36 } 37 38 // decBuffer is an extremely simple, fast implementation of a read-only byte buffer. 39 // It is initialized by calling Size and then copying the data into the slice returned by Bytes(). 40 type decBuffer struct { 41 data []byte 42 offset int // Read offset. 43 } 44 45 func (d *decBuffer) Read(p []byte) (int, error) { 46 n := copy(p, d.data[d.offset:]) 47 if n == 0 && len(p) != 0 { 48 return 0, io.EOF 49 } 50 d.offset += n 51 return n, nil 52 } 53 54 func (d *decBuffer) Drop(n int) { 55 if n > d.Len() { 56 panic("drop") 57 } 58 d.offset += n 59 } 60 61 // Size grows the buffer to exactly n bytes, so d.Bytes() will 62 // return a slice of length n. Existing data is first discarded. 63 func (d *decBuffer) Size(n int) { 64 d.Reset() 65 if cap(d.data) < n { 66 d.data = make([]byte, n) 67 } else { 68 d.data = d.data[0:n] 69 } 70 } 71 72 func (d *decBuffer) ReadByte() (byte, error) { 73 if d.offset >= len(d.data) { 74 return 0, io.EOF 75 } 76 c := d.data[d.offset] 77 d.offset++ 78 return c, nil 79 } 80 81 func (d *decBuffer) Len() int { 82 return len(d.data) - d.offset 83 } 84 85 func (d *decBuffer) Bytes() []byte { 86 return d.data[d.offset:] 87 } 88 89 func (d *decBuffer) Reset() { 90 d.data = d.data[0:0] 91 d.offset = 0 92 } 93 94 // We pass the bytes.Buffer separately for easier testing of the infrastructure 95 // without requiring a full Decoder. 96 func (dec *Decoder) newDecoderState(buf *decBuffer) *decoderState { 97 d := dec.freeList 98 if d == nil { 99 d = new(decoderState) 100 d.dec = dec 101 } else { 102 dec.freeList = d.next 103 } 104 d.b = buf 105 return d 106 } 107 108 func (dec *Decoder) freeDecoderState(d *decoderState) { 109 d.next = dec.freeList 110 dec.freeList = d 111 } 112 113 func overflow(name string) error { 114 return errors.New(`value for "` + name + `" out of range`) 115 } 116 117 // decodeUintReader reads an encoded unsigned integer from an io.Reader. 118 // Used only by the Decoder to read the message length. 119 func decodeUintReader(r io.Reader, buf []byte) (x uint64, width int, err error) { 120 width = 1 121 n, err := io.ReadFull(r, buf[0:width]) 122 if n == 0 { 123 return 124 } 125 b := buf[0] 126 if b <= 0x7f { 127 return uint64(b), width, nil 128 } 129 n = -int(int8(b)) 130 if n > uint64Size { 131 err = errBadUint 132 return 133 } 134 width, err = io.ReadFull(r, buf[0:n]) 135 if err != nil { 136 if err == io.EOF { 137 err = io.ErrUnexpectedEOF 138 } 139 return 140 } 141 // Could check that the high byte is zero but it's not worth it. 142 for _, b := range buf[0:width] { 143 x = x<<8 | uint64(b) 144 } 145 width++ // +1 for length byte 146 return 147 } 148 149 // decodeUint reads an encoded unsigned integer from state.r. 150 // Does not check for overflow. 151 func (state *decoderState) decodeUint() (x uint64) { 152 b, err := state.b.ReadByte() 153 if err != nil { 154 error_(err) 155 } 156 if b <= 0x7f { 157 return uint64(b) 158 } 159 n := -int(int8(b)) 160 if n > uint64Size { 161 error_(errBadUint) 162 } 163 buf := state.b.Bytes() 164 if len(buf) < n { 165 errorf("invalid uint data length %d: exceeds input size %d", n, len(buf)) 166 } 167 // Don't need to check error; it's safe to loop regardless. 168 // Could check that the high byte is zero but it's not worth it. 169 for _, b := range buf[0:n] { 170 x = x<<8 | uint64(b) 171 } 172 state.b.Drop(n) 173 return x 174 } 175 176 // decodeInt reads an encoded signed integer from state.r. 177 // Does not check for overflow. 178 func (state *decoderState) decodeInt() int64 { 179 x := state.decodeUint() 180 if x&1 != 0 { 181 return ^int64(x >> 1) 182 } 183 return int64(x >> 1) 184 } 185 186 // getLength decodes the next uint and makes sure it is a possible 187 // size for a data item that follows, which means it must fit in a 188 // non-negative int and fit in the buffer. 189 func (state *decoderState) getLength() (int, bool) { 190 n := int(state.decodeUint()) 191 if n < 0 || state.b.Len() < n || tooBig <= n { 192 return 0, false 193 } 194 return n, true 195 } 196 197 // decOp is the signature of a decoding operator for a given type. 198 type decOp func(i *decInstr, state *decoderState, v reflect.Value) 199 200 // The 'instructions' of the decoding machine 201 type decInstr struct { 202 op decOp 203 field int // field number of the wire type 204 index []int // field access indices for destination type 205 ovfl error // error message for overflow/underflow (for arrays, of the elements) 206 } 207 208 // ignoreUint discards a uint value with no destination. 209 func ignoreUint(i *decInstr, state *decoderState, v reflect.Value) { 210 state.decodeUint() 211 } 212 213 // ignoreTwoUints discards a uint value with no destination. It's used to skip 214 // complex values. 215 func ignoreTwoUints(i *decInstr, state *decoderState, v reflect.Value) { 216 state.decodeUint() 217 state.decodeUint() 218 } 219 220 // Since the encoder writes no zeros, if we arrive at a decoder we have 221 // a value to extract and store. The field number has already been read 222 // (it's how we knew to call this decoder). 223 // Each decoder is responsible for handling any indirections associated 224 // with the data structure. If any pointer so reached is nil, allocation must 225 // be done. 226 227 // decAlloc takes a value and returns a settable value that can 228 // be assigned to. If the value is a pointer, decAlloc guarantees it points to storage. 229 // The callers to the individual decoders are expected to have used decAlloc. 230 // The individual decoders don't need to it. 231 func decAlloc(v reflect.Value) reflect.Value { 232 for v.Kind() == reflect.Ptr { 233 if v.IsNil() { 234 v.Set(reflect.New(v.Type().Elem())) 235 } 236 v = v.Elem() 237 } 238 return v 239 } 240 241 // decBool decodes a uint and stores it as a boolean in value. 242 func decBool(i *decInstr, state *decoderState, value reflect.Value) { 243 value.SetBool(state.decodeUint() != 0) 244 } 245 246 // decInt8 decodes an integer and stores it as an int8 in value. 247 func decInt8(i *decInstr, state *decoderState, value reflect.Value) { 248 v := state.decodeInt() 249 if v < math.MinInt8 || math.MaxInt8 < v { 250 error_(i.ovfl) 251 } 252 value.SetInt(v) 253 } 254 255 // decUint8 decodes an unsigned integer and stores it as a uint8 in value. 256 func decUint8(i *decInstr, state *decoderState, value reflect.Value) { 257 v := state.decodeUint() 258 if math.MaxUint8 < v { 259 error_(i.ovfl) 260 } 261 value.SetUint(v) 262 } 263 264 // decInt16 decodes an integer and stores it as an int16 in value. 265 func decInt16(i *decInstr, state *decoderState, value reflect.Value) { 266 v := state.decodeInt() 267 if v < math.MinInt16 || math.MaxInt16 < v { 268 error_(i.ovfl) 269 } 270 value.SetInt(v) 271 } 272 273 // decUint16 decodes an unsigned integer and stores it as a uint16 in value. 274 func decUint16(i *decInstr, state *decoderState, value reflect.Value) { 275 v := state.decodeUint() 276 if math.MaxUint16 < v { 277 error_(i.ovfl) 278 } 279 value.SetUint(v) 280 } 281 282 // decInt32 decodes an integer and stores it as an int32 in value. 283 func decInt32(i *decInstr, state *decoderState, value reflect.Value) { 284 v := state.decodeInt() 285 if v < math.MinInt32 || math.MaxInt32 < v { 286 error_(i.ovfl) 287 } 288 value.SetInt(v) 289 } 290 291 // decUint32 decodes an unsigned integer and stores it as a uint32 in value. 292 func decUint32(i *decInstr, state *decoderState, value reflect.Value) { 293 v := state.decodeUint() 294 if math.MaxUint32 < v { 295 error_(i.ovfl) 296 } 297 value.SetUint(v) 298 } 299 300 // decInt64 decodes an integer and stores it as an int64 in value. 301 func decInt64(i *decInstr, state *decoderState, value reflect.Value) { 302 v := state.decodeInt() 303 value.SetInt(v) 304 } 305 306 // decUint64 decodes an unsigned integer and stores it as a uint64 in value. 307 func decUint64(i *decInstr, state *decoderState, value reflect.Value) { 308 v := state.decodeUint() 309 value.SetUint(v) 310 } 311 312 // Floating-point numbers are transmitted as uint64s holding the bits 313 // of the underlying representation. They are sent byte-reversed, with 314 // the exponent end coming out first, so integer floating point numbers 315 // (for example) transmit more compactly. This routine does the 316 // unswizzling. 317 func float64FromBits(u uint64) float64 { 318 v := bits.ReverseBytes64(u) 319 return math.Float64frombits(v) 320 } 321 322 // float32FromBits decodes an unsigned integer, treats it as a 32-bit floating-point 323 // number, and returns it. It's a helper function for float32 and complex64. 324 // It returns a float64 because that's what reflection needs, but its return 325 // value is known to be accurately representable in a float32. 326 func float32FromBits(u uint64, ovfl error) float64 { 327 v := float64FromBits(u) 328 av := v 329 if av < 0 { 330 av = -av 331 } 332 // +Inf is OK in both 32- and 64-bit floats. Underflow is always OK. 333 if math.MaxFloat32 < av && av <= math.MaxFloat64 { 334 error_(ovfl) 335 } 336 return v 337 } 338 339 // decFloat32 decodes an unsigned integer, treats it as a 32-bit floating-point 340 // number, and stores it in value. 341 func decFloat32(i *decInstr, state *decoderState, value reflect.Value) { 342 value.SetFloat(float32FromBits(state.decodeUint(), i.ovfl)) 343 } 344 345 // decFloat64 decodes an unsigned integer, treats it as a 64-bit floating-point 346 // number, and stores it in value. 347 func decFloat64(i *decInstr, state *decoderState, value reflect.Value) { 348 value.SetFloat(float64FromBits(state.decodeUint())) 349 } 350 351 // decComplex64 decodes a pair of unsigned integers, treats them as a 352 // pair of floating point numbers, and stores them as a complex64 in value. 353 // The real part comes first. 354 func decComplex64(i *decInstr, state *decoderState, value reflect.Value) { 355 real := float32FromBits(state.decodeUint(), i.ovfl) 356 imag := float32FromBits(state.decodeUint(), i.ovfl) 357 value.SetComplex(complex(real, imag)) 358 } 359 360 // decComplex128 decodes a pair of unsigned integers, treats them as a 361 // pair of floating point numbers, and stores them as a complex128 in value. 362 // The real part comes first. 363 func decComplex128(i *decInstr, state *decoderState, value reflect.Value) { 364 real := float64FromBits(state.decodeUint()) 365 imag := float64FromBits(state.decodeUint()) 366 value.SetComplex(complex(real, imag)) 367 } 368 369 // decUint8Slice decodes a byte slice and stores in value a slice header 370 // describing the data. 371 // uint8 slices are encoded as an unsigned count followed by the raw bytes. 372 func decUint8Slice(i *decInstr, state *decoderState, value reflect.Value) { 373 n, ok := state.getLength() 374 if !ok { 375 errorf("bad %s slice length: %d", value.Type(), n) 376 } 377 if value.Cap() < n { 378 value.Set(reflect.MakeSlice(value.Type(), n, n)) 379 } else { 380 value.Set(value.Slice(0, n)) 381 } 382 if _, err := state.b.Read(value.Bytes()); err != nil { 383 errorf("error decoding []byte: %s", err) 384 } 385 } 386 387 // decString decodes byte array and stores in value a string header 388 // describing the data. 389 // Strings are encoded as an unsigned count followed by the raw bytes. 390 func decString(i *decInstr, state *decoderState, value reflect.Value) { 391 n, ok := state.getLength() 392 if !ok { 393 errorf("bad %s slice length: %d", value.Type(), n) 394 } 395 // Read the data. 396 data := state.b.Bytes() 397 if len(data) < n { 398 errorf("invalid string length %d: exceeds input size %d", n, len(data)) 399 } 400 s := string(data[:n]) 401 state.b.Drop(n) 402 value.SetString(s) 403 } 404 405 // ignoreUint8Array skips over the data for a byte slice value with no destination. 406 func ignoreUint8Array(i *decInstr, state *decoderState, value reflect.Value) { 407 n, ok := state.getLength() 408 if !ok { 409 errorf("slice length too large") 410 } 411 bn := state.b.Len() 412 if bn < n { 413 errorf("invalid slice length %d: exceeds input size %d", n, bn) 414 } 415 state.b.Drop(n) 416 } 417 418 // Execution engine 419 420 // The encoder engine is an array of instructions indexed by field number of the incoming 421 // decoder. It is executed with random access according to field number. 422 type decEngine struct { 423 instr []decInstr 424 numInstr int // the number of active instructions 425 } 426 427 // decodeSingle decodes a top-level value that is not a struct and stores it in value. 428 // Such values are preceded by a zero, making them have the memory layout of a 429 // struct field (although with an illegal field number). 430 func (dec *Decoder) decodeSingle(engine *decEngine, value reflect.Value) { 431 state := dec.newDecoderState(&dec.buf) 432 defer dec.freeDecoderState(state) 433 state.fieldnum = singletonField 434 if state.decodeUint() != 0 { 435 errorf("decode: corrupted data: non-zero delta for singleton") 436 } 437 instr := &engine.instr[singletonField] 438 instr.op(instr, state, value) 439 } 440 441 // decodeStruct decodes a top-level struct and stores it in value. 442 // Indir is for the value, not the type. At the time of the call it may 443 // differ from ut.indir, which was computed when the engine was built. 444 // This state cannot arise for decodeSingle, which is called directly 445 // from the user's value, not from the innards of an engine. 446 func (dec *Decoder) decodeStruct(engine *decEngine, value reflect.Value) { 447 state := dec.newDecoderState(&dec.buf) 448 defer dec.freeDecoderState(state) 449 state.fieldnum = -1 450 for state.b.Len() > 0 { 451 delta := int(state.decodeUint()) 452 if delta < 0 { 453 errorf("decode: corrupted data: negative delta") 454 } 455 if delta == 0 { // struct terminator is zero delta fieldnum 456 break 457 } 458 fieldnum := state.fieldnum + delta 459 if fieldnum >= len(engine.instr) { 460 error_(errRange) 461 break 462 } 463 instr := &engine.instr[fieldnum] 464 var field reflect.Value 465 if instr.index != nil { 466 // Otherwise the field is unknown to us and instr.op is an ignore op. 467 field = value.FieldByIndex(instr.index) 468 if field.Kind() == reflect.Ptr { 469 field = decAlloc(field) 470 } 471 } 472 instr.op(instr, state, field) 473 state.fieldnum = fieldnum 474 } 475 } 476 477 var noValue reflect.Value 478 479 // ignoreStruct discards the data for a struct with no destination. 480 func (dec *Decoder) ignoreStruct(engine *decEngine) { 481 state := dec.newDecoderState(&dec.buf) 482 defer dec.freeDecoderState(state) 483 state.fieldnum = -1 484 for state.b.Len() > 0 { 485 delta := int(state.decodeUint()) 486 if delta < 0 { 487 errorf("ignore decode: corrupted data: negative delta") 488 } 489 if delta == 0 { // struct terminator is zero delta fieldnum 490 break 491 } 492 fieldnum := state.fieldnum + delta 493 if fieldnum >= len(engine.instr) { 494 error_(errRange) 495 } 496 instr := &engine.instr[fieldnum] 497 instr.op(instr, state, noValue) 498 state.fieldnum = fieldnum 499 } 500 } 501 502 // ignoreSingle discards the data for a top-level non-struct value with no 503 // destination. It's used when calling Decode with a nil value. 504 func (dec *Decoder) ignoreSingle(engine *decEngine) { 505 state := dec.newDecoderState(&dec.buf) 506 defer dec.freeDecoderState(state) 507 state.fieldnum = singletonField 508 delta := int(state.decodeUint()) 509 if delta != 0 { 510 errorf("decode: corrupted data: non-zero delta for singleton") 511 } 512 instr := &engine.instr[singletonField] 513 instr.op(instr, state, noValue) 514 } 515 516 // decodeArrayHelper does the work for decoding arrays and slices. 517 func (dec *Decoder) decodeArrayHelper(state *decoderState, value reflect.Value, elemOp decOp, length int, ovfl error, helper decHelper) { 518 if helper != nil && helper(state, value, length, ovfl) { 519 return 520 } 521 instr := &decInstr{elemOp, 0, nil, ovfl} 522 isPtr := value.Type().Elem().Kind() == reflect.Ptr 523 for i := 0; i < length; i++ { 524 if state.b.Len() == 0 { 525 errorf("decoding array or slice: length exceeds input size (%d elements)", length) 526 } 527 v := value.Index(i) 528 if isPtr { 529 v = decAlloc(v) 530 } 531 elemOp(instr, state, v) 532 } 533 } 534 535 // decodeArray decodes an array and stores it in value. 536 // The length is an unsigned integer preceding the elements. Even though the length is redundant 537 // (it's part of the type), it's a useful check and is included in the encoding. 538 func (dec *Decoder) decodeArray(state *decoderState, value reflect.Value, elemOp decOp, length int, ovfl error, helper decHelper) { 539 if n := state.decodeUint(); n != uint64(length) { 540 errorf("length mismatch in decodeArray") 541 } 542 dec.decodeArrayHelper(state, value, elemOp, length, ovfl, helper) 543 } 544 545 // decodeIntoValue is a helper for map decoding. 546 func decodeIntoValue(state *decoderState, op decOp, isPtr bool, value reflect.Value, instr *decInstr) reflect.Value { 547 v := value 548 if isPtr { 549 v = decAlloc(value) 550 } 551 552 op(instr, state, v) 553 return value 554 } 555 556 // decodeMap decodes a map and stores it in value. 557 // Maps are encoded as a length followed by key:value pairs. 558 // Because the internals of maps are not visible to us, we must 559 // use reflection rather than pointer magic. 560 func (dec *Decoder) decodeMap(mtyp reflect.Type, state *decoderState, value reflect.Value, keyOp, elemOp decOp, ovfl error) { 561 n := int(state.decodeUint()) 562 if value.IsNil() { 563 value.Set(reflect.MakeMapWithSize(mtyp, n)) 564 } 565 keyIsPtr := mtyp.Key().Kind() == reflect.Ptr 566 elemIsPtr := mtyp.Elem().Kind() == reflect.Ptr 567 keyInstr := &decInstr{keyOp, 0, nil, ovfl} 568 elemInstr := &decInstr{elemOp, 0, nil, ovfl} 569 keyP := reflect.New(mtyp.Key()) 570 keyZ := reflect.Zero(mtyp.Key()) 571 elemP := reflect.New(mtyp.Elem()) 572 elemZ := reflect.Zero(mtyp.Elem()) 573 574 // geraldss/go: Hash object names to conserve memory 575 hash := mtyp.Key().Kind() == reflect.String 576 577 for i := 0; i < n; i++ { 578 key := decodeIntoValue(state, keyOp, keyIsPtr, keyP.Elem(), keyInstr) 579 elem := decodeIntoValue(state, elemOp, elemIsPtr, elemP.Elem(), elemInstr) 580 581 // geraldss/go: Hash object names to conserve memory 582 if hash { 583 if str, ok := key.Interface().(string); ok { 584 str = encoding.NAME_HASH.Hash(str) 585 key.SetString(str) 586 } 587 } 588 589 value.SetMapIndex(key, elem) 590 keyP.Elem().Set(keyZ) 591 elemP.Elem().Set(elemZ) 592 } 593 } 594 595 // ignoreArrayHelper does the work for discarding arrays and slices. 596 func (dec *Decoder) ignoreArrayHelper(state *decoderState, elemOp decOp, length int) { 597 instr := &decInstr{elemOp, 0, nil, errors.New("no error")} 598 for i := 0; i < length; i++ { 599 if state.b.Len() == 0 { 600 errorf("decoding array or slice: length exceeds input size (%d elements)", length) 601 } 602 elemOp(instr, state, noValue) 603 } 604 } 605 606 // ignoreArray discards the data for an array value with no destination. 607 func (dec *Decoder) ignoreArray(state *decoderState, elemOp decOp, length int) { 608 if n := state.decodeUint(); n != uint64(length) { 609 errorf("length mismatch in ignoreArray") 610 } 611 dec.ignoreArrayHelper(state, elemOp, length) 612 } 613 614 // ignoreMap discards the data for a map value with no destination. 615 func (dec *Decoder) ignoreMap(state *decoderState, keyOp, elemOp decOp) { 616 n := int(state.decodeUint()) 617 keyInstr := &decInstr{keyOp, 0, nil, errors.New("no error")} 618 elemInstr := &decInstr{elemOp, 0, nil, errors.New("no error")} 619 for i := 0; i < n; i++ { 620 keyOp(keyInstr, state, noValue) 621 elemOp(elemInstr, state, noValue) 622 } 623 } 624 625 // decodeSlice decodes a slice and stores it in value. 626 // Slices are encoded as an unsigned length followed by the elements. 627 func (dec *Decoder) decodeSlice(state *decoderState, value reflect.Value, elemOp decOp, ovfl error, helper decHelper) { 628 u := state.decodeUint() 629 typ := value.Type() 630 size := uint64(typ.Elem().Size()) 631 nBytes := u * size 632 n := int(u) 633 // Take care with overflow in this calculation. 634 if n < 0 || uint64(n) != u || nBytes > tooBig || (size > 0 && nBytes/size != u) { 635 // We don't check n against buffer length here because if it's a slice 636 // of interfaces, there will be buffer reloads. 637 errorf("%s slice too big: %d elements of %d bytes", typ.Elem(), u, size) 638 } 639 if value.Cap() < n { 640 value.Set(reflect.MakeSlice(typ, n, n)) 641 } else { 642 value.Set(value.Slice(0, n)) 643 } 644 dec.decodeArrayHelper(state, value, elemOp, n, ovfl, helper) 645 } 646 647 // ignoreSlice skips over the data for a slice value with no destination. 648 func (dec *Decoder) ignoreSlice(state *decoderState, elemOp decOp) { 649 dec.ignoreArrayHelper(state, elemOp, int(state.decodeUint())) 650 } 651 652 // decodeInterface decodes an interface value and stores it in value. 653 // Interfaces are encoded as the name of a concrete type followed by a value. 654 // If the name is empty, the value is nil and no value is sent. 655 func (dec *Decoder) decodeInterface(ityp reflect.Type, state *decoderState, value reflect.Value) { 656 // Read the name of the concrete type. 657 nr := state.decodeUint() 658 if nr > 1<<31 { // zero is permissible for anonymous types 659 errorf("invalid type name length %d", nr) 660 } 661 if nr > uint64(state.b.Len()) { 662 errorf("invalid type name length %d: exceeds input size", nr) 663 } 664 n := int(nr) 665 name := state.b.Bytes()[:n] 666 state.b.Drop(n) 667 // Allocate the destination interface value. 668 if len(name) == 0 { 669 // Copy the nil interface value to the target. 670 value.Set(reflect.Zero(value.Type())) 671 return 672 } 673 if len(name) > 1024 { 674 errorf("name too long (%d bytes): %.20q...", len(name), name) 675 } 676 // The concrete type must be registered. 677 typi, ok := nameToConcreteType.Load(string(name)) 678 if !ok { 679 errorf("name not registered for interface: %q", name) 680 } 681 typ := typi.(reflect.Type) 682 683 // Read the type id of the concrete value. 684 concreteId := dec.decodeTypeSequence(true) 685 if concreteId < 0 { 686 error_(dec.err) 687 } 688 // Byte count of value is next; we don't care what it is (it's there 689 // in case we want to ignore the value by skipping it completely). 690 state.decodeUint() 691 // Read the concrete value. 692 v := allocValue(typ) 693 dec.decodeValue(concreteId, v) 694 if dec.err != nil { 695 error_(dec.err) 696 } 697 // Assign the concrete value to the interface. 698 // Tread carefully; it might not satisfy the interface. 699 if !typ.AssignableTo(ityp) { 700 errorf("%s is not assignable to type %s", typ, ityp) 701 } 702 // Copy the interface value to the target. 703 value.Set(v) 704 } 705 706 // ignoreInterface discards the data for an interface value with no destination. 707 func (dec *Decoder) ignoreInterface(state *decoderState) { 708 // Read the name of the concrete type. 709 n, ok := state.getLength() 710 if !ok { 711 errorf("bad interface encoding: name too large for buffer") 712 } 713 bn := state.b.Len() 714 if bn < n { 715 errorf("invalid interface value length %d: exceeds input size %d", n, bn) 716 } 717 state.b.Drop(n) 718 id := dec.decodeTypeSequence(true) 719 if id < 0 { 720 error_(dec.err) 721 } 722 // At this point, the decoder buffer contains a delimited value. Just toss it. 723 n, ok = state.getLength() 724 if !ok { 725 errorf("bad interface encoding: data length too large for buffer") 726 } 727 state.b.Drop(n) 728 } 729 730 // decodeGobDecoder decodes something implementing the GobDecoder interface. 731 // The data is encoded as a byte slice. 732 func (dec *Decoder) decodeGobDecoder(ut *userTypeInfo, state *decoderState, value reflect.Value) { 733 // Read the bytes for the value. 734 n, ok := state.getLength() 735 if !ok { 736 errorf("GobDecoder: length too large for buffer") 737 } 738 b := state.b.Bytes() 739 if len(b) < n { 740 errorf("GobDecoder: invalid data length %d: exceeds input size %d", n, len(b)) 741 } 742 b = b[:n] 743 state.b.Drop(n) 744 var err error 745 // We know it's one of these. 746 switch ut.externalDec { 747 case xGob: 748 err = value.Interface().(GobDecoder).GobDecode(b) 749 case xBinary: 750 err = value.Interface().(encoding.BinaryUnmarshaler).UnmarshalBinary(b) 751 case xText: 752 err = value.Interface().(encoding.TextUnmarshaler).UnmarshalText(b) 753 } 754 if err != nil { 755 error_(err) 756 } 757 } 758 759 // ignoreGobDecoder discards the data for a GobDecoder value with no destination. 760 func (dec *Decoder) ignoreGobDecoder(state *decoderState) { 761 // Read the bytes for the value. 762 n, ok := state.getLength() 763 if !ok { 764 errorf("GobDecoder: length too large for buffer") 765 } 766 bn := state.b.Len() 767 if bn < n { 768 errorf("GobDecoder: invalid data length %d: exceeds input size %d", n, bn) 769 } 770 state.b.Drop(n) 771 } 772 773 // Index by Go types. 774 var decOpTable = [...]decOp{ 775 reflect.Bool: decBool, 776 reflect.Int8: decInt8, 777 reflect.Int16: decInt16, 778 reflect.Int32: decInt32, 779 reflect.Int64: decInt64, 780 reflect.Uint8: decUint8, 781 reflect.Uint16: decUint16, 782 reflect.Uint32: decUint32, 783 reflect.Uint64: decUint64, 784 reflect.Float32: decFloat32, 785 reflect.Float64: decFloat64, 786 reflect.Complex64: decComplex64, 787 reflect.Complex128: decComplex128, 788 reflect.String: decString, 789 } 790 791 // Indexed by gob types. tComplex will be added during type.init(). 792 var decIgnoreOpMap = map[typeId]decOp{ 793 tBool: ignoreUint, 794 tInt: ignoreUint, 795 tUint: ignoreUint, 796 tFloat: ignoreUint, 797 tBytes: ignoreUint8Array, 798 tString: ignoreUint8Array, 799 tComplex: ignoreTwoUints, 800 } 801 802 // decOpFor returns the decoding op for the base type under rt and 803 // the indirection count to reach it. 804 func (dec *Decoder) decOpFor(wireId typeId, rt reflect.Type, name string, inProgress map[reflect.Type]*decOp) *decOp { 805 ut := userType(rt) 806 // If the type implements GobEncoder, we handle it without further processing. 807 if ut.externalDec != 0 { 808 return dec.gobDecodeOpFor(ut) 809 } 810 811 // If this type is already in progress, it's a recursive type (e.g. map[string]*T). 812 // Return the pointer to the op we're already building. 813 if opPtr := inProgress[rt]; opPtr != nil { 814 return opPtr 815 } 816 typ := ut.base 817 var op decOp 818 k := typ.Kind() 819 if int(k) < len(decOpTable) { 820 op = decOpTable[k] 821 } 822 if op == nil { 823 inProgress[rt] = &op 824 // Special cases 825 switch t := typ; t.Kind() { 826 case reflect.Array: 827 name = "element of " + name 828 elemId := dec.wireType[wireId].ArrayT.Elem 829 elemOp := dec.decOpFor(elemId, t.Elem(), name, inProgress) 830 ovfl := overflow(name) 831 helper := decArrayHelper[t.Elem().Kind()] 832 op = func(i *decInstr, state *decoderState, value reflect.Value) { 833 state.dec.decodeArray(state, value, *elemOp, t.Len(), ovfl, helper) 834 } 835 836 case reflect.Map: 837 keyId := dec.wireType[wireId].MapT.Key 838 elemId := dec.wireType[wireId].MapT.Elem 839 keyOp := dec.decOpFor(keyId, t.Key(), "key of "+name, inProgress) 840 elemOp := dec.decOpFor(elemId, t.Elem(), "element of "+name, inProgress) 841 ovfl := overflow(name) 842 op = func(i *decInstr, state *decoderState, value reflect.Value) { 843 state.dec.decodeMap(t, state, value, *keyOp, *elemOp, ovfl) 844 } 845 846 case reflect.Slice: 847 name = "element of " + name 848 if t.Elem().Kind() == reflect.Uint8 { 849 op = decUint8Slice 850 break 851 } 852 var elemId typeId 853 if tt, ok := builtinIdToType[wireId]; ok { 854 elemId = tt.(*sliceType).Elem 855 } else { 856 elemId = dec.wireType[wireId].SliceT.Elem 857 } 858 elemOp := dec.decOpFor(elemId, t.Elem(), name, inProgress) 859 ovfl := overflow(name) 860 helper := decSliceHelper[t.Elem().Kind()] 861 op = func(i *decInstr, state *decoderState, value reflect.Value) { 862 state.dec.decodeSlice(state, value, *elemOp, ovfl, helper) 863 } 864 865 case reflect.Struct: 866 // Generate a closure that calls out to the engine for the nested type. 867 ut := userType(typ) 868 enginePtr, err := dec.getDecEnginePtr(wireId, ut) 869 if err != nil { 870 error_(err) 871 } 872 op = func(i *decInstr, state *decoderState, value reflect.Value) { 873 // indirect through enginePtr to delay evaluation for recursive structs. 874 dec.decodeStruct(*enginePtr, value) 875 } 876 case reflect.Interface: 877 op = func(i *decInstr, state *decoderState, value reflect.Value) { 878 state.dec.decodeInterface(t, state, value) 879 } 880 } 881 } 882 if op == nil { 883 errorf("decode can't handle type %s", rt) 884 } 885 return &op 886 } 887 888 // decIgnoreOpFor returns the decoding op for a field that has no destination. 889 func (dec *Decoder) decIgnoreOpFor(wireId typeId, inProgress map[typeId]*decOp) *decOp { 890 // If this type is already in progress, it's a recursive type (e.g. map[string]*T). 891 // Return the pointer to the op we're already building. 892 if opPtr := inProgress[wireId]; opPtr != nil { 893 return opPtr 894 } 895 op, ok := decIgnoreOpMap[wireId] 896 if !ok { 897 inProgress[wireId] = &op 898 if wireId == tInterface { 899 // Special case because it's a method: the ignored item might 900 // define types and we need to record their state in the decoder. 901 op = func(i *decInstr, state *decoderState, value reflect.Value) { 902 state.dec.ignoreInterface(state) 903 } 904 return &op 905 } 906 // Special cases 907 wire := dec.wireType[wireId] 908 switch { 909 case wire == nil: 910 errorf("bad data: undefined type %s", wireId.string()) 911 case wire.ArrayT != nil: 912 elemId := wire.ArrayT.Elem 913 elemOp := dec.decIgnoreOpFor(elemId, inProgress) 914 op = func(i *decInstr, state *decoderState, value reflect.Value) { 915 state.dec.ignoreArray(state, *elemOp, wire.ArrayT.Len) 916 } 917 918 case wire.MapT != nil: 919 keyId := dec.wireType[wireId].MapT.Key 920 elemId := dec.wireType[wireId].MapT.Elem 921 keyOp := dec.decIgnoreOpFor(keyId, inProgress) 922 elemOp := dec.decIgnoreOpFor(elemId, inProgress) 923 op = func(i *decInstr, state *decoderState, value reflect.Value) { 924 state.dec.ignoreMap(state, *keyOp, *elemOp) 925 } 926 927 case wire.SliceT != nil: 928 elemId := wire.SliceT.Elem 929 elemOp := dec.decIgnoreOpFor(elemId, inProgress) 930 op = func(i *decInstr, state *decoderState, value reflect.Value) { 931 state.dec.ignoreSlice(state, *elemOp) 932 } 933 934 case wire.StructT != nil: 935 // Generate a closure that calls out to the engine for the nested type. 936 enginePtr, err := dec.getIgnoreEnginePtr(wireId) 937 if err != nil { 938 error_(err) 939 } 940 op = func(i *decInstr, state *decoderState, value reflect.Value) { 941 // indirect through enginePtr to delay evaluation for recursive structs 942 state.dec.ignoreStruct(*enginePtr) 943 } 944 945 case wire.GobEncoderT != nil, wire.BinaryMarshalerT != nil, wire.TextMarshalerT != nil: 946 op = func(i *decInstr, state *decoderState, value reflect.Value) { 947 state.dec.ignoreGobDecoder(state) 948 } 949 } 950 } 951 if op == nil { 952 errorf("bad data: ignore can't handle type %s", wireId.string()) 953 } 954 return &op 955 } 956 957 // gobDecodeOpFor returns the op for a type that is known to implement 958 // GobDecoder. 959 func (dec *Decoder) gobDecodeOpFor(ut *userTypeInfo) *decOp { 960 rcvrType := ut.user 961 if ut.decIndir == -1 { 962 rcvrType = reflect.PtrTo(rcvrType) 963 } else if ut.decIndir > 0 { 964 for i := int8(0); i < ut.decIndir; i++ { 965 rcvrType = rcvrType.Elem() 966 } 967 } 968 var op decOp 969 op = func(i *decInstr, state *decoderState, value reflect.Value) { 970 // We now have the base type. We need its address if the receiver is a pointer. 971 if value.Kind() != reflect.Ptr && rcvrType.Kind() == reflect.Ptr { 972 value = value.Addr() 973 } 974 state.dec.decodeGobDecoder(ut, state, value) 975 } 976 return &op 977 } 978 979 // compatibleType asks: Are these two gob Types compatible? 980 // Answers the question for basic types, arrays, maps and slices, plus 981 // GobEncoder/Decoder pairs. 982 // Structs are considered ok; fields will be checked later. 983 func (dec *Decoder) compatibleType(fr reflect.Type, fw typeId, inProgress map[reflect.Type]typeId) bool { 984 if rhs, ok := inProgress[fr]; ok { 985 return rhs == fw 986 } 987 inProgress[fr] = fw 988 ut := userType(fr) 989 wire, ok := dec.wireType[fw] 990 // If wire was encoded with an encoding method, fr must have that method. 991 // And if not, it must not. 992 // At most one of the booleans in ut is set. 993 // We could possibly relax this constraint in the future in order to 994 // choose the decoding method using the data in the wireType. 995 // The parentheses look odd but are correct. 996 if (ut.externalDec == xGob) != (ok && wire.GobEncoderT != nil) || 997 (ut.externalDec == xBinary) != (ok && wire.BinaryMarshalerT != nil) || 998 (ut.externalDec == xText) != (ok && wire.TextMarshalerT != nil) { 999 return false 1000 } 1001 if ut.externalDec != 0 { // This test trumps all others. 1002 return true 1003 } 1004 switch t := ut.base; t.Kind() { 1005 default: 1006 // chan, etc: cannot handle. 1007 return false 1008 case reflect.Bool: 1009 return fw == tBool 1010 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: 1011 return fw == tInt 1012 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: 1013 return fw == tUint 1014 case reflect.Float32, reflect.Float64: 1015 return fw == tFloat 1016 case reflect.Complex64, reflect.Complex128: 1017 return fw == tComplex 1018 case reflect.String: 1019 return fw == tString 1020 case reflect.Interface: 1021 return fw == tInterface 1022 case reflect.Array: 1023 if !ok || wire.ArrayT == nil { 1024 return false 1025 } 1026 array := wire.ArrayT 1027 return t.Len() == array.Len && dec.compatibleType(t.Elem(), array.Elem, inProgress) 1028 case reflect.Map: 1029 if !ok || wire.MapT == nil { 1030 return false 1031 } 1032 MapType := wire.MapT 1033 return dec.compatibleType(t.Key(), MapType.Key, inProgress) && dec.compatibleType(t.Elem(), MapType.Elem, inProgress) 1034 case reflect.Slice: 1035 // Is it an array of bytes? 1036 if t.Elem().Kind() == reflect.Uint8 { 1037 return fw == tBytes 1038 } 1039 // Extract and compare element types. 1040 var sw *sliceType 1041 if tt, ok := builtinIdToType[fw]; ok { 1042 sw, _ = tt.(*sliceType) 1043 } else if wire != nil { 1044 sw = wire.SliceT 1045 } 1046 elem := userType(t.Elem()).base 1047 return sw != nil && dec.compatibleType(elem, sw.Elem, inProgress) 1048 case reflect.Struct: 1049 return true 1050 } 1051 } 1052 1053 // typeString returns a human-readable description of the type identified by remoteId. 1054 func (dec *Decoder) typeString(remoteId typeId) string { 1055 typeLock.Lock() 1056 defer typeLock.Unlock() 1057 if t := idToType[remoteId]; t != nil { 1058 // globally known type. 1059 return t.string() 1060 } 1061 return dec.wireType[remoteId].string() 1062 } 1063 1064 // compileSingle compiles the decoder engine for a non-struct top-level value, including 1065 // GobDecoders. 1066 func (dec *Decoder) compileSingle(remoteId typeId, ut *userTypeInfo) (engine *decEngine, err error) { 1067 rt := ut.user 1068 engine = new(decEngine) 1069 engine.instr = make([]decInstr, 1) // one item 1070 name := rt.String() // best we can do 1071 if !dec.compatibleType(rt, remoteId, make(map[reflect.Type]typeId)) { 1072 remoteType := dec.typeString(remoteId) 1073 // Common confusing case: local interface type, remote concrete type. 1074 if ut.base.Kind() == reflect.Interface && remoteId != tInterface { 1075 return nil, errors.New("gob: local interface type " + name + " can only be decoded from remote interface type; received concrete type " + remoteType) 1076 } 1077 return nil, errors.New("gob: decoding into local type " + name + ", received remote type " + remoteType) 1078 } 1079 op := dec.decOpFor(remoteId, rt, name, make(map[reflect.Type]*decOp)) 1080 ovfl := errors.New(`value for "` + name + `" out of range`) 1081 engine.instr[singletonField] = decInstr{*op, singletonField, nil, ovfl} 1082 engine.numInstr = 1 1083 return 1084 } 1085 1086 // compileIgnoreSingle compiles the decoder engine for a non-struct top-level value that will be discarded. 1087 func (dec *Decoder) compileIgnoreSingle(remoteId typeId) *decEngine { 1088 engine := new(decEngine) 1089 engine.instr = make([]decInstr, 1) // one item 1090 op := dec.decIgnoreOpFor(remoteId, make(map[typeId]*decOp)) 1091 ovfl := overflow(dec.typeString(remoteId)) 1092 engine.instr[0] = decInstr{*op, 0, nil, ovfl} 1093 engine.numInstr = 1 1094 return engine 1095 } 1096 1097 // compileDec compiles the decoder engine for a value. If the value is not a struct, 1098 // it calls out to compileSingle. 1099 func (dec *Decoder) compileDec(remoteId typeId, ut *userTypeInfo) (engine *decEngine, err error) { 1100 defer catchError(&err) 1101 rt := ut.base 1102 srt := rt 1103 if srt.Kind() != reflect.Struct || ut.externalDec != 0 { 1104 return dec.compileSingle(remoteId, ut) 1105 } 1106 var wireStruct *structType 1107 // Builtin types can come from global pool; the rest must be defined by the decoder. 1108 // Also we know we're decoding a struct now, so the client must have sent one. 1109 if t, ok := builtinIdToType[remoteId]; ok { 1110 wireStruct, _ = t.(*structType) 1111 } else { 1112 wire := dec.wireType[remoteId] 1113 if wire == nil { 1114 error_(errBadType) 1115 } 1116 wireStruct = wire.StructT 1117 } 1118 if wireStruct == nil { 1119 errorf("type mismatch in decoder: want struct type %s; got non-struct", rt) 1120 } 1121 engine = new(decEngine) 1122 engine.instr = make([]decInstr, len(wireStruct.Field)) 1123 seen := make(map[reflect.Type]*decOp) 1124 // Loop over the fields of the wire type. 1125 for fieldnum := 0; fieldnum < len(wireStruct.Field); fieldnum++ { 1126 wireField := wireStruct.Field[fieldnum] 1127 if wireField.Name == "" { 1128 errorf("empty name for remote field of type %s", wireStruct.Name) 1129 } 1130 ovfl := overflow(wireField.Name) 1131 // Find the field of the local type with the same name. 1132 localField, present := srt.FieldByName(wireField.Name) 1133 // TODO(r): anonymous names 1134 if !present || !isExported(wireField.Name) { 1135 op := dec.decIgnoreOpFor(wireField.Id, make(map[typeId]*decOp)) 1136 engine.instr[fieldnum] = decInstr{*op, fieldnum, nil, ovfl} 1137 continue 1138 } 1139 if !dec.compatibleType(localField.Type, wireField.Id, make(map[reflect.Type]typeId)) { 1140 errorf("wrong type (%s) for received field %s.%s", localField.Type, wireStruct.Name, wireField.Name) 1141 } 1142 op := dec.decOpFor(wireField.Id, localField.Type, localField.Name, seen) 1143 engine.instr[fieldnum] = decInstr{*op, fieldnum, localField.Index, ovfl} 1144 engine.numInstr++ 1145 } 1146 return 1147 } 1148 1149 // getDecEnginePtr returns the engine for the specified type. 1150 func (dec *Decoder) getDecEnginePtr(remoteId typeId, ut *userTypeInfo) (enginePtr **decEngine, err error) { 1151 rt := ut.user 1152 decoderMap, ok := dec.decoderCache[rt] 1153 if !ok { 1154 decoderMap = make(map[typeId]**decEngine) 1155 dec.decoderCache[rt] = decoderMap 1156 } 1157 if enginePtr, ok = decoderMap[remoteId]; !ok { 1158 // To handle recursive types, mark this engine as underway before compiling. 1159 enginePtr = new(*decEngine) 1160 decoderMap[remoteId] = enginePtr 1161 *enginePtr, err = dec.compileDec(remoteId, ut) 1162 if err != nil { 1163 delete(decoderMap, remoteId) 1164 } 1165 } 1166 return 1167 } 1168 1169 // emptyStruct is the type we compile into when ignoring a struct value. 1170 type emptyStruct struct{} 1171 1172 var emptyStructType = reflect.TypeOf(emptyStruct{}) 1173 1174 // getIgnoreEnginePtr returns the engine for the specified type when the value is to be discarded. 1175 func (dec *Decoder) getIgnoreEnginePtr(wireId typeId) (enginePtr **decEngine, err error) { 1176 var ok bool 1177 if enginePtr, ok = dec.ignorerCache[wireId]; !ok { 1178 // To handle recursive types, mark this engine as underway before compiling. 1179 enginePtr = new(*decEngine) 1180 dec.ignorerCache[wireId] = enginePtr 1181 wire := dec.wireType[wireId] 1182 if wire != nil && wire.StructT != nil { 1183 *enginePtr, err = dec.compileDec(wireId, userType(emptyStructType)) 1184 } else { 1185 *enginePtr = dec.compileIgnoreSingle(wireId) 1186 } 1187 if err != nil { 1188 delete(dec.ignorerCache, wireId) 1189 } 1190 } 1191 return 1192 } 1193 1194 // decodeValue decodes the data stream representing a value and stores it in value. 1195 func (dec *Decoder) decodeValue(wireId typeId, value reflect.Value) { 1196 defer catchError(&dec.err) 1197 // If the value is nil, it means we should just ignore this item. 1198 if !value.IsValid() { 1199 dec.decodeIgnoredValue(wireId) 1200 return 1201 } 1202 // Dereference down to the underlying type. 1203 ut := userType(value.Type()) 1204 base := ut.base 1205 var enginePtr **decEngine 1206 enginePtr, dec.err = dec.getDecEnginePtr(wireId, ut) 1207 if dec.err != nil { 1208 return 1209 } 1210 value = decAlloc(value) 1211 engine := *enginePtr 1212 if st := base; st.Kind() == reflect.Struct && ut.externalDec == 0 { 1213 wt := dec.wireType[wireId] 1214 if engine.numInstr == 0 && st.NumField() > 0 && 1215 wt != nil && len(wt.StructT.Field) > 0 { 1216 name := base.Name() 1217 errorf("type mismatch: no fields matched compiling decoder for %s", name) 1218 } 1219 dec.decodeStruct(engine, value) 1220 } else { 1221 dec.decodeSingle(engine, value) 1222 } 1223 } 1224 1225 // decodeIgnoredValue decodes the data stream representing a value of the specified type and discards it. 1226 func (dec *Decoder) decodeIgnoredValue(wireId typeId) { 1227 var enginePtr **decEngine 1228 enginePtr, dec.err = dec.getIgnoreEnginePtr(wireId) 1229 if dec.err != nil { 1230 return 1231 } 1232 wire := dec.wireType[wireId] 1233 if wire != nil && wire.StructT != nil { 1234 dec.ignoreStruct(*enginePtr) 1235 } else { 1236 dec.ignoreSingle(*enginePtr) 1237 } 1238 } 1239 1240 func init() { 1241 var iop, uop decOp 1242 switch reflect.TypeOf(int(0)).Bits() { 1243 case 32: 1244 iop = decInt32 1245 uop = decUint32 1246 case 64: 1247 iop = decInt64 1248 uop = decUint64 1249 default: 1250 panic("gob: unknown size of int/uint") 1251 } 1252 decOpTable[reflect.Int] = iop 1253 decOpTable[reflect.Uint] = uop 1254 1255 // Finally uintptr 1256 switch reflect.TypeOf(uintptr(0)).Bits() { 1257 case 32: 1258 uop = decUint32 1259 case 64: 1260 uop = decUint64 1261 default: 1262 panic("gob: unknown size of uintptr") 1263 } 1264 decOpTable[reflect.Uintptr] = uop 1265 } 1266 1267 // Gob depends on being able to take the address 1268 // of zeroed Values it creates, so use this wrapper instead 1269 // of the standard reflect.Zero. 1270 // Each call allocates once. 1271 func allocValue(t reflect.Type) reflect.Value { 1272 return reflect.New(t).Elem() 1273 }