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