rsc.io/go@v0.0.0-20150416155037-e040fd465409/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 elemOp(instr, state, noValue) 579 } 580 } 581 582 // ignoreArray discards the data for an array value with no destination. 583 func (dec *Decoder) ignoreArray(state *decoderState, elemOp decOp, length int) { 584 if n := state.decodeUint(); n != uint64(length) { 585 errorf("length mismatch in ignoreArray") 586 } 587 dec.ignoreArrayHelper(state, elemOp, length) 588 } 589 590 // ignoreMap discards the data for a map value with no destination. 591 func (dec *Decoder) ignoreMap(state *decoderState, keyOp, elemOp decOp) { 592 n := int(state.decodeUint()) 593 keyInstr := &decInstr{keyOp, 0, nil, errors.New("no error")} 594 elemInstr := &decInstr{elemOp, 0, nil, errors.New("no error")} 595 for i := 0; i < n; i++ { 596 keyOp(keyInstr, state, noValue) 597 elemOp(elemInstr, state, noValue) 598 } 599 } 600 601 // decodeSlice decodes a slice and stores it in value. 602 // Slices are encoded as an unsigned length followed by the elements. 603 func (dec *Decoder) decodeSlice(state *decoderState, value reflect.Value, elemOp decOp, ovfl error, helper decHelper) { 604 u := state.decodeUint() 605 typ := value.Type() 606 size := uint64(typ.Elem().Size()) 607 nBytes := u * size 608 n := int(u) 609 // Take care with overflow in this calculation. 610 if n < 0 || uint64(n) != u || nBytes > tooBig || (size > 0 && nBytes/size != u) { 611 // We don't check n against buffer length here because if it's a slice 612 // of interfaces, there will be buffer reloads. 613 errorf("%s slice too big: %d elements of %d bytes", typ.Elem(), u, size) 614 } 615 if value.Cap() < n { 616 value.Set(reflect.MakeSlice(typ, n, n)) 617 } else { 618 value.Set(value.Slice(0, n)) 619 } 620 dec.decodeArrayHelper(state, value, elemOp, n, ovfl, helper) 621 } 622 623 // ignoreSlice skips over the data for a slice value with no destination. 624 func (dec *Decoder) ignoreSlice(state *decoderState, elemOp decOp) { 625 dec.ignoreArrayHelper(state, elemOp, int(state.decodeUint())) 626 } 627 628 // decodeInterface decodes an interface value and stores it in value. 629 // Interfaces are encoded as the name of a concrete type followed by a value. 630 // If the name is empty, the value is nil and no value is sent. 631 func (dec *Decoder) decodeInterface(ityp reflect.Type, state *decoderState, value reflect.Value) { 632 // Read the name of the concrete type. 633 nr := state.decodeUint() 634 if nr < 0 || nr > 1<<31 { // zero is permissible for anonymous types 635 errorf("invalid type name length %d", nr) 636 } 637 if nr > uint64(state.b.Len()) { 638 errorf("invalid type name length %d: exceeds input size", nr) 639 } 640 b := make([]byte, nr) 641 state.b.Read(b) 642 name := string(b) 643 // Allocate the destination interface value. 644 if name == "" { 645 // Copy the nil interface value to the target. 646 value.Set(reflect.Zero(value.Type())) 647 return 648 } 649 if len(name) > 1024 { 650 errorf("name too long (%d bytes): %.20q...", len(name), name) 651 } 652 // The concrete type must be registered. 653 registerLock.RLock() 654 typ, ok := nameToConcreteType[name] 655 registerLock.RUnlock() 656 if !ok { 657 errorf("name not registered for interface: %q", name) 658 } 659 // Read the type id of the concrete value. 660 concreteId := dec.decodeTypeSequence(true) 661 if concreteId < 0 { 662 error_(dec.err) 663 } 664 // Byte count of value is next; we don't care what it is (it's there 665 // in case we want to ignore the value by skipping it completely). 666 state.decodeUint() 667 // Read the concrete value. 668 v := allocValue(typ) 669 dec.decodeValue(concreteId, v) 670 if dec.err != nil { 671 error_(dec.err) 672 } 673 // Assign the concrete value to the interface. 674 // Tread carefully; it might not satisfy the interface. 675 if !typ.AssignableTo(ityp) { 676 errorf("%s is not assignable to type %s", typ, ityp) 677 } 678 // Copy the interface value to the target. 679 value.Set(v) 680 } 681 682 // ignoreInterface discards the data for an interface value with no destination. 683 func (dec *Decoder) ignoreInterface(state *decoderState) { 684 // Read the name of the concrete type. 685 n, ok := state.getLength() 686 if !ok { 687 errorf("bad interface encoding: name too large for buffer") 688 } 689 b := make([]byte, n) 690 _, err := state.b.Read(b) 691 if err != nil { 692 error_(err) 693 } 694 id := dec.decodeTypeSequence(true) 695 if id < 0 { 696 error_(dec.err) 697 } 698 // At this point, the decoder buffer contains a delimited value. Just toss it. 699 n, ok = state.getLength() 700 if !ok { 701 errorf("bad interface encoding: data length too large for buffer") 702 } 703 state.b.Drop(n) 704 } 705 706 // decodeGobDecoder decodes something implementing the GobDecoder interface. 707 // The data is encoded as a byte slice. 708 func (dec *Decoder) decodeGobDecoder(ut *userTypeInfo, state *decoderState, value reflect.Value) { 709 // Read the bytes for the value. 710 n, ok := state.getLength() 711 if !ok { 712 errorf("GobDecoder: length too large for buffer") 713 } 714 b := make([]byte, n) 715 _, err := state.b.Read(b) 716 if err != nil { 717 error_(err) 718 } 719 // We know it's one of these. 720 switch ut.externalDec { 721 case xGob: 722 err = value.Interface().(GobDecoder).GobDecode(b) 723 case xBinary: 724 err = value.Interface().(encoding.BinaryUnmarshaler).UnmarshalBinary(b) 725 case xText: 726 err = value.Interface().(encoding.TextUnmarshaler).UnmarshalText(b) 727 } 728 if err != nil { 729 error_(err) 730 } 731 } 732 733 // ignoreGobDecoder discards the data for a GobDecoder value with no destination. 734 func (dec *Decoder) ignoreGobDecoder(state *decoderState) { 735 // Read the bytes for the value. 736 n, ok := state.getLength() 737 if !ok { 738 errorf("GobDecoder: length too large for buffer") 739 } 740 b := make([]byte, n) 741 _, err := state.b.Read(b) 742 if err != nil { 743 error_(err) 744 } 745 } 746 747 // Index by Go types. 748 var decOpTable = [...]decOp{ 749 reflect.Bool: decBool, 750 reflect.Int8: decInt8, 751 reflect.Int16: decInt16, 752 reflect.Int32: decInt32, 753 reflect.Int64: decInt64, 754 reflect.Uint8: decUint8, 755 reflect.Uint16: decUint16, 756 reflect.Uint32: decUint32, 757 reflect.Uint64: decUint64, 758 reflect.Float32: decFloat32, 759 reflect.Float64: decFloat64, 760 reflect.Complex64: decComplex64, 761 reflect.Complex128: decComplex128, 762 reflect.String: decString, 763 } 764 765 // Indexed by gob types. tComplex will be added during type.init(). 766 var decIgnoreOpMap = map[typeId]decOp{ 767 tBool: ignoreUint, 768 tInt: ignoreUint, 769 tUint: ignoreUint, 770 tFloat: ignoreUint, 771 tBytes: ignoreUint8Array, 772 tString: ignoreUint8Array, 773 tComplex: ignoreTwoUints, 774 } 775 776 // decOpFor returns the decoding op for the base type under rt and 777 // the indirection count to reach it. 778 func (dec *Decoder) decOpFor(wireId typeId, rt reflect.Type, name string, inProgress map[reflect.Type]*decOp) *decOp { 779 ut := userType(rt) 780 // If the type implements GobEncoder, we handle it without further processing. 781 if ut.externalDec != 0 { 782 return dec.gobDecodeOpFor(ut) 783 } 784 785 // If this type is already in progress, it's a recursive type (e.g. map[string]*T). 786 // Return the pointer to the op we're already building. 787 if opPtr := inProgress[rt]; opPtr != nil { 788 return opPtr 789 } 790 typ := ut.base 791 var op decOp 792 k := typ.Kind() 793 if int(k) < len(decOpTable) { 794 op = decOpTable[k] 795 } 796 if op == nil { 797 inProgress[rt] = &op 798 // Special cases 799 switch t := typ; t.Kind() { 800 case reflect.Array: 801 name = "element of " + name 802 elemId := dec.wireType[wireId].ArrayT.Elem 803 elemOp := dec.decOpFor(elemId, t.Elem(), name, inProgress) 804 ovfl := overflow(name) 805 helper := decArrayHelper[t.Elem().Kind()] 806 op = func(i *decInstr, state *decoderState, value reflect.Value) { 807 state.dec.decodeArray(t, state, value, *elemOp, t.Len(), ovfl, helper) 808 } 809 810 case reflect.Map: 811 keyId := dec.wireType[wireId].MapT.Key 812 elemId := dec.wireType[wireId].MapT.Elem 813 keyOp := dec.decOpFor(keyId, t.Key(), "key of "+name, inProgress) 814 elemOp := dec.decOpFor(elemId, t.Elem(), "element of "+name, inProgress) 815 ovfl := overflow(name) 816 op = func(i *decInstr, state *decoderState, value reflect.Value) { 817 state.dec.decodeMap(t, state, value, *keyOp, *elemOp, ovfl) 818 } 819 820 case reflect.Slice: 821 name = "element of " + name 822 if t.Elem().Kind() == reflect.Uint8 { 823 op = decUint8Slice 824 break 825 } 826 var elemId typeId 827 if tt, ok := builtinIdToType[wireId]; ok { 828 elemId = tt.(*sliceType).Elem 829 } else { 830 elemId = dec.wireType[wireId].SliceT.Elem 831 } 832 elemOp := dec.decOpFor(elemId, t.Elem(), name, inProgress) 833 ovfl := overflow(name) 834 helper := decSliceHelper[t.Elem().Kind()] 835 op = func(i *decInstr, state *decoderState, value reflect.Value) { 836 state.dec.decodeSlice(state, value, *elemOp, ovfl, helper) 837 } 838 839 case reflect.Struct: 840 // Generate a closure that calls out to the engine for the nested type. 841 ut := userType(typ) 842 enginePtr, err := dec.getDecEnginePtr(wireId, ut) 843 if err != nil { 844 error_(err) 845 } 846 op = func(i *decInstr, state *decoderState, value reflect.Value) { 847 // indirect through enginePtr to delay evaluation for recursive structs. 848 dec.decodeStruct(*enginePtr, ut, value) 849 } 850 case reflect.Interface: 851 op = func(i *decInstr, state *decoderState, value reflect.Value) { 852 state.dec.decodeInterface(t, state, value) 853 } 854 } 855 } 856 if op == nil { 857 errorf("decode can't handle type %s", rt) 858 } 859 return &op 860 } 861 862 // decIgnoreOpFor returns the decoding op for a field that has no destination. 863 func (dec *Decoder) decIgnoreOpFor(wireId typeId) decOp { 864 op, ok := decIgnoreOpMap[wireId] 865 if !ok { 866 if wireId == tInterface { 867 // Special case because it's a method: the ignored item might 868 // define types and we need to record their state in the decoder. 869 op = func(i *decInstr, state *decoderState, value reflect.Value) { 870 state.dec.ignoreInterface(state) 871 } 872 return op 873 } 874 // Special cases 875 wire := dec.wireType[wireId] 876 switch { 877 case wire == nil: 878 errorf("bad data: undefined type %s", wireId.string()) 879 case wire.ArrayT != nil: 880 elemId := wire.ArrayT.Elem 881 elemOp := dec.decIgnoreOpFor(elemId) 882 op = func(i *decInstr, state *decoderState, value reflect.Value) { 883 state.dec.ignoreArray(state, elemOp, wire.ArrayT.Len) 884 } 885 886 case wire.MapT != nil: 887 keyId := dec.wireType[wireId].MapT.Key 888 elemId := dec.wireType[wireId].MapT.Elem 889 keyOp := dec.decIgnoreOpFor(keyId) 890 elemOp := dec.decIgnoreOpFor(elemId) 891 op = func(i *decInstr, state *decoderState, value reflect.Value) { 892 state.dec.ignoreMap(state, keyOp, elemOp) 893 } 894 895 case wire.SliceT != nil: 896 elemId := wire.SliceT.Elem 897 elemOp := dec.decIgnoreOpFor(elemId) 898 op = func(i *decInstr, state *decoderState, value reflect.Value) { 899 state.dec.ignoreSlice(state, elemOp) 900 } 901 902 case wire.StructT != nil: 903 // Generate a closure that calls out to the engine for the nested type. 904 enginePtr, err := dec.getIgnoreEnginePtr(wireId) 905 if err != nil { 906 error_(err) 907 } 908 op = func(i *decInstr, state *decoderState, value reflect.Value) { 909 // indirect through enginePtr to delay evaluation for recursive structs 910 state.dec.ignoreStruct(*enginePtr) 911 } 912 913 case wire.GobEncoderT != nil, wire.BinaryMarshalerT != nil, wire.TextMarshalerT != nil: 914 op = func(i *decInstr, state *decoderState, value reflect.Value) { 915 state.dec.ignoreGobDecoder(state) 916 } 917 } 918 } 919 if op == nil { 920 errorf("bad data: ignore can't handle type %s", wireId.string()) 921 } 922 return op 923 } 924 925 // gobDecodeOpFor returns the op for a type that is known to implement 926 // GobDecoder. 927 func (dec *Decoder) gobDecodeOpFor(ut *userTypeInfo) *decOp { 928 rcvrType := ut.user 929 if ut.decIndir == -1 { 930 rcvrType = reflect.PtrTo(rcvrType) 931 } else if ut.decIndir > 0 { 932 for i := int8(0); i < ut.decIndir; i++ { 933 rcvrType = rcvrType.Elem() 934 } 935 } 936 var op decOp 937 op = func(i *decInstr, state *decoderState, value reflect.Value) { 938 // We now have the base type. We need its address if the receiver is a pointer. 939 if value.Kind() != reflect.Ptr && rcvrType.Kind() == reflect.Ptr { 940 value = value.Addr() 941 } 942 state.dec.decodeGobDecoder(ut, state, value) 943 } 944 return &op 945 } 946 947 // compatibleType asks: Are these two gob Types compatible? 948 // Answers the question for basic types, arrays, maps and slices, plus 949 // GobEncoder/Decoder pairs. 950 // Structs are considered ok; fields will be checked later. 951 func (dec *Decoder) compatibleType(fr reflect.Type, fw typeId, inProgress map[reflect.Type]typeId) bool { 952 if rhs, ok := inProgress[fr]; ok { 953 return rhs == fw 954 } 955 inProgress[fr] = fw 956 ut := userType(fr) 957 wire, ok := dec.wireType[fw] 958 // If wire was encoded with an encoding method, fr must have that method. 959 // And if not, it must not. 960 // At most one of the booleans in ut is set. 961 // We could possibly relax this constraint in the future in order to 962 // choose the decoding method using the data in the wireType. 963 // The parentheses look odd but are correct. 964 if (ut.externalDec == xGob) != (ok && wire.GobEncoderT != nil) || 965 (ut.externalDec == xBinary) != (ok && wire.BinaryMarshalerT != nil) || 966 (ut.externalDec == xText) != (ok && wire.TextMarshalerT != nil) { 967 return false 968 } 969 if ut.externalDec != 0 { // This test trumps all others. 970 return true 971 } 972 switch t := ut.base; t.Kind() { 973 default: 974 // chan, etc: cannot handle. 975 return false 976 case reflect.Bool: 977 return fw == tBool 978 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: 979 return fw == tInt 980 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: 981 return fw == tUint 982 case reflect.Float32, reflect.Float64: 983 return fw == tFloat 984 case reflect.Complex64, reflect.Complex128: 985 return fw == tComplex 986 case reflect.String: 987 return fw == tString 988 case reflect.Interface: 989 return fw == tInterface 990 case reflect.Array: 991 if !ok || wire.ArrayT == nil { 992 return false 993 } 994 array := wire.ArrayT 995 return t.Len() == array.Len && dec.compatibleType(t.Elem(), array.Elem, inProgress) 996 case reflect.Map: 997 if !ok || wire.MapT == nil { 998 return false 999 } 1000 MapType := wire.MapT 1001 return dec.compatibleType(t.Key(), MapType.Key, inProgress) && dec.compatibleType(t.Elem(), MapType.Elem, inProgress) 1002 case reflect.Slice: 1003 // Is it an array of bytes? 1004 if t.Elem().Kind() == reflect.Uint8 { 1005 return fw == tBytes 1006 } 1007 // Extract and compare element types. 1008 var sw *sliceType 1009 if tt, ok := builtinIdToType[fw]; ok { 1010 sw, _ = tt.(*sliceType) 1011 } else if wire != nil { 1012 sw = wire.SliceT 1013 } 1014 elem := userType(t.Elem()).base 1015 return sw != nil && dec.compatibleType(elem, sw.Elem, inProgress) 1016 case reflect.Struct: 1017 return true 1018 } 1019 } 1020 1021 // typeString returns a human-readable description of the type identified by remoteId. 1022 func (dec *Decoder) typeString(remoteId typeId) string { 1023 if t := idToType[remoteId]; t != nil { 1024 // globally known type. 1025 return t.string() 1026 } 1027 return dec.wireType[remoteId].string() 1028 } 1029 1030 // compileSingle compiles the decoder engine for a non-struct top-level value, including 1031 // GobDecoders. 1032 func (dec *Decoder) compileSingle(remoteId typeId, ut *userTypeInfo) (engine *decEngine, err error) { 1033 rt := ut.user 1034 engine = new(decEngine) 1035 engine.instr = make([]decInstr, 1) // one item 1036 name := rt.String() // best we can do 1037 if !dec.compatibleType(rt, remoteId, make(map[reflect.Type]typeId)) { 1038 remoteType := dec.typeString(remoteId) 1039 // Common confusing case: local interface type, remote concrete type. 1040 if ut.base.Kind() == reflect.Interface && remoteId != tInterface { 1041 return nil, errors.New("gob: local interface type " + name + " can only be decoded from remote interface type; received concrete type " + remoteType) 1042 } 1043 return nil, errors.New("gob: decoding into local type " + name + ", received remote type " + remoteType) 1044 } 1045 op := dec.decOpFor(remoteId, rt, name, make(map[reflect.Type]*decOp)) 1046 ovfl := errors.New(`value for "` + name + `" out of range`) 1047 engine.instr[singletonField] = decInstr{*op, singletonField, nil, ovfl} 1048 engine.numInstr = 1 1049 return 1050 } 1051 1052 // compileIgnoreSingle compiles the decoder engine for a non-struct top-level value that will be discarded. 1053 func (dec *Decoder) compileIgnoreSingle(remoteId typeId) (engine *decEngine, err error) { 1054 engine = new(decEngine) 1055 engine.instr = make([]decInstr, 1) // one item 1056 op := dec.decIgnoreOpFor(remoteId) 1057 ovfl := overflow(dec.typeString(remoteId)) 1058 engine.instr[0] = decInstr{op, 0, nil, ovfl} 1059 engine.numInstr = 1 1060 return 1061 } 1062 1063 // compileDec compiles the decoder engine for a value. If the value is not a struct, 1064 // it calls out to compileSingle. 1065 func (dec *Decoder) compileDec(remoteId typeId, ut *userTypeInfo) (engine *decEngine, err error) { 1066 defer catchError(&err) 1067 rt := ut.base 1068 srt := rt 1069 if srt.Kind() != reflect.Struct || ut.externalDec != 0 { 1070 return dec.compileSingle(remoteId, ut) 1071 } 1072 var wireStruct *structType 1073 // Builtin types can come from global pool; the rest must be defined by the decoder. 1074 // Also we know we're decoding a struct now, so the client must have sent one. 1075 if t, ok := builtinIdToType[remoteId]; ok { 1076 wireStruct, _ = t.(*structType) 1077 } else { 1078 wire := dec.wireType[remoteId] 1079 if wire == nil { 1080 error_(errBadType) 1081 } 1082 wireStruct = wire.StructT 1083 } 1084 if wireStruct == nil { 1085 errorf("type mismatch in decoder: want struct type %s; got non-struct", rt) 1086 } 1087 engine = new(decEngine) 1088 engine.instr = make([]decInstr, len(wireStruct.Field)) 1089 seen := make(map[reflect.Type]*decOp) 1090 // Loop over the fields of the wire type. 1091 for fieldnum := 0; fieldnum < len(wireStruct.Field); fieldnum++ { 1092 wireField := wireStruct.Field[fieldnum] 1093 if wireField.Name == "" { 1094 errorf("empty name for remote field of type %s", wireStruct.Name) 1095 } 1096 ovfl := overflow(wireField.Name) 1097 // Find the field of the local type with the same name. 1098 localField, present := srt.FieldByName(wireField.Name) 1099 // TODO(r): anonymous names 1100 if !present || !isExported(wireField.Name) { 1101 op := dec.decIgnoreOpFor(wireField.Id) 1102 engine.instr[fieldnum] = decInstr{op, fieldnum, nil, ovfl} 1103 continue 1104 } 1105 if !dec.compatibleType(localField.Type, wireField.Id, make(map[reflect.Type]typeId)) { 1106 errorf("wrong type (%s) for received field %s.%s", localField.Type, wireStruct.Name, wireField.Name) 1107 } 1108 op := dec.decOpFor(wireField.Id, localField.Type, localField.Name, seen) 1109 engine.instr[fieldnum] = decInstr{*op, fieldnum, localField.Index, ovfl} 1110 engine.numInstr++ 1111 } 1112 return 1113 } 1114 1115 // getDecEnginePtr returns the engine for the specified type. 1116 func (dec *Decoder) getDecEnginePtr(remoteId typeId, ut *userTypeInfo) (enginePtr **decEngine, err error) { 1117 rt := ut.user 1118 decoderMap, ok := dec.decoderCache[rt] 1119 if !ok { 1120 decoderMap = make(map[typeId]**decEngine) 1121 dec.decoderCache[rt] = decoderMap 1122 } 1123 if enginePtr, ok = decoderMap[remoteId]; !ok { 1124 // To handle recursive types, mark this engine as underway before compiling. 1125 enginePtr = new(*decEngine) 1126 decoderMap[remoteId] = enginePtr 1127 *enginePtr, err = dec.compileDec(remoteId, ut) 1128 if err != nil { 1129 delete(decoderMap, remoteId) 1130 } 1131 } 1132 return 1133 } 1134 1135 // emptyStruct is the type we compile into when ignoring a struct value. 1136 type emptyStruct struct{} 1137 1138 var emptyStructType = reflect.TypeOf(emptyStruct{}) 1139 1140 // getIgnoreEnginePtr returns the engine for the specified type when the value is to be discarded. 1141 func (dec *Decoder) getIgnoreEnginePtr(wireId typeId) (enginePtr **decEngine, err error) { 1142 var ok bool 1143 if enginePtr, ok = dec.ignorerCache[wireId]; !ok { 1144 // To handle recursive types, mark this engine as underway before compiling. 1145 enginePtr = new(*decEngine) 1146 dec.ignorerCache[wireId] = enginePtr 1147 wire := dec.wireType[wireId] 1148 if wire != nil && wire.StructT != nil { 1149 *enginePtr, err = dec.compileDec(wireId, userType(emptyStructType)) 1150 } else { 1151 *enginePtr, err = dec.compileIgnoreSingle(wireId) 1152 } 1153 if err != nil { 1154 delete(dec.ignorerCache, wireId) 1155 } 1156 } 1157 return 1158 } 1159 1160 // decodeValue decodes the data stream representing a value and stores it in value. 1161 func (dec *Decoder) decodeValue(wireId typeId, value reflect.Value) { 1162 defer catchError(&dec.err) 1163 // If the value is nil, it means we should just ignore this item. 1164 if !value.IsValid() { 1165 dec.decodeIgnoredValue(wireId) 1166 return 1167 } 1168 // Dereference down to the underlying type. 1169 ut := userType(value.Type()) 1170 base := ut.base 1171 var enginePtr **decEngine 1172 enginePtr, dec.err = dec.getDecEnginePtr(wireId, ut) 1173 if dec.err != nil { 1174 return 1175 } 1176 value = decAlloc(value) 1177 engine := *enginePtr 1178 if st := base; st.Kind() == reflect.Struct && ut.externalDec == 0 { 1179 wt := dec.wireType[wireId] 1180 if engine.numInstr == 0 && st.NumField() > 0 && 1181 wt != nil && len(wt.StructT.Field) > 0 { 1182 name := base.Name() 1183 errorf("type mismatch: no fields matched compiling decoder for %s", name) 1184 } 1185 dec.decodeStruct(engine, ut, value) 1186 } else { 1187 dec.decodeSingle(engine, ut, value) 1188 } 1189 } 1190 1191 // decodeIgnoredValue decodes the data stream representing a value of the specified type and discards it. 1192 func (dec *Decoder) decodeIgnoredValue(wireId typeId) { 1193 var enginePtr **decEngine 1194 enginePtr, dec.err = dec.getIgnoreEnginePtr(wireId) 1195 if dec.err != nil { 1196 return 1197 } 1198 wire := dec.wireType[wireId] 1199 if wire != nil && wire.StructT != nil { 1200 dec.ignoreStruct(*enginePtr) 1201 } else { 1202 dec.ignoreSingle(*enginePtr) 1203 } 1204 } 1205 1206 func init() { 1207 var iop, uop decOp 1208 switch reflect.TypeOf(int(0)).Bits() { 1209 case 32: 1210 iop = decInt32 1211 uop = decUint32 1212 case 64: 1213 iop = decInt64 1214 uop = decUint64 1215 default: 1216 panic("gob: unknown size of int/uint") 1217 } 1218 decOpTable[reflect.Int] = iop 1219 decOpTable[reflect.Uint] = uop 1220 1221 // Finally uintptr 1222 switch reflect.TypeOf(uintptr(0)).Bits() { 1223 case 32: 1224 uop = decUint32 1225 case 64: 1226 uop = decUint64 1227 default: 1228 panic("gob: unknown size of uintptr") 1229 } 1230 decOpTable[reflect.Uintptr] = uop 1231 } 1232 1233 // Gob depends on being able to take the address 1234 // of zeroed Values it creates, so use this wrapper instead 1235 // of the standard reflect.Zero. 1236 // Each call allocates once. 1237 func allocValue(t reflect.Type) reflect.Value { 1238 return reflect.New(t).Elem() 1239 }